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