security/nss/coreconf/mkdepend/main.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial