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