Compare commits
10 Commits
160dbcd50f
...
da9c5ff82a
Author | SHA1 | Date | |
---|---|---|---|
da9c5ff82a | |||
|
750501c907 | ||
|
286b508f10 | ||
|
ddeede0ebe | ||
|
410cc2b6c6 | ||
|
18a23222a9 | ||
|
29a19692fe | ||
|
318b385ce4 | ||
|
b5bd8477e4 | ||
|
b82eff1c72 |
@ -1,5 +1,9 @@
|
|||||||
cmake_minimum_required(VERSION 3.6)
|
cmake_minimum_required(VERSION 3.6)
|
||||||
project(magidoor)
|
project(magidoor)
|
||||||
|
|
||||||
set(SOURCE_FILES MD_Sendfile.c MD_Getc.c MD_Printf.c MD_Init.c MagiDoor.h)
|
if (WIN32)
|
||||||
|
set(SOURCE_FILES MD_Sendfile.c MD_Getc.c MD_Printf.c MD_Init.c MD_AnsiCons.c MD_AnsiCons.h MagiDoor.h)
|
||||||
|
else()
|
||||||
|
set(SOURCE_FILES MD_Sendfile.c MD_Getc.c MD_Printf.c MD_Init.c MagiDoor.h)
|
||||||
|
endif()
|
||||||
add_library(mdoor STATIC ${SOURCE_FILES})
|
add_library(mdoor STATIC ${SOURCE_FILES})
|
@ -1,4 +1,4 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
extern int ansi_write(FILE* fp, const char* buf, int len);
|
extern int ansi_write(FILE* fp, const char* buf, int len);
|
||||||
|
102
MD_Getc.c
102
MD_Getc.c
@ -2,34 +2,59 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#if defined(_MSC_VER) || defined(WIN32)
|
#if defined(_MSC_VER) || defined(WIN32)
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
|
#define STDIN_FILENO 0
|
||||||
|
#define STDOUT_FILENO 1
|
||||||
#else
|
#else
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#endif
|
#include <sys/select.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "MagiDoor.h"
|
#include "MagiDoor.h"
|
||||||
|
|
||||||
|
#define IAC 255
|
||||||
|
|
||||||
extern time_t mdtimeout;
|
extern time_t mdtimeout;
|
||||||
extern time_t mdtimeremaining;
|
extern time_t mdtimeremaining;
|
||||||
|
|
||||||
char md_getc() {
|
char md_getc() {
|
||||||
char c;
|
char c;
|
||||||
ssize_t ret;
|
do {
|
||||||
|
c = md_getche(60, 0);
|
||||||
|
} while(c == -1);
|
||||||
|
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
char md_getche(uint32_t sec, uint32_t usec) {
|
||||||
|
char c = 'x';
|
||||||
|
int ret;
|
||||||
|
int rs;
|
||||||
|
|
||||||
|
int stage = 0;
|
||||||
|
|
||||||
|
struct timeval tv;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
fd_set rfd;
|
||||||
|
FD_ZERO(&rfd);
|
||||||
if (mdcontrol.socket == -1) {
|
if (mdcontrol.socket == -1) {
|
||||||
ret = read(STDIN_FILENO, &c, 1);
|
FD_SET(STDIN_FILENO, &rfd);
|
||||||
} else {
|
} else {
|
||||||
ret = recv(mdcontrol.socket, &c, 1, 0);
|
FD_SET(mdcontrol.socket, &rfd);
|
||||||
}
|
}
|
||||||
if (ret == 0) {
|
|
||||||
md_exit(0);
|
tv.tv_sec = sec;
|
||||||
|
tv.tv_usec = usec;
|
||||||
|
|
||||||
|
if (mdcontrol.socket == -1) {
|
||||||
|
rs = select(STDIN_FILENO + 1, &rfd, NULL, NULL, &tv);
|
||||||
|
} else {
|
||||||
|
rs = select(mdcontrol.socket + 1, &rfd, NULL, NULL, &tv);
|
||||||
}
|
}
|
||||||
#if defined(_MSC_VER) || defined(WIN32)
|
|
||||||
if (ret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK) {
|
if (rs == 0) {
|
||||||
#else
|
|
||||||
if (ret == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
|
|
||||||
#endif
|
|
||||||
if (mdtimeout <= time(NULL)) {
|
if (mdtimeout <= time(NULL)) {
|
||||||
md_printf("\r\nIdle timeout!\r\n");
|
md_printf("\r\nIdle timeout!\r\n");
|
||||||
md_exit(0);
|
md_exit(0);
|
||||||
@ -38,15 +63,54 @@ char md_getc() {
|
|||||||
md_printf("\r\nOut of time!\r\n");
|
md_printf("\r\nOut of time!\r\n");
|
||||||
md_exit(0);
|
md_exit(0);
|
||||||
}
|
}
|
||||||
usleep(100);
|
return -1;
|
||||||
continue;
|
} else if (rs == -1 && errno != EINTR) {
|
||||||
|
md_exit(0);
|
||||||
|
} else if (mdcontrol.socket != -1 && FD_ISSET(mdcontrol.socket, &rfd)) {
|
||||||
|
ret = recv(mdcontrol.socket, &c, 1, 0);
|
||||||
|
if (ret <= 0) {
|
||||||
|
md_exit(0);
|
||||||
|
}
|
||||||
|
if ((unsigned char)c == IAC && stage == 0) {
|
||||||
|
stage = 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (stage == 1) {
|
||||||
|
if ((unsigned char)c == IAC) {
|
||||||
|
stage = 0;
|
||||||
|
} else if ((unsigned char)c == 250) {
|
||||||
|
stage = 3;
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
stage = 2;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (stage == 2) {
|
||||||
|
stage = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (stage == 3) {
|
||||||
|
if ((unsigned char)c == 240) {
|
||||||
|
stage = 0;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
mdtimeout = time(NULL) + 900;
|
||||||
|
return c;
|
||||||
|
} else if (mdcontrol.socket == -1 && FD_ISSET(STDIN_FILENO, &rfd)) {
|
||||||
|
ret = read(STDIN_FILENO, &c, 1);
|
||||||
|
if (ret <= 0) {
|
||||||
|
md_exit(0);
|
||||||
|
}
|
||||||
|
mdtimeout = time(NULL) + 900;
|
||||||
|
return c;
|
||||||
}
|
}
|
||||||
mdtimeout = time(NULL) + 900;
|
|
||||||
return c;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char md_get_answer(char *options) {
|
char md_get_answer(const char *options) {
|
||||||
char c;
|
char c;
|
||||||
c = md_getc();
|
c = md_getc();
|
||||||
while (strchr(options, c) == NULL) {
|
while (strchr(options, c) == NULL) {
|
||||||
@ -60,6 +124,8 @@ int md_getstring(char *ptr, int maxlen, char minchar, char maxchar) {
|
|||||||
int len = 0;
|
int len = 0;
|
||||||
static char lastc = 'x';
|
static char lastc = 'x';
|
||||||
|
|
||||||
|
*ptr = '\0';
|
||||||
|
|
||||||
while (len < maxlen) {
|
while (len < maxlen) {
|
||||||
c = md_getc();
|
c = md_getc();
|
||||||
if (c == '\n' || c == '\0') {
|
if (c == '\n' || c == '\0') {
|
||||||
@ -76,7 +142,7 @@ int md_getstring(char *ptr, int maxlen, char minchar, char maxchar) {
|
|||||||
}
|
}
|
||||||
if (c == '\b' || c == 127) {
|
if (c == '\b' || c == 127) {
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
md_printf("\x1b[D \x1b[D");
|
md_printf("\b \b");
|
||||||
len--;
|
len--;
|
||||||
ptr[len] = '\0';
|
ptr[len] = '\0';
|
||||||
}
|
}
|
||||||
@ -92,4 +158,4 @@ int md_getstring(char *ptr, int maxlen, char minchar, char maxchar) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
16
MD_Init.c
16
MD_Init.c
@ -1,12 +1,14 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#if defined(WIN32) || defined(_MSC_VER)
|
#if defined(WIN32) || defined(_MSC_VER)
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
|
#define strcasecmp stricmp
|
||||||
#else
|
#else
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include "MagiDoor.h"
|
#include "MagiDoor.h"
|
||||||
|
|
||||||
@ -172,6 +174,16 @@ int read_doorsys(const char *dropfile) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined(WIN32) && !defined(_MSC_VER)
|
||||||
|
|
||||||
|
void md_sighuphandler(int i)
|
||||||
|
{
|
||||||
|
md_exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
void md_exit(int exitcode) {
|
void md_exit(int exitcode) {
|
||||||
if (mdcontrol.socket == -1) {
|
if (mdcontrol.socket == -1) {
|
||||||
#if !defined(WIN32) && !defined(_MSC_VER)
|
#if !defined(WIN32) && !defined(_MSC_VER)
|
||||||
@ -187,6 +199,8 @@ void md_init(const char *dropfile, int socket) {
|
|||||||
char *filename = strrchr(dropfile, PATH_SEP);
|
char *filename = strrchr(dropfile, PATH_SEP);
|
||||||
#if !defined(WIN32) && !defined(_MSC_VER)
|
#if !defined(WIN32) && !defined(_MSC_VER)
|
||||||
struct termios ttystate;
|
struct termios ttystate;
|
||||||
|
signal(SIGHUP, &md_sighuphandler);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
|
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
|
||||||
#if defined(_MSC_VER) || defined(WIN32)
|
#if defined(_MSC_VER) || defined(WIN32)
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include "MD_AnsiCons.h"
|
#include "MD_AnsiCons.h"
|
||||||
|
#define STDOUT_FILENO 1
|
||||||
#else
|
#else
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
#include "MagiDoor.h"
|
#include "MagiDoor.h"
|
||||||
|
|
||||||
|
19
MagiDoor.h
19
MagiDoor.h
@ -1,6 +1,11 @@
|
|||||||
#ifndef __MAGIDOOR_H__
|
#ifndef __MAGIDOOR_H__
|
||||||
#define __MAGIDOOR_H__
|
#define __MAGIDOOR_H__
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define MAGIDOOR_VERSION_MAJOR 1
|
||||||
|
#define MAGIDOOR_VERSION_MINOR 3
|
||||||
|
|
||||||
#define TRUE 1
|
#define TRUE 1
|
||||||
#define FALSE 0
|
#define FALSE 0
|
||||||
|
|
||||||
@ -18,14 +23,16 @@ typedef struct MDDoorControl {
|
|||||||
|
|
||||||
extern MDDoorControl_t mdcontrol;
|
extern MDDoorControl_t mdcontrol;
|
||||||
|
|
||||||
extern void md_init(const char *dropfile, int socket);
|
|
||||||
|
extern char md_getche(uint32_t sec, uint32_t usec);
|
||||||
|
extern void md_init(const char* dropfile, int socket);
|
||||||
extern void md_exit(int exitcode);
|
extern void md_exit(int exitcode);
|
||||||
extern void md_putchar(char c);
|
extern void md_putchar(char c);
|
||||||
extern void md_printf(const char *fmt, ...);
|
extern void md_printf(const char* fmt, ...);
|
||||||
extern char md_getc();
|
extern char md_getc();
|
||||||
extern int md_getstring(char *ptr, int maxlen, char minchar, char maxchar);
|
extern int md_getstring(char* ptr, int maxlen, char minchar, char maxchar);
|
||||||
extern void md_sendfile(const char *filename, int pause);
|
extern void md_sendfile(const char* filename, int pause);
|
||||||
extern void md_clr_scr();
|
extern void md_clr_scr();
|
||||||
extern void md_set_cursor(int y, int x);
|
extern void md_set_cursor(int y, int x);
|
||||||
extern char md_get_answer(char *options);
|
extern char md_get_answer(const char* options);
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user