fixfixes.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. /* Fix for machine name #ifdefs that are not in the namespace reserved
  393. by the C standard. They won't be defined if compiling with -ansi,
  394. and the headers will break. We go to some trouble to only change
  395. #ifdefs where the macro is defined by GCC in non-ansi mode; this
  396. minimizes the number of headers touched. */
  397. #define SCRATCHSZ 64 /* hopefully long enough */
  398. FIX_PROC_HEAD( machine_name_fix )
  399. {
  400. regmatch_t match[2];
  401. const char *line, *base, *limit, *p, *q;
  402. regex_t *label_re, *name_re;
  403. char scratch[SCRATCHSZ];
  404. size_t len;
  405. IGNORE_ARG(filname);
  406. IGNORE_ARG(p_fixd);
  407. if (!mn_get_regexps (&label_re, &name_re, "machine_name_fix"))
  408. {
  409. fputs( "The target machine has no needed machine name fixes\n", stderr );
  410. goto done;
  411. }
  412. scratch[0] = '_';
  413. scratch[1] = '_';
  414. for (base = text;
  415. xregexec (label_re, base, 2, match, 0) == 0;
  416. base = limit)
  417. {
  418. base += match[0].rm_eo;
  419. /* We're looking at an #if or #ifdef. Scan forward for the
  420. next non-escaped newline. */
  421. line = limit = base;
  422. do
  423. {
  424. limit++;
  425. limit = strchr (limit, '\n');
  426. if (!limit)
  427. goto done;
  428. }
  429. while (limit[-1] == '\\');
  430. /* If the 'name_pat' matches in between base and limit, we have
  431. a bogon. It is not worth the hassle of excluding comments
  432. because comments on #if/#ifdef lines are rare, and strings on
  433. such lines are illegal.
  434. REG_NOTBOL means 'base' is not at the beginning of a line, which
  435. shouldn't matter since the name_re has no ^ anchor, but let's
  436. be accurate anyway. */
  437. for (;;)
  438. {
  439. again:
  440. if (base == limit)
  441. break;
  442. if (xregexec (name_re, base, 1, match, REG_NOTBOL))
  443. goto done; /* No remaining match in this file */
  444. /* Match; is it on the line? */
  445. if (match[0].rm_eo > limit - base)
  446. break;
  447. p = base + match[0].rm_so;
  448. base += match[0].rm_eo;
  449. /* One more test: if on the same line we have the same string
  450. with the appropriate underscores, then leave it alone.
  451. We want exactly two leading and trailing underscores. */
  452. if (*p == '_')
  453. {
  454. len = base - p - ((*base == '_') ? 2 : 1);
  455. q = p + 1;
  456. }
  457. else
  458. {
  459. len = base - p - ((*base == '_') ? 1 : 0);
  460. q = p;
  461. }
  462. if (len + 4 > SCRATCHSZ)
  463. abort ();
  464. memcpy (&scratch[2], q, len);
  465. len += 2;
  466. scratch[len++] = '_';
  467. scratch[len++] = '_';
  468. for (q = line; q <= limit - len; q++)
  469. if (*q == '_' && !strncmp (q, scratch, len))
  470. goto again;
  471. fwrite (text, 1, p - text, stdout);
  472. fwrite (scratch, 1, len, stdout);
  473. text = base;
  474. }
  475. }
  476. done:
  477. fputs (text, stdout);
  478. }
  479. FIX_PROC_HEAD( wrap_fix )
  480. {
  481. tSCC z_no_wrap_pat[] = "^#if.*__need_";
  482. static regex_t no_wrapping_re; /* assume zeroed data */
  483. tCC* pz_name = NULL;
  484. if (no_wrapping_re.allocated == 0)
  485. compile_re( z_no_wrap_pat, &no_wrapping_re, 0, "no-wrap pattern",
  486. "wrap-fix" );
  487. /*
  488. * IF we do *not* match the no-wrap re, then we have a double negative.
  489. * A double negative means YES.
  490. */
  491. if (xregexec( &no_wrapping_re, text, 0, NULL, 0 ) != 0)
  492. {
  493. /*
  494. * A single file can get wrapped more than once by different fixes.
  495. * A single fix can wrap multiple files. Therefore, guard with
  496. * *both* the fix name and the file name.
  497. */
  498. size_t ln = strlen( filname ) + strlen( p_fixd->fix_name ) + 14;
  499. char* pz = XNEWVEC (char, ln);
  500. pz_name = pz;
  501. sprintf( pz, "FIXINC_WRAP_%s-%s", filname, p_fixd->fix_name );
  502. for (pz += 12; 1; pz++) {
  503. char ch = *pz;
  504. if (ch == NUL)
  505. break;
  506. if (! ISALNUM( ch )) {
  507. *pz = '_';
  508. }
  509. else {
  510. *pz = TOUPPER( ch );
  511. }
  512. }
  513. printf( "#ifndef %s\n", pz_name );
  514. printf( "#define %s 1\n\n", pz_name );
  515. }
  516. if (p_fixd->patch_args[1] == (tCC*)NULL)
  517. fputs( text, stdout );
  518. else {
  519. fputs( p_fixd->patch_args[1], stdout );
  520. fputs( text, stdout );
  521. if (p_fixd->patch_args[2] != (tCC*)NULL)
  522. fputs( p_fixd->patch_args[2], stdout );
  523. }
  524. if (pz_name != NULL) {
  525. printf( "\n#endif /* %s */\n", pz_name );
  526. free( (void*)pz_name );
  527. }
  528. }
  529. /*
  530. * Search for multiple copies of a regular expression. Each block
  531. * of matched text is replaced with the format string, as described
  532. * above in `format_write'.
  533. */
  534. FIX_PROC_HEAD( gnu_type_fix )
  535. {
  536. const char* pz_pat;
  537. regex_t re;
  538. regmatch_t rm[GTYPE_SE_CT+1];
  539. IGNORE_ARG(filname);
  540. {
  541. tTestDesc* pTD = p_fixd->p_test_desc;
  542. int ct = p_fixd->test_ct;
  543. for (;;)
  544. {
  545. if (ct-- <= 0)
  546. {
  547. fprintf (stderr, zNeedsArg, p_fixd->fix_name, "search text", 1);
  548. exit (EXIT_BROKEN);
  549. }
  550. if (pTD->type == TT_EGREP)
  551. {
  552. pz_pat = pTD->pz_test_text;
  553. break;
  554. }
  555. pTD++;
  556. }
  557. }
  558. compile_re (pz_pat, &re, 1, "gnu type typedef", "gnu_type_fix");
  559. while (xregexec (&re, text, GTYPE_SE_CT+1, rm, 0) == 0)
  560. {
  561. text = emit_gnu_type (text, rm);
  562. }
  563. /*
  564. * Dump out the rest of the file
  565. */
  566. fputs (text, stdout);
  567. }
  568. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  569. test for fix selector
  570. THIS IS THE ONLY EXPORTED ROUTINE
  571. */
  572. void
  573. apply_fix( tFixDesc* p_fixd, tCC* filname )
  574. {
  575. #define _FT_(n,p) { n, p },
  576. static fix_entry_t fix_table[] = { FIXUP_TABLE { NULL, NULL }};
  577. #undef _FT_
  578. #define FIX_TABLE_CT (ARRAY_SIZE (fix_table)-1)
  579. tCC* fixname = p_fixd->patch_args[0];
  580. char* buf;
  581. int ct = FIX_TABLE_CT;
  582. fix_entry_t* pfe = fix_table;
  583. for (;;)
  584. {
  585. if (strcmp (pfe->fix_name, fixname) == 0)
  586. break;
  587. if (--ct <= 0)
  588. {
  589. fprintf (stderr, "fixincl error: the `%s' fix is unknown\n",
  590. fixname );
  591. exit (EXIT_BROKEN);
  592. }
  593. pfe++;
  594. }
  595. buf = load_file_data (stdin);
  596. (*pfe->fix_proc)( filname, buf, p_fixd );
  597. }
  598. #ifdef SEPARATE_FIX_PROC
  599. tSCC z_usage[] =
  600. "USAGE: applyfix <fix-name> <file-to-fix> <file-source> <file-destination>\n";
  601. tSCC z_reopen[] =
  602. "FS error %d (%s) reopening %s as std%s\n";
  603. int
  604. main( int argc, char** argv )
  605. {
  606. tFixDesc* pFix;
  607. char* pz_tmptmp;
  608. char* pz_tmp_base;
  609. char* pz_tmp_dot;
  610. if (argc != 5)
  611. {
  612. usage_failure:
  613. fputs (z_usage, stderr);
  614. return EXIT_FAILURE;
  615. }
  616. initialize_opts ();
  617. {
  618. char* pz = argv[1];
  619. long idx;
  620. if (! ISDIGIT ( *pz ))
  621. goto usage_failure;
  622. idx = strtol (pz, &pz, 10);
  623. if ((*pz != NUL) || ((unsigned)idx >= FIX_COUNT))
  624. goto usage_failure;
  625. pFix = fixDescList + idx;
  626. }
  627. if (freopen (argv[3], "r", stdin) != stdin)
  628. {
  629. fprintf (stderr, z_reopen, errno, strerror( errno ), argv[3], "in");
  630. return EXIT_FAILURE;
  631. }
  632. pz_tmptmp = XNEWVEC (char, strlen (argv[4]) + 5);
  633. strcpy( pz_tmptmp, argv[4] );
  634. /* Don't lose because "12345678" and "12345678X" map to the same
  635. file under DOS restricted 8+3 file namespace. Note that DOS
  636. doesn't allow more than one dot in the trunk of a file name. */
  637. pz_tmp_base = basename( pz_tmptmp );
  638. pz_tmp_dot = strchr( pz_tmp_base, '.' );
  639. #ifdef _PC_NAME_MAX
  640. if (pathconf( pz_tmptmp, _PC_NAME_MAX ) <= 12 /* is this DOS or Windows9X? */
  641. && pz_tmp_dot != (char*)NULL)
  642. strcpy (pz_tmp_dot+1, "X"); /* nuke the original extension */
  643. else
  644. #endif /* _PC_NAME_MAX */
  645. strcat (pz_tmptmp, ".X");
  646. if (freopen (pz_tmptmp, "w", stdout) != stdout)
  647. {
  648. fprintf (stderr, z_reopen, errno, strerror( errno ), pz_tmptmp, "out");
  649. return EXIT_FAILURE;
  650. }
  651. apply_fix (pFix, argv[1]);
  652. fclose (stdout);
  653. fclose (stdin);
  654. unlink (argv[4]);
  655. if (rename (pz_tmptmp, argv[4]) != 0)
  656. {
  657. fprintf (stderr, "error %d (%s) renaming %s to %s\n", errno,
  658. strerror( errno ), pz_tmptmp, argv[4]);
  659. return EXIT_FAILURE;
  660. }
  661. return EXIT_SUCCESS;
  662. }
  663. #endif