From aed06c9dec77e6d612dadaa0e13d09ca68be28d3 Mon Sep 17 00:00:00 2001 From: Emil Date: Thu, 3 Aug 2023 03:15:20 -0600 Subject: [PATCH] Working on pair parser --- include/parse.h | 3 ++- src/irc.c | 8 ++++++- src/main.c | 9 +++++-- src/parse.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 88 insertions(+), 6 deletions(-) diff --git a/include/parse.h b/include/parse.h index adafb8a..d3ac359 100644 --- a/include/parse.h +++ b/include/parse.h @@ -11,8 +11,9 @@ typedef struct extern creds_t creds; +DECL char * slurp(const char * fn); +DECL int parse_pair(char * const buf, size_t const len); DECL void parse_command(char * cmd); -DECL int parse_creds(char const * creds_file); DECL void clean_creds(void); #define CREDS_PARSER_H diff --git a/src/irc.c b/src/irc.c index bae91d3..06f74f7 100644 --- a/src/irc.c +++ b/src/irc.c @@ -113,7 +113,7 @@ event_channel(irc_session_t * session, free(current_username); } -int +DECL int init(void) { if(api_init()) @@ -130,6 +130,12 @@ init(void) creds.server, creds.port, creds.password, creds.username, creds.username, creds.username); atexit(rope); + return 0; +} + +DECL int +loop(void) +{ /* We should figure out how the failure happens so we can tell the user that. */ if (irc_run(session) != 0) { ERR(1, "Error running IRC session\nPossible issue: bad URL," diff --git a/src/main.c b/src/main.c index 0134f67..a71722a 100644 --- a/src/main.c +++ b/src/main.c @@ -59,6 +59,7 @@ main (int argc, if (argc > 1) { char * arg; + char * buf; while (++argv, --argc) { arg = *argv; @@ -83,8 +84,12 @@ main (int argc, { creds.password = argv[1]; } else if (strcmp(arg, "auth") == 0) { + buf = slurp(authfile); + if (!buf) + { PERROR(1); } + authfile = argv[1]; - if (parse_creds(authfile)) + if (parse_pair(buf, strlen(buf))) { ERR(CREDS_ERROR, "Cannot parse creds"); } } ++argv; --argc; @@ -103,7 +108,7 @@ main (int argc, if (init()) { return 1; } - return init(); + return loop(); help: help(); return 1; diff --git a/src/parse.c b/src/parse.c index 4299909..6ffa133 100644 --- a/src/parse.c +++ b/src/parse.c @@ -26,6 +26,8 @@ #define PARAMS_COUNT 2 +#define ARRAY_SIZE(x) ((size_t) (sizeof x) / (size_t) (sizeof *x)) + creds_t creds = {0}; enum cred_param_ids_e @@ -37,7 +39,8 @@ enum cred_param_ids_e PORT }; -char const * cred_param_names_g[] = { +char const * cred_names[] = +{ "username", "password", "channel", @@ -45,12 +48,35 @@ char const * cred_param_names_g[] = { "port" }; +DECL char * +slurp(const char * fn) +{ + size_t len; + char * b; + FILE * fp = fopen(fn, "r"); + if (fp) + { + fseek(fp, 0, SEEK_END); + len = ftell(fp); + rewind(fp); + b = malloc(len); + if (b) + { fread(b, 1, len, fp); } + fclose(fp); + return b; + } + else + { return NULL; } +} + DECL void parse_command(char * cmd) { size_t i = 0; /* size_t len = strlen(cmd); */ - while (cmd[i] && + /* TODO does not handle commands with leading space, + use custom implemented to-spec isspace implementation */ + while (cmd[i] != '\0' && cmd[i] != ' ') { ++i; } if (cmd[i] == '\0') @@ -79,6 +105,49 @@ parse_command(char * cmd) } DECL int +parse_pair(char const * buf, size_t const len) +{ + size_t i, f, x; + for (i = 0; i < len; ++i) + { + if (buf[i] == '=') + { + /* X macro for handling this data may be better */ + for (f = 0; f < ARRAY_SIZE(cred_names[i]); ++i) + { + if (strncmp(buf, cred_names[i], ARRAY_SIZE(cred_names[i])) == 0) + { + x = i; + while (i != '\0' || + i != '\n') + { ++i; } + x = i - x; + switch (i) + { + case USERNAME: creds.username = strndup(buf+x); break; + case PASSWORD: creds.password = strndup(buf+x); break; + case CHANNEL: creds.channel = strndup(buf+x); break; + case SERVER: creds.server = strndup(buf+x); break; + case PORT: creds.port = atoi(buf+x); break; + } + while (i != '\0' || + i != '\n') + buf[i] = '\0'; + } +#ifndef NDEBUG + else + { fprintf(stderr, "DISCARDED\n"); } +#endif /* !NDEBUG */ + + } + } + } + return 0; +} + +#if 0 + +DECL int parse_creds(char const * creds_file) { /* don't put declarations in loops */ @@ -155,6 +224,7 @@ fail: return 1; } +#endif /* 0 */ void clean_creds(void)