Working on pair parser

This commit is contained in:
Emil 2023-08-03 03:15:20 -06:00
parent 6aba8323d9
commit aed06c9dec
4 changed files with 88 additions and 6 deletions

View File

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

View File

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

View File

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

View File

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