summaryrefslogtreecommitdiffstats
path: root/tools/idevicenotificationproxy.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2022-04-30 13:31:20 +0200
committerGravatar Nikias Bassen2022-04-30 13:31:20 +0200
commit6cb13f9e6d3939930aecf91d8e23d1896a3b92e5 (patch)
tree371e4676ac914d9eef6bb4cfc0b5b6dc6f27da4f /tools/idevicenotificationproxy.c
parent3b5cad28fabb236e05b8fff82fab5098127aa2bb (diff)
downloadlibimobiledevice-6cb13f9e6d3939930aecf91d8e23d1896a3b92e5.tar.gz
libimobiledevice-6cb13f9e6d3939930aecf91d8e23d1896a3b92e5.tar.bz2
tools: Use getopt for option parsing in all tools
Diffstat (limited to 'tools/idevicenotificationproxy.c')
-rw-r--r--tools/idevicenotificationproxy.c180
1 files changed, 97 insertions, 83 deletions
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 @@
27 27
28#include <stdio.h> 28#include <stdio.h>
29#include <string.h> 29#include <string.h>
30#include <stdlib.h>
31#include <getopt.h>
30#include <errno.h> 32#include <errno.h>
31#include <signal.h> 33#include <signal.h>
32#include <stdlib.h>
33 34
34#ifdef WIN32 35#ifdef WIN32
35#include <windows.h> 36#include <windows.h>
@@ -59,28 +60,29 @@ static void clean_exit(int sig)
59 quit_flag++; 60 quit_flag++;
60} 61}
61 62
62static void print_usage(int argc, char **argv) 63static void print_usage(int argc, char **argv, int is_error)
63{ 64{
64 char *name = NULL; 65 char *name = strrchr(argv[0], '/');
65 66 fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] COMMAND\n", (name ? name + 1: argv[0]));
66 name = strrchr(argv[0], '/'); 67 fprintf(is_error ? stderr : stdout,
67 printf("Usage: %s [OPTIONS] COMMAND\n", (name ? name + 1: argv[0])); 68 "\n"
68 printf("\n"); 69 "Post or observe notifications on a device.\n"
69 printf("Post or observe notifications on a device.\n"); 70 "\n"
70 printf("\n"); 71 "Where COMMAND is one of:\n"
71 printf("Where COMMAND is one of:\n"); 72 " post ID [...] post notification IDs to device and exit\n"
72 printf(" post ID [...]\t\tpost notification IDs to device and exit\n"); 73 " observe ID [...] observe notification IDs in foreground until CTRL+C\n"
73 printf(" observe ID [...]\tobserve notification IDs in the foreground until CTRL+C or signal is received\n"); 74 " or signal is received\n"
74 printf("\n"); 75 "\n"
75 printf("The following OPTIONS are accepted:\n"); 76 "The following OPTIONS are accepted:\n"
76 printf(" -u, --udid UDID\ttarget specific device by UDID\n"); 77 " -u, --udid UDID target specific device by UDID\n"
77 printf(" -n, --network\t\tconnect to network device\n"); 78 " -n, --network connect to network device\n"
78 printf(" -d, --debug\t\tenable communication debugging\n"); 79 " -d, --debug enable communication debugging\n"
79 printf(" -h, --help\t\tprints usage information\n"); 80 " -h, --help prints usage information\n"
80 printf(" -v, --version\t\tprints version information\n"); 81 " -v, --version prints version information\n"
81 printf("\n"); 82 "\n"
82 printf("Homepage: <" PACKAGE_URL ">\n"); 83 "Homepage: <" PACKAGE_URL ">\n"
83 printf("Bug Reports: <" PACKAGE_BUGREPORT ">\n"); 84 "Bug Reports: <" PACKAGE_BUGREPORT ">\n"
85 );
84} 86}
85 87
86static void notify_cb(const char *notification, void *user_data) 88static void notify_cb(const char *notification, void *user_data)
@@ -97,7 +99,7 @@ int main(int argc, char *argv[])
97 np_client_t gnp = NULL; 99 np_client_t gnp = NULL;
98 100
99 int result = -1; 101 int result = -1;
100 int i; 102 int i = 0;
101 const char* udid = NULL; 103 const char* udid = NULL;
102 int use_network = 0; 104 int use_network = 0;
103 int cmd = CMD_NONE; 105 int cmd = CMD_NONE;
@@ -107,6 +109,16 @@ int main(int argc, char *argv[])
107 char **nspec = NULL; 109 char **nspec = NULL;
108 char **nspectmp = NULL; 110 char **nspectmp = NULL;
109 111
112 int c = 0;
113 const struct option longopts[] = {
114 { "debug", no_argument, NULL, 'd' },
115 { "help", no_argument, NULL, 'h' },
116 { "udid", required_argument, NULL, 'u' },
117 { "network", no_argument, NULL, 'n' },
118 { "version", no_argument, NULL, 'v' },
119 { NULL, 0, NULL, 0}
120 };
121
110 signal(SIGINT, clean_exit); 122 signal(SIGINT, clean_exit);
111 signal(SIGTERM, clean_exit); 123 signal(SIGTERM, clean_exit);
112#ifndef WIN32 124#ifndef WIN32
@@ -115,80 +127,82 @@ int main(int argc, char *argv[])
115#endif 127#endif
116 128
117 /* parse cmdline args */ 129 /* parse cmdline args */
118 for (i = 1; i < argc; i++) { 130 while ((c = getopt_long(argc, argv, "dhu:nv", longopts, NULL)) != -1) {
119 if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { 131 switch (c) {
132 case 'd':
120 idevice_set_debug_level(1); 133 idevice_set_debug_level(1);
121 continue; 134 break;
122 } 135 case 'u':
123 else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) { 136 if (!*optarg) {
124 i++; 137 fprintf(stderr, "ERROR: UDID argument must not be empty!\n");
125 if (!argv[i] || !*argv[i]) { 138 print_usage(argc, argv, 1);
126 print_usage(argc, argv); 139 return 2;
127 result = 0;
128 goto cleanup;
129 } 140 }
130 udid = argv[i]; 141 udid = optarg;
131 continue; 142 break;
132 } 143 case 'n':
133 else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
134 print_usage(argc, argv);
135 result = 0;
136 goto cleanup;
137 }
138 else if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--network")) {
139 use_network = 1; 144 use_network = 1;
140 continue; 145 break;
141 } 146 case 'h':
142 else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) { 147 print_usage(argc, argv, 0);
148 return 0;
149 case 'v':
143 printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); 150 printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION);
144 result = 0; 151 return 0;
145 goto cleanup; 152 default:
153 print_usage(argc, argv, 1);
154 return 2;
146 } 155 }
147 else if (!strcmp(argv[i], "post") || !strcmp(argv[i], "observe")) { 156 }
148 cmd = CMD_POST; 157 argc -= optind;
149 if (!strcmp(argv[i], "observe")) { 158 argv += optind;
150 cmd = CMD_OBSERVE;
151 }
152
153 i++;
154 159
155 if (!argv[i] || argv[i] == NULL || (!strncmp(argv[i], "-", 1))) { 160 if (!argv[i]) {
156 printf("Please supply a valid notification identifier.\n"); 161 fprintf(stderr, "ERROR: Missing command\n");
157 print_usage(argc, argv); 162 print_usage(argc+optind, argv-optind, 1);
158 goto cleanup; 163 return 2;
159 } 164 }
160 165
161 count = 0; 166 if (!strcmp(argv[i], "post")) {
162 nspec = malloc(sizeof(char*) * (count+1)); 167 cmd = CMD_POST;
163 168 } else if (!strcmp(argv[i], "observe")) {
164 while(1) { 169 cmd = CMD_OBSERVE;
165 if (argv[i] && (strlen(argv[i]) >= 2) && (strncmp(argv[i], "-", 1) != 0)) { 170 }
166 nspectmp = realloc(nspec, sizeof(char*) * (count+1));
167 nspectmp[count] = strdup(argv[i]);
168 nspec = nspectmp;
169 count = count+1;
170 i++;
171 } else {
172 i--;
173 break;
174 }
175 }
176 171
177 nspectmp = realloc(nspec, sizeof(char*) * (count+1)); 172 if (cmd == CMD_POST || cmd == CMD_OBSERVE) {
178 nspectmp[count] = NULL; 173 i++;
179 nspec = nspectmp; 174 if (!argv[i]) {
180 continue; 175 fprintf(stderr, "ERROR: Please supply a valid notification identifier.\n");
176 print_usage(argc+optind, argv-optind, 1);
177 return 2;
181 } 178 }
182 else { 179
183 print_usage(argc, argv); 180 count = 0;
184 return 0; 181 nspec = malloc(sizeof(char*) * (count+1));
182
183 while(1) {
184 if (argv[i] && (strlen(argv[i]) >= 2) && (strncmp(argv[i], "-", 1) != 0)) {
185 nspectmp = realloc(nspec, sizeof(char*) * (count+1));
186 nspectmp[count] = strdup(argv[i]);
187 nspec = nspectmp;
188 count = count+1;
189 i++;
190 } else {
191 i--;
192 break;
193 }
185 } 194 }
195
196 nspectmp = realloc(nspec, sizeof(char*) * (count+1));
197 nspectmp[count] = NULL;
198 nspec = nspectmp;
186 } 199 }
187 200
188 /* verify options */ 201 /* verify options */
189 if (cmd == CMD_NONE) { 202 if (cmd == CMD_NONE) {
190 print_usage(argc, argv); 203 fprintf(stderr, "ERROR: Unsupported command '%s'\n", argv[0]);
191 goto cleanup; 204 print_usage(argc+optind, argv-optind, 1);
205 return 2;
192 } 206 }
193 207
194 if (IDEVICE_E_SUCCESS != idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX)) { 208 if (IDEVICE_E_SUCCESS != idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX)) {