From 9c24e438b689af042672182f6d87f824ef435492 Mon Sep 17 00:00:00 2001 From: Anastasis Grammenos Date: Thu, 4 Oct 2018 15:46:42 +0300 Subject: More error/logs and fix failing test --- src/actionparser.c | 40 ++++++++++++++++++++++++---------------- src/actionparser.h | 3 ++- src/actions.c | 7 +++++-- src/ck.c | 3 +++ src/ckerrlog.c | 37 +++++++++++++++++++++++++++++++++---- src/ckerrlog.h | 25 ++++++++++++++----------- tests/add | 2 +- 7 files changed, 82 insertions(+), 35 deletions(-) diff --git a/src/actionparser.c b/src/actionparser.c index 82ae25e..deae72a 100644 --- a/src/actionparser.c +++ b/src/actionparser.c @@ -16,14 +16,17 @@ #include "actionparser.h" #include "ckutil.h" #include "ckinfo.h" +#include "ckerrlog.h" + +#define COMPONENT "action parser" /* accepted commands */ /* [0] is the count */ const char* const strINIT[] = {"3", "init", "i", "-i"}; const char* const strADD[] = {"3", "add", "a", "-a"}; -const char* const strDEL[] = {"3", "del", "d", "-d"}; +const char* const strDEL[] = {"4", "delete", "del","d", "-d"}; const char* const strEDIT[] = {"3", "edit", "e", "-e"}; -const char* const strLIST[] = {"5", "list", "l", "ls", "-l", "-ls"}; +const char* const strLIST[] = {"5", "list", "ls", "l", "-l", "-ls"}; const char* const strSEARCH[] = {"3", "search", "s", "-s"}; const char* const strHELP[] = {"5", "help", "h", "-?", "-h", "--help"}; const char* const strConfDir[] = {"4", "config", "conf", "c", "-c"}; @@ -236,7 +239,7 @@ void get_config(UserOpt *opt) { } char * defaultConf = ".ck"; char * home = getenv("HOME"); - opt->confDir = malloc(strlen(defaultConf) + 1 /* '/' */ + strlen(home)+1); + opt->confDir = malloc(strlen(defaultConf) + 1 /* '/' */ + strlen(home) + 1); str_join_dirname_with_basename(opt->confDir, home, defaultConf); // rewind @@ -244,20 +247,21 @@ void get_config(UserOpt *opt) { token = opts[pos]; } -void version() { +int version() { /* get first token */ next_token(); for (int i = 1; i < atoi(strVersion[0]) + 1; i++) { if (strcmp(token, strVersion[i]) == 0) { print_version(); - exit(0); + return 1; } } // rewind pos = pos - 1; token = opts[pos]; + return 0; } void get_possible_action_strings(char *dest, CkAction ckAction) { @@ -316,27 +320,28 @@ void print_parser_error(UserOpt *opt) { sprintf(errStr, "Usage: ........"); break; } - printf("Parsing error\n%s\n", errStr); + ERR("%s", errStr); } void print_parser_help() { char names[STR_S]; - printf("ck - the config keeper\n"); - printf("Usage:\n"); + ckhelp("ck - the config keeper"); + ckhelp("Usage:"); get_possible_action_strings(names, CKA_INIT); - printf("Initialize: \t%s\n", names); + ckhelp("Initialize: \t%s", names); get_possible_action_strings(names, CKA_ADD); - printf("Add config: \t%s\n", names); + ckhelp("Add config: \t%s", names); get_possible_action_strings(names, CKA_DEL); - printf("Delete config: \t%s\n", names); + ckhelp("Delete config: \t%s", names); get_possible_action_strings(names, CKA_EDIT); - printf("Edit config: \t%s\n", names); + ckhelp("Edit config: \t%s", names); get_possible_action_strings(names, CKA_LIST); - printf("List configs: \t%s\n", names); + ckhelp("List configs: \t%s", names); get_possible_action_strings(names, CKA_SEARCH); - printf("Search: \t%s\n", names); + ckhelp("Search: \t%s", names); get_possible_action_strings(names, CKA_HELP); - printf("Print this: \t%s\n", names); + ckhelp("Print this: \t%s", names); + report_help(); } ActionParseResult parse_action(int argc, char* argv[], UserOpt *opt) { @@ -352,10 +357,13 @@ ActionParseResult parse_action(int argc, char* argv[], UserOpt *opt) { next_token(); /* handle version info */ - version(); + if (version()) { + return APR_VERSION; + } /* figure what is the config file */ get_config(opt); + cklog("Found ck configuration directory in %s", opt->confDir); /* find the action */ determine_action(opt); diff --git a/src/actionparser.h b/src/actionparser.h index 01f32a5..84dc092 100644 --- a/src/actionparser.h +++ b/src/actionparser.h @@ -56,7 +56,8 @@ typedef enum ActionParserResults ActionParseResult; enum ActionParserResults { APR_OK, APR_ERR, - APR_HELP + APR_HELP, + APR_VERSION }; typedef struct UserOptions UserOpt; diff --git a/src/actions.c b/src/actions.c index 186bf9d..2a94427 100644 --- a/src/actions.c +++ b/src/actions.c @@ -11,11 +11,14 @@ #include #include +#define COMPONENT "action" + #include "actions.h" #include "actionhelper.h" #include "dblayer.h" #include "ckutil.h" #include "cklist.h" +#include "ckerrlog.h" int run_INIT(UserOpt * opt, Conf *conf) { UNUSED(conf); @@ -48,10 +51,10 @@ int run_ADD(UserOpt * opt, Conf *conf) { case ADD_NO_ERR: break; case ADD_ERR_WRONG_CONFIG: - PRINT_ERR("The config file specified doesn't exist or is a link."); + ERR("The config file specified doesn't exist or is a link."); goto error; case ADD_ERR_WRONG_FLAGS: - PRINT_ERR("Flags are: -s for secret and -p for primary."); + ERR("Flags are: -s for secret and -p for primary."); goto error; } add_print_opts(&addOpt); diff --git a/src/ck.c b/src/ck.c index 335d72f..4a0a626 100644 --- a/src/ck.c +++ b/src/ck.c @@ -46,6 +46,8 @@ int main(int argc, char *argv[]) { case APR_ERR: print_parser_error(&opt); goto error; + case APR_VERSION: + goto close; case APR_OK: break; } @@ -78,6 +80,7 @@ int main(int argc, char *argv[]) { error: free_user_opt(&opt); free_conf(&conf); + close: report_errlog(); return 0; } diff --git a/src/ckerrlog.c b/src/ckerrlog.c index 1be1c08..9e1fc51 100644 --- a/src/ckerrlog.c +++ b/src/ckerrlog.c @@ -15,11 +15,13 @@ #define COMPONENT "ckerrlog" -static char *err; -static char *log; static int loglvl; static char buf[STR_M]; +#define X(stream) static char *stream; +CK_STREAMS +#undef X + char * get_time() { time_t rawtime; struct tm * timeinfo; @@ -30,8 +32,9 @@ char * get_time() { } void initialize_errlog() { - err = NULL; - log = NULL; +#define X(stream) stream = NULL; + CK_STREAMS +#undef X loglvl = 0; cklog("%s Log session started", get_time()); } @@ -78,9 +81,35 @@ void cklog(char *txt, ...) { va_end(args); } +void ckhelp(char *txt, ...) { + va_list args; + va_start(args, txt); + add_help_with_delim("\n", txt, args); + va_end(args); +} + void ckerr_add_component(char *txt, ...) { va_list args; va_start(args, txt); add_err_with_delim(" ", txt, args); va_end(args); } + +void ckhelp_add_component(char *txt, ...) { + va_list args; + va_start(args, txt); + add_help_with_delim(" ", txt, args); + va_end(args); +} + +void report_err() { + printf("%s", err); + free(err); + err = NULL; +} + +void report_help() { + printf("%s", help); + free(help); + help = NULL; +} diff --git a/src/ckerrlog.h b/src/ckerrlog.h index b254d4b..be5257d 100644 --- a/src/ckerrlog.h +++ b/src/ckerrlog.h @@ -18,23 +18,26 @@ #define CK_STREAMS \ X(err) \ - X(log) + X(log) \ + X(help) void ckerr_add_component(char *txt, ...); -#define ERR(...) \ - ckerr_add_component("-[%s]", COMPONENT); \ - ckerr(__VA_ARGS__); - -typedef struct st_ErrLog ErrLog; -struct st_ErrLog { - char *err; - char *log; -}; - extern void initialize_errlog(); extern void report_errlog(); extern void ckerr(char *err, ...); extern void cklog(char *log, ...); +extern void ckhelp(char *log, ...); +extern void report_err(); +extern void report_help(); + +/**********/ +/* Macros */ +/**********/ + +#define ERR(...) \ + ckerr_add_component("Error in [%s]:", COMPONENT); \ + ckerr(__VA_ARGS__); \ + report_err(); #endif /* CKERRLOG_H */ diff --git a/tests/add b/tests/add index 5390325..b1c592f 100644 --- a/tests/add +++ b/tests/add @@ -34,7 +34,7 @@ function run_add { FOLDER=sec fi - if [ ! -f $TEST_LOCATION/$FOLDER/$1_$2 ]; then + if [ ! -f $TEST_LOCATION/$FOLDER/$1/$2 ]; then echo -e $ERROR$2" No move (add "$3" "$4")" exit 1 fi -- cgit v1.2.3