PROG = odot
CC = gcc
-CFILE = $(PROG).c database.c
+CFILE = $(PROG).c database.c actions.c function.c
HEADER = $(PROG).h sqlite3.h
-OBJECTS = $(PROG).o database.o task.o sqlite3.o
+OBJECTS = $(PROG).o database.o actions.o function.o sqlite3.o
LDFLAGS = -L .
LDLIBS = -lpthread
-CFLAGS = -Wall -Wextra -O2
+CFLAGS = -O2
+WARNINGS = -Werror -Wall -Wextra -Wpedantic -Wconversion -Wformat=2 -Wformat-signedness -Wstrict-prototypes -Wshadow -Wno-unused
CPPFLAGS = -I .
-ALL_CFLAGS = $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(LDLIBS)
+ALL_CFLAGS = $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(LDLIBS) $(WARNINGS)
sql: sqlite3.c
$(CC) sqlite3.c $(CFLAGS) -c
$(CC) $(CFILE) sqlite3.c $(CFLAGS) -o $(PROG)
install: $(CFILE) $(HEADER) sqlite3.c
- $(CC) $(CFILE) sqlite3.c $(CFLAGS) -o $(DESTDIR)/$(PROG)
+ $(CC) $(CFILE) sqlite3.c $(CFLAGS) -o $(PROG)
+ install -CDTm 755 $(PROG) $(DESTDIR)/$(PROG)
debug: $(CFILE)
$(CC) $(CFILE) sqlite3.o $(CFLAGS) -ggdb3 -Og -o $(PROG)
extern u_long hash;
-extern int exists,showall,showdone;
+extern int exists,
+ showall,
+ showdone;
void newtask(sqlite3 *db){
char *cmd = malloc(MAXLINE*sizeof(char));
- sprintf(cmd,"%s '%s';",GETTASK,task);
- sqlcmd(db,cmd,'c');
-
if (exists == 0){
sprintf(cmd,"%s (%lu, '%s', '%s', 0);",INSERT,hash,task,group);
sqlcmd(db,cmd,'q');
+ } else {
+ printf("Task already exists\nMark as done? [y/(n)] ");
+ if (getchar() != 'n') done(db);
}
+ free(cmd);
return;
}
void done(sqlite3 *db){
char *cmd = malloc(MAXLINE*sizeof(char));
- sprintf(cmd,"%s '%s';",GETTASK,task);
- sqlcmd(db,cmd,'c');
-
-
if (exists == 1) {
sprintf(cmd,"%s %lu;",DONE,hash);
sqlcmd(db,cmd,'q');
sprintf(cmd,"%s %lu;", GETTASK,hash);
sqlcmd(db,cmd,'t');
- sprintf(cmd,"SELECT Done, Task FROM Tasks WHERE Hash = %lu",hash);
printf("\n");
+ sprintf(cmd,"SELECT Done, Task FROM Tasks WHERE Hash = %lu;",hash);
sqlcmd(db,cmd,'p');
+ } else {
+ printf("Task does not exist\nAdd task [y/n] ");
+ if (getchar() != 'n') newtask(db);
}
+
+ free(cmd);
return;
}
void update(sqlite3 *db){
+ /* terminates if newgroup isn't set*/
+ if (strcmp(newgroup,"") == 0) error(5);
+ char *cmd = malloc(MAXLINE*sizeof(char));
+ if (exists == 1) {
+ sprintf(cmd,"%s '%s' WHERE Hash = %lu;",CHANGEGROUP,newgroup,hash);
+ sqlcmd(db,cmd,'q');
+ strcpy(group,newgroup);
+ } else {
+ printf("Task does not exist\nAdd task [y/n] ");
+ if (getchar() != 'n') newtask(db);
+ }
+ free(cmd);
+ return;
}
void removetask(sqlite3 *db){
char *cmd = malloc(MAXLINE*sizeof(char));
- sprintf(cmd,"%s '%s';",GETTASK,task);
- sqlcmd(db,cmd,'c');
-
if (exists == 1){
sprintf(cmd,"%s %lu;",DELETE,hash);
sqlcmd(db,cmd,'c');
}
+ free(cmd);
return;
}
sqlcmd(db,PRINTALL,'p');
break;
}
+ free(cmd);
+ return;
+}
+
+void dbcheck(sqlite3 *db){
+ char *cmd = malloc(MAXLINE*sizeof(char));
+
+ sprintf(cmd,"%s '%s';",GETTASK,task);
+ sqlcmd(db,cmd,'c');
+
+ free(cmd);
return;
}
extern u_long hash;
extern int exists;
-sqlite3 *accessdb(char *file){
- sqlite3 *db;
-
- int err = sqlite3_open(file, &db);
- if (err) sqlerror(db);
-
- return db;
-}
-
void sqlcmd(sqlite3 *db, char *cmd, char action){
- int err;
+ int err = 0;
switch (action) {
case 'q':
err = sqlite3_exec(db,cmd,NULL,NULL,NULL);
err = sqlite3_exec(db,cmd,taskcallback,0,NULL);
break;
case 'c':
- err = sqlite3_exec(db,cmd,taskcallback,0,NULL);
+ err = sqlite3_exec(db,cmd,checkcallback,0,NULL);
break;
}
- if (err) sqlerror(db);
+ if (err < 0) sqlerror(db);
return;
}
extern int exists;
u_long genhash(void){
- char *tmp = malloc((strlen(task)+strlen(group)+1) * sizeof(char));
+ char *tmp = calloc((strlen(task)+strlen(group)+1), sizeof(char));
strcat(tmp,task);
strcat(tmp,group);
int h = 11235813;
- while (*tmp++) h = (~(h << 5) ^ *tmp);
+ for (int i = 0; i < strlen(tmp);i++)
+ h = ~((h << 5) ^ tmp[i]);
+
return h;
}
if (err) error(2);
}
- strcat(db,dir);
+ sprintf(db,"%s/odot.db",dir);
return db;
}
/* global variables */
int showdone = 0,
showall = 0,
+ quiet = 0,
exists = 0;
u_long hash;
char *group,
*newgroup,
*task,
- *action;
+ action;
int main(int argc, char *argv[]){
/* show help if no arguments unless its just showing tasks */
if (argc == 2 && strcmp(argv[1],"show") != 0) help();
+ int err = 0;
sqlite3 *db;
- int err = sqlite3_open(filepath(),&db);
+ err = sqlite3_open(filepath(),&db);
+ if (err < 0) sqlerror(db);
sqlcmd(db,BUILDTABLE,'c');
/* show all tasks if called alone */
group = calloc(MAXLINE,sizeof(char));
task = calloc(MAXLINE,sizeof(char));
- action = calloc(MAXLINE,sizeof(char));
newgroup = calloc(MAXLINE,sizeof(char));
parseopt(argc,argv);
hash = genhash();
- operate(db);
+ dbcheck(db);
+
+ switch (action){
+ case 'n':
+ newtask(db);
+ break;
+ case 'd':
+ done(db);
+ break;
+ case 'u':
+ update(db);
+ break;
+ case 'r':
+ removetask(db);
+ break;
+ case 's':
+ /* always gets shown unless quiet is set */
+ break;
+ case '?':
+ error(4);
+ }
+
+ /* show db after action */
+ if (quiet == 0){
+ show(db);
+ }
sqlite3_close(db);
return 0;
void parseopt(int n, char **args){
char c;
- while ((c = getopt(n,args,"g:G:adh")) != -1){
+ while ((c = getopt(n,args,"g:G:adhqV")) != -1){
switch (c) {
case 'h':
help();
break;
+ case 'V':
+ printf("v%s\n",VERSION);
+ exit(0);
+ break;
case 'a':
showall = 1;
break;
case 'd':
showdone = 1;
break;
+ case 'q':
+ quiet = 1;
+ break;
case 'g':
strcpy(group,optarg);
break;
}
}
- /* get subcommand */
- strcpy(action,args[optind]);
+ /* get subcommand char */
+ action = args[optind][0];
/* rest of arguments become task */
for (int j = optind + 1; j < n; j++){
case 4:
fprintf(stderr,"Unknown Command\n");
help();
+ break;
+ case 5:
+ fprintf(stderr,"Specify new group with -G\n");
+ break;
}
exit(err);
}
printf("\tSubcommands:\n");
printf("\tnew\tadd new task to database\n");
printf("\tdone\tmark task as done in database\n");
+ printf("\tupdate\tupdate task group, requires -G\n");
printf("\tshow\tshow tasks in database\n");
printf("\tremove\tremove task from database\t\n");
printf("\tOptions:\n");
printf("\t-a\tshow all groups\n");
printf("\t-d\talso show completed tasks\n");
printf("\t-g\tset group for task\n");
+ printf("\t-G\tnew group for update");
printf("\t-h\tshow this help\n");
exit(0);
+#include <dirent.h>
+#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <unistd.h>
-#include <time.h>
-#include <math.h>
#include <sys/stat.h>
-#include <dirent.h>
+#include <time.h>
+#include <unistd.h>
#include "sqlite3.h"
+#define VERSION "0.2.1"
#define MAXLINE 10000
-#define BUILDTABLE "CREATE TABLE IF NOT EXISTS Tasks (Hash int NOT NULL PRIMARY KEY, Task varchar(1000) NOT NULL, Type varchar(8), Done int NOT NULL DEFAULT 0);"
+#define BUILDTABLE "CREATE TABLE IF NOT EXISTS Tasks (Hash int NOT NULL PRIMARY KEY, Task varchar(10000) NOT NULL, Type varchar(32), Done int NOT NULL DEFAULT 0, Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, Duedate DATETIME);"
#define INSERT "INSERT INTO Tasks VALUES"
#define DELETE "DELETE FROM Tasks WHERE Hash ="
#define DONE "UPDATE Tasks SET Done = 1 WHERE Hash ="
#define GETTASK "SELECT Task, Type, Hash FROM Tasks WHERE Task ="
-#define CHANGEGROUP "UPDATE Tasks SET Group ="
+#define CHANGEGROUP "UPDATE Tasks SET Type ="
#define PRINT "SELECT Done, Type, Task FROM Tasks WHERE Done = 0 ORDER BY Type;"
#define PRINTALL "SELECT Done, Type, Task FROM Tasks ORDER BY Type;"
#define PRINTGROUP "SELECT Done, Task FROM Tasks WHERE Done = 0 AND Type ="
void error(int);
void help(void);
-void parseopt(int, char**);
+void parseopt(int, char **);
void operate(sqlite3 *);
/* action.c */
+void dbcheck(sqlite3 *db);
void newtask(sqlite3 *);
void done(sqlite3 *);
void update(sqlite3 *);
/* function.c */
char *filepath(void);
u_long genhash(void);
-void checksame(char *,char *);
+void checksame(char *, char *);
-/* functions for interfacing with database: database.c */
+/* functions for interfacing with sqlite database: database.c */
sqlite3 *accessdb(char *);
-void sqlcmd(sqlite3 *,char *,char);
+void sqlcmd(sqlite3 *, char *, char);
void sqlerror(sqlite3 *);
-int printcallback(void *,int,char **,char **);
-int groupcallback(void *,int,char **,char **);
-int taskcallback(void *,int,char **,char **);
+int printcallback(void *, int, char **, char **);
+int groupcallback(void *, int, char **, char **);
+int taskcallback(void *, int, char **, char **);
+int checkcallback(void *, int, char **, char **);