files.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 1, 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. #ifdef VMS
  16. #include <ssdef.h>
  17. #define unlink delete
  18. #define PFILE1 "GNU_BISON:[000000]BISON.SIMPLE"
  19. #define PFILE2 "GNU_BISON:[000000]BISON.HAIRY"
  20. #endif
  21. #include <stdio.h>
  22. #include "files.h"
  23. #include "new.h"
  24. #include "gram.h"
  25. FILE *finput = NULL;
  26. FILE *foutput = NULL;
  27. FILE *fdefines = NULL;
  28. FILE *ftable = NULL;
  29. FILE *fattrs = NULL;
  30. FILE *fguard = NULL;
  31. FILE *faction = NULL;
  32. FILE *fparser = NULL;
  33. /* File name specified with -o for the output file, or 0 if no -o. */
  34. char *spec_outfile;
  35. char *infile;
  36. char *outfile;
  37. char *defsfile;
  38. char *tabfile;
  39. char *attrsfile;
  40. char *guardfile;
  41. char *actfile;
  42. char *tmpattrsfile;
  43. char *tmptabfile;
  44. extern char *mktemp(); /* So the compiler won't complain */
  45. extern char *getenv();
  46. extern char *parser_name;
  47. FILE *tryopen(); /* This might be a good idea */
  48. void done();
  49. extern int verboseflag;
  50. extern int definesflag;
  51. int fixed_outfiles = 0;
  52. char*
  53. stringappend(string1, end1, string2)
  54. char *string1;
  55. int end1;
  56. char *string2;
  57. {
  58. register char *ostring;
  59. register char *cp, *cp1;
  60. register int i;
  61. cp = string2; i = 0;
  62. while (*cp++) i++;
  63. ostring = NEW2(i+end1+1, char);
  64. cp = ostring;
  65. cp1 = string1;
  66. for (i = 0; i < end1; i++)
  67. *cp++ = *cp1++;
  68. cp1 = string2;
  69. while (*cp++ = *cp1++) ;
  70. return ostring;
  71. }
  72. /* JF this has been hacked to death. Nowaday it sets up the file names for
  73. the output files, and opens the tmp files and the parser */
  74. void
  75. openfiles()
  76. {
  77. char *name_base;
  78. register char *cp;
  79. char *filename;
  80. int base_length;
  81. int short_base_length;
  82. #ifdef VMS
  83. char *tmp_base = "sys$scratch:b_";
  84. #endif
  85. #ifdef AMIGA
  86. char *tmp_base = "T:b.";
  87. #else
  88. char *tmp_base = "/tmp/b.";
  89. #endif
  90. int tmp_len = strlen (tmp_base);
  91. if (spec_outfile)
  92. {
  93. /* -o was specified. The precise -o name will be used for ftable.
  94. For other output files, remove the ".cc" or ".tab.cc" suffix. */
  95. name_base = spec_outfile;
  96. /* BASE_LENGTH includes ".tab" but not ".cc". */
  97. base_length = strlen (name_base);
  98. if (!strcmp (name_base + base_length - 3, ".cc"))
  99. base_length -= 3;
  100. /* SHORT_BASE_LENGTH includes neither ".tab" nor ".cc". */
  101. short_base_length = base_length;
  102. if (!strcmp (name_base + short_base_length - 4, ".tab"))
  103. short_base_length -= 4;
  104. else if (!strcmp (name_base + short_base_length - 4, "_tab"))
  105. short_base_length -= 4;
  106. }
  107. else
  108. {
  109. /* -o was not specified; compute output file name from input
  110. or use y.tab.cc, etc., if -y was specified. */
  111. name_base = fixed_outfiles ? "y.y" : infile;
  112. /* Discard any directory names from the input file name
  113. to make the base of the output. */
  114. cp = name_base;
  115. while (*cp)
  116. {
  117. if (*cp == '/')
  118. name_base = cp+1;
  119. cp++;
  120. }
  121. /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
  122. base_length = strlen (name_base);
  123. if (!strcmp (name_base + base_length - 2, ".y"))
  124. base_length -= 2;
  125. short_base_length = base_length;
  126. #ifdef VMS
  127. name_base = stringappend(name_base, short_base_length, "_tab");
  128. #else
  129. name_base = stringappend(name_base, short_base_length, ".tab");
  130. #endif
  131. base_length = short_base_length + 4;
  132. }
  133. finput = tryopen(infile, "r");
  134. filename = getenv ("BISON_SIMPLE");
  135. fparser = tryopen(filename ? filename : PFILE1, "r");
  136. parser_name = (char *) malloc ((unsigned) (short_base_length + 1) * sizeof(char));
  137. {
  138. char *s, *r;
  139. for(s = name_base, r = parser_name; *s != '.';) *r++ = *s++ ;
  140. *r = '\0';
  141. }
  142. if (verboseflag)
  143. {
  144. outfile = stringappend(name_base, short_base_length, ".output");
  145. foutput = tryopen(outfile, "w");
  146. }
  147. defsfile = stringappend(name_base, base_length, ".h");
  148. if (definesflag)
  149. {
  150. fdefines = tryopen(defsfile, "w");
  151. }
  152. actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  153. faction = tryopen(actfile, "w+");
  154. unlink(actfile);
  155. tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  156. fattrs = tryopen(tmpattrsfile,"w+");
  157. unlink(tmpattrsfile);
  158. tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  159. ftable = tryopen(tmptabfile, "w+");
  160. unlink(tmptabfile);
  161. /* These are opened by `done' or `open_extra_files', if at all */
  162. if (spec_outfile)
  163. tabfile = spec_outfile;
  164. else
  165. tabfile = stringappend(name_base, base_length, ".cc");
  166. #ifdef VMS
  167. attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  168. guardfile = stringappend(name_base, short_base_length, "_guard.cc");
  169. #else
  170. attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  171. guardfile = stringappend(name_base, short_base_length, ".guard.cc");
  172. #endif
  173. }
  174. /* open the output files needed only for the semantic parser.
  175. This is done when %semantic_parser is seen in the declarations section. */
  176. void
  177. open_extra_files()
  178. {
  179. FILE *ftmp;
  180. int c;
  181. char *filename;
  182. /* JF change open parser file */
  183. fclose(fparser);
  184. filename = (char *) getenv ("BISON_HAIRY");
  185. fparser= tryopen(filename ? filename : PFILE1, "r");
  186. /* JF change from inline attrs file to separate one */
  187. ftmp = tryopen(attrsfile, "w");
  188. rewind(fattrs);
  189. while((c=getc(fattrs))!=EOF) /* Thank god for buffering */
  190. putc(c,ftmp);
  191. fclose(fattrs);
  192. fattrs=ftmp;
  193. fguard = tryopen(guardfile, "w");
  194. }
  195. /* JF to make file opening easier. This func tries to open file
  196. NAME with mode MODE, and prints an error message if it fails. */
  197. FILE *
  198. tryopen(name, mode)
  199. char *name;
  200. char *mode;
  201. {
  202. FILE *ptr;
  203. ptr = fopen(name, mode);
  204. if (ptr == NULL)
  205. {
  206. fprintf(stderr, "bison: ");
  207. perror(name);
  208. done(2);
  209. }
  210. return ptr;
  211. }
  212. void
  213. done(k)
  214. int k;
  215. {
  216. if (faction)
  217. fclose(faction);
  218. if (fattrs)
  219. fclose(fattrs);
  220. if (fguard)
  221. fclose(fguard);
  222. if (finput)
  223. fclose(finput);
  224. if (fparser)
  225. fclose(fparser);
  226. if (foutput)
  227. fclose(foutput);
  228. /* JF write out the output file */
  229. if (k == 0 && ftable)
  230. {
  231. FILE *ftmp;
  232. register int c;
  233. ftmp=tryopen(tabfile, "w");
  234. rewind(ftable);
  235. while((c=getc(ftable)) != EOF)
  236. putc(c,ftmp);
  237. fclose(ftmp);
  238. fclose(ftable);
  239. }
  240. #ifdef VMS
  241. delete(actfile);
  242. delete(tmpattrsfile);
  243. delete(tmptabfile);
  244. if (k==0) sys$exit(SS$_NORMAL);
  245. sys$exit(SS$_ABORT);
  246. #else
  247. exit(k);
  248. #endif
  249. }