fixlib.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* Install modified versions of certain ANSI-incompatible system header
  2. files which are fixed to work correctly with ANSI C and placed in a
  3. directory that GCC will search.
  4. Copyright (C) 1999, 2000, 2001, 2004, 2009 Free Software Foundation, Inc.
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. GCC is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. #include "fixlib.h"
  18. /* * * * * * * * * * * * *
  19. load_file_data loads all the contents of a file into malloc-ed memory.
  20. Its argument is the file pointer of the file to read in; the returned
  21. result is the NUL terminated contents of the file. The file
  22. is presumed to be an ASCII text file containing no NULs. */
  23. char *
  24. load_file_data (FILE* fp)
  25. {
  26. char *pz_data = (char*)NULL;
  27. int space_left = -1; /* allow for terminating NUL */
  28. size_t space_used = 0;
  29. if (fp == (FILE*)NULL)
  30. return pz_data;
  31. do
  32. {
  33. size_t size_read;
  34. if (space_left < 1024)
  35. {
  36. space_left += 4096;
  37. pz_data = XRESIZEVEC (char, pz_data, space_left + space_used + 1 );
  38. }
  39. size_read = fread (pz_data + space_used, 1, space_left, fp);
  40. if (size_read == 0)
  41. {
  42. if (feof (fp))
  43. break;
  44. if (ferror (fp))
  45. {
  46. int err = errno;
  47. if (err != EISDIR)
  48. fprintf (stderr, "error %d (%s) reading input\n", err,
  49. xstrerror (err));
  50. free ((void *) pz_data);
  51. return (char *) NULL;
  52. }
  53. }
  54. space_left -= size_read;
  55. space_used += size_read;
  56. } while (! feof (fp));
  57. pz_data = XRESIZEVEC (char, pz_data, space_used+1 );
  58. pz_data[ space_used ] = NUL;
  59. return pz_data;
  60. }
  61. #ifdef IS_CXX_HEADER_NEEDED
  62. t_bool
  63. is_cxx_header (tCC* fname, tCC* text)
  64. {
  65. /* First, check to see if the file is in a C++ directory */
  66. for (;;)
  67. {
  68. switch (*(fname++))
  69. {
  70. case 'C': /* check for "CC/" */
  71. if ((fname[0] == 'C') && (fname[1] == '/'))
  72. return BOOL_TRUE;
  73. break;
  74. case 'x': /* check for "xx/" */
  75. if ((fname[0] == 'x') && (fname[1] == '/'))
  76. return BOOL_TRUE;
  77. break;
  78. case '+': /* check for "++" */
  79. if (fname[0] == '+')
  80. return BOOL_TRUE;
  81. break;
  82. case NUL:
  83. goto not_cxx_name;
  84. }
  85. } not_cxx_name:;
  86. /* Or it might contain one of several phrases which indicate C++ code.
  87. Currently recognized are:
  88. extern "C++"
  89. -*- (Mode: )? C++ -*- (emacs mode marker)
  90. template <
  91. */
  92. {
  93. tSCC cxxpat[] = "\
  94. extern[ \t]*\"C\\+\\+\"|\
  95. -\\*-[ \t]*([mM]ode:[ \t]*)?[cC]\\+\\+[; \t]*-\\*-|\
  96. template[ \t]*<|\
  97. ^[ \t]*class[ \t]|\
  98. (public|private|protected):|\
  99. ^[ \t]*#[ \t]*pragma[ \t]+(interface|implementation)\
  100. ";
  101. static regex_t cxxre;
  102. static int compiled;
  103. if (!compiled)
  104. compile_re (cxxpat, &cxxre, 0, "contents check", "is_cxx_header");
  105. if (xregexec (&cxxre, text, 0, 0, 0) == 0)
  106. return BOOL_TRUE;
  107. }
  108. return BOOL_FALSE;
  109. }
  110. #endif /* CXX_TYPE_NEEDED */
  111. #ifdef SKIP_QUOTE_NEEDED
  112. /*
  113. * Skip over a quoted string. Single quote strings may
  114. * contain multiple characters if the first character is
  115. * a backslash. Especially a backslash followed by octal digits.
  116. * We are not doing a correctness syntax check here.
  117. */
  118. tCC*
  119. skip_quote(char q, char* text )
  120. {
  121. for (;;)
  122. {
  123. char ch = *(text++);
  124. switch (ch)
  125. {
  126. case '\\':
  127. text++; /* skip over whatever character follows */
  128. break;
  129. case '"':
  130. case '\'':
  131. if (ch != q)
  132. break;
  133. /*FALLTHROUGH*/
  134. case '\n':
  135. case NUL:
  136. goto skip_done;
  137. }
  138. } skip_done:;
  139. return text;
  140. }
  141. #endif /* SKIP_QUOTE_NEEDED */
  142. /* * * * * * * * * * * * *
  143. Compile one regular expression pattern for later use. PAT contains
  144. the pattern, RE points to a regex_t structure (which should have
  145. been bzeroed). MATCH is 1 if we need to know where the regex
  146. matched, 0 if not. If xregcomp fails, prints an error message and
  147. aborts; E1 and E2 are strings to shove into the error message.
  148. The patterns we search for are all egrep patterns.
  149. REG_EXTENDED|REG_NEWLINE produces identical regex syntax/semantics
  150. to egrep (verified from 4.4BSD Programmer's Reference Manual). */
  151. void
  152. compile_re( tCC* pat, regex_t* re, int match, tCC* e1, tCC* e2 )
  153. {
  154. tSCC z_bad_comp[] = "fixincl ERROR: cannot compile %s regex for %s\n\
  155. \texpr = `%s'\n\terror %s\n";
  156. int flags, err;
  157. flags = (match ? REG_EXTENDED|REG_NEWLINE
  158. : REG_EXTENDED|REG_NEWLINE|REG_NOSUB);
  159. err = xregcomp (re, pat, flags);
  160. if (err)
  161. {
  162. char rerrbuf[1024];
  163. regerror (err, re, rerrbuf, 1024);
  164. fprintf (stderr, z_bad_comp, e1, e2, pat, rerrbuf);
  165. exit (EXIT_FAILURE);
  166. }
  167. }
  168. /* * * * * * * * * * * * *
  169. Helper routine and data for the machine_name test and fix. */
  170. tSCC mn_label_pat[] = "^[ \t]*#[ \t]*(if|ifdef|ifndef)[ \t]+";
  171. static regex_t mn_label_re;
  172. static regex_t mn_name_re;
  173. static int mn_compiled = 0;
  174. t_bool
  175. mn_get_regexps(regex_t** label_re, regex_t** name_re, tCC* who )
  176. {
  177. if (! pz_mn_name_pat)
  178. return BOOL_FALSE;
  179. if (! mn_compiled)
  180. {
  181. compile_re (mn_label_pat, &mn_label_re, 1, "label pattern", who);
  182. compile_re (pz_mn_name_pat, &mn_name_re, 1, "name pattern", who);
  183. mn_compiled++;
  184. }
  185. *label_re = &mn_label_re;
  186. *name_re = &mn_name_re;
  187. return BOOL_TRUE;
  188. }
  189. #ifdef SEPARATE_FIX_PROC
  190. char*
  191. make_raw_shell_str( char* pz_d, tCC* pz_s, size_t smax )
  192. {
  193. tSCC zQ[] = "'\\''";
  194. size_t dtaSize;
  195. char* pz_d_start = pz_d;
  196. smax--; /* adjust for trailing NUL */
  197. dtaSize = strlen( pz_s ) + 3;
  198. {
  199. const char* pz = pz_s - 1;
  200. for (;;) {
  201. pz = strchr( pz+1, '\'' );
  202. if (pz == (char*)NULL)
  203. break;
  204. dtaSize += sizeof( zQ )-1;
  205. }
  206. }
  207. if (dtaSize > smax)
  208. return (char*)NULL;
  209. *(pz_d++) = '\'';
  210. for (;;) {
  211. if ((size_t) (pz_d - pz_d_start) >= smax)
  212. return (char*)NULL;
  213. switch (*(pz_d++) = *(pz_s++)) {
  214. case NUL:
  215. goto loopDone;
  216. case '\'':
  217. if ((size_t) (pz_d - pz_d_start) >= smax - sizeof( zQ )-1)
  218. return (char*)NULL;
  219. strcpy( pz_d-1, zQ );
  220. pz_d += sizeof( zQ )-2;
  221. }
  222. } loopDone:;
  223. pz_d[-1] = '\'';
  224. *pz_d = NUL;
  225. return pz_d;
  226. }
  227. #endif