From: Huck Boles Date: Mon, 2 Jan 2023 06:00:08 +0000 (-0600) Subject: Mon Jan 2 12:00:08 AM CST 2023 automatic backup X-Git-Url: https://git.huck.website/?a=commitdiff_plain;h=1bf713ece130566641a4c533c701dba1bebccc24;p=odot.git Mon Jan 2 12:00:08 AM CST 2023 automatic backup --- diff --git a/Makefile b/Makefile index c6e65dd..3254a15 100644 --- a/Makefile +++ b/Makefile @@ -5,14 +5,15 @@ DESTDIR = $(PREFIX)/bin 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 @@ -30,7 +31,8 @@ build: $(CFILE) $(HEADER) sqlite3.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) diff --git a/actions.c b/actions.c index 8fab80d..fa6d249 100644 --- a/actions.c +++ b/actions.c @@ -6,28 +6,27 @@ extern char *task, 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'); @@ -35,27 +34,43 @@ void done(sqlite3 *db){ 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; } @@ -81,5 +96,16 @@ void show(sqlite3 *db){ 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; } diff --git a/database.c b/database.c index 3490f13..0d44fd6 100644 --- a/database.c +++ b/database.c @@ -7,17 +7,8 @@ extern char *group, 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); @@ -29,10 +20,10 @@ void sqlcmd(sqlite3 *db, char *cmd, char action){ 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; } diff --git a/function.c b/function.c index ebdaf69..dabfd32 100644 --- a/function.c +++ b/function.c @@ -10,12 +10,14 @@ extern u_long hash; 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; } @@ -41,7 +43,7 @@ char *filepath(void){ if (err) error(2); } - strcat(db,dir); + sprintf(db,"%s/odot.db",dir); return db; } diff --git a/odot.c b/odot.c index 149bb1f..53fe21e 100644 --- a/odot.c +++ b/odot.c @@ -3,6 +3,7 @@ /* global variables */ int showdone = 0, showall = 0, + quiet = 0, exists = 0; u_long hash; @@ -10,14 +11,16 @@ 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 */ @@ -30,13 +33,37 @@ int main(int argc, char *argv[]){ 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; @@ -44,17 +71,24 @@ int main(int argc, char *argv[]){ 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; @@ -67,8 +101,8 @@ void parseopt(int n, char **args){ } } - /* 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++){ @@ -91,6 +125,10 @@ void error(int err){ case 4: fprintf(stderr,"Unknown Command\n"); help(); + break; + case 5: + fprintf(stderr,"Specify new group with -G\n"); + break; } exit(err); } @@ -100,12 +138,14 @@ void help(){ 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); diff --git a/odot.h b/odot.h index 3b4a610..e76bb07 100644 --- a/odot.h +++ b/odot.h @@ -1,23 +1,24 @@ +#include +#include #include #include #include -#include -#include -#include #include -#include +#include +#include #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 =" @@ -25,10 +26,11 @@ 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 *); @@ -38,12 +40,13 @@ void show(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 **);