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 if (strncmp(argv[0]+2, "system", 6) == 0) {
260 if (incp >= includedirs + MAXDIRS)
261 fatalerr("Too many -I flags.\n");
263 if (**(incp-1) == '\0') {
264 *(incp-1) = *(++argv);
270 defincdir = argv[0]+2;
272 /* do not use if endmarker processing */
274 if (endmarker) break;
278 if (endmarker) break;
279 if (argv[0][2] == '\0') {
282 width = atoi(argv[0]);
284 width = atoi(argv[0]+2);
287 if (endmarker) break;
288 if (argv[0][2] == '\0') {
293 objsuffix = argv[0]+2;
296 if (endmarker) break;
297 if (argv[0][2] == '\0') {
302 objprefix = argv[0]+2;
305 if (endmarker) break;
309 _debugmask = atoi(argv[0]+2);
313 if (endmarker) break;
315 if (*startat == '\0') {
320 fatalerr("-s flag's value should start %s\n",
324 if (endmarker) break;
325 makefile = argv[0]+2;
326 if (*makefile == '\0') {
327 makefile = *(++argv);
333 warn_multiple = TRUE;
336 /* Ignore -O, -g so we can just pass ${CFLAGS} to
343 if (endmarker) break;
344 /* fatalerr("unknown opt = %s\n", argv[0]); */
345 warning("ignoring option %s\n", argv[0]);
350 if (incp >= includedirs + MAXDIRS)
351 fatalerr("Too many -I flags.\n");
354 if (incp >= includedirs + MAXDIRS)
355 fatalerr("Too many -I flags.\n");
356 *incp++ = INCLUDEDIR;
358 if (incp >= includedirs + MAXDIRS)
359 fatalerr("Too many -I flags.\n");
360 *incp++ = POSTINCDIR;
362 } else if (*defincdir) {
363 if (incp >= includedirs + MAXDIRS)
364 fatalerr("Too many -I flags.\n");
368 redirect(startat, makefile);
374 /* should really reset SIGINT to SIG_IGN if it was. */
376 signal (SIGHUP, catch);
378 signal (SIGINT, catch);
380 signal (SIGQUIT, catch);
382 signal (SIGILL, catch);
384 signal (SIGBUS, catch);
386 signal (SIGSEGV, catch);
388 signal (SIGSYS, catch);
391 sig_act.sa_handler = catch;
393 sigemptyset(&sig_act.sa_mask);
394 sigaddset(&sig_act.sa_mask, SIGINT);
395 sigaddset(&sig_act.sa_mask, SIGQUIT);
397 sigaddset(&sig_act.sa_mask, SIGBUS);
399 sigaddset(&sig_act.sa_mask, SIGILL);
400 sigaddset(&sig_act.sa_mask, SIGSEGV);
401 sigaddset(&sig_act.sa_mask, SIGHUP);
402 sigaddset(&sig_act.sa_mask, SIGPIPE);
404 sigaddset(&sig_act.sa_mask, SIGSYS);
407 sig_act.sa_mask = ((1<<(SIGINT -1))
420 #endif /* _POSIX_SOURCE */
421 sig_act.sa_flags = 0;
422 sigaction(SIGHUP, &sig_act, (struct sigaction *)0);
423 sigaction(SIGINT, &sig_act, (struct sigaction *)0);
424 sigaction(SIGQUIT, &sig_act, (struct sigaction *)0);
425 sigaction(SIGILL, &sig_act, (struct sigaction *)0);
427 sigaction(SIGBUS, &sig_act, (struct sigaction *)0);
429 sigaction(SIGSEGV, &sig_act, (struct sigaction *)0);
431 sigaction(SIGSYS, &sig_act, (struct sigaction *)0);
436 * now peruse through the list of files.
438 for(fp=filelist; *fp; fp++) {
439 filecontent = getfile(*fp);
440 ip = newinclude(*fp, (char *)NULL);
442 find_includes(filecontent, ip, ip, 0, FALSE);
443 freefile(filecontent);
444 recursive_pr_include(ip, ip->i_file, base_name(*fp));
452 struct filepointer *getfile(file)
456 struct filepointer *content;
459 content = (struct filepointer *)malloc(sizeof(struct filepointer));
460 if ((fd = open(file, O_RDONLY)) < 0) {
461 warning("cannot open \"%s\"\n", file);
462 content->f_p = content->f_base = content->f_end = (char *)malloc(1);
463 *content->f_p = '\0';
467 content->f_base = (char *)malloc(st.st_size+1);
468 if (content->f_base == NULL)
469 fatalerr("cannot allocate mem\n");
470 if ((st.st_size = read(fd, content->f_base, st.st_size)) < 0)
471 fatalerr("failed to read %s\n", file);
473 content->f_len = st.st_size+1;
474 content->f_p = content->f_base;
475 content->f_end = content->f_base + st.st_size;
476 *content->f_end = '\0';
483 struct filepointer *fp;
492 register char *p = (char *)malloc(strlen(str) + 1);
494 strncpy(p, str, strlen(str) + 1);
499 register char *str, **list;
503 for (i=0; *list; i++, list++)
504 if (strcmp(str, *list) == 0)
510 * Get the next line. We only return lines beginning with '#' since that
511 * is all this program is ever interested in.
513 char *our_getline(filep)
514 register struct filepointer *filep;
516 register char *p, /* walking pointer */
517 *eof, /* end of file pointer */
518 *bol; /* beginning of line pointer */
519 register int lineno; /* line number */
524 return((char *)NULL);
525 lineno = filep->f_line;
527 for(bol = p--; ++p < eof; ) {
528 if (*p == '/' && (p+1) < eof && *(p+1) == '*') {
529 /* consume C comments */
530 *p++ = ' ', *p++ = ' ';
532 if (*p == '*' && *(p+1) == '/') {
533 *p++ = ' ', *p = ' ';
542 else if (*p == '/' && (p+1) < eof && *(p+1) == '/') {
543 /* consume C++ comments */
546 while (p < eof && *p) {
547 if (*p == '\\' && (p+1) < eof &&
552 else if (*p == '?' && (p+3) < eof &&
562 break; /* to process end of line */
567 else if (*p == '\\') {
568 if (*(p+1) == '\n') {
574 else if (*p == '\n') {
580 /* punt lines with just # (yacc generated) */
582 *cp && (*cp == ' ' || *cp == '\t'); cp++);
592 filep->f_line = lineno;
597 * Strip the file name down to what we want to see in the Makefile.
598 * It will have objprefix and objsuffix around it.
600 char *base_name(file)
606 for(p=file+strlen(file); p>file && *p != '.'; p--) ;
613 #if defined(USG) && !defined(CRAY) && !defined(SVR4)
614 int rename (from, to)
618 if (link (from, to) == 0) {
628 redirect(line, makefile)
634 char backup[ BUFSIZ ],
636 boolean found = FALSE;
640 * if makefile is "-" then let it pour onto stdout.
642 if (makefile && *makefile == '-' && *(makefile+1) == '\0') {
648 * use a default makefile is not specified.
651 if (stat("Makefile", &st) == 0)
652 makefile = "Makefile";
653 else if (stat("makefile", &st) == 0)
654 makefile = "makefile";
656 fatalerr("[mM]akefile is not present\n");
660 if ((fdin = fopen(makefile, "r")) == NULL)
661 fatalerr("cannot open \"%s\"\n", makefile);
662 snprintf(backup, BUFSIZ, "%s.bak", makefile);
667 if (rename(makefile, backup) < 0)
668 fatalerr("cannot rename %s to %s\n", makefile, backup);
670 if ((fdin = fopen(backup, "r")) == NULL)
671 fatalerr("cannot open \"%s\"\n", backup);
673 if ((fdout = freopen(makefile, "w", stdout)) == NULL)
674 fatalerr("cannot open \"%s\"\n", backup);
676 while (!found && fgets(buf, BUFSIZ, fdin)) {
677 if (*buf == '#' && strncmp(line, buf, len) == 0)
683 warning("Adding new delimiting line \"%s\" and dependencies...\n",
685 puts(line); /* same as fputs(fdout); but with newline */
687 while (fgets(buf, BUFSIZ, fdin)) {
692 #if defined(USGISH) || defined(_SEQUENT_)
693 chmod(makefile, st.st_mode);
695 fchmod(fileno(fdout), st.st_mode);
700 #if NeedVarargsPrototypes
701 fatalerr(char *msg, ...)
704 fatalerr(msg,x1,x2,x3,x4,x5,x6,x7,x8,x9)
708 #if NeedVarargsPrototypes
711 fprintf(stderr, "%s: error: ", ProgramName);
712 #if NeedVarargsPrototypes
714 vfprintf(stderr, msg, args);
717 fprintf(stderr, msg,x1,x2,x3,x4,x5,x6,x7,x8,x9);
723 #if NeedVarargsPrototypes
724 warning(char *msg, ...)
727 warning(msg,x1,x2,x3,x4,x5,x6,x7,x8,x9)
731 #if NeedVarargsPrototypes
734 fprintf(stderr, "%s: warning: ", ProgramName);
735 #if NeedVarargsPrototypes
737 vfprintf(stderr, msg, args);
740 fprintf(stderr, msg,x1,x2,x3,x4,x5,x6,x7,x8,x9);
745 #if NeedVarargsPrototypes
746 warning1(char *msg, ...)
749 warning1(msg,x1,x2,x3,x4,x5,x6,x7,x8,x9)
753 #if NeedVarargsPrototypes
756 vfprintf(stderr, msg, args);
759 fprintf(stderr, msg,x1,x2,x3,x4,x5,x6,x7,x8,x9);