fixfixes.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /*
  2. Test to see if a particular fix should be applied to a header file.
  3. Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2009
  4. Free Software Foundation, Inc.
  5. = = = = = = = = = = = = = = = = = = = = = = = = =
  6. NOTE TO DEVELOPERS
  7. The routines you write here must work closely with fixincl.c.
  8. Here are the rules:
  9. 1. Every test procedure name must be suffixed with "_fix".
  10. These routines will be referenced from inclhack.def, sans the suffix.
  11. 2. Use the "FIX_PROC_HEAD()" macro _with_ the "_fix" suffix
  12. (I cannot use the ## magic from ANSI C) for defining your entry point.
  13. 3. Put your test name into the FIXUP_TABLE.
  14. 4. Do not read anything from stdin. It is closed.
  15. 5. Write to stderr only in the event of a reportable error
  16. In such an event, call "exit (EXIT_FAILURE)".
  17. 6. You have access to the fixDescList entry for the fix in question.
  18. This may be useful, for example, if there are interesting strings
  19. or pre-compiled regular expressions stored there.
  20. = = = = = = = = = = = = = = = = = = = = = = = = =
  21. This file is part of GCC.
  22. GCC is free software; you can redistribute it and/or modify
  23. it under the terms of the GNU General Public License as published by
  24. the Free Software Foundation; either version 3, or (at your option)
  25. any later version.
  26. GCC is distributed in the hope that it will be useful,
  27. but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. GNU General Public License for more details.
  30. You should have received a copy of the GNU General Public License
  31. along with GCC; see the file COPYING3. If not see
  32. <http://www.gnu.org/licenses/>. */
  33. #include "fixlib.h"
  34. #define GTYPE_SE_CT 1
  35. #ifdef SEPARATE_FIX_PROC
  36. #include "fixincl.x"
  37. #endif
  38. tSCC zNeedsArg[] = "fixincl error: `%s' needs %s argument (c_fix_arg[%d])\n";
  39. typedef void t_fix_proc (const char *, const char *, tFixDesc *) ;
  40. typedef struct {
  41. const char* fix_name;
  42. t_fix_proc* fix_proc;
  43. } fix_entry_t;
  44. #define FIXUP_TABLE \
  45. _FT_( "char_macro_def", char_macro_def_fix ) \
  46. _FT_( "char_macro_use", char_macro_use_fix ) \
  47. _FT_( "format", format_fix ) \
  48. _FT_( "machine_name", machine_name_fix ) \
  49. _FT_( "wrap", wrap_fix ) \
  50. _FT_( "gnu_type", gnu_type_fix )
  51. #define FIX_PROC_HEAD( fix ) \
  52. static void fix (const char* filname ATTRIBUTE_UNUSED , \
  53. const char* text ATTRIBUTE_UNUSED , \
  54. tFixDesc* p_fixd ATTRIBUTE_UNUSED )
  55. #ifdef NEED_PRINT_QUOTE
  56. /*
  57. * Skip over a quoted string. Single quote strings may
  58. * contain multiple characters if the first character is
  59. * a backslash. Especially a backslash followed by octal digits.
  60. * We are not doing a correctness syntax check here.
  61. */
  62. static char*
  63. print_quote(char q, char* text )
  64. {
  65. fputc( q, stdout );
  66. for (;;)
  67. {
  68. char ch = *(text++);
  69. fputc( ch, stdout );
  70. switch (ch)
  71. {
  72. case '\\':
  73. if (*text == NUL)
  74. goto quote_done;
  75. fputc( *(text++), stdout );
  76. break;
  77. case '"':
  78. case '\'':
  79. if (ch != q)
  80. break;
  81. /*FALLTHROUGH*/
  82. case '\n':
  83. case NUL:
  84. goto quote_done;
  85. }
  86. } quote_done:;
  87. return text;
  88. }
  89. #endif /* NEED_PRINT_QUOTE */
  90. /*
  91. * Emit the GNU standard type wrapped up in such a way that
  92. * this thing can be encountered countless times during a compile
  93. * and not cause even a warning.
  94. */
  95. static const char*
  96. emit_gnu_type (const char* text, regmatch_t* rm )
  97. {
  98. char z_TYPE[ 64 ];
  99. char z_type[ 64 ];
  100. fwrite (text, rm[0].rm_so, 1, stdout);
  101. {
  102. const char* ps = text + rm[1].rm_so;
  103. const char* pe = text + rm[1].rm_eo;
  104. char* pd = z_type;
  105. char* pD = z_TYPE;
  106. while (ps < pe)
  107. *(pD++) = TOUPPER( *(pd++) = *(ps++) );
  108. *pD = *pd = NUL;
  109. }
  110. /*
  111. * Now print out the reformed typedef,
  112. * with a C++ guard for WCHAR
  113. */
  114. {
  115. tSCC z_fmt[] = "\
  116. #if !defined(_GCC_%s_T)%s\n\
  117. #define _GCC_%s_T\n\
  118. typedef __%s_TYPE__ %s_t;\n\
  119. #endif\n";
  120. const char *const pz_guard = (strcmp (z_type, "wchar") == 0)
  121. ? " && ! defined(__cplusplus)" : "";
  122. printf (z_fmt, z_TYPE, pz_guard, z_TYPE, z_TYPE, z_type);
  123. }
  124. return text += rm[0].rm_eo;
  125. }
  126. /*
  127. * Copy the `format' string to std out, replacing `%n' expressions
  128. * with the matched text from a regular expression evaluation.
  129. * Doubled '%' characters will be replaced with a single copy.
  130. * '%' characters in other contexts and all other characters are
  131. * copied out verbatim.
  132. */
  133. static void
  134. format_write (tCC* format, tCC* text, regmatch_t av[] )
  135. {
  136. int c;
  137. while ((c = (unsigned)*(format++)) != NUL) {
  138. if (c != '%')
  139. {
  140. putchar(c);
  141. continue;
  142. }
  143. c = (unsigned)*(format++);
  144. /*
  145. * IF the character following a '%' is not a digit,
  146. * THEN we will always emit a '%' and we may or may
  147. * not emit the following character. We will end on
  148. * a NUL and we will emit only one of a pair of '%'.
  149. */
  150. if (! ISDIGIT ( c ))
  151. {
  152. putchar( '%' );
  153. switch (c) {
  154. case NUL:
  155. return;
  156. case '%':
  157. break;
  158. default:
  159. putchar(c);
  160. }
  161. }
  162. /*
  163. * Emit the matched subexpression numbered 'c'.
  164. * IF, of course, there was such a match...
  165. */
  166. else {
  167. regmatch_t* pRM = av + (c - (unsigned)'0');
  168. size_t len;
  169. if (pRM->rm_so < 0)
  170. continue;
  171. len = pRM->rm_eo - pRM->rm_so;
  172. if (len > 0)
  173. fwrite(text + pRM->rm_so, len, 1, stdout);
  174. }
  175. }
  176. }
  177. /*
  178. * Search for multiple copies of a regular expression. Each block
  179. * of matched text is replaced with the format string, as described
  180. * above in `format_write'.
  181. */
  182. FIX_PROC_HEAD( format_fix )
  183. {
  184. tCC* pz_pat = p_fixd->patch_args[2];
  185. tCC* pz_fmt = p_fixd->patch_args[1];
  186. regex_t re;
  187. regmatch_t rm[10];
  188. IGNORE_ARG(filname);
  189. /*
  190. * We must have a format
  191. */
  192. if (pz_fmt == (tCC*)NULL)
  193. {
  194. fprintf( stderr, zNeedsArg, p_fixd->fix_name, "replacement format", 0 );
  195. exit (EXIT_BROKEN);
  196. }
  197. /*
  198. * IF we don't have a search text, then go find the first
  199. * regular expression among the tests.
  200. */
  201. if (pz_pat == (tCC*)NULL)
  202. {
  203. tTestDesc* pTD = p_fixd->p_test_desc;
  204. int ct = p_fixd->test_ct;
  205. for (;;)
  206. {
  207. if (ct-- <= 0)
  208. {
  209. fprintf( stderr, zNeedsArg, p_fixd->fix_name, "search text", 1 );
  210. exit (EXIT_BROKEN);
  211. }
  212. if (pTD->type == TT_EGREP)
  213. {
  214. pz_pat = pTD->pz_test_text;
  215. break;
  216. }
  217. pTD++;
  218. }
  219. }
  220. /*
  221. * Replace every copy of the text we find
  222. */
  223. compile_re (pz_pat, &re, 1, "format search-text", "format_fix" );
  224. while (xregexec (&re, text, 10, rm, 0) == 0)
  225. {
  226. fwrite( text, rm[0].rm_so, 1, stdout );
  227. format_write( pz_fmt, text, rm );
  228. text += rm[0].rm_eo;
  229. }
  230. /*
  231. * Dump out the rest of the file
  232. */
  233. fputs (text, stdout);
  234. }
  235. /* Scan the input file for all occurrences of text like this:
  236. #define TIOCCONS _IO(T, 12)
  237. and change them to read like this:
  238. #define TIOCCONS _IO('T', 12)
  239. which is the required syntax per the C standard. (The definition of
  240. _IO also has to be tweaked - see below.) 'IO' is actually whatever you
  241. provide as the `c_fix_arg' argument. */
  242. FIX_PROC_HEAD( char_macro_use_fix )
  243. {
  244. /* This regexp looks for a traditional-syntax #define (# in column 1)
  245. of an object-like macro. */
  246. static const char pat[] =
  247. "^#[ \t]*define[ \t]+[_A-Za-z][_A-Za-z0-9]*[ \t]+";
  248. static regex_t re;
  249. const char* str = p_fixd->patch_args[1];
  250. regmatch_t rm[1];
  251. const char *p, *limit;
  252. size_t len;
  253. IGNORE_ARG(filname);
  254. if (str == NULL)
  255. {
  256. fprintf (stderr, zNeedsArg, p_fixd->fix_name, "ioctl type", 0);
  257. exit (EXIT_BROKEN);
  258. }
  259. len = strlen (str);
  260. compile_re (pat, &re, 1, "macro pattern", "char_macro_use_fix");
  261. for (p = text;
  262. xregexec (&re, p, 1, rm, 0) == 0;
  263. p = limit + 1)
  264. {
  265. /* p + rm[0].rm_eo is the first character of the macro replacement.
  266. Find the end of the macro replacement, and the STR we were
  267. sent to look for within the replacement. */
  268. p += rm[0].rm_eo;
  269. limit = p - 1;
  270. do
  271. {
  272. limit = strchr (limit + 1, '\n');
  273. if (!limit)
  274. goto done;
  275. }
  276. while (limit[-1] == '\\');
  277. do
  278. {
  279. if (*p == str[0] && !strncmp (p+1, str+1, len-1))
  280. goto found;
  281. }
  282. while (++p < limit - len);
  283. /* Hit end of line. */
  284. continue;
  285. found:
  286. /* Found STR on this line. If the macro needs fixing,
  287. the next few chars will be whitespace or uppercase,
  288. then an open paren, then a single letter. */
  289. while ((ISSPACE (*p) || ISUPPER (*p)) && p < limit) p++;
  290. if (*p++ != '(')
  291. continue;
  292. if (!ISALPHA (*p))
  293. continue;
  294. if (ISIDNUM (p[1]))
  295. continue;
  296. /* Splat all preceding text into the output buffer,
  297. quote the character at p, then proceed. */
  298. fwrite (text, 1, p - text, stdout);
  299. putchar ('\'');
  300. putchar (*p);
  301. putchar ('\'');
  302. text = p + 1;
  303. }
  304. done:
  305. fputs (text, stdout);
  306. }
  307. /* Scan the input file for all occurrences of text like this:
  308. #define xxxIOxx(x, y) (....'x'<<16....)
  309. and change them to read like this:
  310. #define xxxIOxx(x, y) (....x<<16....)
  311. which is the required syntax per the C standard. (The uses of _IO
  312. also has to be tweaked - see above.) 'IO' is actually whatever
  313. you provide as the `c_fix_arg' argument. */
  314. FIX_PROC_HEAD( char_macro_def_fix )
  315. {
  316. /* This regexp looks for any traditional-syntax #define (# in column 1). */
  317. static const char pat[] =
  318. "^#[ \t]*define[ \t]+";
  319. static regex_t re;
  320. const char* str = p_fixd->patch_args[1];
  321. regmatch_t rm[1];
  322. const char *p, *limit;
  323. char arg;
  324. size_t len;
  325. IGNORE_ARG(filname);
  326. if (str == NULL)
  327. {
  328. fprintf (stderr, zNeedsArg, p_fixd->fix_name, "ioctl type", 0);
  329. exit (EXIT_BROKEN);
  330. }
  331. len = strlen (str);
  332. compile_re (pat, &re, 1, "macro pattern", "fix_char_macro_defines");
  333. for (p = text;
  334. xregexec (&re, p, 1, rm, 0) == 0;
  335. p = limit + 1)
  336. {
  337. /* p + rm[0].rm_eo is the first character of the macro name.
  338. Find the end of the macro replacement, and the STR we were
  339. sent to look for within the name. */
  340. p += rm[0].rm_eo;
  341. limit = p - 1;
  342. do
  343. {
  344. limit = strchr (limit + 1, '\n');
  345. if (!limit)
  346. goto done;
  347. }
  348. while (limit[-1] == '\\');
  349. do
  350. {
  351. if (*p == str[0] && !strncmp (p+1, str+1, len-1))
  352. goto found;
  353. p++;
  354. }
  355. while (ISIDNUM (*p));
  356. /* Hit end of macro name without finding the string. */
  357. continue;
  358. found:
  359. /* Found STR in this macro name. If the macro needs fixing,
  360. there may be a few uppercase letters, then there will be an
  361. open paren with _no_ intervening whitespace, and then a
  362. single letter. */
  363. while (ISUPPER (*p) && p < limit) p++;
  364. if (*p++ != '(')
  365. continue;
  366. if (!ISALPHA (*p))
  367. continue;
  368. if (ISIDNUM (p[1]))
  369. continue;
  370. /* The character at P is the one to look for in the following
  371. text. */
  372. arg = *p;
  373. p += 2;
  374. while (p < limit)
  375. {
  376. if (p[-1] == '\'' && p[0] == arg && p[1] == '\'')
  377. {
  378. /* Remove the quotes from this use of ARG. */
  379. p--;
  380. fwrite (text, 1, p - text, stdout);
  381. putchar (arg);
  382. p += 3;
  383. text = p;
  384. }
  385. else
  386. p++;
  387. }
  388. }
  389. done:
  390. fputs (text, stdout);
  391. }
  392. /* Check if the pattern at pos is actually in a "__has_include(...)"
  393. directive. Return the pointer to the ')' of this
  394. "__has_include(...)" if it is, NULL otherwise. */
  395. static const char *
  396. check_has_inc (const char *begin, const char *pos, const char *end)
  397. {
  398. static const char has_inc[] = "__has_include";
  399. const size_t has_inc_len = sizeof (has_inc) - 1;
  400. const char *p;
  401. for (p = memmem (begin, pos - begin, has_inc, has_inc_len);
  402. p != NULL;
  403. p = memmem (p, pos - p, has_inc, has_inc_len))
  404. {
  405. p += has_inc_len;
  406. while (p < end && ISSPACE (*p))
  407. p++;
  408. /* "__has_include" may appear as "defined(__has_include)",
  409. search for the next appearance then. */
  410. if (*p != '(')
  411. continue;
  412. /* To avoid too much complexity, just hope there is never a
  413. ')' in a header name. */
  414. p = memchr (p, ')', end - p);
  415. if (p == NULL || p > pos)
  416. return p;
  417. }
  418. return NULL;
  419. }
  420. /* Fix for machine name #ifdefs that are not in the namespace reserved
  421. by the C standard. They won't be defined if compiling with -ansi,
  422. and the headers will break. We go to some trouble to only change
  423. #ifdefs where the macro is defined by GCC in non-ansi mode; this
  424. minimizes the number of headers touched. */
  425. #define SCRATCHSZ 64 /* hopefully long enough */
  426. FIX_PROC_HEAD( machine_name_fix )
  427. {
  428. regmatch_t match[2];
  429. const char *line, *base, *limit, *p, *q;
  430. regex_t *label_re, *name_re;
  431. char scratch[SCRATCHSZ];
  432. size_t len;
  433. IGNORE_ARG(filname);
  434. IGNORE_ARG(p_fixd);
  435. if (!mn_get_regexps (&label_re, &name_re, "machine_name_fix"))
  436. {
  437. fputs( "The target machine has no needed machine name fixes\n", stderr );
  438. goto done;
  439. }
  440. scratch[0] = '_';
  441. scratch[1] = '_';
  442. for (base = text;
  443. xregexec (label_re, base, 2, match, 0) == 0;
  444. base = limit)
  445. {
  446. base += match[0].rm_eo;
  447. /* We're looking at an #if or #ifdef. Scan forward for the
  448. next non-escaped newline. */
  449. line = limit = base;
  450. do
  451. {
  452. limit++;
  453. limit = strchr (limit, '\n');
  454. if (!limit)
  455. goto done;
  456. }
  457. while (limit[-1] == '\\');
  458. /* If the 'name_pat' matches in between base and limit, we have
  459. a bogon. It is not worth the hassle of excluding comments
  460. because comments on #if/#ifdef lines are rare, and strings on
  461. such lines are only legal in a "__has_include" directive.
  462. REG_NOTBOL means 'base' is not at the beginning of a line, which
  463. shouldn't matter since the name_re has no ^ anchor, but let's
  464. be accurate anyway. */
  465. for (;;)
  466. {
  467. again:
  468. if (base == limit)
  469. break;
  470. if (xregexec (name_re, base, 1, match, REG_NOTBOL))
  471. goto done; /* No remaining match in this file */
  472. /* Match; is it on the line? */
  473. if (match[0].rm_eo > limit - base)
  474. break;
  475. p = base + match[0].rm_so;
  476. /* Check if the match is in __has_include(...) (PR 91085). */
  477. q = check_has_inc (base, p, limit);
  478. if (q)
  479. {
  480. base = q + 1;
  481. goto again;
  482. }
  483. base += match[0].rm_eo;
  484. /* One more test: if on the same line we have the same string
  485. with the appropriate underscores, then leave it alone.
  486. We want exactly two leading and trailing underscores. */
  487. if (*p == '_')
  488. {
  489. len = base - p - ((*base == '_') ? 2 : 1);
  490. q = p + 1;
  491. }
  492. else
  493. {
  494. len = base - p - ((*base == '_') ? 1 : 0);
  495. q = p;
  496. }
  497. if (len + 4 > SCRATCHSZ)
  498. abort ();
  499. memcpy (&scratch[2], q, len);
  500. len += 2;
  501. scratch[len++] = '_';
  502. scratch[len++] = '_';
  503. for (q = line; q <= limit - len; q++)
  504. if (*q == '_' && !strncmp (q, scratch, len))
  505. goto again;
  506. fwrite (text, 1, p - text, stdout);
  507. fwrite (scratch, 1, len, stdout);
  508. text = base;
  509. }
  510. }
  511. done:
  512. fputs (text, stdout);
  513. }
  514. FIX_PROC_HEAD( wrap_fix )
  515. {
  516. tSCC z_no_wrap_pat[] = "^#if.*__need_";
  517. static regex_t no_wrapping_re; /* assume zeroed data */
  518. tCC* pz_name = NULL;
  519. if (no_wrapping_re.allocated == 0)
  520. compile_re( z_no_wrap_pat, &no_wrapping_re, 0, "no-wrap pattern",
  521. "wrap-fix" );
  522. /*
  523. * IF we do *not* match the no-wrap re, then we have a double negative.
  524. * A double negative means YES.
  525. */
  526. if (xregexec( &no_wrapping_re, text, 0, NULL, 0 ) != 0)
  527. {
  528. /*
  529. * A single file can get wrapped more than once by different fixes.
  530. * A single fix can wrap multiple files. Therefore, guard with
  531. * *both* the fix name and the file name.
  532. */
  533. size_t ln = strlen( filname ) + strlen( p_fixd->fix_name ) + 14;
  534. char* pz = XNEWVEC (char, ln);
  535. pz_name = pz;
  536. sprintf( pz, "FIXINC_WRAP_%s-%s", filname, p_fixd->fix_name );
  537. for (pz += 12; 1; pz++) {
  538. char ch = *pz;
  539. if (ch == NUL)
  540. break;
  541. if (! ISALNUM( ch )) {
  542. *pz = '_';
  543. }
  544. else {
  545. *pz = TOUPPER( ch );
  546. }
  547. }
  548. printf( "#ifndef %s\n", pz_name );
  549. printf( "#define %s 1\n\n", pz_name );
  550. }
  551. if (p_fixd->patch_args[1] == (tCC*)NULL)
  552. fputs( text, stdout );
  553. else {
  554. fputs( p_fixd->patch_args[1], stdout );
  555. fputs( text, stdout );
  556. if (p_fixd->patch_args[2] != (tCC*)NULL)
  557. fputs( p_fixd->patch_args[2], stdout );
  558. }
  559. if (pz_name != NULL) {
  560. printf( "\n#endif /* %s */\n", pz_name );
  561. free( (void*)pz_name );
  562. }
  563. }
  564. /*
  565. * Search for multiple copies of a regular expression. Each block
  566. * of matched text is replaced with the format string, as described
  567. * above in `format_write'.
  568. */
  569. FIX_PROC_HEAD( gnu_type_fix )
  570. {
  571. const char* pz_pat;
  572. regex_t re;
  573. regmatch_t rm[GTYPE_SE_CT+1];
  574. IGNORE_ARG(filname);
  575. {
  576. tTestDesc* pTD = p_fixd->p_test_desc;
  577. int ct = p_fixd->test_ct;
  578. for (;;)
  579. {
  580. if (ct-- <= 0)
  581. {
  582. fprintf (stderr, zNeedsArg, p_fixd->fix_name, "search text", 1);
  583. exit (EXIT_BROKEN);
  584. }
  585. if (pTD->type == TT_EGREP)
  586. {
  587. pz_pat = pTD->pz_test_text;
  588. break;
  589. }
  590. pTD++;
  591. }
  592. }
  593. compile_re (pz_pat, &re, 1, "gnu type typedef", "gnu_type_fix");
  594. while (xregexec (&re, text, GTYPE_SE_CT+1, rm, 0) == 0)
  595. {
  596. text = emit_gnu_type (text, rm);
  597. }
  598. /*
  599. * Dump out the rest of the file
  600. */
  601. fputs (text, stdout);
  602. }
  603. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  604. test for fix selector
  605. THIS IS THE ONLY EXPORTED ROUTINE
  606. */
  607. void
  608. apply_fix( tFixDesc* p_fixd, tCC* filname )
  609. {
  610. #define _FT_(n,p) { n, p },
  611. static fix_entry_t fix_table[] = { FIXUP_TABLE { NULL, NULL }};
  612. #undef _FT_
  613. #define FIX_TABLE_CT (ARRAY_SIZE (fix_table)-1)
  614. tCC* fixname = p_fixd->patch_args[0];
  615. char* buf;
  616. int ct = FIX_TABLE_CT;
  617. fix_entry_t* pfe = fix_table;
  618. for (;;)
  619. {
  620. if (strcmp (pfe->fix_name, fixname) == 0)
  621. break;
  622. if (--ct <= 0)
  623. {
  624. fprintf (stderr, "fixincl error: the `%s' fix is unknown\n",
  625. fixname );
  626. exit (EXIT_BROKEN);
  627. }
  628. pfe++;
  629. }
  630. buf = load_file_data (stdin);
  631. (*pfe->fix_proc)( filname, buf, p_fixd );
  632. }
  633. #ifdef SEPARATE_FIX_PROC
  634. tSCC z_usage[] =
  635. "USAGE: applyfix <fix-name> <file-to-fix> <file-source> <file-destination>\n";
  636. tSCC z_reopen[] =
  637. "FS error %d (%s) reopening %s as std%s\n";
  638. int
  639. main( int argc, char** argv )
  640. {
  641. tFixDesc* pFix;
  642. char* pz_tmptmp;
  643. #ifdef _PC_NAME_MAX
  644. char* pz_tmp_base;
  645. char* pz_tmp_dot;
  646. #endif
  647. if (argc != 5)
  648. {
  649. usage_failure:
  650. fputs (z_usage, stderr);
  651. return EXIT_FAILURE;
  652. }
  653. initialize_opts ();
  654. {
  655. char* pz = argv[1];
  656. long idx;
  657. if (! ISDIGIT ( *pz ))
  658. goto usage_failure;
  659. idx = strtol (pz, &pz, 10);
  660. if ((*pz != NUL) || ((unsigned)idx >= FIX_COUNT))
  661. goto usage_failure;
  662. pFix = fixDescList + idx;
  663. }
  664. if (freopen (argv[3], "r", stdin) != stdin)
  665. {
  666. fprintf (stderr, z_reopen, errno, strerror( errno ), argv[3], "in");
  667. return EXIT_FAILURE;
  668. }
  669. pz_tmptmp = XNEWVEC (char, strlen (argv[4]) + 5);
  670. strcpy( pz_tmptmp, argv[4] );
  671. #ifdef _PC_NAME_MAX
  672. /* Don't lose because "12345678" and "12345678X" map to the same
  673. file under DOS restricted 8+3 file namespace. Note that DOS
  674. doesn't allow more than one dot in the trunk of a file name. */
  675. pz_tmp_base = basename( pz_tmptmp );
  676. pz_tmp_dot = strchr( pz_tmp_base, '.' );
  677. if (pathconf( pz_tmptmp, _PC_NAME_MAX ) <= 12 /* is this DOS or Windows9X? */
  678. && pz_tmp_dot != (char*)NULL)
  679. strcpy (pz_tmp_dot+1, "X"); /* nuke the original extension */
  680. else
  681. #endif /* _PC_NAME_MAX */
  682. strcat (pz_tmptmp, ".X");
  683. if (freopen (pz_tmptmp, "w", stdout) != stdout)
  684. {
  685. fprintf (stderr, z_reopen, errno, strerror( errno ), pz_tmptmp, "out");
  686. return EXIT_FAILURE;
  687. }
  688. /* Second parameter of apply_fix is file name */
  689. apply_fix (pFix, argv[2]);
  690. fclose (stdout);
  691. fclose (stdin);
  692. unlink (argv[4]);
  693. if (rename (pz_tmptmp, argv[4]) != 0)
  694. {
  695. fprintf (stderr, "error %d (%s) renaming %s to %s\n", errno,
  696. strerror( errno ), pz_tmptmp, argv[4]);
  697. return EXIT_FAILURE;
  698. }
  699. return EXIT_SUCCESS;
  700. }
  701. #endif