/* * This file was AUTO-GENERATED by pypgmopts. * * https://github.com/johndoe31415/pypgmopts * * Do not edit it by hand, your changes will be overwritten. * * Generated at: 2019-10-25 11:06:30 */ #include #include #include #include #include #include #include #include #include "argparse_server.h" static enum argparse_server_option_t last_parsed_option; static char last_error_message[256]; static const char *option_texts[] = { [ARG_SERVER_PORT] = "-p / --port", [ARG_SERVER_SILENT] = "-s / --silent", [ARG_SERVER_VERBOSE] = "-v / --verbose", [ARG_SERVER_FILENAME] = "filename", }; enum argparse_server_option_internal_t { ARG_SERVER_PORT_SHORT = 'p', ARG_SERVER_SILENT_SHORT = 's', ARG_SERVER_VERBOSE_SHORT = 'v', ARG_SERVER_PORT_LONG = 1000, ARG_SERVER_SILENT_LONG = 1001, ARG_SERVER_VERBOSE_LONG = 1002, ARG_SERVER_FILENAME_LONG = 1003, }; static void errmsg_callback(const char *errmsg, ...) { va_list ap; va_start(ap, errmsg); vsnprintf(last_error_message, sizeof(last_error_message), errmsg, ap); va_end(ap); } static void errmsg_option_callback(enum argparse_server_option_t error_option, const char *errmsg, ...) { last_parsed_option = error_option; va_list ap; va_start(ap, errmsg); vsnprintf(last_error_message, sizeof(last_error_message), errmsg, ap); va_end(ap); } bool argparse_server_parse(int argc, char **argv, argparse_server_callback_t argument_callback, argparse_server_plausibilization_callback_t plausibilization_callback) { last_parsed_option = ARGPARSE_SERVER_NO_OPTION; const char *short_options = "p:sv"; struct option long_options[] = { { "port", required_argument, 0, ARG_SERVER_PORT_LONG }, { "silent", no_argument, 0, ARG_SERVER_SILENT_LONG }, { "verbose", no_argument, 0, ARG_SERVER_VERBOSE_LONG }, { "filename", required_argument, 0, ARG_SERVER_FILENAME_LONG }, { 0 } }; while (true) { int optval = getopt_long(argc, argv, short_options, long_options, NULL); if (optval == -1) { break; } last_error_message[0] = 0; enum argparse_server_option_internal_t arg = (enum argparse_server_option_internal_t)optval; switch (arg) { case ARG_SERVER_PORT_SHORT: case ARG_SERVER_PORT_LONG: last_parsed_option = ARG_SERVER_PORT; if (!argument_callback(ARG_SERVER_PORT, optarg, errmsg_callback)) { return false; } break; case ARG_SERVER_SILENT_SHORT: case ARG_SERVER_SILENT_LONG: last_parsed_option = ARG_SERVER_SILENT; if (!argument_callback(ARG_SERVER_SILENT, optarg, errmsg_callback)) { return false; } break; case ARG_SERVER_VERBOSE_SHORT: case ARG_SERVER_VERBOSE_LONG: last_parsed_option = ARG_SERVER_VERBOSE; if (!argument_callback(ARG_SERVER_VERBOSE, optarg, errmsg_callback)) { return false; } break; default: last_parsed_option = ARGPARSE_SERVER_NO_OPTION; errmsg_callback("unrecognized option supplied"); return false; } } const int positional_argument_cnt = argc - optind; last_parsed_option = ARGPARSE_SERVER_POSITIONAL_ARG; if (positional_argument_cnt != 1) { errmsg_callback("expected exactly 1 positional argument, but %d given.", positional_argument_cnt); return false; } int positional_index = optind; last_parsed_option = ARG_SERVER_FILENAME; if (!argument_callback(ARG_SERVER_FILENAME, argv[positional_index++], errmsg_callback)) { return false; } if (plausibilization_callback) { if (!plausibilization_callback(errmsg_option_callback)) { return false; } } return true; } void argparse_server_show_syntax(void) { fprintf(stderr, "usage: luksrku server [-p port] [-s] [-v] filename\n"); fprintf(stderr, "\n"); fprintf(stderr, "Starts a luksrku key server.\n"); fprintf(stderr, "\n"); fprintf(stderr, "positional arguments:\n"); fprintf(stderr, " filename Database file to load keys from.\n"); fprintf(stderr, "\n"); fprintf(stderr, "optional arguments:\n"); fprintf(stderr, " -p port, --port port Port that is used for both UDP and TCP communication.\n"); fprintf(stderr, " Defaults to 23170.\n"); fprintf(stderr, " -s, --silent Do not answer UDP queries for clients trying to find a\n"); fprintf(stderr, " key server, only serve key database using TCP.\n"); fprintf(stderr, " -v, --verbose Increase verbosity. Can be specified multiple times.\n"); } void argparse_server_parse_or_quit(int argc, char **argv, argparse_server_callback_t argument_callback, argparse_server_plausibilization_callback_t plausibilization_callback) { if (!argparse_server_parse(argc, argv, argument_callback, plausibilization_callback)) { if (last_parsed_option > ARGPARSE_SERVER_POSITIONAL_ARG) { if (last_error_message[0]) { fprintf(stderr, "luksrku server: error parsing argument %s -- %s\n", option_texts[last_parsed_option], last_error_message); } else { fprintf(stderr, "luksrku server: error parsing argument %s -- no details available\n", option_texts[last_parsed_option]); } } else if (last_parsed_option == ARGPARSE_SERVER_POSITIONAL_ARG) { fprintf(stderr, "luksrku server: error parsing optional arguments -- %s\n", last_error_message); } argparse_server_show_syntax(); exit(EXIT_FAILURE); } } #ifdef __ARGPARSE_MAIN__ /* gcc -D __ARGPARSE_MAIN__ -O2 -Wall -o argparse argparse_server.c */ static const char *option_enum_to_str(enum argparse_server_option_t option) { switch (option) { case ARG_SERVER_PORT: return "ARG_SERVER_PORT"; case ARG_SERVER_SILENT: return "ARG_SERVER_SILENT"; case ARG_SERVER_VERBOSE: return "ARG_SERVER_VERBOSE"; case ARG_SERVER_FILENAME: return "ARG_SERVER_FILENAME"; } return "UNKNOWN"; } bool arg_print_callback(enum argparse_server_option_t option, const char *value, argparse_server_errmsg_callback_t errmsg_callback) { fprintf(stderr, "%s = \"%s\"\n", option_enum_to_str(option), value); return true; } int main(int argc, char **argv) { argparse_server_parse_or_quit(argc, argv, arg_print_callback, NULL); return 0; } #endif