Compare commits

...

10 Commits

Author SHA1 Message Date
da9c5ff82a Allow win32 to build with cmake 2024-10-19 16:50:03 +10:00
Andrew Pamment
750501c907 Fix for windows 2022-06-26 19:38:12 +10:00
Andrew Pamment
286b508f10 Add a timed getche function 2022-06-02 12:00:01 +10:00
Andrew Pamment
ddeede0ebe fix timeout 2021-10-27 10:47:53 +10:00
Andrew Pamment
410cc2b6c6 Update getc 2021-03-13 12:09:38 +10:00
Andrew Pamment
18a23222a9 fixes 2021-03-12 19:14:22 +10:00
Andrew Pamment
29a19692fe Handle IAC (well ignore it) 2020-10-12 17:10:28 +10:00
Andrew Pamment
318b385ce4 Fix get string 2020-06-06 10:44:32 +10:00
Andrew Pamment
b5bd8477e4 Change from sigterm to sighup 2020-05-30 17:58:16 +10:00
Andrew Pamment
b82eff1c72 Add SIGTERM handler 2020-05-30 17:51:45 +10:00
6 changed files with 120 additions and 28 deletions

View File

@ -1,5 +1,9 @@
cmake_minimum_required(VERSION 3.6)
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})

100
MD_Getc.c
View File

@ -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);
continue;
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;
}
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';
}

View File

@ -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;

View File

@ -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"

View File

@ -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,14 +23,16 @@ typedef struct MDDoorControl {
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_putchar(char c);
extern void md_printf(const char *fmt, ...);
extern void md_printf(const char* fmt, ...);
extern char md_getc();
extern int md_getstring(char *ptr, int maxlen, char minchar, char maxchar);
extern void md_sendfile(const char *filename, int pause);
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