OSDN Git Service

Prepare for fixincludes on BeOS
[pf3gnuchains/gcc-fork.git] / gcc / fixinc / fixlib.c
1
2 /* Install modified versions of certain ANSI-incompatible system header
3    files which are fixed to work correctly with ANSI C and placed in a
4    directory that GNU C will search.
5
6    Copyright (C) 1999, 2000 Free Software Foundation, Inc.
7
8 This file is part of GNU CC.
9
10 GNU CC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 GNU CC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GNU CC; see the file COPYING.  If not, write to
22 the Free Software Foundation, 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.  */
24
25 #include "fixlib.h"
26
27 /* * * * * * * * * * * * *
28  
29    load_file_data loads all the contents of a file into malloc-ed memory.
30    Its argument is the file pointer of the file to read in; the returned
31    result is the NUL terminated contents of the file.  The file
32    is presumed to be an ASCII text file containing no NULs.  */
33
34 char *
35 load_file_data (fp)
36      FILE* fp;
37 {
38   char *pz_data = (char*)NULL;
39   int    space_left = -1;  /* allow for terminating NUL */
40   size_t space_used = 0;
41
42   do
43     {
44       size_t  size_read;
45
46       if (space_left < 1024)
47         {
48           space_left += 4096;
49           pz_data = xrealloc ((void*)pz_data, space_left + space_used + 1 );
50         }
51       size_read = fread (pz_data + space_used, 1, space_left, fp);
52
53       if (size_read == 0)
54         {
55           if (feof (fp))
56             break;
57
58           if (ferror (fp))
59             {
60               int err = errno;
61               if (err != EISDIR)
62                 fprintf (stderr, "error %d (%s) reading input\n", err,
63                          xstrerror (err));
64               free ((void *) pz_data);
65               fclose (fp);
66               return (char *) NULL;
67             }
68         }
69
70       space_left -= size_read;
71       space_used += size_read;
72     } while (! feof (fp));
73
74   pz_data = xrealloc ((void*)pz_data, space_used+1 );
75   pz_data[ space_used ] = NUL;
76   fclose (fp);
77
78   return pz_data;
79 }
80
81 #ifdef IS_CXX_HEADER_NEEDED
82 t_bool
83 is_cxx_header (fname, text)
84      tCC *fname;
85      tCC *text;
86 {
87   /*  First, check to see if the file is in a C++ directory */
88   for (;;)
89     {
90       switch (*(fname++))
91         {
92         case 'C': /* check for "CC/" */
93           if ((fname[0] == 'C') && (fname[1] == '/'))
94             return BOOL_TRUE;
95           break;
96
97         case 'x': /* check for "xx/" */
98           if ((fname[0] == 'x') && (fname[1] == '/'))
99             return BOOL_TRUE;
100           break;
101
102         case '+': /* check for "++" */
103           if (fname[0] == '+')
104             return BOOL_TRUE;
105           break;
106
107         case NUL:
108           goto not_cxx_name;
109         }
110     } not_cxx_name:;
111
112   /* Or it might contain one of several phrases which indicate C++ code.
113      Currently recognized are:
114      extern "C++"
115      -*- (Mode: )? C++ -*-   (emacs mode marker)
116      template <
117    */
118     {
119       tSCC cxxpat[] = "\
120 extern[ \t]*\"C\\+\\+\"|\
121 -\\*-[ \t]*([mM]ode:[ \t]*)?[cC]\\+\\+[; \t]*-\\*-|\
122 template[ \t]*<|\
123 ^[ \t]*class[ \t]|\
124 (public|private|protected):|\
125 ^[ \t]*#[ \t]*pragma[ \t]+(interface|implementation)\
126 ";
127       static regex_t cxxre;
128       static int compiled;
129
130       if (!compiled)
131         compile_re (cxxpat, &cxxre, 0, "contents check", "is_cxx_header");
132
133       if (regexec (&cxxre, text, 0, 0, 0) == 0)
134         return BOOL_TRUE;
135     }
136                    
137   return BOOL_FALSE;
138 }
139 #endif /* CXX_TYPE_NEEDED */
140
141 #ifdef SKIP_QUOTE_NEEDED
142 /*
143  *  Skip over a quoted string.  Single quote strings may
144  *  contain multiple characters if the first character is
145  *  a backslash.  Especially a backslash followed by octal digits.
146  *  We are not doing a correctness syntax check here.
147  */
148 tCC*
149 skip_quote( q, text )
150   char  q;
151   char* text;
152 {
153   for (;;)
154     {
155       char ch = *(text++);
156       switch (ch)
157         {
158         case '\\':
159           text++; /* skip over whatever character follows */
160           break;
161
162         case '"':
163         case '\'':
164           if (ch != q)
165             break;
166           /*FALLTHROUGH*/
167
168         case '\n':
169         case NUL:
170           goto skip_done;
171         }
172     } skip_done:;
173
174   return text;
175 }
176 #endif /* SKIP_QUOTE_NEEDED */
177
178 /* * * * * * * * * * * * *
179  
180    Compile one regular expression pattern for later use.  PAT contains
181    the pattern, RE points to a regex_t structure (which should have
182    been bzeroed).  MATCH is 1 if we need to know where the regex
183    matched, 0 if not. If regcomp fails, prints an error message and
184    aborts; E1 and E2 are strings to shove into the error message.
185
186    The patterns we search for are all egrep patterns.
187    REG_EXTENDED|REG_NEWLINE produces identical regex syntax/semantics
188    to egrep (verified from 4.4BSD Programmer's Reference Manual).  */
189 void
190 compile_re( pat, re, match, e1, e2 )
191      tCC *pat;
192      regex_t *re;
193      int match;
194      tCC *e1;
195      tCC *e2;
196 {
197   tSCC z_bad_comp[] = "fixincl ERROR:  cannot compile %s regex for %s\n\
198 \texpr = `%s'\n\terror %s\n";
199   int flags, err;
200
201   flags = (match ? REG_EXTENDED|REG_NEWLINE
202            : REG_EXTENDED|REG_NEWLINE|REG_NOSUB);
203   err = regcomp (re, pat, flags);
204
205   if (err)
206     {
207       char rerrbuf[1024];
208       regerror (err, re, rerrbuf, 1024);
209       fprintf (stderr, z_bad_comp, e1, e2, pat, rerrbuf);
210       exit (EXIT_FAILURE);
211     }
212 }
213
214 /* * * * * * * * * * * * *
215
216    Helper routine and data for the machine_name test and fix.
217    machname.h is created by black magic in the Makefile.  */
218
219 #ifdef MN_NAME_PAT
220
221 tSCC mn_label_pat[] = "^[ \t]*#[ \t]*(if|ifdef|ifndef)[ \t]+";
222 static regex_t mn_label_re;
223
224 tSCC mn_name_pat[] = MN_NAME_PAT;
225 static regex_t mn_name_re;
226
227 static int mn_compiled = 0;
228
229 void
230 mn_get_regexps( label_re, name_re, who )
231      regex_t **label_re;
232      regex_t **name_re;
233      tCC *who;
234 {
235   if (! mn_compiled)
236     {
237       compile_re (mn_label_pat, &mn_label_re, 1, "label pattern", who);
238       compile_re (mn_name_pat, &mn_name_re, 1, "name pattern", who);
239       mn_compiled++;
240     }
241   *label_re = &mn_label_re;
242   *name_re = &mn_name_re;
243 }
244 #endif
245
246
247 #ifdef SEPARATE_FIX_PROC
248
249 char*
250 make_raw_shell_str( pz_d, pz_s, smax )
251   char*       pz_d;
252   tCC*        pz_s;
253   size_t      smax;
254 {
255   tSCC zQ[] = "'\\''";
256   size_t     dtaSize;
257   char*      pz_d_start = pz_d;
258
259   smax--; /* adjust for trailing NUL */
260
261   dtaSize = strlen( pz_s ) + 3;
262
263   {
264     const char* pz = pz_s - 1;
265
266     for (;;) {
267       pz = strchr( pz+1, '\'' );
268       if (pz == (char*)NULL)
269         break;
270       dtaSize += sizeof( zQ )-1;
271     }
272   }
273   if (dtaSize > smax)
274     return (char*)NULL;
275
276   *(pz_d++) = '\'';
277
278   for (;;) {
279     if (pz_d - pz_d_start >= smax)
280       return (char*)NULL;
281     switch (*(pz_d++) = *(pz_s++)) {
282     case NUL:
283       goto loopDone;
284
285     case '\'':
286       if (pz_d - pz_d_start >= smax - sizeof( zQ )-1)
287         return (char*)NULL;
288       strcpy( pz_d-1, zQ );
289       pz_d += sizeof( zQ )-2;
290     }
291   } loopDone:;
292   pz_d[-1] = '\'';
293   *pz_d    = NUL;
294
295   return pz_d;
296 }
297
298 #endif