From 6cb13f9e6d3939930aecf91d8e23d1896a3b92e5 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Sat, 30 Apr 2022 13:31:20 +0200 Subject: tools: Use getopt for option parsing in all tools --- tools/idevicenotificationproxy.c | 180 +++++++++++++++++++++------------------ 1 file changed, 97 insertions(+), 83 deletions(-) (limited to 'tools/idevicenotificationproxy.c') diff --git a/tools/idevicenotificationproxy.c b/tools/idevicenotificationproxy.c index 00bcba8..d1e25c1 100644 --- a/tools/idevicenotificationproxy.c +++ b/tools/idevicenotificationproxy.c @@ -27,9 +27,10 @@ #include #include +#include +#include #include #include -#include #ifdef WIN32 #include @@ -59,28 +60,29 @@ static void clean_exit(int sig) quit_flag++; } -static void print_usage(int argc, char **argv) +static void print_usage(int argc, char **argv, int is_error) { - char *name = NULL; - - name = strrchr(argv[0], '/'); - printf("Usage: %s [OPTIONS] COMMAND\n", (name ? name + 1: argv[0])); - printf("\n"); - printf("Post or observe notifications on a device.\n"); - printf("\n"); - printf("Where COMMAND is one of:\n"); - printf(" post ID [...]\t\tpost notification IDs to device and exit\n"); - printf(" observe ID [...]\tobserve notification IDs in the foreground until CTRL+C or signal is received\n"); - printf("\n"); - printf("The following OPTIONS are accepted:\n"); - printf(" -u, --udid UDID\ttarget specific device by UDID\n"); - printf(" -n, --network\t\tconnect to network device\n"); - printf(" -d, --debug\t\tenable communication debugging\n"); - printf(" -h, --help\t\tprints usage information\n"); - printf(" -v, --version\t\tprints version information\n"); - printf("\n"); - printf("Homepage: <" PACKAGE_URL ">\n"); - printf("Bug Reports: <" PACKAGE_BUGREPORT ">\n"); + char *name = strrchr(argv[0], '/'); + fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] COMMAND\n", (name ? name + 1: argv[0])); + fprintf(is_error ? stderr : stdout, + "\n" + "Post or observe notifications on a device.\n" + "\n" + "Where COMMAND is one of:\n" + " post ID [...] post notification IDs to device and exit\n" + " observe ID [...] observe notification IDs in foreground until CTRL+C\n" + " or signal is received\n" + "\n" + "The following OPTIONS are accepted:\n" + " -u, --udid UDID target specific device by UDID\n" + " -n, --network connect to network device\n" + " -d, --debug enable communication debugging\n" + " -h, --help prints usage information\n" + " -v, --version prints version information\n" + "\n" + "Homepage: <" PACKAGE_URL ">\n" + "Bug Reports: <" PACKAGE_BUGREPORT ">\n" + ); } static void notify_cb(const char *notification, void *user_data) @@ -97,7 +99,7 @@ int main(int argc, char *argv[]) np_client_t gnp = NULL; int result = -1; - int i; + int i = 0; const char* udid = NULL; int use_network = 0; int cmd = CMD_NONE; @@ -107,6 +109,16 @@ int main(int argc, char *argv[]) char **nspec = NULL; char **nspectmp = NULL; + int c = 0; + const struct option longopts[] = { + { "debug", no_argument, NULL, 'd' }, + { "help", no_argument, NULL, 'h' }, + { "udid", required_argument, NULL, 'u' }, + { "network", no_argument, NULL, 'n' }, + { "version", no_argument, NULL, 'v' }, + { NULL, 0, NULL, 0} + }; + signal(SIGINT, clean_exit); signal(SIGTERM, clean_exit); #ifndef WIN32 @@ -115,80 +127,82 @@ int main(int argc, char *argv[]) #endif /* parse cmdline args */ - for (i = 1; i < argc; i++) { - if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { + while ((c = getopt_long(argc, argv, "dhu:nv", longopts, NULL)) != -1) { + switch (c) { + case 'd': idevice_set_debug_level(1); - continue; - } - else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) { - i++; - if (!argv[i] || !*argv[i]) { - print_usage(argc, argv); - result = 0; - goto cleanup; + break; + case 'u': + if (!*optarg) { + fprintf(stderr, "ERROR: UDID argument must not be empty!\n"); + print_usage(argc, argv, 1); + return 2; } - udid = argv[i]; - continue; - } - else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { - print_usage(argc, argv); - result = 0; - goto cleanup; - } - else if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--network")) { + udid = optarg; + break; + case 'n': use_network = 1; - continue; - } - else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) { + break; + case 'h': + print_usage(argc, argv, 0); + return 0; + case 'v': printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); - result = 0; - goto cleanup; + return 0; + default: + print_usage(argc, argv, 1); + return 2; } - else if (!strcmp(argv[i], "post") || !strcmp(argv[i], "observe")) { - cmd = CMD_POST; - if (!strcmp(argv[i], "observe")) { - cmd = CMD_OBSERVE; - } - - i++; + } + argc -= optind; + argv += optind; - if (!argv[i] || argv[i] == NULL || (!strncmp(argv[i], "-", 1))) { - printf("Please supply a valid notification identifier.\n"); - print_usage(argc, argv); - goto cleanup; - } + if (!argv[i]) { + fprintf(stderr, "ERROR: Missing command\n"); + print_usage(argc+optind, argv-optind, 1); + return 2; + } - count = 0; - nspec = malloc(sizeof(char*) * (count+1)); - - while(1) { - if (argv[i] && (strlen(argv[i]) >= 2) && (strncmp(argv[i], "-", 1) != 0)) { - nspectmp = realloc(nspec, sizeof(char*) * (count+1)); - nspectmp[count] = strdup(argv[i]); - nspec = nspectmp; - count = count+1; - i++; - } else { - i--; - break; - } - } + if (!strcmp(argv[i], "post")) { + cmd = CMD_POST; + } else if (!strcmp(argv[i], "observe")) { + cmd = CMD_OBSERVE; + } - nspectmp = realloc(nspec, sizeof(char*) * (count+1)); - nspectmp[count] = NULL; - nspec = nspectmp; - continue; + if (cmd == CMD_POST || cmd == CMD_OBSERVE) { + i++; + if (!argv[i]) { + fprintf(stderr, "ERROR: Please supply a valid notification identifier.\n"); + print_usage(argc+optind, argv-optind, 1); + return 2; } - else { - print_usage(argc, argv); - return 0; + + count = 0; + nspec = malloc(sizeof(char*) * (count+1)); + + while(1) { + if (argv[i] && (strlen(argv[i]) >= 2) && (strncmp(argv[i], "-", 1) != 0)) { + nspectmp = realloc(nspec, sizeof(char*) * (count+1)); + nspectmp[count] = strdup(argv[i]); + nspec = nspectmp; + count = count+1; + i++; + } else { + i--; + break; + } } + + nspectmp = realloc(nspec, sizeof(char*) * (count+1)); + nspectmp[count] = NULL; + nspec = nspectmp; } /* verify options */ if (cmd == CMD_NONE) { - print_usage(argc, argv); - goto cleanup; + fprintf(stderr, "ERROR: Unsupported command '%s'\n", argv[0]); + print_usage(argc+optind, argv-optind, 1); + return 2; } if (IDEVICE_E_SUCCESS != idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX)) { -- cgit v1.1-32-gdbae