From 92e3bc3c3beee18adae630557ab5d9bc6fe20555 Mon Sep 17 00:00:00 2001 From: FRIGN Date: Wed, 17 Sep 2014 17:53:09 +0200 Subject: Rework data-structures --- ratatox.c | 143 +++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 100 insertions(+), 43 deletions(-) (limited to 'ratatox.c') diff --git a/ratatox.c b/ratatox.c index 5b88dca..e823b48 100644 --- a/ratatox.c +++ b/ratatox.c @@ -31,32 +31,55 @@ struct node { #include "config.h" -struct fifo { +struct file { + int type; const char *name; int flags; mode_t mode; +}; + +enum { + IN, + OUT, + ERR, + NR_GFILES +}; + +struct slot { + const char *name; void (*cb)(void *); + int outtype; + int fd[NR_GFILES]; }; enum { - NAME_FIFO, - STATUS_FIFO, - FRIENDREQ_FIFO, - NR_GFIFOS + NAME, + STATUS, + REQUEST, +}; + +enum { + FIFO, + OUT_F, + STATIC, + FOLDER }; static void setname(void *); static void setstatusmsg(void *); static void sendfriendreq(void *); -/* Global FIFOs for modifying our own state, they go in $(PWD)/{name,status}_in */ -static struct fifo gfifos[] = { - { .name = "name_in", .flags = O_RDONLY | O_NONBLOCK, .mode = 0644, .cb = setname }, - { .name = "statusmsg_in", .flags = O_RDONLY | O_NONBLOCK, .mode = 0644, .cb = setstatusmsg }, - { .name = "friendreq_in", .flags = O_RDONLY | O_NONBLOCK, .mode = 0644, .cb = sendfriendreq }, +static struct slot gslots[] = { + [NAME] = { .name = "name", .cb = setname, .outtype = STATIC}, + [STATUS] = { .name = "status", .cb = setstatusmsg, .outtype = STATIC}, + [REQUEST] = { .name = "request", .cb = sendfriendreq, .outtype = FOLDER} }; -static int globalfd[NR_GFIFOS]; +static struct file gfiles[] = { + { .type = FIFO, .name = "in", .flags = O_RDONLY | O_NONBLOCK, .mode = 0644}, + { .type = OUT_F, .name = "out", .flags = O_WRONLY | O_TRUNC | O_CREAT, .mode = 0644}, + { .type = OUT_F, .name = "err", .flags = O_WRONLY | O_TRUNC | O_CREAT, .mode = 0644}, +}; enum { TEXT_IN_FIFO, @@ -65,9 +88,9 @@ enum { }; /* Friend related FIFOs, they go in sizeof(name) - 1) r = sizeof(name) - 1; name[r] = '\0'; - writeline("name_out", "w", "%s\n", name); + dprintf(gslots[NAME].fd[OUT], "%s\n", name); /* Dump status message */ r = tox_get_self_status_message(tox, statusmsg, @@ -587,7 +641,7 @@ localinit(void) if (r > sizeof(statusmsg) - 1) r = sizeof(statusmsg) - 1; statusmsg[r] = '\0'; - writeline("statusmsg_out", "w", "%s\n", statusmsg); + dprintf(gslots[STATUS].fd[OUT], "%s\n", name); /* Dump ID */ fp = fopen("id", "w"); @@ -858,7 +912,7 @@ setname(void *data) int r; again: - r = read(globalfd[NAME_FIFO], name, sizeof(name) - 1); + r = read(gslots[NAME].fd[IN], name, sizeof(name) - 1); if (r < 0) { if (errno == EINTR) goto again; @@ -873,7 +927,7 @@ again: tox_set_name(tox, name, r); datasave(); printout("Changed name to %s\n", name); - writeline("name_out", "w", "%s\n", name); + dprintf(gslots[NAME].fd[OUT], "%s\n", name); } static void @@ -883,7 +937,7 @@ setstatusmsg(void *data) int r; again: - r = read(globalfd[STATUS_FIFO], statusmsg, sizeof(statusmsg) - 1); + r = read(gslots[STATUS].fd[IN], statusmsg, sizeof(statusmsg) - 1); if (r < 0) { if (errno == EINTR) goto again; @@ -898,7 +952,7 @@ again: tox_set_status_message(tox, statusmsg, r); datasave(); printout("Changed status message to %s\n", statusmsg); - writeline("statusmsg_out", "w", "%s\n", statusmsg); + dprintf(gslots[STATUS].fd[OUT], "%s\n", statusmsg); } static void @@ -910,7 +964,7 @@ sendfriendreq(void *data) int r; again: - r = read(globalfd[FRIENDREQ_FIFO], buf, sizeof(buf) - 1); + r = read(gslots[REQUEST].fd[IN], buf, sizeof(buf) - 1); if (r < 0) { if (errno == EINTR) goto again; @@ -937,25 +991,25 @@ again: r = tox_add_friend(tox, id, buf, strlen(buf)); switch (r) { case TOX_FAERR_TOOLONG: - fprintf(stderr, "Message is too long\n"); + dprintf(gslots[REQUEST].fd[ERR], "Message is too long\n"); break; case TOX_FAERR_NOMESSAGE: - fprintf(stderr, "Please add a message to your request\n"); + dprintf(gslots[REQUEST].fd[ERR], "Please add a message to your request\n"); break; case TOX_FAERR_OWNKEY: - fprintf(stderr, "That appears to be your own ID\n"); + dprintf(gslots[REQUEST].fd[ERR], "That appears to be your own ID\n"); break; case TOX_FAERR_ALREADYSENT: - fprintf(stderr, "Friend request already sent\n"); + dprintf(gslots[REQUEST].fd[ERR], "Friend request already sent\n"); break; case TOX_FAERR_UNKNOWN: - fprintf(stderr, "Unknown error while sending your request\n"); + dprintf(gslots[REQUEST].fd[ERR], "Unknown error while sending your request\n"); break; case TOX_FAERR_BADCHECKSUM: - fprintf(stderr, "Bad checksum while verifying address\n"); + dprintf(gslots[REQUEST].fd[ERR], "Bad checksum while verifying address\n"); break; case TOX_FAERR_SETNEWNOSPAM: - fprintf(stderr, "Friend already added but nospam doesn't match\n"); + dprintf(gslots[REQUEST].fd[ERR], "Friend already added but nospam doesn't match\n"); break; default: printout("Friend request sent\n"); @@ -970,7 +1024,7 @@ loop(void) struct friend *f; time_t t0, t1; int connected = 0; - int i, n; + int i, m, n; int fdmax; fd_set rfds; struct timeval tv; @@ -999,10 +1053,13 @@ loop(void) FD_SET(STDIN_FILENO, &rfds); fdmax = STDIN_FILENO; - for (i = 0; i < NR_GFIFOS; i++) { - FD_SET(globalfd[i], &rfds); - if (globalfd[i] > fdmax) - fdmax = globalfd[i]; + for (i = 0; i < LEN(gslots); i++) { + for (m = 0; m < LEN(gfiles); m++) { + FD_SET(gslots[i].fd[m], &rfds); + if (gslots[i].fd[m] > fdmax) { + fdmax = gslots[i].fd[m]; + } + } } TAILQ_FOREACH(f, &friendhead, entry) { @@ -1072,10 +1129,10 @@ loop(void) if (FD_ISSET(STDIN_FILENO, &rfds) != 0) cmdrun(); - for (i = 0; i < NR_GFIFOS; i++) { - if (FD_ISSET(globalfd[i], &rfds) == 0) + for (i = 0; i < LEN(gslots); i++) { + if (FD_ISSET(gslots[i].fd[IN], &rfds) == 0) continue; - (*gfifos[i].cb)(NULL); + (*gslots[i].cb)(NULL); } TAILQ_FOREACH(f, &friendhead, entry) { -- cgit v1.2.3