]>
Commit | Line | Data |
---|---|---|
5daf7ede | 1 | |
d6d7d999 | 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 | ||
5daf7ede | 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; | |
27928b9a | 49 | if (pz_data) |
50 | pz_data = realloc ((void*)pz_data, space_left + space_used + 1 ); | |
51 | else | |
52 | pz_data = malloc (space_left + space_used + 1 ); | |
5daf7ede | 53 | } |
54 | size_read = fread (pz_data + space_used, 1, space_left, fp); | |
55 | ||
56 | if (size_read == 0) | |
57 | { | |
58 | if (feof (fp)) | |
59 | break; | |
60 | ||
61 | if (ferror (fp)) | |
62 | { | |
63 | int err = errno; | |
64 | if (err != EISDIR) | |
65 | fprintf (stderr, "error %d (%s) reading input\n", err, | |
66 | strerror (err)); | |
67 | free ((void *) pz_data); | |
68 | fclose (fp); | |
69 | return (char *) NULL; | |
70 | } | |
71 | } | |
72 | ||
73 | space_left -= size_read; | |
74 | space_used += size_read; | |
75 | } while (! feof (fp)); | |
76 | ||
77 | pz_data = realloc ((void*)pz_data, space_used+1 ); | |
78 | pz_data[ space_used ] = NUL; | |
79 | fclose (fp); | |
80 | ||
81 | return pz_data; | |
82 | } | |
a98784e9 | 83 | |
84 | ||
85 | t_bool | |
86 | is_cxx_header (fname, text) | |
87 | tCC *fname; | |
88 | tCC *text; | |
89 | { | |
90 | /* First, check to see if the file is in a C++ directory */ | |
91 | for (;;) | |
92 | { | |
93 | switch (*(fname++)) | |
94 | { | |
95 | case 'C': /* check for "CC/" */ | |
96 | if ((fname[0] == 'C') && (fname[1] == '/')) | |
97 | return BOOL_TRUE; | |
98 | break; | |
99 | ||
100 | case 'x': /* check for "xx/" */ | |
101 | if ((fname[0] == 'x') && (fname[1] == '/')) | |
102 | return BOOL_TRUE; | |
103 | break; | |
104 | ||
105 | case '+': /* check for "++" */ | |
106 | if (fname[0] == '+') | |
107 | return BOOL_TRUE; | |
108 | break; | |
109 | ||
110 | case NUL: | |
111 | goto not_cxx_name; | |
112 | } | |
113 | } not_cxx_name:; | |
114 | ||
ada96dc2 | 115 | /* Or it might contain one of several phrases which indicate C++ code. |
116 | Currently recognized are: | |
117 | extern "C++" | |
118 | -*- (Mode: )? C++ -*- (emacs mode marker) | |
119 | template < | |
120 | */ | |
a98784e9 | 121 | { |
ada96dc2 | 122 | tSCC cxxpat[] = "\ |
123 | extern[ \t]*\"C\\+\\+\"|\ | |
124 | -\\*-[ \t]*([mM]ode:[ \t]*)?[cC]\\+\\+[; \t]*-\\*-|\ | |
125 | template[ \t]*<"; | |
126 | static regex_t cxxre; | |
127 | static int compiled; | |
128 | ||
129 | if (!compiled) | |
130 | compile_re (cxxpat, &cxxre, 0, "contents check", "is_cxx_header"); | |
131 | ||
132 | if (regexec (&cxxre, text, 0, 0, 0) == 0) | |
133 | return BOOL_TRUE; | |
134 | } | |
135 | ||
a98784e9 | 136 | return BOOL_FALSE; |
137 | } | |
d6d7d999 | 138 | |
139 | /* * * * * * * * * * * * * | |
140 | ||
141 | Compile one regular expression pattern for later use. PAT contains | |
142 | the pattern, RE points to a regex_t structure (which should have | |
143 | been bzeroed). MATCH is 1 if we need to know where the regex | |
144 | matched, 0 if not. If regcomp fails, prints an error message and | |
145 | aborts; E1 and E2 are strings to shove into the error message. | |
146 | ||
147 | The patterns we search for are all egrep patterns. | |
148 | REG_EXTENDED|REG_NEWLINE produces identical regex syntax/semantics | |
149 | to egrep (verified from 4.4BSD Programmer's Reference Manual). */ | |
150 | void | |
151 | compile_re( pat, re, match, e1, e2 ) | |
152 | tCC *pat; | |
153 | regex_t *re; | |
154 | int match; | |
155 | tCC *e1; | |
156 | tCC *e2; | |
157 | { | |
158 | tSCC z_bad_comp[] = "fixincl ERROR: cannot compile %s regex for %s\n\ | |
159 | \texpr = `%s'\n\terror %s\n"; | |
160 | int flags, err; | |
161 | ||
162 | flags = (match ? REG_EXTENDED|REG_NEWLINE | |
163 | : REG_EXTENDED|REG_NEWLINE|REG_NOSUB); | |
164 | err = regcomp (re, pat, flags); | |
165 | ||
166 | if (err) | |
167 | { | |
168 | char rerrbuf[1024]; | |
169 | regerror (err, re, rerrbuf, 1024); | |
170 | fprintf (stderr, z_bad_comp, e1, e2, pat, rerrbuf); | |
171 | exit (EXIT_FAILURE); | |
172 | } | |
173 | } | |
17f8e521 | 174 | |
175 | /* * * * * * * * * * * * * | |
176 | ||
177 | Helper routine and data for the machine_name test and fix. | |
178 | machname.h is created by black magic in the Makefile. */ | |
179 | ||
180 | #include "machname.h" | |
181 | ||
182 | tSCC mn_label_pat[] = "^[ \t]*#[ \t]*(if|ifdef|ifndef)[ \t]+"; | |
183 | static regex_t mn_label_re; | |
184 | ||
185 | tSCC mn_name_pat[] = MN_NAME_PAT; | |
186 | static regex_t mn_name_re; | |
187 | ||
188 | static int mn_compiled = 0; | |
189 | ||
190 | void | |
191 | mn_get_regexps( label_re, name_re, who ) | |
192 | regex_t **label_re; | |
193 | regex_t **name_re; | |
194 | tCC *who; | |
195 | { | |
196 | if (! mn_compiled) | |
197 | { | |
198 | compile_re (mn_label_pat, &mn_label_re, 1, "label pattern", who); | |
199 | compile_re (mn_name_pat, &mn_name_re, 1, "name pattern", who); | |
200 | mn_compiled++; | |
201 | } | |
202 | *label_re = &mn_label_re; | |
203 | *name_re = &mn_name_re; | |
204 | } |