From 4577dd26b3ecbf7ccc5ff55efa991334136866e1 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Thu, 19 Jan 2023 06:27:51 +0200 Subject: [PATCH] foo --- src/dos/input.c | 16 ++++++--- src/ftp.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/ftp.h | 10 ++++++ src/input.h | 3 +- src/main.c | 83 +++++++++++++++++++++++++++++++++++++++----- src/unix/input.c | 21 ++++++++++- src/unix/tgfx.c | 1 + 7 files changed, 219 insertions(+), 17 deletions(-) diff --git a/src/dos/input.c b/src/dos/input.c index 99a40de..4b36e4a 100644 --- a/src/dos/input.c +++ b/src/dos/input.c @@ -10,9 +10,17 @@ void cleanup_input(void) { } -int wait_input(union event *ev) +int poll_input(union event *ev) { - ev->type = EV_KEY; - ev->key.key = getch(); - return 1; + if(have_input()) { + ev->type = EV_KEY; + ev->key.key = getch(); + return 1; + } + return 0; +} + +int have_input(void) +{ + return kbhit(); } diff --git a/src/ftp.c b/src/ftp.c index 262140d..59bc284 100644 --- a/src/ftp.c +++ b/src/ftp.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -11,14 +12,20 @@ #ifdef __unix__ #include -#define closesocket(s) close(s) +#include +#define closesocket close +#define fcntlsocket fcntl #endif +static int handle_control(struct ftp *ftp); +static int handle_data(struct ftp *ftp, int s); +static void proc_control(struct ftp *ftp, const char *buf); + struct ftp *ftp_alloc(void) { struct ftp *ftp; - if(!(ftp = malloc(sizeof *ftp))) { + if(!(ftp = calloc(1, sizeof *ftp))) { return 0; } ftp->ctl = ftp->data = -1; @@ -28,6 +35,7 @@ struct ftp *ftp_alloc(void) void ftp_free(struct ftp *ftp) { if(!ftp) return; + if(ftp->ctl >= 0) { ftp_close(ftp); } @@ -61,6 +69,9 @@ int ftp_connect(struct ftp *ftp, const char *hostname, int port) ftp->ctl = -1; return -1; } + + fcntlsocket(ftp->ctl, F_SETFL, fcntlsocket(ftp->ctl, F_GETFL) | O_NONBLOCK); + ftp->num_crecv = 0; return 0; } @@ -68,10 +79,97 @@ void ftp_close(struct ftp *ftp) { if(ftp->ctl >= 0) { closesocket(ftp->ctl); + ftp->ctl = -1; + ftp->num_crecv = 0; } if(ftp->data >= 0) { closesocket(ftp->data); + ftp->data = -1; + ftp->num_drecv = 0; + } +} + +int ftp_sockets(struct ftp *ftp, int *sockv, int maxsize) +{ + if(ftp->ctl >= 0 && maxsize) { + *sockv++ = ftp->ctl; + maxsize--; } + return 1; +} + +int ftp_pending(struct ftp *ftp) +{ + int maxfd = -1; + fd_set rdset; + struct timeval tv = {0, 0}; + + FD_ZERO(&rdset); + if(ftp->ctl >= 0) { + FD_SET(ftp->ctl, &rdset); + maxfd = ftp->ctl; + } + if(ftp->data >= 0) { + FD_SET(ftp->data, &rdset); + if(ftp->data > maxfd) maxfd = ftp->data; + } + if(maxfd == -1) return 0; + + return select(maxfd + 1, &rdset, 0, 0, &tv) > 0 ? 1 : 0; +} + +int ftp_handle(struct ftp *ftp, int s) +{ + if(s == ftp->ctl) { + return handle_control(ftp); + } + if(s == ftp->data) { + return handle_data(ftp, s); + } + return -1; +} + +static int handle_control(struct ftp *ftp) +{ + int i, sz, rd; + char *buf, *start, *end; + + while((sz = sizeof ftp->crecv - ftp->num_crecv) > 0) { + start = ftp->crecv + ftp->num_crecv; + if((rd = recv(ftp->ctl, start, sz, 0)) == -1) { + if(errno == EINTR) continue; + /* assume EWOULDBLOCK, try again next time */ + return 0; + } + if(rd == 0) { + ftp_close(ftp); + return -1; + } + + end = start + rd; + buf = ftp->crecv; + for(i=0; icrecv && buf < end) { + ftp->num_crecv = end - buf; + memmove(ftp->crecv, buf, ftp->num_crecv); + } + } + return 0; +} + +static int handle_data(struct ftp *ftp, int s) +{ + return -1; +} + +static void proc_control(struct ftp *ftp, const char *buf) +{ } int ftp_update(struct ftp *ftp) diff --git a/src/ftp.h b/src/ftp.h index c843e53..2d69ce7 100644 --- a/src/ftp.h +++ b/src/ftp.h @@ -11,6 +11,11 @@ struct ftp_dirent { struct ftp { int ctl, data; /* sockets */ + char crecv[256]; + int num_crecv; + char drecv[256]; + int num_drecv; + char *cwd; struct ftp_dirent *dent; int num_dent; @@ -22,6 +27,11 @@ void ftp_free(struct ftp *ftp); int ftp_connect(struct ftp *ftp, const char *host, int port); void ftp_close(struct ftp *ftp); +int ftp_sockets(struct ftp *ftp, int *sockv, int maxsize); +int ftp_pending(struct ftp *ftp); + +int ftp_handle(struct ftp *ftp, int s); + int ftp_update(struct ftp *ftp); int ftp_chdir(struct ftp *ftp, const char *dirname); diff --git a/src/input.h b/src/input.h index 412b2a0..313d4f2 100644 --- a/src/input.h +++ b/src/input.h @@ -33,6 +33,7 @@ union event { int init_input(void); void cleanup_input(void); -int wait_input(union event *ev); +int poll_input(union event *ev); +int have_input(void); #endif /* INPUT_H_ */ diff --git a/src/main.c b/src/main.c index d6ec65b..611508d 100644 --- a/src/main.c +++ b/src/main.c @@ -1,14 +1,23 @@ +#include +#include +#include #include "tgfx.h" #include "input.h" #include "tui.h" #include "ftp.h" +int proc_input(void); +int keypress(int key); + struct ftp *ftp; struct tui_widget *uilist; int main(void) { - union event ev; + int i, numsock, maxfd; + int ftpsock[16]; + fd_set rdset; + struct timeval tv; if(!(ftp = ftp_alloc())) { return 1; @@ -31,26 +40,82 @@ int main(void) tui_add_list_item(uilist, "another item"); tui_add_list_item(uilist, "foo"); - tg_setcursor(0, 24); + tg_setcursor(0, 23); tui_draw(uilist); - while(wait_input(&ev)) { + for(;;) { + FD_ZERO(&rdset); + maxfd = 0; + + numsock = ftp_sockets(ftp, ftpsock, sizeof ftpsock); + for(i=0; i maxfd) maxfd = ftpsock[i]; + } + +#ifdef __unix__ + FD_SET(0, &rdset); + tv.tv_sec = 120; + tv.tv_usec = 0; +#else + tv.tv_sec = tv.tv_usec = 0; +#endif + + if(select(maxfd + 1, &rdset, 0, 0, &tv) == -1 && errno == EINTR) { + continue; + } + +#ifdef __unix__ + if(FD_ISSET(0, &rdset)) { + if(proc_input() == -1) { + break; + } + } +#endif + + for(i=0; i +#include #include #include "input.h" int init_input(void) { + nodelay(stdscr, TRUE); return 0; } @@ -10,9 +13,25 @@ void cleanup_input(void) { } -int wait_input(union event *ev) +int poll_input(union event *ev) { ev->type = EV_KEY; ev->key.key = getch(); return 1; } + +int have_input(void) +{ + fd_set rdset; + struct timeval tv = {0, 0}; + + FD_ZERO(&rdset); + FD_SET(0, &rdset); + + while(select(1, &rdset, 0, 0, &tv) == -1 && errno == EINTR); + + if(FD_ISSET(0, &rdset)) { + return 1; + } + return 0; +} diff --git a/src/unix/tgfx.c b/src/unix/tgfx.c index f88fd15..b7f9f82 100644 --- a/src/unix/tgfx.c +++ b/src/unix/tgfx.c @@ -35,6 +35,7 @@ void tg_redraw(void) void tg_clear(void) { + clear(); } -- 1.7.10.4