static void add_option_passed_null(void **state) { args *args = *state; assert_int_equal( ARGPARSE_PASSED_NULL, args_add_option(args, NULL) ); }
static void empty_option_fail(void **state) { args *args = *state; option *opt = option_new("", "", ""); assert_int_equal( ARGPARSE_EMPTY_OPTION, args_add_option(args, opt) ); option_free(opt); }
/** * Everything begins "here" * * @param argc The number of command line arguments * @param argv Command line arguments * @return Zero on and only on success */ int main(int argc, char* argv[]) { int rc = 0; size_t first, diff, end; args_init((char*)"Rotate images in a pattern", (char*)"crazy-rotate [--] <first> <gaps+1> [<last>]", NULL, NULL, 1, 0, args_standard_abbreviations); args_add_option(args_new_argumentless(NULL, 0, (char*)"--help", NULL), (char*)"Prints this help message"); args_parse(argc, argv); args_support_alternatives(); if (args_opts_used((char*)"--help")) { args_help(); goto exit; } if (args_unrecognised_count || (args_files_count < 2) || (args_files_count > 3)) goto invalid_opts; first = parse_size(args_files[0]); diff = parse_size(args_files[1]); end = args_files_count == 3 ? parse_size(args_files[2]) : (SIZE_MAX - 1); if (!first || !diff || !end) goto invalid_opts; if (end++ == SIZE_MAX) t ((errno = ERANGE)); t (perform_rotate(first, diff, end)); exit: args_dispose(); return rc; invalid_opts: args_help(); fail: if (errno) perror(*argv); rc = 1; goto exit; }
ARGPARSEcode subcommand_add_option(subcommand *scmd, option *opt) { if (!scmd->args) scmd->args = args_new(); return args_add_option(scmd->args, opt); }
int main(int argc, char** argv_) { int found = 0; int first_pid = 1; char* usage_str; ssize_t i, n; argv = argv_; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); n = (ssize_t)(strlen(_(" [options] [program...]")) + strlen(*argv) + 1); usage_str = alloca((size_t)n * sizeof(char)); sprintf(usage_str, "%s%s", *argv, _(" [options] [program...]")); args_init(!strcmp(argv[0], "dpidof") ? _("epidof with display isolation") : _("pidof with environment constraints"), usage_str, NULL, 0, 1, 0, args_standard_abbreviations); args_add_option(args_new_argumentless(NULL, 0, "-c", "--check-root", NULL), _("Restrict to processes running under the same root")); args_add_option(args_new_argumentless(NULL, 0, "-s", "--single-shot", NULL), _("Return only one process ID")); args_add_option(args_new_argumentless(NULL, 0, "-x", "--scripts", NULL), _("Test the name of scripts")); args_add_option(args_new_argumented (NULL, _("PID"), 0, "-o", "--omit-pid", NULL), _("Do not return a specific process ID")); args_add_option(args_new_argumentless(NULL, 0, "-h", "--help", NULL), _("Display this help information")); args_add_option(args_new_argumentless(NULL, 0, "-V", "--version", NULL), _("Print the name and version of this program")); environment_parse(&argc, argv); args_parse(argc, argv); if (args_unrecognised_count || args_opts_used("-h")) args_help(), fprintf(stderr, "%s\n\n", _(environment_synopsis)); else if (args_opts_used("-V")) printf("%s " VERSION, !strcmp(argv[0], "dpidof") ? "dpidof" : "epidof"); else goto cont; return args_unrecognised_count ? EXIT_FAILURE : EXIT_SUCCESS; cont: /* process command-line options */ if (args_opts_used("-s")) opt_single_shot = 1; if (args_opts_used("-x")) opt_scripts_too = 1; if (args_opts_used("-c") && (geteuid() == 0)) epidof_root = pid_link(getpid(), "root"); if (args_opts_used("-o")) { char** arr = args_opts_get("-o"); for (i = 0, n = (ssize_t)args_opts_get_count("-o"); i < n; i++) add_to_omit_list(arr[i]); } /* main loop */ for (n = 0; n < args_files_count; n++) /* for each program */ { program = args_files[n]; proc_count = 0; select_procs(); /* get the list of matching processes */ if (proc_count > 0) { found = 1; for (i = (ssize_t)proc_count - 1; i >= 0; i--) /* and display their PIDs */ { printf(first_pid ? "%ld" : " %ld", (long)(procs[i])); first_pid = 0; if (opt_single_shot) break; } } } /* final line feed */ if (found) printf("\n"); xfree(procs); xfree(omitted_procs); xfree(epidof_root); args_dispose(); environment_dispose(); return !found; }