00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "def.h"
00031 #ifdef __hpux
00032 #define sigvec sigvector
00033 #endif
00034
00035 #ifdef X_POSIX_C_SOURCE
00036 #define _POSIX_C_SOURCE X_POSIX_C_SOURCE
00037 #include <signal.h>
00038 #undef _POSIX_C_SOURCE
00039 #else
00040 #if defined(X_NOT_POSIX) || defined(_POSIX_SOURCE)
00041 #include <signal.h>
00042 #else
00043 #define _POSIX_SOURCE
00044 #include <signal.h>
00045 #undef _POSIX_SOURCE
00046 #endif
00047 #endif
00048
00049 #include <stdarg.h>
00050 #ifndef WIN32
00051 #include <unistd.h>
00052 #else
00053 #include <io.h>
00054 #endif
00055 #if !defined(__hpux)
00056 # if defined(__APPLE__)
00057 # include <AvailabilityMacros.h>
00058 # if !defined(MAC_OS_X_VERSION_10_4)
00059 extern int fchmod(int, int);
00060 # endif
00061 # elif defined(__CYGWIN__)
00062 extern int fchmod(int, mode_t);
00063 #else
00064 extern int fchmod(int, int);
00065 # endif
00066 #endif
00067
00068 #ifdef MINIX
00069 #define USE_CHMOD 1
00070 #endif
00071
00072 #ifdef DEBUG
00073 int _debugmask;
00074 #endif
00075
00076 char *ProgramName;
00077
00078 char *directives[] = {
00079 "if",
00080 "ifdef",
00081 "ifndef",
00082 "else",
00083 "endif",
00084 "define",
00085 "undef",
00086 "include",
00087 "line",
00088 "pragma",
00089 "error",
00090 "ident",
00091 "sccs",
00092 "elif",
00093 "eject",
00094 "warning",
00095 NULL
00096 };
00097
00098 #define MAKEDEPEND
00099 #include "imakemdep.h"
00100 #undef MAKEDEPEND
00101
00102 struct inclist inclist[ MAXFILES ],
00103 *inclistp = inclist,
00104 maininclist;
00105
00106 char *filelist[ MAXFILES ];
00107 char *targetlist[ MAXFILES ];
00108 char *includedirs[ MAXDIRS + 1 ];
00109 char *notdotdot[ MAXDIRS ];
00110 char *objprefix = "";
00111 char *objsuffix = OBJSUFFIX;
00112 char *startat = "# DO NOT DELETE";
00113 char *isysroot = "";
00114 int width = 78;
00115 boolean append = FALSE;
00116 boolean printed = FALSE;
00117 boolean verbose = FALSE;
00118 boolean show_where_not = FALSE;
00119 boolean warn_multiple = FALSE;
00120
00121 void freefile(struct filepointer*);
00122 void redirect(char*, char*);
00123
00124 static
00125 #ifdef SIGNALRETURNSINT
00126 int
00127 #else
00128 void
00129 #endif
00130 catch (int sig)
00131 {
00132 fflush(stdout);
00133 fatalerr("got signal %d\n", sig);
00134 }
00135
00136 #if defined(USG) || (defined(i386) && defined(SYSV)) || defined(WIN32) || defined(__EMX__) || defined(Lynx_22)
00137 #define USGISH
00138 #endif
00139
00140 #ifndef USGISH
00141 #ifndef _POSIX_SOURCE
00142 #define sigaction sigvec
00143 #define sa_handler sv_handler
00144 #define sa_mask sv_mask
00145 #define sa_flags sv_flags
00146 #endif
00147 struct sigaction sig_act;
00148 #endif
00149
00150 extern void define2(char *name, char *val, struct inclist *file);
00151 extern void define(char *def, struct inclist *file);
00152 extern void undefine(char *symbol, struct inclist *file);
00153 extern int find_includes(struct filepointer *filep, struct inclist *file,
00154 struct inclist *file_red, int recursion,
00155 boolean failOK);
00156 extern void recursive_pr_include(struct inclist *head, char *file, char *base, char *dep);
00157 extern void inc_clean();
00158
00159 int main_orig(argc, argv)
00160 int argc;
00161 char **argv;
00162 {
00163 register char **fp = filelist;
00164 register char **tp = targetlist;
00165 register char **incp = includedirs;
00166 register char *p;
00167 register struct inclist *ip;
00168 char *makefile = NULL;
00169 struct filepointer *filecontent;
00170 struct symtab *psymp = predefs;
00171 char *endmarker = NULL;
00172 char *defincdir = NULL;
00173 char **undeflist = NULL;
00174 int numundefs = 0, i;
00175 int numfiles = 0;
00176
00177 ProgramName = argv[0];
00178
00179 while (psymp->s_name) {
00180 define2(psymp->s_name, psymp->s_value, &maininclist);
00181 psymp++;
00182 }
00183 if (argc == 2 && argv[1][0] == '@') {
00184 struct stat ast;
00185 int afd;
00186 char *args;
00187 char **nargv;
00188 int nargc;
00189 char quotechar = '\0';
00190
00191 nargc = 1;
00192 if ((afd = open(argv[1] + 1, O_RDONLY)) < 0)
00193 fatalerr("cannot open \"%s\"\n", argv[1] + 1);
00194 fstat(afd, &ast);
00195 args = (char *)malloc(ast.st_size + 1);
00196 if ((ast.st_size = read(afd, args, ast.st_size)) < 0)
00197 fatalerr("failed to read %s\n", argv[1] + 1);
00198 args[ast.st_size] = '\0';
00199 close(afd);
00200 for (p = args; *p; p++) {
00201 if (quotechar) {
00202 if (quotechar == '\\' ||
00203 (*p == quotechar && p[-1] != '\\'))
00204 quotechar = '\0';
00205 continue;
00206 }
00207 switch (*p) {
00208 case '\\':
00209 case '"':
00210 case '\'':
00211 quotechar = *p;
00212 break;
00213 case ' ':
00214 case '\n':
00215 *p = '\0';
00216 if (p > args && p[-1])
00217 nargc++;
00218 break;
00219 }
00220 }
00221 if (p[-1])
00222 nargc++;
00223 nargv = (char **)malloc(nargc * sizeof(char *));
00224 nargv[0] = argv[0];
00225 argc = 1;
00226 for (p = args; argc < nargc; p += strlen(p) + 1)
00227 if (*p) nargv[argc++] = p;
00228 argv = nargv;
00229 }
00230 for (argc--, argv++; argc; argc--, argv++) {
00231
00232 if (endmarker && strcmp(endmarker, *argv) == 0) {
00233 endmarker = NULL;
00234 continue;
00235 }
00236 if (**argv != '-') {
00237
00238 if (endmarker && **argv == '+')
00239 continue;
00240 *fp++ = argv[0];
00241 *tp++ = 0;
00242 ++numfiles;
00243 continue;
00244 }
00245 switch (argv[0][1]) {
00246 case '-':
00247 endmarker = &argv[0][2];
00248 if (endmarker[0] == '\0') endmarker = "--";
00249 break;
00250 case 't':
00251 if (endmarker) break;
00252 if (numfiles == 0) {
00253 fatalerr("-t should follow a file name\n");
00254 } else {
00255 *(tp - 1) = argv[0] + 2;
00256 }
00257 break;
00258 case 'D':
00259 if (argv[0][2] == '\0') {
00260 argv++;
00261 argc--;
00262 }
00263 for (p = argv[0] + 2; *p ; p++)
00264 if (*p == '=') {
00265 *p = ' ';
00266 break;
00267 }
00268 define(argv[0] + 2, &maininclist);
00269 break;
00270 case 'I':
00271 if (incp >= includedirs + MAXDIRS)
00272 fatalerr("Too many -I flags.\n");
00273 *incp++ = argv[0] + 2;
00274 if (**(incp - 1) == '\0') {
00275 *(incp - 1) = *(++argv);
00276 argc--;
00277 }
00278 break;
00279 case 'U':
00280
00281 numundefs++;
00282 if (numundefs == 1)
00283 undeflist = malloc(sizeof(char *));
00284 else
00285 undeflist = realloc(undeflist,
00286 numundefs * sizeof(char *));
00287 if (argv[0][2] == '\0') {
00288 argv++;
00289 argc--;
00290 }
00291 undeflist[numundefs - 1] = argv[0] + 2;
00292 break;
00293 case 'Y':
00294 defincdir = argv[0] + 2;
00295 break;
00296 case 'i':
00297 if (!strcmp(argv[0] + 2, "sysroot")) {
00298 argv++;
00299 argc--;
00300 isysroot = argv[0];
00301 }
00302 break;
00303
00304 case 'a':
00305 if (endmarker) break;
00306 append = TRUE;
00307 break;
00308 case 'w':
00309 if (endmarker) break;
00310 if (argv[0][2] == '\0') {
00311 argv++;
00312 argc--;
00313 width = atoi(argv[0]);
00314 } else
00315 width = atoi(argv[0] + 2);
00316 break;
00317 case 'o':
00318 if (endmarker) break;
00319 if (argv[0][2] == '\0') {
00320 argv++;
00321 argc--;
00322 objsuffix = argv[0];
00323 } else
00324 objsuffix = argv[0] + 2;
00325 break;
00326 case 'p':
00327 if (endmarker) break;
00328 if (argv[0][2] == '\0') {
00329 argv++;
00330 argc--;
00331 objprefix = argv[0];
00332 } else
00333 objprefix = argv[0] + 2;
00334 break;
00335 case 'v':
00336 if (endmarker) break;
00337 verbose = TRUE;
00338 #ifdef DEBUG
00339 if (argv[0][2])
00340 _debugmask = atoi(argv[0] + 2);
00341 #endif
00342 break;
00343 case 's':
00344 if (endmarker) break;
00345 startat = argv[0] + 2;
00346 if (*startat == '\0') {
00347 startat = *(++argv);
00348 argc--;
00349 }
00350 if (*startat != '#')
00351 fatalerr("-s flag's value should start %s\n",
00352 "with '#'.");
00353 break;
00354 case 'f':
00355 if (endmarker) break;
00356 makefile = argv[0] + 2;
00357 if (*makefile == '\0') {
00358 makefile = *(++argv);
00359 argc--;
00360 }
00361 break;
00362
00363 case 'm':
00364 warn_multiple = TRUE;
00365 break;
00366
00367
00368
00369
00370 case 'O':
00371 case 'g':
00372 break;
00373 default:
00374 if (endmarker) break;
00375
00376 warning("ignoring option %s\n", argv[0]);
00377 }
00378 }
00379
00380 for (i = 0; i < numundefs; i++)
00381 undefine(undeflist[i], &maininclist);
00382 if (numundefs > 0)
00383 free(undeflist);
00384
00385 if (!defincdir) {
00386 #ifdef PREINCDIR
00387 if (incp >= includedirs + MAXDIRS)
00388 fatalerr("Too many -I flags.\n");
00389 *incp++ = PREINCDIR;
00390 #endif
00391 #ifdef __EMX__
00392 {
00393 char *emxinc = getenv("C_INCLUDE_PATH");
00394
00395 if (emxinc) {
00396 char *beg, *end;
00397 beg = (char*)strdup(emxinc);
00398 for (;;) {
00399 end = (char*)strchr(beg, ';');
00400 if (end) *end = 0;
00401 if (incp >= includedirs + MAXDIRS)
00402 fatalerr("Too many include dirs\n");
00403 *incp++ = beg;
00404 if (!end) break;
00405 beg = end + 1;
00406 }
00407 }
00408 }
00409 #else
00410 if (incp >= includedirs + MAXDIRS)
00411 fatalerr("Too many -I flags.\n");
00412 *incp++ = INCLUDEDIR;
00413 #endif
00414
00415 #ifdef POSTINCDIR
00416 if (incp >= includedirs + MAXDIRS)
00417 fatalerr("Too many -I flags.\n");
00418 *incp++ = POSTINCDIR;
00419 #endif
00420 } else if (*defincdir) {
00421 if (incp >= includedirs + MAXDIRS)
00422 fatalerr("Too many -I flags.\n");
00423 *incp++ = defincdir;
00424 }
00425
00426 redirect(startat, makefile);
00427
00428
00429
00430
00431 #ifdef USGISH
00432
00433 #ifdef SIGHUP
00434 signal(SIGHUP, catch);
00435 #endif
00436 signal(SIGINT, catch);
00437 #ifdef SIGQUIT
00438 signal(SIGQUIT, catch);
00439 #endif
00440 signal(SIGILL, catch);
00441 #ifdef SIGBUS
00442 signal(SIGBUS, catch);
00443 #endif
00444 signal(SIGSEGV, catch);
00445 #ifdef SIGSYS
00446 signal(SIGSYS, catch);
00447 #endif
00448 #else
00449 sig_act.sa_handler = catch ;
00450 #ifdef _POSIX_SOURCE
00451 sigemptyset(&sig_act.sa_mask);
00452 sigaddset(&sig_act.sa_mask, SIGINT);
00453 sigaddset(&sig_act.sa_mask, SIGQUIT);
00454 #ifdef SIGBUS
00455 sigaddset(&sig_act.sa_mask, SIGBUS);
00456 #endif
00457 sigaddset(&sig_act.sa_mask, SIGILL);
00458 sigaddset(&sig_act.sa_mask, SIGSEGV);
00459 sigaddset(&sig_act.sa_mask, SIGHUP);
00460 sigaddset(&sig_act.sa_mask, SIGPIPE);
00461 #ifdef SIGSYS
00462 sigaddset(&sig_act.sa_mask, SIGSYS);
00463 #endif
00464 #else
00465 sig_act.sa_mask = ((1 << (SIGINT - 1))
00466 | (1 << (SIGQUIT - 1))
00467 #ifdef SIGBUS
00468 | (1 << (SIGBUS - 1))
00469 #endif
00470 | (1 << (SIGILL - 1))
00471 | (1 << (SIGSEGV - 1))
00472 | (1 << (SIGHUP - 1))
00473 | (1 << (SIGPIPE - 1))
00474 #ifdef SIGSYS
00475 | (1 << (SIGSYS - 1))
00476 #endif
00477 );
00478 #endif
00479 sig_act.sa_flags = 0;
00480 sigaction(SIGHUP, &sig_act, (struct sigaction *)0);
00481 sigaction(SIGINT, &sig_act, (struct sigaction *)0);
00482 sigaction(SIGQUIT, &sig_act, (struct sigaction *)0);
00483 sigaction(SIGILL, &sig_act, (struct sigaction *)0);
00484 #ifdef SIGBUS
00485 sigaction(SIGBUS, &sig_act, (struct sigaction *)0);
00486 #endif
00487 sigaction(SIGSEGV, &sig_act, (struct sigaction *)0);
00488 #ifdef SIGSYS
00489 sigaction(SIGSYS, &sig_act, (struct sigaction *)0);
00490 #endif
00491 #endif
00492
00493
00494
00495
00496 for (fp = filelist, tp = targetlist; *fp; fp++, tp++) {
00497 filecontent = getfile(*fp);
00498 ip = newinclude(*fp, (char *)NULL);
00499
00500 find_includes(filecontent, ip, ip, 0, FALSE);
00501 freefile(filecontent);
00502 if (!rootBuild)
00503 recursive_pr_include(ip, ip->i_file, base_name(*fp), *tp);
00504 else
00505 recursive_pr_include(ip, ip->i_file, base_name(makefile), *tp);
00506 inc_clean();
00507 }
00508 if (!rootBuild) {
00509 if (printed)
00510 printf("\n");
00511 exit(0);
00512 }
00513 return 0;
00514 }
00515
00516 #ifdef __EMX__
00517
00518
00519
00520 static int elim_cr(char *buf, int sz)
00521 {
00522 int i, wp;
00523 for (i = wp = 0; i < sz; i++) {
00524 if (buf[i] != '\r')
00525 buf[wp++] = buf[i];
00526 }
00527 return wp;
00528 }
00529 #endif
00530
00531 struct filepointer *getfile(file)
00532 char *file;
00533 {
00534 register int fd;
00535 struct filepointer *content;
00536 struct stat st;
00537
00538 content = (struct filepointer *)malloc(sizeof(struct filepointer));
00539 if ((fd = open(file, O_RDONLY)) < 0) {
00540 warning("cannot open \"%s\"\n", file);
00541 content->f_p = content->f_base = content->f_end = (char *)malloc(1);
00542 *content->f_p = '\0';
00543 return(content);
00544 }
00545 fstat(fd, &st);
00546 content->f_base = (char *)malloc(st.st_size + 1);
00547 if (content->f_base == NULL)
00548 fatalerr("cannot allocate mem\n");
00549 if ((st.st_size = read(fd, content->f_base, st.st_size)) < 0)
00550 fatalerr("failed to read %s\n", file);
00551 #ifdef __EMX__
00552 st.st_size = elim_cr(content->f_base, st.st_size);
00553 #endif
00554 close(fd);
00555 content->f_len = st.st_size + 1;
00556 content->f_p = content->f_base;
00557 content->f_end = content->f_base + st.st_size;
00558 *content->f_end = '\0';
00559 content->f_line = 0;
00560 return(content);
00561 }
00562
00563 void
00564 freefile(fp)
00565 struct filepointer *fp;
00566 {
00567 free(fp->f_base);
00568 free(fp);
00569 }
00570
00571 char *copy(str)
00572 register char *str;
00573 {
00574 register char *p = (char *)malloc(strlen(str) + 1);
00575
00576 strcpy(p, str);
00577 return(p);
00578 }
00579
00580 int match(str, list)
00581 register char *str, **list;
00582 {
00583 register int i;
00584
00585 for (i = 0; *list; i++, list++)
00586 if (strcmp(str, *list) == 0)
00587 return(i);
00588 return(-1);
00589 }
00590
00591
00592
00593
00594
00595 char *rgetline(filep)
00596 register struct filepointer *filep;
00597 {
00598 register char *p,
00599 *eof,
00600 *bol;
00601 register int lineno;
00602
00603 p = filep->f_p;
00604 eof = filep->f_end;
00605 if (p >= eof)
00606 return((char *)NULL);
00607 lineno = filep->f_line;
00608
00609 for (bol = p--; ++p < eof;) {
00610 if (*p == '/') {
00611 if (*(p + 1) == '/') {
00612 *p++ = ' ', *p++ = ' ';
00613 while (*p && *p != '\n')
00614 *p++ = ' ';
00615 p--;
00616 continue;
00617 } else if (*(p + 1) == '*') {
00618 *p++ = ' ', *p++ = ' ';
00619 while (*p) {
00620 if (*p == '*' && *(p + 1) == '/') {
00621 *p++ = ' ', *p = ' ';
00622 break;
00623 } else if (*p == '\n')
00624 lineno++;
00625 *p++ = ' ';
00626 }
00627 continue;
00628 }
00629 } else if (*p == '\\') {
00630 if (*(p + 1) == '\n') {
00631 *p = ' ';
00632 *(p + 1) = ' ';
00633 lineno++;
00634 }
00635 } else if (*p == '\n') {
00636 lineno++;
00637 if (*bol == '#') {
00638 register char *cp;
00639
00640 *p++ = '\0';
00641
00642 for (cp = bol + 1;
00643 (*cp == ' ' || *cp == '\t'); cp++) {};
00644 if (*cp) goto done;
00645 }
00646 bol = p + 1;
00647 }
00648 }
00649 if (*bol != '#')
00650 bol = NULL;
00651 done:
00652 filep->f_p = p;
00653 filep->f_line = lineno;
00654 return(bol);
00655 }
00656
00657
00658
00659
00660
00661 char *base_name(file)
00662 register char *file;
00663 {
00664 register char *p;
00665
00666 file = copy(file);
00667 for (p = file + strlen(file); p > file && *p != '.'; p--) ;
00668
00669 if (*p == '.')
00670 *p = '\0';
00671 return(file);
00672 }
00673
00674 #if defined(USG) && !defined(CRAY) && !defined(SVR4) && !defined(__EMX__) && !defined(clipper) && !defined(__clipper__)
00675 int rename(from, to)
00676 char *from, *to;
00677 {
00678 (void) unlink(to);
00679 if (link(from, to) == 0) {
00680 unlink(from);
00681 return 0;
00682 } else {
00683 return -1;
00684 }
00685 }
00686 #endif
00687
00688 void
00689 redirect(line, makefile)
00690 char *line,
00691 *makefile;
00692 {
00693 struct stat st;
00694 FILE *fdin = 0, *fdout = 0;
00695 char backup[ BUFSIZ ],
00696 buf[ BUFSIZ ];
00697 boolean found = FALSE;
00698 int len;
00699
00700
00701
00702
00703 if (makefile && *makefile == '-' && *(makefile + 1) == '\0') {
00704 puts(line);
00705 return;
00706 }
00707
00708
00709
00710
00711 if (!makefile) {
00712 if (stat("Makefile", &st) == 0)
00713 makefile = "Makefile";
00714 else if (stat("makefile", &st) == 0)
00715 makefile = "makefile";
00716 else
00717 fatalerr("[mM]akefile is not present\n");
00718 } else
00719 stat(makefile, &st);
00720 if (!rootBuild) {
00721 if ((fdin = fopen(makefile, "r")) == NULL)
00722 fatalerr("cannot open \"%s\"\n", makefile);
00723 sprintf(backup, "%s.bak", makefile);
00724 unlink(backup);
00725 #if defined(WIN32) || defined(__EMX__)
00726 fclose(fdin);
00727 #endif
00728 if (rename(makefile, backup) < 0)
00729 fatalerr("cannot rename %s to %s\n", makefile, backup);
00730 #if defined(WIN32) || defined(__EMX__)
00731 if ((fdin = fopen(backup, "r")) == NULL)
00732 fatalerr("cannot open \"%s\"\n", backup);
00733 #endif
00734 }
00735 if ((fdout = freopen(makefile, "w", stdout)) == NULL)
00736 fatalerr("cannot open \"%s\"\n", makefile);
00737 if (!rootBuild) {
00738 len = strlen(line);
00739 while (!found && fgets(buf, BUFSIZ, fdin)) {
00740 if (*buf == '#' && strncmp(line, buf, len) == 0)
00741 found = TRUE;
00742 fputs(buf, fdout);
00743 }
00744 if (!found) {
00745 if (verbose)
00746 warning("Adding new delimiting line \"%s\" and dependencies...\n",
00747 line);
00748 puts(line);
00749 } else if (append) {
00750 while (fgets(buf, BUFSIZ, fdin)) {
00751 fputs(buf, fdout);
00752 }
00753 }
00754 fflush(fdout);
00755 #if defined(USGISH) || defined(_SEQUENT_) || defined(USE_CHMOD)
00756 chmod(makefile, st.st_mode);
00757 #else
00758 fchmod(fileno(fdout), st.st_mode);
00759 fclose(fdin);
00760 #endif
00761 } else {
00762 printf(" ");
00763 fflush(fdout);
00764 }
00765 }
00766
00767 void fatalerr(char *msg, ...)
00768 {
00769 va_list args;
00770 fprintf(stderr, "%s: error: ", ProgramName);
00771 va_start(args, msg);
00772 vfprintf(stderr, msg, args);
00773 va_end(args);
00774 exit(1);
00775 }
00776
00777 void warning(char *msg, ...)
00778 {
00779 if (!rootBuild) {
00780 va_list args;
00781 fprintf(stderr, "%s: warning: ", ProgramName);
00782 va_start(args, msg);
00783 vfprintf(stderr, msg, args);
00784 va_end(args);
00785 }
00786 }
00787
00788 void warning1(char *msg, ...)
00789 {
00790 if (!rootBuild) {
00791 va_list args;
00792 va_start(args, msg);
00793 vfprintf(stderr, msg, args);
00794 va_end(args);
00795 }
00796 }