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[] = {
64 struct symtab deflist[ MAXDEFINES ];
65 struct inclist inclist[ MAXFILES ],
68 char *filelist[ MAXFILES ];
69 char *includedirs[ MAXDIRS ];
70 char *notdotdot[ MAXDIRS ];
72 char *startat = "# DO NOT DELETE THIS LINE -- make depend depends on it.";
74 boolean printed = FALSE;
75 boolean verbose = FALSE;
76 boolean show_where_not = FALSE;
78 #if defined(POSIX) || (defined(mips) && defined(SYSTYPE_SYSV))
79 typedef void sigreturn_t;
81 typedef int sigreturn_t;
85 #if !(defined(mips) && defined(SYSTYPE_SYSV))
86 struct sigvec sig_vec = {
98 #endif /* mips && SYSTYPE_SYSV */
104 register struct symtab *symp = deflist;
105 register char **fp = filelist;
106 register char **incp = includedirs;
108 register struct inclist *ip;
109 char *makefile = NULL;
110 struct filepointer *filecontent;
111 struct symtab *psymp = predefs;
112 char *endmarker = NULL;
114 while (psymp->s_name)
116 for(argc--, argv++; argc; argc--, argv++) {
117 /* if looking for endmarker then check before parsing */
118 if (endmarker && strcmp (endmarker, *argv) == 0) {
128 endmarker = &argv[0][2];
129 if (endmarker[0] == '\0') endmarker = "--";
132 symp->s_name = argv[0]+2;
133 if (*symp->s_name == '\0') {
134 symp->s_name = *(++argv);
137 for (p=symp->s_name; *p ; p++)
147 if (**(incp-1) == '\0') {
148 *(incp-1) = *(++argv);
152 /* do not use if endmarker processing */
154 if (endmarker) break;
155 if (argv[0][2] == '\0') {
158 width = atoi(argv[0]);
160 width = atoi(argv[0]+2);
163 if (endmarker) break;
164 if (argv[0][2] == '\0') {
172 if (endmarker) break;
176 debug = atoi(argv[0]+2);
180 if (endmarker) break;
182 if (*startat == '\0') {
187 log_fatal("-s flag's value should start %s\n",
191 if (endmarker) break;
192 makefile = argv[0]+2;
193 if (*makefile == '\0') {
194 makefile = *(++argv);
199 /* Ignore -O, -g so we can just pass ${CFLAGS} to
206 if (endmarker) break;
207 /* log_fatal("unknown opt = %s\n", argv[0]); */
208 log("ignoring option %s\n", argv[0]);
211 *incp++ = INCLUDEDIR;
213 redirect(startat, makefile);
218 #if defined (mips) && defined (SYSTYPE_SYSV)
219 /* should really reset SIGINT to SIG_IGN if it was. */
220 signal (SIGHUP, catch);
221 signal (SIGINT, catch);
222 signal (SIGQUIT, catch);
223 signal (SIGILL, catch);
224 signal (SIGBUS, catch);
225 signal (SIGSEGV, catch);
226 signal (SIGSYS, catch);
227 #else /* not (mips && SYSTYPE_SYSV) */
228 sigvec(SIGHUP, &sig_vec, (struct sigvec *)0);
229 sigvec(SIGINT, &sig_vec, (struct sigvec *)0);
230 sigvec(SIGQUIT, &sig_vec, (struct sigvec *)0);
231 sigvec(SIGILL, &sig_vec, (struct sigvec *)0);
232 sigvec(SIGBUS, &sig_vec, (struct sigvec *)0);
233 sigvec(SIGSEGV, &sig_vec, (struct sigvec *)0);
234 sigvec(SIGSYS, &sig_vec, (struct sigvec *)0);
235 #endif /* mips && SYSTYPE_SYSV */
238 * now peruse through the list of files.
240 for(fp=filelist; *fp; fp++) {
241 filecontent = getfile(*fp);
242 ip = newinclude(*fp, (char *)NULL);
244 find_includes(filecontent, ip, ip, 0);
245 freefile(filecontent);
246 recursive_pr_include(ip, ip->i_file, basename(*fp));
254 struct filepointer *getfile(file)
258 struct filepointer *content;
261 content = (struct filepointer *)malloc(sizeof(struct filepointer));
262 if ((fd = open(file, O_RDONLY)) < 0) {
263 log("cannot open \"%s\"\n", file);
264 content->f_p = content->f_base = content->f_end = malloc(1);
265 *content->f_p = '\0';
269 content->f_len = st.st_size+1;
270 content->f_base = malloc(content->f_len);
271 if (content->f_base == NULL)
272 log_fatal("cannot allocate mem\n");
273 if (read(fd, content->f_base, st.st_size) != st.st_size)
274 log_fatal("cannot read all of %s\n", file);
276 content->f_p = content->f_base;
277 content->f_end = content->f_base + st.st_size;
278 *content->f_end = '\0';
284 struct filepointer *fp;
291 log_fatal(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9)
292 char *x0, *x1, *x2, *x3, *x4, *x5, *x6, *x7, *x8, *x9;
294 log(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9);
299 log(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9)
300 char *x0, *x1, *x2, *x3, *x4, *x5, *x6, *x7, *x8, *x9;
302 fprintf(stderr, x0,x1,x2,x3,x4,x5,x6,x7,x8,x9);
308 register char *p = malloc(strlen(str) + 1);
315 register char *str, **list;
319 for (i=0; *list; i++, list++)
320 if (strcmp(str, *list) == 0)
326 * Get the next line. We only return lines beginning with '#' since that
327 * is all this program is ever interested in.
330 register struct filepointer *filep;
332 register char *p, /* walking pointer */
333 *eof, /* end of file pointer */
334 *bol; /* beginning of line pointer */
335 register lineno; /* line number */
340 return((char *)NULL);
341 lineno = filep->f_line;
343 for(bol = p--; ++p < eof; ) {
344 if (*p == '/' && *(p+1) == '*') { /* consume comments */
345 *p++ = ' ', *p++ = ' ';
347 if (*p == '*' && *(p+1) == '/') {
348 *p++ = ' ', *p = ' ';
357 else if (*p == '\n') {
370 filep->f_line = lineno;
379 for (p=file+strlen(file); p>file && *p != '/'; p--) ;
385 for(p=file+strlen(file); p>file && *p != '.'; p--) ;
392 redirect(line, makefile)
398 char backup[ BUFSIZ ],
400 boolean found = FALSE;
404 * if makefile is "-" then let it pour onto stdout.
406 if (makefile && *makefile == '-' && *(makefile+1) == '\0')
410 * use a default makefile is not specified.
413 if (stat("makefile", &st) == 0)
414 makefile = "makefile";
415 else if (stat("Makefile", &st) == 0)
416 makefile = "Makefile";
418 log_fatal("[mM]akefile is not present\n");
422 if ((fdin = fopen(makefile, "r")) == NULL)
423 log_fatal("cannot open \"%s\"\n", makefile);
424 sprintf(backup, "%s.bak", makefile);
426 if (rename(makefile, backup) < 0)
427 log_fatal("cannot rename %s to %s\n", makefile, backup);
428 if ((fdout = freopen(makefile, "w", stdout)) == NULL)
429 log_fatal("cannot open \"%s\"\n", backup);
431 while (fgets(buf, BUFSIZ, fdin) && !found) {
432 if (*buf == '#' && strncmp(line, buf, len) == 0)
437 log("Adding new delimiting line \"%s\"\nAdding dependencies...\n",
439 puts(line); /* same as fputs(fdout); but with newline */
442 #if defined (mips) && defined (SYSTYPE_SYSV)
443 chmod(makefile, st.st_mode);
444 #else /* not (mips && SYSTYPE_SYSV) */
445 fchmod(fileno(fdout), st.st_mode);
446 #endif /* mips && SYSTYPE_SYSV */