Refactored to add dispatcher and universal command handlers

This commit is contained in:
Mike Dvorkin
2010-08-07 16:39:11 -07:00
parent 3d1786c080
commit 24c793fce5
9 changed files with 69 additions and 55 deletions

View File

@@ -17,6 +17,8 @@ SRC = \
$(SRCDIR)/action.c \ $(SRCDIR)/action.c \
$(SRCDIR)/args.c \ $(SRCDIR)/args.c \
$(SRCDIR)/db.c \ $(SRCDIR)/db.c \
$(SRCDIR)/help.c \
$(SRCDIR)/note.c \
$(SRCDIR)/pager.c \ $(SRCDIR)/pager.c \
$(SRCDIR)/pit.c \ $(SRCDIR)/pit.c \
$(SRCDIR)/project.c \ $(SRCDIR)/project.c \
@@ -28,6 +30,8 @@ OBJ = \
$(OBJDIR)/action.o \ $(OBJDIR)/action.o \
$(OBJDIR)/args.o \ $(OBJDIR)/args.o \
$(OBJDIR)/db.o \ $(OBJDIR)/db.o \
$(OBJDIR)/help.o \
$(OBJDIR)/note.o \
$(OBJDIR)/pager.o \ $(OBJDIR)/pager.o \
$(OBJDIR)/pit.o \ $(OBJDIR)/pit.o \
$(OBJDIR)/project.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 $(OBJDIR)/db.o: $(SRCDIR)/db.c $(SRCDIR)/pit.h $(SRCDIR)/object.h $(SRCDIR)/table.h
$(CC) -o $(OBJDIR)/db.o -c $(SRCDIR)/db.c $(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 $(OBJDIR)/pager.o: $(SRCDIR)/pager.c $(SRCDIR)/pit.h $(SRCDIR)/object.h $(SRCDIR)/table.h
$(CC) -o $(OBJDIR)/pager.o -c $(SRCDIR)/pager.c $(CC) -o $(OBJDIR)/pager.o -c $(SRCDIR)/pager.c

View File

@@ -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; 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.username, current_user(), sizeof(action.username) - 1);
strncpy(action.message, message, sizeof(action.message) - 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(); action_list();
return 1;
} }

View File

@@ -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 **arg = &argv[1];
char *file_name = pit_file_name(); char *file_name = pit_file_name();
int reply = 'Y'; int reply = 'Y';
@@ -64,7 +64,6 @@ int pit_init(char *argv[]) {
pit_db_initialize(); pit_db_initialize();
printf("Created empty %s\n", file_name); printf("Created empty %s\n", file_name);
} }
return 1;
} }
void pit_db_load() { void pit_db_load() {

9
src/help.c Normal file
View File

@@ -0,0 +1,9 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "pit.h"
void pit_help(char *argv[])
{
puts("pit: help is not implemented yet");
}

9
src/note.c Normal file
View File

@@ -0,0 +1,9 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "pit.h"
void pit_note(char *argv[])
{
puts("pit: note is not implemented yet");
}

View File

@@ -9,11 +9,6 @@ PTable tasks;
PTable notes; PTable notes;
PTable actions; PTable actions;
static int usage() {
printf("usage...\n");
return 1;
}
/* /*
** Suicide. ** Suicide.
*/ */
@@ -42,29 +37,23 @@ void perish(char *prefix)
exit(0); exit(0);
} }
void pit_status(char *argv[])
{
puts("pit: status is not implemented yet");
}
int main(int argc, char *argv[]) { 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 };
/*** if (argc == 1) argv[1] = "help";
int i; for(i = 0; i < ARRAY_SIZE(commands); i++) {
printf("argc: %d\n", argc); if (strstr(commands[i], argv[1]) == commands[i]) {
for(i = 0; i < argc; i++) { handlers[i](&argv[1]);
printf("argv[%d]: [%s]\n", i, argv[i]); return 1;
}
***/
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]);
} }
} }
return usage(); printf("invalid command: %s", argv[1]);
} return 0;
}

View File

@@ -11,13 +11,15 @@ typedef int bool;
#include "table.h" #include "table.h"
#include "pager.h" #include "pager.h"
/* #defines */
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#ifndef FALSE #ifndef FALSE
#define FALSE (0) #define FALSE (0)
#endif #endif
#ifndef TRUE #ifndef TRUE
#define TRUE (1) #define TRUE (1)
#endif #endif
#ifndef min #ifndef min
@@ -39,7 +41,20 @@ extern PTable notes;
extern PTable projects; extern PTable projects;
extern PTable tasks; 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_is_option(char **arg);
int pit_arg_option(char **arg); int pit_arg_option(char **arg);
char *pit_arg_string(char **arg, char *required); 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_date(char **arg, char *required);
time_t pit_arg_time(char **arg, char *required); time_t pit_arg_time(char **arg, char *required);
/* db.c */ /* Misc utilities */
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 */
void die(char *msg, ...); void die(char *msg, ...);
void perish(char *prefix); void perish(char *prefix);
char *mem2str(char *mem, int len); char *mem2str(char *mem, int len);

View File

@@ -96,7 +96,7 @@ static int delete_project(unsigned long number)
return 1; return 1;
} }
int pit_project(char *argv[]) void pit_project(char *argv[])
{ {
char **arg = &argv[1]; char **arg = &argv[1];
unsigned long number; unsigned long number;
@@ -129,6 +129,4 @@ int pit_project(char *argv[])
show_project(number); show_project(number);
} }
} }
return 1;
} }

View File

@@ -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 **arg = &argv[1];
char *name = NULL, *status = NULL, *priority = NULL; char *name = NULL, *status = NULL, *priority = NULL;
@@ -267,5 +267,4 @@ int pit_task(char *argv[])
} }
} }
} }
return 1;
} }