summaryrefslogtreecommitdiffstats
path: root/tools/idevicedebugserverproxy.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/idevicedebugserverproxy.c
parent3b5cad28fabb236e05b8fff82fab5098127aa2bb (diff)
downloadlibimobiledevice-6cb13f9e6d3939930aecf91d8e23d1896a3b92e5.tar.gz
libimobiledevice-6cb13f9e6d3939930aecf91d8e23d1896a3b92e5.tar.bz2
tools: Use getopt for option parsing in all tools
Diffstat (limited to 'tools/idevicedebugserverproxy.c')
-rw-r--r--tools/idevicedebugserverproxy.c105
1 files changed, 56 insertions, 49 deletions
diff --git a/tools/idevicedebugserverproxy.c b/tools/idevicedebugserverproxy.c
index 79c2d38..8a3b4ff 100644
--- a/tools/idevicedebugserverproxy.c
+++ b/tools/idevicedebugserverproxy.c
@@ -29,6 +29,7 @@
29#include <stdio.h> 29#include <stdio.h>
30#include <stdlib.h> 30#include <stdlib.h>
31#include <string.h> 31#include <string.h>
32#include <getopt.h>
32#include <errno.h> 33#include <errno.h>
33#include <signal.h> 34#include <signal.h>
34#ifdef WIN32 35#ifdef WIN32
@@ -75,26 +76,26 @@ static void clean_exit(int sig)
75 quit_flag++; 76 quit_flag++;
76} 77}
77 78
78static void print_usage(int argc, char **argv) 79static void print_usage(int argc, char **argv, int is_error)
79{ 80{
80 char *name = NULL; 81 char *name = strrchr(argv[0], '/');
81 82 fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] [PORT]\n", (name ? name + 1: argv[0]));
82 name = strrchr(argv[0], '/'); 83 fprintf(is_error ? stderr : stdout,
83 printf("Usage: %s [OPTIONS] [PORT]\n", (name ? name + 1: argv[0])); 84 "\n"
84 printf("\n"); 85 "Proxy debugserver connection from device to a local socket at PORT.\n"
85 printf("Proxy debugserver connection from device to a local socket at PORT.\n"); 86 "If PORT is omitted, the next available port will be used and printed\n"
86 printf("If PORT is omitted, the next available port will be used and printed\n"); 87 "to stdout.\n"
87 printf("to stdout.\n"); 88 "\n"
88 printf("\n"); 89 "OPTIONS:\n"
89 printf("OPTIONS:\n"); 90 " -u, --udid UDID target specific device by UDID\n"
90 printf(" -u, --udid UDID\ttarget specific device by UDID\n"); 91 " -n, --network connect to network device\n"
91 printf(" -n, --network\t\tconnect to network device\n"); 92 " -d, --debug enable communication debugging\n"
92 printf(" -d, --debug\t\tenable communication debugging\n"); 93 " -h, --help prints usage information\n"
93 printf(" -h, --help\t\tprints usage information\n"); 94 " -v, --version prints version information\n"
94 printf(" -v, --version\t\tprints version information\n"); 95 "\n"
95 printf("\n"); 96 "Homepage: <" PACKAGE_URL ">\n"
96 printf("Homepage: <" PACKAGE_URL ">\n"); 97 "Bug Reports: <" PACKAGE_BUGREPORT ">\n"
97 printf("Bug Reports: <" PACKAGE_BUGREPORT ">\n"); 98 );
98} 99}
99 100
100static void* connection_handler(void* data) 101static void* connection_handler(void* data)
@@ -182,7 +183,15 @@ int main(int argc, char *argv[])
182 uint16_t local_port = 0; 183 uint16_t local_port = 0;
183 int server_fd; 184 int server_fd;
184 int result = EXIT_SUCCESS; 185 int result = EXIT_SUCCESS;
185 int i; 186 int c = 0;
187 const struct option longopts[] = {
188 { "debug", no_argument, NULL, 'd' },
189 { "help", no_argument, NULL, 'h' },
190 { "udid", required_argument, NULL, 'u' },
191 { "network", no_argument, NULL, 'n' },
192 { "version", no_argument, NULL, 'v' },
193 { NULL, 0, NULL, 0}
194 };
186 195
187#ifndef WIN32 196#ifndef WIN32
188 struct sigaction sa; 197 struct sigaction sa;
@@ -207,43 +216,41 @@ int main(int argc, char *argv[])
207#endif 216#endif
208 217
209 /* parse cmdline arguments */ 218 /* parse cmdline arguments */
210 for (i = 1; i < argc; i++) { 219 while ((c = getopt_long(argc, argv, "dhu:nv", longopts, NULL)) != -1) {
211 if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { 220 switch (c) {
221 case 'd':
212 debug_mode = 1; 222 debug_mode = 1;
213 idevice_set_debug_level(1); 223 idevice_set_debug_level(1);
214 socket_set_verbose(3); 224 socket_set_verbose(3);
215 continue; 225 break;
216 } 226 case 'u':
217 else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) { 227 if (!*optarg) {
218 i++; 228 fprintf(stderr, "ERROR: UDID argument must not be empty!\n");
219 if (!argv[i] || !*argv[i]) { 229 print_usage(argc, argv, 1);
220 print_usage(argc, argv); 230 return 2;
221 return 0;
222 } 231 }
223 udid = argv[i]; 232 udid = optarg;
224 continue; 233 break;
225 } 234 case 'n':
226 else if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--network")) {
227 use_network = 1; 235 use_network = 1;
228 continue; 236 break;
229 } 237 case 'h':
230 else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { 238 print_usage(argc, argv, 0);
231 print_usage(argc, argv); 239 return 0;
232 return EXIT_SUCCESS; 240 case 'v':
233 }
234 else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) {
235 printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); 241 printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION);
236 return EXIT_SUCCESS; 242 return 0;
237 } 243 default:
238 else if (atoi(argv[i]) > 0) { 244 print_usage(argc, argv, 1);
239 local_port = atoi(argv[i]); 245 return 2;
240 continue;
241 }
242 else {
243 print_usage(argc, argv);
244 return EXIT_SUCCESS;
245 } 246 }
246 } 247 }
248 argc -= optind;
249 argv += optind;
250
251 if (argv[0] && (atoi(argv[0]) > 0)) {
252 local_port = atoi(argv[0]);
253 }
247 254
248 /* start services and connect to device */ 255 /* start services and connect to device */
249 ret = idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX); 256 ret = idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX);