diff --git a/Makefile b/Makefile index 6f297c0..ee4301c 100644 --- a/Makefile +++ b/Makefile @@ -17,21 +17,25 @@ SRC = \ $(SRCDIR)/activity.c \ $(SRCDIR)/args.c \ $(SRCDIR)/db.c \ + $(SRCDIR)/pager.c \ $(SRCDIR)/pit.c \ $(SRCDIR)/project.c \ $(SRCDIR)/table.c \ $(SRCDIR)/task.c \ - $(SRCDIR)/user.c + $(SRCDIR)/user.c \ + $(SRCDIR)/util.c OBJ = \ $(OBJDIR)/activity.o \ $(OBJDIR)/args.o \ $(OBJDIR)/db.o \ + $(OBJDIR)/pager.o \ $(OBJDIR)/pit.o \ $(OBJDIR)/project.o \ $(OBJDIR)/table.o \ $(OBJDIR)/task.o \ - $(OBJDIR)/user.o + $(OBJDIR)/user.o \ + $(OBJDIR)/util.o APP = pit @@ -44,30 +48,36 @@ $(OBJDIR): $(APP): $(OBJ) $(CC) -o $(BINDIR)/$(APP) $(OBJ) -$(OBJDIR)/activity.o: $(SRCDIR)/activity.c $(SRCDIR)/pit.h $(SRCDIR)/models.h $(SRCDIR)/table.h +$(OBJDIR)/activity.o: $(SRCDIR)/activity.c $(SRCDIR)/pit.h $(SRCDIR)/object.h $(SRCDIR)/table.h $(CC) -o $(OBJDIR)/activity.o -c $(SRCDIR)/activity.c -$(OBJDIR)/args.o: $(SRCDIR)/args.c $(SRCDIR)/pit.h $(SRCDIR)/models.h $(SRCDIR)/table.h +$(OBJDIR)/args.o: $(SRCDIR)/args.c $(SRCDIR)/pit.h $(SRCDIR)/object.h $(SRCDIR)/table.h $(CC) -o $(OBJDIR)/args.o -c $(SRCDIR)/args.c -$(OBJDIR)/db.o: $(SRCDIR)/db.c $(SRCDIR)/pit.h $(SRCDIR)/models.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 -$(OBJDIR)/pit.o: $(SRCDIR)/pit.c $(SRCDIR)/pit.h $(SRCDIR)/models.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 + +$(OBJDIR)/pit.o: $(SRCDIR)/pit.c $(SRCDIR)/pit.h $(SRCDIR)/object.h $(SRCDIR)/table.h $(CC) -o $(OBJDIR)/pit.o -c $(SRCDIR)/pit.c -$(OBJDIR)/project.o: $(SRCDIR)/project.c $(SRCDIR)/pit.h $(SRCDIR)/models.h $(SRCDIR)/table.h +$(OBJDIR)/project.o: $(SRCDIR)/project.c $(SRCDIR)/pit.h $(SRCDIR)/object.h $(SRCDIR)/table.h $(CC) -o $(OBJDIR)/project.o -c $(SRCDIR)/project.c -$(OBJDIR)/table.o: $(SRCDIR)/table.c $(SRCDIR)/pit.h $(SRCDIR)/models.h $(SRCDIR)/table.h +$(OBJDIR)/table.o: $(SRCDIR)/table.c $(SRCDIR)/pit.h $(SRCDIR)/object.h $(SRCDIR)/table.h $(CC) -o $(OBJDIR)/table.o -c $(SRCDIR)/table.c -$(OBJDIR)/task.o: $(SRCDIR)/task.c $(SRCDIR)/pit.h $(SRCDIR)/models.h $(SRCDIR)/table.h +$(OBJDIR)/task.o: $(SRCDIR)/task.c $(SRCDIR)/pit.h $(SRCDIR)/object.h $(SRCDIR)/table.h $(CC) -o $(OBJDIR)/task.o -c $(SRCDIR)/task.c -$(OBJDIR)/user.o: $(SRCDIR)/user.c $(SRCDIR)/pit.h $(SRCDIR)/models.h $(SRCDIR)/table.h +$(OBJDIR)/user.o: $(SRCDIR)/user.c $(SRCDIR)/pit.h $(SRCDIR)/object.h $(SRCDIR)/table.h $(CC) -o $(OBJDIR)/user.o -c $(SRCDIR)/user.c +$(OBJDIR)/util.o: $(SRCDIR)/util.c $(SRCDIR)/pit.h $(SRCDIR)/object.h $(SRCDIR)/table.h + $(CC) -o $(OBJDIR)/util.o -c $(SRCDIR)/util.c + clean: rm -f $(OBJDIR)/*.o rm -f $(BINDIR)/$(APP) diff --git a/src/db.c b/src/db.c index a0b3975..85a2e77 100644 --- a/src/db.c +++ b/src/db.c @@ -2,53 +2,10 @@ #include #include #include -#include -#include #include "pit.h" #define PITFILE "~/.pit" -char *mem2str(char *mem, int len) { - char *str = malloc(len + 1); - memcpy(str, mem, len); - str[len] = '\0'; - - return str; -} - -static char *home_dir(char *username, int len) { - char *str = mem2str(username, len); - struct passwd *pw = getpwnam(str); - free(str); - - return (pw ? pw->pw_dir : NULL); -} - -static char *expand_path(char *path, char *expanded) { - if (!path || *path != '~') { - return path; - } else { - char *next = path + 1; - if (*next == '/') { /* Path without the username, i.e. ~/file */ - strcpy(expanded, getenv("HOME")); - strcat(expanded, next); - } else { /* Path with the username, i.e. ~username/file */ - char *slash = strchr(next, '/'); - if (!slash) { - slash = next + strlen(next); - } - char *home = home_dir(next, slash - next); - if (!home) { /* Ex. non-existent username. */ - perish(path); - } else { - strcpy(expanded, home); - strcat(expanded, slash); - } - } - } - - return expanded; -} static char *pit_file_name() { diff --git a/src/models.h b/src/object.h similarity index 100% rename from src/models.h rename to src/object.h diff --git a/src/pager.c b/src/pager.c new file mode 100644 index 0000000..c491b6e --- /dev/null +++ b/src/pager.c @@ -0,0 +1,4 @@ +#include +#include +#include +#include "pit.h" diff --git a/src/pit.h b/src/pit.h index 36c7a18..787a7a7 100644 --- a/src/pit.h +++ b/src/pit.h @@ -6,7 +6,7 @@ typedef unsigned long ulong; typedef unsigned char uchar; #include -#include "models.h" +#include "object.h" #include "table.h" /* Externals. */ @@ -22,27 +22,31 @@ extern PTable users; #define for_each_task(ptr) for (ptr = (PTask)tasks->slots; (ptr - (PTask)tasks->slots) / sizeof(PTask) < tasks->number_of_records; ptr++) #define for_each_user(ptr) for (ptr = (PUser)users->slots; (ptr - (PUser)users->slots) / sizeof(PUser) < users->number_of_records; ptr++) -/* Command line parsing. */ +/* args.c */ int pit_arg_is_option(char **arg); int pit_arg_option(char **arg); char *pit_arg_string(char **arg, char *required); ulong pit_arg_number(char **arg, char *required); time_t pit_arg_time(char **arg, char *required); -/* Database. */ +/* db.c */ int pit_init(char *argv[]); void pit_db_load(); void pit_db_save(); void pit_db_initialize(); -/* Models. */ +/* activity.c project.c task.c user.c */ int pit_project(char *argv[]); int pit_task(char *argv[]); char *pit_current_user(); PActivity pit_add_activity(ulong id, char *subject, char *message, ulong user_id); -/* Utilities. */ +/* util.c */ void die(char *msg, ...); void perish(char *prefix); +char *mem2str(char *mem, int len); +char *home_dir(char *username, int len); +char *expand_path(char *path, char *expanded); + #endif diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..4a62752 --- /dev/null +++ b/src/util.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include "pit.h" + +char *mem2str(char *mem, int len) { + char *str = malloc(len + 1); + memcpy(str, mem, len); + str[len] = '\0'; + + return str; +} + +char *home_dir(char *username, int len) { + char *str = mem2str(username, len); + struct passwd *pw = getpwnam(str); + free(str); + + return (pw ? pw->pw_dir : NULL); +} + +char *expand_path(char *path, char *expanded) { + if (!path || *path != '~') { + return path; + } else { + char *next = path + 1; + if (*next == '/') { /* Path without the username, i.e. ~/file */ + strcpy(expanded, getenv("HOME")); + strcat(expanded, next); + } else { /* Path with the username, i.e. ~username/file */ + char *slash = strchr(next, '/'); + if (!slash) { + slash = next + strlen(next); + } + char *home = home_dir(next, slash - next); + if (!home) { /* Ex. non-existent username. */ + perish(path); + } else { + strcpy(expanded, home); + strcat(expanded, slash); + } + } + } + + return expanded; +}