2 * $XConsortium: main.c,v 1.20 88/10/22 11:26:14 jim Exp $
6 #define sigvec sigvector
8 #include <sys/signal.h>
14 char *directives[] = {
30 struct symtab predefs[] = {
61 struct symtab deflist[ MAXDEFINES ];
62 struct inclist inclist[ MAXFILES ],
65 char *filelist[ MAXFILES ];
66 char *includedirs[ MAXDIRS ];
67 char *notdotdot[ MAXDIRS ];
69 char *startat = "# DO NOT DELETE THIS LINE -- make depend depends on it.";
71 boolean printed = FALSE;
72 boolean verbose = FALSE;
73 boolean show_where_not = FALSE;
75 #if defined(POSIX) || (defined(mips) && defined(SYSTYPE_SYSV))
76 typedef void sigreturn_t;
78 typedef int sigreturn_t;
82 #if !(defined(mips) && defined(SYSTYPE_SYSV))
83 struct sigvec sig_vec = {
95 #endif /* mips && SYSTYPE_SYSV */
101 register struct symtab *symp = deflist;
102 register char **fp = filelist;
103 register char **incp = includedirs;
105 register struct inclist *ip;
106 char *makefile = NULL;
107 struct filepointer *filecontent;
108 struct symtab *psymp = predefs;
109 char *endmarker = NULL;
111 while (psymp->s_name)
113 for(argc--, argv++; argc; argc--, argv++) {
114 /* if looking for endmarker then check before parsing */
115 if (endmarker && strcmp (endmarker, *argv) == 0) {
125 endmarker = &argv[0][2];
126 if (endmarker[0] == '\0') endmarker = "--";
129 symp->s_name = argv[0]+2;
130 if (*symp->s_name == '\0') {
131 symp->s_name = *(++argv);
134 for (p=symp->s_name; *p ; p++)
144 if (**(incp-1) == '\0') {
145 *(incp-1) = *(++argv);
149 /* do not use if endmarker processing */
151 if (endmarker) break;
152 if (argv[0][2] == '\0') {
155 width = atoi(argv[0]);
157 width = atoi(argv[0]+2);
160 if (endmarker) break;
161 if (argv[0][2] == '\0') {
169 if (endmarker) break;
173 debug = atoi(argv[0]+2);
177 if (endmarker) break;
179 if (*startat == '\0') {
184 log_fatal("-s flag's value should start %s\n",
188 if (endmarker) break;
189 makefile = argv[0]+2;
190 if (*makefile == '\0') {
191 makefile = *(++argv);
196 /* Ignore -O, -g so we can just pass ${CFLAGS} to
203 if (endmarker) break;
204 /* log_fatal("unknown opt = %s\n", argv[0]); */
205 log("ignoring option %s\n", argv[0]);
208 *incp++ = INCLUDEDIR;
210 redirect(startat, makefile);
215 #if defined (mips) && defined (SYSTYPE_SYSV)
216 /* should really reset SIGINT to SIG_IGN if it was. */
217 signal (SIGHUP, catch);
218 signal (SIGINT, catch);
219 signal (SIGQUIT, catch);
220 signal (SIGILL, catch);
221 signal (SIGBUS, catch);
222 signal (SIGSEGV, catch);
223 signal (SIGSYS, catch);
224 #else /* not (mips && SYSTYPE_SYSV) */
225 sigvec(SIGHUP, &sig_vec, (struct sigvec *)0);
226 sigvec(SIGINT, &sig_vec, (struct sigvec *)0);
227 sigvec(SIGQUIT, &sig_vec, (struct sigvec *)0);
228 sigvec(SIGILL, &sig_vec, (struct sigvec *)0);
229 sigvec(SIGBUS, &sig_vec, (struct sigvec *)0);
230 sigvec(SIGSEGV, &sig_vec, (struct sigvec *)0);
231 sigvec(SIGSYS, &sig_vec, (struct sigvec *)0);
232 #endif /* mips && SYSTYPE_SYSV */
235 * now peruse through the list of files.
237 for(fp=filelist; *fp; fp++) {
238 filecontent = getfile(*fp);
239 ip = newinclude(*fp, (char *)NULL);
241 find_includes(filecontent, ip, ip, 0);
242 freefile(filecontent);
243 recursive_pr_include(ip, ip->i_file, basename(*fp));
251 struct filepointer *getfile(file)
255 struct filepointer *content;
258 content = (struct filepointer *)malloc(sizeof(struct filepointer));
259 if ((fd = open(file, O_RDONLY)) < 0) {
260 log("cannot open \"%s\"\n", file);
261 content->f_p = content->f_base = content->f_end = malloc(1);
262 *content->f_p = '\0';
266 content->f_len = st.st_size+1;
267 content->f_base = malloc(content->f_len);
268 if (content->f_base == NULL)
269 log_fatal("cannot allocate mem\n");
270 if (read(fd, content->f_base, st.st_size) != st.st_size)
271 log_fatal("cannot read all of %s\n", file);
273 content->f_p = content->f_base;
274 content->f_end = content->f_base + st.st_size;
275 *content->f_end = '\0';
281 struct filepointer *fp;
288 log_fatal(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9)
290 log(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9);
295 log(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9)
297 fprintf(stderr, x0,x1,x2,x3,x4,x5,x6,x7,x8,x9);
303 register char *p = malloc(strlen(str) + 1);
310 register char *str, **list;
314 for (i=0; *list; i++, list++)
315 if (strcmp(str, *list) == 0)
321 * Get the next line. We only return lines beginning with '#' since that
322 * is all this program is ever interested in.
325 register struct filepointer *filep;
327 register char *p, /* walking pointer */
328 *eof, /* end of file pointer */
329 *bol; /* beginning of line pointer */
330 register lineno; /* line number */
335 return((char *)NULL);
336 lineno = filep->f_line;
338 for(bol = p--; ++p < eof; ) {
339 if (*p == '/' && *(p+1) == '*') { /* consume comments */
340 *p++ = ' ', *p++ = ' ';
342 if (*p == '*' && *(p+1) == '/') {
343 *p++ = ' ', *p = ' ';
352 else if (*p == '\n') {
365 filep->f_line = lineno;
374 for (p=file+strlen(file); p>file && *p != '/'; p--) ;
380 for(p=file+strlen(file); p>file && *p != '.'; p--) ;
387 redirect(line, makefile)
393 char backup[ BUFSIZ ],
395 boolean found = FALSE;
399 * if makefile is "-" then let it pour onto stdout.
401 if (makefile && *makefile == '-' && *(makefile+1) == '\0')
405 * use a default makefile is not specified.
408 if (stat("makefile", &st) == 0)
409 makefile = "makefile";
410 else if (stat("Makefile", &st) == 0)
411 makefile = "Makefile";
413 log_fatal("[mM]akefile is not present\n");
417 if ((fdin = fopen(makefile, "r")) == NULL)
418 log_fatal("cannot open \"%s\"\n", makefile);
419 sprintf(backup, "%s.bak", makefile);
421 if (rename(makefile, backup) < 0)
422 log_fatal("cannot rename %s to %s\n", makefile, backup);
423 if ((fdout = freopen(makefile, "w", stdout)) == NULL)
424 log_fatal("cannot open \"%s\"\n", backup);
426 while (fgets(buf, BUFSIZ, fdin) && !found) {
427 if (*buf == '#' && strncmp(line, buf, len) == 0)
432 log("Adding new delimiting line \"%s\"\nAdding dependencies...\n",
434 puts(line); /* same as fputs(fdout); but with newline */
437 #if defined (mips) && defined (SYSTYPE_SYSV)
438 chmod(makefile, st.st_mode);
439 #else /* not (mips && SYSTYPE_SYSV) */
440 fchmod(fileno(fdout), st.st_mode);
441 #endif /* mips && SYSTYPE_SYSV */