Seperated '=' handle, improved build, renamed DELC to DECL

This commit is contained in:
Emil 2023-08-02 21:24:30 -06:00
parent d808ef6bae
commit 71038cee03
14 changed files with 81 additions and 53 deletions

View File

@ -1,3 +1,10 @@
# note that this does not properly handle options
probotic:
# note that this file does not properly handle options
SRC := api.c irc.c main.c parse.c unity.c
HDR := api.h error.h irc.h irccolors.h parse.h stmt.h
probotic: $(SRC) $(HDR)
./build.sh
# do nothing but update them...
$(SRC) $(HDR):

View File

@ -1,19 +1,25 @@
INSERT INTO difficulty VALUES('easy');
INSERT INTO difficulty VALUES('medium');
INSERT INTO difficulty VALUES('hard');
INSERT INTO difficulty VALUES('fuck you');
INSERT INTO difficulty VALUES('Easy');
INSERT INTO difficulty VALUES('Medium');
INSERT INTO difficulty VALUES('Hard');
INSERT INTO difficulty VALUES('Fuck You');
INSERT INTO tag VALUES('math');
INSERT INTO tag VALUES('AI');
INSERT INTO tag VALUES('networking');
INSERT INTO tag VALUES('games');
INSERT INTO tag VALUES('rendering');
INSERT INTO tag VALUES('simulation');
INSERT INTO tag VALUES('Algorithms');
INSERT INTO tag VALUES('Games');
INSERT INTO tag VALUES('Math');
INSERT INTO tag VALUES('Networking');
INSERT INTO tag VALUES('Rendering');
INSERT INTO tag VALUES('Simulation');
INSERT INTO tag VALUES('Tools');
-- I believe the list of links might be better suited as a detacted part of this
-- I also have no idea how this would format this internally
-- Does SQL would support 'a' 'b' concat syntax? as in C's puts("abc" "def")?
--
INSERT INTO project (title, body) VALUES (
'test project',
'desc'
'IRC Bot',
'Build an IRC Bot using SQLite3 and libircclient\nhttps://www.sqlite.org/index.html\n
http://www.ulduzsoft.com/libircclient/'
);
INSERT INTO assignment VALUES (

View File

@ -13,7 +13,6 @@ DROP TABLE IF EXISTS difficulty;
CREATE TABLE difficulty (
diff VARCHAR(16)
);
DROP TABLE IF EXISTS tag;
CREATE TABLE tag (

View File

@ -1,9 +1,9 @@
#ifndef API_H_
DELC int api_init(void);
DELC void api_rope(void);
DELC void rope(void);
DELC char * remind(char * who);
DECL int api_init(void);
DECL void api_rope(void);
DECL void rope(void);
DECL char * remind(char * who);
#define API_H_
#endif

View File

@ -3,8 +3,8 @@
#include <stdio.h>
#define ERR(ret,msg) do { fputs(msg "\n", stderr); return (ret); } while (0)
#define PERROR(ret,name) do { perror(name); return (ret); } while (0)
#define ERRMSG(msg) fputs(msg "\n", stderr)
#define PERROR(name) perror(name)
#define DB_ERROR 100
#define IRC_ERROR 200

View File

@ -11,7 +11,7 @@ extern char const * channel;
extern char * current_username;
DELC int init(creds_t const * creds, char const * server, int port);
DECL int init(creds_t const * creds, char const * server, int port);
#define IRC_H_
#endif

View File

@ -1,3 +1,5 @@
#ifndef IRCCOLOR_H_
#define IRC_COLOR_WHITE "\x03\x00"
#define IRC_COLOR_BLACK "\x03\x01"
#define IRC_COLOR_BLUE "\x03\x02"
@ -14,3 +16,6 @@
#define IRC_COLOR_PINK "\x03\x13"
#define IRC_COLOR_GREY "\x03\x14"
#define IRC_COLOR_LIGHT_GREY "\x03\x15"
#define IRCCOLOR_H_
#endif

View File

@ -6,9 +6,9 @@ typedef struct
char * password;
} creds_t;
DELC void parse_command(char * cmd);
DELC int parse_creds(creds_t * creds, char const * creds_file);
DELC void clean_creds(creds_t * creds);
DECL void parse_command(char * cmd);
DECL int parse_creds(creds_t * creds, char const * creds_file);
DECL void clean_creds(creds_t * creds);
#define CREDS_PARSER_H
#endif

View File

@ -1,5 +0,0 @@
#ifndef UTILS_H_
#define UTILS_H_
#endif

View File

@ -22,7 +22,7 @@
static sqlite3 * connection = NULL;
DELC int
DECL int
api_init(void)
{
DBERR(sqlite3_open(DBFILE, &connection));
@ -30,14 +30,14 @@ api_init(void)
return 0;
}
DELC void
DECL void
api_rope(void)
{
DBERR(sqlite3_finalize(remind_stmt));
sqlite3_close(connection);
}
DELC void
DECL void
rope(void)
{
if (session)
@ -45,7 +45,7 @@ rope(void)
api_rope();
}
DELC char *
DECL char *
remind(char * who)
{
char * r;

View File

@ -37,7 +37,7 @@ char * current_username;
#define IRCMSG(msg) irc_cmd_msg(session, channel, msg)
DELC char *
DECL char *
get_username(const char * origin)
{
const char USERNAME_TERMINATOR = '!';
@ -51,7 +51,7 @@ get_username(const char * origin)
return r;
}
DELC void
DECL void
ircmsg(const char* fmt,
...)
{
@ -69,7 +69,7 @@ ircmsg(const char* fmt,
va_end(args);
}
DELC void
DECL void
event_connect(irc_session_t * session,
const char * event,
const char * origin,
@ -83,7 +83,7 @@ event_connect(irc_session_t * session,
irc_cmd_join(session, channel, 0);
}
DELC void
DECL void
event_channel(irc_session_t * session,
char const * event,
char const * origin,

View File

@ -30,7 +30,7 @@
/* args: server port channel [username] - defaults to probotic */
int
main(int argc,
main(int const argc,
char const ** argv)
{

View File

@ -29,16 +29,16 @@
enum cred_param_ids_e
{
USERNAME,
PASSWORD
PASSWORD
};
/* TODO: move = to the handler */
char const * cred_param_names_g[] = {
"username=",
"password="
"username",
"password"
};
DELC void
DECL void
parse_command(char * cmd)
{
size_t i = 0;
@ -71,39 +71,55 @@ parse_command(char * cmd)
}
}
DELC int
DECL int
parse_creds(creds_t * creds,
char const * creds_file)
{
/* don't put declarations in loops */
FILE * stream;
char * values[PARAMS_COUNT];
char * line = NULL;
char const * val;
size_t nread = 0;
size_t param_len;
size_t val_len;
size_t i;
#ifndef NDEBUG
size_t column = 1;
#endif /* !NDEBUG */
creds->username = NULL;
creds->password = NULL;
stream = fopen(creds_file, "r");
if (stream == NULL)
{ PERROR(PROGN); }
{ PERROR(1,PROGN); }
char * values[PARAMS_COUNT];
memset(values, 0, sizeof(char *) * PARAMS_COUNT);
char * line = NULL;
while (getline(&line, &nread, stream) > 0)
{
for (size_t i = 0; i < PARAMS_COUNT; i++)
for (i = 0; i < PARAMS_COUNT; i++)
{
size_t param_len = strlen(cred_param_names_g[i]);
if (!strncmp(cred_param_names_g[i], line, param_len))
/* Ideally this should be optimized out as the literals are known constants */
param_len = strlen(cred_param_names_g[i]);
/* TODO lookahead for a = b notation? */
if (!strncmp(cred_param_names_g[i], line, param_len) &&
line[param_len] == '=')
{
/* Starts with the current parameter specifier */
char const * val = line + param_len;
size_t val_len = strlen(val);
val = line + param_len + 1;
val_len = strlen(val);
/* Duplicates and gets rid of a newline */
values[i] = strndup(val, val[val_len - 1] == '\n' ? val_len - 1 : val_len);
break;
}
#ifndef NDEBUG
else
{ fprintf(stderr, "line %ld '%s' DISCARDED\n", column, line); }
++column;
#endif /* !NDEBUG */
}
free(line);

View File

@ -1,4 +1,4 @@
#define DELC static
#define DECL static
#include "irc.c"
#include "parse.c"
#include "api.c"