]> andersk Git - moira.git/blob - util/makedepend/main.c
aef00c2a00be7c7abb7468c82194eade760223b4
[moira.git] / util / makedepend / main.c
1 /*
2  * $XConsortium: main.c,v 1.20 88/10/22 11:26:14 jim Exp $
3  */
4 #include "def.h"
5 #ifdef hpux
6 #define sigvec sigvector
7 #endif /* hpux */
8 #include        <sys/signal.h>
9
10 #ifdef DEBUG
11 int     debug;
12 #endif
13
14 char    *directives[] = {
15         "if",
16         "ifdef",
17         "ifndef",
18         "else",
19         "endif",
20         "define",
21         "undef",
22         "include",
23         "line",
24         "pragma",
25         "elif",
26         "eject",
27         NULL
28 };
29
30 struct symtab   predefs[] = {
31 #ifdef apollo
32         {"apollo", NULL},
33 #endif
34 #ifdef ibm032
35         {"ibm032", NULL},
36 #endif
37 #ifdef sun
38         {"sun", NULL},
39 #endif
40 #ifdef hpux
41         {"hpux", NULL},
42 #endif
43 #ifdef vax
44         {"vax", NULL},
45 #endif
46 #ifdef VMS
47         {"VMS", NULL},
48 #endif
49 #ifdef ultrix
50         {"ultrix", NULL},
51 #endif
52 #ifdef mips
53         {"mips", NULL},
54 #endif
55         {NULL, NULL}
56 };
57
58 struct symtab   deflist[ MAXDEFINES ];
59 struct  inclist inclist[ MAXFILES ],
60                 *inclistp = inclist;
61
62 char    *filelist[ MAXFILES ];
63 char    *includedirs[ MAXDIRS ];
64 char    *notdotdot[ MAXDIRS ];
65 char    *objfile = ".o";
66 char    *startat = "# DO NOT DELETE THIS LINE -- make depend depends on it.";
67 int     width = 78;
68 boolean printed = FALSE;
69 boolean verbose = FALSE;
70 boolean show_where_not = FALSE;
71
72 #if defined(POSIX) || (defined(mips) && defined(SYSTYPE_SYSV))
73 typedef void sigreturn_t;
74 #else
75 typedef int sigreturn_t;
76 #endif
77 sigreturn_t catch();
78
79 #if !(defined(mips) && defined(SYSTYPE_SYSV))
80 struct sigvec sig_vec = {
81         catch,
82          (1<<(SIGINT -1))
83         |(1<<(SIGQUIT-1))
84         |(1<<(SIGBUS-1))
85         |(1<<(SIGILL-1))
86         |(1<<(SIGSEGV-1))
87         |(1<<(SIGHUP-1))
88         |(1<<(SIGPIPE-1))
89         |(1<<(SIGSYS-1)),
90         0
91 };
92 #endif /* mips && SYSTYPE_SYSV */
93
94 main(argc, argv)
95         int     argc;
96         char    **argv;
97 {
98         register struct symtab  *symp = deflist;
99         register char   **fp = filelist;
100         register char   **incp = includedirs;
101         register char   *p;
102         register struct inclist *ip;
103         char    *makefile = NULL;
104         struct filepointer      *filecontent;
105         struct symtab *psymp = predefs;
106         char *endmarker = NULL;
107
108         while (psymp->s_name)
109             *symp++ = *psymp++;
110         for(argc--, argv++; argc; argc--, argv++) {
111                 /* if looking for endmarker then check before parsing */
112                 if (endmarker && strcmp (endmarker, *argv) == 0) {
113                     endmarker = NULL;
114                     continue;
115                 }
116                 if (**argv != '-') {
117                         *fp++ = argv[0];
118                         continue;
119                 }
120                 switch(argv[0][1]) {
121                 case '-':
122                         endmarker = &argv[0][2];
123                         if (endmarker[0] == '\0') endmarker = "--";
124                         break;
125                 case 'D':
126                         symp->s_name = argv[0]+2;
127                         if (*symp->s_name == '\0') {
128                                 symp->s_name = *(++argv);
129                                 argc--;
130                         }
131                         for (p=symp->s_name; *p ; p++)
132                                 if (*p == '=') {
133                                         *p++ = '\0';
134                                         break;
135                                 }
136                         symp->s_value = p;
137                         symp++;
138                         break;
139                 case 'I':
140                         *incp++ = argv[0]+2;
141                         if (**(incp-1) == '\0') {
142                                 *(incp-1) = *(++argv);
143                                 argc--;
144                         }
145                         break;
146                 /* do not use if endmarker processing */
147                 case 'w':
148                         if (endmarker) break;
149                         if (argv[0][2] == '\0') {
150                                 argv++;
151                                 argc--;
152                                 width = atoi(argv[0]);
153                         } else
154                                 width = atoi(argv[0]+2);
155                         break;
156                 case 'o':
157                         if (endmarker) break;
158                         if (argv[0][2] == '\0') {
159                                 argv++;
160                                 argc--;
161                                 objfile = argv[0];
162                         } else
163                                 objfile = argv[0]+2;
164                         break;
165                 case 'v':
166                         if (endmarker) break;
167                         verbose = TRUE;
168 #ifdef DEBUG
169                         if (argv[0][2])
170                                 debug = atoi(argv[0]+2);
171 #endif
172                         break;
173                 case 's':
174                         if (endmarker) break;
175                         startat = argv[0]+2;
176                         if (*startat == '\0') {
177                                 startat = *(++argv);
178                                 argc--;
179                         }
180                         if (*startat != '#')
181                                 log_fatal("-s flag's value should start %s\n",
182                                         "with '#'.");
183                         break;
184                 case 'f':
185                         if (endmarker) break;
186                         makefile = argv[0]+2;
187                         if (*makefile == '\0') {
188                                 makefile = *(++argv);
189                                 argc--;
190                         }
191                         break;
192                 
193                 /* Ignore -O, -g so we can just pass ${CFLAGS} to
194                    makedepend
195                  */
196                 case 'O':
197                 case 'g':
198                         break;
199                 default:
200                         if (endmarker) break;
201         /*              log_fatal("unknown opt = %s\n", argv[0]); */
202                         log("ignoring option %s\n", argv[0]);
203                 }
204         }
205         *incp++ = INCLUDEDIR;
206
207         redirect(startat, makefile);
208
209         /*
210          * catch signals.
211          */
212 #if defined (mips) && defined (SYSTYPE_SYSV)
213 /*  should really reset SIGINT to SIG_IGN if it was.  */
214         signal (SIGHUP, catch);
215         signal (SIGINT, catch);
216         signal (SIGQUIT, catch);
217         signal (SIGILL, catch);
218         signal (SIGBUS, catch);
219         signal (SIGSEGV, catch);
220         signal (SIGSYS, catch);
221 #else /* not (mips && SYSTYPE_SYSV) */
222         sigvec(SIGHUP, &sig_vec, (struct sigvec *)0);
223         sigvec(SIGINT, &sig_vec, (struct sigvec *)0);
224         sigvec(SIGQUIT, &sig_vec, (struct sigvec *)0);
225         sigvec(SIGILL, &sig_vec, (struct sigvec *)0);
226         sigvec(SIGBUS, &sig_vec, (struct sigvec *)0);
227         sigvec(SIGSEGV, &sig_vec, (struct sigvec *)0);
228         sigvec(SIGSYS, &sig_vec, (struct sigvec *)0);
229 #endif /* mips && SYSTYPE_SYSV */
230
231         /*
232          * now peruse through the list of files.
233          */
234         for(fp=filelist; *fp; fp++) {
235                 filecontent = getfile(*fp);
236                 ip = newinclude(*fp, (char *)NULL);
237
238                 find_includes(filecontent, ip, ip, 0);
239                 freefile(filecontent);
240                 recursive_pr_include(ip, ip->i_file, basename(*fp));
241                 inc_clean();
242         }
243         if (printed)
244                 printf("\n");
245         exit(0);
246 }
247
248 struct filepointer *getfile(file)
249         char    *file;
250 {
251         register int    fd;
252         struct filepointer      *content;
253         struct stat     st;
254
255         content = (struct filepointer *)malloc(sizeof(struct filepointer));
256         if ((fd = open(file, O_RDONLY)) < 0) {
257                 log("cannot open \"%s\"\n", file);
258                 content->f_p = content->f_base = content->f_end = malloc(1);
259                 *content->f_p = '\0';
260                 return(content);
261         }
262         fstat(fd, &st);
263         content->f_len = st.st_size+1;
264         content->f_base = malloc(content->f_len);
265         if (content->f_base == NULL)
266                 log_fatal("cannot allocate mem\n");
267         if (read(fd, content->f_base, st.st_size) != st.st_size)
268                 log_fatal("cannot read all of %s\n", file);
269         close(fd);
270         content->f_p = content->f_base;
271         content->f_end = content->f_base + st.st_size;
272         *content->f_end = '\0';
273         content->f_line = 0;
274         return(content);
275 }
276
277 freefile(fp)
278         struct filepointer      *fp;
279 {
280         free(fp->f_base);
281         free(fp);
282 }
283
284 /*VARARGS*/
285 log_fatal(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9)
286 {
287         log(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9);
288         exit (1);
289 }
290
291 /*VARARGS0*/
292 log(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9)
293 {
294         fprintf(stderr, x0,x1,x2,x3,x4,x5,x6,x7,x8,x9);
295 }
296
297 char *copy(str)
298         register char   *str;
299 {
300         register char   *p = malloc(strlen(str) + 1);
301
302         strcpy(p, str);
303         return(p);
304 }
305
306 match(str, list)
307         register char   *str, **list;
308 {
309         register int    i;
310
311         for (i=0; *list; i++, list++)
312                 if (strcmp(str, *list) == 0)
313                         return(i);
314         return(-1);
315 }
316
317 /*
318  * Get the next line.  We only return lines beginning with '#' since that
319  * is all this program is ever interested in.
320  */
321 char *getline(filep)
322         register struct filepointer     *filep;
323 {
324         register char   *p,     /* walking pointer */
325                         *eof,   /* end of file pointer */
326                         *bol;   /* beginning of line pointer */
327         register        lineno; /* line number */
328
329         p = filep->f_p;
330         eof = filep->f_end;
331         if (p >= eof)
332                 return((char *)NULL);
333         lineno = filep->f_line;
334
335         for(bol = p--; ++p < eof; ) {
336                 if (*p == '/' && *(p+1) == '*') { /* consume comments */
337                         *p++ = ' ', *p++ = ' ';
338                         while (*p) {
339                                 if (*p == '*' && *(p+1) == '/') {
340                                         *p++ = ' ', *p = ' ';
341                                         break;
342                                 }
343                                 else if (*p == '\n')
344                                         lineno++;
345                                 *p++ = ' ';
346                         }
347                         continue;
348                 }
349                 else if (*p == '\n') {
350                         lineno++;
351                         if (*bol == '#') {
352                                 *p++ = '\0';
353                                 goto done;
354                         }
355                         bol = p+1;
356                 }
357         }
358         if (*bol != '#')
359                 bol = NULL;
360 done:
361         filep->f_p = p;
362         filep->f_line = lineno;
363         return(bol);
364 }
365
366 char *basename(file)
367         register char   *file;
368 {
369         register char   *p;
370
371         for (p=file+strlen(file); p>file && *p != '/'; p--) ;
372
373         if (*p == '/')
374                 p++;
375
376         file = copy(p);
377         for(p=file+strlen(file); p>file && *p != '.'; p--) ;
378
379         if (*p == '.')
380                 *p = '\0';
381         return(file);
382 }
383
384 redirect(line, makefile)
385         char    *line,
386                 *makefile;
387 {
388         struct stat     st;
389         FILE    *fdin, *fdout;
390         char    backup[ BUFSIZ ],
391                 buf[ BUFSIZ ];
392         boolean found = FALSE;
393         int     len;
394
395         /*
396          * if makefile is "-" then let it pour onto stdout.
397          */
398         if (makefile && *makefile == '-' && *(makefile+1) == '\0')
399                 return;
400
401         /*
402          * use a default makefile is not specified.
403          */
404         if (!makefile) {
405                 if (stat("makefile", &st) == 0)
406                         makefile = "makefile";
407                 else if (stat("Makefile", &st) == 0)
408                         makefile = "Makefile";
409                 else
410                         log_fatal("[mM]akefile is not present\n");
411         }
412         else
413             stat(makefile, &st);
414         if ((fdin = fopen(makefile, "r")) == NULL)
415                 log_fatal("cannot open \"%s\"\n", makefile);
416         sprintf(backup, "%s.bak", makefile);
417         unlink(backup);
418         if (rename(makefile, backup) < 0)
419                 log_fatal("cannot rename %s to %s\n", makefile, backup);
420         if ((fdout = freopen(makefile, "w", stdout)) == NULL)
421                 log_fatal("cannot open \"%s\"\n", backup);
422         len = strlen(line);
423         while (fgets(buf, BUFSIZ, fdin) && !found) {
424                 if (*buf == '#' && strncmp(line, buf, len) == 0)
425                         found = TRUE;
426                 fputs(buf, fdout);
427         }
428         if (!found) {
429                 log("Adding new delimiting line \"%s\"\nAdding dependencies...\n",
430                         line);
431                 puts(line); /* same as fputs(fdout); but with newline */
432         }
433         fflush(fdout);
434 #if defined (mips) && defined (SYSTYPE_SYSV)
435         chmod(makefile, st.st_mode);
436 #else /* not (mips && SYSTYPE_SYSV) */
437         fchmod(fileno(fdout), st.st_mode);
438 #endif /* mips && SYSTYPE_SYSV */
439 }
This page took 0.454432 seconds and 3 git commands to generate.