fixlib.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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
  228. #if defined(__MINGW32__)
  229. void
  230. fix_path_separators (char* p)
  231. {
  232. while (p != NULL)
  233. {
  234. p = strchr (p, '\\');
  235. if (p != NULL)
  236. {
  237. *p = '/';
  238. ++p;
  239. }
  240. }
  241. }
  242. /* Count number of needle character ocurrences in str */
  243. static int
  244. count_occurrences_of_char (char* str, char needle)
  245. {
  246. int cnt = 0;
  247. while (str)
  248. {
  249. str = strchr (str, needle);
  250. if (str)
  251. {
  252. ++str;
  253. ++cnt;
  254. }
  255. }
  256. return cnt;
  257. }
  258. /* On Mingw32, system function will just start cmd by default.
  259. Call system function, but prepend ${CONFIG_SHELL} or ${SHELL} -c to the command,
  260. replace newlines with '$'\n'', enclose command with double quotes
  261. and escape special characters which were originally enclosed in single quotes.
  262. */
  263. int
  264. system_with_shell (char* s)
  265. {
  266. static const char z_shell_start_args[] = " -c \"";
  267. static const char z_shell_end_args[] = "\"";
  268. static const char z_shell_newline[] = "'$'\\n''";
  269. /* Use configured shell if present */
  270. char *env_shell = getenv ("CONFIG_SHELL");
  271. int newline_cnt = count_occurrences_of_char (s, '\n');
  272. int escapes_cnt = count_occurrences_of_char( s, '\\')
  273. + count_occurrences_of_char (s, '"')
  274. + count_occurrences_of_char (s, '`');
  275. char *long_cmd;
  276. char *cmd_endp;
  277. int sys_result;
  278. char *s_scan;
  279. int in_quotes;
  280. if (env_shell == NULL)
  281. env_shell = getenv ("SHELL");
  282. /* If neither CONFIGURED_SHELL nor SHELL is set, just call standard system function */
  283. if (env_shell == NULL)
  284. return system (s);
  285. /* Allocate enough memory to fit newly created command string */
  286. long_cmd = XNEWVEC (char, strlen (env_shell)
  287. + strlen (z_shell_start_args)
  288. + strlen (s)
  289. + newline_cnt * (strlen (z_shell_newline) - 1)
  290. + escapes_cnt
  291. + strlen (z_shell_end_args)
  292. + 1);
  293. /* Start with ${SHELL} */
  294. strcpy (long_cmd, env_shell);
  295. cmd_endp = long_cmd + strlen (long_cmd);
  296. /* Opening quote */
  297. strcpy (cmd_endp, z_shell_start_args);
  298. cmd_endp += strlen (z_shell_start_args);
  299. /* Replace newlines and escape special chars */
  300. in_quotes = 0;
  301. for (s_scan = s; *s_scan; ++s_scan)
  302. {
  303. switch (*s_scan)
  304. {
  305. case '\n':
  306. if (in_quotes)
  307. {
  308. /* Replace newline inside quotes with '$'\n'' */
  309. strcpy (cmd_endp, z_shell_newline);
  310. cmd_endp += strlen (z_shell_newline);
  311. }
  312. else
  313. {
  314. /* Replace newlines outside quotes with ; and merge subsequent newlines */
  315. *(cmd_endp++) = ';';
  316. *(cmd_endp++) = ' ';
  317. while (*(s_scan + 1) == '\n' || *(s_scan + 1) == ' ' || *(s_scan + 1) == '\t')
  318. ++s_scan;
  319. }
  320. break;
  321. case '\'':
  322. /* Escape single quote and toggle in_quotes flag */
  323. in_quotes = !in_quotes;
  324. *(cmd_endp++) = *s_scan;
  325. break;
  326. case '\\':
  327. case '`':
  328. /* Escape backslash and backtick inside quotes */
  329. if (in_quotes)
  330. *(cmd_endp++) = '\\';
  331. *(cmd_endp++) = *s_scan;
  332. break;
  333. case '"':
  334. /* Escape double quotes always */
  335. *(cmd_endp++) = '\\';
  336. *(cmd_endp++) = *s_scan;
  337. break;
  338. default:
  339. *(cmd_endp++) = *s_scan;
  340. }
  341. }
  342. /* Closing quote */
  343. strcpy (cmd_endp, z_shell_end_args);
  344. sys_result = system (long_cmd);
  345. free (long_cmd);
  346. return sys_result;
  347. }
  348. #endif /* defined(__MINGW32__) */