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