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