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)
|
||||
project(magidoor)
|
||||
|
||||
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})
|
94
MD_Getc.c
94
MD_Getc.c
@ -2,34 +2,59 @@
|
||||
#include <stdio.h>
|
||||
#if defined(_MSC_VER) || defined(WIN32)
|
||||
#include <winsock2.h>
|
||||
#define STDIN_FILENO 0
|
||||
#define STDOUT_FILENO 1
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
#include <sys/select.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include "MagiDoor.h"
|
||||
|
||||
#define IAC 255
|
||||
|
||||
extern time_t mdtimeout;
|
||||
extern time_t mdtimeremaining;
|
||||
|
||||
char md_getc() {
|
||||
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) {
|
||||
fd_set rfd;
|
||||
FD_ZERO(&rfd);
|
||||
if (mdcontrol.socket == -1) {
|
||||
ret = read(STDIN_FILENO, &c, 1);
|
||||
FD_SET(STDIN_FILENO, &rfd);
|
||||
} 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) {
|
||||
#else
|
||||
if (ret == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
|
||||
#endif
|
||||
|
||||
if (rs == 0) {
|
||||
if (mdtimeout <= time(NULL)) {
|
||||
md_printf("\r\nIdle timeout!\r\n");
|
||||
md_exit(0);
|
||||
@ -38,15 +63,54 @@ char md_getc() {
|
||||
md_printf("\r\nOut of time!\r\n");
|
||||
md_exit(0);
|
||||
}
|
||||
usleep(100);
|
||||
return -1;
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char md_get_answer(char *options) {
|
||||
char md_get_answer(const char *options) {
|
||||
char c;
|
||||
c = md_getc();
|
||||
while (strchr(options, c) == NULL) {
|
||||
@ -60,6 +124,8 @@ int md_getstring(char *ptr, int maxlen, char minchar, char maxchar) {
|
||||
int len = 0;
|
||||
static char lastc = 'x';
|
||||
|
||||
*ptr = '\0';
|
||||
|
||||
while (len < maxlen) {
|
||||
c = md_getc();
|
||||
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 (len > 0) {
|
||||
md_printf("\x1b[D \x1b[D");
|
||||
md_printf("\b \b");
|
||||
len--;
|
||||
ptr[len] = '\0';
|
||||
}
|
||||
|
16
MD_Init.c
16
MD_Init.c
@ -1,12 +1,14 @@
|
||||
#include <stdio.h>
|
||||
#if defined(WIN32) || defined(_MSC_VER)
|
||||
#include <winsock2.h>
|
||||
#define strcasecmp stricmp
|
||||
#else
|
||||
#include <termios.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include "MagiDoor.h"
|
||||
|
||||
@ -172,6 +174,16 @@ int read_doorsys(const char *dropfile) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if !defined(WIN32) && !defined(_MSC_VER)
|
||||
|
||||
void md_sighuphandler(int i)
|
||||
{
|
||||
md_exit(-1);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
void md_exit(int exitcode) {
|
||||
if (mdcontrol.socket == -1) {
|
||||
#if !defined(WIN32) && !defined(_MSC_VER)
|
||||
@ -187,6 +199,8 @@ void md_init(const char *dropfile, int socket) {
|
||||
char *filename = strrchr(dropfile, PATH_SEP);
|
||||
#if !defined(WIN32) && !defined(_MSC_VER)
|
||||
struct termios ttystate;
|
||||
signal(SIGHUP, &md_sighuphandler);
|
||||
|
||||
#endif
|
||||
int ret = -1;
|
||||
|
||||
|
@ -1,12 +1,13 @@
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#if defined(_MSC_VER) || defined(WIN32)
|
||||
#include <winsock2.h>
|
||||
#include "MD_AnsiCons.h"
|
||||
#define STDOUT_FILENO 1
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include "MagiDoor.h"
|
||||
|
||||
|
@ -1,6 +1,11 @@
|
||||
#ifndef __MAGIDOOR_H__
|
||||
#define __MAGIDOOR_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define MAGIDOOR_VERSION_MAJOR 1
|
||||
#define MAGIDOOR_VERSION_MINOR 3
|
||||
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
|
||||
@ -18,6 +23,8 @@ typedef struct MDDoorControl {
|
||||
|
||||
extern MDDoorControl_t mdcontrol;
|
||||
|
||||
|
||||
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_putchar(char c);
|
||||
@ -27,5 +34,5 @@ extern int md_getstring(char *ptr, int maxlen, char minchar, char maxchar);
|
||||
extern void md_sendfile(const char* filename, int pause);
|
||||
extern void md_clr_scr();
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user