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[] = {
58 struct symtab deflist[ MAXDEFINES ];
59 struct inclist inclist[ MAXFILES ],
62 char *filelist[ MAXFILES ];
63 char *includedirs[ MAXDIRS ];
64 char *notdotdot[ MAXDIRS ];
66 char *startat = "# DO NOT DELETE THIS LINE -- make depend depends on it.";
68 boolean printed = FALSE;
69 boolean verbose = FALSE;
70 boolean show_where_not = FALSE;
72 #if defined(POSIX) || (defined(mips) && defined(SYSTYPE_SYSV))
73 typedef void sigreturn_t;
75 typedef int sigreturn_t;
79 #if !(defined(mips) && defined(SYSTYPE_SYSV))
80 struct sigvec sig_vec = {
92 #endif /* mips && SYSTYPE_SYSV */
98 register struct symtab *symp = deflist;
99 register char **fp = filelist;
100 register char **incp = includedirs;
102 register struct inclist *ip;
103 char *makefile = NULL;
104 struct filepointer *filecontent;
105 struct symtab *psymp = predefs;
106 char *endmarker = NULL;
108 while (psymp->s_name)
110 for(argc--, argv++; argc; argc--, argv++) {
111 /* if looking for endmarker then check before parsing */
112 if (endmarker && strcmp (endmarker, *argv) == 0) {
122 endmarker = &argv[0][2];
123 if (endmarker[0] == '\0') endmarker = "--";
126 symp->s_name = argv[0]+2;
127 if (*symp->s_name == '\0') {
128 symp->s_name = *(++argv);
131 for (p=symp->s_name; *p ; p++)
141 if (**(incp-1) == '\0') {
142 *(incp-1) = *(++argv);
146 /* do not use if endmarker processing */
148 if (endmarker) break;
149 if (argv[0][2] == '\0') {
152 width = atoi(argv[0]);
154 width = atoi(argv[0]+2);
157 if (endmarker) break;
158 if (argv[0][2] == '\0') {
166 if (endmarker) break;
170 debug = atoi(argv[0]+2);
174 if (endmarker) break;
176 if (*startat == '\0') {
181 log_fatal("-s flag's value should start %s\n",
185 if (endmarker) break;
186 makefile = argv[0]+2;
187 if (*makefile == '\0') {
188 makefile = *(++argv);
193 /* Ignore -O, -g so we can just pass ${CFLAGS} to
200 if (endmarker) break;
201 /* log_fatal("unknown opt = %s\n", argv[0]); */
202 log("ignoring option %s\n", argv[0]);
205 *incp++ = INCLUDEDIR;
207 redirect(startat, makefile);
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 */
232 * now peruse through the list of files.
234 for(fp=filelist; *fp; fp++) {
235 filecontent = getfile(*fp);
236 ip = newinclude(*fp, (char *)NULL);
238 find_includes(filecontent, ip, ip, 0);
239 freefile(filecontent);
240 recursive_pr_include(ip, ip->i_file, basename(*fp));
248 struct filepointer *getfile(file)
252 struct filepointer *content;
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';
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);
270 content->f_p = content->f_base;
271 content->f_end = content->f_base + st.st_size;
272 *content->f_end = '\0';
278 struct filepointer *fp;
285 log_fatal(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9)
287 log(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9);
292 log(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9)
294 fprintf(stderr, x0,x1,x2,x3,x4,x5,x6,x7,x8,x9);
300 register char *p = malloc(strlen(str) + 1);
307 register char *str, **list;
311 for (i=0; *list; i++, list++)
312 if (strcmp(str, *list) == 0)
318 * Get the next line. We only return lines beginning with '#' since that
319 * is all this program is ever interested in.
322 register struct filepointer *filep;
324 register char *p, /* walking pointer */
325 *eof, /* end of file pointer */
326 *bol; /* beginning of line pointer */
327 register lineno; /* line number */
332 return((char *)NULL);
333 lineno = filep->f_line;
335 for(bol = p--; ++p < eof; ) {
336 if (*p == '/' && *(p+1) == '*') { /* consume comments */
337 *p++ = ' ', *p++ = ' ';
339 if (*p == '*' && *(p+1) == '/') {
340 *p++ = ' ', *p = ' ';
349 else if (*p == '\n') {
362 filep->f_line = lineno;
371 for (p=file+strlen(file); p>file && *p != '/'; p--) ;
377 for(p=file+strlen(file); p>file && *p != '.'; p--) ;
384 redirect(line, makefile)
390 char backup[ BUFSIZ ],
392 boolean found = FALSE;
396 * if makefile is "-" then let it pour onto stdout.
398 if (makefile && *makefile == '-' && *(makefile+1) == '\0')
402 * use a default makefile is not specified.
405 if (stat("makefile", &st) == 0)
406 makefile = "makefile";
407 else if (stat("Makefile", &st) == 0)
408 makefile = "Makefile";
410 log_fatal("[mM]akefile is not present\n");
414 if ((fdin = fopen(makefile, "r")) == NULL)
415 log_fatal("cannot open \"%s\"\n", makefile);
416 sprintf(backup, "%s.bak", makefile);
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);
423 while (fgets(buf, BUFSIZ, fdin) && !found) {
424 if (*buf == '#' && strncmp(line, buf, len) == 0)
429 log("Adding new delimiting line \"%s\"\nAdding dependencies...\n",
431 puts(line); /* same as fputs(fdout); but with newline */
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 */