files.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* Open and close files for bison,
  2. Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the BISON General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute BISON,
  9. but only under the conditions described in the BISON General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with BISON so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, you are welcome to use, share and improve this program.
  15. You are forbidden to forbid anyone else to use, share and improve
  16. what you give them. Help stamp out software-hoarding! */
  17. #ifdef VMS
  18. #include <ssdef.h>
  19. #define unlink delete
  20. #define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
  21. #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
  22. #endif
  23. #include <stdio.h>
  24. #include "files.h"
  25. #include "new.h"
  26. #include "gram.h"
  27. FILE *finput = NULL;
  28. FILE *foutput = NULL;
  29. FILE *fdefines = NULL;
  30. FILE *ftable = NULL;
  31. FILE *fattrs = NULL;
  32. FILE *fguard = NULL;
  33. FILE *faction = NULL;
  34. FILE *fparser = NULL;
  35. /* File name specified with -o for the output file, or 0 if no -o. */
  36. char *spec_outfile;
  37. char *infile;
  38. char *outfile;
  39. char *defsfile;
  40. char *tabfile;
  41. char *attrsfile;
  42. char *guardfile;
  43. char *actfile;
  44. char *tmpattrsfile;
  45. char *tmptabfile;
  46. char *mktemp(); /* So the compiler won't complain */
  47. FILE *tryopen(); /* This might be a good idea */
  48. extern int verboseflag;
  49. extern int definesflag;
  50. int fixed_outfiles = 0;
  51. char*
  52. stringappend(string1, end1, string2)
  53. char *string1;
  54. int end1;
  55. char *string2;
  56. {
  57. register char *ostring;
  58. register char *cp, *cp1;
  59. register int i;
  60. cp = string2; i = 0;
  61. while (*cp++) i++;
  62. ostring = NEW2(i+end1+1, char);
  63. cp = ostring;
  64. cp1 = string1;
  65. for (i = 0; i < end1; i++)
  66. *cp++ = *cp1++;
  67. cp1 = string2;
  68. while (*cp++ = *cp1++) ;
  69. return ostring;
  70. }
  71. /* JF this has been hacked to death. Nowaday it sets up the file names for
  72. the output files, and opens the tmp files and the parser */
  73. openfiles()
  74. {
  75. char *name_base;
  76. register char *cp;
  77. char *filename;
  78. int base_length;
  79. int short_base_length;
  80. #ifdef VMS
  81. char *tmp_base = "sys$scratch:b_";
  82. #else
  83. char *tmp_base = "/tmp/b.";
  84. #endif
  85. int tmp_len = strlen (tmp_base);
  86. if (spec_outfile)
  87. {
  88. /* -o was specified. The precise -o name will be used for ftable.
  89. For other output files, remove the ".c" or ".tab.c" suffix. */
  90. name_base = spec_outfile;
  91. /* BASE_LENGTH includes ".tab" but not ".c". */
  92. base_length = strlen (name_base);
  93. if (!strcmp (name_base + base_length - 2, ".c"))
  94. base_length -= 2;
  95. /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
  96. short_base_length = base_length;
  97. if (!strcmp (name_base + short_base_length - 4, ".tab"))
  98. short_base_length -= 4;
  99. else if (!strcmp (name_base + short_base_length - 4, "_tab"))
  100. short_base_length -= 4;
  101. }
  102. else
  103. {
  104. /* -o was not specified; compute output file name from input
  105. or use y.tab.c, etc., if -y was specified. */
  106. name_base = fixed_outfiles ? "y.y" : infile;
  107. /* Discard any directory names from the input file name
  108. to make the base of the output. */
  109. cp = name_base;
  110. while (*cp)
  111. {
  112. if (*cp == '/')
  113. name_base = cp+1;
  114. cp++;
  115. }
  116. /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
  117. base_length = strlen (name_base);
  118. if (!strcmp (name_base + base_length - 2, ".y"))
  119. base_length -= 2;
  120. short_base_length = base_length;
  121. #ifdef VMS
  122. name_base = stringappend(name_base, short_base_length, "_tab");
  123. #else
  124. name_base = stringappend(name_base, short_base_length, ".tab");
  125. #endif
  126. base_length = short_base_length + 4;
  127. }
  128. finput = tryopen(infile, "r");
  129. filename = (char *) getenv ("BISON_SIMPLE");
  130. fparser = tryopen(filename ? filename : PFILE, "r");
  131. if (verboseflag)
  132. {
  133. outfile = stringappend(name_base, short_base_length, ".output");
  134. foutput = tryopen(outfile, "w");
  135. }
  136. if (definesflag)
  137. {
  138. defsfile = stringappend(name_base, base_length, ".h");
  139. fdefines = tryopen(defsfile, "w");
  140. }
  141. actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  142. faction = tryopen(actfile, "w+");
  143. unlink(actfile);
  144. tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  145. fattrs = tryopen(tmpattrsfile,"w+");
  146. unlink(tmpattrsfile);
  147. tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  148. ftable = tryopen(tmptabfile, "w+");
  149. unlink(tmptabfile);
  150. /* These are opened by `done' or `open_extra_files', if at all */
  151. if (spec_outfile)
  152. tabfile = spec_outfile;
  153. else
  154. tabfile = stringappend(name_base, base_length, ".c");
  155. #ifdef VMS
  156. attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  157. guardfile = stringappend(name_base, short_base_length, "_guard.c");
  158. #else
  159. attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  160. guardfile = stringappend(name_base, short_base_length, ".guard.c");
  161. #endif
  162. }
  163. /* open the output files needed only for the semantic parser.
  164. This is done when %semantic_parser is seen in the declarations section. */
  165. open_extra_files()
  166. {
  167. FILE *ftmp;
  168. int c;
  169. char *filename;
  170. /* JF change open parser file */
  171. fclose(fparser);
  172. filename = (char *) getenv ("BISON_HAIRY");
  173. fparser= tryopen(filename ? filename : PFILE1, "r");
  174. /* JF change from inline attrs file to separate one */
  175. ftmp = tryopen(attrsfile, "w");
  176. rewind(fattrs);
  177. while((c=getc(fattrs))!=EOF) /* Thank god for buffering */
  178. putc(c,ftmp);
  179. fclose(fattrs);
  180. fattrs=ftmp;
  181. fguard = tryopen(guardfile, "w");
  182. }
  183. /* JF to make file opening easier. This func tries to open file
  184. NAME with mode MODE, and prints an error message if it fails. */
  185. FILE *
  186. tryopen(name, mode)
  187. char *name;
  188. char *mode;
  189. {
  190. FILE *ptr;
  191. ptr = fopen(name, mode);
  192. if (ptr == NULL)
  193. {
  194. fprintf(stderr, "bison: ");
  195. perror(name);
  196. done(2);
  197. }
  198. return ptr;
  199. }
  200. done(k)
  201. int k;
  202. {
  203. if (faction)
  204. fclose(faction);
  205. if (fattrs)
  206. fclose(fattrs);
  207. if (fguard)
  208. fclose(fguard);
  209. if (finput)
  210. fclose(finput);
  211. if (fparser)
  212. fclose(fparser);
  213. /* JF we don't need this anymore
  214. if (fparser1)
  215. fclose(fparser1); */
  216. if (foutput)
  217. fclose(foutput);
  218. /* JF write out the output file */
  219. if (k == 0 && ftable)
  220. {
  221. FILE *ftmp;
  222. register int c;
  223. ftmp=tryopen(tabfile, "w");
  224. rewind(ftable);
  225. while((c=getc(ftable)) != EOF)
  226. putc(c,ftmp);
  227. fclose(ftmp);
  228. fclose(ftable);
  229. }
  230. #ifdef VMS
  231. delete(actfile);
  232. delete(tmpattrsfile);
  233. delete(tmptabfile);
  234. if (k==0) sys$exit(SS$_NORMAL);
  235. sys$exit(SS$_ABORT);
  236. #else
  237. exit(k);
  238. #endif
  239. }