Annotation of researchv9/X11/src/X.V11R1/util/makedepend/main.c, revision 1.1.1.2

1.1       root        1: /*
                      2:  * $Header: main.c,v 1.6 87/08/14 17:59:51 toddb Exp $
                      3:  *
                      4:  * $Log:       main.c,v $
                      5:  * Revision 1.6  87/08/14  17:59:51  toddb
                      6:  * Added call to inc_clean() after traversing graph.
                      7:  * 
                      8:  * Revision 1.5  87/06/29  16:16:50  toddb
                      9:  * One too few bytes allocated.
                     10:  * 
                     11:  * Revision 1.4  87/06/29  16:13:18  toddb
                     12:  * Initial revision.
                     13:  * 
                     14:  * Revision 1.4  87/04/28  19:04:06  karlton
                     15:  * make sure makefile has contents before dereferencing
                     16:  * 
                     17:  * Revision 1.3  87/04/09  17:25:30  karlton
                     18:  * restore stat of Makefile
                     19:  * 
                     20:  * Revision 1.2  87/04/09  13:45:24  rich
                     21:  * added code to ignore unknown options.
                     22:  * 
                     23:  * Revision 1.1  87/04/08  16:40:48  rich
                     24:  * Initial revision
                     25:  * 
                     26:  * Revision 1.2  86/04/18  14:07:07  toddb
                     27:  * main() now initializes the delimiter message in the makefile if
                     28:  * one does not exist.
                     29:  * 
                     30:  * Revision 1.1  86/04/15  08:34:35  toddb
                     31:  * Initial revision
                     32:  * 
                     33:  */
                     34: #include "def.h"
                     35: #include       <sys/signal.h>
                     36: 
                     37: #ifdef DEBUG
                     38: int    debug;
                     39: #endif DEBUG
                     40: 
                     41: char   *directives[] = {
                     42:        "if",
                     43:        "ifdef",
                     44:        "ifndef",
                     45:        "else",
                     46:        "endif",
                     47:        "define",
                     48:        "undef",
                     49:        "include",
                     50:        "line",
                     51:        NULL
                     52: };
                     53: 
                     54: struct symtab  deflist[ MAXDEFINES ];
                     55: struct inclist inclist[ MAXFILES ],
                     56:                *inclistp = inclist;
                     57: 
                     58: char   *filelist[ MAXFILES ];
                     59: char   *includedirs[ MAXDIRS ];
                     60: char   *notdotdot[ MAXDIRS ];
                     61: char   *objfile = ".o";
                     62: char   *startat = "# DO NOT DELETE THIS LINE -- make depend depends on it.";
                     63: int    width = 78;
                     64: boolean        printed = FALSE;
                     65: boolean        verbose = FALSE;
                     66: boolean        show_where_not = FALSE;
                     67: int    catch();
                     68: 
                     69: 
                     70: main(argc, argv)
                     71:        int     argc;
                     72:        char    **argv;
                     73: {
                     74:        register struct symtab  *symp = deflist;
                     75:        register char   **fp = filelist;
                     76:        register char   **incp = includedirs;
                     77:        register char   *p;
                     78:        register int    i;
                     79:        register struct inclist *ip;
                     80:        char    *makefile = NULL;
                     81:        struct filepointer      *filecontent;
                     82: 
                     83:        for(argc--, argv++; argc; argc--, argv++) {
                     84:                if (**argv != '-') {
                     85:                        *fp++ = argv[0];
                     86:                        continue;
                     87:                }
                     88:                switch(argv[0][1]) {
                     89:                case 'D':
                     90:                        symp->s_name = argv[0]+2;
                     91:                        if (*symp->s_name == '\0') {
                     92:                                symp->s_name = *(++argv);
                     93:                                argc--;
                     94:                        }
                     95:                        for (p=symp->s_name; *p ; p++)
                     96:                                if (*p == '=') {
                     97:                                        *p++ = '\0';
                     98:                                        break;
                     99:                                }
                    100:                        symp->s_value = p;
                    101:                        symp++;
                    102:                        break;
                    103:                case 'I':
                    104:                        *incp++ = argv[0]+2;
                    105:                        if (**(incp-1) == '\0') {
                    106:                                *(incp-1) = *(++argv);
                    107:                                argc--;
                    108:                        }
                    109:                        break;
                    110:                case 'w':
                    111:                        if (argv[0][2] == '\0') {
                    112:                                argv++;
                    113:                                argc--;
                    114:                                width = atoi(argv[0]);
                    115:                        } else
                    116:                                width = atoi(argv[0]+2);
                    117:                        break;
                    118:                case 'o':
                    119:                        if (argv[0][2] == '\0') {
                    120:                                argv++;
                    121:                                argc--;
                    122:                                objfile = argv[0];
                    123:                        } else
                    124:                                objfile = argv[0]+2;
                    125:                        break;
                    126:                case 'v':
                    127:                        verbose = TRUE;
                    128: #ifdef DEBUG
                    129:                        if (argv[0][2])
                    130:                                debug = atoi(argv[0]+2);
                    131: #endif DEBUG
                    132:                        break;
                    133:                case 's':
                    134:                        startat = argv[0]+2;
                    135:                        if (*startat == '\0') {
                    136:                                startat = *(++argv);
                    137:                                argc--;
                    138:                        }
                    139:                        if (*startat != '#')
                    140:                                log_fatal("-s flag's value should start %s\n",
                    141:                                        "with '#'.");
                    142:                        break;
                    143:                case 'f':
                    144:                        makefile = argv[0]+2;
                    145:                        if (*makefile == '\0') {
                    146:                                makefile = *(++argv);
                    147:                                argc--;
                    148:                        }
                    149:                        break;
                    150:                default:
                    151:        /*              log_fatal("unknown opt = %s\n", argv[0]); */
                    152:                        log("ignoring option %s\n", argv[0]);
                    153:                }
                    154:        }
                    155:        *incp++ = INCLUDEDIR;
                    156: 
                    157:        redirect(startat, makefile);
                    158: 
                    159:        /*
                    160:         * catch signals.
                    161:         */
1.1.1.2 ! root      162:        signal(SIGHUP, catch);
        !           163:        signal(SIGINT, catch);
        !           164:        signal(SIGQUIT, catch);
        !           165:        signal(SIGILL, catch);
        !           166:        signal(SIGBUS, catch);
        !           167:        signal(SIGSEGV, catch);
        !           168:        signal(SIGSYS, catch);
1.1       root      169: 
                    170:        /*
                    171:         * now peruse through the list of files.
                    172:         */
                    173:        for(fp=filelist; *fp; fp++) {
                    174:                filecontent = getfile(*fp);
                    175:                ip = newinclude(*fp, (char *)NULL);
                    176: 
                    177:                find_includes(filecontent, ip, ip, 0);
                    178:                freefile(filecontent);
                    179:                recursive_pr_include(ip, ip->i_file, basename(*fp));
                    180:                inc_clean();
                    181:        }
                    182:        if (printed)
                    183:                printf("\n");
                    184:        exit(0);
                    185: }
                    186: 
                    187: struct filepointer *getfile(file)
                    188:        char    *file;
                    189: {
                    190:        register int    fd;
                    191:        struct filepointer      *content;
                    192:        struct stat     st;
                    193: 
                    194:        content = (struct filepointer *)malloc(sizeof(struct filepointer));
                    195:        if ((fd = open(file, O_RDONLY)) < 0) {
                    196:                log("cannot open \"%s\"\n", file);
                    197:                content->f_p = content->f_base = content->f_end = malloc(1);
                    198:                *content->f_p = '\0';
                    199:                return(content);
                    200:        }
                    201:        fstat(fd, &st);
                    202:        content->f_len = st.st_size+1;
                    203:        content->f_base = malloc(content->f_len);
                    204:        if (content->f_base == NULL)
                    205:                log_fatal("cannot allocate mem\n");
                    206:        if (read(fd, content->f_base, st.st_size) != st.st_size)
                    207:                log_fatal("cannot read all of %s\n", file);
                    208:        close(fd);
                    209:        content->f_p = content->f_base;
                    210:        content->f_end = content->f_base + st.st_size;
                    211:        *content->f_end = '\0';
                    212:        content->f_line = 0;
                    213:        return(content);
                    214: }
                    215: 
                    216: freefile(fp)
                    217:        struct filepointer      *fp;
                    218: {
                    219:        free(fp->f_base);
                    220:        free(fp);
                    221: }
                    222: 
                    223: /*VARARGS*/
                    224: log_fatal(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9)
                    225: {
                    226:        log(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9);
                    227:        exit (1);
                    228: }
                    229: 
                    230: /*VARARGS0*/
                    231: log(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9)
                    232: {
                    233:        fprintf(stderr, x0,x1,x2,x3,x4,x5,x6,x7,x8,x9);
                    234: }
                    235: 
                    236: char *copy(str)
                    237:        register char   *str;
                    238: {
                    239:        register char   *p = malloc(strlen(str) + 1);
                    240: 
                    241:        strcpy(p, str);
                    242:        return(p);
                    243: }
                    244: 
                    245: match(str, list)
                    246:        register char   *str, **list;
                    247: {
                    248:        register int    i;
                    249: 
                    250:        for (i=0; *list; i++, list++)
                    251:                if (strcmp(str, *list) == 0)
                    252:                        return(i);
                    253:        return(-1);
                    254: }
                    255: 
                    256: /*
                    257:  * Get the next line.  We only return lines beginning with '#' since that
                    258:  * is all this program is ever interested in.
                    259:  */
                    260: char *getline(filep)
                    261:        register struct filepointer     *filep;
                    262: {
                    263:        register char   *p,     /* walking pointer */
                    264:                        *eof,   /* end of file pointer */
                    265:                        *bol;   /* beginning of line pointer */
                    266:        register        lineno; /* line number */
                    267:        register        boolean interesting = FALSE;
                    268: 
                    269:        p = filep->f_p;
                    270:        eof = filep->f_end;
                    271:        if (p >= eof)
                    272:                return((char *)NULL);
                    273:        lineno = filep->f_line;
                    274: 
                    275:        for(bol = p--; ++p < eof; ) {
                    276:                if (*p == '/' && *(p+1) == '*') { /* consume comments */
                    277:                        *p++ = ' ', *p++ = ' ';
                    278:                        while (*p) {
                    279:                                if (*p == '*' && *(p+1) == '/') {
                    280:                                        *p++ = ' ', *p = ' ';
                    281:                                        break;
                    282:                                }
                    283:                                else if (*p == '\n')
                    284:                                        lineno++;
                    285:                                *p++ = ' ';
                    286:                        }
                    287:                        continue;
                    288:                }
                    289:                else if (*p == '\n') {
                    290:                        lineno++;
                    291:                        if (*bol == '#') {
                    292:                                *p++ = '\0';
                    293:                                goto done;
                    294:                        }
                    295:                        bol = p+1;
                    296:                }
                    297:        }
                    298:        if (*bol != '#')
                    299:                bol = NULL;
                    300: done:
                    301:        filep->f_p = p;
                    302:        filep->f_line = lineno;
                    303:        return(bol);
                    304: }
                    305: 
                    306: char *basename(file)
                    307:        register char   *file;
                    308: {
                    309:        register char   *p;
                    310: 
                    311:        for (p=file+strlen(file); p>file && *p != '/'; p--) ;
                    312: 
                    313:        if (*p == '/')
                    314:                p++;
                    315: 
                    316:        file = copy(p);
                    317:        for(p=file+strlen(file); p>file && *p != '.'; p--) ;
                    318: 
                    319:        if (*p == '.')
                    320:                *p = '\0';
                    321:        return(file);
                    322: }
                    323: 
                    324: redirect(line, makefile)
                    325:        char    *line,
                    326:                *makefile;
                    327: {
                    328:        struct stat     st;
                    329:        FILE    *fdin, *fdout;
                    330:        char    backup[ BUFSIZ ],
                    331:                buf[ BUFSIZ ];
                    332:        boolean found = FALSE;
                    333:        int     len;
                    334: 
                    335:        /*
                    336:         * if makefile is "-" then let it pour onto stdout.
                    337:         */
                    338:        if (makefile && *makefile == '-' && *(makefile+1) == '\0')
                    339:                return;
                    340: 
                    341:        /*
                    342:         * use a default makefile is not specified.
                    343:         */
                    344:        if (!makefile) {
                    345:                if (stat("makefile", &st) == 0)
                    346:                        makefile = "makefile";
                    347:                else if (stat("Makefile", &st) == 0)
                    348:                        makefile = "Makefile";
                    349:                else
                    350:                        log_fatal("[mM]akefile is not present\n");
                    351:        }
                    352:        else
                    353:            stat(makefile, &st);
                    354:        if ((fdin = fopen(makefile, "r")) == NULL)
                    355:                log_fatal("cannot open \"%s\"\n", makefile);
                    356:        sprintf(backup, "%s.bak", makefile);
                    357:        unlink(backup);
1.1.1.2 ! root      358:        
        !           359:        if (link(makefile, backup) < 0)
1.1       root      360:                log_fatal("cannot rename %s to %s\n", makefile, backup);
1.1.1.2 ! root      361:        unlink(makefile);
1.1       root      362:        if ((fdout = freopen(makefile, "w", stdout)) == NULL)
                    363:                log_fatal("cannot open \"%s\"\n", backup);
                    364:        len = strlen(line);
                    365:        while (fgets(buf, BUFSIZ, fdin) && !found) {
                    366:                if (*buf == '#' && strncmp(line, buf, len) == 0)
                    367:                        found = TRUE;
                    368:                fputs(buf, fdout);
                    369:        }
                    370:        if (!found) {
                    371:                log("delimiting line \"%s\" not found...\nAppending...\n",
                    372:                        line);
                    373:                puts(line); /* same as fputs(fdout); but with newline */
                    374:        }
                    375:        fflush(fdout);
                    376:        fchmod(fileno(fdout), st.st_mode);
                    377: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.