summaryrefslogtreecommitdiff
path: root/fixincludes/fixlib.c
blob: 4cdefe0bef4220bbc73e6cf1819f611d0bca6856 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* Install modified versions of certain ANSI-incompatible system header
   files which are fixed to work correctly with ANSI C and placed in a
   directory that GCC will search.

   Copyright (C) 1999, 2000, 2001, 2004, 2009 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "fixlib.h"

/* * * * * * * * * * * * *
 
   load_file_data loads all the contents of a file into malloc-ed memory.
   Its argument is the file pointer of the file to read in; the returned
   result is the NUL terminated contents of the file.  The file
   is presumed to be an ASCII text file containing no NULs.  */

char *
load_file_data (FILE* fp)
{
  char *pz_data = (char*)NULL;
  int    space_left = -1;  /* allow for terminating NUL */
  size_t space_used = 0;

  if (fp == (FILE*)NULL)
    return pz_data;

  do
    {
      size_t  size_read;

      if (space_left < 1024)
        {
          space_left += 4096;
	  pz_data = XRESIZEVEC (char, pz_data, space_left + space_used + 1 );
        }
      size_read = fread (pz_data + space_used, 1, space_left, fp);

      if (size_read == 0)
        {
          if (feof (fp))
            break;

          if (ferror (fp))
            {
              int err = errno;
              if (err != EISDIR)
                fprintf (stderr, "error %d (%s) reading input\n", err,
                         xstrerror (err));
              free ((void *) pz_data);
              return (char *) NULL;
            }
        }

      space_left -= size_read;
      space_used += size_read;
    } while (! feof (fp));

  pz_data = XRESIZEVEC (char, pz_data, space_used+1 );
  pz_data[ space_used ] = NUL;

  return pz_data;
}

#ifdef IS_CXX_HEADER_NEEDED
t_bool
is_cxx_header (tCC* fname, tCC* text)
{
  /*  First, check to see if the file is in a C++ directory */
  for (;;)
    {
      switch (*(fname++))
        {
        case 'C': /* check for "CC/" */
          if ((fname[0] == 'C') && (fname[1] == '/'))
            return BOOL_TRUE;
          break;

        case 'x': /* check for "xx/" */
          if ((fname[0] == 'x') && (fname[1] == '/'))
            return BOOL_TRUE;
          break;

        case '+': /* check for "++" */
          if (fname[0] == '+')
            return BOOL_TRUE;
          break;

        case NUL:
          goto not_cxx_name;
        }
    } not_cxx_name:;

  /* Or it might contain one of several phrases which indicate C++ code.
     Currently recognized are:
     extern "C++"
     -*- (Mode: )? C++ -*-   (emacs mode marker)
     template <
   */
    {
      tSCC cxxpat[] = "\
extern[ \t]*\"C\\+\\+\"|\
-\\*-[ \t]*([mM]ode:[ \t]*)?[cC]\\+\\+[; \t]*-\\*-|\
template[ \t]*<|\
^[ \t]*class[ \t]|\
(public|private|protected):|\
^[ \t]*#[ \t]*pragma[ \t]+(interface|implementation)\
";
      static regex_t cxxre;
      static int compiled;

      if (!compiled)
	compile_re (cxxpat, &cxxre, 0, "contents check", "is_cxx_header");

      if (xregexec (&cxxre, text, 0, 0, 0) == 0)
	return BOOL_TRUE;
    }
		   
  return BOOL_FALSE;
}
#endif /* CXX_TYPE_NEEDED */

#ifdef SKIP_QUOTE_NEEDED
/*
 *  Skip over a quoted string.  Single quote strings may
 *  contain multiple characters if the first character is
 *  a backslash.  Especially a backslash followed by octal digits.
 *  We are not doing a correctness syntax check here.
 */
tCC*
skip_quote(char q, char* text )
{
  for (;;)
    {
      char ch = *(text++);
      switch (ch)
        {
        case '\\':
          text++; /* skip over whatever character follows */
          break;

        case '"':
        case '\'':
          if (ch != q)
            break;
          /*FALLTHROUGH*/

        case '\n':
        case NUL:
          goto skip_done;
        }
    } skip_done:;

  return text;
}
#endif /* SKIP_QUOTE_NEEDED */

/* * * * * * * * * * * * *
 
   Compile one regular expression pattern for later use.  PAT contains
   the pattern, RE points to a regex_t structure (which should have
   been bzeroed).  MATCH is 1 if we need to know where the regex
   matched, 0 if not. If xregcomp fails, prints an error message and
   aborts; E1 and E2 are strings to shove into the error message.

   The patterns we search for are all egrep patterns.
   REG_EXTENDED|REG_NEWLINE produces identical regex syntax/semantics
   to egrep (verified from 4.4BSD Programmer's Reference Manual).  */
void
compile_re( tCC* pat, regex_t* re, int match, tCC* e1, tCC* e2 )
{
  tSCC z_bad_comp[] = "fixincl ERROR:  cannot compile %s regex for %s\n\
\texpr = `%s'\n\terror %s\n";
  int flags, err;

  flags = (match ? REG_EXTENDED|REG_NEWLINE
	   : REG_EXTENDED|REG_NEWLINE|REG_NOSUB);
  err = xregcomp (re, pat, flags);

  if (err)
    {
      char rerrbuf[1024];
      regerror (err, re, rerrbuf, 1024);
      fprintf (stderr, z_bad_comp, e1, e2, pat, rerrbuf);
      exit (EXIT_FAILURE);
    }
}

/* * * * * * * * * * * * *

   Helper routine and data for the machine_name test and fix.  */

tSCC mn_label_pat[] = "^[ \t]*#[ \t]*(if|ifdef|ifndef)[ \t]+";
static regex_t mn_label_re;
static regex_t mn_name_re;

