/* BSD-2-Clause license * * Copyright (c) 2018-2023 NST , sss . * */ #include #include #include #include #include #include #include #include "config_file.h" #include "globals.h" static void print_help(const char *prog_path) { printf("Usage: %s [-c:d....]\nOptions:\n" "\t-c\t--config_file\t\"Use settings from" " specified file instead of default config file\"\n" "\t-d\t--daemon\t\"Daemon mode (fork to background)\"\n" "\t--http_port\t\"Set port for http/ws server\"\n" "\t--help\t\"Print tthis help message\"\n", prog_path); exit(EXIT_FAILURE); } void handle_cmdline_args(int argc, char **argv) { int c; char config_file_path[_POSIX_PATH_MAX] = {0}; bool daemon = false, wrong_options = false; while (1) { int option_index = 0; static struct option long_options[] = { {"config_file", required_argument, 0, 0}, {"daemon", no_argument, 0, 0}, {"ws_port", required_argument, 0, 0}, {"help", no_argument, 0, 0} }; c = getopt_long( argc, argv, "c:dp:", long_options, &option_index); if (c == -1) break; switch (c) { case 0: { if (!strcmp(long_options[option_index].name, "config_file") && optarg) { strncpy(config_file_path, optarg, _POSIX_PATH_MAX - 1); config_file_path[_POSIX_PATH_MAX - 1] = 0; } else if (!strcmp( long_options[option_index].name, "ws_port") && optarg) g_globals.settings.ws_port = atoi(optarg); else if (!strcmp( long_options[option_index].name, "daemon")) daemon = true; else if (!strcmp( long_options[option_index].name, "help")) print_help(argv[0]); } break; case 'c': { if (optarg) //clang static analyzer... { strncpy(config_file_path, optarg, _POSIX_PATH_MAX - 1); config_file_path[_POSIX_PATH_MAX - 1] = 0; } } break; case 'p': { if (optarg) //clang static analyzer... { g_globals.settings.ws_port = atoi(optarg); } } break; case 'd': { daemon = true; } break; case '?': { wrong_options = true; } break; default: break; } } if (wrong_options) { print_help(argv[0]); } else { g_globals.settings.daemon = daemon; find_config_file(config_file_path); } }