File indexing completed on 2018-03-02 18:44:44 UTC
view on githubraw file Latest commit c596602b on 2009-04-06 21:17:32 UTC
bf5846c3a1 Ed H*0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070 #include "def.h"
0071 #ifdef hpux
0072 #define sigvec sigvector
0073 #endif
0074
0075 #include <signal.h>
0076
0077 #ifdef HAVE_STDARG_H
0078 #include <stdarg.h>
0079 #endif
0080
0081 #ifdef DEBUG
0082 int _debugmask;
0083 #endif
0084
0085 char *ProgramName;
0086
0087 char *directives[] = {
0088 "if",
0089 "ifdef",
0090 "ifndef",
0091 "else",
0092 "endif",
0093 "define",
0094 "undef",
0095 "include",
0096 "line",
0097 "pragma",
0098 "error",
0099 "ident",
0100 "sccs",
0101 "elif",
0102 "eject",
0103 NULL
0104 };
0105
0106 struct symtab predefs[] = {
0107 #ifdef apollo
0108 {"apollo", "1"},
0109 #endif
0110 #ifdef ibm032
0111 {"ibm032", "1"},
0112 #endif
0113 #ifdef sun
0114 {"sun", "1"},
0115 #ifdef sparc
0116 {"sparc", "1"},
0117 #endif
0118 #endif
0119 #ifdef solaris20
0120 {"solaris20", "1"},
0121 #endif
0122 #ifdef __i386
0123 {"__i386", "1"},
0124 #endif
0125 #ifdef hpux
0126 {"hpux", "1"},
0127 #endif
0128 #ifdef vax
0129 {"vax", "1"},
0130 #endif
0131 #ifdef VMS
0132 {"VMS", "1"},
0133 #endif
0134 #ifdef ultrix
0135 {"ultrix", "1"},
0136 #endif
0137 #ifdef mips
0138 {"mips", "1"},
0139 #endif
0140 #ifdef __SCO__
0141 {"__SCO__", "1"},
0142 #endif
0143 #ifdef __DGUX
0144 {"__DGUX", "1"},
0145 #endif
0146 #ifdef __DGUX__
0147 {"__DGUX__", "1"},
0148 #endif
0149 #ifdef __m88k__
0150 {"__m88k__", "1"},
0151 #endif
0152 {NULL, NULL}
0153 };
0154 #define OBJSUFFIX ".o"
0155 #define INCLUDEDIR "/usr/include"
0156
0157 struct inclist inclist[ MAXFILES ],
0158 *inclistp = inclist,
0159 maininclist;
0160
0161 char *filelist[ MAXFILES ];
0162 char *includedirs[ MAXDIRS + 1 ];
0163 char *notdotdot[ MAXDIRS ];
0164 char *objprefix = "";
0165 char *objsuffix = OBJSUFFIX;
0166 char *startat = "# DO NOT DELETE";
0167 int width = 78;
0168 boolean append = FALSE;
0169 boolean printed = FALSE;
0170 boolean verbose = FALSE;
0171 boolean show_where_not = FALSE;
0172 boolean warn_multiple = FALSE;
0173
0174 static
0175 #ifdef SIGNALRETURNSINT
0176 int
0177 #else
0178 void
0179 #endif
0180 catch (sig)
0181 int sig;
0182 {
0183 fflush (stdout);
0184 fatalerr ("got signal %d\n", sig);
0185 }
0186
0187 #if defined(USG) || (defined(SYSV386) && defined(SYSV)) || defined(WIN32) || defined(__i386)
0188 #define USGISH
0189 #endif
0190
10ab77bf96 Ed H*0191 #ifdef sparc
0192 #define USGISH
0193 #endif
0194
bf5846c3a1 Ed H*0195 #ifndef USGISH
0196 #ifndef _POSIX_SOURCE
0197 #define sigaction sigvec
0198 #define sa_handler sv_handler
0199 #define sa_mask sv_mask
0200 #define sa_flags sv_flags
0201 #endif
0202 struct sigaction sig_act;
0203 #endif
0204
0205 main(argc, argv)
0206 int argc;
0207 char **argv;
0208 {
0209 register char **fp = filelist;
0210 register char **incp = includedirs;
0211 register char *p;
0212 register struct inclist *ip;
0213 char *makefile = NULL;
0214 struct filepointer *filecontent;
0215 struct symtab *psymp = predefs;
0216 char *endmarker = NULL;
0217 char *defincdir = NULL;
0218
0219 ProgramName = argv[0];
0220
0221 while (psymp->s_name)
0222 {
0223 define2(psymp->s_name, psymp->s_value, &maininclist);
0224 psymp++;
0225 }
0226 if (argc == 2 && argv[1][0] == '@') {
0227 struct stat ast;
0228 int afd;
0229 char *args;
0230 char **nargv;
0231 int nargc;
0232 char quotechar = '\0';
0233
0234 nargc = 1;
0235 if ((afd = open(argv[1]+1, O_RDONLY)) < 0)
0236 fatalerr("cannot open \"%s\"\n", argv[1]+1);
0237 fstat(afd, &ast);
0238 args = (char *)malloc(ast.st_size + 1);
0239 if ((ast.st_size = read(afd, args, ast.st_size)) < 0)
0240 fatalerr("failed to read %s\n", argv[1]+1);
0241 args[ast.st_size] = '\0';
0242 close(afd);
0243 for (p = args; *p; p++) {
0244 if (quotechar) {
0245 if (quotechar == '\\' ||
0246 (*p == quotechar && p[-1] != '\\'))
0247 quotechar = '\0';
0248 continue;
0249 }
0250 switch (*p) {
0251 case '\\':
0252 case '"':
0253 case '\'':
0254 quotechar = *p;
0255 break;
0256 case ' ':
0257 case '\n':
0258 *p = '\0';
0259 if (p > args && p[-1])
0260 nargc++;
0261 break;
0262 }
0263 }
0264 if (p[-1])
0265 nargc++;
0266 nargv = (char **)malloc(nargc * sizeof(char *));
0267 nargv[0] = argv[0];
0268 argc = 1;
0269 for (p = args; argc < nargc; p += strlen(p) + 1)
0270 if (*p) nargv[argc++] = p;
0271 argv = nargv;
0272 }
0273 for(argc--, argv++; argc; argc--, argv++) {
0274
0275 if (endmarker && strcmp (endmarker, *argv) == 0) {
0276 endmarker = NULL;
0277 continue;
0278 }
0279 if (**argv != '-') {
0280
0281 if (endmarker && **argv == '+')
0282 continue;
0283 *fp++ = argv[0];
0284 continue;
0285 }
0286 switch(argv[0][1]) {
0287 case '-':
0288 endmarker = &argv[0][2];
0289 if (endmarker[0] == '\0') endmarker = "--";
0290 break;
0291 case 'D':
0292 if (argv[0][2] == '\0') {
0293 argv++;
0294 argc--;
0295 }
0296 for (p=argv[0] + 2; *p ; p++)
0297 if (*p == '=') {
0298 *p = ' ';
0299 break;
0300 }
0301 define(argv[0] + 2, &maininclist);
0302 break;
0303 case 'I':
0304 if (incp >= includedirs + MAXDIRS)
0305 fatalerr("Too many -I flags.\n");
0306 *incp++ = argv[0]+2;
0307 if (**(incp-1) == '\0') {
0308 *(incp-1) = *(++argv);
0309 argc--;
0310 }
0311 break;
0312 case 'Y':
0313 defincdir = argv[0]+2;
0314 break;
0315
0316 case 'a':
0317 if (endmarker) break;
0318 append = TRUE;
0319 break;
0320 case 'w':
0321 if (endmarker) break;
0322 if (argv[0][2] == '\0') {
0323 argv++;
0324 argc--;
0325 width = atoi(argv[0]);
0326 } else
0327 width = atoi(argv[0]+2);
0328 break;
0329 case 'o':
0330 if (endmarker) break;
0331 if (argv[0][2] == '\0') {
0332 argv++;
0333 argc--;
0334 objsuffix = argv[0];
0335 } else
0336 objsuffix = argv[0]+2;
0337 break;
0338 case 'p':
0339 if (endmarker) break;
0340 if (argv[0][2] == '\0') {
0341 argv++;
0342 argc--;
0343 objprefix = argv[0];
0344 } else
0345 objprefix = argv[0]+2;
0346 break;
0347 case 'v':
0348 if (endmarker) break;
0349 verbose = TRUE;
0350 #ifdef DEBUG
0351 if (argv[0][2])
0352 _debugmask = atoi(argv[0]+2);
0353 #endif
0354 break;
0355 case 's':
0356 if (endmarker) break;
0357 startat = argv[0]+2;
0358 if (*startat == '\0') {
0359 startat = *(++argv);
0360 argc--;
0361 }
0362 if (*startat != '#')
0363 fatalerr("-s flag's value should start %s\n",
0364 "with '#'.");
0365 break;
0366 case 'f':
0367 if (endmarker) break;
0368 makefile = argv[0]+2;
0369 if (*makefile == '\0') {
0370 makefile = *(++argv);
0371 argc--;
0372 }
0373 break;
0374
0375 case 'm':
0376 warn_multiple = TRUE;
0377 break;
0378
0379
0380
0381
0382 case 'O':
0383 case 'g':
0384 break;
0385 default:
0386 if (endmarker) break;
0387
0388 warning("ignoring option %s\n", argv[0]);
0389 }
0390 }
0391 if (!defincdir) {
0392 #ifdef PREINCDIR
0393 if (incp >= includedirs + MAXDIRS)
0394 fatalerr("Too many -I flags.\n");
0395 *incp++ = PREINCDIR;
0396 #endif
0397 if (incp >= includedirs + MAXDIRS)
0398 fatalerr("Too many -I flags.\n");
0399 *incp++ = INCLUDEDIR;
0400 #ifdef POSTINCDIR
0401 if (incp >= includedirs + MAXDIRS)
0402 fatalerr("Too many -I flags.\n");
0403 *incp++ = POSTINCDIR;
0404 #endif
0405 } else if (*defincdir) {
0406 if (incp >= includedirs + MAXDIRS)
0407 fatalerr("Too many -I flags.\n");
0408 *incp++ = defincdir;
0409 }
0410
0411 redirect(startat, makefile);
0412
0413
0414
0415
0416 #ifdef USGISH
0417
0418 #ifdef SIGHUP
0419 signal (SIGHUP, catch);
0420 #endif
0421 signal (SIGINT, catch);
0422 #ifdef SIGQUIT
0423 signal (SIGQUIT, catch);
0424 #endif
0425 signal (SIGILL, catch);
0426 #ifdef SIGBUS
0427 signal (SIGBUS, catch);
0428 #endif
0429 signal (SIGSEGV, catch);
0430 #ifdef SIGSYS
0431 signal (SIGSYS, catch);
0432 #endif
0433 #else
0434 sig_act.sa_handler = catch;
0435 #ifdef _POSIX_SOURCE
0436 sigemptyset(&sig_act.sa_mask);
0437 sigaddset(&sig_act.sa_mask, SIGINT);
0438 sigaddset(&sig_act.sa_mask, SIGQUIT);
0439 #ifdef SIGBUS
0440 sigaddset(&sig_act.sa_mask, SIGBUS);
0441 #endif
0442 sigaddset(&sig_act.sa_mask, SIGILL);
0443 sigaddset(&sig_act.sa_mask, SIGSEGV);
0444 sigaddset(&sig_act.sa_mask, SIGHUP);
0445 sigaddset(&sig_act.sa_mask, SIGPIPE);
0446 #ifdef SIGSYS
0447 sigaddset(&sig_act.sa_mask, SIGSYS);
0448 #endif
0449 #else
0450 sig_act.sa_mask = ((1<<(SIGINT -1))
0451 |(1<<(SIGQUIT-1))
0452 #ifdef SIGBUS
0453 |(1<<(SIGBUS-1))
0454 #endif
0455 |(1<<(SIGILL-1))
0456 |(1<<(SIGSEGV-1))
0457 |(1<<(SIGHUP-1))
0458 |(1<<(SIGPIPE-1))
0459 #ifdef SIGSYS
0460 |(1<<(SIGSYS-1))
0461 #endif
0462 );
0463 #endif
0464 sig_act.sa_flags = 0;
0465 sigaction(SIGHUP, &sig_act, (struct sigaction *)0);
0466 sigaction(SIGINT, &sig_act, (struct sigaction *)0);
0467 sigaction(SIGQUIT, &sig_act, (struct sigaction *)0);
0468 sigaction(SIGILL, &sig_act, (struct sigaction *)0);
0469 #ifdef SIGBUS
0470 sigaction(SIGBUS, &sig_act, (struct sigaction *)0);
0471 #endif
0472 sigaction(SIGSEGV, &sig_act, (struct sigaction *)0);
0473 #ifdef SIGSYS
0474 sigaction(SIGSYS, &sig_act, (struct sigaction *)0);
0475 #endif
0476 #endif
0477
0478
0479
0480
0481 for(fp=filelist; *fp; fp++) {
0482 filecontent = getfile(*fp);
0483 ip = newinclude(*fp, (char *)NULL);
0484
0485 find_includes(filecontent, ip, ip, 0, FALSE);
0486 freefile(filecontent);
0487 recursive_pr_include(ip, ip->i_file, base_name(*fp));
0488 inc_clean();
0489 }
0490 if (printed)
0491 printf("\n");
0492 exit(0);
0493 }
0494
0495 struct filepointer *getfile(file)
0496 char *file;
0497 {
0498 register int fd;
0499 struct filepointer *content;
0500 struct stat st;
0501
0502 content = (struct filepointer *)malloc(sizeof(struct filepointer));
0503 if ((fd = open(file, O_RDONLY)) < 0) {
0504 warning("cannot open \"%s\"\n", file);
0505 content->f_p = content->f_base = content->f_end = (char *)malloc(1);
0506 *content->f_p = '\0';
0507 return(content);
0508 }
0509 fstat(fd, &st);
0510 content->f_base = (char *)malloc(st.st_size+1);
0511 if (content->f_base == NULL)
0512 fatalerr("cannot allocate mem\n");
0513 if ((st.st_size = read(fd, content->f_base, st.st_size)) < 0)
0514 fatalerr("failed to read %s\n", file);
0515 close(fd);
0516 content->f_len = st.st_size+1;
0517 content->f_p = content->f_base;
0518 content->f_end = content->f_base + st.st_size;
0519 *content->f_end = '\0';
0520 content->f_line = 0;
0521 return(content);
0522 }
0523
0524 freefile(fp)
0525 struct filepointer *fp;
0526 {
0527 free(fp->f_base);
0528 free(fp);
0529 }
0530
0531 char *copy(str)
0532 register char *str;
0533 {
0534 register char *p = (char *)malloc(strlen(str) + 1);
0535
0536 strcpy(p, str);
0537 return(p);
0538 }
0539
0540 match(str, list)
0541 register char *str, **list;
0542 {
0543 register int i;
0544
0545 for (i=0; *list; i++, list++)
0546 if (strcmp(str, *list) == 0)
0547 return(i);
0548 return(-1);
0549 }
0550
0551
0552
0553
0554
c596602bd4 Oliv*0555 char *getppline(filep)
bf5846c3a1 Ed H*0556 register struct filepointer *filep;
0557 {
0558 register char *p,
0559 *eof,
0560 *bol;
0561 register lineno;
0562
0563 p = filep->f_p;
0564 eof = filep->f_end;
0565 if (p >= eof)
0566 return((char *)NULL);
0567 lineno = filep->f_line;
0568
0569 for(bol = p--; ++p < eof; ) {
0570 if (*p == '/' && *(p+1) == '*') {
0571 *p++ = ' ', *p++ = ' ';
0572 while (*p) {
0573 if (*p == '*' && *(p+1) == '/') {
0574 *p++ = ' ', *p = ' ';
0575 break;
0576 }
0577 else if (*p == '\n')
0578 lineno++;
0579 *p++ = ' ';
0580 }
0581 continue;
0582 }
0583 #ifdef WIN32
0584 else if (*p == '/' && *(p+1) == '/') {
0585 *p++ = ' ', *p++ = ' ';
0586 while (*p && *p != '\n')
0587 *p++ = ' ';
0588 lineno++;
0589 continue;
0590 }
0591 #endif
0592 else if (*p == '\\') {
0593 if (*(p+1) == '\n') {
0594 *p = ' ';
0595 *(p+1) = ' ';
0596 lineno++;
0597 }
0598 }
0599 else if (*p == '\n') {
0600 lineno++;
0601 if (*bol == '#') {
0602 register char *cp;
0603
0604 *p++ = '\0';
0605
0606 for (cp = bol+1;
0607 *cp && (*cp == ' ' || *cp == '\t'); cp++);
0608 if (*cp) goto done;
0609 }
0610 bol = p+1;
0611 }
0612 }
0613 if (*bol != '#')
0614 bol = NULL;
0615 done:
0616 filep->f_p = p;
0617 filep->f_line = lineno;
0618 return(bol);
0619 }
0620
0621
0622
0623
0624
0625 char *base_name(file)
0626 register char *file;
0627 {
0628 register char *p;
0629
0630 file = copy(file);
0631 for(p=file+strlen(file); p>file && *p != '.'; p--) ;
0632
0633 if (*p == '.')
0634 *p = '\0';
0635 return(file);
0636 }
0637
0638 #if defined(USG) && !defined(CRAY) && !defined(SVR4)
0639 int rename (from, to)
0640 char *from, *to;
0641 {
0642 (void) unlink (to);
0643 if (link (from, to) == 0) {
0644 unlink (from);
0645 return 0;
0646 } else {
0647 return -1;
0648 }
0649 }
0650 #endif
0651
0652 redirect(line, makefile)
0653 char *line,
0654 *makefile;
0655 {
0656 struct stat st;
0657 FILE *fdin, *fdout;
0658 char backup[ BUFSIZ ],
0659 buf[ BUFSIZ ];
0660 boolean found = FALSE;
0661 int len;
0662
0663
0664
0665
0666 if (makefile && *makefile == '-' && *(makefile+1) == '\0')
0667 return;
0668
0669
0670
0671
0672 if (!makefile) {
0673 if (stat("Makefile", &st) == 0)
0674 makefile = "Makefile";
0675 else if (stat("makefile", &st) == 0)
0676 makefile = "makefile";
0677 else
0678 fatalerr("[mM]akefile is not present\n");
0679 }
0680 else
0681 stat(makefile, &st);
0682 if ((fdin = fopen(makefile, "r")) == NULL)
0683 fatalerr("cannot open \"%s\"\n", makefile);
0684 sprintf(backup, "%s.bak", makefile);
0685 unlink(backup);
0686 #ifdef WIN32
0687 fclose(fdin);
0688 #endif
0689 if (rename(makefile, backup) < 0)
0690 fatalerr("cannot rename %s to %s\n", makefile, backup);
0691 #ifdef WIN32
0692 if ((fdin = fopen(backup, "r")) == NULL)
0693 fatalerr("cannot open \"%s\"\n", backup);
0694 #endif
0695 if ((fdout = freopen(makefile, "w", stdout)) == NULL)
0696 fatalerr("cannot open \"%s\"\n", backup);
0697 len = strlen(line);
0698 while (!found && fgets(buf, BUFSIZ, fdin)) {
0699 if (*buf == '#' && strncmp(line, buf, len) == 0)
0700 found = TRUE;
0701 fputs(buf, fdout);
0702 }
0703 if (!found) {
0704 if (verbose)
0705 warning("Adding new delimiting line \"%s\" and dependencies...\n",
0706 line);
0707 puts(line);
0708 } else if (append) {
0709 while (fgets(buf, BUFSIZ, fdin)) {
0710 fputs(buf, fdout);
0711 }
0712 }
0713 fflush(fdout);
0714 #if defined(USGISH) || defined(_SEQUENT_)
0715 chmod(makefile, st.st_mode);
0716 #else
0717 fchmod(fileno(fdout), st.st_mode);
0718 #endif
0719 }
0720
0721 #ifdef HAVE_STDARG_H
0722 fatalerr(char *msg, ...)
0723 #else
0724
0725 fatalerr(msg,x1,x2,x3,x4,x5,x6,x7,x8,x9)
0726 char *msg;
0727 #endif
0728 {
0729 #ifdef HAVE_STDARG_H
0730 va_list args;
0731 #endif
0732 fprintf(stderr, "%s: error: ", ProgramName);
0733 #ifdef HAVE_STDARG_H
0734 va_start(args, msg);
0735 vfprintf(stderr, msg, args);
0736 va_end(args);
0737 #else
0738 fprintf(stderr, msg,x1,x2,x3,x4,x5,x6,x7,x8,x9);
0739 #endif
0740 exit (1);
0741 }
0742
0743 #ifdef HAVE_STDARG_H
0744 warning(char *msg, ...)
0745 #else
0746
0747 warning(msg,x1,x2,x3,x4,x5,x6,x7,x8,x9)
0748 char *msg;
0749 #endif
0750 {
0751 #ifdef HAVE_STDARG_H
0752 va_list args;
0753 #endif
0754 fprintf(stderr, "%s: warning: ", ProgramName);
0755 #ifdef HAVE_STDARG_H
0756 va_start(args, msg);
0757 vfprintf(stderr, msg, args);
0758 va_end(args);
0759 #else
0760 fprintf(stderr, msg,x1,x2,x3,x4,x5,x6,x7,x8,x9);
0761 #endif
0762 }
0763
0764 #ifdef HAVE_STDARG_H
0765 warning1(char *msg, ...)
0766 #else
0767
0768 warning1(msg,x1,x2,x3,x4,x5,x6,x7,x8,x9)
0769 char *msg;
0770 #endif
0771 {
0772 #ifdef HAVE_STDARG_H
0773 va_list args;
0774 va_start(args, msg);
0775 vfprintf(stderr, msg, args);
0776 va_end(args);
0777 #else
0778 fprintf(stderr, msg,x1,x2,x3,x4,x5,x6,x7,x8,x9);
0779 #endif
0780 }