Daily bump.
[official-gcc.git] / fixincludes / fixlib.c
blob4cdefe0bef4220bbc73e6cf1819f611d0bca6856
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 GCC will search.
6 Copyright (C) 1999, 2000, 2001, 2004, 2009 Free Software Foundation, Inc.
8 This file is part of GCC.
10 GCC 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 3, or (at your option)
13 any later version.
15 GCC 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.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 #include "fixlib.h"
26 /* * * * * * * * * * * * *
28 load_file_data loads all the contents of a file into malloc-ed memory.
29 Its argument is the file pointer of the file to read in; the returned
30 result is the NUL terminated contents of the file. The file
31 is presumed to be an ASCII text file containing no NULs. */
33 char *
34 load_file_data (FILE* fp)
36 char *pz_data = (char*)NULL;
37 int space_left = -1; /* allow for terminating NUL */
38 size_t space_used = 0;
40 if (fp == (FILE*)NULL)
41 return pz_data;
45 size_t size_read;
47 if (space_left < 1024)
49 space_left += 4096;
50 pz_data = XRESIZEVEC (char, pz_data, space_left + space_used + 1 );
52 size_read = fread (pz_data + space_used, 1, space_left, fp);
54 if (size_read == 0)
56 if (feof (fp))
57 break;
59 if (ferror (fp))
61 int err = errno;
62 if (err != EISDIR)
63 fprintf (stderr, "error %d (%s) reading input\n", err,
64 xstrerror (err));
65 free ((void *) pz_data);
66 return (char *) NULL;
70 space_left -= size_read;
71 space_used += size_read;
72 } while (! feof (fp));
74 pz_data = XRESIZEVEC (char, pz_data, space_used+1 );
75 pz_data[ space_used ] = NUL;
77 return pz_data;
80 #ifdef IS_CXX_HEADER_NEEDED
81 t_bool
82 is_cxx_header (tCC* fname, tCC* text)
84 /* First, check to see if the file is in a C++ directory */
85 for (;;)
87 switch (*(fname++))
89 case 'C': /* check for "CC/" */
90 if ((fname[0] == 'C') && (fname[1] == '/'))
91 return BOOL_TRUE;
92 break;
94 case 'x': /* check for "xx/" */
95 if ((fname[0] == 'x') && (fname[1] == '/'))
96 return BOOL_TRUE;
97 break;
99 case '+': /* check for "++" */
100 if (fname[0] == '+')
101 return BOOL_TRUE;
102 break;
104 case NUL:
105 goto not_cxx_name;
107 } not_cxx_name:;
109 /* Or it might contain one of several phrases which indicate C++ code.
110 Currently recognized are:
111 extern "C++"
112 -*- (Mode: )? C++ -*- (emacs mode marker)
113 template <
116 tSCC cxxpat[] = "\
117 extern[ \t]*\"C\\+\\+\"|\
118 -\\*-[ \t]*([mM]ode:[ \t]*)?[cC]\\+\\+[; \t]*-\\*-|\
119 template[ \t]*<|\
120 ^[ \t]*class[ \t]|\
121 (public|private|protected):|\
122 ^[ \t]*#[ \t]*pragma[ \t]+(interface|implementation)\
124 static regex_t cxxre;
125 static int compiled;
127 if (!compiled)
128 compile_re (cxxpat, &cxxre, 0, "contents check", "is_cxx_header");
130 if (xregexec (&cxxre, text, 0, 0, 0) == 0)
131 return BOOL_TRUE;
134 return BOOL_FALSE;
136 #endif /* CXX_TYPE_NEEDED */
138 #ifdef SKIP_QUOTE_NEEDED
140 * Skip over a quoted string. Single quote strings may
141 * contain multiple characters if the first character is
142 * a backslash. Especially a backslash followed by octal digits.
143 * We are not doing a correctness syntax check here.
145 tCC*
146 skip_quote(char q, char* text )
148 for (;;)
150 char ch = *(text++);
151 switch (ch)
153 case '\\':
154 text++; /* skip over whatever character follows */
155 break;
157 case '"':
158 case '\'':
159 if (ch != q)
160 break;
161 /*FALLTHROUGH*/
163 case '\n':
164 case NUL:
165 goto skip_done;
167 } skip_done:;
169 return text;
171 #endif /* SKIP_QUOTE_NEEDED */
173 /* * * * * * * * * * * * *
175 Compile one regular expression pattern for later use. PAT contains
176 the pattern, RE points to a regex_t structure (which should have
177 been bzeroed). MATCH is 1 if we need to know where the regex
178 matched, 0 if not. If xregcomp fails, prints an error message and
179 aborts; E1 and E2 are strings to shove into the error message.
181 The patterns we search for are all egrep patterns.
182 REG_EXTENDED|REG_NEWLINE produces identical regex syntax/semantics
183 to egrep (verified from 4.4BSD Programmer's Reference Manual). */
184 void
185 compile_re( tCC* pat, regex_t* re, int match, tCC* e1, tCC* e2 )
187 tSCC z_bad_comp[] = "fixincl ERROR: cannot compile %s regex for %s\n\
188 \texpr = `%s'\n\terror %s\n";
189 int flags, err;
191 flags = (match ? REG_EXTENDED|REG_NEWLINE
192 : REG_EXTENDED|REG_NEWLINE|REG_NOSUB);
193 err = xregcomp (re, pat, flags);
195 if (err)
197 char rerrbuf[1024];
198 regerror (err, re, rerrbuf, 1024);
199 fprintf (stderr, z_bad_comp, e1, e2, pat, rerrbuf);
200 exit (EXIT_FAILURE);
204 /* * * * * * * * * * * * *
206 Helper routine and data for the machine_name test and fix. */
208 tSCC mn_label_pat[] = "^[ \t]*#[ \t]*(if|ifdef|ifndef)[ \t]+";
209 static regex_t mn_label_re;
210 static regex_t mn_name_re;
212 static int mn_compiled = 0;
214 t_bool
215 mn_get_regexps(regex_t** label_re, regex_t** name_re, tCC* who )
217 if (! pz_mn_name_pat)
218 return BOOL_FALSE;
220 if (! mn_compiled)
222 compile_re (mn_label_pat, &mn_label_re, 1, "label pattern", who);
223 compile_re (pz_mn_name_pat, &mn_name_re, 1, "name pattern", who);
224 mn_compiled++;
226 *label_re = &mn_label_re;
227 *name_re = &mn_name_re;
228 return BOOL_TRUE;
232 #ifdef SEPARATE_FIX_PROC
234 char*
235 make_raw_shell_str( char* pz_d, tCC* pz_s, size_t smax )
237 tSCC zQ[] = "'\\''";
238 size_t dtaSize;
239 char* pz_d_start = pz_d;
241 smax--; /* adjust for trailing NUL */
243 dtaSize = strlen( pz_s ) + 3;
246 const char* pz = pz_s - 1;
248 for (;;) {
249 pz = strchr( pz+1, '\'' );
250 if (pz == (char*)NULL)
251 break;
252 dtaSize += sizeof( zQ )-1;
255 if (dtaSize > smax)
256 return (char*)NULL;
258 *(pz_d++) = '\'';
260 for (;;) {
261 if ((size_t) (pz_d - pz_d_start) >= smax)
262 return (char*)NULL;
263 switch (*(pz_d++) = *(pz_s++)) {
264 case NUL:
265 goto loopDone;
267 case '\'':
268 if ((size_t) (pz_d - pz_d_start) >= smax - sizeof( zQ )-1)
269 return (char*)NULL;
270 strcpy( pz_d-1, zQ );
271 pz_d += sizeof( zQ )-2;
273 } loopDone:;
274 pz_d[-1] = '\'';
275 *pz_d = NUL;
277 return pz_d;
280 #endif
282 #if defined(__MINGW32__)
283 void
284 fix_path_separators (char* p)
286 while (p != NULL)
288 p = strchr (p, '\\');
289 if (p != NULL)
291 *p = '/';
292 ++p;
297 /* Count number of needle character ocurrences in str */
298 static int
299 count_occurrences_of_char (char* str, char needle)
301 int cnt = 0;
303 while (str)
305 str = strchr (str, needle);
306 if (str)
308 ++str;
309 ++cnt;
313 return cnt;
316 /* On Mingw32, system function will just start cmd by default.
317 Call system function, but prepend ${CONFIG_SHELL} or ${SHELL} -c to the command,
318 replace newlines with '$'\n'', enclose command with double quotes
319 and escape special characters which were originally enclosed in single quotes.
322 system_with_shell (char* s)
324 static const char z_shell_start_args[] = " -c \"";
325 static const char z_shell_end_args[] = "\"";
326 static const char z_shell_newline[] = "'$'\\n''";
328 /* Use configured shell if present */
329 char *env_shell = getenv ("CONFIG_SHELL");
330 int newline_cnt = count_occurrences_of_char (s, '\n');
331 int escapes_cnt = count_occurrences_of_char( s, '\\')
332 + count_occurrences_of_char (s, '"')
333 + count_occurrences_of_char (s, '`');
334 char *long_cmd;
335 char *cmd_endp;
336 int sys_result;
337 char *s_scan;
338 int in_quotes;
340 if (env_shell == NULL)
341 env_shell = getenv ("SHELL");
343 /* If neither CONFIGURED_SHELL nor SHELL is set, just call standard system function */
344 if (env_shell == NULL)
345 return system (s);
347 /* Allocate enough memory to fit newly created command string */
348 long_cmd = XNEWVEC (char, strlen (env_shell)
349 + strlen (z_shell_start_args)
350 + strlen (s)
351 + newline_cnt * (strlen (z_shell_newline) - 1)
352 + escapes_cnt
353 + strlen (z_shell_end_args)
354 + 1);
356 /* Start with ${SHELL} */
357 strcpy (long_cmd, env_shell);
358 cmd_endp = long_cmd + strlen (long_cmd);
360 /* Opening quote */
361 strcpy (cmd_endp, z_shell_start_args);
362 cmd_endp += strlen (z_shell_start_args);
364 /* Replace newlines and escape special chars */
365 in_quotes = 0;
366 for (s_scan = s; *s_scan; ++s_scan)
368 switch (*s_scan)
370 case '\n':
371 if (in_quotes)
373 /* Replace newline inside quotes with '$'\n'' */
374 strcpy (cmd_endp, z_shell_newline);
375 cmd_endp += strlen (z_shell_newline);
377 else
379 /* Replace newlines outside quotes with ; and merge subsequent newlines */
380 *(cmd_endp++) = ';';
381 *(cmd_endp++) = ' ';
382 while (*(s_scan + 1) == '\n' || *(s_scan + 1) == ' ' || *(s_scan + 1) == '\t')
383 ++s_scan;
385 break;
386 case '\'':
387 /* Escape single quote and toggle in_quotes flag */
388 in_quotes = !in_quotes;
389 *(cmd_endp++) = *s_scan;
390 break;
391 case '\\':
392 case '`':
393 /* Escape backslash and backtick inside quotes */
394 if (in_quotes)
395 *(cmd_endp++) = '\\';
396 *(cmd_endp++) = *s_scan;
397 break;
398 case '"':
399 /* Escape double quotes always */
400 *(cmd_endp++) = '\\';
401 *(cmd_endp++) = *s_scan;
402 break;
403 default:
404 *(cmd_endp++) = *s_scan;
408 /* Closing quote */
409 strcpy (cmd_endp, z_shell_end_args);
411 sys_result = system (long_cmd);
413 free (long_cmd);
415 return sys_result;
418 #endif /* defined(__MINGW32__) */