comparison libcommon/globalopts.c @ 45:9eb5460f51a6

main tools: support both pcsc and serial back ends
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 21 Mar 2021 01:56:49 +0000
parents c9ef9e91dd8e
children
comparison
equal deleted inserted replaced
44:0bc89d61fc59 45:9eb5460f51a6
1 /* 1 /*
2 * This module implements parsing of global command line options. 2 * This module implements parsing of global command line options.
3 */ 3 */
4 4
5 #include <ctype.h>
5 #include <stdio.h> 6 #include <stdio.h>
6 #include <stdlib.h> 7 #include <stdlib.h>
7 #include <unistd.h> 8 #include <unistd.h>
8 9
9 unsigned calypso_fd, pcsc_reader_num; 10 unsigned calypso_fd, pcsc_reader_num, serial_spenh;
11 int use_pcsc;
12 char *serial_device, *serial_baud;
10 13
11 parse_global_options(argc, argv) 14 parse_global_options(argc, argv)
12 char **argv; 15 char **argv;
13 { 16 {
14 extern char *optarg; 17 extern char *optarg;
15 int c; 18 int c;
16 19
17 while ((c = getopt(argc, argv, "+C:p:")) != EOF) { 20 while ((c = getopt(argc, argv, "+b:C:d:e:p:")) != EOF) {
18 switch (c) { 21 switch (c) {
22 case 'b':
23 serial_baud = optarg;
24 continue;
19 case 'C': 25 case 'C':
20 calypso_fd = atoi(optarg); 26 calypso_fd = atoi(optarg);
21 continue; 27 continue;
28 case 'd':
29 serial_device = optarg;
30 continue;
31 case 'e':
32 if (!isdigit(optarg[0]) || optarg[1]) {
33 bad_minus_e: fprintf(stderr, "error: bad -e argument\n");
34 exit(1);
35 }
36 serial_spenh = optarg[0] - '0';
37 switch (serial_spenh) {
38 case 1:
39 case 2:
40 case 4:
41 case 8:
42 break;
43 default:
44 goto bad_minus_e;
45 }
46 continue;
22 case 'p': 47 case 'p':
48 use_pcsc = 1;
23 pcsc_reader_num = atoi(optarg); 49 pcsc_reader_num = atoi(optarg);
24 continue; 50 continue;
25 case '?': 51 case '?':
26 default: 52 default:
27 /* error msg already printed */ 53 /* error msg already printed */
28 exit(1); 54 exit(1);
29 } 55 }
30 } 56 }
57 if (serial_device && use_pcsc) {
58 fprintf(stderr,
59 "error: -d and -p target selection options are mutually exclusive\n");
60 exit(1);
61 }
62 if (!serial_device && (serial_baud || serial_spenh)) {
63 fprintf(stderr,
64 "error: -b and -e options are meaningless without -d\n");
65 exit(1);
66 }
67 if (serial_baud && serial_spenh) {
68 fprintf(stderr,
69 "error: -b and -e options are mutually exclusive\n");
70 exit(1);
71 }
31 return(0); 72 return(0);
32 } 73 }