Print this page
10120 smatch indenting fixes for usr/src/cmd
Reviewed by: Gergő Doma <domag02@gmail.com>
Portions contributed by: Joyce McIntosh <joyce.mcintosh@nexenta.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/valtools/ckitem.c
+++ new/usr/src/cmd/valtools/ckitem.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License, Version 1.0 only
6 6 * (the "License"). You may not use this file except in compliance
7 7 * with the License.
8 8 *
9 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 * or http://www.opensolaris.org/os/licensing.
11 11 * See the License for the specific language governing permissions
12 12 * and limitations under the License.
13 13 *
14 14 * When distributing Covered Code, include this CDDL HEADER in each
15 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 16 * If applicable, add the following below this CDDL HEADER, with the
17 17 * fields enclosed by brackets "[]" replaced with your own identifying
18 18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 19 *
20 20 * CDDL HEADER END
↓ open down ↓ |
20 lines elided |
↑ open up ↑ |
21 21 */
22 22 /*
23 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 28 /* All Rights Reserved */
29 29
30 30
31 -#pragma ident "%Z%%M% %I% %E% SMI"
31 +/*
32 + * Copyright (c) 2018, Joyent, Inc.
33 + */
32 34
33 35 #include <stdio.h>
34 36 #include <ctype.h>
35 37 #include <string.h>
36 38 #include <signal.h>
37 39 #include <valtools.h>
38 40 #include <stdlib.h>
39 41 #include <locale.h>
40 42 #include <libintl.h>
41 43 #include <limits.h>
42 44 #include <wchar.h>
43 45 #include "usage.h"
44 46 #include "libadm.h"
45 47
46 48 #define BADPID (-2)
47 49 #define INVISMAXSIZE 36
48 50
49 51 static char *prog;
50 52 static char *deflt = NULL, *prompt = NULL, *error = NULL, *help = NULL;
51 53 static int kpid = BADPID;
52 54 static int signo;
53 55
54 56 static char *label, **invis;
55 57 static int ninvis = 0;
56 58 static int max = 1;
57 59 static int attr = CKALPHA;
58 60
59 61 #define MAXSIZE 128
60 62 #define LSIZE 1024
61 63 #define INTERR \
62 64 "%s: ERROR: internal error occurred while attempting menu setup\n"
63 65 #define MYOPTS \
64 66 "\t-f file #file containing choices\n" \
65 67 "\t-l label #menu label\n" \
66 68 "\t-i invis [, ...] #invisible menu choices\n" \
67 69 "\t-m max #maximum choices user may select\n" \
68 70 "\t-n #do not sort choices alphabetically\n" \
69 71 "\t-o #don't prompt if only one choice\n" \
70 72 "\t-u #unnumbered choices\n"
71 73
72 74 static const char husage[] = "Wh";
73 75 static const char eusage[] = "We";
74 76
75 77 static void
76 78 usage(void)
77 79 {
78 80 switch (*prog) {
79 81 default:
80 82 (void) fprintf(stderr,
81 83 gettext("usage: %s [options] [choice [...]]\n"), prog);
82 84 (void) fprintf(stderr, gettext(OPTMESG));
83 85 (void) fprintf(stderr, gettext(MYOPTS));
84 86 (void) fprintf(stderr, gettext(STDOPTS));
85 87 break;
86 88
87 89 case 'h':
88 90 (void) fprintf(stderr,
89 91 gettext("usage: %s [options] [choice [...]]\n"), prog);
90 92 (void) fprintf(stderr, gettext(OPTMESG));
91 93 (void) fprintf(stderr,
92 94 gettext("\t-W width\n\t-h help\n"));
93 95 break;
94 96
95 97 case 'e':
96 98 (void) fprintf(stderr,
97 99 gettext("usage: %s [options] [choice [...]]\n"), prog);
98 100 (void) fprintf(stderr, gettext(OPTMESG));
99 101 (void) fprintf(stderr,
100 102 gettext("\t-W width\n\t-e error\n"));
101 103 break;
102 104 }
103 105 exit(1);
104 106 }
105 107
106 108 /*
107 109 * Given argv[0], return a pointer to the basename of the program.
108 110 */
109 111 static char *
110 112 prog_name(char *arg0)
111 113 {
112 114 char *str;
113 115
114 116 /* first strip trailing '/' characters (exec() allows these!) */
115 117 str = arg0 + strlen(arg0);
116 118 while (str > arg0 && *--str == '/')
117 119 *str = '\0';
118 120 if ((str = strrchr(arg0, '/')) != NULL)
119 121 return (str + 1);
120 122 return (arg0);
121 123 }
122 124
123 125 int
124 126 main(int argc, char **argv)
125 127 {
126 128 CKMENU *mp;
127 129 FILE *fp = NULL;
128 130 int c, i;
129 131 char **item;
130 132 char temp[LSIZE * MB_LEN_MAX];
131 133 size_t mmax;
132 134 size_t invismaxsize = INVISMAXSIZE;
133 135 size_t n, r;
134 136 wchar_t wline[LSIZE], wtemp[LSIZE];
135 137
136 138 (void) setlocale(LC_ALL, "");
137 139
138 140 #if !defined(TEXT_DOMAIN)
↓ open down ↓ |
97 lines elided |
↑ open up ↑ |
139 141 #define TEXT_DOMAIN "SYS_TEST"
140 142 #endif
141 143 (void) textdomain(TEXT_DOMAIN);
142 144
143 145 prog = prog_name(argv[0]);
144 146
145 147 invis = (char **)calloc(invismaxsize, sizeof (char *));
146 148 if (!invis) {
147 149 (void) fprintf(stderr,
148 150 gettext("Not enough memory\n"));
149 - exit(1);
151 + exit(1);
150 152 }
151 153 while ((c = getopt(argc, argv, "m:oni:l:f:ud:p:e:h:k:s:QW:?")) != EOF) {
152 154 /* check for invalid option */
153 155 if ((*prog == 'e') && !strchr(eusage, c))
154 156 usage(); /* no valid options */
155 157 if ((*prog == 'h') && !strchr(husage, c))
156 158 usage();
157 159
158 160 switch (c) {
159 161 case 'Q':
160 162 ckquit = 0;
161 163 break;
162 164
163 165 case 'W':
164 166 ckwidth = atol(optarg);
165 167 if (ckwidth < 0) {
166 168 (void) fprintf(stderr,
167 169 gettext("%s: ERROR: negative display width specified\n"),
168 170 prog);
169 171 exit(1);
170 172 }
171 173 break;
172 174
173 175 case 'm':
174 176 max = atoi(optarg);
175 177 if (max > SHRT_MAX || max < SHRT_MIN) {
176 178 (void) fprintf(stderr,
177 179 gettext("%s: ERROR: too large or too small max value specified\n"),
178 180 prog);
179 181 exit(1);
180 182 }
181 183 break;
182 184
183 185 case 'o':
184 186 attr |= CKONEFLAG;
185 187 break;
186 188
187 189 case 'n':
188 190 attr &= ~CKALPHA;
189 191 break;
↓ open down ↓ |
30 lines elided |
↑ open up ↑ |
190 192
191 193 case 'i':
192 194 invis[ninvis++] = optarg;
193 195 if (ninvis == invismaxsize) {
194 196 invismaxsize += INVISMAXSIZE;
195 197 invis = (char **)realloc(invis,
196 198 invismaxsize * sizeof (char *));
197 199 if (!invis) {
198 200 (void) fprintf(stderr,
199 201 gettext("Not enough memory\n"));
200 - exit(1);
202 + exit(1);
201 203 }
202 204 (void) memset(invis + ninvis, 0,
203 205 (invismaxsize - ninvis) *
204 206 sizeof (char *));
205 207 }
206 208 break;
207 209
208 210 case 'l':
209 211 label = optarg;
210 212 break;
211 213
212 214 case 'f':
213 215 if ((fp = fopen(optarg, "r")) == NULL) {
214 216 (void) fprintf(stderr,
215 217 gettext("%s: ERROR: can't open %s\n"),
216 218 prog, optarg);
217 219 exit(1);
218 220 }
219 221 break;
220 222
221 223 case 'u':
222 224 attr |= CKUNNUM;
223 225 break;
224 226
225 227 case 'd':
226 228 deflt = optarg;
227 229 break;
228 230
229 231 case 'p':
230 232 prompt = optarg;
231 233 break;
232 234
233 235 case 'e':
234 236 error = optarg;
235 237 break;
236 238
237 239 case 'h':
238 240 help = optarg;
239 241 break;
240 242
241 243 case 'k':
242 244 kpid = atoi(optarg);
243 245 break;
244 246
245 247 case 's':
246 248 signo = atoi(optarg);
247 249 break;
248 250
249 251 default:
250 252 usage();
251 253 }
252 254 }
253 255
254 256 if (signo) {
255 257 if (kpid == BADPID)
256 258 usage();
257 259 } else
258 260 signo = SIGTERM;
259 261
260 262 mp = allocmenu(label, attr);
261 263 if (fp) {
262 264 *wtemp = L'\0';
263 265 while (fgetws(wline, LSIZE, fp)) {
264 266 /*
265 267 * Skip comment lines, those beginning with '#'.
266 268 * Note: AT&T forgot this & needs the next 2 lines.
267 269 */
268 270 if (*wline == L'#')
269 271 continue;
270 272 n = wcslen(wline);
271 273 if ((n != 0) && (wline[n - 1] == L'\n'))
272 274 wline[n - 1] = L'\0';
273 275 /*
274 276 * if the line begins with a space character,
275 277 * this is a continuous line to the previous line.
276 278 */
277 279 if (iswspace(*wline)) {
278 280 (void) wcscat(wtemp, L"\n");
279 281 (void) wcscat(wtemp, wline);
280 282 } else {
281 283 if (*wtemp) {
282 284 n = wcslen(wtemp);
283 285 r = wcstombs(temp, wtemp,
284 286 n * MB_LEN_MAX);
285 287 if (r == (size_t)-1) {
286 288 (void) fprintf(stderr,
287 289 gettext("Invalid character in the menu definition.\n"));
288 290 exit(1);
289 291 }
290 292 if (setitem(mp, temp)) {
291 293 (void) fprintf(stderr,
292 294 gettext(INTERR), prog);
293 295 exit(1);
294 296 }
295 297 }
296 298 (void) wcscpy(wtemp, wline);
297 299 }
298 300 }
299 301 if (*wtemp) {
300 302 n = wcslen(wtemp);
301 303 r = wcstombs(temp, wtemp, n * MB_LEN_MAX);
302 304 if (r == (size_t)-1) {
303 305 (void) fprintf(stderr,
304 306 gettext("Invalid character in the menu definition.\n"));
305 307 exit(1);
306 308 }
307 309 if (setitem(mp, temp)) {
308 310 (void) fprintf(stderr, gettext(INTERR), prog);
309 311 exit(1);
310 312 }
311 313 }
312 314 }
313 315
314 316 while (optind < argc) {
315 317 if (setitem(mp, argv[optind++])) {
316 318 (void) fprintf(stderr, gettext(INTERR), prog);
317 319 exit(1);
318 320 }
319 321 }
320 322
321 323 for (n = 0; n < ninvis; ) {
322 324 if (setinvis(mp, invis[n++])) {
323 325 (void) fprintf(stderr, gettext(INTERR), prog);
324 326 exit(1);
325 327 }
326 328 }
327 329
328 330 if (*prog == 'e') {
329 331 ckindent = 0;
330 332 ckitem_err(mp, error);
331 333 exit(0);
332 334 } else if (*prog == 'h') {
333 335 ckindent = 0;
334 336 ckitem_hlp(mp, help);
335 337 exit(0);
336 338 }
337 339
338 340 if (max < 1) {
339 341 mmax = mp->nchoices;
340 342 } else {
341 343 mmax = max;
342 344 }
343 345
344 346 /*
345 347 * if -o option is specified, mp->nchoices is 1, and if no invisible
346 348 * item is specified, ckitem() will consume two entries of item,
347 349 * even though 'max' is set to 1. So to take care of that problem, we
348 350 * allocate one extra element for item
349 351 */
350 352 item = (char **)calloc(mmax+1, sizeof (char *));
351 353 if (!item) {
352 354 (void) fprintf(stderr,
353 355 gettext("Not enough memory\n"));
354 356 exit(1);
355 357 }
356 358 n = ckitem(mp, item, max, deflt, error, help, prompt);
357 359 if (n == 3) {
358 360 if (kpid > -2)
359 361 (void) kill(kpid, signo);
360 362 (void) puts("q");
361 363 } else if (n == 0) {
362 364 i = 0;
363 365 while (item[i])
364 366 (void) puts(item[i++]);
365 367 }
366 368 return (n);
367 369 }
↓ open down ↓ |
157 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX