Compare commits

..

No commits in common. "da9c5ff82adfc66445ae942db958190b93124723" and "160dbcd50f7f6c58e2657709cac4ce992f3ffd7a" have entirely different histories.

6 changed files with 28 additions and 120 deletions

View File

@ -1,9 +1,5 @@
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()
set(SOURCE_FILES MD_Sendfile.c MD_Getc.c MD_Printf.c MD_Init.c MagiDoor.h)
add_library(mdoor STATIC ${SOURCE_FILES})

View File

@ -1,4 +1,4 @@
#pragma once
#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
View File

@ -2,59 +2,34 @@
#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>
#include <sys/select.h>
#include <unistd.h>
#endif
#include <unistd.h>
#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;
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;
ssize_t ret;
while (1) {
fd_set rfd;
FD_ZERO(&rfd);
if (mdcontrol.socket == -1) {
FD_SET(STDIN_FILENO, &rfd);
ret = read(STDIN_FILENO, &c, 1);
} else {
FD_SET(mdcontrol.socket, &rfd);
ret = recv(mdcontrol.socket, &c, 1, 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 (ret == 0) {
md_exit(0);
}
if (rs == 0) {
#if defined(_MSC_VER) || defined(WIN32)
if (ret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK) {
#else
if (ret == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
#endif
if (mdtimeout <= time(NULL)) {
md_printf("\r\nIdle timeout!\r\n");
md_exit(0);
@ -63,54 +38,15 @@ char md_getche(uint32_t sec, uint32_t usec) {
md_printf("\r\nOut of time!\r\n");
md_exit(0);
}
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;
usleep(100);
continue;
}
mdtimeout = time(NULL) + 900;
return c;
}
}
char md_get_answer(const char *options) {
char md_get_answer(char *options) {
char c;
c = md_getc();
while (strchr(options, c) == NULL) {
@ -124,8 +60,6 @@ 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') {
@ -142,7 +76,7 @@ int md_getstring(char *ptr, int maxlen, char minchar, char maxchar) {
}
if (c == '\b' || c == 127) {
if (len > 0) {
md_printf("\b \b");
md_printf("\x1b[D \x1b[D");
len--;
ptr[len] = '\0';
}
@ -158,4 +92,4 @@ int md_getstring(char *ptr, int maxlen, char minchar, char maxchar) {
}
return len;
}
}

View File

@ -1,14 +1,12 @@
#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"
@ -174,16 +172,6 @@ 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)
@ -199,8 +187,6 @@ 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,13 +1,12 @@
#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,11 +1,6 @@
#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
@ -23,16 +18,14 @@ 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_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(const char* options);
#endif
extern char md_get_answer(char *options);
#endif