xref: /trunk/main/soltools/mkdepend/main.c (revision e932034bf45507ae970bfd7165fb7b2a983ba40d)
1cdf0e10cSrcweir /* $XConsortium: main.c,v 1.84 94/11/30 16:10:44 kaleb Exp $ */
2cdf0e10cSrcweir /* $XFree86: xc/config/makedepend/main.c,v 3.4 1995/07/15 14:53:49 dawes Exp $ */
3cdf0e10cSrcweir /*
4cdf0e10cSrcweir 
5cdf0e10cSrcweir Copyright (c) 1993, 1994  X Consortium
6cdf0e10cSrcweir 
7cdf0e10cSrcweir Permission is hereby granted, free of charge, to any person obtaining a copy
8cdf0e10cSrcweir of this software and associated documentation files (the "Software"), to deal
9cdf0e10cSrcweir in the Software without restriction, including without limitation the rights
10cdf0e10cSrcweir to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11cdf0e10cSrcweir copies of the Software, and to permit persons to whom the Software is
12cdf0e10cSrcweir furnished to do so, subject to the following conditions:
13cdf0e10cSrcweir 
14cdf0e10cSrcweir The above copyright notice and this permission notice shall be included in
15cdf0e10cSrcweir all copies or substantial portions of the Software.
16cdf0e10cSrcweir 
17cdf0e10cSrcweir THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18cdf0e10cSrcweir IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19cdf0e10cSrcweir FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
20cdf0e10cSrcweir X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21cdf0e10cSrcweir AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22cdf0e10cSrcweir CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23cdf0e10cSrcweir 
24cdf0e10cSrcweir Except as contained in this notice, the name of the X Consortium shall not be
25cdf0e10cSrcweir used in advertising or otherwise to promote the sale, use or other dealings
26cdf0e10cSrcweir in this Software without prior written authorization from the X Consortium.
27cdf0e10cSrcweir 
28cdf0e10cSrcweir */
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #if defined(FREEBSD) || defined(MACOSX)
31cdf0e10cSrcweir #include <sys/types.h>
32cdf0e10cSrcweir #include <sys/stat.h>
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #ifdef _MSC_VER     /* Define ssize_t */
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #if !defined(_W64)
38cdf0e10cSrcweir #if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300
39cdf0e10cSrcweir #define _W64 __w64
40cdf0e10cSrcweir #else
41cdf0e10cSrcweir #define _W64
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir #endif
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #ifdef  _WIN64
46cdf0e10cSrcweir typedef __int64    ssize_t;
47cdf0e10cSrcweir #else
48cdf0e10cSrcweir typedef _W64 int   ssize_t;
49cdf0e10cSrcweir #endif
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #endif
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #include "def.h"
54cdf0e10cSrcweir #include <string.h>
55cdf0e10cSrcweir #ifdef hpux
56cdf0e10cSrcweir #define sigvec sigvector
57cdf0e10cSrcweir #endif /* hpux */
58cdf0e10cSrcweir 
59cdf0e10cSrcweir #ifdef X_POSIX_C_SOURCE
60cdf0e10cSrcweir #define _POSIX_C_SOURCE X_POSIX_C_SOURCE
61cdf0e10cSrcweir #include <signal.h>
62cdf0e10cSrcweir #undef _POSIX_C_SOURCE
63cdf0e10cSrcweir #else
64cdf0e10cSrcweir #if defined(X_NOT_POSIX) || defined(_POSIX_SOURCE)
65cdf0e10cSrcweir #include <signal.h>
66cdf0e10cSrcweir #else
67cdf0e10cSrcweir #define _POSIX_SOURCE
68cdf0e10cSrcweir #include <signal.h>
69cdf0e10cSrcweir #undef _POSIX_SOURCE
70cdf0e10cSrcweir #endif
71cdf0e10cSrcweir #endif
72cdf0e10cSrcweir 
73cdf0e10cSrcweir #include <stdarg.h>
74cdf0e10cSrcweir 
75cdf0e10cSrcweir #ifdef MINIX
76cdf0e10cSrcweir #define USE_CHMOD   1
77cdf0e10cSrcweir #endif
78cdf0e10cSrcweir 
79cdf0e10cSrcweir #ifdef DEBUG
80cdf0e10cSrcweir int _debugmask;
81cdf0e10cSrcweir #endif
82cdf0e10cSrcweir 
83cdf0e10cSrcweir char *ProgramName;
84cdf0e10cSrcweir 
85cdf0e10cSrcweir #define OBJSUFFIX ".obj"
86cdf0e10cSrcweir #define INCLUDEDIR "."
87cdf0e10cSrcweir 
88cdf0e10cSrcweir char    *directives[] = {
89cdf0e10cSrcweir     "if",
90cdf0e10cSrcweir     "ifdef",
91cdf0e10cSrcweir     "ifndef",
92cdf0e10cSrcweir     "else",
93cdf0e10cSrcweir     "endif",
94cdf0e10cSrcweir     "define",
95cdf0e10cSrcweir     "undef",
96cdf0e10cSrcweir     "include",
97cdf0e10cSrcweir     "line",
98cdf0e10cSrcweir     "pragma",
99cdf0e10cSrcweir     "error",
100cdf0e10cSrcweir     "ident",
101cdf0e10cSrcweir     "sccs",
102cdf0e10cSrcweir     "elif",
103cdf0e10cSrcweir     "eject",
104cdf0e10cSrcweir     NULL
105cdf0e10cSrcweir };
106cdf0e10cSrcweir 
107cdf0e10cSrcweir #define MAKEDEPEND
108cdf0e10cSrcweir #include "imakemdep.h"  /* from config sources */
109cdf0e10cSrcweir #undef MAKEDEPEND
110cdf0e10cSrcweir 
111cdf0e10cSrcweir /*******   function declarations ********/
112cdf0e10cSrcweir /*******   added by -Wall project *******/
113cdf0e10cSrcweir void redirect(char * line, char * makefile );
114cdf0e10cSrcweir 
115cdf0e10cSrcweir struct  inclist inclist[ MAXFILES ],
116cdf0e10cSrcweir         *inclistp = inclist;
117cdf0e10cSrcweir 
118cdf0e10cSrcweir struct symhash *maininclist = NULL;
119cdf0e10cSrcweir 
120cdf0e10cSrcweir char    *filelist[ MAXFILES ];
121cdf0e10cSrcweir char    *includedirs[ MAXDIRS + 1 ];
122cdf0e10cSrcweir char    *notdotdot[ MAXDIRS ];
123cdf0e10cSrcweir char    *objprefix = "";
124cdf0e10cSrcweir char    *objsuffix = OBJSUFFIX;
125cdf0e10cSrcweir char    *startat = "# DO NOT DELETE";
126cdf0e10cSrcweir int width = 78;
127cdf0e10cSrcweir boolean append = FALSE;
128cdf0e10cSrcweir boolean printed = FALSE;
129cdf0e10cSrcweir boolean verbose = FALSE;
130cdf0e10cSrcweir boolean show_where_not = FALSE;
131cdf0e10cSrcweir boolean warn_multiple = FALSE;  /* Warn on multiple includes of same file */
132cdf0e10cSrcweir 
133cdf0e10cSrcweir static
134cdf0e10cSrcweir #ifdef SIGNALRETURNSINT
135cdf0e10cSrcweir int
136cdf0e10cSrcweir #else
137cdf0e10cSrcweir void
138cdf0e10cSrcweir #endif
139cdf0e10cSrcweir catch (sig)
140cdf0e10cSrcweir     int sig;
141cdf0e10cSrcweir {
142cdf0e10cSrcweir     fflush (stdout);
143*e932034bSJim Jagielski     abort();
144cdf0e10cSrcweir     fatalerr ("got signal %d\n", sig);
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir #if defined(USG) || (defined(i386) && defined(SYSV)) || defined(WIN32) || defined(OS2) || defined(Lynx_22)
148cdf0e10cSrcweir #define USGISH
149cdf0e10cSrcweir #endif
150cdf0e10cSrcweir 
151cdf0e10cSrcweir #ifndef USGISH
152cdf0e10cSrcweir #ifndef _POSIX_SOURCE
153cdf0e10cSrcweir #define sigaction sigvec
154cdf0e10cSrcweir #define sa_handler sv_handler
155cdf0e10cSrcweir #define sa_mask sv_mask
156cdf0e10cSrcweir #define sa_flags sv_flags
157cdf0e10cSrcweir #endif
158cdf0e10cSrcweir struct sigaction sig_act;
159cdf0e10cSrcweir #endif /* USGISH */
160cdf0e10cSrcweir 
161cdf0e10cSrcweir boolean native_win_slashes = FALSE;
162cdf0e10cSrcweir 
163cdf0e10cSrcweir int main(argc, argv)
164cdf0e10cSrcweir     int argc;
165cdf0e10cSrcweir     char    **argv;
166cdf0e10cSrcweir {
167cdf0e10cSrcweir     register char   **fp = filelist;
168cdf0e10cSrcweir     register char   **incp = includedirs;
169cdf0e10cSrcweir     register char   *p;
170cdf0e10cSrcweir     register struct inclist *ip;
171cdf0e10cSrcweir     char    *makefile = NULL;
172cdf0e10cSrcweir     struct filepointer  *filecontent;
173cdf0e10cSrcweir     struct pair *psymp = predefs;
174cdf0e10cSrcweir     char *endmarker = NULL;
175cdf0e10cSrcweir     char *defincdir = NULL;
176cdf0e10cSrcweir     struct IncludesCollection* incCollection;
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     ProgramName = argv[0];
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     while (psymp->p_name)
181cdf0e10cSrcweir     {
182cdf0e10cSrcweir         hash_define(psymp->p_name, psymp->p_value, &maininclist);
183cdf0e10cSrcweir         psymp++;
184cdf0e10cSrcweir     }
185cdf0e10cSrcweir     if (argc == 2 && argv[1][0] == '@') {
186cdf0e10cSrcweir         struct stat ast;
187cdf0e10cSrcweir         int afd;
188cdf0e10cSrcweir         char *args;
189cdf0e10cSrcweir         char **nargv;
190cdf0e10cSrcweir         int nargc;
191cdf0e10cSrcweir         char quotechar = '\0';
192cdf0e10cSrcweir 
193cdf0e10cSrcweir         nargc = 1;
194cdf0e10cSrcweir         if ((afd = open(argv[1]+1, O_RDONLY)) < 0)
195cdf0e10cSrcweir         fatalerr("cannot open \"%s\"\n", argv[1]+1);
196cdf0e10cSrcweir         fstat(afd, &ast);
197cdf0e10cSrcweir         args = (char *)malloc(ast.st_size + 1);
198cdf0e10cSrcweir         if ((ast.st_size = read(afd, args, (size_t) ast.st_size)) < 0)
199cdf0e10cSrcweir         fatalerr("failed to read %s\n", argv[1]+1);
200cdf0e10cSrcweir         args[ast.st_size] = '\0';
201cdf0e10cSrcweir         close(afd);
202cdf0e10cSrcweir         for (p = args; *p; p++) {
203cdf0e10cSrcweir         if (quotechar) {
204cdf0e10cSrcweir             if (quotechar == '\\' ||
205cdf0e10cSrcweir             (*p == quotechar && p[-1] != '\\'))
206cdf0e10cSrcweir             quotechar = '\0';
207cdf0e10cSrcweir             continue;
208cdf0e10cSrcweir         }
209cdf0e10cSrcweir         switch (*p) {
210cdf0e10cSrcweir         case '\\':
211cdf0e10cSrcweir         case '"':
212cdf0e10cSrcweir         case '\'':
213cdf0e10cSrcweir             quotechar = *p;
214cdf0e10cSrcweir             break;
215cdf0e10cSrcweir         case ' ':
216cdf0e10cSrcweir         case '\n':
217cdf0e10cSrcweir             *p = '\0';
218cdf0e10cSrcweir             if (p > args && p[-1])
219cdf0e10cSrcweir             nargc++;
220cdf0e10cSrcweir             break;
221cdf0e10cSrcweir         }
222cdf0e10cSrcweir         }
223cdf0e10cSrcweir         if (p[-1])
224cdf0e10cSrcweir         nargc++;
225cdf0e10cSrcweir         nargv = (char **)malloc(nargc * sizeof(char *));
226cdf0e10cSrcweir         nargv[0] = argv[0];
227cdf0e10cSrcweir         argc = 1;
228cdf0e10cSrcweir         for (p = args; argc < nargc; p += strlen(p) + 1)
229cdf0e10cSrcweir         if (*p) nargv[argc++] = p;
230cdf0e10cSrcweir         argv = nargv;
231cdf0e10cSrcweir     }
232cdf0e10cSrcweir     for(argc--, argv++; argc; argc--, argv++) {
233cdf0e10cSrcweir             /* if looking for endmarker then check before parsing */
234cdf0e10cSrcweir         if (endmarker && strcmp (endmarker, *argv) == 0) {
235cdf0e10cSrcweir             endmarker = NULL;
236cdf0e10cSrcweir             continue;
237cdf0e10cSrcweir         }
238cdf0e10cSrcweir         if (**argv != '-') {
239cdf0e10cSrcweir             /* treat +thing as an option for C++ */
240cdf0e10cSrcweir             if (endmarker && **argv == '+')
241cdf0e10cSrcweir                 continue;
242cdf0e10cSrcweir             *fp++ = argv[0];
243cdf0e10cSrcweir             continue;
244cdf0e10cSrcweir         }
245cdf0e10cSrcweir         switch(argv[0][1]) {
246cdf0e10cSrcweir         case '-':
247cdf0e10cSrcweir             endmarker = &argv[0][2];
248cdf0e10cSrcweir             if (endmarker[0] == '\0') endmarker = "--";
249cdf0e10cSrcweir             break;
250cdf0e10cSrcweir         case 'D':
251cdf0e10cSrcweir             if (argv[0][2] == '\0') {
252cdf0e10cSrcweir                 argv++;
253cdf0e10cSrcweir                 argc--;
254cdf0e10cSrcweir             }
255cdf0e10cSrcweir             for (p=argv[0] + 2; *p ; p++)
256cdf0e10cSrcweir                 if (*p == '=') {
257cdf0e10cSrcweir                     *p = ' ';
258cdf0e10cSrcweir                     break;
259cdf0e10cSrcweir                 }
260cdf0e10cSrcweir             define(argv[0] + 2, &maininclist);
261cdf0e10cSrcweir             break;
262cdf0e10cSrcweir         case 'I':
263cdf0e10cSrcweir             if (incp >= includedirs + MAXDIRS)
264cdf0e10cSrcweir                 fatalerr("Too many -I flags.\n");
265cdf0e10cSrcweir             *incp++ = argv[0]+2;
266cdf0e10cSrcweir             if (**(incp-1) == '\0') {
267cdf0e10cSrcweir                 *(incp-1) = *(++argv);
268cdf0e10cSrcweir                 argc--;
269cdf0e10cSrcweir             }
270cdf0e10cSrcweir             break;
271cdf0e10cSrcweir         case 'Y':
272cdf0e10cSrcweir             defincdir = argv[0]+2;
273cdf0e10cSrcweir             break;
274cdf0e10cSrcweir         /* do not use if endmarker processing */
275cdf0e10cSrcweir         case 'a':
276cdf0e10cSrcweir             if (endmarker) break;
277cdf0e10cSrcweir             append = TRUE;
278cdf0e10cSrcweir             break;
279cdf0e10cSrcweir         case 'w':
280cdf0e10cSrcweir             if (endmarker) break;
281cdf0e10cSrcweir             if (argv[0][2] == '\0') {
282cdf0e10cSrcweir                 argv++;
283cdf0e10cSrcweir                 argc--;
284cdf0e10cSrcweir                 width = atoi(argv[0]);
285cdf0e10cSrcweir             } else
286cdf0e10cSrcweir                 width = atoi(argv[0]+2);
287cdf0e10cSrcweir             break;
288cdf0e10cSrcweir         case 'n':
289cdf0e10cSrcweir             // Use "-n" switch to generate dependencies with windows-native slash style
290cdf0e10cSrcweir             native_win_slashes = TRUE;
291cdf0e10cSrcweir             break;
292cdf0e10cSrcweir         case 'o':
293cdf0e10cSrcweir             if (endmarker) break;
294cdf0e10cSrcweir             if (argv[0][2] == '\0') {
295cdf0e10cSrcweir                 argv++;
296cdf0e10cSrcweir                 argc--;
297cdf0e10cSrcweir                 objsuffix = argv[0];
298cdf0e10cSrcweir             } else
299cdf0e10cSrcweir                 objsuffix = argv[0]+2;
300cdf0e10cSrcweir             break;
301cdf0e10cSrcweir         case 'p':
302cdf0e10cSrcweir             if (endmarker) break;
303cdf0e10cSrcweir             if (argv[0][2] == '\0') {
304cdf0e10cSrcweir                 argv++;
305cdf0e10cSrcweir                 argc--;
306cdf0e10cSrcweir                 objprefix = argv[0];
307cdf0e10cSrcweir             } else
308cdf0e10cSrcweir                 objprefix = argv[0]+2;
309cdf0e10cSrcweir             break;
310cdf0e10cSrcweir         case 'v':
311cdf0e10cSrcweir             if (endmarker) break;
312cdf0e10cSrcweir             verbose = TRUE;
313cdf0e10cSrcweir #ifdef DEBUG
314cdf0e10cSrcweir             if (argv[0][2])
315cdf0e10cSrcweir                 _debugmask = atoi(argv[0]+2);
316cdf0e10cSrcweir #endif
317cdf0e10cSrcweir             break;
318cdf0e10cSrcweir         case 's':
319cdf0e10cSrcweir             if (endmarker) break;
320cdf0e10cSrcweir             startat = argv[0]+2;
321cdf0e10cSrcweir             if (*startat == '\0') {
322cdf0e10cSrcweir                 startat = *(++argv);
323cdf0e10cSrcweir                 argc--;
324cdf0e10cSrcweir             }
325cdf0e10cSrcweir             if (*startat != '#')
326cdf0e10cSrcweir                 fatalerr("-s flag's value should start %s\n",
327cdf0e10cSrcweir                     "with '#'.");
328cdf0e10cSrcweir             break;
329cdf0e10cSrcweir         case 'f':
330cdf0e10cSrcweir             if (endmarker) break;
331cdf0e10cSrcweir             makefile = argv[0]+2;
332cdf0e10cSrcweir             if (*makefile == '\0') {
333cdf0e10cSrcweir                 makefile = *(++argv);
334cdf0e10cSrcweir                 argc--;
335cdf0e10cSrcweir             }
336cdf0e10cSrcweir             break;
337cdf0e10cSrcweir 
338cdf0e10cSrcweir         case 'm':
339cdf0e10cSrcweir             warn_multiple = TRUE;
340cdf0e10cSrcweir             break;
341cdf0e10cSrcweir 
342cdf0e10cSrcweir         /* Ignore -O, -g so we can just pass ${CFLAGS} to
343cdf0e10cSrcweir            makedepend
344cdf0e10cSrcweir          */
345cdf0e10cSrcweir         case 'O':
346cdf0e10cSrcweir         case 'g':
347cdf0e10cSrcweir             break;
348cdf0e10cSrcweir         default:
349cdf0e10cSrcweir             if (endmarker) break;
350cdf0e10cSrcweir     /*      fatalerr("unknown opt = %s\n", argv[0]); */
351cdf0e10cSrcweir             warning("ignoring option %s\n", argv[0]);
352cdf0e10cSrcweir         }
353cdf0e10cSrcweir     }
354cdf0e10cSrcweir 
355cdf0e10cSrcweir     convert_slashes(objprefix);
356cdf0e10cSrcweir     objprefix = append_slash(objprefix);
357cdf0e10cSrcweir 
358cdf0e10cSrcweir     if (!defincdir) {
359cdf0e10cSrcweir #ifdef PREINCDIR
360cdf0e10cSrcweir         if (incp >= includedirs + MAXDIRS)
361cdf0e10cSrcweir         fatalerr("Too many -I flags.\n");
362cdf0e10cSrcweir         *incp++ = PREINCDIR;
363cdf0e10cSrcweir #endif
364cdf0e10cSrcweir         if (incp >= includedirs + MAXDIRS)
365cdf0e10cSrcweir         fatalerr("Too many -I flags.\n");
366cdf0e10cSrcweir         *incp++ = INCLUDEDIR;
367cdf0e10cSrcweir #ifdef POSTINCDIR
368cdf0e10cSrcweir         if (incp >= includedirs + MAXDIRS)
369cdf0e10cSrcweir         fatalerr("Too many -I flags.\n");
370cdf0e10cSrcweir         *incp++ = POSTINCDIR;
371cdf0e10cSrcweir #endif
372cdf0e10cSrcweir     } else if (*defincdir) {
373cdf0e10cSrcweir         if (incp >= includedirs + MAXDIRS)
374cdf0e10cSrcweir         fatalerr("Too many -I flags.\n");
375cdf0e10cSrcweir         *incp++ = defincdir;
376cdf0e10cSrcweir     }
377cdf0e10cSrcweir 
378cdf0e10cSrcweir     redirect(startat, makefile);
379cdf0e10cSrcweir 
380cdf0e10cSrcweir     /*
381cdf0e10cSrcweir      * catch signals.
382cdf0e10cSrcweir      */
383cdf0e10cSrcweir #ifdef USGISH
384cdf0e10cSrcweir /*  should really reset SIGINT to SIG_IGN if it was.  */
385cdf0e10cSrcweir #ifdef SIGHUP
386cdf0e10cSrcweir     signal (SIGHUP, catch);
387cdf0e10cSrcweir #endif
388cdf0e10cSrcweir     signal (SIGINT, catch);
389cdf0e10cSrcweir #ifdef SIGQUIT
390cdf0e10cSrcweir     signal (SIGQUIT, catch);
391cdf0e10cSrcweir #endif
392cdf0e10cSrcweir     signal (SIGILL, catch);
393cdf0e10cSrcweir #ifdef SIGBUS
394cdf0e10cSrcweir     signal (SIGBUS, catch);
395cdf0e10cSrcweir #endif
396cdf0e10cSrcweir     signal (SIGSEGV, catch);
397cdf0e10cSrcweir #ifdef SIGSYS
398cdf0e10cSrcweir     signal (SIGSYS, catch);
399cdf0e10cSrcweir #endif
400cdf0e10cSrcweir     signal (SIGFPE, catch);
401cdf0e10cSrcweir #else
402cdf0e10cSrcweir     sig_act.sa_handler = catch;
403cdf0e10cSrcweir #ifdef _POSIX_SOURCE
404cdf0e10cSrcweir     sigemptyset(&sig_act.sa_mask);
405cdf0e10cSrcweir     sigaddset(&sig_act.sa_mask, SIGINT);
406cdf0e10cSrcweir     sigaddset(&sig_act.sa_mask, SIGQUIT);
407cdf0e10cSrcweir #ifdef SIGBUS
408cdf0e10cSrcweir     sigaddset(&sig_act.sa_mask, SIGBUS);
409cdf0e10cSrcweir #endif
410cdf0e10cSrcweir     sigaddset(&sig_act.sa_mask, SIGILL);
411cdf0e10cSrcweir     sigaddset(&sig_act.sa_mask, SIGSEGV);
412cdf0e10cSrcweir     sigaddset(&sig_act.sa_mask, SIGHUP);
413cdf0e10cSrcweir     sigaddset(&sig_act.sa_mask, SIGPIPE);
414cdf0e10cSrcweir #ifdef SIGSYS
415cdf0e10cSrcweir     sigaddset(&sig_act.sa_mask, SIGSYS);
416cdf0e10cSrcweir #endif
417cdf0e10cSrcweir #else
418cdf0e10cSrcweir     sig_act.sa_mask = ((1<<(SIGINT -1))
419cdf0e10cSrcweir                |(1<<(SIGQUIT-1))
420cdf0e10cSrcweir #ifdef SIGBUS
421cdf0e10cSrcweir                |(1<<(SIGBUS-1))
422cdf0e10cSrcweir #endif
423cdf0e10cSrcweir                |(1<<(SIGILL-1))
424cdf0e10cSrcweir                |(1<<(SIGSEGV-1))
425cdf0e10cSrcweir                |(1<<(SIGHUP-1))
426cdf0e10cSrcweir                |(1<<(SIGPIPE-1))
427cdf0e10cSrcweir #ifdef SIGSYS
428cdf0e10cSrcweir                |(1<<(SIGSYS-1))
429cdf0e10cSrcweir #endif
430cdf0e10cSrcweir                );
431cdf0e10cSrcweir #endif /* _POSIX_SOURCE */
432cdf0e10cSrcweir     sig_act.sa_flags = 0;
433cdf0e10cSrcweir     sigaction(SIGHUP, &sig_act, (struct sigaction *)0);
434cdf0e10cSrcweir     sigaction(SIGINT, &sig_act, (struct sigaction *)0);
435cdf0e10cSrcweir     sigaction(SIGQUIT, &sig_act, (struct sigaction *)0);
436cdf0e10cSrcweir     sigaction(SIGILL, &sig_act, (struct sigaction *)0);
437cdf0e10cSrcweir #ifdef SIGBUS
438cdf0e10cSrcweir     sigaction(SIGBUS, &sig_act, (struct sigaction *)0);
439cdf0e10cSrcweir #endif
440cdf0e10cSrcweir     sigaction(SIGSEGV, &sig_act, (struct sigaction *)0);
441cdf0e10cSrcweir #ifdef SIGSYS
442cdf0e10cSrcweir     sigaction(SIGSYS, &sig_act, (struct sigaction *)0);
443cdf0e10cSrcweir #endif
444cdf0e10cSrcweir #endif /* USGISH */
445cdf0e10cSrcweir 
446cdf0e10cSrcweir     /*
447cdf0e10cSrcweir      * now peruse through the list of files.
448cdf0e10cSrcweir      */
449cdf0e10cSrcweir     incCollection = create_IncludesCollection();
450cdf0e10cSrcweir 
451cdf0e10cSrcweir     for(fp=filelist; *fp; fp++) {
452cdf0e10cSrcweir         struct symhash *includes;
453cdf0e10cSrcweir         filecontent = getfile(*fp);
454cdf0e10cSrcweir         ip = newinclude(*fp, (char *)NULL);
455cdf0e10cSrcweir 
456cdf0e10cSrcweir         includes = hash_copy( maininclist );
457cdf0e10cSrcweir         find_includes(filecontent, ip, ip, 0, FALSE, incCollection, includes);
458cdf0e10cSrcweir         hash_free( includes );
459cdf0e10cSrcweir 
460cdf0e10cSrcweir         freefile(filecontent);
461cdf0e10cSrcweir         recursive_pr_include(ip, ip->i_file, base_name(*fp));
462cdf0e10cSrcweir         inc_clean();
463cdf0e10cSrcweir     }
464cdf0e10cSrcweir     if (printed)
465cdf0e10cSrcweir         printf("\n");
466cdf0e10cSrcweir 
467cdf0e10cSrcweir     delete_IncludesCollection(incCollection);
468cdf0e10cSrcweir 
469cdf0e10cSrcweir     exit(0);
470cdf0e10cSrcweir }
471cdf0e10cSrcweir 
472cdf0e10cSrcweir struct filepointer *getfile(file)
473cdf0e10cSrcweir     char    *file;
474cdf0e10cSrcweir {
475cdf0e10cSrcweir     register int    fd;
476cdf0e10cSrcweir     struct filepointer  *content;
477cdf0e10cSrcweir     struct stat st;
478cdf0e10cSrcweir     off_t       size_backup;
479cdf0e10cSrcweir     ssize_t     bytes_read;
480cdf0e10cSrcweir     size_t      malloc_size;
481cdf0e10cSrcweir 
482cdf0e10cSrcweir     content = (struct filepointer *)malloc(sizeof(struct filepointer));
483cdf0e10cSrcweir     if ((fd = open(file, O_RDONLY)) < 0) {
484cdf0e10cSrcweir         warning("makedepend:  Cannot open file \"%s\"\n", file);
485cdf0e10cSrcweir         content->f_p = content->f_base = content->f_end = (char *)malloc(1);
486cdf0e10cSrcweir         *content->f_p = '\0';
487cdf0e10cSrcweir         return(content);
488cdf0e10cSrcweir     }
489cdf0e10cSrcweir     fstat(fd, &st);
490cdf0e10cSrcweir 
491cdf0e10cSrcweir     size_backup = st.st_size;
492cdf0e10cSrcweir     malloc_size = size_backup;
493cdf0e10cSrcweir     /* Since off_t is larger than size_t, need to test for
494cdf0e10cSrcweir      * truncation.
495cdf0e10cSrcweir      */
496cdf0e10cSrcweir     if ( (off_t)malloc_size != size_backup )
497cdf0e10cSrcweir     {
498cdf0e10cSrcweir         close( fd );
499cdf0e10cSrcweir         warning("makedepend:  File \"%s\" size larger than can fit in size_t.  Cannot allocate memory for contents.\n", file);
500cdf0e10cSrcweir         content->f_p = content->f_base = content->f_end = (char *)malloc(1);
501cdf0e10cSrcweir         *content->f_p = '\0';
502cdf0e10cSrcweir         return(content);
503cdf0e10cSrcweir     }
504cdf0e10cSrcweir 
505cdf0e10cSrcweir     content->f_base = (char *)malloc(malloc_size+1);
506cdf0e10cSrcweir     if (content->f_base == NULL)
507cdf0e10cSrcweir         fatalerr("makedepend:  Cannot allocate memory to process file \"%s\"\n", file);
508cdf0e10cSrcweir     if ((bytes_read = read(fd, content->f_base, malloc_size)) < 0)
509cdf0e10cSrcweir         if ( st.st_mode & S_IFREG )
510cdf0e10cSrcweir             fatalerr("makedepend:  Failed to read file \"%s\"\n", file);
511cdf0e10cSrcweir 
512cdf0e10cSrcweir     close(fd);
513cdf0e10cSrcweir     content->f_len = bytes_read+1;
514cdf0e10cSrcweir     content->f_p = content->f_base;
515cdf0e10cSrcweir     content->f_end = content->f_base + bytes_read;
516cdf0e10cSrcweir     *content->f_end = '\0';
517cdf0e10cSrcweir     content->f_line = 0;
518cdf0e10cSrcweir     return(content);
519cdf0e10cSrcweir }
520cdf0e10cSrcweir 
521cdf0e10cSrcweir void freefile(fp)
522cdf0e10cSrcweir     struct filepointer  *fp;
523cdf0e10cSrcweir {
524cdf0e10cSrcweir     free(fp->f_base);
525cdf0e10cSrcweir     free(fp);
526cdf0e10cSrcweir }
527cdf0e10cSrcweir 
528cdf0e10cSrcweir char *copy(str)
529cdf0e10cSrcweir     register char   *str;
530cdf0e10cSrcweir {
531cdf0e10cSrcweir     register char   *p = (char *)malloc(strlen(str) + 1);
532cdf0e10cSrcweir 
533cdf0e10cSrcweir     strcpy(p, str);
534cdf0e10cSrcweir     return(p);
535cdf0e10cSrcweir }
536cdf0e10cSrcweir 
537cdf0e10cSrcweir int match(str, list)
538cdf0e10cSrcweir     register char   *str, **list;
539cdf0e10cSrcweir {
540cdf0e10cSrcweir     register int    i;
541cdf0e10cSrcweir 
542cdf0e10cSrcweir     for (i=0; *list; i++, list++)
543cdf0e10cSrcweir         if (strcmp(str, *list) == 0)
544cdf0e10cSrcweir             return(i);
545cdf0e10cSrcweir     return(-1);
546cdf0e10cSrcweir }
547cdf0e10cSrcweir 
548cdf0e10cSrcweir /*
549cdf0e10cSrcweir  * Get the next line.  We only return lines beginning with '#' since that
550cdf0e10cSrcweir  * is all this program is ever interested in.
551cdf0e10cSrcweir  */
552cdf0e10cSrcweir char *get_line(filep)
553cdf0e10cSrcweir     register struct filepointer *filep;
554cdf0e10cSrcweir {
555cdf0e10cSrcweir     register char   *p, /* walking pointer */
556cdf0e10cSrcweir             *eof,   /* end of file pointer */
557cdf0e10cSrcweir             *bol;   /* beginning of line pointer */
558cdf0e10cSrcweir     register int    lineno; /* line number */
559cdf0e10cSrcweir 
560cdf0e10cSrcweir     p = filep->f_p;
561cdf0e10cSrcweir     eof = filep->f_end;
562cdf0e10cSrcweir     if (p >= eof)
563cdf0e10cSrcweir         return((char *)NULL);
564cdf0e10cSrcweir     lineno = filep->f_line;
565cdf0e10cSrcweir 
566cdf0e10cSrcweir     for(bol = p--; ++p < eof; ) {
567cdf0e10cSrcweir         if (*p == '/' && *(p+1) == '*') { /* consume comments */
568cdf0e10cSrcweir             *p++ = ' ', *p++ = ' ';
569cdf0e10cSrcweir             while (*p) {
570cdf0e10cSrcweir                 if (*p == '*' && *(p+1) == '/') {
571cdf0e10cSrcweir                     *p++ = ' ', *p = ' ';
572cdf0e10cSrcweir                     break;
573cdf0e10cSrcweir                 }
574cdf0e10cSrcweir                 else if (*p == '\n')
575cdf0e10cSrcweir                     lineno++;
576cdf0e10cSrcweir                 *p++ = ' ';
577cdf0e10cSrcweir             }
578cdf0e10cSrcweir             continue;
579cdf0e10cSrcweir         }
580cdf0e10cSrcweir         else if (*p == '/' && *(p+1) == '/') { /* consume comments */
581cdf0e10cSrcweir             *p++ = ' ', *p++ = ' ';
582cdf0e10cSrcweir             while (*p && *p != '\n')
583cdf0e10cSrcweir                 *p++ = ' ';
584cdf0e10cSrcweir             if ( *p == '\n' )
585cdf0e10cSrcweir                 p--;
586cdf0e10cSrcweir             lineno++;
587cdf0e10cSrcweir             continue;
588cdf0e10cSrcweir         }
589cdf0e10cSrcweir         else if (*p == '\\') {
590cdf0e10cSrcweir             if (*(p+1) == '\n') {
591cdf0e10cSrcweir                 *p = ' ';
592cdf0e10cSrcweir                 *(p+1) = ' ';
593cdf0e10cSrcweir                 lineno++;
594cdf0e10cSrcweir             }
595cdf0e10cSrcweir         }
596cdf0e10cSrcweir         else if (*p == '\n') {
597cdf0e10cSrcweir             lineno++;
598cdf0e10cSrcweir             if (*bol == '#') {
599cdf0e10cSrcweir                 register char *cp;
600cdf0e10cSrcweir 
601cdf0e10cSrcweir                 *p++ = '\0';
602cdf0e10cSrcweir                 /* punt lines with just # (yacc generated) */
603cdf0e10cSrcweir                 for (cp = bol+1;
604cdf0e10cSrcweir                      *cp && (*cp == ' ' || *cp == '\t'); cp++);
605cdf0e10cSrcweir                 if (*cp) goto done;
606cdf0e10cSrcweir             }
607cdf0e10cSrcweir             bol = p+1;
608cdf0e10cSrcweir         }
609cdf0e10cSrcweir     }
610cdf0e10cSrcweir     if (*bol != '#')
611cdf0e10cSrcweir         bol = NULL;
612cdf0e10cSrcweir done:
613cdf0e10cSrcweir     filep->f_p = p;
614cdf0e10cSrcweir     filep->f_line = lineno;
615cdf0e10cSrcweir     return(bol);
616cdf0e10cSrcweir }
617cdf0e10cSrcweir 
618cdf0e10cSrcweir /*
619cdf0e10cSrcweir  * Strip the file name down to what we want to see in the Makefile.
620cdf0e10cSrcweir  * It will have objprefix and objsuffix around it.
621cdf0e10cSrcweir  */
622cdf0e10cSrcweir char *base_name(file)
623cdf0e10cSrcweir     register char   *file;
624cdf0e10cSrcweir {
625cdf0e10cSrcweir     register char   *p;
626cdf0e10cSrcweir 
627cdf0e10cSrcweir     file = copy(file);
628cdf0e10cSrcweir     for(p=file+strlen(file); p>file && *p != '.'; p--) ;
629cdf0e10cSrcweir 
630cdf0e10cSrcweir     if (*p == '.')
631cdf0e10cSrcweir         *p = '\0';
632cdf0e10cSrcweir 
633cdf0e10cSrcweir     while (p > file) {
634cdf0e10cSrcweir         if ( *p == '/' ||  *p == '\\') {
635cdf0e10cSrcweir             file = p + 1;
636cdf0e10cSrcweir             break;
63726d9e9ddSpfg         }
638cdf0e10cSrcweir         p--;
63926d9e9ddSpfg     }
640cdf0e10cSrcweir     return(file);
641cdf0e10cSrcweir }
642cdf0e10cSrcweir 
643cdf0e10cSrcweir #if defined(USG) && !defined(CRAY) && !defined(SVR4)
644cdf0e10cSrcweir int rename (from, to)
645cdf0e10cSrcweir     char *from, *to;
646cdf0e10cSrcweir {
647cdf0e10cSrcweir     (void) unlink (to);
648cdf0e10cSrcweir     if (link (from, to) == 0) {
649cdf0e10cSrcweir     unlink (from);
650cdf0e10cSrcweir     return 0;
651cdf0e10cSrcweir     } else {
652cdf0e10cSrcweir     return -1;
653cdf0e10cSrcweir     }
654cdf0e10cSrcweir }
655cdf0e10cSrcweir #endif /* USGISH */
656cdf0e10cSrcweir 
657cdf0e10cSrcweir void redirect(line, makefile)
658cdf0e10cSrcweir     char    *line,
659cdf0e10cSrcweir         *makefile;
660cdf0e10cSrcweir {
661cdf0e10cSrcweir     struct stat st;
662cdf0e10cSrcweir     FILE    *fdin, *fdout;
663*e932034bSJim Jagielski     char    backup[ OURBUFSIZ ],
664*e932034bSJim Jagielski         buf[ OURBUFSIZ ];
665cdf0e10cSrcweir     boolean found = FALSE;
666cdf0e10cSrcweir     int len;
667cdf0e10cSrcweir 
668cdf0e10cSrcweir     /*
669cdf0e10cSrcweir      * if makefile is "-" then let it pour onto stdout.
670cdf0e10cSrcweir      */
671cdf0e10cSrcweir     if (makefile && *makefile == '-' && *(makefile+1) == '\0')
672cdf0e10cSrcweir         return;
673cdf0e10cSrcweir 
674cdf0e10cSrcweir     /*
675cdf0e10cSrcweir      * use a default makefile is not specified.
676cdf0e10cSrcweir      */
677cdf0e10cSrcweir     if (!makefile) {
678cdf0e10cSrcweir         if (stat("Makefile", &st) == 0)
679cdf0e10cSrcweir             makefile = "Makefile";
680cdf0e10cSrcweir         else if (stat("makefile", &st) == 0)
681cdf0e10cSrcweir             makefile = "makefile";
682cdf0e10cSrcweir         else
683cdf0e10cSrcweir             fatalerr("[mM]akefile is not present\n");
684cdf0e10cSrcweir     }
685cdf0e10cSrcweir     else
686cdf0e10cSrcweir         stat(makefile, &st);
687cdf0e10cSrcweir     if ((fdin = fopen(makefile, "r")) == NULL)
688cdf0e10cSrcweir         fatalerr("cannot open \"%s\"\n", makefile);
689cdf0e10cSrcweir     sprintf(backup, "%s.bak", makefile);
690cdf0e10cSrcweir     unlink(backup);
691cdf0e10cSrcweir #if defined(WIN32) || defined(OS2)
692cdf0e10cSrcweir     fclose(fdin);
693cdf0e10cSrcweir #endif
694cdf0e10cSrcweir     if (rename(makefile, backup) < 0)
695cdf0e10cSrcweir         fatalerr("cannot rename %s to %s\n", makefile, backup);
696cdf0e10cSrcweir #if defined(WIN32) || defined(OS2)
697cdf0e10cSrcweir     if ((fdin = fopen(backup, "r")) == NULL)
698cdf0e10cSrcweir         fatalerr("cannot open \"%s\"\n", backup);
699cdf0e10cSrcweir #endif
700cdf0e10cSrcweir     if ((fdout = freopen(makefile, "w", stdout)) == NULL)
701cdf0e10cSrcweir         fatalerr("cannot open \"%s\"\n", backup);
702cdf0e10cSrcweir     len = strlen(line);
703*e932034bSJim Jagielski     while (!found && fgets(buf, OURBUFSIZ, fdin)) {
704cdf0e10cSrcweir         if (*buf == '#' && strncmp(line, buf, len) == 0)
705cdf0e10cSrcweir             found = TRUE;
706cdf0e10cSrcweir         fputs(buf, fdout);
707cdf0e10cSrcweir     }
708cdf0e10cSrcweir     if (!found) {
709cdf0e10cSrcweir         if (verbose)
710cdf0e10cSrcweir         warning("Adding new delimiting line \"%s\" and dependencies...\n",
711cdf0e10cSrcweir             line);
712cdf0e10cSrcweir         puts(line); /* same as fputs(fdout); but with newline */
713cdf0e10cSrcweir     } else if (append) {
714*e932034bSJim Jagielski         while (fgets(buf, OURBUFSIZ, fdin)) {
715cdf0e10cSrcweir         fputs(buf, fdout);
716cdf0e10cSrcweir         }
717cdf0e10cSrcweir     }
718cdf0e10cSrcweir     fflush(fdout);
719cdf0e10cSrcweir #if defined(USGISH) || defined(_SEQUENT_) || defined(USE_CHMOD)
720cdf0e10cSrcweir     chmod(makefile, st.st_mode);
721cdf0e10cSrcweir #else
722cdf0e10cSrcweir     fchmod(fileno(fdout), st.st_mode);
723cdf0e10cSrcweir #endif /* USGISH */
724cdf0e10cSrcweir     fclose(fdin);
725cdf0e10cSrcweir }
726cdf0e10cSrcweir 
727cdf0e10cSrcweir void fatalerr(char *msg, ...)
728cdf0e10cSrcweir {
729cdf0e10cSrcweir     va_list args;
730cdf0e10cSrcweir     fprintf(stderr, "%s: error:  ", ProgramName);
731cdf0e10cSrcweir     va_start(args, msg);
732cdf0e10cSrcweir     vfprintf(stderr, msg, args);
733cdf0e10cSrcweir     va_end(args);
734cdf0e10cSrcweir     exit (1);
735cdf0e10cSrcweir }
736cdf0e10cSrcweir 
737cdf0e10cSrcweir void warning(char *msg, ...)
738cdf0e10cSrcweir {
739cdf0e10cSrcweir #ifdef DEBUG_MKDEPEND
740cdf0e10cSrcweir     va_list args;
741cdf0e10cSrcweir     fprintf(stderr, "%s: warning:  ", ProgramName);
742cdf0e10cSrcweir     va_start(args, msg);
743cdf0e10cSrcweir     vfprintf(stderr, msg, args);
744cdf0e10cSrcweir     va_end(args);
745cdf0e10cSrcweir #else
746cdf0e10cSrcweir     (void)msg;
747cdf0e10cSrcweir #endif /* DEBUG_MKDEPEND */
748cdf0e10cSrcweir }
749cdf0e10cSrcweir 
750cdf0e10cSrcweir void warning1(char *msg, ...)
751cdf0e10cSrcweir {
752cdf0e10cSrcweir #ifdef DEBUG_MKDEPEND
753cdf0e10cSrcweir     va_list args;
754cdf0e10cSrcweir     va_start(args, msg);
755cdf0e10cSrcweir     vfprintf(stderr, msg, args);
756cdf0e10cSrcweir     va_end(args);
757cdf0e10cSrcweir #else
758cdf0e10cSrcweir     (void)msg;
759cdf0e10cSrcweir #endif /* DEBUG_MKDEPEND */
760cdf0e10cSrcweir }
761cdf0e10cSrcweir 
762cdf0e10cSrcweir void convert_slashes(path)
763cdf0e10cSrcweir     char* path;
764cdf0e10cSrcweir {
765cdf0e10cSrcweir #if defined (WNT) || defined(OS2)
766cdf0e10cSrcweir     /*
767cdf0e10cSrcweir      * Convert backslashes to slashes
768cdf0e10cSrcweir      */
769cdf0e10cSrcweir     char *ptr;
770cdf0e10cSrcweir     if (native_win_slashes) {
771cdf0e10cSrcweir         for (ptr = (char*)path; *ptr; ++ptr)
772cdf0e10cSrcweir             if (*ptr == '/')
773cdf0e10cSrcweir                 *ptr = '\\';
774cdf0e10cSrcweir     } else {
775cdf0e10cSrcweir         for (ptr = (char*)path; *ptr; ++ptr)
776cdf0e10cSrcweir             if (*ptr == '\\')
777cdf0e10cSrcweir                 *ptr = '/';
77826d9e9ddSpfg     }
779cdf0e10cSrcweir #else
780cdf0e10cSrcweir     (void)path;
781cdf0e10cSrcweir #endif
782cdf0e10cSrcweir }
783cdf0e10cSrcweir 
784cdf0e10cSrcweir char* append_slash(path)
785cdf0e10cSrcweir     char* path;
786cdf0e10cSrcweir {
787cdf0e10cSrcweir     char *new_string;
788cdf0e10cSrcweir     if ((path[strlen(path) - 1] == '/') || (path[strlen(path) - 1] == '\\')) {
789cdf0e10cSrcweir         new_string = path;
790cdf0e10cSrcweir     } else {
791cdf0e10cSrcweir         new_string = (char*)malloc(sizeof(char) * (strlen(path) + 2));
792cdf0e10cSrcweir         strcpy(new_string, path);
793cdf0e10cSrcweir         if (native_win_slashes)
794cdf0e10cSrcweir             strcat(new_string, "\\");
795cdf0e10cSrcweir         else
796cdf0e10cSrcweir             strcat(new_string, "/");
79726d9e9ddSpfg     }
798cdf0e10cSrcweir     return new_string;
799cdf0e10cSrcweir }
800cdf0e10cSrcweir 
801