#include #include #include #include "landerctl.h" const char *cfg_file_name = ".landerrc"; const char *usage = "%s [-SPFsv] [-c CONFIG_FILE] [ARGS]\n"; int landerctl_parse_config(landerctl_cfg *cfg, const char *path) { // Parse default file location instead if (path == NULL) { const char *home_dir = getenv("HOME"); if (home_dir == NULL) { path = cfg_file_name; } else { // This is a blatant memleak if a custom config file is set, but it really // doesn't matter for a short-lived CLI tool char *buf = malloc(strlen(home_dir) + strlen(cfg_file_name) + 2); sprintf(buf, "%s/%s", home_dir, cfg_file_name); path = buf; } } char *err_msg = NULL; landerctl_cfg_err parse_res = landerctl_cfg_parse(cfg, path); switch (parse_res) { case landerctl_cfg_err_ok: break; case landerctl_cfg_err_not_found: err_msg = "Config file not found"; break; case landerctl_cfg_err_invalid: err_msg = "Invalid config file"; break; case landerctl_cfg_err_incomplete: err_msg = "Incomplete config file"; break; } if (err_msg != NULL) { fprintf(stderr, "%s\n", err_msg); return 1; } return 0; } int landerctl_parse_args(landerctl_args *args, int argc, char **argv) { optind = 1; int c; const char *cfg_path = NULL; while ((c = getopt(argc, argv, "SPFsvc:")) != -1) { switch (c) { case 'S': args->mode = landerctl_mode_short; break; case 'P': args->mode = landerctl_mode_paste; break; case 'F': args->mode = landerctl_mode_file; break; case 's': args->secure = true; break; case 'v': args->verbose = true; break; case 'c': cfg_path = optarg; break; case '?': fprintf(stderr, usage, argv[0]); return 2; } } int res; if ((res = landerctl_parse_config(&args->cfg, cfg_path))) { return res; } args->args.arr = &argv[optind]; args->args.len = argc - optind; return 0; }