files.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* Open and close files for bison,
  2. Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3. This file is part of Bison, the GNU Compiler Compiler.
  4. Bison is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. Bison is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Bison; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. #if defined (VMS) & !defined (__VMS_POSIX)
  16. #include <ssdef.h>
  17. #define unlink delete
  18. #ifndef XPFILE
  19. #define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
  20. #endif
  21. #ifndef XPFILE1
  22. #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
  23. #endif
  24. #endif
  25. #include <stdio.h>
  26. #include "system.h"
  27. #include "files.h"
  28. #include "new.h"
  29. #include "gram.h"
  30. FILE *finput = NULL;
  31. FILE *foutput = NULL;
  32. FILE *fdefines = NULL;
  33. FILE *ftable = NULL;
  34. FILE *fattrs = NULL;
  35. FILE *fguard = NULL;
  36. FILE *faction = NULL;
  37. FILE *fparser = NULL;
  38. /* File name specified with -o for the output file, or 0 if no -o. */
  39. char *spec_outfile;
  40. char *infile;
  41. char *outfile;
  42. char *defsfile;
  43. char *tabfile;
  44. char *attrsfile;
  45. char *guardfile;
  46. char *actfile;
  47. char *tmpattrsfile;
  48. char *tmptabfile;
  49. char *tmpdefsfile;
  50. extern char *mktemp(); /* So the compiler won't complain */
  51. extern char *getenv();
  52. extern void perror();
  53. FILE *tryopen(); /* This might be a good idea */
  54. void done();
  55. extern char *program_name;
  56. extern int verboseflag;
  57. extern int definesflag;
  58. int fixed_outfiles = 0;
  59. char*
  60. stringappend(string1, end1, string2)
  61. char *string1;
  62. int end1;
  63. char *string2;
  64. {
  65. register char *ostring;
  66. register char *cp, *cp1;
  67. register int i;
  68. cp = string2; i = 0;
  69. while (*cp++) i++;
  70. ostring = NEW2(i+end1+1, char);
  71. cp = ostring;
  72. cp1 = string1;
  73. for (i = 0; i < end1; i++)
  74. *cp++ = *cp1++;
  75. cp1 = string2;
  76. while (*cp++ = *cp1++) ;
  77. return ostring;
  78. }
  79. /* JF this has been hacked to death. Nowaday it sets up the file names for
  80. the output files, and opens the tmp files and the parser */
  81. void
  82. openfiles()
  83. {
  84. char *name_base;
  85. register char *cp;
  86. char *filename;
  87. int base_length;
  88. int short_base_length;
  89. #if defined (VMS) & !defined (__VMS_POSIX)
  90. char *tmp_base = "sys$scratch:b_";
  91. #else
  92. char *tmp_base = "/tmp/b.";
  93. #endif
  94. int tmp_len;
  95. #ifdef MSDOS
  96. tmp_base = getenv ("TMP");
  97. if (tmp_base == 0)
  98. tmp_base = "";
  99. strlwr (infile);
  100. #endif /* MSDOS */
  101. tmp_len = strlen (tmp_base);
  102. if (spec_outfile)
  103. {
  104. /* -o was specified. The precise -o name will be used for ftable.
  105. For other output files, remove the ".c" or ".tab.c" suffix. */
  106. name_base = spec_outfile;
  107. #ifdef MSDOS
  108. strlwr (name_base);
  109. #endif /* MSDOS */
  110. /* BASE_LENGTH includes ".tab" but not ".c". */
  111. base_length = strlen (name_base);
  112. if (!strcmp (name_base + base_length - 2, ".c"))
  113. base_length -= 2;
  114. /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
  115. short_base_length = base_length;
  116. if (!strncmp (name_base + short_base_length - 4, ".tab", 4))
  117. short_base_length -= 4;
  118. else if (!strncmp (name_base + short_base_length - 4, "_tab", 4))
  119. short_base_length -= 4;
  120. }
  121. else if (spec_file_prefix)
  122. {
  123. /* -b was specified. Construct names from it. */
  124. /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
  125. short_base_length = strlen (spec_file_prefix);
  126. /* Count room for `.tab'. */
  127. base_length = short_base_length + 4;
  128. name_base = (char *) xmalloc (base_length + 1);
  129. /* Append `.tab'. */
  130. strcpy (name_base, spec_file_prefix);
  131. #ifdef VMS
  132. strcat (name_base, "_tab");
  133. #else
  134. strcat (name_base, ".tab");
  135. #endif
  136. #ifdef MSDOS
  137. strlwr (name_base);
  138. #endif /* MSDOS */
  139. }
  140. else
  141. {
  142. /* -o was not specified; compute output file name from input
  143. or use y.tab.c, etc., if -y was specified. */
  144. name_base = fixed_outfiles ? "y.y" : infile;
  145. /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
  146. base_length = strlen (name_base);
  147. if (!strcmp (name_base + base_length - 2, ".y"))
  148. base_length -= 2;
  149. short_base_length = base_length;
  150. #ifdef VMS
  151. name_base = stringappend(name_base, short_base_length, "_tab");
  152. #else
  153. #ifdef MSDOS
  154. name_base = stringappend(name_base, short_base_length, "_tab");
  155. #else
  156. name_base = stringappend(name_base, short_base_length, ".tab");
  157. #endif /* not MSDOS */
  158. #endif
  159. base_length = short_base_length + 4;
  160. }
  161. finput = tryopen(infile, "r");
  162. filename = getenv("BISON_SIMPLE");
  163. #ifdef MSDOS
  164. /* File doesn't exist in current directory; try in INIT directory. */
  165. cp = getenv("INIT");
  166. if (filename == 0 && cp != NULL)
  167. {
  168. filename = xmalloc(strlen(cp) + strlen(PFILE) + 2);
  169. strcpy(filename, cp);
  170. cp = filename + strlen(filename);
  171. *cp++ = '/';
  172. strcpy(cp, PFILE);
  173. }
  174. #endif /* MSDOS */
  175. fparser = tryopen(filename ? filename : PFILE, "r");
  176. if (verboseflag)
  177. {
  178. #ifdef MSDOS
  179. outfile = stringappend(name_base, short_base_length, ".out");
  180. #else
  181. /* We used to use just .out if spec_name_prefix (-p) was used,
  182. but that conflicts with Posix. */
  183. outfile = stringappend(name_base, short_base_length, ".output");
  184. #endif
  185. foutput = tryopen(outfile, "w");
  186. }
  187. #ifdef MSDOS
  188. actfile = mktemp(stringappend(tmp_base, tmp_len, "acXXXXXX"));
  189. tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "atXXXXXX"));
  190. tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "taXXXXXX"));
  191. tmpdefsfile = mktemp(stringappend(tmp_base, tmp_len, "deXXXXXX"));
  192. #else
  193. actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  194. tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  195. tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  196. tmpdefsfile = mktemp(stringappend(tmp_base, tmp_len, "defs.XXXXXX"));
  197. #endif /* not MSDOS */
  198. faction = tryopen(actfile, "w+");
  199. fattrs = tryopen(tmpattrsfile,"w+");
  200. ftable = tryopen(tmptabfile, "w+");
  201. if (definesflag)
  202. {
  203. defsfile = stringappend(name_base, base_length, ".h");
  204. fdefines = tryopen(tmpdefsfile, "w+");
  205. }
  206. #ifndef MSDOS
  207. unlink(actfile);
  208. unlink(tmpattrsfile);
  209. unlink(tmptabfile);
  210. unlink(tmpdefsfile);
  211. #endif
  212. /* These are opened by `done' or `open_extra_files', if at all */
  213. if (spec_outfile)
  214. tabfile = spec_outfile;
  215. else
  216. tabfile = stringappend(name_base, base_length, ".c");
  217. #ifdef VMS
  218. attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  219. guardfile = stringappend(name_base, short_base_length, "_guard.c");
  220. #else
  221. #ifdef MSDOS
  222. attrsfile = stringappend(name_base, short_base_length, ".sth");
  223. guardfile = stringappend(name_base, short_base_length, ".guc");
  224. #else
  225. attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  226. guardfile = stringappend(name_base, short_base_length, ".guard.c");
  227. #endif /* not MSDOS */
  228. #endif /* not VMS */
  229. }
  230. /* open the output files needed only for the semantic parser.
  231. This is done when %semantic_parser is seen in the declarations section. */
  232. void
  233. open_extra_files()
  234. {
  235. FILE *ftmp;
  236. int c;
  237. char *filename, *cp;
  238. fclose(fparser);
  239. filename = (char *) getenv ("BISON_HAIRY");
  240. #ifdef MSDOS
  241. /* File doesn't exist in current directory; try in INIT directory. */
  242. cp = getenv("INIT");
  243. if (filename == 0 && cp != NULL)
  244. {
  245. filename = xmalloc(strlen(cp) + strlen(PFILE1) + 2);
  246. strcpy(filename, cp);
  247. cp = filename + strlen(filename);
  248. *cp++ = '/';
  249. strcpy(cp, PFILE1);
  250. }
  251. #endif
  252. fparser= tryopen(filename ? filename : PFILE1, "r");
  253. /* JF change from inline attrs file to separate one */
  254. ftmp = tryopen(attrsfile, "w");
  255. rewind(fattrs);
  256. while((c=getc(fattrs))!=EOF) /* Thank god for buffering */
  257. putc(c,ftmp);
  258. fclose(fattrs);
  259. fattrs=ftmp;
  260. fguard = tryopen(guardfile, "w");
  261. }
  262. /* JF to make file opening easier. This func tries to open file
  263. NAME with mode MODE, and prints an error message if it fails. */
  264. FILE *
  265. tryopen(name, mode)
  266. char *name;
  267. char *mode;
  268. {
  269. FILE *ptr;
  270. ptr = fopen(name, mode);
  271. if (ptr == NULL)
  272. {
  273. fprintf(stderr, "%s: ", program_name);
  274. perror(name);
  275. done(2);
  276. }
  277. return ptr;
  278. }
  279. void
  280. done(k)
  281. int k;
  282. {
  283. if (faction)
  284. fclose(faction);
  285. if (fattrs)
  286. fclose(fattrs);
  287. if (fguard)
  288. fclose(fguard);
  289. if (finput)
  290. fclose(finput);
  291. if (fparser)
  292. fclose(fparser);
  293. if (foutput)
  294. fclose(foutput);
  295. /* JF write out the output file */
  296. if (k == 0 && ftable)
  297. {
  298. FILE *ftmp;
  299. register int c;
  300. ftmp=tryopen(tabfile, "w");
  301. rewind(ftable);
  302. while((c=getc(ftable)) != EOF)
  303. putc(c,ftmp);
  304. fclose(ftmp);
  305. fclose(ftable);
  306. if (definesflag)
  307. {
  308. ftmp = tryopen(defsfile, "w");
  309. fflush(fdefines);
  310. rewind(fdefines);
  311. while((c=getc(fdefines)) != EOF)
  312. putc(c,ftmp);
  313. fclose(ftmp);
  314. fclose(fdefines);
  315. }
  316. }
  317. #if defined (VMS) & !defined (__VMS_POSIX)
  318. if (faction)
  319. delete(actfile);
  320. if (fattrs)
  321. delete(tmpattrsfile);
  322. if (ftable)
  323. delete(tmptabfile);
  324. if (k==0) sys$exit(SS$_NORMAL);
  325. sys$exit(SS$_ABORT);
  326. #else
  327. #ifdef MSDOS
  328. if (actfile) unlink(actfile);
  329. if (tmpattrsfile) unlink(tmpattrsfile);
  330. if (tmptabfile) unlink(tmptabfile);
  331. if (tmpdefsfile) unlink(tmpdefsfile);
  332. #endif /* MSDOS */
  333. exit(k);
  334. #endif /* not VMS, or __VMS_POSIX */
  335. }