123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761 |
- /*
- * CDE - Common Desktop Environment
- *
- * Copyright (c) 1993-2012, The Open Group. All rights reserved.
- *
- * These libraries and programs are free software; you can
- * redistribute them and/or modify them under the terms of the GNU
- * Lesser General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * These libraries and programs are distributed in the hope that
- * they will be useful, but WITHOUT ANY WARRANTY; without even the
- * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU Lesser General Public License for more
- * details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with these librararies and programs; if not, write
- * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
- * Floor, Boston, MA 02110-1301 USA
- */
- /* $TOG: main.c /main/86 1998/03/25 08:17:50 kaleb $ */
- /*
- Copyright (c) 1993, 1994, 1998 The Open Group
- All Rights Reserved.
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- Except as contained in this notice, the name of The Open Group shall not be
- used in advertising or otherwise to promote the sale, use or other dealings
- in this Software without prior written authorization from The Open Group.
- */
- #include "def.h"
- #ifdef hpux
- #define sigvec sigvector
- #endif /* hpux */
- #ifdef X_POSIX_C_SOURCE
- #define _POSIX_C_SOURCE X_POSIX_C_SOURCE
- #include <signal.h>
- #undef _POSIX_C_SOURCE
- #else
- #if defined(X_NOT_POSIX) || defined(_POSIX_SOURCE)
- #include <signal.h>
- #else
- #define _POSIX_SOURCE
- #include <signal.h>
- #undef _POSIX_SOURCE
- #endif
- #endif
- #if NeedVarargsPrototypes
- #include <stdarg.h>
- #endif
- #ifdef DEBUG
- int _debugmask;
- #endif
- char *ProgramName;
- char *directives[] = {
- "if",
- "ifdef",
- "ifndef",
- "else",
- "endif",
- "define",
- "undef",
- "include",
- "line",
- "pragma",
- "error",
- "ident",
- "sccs",
- "elif",
- "eject",
- "warning",
- NULL
- };
- #define MAKEDEPEND
- #include "imakemdep.h" /* from config sources */
- #undef MAKEDEPEND
- struct inclist inclist[ MAXFILES ],
- *inclistp = inclist,
- maininclist;
- char *filelist[ MAXFILES ];
- char *includedirs[ MAXDIRS + 1 ];
- char *notdotdot[ MAXDIRS ];
- char *objprefix = "";
- char *objsuffix = OBJSUFFIX;
- char *startat = "# DO NOT DELETE";
- int width = 78;
- boolean append = FALSE;
- boolean printed = FALSE;
- boolean verbose = FALSE;
- boolean show_where_not = FALSE;
- boolean warn_multiple = FALSE; /* Warn on multiple includes of same file */
- void freefile();
- void redirect();
- #if !NeedVarargsPrototypes
- void fatalerr();
- void warning();
- void warning1();
- #endif
- static
- #ifdef SIGNALRETURNSINT
- int
- #else
- void
- #endif
- catch (sig)
- int sig;
- {
- fflush (stdout);
- fatalerr ("got signal %d\n", sig);
- }
- #if defined(USG) || (defined(i386) && defined(SYSV)) || defined(WIN32)
- #define USGISH
- #endif
- #ifndef USGISH
- #ifndef _POSIX_SOURCE
- #define sigaction sigvec
- #define sa_handler sv_handler
- #define sa_mask sv_mask
- #define sa_flags sv_flags
- #endif
- struct sigaction sig_act;
- #endif /* USGISH */
- int main(argc, argv)
- int argc;
- char **argv;
- {
- register char **fp = filelist;
- register char **incp = includedirs;
- register char *p;
- register struct inclist *ip;
- char *makefile = NULL;
- struct filepointer *filecontent;
- struct symtab *psymp = predefs;
- char *endmarker = NULL;
- char *defincdir = NULL;
- ProgramName = argv[0];
- while (psymp->s_name)
- {
- define2(psymp->s_name, psymp->s_value, &maininclist);
- psymp++;
- }
- if (argc == 2 && argv[1][0] == '@') {
- struct stat ast;
- int afd;
- char *args;
- char **nargv;
- int nargc;
- char quotechar = '\0';
- nargc = 1;
- if ((afd = open(argv[1]+1, O_RDONLY)) < 0)
- fatalerr("cannot open \"%s\"\n", argv[1]+1);
- fstat(afd, &ast);
- args = (char *)malloc(ast.st_size + 1);
- if ((ast.st_size = read(afd, args, ast.st_size)) < 0)
- fatalerr("failed to read %s\n", argv[1]+1);
- args[ast.st_size] = '\0';
- close(afd);
- for (p = args; *p; p++) {
- if (quotechar) {
- if (quotechar == '\\' ||
- (*p == quotechar && p[-1] != '\\'))
- quotechar = '\0';
- continue;
- }
- switch (*p) {
- case '\\':
- case '"':
- case '\'':
- quotechar = *p;
- break;
- case ' ':
- case '\n':
- *p = '\0';
- if (p > args && p[-1])
- nargc++;
- break;
- }
- }
- if (p[-1])
- nargc++;
- nargv = (char **)malloc(nargc * sizeof(char *));
- nargv[0] = argv[0];
- argc = 1;
- for (p = args; argc < nargc; p += strlen(p) + 1)
- if (*p) nargv[argc++] = p;
- argv = nargv;
- }
- for(argc--, argv++; argc; argc--, argv++) {
- /* if looking for endmarker then check before parsing */
- if (endmarker && strcmp (endmarker, *argv) == 0) {
- endmarker = NULL;
- continue;
- }
- if (**argv != '-') {
- /* treat +thing as an option for C++ */
- if (endmarker && **argv == '+')
- continue;
- *fp++ = argv[0];
- continue;
- }
- switch(argv[0][1]) {
- case '-':
- endmarker = &argv[0][2];
- if (endmarker[0] == '\0') endmarker = "--";
- break;
- case 'D':
- if (argv[0][2] == '\0') {
- argv++;
- argc--;
- }
- for (p=argv[0] + 2; *p ; p++)
- if (*p == '=') {
- *p = ' ';
- break;
- }
- define(argv[0] + 2, &maininclist);
- break;
- case 'I':
- if (incp >= includedirs + MAXDIRS)
- fatalerr("Too many -I flags.\n");
- *incp++ = argv[0]+2;
- if (**(incp-1) == '\0') {
- *(incp-1) = *(++argv);
- argc--;
- }
- break;
- case 'i':
- if (strncmp(argv[0]+2, "system", 6) == 0) {
- if (incp >= includedirs + MAXDIRS)
- fatalerr("Too many -I flags.\n");
- *incp++ = argv[0]+8;
- if (**(incp-1) == '\0') {
- *(incp-1) = *(++argv);
- argc--;
- }
- }
- break;
- case 'Y':
- defincdir = argv[0]+2;
- break;
- /* do not use if endmarker processing */
- case 'a':
- if (endmarker) break;
- append = TRUE;
- break;
- case 'w':
- if (endmarker) break;
- if (argv[0][2] == '\0') {
- argv++;
- argc--;
- width = atoi(argv[0]);
- } else
- width = atoi(argv[0]+2);
- break;
- case 'o':
- if (endmarker) break;
- if (argv[0][2] == '\0') {
- argv++;
- argc--;
- objsuffix = argv[0];
- } else
- objsuffix = argv[0]+2;
- break;
- case 'p':
- if (endmarker) break;
- if (argv[0][2] == '\0') {
- argv++;
- argc--;
- objprefix = argv[0];
- } else
- objprefix = argv[0]+2;
- break;
- case 'v':
- if (endmarker) break;
- verbose = TRUE;
- #ifdef DEBUG
- if (argv[0][2])
- _debugmask = atoi(argv[0]+2);
- #endif
- break;
- case 's':
- if (endmarker) break;
- startat = argv[0]+2;
- if (*startat == '\0') {
- startat = *(++argv);
- argc--;
- }
- if (*startat != '#')
- fatalerr("-s flag's value should start %s\n",
- "with '#'.");
- break;
- case 'f':
- if (endmarker) break;
- makefile = argv[0]+2;
- if (*makefile == '\0') {
- makefile = *(++argv);
- argc--;
- }
- break;
- case 'm':
- warn_multiple = TRUE;
- break;
-
- /* Ignore -O, -g so we can just pass ${CFLAGS} to
- makedepend
- */
- case 'O':
- case 'g':
- break;
- default:
- if (endmarker) break;
- /* fatalerr("unknown opt = %s\n", argv[0]); */
- warning("ignoring option %s\n", argv[0]);
- }
- }
- if (!defincdir) {
- #ifdef PREINCDIR
- if (incp >= includedirs + MAXDIRS)
- fatalerr("Too many -I flags.\n");
- *incp++ = PREINCDIR;
- #endif
- if (incp >= includedirs + MAXDIRS)
- fatalerr("Too many -I flags.\n");
- *incp++ = INCLUDEDIR;
- #ifdef POSTINCDIR
- if (incp >= includedirs + MAXDIRS)
- fatalerr("Too many -I flags.\n");
- *incp++ = POSTINCDIR;
- #endif
- } else if (*defincdir) {
- if (incp >= includedirs + MAXDIRS)
- fatalerr("Too many -I flags.\n");
- *incp++ = defincdir;
- }
- redirect(startat, makefile);
- /*
- * catch signals.
- */
- #ifdef USGISH
- /* should really reset SIGINT to SIG_IGN if it was. */
- #ifdef SIGHUP
- signal (SIGHUP, catch);
- #endif
- signal (SIGINT, catch);
- #ifdef SIGQUIT
- signal (SIGQUIT, catch);
- #endif
- signal (SIGILL, catch);
- #ifdef SIGBUS
- signal (SIGBUS, catch);
- #endif
- signal (SIGSEGV, catch);
- #ifdef SIGSYS
- signal (SIGSYS, catch);
- #endif
- #else
- sig_act.sa_handler = catch;
- #ifdef _POSIX_SOURCE
- sigemptyset(&sig_act.sa_mask);
- sigaddset(&sig_act.sa_mask, SIGINT);
- sigaddset(&sig_act.sa_mask, SIGQUIT);
- #ifdef SIGBUS
- sigaddset(&sig_act.sa_mask, SIGBUS);
- #endif
- sigaddset(&sig_act.sa_mask, SIGILL);
- sigaddset(&sig_act.sa_mask, SIGSEGV);
- sigaddset(&sig_act.sa_mask, SIGHUP);
- sigaddset(&sig_act.sa_mask, SIGPIPE);
- #ifdef SIGSYS
- sigaddset(&sig_act.sa_mask, SIGSYS);
- #endif
- #else
- sig_act.sa_mask = ((1<<(SIGINT -1))
- |(1<<(SIGQUIT-1))
- #ifdef SIGBUS
- |(1<<(SIGBUS-1))
- #endif
- |(1<<(SIGILL-1))
- |(1<<(SIGSEGV-1))
- |(1<<(SIGHUP-1))
- |(1<<(SIGPIPE-1))
- #ifdef SIGSYS
- |(1<<(SIGSYS-1))
- #endif
- );
- #endif /* _POSIX_SOURCE */
- sig_act.sa_flags = 0;
- sigaction(SIGHUP, &sig_act, (struct sigaction *)0);
- sigaction(SIGINT, &sig_act, (struct sigaction *)0);
- sigaction(SIGQUIT, &sig_act, (struct sigaction *)0);
- sigaction(SIGILL, &sig_act, (struct sigaction *)0);
- #ifdef SIGBUS
- sigaction(SIGBUS, &sig_act, (struct sigaction *)0);
- #endif
- sigaction(SIGSEGV, &sig_act, (struct sigaction *)0);
- #ifdef SIGSYS
- sigaction(SIGSYS, &sig_act, (struct sigaction *)0);
- #endif
- #endif /* USGISH */
- /*
- * now peruse through the list of files.
- */
- for(fp=filelist; *fp; fp++) {
- filecontent = getfile(*fp);
- ip = newinclude(*fp, (char *)NULL);
- find_includes(filecontent, ip, ip, 0, FALSE);
- freefile(filecontent);
- recursive_pr_include(ip, ip->i_file, base_name(*fp));
- inc_clean();
- }
- if (printed)
- printf("\n");
- exit(0);
- }
- struct filepointer *getfile(file)
- char *file;
- {
- register int fd;
- struct filepointer *content;
- struct stat st;
- content = (struct filepointer *)malloc(sizeof(struct filepointer));
- if ((fd = open(file, O_RDONLY)) < 0) {
- warning("cannot open \"%s\"\n", file);
- content->f_p = content->f_base = content->f_end = (char *)malloc(1);
- *content->f_p = '\0';
- return(content);
- }
- fstat(fd, &st);
- content->f_base = (char *)malloc(st.st_size+1);
- if (content->f_base == NULL)
- fatalerr("cannot allocate mem\n");
- if ((st.st_size = read(fd, content->f_base, st.st_size)) < 0)
- fatalerr("failed to read %s\n", file);
- close(fd);
- content->f_len = st.st_size+1;
- content->f_p = content->f_base;
- content->f_end = content->f_base + st.st_size;
- *content->f_end = '\0';
- content->f_line = 0;
- return(content);
- }
- void
- freefile(fp)
- struct filepointer *fp;
- {
- free(fp->f_base);
- free(fp);
- }
- char *copy(str)
- register char *str;
- {
- register char *p = (char *)malloc(strlen(str) + 1);
- strncpy(p, str, strlen(str) + 1);
- return(p);
- }
- int match(str, list)
- register char *str, **list;
- {
- register int i;
- for (i=0; *list; i++, list++)
- if (strcmp(str, *list) == 0)
- return(i);
- return(-1);
- }
- /*
- * Get the next line. We only return lines beginning with '#' since that
- * is all this program is ever interested in.
- */
- char *our_getline(filep)
- register struct filepointer *filep;
- {
- register char *p, /* walking pointer */
- *eof, /* end of file pointer */
- *bol; /* beginning of line pointer */
- register int lineno; /* line number */
- p = filep->f_p;
- eof = filep->f_end;
- if (p >= eof)
- return((char *)NULL);
- lineno = filep->f_line;
- for(bol = p--; ++p < eof; ) {
- if (*p == '/' && (p+1) < eof && *(p+1) == '*') {
- /* consume C comments */
- *p++ = ' ', *p++ = ' ';
- while (*p) {
- if (*p == '*' && *(p+1) == '/') {
- *p++ = ' ', *p = ' ';
- break;
- }
- else if (*p == '\n')
- lineno++;
- *p++ = ' ';
- }
- continue;
- }
- else if (*p == '/' && (p+1) < eof && *(p+1) == '/') {
- /* consume C++ comments */
- *p++ = ' ';
- *p++ = ' ';
- while (p < eof && *p) {
- if (*p == '\\' && (p+1) < eof &&
- *(p+1) == '\n') {
- *(p++) = ' ';
- lineno++;
- }
- else if (*p == '?' && (p+3) < eof &&
- *(p+1) == '?' &&
- *(p+2) == '/' &&
- *(p+3) == '\n') {
- *(p++) = ' ';
- *(p++) = ' ';
- *(p++) = ' ';
- lineno++;
- }
- else if (*p == '\n')
- break; /* to process end of line */
- *(p++) = ' ';
- }
- --p;
- }
- else if (*p == '\\') {
- if (*(p+1) == '\n') {
- *p = ' ';
- *(p+1) = ' ';
- lineno++;
- }
- }
- else if (*p == '\n') {
- lineno++;
- if (*bol == '#') {
- register char *cp;
- *p++ = '\0';
- /* punt lines with just # (yacc generated) */
- for (cp = bol+1;
- *cp && (*cp == ' ' || *cp == '\t'); cp++);
- if (*cp) goto done;
- }
- bol = p+1;
- }
- }
- if (*bol != '#')
- bol = NULL;
- done:
- filep->f_p = p;
- filep->f_line = lineno;
- return(bol);
- }
- /*
- * Strip the file name down to what we want to see in the Makefile.
- * It will have objprefix and objsuffix around it.
- */
- char *base_name(file)
- register char *file;
- {
- register char *p;
- file = copy(file);
- for(p=file+strlen(file); p>file && *p != '.'; p--) ;
- if (*p == '.')
- *p = '\0';
- return(file);
- }
- #if defined(USG) && !defined(CRAY) && !defined(SVR4)
- int rename (from, to)
- char *from, *to;
- {
- (void) unlink (to);
- if (link (from, to) == 0) {
- unlink (from);
- return 0;
- } else {
- return -1;
- }
- }
- #endif /* USGISH */
- void
- redirect(line, makefile)
- char *line,
- *makefile;
- {
- struct stat st;
- FILE *fdin, *fdout;
- char backup[ BUFSIZ ],
- buf[ BUFSIZ ];
- boolean found = FALSE;
- int len;
- /*
- * if makefile is "-" then let it pour onto stdout.
- */
- if (makefile && *makefile == '-' && *(makefile+1) == '\0') {
- puts(line);
- return;
- }
- /*
- * use a default makefile is not specified.
- */
- if (!makefile) {
- if (stat("Makefile", &st) == 0)
- makefile = "Makefile";
- else if (stat("makefile", &st) == 0)
- makefile = "makefile";
- else
- fatalerr("[mM]akefile is not present\n");
- }
- else
- stat(makefile, &st);
- if ((fdin = fopen(makefile, "r")) == NULL)
- fatalerr("cannot open \"%s\"\n", makefile);
- snprintf(backup, BUFSIZ, "%s.bak", makefile);
- unlink(backup);
- #ifdef WIN32
- fclose(fdin);
- #endif
- if (rename(makefile, backup) < 0)
- fatalerr("cannot rename %s to %s\n", makefile, backup);
- #ifdef WIN32
- if ((fdin = fopen(backup, "r")) == NULL)
- fatalerr("cannot open \"%s\"\n", backup);
- #endif
- if ((fdout = freopen(makefile, "w", stdout)) == NULL)
- fatalerr("cannot open \"%s\"\n", backup);
- len = strlen(line);
- while (!found && fgets(buf, BUFSIZ, fdin)) {
- if (*buf == '#' && strncmp(line, buf, len) == 0)
- found = TRUE;
- fputs(buf, fdout);
- }
- if (!found) {
- if (verbose)
- warning("Adding new delimiting line \"%s\" and dependencies...\n",
- line);
- puts(line); /* same as fputs(fdout); but with newline */
- } else if (append) {
- while (fgets(buf, BUFSIZ, fdin)) {
- fputs(buf, fdout);
- }
- }
- fflush(fdout);
- #if defined(USGISH) || defined(_SEQUENT_)
- chmod(makefile, st.st_mode);
- #else
- fchmod(fileno(fdout), st.st_mode);
- #endif /* USGISH */
- }
- void
- #if NeedVarargsPrototypes
- fatalerr(char *msg, ...)
- #else
- /*VARARGS*/
- fatalerr(msg,x1,x2,x3,x4,x5,x6,x7,x8,x9)
- char *msg;
- #endif
- {
- #if NeedVarargsPrototypes
- va_list args;
- #endif
- fprintf(stderr, "%s: error: ", ProgramName);
- #if NeedVarargsPrototypes
- va_start(args, msg);
- vfprintf(stderr, msg, args);
- va_end(args);
- #else
- fprintf(stderr, msg,x1,x2,x3,x4,x5,x6,x7,x8,x9);
- #endif
- exit (1);
- }
- void
- #if NeedVarargsPrototypes
- warning(char *msg, ...)
- #else
- /*VARARGS0*/
- warning(msg,x1,x2,x3,x4,x5,x6,x7,x8,x9)
- char *msg;
- #endif
- {
- #if NeedVarargsPrototypes
- va_list args;
- #endif
- fprintf(stderr, "%s: warning: ", ProgramName);
- #if NeedVarargsPrototypes
- va_start(args, msg);
- vfprintf(stderr, msg, args);
- va_end(args);
- #else
- fprintf(stderr, msg,x1,x2,x3,x4,x5,x6,x7,x8,x9);
- #endif
- }
- void
- #if NeedVarargsPrototypes
- warning1(char *msg, ...)
- #else
- /*VARARGS0*/
- warning1(msg,x1,x2,x3,x4,x5,x6,x7,x8,x9)
- char *msg;
- #endif
- {
- #if NeedVarargsPrototypes
- va_list args;
- va_start(args, msg);
- vfprintf(stderr, msg, args);
- va_end(args);
- #else
- fprintf(stderr, msg,x1,x2,x3,x4,x5,x6,x7,x8,x9);
- #endif
- }
|