xref: /aoo42x/main/soltools/mkdepend/main.c (revision 0d22dc06)
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
catch(sig)139cdf0e10cSrcweir catch (sig)
140cdf0e10cSrcweir     int sig;
141cdf0e10cSrcweir {
142cdf0e10cSrcweir 	fflush (stdout);
143cdf0e10cSrcweir 	fatalerr ("got signal %d\n", sig);
144cdf0e10cSrcweir }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir #if defined(USG) || (defined(i386) && defined(SYSV)) || defined(WIN32) || defined(OS2) || defined(Lynx_22)
147cdf0e10cSrcweir #define USGISH
148cdf0e10cSrcweir #endif
149cdf0e10cSrcweir 
150cdf0e10cSrcweir #ifndef USGISH
151cdf0e10cSrcweir #ifndef _POSIX_SOURCE
152cdf0e10cSrcweir #define sigaction sigvec
153cdf0e10cSrcweir #define sa_handler sv_handler
154cdf0e10cSrcweir #define sa_mask sv_mask
155cdf0e10cSrcweir #define sa_flags sv_flags
156cdf0e10cSrcweir #endif
157cdf0e10cSrcweir struct sigaction sig_act;
158cdf0e10cSrcweir #endif /* USGISH */
159cdf0e10cSrcweir 
160cdf0e10cSrcweir boolean	native_win_slashes = FALSE;
161cdf0e10cSrcweir 
main(argc,argv)162cdf0e10cSrcweir int main(argc, argv)
163cdf0e10cSrcweir 	int	argc;
164cdf0e10cSrcweir 	char	**argv;
165cdf0e10cSrcweir {
166cdf0e10cSrcweir 	register char	**fp = filelist;
167cdf0e10cSrcweir 	register char	**incp = includedirs;
168cdf0e10cSrcweir 	register char	*p;
169cdf0e10cSrcweir 	register struct inclist	*ip;
170cdf0e10cSrcweir 	char	*makefile = NULL;
171cdf0e10cSrcweir 	struct filepointer	*filecontent;
172cdf0e10cSrcweir 	struct pair *psymp = predefs;
173cdf0e10cSrcweir 	char *endmarker = NULL;
174cdf0e10cSrcweir 	char *defincdir = NULL;
175cdf0e10cSrcweir     struct IncludesCollection* incCollection;
176cdf0e10cSrcweir 
177cdf0e10cSrcweir 	ProgramName = argv[0];
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 	while (psymp->p_name)
180cdf0e10cSrcweir 	{
181cdf0e10cSrcweir 	    hash_define(psymp->p_name, psymp->p_value, &maininclist);
182cdf0e10cSrcweir 	    psymp++;
183cdf0e10cSrcweir 	}
184cdf0e10cSrcweir 	if (argc == 2 && argv[1][0] == '@') {
185cdf0e10cSrcweir 	    struct stat ast;
186cdf0e10cSrcweir 	    int afd;
187cdf0e10cSrcweir 	    char *args;
188cdf0e10cSrcweir 	    char **nargv;
189cdf0e10cSrcweir 	    int nargc;
190cdf0e10cSrcweir 	    char quotechar = '\0';
191cdf0e10cSrcweir 
192cdf0e10cSrcweir 	    nargc = 1;
193cdf0e10cSrcweir 	    if ((afd = open(argv[1]+1, O_RDONLY)) < 0)
194cdf0e10cSrcweir 		fatalerr("cannot open \"%s\"\n", argv[1]+1);
195cdf0e10cSrcweir 	    fstat(afd, &ast);
196cdf0e10cSrcweir 	    args = (char *)malloc(ast.st_size + 1);
197cdf0e10cSrcweir 	    if ((ast.st_size = read(afd, args, (size_t) ast.st_size)) < 0)
198cdf0e10cSrcweir 		fatalerr("failed to read %s\n", argv[1]+1);
199cdf0e10cSrcweir 	    args[ast.st_size] = '\0';
200cdf0e10cSrcweir 	    close(afd);
201cdf0e10cSrcweir 	    for (p = args; *p; p++) {
202cdf0e10cSrcweir 		if (quotechar) {
203cdf0e10cSrcweir 		    if (quotechar == '\\' ||
204cdf0e10cSrcweir 			(*p == quotechar && p[-1] != '\\'))
205cdf0e10cSrcweir 			quotechar = '\0';
206cdf0e10cSrcweir 		    continue;
207cdf0e10cSrcweir 		}
208cdf0e10cSrcweir 		switch (*p) {
209cdf0e10cSrcweir 		case '\\':
210cdf0e10cSrcweir 		case '"':
211cdf0e10cSrcweir 		case '\'':
212cdf0e10cSrcweir 		    quotechar = *p;
213cdf0e10cSrcweir 		    break;
214cdf0e10cSrcweir 		case ' ':
215cdf0e10cSrcweir 		case '\n':
216cdf0e10cSrcweir 		    *p = '\0';
217cdf0e10cSrcweir 		    if (p > args && p[-1])
218cdf0e10cSrcweir 			nargc++;
219cdf0e10cSrcweir 		    break;
220cdf0e10cSrcweir 		}
221cdf0e10cSrcweir 	    }
222cdf0e10cSrcweir 	    if (p[-1])
223cdf0e10cSrcweir 		nargc++;
224cdf0e10cSrcweir 	    nargv = (char **)malloc(nargc * sizeof(char *));
225cdf0e10cSrcweir 	    nargv[0] = argv[0];
226cdf0e10cSrcweir 	    argc = 1;
227cdf0e10cSrcweir 	    for (p = args; argc < nargc; p += strlen(p) + 1)
228cdf0e10cSrcweir 		if (*p) nargv[argc++] = p;
229cdf0e10cSrcweir 	    argv = nargv;
230cdf0e10cSrcweir 	}
231cdf0e10cSrcweir 	for(argc--, argv++; argc; argc--, argv++) {
232cdf0e10cSrcweir 	    	/* if looking for endmarker then check before parsing */
233cdf0e10cSrcweir 		if (endmarker && strcmp (endmarker, *argv) == 0) {
234cdf0e10cSrcweir 		    endmarker = NULL;
235cdf0e10cSrcweir 		    continue;
236cdf0e10cSrcweir 		}
237cdf0e10cSrcweir 		if (**argv != '-') {
238cdf0e10cSrcweir 			/* treat +thing as an option for C++ */
239cdf0e10cSrcweir 			if (endmarker && **argv == '+')
240cdf0e10cSrcweir 				continue;
241cdf0e10cSrcweir 			*fp++ = argv[0];
242cdf0e10cSrcweir 			continue;
243cdf0e10cSrcweir 		}
244cdf0e10cSrcweir 		switch(argv[0][1]) {
245cdf0e10cSrcweir 		case '-':
246cdf0e10cSrcweir 			endmarker = &argv[0][2];
247cdf0e10cSrcweir 			if (endmarker[0] == '\0') endmarker = "--";
248cdf0e10cSrcweir 			break;
249cdf0e10cSrcweir 		case 'D':
250cdf0e10cSrcweir 			if (argv[0][2] == '\0') {
251cdf0e10cSrcweir 				argv++;
252cdf0e10cSrcweir 				argc--;
253cdf0e10cSrcweir 			}
254cdf0e10cSrcweir 			for (p=argv[0] + 2; *p ; p++)
255cdf0e10cSrcweir 				if (*p == '=') {
256cdf0e10cSrcweir 					*p = ' ';
257cdf0e10cSrcweir 					break;
258cdf0e10cSrcweir 				}
259cdf0e10cSrcweir 			define(argv[0] + 2, &maininclist);
260cdf0e10cSrcweir 			break;
261cdf0e10cSrcweir 		case 'I':
262cdf0e10cSrcweir 			if (incp >= includedirs + MAXDIRS)
263cdf0e10cSrcweir 			    fatalerr("Too many -I flags.\n");
264cdf0e10cSrcweir 			*incp++ = argv[0]+2;
265cdf0e10cSrcweir 			if (**(incp-1) == '\0') {
266cdf0e10cSrcweir 				*(incp-1) = *(++argv);
267cdf0e10cSrcweir 				argc--;
268cdf0e10cSrcweir 			}
269cdf0e10cSrcweir 			break;
270cdf0e10cSrcweir 		case 'Y':
271cdf0e10cSrcweir 			defincdir = argv[0]+2;
272cdf0e10cSrcweir 			break;
273cdf0e10cSrcweir 		/* do not use if endmarker processing */
274cdf0e10cSrcweir 		case 'a':
275cdf0e10cSrcweir 			if (endmarker) break;
276cdf0e10cSrcweir 			append = TRUE;
277cdf0e10cSrcweir 			break;
278cdf0e10cSrcweir 		case 'w':
279cdf0e10cSrcweir 			if (endmarker) break;
280cdf0e10cSrcweir 			if (argv[0][2] == '\0') {
281cdf0e10cSrcweir 				argv++;
282cdf0e10cSrcweir 				argc--;
283cdf0e10cSrcweir 				width = atoi(argv[0]);
284cdf0e10cSrcweir 			} else
285cdf0e10cSrcweir 				width = atoi(argv[0]+2);
286cdf0e10cSrcweir 			break;
287cdf0e10cSrcweir 		case 'n':
288cdf0e10cSrcweir             // Use "-n" switch to generate dependencies with windows-native slash style
289cdf0e10cSrcweir             native_win_slashes = TRUE;
290cdf0e10cSrcweir 			break;
291cdf0e10cSrcweir 		case 'o':
292cdf0e10cSrcweir 			if (endmarker) break;
293cdf0e10cSrcweir 			if (argv[0][2] == '\0') {
294cdf0e10cSrcweir 				argv++;
295cdf0e10cSrcweir 				argc--;
296cdf0e10cSrcweir 				objsuffix = argv[0];
297cdf0e10cSrcweir 			} else
298cdf0e10cSrcweir 				objsuffix = argv[0]+2;
299cdf0e10cSrcweir 			break;
300cdf0e10cSrcweir 		case 'p':
301cdf0e10cSrcweir 			if (endmarker) break;
302cdf0e10cSrcweir 			if (argv[0][2] == '\0') {
303cdf0e10cSrcweir 				argv++;
304cdf0e10cSrcweir 				argc--;
305cdf0e10cSrcweir 				objprefix = argv[0];
306cdf0e10cSrcweir 			} else
307cdf0e10cSrcweir 				objprefix = argv[0]+2;
308cdf0e10cSrcweir 			break;
309cdf0e10cSrcweir 		case 'v':
310cdf0e10cSrcweir 			if (endmarker) break;
311cdf0e10cSrcweir 			verbose = TRUE;
312cdf0e10cSrcweir #ifdef DEBUG
313cdf0e10cSrcweir 			if (argv[0][2])
314cdf0e10cSrcweir 				_debugmask = atoi(argv[0]+2);
315cdf0e10cSrcweir #endif
316cdf0e10cSrcweir 			break;
317cdf0e10cSrcweir 		case 's':
318cdf0e10cSrcweir 			if (endmarker) break;
319cdf0e10cSrcweir 			startat = argv[0]+2;
320cdf0e10cSrcweir 			if (*startat == '\0') {
321cdf0e10cSrcweir 				startat = *(++argv);
322cdf0e10cSrcweir 				argc--;
323cdf0e10cSrcweir 			}
324cdf0e10cSrcweir 			if (*startat != '#')
325cdf0e10cSrcweir 				fatalerr("-s flag's value should start %s\n",
326cdf0e10cSrcweir 					"with '#'.");
327cdf0e10cSrcweir 			break;
328cdf0e10cSrcweir 		case 'f':
329cdf0e10cSrcweir 			if (endmarker) break;
330cdf0e10cSrcweir 			makefile = argv[0]+2;
331cdf0e10cSrcweir 			if (*makefile == '\0') {
332cdf0e10cSrcweir 				makefile = *(++argv);
333cdf0e10cSrcweir 				argc--;
334cdf0e10cSrcweir 			}
335cdf0e10cSrcweir 			break;
336cdf0e10cSrcweir 
337cdf0e10cSrcweir 		case 'm':
338cdf0e10cSrcweir 			warn_multiple = TRUE;
339cdf0e10cSrcweir 			break;
340cdf0e10cSrcweir 
341cdf0e10cSrcweir 		/* Ignore -O, -g so we can just pass ${CFLAGS} to
342cdf0e10cSrcweir 		   makedepend
343cdf0e10cSrcweir 		 */
344cdf0e10cSrcweir 		case 'O':
345cdf0e10cSrcweir 		case 'g':
346cdf0e10cSrcweir 			break;
347cdf0e10cSrcweir 		default:
348cdf0e10cSrcweir 			if (endmarker) break;
349cdf0e10cSrcweir 	/*		fatalerr("unknown opt = %s\n", argv[0]); */
350cdf0e10cSrcweir 			warning("ignoring option %s\n", argv[0]);
351cdf0e10cSrcweir 		}
352cdf0e10cSrcweir 	}
353cdf0e10cSrcweir 
354cdf0e10cSrcweir     convert_slashes(objprefix);
355cdf0e10cSrcweir     objprefix = append_slash(objprefix);
356cdf0e10cSrcweir 
357cdf0e10cSrcweir     if (!defincdir) {
358cdf0e10cSrcweir #ifdef PREINCDIR
359cdf0e10cSrcweir 	    if (incp >= includedirs + MAXDIRS)
360cdf0e10cSrcweir 		fatalerr("Too many -I flags.\n");
361cdf0e10cSrcweir 	    *incp++ = PREINCDIR;
362cdf0e10cSrcweir #endif
363cdf0e10cSrcweir 	    if (incp >= includedirs + MAXDIRS)
364cdf0e10cSrcweir 		fatalerr("Too many -I flags.\n");
365cdf0e10cSrcweir 	    *incp++ = INCLUDEDIR;
366cdf0e10cSrcweir #ifdef POSTINCDIR
367cdf0e10cSrcweir 	    if (incp >= includedirs + MAXDIRS)
368cdf0e10cSrcweir 		fatalerr("Too many -I flags.\n");
369cdf0e10cSrcweir 	    *incp++ = POSTINCDIR;
370cdf0e10cSrcweir #endif
371cdf0e10cSrcweir 	} else if (*defincdir) {
372cdf0e10cSrcweir 	    if (incp >= includedirs + MAXDIRS)
373cdf0e10cSrcweir 		fatalerr("Too many -I flags.\n");
374cdf0e10cSrcweir 	    *incp++ = defincdir;
375cdf0e10cSrcweir 	}
376cdf0e10cSrcweir 
377cdf0e10cSrcweir 	redirect(startat, makefile);
378cdf0e10cSrcweir 
379cdf0e10cSrcweir 	/*
380cdf0e10cSrcweir 	 * catch signals.
381cdf0e10cSrcweir 	 */
382cdf0e10cSrcweir #ifdef USGISH
383cdf0e10cSrcweir /*  should really reset SIGINT to SIG_IGN if it was.  */
384cdf0e10cSrcweir #ifdef SIGHUP
385cdf0e10cSrcweir 	signal (SIGHUP, catch);
386cdf0e10cSrcweir #endif
387cdf0e10cSrcweir 	signal (SIGINT, catch);
388cdf0e10cSrcweir #ifdef SIGQUIT
389cdf0e10cSrcweir 	signal (SIGQUIT, catch);
390cdf0e10cSrcweir #endif
391cdf0e10cSrcweir 	signal (SIGILL, catch);
392cdf0e10cSrcweir #ifdef SIGBUS
393cdf0e10cSrcweir 	signal (SIGBUS, catch);
394cdf0e10cSrcweir #endif
395cdf0e10cSrcweir 	signal (SIGSEGV, catch);
396cdf0e10cSrcweir #ifdef SIGSYS
397cdf0e10cSrcweir 	signal (SIGSYS, catch);
398cdf0e10cSrcweir #endif
399cdf0e10cSrcweir 	signal (SIGFPE, catch);
400cdf0e10cSrcweir #else
401cdf0e10cSrcweir 	sig_act.sa_handler = catch;
402cdf0e10cSrcweir #ifdef _POSIX_SOURCE
403cdf0e10cSrcweir 	sigemptyset(&sig_act.sa_mask);
404cdf0e10cSrcweir 	sigaddset(&sig_act.sa_mask, SIGINT);
405cdf0e10cSrcweir 	sigaddset(&sig_act.sa_mask, SIGQUIT);
406cdf0e10cSrcweir #ifdef SIGBUS
407cdf0e10cSrcweir 	sigaddset(&sig_act.sa_mask, SIGBUS);
408cdf0e10cSrcweir #endif
409cdf0e10cSrcweir 	sigaddset(&sig_act.sa_mask, SIGILL);
410cdf0e10cSrcweir 	sigaddset(&sig_act.sa_mask, SIGSEGV);
411cdf0e10cSrcweir 	sigaddset(&sig_act.sa_mask, SIGHUP);
412cdf0e10cSrcweir 	sigaddset(&sig_act.sa_mask, SIGPIPE);
413cdf0e10cSrcweir #ifdef SIGSYS
414cdf0e10cSrcweir 	sigaddset(&sig_act.sa_mask, SIGSYS);
415cdf0e10cSrcweir #endif
416cdf0e10cSrcweir #else
417cdf0e10cSrcweir 	sig_act.sa_mask = ((1<<(SIGINT -1))
418cdf0e10cSrcweir 			   |(1<<(SIGQUIT-1))
419cdf0e10cSrcweir #ifdef SIGBUS
420cdf0e10cSrcweir 			   |(1<<(SIGBUS-1))
421cdf0e10cSrcweir #endif
422cdf0e10cSrcweir 			   |(1<<(SIGILL-1))
423cdf0e10cSrcweir 			   |(1<<(SIGSEGV-1))
424cdf0e10cSrcweir 			   |(1<<(SIGHUP-1))
425cdf0e10cSrcweir 			   |(1<<(SIGPIPE-1))
426cdf0e10cSrcweir #ifdef SIGSYS
427cdf0e10cSrcweir 			   |(1<<(SIGSYS-1))
428cdf0e10cSrcweir #endif
429cdf0e10cSrcweir 			   );
430cdf0e10cSrcweir #endif /* _POSIX_SOURCE */
431cdf0e10cSrcweir 	sig_act.sa_flags = 0;
432cdf0e10cSrcweir 	sigaction(SIGHUP, &sig_act, (struct sigaction *)0);
433cdf0e10cSrcweir 	sigaction(SIGINT, &sig_act, (struct sigaction *)0);
434cdf0e10cSrcweir 	sigaction(SIGQUIT, &sig_act, (struct sigaction *)0);
435cdf0e10cSrcweir 	sigaction(SIGILL, &sig_act, (struct sigaction *)0);
436cdf0e10cSrcweir #ifdef SIGBUS
437cdf0e10cSrcweir 	sigaction(SIGBUS, &sig_act, (struct sigaction *)0);
438cdf0e10cSrcweir #endif
439cdf0e10cSrcweir 	sigaction(SIGSEGV, &sig_act, (struct sigaction *)0);
440cdf0e10cSrcweir #ifdef SIGSYS
441cdf0e10cSrcweir 	sigaction(SIGSYS, &sig_act, (struct sigaction *)0);
442cdf0e10cSrcweir #endif
443cdf0e10cSrcweir #endif /* USGISH */
444cdf0e10cSrcweir 
445cdf0e10cSrcweir 	/*
446cdf0e10cSrcweir 	 * now peruse through the list of files.
447cdf0e10cSrcweir 	 */
448cdf0e10cSrcweir     incCollection = create_IncludesCollection();
449cdf0e10cSrcweir 
450cdf0e10cSrcweir     for(fp=filelist; *fp; fp++) {
451cdf0e10cSrcweir 		struct symhash *includes;
452cdf0e10cSrcweir 		filecontent = getfile(*fp);
453cdf0e10cSrcweir 		ip = newinclude(*fp, (char *)NULL);
454cdf0e10cSrcweir 
455cdf0e10cSrcweir 		includes = hash_copy( maininclist );
456cdf0e10cSrcweir 		find_includes(filecontent, ip, ip, 0, FALSE, incCollection, includes);
457cdf0e10cSrcweir 		hash_free( includes );
458cdf0e10cSrcweir 
459cdf0e10cSrcweir 		freefile(filecontent);
460cdf0e10cSrcweir 		recursive_pr_include(ip, ip->i_file, base_name(*fp));
461cdf0e10cSrcweir 		inc_clean();
462cdf0e10cSrcweir 	}
463cdf0e10cSrcweir 	if (printed)
464cdf0e10cSrcweir 		printf("\n");
465cdf0e10cSrcweir 
466cdf0e10cSrcweir 	delete_IncludesCollection(incCollection);
467cdf0e10cSrcweir 
468cdf0e10cSrcweir 	exit(0);
469cdf0e10cSrcweir }
470cdf0e10cSrcweir 
getfile(file)471cdf0e10cSrcweir struct filepointer *getfile(file)
472cdf0e10cSrcweir 	char	*file;
473cdf0e10cSrcweir {
474cdf0e10cSrcweir 	register int	fd;
475cdf0e10cSrcweir 	struct filepointer	*content;
476cdf0e10cSrcweir 	struct stat	st;
477cdf0e10cSrcweir 	off_t		size_backup;
478cdf0e10cSrcweir 	ssize_t		bytes_read;
479cdf0e10cSrcweir 	size_t		malloc_size;
480cdf0e10cSrcweir 
481cdf0e10cSrcweir 	content = (struct filepointer *)malloc(sizeof(struct filepointer));
482cdf0e10cSrcweir 	if ((fd = open(file, O_RDONLY)) < 0) {
483cdf0e10cSrcweir 		warning("makedepend:  Cannot open file \"%s\"\n", file);
484cdf0e10cSrcweir 		content->f_p = content->f_base = content->f_end = (char *)malloc(1);
485cdf0e10cSrcweir 		*content->f_p = '\0';
486cdf0e10cSrcweir 		return(content);
487cdf0e10cSrcweir 	}
488cdf0e10cSrcweir 	fstat(fd, &st);
489cdf0e10cSrcweir 
490cdf0e10cSrcweir 	size_backup = st.st_size;
491cdf0e10cSrcweir 	malloc_size = size_backup;
492cdf0e10cSrcweir 	/* Since off_t is larger than size_t, need to test for
493cdf0e10cSrcweir 	 * truncation.
494cdf0e10cSrcweir 	 */
495cdf0e10cSrcweir 	if ( (off_t)malloc_size != size_backup )
496cdf0e10cSrcweir 	{
497cdf0e10cSrcweir 		close( fd );
498cdf0e10cSrcweir 		warning("makedepend:  File \"%s\" size larger than can fit in size_t.  Cannot allocate memory for contents.\n", file);
499cdf0e10cSrcweir 		content->f_p = content->f_base = content->f_end = (char *)malloc(1);
500cdf0e10cSrcweir 		*content->f_p = '\0';
501cdf0e10cSrcweir 		return(content);
502cdf0e10cSrcweir 	}
503cdf0e10cSrcweir 
504cdf0e10cSrcweir 	content->f_base = (char *)malloc(malloc_size+1);
505cdf0e10cSrcweir 	if (content->f_base == NULL)
506cdf0e10cSrcweir 		fatalerr("makedepend:  Cannot allocate memory to process file \"%s\"\n", file);
507cdf0e10cSrcweir 	if ((bytes_read = read(fd, content->f_base, malloc_size)) < 0)
508cdf0e10cSrcweir 		if ( st.st_mode & S_IFREG )
509cdf0e10cSrcweir 			fatalerr("makedepend:  Failed to read file \"%s\"\n", file);
510cdf0e10cSrcweir 
511cdf0e10cSrcweir 	close(fd);
512cdf0e10cSrcweir 	content->f_len = bytes_read+1;
513cdf0e10cSrcweir 	content->f_p = content->f_base;
514cdf0e10cSrcweir 	content->f_end = content->f_base + bytes_read;
515cdf0e10cSrcweir 	*content->f_end = '\0';
516cdf0e10cSrcweir 	content->f_line = 0;
517cdf0e10cSrcweir 	return(content);
518cdf0e10cSrcweir }
519cdf0e10cSrcweir 
freefile(fp)520cdf0e10cSrcweir void freefile(fp)
521cdf0e10cSrcweir 	struct filepointer	*fp;
522cdf0e10cSrcweir {
523cdf0e10cSrcweir 	free(fp->f_base);
524cdf0e10cSrcweir 	free(fp);
525cdf0e10cSrcweir }
526cdf0e10cSrcweir 
copy(str)527cdf0e10cSrcweir char *copy(str)
528cdf0e10cSrcweir 	register char	*str;
529cdf0e10cSrcweir {
530cdf0e10cSrcweir 	register char	*p = (char *)malloc(strlen(str) + 1);
531cdf0e10cSrcweir 
532cdf0e10cSrcweir 	strcpy(p, str);
533cdf0e10cSrcweir 	return(p);
534cdf0e10cSrcweir }
535cdf0e10cSrcweir 
match(str,list)536cdf0e10cSrcweir int match(str, list)
537cdf0e10cSrcweir 	register char	*str, **list;
538cdf0e10cSrcweir {
539cdf0e10cSrcweir 	register int	i;
540cdf0e10cSrcweir 
541cdf0e10cSrcweir 	for (i=0; *list; i++, list++)
542cdf0e10cSrcweir 		if (strcmp(str, *list) == 0)
543cdf0e10cSrcweir 			return(i);
544cdf0e10cSrcweir 	return(-1);
545cdf0e10cSrcweir }
546cdf0e10cSrcweir 
547cdf0e10cSrcweir /*
548cdf0e10cSrcweir  * Get the next line.  We only return lines beginning with '#' since that
549cdf0e10cSrcweir  * is all this program is ever interested in.
550cdf0e10cSrcweir  */
get_line(filep)551cdf0e10cSrcweir char *get_line(filep)
552cdf0e10cSrcweir 	register struct filepointer	*filep;
553cdf0e10cSrcweir {
554cdf0e10cSrcweir 	register char	*p,	/* walking pointer */
555cdf0e10cSrcweir 			*eof,	/* end of file pointer */
556cdf0e10cSrcweir 			*bol;	/* beginning of line pointer */
557cdf0e10cSrcweir 	register int	lineno;	/* line number */
558cdf0e10cSrcweir 
559cdf0e10cSrcweir 	p = filep->f_p;
560cdf0e10cSrcweir 	eof = filep->f_end;
561cdf0e10cSrcweir 	if (p >= eof)
562cdf0e10cSrcweir 		return((char *)NULL);
563cdf0e10cSrcweir 	lineno = filep->f_line;
564cdf0e10cSrcweir 
565cdf0e10cSrcweir 	for(bol = p--; ++p < eof; ) {
566cdf0e10cSrcweir 		if (*p == '/' && *(p+1) == '*') { /* consume comments */
567cdf0e10cSrcweir 			*p++ = ' ', *p++ = ' ';
568cdf0e10cSrcweir 			while (*p) {
569cdf0e10cSrcweir 				if (*p == '*' && *(p+1) == '/') {
570cdf0e10cSrcweir 					*p++ = ' ', *p = ' ';
571cdf0e10cSrcweir 					break;
572cdf0e10cSrcweir 				}
573cdf0e10cSrcweir 				else if (*p == '\n')
574cdf0e10cSrcweir 					lineno++;
575cdf0e10cSrcweir 				*p++ = ' ';
576cdf0e10cSrcweir 			}
577cdf0e10cSrcweir 			continue;
578cdf0e10cSrcweir 		}
579cdf0e10cSrcweir 		else if (*p == '/' && *(p+1) == '/') { /* consume comments */
580cdf0e10cSrcweir 			*p++ = ' ', *p++ = ' ';
581cdf0e10cSrcweir 			while (*p && *p != '\n')
582cdf0e10cSrcweir 				*p++ = ' ';
583cdf0e10cSrcweir 			if ( *p == '\n' )
584cdf0e10cSrcweir 				p--;
585cdf0e10cSrcweir 			lineno++;
586cdf0e10cSrcweir 			continue;
587cdf0e10cSrcweir 		}
588cdf0e10cSrcweir 		else if (*p == '\\') {
589cdf0e10cSrcweir 			if (*(p+1) == '\n') {
590cdf0e10cSrcweir 				*p = ' ';
591cdf0e10cSrcweir 				*(p+1) = ' ';
592cdf0e10cSrcweir 				lineno++;
593cdf0e10cSrcweir 			}
594cdf0e10cSrcweir 		}
595cdf0e10cSrcweir 		else if (*p == '\n') {
596cdf0e10cSrcweir 			lineno++;
597cdf0e10cSrcweir 			if (*bol == '#') {
598cdf0e10cSrcweir 				register char *cp;
599cdf0e10cSrcweir 
600cdf0e10cSrcweir 				*p++ = '\0';
601cdf0e10cSrcweir 				/* punt lines with just # (yacc generated) */
602cdf0e10cSrcweir 				for (cp = bol+1;
603cdf0e10cSrcweir 				     *cp && (*cp == ' ' || *cp == '\t'); cp++);
604cdf0e10cSrcweir 				if (*cp) goto done;
605cdf0e10cSrcweir 			}
606cdf0e10cSrcweir 			bol = p+1;
607cdf0e10cSrcweir 		}
608cdf0e10cSrcweir 	}
609cdf0e10cSrcweir 	if (*bol != '#')
610cdf0e10cSrcweir 		bol = NULL;
611cdf0e10cSrcweir done:
612cdf0e10cSrcweir 	filep->f_p = p;
613cdf0e10cSrcweir 	filep->f_line = lineno;
614cdf0e10cSrcweir 	return(bol);
615cdf0e10cSrcweir }
616cdf0e10cSrcweir 
617cdf0e10cSrcweir /*
618cdf0e10cSrcweir  * Strip the file name down to what we want to see in the Makefile.
619cdf0e10cSrcweir  * It will have objprefix and objsuffix around it.
620cdf0e10cSrcweir  */
base_name(file)621cdf0e10cSrcweir char *base_name(file)
622cdf0e10cSrcweir 	register char	*file;
623cdf0e10cSrcweir {
624cdf0e10cSrcweir 	register char	*p;
625cdf0e10cSrcweir 
626cdf0e10cSrcweir 	file = copy(file);
627cdf0e10cSrcweir 	for(p=file+strlen(file); p>file && *p != '.'; p--) ;
628cdf0e10cSrcweir 
629cdf0e10cSrcweir 	if (*p == '.')
630cdf0e10cSrcweir 		*p = '\0';
631cdf0e10cSrcweir 
632cdf0e10cSrcweir     while (p > file) {
633cdf0e10cSrcweir         if ( *p == '/' ||  *p == '\\') {
634cdf0e10cSrcweir             file = p + 1;
635cdf0e10cSrcweir             break;
63626d9e9ddSpfg         }
637cdf0e10cSrcweir         p--;
63826d9e9ddSpfg     }
639cdf0e10cSrcweir 	return(file);
640cdf0e10cSrcweir }
641cdf0e10cSrcweir 
642cdf0e10cSrcweir #if defined(USG) && !defined(CRAY) && !defined(SVR4)
rename(from,to)643cdf0e10cSrcweir int rename (from, to)
644cdf0e10cSrcweir     char *from, *to;
645cdf0e10cSrcweir {
646cdf0e10cSrcweir     (void) unlink (to);
647cdf0e10cSrcweir     if (link (from, to) == 0) {
648cdf0e10cSrcweir 	unlink (from);
649cdf0e10cSrcweir 	return 0;
650cdf0e10cSrcweir     } else {
651cdf0e10cSrcweir 	return -1;
652cdf0e10cSrcweir     }
653cdf0e10cSrcweir }
654cdf0e10cSrcweir #endif /* USGISH */
655cdf0e10cSrcweir 
redirect(line,makefile)656cdf0e10cSrcweir void redirect(line, makefile)
657cdf0e10cSrcweir 	char	*line,
658cdf0e10cSrcweir 		*makefile;
659cdf0e10cSrcweir {
660cdf0e10cSrcweir 	struct stat	st;
661cdf0e10cSrcweir 	FILE	*fdin, *fdout;
662*c56f8ff5SJim Jagielski 	char	backup[ OURBUFSIZ ],
663*c56f8ff5SJim Jagielski 		buf[ OURBUFSIZ ];
664cdf0e10cSrcweir 	boolean	found = FALSE;
665cdf0e10cSrcweir 	int	len;
666cdf0e10cSrcweir 
667cdf0e10cSrcweir 	/*
668cdf0e10cSrcweir 	 * if makefile is "-" then let it pour onto stdout.
669cdf0e10cSrcweir 	 */
670cdf0e10cSrcweir 	if (makefile && *makefile == '-' && *(makefile+1) == '\0')
671cdf0e10cSrcweir 		return;
672cdf0e10cSrcweir 
673cdf0e10cSrcweir 	/*
674cdf0e10cSrcweir 	 * use a default makefile is not specified.
675cdf0e10cSrcweir 	 */
676cdf0e10cSrcweir 	if (!makefile) {
677cdf0e10cSrcweir 		if (stat("Makefile", &st) == 0)
678cdf0e10cSrcweir 			makefile = "Makefile";
679cdf0e10cSrcweir 		else if (stat("makefile", &st) == 0)
680cdf0e10cSrcweir 			makefile = "makefile";
681cdf0e10cSrcweir 		else
682cdf0e10cSrcweir 			fatalerr("[mM]akefile is not present\n");
683cdf0e10cSrcweir 	}
684cdf0e10cSrcweir 	else
685cdf0e10cSrcweir 	    stat(makefile, &st);
686cdf0e10cSrcweir 	if ((fdin = fopen(makefile, "r")) == NULL)
687cdf0e10cSrcweir 		fatalerr("cannot open \"%s\"\n", makefile);
688cdf0e10cSrcweir 	sprintf(backup, "%s.bak", makefile);
689cdf0e10cSrcweir 	unlink(backup);
690cdf0e10cSrcweir #if defined(WIN32) || defined(OS2)
691cdf0e10cSrcweir 	fclose(fdin);
692cdf0e10cSrcweir #endif
693cdf0e10cSrcweir 	if (rename(makefile, backup) < 0)
694cdf0e10cSrcweir 		fatalerr("cannot rename %s to %s\n", makefile, backup);
695cdf0e10cSrcweir #if defined(WIN32) || defined(OS2)
696cdf0e10cSrcweir 	if ((fdin = fopen(backup, "r")) == NULL)
697cdf0e10cSrcweir 		fatalerr("cannot open \"%s\"\n", backup);
698cdf0e10cSrcweir #endif
699cdf0e10cSrcweir 	if ((fdout = freopen(makefile, "w", stdout)) == NULL)
700cdf0e10cSrcweir 		fatalerr("cannot open \"%s\"\n", backup);
701cdf0e10cSrcweir 	len = strlen(line);
702*c56f8ff5SJim Jagielski 	while (!found && fgets(buf, OURBUFSIZ, fdin)) {
703cdf0e10cSrcweir 		if (*buf == '#' && strncmp(line, buf, len) == 0)
704cdf0e10cSrcweir 			found = TRUE;
705cdf0e10cSrcweir 		fputs(buf, fdout);
706cdf0e10cSrcweir 	}
707cdf0e10cSrcweir 	if (!found) {
708cdf0e10cSrcweir 		if (verbose)
709cdf0e10cSrcweir 		warning("Adding new delimiting line \"%s\" and dependencies...\n",
710cdf0e10cSrcweir 			line);
711cdf0e10cSrcweir 		puts(line); /* same as fputs(fdout); but with newline */
712cdf0e10cSrcweir 	} else if (append) {
713*c56f8ff5SJim Jagielski 	    while (fgets(buf, OURBUFSIZ, fdin)) {
714cdf0e10cSrcweir 		fputs(buf, fdout);
715cdf0e10cSrcweir 	    }
716cdf0e10cSrcweir 	}
717cdf0e10cSrcweir 	fflush(fdout);
718cdf0e10cSrcweir #if defined(USGISH) || defined(_SEQUENT_) || defined(USE_CHMOD)
719cdf0e10cSrcweir 	chmod(makefile, st.st_mode);
720cdf0e10cSrcweir #else
721cdf0e10cSrcweir 	fchmod(fileno(fdout), st.st_mode);
722cdf0e10cSrcweir #endif /* USGISH */
723cdf0e10cSrcweir 	fclose(fdin);
724cdf0e10cSrcweir }
725cdf0e10cSrcweir 
fatalerr(char * msg,...)726cdf0e10cSrcweir void fatalerr(char *msg, ...)
727cdf0e10cSrcweir {
728cdf0e10cSrcweir 	va_list args;
729cdf0e10cSrcweir 	fprintf(stderr, "%s: error:  ", ProgramName);
730cdf0e10cSrcweir 	va_start(args, msg);
731cdf0e10cSrcweir 	vfprintf(stderr, msg, args);
732cdf0e10cSrcweir 	va_end(args);
733cdf0e10cSrcweir 	exit (1);
734cdf0e10cSrcweir }
735cdf0e10cSrcweir 
warning(char * msg,...)736cdf0e10cSrcweir void warning(char *msg, ...)
737cdf0e10cSrcweir {
738cdf0e10cSrcweir #ifdef DEBUG_MKDEPEND
739cdf0e10cSrcweir 	va_list args;
740cdf0e10cSrcweir 	fprintf(stderr, "%s: warning:  ", ProgramName);
741cdf0e10cSrcweir 	va_start(args, msg);
742cdf0e10cSrcweir 	vfprintf(stderr, msg, args);
743cdf0e10cSrcweir 	va_end(args);
744cdf0e10cSrcweir #else
745cdf0e10cSrcweir 	(void)msg;
746cdf0e10cSrcweir #endif /* DEBUG_MKDEPEND */
747cdf0e10cSrcweir }
748cdf0e10cSrcweir 
warning1(char * msg,...)749cdf0e10cSrcweir void warning1(char *msg, ...)
750cdf0e10cSrcweir {
751cdf0e10cSrcweir #ifdef DEBUG_MKDEPEND
752cdf0e10cSrcweir 	va_list args;
753cdf0e10cSrcweir 	va_start(args, msg);
754cdf0e10cSrcweir 	vfprintf(stderr, msg, args);
755cdf0e10cSrcweir 	va_end(args);
756cdf0e10cSrcweir #else
757cdf0e10cSrcweir 	(void)msg;
758cdf0e10cSrcweir #endif /* DEBUG_MKDEPEND */
759cdf0e10cSrcweir }
760cdf0e10cSrcweir 
convert_slashes(path)761cdf0e10cSrcweir void convert_slashes(path)
762cdf0e10cSrcweir     char* path;
763cdf0e10cSrcweir {
764cdf0e10cSrcweir #if defined (WNT) || defined(OS2)
765cdf0e10cSrcweir 	/*
766cdf0e10cSrcweir 	 * Convert backslashes to slashes
767cdf0e10cSrcweir 	 */
768cdf0e10cSrcweir     char *ptr;
769cdf0e10cSrcweir     if (native_win_slashes) {
770cdf0e10cSrcweir         for (ptr = (char*)path; *ptr; ++ptr)
771cdf0e10cSrcweir             if (*ptr == '/')
772cdf0e10cSrcweir                 *ptr = '\\';
773cdf0e10cSrcweir     } else {
774cdf0e10cSrcweir         for (ptr = (char*)path; *ptr; ++ptr)
775cdf0e10cSrcweir             if (*ptr == '\\')
776cdf0e10cSrcweir                 *ptr = '/';
77726d9e9ddSpfg     }
778cdf0e10cSrcweir #else
779cdf0e10cSrcweir     (void)path;
780cdf0e10cSrcweir #endif
781cdf0e10cSrcweir }
782cdf0e10cSrcweir 
append_slash(path)783cdf0e10cSrcweir char* append_slash(path)
784cdf0e10cSrcweir     char* path;
785cdf0e10cSrcweir {
786cdf0e10cSrcweir     char *new_string;
787cdf0e10cSrcweir     if ((path[strlen(path) - 1] == '/') || (path[strlen(path) - 1] == '\\')) {
788cdf0e10cSrcweir         new_string = path;
789cdf0e10cSrcweir     } else {
790cdf0e10cSrcweir         new_string = (char*)malloc(sizeof(char) * (strlen(path) + 2));
791cdf0e10cSrcweir         strcpy(new_string, path);
792cdf0e10cSrcweir         if (native_win_slashes)
793cdf0e10cSrcweir             strcat(new_string, "\\");
794cdf0e10cSrcweir         else
795cdf0e10cSrcweir             strcat(new_string, "/");
79626d9e9ddSpfg     }
797cdf0e10cSrcweir     return new_string;
798cdf0e10cSrcweir }
799cdf0e10cSrcweir 
800