From 433244a63981d7bb3d3a6aef47816e0830ce428a Mon Sep 17 00:00:00 2001 From: Hassan Afify Date: Fri, 17 Apr 2020 07:15:48 +0300 Subject: [PATCH] [refactor] validate_dyn, pledge, N & n options - add: - validate_dyn function - OpenBSD pledge - N n options - remove: - extra lines - c option - math.h - sys/stat.h - sys/types.h - enum fill - replace: - print_t return void - struct Task and Task_t name to stack max 256 --- Makefile | 4 +- README.md | 5 +- config.def.h | 3 - splanner.1 | 11 ++- splanner.c | 286 ++++++++++++++++++++++++++++++++++------------------------- util.h | 5 +- 6 files changed, 177 insertions(+), 137 deletions(-) diff --git a/Makefile b/Makefile index 794c322..7a4a65b 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ install: splanner chmod 644 ${DESTDIR}${MANPREFIX}/man1/splanner.1 uninstall: - rm -f ${DESTDIR}${PREFIX}/bin/splanner - rm -f ${DESTDIR}${MANPREFIX}/man1/splanner.1 + rm -f ${DESTDIR}${PREFIX}/bin/splanner\ + ${DESTDIR}${MANPREFIX}/man1/splanner.1 .PHONY: all options clean dist install uninstall diff --git a/README.md b/README.md index 954b4d7..813074a 100644 --- a/README.md +++ b/README.md @@ -18,14 +18,15 @@ $ splanner Options ------- ```sh -$ splanner [-Aacv] +$ splanner [-AaNnv] $ man splanner ``` | option | description | |:------:|:---------------------------------------------------------| | `-A` | print all tasks, 12-hour clock format. | | `-a` | print all tasks. | -| `-c` | use cache file. | +| `-N` | print all upcoming tasks, 12-hour clock format. | +| `-n` | print all upcoming tasks. | | `-v` | print version. | Configuration diff --git a/config.def.h b/config.def.h index c4c74b4..e6bbd35 100644 --- a/config.def.h +++ b/config.def.h @@ -3,9 +3,6 @@ #ifndef CONFIG_H #define CONFIG_H -/* cache location */ -static const char cache_dir[] = "/tmp"; - static Task config_tasks[] = { /* name, day, month, start(hour, min) duration */ {"fixed0", e_day, e_mon, {5, 16}, 30}, diff --git a/splanner.1 b/splanner.1 index 6d43bbe..40cb3c6 100644 --- a/splanner.1 +++ b/splanner.1 @@ -3,7 +3,7 @@ splanner \- simple personal planner. .SH SYNOPSIS .B splanner -.RB [ \-Aacv ] +.RB [ \-AaNnv ] .SH DESCRIPTION splanner is a simple personal planer for unix-like systems. Show tasks of each day, next task & day tasks. .P @@ -16,11 +16,14 @@ print all tasks, 12-hour clock format. .B \-a print all tasks. .TP -.B \-c -use cache file. +.B \-N +print all upcoming tasks, 12-hour clock format. +.TP +.B \-n +print all upcoming tasks. .TP .B \-v -print version information to standard output, then exits. +print version. .SH USAGE .TP .B splanner diff --git a/splanner.c b/splanner.c index 8d0a680..87325d8 100644 --- a/splanner.c +++ b/splanner.c @@ -2,22 +2,19 @@ * See LICENSE file for copyright and license details. */ -#include #include #include #include -#include -#include #include #include /* typedef */ -enum { after_last = 25, fill = -2, dyn = -1 }; +enum { after_last = 25, dyn = -1 }; enum week { Sun, Mon, Tue, Wed, Thu, Fri, Sat, e_day }; enum year { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, e_mon }; typedef struct { - char *name; + char name[256]; enum week day; enum year mon; int st[2]; @@ -25,7 +22,7 @@ typedef struct { } Task; typedef struct { - char *name; + char name[256]; time_t st; time_t end; } Task_t; @@ -33,43 +30,54 @@ typedef struct { #include "config.h" #include "util.h" -/* global variables */ -size_t config_l = LEN(config_tasks); - /* function declarations */ static void usage(void); -static time_t convert_hour_min_timet(const int*); static char *convert_timet_to_str(const time_t, const char); +static time_t convert_hour_min_timet(const int*); static time_t adj_st(const int*, const time_t); static time_t adj_end(const time_t, int); static int accepted_task(Task); - -static void gather_info(size_t*, size_t*); -static Task *create_acct(size_t, size_t*); +static void gather_info(size_t*, size_t*, size_t); +static Task *create_acct(size_t, size_t*, size_t); static Task_t *create_rett(Task*, size_t*); -static void validate_fixed(size_t*, size_t*, Task_t*, Task*); - -static int print_t(const char, const char); +static int validate_fixed(size_t*, size_t*, Task_t*, Task*); +static int validate_dyn(size_t*, Task_t*, Task*); +static void print_t(const char); /* function implementations */ static void usage(void) { - die("usage: splanner -[Aacv]\noptions:\n \ + die("usage: splanner -[AaNnv]\noptions:\n \ -A print all tasks, 12-hour clock format.\n \ -a print all tasks.\n \ -N print all upcoming tasks, 12-hour clock format.\n \ -n print all upcoming tasks.\n \ --c use cache file.\n \ -v print version."); } +static char * +convert_timet_to_str(const time_t input_time, const char option) +{ + char *result; + size_t result_size = (size_t)32; + struct tm new_time = *localtime(&input_time); + + result = ecalloc(result_size, sizeof(char)); + if ((option == 'A' || option == 'N') && (new_time.tm_hour > 12)){ + new_time.tm_hour = new_time.tm_hour - 12; + } + (void)snprintf(result, result_size, "%.2d:%.2d", + new_time.tm_hour, new_time.tm_min); + return result; +} + static time_t convert_hour_min_timet(const int *arr) { const int hours = arr[0]; const int minutes = arr[1]; - time_t now ; + time_t now; time_t converted; struct tm tmnow; @@ -78,44 +86,22 @@ convert_hour_min_timet(const int *arr) tmnow.tm_hour = hours; tmnow.tm_min = minutes; tmnow.tm_sec = 0; - tmnow.tm_mday = tmnow.tm_mday; - tmnow.tm_mon = tmnow.tm_mon; - tmnow.tm_year = tmnow.tm_year; converted = mktime(&tmnow); return converted; } -static char -*convert_timet_to_str(const time_t input_time, const char option) -{ - char *result; - size_t result_size = (size_t)32; - struct tm new_time = *localtime(&input_time); - - result = ecalloc(result_size, sizeof(char)); - if ((option == 'A') && (new_time.tm_hour > 12)){ - new_time.tm_hour = new_time.tm_hour - 12; - } - (void)snprintf(result, result_size, "%.2d:%.2d", - new_time.tm_hour, new_time.tm_min); - return result; -} - static time_t adj_st(const int *config_start_time_array, time_t last_task_endtime) { time_t result; int config_hours = config_start_time_array[0]; - /* after_last */ if (config_hours == (int)after_last) { result = last_task_endtime; - /* fixed start */ } else { result = convert_hour_min_timet(config_start_time_array); } - return result; } @@ -153,8 +139,10 @@ accepted_task(Task tested_task) } static void -gather_info(size_t *acctl, size_t *fixedl) +gather_info(size_t *acctl, size_t *fixedl, size_t config_l) { + /* count accepted tasks */ + /* count fixed tasks */ size_t i; for (i=0; i 0){ - if((acct[i-1].dur == (int)dyn) && + if ((acct[i-1].dur == (int)dyn) && (acct[i].st[0] == (int)after_last)){ die("[FAILED] %s (dyn) --> %s (dyn)\ndynamic duration must be followed by fixed task start time.", acct[i-1].name, acct[i].name); } if (acct[i-1].dur == (int)dyn) { rett[i-1].end = rett[i].st; - } } @@ -229,19 +218,22 @@ static Task_t return rett; } -static void +static int validate_fixed(size_t *fixedl, size_t *acctl, Task_t *rett, Task *acct) { Task *fixed; int *btwa; int *btwp; - size_t btwal; - size_t i, a, x; - int tdur = 0; + double dfix; int dmin = 60 * 24; int dsec = dmin * 60; - double dfix; +// int tfix = 0; + int fail = 0; /* to print all found errors */ + int fixdie = 0; + int tdur = 0; + size_t btwal; + size_t i, a, x; fixed = ecalloc(*fixedl, sizeof(Task)); a = 0; @@ -273,13 +265,13 @@ validate_fixed(size_t *fixedl, size_t *acctl, Task_t *rett, Task *acct) } /* show fixes tasks and possible duration between them */ - for (i=0; i %s %d\n", fixed[i].name, fixed[i+1].name, btwa[i]); - } - } - +// for (i=0; i %s %d\n", fixed[i].name, fixed[i+1].name, btwa[i]); +// } +// } // printf("total need fix = %d\n", tfix); + /* calculate between each 2 fixed start times */ /* for each 2 calculate max duration between them */ for (i=(size_t)1; i<*fixedl; i++){ @@ -289,23 +281,25 @@ validate_fixed(size_t *fixedl, size_t *acctl, Task_t *rett, Task *acct) btwp[i-1] = (int)dfix; } - /* calculate actual sum duration of between tasks */ - /* compare 2 arrays btwa & btwp fail if not equal */ -// for (i=0; i 0) + return -1; + + return 0; } static int -print_t(const char print_m, const char cache_m) +validate_dyn(size_t *acctl, Task_t *rett, Task *acct) +{ + /* + * case 0: between 2 fixes and no dynamic -> less than possible + * case 1: dynamic duration is 0 or less + */ + + const int dmin = 60 * 24; + const int dsec = dmin * 60; + time_t tduration = 0; + int status = 0; /* to print all found errors */ + size_t i; + + for (i=0; i<*acctl; i++) { + if (i < (*acctl)-1){ + tduration = (rett[i+1].st - rett[i].st)/60; + } else if (i == (*acctl)-1) { + tduration = (rett[0].st + dsec - rett[i].st)/60; + } + + if ((tduration <= 0) || + (acct[i].dur != (int)dyn && (int)tduration != acct[i].dur)) { + printf("[fix] %s duration is %ld min\n", + rett[i].name, (long)tduration); + status = -1; + } + } + return status; +} + +static void +print_t(const char print_m) { Task *acct; Task_t *rett; - + char *st_str; + char *end_str; size_t *acctl; size_t *fixedl; size_t *tnmax; - int dmin = 60 * 24; - int dsec = dmin * 60; - int tfix = 0; - int fixdie = 0; - size_t i; - /* count accepted tasks */ - /* count fixed tasks */ + const int dmin = 60 * 24; + const int dsec = dmin * 60; + const time_t now_seconds = time(NULL); + size_t config_l = LEN(config_tasks); + time_t tduration = 0; + acctl = ecalloc((size_t)1, sizeof(size_t)); fixedl = ecalloc((size_t)1, sizeof(size_t)); tnmax = ecalloc((size_t)1, sizeof(size_t)); - gather_info(acctl, fixedl); + gather_info(acctl, fixedl, config_l); + acct = create_acct(*acctl, tnmax, config_l); + rett = create_rett(acct, acctl); - /* copy accepted from config_tasks */ - /* set max task name length */ - acct = create_acct(*acctl, tnmax); + if (validate_dyn(acctl, rett, acct) < 0) + die("validation dynamic"); - /* copy accepted tasks to returned Task_t array */ - rett = create_rett(acct, acctl); + if (validate_fixed(fixedl, acctl, rett, acct) < 0) + die("validation fixed"); for (i=0; i<*acctl; i++) { - - char *st_str = convert_timet_to_str(rett[i].st, 'A'); - char *end_str = convert_timet_to_str(rett[i].end, 'A'); - int tduration = 0; - char *fixmsg; - char *fixmsge; + st_str = convert_timet_to_str(rett[i].st, print_m); + end_str = convert_timet_to_str(rett[i].end, print_m); if (i < (*acctl)-1){ tduration = (rett[i+1].st - rett[i].st)/60; @@ -372,51 +401,62 @@ print_t(const char print_m, const char cache_m) tduration = (rett[0].st + dsec - rett[i].st)/60; } - tfix += tduration; - /* case between 2 fixes and no dynamic less than possible */ - /* case dynamic duration is 0 or less */ - if ( - (tduration <= 0) || - (acct[i].dur != dyn && tduration != acct[i].dur) - ) { - - fixdie = 1; - fixmsg = "\033[33m"; - fixmsge = " <---- [FIX]\033[0m"; -// printf("[\033[33mfix\033[0m] %s duration is %d min\n", -// rett[i].name, tduration); - - } else { - fixmsg = ""; - fixmsge = ""; + if (print_m == 'a' || print_m == 'A') { + printf("%*s %s -> %s\t%ld\n", + ~(int)*tnmax, + rett[i].name, + st_str, + end_str, + (long)tduration); + + } else if (print_m == 'n' || print_m == 'N' || print_m == 's') { + if (rett[i].end > now_seconds){ + printf("%*s %s -> %s\t%ld\n", + ~(int)*tnmax, + rett[i].name, + st_str, + end_str, + (long)tduration); + if (print_m == 's') { + free(st_str); + free(end_str); + break; + } + } } - printf("%s%*s %s -> %s\t%d %s\n",fixmsg, ~(int)*tnmax, - rett[i].name, - st_str, - end_str, - tduration, - fixmsge); free(st_str); free(end_str); - free(rett[i].name); } - validate_fixed(fixedl, acctl, rett, acct); - + free(acct); + free(rett); free(acctl); free(fixedl); free(tnmax); - free(acct); - free(rett); - return 0; } int main(int argc, char *argv[]) { +#ifdef __OpenBSD__ + if (pledge("stdio", NULL) == -1) + die("pledge"); +#endif /* __OpenBSD__ */ if (argc == 1) { - FAIL_IF(print_t('n', 'r') < 0, "Error Calculating.\n"); + print_t('s'); + } else if (argc == 2) { + if (strcmp("-v", argv[1]) == 0){ + die("splanner-"VERSION); + } else if ( + strcmp("-a", argv[1]) == 0 || + strcmp("-A", argv[1]) == 0 || + strcmp("-N", argv[1]) == 0 || + strcmp("-n", argv[1]) == 0) { + print_t(argv[1][1]); + } else { + usage(); + } } else { usage(); } diff --git a/util.h b/util.h index e44f747..6fea049 100644 --- a/util.h +++ b/util.h @@ -2,10 +2,9 @@ #define MAX(A, B) ((A) > (B) ? (A) : (B)) #define MIN(A, B) ((A) < (B) ? (A) : (B)) -#define LEN(A) (sizeof(A)/sizeof(A[0])) +#define LEN(A) (sizeof(A)/sizeof(A[0])) #define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B)) -#define FAIL_IF(EXP, MSG) {if(EXP){fprintf(stderr, "[\033[31mFAILED %d\033[0m] %s\n", __LINE__, MSG);exit(EXIT_FAILURE);}} -#define WARN(EXP, MSG) {if(EXP){printf("[\033[33mWARN %d\033[0m] %s\n", __LINE__, MSG);return -1;}} +#define FAIL_IF(EXP, MSG) {if(EXP){fprintf(stderr, "[\033[31mFAILED %d\033[0m] %s\n", __LINE__, MSG);exit(EXIT_FAILURE);}} void *ecalloc(size_t, size_t); void die(const char *fmt, ...); -- 2.11.4.GIT