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/ul/ul.c
+++ new/usr/src/cmd/ul/ul.c
1 1 /*
2 2 * Copyright 2000 Sun Microsystems, Inc. All rights reserved.
3 3 * Use is subject to license terms.
4 4 */
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
5 5
6 6 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
7 7 /* All Rights Reserved */
8 8
9 9 /*
10 10 * Copyright (c) 1980 Regents of the University of California.
11 11 * All rights reserved. The Berkeley software License Agreement
12 12 * specifies the terms and conditions for redistribution.
13 13 */
14 14
15 -#pragma ident "%Z%%M% %I% %E% SMI"
15 +/*
16 + * Copyright (c) 2018, Joyent, Inc.
17 + */
16 18
17 19 #include <stdio.h>
18 20 #include <locale.h>
19 21 #include <wctype.h>
20 22 #include <widec.h>
21 23 #include <euc.h>
22 24 #include <getwidth.h>
23 25 #include <limits.h>
24 26 #include <stdlib.h>
25 27 #include <curses.h>
26 28 #include <term.h>
27 29 #include <string.h>
28 30
29 31 #define IESC L'\033'
30 32 #define SO L'\016'
31 33 #define SI L'\017'
32 34 #define HFWD L'9'
33 35 #define HREV L'8'
34 36 #define FREV L'7'
35 37 #define CDUMMY -1
36 38
37 39 #define NORMAL 000
38 40 #define ALTSET 001 /* Reverse */
39 41 #define SUPERSC 002 /* Dim */
40 42 #define SUBSC 004 /* Dim | Ul */
41 43 #define UNDERL 010 /* Ul */
42 44 #define BOLD 020 /* Bold */
43 45
44 46 #define MEMFCT 16
45 47 /*
46 48 * MEMFCT is a number that is likely to be large enough as a factor for
47 49 * allocating more memory and to be small enough so as not wasting memory
48 50 */
49 51
50 52 int must_use_uc, must_overstrike;
51 53 char *CURS_UP, *CURS_RIGHT, *CURS_LEFT,
52 54 *ENTER_STANDOUT, *EXIT_STANDOUT, *ENTER_UNDERLINE, *EXIT_UNDERLINE,
53 55 *ENTER_DIM, *ENTER_BOLD, *ENTER_REVERSE, *UNDER_CHAR, *EXIT_ATTRIBUTES;
54 56
55 57 struct CHAR {
56 58 char c_mode;
57 59 wchar_t c_char;
58 60 };
59 61
60 62 struct CHAR obuf[LINE_MAX];
61 63 int col, maxcol;
62 64 int mode;
63 65 int halfpos;
64 66 int upln;
65 67 int iflag;
66 68
67 69 eucwidth_t wp;
68 70 int scrw[4];
69 71
70 72 void setmode(int newmode);
71 73 void outc(wchar_t c);
72 74 int outchar(char c);
73 75 void initcap(void);
74 76 void reverse(void);
75 77 void fwd(void);
76 78 void initbuf(void);
77 79 void iattr(void);
78 80 void overstrike(void);
79 81 void flushln(void);
80 82 void ul_filter(FILE *f);
81 83 void ul_puts(char *str);
82 84
83 85 int
84 86 main(int argc, char **argv)
85 87 {
86 88 int c;
87 89 char *termtype;
88 90 FILE *f;
89 91 char termcap[1024];
90 92 extern int optind;
91 93 extern char *optarg;
92 94
93 95 (void) setlocale(LC_ALL, "");
94 96 #if !defined(TEXT_DOMAIN)
95 97 #define TEXT_DOMAIN "SYS_TEST"
96 98 #endif
97 99 (void) textdomain(TEXT_DOMAIN);
98 100
99 101 getwidth(&wp);
100 102 scrw[0] = 1;
101 103 scrw[1] = wp._scrw1;
102 104 scrw[2] = wp._scrw2;
103 105 scrw[3] = wp._scrw3;
104 106
105 107 termtype = getenv("TERM");
106 108 if (termtype == NULL || (argv[0][0] == 'c' && !isatty(1)))
107 109 termtype = "lpr";
108 110 while ((c = getopt(argc, argv, "it:T:")) != EOF)
109 111 switch (c) {
110 112
111 113 case 't':
112 114 case 'T': /* for nroff compatibility */
113 115 termtype = optarg;
114 116 break;
115 117 case 'i':
116 118 iflag = 1;
117 119 break;
118 120
119 121 default:
120 122 (void) fprintf(stderr,
121 123 gettext("\
122 124 Usage: %s [ -i ] [ -t terminal ] [ filename...]\n"),
123 125 argv[0]);
124 126 exit(1);
125 127 }
126 128
127 129 switch (tgetent(termcap, termtype)) {
128 130
129 131 case 1:
130 132 break;
131 133
132 134 default:
133 135 (void) fprintf(stderr, gettext("trouble reading termcap"));
134 136 /*FALLTHROUGH*/
135 137
136 138 case 0:
137 139 /* No such terminal type - assume dumb */
138 140 (void) strcpy(termcap, "dumb:os:col#80:cr=^M:sf=^J:am:");
139 141 break;
140 142 }
141 143 initcap();
142 144 if ((tgetflag("os") && ENTER_BOLD == NULL) || (tgetflag("ul") &&
143 145 ENTER_UNDERLINE == NULL && UNDER_CHAR == NULL))
144 146 must_overstrike = 1;
145 147 initbuf();
146 148 if (optind == argc)
147 149 ul_filter(stdin);
148 150 else for (; optind < argc; optind++) {
149 151 f = fopen(argv[optind], "r");
150 152 if (f == NULL) {
151 153 perror(argv[optind]);
152 154 exit(1);
153 155 } else
154 156 ul_filter(f);
155 157 }
156 158 return (0);
157 159 }
158 160
159 161 void
160 162 ul_filter(FILE *f)
161 163 {
162 164 wchar_t c;
163 165 int i;
164 166
165 167 while ((c = getwc(f)) != EOF) {
166 168 if (maxcol >= LINE_MAX) {
167 169 (void) fprintf(stderr,
168 170 gettext("Input line longer than %d characters\n"), LINE_MAX);
169 171 exit(1);
170 172 }
171 173 switch (c) {
172 174
173 175 case L'\b':
174 176 if (col > 0)
175 177 col--;
176 178 continue;
177 179
178 180 case L'\t':
179 181 col = (col+8) & ~07;
180 182 if (col > maxcol)
181 183 maxcol = col;
182 184 continue;
183 185
184 186 case L'\r':
185 187 col = 0;
186 188 continue;
187 189
188 190 case SO:
189 191 mode |= ALTSET;
190 192 continue;
191 193
192 194 case SI:
193 195 mode &= ~ALTSET;
194 196 continue;
195 197
196 198 case IESC:
197 199 switch (c = getwc(f)) {
198 200 case HREV:
199 201 if (halfpos == 0) {
200 202 mode |= SUPERSC;
201 203 halfpos--;
202 204 } else if (halfpos > 0) {
203 205 mode &= ~SUBSC;
204 206 halfpos--;
205 207 } else {
206 208 halfpos = 0;
207 209 reverse();
208 210 }
209 211 continue;
210 212
211 213 case HFWD:
212 214 if (halfpos == 0) {
213 215 mode |= SUBSC;
214 216 halfpos++;
215 217 } else if (halfpos < 0) {
216 218 mode &= ~SUPERSC;
217 219 halfpos++;
218 220 } else {
219 221 halfpos = 0;
220 222 fwd();
221 223 }
222 224 continue;
223 225 case FREV:
224 226 reverse();
225 227 continue;
226 228
227 229 default:
228 230 (void) fprintf(stderr,
229 231 gettext("Unknown escape sequence in input: %o, %o\n"),
230 232 IESC, c);
231 233 exit(1);
232 234 }
233 235 continue;
234 236
235 237 case L'_':
236 238 if (obuf[col].c_char)
237 239 obuf[col].c_mode |= UNDERL | mode;
238 240 else
239 241 obuf[col].c_char = '_';
240 242 /*FALLTHROUGH*/
241 243
242 244 case L' ':
243 245 col++;
244 246 if (col > maxcol)
245 247 maxcol = col;
246 248 continue;
247 249
248 250 case L'\n':
249 251 flushln();
250 252 continue;
251 253
252 254 default:
253 255 if (c < L' ') /* non printing */
254 256 continue;
255 257 if (obuf[col].c_char == L'\0') {
256 258 obuf[col].c_char = c;
257 259 obuf[col].c_mode = mode;
258 260 i = scrw[wcsetno(c)];
259 261 while (--i > 0)
260 262 obuf[++col].c_char = CDUMMY;
261 263 } else if (obuf[col].c_char == L'_') {
262 264 obuf[col].c_char = c;
263 265 obuf[col].c_mode |= UNDERL|mode;
264 266 i = scrw[wcsetno(c)];
265 267 while (--i > 0)
266 268 obuf[++col].c_char = CDUMMY;
267 269 } else if (obuf[col].c_char == c)
268 270 obuf[col].c_mode |= BOLD|mode;
269 271 else {
270 272 obuf[col].c_char = c;
271 273 obuf[col].c_mode = mode;
272 274 }
273 275 col++;
274 276 if (col > maxcol)
275 277 maxcol = col;
276 278 continue;
277 279 }
278 280 }
279 281 if (maxcol)
280 282 flushln();
281 283 }
282 284
283 285 void
284 286 flushln(void)
285 287 {
286 288 int lastmode;
287 289 int i;
288 290 int hadmodes = 0;
289 291
290 292 lastmode = NORMAL;
291 293 for (i = 0; i < maxcol; i++) {
292 294 if (obuf[i].c_mode != lastmode) {
293 295 hadmodes++;
294 296 setmode(obuf[i].c_mode);
295 297 lastmode = obuf[i].c_mode;
296 298 }
297 299 if (obuf[i].c_char == L'\0') {
298 300 if (upln) {
299 301 ul_puts(CURS_RIGHT);
300 302 } else
301 303 outc(L' ');
302 304 } else
303 305 outc(obuf[i].c_char);
304 306 }
305 307 if (lastmode != NORMAL) {
306 308 setmode(0);
307 309 }
308 310 if (must_overstrike && hadmodes)
309 311 overstrike();
310 312 (void) putwchar(L'\n');
311 313 if (iflag && hadmodes)
312 314 iattr();
313 315 if (upln)
314 316 upln--;
315 317 initbuf();
316 318 }
317 319
318 320 /*
319 321 * For terminals that can overstrike, overstrike underlines and bolds.
320 322 * We don't do anything with halfline ups and downs, or Greek.
321 323 */
322 324 void
323 325 overstrike(void)
324 326 {
325 327 int i, n;
326 328 wchar_t *cp, *scp;
327 329 size_t szbf = 256, tszbf;
328 330 int hadbold = 0;
329 331
330 332 scp = (wchar_t *)malloc(sizeof (wchar_t) * szbf);
331 333 if (!scp) {
332 334 /* this kind of message need not to be gettext'ed */
333 335 (void) fprintf(stderr, "malloc failed\n");
334 336 exit(1);
335 337 }
336 338 cp = scp;
337 339 tszbf = szbf;
338 340 #ifdef DEBUG
339 341 /*
340 342 * to allocate a memory after the chunk of the current scp
341 343 * and to make sure the following realloc() allocates
342 344 * memory from different chunks.
343 345 */
344 346 (void) malloc(1024 * 1024);
345 347 #endif
346 348
347 349 /* Set up overstrike buffer */
348 350 for (i = 0; i < maxcol; i++) {
349 351 n = scrw[wcsetno(obuf[i].c_char)];
350 352 if (tszbf <= n) {
351 353 /* may not enough buffer for this char */
352 354 size_t pos;
353 355
354 356 /* obtain the offset of cp */
355 357 pos = cp - scp;
356 358 /* reallocate another (n * MEMFCT) * sizeof (wchar_t) */
357 359 scp = (wchar_t *)realloc(scp,
358 360 sizeof (wchar_t) * (szbf + (n * MEMFCT)));
359 361 if (!scp) {
360 362 (void) fprintf(stderr, "malloc failed\n");
361 363 exit(1);
362 364 }
363 365 /* get the new address of cp */
364 366 cp = scp + pos;
365 367 szbf += n * MEMFCT;
366 368 tszbf += n * MEMFCT;
367 369 }
368 370 switch (obuf[i].c_mode) {
369 371 case NORMAL:
370 372 default:
371 373 tszbf -= n;
372 374 *cp++ = L' ';
373 375 while (--n > 0) {
374 376 *cp++ = L' ';
375 377 i++;
376 378 }
377 379 break;
378 380 case UNDERL:
379 381 tszbf -= n;
380 382 *cp++ = L'_';
381 383 while (--n > 0) {
382 384 *cp++ = L'_';
383 385 i++;
384 386 }
385 387 break;
386 388 case BOLD:
387 389 tszbf--;
388 390 *cp++ = obuf[i].c_char;
389 391 hadbold = 1;
390 392 break;
391 393 }
392 394 }
393 395 (void) putwchar(L'\r');
394 396 for (*cp = L' '; *cp == L' '; cp--)
395 397 *cp = L'\0';
396 398 for (cp = scp; *cp; cp++)
397 399 (void) putwchar(*cp);
398 400 if (hadbold) {
399 401 (void) putwchar(L'\r');
400 402 for (cp = scp; *cp; cp++)
401 403 (void) putwchar(*cp == L'_' ? L' ' : *cp);
402 404 (void) putwchar(L'\r');
403 405 for (cp = scp; *cp; cp++)
404 406 (void) putwchar(*cp == L'_' ? L' ' : *cp);
405 407 }
406 408 free(scp);
407 409 }
408 410
409 411 void
410 412 iattr(void)
411 413 {
412 414 int i, n;
413 415 wchar_t *cp, *scp;
414 416 wchar_t cx;
415 417 size_t szbf = 256, tszbf;
416 418
417 419 scp = (wchar_t *)malloc(sizeof (wchar_t) * szbf);
418 420 if (!scp) {
419 421 /* this kind of message need not to be gettext'ed */
420 422 (void) fprintf(stderr, "malloc failed\n");
421 423 exit(1);
422 424 }
423 425 cp = scp;
424 426 tszbf = szbf;
425 427 #ifdef DEBUG
426 428 /*
427 429 * to allocate a memory after the chunk of the current scp
428 430 * and to make sure the following realloc() allocates
429 431 * memory from different chunks.
430 432 */
431 433 (void) malloc(1024 * 1024);
432 434 #endif
433 435 for (i = 0; i < maxcol; i++) {
434 436 switch (obuf[i].c_mode) {
435 437 case NORMAL: cx = ' '; break;
436 438 case ALTSET: cx = 'g'; break;
437 439 case SUPERSC: cx = '^'; break;
438 440 case SUBSC: cx = 'v'; break;
439 441 case UNDERL: cx = '_'; break;
440 442 case BOLD: cx = '!'; break;
441 443 default: cx = 'X'; break;
442 444 }
443 445 n = scrw[wcsetno(obuf[i].c_char)];
444 446 if (tszbf <= n) {
445 447 /* may not enough buffer for this char */
446 448 size_t pos;
447 449
448 450 /* obtain the offset of cp */
449 451 pos = cp - scp;
450 452 /* reallocate another (n * MEMFCT) * sizeof (wchar_t) */
451 453 scp = (wchar_t *)realloc(scp,
452 454 sizeof (wchar_t) * (szbf + (n * MEMFCT)));
↓ open down ↓ |
427 lines elided |
↑ open up ↑ |
453 455 if (!scp) {
454 456 (void) fprintf(stderr, "malloc failed\n");
455 457 exit(1);
456 458 }
457 459 /* get the new address of cp */
458 460 cp = scp + pos;
459 461 szbf += n * MEMFCT;
460 462 tszbf += n * MEMFCT;
461 463 }
462 464 tszbf -= n;
463 - *cp++ = cx;
465 + *cp++ = cx;
464 466 while (--n > 0) {
465 467 *cp++ = cx;
466 468 i++;
467 469 }
468 470 }
469 471 for (*cp = L' '; *cp == L' '; cp--)
470 472 *cp = L'\0';
471 473 for (cp = scp; *cp; cp++)
472 474 (void) putwchar(*cp);
473 475 (void) putwchar(L'\n');
474 476 free(scp);
475 477 }
476 478
477 479 void
478 480 initbuf(void)
479 481 {
480 482 int i;
481 483
482 484 /* following depends on NORMAL == 000 */
483 485 for (i = 0; i < LINE_MAX; i++)
484 486 obuf[i].c_char = obuf[i].c_mode = 0;
485 487
486 488 col = 0;
487 489 maxcol = 0;
488 490 mode &= ALTSET;
489 491 }
490 492
491 493 void
492 494 fwd(void)
493 495 {
494 496 int oldcol, oldmax;
495 497
496 498 oldcol = col;
497 499 oldmax = maxcol;
498 500 flushln();
499 501 col = oldcol;
500 502 maxcol = oldmax;
501 503 }
502 504
503 505 void
504 506 reverse(void)
505 507 {
506 508 upln++;
507 509 fwd();
508 510 ul_puts(CURS_UP);
509 511 ul_puts(CURS_UP);
510 512 upln++;
511 513 }
512 514
513 515 void
514 516 initcap(void)
515 517 {
516 518 static char tcapbuf[512];
517 519 char *bp = tcapbuf;
518 520
519 521 /* This nonsense attempts to work with both old and new termcap */
520 522 CURS_UP = tgetstr("up", &bp);
521 523 CURS_RIGHT = tgetstr("ri", &bp);
522 524 if (CURS_RIGHT == NULL)
523 525 CURS_RIGHT = tgetstr("nd", &bp);
524 526 CURS_LEFT = tgetstr("le", &bp);
525 527 if (CURS_LEFT == NULL)
526 528 CURS_LEFT = tgetstr("bc", &bp);
527 529 if (CURS_LEFT == NULL && tgetflag("bs"))
528 530 CURS_LEFT = "\b";
529 531
530 532 ENTER_STANDOUT = tgetstr("so", &bp);
531 533 EXIT_STANDOUT = tgetstr("se", &bp);
532 534 ENTER_UNDERLINE = tgetstr("us", &bp);
533 535 EXIT_UNDERLINE = tgetstr("ue", &bp);
534 536 ENTER_DIM = tgetstr("mh", &bp);
535 537 ENTER_BOLD = tgetstr("md", &bp);
536 538 ENTER_REVERSE = tgetstr("mr", &bp);
537 539 EXIT_ATTRIBUTES = tgetstr("me", &bp);
538 540
539 541 if (!ENTER_BOLD && ENTER_REVERSE)
540 542 ENTER_BOLD = ENTER_REVERSE;
541 543 if (!ENTER_BOLD && ENTER_STANDOUT)
542 544 ENTER_BOLD = ENTER_STANDOUT;
543 545 if (!ENTER_UNDERLINE && ENTER_STANDOUT) {
544 546 ENTER_UNDERLINE = ENTER_STANDOUT;
545 547 EXIT_UNDERLINE = EXIT_STANDOUT;
546 548 }
547 549 if (!ENTER_DIM && ENTER_STANDOUT)
548 550 ENTER_DIM = ENTER_STANDOUT;
549 551 if (!ENTER_REVERSE && ENTER_STANDOUT)
550 552 ENTER_REVERSE = ENTER_STANDOUT;
551 553 if (!EXIT_ATTRIBUTES && EXIT_STANDOUT)
552 554 EXIT_ATTRIBUTES = EXIT_STANDOUT;
553 555
554 556 /*
555 557 * Note that we use REVERSE for the alternate character set,
556 558 * not the as/ae capabilities. This is because we are modelling
557 559 * the model 37 teletype (since that's what nroff outputs) and
558 560 * the typical as/ae is more of a graphics set, not the greek
559 561 * letters the 37 has.
560 562 */
561 563
562 564 #ifdef notdef
563 565 printf("so %s se %s us %s ue %s me %s\n",
564 566 ENTER_STANDOUT, EXIT_STANDOUT, ENTER_UNDERLINE,
565 567 EXIT_UNDERLINE, EXIT_ATTRIBUTES);
566 568 #endif
567 569 UNDER_CHAR = tgetstr("uc", &bp);
568 570 must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
569 571 }
570 572
571 573 int
572 574 outchar(char c)
573 575 {
574 576 (void) putchar(c&0177);
575 577 return (0);
576 578 }
577 579
578 580 void
579 581 ul_puts(char *str)
580 582 {
581 583 if (str)
582 584 (void) tputs(str, 1, outchar);
583 585 }
584 586
585 587 static int curmode = 0;
586 588
587 589 void
588 590 outc(wchar_t c)
589 591 {
590 592 int m, n;
591 593
592 594 if (c == CDUMMY)
593 595 return;
594 596 (void) putwchar(c);
595 597 if (must_use_uc && (curmode & UNDERL)) {
596 598 m = n = scrw[wcsetno(c)];
597 599 ul_puts(CURS_LEFT);
598 600 while (--m > 0)
599 601 ul_puts(CURS_LEFT);
600 602 ul_puts(UNDER_CHAR);
601 603 while (--n > 0)
602 604 ul_puts(UNDER_CHAR);
603 605 }
604 606 }
605 607
606 608 void
607 609 setmode(int newmode)
608 610 {
609 611 if (!iflag) {
610 612 if (curmode != NORMAL && newmode != NORMAL)
611 613 setmode(NORMAL);
612 614 switch (newmode) {
613 615 case NORMAL:
614 616 switch (curmode) {
615 617 case NORMAL:
616 618 break;
617 619 case UNDERL:
618 620 ul_puts(EXIT_UNDERLINE);
619 621 break;
620 622 default:
621 623 /* This includes standout */
622 624 ul_puts(EXIT_ATTRIBUTES);
623 625 break;
624 626 }
625 627 break;
626 628 case ALTSET:
627 629 ul_puts(ENTER_REVERSE);
628 630 break;
629 631 case SUPERSC:
630 632 /*
631 633 * This only works on a few terminals.
632 634 * It should be fixed.
633 635 */
634 636 ul_puts(ENTER_UNDERLINE);
635 637 ul_puts(ENTER_DIM);
636 638 break;
637 639 case SUBSC:
638 640 ul_puts(ENTER_DIM);
639 641 break;
640 642 case UNDERL:
641 643 ul_puts(ENTER_UNDERLINE);
642 644 break;
643 645 case BOLD:
644 646 ul_puts(ENTER_BOLD);
645 647 break;
646 648 default:
647 649 /*
648 650 * We should have some provision here for multiple modes
649 651 * on at once. This will have to come later.
650 652 */
651 653 ul_puts(ENTER_STANDOUT);
652 654 break;
653 655 }
654 656 }
655 657 curmode = newmode;
656 658 }
↓ open down ↓ |
183 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX