PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/js/config/mkdepend/main.c

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