static int mn_compiled = 0;

t_bool
mn_get_regexps(regex_t** label_re, regex_t** name_re, tCC* who )
{
  if (! pz_mn_name_pat)
    return BOOL_FALSE;

  if (! mn_compiled)
    {
      compile_re (mn_label_pat, &mn_label_re, 1, "label pattern", who);
      compile_re (pz_mn_name_pat, &mn_name_re, 1, "name pattern", who);
      mn_compiled++;
    }
  *label_re = &mn_label_re;
  *name_re = &mn_name_re;
  return BOOL_TRUE;
}


#ifdef SEPARATE_FIX_PROC

char*
make_raw_shell_str( char* pz_d, tCC* pz_s, size_t smax )
{
  tSCC zQ[] = "'\\''";
  size_t     dtaSize;
  char*      pz_d_start = pz_d;

  smax--; /* adjust for trailing NUL */

  dtaSize = strlen( pz_s ) + 3;

  {
    const char* pz = pz_s - 1;

    for (;;) {
      pz = strchr( pz+1, '\'' );
      if (pz == (char*)NULL)
        break;
      dtaSize += sizeof( zQ )-1;
    }
  }
  if (dtaSize > smax)
    return (char*)NULL;

  *(pz_d++) = '\'';

  for (;;) {
    if ((size_t) (pz_d - pz_d_start) >= smax)
      return (char*)NULL;
    switch (*(pz_d++) = *(pz_s++)) {
    case NUL:
      goto loopDone;

    case '\'':
      if ((size_t) (pz_d - pz_d_start) >= smax - sizeof( zQ )-1)
	return (char*)NULL;
      strcpy( pz_d-1, zQ );
      pz_d += sizeof( zQ )-2;
    }
  } loopDone:;
  pz_d[-1] = '\'';
  *pz_d    = NUL;

  return pz_d;
}

#endif

#if defined(__MINGW32__)
void
fix_path_separators (char* p)
{
    while (p != NULL)
      {
        p = strchr (p, '\\');
        if (p != NULL)
          {
            *p = '/';
            ++p;
          }
      }
}

/* Count number of needle character ocurrences in str */
static int
count_occurrences_of_char (char* str, char needle)
{
  int cnt = 0;

  while (str)
    {
       str = strchr (str, needle);
       if (str)
         {
           ++str;
           ++cnt;
         }
    }

  return cnt;
}

/* On Mingw32, system function will just start cmd by default.
   Call system function, but prepend ${CONFIG_SHELL} or ${SHELL} -c to the command,
   replace newlines with '$'\n'', enclose command with double quotes
   and escape special characters which were originally enclosed in single quotes.
 */
int
system_with_shell (char* s)
{
  static const char z_shell_start_args[] = " -c \"";
  static const char z_shell_end_args[] = "\"";
  static const char z_shell_newline[] = "'$'\\n''";

  /* Use configured shell if present */
  char *env_shell = getenv ("CONFIG_SHELL");
  int newline_cnt = count_occurrences_of_char (s, '\n');
  int escapes_cnt  = count_occurrences_of_char( s, '\\')
                      + count_occurrences_of_char (s, '"')
                      + count_occurrences_of_char (s, '`');
  char *long_cmd;
  char *cmd_endp;
  int sys_result;
  char *s_scan;
  int in_quotes;

  if (env_shell == NULL)
    env_shell = getenv ("SHELL");

  /* If neither CONFIGURED_SHELL nor SHELL is set, just call standard system function */
  if (env_shell == NULL)
    return system (s);

  /* Allocate enough memory to fit newly created command string */
  long_cmd = XNEWVEC (char, strlen (env_shell)
                      + strlen (z_shell_start_args)
                      + strlen (s)
                      + newline_cnt * (strlen (z_shell_newline) - 1)
                      + escapes_cnt
                      + strlen (z_shell_end_args)
                      + 1);

  /* Start with ${SHELL} */
  strcpy (long_cmd, env_shell);
  cmd_endp = long_cmd + strlen (long_cmd);

  /* Opening quote */
  strcpy (cmd_endp, z_shell_start_args);
  cmd_endp += strlen (z_shell_start_args);

  /* Replace newlines and escape special chars */
  in_quotes = 0;
  for (s_scan = s; *s_scan; ++s_scan)
    {
      switch (*s_scan)
        {
          case '\n':
            if (in_quotes)
              {
                /* Replace newline inside quotes with '$'\n'' */
                strcpy (cmd_endp, z_shell_newline);
                cmd_endp += strlen (z_shell_newline);
              }
            else
              {
                /* Replace newlines outside quotes with ; and merge subsequent newlines */
                *(cmd_endp++) = ';';
                *(cmd_endp++) = ' ';
                while (*(s_scan + 1) == '\n' || *(s_scan + 1) == ' ' || *(s_scan + 1) == '\t')
                  ++s_scan;
              }
            break;
          case '\'':
            /* Escape single quote and toggle in_quotes flag */
            in_quotes = !in_quotes;
            *(cmd_endp++) = *s_scan;
            break;
          case '\\':
          case '`':
            /* Escape backslash and backtick inside quotes */
            if (in_quotes)
               *(cmd_endp++) = '\\';
            *(cmd_endp++) = *s_scan;
            break;
          case '"':
            /* Escape double quotes always */
            *(cmd_endp++) = '\\';
            *(cmd_endp++) = *s_scan;
            break;
          default:
            *(cmd_endp++) = *s_scan;
        }
    }

  /* Closing quote */
  strcpy (cmd_endp, z_shell_end_args);

  sys_result = system (long_cmd);

  free (long_cmd);

  return sys_result;
}

#endif /* defined(__MINGW32__) */