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

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

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 *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() {

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 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();
}
printf("invalid command: %s", argv[1]);
return 0;
}

View File

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

View File

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

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