files.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #include <stdio.h>
  18. #include "files.h"
  19. #include "new.h"
  20. #include "gram.h"
  21. FILE *finput = NULL;
  22. FILE *foutput = NULL;
  23. FILE *fdefines = NULL;
  24. FILE *ftable = NULL;
  25. FILE *fattrs = NULL;
  26. FILE *fguard = NULL;
  27. FILE *faction = NULL;
  28. FILE *fparser = NULL;
  29. /* FILE *fparser1 = NULL; JF we don't need this anymore */
  30. char *infile;
  31. char *outfile;
  32. char *defsfile;
  33. char *tabfile;
  34. char *attrsfile;
  35. char *guardfile;
  36. char *actfile;
  37. char *tmpattrsfile;
  38. char *tmptabfile;
  39. /* JF nobody really uses these */
  40. /* char *pfile;
  41. char *pfile1; */
  42. char *mktemp(); /* So the compiler won't complain */
  43. FILE *tryopen(); /* This might be a good idea */
  44. extern int verboseflag;
  45. extern int definesflag;
  46. int fixed_outfiles = 0;
  47. char*
  48. stringappend(string1, end1, string2)
  49. char *string1;
  50. int end1;
  51. char *string2;
  52. {
  53. register char *ostring;
  54. register char *cp, *cp1;
  55. register int i;
  56. cp = string2; i = 0;
  57. while (*cp++) i++;
  58. ostring = NEW2(i+end1+1, char);
  59. cp = ostring;
  60. cp1 = string1;
  61. for (i = 0; i < end1; i++)
  62. *cp++ = *cp1++;
  63. cp1 = string2;
  64. while (*cp++ = *cp1++) ;
  65. return ostring;
  66. }
  67. /* JF this has been hacked to death. Nowaday it sets up the file names for
  68. the output files, and opens the tmp files and the parser */
  69. openfiles()
  70. {
  71. char *name_base;
  72. register int i;
  73. register char *cp;
  74. name_base = fixed_outfiles ? "y.y" : infile;
  75. cp = name_base;
  76. i = 0;
  77. while (*cp++) i++;
  78. cp -= 2;
  79. if (*cp-- == 'y' && *cp == '.')
  80. i -= 2;
  81. finput = tryopen(infile, "r");
  82. fparser = tryopen(PFILE, "r");
  83. if (verboseflag)
  84. {
  85. outfile = stringappend(name_base, i, ".output");
  86. foutput = tryopen(outfile, "w");
  87. }
  88. if (definesflag)
  89. {
  90. defsfile = stringappend(name_base, i, ".tab.h");
  91. fdefines = tryopen(defsfile, "w");
  92. }
  93. actfile = mktemp(stringappend("", 0, "/tmp/b.act.XXXXXX"));
  94. faction = tryopen(actfile, "w+");
  95. unlink(actfile);
  96. tmpattrsfile = mktemp(stringappend("", 0, "/tmp/b.attrs.XXXXXX"));
  97. fattrs = tryopen(tmpattrsfile,"w+");
  98. unlink(tmpattrsfile);
  99. tmptabfile = mktemp(stringappend("", 0, "/tmp/b.tab.XXXXXX"));
  100. tabfile = stringappend(name_base, i, ".tab.c");
  101. ftable = tryopen(tmptabfile, "w+");
  102. unlink(tmptabfile);
  103. /* These are opened by open_extra_files, if at all */
  104. attrsfile = stringappend(name_base, i, ".stype.h");
  105. guardfile = stringappend(name_base, i, ".guard.c");
  106. }
  107. /* open the output files needed only for the semantic parser.
  108. This is done when %semantic_parser is seen in the declarations section. */
  109. open_extra_files()
  110. {
  111. FILE *ftmp;
  112. int c;
  113. /* JF change open parser file */
  114. fclose(fparser);
  115. fparser= tryopen(PFILE1, "r");
  116. /* JF change from inline attrs file to separate one */
  117. ftmp = tryopen(attrsfile, "w");
  118. rewind(fattrs);
  119. while((c=getc(fattrs))!=EOF) /* Thank god for buffering */
  120. putc(c,ftmp);
  121. fclose(fattrs);
  122. fattrs=ftmp;
  123. fguard = tryopen(guardfile, "w");
  124. }
  125. /* JF to make file opening easier. This func tries to open file
  126. NAME with mode MODE, and prints an error message if it fails. */
  127. FILE *
  128. tryopen(name, mode)
  129. char *name;
  130. char *mode;
  131. {
  132. FILE *ptr;
  133. ptr = fopen(name, mode);
  134. if (ptr == NULL)
  135. {
  136. fprintf(stderr, "bison: ");
  137. perror(name);
  138. done(2);
  139. }
  140. return ptr;
  141. }
  142. done(k)
  143. int k;
  144. {
  145. if (faction)
  146. fclose(faction);
  147. if (fattrs)
  148. fclose(fattrs);
  149. if (fguard)
  150. fclose(fguard);
  151. if (finput)
  152. fclose(finput);
  153. if (fparser)
  154. fclose(fparser);
  155. /* JF we don't need this anymore
  156. if (fparser1)
  157. fclose(fparser1); */
  158. if (foutput)
  159. fclose(foutput);
  160. /* JF write out the output file */
  161. if (k == 0 && ftable)
  162. {
  163. FILE *ftmp;
  164. register int c;
  165. ftmp=tryopen(tabfile, "w");
  166. rewind(ftable);
  167. while((c=getc(ftable)) != EOF)
  168. putc(c,ftmp);
  169. fclose(ftmp);
  170. fclose(ftable);
  171. }
  172. exit(k);
  173. }