1
/* $XConsortium: main.c,v 1.83 94/04/17 20:10:36 gildea Exp $ */
4
Copyright (c) 1993, 1994 X Consortium
6
Permission is hereby granted, free of charge, to any person obtaining a copy
7
of this software and associated documentation files (the "Software"), to deal
8
in the Software without restriction, including without limitation the rights
9
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
copies of the Software, and to permit persons to whom the Software is
11
furnished to do so, subject to the following conditions:
13
The above copyright notice and this permission notice shall be included in
14
all copies or substantial portions of the Software.
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
Except as contained in this notice, the name of the X Consortium shall not be
24
used in advertising or otherwise to promote the sale, use or other dealings
25
in this Software without prior written authorization from the X Consortium.
31
#define sigvec sigvector
36
#if NeedVarargsPrototypes
46
char *directives[] = {
66
#include "imakemdep.h" /* from config sources */
69
struct inclist inclist[ MAXFILES ],
73
char *filelist[ MAXFILES ];
74
char *includedirs[ MAXDIRS + 1 ];
75
char *notdotdot[ MAXDIRS ];
77
char *objsuffix = OBJSUFFIX;
78
char *startat = "# DO NOT DELETE";
80
boolean append = FALSE;
81
boolean printed = FALSE;
82
boolean verbose = FALSE;
83
boolean show_where_not = FALSE;
84
boolean warn_multiple = FALSE; /* Warn on multiple includes of same file */
87
#ifdef SIGNALRETURNSINT
96
fatalerr ("got signal %d\n", sig);
99
#if defined(USG) || (defined(SYSV386) && defined(SYSV)) || defined(WIN32)
104
#ifndef _POSIX_SOURCE
105
#define sigaction sigvec
106
#define sa_handler sv_handler
107
#define sa_mask sv_mask
108
#define sa_flags sv_flags
110
struct sigaction sig_act;
117
register char **fp = filelist;
118
register char **incp = includedirs;
120
register struct inclist *ip;
121
char *makefile = NULL;
122
struct filepointer *filecontent;
123
struct symtab *psymp = predefs;
124
char *endmarker = NULL;
125
char *defincdir = NULL;
127
ProgramName = argv[0];
129
while (psymp->s_name)
131
define2(psymp->s_name, psymp->s_value, &maininclist);
134
if (argc == 2 && argv[1][0] == '@') {
140
char quotechar = '\0';
143
if ((afd = open(argv[1]+1, O_RDONLY)) < 0)
144
fatalerr("cannot open \"%s\"\n", argv[1]+1);
146
args = (char *)malloc(ast.st_size + 1);
147
if ((ast.st_size = read(afd, args, ast.st_size)) < 0)
148
fatalerr("failed to read %s\n", argv[1]+1);
149
args[ast.st_size] = '\0';
151
for (p = args; *p; p++) {
153
if (quotechar == '\\' ||
154
(*p == quotechar && p[-1] != '\\'))
167
if (p > args && p[-1])
174
nargv = (char **)malloc(nargc * sizeof(char *));
177
for (p = args; argc < nargc; p += strlen(p) + 1)
178
if (*p) nargv[argc++] = p;
181
for(argc--, argv++; argc; argc--, argv++) {
182
/* if looking for endmarker then check before parsing */
183
if (endmarker && strcmp (endmarker, *argv) == 0) {
188
/* treat +thing as an option for C++ */
189
if (endmarker && **argv == '+')
196
endmarker = &argv[0][2];
197
if (endmarker[0] == '\0') endmarker = "--";
200
if (argv[0][2] == '\0') {
204
for (p=argv[0] + 2; *p ; p++)
209
define(argv[0] + 2, &maininclist);
212
if (incp >= includedirs + MAXDIRS)
213
fatalerr("Too many -I flags.\n");
215
if (**(incp-1) == '\0') {
216
*(incp-1) = *(++argv);
221
defincdir = argv[0]+2;
223
/* do not use if endmarker processing */
225
if (endmarker) break;
229
if (endmarker) break;
230
if (argv[0][2] == '\0') {
233
width = atoi(argv[0]);
235
width = atoi(argv[0]+2);
238
if (endmarker) break;
239
if (argv[0][2] == '\0') {
244
objsuffix = argv[0]+2;
247
if (endmarker) break;
248
if (argv[0][2] == '\0') {
253
objprefix = argv[0]+2;
256
if (endmarker) break;
260
_debugmask = atoi(argv[0]+2);
264
if (endmarker) break;
266
if (*startat == '\0') {
271
fatalerr("-s flag's value should start %s\n",
275
if (endmarker) break;
276
makefile = argv[0]+2;
277
if (*makefile == '\0') {
278
makefile = *(++argv);
284
warn_multiple = TRUE;
287
/* Ignore -O, -g so we can just pass ${CFLAGS} to
294
if (endmarker) break;
295
/* fatalerr("unknown opt = %s\n", argv[0]); */
296
warning("ignoring option %s\n", argv[0]);
301
if (incp >= includedirs + MAXDIRS)
302
fatalerr("Too many -I flags.\n");
305
if (incp >= includedirs + MAXDIRS)
306
fatalerr("Too many -I flags.\n");
307
*incp++ = INCLUDEDIR;
309
if (incp >= includedirs + MAXDIRS)
310
fatalerr("Too many -I flags.\n");
311
*incp++ = POSTINCDIR;
313
} else if (*defincdir) {
314
if (incp >= includedirs + MAXDIRS)
315
fatalerr("Too many -I flags.\n");
319
redirect(startat, makefile);
325
/* should really reset SIGINT to SIG_IGN if it was. */
327
signal (SIGHUP, catch);
329
signal (SIGINT, catch);
331
signal (SIGQUIT, catch);
333
signal (SIGILL, catch);
335
signal (SIGBUS, catch);
337
signal (SIGSEGV, catch);
339
signal (SIGSYS, catch);
342
sig_act.sa_handler = catch;
344
sigemptyset(&sig_act.sa_mask);
345
sigaddset(&sig_act.sa_mask, SIGINT);
346
sigaddset(&sig_act.sa_mask, SIGQUIT);
348
sigaddset(&sig_act.sa_mask, SIGBUS);
350
sigaddset(&sig_act.sa_mask, SIGILL);
351
sigaddset(&sig_act.sa_mask, SIGSEGV);
352
sigaddset(&sig_act.sa_mask, SIGHUP);
353
sigaddset(&sig_act.sa_mask, SIGPIPE);
355
sigaddset(&sig_act.sa_mask, SIGSYS);
358
sig_act.sa_mask = ((1<<(SIGINT -1))
371
#endif /* _POSIX_SOURCE */
372
sig_act.sa_flags = 0;
373
sigaction(SIGHUP, &sig_act, (struct sigaction *)0);
374
sigaction(SIGINT, &sig_act, (struct sigaction *)0);
375
sigaction(SIGQUIT, &sig_act, (struct sigaction *)0);
376
sigaction(SIGILL, &sig_act, (struct sigaction *)0);
378
sigaction(SIGBUS, &sig_act, (struct sigaction *)0);
380
sigaction(SIGSEGV, &sig_act, (struct sigaction *)0);
382
sigaction(SIGSYS, &sig_act, (struct sigaction *)0);
387
* now peruse through the list of files.
389
for(fp=filelist; *fp; fp++) {
390
filecontent = getfile(*fp);
391
ip = newinclude(*fp, (char *)NULL);
393
find_includes(filecontent, ip, ip, 0, FALSE);
394
freefile(filecontent);
395
recursive_pr_include(ip, ip->i_file, base_name(*fp));
403
struct filepointer *getfile(file)
407
struct filepointer *content;
410
content = (struct filepointer *)malloc(sizeof(struct filepointer));
411
if ((fd = open(file, O_RDONLY)) < 0) {
412
warning("cannot open \"%s\"\n", file);
413
content->f_p = content->f_base = content->f_end = (char *)malloc(1);
414
*content->f_p = '\0';
418
content->f_base = (char *)malloc(st.st_size+1);
419
if (content->f_base == NULL)
420
fatalerr("cannot allocate mem\n");
421
if ((st.st_size = read(fd, content->f_base, st.st_size)) < 0)
422
fatalerr("failed to read %s\n", file);
424
content->f_len = st.st_size+1;
425
content->f_p = content->f_base;
426
content->f_end = content->f_base + st.st_size;
427
*content->f_end = '\0';
433
struct filepointer *fp;
442
register char *p = (char *)malloc(strlen(str) + 1);
449
register char *str, **list;
453
for (i=0; *list; i++, list++)
454
if (strcmp(str, *list) == 0)
460
* Get the next line. We only return lines beginning with '#' since that
461
* is all this program is ever interested in.
464
register struct filepointer *filep;
466
register char *p, /* walking pointer */
467
*eof, /* end of file pointer */
468
*bol; /* beginning of line pointer */
469
register lineno; /* line number */
474
return((char *)NULL);
475
lineno = filep->f_line;
477
for(bol = p--; ++p < eof; ) {
478
if (*p == '/' && *(p+1) == '*') { /* consume comments */
479
*p++ = ' ', *p++ = ' ';
481
if (*p == '*' && *(p+1) == '/') {
482
*p++ = ' ', *p = ' ';
492
else if (*p == '/' && *(p+1) == '/') { /* consume comments */
493
*p++ = ' ', *p++ = ' ';
494
while (*p && *p != '\n')
500
else if (*p == '\\') {
501
if (*(p+1) == '\n') {
507
else if (*p == '\n') {
513
/* punt lines with just # (yacc generated) */
515
*cp && (*cp == ' ' || *cp == '\t'); cp++);
525
filep->f_line = lineno;
530
* Strip the file name down to what we want to see in the Makefile.
531
* It will have objprefix and objsuffix around it.
533
char *base_name(file)
539
for(p=file+strlen(file); p>file && *p != '.'; p--) ;
546
#if defined(USG) && !defined(CRAY) && !defined(SVR4)
547
int rename (from, to)
551
if (link (from, to) == 0) {
560
redirect(line, makefile)
566
char backup[ BUFSIZ ],
568
boolean found = FALSE;
572
* if makefile is "-" then let it pour onto stdout.
574
if (makefile && *makefile == '-' && *(makefile+1) == '\0')
578
* use a default makefile is not specified.
581
if (stat("Makefile", &st) == 0)
582
makefile = "Makefile";
583
else if (stat("makefile", &st) == 0)
584
makefile = "makefile";
586
fatalerr("[mM]akefile is not present\n");
590
if ((fdin = fopen(makefile, "r")) == NULL)
591
fatalerr("cannot open \"%s\"\n", makefile);
592
sprintf(backup, "%s.bak", makefile);
597
if (rename(makefile, backup) < 0)
598
fatalerr("cannot rename %s to %s\n", makefile, backup);
600
if ((fdin = fopen(backup, "r")) == NULL)
601
fatalerr("cannot open \"%s\"\n", backup);
603
if ((fdout = freopen(makefile, "w", stdout)) == NULL)
604
fatalerr("cannot open \"%s\"\n", backup);
606
while (!found && fgets(buf, BUFSIZ, fdin)) {
607
if (*buf == '#' && strncmp(line, buf, len) == 0)
613
warning("Adding new delimiting line \"%s\" and dependencies...\n",
615
puts(line); /* same as fputs(fdout); but with newline */
617
while (fgets(buf, BUFSIZ, fdin)) {
622
#if defined(USGISH) || defined(_SEQUENT_)
623
chmod(makefile, st.st_mode);
625
fchmod(fileno(fdout), st.st_mode);
629
#if NeedVarargsPrototypes
630
fatalerr(char *msg, ...)
633
fatalerr(msg,x1,x2,x3,x4,x5,x6,x7,x8,x9)
637
#if NeedVarargsPrototypes
640
fprintf(stderr, "%s: error: ", ProgramName);
641
#if NeedVarargsPrototypes
643
vfprintf(stderr, msg, args);
646
fprintf(stderr, msg,x1,x2,x3,x4,x5,x6,x7,x8,x9);
651
#if NeedVarargsPrototypes
652
warning(char *msg, ...)
655
warning(msg,x1,x2,x3,x4,x5,x6,x7,x8,x9)
659
#if NeedVarargsPrototypes
662
fprintf(stderr, "%s: warning: ", ProgramName);
663
#if NeedVarargsPrototypes
665
vfprintf(stderr, msg, args);
668
fprintf(stderr, msg,x1,x2,x3,x4,x5,x6,x7,x8,x9);
672
#if NeedVarargsPrototypes
673
warning1(char *msg, ...)
676
warning1(msg,x1,x2,x3,x4,x5,x6,x7,x8,x9)
680
#if NeedVarargsPrototypes
683
vfprintf(stderr, msg, args);
686
fprintf(stderr, msg,x1,x2,x3,x4,x5,x6,x7,x8,x9);