msg_338.c revision 1.6 1 /* $NetBSD: msg_338.c,v 1.6 2021/10/09 13:57:55 rillig Exp $ */
2 # 3 "msg_338.c"
3
4 // Test for message: option '%c' should be handled in the switch [338]
5
6 int getopt(int, char *const *, const char *);
7 extern char *optarg;
8
9 int
10 main(int argc, char **argv)
11 {
12 int o;
13
14 while ((o = getopt(argc, argv, "a:bc:d")) != -1) { /* expect: 338 *//* expect: 338 */
15 switch (o) {
16 case 'a':
17 break;
18 case 'b':
19 /*
20 * The following while loop must not finish the check
21 * for the getopt options.
22 */
23 while (optarg[0] != '\0')
24 optarg++;
25 break;
26 case 'e': /* expect: option 'e' should be listed */
27 break;
28 case 'f': /* expect: option 'f' should be listed */
29 /*
30 * The case labels in nested switch statements are
31 * ignored by the check for getopt options.
32 */
33 switch (optarg[0]) {
34 case 'X':
35 break;
36 }
37 break;
38 case '?':
39 default:
40 break;
41 }
42 }
43
44 /* A while loop that is not related to getopt is simply skipped. */
45 while (o != 0) {
46 switch (o) {
47 case '?':
48 o = ':';
49 }
50 }
51
52 return 0;
53 }
54
55 void usage(void);
56
57 /*
58 * Before ckgetopt.c 1.11 from 2021-08-23, lint wrongly warned about a
59 * missing '?' in the switch statement, even though it was there.
60 *
61 * Seen in usr.bin/ftp/main.c 1.127 from 2020-07-18.
62 */
63 int
64 question_option(int argc, char **argv)
65 {
66 int c;
67
68 while ((c = getopt(argc, argv, "?x")) != -1) {
69 switch (c) {
70 case 'x':
71 break;
72 case '?':
73 usage();
74 return 0;
75 default:
76 usage();
77 return 1;
78 }
79 }
80 return 0;
81 }
82
83 /*
84 * If the first character of the options string is ':', getopt does not print
85 * its own error messages. Getopt returns ':' if an option is missing its
86 * argument; that is handled by the 'default:' already.
87 */
88 int
89 suppress_errors(int argc, char **argv)
90 {
91 int c;
92
93 /* expect+1: warning: option 'o' should be handled in the switch [338] */
94 while ((c = getopt(argc, argv, ":b:o")) != -1) {
95 switch (c) {
96 case 'b':
97 return 'b';
98 default:
99 usage();
100 }
101 }
102 return 0;
103 }
104
105 /*
106 * If the first character of the options string is ':', getopt returns ':'
107 * if an option is missing its argument. This condition can be handled
108 * separately from '?', which getopt returns for unknown options.
109 */
110 int
111 missing_argument(int argc, char **argv)
112 {
113 int c;
114
115 /* expect+1: warning: option 'o' should be handled in the switch [338] */
116 while ((c = getopt(argc, argv, ":b:o")) != -1) {
117 switch (c) {
118 case 'b':
119 return 'b';
120 case ':':
121 return 'm';
122 default:
123 usage();
124 }
125 }
126 return 0;
127 }
128
129 /*
130 * Getopt only returns ':' if ':' is the first character in the options
131 * string. Everywhere else, a ':' marks the preceding option as having a
132 * required argument. In theory, if the options string contained "a::x",
133 * that could be interpreted as '-a argument', followed by '-:' and '-x',
134 * but nobody does that.
135 */
136 int
137 unreachable_colon(int argc, char **argv)
138 {
139 int c;
140
141 /* expect+1: warning: option 'b' should be handled in the switch [338] */
142 while ((c = getopt(argc, argv, "b:")) != -1) {
143 switch (c) {
144 /* TODO: expect+1: warning: option ':' should be listed in the options string [339] */
145 case ':':
146 return 'm';
147 default:
148 usage();
149 }
150 }
151 return 0;
152 }
153