2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $TOG: main.c /main/86 1998/03/25 08:17:50 kaleb $ */
26 Copyright (c) 1993, 1994, 1998 The Open Group
30 The above copyright notice and this permission notice shall be included in
31 all copies or substantial portions of the Software.
33 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
37 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
38 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 Except as contained in this notice, the name of The Open Group shall not be
41 used in advertising or otherwise to promote the sale, use or other dealings
42 in this Software without prior written authorization from The Open Group.
48 #define sigvec sigvector
51 #ifdef X_POSIX_C_SOURCE
52 #define _POSIX_C_SOURCE X_POSIX_C_SOURCE
54 #undef _POSIX_C_SOURCE
56 #if defined(X_NOT_POSIX) || defined(_POSIX_SOURCE)
65 #if NeedVarargsPrototypes
75 char *directives[] = {
96 #include "imakemdep.h" /* from config sources */
99 struct inclist inclist[ MAXFILES ],
103 char *filelist[ MAXFILES ];
104 char *includedirs[ MAXDIRS + 1 ];
105 char *notdotdot[ MAXDIRS ];
106 char *objprefix = "";
107 char *objsuffix = OBJSUFFIX;
108 char *startat = "# DO NOT DELETE";
110 boolean append = FALSE;
111 boolean printed = FALSE;
112 boolean verbose = FALSE;
113 boolean show_where_not = FALSE;
114 boolean warn_multiple = FALSE; /* Warn on multiple includes of same file */
118 #if !NeedVarargsPrototypes
125 #ifdef SIGNALRETURNSINT
134 fatalerr ("got signal %d\n", sig);
137 #if defined(USG) || (defined(i386) && defined(SYSV)) || defined(WIN32)
142 #ifndef _POSIX_SOURCE
143 #define sigaction sigvec
144 #define sa_handler sv_handler
145 #define sa_mask sv_mask
146 #define sa_flags sv_flags
148 struct sigaction sig_act;
155 register char **fp = filelist;
156 register char **incp = includedirs;
158 register struct inclist *ip;
159 char *makefile = NULL;
160 struct filepointer *filecontent;
161 struct symtab *psymp = predefs;
162 char *endmarker = NULL;
163 char *defincdir = NULL;
165 ProgramName = argv[0];
167 while (psymp->s_name)
169 define2(psymp->s_name, psymp->s_value, &maininclist);
172 if (argc == 2 && argv[1][0] == '@') {
178 char quotechar = '\0';
181 if ((afd = open(argv[1]+1, O_RDONLY)) < 0)
182 fatalerr("cannot open \"%s\"\n", argv[1]+1);
184 args = (char *)malloc(ast.st_size + 1);
185 if ((ast.st_size = read(afd, args, ast.st_size)) < 0)
186 fatalerr("failed to read %s\n", argv[1]+1);
187 args[ast.st_size] = '\0';
189 for (p = args; *p; p++) {
191 if (quotechar == '\\' ||
192 (*p == quotechar && p[-1] != '\\'))
205 if (p > args && p[-1])
212 nargv = (char **)malloc(nargc * sizeof(char *));
215 for (p = args; argc < nargc; p += strlen(p) + 1)
216 if (*p) nargv[argc++] = p;
219 for(argc--, argv++; argc; argc--, argv++) {
220 /* if looking for endmarker then check before parsing */
221 if (endmarker && strcmp (endmarker, *argv) == 0) {
226 /* treat +thing as an option for C++ */
227 if (endmarker && **argv == '+')
234 endmarker = &argv[0][2];
235 if (endmarker[0] == '\0') endmarker = "--";
238 if (argv[0][2] == '\0') {
242 for (p=argv[0] + 2; *p ; p++)
247 define(argv[0] + 2, &maininclist);
250 if (incp >= includedirs + MAXDIRS)
251 fatalerr("Too many -I flags.\n");
253 if (**(incp-1) == '\0') {
254 *(incp-1) = *(++argv);
259 defincdir = argv[0]+2;
261 /* do not use if endmarker processing */
263 if (endmarker) break;
267 if (endmarker) break;
268 if (argv[0][2] == '\0') {
271 width = atoi(argv[0]);
273 width = atoi(argv[0]+2);
276 if (endmarker) break;
277 if (argv[0][2] == '\0') {
282 objsuffix = argv[0]+2;
285 if (endmarker) break;
286 if (argv[0][2] == '\0') {
291 objprefix = argv[0]+2;
294 if (endmarker) break;
298 _debugmask = atoi(argv[0]+2);
302 if (endmarker) break;
304 if (*startat == '\0') {
309 fatalerr("-s flag's value should start %s\n",
313 if (endmarker) break;
314 makefile = argv[0]+2;
315 if (*makefile == '\0') {
316 makefile = *(++argv);
322 warn_multiple = TRUE;
325 /* Ignore -O, -g so we can just pass ${CFLAGS} to
332 if (endmarker) break;
333 /* fatalerr("unknown opt = %s\n", argv[0]); */
334 warning("ignoring option %s\n", argv[0]);
339 if (incp >= includedirs + MAXDIRS)
340 fatalerr("Too many -I flags.\n");
343 if (incp >= includedirs + MAXDIRS)
344 fatalerr("Too many -I flags.\n");
345 *incp++ = INCLUDEDIR;
347 if (incp >= includedirs + MAXDIRS)
348 fatalerr("Too many -I flags.\n");
349 *incp++ = POSTINCDIR;
351 } else if (*defincdir) {
352 if (incp >= includedirs + MAXDIRS)
353 fatalerr("Too many -I flags.\n");
357 redirect(startat, makefile);
363 /* should really reset SIGINT to SIG_IGN if it was. */
365 signal (SIGHUP, catch);
367 signal (SIGINT, catch);
369 signal (SIGQUIT, catch);
371 signal (SIGILL, catch);
373 signal (SIGBUS, catch);
375 signal (SIGSEGV, catch);
377 signal (SIGSYS, catch);
380 sig_act.sa_handler = catch;
382 sigemptyset(&sig_act.sa_mask);
383 sigaddset(&sig_act.sa_mask, SIGINT);
384 sigaddset(&sig_act.sa_mask, SIGQUIT);
386 sigaddset(&sig_act.sa_mask, SIGBUS);
388 sigaddset(&sig_act.sa_mask, SIGILL);
389 sigaddset(&sig_act.sa_mask, SIGSEGV);
390 sigaddset(&sig_act.sa_mask, SIGHUP);
391 sigaddset(&sig_act.sa_mask, SIGPIPE);
393 sigaddset(&sig_act.sa_mask, SIGSYS);
396 sig_act.sa_mask = ((1<<(SIGINT -1))
409 #endif /* _POSIX_SOURCE */
410 sig_act.sa_flags = 0;
411 sigaction(SIGHUP, &sig_act, (struct sigaction *)0);
412 sigaction(SIGINT, &sig_act, (struct sigaction *)0);
413 sigaction(SIGQUIT, &sig_act, (struct sigaction *)0);
414 sigaction(SIGILL, &sig_act, (struct sigaction *)0);
416 sigaction(SIGBUS, &sig_act, (struct sigaction *)0);
418 sigaction(SIGSEGV, &sig_act, (struct sigaction *)0);
420 sigaction(SIGSYS, &sig_act, (struct sigaction *)0);
425 * now peruse through the list of files.
427 for(fp=filelist; *fp; fp++) {
428 filecontent = getfile(*fp);
429 ip = newinclude(*fp, (char *)NULL);
431 find_includes(filecontent, ip, ip, 0, FALSE);
432 freefile(filecontent);
433 recursive_pr_include(ip, ip->i_file, base_name(*fp));
441 struct filepointer *getfile(file)
445 struct filepointer *content;
448 content = (struct filepointer *)malloc(sizeof(struct filepointer));
449 if ((fd = open(file, O_RDONLY)) < 0) {
450 warning("cannot open \"%s\"\n", file);
451 content->f_p = content->f_base = content->f_end = (char *)malloc(1);
452 *content->f_p = '\0';
456 content->f_base = (char *)malloc(st.st_size+1);
457 if (content->f_base == NULL)
458 fatalerr("cannot allocate mem\n");
459 if ((st.st_size = read(fd, content->f_base, st.st_size)) < 0)
460 fatalerr("failed to read %s\n", file);
462 content->f_len = st.st_size+1;
463 content->f_p = content->f_base;
464 content->f_end = content->f_base + st.st_size;
465 *content->f_end = '\0';
472 struct filepointer *fp;
481 register char *p = (char *)malloc(strlen(str) + 1);
488 register char *str, **list;
492 for (i=0; *list; i++, list++)
493 if (strcmp(str, *list) == 0)
499 * Get the next line. We only return lines beginning with '#' since that
500 * is all this program is ever interested in.
502 char *our_getline(filep)
503 register struct filepointer *filep;
505 register char *p, /* walking pointer */
506 *eof, /* end of file pointer */
507 *bol; /* beginning of line pointer */
508 register int lineno; /* line number */
513 return((char *)NULL);
514 lineno = filep->f_line;
516 for(bol = p--; ++p < eof; ) {
517 if (*p == '/' && *(p+1) == '*') { /* consume comments */
518 *p++ = ' ', *p++ = ' ';
520 if (*p == '*' && *(p+1) == '/') {
521 *p++ = ' ', *p = ' ';
531 else if (*p == '/' && *(p+1) == '/') { /* consume comments */
532 *p++ = ' ', *p++ = ' ';
533 while (*p && *p != '\n')
539 else if (*p == '\\') {
540 if (*(p+1) == '\n') {
546 else if (*p == '\n') {
552 /* punt lines with just # (yacc generated) */
554 *cp && (*cp == ' ' || *cp == '\t'); cp++);
564 filep->f_line = lineno;
569 * Strip the file name down to what we want to see in the Makefile.
570 * It will have objprefix and objsuffix around it.
572 char *base_name(file)
578 for(p=file+strlen(file); p>file && *p != '.'; p--) ;
585 #if defined(USG) && !defined(CRAY) && !defined(SVR4)
586 int rename (from, to)
590 if (link (from, to) == 0) {
600 redirect(line, makefile)
606 char backup[ BUFSIZ ],
608 boolean found = FALSE;
612 * if makefile is "-" then let it pour onto stdout.
614 if (makefile && *makefile == '-' && *(makefile+1) == '\0') {
620 * use a default makefile is not specified.
623 if (stat("Makefile", &st) == 0)
624 makefile = "Makefile";
625 else if (stat("makefile", &st) == 0)
626 makefile = "makefile";
628 fatalerr("[mM]akefile is not present\n");
632 if ((fdin = fopen(makefile, "r")) == NULL)
633 fatalerr("cannot open \"%s\"\n", makefile);
634 sprintf(backup, "%s.bak", makefile);
639 if (rename(makefile, backup) < 0)
640 fatalerr("cannot rename %s to %s\n", makefile, backup);
642 if ((fdin = fopen(backup, "r")) == NULL)
643 fatalerr("cannot open \"%s\"\n", backup);
645 if ((fdout = freopen(makefile, "w", stdout)) == NULL)
646 fatalerr("cannot open \"%s\"\n", backup);
648 while (!found && fgets(buf, BUFSIZ, fdin)) {
649 if (*buf == '#' && strncmp(line, buf, len) == 0)
655 warning("Adding new delimiting line \"%s\" and dependencies...\n",
657 puts(line); /* same as fputs(fdout); but with newline */
659 while (fgets(buf, BUFSIZ, fdin)) {
664 #if defined(USGISH) || defined(_SEQUENT_)
665 chmod(makefile, st.st_mode);
667 fchmod(fileno(fdout), st.st_mode);
672 #if NeedVarargsPrototypes
673 fatalerr(char *msg, ...)
676 fatalerr(msg,x1,x2,x3,x4,x5,x6,x7,x8,x9)
680 #if NeedVarargsPrototypes
683 fprintf(stderr, "%s: error: ", ProgramName);
684 #if NeedVarargsPrototypes
686 vfprintf(stderr, msg, args);
689 fprintf(stderr, msg,x1,x2,x3,x4,x5,x6,x7,x8,x9);
695 #if NeedVarargsPrototypes
696 warning(char *msg, ...)
699 warning(msg,x1,x2,x3,x4,x5,x6,x7,x8,x9)
703 #if NeedVarargsPrototypes
706 fprintf(stderr, "%s: warning: ", ProgramName);
707 #if NeedVarargsPrototypes
709 vfprintf(stderr, msg, args);
712 fprintf(stderr, msg,x1,x2,x3,x4,x5,x6,x7,x8,x9);
717 #if NeedVarargsPrototypes
718 warning1(char *msg, ...)
721 warning1(msg,x1,x2,x3,x4,x5,x6,x7,x8,x9)
725 #if NeedVarargsPrototypes
728 vfprintf(stderr, msg, args);
731 fprintf(stderr, msg,x1,x2,x3,x4,x5,x6,x7,x8,x9);