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