From a0db476bed7d7b814c426d23b55a74d585a6d8cb Mon Sep 17 00:00:00 2001 From: Anastasis Grammenos Date: Fri, 5 Oct 2018 18:16:16 +0300 Subject: errlog in actionparser and configparser --- src/actionparser.c | 170 +++++++++++++++++++++++++++-------------------------- src/actionparser.h | 2 +- src/actions.c | 4 +- src/ck.c | 15 ++--- src/ckerrlog.c | 35 ++++++++--- src/ckerrlog.h | 35 ++++++++--- src/confparser.c | 23 +++++--- 7 files changed, 164 insertions(+), 120 deletions(-) diff --git a/src/actionparser.c b/src/actionparser.c index d5ad071..266f771 100644 --- a/src/actionparser.c +++ b/src/actionparser.c @@ -10,7 +10,7 @@ * ----------------------------------------------------------------------------- * * The following code is resposinble for parsing the command line arguments - * and report any errors that might come up. + * and finding the correct ck action. * * -------------------------------------------------------------------------- */ #include "actionparser.h" @@ -18,19 +18,19 @@ #include "ckinfo.h" #include "ckerrlog.h" -ERRLOG(action parser); +ERRLOG(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[] = {"4", "delete", "del","d", "-d"}; -const char* const strEDIT[] = {"3", "edit", "e", "-e"}; -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"}; -const char* const strVersion[] = {"2", "version", "--version"}; +const char* const strINIT[] = {"3", "init", "i", "-i"}; +const char* const strADD[] = {"3", "add", "a", "-a"}; +const char* const strDEL[] = {"4", "delete", "del","d", "-d"}; +const char* const strEDIT[] = {"3", "edit", "e", "-e"}; +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"}; +const char* const strVersion[] = {"2", "version", "--version"}; const char* const strVerbose1[] = {"2", "--verbose", "-v"}; const char* const strVerbose2[] = {"2", "--Verbose", "-V"}; @@ -88,7 +88,7 @@ int parse_INIT(UserOpt *opt) { } fill_args_list(arg_num, opt); - return 1; + return 0; } int parse_ADD(UserOpt *opt) { @@ -101,7 +101,7 @@ int parse_ADD(UserOpt *opt) { int arg_num = optNum - pos; fill_args_list(arg_num, opt); - return 1; + return 0; } int parse_DEL(UserOpt *opt) { @@ -113,7 +113,7 @@ int parse_DEL(UserOpt *opt) { int arg_num = optNum - pos; fill_args_list(arg_num, opt); - return 1; + return 0; } int parse_EDIT(UserOpt *opt) { @@ -125,7 +125,7 @@ int parse_EDIT(UserOpt *opt) { int arg_num = optNum - pos; fill_args_list(arg_num, opt); - return 1; + return 0; } int parse_LIST(UserOpt *opt) { @@ -137,7 +137,7 @@ int parse_LIST(UserOpt *opt) { int arg_num = optNum - pos; fill_args_list(arg_num, opt); - return 1; + return 0; } int parse_SEARCH(UserOpt *opt) { @@ -149,7 +149,7 @@ int parse_SEARCH(UserOpt *opt) { int arg_num = optNum - pos; fill_args_list(arg_num, opt); - return 1; + return 0; } int parse_HELP(UserOpt *opt) { @@ -181,6 +181,7 @@ void determine_action(UserOpt *opt) { for (i = 1; i < atoi(str##ACTION[0]) + 1; i++) { \ if (strcmp(token, str##ACTION[i]) == 0) { \ opt->action = CKA_##ACTION; \ + LOG("Action to perform: %s", str##ACTION[1]); \ return; \ } \ } @@ -212,31 +213,29 @@ void free_user_opt(UserOpt *opt) { /* If the used has specified a config file other * than the default get it now */ -void get_config(UserOpt *opt) { +int get_config(UserOpt *opt) { /* get first token */ - next_token(); - - for (int i = 1; i < atoi(strConfDir[0]) + 1; i++) { - if (strcmp(token, strConfDir[i]) == 0) { - if (next_token() == -1) { - printf("Config needs a value\n"); - exit(1); - } - if (strcmp(token, ".") == 0){ - /* WHAT? */ - printf("Dot\n"); + if (next_token() != -1) { + for (int i = 1; i < atoi(strConfDir[0]) + 1; i++) { + if (strcmp(token, strConfDir[i]) == 0) { + if (next_token() == -1) { + ERR("Config needs a value"); + return -1; + } + char dir[STR_L]; + realpath(token, dir); + if (!util_is_dir(dir)) { + ERR("%s is not a directory", token); + return -1; + } + opt->confDir = malloc(strlen(dir) + 1); + strcpy(opt->confDir, dir); + // remove trailing `/` + if (opt->confDir[strlen(dir) - 1] == '/') { + opt->confDir[strlen(dir) - 1] = '\0'; + } + return 0; } - if (!util_is_dir(token)) { - printf("%s is not a directory\n", token); - exit(1); - } - opt->confDir = malloc(strlen(token) + 1); - strcpy(opt->confDir, token); - // remove trailing `/` - if (opt->confDir[strlen(token) - 1] == '/') { - opt->confDir[strlen(token) - 1] = '\0'; - } - return; } } char * defaultConf = ".ck"; @@ -247,46 +246,46 @@ void get_config(UserOpt *opt) { // rewind pos = pos - 1; token = opts[pos]; + return 0; } 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(); - return 1; + if (next_token() != -1) { + for (int i = 1; i < atoi(strVersion[0]) + 1; i++) { + if (strcmp(token, strVersion[i]) == 0) { + print_version(); + return 1; + } } + // rewind + pos = pos - 1; + token = opts[pos]; } - - // rewind - pos = pos - 1; - token = opts[pos]; return 0; } void verbose() { /* get first token */ - next_token(); - - for (int i = 1; i < atoi(strVerbose1[0]) + 1; i++) { - if (strcmp(token, strVerbose1[i]) == 0) { - errlog_set_verbose(1);; - return; + if (next_token() != -1) { + for (int i = 1; i < atoi(strVerbose1[0]) + 1; i++) { + if (strcmp(token, strVerbose1[i]) == 0) { + errlog_set_verbose(1); + return; + } } - } - for (int i = 1; i < atoi(strVerbose2[0]) + 1; i++) { - if (strcmp(token, strVerbose2[i]) == 0) { - errlog_set_verbose(2);; - return; + for (int i = 1; i < atoi(strVerbose2[0]) + 1; i++) { + if (strcmp(token, strVerbose2[i]) == 0) { + errlog_set_verbose(2); + return; + } } - } - // rewind - pos = pos - 1; - token = opts[pos]; + // rewind + pos = pos - 1; + token = opts[pos]; + } } void get_possible_action_strings(char *dest, CkAction ckAction) { @@ -369,49 +368,54 @@ void print_parser_help() { report_help(); } -ActionParseResult parse_action(int argc, char* argv[], UserOpt *opt) { +int parse_action(int argc, char* argv[], UserOpt *opt) { /* make empty user opt */ *opt = make_empty_user_opt(); - if (argc < 2) { - return APR_HELP; - } opts = argv; optNum = argc; - /* skip the program name */ next_token(); /* handle version info */ if (version()) { - return APR_VERSION; + return 1; } - /* set verbose level */ verbose(); - /* figure what is the config file */ - get_config(opt); - cklog("Found ck configuration directory in %s", opt->confDir); + if (get_config(opt)) { + return 1; + } + + /* If the remaining arguments are < 2 + * print help and exit */ + if (optNum - pos < 2) { + print_parser_help(); + return -1; + } /* find the action */ determine_action(opt); if (opt->action == CK_WRONG_ACTION) { opt->err = PERR_UNKONW_ACTION; - return APR_ERR; + print_parser_error(opt); + return -1; } if (opt->action == CKA_HELP) { - return APR_HELP; + print_parser_help(); + return -1; } // parse values - if (!parse_vals(opt)) { - return APR_ERR; + if (parse_vals(opt)) { + print_parser_error(opt); + return -1; } if (opt->err == PERR_NOERR) { - return APR_OK; - } - else { - return APR_ERR; + return 0; } + print_parser_error(opt); + return -1; } + diff --git a/src/actionparser.h b/src/actionparser.h index 84dc092..9e7209d 100644 --- a/src/actionparser.h +++ b/src/actionparser.h @@ -71,7 +71,7 @@ struct UserOptions { /* Parse cli args, fill UserOpt struct * and return the result enum */ -extern ActionParseResult parse_action(int argc, char* argv[], UserOpt *opt); +extern int parse_action(int argc, char* argv[], UserOpt *opt); extern void print_parser_error(); extern void print_parser_help(); extern void free_user_opt(UserOpt *opt); diff --git a/src/actions.c b/src/actions.c index 337911f..d5f9a29 100644 --- a/src/actions.c +++ b/src/actions.c @@ -11,8 +11,6 @@ #include #include -#define COMPONENT "action" - #include "actions.h" #include "actionhelper.h" #include "dblayer.h" @@ -20,6 +18,8 @@ #include "cklist.h" #include "ckerrlog.h" +ERRLOG(action); + int run_INIT(UserOpt * opt, Conf *conf) { UNUSED(conf); if (db_exists(opt)) { diff --git a/src/ck.c b/src/ck.c index 4a0a626..f9a2ad1 100644 --- a/src/ck.c +++ b/src/ck.c @@ -35,21 +35,16 @@ #include "ckerrlog.h" int main(int argc, char *argv[]) { - initialize_errlog(); + initialize_errlog(argc, argv); UserOpt opt; Conf conf = {.vc_dir = NULL, .scrt_dir = NULL}; /* get user opt */ - switch(parse_action(argc, argv, &opt)) { - case APR_HELP: - print_parser_help(); + int rc = parse_action(argc, argv, &opt); + if (rc < 0) { goto error; - case APR_ERR: - print_parser_error(&opt); - goto error; - case APR_VERSION: + } + else if (rc == 1) { goto close; - case APR_OK: - break; } /* If the action is init don't load the config, skip to running init*/ diff --git a/src/ckerrlog.c b/src/ckerrlog.c index 9a63ffd..357d112 100644 --- a/src/ckerrlog.c +++ b/src/ckerrlog.c @@ -14,7 +14,7 @@ #include "ckutil.h" #include "cklist.h" -ERRLOG(ckerrlog); +ERRLOG(logger); static int loglvl; static char buf[STR_M]; @@ -32,12 +32,13 @@ char * get_time() { return buf; } -void initialize_errlog() { -#define X(stream) stream = NULL; - CK_STREAMS -#undef X - loglvl = 0; - cklog("%s Log session started", get_time()); +void log_command(int argc, char* argv[]) { + char tmp[STR_L] = ""; + for(int i = 0; i < argc; i++) { + strcat(tmp, argv[i]); + strcat(tmp, " "); + } + LOG("Command issued: %s", tmp); } #define X(stream) \ @@ -66,6 +67,16 @@ CK_STREAMS CK_STREAMS #undef X +#define X(stream) \ + void ck## stream ##_with_delim(char *d, char *txt, ...) { \ + va_list args; \ + va_start(args, txt); \ + add_## stream ##_with_delim(d, txt, args); \ + va_end(args); \ + } +CK_STREAMS +#undef X + #define X(stream) \ void reset_## stream() { \ list_free(stream); \ @@ -111,3 +122,13 @@ void ckerr_add_component(char *txt, ...) { extern void errlog_set_verbose(int level) { loglvl = level; } + + +void initialize_errlog(int argc, char* argv[]) { +#define X(stream) stream = NULL; + CK_STREAMS +#undef X + loglvl = 0; + LOG("%s Log session started", get_time()); + log_command(argc, argv); +} diff --git a/src/ckerrlog.h b/src/ckerrlog.h index d3d7243..2506e99 100644 --- a/src/ckerrlog.h +++ b/src/ckerrlog.h @@ -19,19 +19,23 @@ #define CK_STREAMS \ X(err) \ X(log) \ - X(help) + X(logv) \ + X(help) -void ckerr_add_component(char *txt, ...); - -extern void initialize_errlog(); +extern void initialize_errlog(int argc, char* argv[]); extern void report_errlog(); extern void errlog_set_verbose(int level); extern void ckerr(char *err, ...); extern void cklog(char *log, ...); extern void ckhelp(char *log, ...); -extern void report_err(); -extern void report_help(); + +#define X(stream) \ + extern void ck## stream(char *log, ...); \ + void ck## stream ##_with_delim(char *d, char *txt, ...); \ + void report_## stream(); +CK_STREAMS +#undef X /**********/ /* Macros */ @@ -41,9 +45,22 @@ extern void report_help(); #define ERRLOG(_COMPONENT) \ static const char COMPONENT[STR_S] = #_COMPONENT -#define ERR(...) \ - ckerr("Error in [%s]:", COMPONENT); \ - ckerr(__VA_ARGS__); \ +#define ERR(...) \ + ckerr_with_delim("\n", "Error in [%s]:", COMPONENT); \ + ckerr(__VA_ARGS__); \ + cklog_with_delim(" ", "ERROR: [%s]", COMPONENT); \ + cklog(__VA_ARGS__); \ report_err(); +/* Print help message */ +#define HELP(...) \ + ckhelp(__VA_ARGS__); \ + report_help(); + +/* Log event */ +#define LOG(...) \ + cklog_with_delim(" ", "[%s]", COMPONENT); \ + cklog(__VA_ARGS__); + +#define LOG_V(...) #endif /* CKERRLOG_H */ diff --git a/src/confparser.c b/src/confparser.c index 7f61037..64bfe34 100644 --- a/src/confparser.c +++ b/src/confparser.c @@ -14,7 +14,9 @@ #include "ckutil.h" #include "confparser.h" +#include "ckerrlog.h" +ERRLOG(configfile); const char * const CONFIG_NAME = "/ckrc"; @@ -86,13 +88,19 @@ ConfigParserResult parse(Conf *conf, UserOpt *opt) { if (!util_is_dir(matched)) { \ return CPR_WRONG_##var; \ } \ + LOG("Found %s: %s", name, conf->var); \ break; CONFIG_VARIABLES_TABLE #undef X case CV_NO_VAL_OR_COMMENT: break; default: - printf("%s:\n%s\n", "Config error in line", line); + if (line[strlen(line) - 1] == '\n') { + ERR("Config error in line: %.*s", strlen(line) - 1, line); + } + else { + ERR("Config error in line: %s", line); + } } } #define X(var, str) \ @@ -107,21 +115,20 @@ ConfigParserResult parse(Conf *conf, UserOpt *opt) { } int config_file_parse(Conf *conf, UserOpt *opt) { + LOG("Using '%s' for ck configuration directory", opt->confDir); switch (parse(conf, opt)) { #define X(var,str,name) \ case CPR_WRONG_##var: \ - printf("--[ Config error ]--\n" \ - "%s: %s\n" \ - "defined in config does not exist\n", name, conf->var); \ + ERR("%s %s defined in config doesn't exist", name, conf->var); \ return 0; \ break; CONFIG_VARIABLES_TABLE #undef X case CPR_NO_CONFIG_FILE: - printf("The config file specified could not be found\n"); + ERR("The config file specified could not be found"); return 0; case CPR_WRONG_CONFIG: - printf("Config help\n"); + ERR("Config help\n"); case CPR_OK: return 1; } @@ -139,13 +146,13 @@ void free_conf(Conf *conf) { int init_create_config_file(UserOpt *opt) { char absVCdir[STR_L]; if (!util_file_exists(list_get_at(opt->args, 0), absVCdir)) { - printf("Version control directory: %s\ndoes not exist.\n", list_get_at(opt->args, 0)); + ERR("Version control directory: %s\ndoes not exist.\n", list_get_at(opt->args, 0)); return 1; } char absSRdir[STR_L]; if (!util_file_exists(list_get_at(opt->args, 1), absSRdir)) { - printf("Secret directory: %s\ndoes not exist.\n", list_get_at(opt->args, 1)); + ERR("Secret directory: %s\ndoes not exist.\n", list_get_at(opt->args, 1)); return 1; } -- cgit v1.2.3