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/sgs/m4/common/m4.c
+++ new/usr/src/cmd/sgs/m4/common/m4.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 (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
↓ open down ↓ |
22 lines elided |
↑ open up ↑ |
23 23 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 25
26 26 /*
27 27 * Copyright (c) 2011 Gary Mills
28 28 */
29 29
30 30 /* Copyright (c) 1988 AT&T */
31 31 /* All Rights Reserved */
32 32
33 +/*
34 + * Copyright (c) 2018, Joyent, Inc.
35 + */
36 +
33 37 #include <signal.h>
34 38 #include <unistd.h>
35 39 #include <fcntl.h>
36 40 #include "m4.h"
37 41
38 42 #if defined(__lint)
39 43 extern int yydebug;
40 44 #endif
41 45
42 46 #define match(c, s) (c == *s && (!s[1] || inpmatch(s+1)))
43 47
44 48 static char tmp_name[] = "/tmp/m4aXXXXX";
45 49 static wchar_t prev_char;
46 50 static int mb_cur_max;
47 51
48 52 static void getflags(int *, char ***, int *);
49 53 static void initalloc(void);
50 54 static void expand(wchar_t **, int);
51 55 static void lnsync(FILE *);
52 56 static void fpath(FILE *);
53 57 static void puttok(wchar_t *);
54 58 static void error3(void);
55 59 static wchar_t itochr(int);
56 60 /*LINTED: E_STATIC_UNUSED*/
57 61 static wchar_t *chkbltin(wchar_t *);
58 62 static wchar_t *inpmatch(wchar_t *);
59 63 static void chkspace(char **, int *, char ***);
60 64 static void catchsig(int);
61 65 static FILE *m4open(char ***, char *, int *);
62 66 static void showwrap(void);
63 67 static void sputchr(wchar_t, FILE *);
64 68 static void putchr(wchar_t);
65 69 static void *xcalloc(size_t, size_t);
66 70 static wint_t myfgetwc(FILE *, int);
67 71 static wint_t myfputwc(wchar_t, FILE *);
68 72 static int myfeof(int);
69 73
70 74 int
71 75 main(int argc, char **argv)
72 76 {
73 77 wchar_t t;
74 78 int i, opt_end = 0;
75 79 int sigs[] = {SIGHUP, SIGINT, SIGPIPE, 0};
76 80
77 81 #if defined(__lint)
78 82 yydebug = 0;
79 83 #endif
80 84
81 85 for (i = 0; sigs[i]; ++i) {
82 86 if (signal(sigs[i], SIG_IGN) != SIG_IGN)
83 87 (void) signal(sigs[i], catchsig);
84 88 }
85 89 tempfile = mktemp(tmp_name);
86 90 (void) close(creat(tempfile, 0));
87 91
88 92 (void) setlocale(LC_ALL, "");
89 93
90 94 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
91 95 #define TEXT_DOMAIN "SYS_TEST"
92 96 #endif
93 97 (void) textdomain(TEXT_DOMAIN);
94 98
95 99 if ((mb_cur_max = MB_CUR_MAX) > 1)
96 100 wide = 1;
97 101
98 102 procnam = argv[0];
99 103 getflags(&argc, &argv, &opt_end);
100 104 initalloc();
101 105
102 106 setfname("-");
103 107 if (argc > 1) {
104 108 --argc;
105 109 ++argv;
106 110 if (strcmp(argv[0], "-")) {
107 111 ifile[ifx] = m4open(&argv, "r", &argc);
108 112 setfname(argv[0]);
109 113 }
110 114 }
111 115
112 116 for (;;) {
113 117 token[0] = t = getchr();
114 118 token[1] = EOS;
115 119
116 120 if (t == WEOF) {
117 121 if (ifx > 0) {
118 122 (void) fclose(ifile[ifx]);
119 123 ipflr = ipstk[--ifx];
120 124 continue;
121 125 }
122 126
123 127 getflags(&argc, &argv, &opt_end);
124 128
125 129 if (argc <= 1)
126 130 /*
127 131 * If dowrap() has been called, the m4wrap
128 132 * macro has been processed, and a linked
129 133 * list of m4wrap strings has been created.
130 134 * The list starts at wrapstart.
131 135 */
132 136 if (wrapstart) {
133 137 /*
134 138 * Now that EOF has been processed,
135 139 * display the m4wrap strings.
136 140 */
137 141 showwrap();
138 142 continue;
139 143 } else
140 144 break;
141 145 --argc;
142 146 ++argv;
143 147
144 148 if (ifile[ifx] != stdin)
145 149 (void) fclose(ifile[ifx]);
146 150
147 151 if (strcmp(argv[0], "-"))
148 152 ifile[ifx] = m4open(&argv, "r", &argc);
149 153 else
150 154 ifile[ifx] = stdin;
151 155
152 156 setfname(argv[0]);
153 157 continue;
154 158 }
155 159
156 160 if (is_alpha(t) || t == '_') {
157 161 wchar_t *tp = token+1;
158 162 int tlim = toksize;
159 163 struct nlist *macadd; /* temp variable */
160 164
161 165 while ((*tp = getchr()) != WEOF &&
162 166 (is_alnum(*tp) || *tp == '_')) {
163 167 tp++;
164 168 if (--tlim <= 0)
165 169 error2(gettext(
166 170 "more than %d chars in word"),
167 171 toksize);
168 172 }
169 173 putbak(*tp);
170 174 *tp = EOS;
171 175
172 176 macadd = lookup(token);
173 177 *Ap = (wchar_t *)macadd;
174 178 if (macadd->def) {
175 179 if ((wchar_t *)(++Ap) >= astklm) {
176 180 --Ap;
177 181 error2(gettext(
178 182 "more than %d items on "
179 183 "argument stack"),
180 184 stksize);
181 185 }
182 186
183 187 if (Cp++ == NULL)
184 188 Cp = callst;
185 189
186 190 Cp->argp = Ap;
187 191 *Ap++ = op;
188 192 puttok(token);
189 193 stkchr(EOS);
190 194 t = getchr();
191 195 putbak(t);
192 196
193 197 if (t != '(')
194 198 pbstr(L"()");
195 199 else /* try to fix arg count */
196 200 *Ap++ = op;
197 201
198 202 Cp->plev = 0;
199 203 } else {
200 204 puttok(token);
201 205 }
202 206 } else if (match(t, lquote)) {
203 207 int qlev = 1;
204 208
205 209 for (;;) {
206 210 token[0] = t = getchr();
207 211 token[1] = EOS;
208 212
209 213 if (match(t, rquote)) {
210 214 if (--qlev > 0)
211 215 puttok(token);
212 216 else
213 217 break;
214 218 } else if (match(t, lquote)) {
215 219 ++qlev;
216 220 puttok(token);
217 221 } else {
218 222 if (t == WEOF)
219 223 error(gettext(
220 224 "EOF in quote"));
221 225 putchr(t);
222 226 }
223 227 }
224 228 } else if (match(t, lcom) &&
225 229 ((lcom[0] != L'#' || lcom[1] != L'\0') ||
226 230 prev_char != '$')) {
227 231
228 232 /*
229 233 * Don't expand commented macro (between lcom and
230 234 * rcom).
231 235 * What we know so far is that we have found the
232 236 * left comment char (lcom).
233 237 * Make sure we haven't found '#' (lcom) immediately
234 238 * preceded by '$' because we want to expand "$#".
235 239 */
236 240
237 241 puttok(token);
238 242 for (;;) {
239 243 token[0] = t = getchr();
240 244 token[1] = EOS;
241 245 if (match(t, rcom)) {
242 246 puttok(token);
243 247 break;
244 248 } else {
245 249 if (t == WEOF)
246 250 error(gettext(
247 251 "EOF in comment"));
248 252 putchr(t);
249 253 }
250 254 }
251 255 } else if (Cp == NULL) {
252 256 putchr(t);
253 257 } else if (t == '(') {
254 258 if (Cp->plev)
255 259 stkchr(t);
256 260 else {
257 261 /* skip white before arg */
258 262 while ((t = getchr()) != WEOF && is_space(t))
259 263 ;
260 264
261 265 putbak(t);
262 266 }
263 267
264 268 ++Cp->plev;
265 269 } else if (t == ')') {
266 270 --Cp->plev;
267 271
268 272 if (Cp->plev == 0) {
269 273 stkchr(EOS);
270 274 expand(Cp->argp, Ap-Cp->argp-1);
271 275 op = *Cp->argp;
272 276 Ap = Cp->argp-1;
273 277
274 278 if (--Cp < callst)
275 279 Cp = NULL;
276 280 } else
277 281 stkchr(t);
278 282 } else if (t == ',' && Cp->plev <= 1) {
279 283 stkchr(EOS);
280 284 *Ap = op;
281 285
282 286 if ((wchar_t *)(++Ap) >= astklm) {
283 287 --Ap;
284 288 error2(gettext(
285 289 "more than %d items on argument stack"),
286 290 stksize);
287 291 }
288 292
289 293 while ((t = getchr()) != WEOF && is_space(t))
290 294 ;
291 295
292 296 putbak(t);
293 297 } else {
294 298 stkchr(t);
295 299 }
296 300 }
297 301
298 302 if (Cp != NULL)
299 303 error(gettext(
300 304 "EOF in argument list"));
301 305
302 306 delexit(exitstat, 1);
303 307 return (0);
304 308 }
305 309
306 310 static wchar_t *
307 311 inpmatch(wchar_t *s)
308 312 {
309 313 wchar_t *tp = token+1;
310 314
311 315 while (*s) {
312 316 *tp = getchr();
313 317
314 318 if (*tp++ != *s++) {
315 319 *tp = EOS;
316 320 pbstr(token+1);
317 321 return (0);
318 322 }
319 323 }
320 324
321 325 *tp = EOS;
322 326 return (token);
323 327 }
324 328
325 329 static void
326 330 getflags(int *xargc, char ***xargv, int *option_end)
327 331 {
328 332 char *arg;
329 333 char *t;
330 334 wchar_t *s[3];
331 335
332 336 while (*xargc > 1) {
333 337 arg = (*xargv)[1]; /* point arg to current argument */
334 338
335 339 /*
336 340 * This argument is not an option if it equals "-" or if
337 341 * "--" has already been parsed.
338 342 */
339 343 if (arg[0] != '-' || arg[1] == EOS || *option_end)
340 344 break;
341 345 if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0') {
342 346 *option_end = 1;
343 347 } else {
344 348 switch (arg[1]) {
345 349 case 'B':
346 350 chkspace(&arg, xargc, xargv);
347 351 bufsize = atoi(&arg[2]);
348 352 if (bufsize <= 0) {
349 353 bufsize = DEF_BUFSIZE;
350 354 }
351 355 break;
352 356 case 'D':
353 357 initalloc();
354 358 chkspace(&arg, xargc, xargv);
355 359 for (t = &arg[2]; *t; t++) {
356 360 if (*t == '=') {
357 361 *t++ = EOS;
358 362 break;
359 363 }
360 364 }
361 365 s[1] = str2wstr(&arg[2], 1);
362 366 s[2] = str2wstr(t, 1);
363 367 dodef(&s[0], 2);
364 368 free(s[1]);
365 369 free(s[2]);
366 370 break;
367 371 case 'H':
368 372 chkspace(&arg, xargc, xargv);
369 373 hshsize = atoi(&arg[2]);
370 374 if (hshsize <= 0) {
371 375 hshsize = DEF_HSHSIZE;
372 376 }
373 377 break;
374 378 case 'S':
375 379 chkspace(&arg, xargc, xargv);
376 380 stksize = atoi(&arg[2]);
377 381 if (stksize <= 0) {
378 382 stksize = DEF_STKSIZE;
379 383 }
380 384 break;
381 385 case 'T':
382 386 chkspace(&arg, xargc, xargv);
383 387 toksize = atoi(&arg[2]);
384 388 if (toksize <= 0) {
385 389 toksize = DEF_TOKSIZE;
386 390 }
387 391 break;
388 392 case 'U':
389 393 initalloc();
390 394 chkspace(&arg, xargc, xargv);
391 395 s[1] = str2wstr(&arg[2], 1);
392 396 doundef(&s[0], 1);
393 397 free(s[1]);
394 398 break;
395 399 case 'e':
396 400 setbuf(stdout, NULL);
397 401 (void) signal(SIGINT, SIG_IGN);
398 402 break;
399 403 case 's':
400 404 /* turn on line sync */
401 405 sflag = 1;
402 406 break;
403 407 default:
404 408 (void) fprintf(stderr,
405 409 gettext("%s: bad option: %s\n"),
406 410 procnam, arg);
407 411 delexit(NOT_OK, 0);
408 412 }
409 413 } /* end else not "--" */
410 414
411 415 (*xargv)++;
412 416 --(*xargc);
413 417 } /* end while options to process */
414 418 }
415 419
416 420 /*
417 421 * Function: chkspace
418 422 *
419 423 * If there is a space between the option and its argument,
420 424 * adjust argptr so that &arg[2] will point to beginning of the option argument.
421 425 * This will ensure that processing in getflags() will work, because &arg[2]
422 426 * will point to the beginning of the option argument whether or not we have
423 427 * a space between the option and its argument. If there is a space between
424 428 * the option and its argument, also adjust xargv and xargc because we are
425 429 * processing the next argument.
426 430 */
427 431 static void
428 432 chkspace(char **argptr, int *xargc, char ***xargv)
429 433 {
430 434 if ((*argptr)[2] == EOS) {
431 435 /* there is a space between the option and its argument */
432 436 (*xargv)++; /* look at the next argument */
433 437 --(*xargc);
434 438 /*
435 439 * Adjust argptr if the option is followed by an
436 440 * option argument.
437 441 */
438 442 if (*xargc > 1) {
439 443 *argptr = (*xargv)[1];
440 444 /* point &arg[2] to beginning of option argument */
441 445 *argptr -= 2;
442 446 }
443 447 }
444 448 }
445 449
446 450 static void
447 451 initalloc(void)
448 452 {
449 453 static int done = 0;
450 454 int t;
451 455
452 456 if (done++)
453 457 return;
454 458
455 459 hshtab = xcalloc(hshsize, sizeof (struct nlist *));
456 460 callst = xcalloc(stksize/3+1, sizeof (struct call));
457 461 Ap = argstk = xcalloc(stksize+3, sizeof (wchar_t *));
458 462 ipstk[0] = ipflr = ip = ibuf = xcalloc(bufsize+1, sizeof (wchar_t));
459 463 op = obuf = xcalloc(bufsize+1, sizeof (wchar_t));
460 464 token = xcalloc(toksize+1, sizeof (wchar_t));
461 465
462 466 astklm = (wchar_t *)(&argstk[stksize]);
463 467 ibuflm = &ibuf[bufsize];
464 468 obuflm = &obuf[bufsize];
465 469 toklm = &token[toksize];
466 470
467 471 for (t = 0; barray[t].bname; ++t) {
468 472 wchar_t p[2] = {0, EOS};
469 473
470 474 p[0] = builtin(t);
471 475 install(barray[t].bname, p, NOPUSH);
472 476 }
473 477 install(L"unix", nullstr, NOPUSH);
474 478 }
475 479
476 480 void
477 481 install(wchar_t *nam, wchar_t *val, int mode)
478 482 {
479 483 struct nlist *np;
480 484 wchar_t *cp;
481 485 int l;
482 486
483 487 if (mode == PUSH)
484 488 (void) lookup(nam); /* lookup sets hshval */
485 489 else
486 490 while (undef(nam)) /* undef calls lookup */
487 491 ;
488 492
489 493 np = xcalloc(1, sizeof (*np));
490 494 np->name = wstrdup(nam);
491 495 np->next = hshtab[hshval];
492 496 hshtab[hshval] = np;
493 497
494 498 cp = xcalloc((l = wcslen(val))+1, sizeof (*val));
495 499 np->def = cp;
496 500 cp = &cp[l];
497 501
498 502 while (*val)
499 503 *--cp = *val++;
500 504 }
501 505
502 506 struct nlist *
503 507 lookup(wchar_t *str)
504 508 {
505 509 wchar_t *s1;
506 510 struct nlist *np;
507 511 static struct nlist nodef;
508 512
509 513 s1 = str;
510 514
511 515 for (hshval = 0; *s1; )
512 516 hshval += *s1++;
513 517
514 518 hshval %= hshsize;
515 519
516 520 for (np = hshtab[hshval]; np != NULL; np = np->next) {
517 521 if (*str == *np->name && wcscmp(str, np->name) == 0)
518 522 return (np);
519 523 }
520 524 return (&nodef);
521 525 }
522 526
523 527 static void
524 528 expand(wchar_t **a1, int c)
525 529 {
526 530 wchar_t *dp;
527 531 struct nlist *sp;
528 532
529 533 sp = (struct nlist *)a1[-1];
530 534
531 535 if (sp->tflag || trace) {
532 536 #if !defined(__lint) /* lint doesn't grok "%ws" */
533 537 int i;
534 538
535 539 (void) fprintf(stderr,
536 540 "Trace(%d): %ws", Cp-callst, a1[0]);
537 541 #endif
538 542
539 543 if (c > 0) {
540 544 #if !defined(__lint) /* lint doesn't grok "%ws" */
541 545 (void) fprintf(stderr, "(%ws", chkbltin(a1[1]));
542 546 for (i = 2; i <= c; ++i)
543 547 (void) fprintf(stderr, ",%ws", chkbltin(a1[i]));
544 548 #endif
545 549 (void) fprintf(stderr, ")");
546 550 }
547 551 (void) fprintf(stderr, "\n");
548 552 }
549 553
550 554 dp = sp->def;
551 555
552 556 for (; *dp; ++dp) {
553 557 if (is_builtin(*dp)) {
554 558 (*barray[builtin_idx(*dp)].bfunc)(a1, c);
555 559 } else if (dp[1] == '$') {
556 560 if (is_digit(*dp)) {
557 561 int n;
558 562 if ((n = *dp-'0') <= c)
559 563 pbstr(a1[n]);
560 564 ++dp;
561 565 } else if (*dp == '#') {
562 566 pbnum((long)c);
563 567 ++dp;
564 568 } else if (*dp == '*' || *dp == '@') {
565 569 int i = c;
566 570 wchar_t **a = a1;
567 571
568 572 if (i > 0)
↓ open down ↓ |
526 lines elided |
↑ open up ↑ |
569 573 for (;;) {
570 574 if (*dp == '@')
571 575 pbstr(rquote);
572 576
573 577 pbstr(a[i--]);
574 578
575 579 if (*dp == '@')
576 580 pbstr(lquote);
577 581
578 582 if (i <= 0)
579 - break;
583 + break;
580 584
581 585 pbstr(L",");
582 586 }
583 587 ++dp;
584 588 } else
585 589 putbak(*dp);
586 590 } else
587 591 putbak(*dp);
588 592 }
589 593 }
590 594
591 595 void
592 596 setfname(char *s)
593 597 {
594 598 if (fname[ifx])
595 599 free(fname[ifx]);
596 600 if ((fname[ifx] = strdup(s)) == NULL)
597 601 error(gettext("out of storage"));
598 602 fline[ifx] = 1;
599 603 nflag = 1;
600 604 lnsync(stdout);
601 605 }
602 606
603 607 static void
604 608 lnsync(FILE *iop)
605 609 {
606 610 static int cline = 0;
607 611 static int cfile = 0;
608 612
609 613 if (!sflag || iop != stdout)
610 614 return;
611 615
612 616 if (nflag || ifx != cfile) {
613 617 nflag = 0;
614 618 cfile = ifx;
615 619 (void) fprintf(iop, "#line %d \"", cline = fline[ifx]);
616 620 fpath(iop);
617 621 (void) fprintf(iop, "\"\n");
618 622 } else if (++cline != fline[ifx])
619 623 (void) fprintf(iop, "#line %d\n", cline = fline[ifx]);
620 624 }
621 625
622 626 static void
623 627 fpath(FILE *iop)
624 628 {
625 629 int i;
626 630
627 631 if (fname[0] == NULL)
628 632 return;
629 633
630 634 (void) fprintf(iop, "%s", fname[0]);
631 635
632 636 for (i = 1; i <= ifx; ++i)
633 637 (void) fprintf(iop, ":%s", fname[i]);
634 638 }
635 639
636 640 /* ARGSUSED */
637 641 static void
638 642 catchsig(int i)
639 643 {
640 644 (void) signal(SIGHUP, SIG_IGN);
641 645 (void) signal(SIGINT, SIG_IGN);
642 646 delexit(NOT_OK, 0);
643 647 }
644 648
645 649 void
646 650 delexit(int code, int flushio)
647 651 {
648 652 int i;
649 653
650 654 cf = stdout;
651 655
652 656 /*
653 657 * if (ofx != 0) {
654 658 * ofx = 0;
655 659 * code = NOT_OK;
656 660 * }
657 661 */
658 662 ofx = 0; /* ensure that everything comes out */
659 663 for (i = 1; i < 10; i++)
660 664 undiv(i, code);
661 665
662 666 tempfile[7] = 'a';
663 667 (void) unlink(tempfile);
664 668
665 669 /* flush standard I/O buffers, ie: call exit() not _exit() */
666 670 if (flushio)
667 671 exit(code);
668 672
669 673 _exit(code);
670 674 }
671 675
672 676 static void
673 677 puttok(wchar_t *tp)
674 678 {
675 679 if (Cp) {
676 680 while (*tp)
677 681 stkchr(*tp++);
678 682 } else if (cf) {
679 683 while (*tp) {
680 684 sputchr(*tp++, cf);
681 685 }
682 686 }
683 687 }
684 688
685 689 void
686 690 pbstr(wchar_t *str)
687 691 {
688 692 wchar_t *p;
689 693
690 694 for (p = str + wcslen(str); --p >= str; )
691 695 putbak(*p);
692 696 }
693 697
694 698 void
695 699 undiv(int i, int code)
696 700 {
697 701 FILE *fp;
698 702 wint_t c;
699 703
700 704 if (i < 1 || i > 9 || i == ofx || !ofile[i])
701 705 return;
702 706
703 707 (void) fclose(ofile[i]);
704 708 tempfile[7] = 'a'+i;
705 709
706 710 if (code == OK && cf) {
707 711 fp = xfopen(tempfile, "r");
708 712
709 713 if (wide) {
710 714 while ((c = myfgetwc(fp, -1)) != WEOF)
711 715 sputchr((wchar_t)c, cf);
712 716 } else {
713 717 while ((c = (wint_t)getc(fp)) != WEOF)
714 718 sputchr((wchar_t)c, cf);
715 719 }
716 720
717 721 (void) fclose(fp);
718 722 }
719 723
720 724 (void) unlink(tempfile);
721 725 ofile[i] = NULL;
722 726 }
723 727
724 728 void
725 729 pbnum(long num)
726 730 {
727 731 pbnbr(num, 10, 1);
728 732 }
729 733
730 734 void
731 735 pbnbr(long nbr, int base, int len)
732 736 {
733 737 int neg = 0;
734 738
735 739 if (base <= 0)
736 740 return;
737 741
738 742 if (nbr < 0)
739 743 neg = 1;
740 744 else
741 745 nbr = -nbr;
742 746
743 747 while (nbr < 0) {
744 748 int i;
745 749 if (base > 1) {
746 750 i = nbr%base;
747 751 nbr /= base;
748 752 #if (-3 % 2) != -1
749 753 while (i > 0) {
750 754 i -= base;
751 755 ++nbr;
752 756 }
753 757 #endif
754 758 i = -i;
755 759 } else {
756 760 i = 1;
757 761 ++nbr;
758 762 }
759 763 putbak(itochr(i));
760 764 --len;
761 765 }
762 766
763 767 while (--len >= 0)
764 768 putbak('0');
765 769
766 770 if (neg)
767 771 putbak('-');
768 772 }
769 773
770 774 static wchar_t
771 775 itochr(int i)
772 776 {
773 777 if (i > 9)
774 778 return ((wchar_t)(i-10+'A'));
775 779 else
776 780 return ((wchar_t)(i+'0'));
777 781 }
778 782
779 783 long
780 784 ctol(wchar_t *str)
781 785 {
782 786 int sign;
783 787 long num;
784 788
785 789 while (is_space(*str))
786 790 ++str;
787 791 num = 0;
788 792 if (*str == '-') {
789 793 sign = -1;
790 794 ++str;
791 795 } else
792 796 sign = 1;
793 797 while (is_digit(*str))
794 798 num = num*10 + *str++ - '0';
795 799 return (sign * num);
796 800 }
797 801
798 802 int
799 803 min(int a, int b)
800 804 {
801 805 if (a > b)
802 806 return (b);
803 807 return (a);
804 808 }
805 809
806 810 FILE *
807 811 xfopen(char *name, char *mode)
808 812 {
809 813 FILE *fp;
810 814
811 815 if ((fp = fopen(name, mode)) == NULL)
812 816 errorf(gettext("cannot open file: %s"),
813 817 strerror(errno));
814 818
815 819 return (fp);
816 820 }
817 821
818 822 /*
819 823 * m4open
820 824 *
821 825 * Continue processing files when unable to open the given file argument.
822 826 */
823 827 FILE *
824 828 m4open(char ***argvec, char *mode, int *argcnt)
825 829 {
826 830 FILE *fp;
827 831 char *arg;
828 832
829 833 while (*argcnt > 0) {
830 834 arg = (*argvec)[0]; /* point arg to current file name */
831 835 if (arg[0] == '-' && arg[1] == EOS)
832 836 return (stdin);
833 837 else {
834 838 if ((fp = fopen(arg, mode)) == NULL) {
835 839 (void) fprintf(stderr, gettext(
836 840 "m4: cannot open %s: "), arg);
837 841 perror("");
838 842 if (*argcnt == 1) {
839 843 /* last arg therefore exit */
840 844 error3();
841 845 } else {
842 846 exitstat = 1;
843 847 (*argvec)++; /* try next arg */
844 848 (*argcnt)--;
845 849 }
846 850 } else
847 851 break;
848 852 }
849 853 }
850 854 return (fp);
851 855 }
852 856
853 857 void *
854 858 xmalloc(size_t size)
855 859 {
856 860 void *ptr;
857 861
858 862 if ((ptr = malloc(size)) == NULL)
859 863 error(gettext("out of storage"));
860 864 return (ptr);
861 865 }
862 866
863 867 static void *
864 868 xcalloc(size_t nbr, size_t size)
865 869 {
866 870 void *ptr;
867 871
868 872 ptr = xmalloc(nbr * size);
869 873 (void) memset(ptr, '\0', nbr * size);
870 874 return (ptr);
871 875 }
872 876
873 877 /* Typical format: "cannot open file: %s" */
874 878 /* PRINTFLIKE1 */
875 879 void
876 880 errorf(char *str, char *serr)
877 881 {
878 882 char buf[500];
879 883
880 884 (void) snprintf(buf, sizeof (buf), str, serr);
881 885 error(buf);
882 886 }
883 887
884 888 /* PRINTFLIKE1 */
885 889 void
886 890 error2(char *str, int num)
887 891 {
888 892 char buf[500];
889 893
890 894 (void) snprintf(buf, sizeof (buf), str, num);
891 895 error(buf);
892 896 }
893 897
894 898 void
895 899 error(char *str)
896 900 {
897 901 (void) fprintf(stderr, "\n%s:", procnam);
898 902 fpath(stderr);
899 903 (void) fprintf(stderr, ":%d %s\n", fline[ifx], str);
900 904 error3();
901 905 }
902 906
903 907 static void
904 908 error3()
905 909 {
906 910 if (Cp) {
907 911 struct call *mptr;
908 912
909 913 /* fix limit */
910 914 *op = EOS;
911 915 (Cp+1)->argp = Ap+1;
912 916
913 917 for (mptr = callst; mptr <= Cp; ++mptr) {
914 918 wchar_t **aptr, **lim;
915 919
916 920 aptr = mptr->argp;
917 921 lim = (mptr+1)->argp-1;
918 922 if (mptr == callst)
919 923 (void) fputws(*aptr, stderr);
920 924 ++aptr;
921 925 (void) fputs("(", stderr);
922 926 if (aptr < lim)
923 927 for (;;) {
924 928 (void) fputws(*aptr++, stderr);
925 929 if (aptr >= lim)
926 930 break;
927 931 (void) fputs(",", stderr);
928 932 }
929 933 }
930 934 while (--mptr >= callst)
931 935 (void) fputs(")", stderr);
932 936
933 937 (void) fputs("\n", stderr);
934 938 }
935 939 delexit(NOT_OK, 1);
936 940 }
937 941
938 942 static wchar_t *
939 943 chkbltin(wchar_t *s)
940 944 {
941 945 static wchar_t buf[24];
942 946
943 947 if (is_builtin(*s)) {
944 948 (void) swprintf(buf, sizeof (buf)/sizeof (wchar_t), L"<%ls>",
945 949 barray[builtin_idx(*s)].bname);
946 950 return (buf);
947 951 }
948 952 return (s);
949 953 }
950 954
951 955 wchar_t
952 956 getchr()
953 957 {
954 958 static wchar_t C;
955 959
956 960 prev_char = C;
957 961 if (ip > ipflr)
958 962 return (*--ip);
959 963 if (wide) {
960 964 C = (wchar_t)(myfeof(ifx) ? WEOF : myfgetwc(NULL, ifx));
961 965 } else {
962 966 C = (wchar_t)(feof(ifile[ifx]) ?
963 967 WEOF : (wint_t)getc(ifile[ifx]));
964 968 }
965 969 if (C == '\n')
966 970 fline[ifx]++;
967 971 return (C);
968 972 }
969 973
970 974 /*
971 975 * showwrap
972 976 *
973 977 * Loop through the list of m4wrap strings. Call pbstr() so that the
974 978 * string will be displayed, then delete the list entry and free the memory
975 979 * allocated for it.
976 980 */
977 981 static void
978 982 showwrap()
979 983 {
980 984 struct Wrap *prev;
981 985
982 986 while (wrapstart) {
983 987 pbstr(wrapstart->wrapstr);
984 988 free(wrapstart->wrapstr);
985 989 prev = wrapstart;
986 990 wrapstart = wrapstart->nxt;
987 991 free(prev);
988 992 }
989 993 }
990 994
991 995 static void
992 996 sputchr(wchar_t c, FILE *f)
993 997 {
994 998 wint_t ret;
995 999
996 1000 if (is_builtin(c))
997 1001 return;
998 1002 if (wide)
999 1003 ret = myfputwc(c, f);
1000 1004 else
1001 1005 ret = (wint_t)putc((int)c, f);
1002 1006 if (ret == WEOF)
1003 1007 error(gettext("output error"));
1004 1008 if (ret == '\n')
1005 1009 lnsync(f);
1006 1010 }
1007 1011
1008 1012 static void
1009 1013 putchr(wchar_t c)
1010 1014 {
1011 1015 wint_t ret;
1012 1016
1013 1017 if (Cp)
1014 1018 stkchr(c);
1015 1019 else if (cf) {
1016 1020 if (sflag)
1017 1021 sputchr(c, cf);
1018 1022 else {
1019 1023 if (is_builtin(c))
1020 1024 return;
1021 1025 if (wide)
1022 1026 ret = myfputwc(c, cf);
1023 1027 else
1024 1028 ret = (wint_t)putc((int)c, cf);
1025 1029 if (ret == WEOF) {
1026 1030 error(gettext("output error"));
1027 1031 }
1028 1032 }
1029 1033 }
1030 1034 }
1031 1035
1032 1036 wchar_t *
1033 1037 wstrdup(wchar_t *p)
1034 1038 {
1035 1039 size_t len = wcslen(p);
1036 1040 wchar_t *ret;
1037 1041
1038 1042 ret = xmalloc((len + 1) * sizeof (wchar_t));
1039 1043 (void) wcscpy(ret, p);
1040 1044 return (ret);
1041 1045 }
1042 1046
1043 1047 int
1044 1048 wstoi(wchar_t *p)
1045 1049 {
1046 1050 return ((int)wcstol(p, NULL, 10));
1047 1051 }
1048 1052
1049 1053 char *
1050 1054 wstr2str(wchar_t *from, int alloc)
1051 1055 {
1052 1056 static char *retbuf;
1053 1057 static size_t bsiz;
1054 1058 char *p, *ret;
1055 1059
1056 1060 if (alloc) {
1057 1061 ret = p = xmalloc(wcslen(from) * mb_cur_max + 1);
1058 1062 } else {
1059 1063 while (bsiz < (wcslen(from) * mb_cur_max + 1)) {
1060 1064 if ((p = realloc(retbuf, bsiz + 256)) == NULL)
1061 1065 error(gettext("out of storage"));
1062 1066 bsiz += 256;
1063 1067 retbuf = p;
1064 1068 }
1065 1069 ret = p = retbuf;
1066 1070 }
1067 1071
1068 1072 if (wide) {
1069 1073 while (*from) {
1070 1074 int len;
1071 1075
1072 1076 if (*from & INVALID_CHAR) {
1073 1077 *p = (char)(*from & ~INVALID_CHAR);
1074 1078 len = 1;
1075 1079 } else {
1076 1080 if ((len = wctomb(p, *from)) == -1) {
1077 1081 *p = (char)*from;
1078 1082 len = 1;
1079 1083 }
1080 1084 }
1081 1085 p += len;
1082 1086 from++;
1083 1087 }
1084 1088 } else {
1085 1089 while (*from)
1086 1090 *p++ = (char)*from++;
1087 1091 }
1088 1092 *p = '\0';
1089 1093
1090 1094 return (ret);
1091 1095 }
1092 1096
1093 1097 wchar_t *
1094 1098 str2wstr(char *from, int alloc)
1095 1099 {
1096 1100 static wchar_t *retbuf;
1097 1101 static size_t bsiz;
1098 1102 wchar_t *p, *ret;
1099 1103
1100 1104 if (alloc) {
1101 1105 ret = p = xmalloc((strlen(from) + 1) * sizeof (wchar_t));
1102 1106 } else {
1103 1107 while (bsiz < (strlen(from) + 1)) {
1104 1108 if ((p = realloc(retbuf,
1105 1109 (bsiz + 256) * sizeof (wchar_t))) == NULL) {
1106 1110 error(gettext("out of storage"));
1107 1111 }
1108 1112 bsiz += 256;
1109 1113 retbuf = p;
1110 1114 }
1111 1115 ret = p = retbuf;
1112 1116 }
1113 1117
1114 1118 if (wide) {
1115 1119 while (*from) {
1116 1120 int len;
1117 1121 wchar_t wc;
1118 1122
1119 1123 if ((len = mbtowc(&wc, from, mb_cur_max)) <= 0) {
1120 1124 wc = *from | INVALID_CHAR;
1121 1125 len = 1;
1122 1126 }
1123 1127 *p++ = wc;
1124 1128 from += len;
1125 1129 }
1126 1130 } else {
1127 1131 while (*from)
1128 1132 *p++ = (unsigned char) *from++;
1129 1133 }
1130 1134 *p = 0;
1131 1135
1132 1136 return (ret);
1133 1137 }
1134 1138
1135 1139 static wint_t
1136 1140 myfgetwc(FILE *fp, int idx)
1137 1141 {
1138 1142 int i, c, len, nb;
1139 1143 wchar_t wc;
1140 1144 unsigned char *buf;
1141 1145
1142 1146 if (fp == NULL)
1143 1147 fp = ifile[idx];
1144 1148 else
1145 1149 idx = 10; /* extra slot */
1146 1150 buf = ibuffer[idx].buffer;
1147 1151 nb = ibuffer[idx].nbytes;
1148 1152 len = 0;
1149 1153 for (i = 1; i <= mb_cur_max; i++) {
1150 1154 if (nb < i) {
1151 1155 c = getc(fp);
1152 1156 if (c == EOF) {
1153 1157 if (nb == 0)
1154 1158 return (WEOF);
1155 1159 else
1156 1160 break;
1157 1161 }
1158 1162 buf[nb++] = (unsigned char)c;
1159 1163 }
1160 1164 if ((len = mbtowc(&wc, (char *)buf, i)) >= 0)
1161 1165 break;
1162 1166 }
1163 1167 if (len <= 0) {
1164 1168 wc = buf[0] | INVALID_CHAR;
1165 1169 len = 1;
1166 1170 }
1167 1171 nb -= len;
1168 1172 if (nb > 0) {
1169 1173 for (i = 0; i < nb; i++)
1170 1174 buf[i] = buf[i + len];
1171 1175 }
1172 1176 ibuffer[idx].nbytes = nb;
1173 1177 return (wc);
1174 1178 }
1175 1179
1176 1180 static wint_t
1177 1181 myfputwc(wchar_t wc, FILE *fp)
1178 1182 {
1179 1183 if (wc & INVALID_CHAR) {
1180 1184 wc &= ~INVALID_CHAR;
1181 1185 return (fputc((int)wc, fp));
1182 1186 }
1183 1187 return (fputwc(wc, fp));
1184 1188 }
1185 1189
1186 1190 static int
1187 1191 myfeof(int idx)
1188 1192 {
1189 1193 return (ibuffer[idx].nbytes == 0 && feof(ifile[idx]));
1190 1194 }
↓ open down ↓ |
601 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX