00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "fixlib.h"
00026
00027
00028
00029
00030
00031
00032
00033
00034 char *
00035 load_file_data (fp)
00036 FILE* fp;
00037 {
00038 char *pz_data = (char*)NULL;
00039 int space_left = -1;
00040 size_t space_used = 0;
00041
00042 if (fp == (FILE*)NULL)
00043 return pz_data;
00044
00045 do
00046 {
00047 size_t size_read;
00048
00049 if (space_left < 1024)
00050 {
00051 space_left += 4096;
00052 pz_data = xrealloc ((void*)pz_data, space_left + space_used + 1 );
00053 }
00054 size_read = fread (pz_data + space_used, 1, space_left, fp);
00055
00056 if (size_read == 0)
00057 {
00058 if (feof (fp))
00059 break;
00060
00061 if (ferror (fp))
00062 {
00063 int err = errno;
00064 if (err != EISDIR)
00065 fprintf (stderr, "error %d (%s) reading input\n", err,
00066 xstrerror (err));
00067 free ((void *) pz_data);
00068 return (char *) NULL;
00069 }
00070 }
00071
00072 space_left -= size_read;
00073 space_used += size_read;
00074 } while (! feof (fp));
00075
00076 pz_data = xrealloc ((void*)pz_data, space_used+1 );
00077 pz_data[ space_used ] = NUL;
00078
00079 return pz_data;
00080 }
00081
00082 #ifdef IS_CXX_HEADER_NEEDED
00083 t_bool
00084 is_cxx_header (fname, text)
00085 tCC *fname;
00086 tCC *text;
00087 {
00088
00089 for (;;)
00090 {
00091 switch (*(fname++))
00092 {
00093 case 'C':
00094 if ((fname[0] == 'C') && (fname[1] == '/'))
00095 return BOOL_TRUE;
00096 break;
00097
00098 case 'x':
00099 if ((fname[0] == 'x') && (fname[1] == '/'))
00100 return BOOL_TRUE;
00101 break;
00102
00103 case '+':
00104 if (fname[0] == '+')
00105 return BOOL_TRUE;
00106 break;
00107
00108 case NUL:
00109 goto not_cxx_name;
00110 }
00111 } not_cxx_name:;
00112
00113
00114
00115
00116
00117
00118
00119 {
00120 tSCC cxxpat[] = "\
00121 extern[ \t]*\"C\\+\\+\"|\
00122 -\\*-[ \t]*([mM]ode:[ \t]*)?[cC]\\+\\+[; \t]*-\\*-|\
00123 template[ \t]*<|\
00124 ^[ \t]*class[ \t]|\
00125 (public|private|protected):|\
00126 ^[ \t]*#[ \t]*pragma[ \t]+(interface|implementation)\
00127 ";
00128 static regex_t cxxre;
00129 static int compiled;
00130
00131 if (!compiled)
00132 compile_re (cxxpat, &cxxre, 0, "contents check", "is_cxx_header");
00133
00134 if (regexec (&cxxre, text, 0, 0, 0) == 0)
00135 return BOOL_TRUE;
00136 }
00137
00138 return BOOL_FALSE;
00139 }
00140 #endif
00141
00142 #ifdef SKIP_QUOTE_NEEDED
00143
00144
00145
00146
00147
00148
00149 tCC*
00150 skip_quote( q, text )
00151 char q;
00152 char* text;
00153 {
00154 for (;;)
00155 {
00156 char ch = *(text++);
00157 switch (ch)
00158 {
00159 case '\\':
00160 text++;
00161 break;
00162
00163 case '"':
00164 case '\'':
00165 if (ch != q)
00166 break;
00167
00168
00169 case '\n':
00170 case NUL:
00171 goto skip_done;
00172 }
00173 } skip_done:;
00174
00175 return text;
00176 }
00177 #endif
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 void
00191 compile_re( pat, re, match, e1, e2 )
00192 tCC *pat;
00193 regex_t *re;
00194 int match;
00195 tCC *e1;
00196 tCC *e2;
00197 {
00198 tSCC z_bad_comp[] = "fixincl ERROR: cannot compile %s regex for %s\n\
00199 \texpr = `%s'\n\terror %s\n";
00200 int flags, err;
00201
00202 flags = (match ? REG_EXTENDED|REG_NEWLINE
00203 : REG_EXTENDED|REG_NEWLINE|REG_NOSUB);
00204 err = regcomp (re, pat, flags);
00205
00206 if (err)
00207 {
00208 char rerrbuf[1024];
00209 regerror (err, re, rerrbuf, 1024);
00210 fprintf (stderr, z_bad_comp, e1, e2, pat, rerrbuf);
00211 exit (EXIT_FAILURE);
00212 }
00213 }
00214
00215
00216
00217
00218
00219
00220 #ifdef MN_NAME_PAT
00221
00222 tSCC mn_label_pat[] = "^[ \t]*#[ \t]*(if|ifdef|ifndef)[ \t]+";
00223 static regex_t mn_label_re;
00224
00225 tSCC mn_name_pat[] = MN_NAME_PAT;
00226 static regex_t mn_name_re;
00227
00228 static int mn_compiled = 0;
00229
00230 void
00231 mn_get_regexps( label_re, name_re, who )
00232 regex_t **label_re;
00233 regex_t **name_re;
00234 tCC *who;
00235 {
00236 if (! mn_compiled)
00237 {
00238 compile_re (mn_label_pat, &mn_label_re, 1, "label pattern", who);
00239 compile_re (mn_name_pat, &mn_name_re, 1, "name pattern", who);
00240 mn_compiled++;
00241 }
00242 *label_re = &mn_label_re;
00243 *name_re = &mn_name_re;
00244 }
00245 #endif
00246
00247
00248 #ifdef SEPARATE_FIX_PROC
00249
00250 char*
00251 make_raw_shell_str( pz_d, pz_s, smax )
00252 char* pz_d;
00253 tCC* pz_s;
00254 size_t smax;
00255 {
00256 tSCC zQ[] = "'\\''";
00257 size_t dtaSize;
00258 char* pz_d_start = pz_d;
00259
00260 smax--;
00261
00262 dtaSize = strlen( pz_s ) + 3;
00263
00264 {
00265 const char* pz = pz_s - 1;
00266
00267 for (;;) {
00268 pz = strchr( pz+1, '\'' );
00269 if (pz == (char*)NULL)
00270 break;
00271 dtaSize += sizeof( zQ )-1;
00272 }
00273 }
00274 if (dtaSize > smax)
00275 return (char*)NULL;
00276
00277 *(pz_d++) = '\'';
00278
00279 for (;;) {
00280 if (pz_d - pz_d_start >= smax)
00281 return (char*)NULL;
00282 switch (*(pz_d++) = *(pz_s++)) {
00283 case NUL:
00284 goto loopDone;
00285
00286 case '\'':
00287 if (pz_d - pz_d_start >= smax - sizeof( zQ )-1)
00288 return (char*)NULL;
00289 strcpy( pz_d-1, zQ );
00290 pz_d += sizeof( zQ )-2;
00291 }
00292 } loopDone:;
00293 pz_d[-1] = '\'';
00294 *pz_d = NUL;
00295
00296 return pz_d;
00297 }
00298
00299 #endif