diff --git a/Makefile b/Makefile index b1d8282..1be1a2b 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,8 @@ SRC = \ $(SRCDIR)/action.c \ $(SRCDIR)/args.c \ $(SRCDIR)/db.c \ + $(SRCDIR)/help.c \ + $(SRCDIR)/note.c \ $(SRCDIR)/pager.c \ $(SRCDIR)/pit.c \ $(SRCDIR)/project.c \ @@ -28,6 +30,8 @@ OBJ = \ $(OBJDIR)/action.o \ $(OBJDIR)/args.o \ $(OBJDIR)/db.o \ + $(OBJDIR)/help.o \ + $(OBJDIR)/note.o \ $(OBJDIR)/pager.o \ $(OBJDIR)/pit.o \ $(OBJDIR)/project.o \ @@ -55,6 +59,12 @@ $(OBJDIR)/args.o: $(SRCDIR)/args.c $(SRCDIR)/pit.h $(SRCDIR)/object.h $(SRCDIR)/ $(OBJDIR)/db.o: $(SRCDIR)/db.c $(SRCDIR)/pit.h $(SRCDIR)/object.h $(SRCDIR)/table.h $(CC) -o $(OBJDIR)/db.o -c $(SRCDIR)/db.c +$(OBJDIR)/help.o: $(SRCDIR)/help.c $(SRCDIR)/pit.h $(SRCDIR)/object.h $(SRCDIR)/table.h + $(CC) -o $(OBJDIR)/help.o -c $(SRCDIR)/help.c + +$(OBJDIR)/note.o: $(SRCDIR)/note.c $(SRCDIR)/pit.h $(SRCDIR)/object.h $(SRCDIR)/table.h + $(CC) -o $(OBJDIR)/note.o -c $(SRCDIR)/note.c + $(OBJDIR)/pager.o: $(SRCDIR)/pager.c $(SRCDIR)/pit.h $(SRCDIR)/object.h $(SRCDIR)/table.h $(CC) -o $(OBJDIR)/pager.o -c $(SRCDIR)/pager.c diff --git a/src/action.c b/src/action.c index 691f987..edcc545 100644 --- a/src/action.c +++ b/src/action.c @@ -18,7 +18,7 @@ static void action_list() } } -uchar *pit_action(ulong id, char *subject, char *message) +void pit_action(ulong id, char *subject, char *message) { static Action action; @@ -29,11 +29,10 @@ uchar *pit_action(ulong id, char *subject, char *message) strncpy(action.username, current_user(), sizeof(action.username) - 1); strncpy(action.message, message, sizeof(action.message) - 1); - return pit_table_insert(actions, (uchar *)&action); + pit_table_insert(actions, (uchar *)&action); } -int pit_log(char *argv[]) +void pit_log(char *argv[]) { action_list(); - return 1; } \ No newline at end of file diff --git a/src/db.c b/src/db.c index 5425b29..44734a8 100644 --- a/src/db.c +++ b/src/db.c @@ -48,7 +48,7 @@ static void write_header(FILE *file) } } -int pit_init(char *argv[]) { +void pit_init(char *argv[]) { char **arg = &argv[1]; char *file_name = pit_file_name(); int reply = 'Y'; @@ -64,7 +64,6 @@ int pit_init(char *argv[]) { pit_db_initialize(); printf("Created empty %s\n", file_name); } - return 1; } void pit_db_load() { diff --git a/src/help.c b/src/help.c new file mode 100644 index 0000000..ea8773e --- /dev/null +++ b/src/help.c @@ -0,0 +1,9 @@ +#include +#include +#include +#include "pit.h" + +void pit_help(char *argv[]) +{ + puts("pit: help is not implemented yet"); +} \ No newline at end of file diff --git a/src/note.c b/src/note.c new file mode 100644 index 0000000..7fc8f58 --- /dev/null +++ b/src/note.c @@ -0,0 +1,9 @@ +#include +#include +#include +#include "pit.h" + +void pit_note(char *argv[]) +{ + puts("pit: note is not implemented yet"); +} \ No newline at end of file diff --git a/src/pit.c b/src/pit.c index fb5ee04..9bf9c7e 100644 --- a/src/pit.c +++ b/src/pit.c @@ -9,11 +9,6 @@ PTable tasks; PTable notes; PTable actions; -static int usage() { - printf("usage...\n"); - return 1; -} - /* ** Suicide. */ @@ -42,29 +37,23 @@ void perish(char *prefix) exit(0); } +void pit_status(char *argv[]) +{ + puts("pit: status is not implemented yet"); +} + int main(int argc, char *argv[]) { - char *commands[] = { "project", "task", "note", "log", "init" }; + register int i; + char *commands[] = { "project", "task", "note", "log", "init", "status", "help" }; + void (*handlers[])(char *argv[]) = { pit_project, pit_task, pit_note, pit_log, pit_init, pit_status, pit_help }; - /*** - int i; - printf("argc: %d\n", argc); - for(i = 0; i < argc; i++) { - printf("argv[%d]: [%s]\n", i, argv[i]); - } - ***/ - - if (argc > 1) { - if (strstr(commands[0], argv[1]) == commands[0]) { - return pit_project(&argv[1]); - } else if (strstr(commands[1], argv[1]) == commands[1]) { - return pit_task(&argv[1]); - } else if (strstr(commands[2], argv[1]) == commands[2]) { - return 1; /* pit_note(&argv[1]); */ - } else if (strstr(commands[3], argv[1]) == commands[3]) { - return pit_log(&argv[1]); - } else if (strstr(commands[4], argv[1]) == commands[4]) { - return pit_init(&argv[1]); + if (argc == 1) argv[1] = "help"; + for(i = 0; i < ARRAY_SIZE(commands); i++) { + if (strstr(commands[i], argv[1]) == commands[i]) { + handlers[i](&argv[1]); + return 1; } } - return usage(); -} \ No newline at end of file + printf("invalid command: %s", argv[1]); + return 0; +} diff --git a/src/pit.h b/src/pit.h index dfc6e84..93631ea 100644 --- a/src/pit.h +++ b/src/pit.h @@ -11,13 +11,15 @@ typedef int bool; #include "table.h" #include "pager.h" -/* #defines */ + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) + #ifndef FALSE #define FALSE (0) #endif #ifndef TRUE -#define TRUE (1) +#define TRUE (1) #endif #ifndef min @@ -39,7 +41,20 @@ extern PTable notes; extern PTable projects; extern PTable tasks; -/* args.c */ +/* Command handlers and database APIs */ +void pit_init(char *argv[]); +void pit_project(char *argv[]); +void pit_task(char *argv[]); +void pit_note(char *argv[]); +void pit_log(char *argv[]); +void pit_status(char *argv[]); +void pit_help(char *argv[]); +void pit_db_load(); +void pit_db_save(); +void pit_db_initialize(); +void pit_action(ulong id, char *subject, char *message); + +/* Argument parsing helpers */ int pit_arg_is_option(char **arg); int pit_arg_option(char **arg); char *pit_arg_string(char **arg, char *required); @@ -47,20 +62,7 @@ ulong pit_arg_number(char **arg, char *required); time_t pit_arg_date(char **arg, char *required); time_t pit_arg_time(char **arg, char *required); -/* db.c */ -int pit_init(char *argv[]); -void pit_db_load(); -void pit_db_save(); -void pit_db_initialize(); - -/* log.c project.c task.c user.c */ -int pit_project(char *argv[]); -int pit_task(char *argv[]); -int pit_log(char *argv[]); -char *pit_current_user(); -uchar *pit_action(ulong id, char *subject, char *message); - -/* util.c */ +/* Misc utilities */ void die(char *msg, ...); void perish(char *prefix); char *mem2str(char *mem, int len); diff --git a/src/project.c b/src/project.c index b993ea0..0099a87 100644 --- a/src/project.c +++ b/src/project.c @@ -96,7 +96,7 @@ static int delete_project(unsigned long number) return 1; } -int pit_project(char *argv[]) +void pit_project(char *argv[]) { char **arg = &argv[1]; unsigned long number; @@ -129,6 +129,4 @@ int pit_project(char *argv[]) show_project(number); } } - - return 1; } diff --git a/src/task.c b/src/task.c index 8c1608c..43e414b 100644 --- a/src/task.c +++ b/src/task.c @@ -214,7 +214,7 @@ static void task_parse_options(char **arg, char **name, char **status, char **pr } } -int pit_task(char *argv[]) +void pit_task(char *argv[]) { char **arg = &argv[1]; char *name = NULL, *status = NULL, *priority = NULL; @@ -267,5 +267,4 @@ int pit_task(char *argv[]) } } } - return 1; }