main: add -c,--config command line option

This option tells f00bar to load the configuration from this file
instead of searching the default locations.
This commit is contained in:
Daniel Eklöf 2019-05-09 19:31:34 +02:00
parent f6ed729cf3
commit 937a684f9e
2 changed files with 28 additions and 5 deletions

View file

@ -8,6 +8,9 @@ f00bar - modular status panel for X11 and Wayland
# OPTIONS # OPTIONS
*-c, --config=FILE*
Use an alternative configuration file instead of the default one.
*-C, --validate* *-C, --validate*
Verify the configuration and then quit. If no errors are detected, Verify the configuration and then quit. If no errors are detected,
nothing is printed and the exit code is 0. If there are errors, nothing is printed and the exit code is 0. If there are errors,

30
main.c
View file

@ -124,7 +124,8 @@ print_usage(const char *prog_name)
printf("Usage: %s [OPTION]...\n", prog_name); printf("Usage: %s [OPTION]...\n", prog_name);
printf("\n"); printf("\n");
printf("Options:\n"); printf("Options:\n");
printf(" -C,--validate verify configuration then quit\n" printf(" -c,--config=FILE alternative configuration file\n"
" -C,--validate verify configuration then quit\n"
" -v,--version print f00sel version and quit\n"); " -v,--version print f00sel version and quit\n");
} }
@ -134,6 +135,7 @@ main(int argc, char *const *argv)
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
static const struct option longopts[] = { static const struct option longopts[] = {
{"config", required_argument, 0, 'c'},
{"validate", no_argument, 0, 'C'}, {"validate", no_argument, 0, 'C'},
{"version", no_argument, 0, 'v'}, {"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
@ -141,13 +143,29 @@ main(int argc, char *const *argv)
}; };
bool verify_config = false; bool verify_config = false;
char *config_path = NULL;
while (true) { while (true) {
int c = getopt_long(argc, argv, ":Cvh", longopts, NULL); int c = getopt_long(argc, argv, ":c:Cvh", longopts, NULL);
if (c == -1) if (c == -1)
break; break;
switch (c) { switch (c) {
case 'c': {
struct stat st;
if (stat(optarg, &st) == -1) {
LOG_ERRNO("%s: invalid configuration file", optarg);
return EXIT_FAILURE;
} else if (!S_ISREG(st.st_mode)) {
LOG_ERR("%s: invalid configuration file: not a regular file",
optarg);
return EXIT_FAILURE;
}
config_path = strdup(optarg);
break;
}
case 'C': case 'C':
verify_config = true; verify_config = true;
break; break;
@ -188,10 +206,12 @@ main(int argc, char *const *argv)
return 1; return 1;
} }
char *config_path = get_config_path();
if (config_path == NULL) { if (config_path == NULL) {
LOG_ERR("could not find a configuration (see man 5 f00bar)"); config_path = get_config_path();
return 1; if (config_path == NULL) {
LOG_ERR("could not find a configuration (see man 5 f00bar)");
return 1;
}
} }
struct bar *bar = load_bar(config_path); struct bar *bar = load_bar(config_path);