diff options
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 62 |
1 files changed, 38 insertions, 24 deletions
@@ -6,6 +6,19 @@ #define DATABASE_PATH "ftag.db" +static void ftag_init(int, char **); +static void ftag_help(int, char **); + +struct ftag_command { + const char *name; + void (*func)(int argc, char **argv); +}; + +static const struct ftag_command toplevel_commands[] = { + {.name = "init", .func = ftag_init}, + {.name = "help", .func = ftag_help} +}; +static const int toplevel_command_count = sizeof(toplevel_commands) / sizeof(struct ftag_command); static sqlite3 *db; static void __sqlite3_check(int rc, sqlite3 *db, const char *file, int line) @@ -19,41 +32,42 @@ static void __sqlite3_check(int rc, sqlite3 *db, const char *file, int line) #define sqlite3_check(RC, DB) __sqlite3_check(RC, DB, __FILE__, __LINE__) -static void ftag_init(void) +static void ftag_init(int, char **) { - sqlite3_exec("") + ; } -static void usage(void) +static void ftag_help(int, char **) { - printf("usage: ftag COMMAND [COMMAND-ARG]...\n"); + printf("Usage: ftag COMMAND [COMMAND-ARG]...\n"); printf("Available values for COMMAND:\n"); - printf(" init\n"); + printf(" init initialize the database\n"); + printf(" help print this message\n"); } -int main(int argc, char *argv[]) +static void parse_args(int argc, + char **argv, + const struct ftag_command *commands, + int command_count) { - char *err_msg; - int rc; + assert(argc > 0); + assert(command_count > 0); + for (int i = 0; i < command_count; i++) { + if (strcmp(argv[0], commands[i].name) == 0) { + commands[i].func(argc-1, argv+1); + return; + } + } +} +int main(int argc, char *argv[]) +{ if (argc == 1) { - usage(); - exit(1); + ftag_help(0, NULL); + exit(EXIT_FAILURE); } - rc = sqlite3_open(DATABASE_PATH, &db); - sqlite3_check(rc, db); + parse_args(argc-1, argv+1, toplevel_commands, toplevel_command_count); - if (strcmp(argv[1], "init") == 0) - ftag_init(); - else - assert(0); - - sqlite3_free(err_msg); - sqlite3_close(db); - return 0; + return EXIT_SUCCESS; } - -/* Local Variables: */ -/* compile-command: "gcc -std=c99 -Wall -O0 -g3 $(pkg-config --libs sqlite3) -o sqlite-first main.c" */ -/* End: */ |
