FILES.C 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. #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. extern char *mktemp(); /* So the compiler won't complain */
  50. extern char *getenv();
  51. FILE *tryopen(); /* This might be a good idea */
  52. void done();
  53. extern int verboseflag;
  54. extern int definesflag;
  55. int fixed_outfiles = 0;
  56. char*
  57. stringappend(string1, end1, string2)
  58. char *string1;
  59. int end1;
  60. char *string2;
  61. {
  62. register char *ostring;
  63. register char *cp, *cp1;
  64. register int i;
  65. cp = string2; i = 0;
  66. while (*cp++) i++;
  67. ostring = NEW2(i+end1+1, char);
  68. cp = ostring;
  69. cp1 = string1;
  70. for (i = 0; i < end1; i++)
  71. *cp++ = *cp1++;
  72. cp1 = string2;
  73. while (*cp++ = *cp1++) ;
  74. return ostring;
  75. }
  76. /* JF this has been hacked to death. Nowaday it sets up the file names for
  77. the output files, and opens the tmp files and the parser */
  78. void
  79. openfiles()
  80. {
  81. char *name_base;
  82. register char *cp;
  83. char *filename;
  84. int base_length;
  85. int short_base_length;
  86. #ifdef VMS
  87. char *tmp_base = "sys$scratch:b_";
  88. #else
  89. char *tmp_base = "/tmp/b.";
  90. #endif
  91. int tmp_len;
  92. #ifdef MSDOS
  93. tmp_base = getenv ("TMP");
  94. if (tmp_base == 0)
  95. tmp_base = "";
  96. strlwr (infile);
  97. #endif /* MSDOS */
  98. tmp_len = strlen (tmp_base);
  99. if (spec_outfile)
  100. {
  101. /* -o was specified. The precise -o name will be used for ftable.
  102. For other output files, remove the ".c" or ".tab.c" suffix. */
  103. name_base = spec_outfile;
  104. #ifdef MSDOS
  105. strlwr (name_base);
  106. #endif /* MSDOS */
  107. /* BASE_LENGTH includes ".tab" but not ".c". */
  108. base_length = strlen (name_base);
  109. if (!strcmp (name_base + base_length - 2, ".c"))
  110. base_length -= 2;
  111. /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
  112. short_base_length = base_length;
  113. if (!strncmp (name_base + short_base_length - 4, ".tab", 4))
  114. short_base_length -= 4;
  115. else if (!strncmp (name_base + short_base_length - 4, "_tab", 4))
  116. short_base_length -= 4;
  117. }
  118. else if (spec_file_prefix)
  119. {
  120. /* -b was specified. Construct names from it. */
  121. name_base = spec_name_prefix;
  122. #ifdef MSDOS
  123. strlwr (name_base);
  124. #endif /* MSDOS */
  125. /* Pretend this contains `.tab' whether it does or not. */
  126. base_length = strlen (name_base);
  127. /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
  128. short_base_length = base_length;
  129. }
  130. else
  131. {
  132. /* -o was not specified; compute output file name from input
  133. or use y.tab.c, etc., if -y was specified. */
  134. name_base = fixed_outfiles ? "y.y" : infile;
  135. /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
  136. base_length = strlen (name_base);
  137. if (!strcmp (name_base + base_length - 2, ".y"))
  138. base_length -= 2;
  139. short_base_length = base_length;
  140. #ifdef VMS
  141. name_base = stringappend(name_base, short_base_length, "_tab");
  142. #else
  143. #ifdef MSDOS
  144. name_base = stringappend(name_base, short_base_length, "_tab");
  145. #else
  146. name_base = stringappend(name_base, short_base_length, ".tab");
  147. #endif /* not MSDOS */
  148. #endif
  149. base_length = short_base_length + 4;
  150. }
  151. finput = tryopen(infile, "r");
  152. filename = getenv("BISON_SIMPLE");
  153. #ifdef MSDOS
  154. /* file doesn't exist in curent directory, try in INIT directory */
  155. cp = getenv("INIT");
  156. if (filename == 0 && cp != 0)
  157. {
  158. filename = malloc(strlen(cp) + strlen(PFILE) + 2);
  159. strcpy(filename, cp);
  160. cp = filename + strlen(filename);
  161. *cp++ = '/';
  162. strcpy(cp, PFILE);
  163. }
  164. #endif /* MSDOS */
  165. fparser = tryopen(filename ? filename : PFILE, "r");
  166. if (verboseflag)
  167. {
  168. #ifdef MSDOS
  169. outfile = stringappend(name_base, short_base_length, ".out");
  170. #else
  171. if (spec_name_prefix)
  172. outfile = stringappend(name_base, short_base_length, ".out");
  173. else
  174. outfile = stringappend(name_base, short_base_length, ".output");
  175. #endif
  176. foutput = tryopen(outfile, "w");
  177. }
  178. if (definesflag)
  179. {
  180. defsfile = stringappend(name_base, base_length, ".h");
  181. fdefines = tryopen(defsfile, "w");
  182. }
  183. #ifdef MSDOS
  184. actfile = mktemp(stringappend(tmp_base, tmp_len, "acXXXXXX"));
  185. tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "atXXXXXX"));
  186. tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "taXXXXXX"));
  187. #else
  188. actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  189. tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  190. tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  191. #endif /* not MSDOS */
  192. faction = tryopen(actfile, "w+");
  193. fattrs = tryopen(tmpattrsfile,"w+");
  194. ftable = tryopen(tmptabfile, "w+");
  195. #ifndef MSDOS
  196. unlink(actfile);
  197. unlink(tmpattrsfile);
  198. unlink(tmptabfile);
  199. #endif
  200. /* These are opened by `done' or `open_extra_files', if at all */
  201. if (spec_outfile)
  202. tabfile = spec_outfile;
  203. else
  204. tabfile = stringappend(name_base, base_length, ".c");
  205. #ifdef VMS
  206. attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  207. guardfile = stringappend(name_base, short_base_length, "_guard.c");
  208. #else
  209. #ifdef MSDOS
  210. attrsfile = stringappend(name_base, short_base_length, ".sth");
  211. guardfile = stringappend(name_base, short_base_length, ".guc");
  212. #else
  213. attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  214. guardfile = stringappend(name_base, short_base_length, ".guard.c");
  215. #endif /* not MSDOS */
  216. #endif /* not VMS */
  217. }
  218. /* open the output files needed only for the semantic parser.
  219. This is done when %semantic_parser is seen in the declarations section. */
  220. void
  221. open_extra_files()
  222. {
  223. FILE *ftmp;
  224. int c;
  225. char *filename, *cp;
  226. fclose(fparser);
  227. filename = (char *) getenv ("BISON_HAIRY");
  228. #ifdef MSDOS
  229. /* file doesn't exist in curent directory, try in INIT directory */
  230. cp = getenv("INIT");
  231. if (filename == 0 && cp != 0)
  232. {
  233. filename = malloc(strlen(cp) + strlen(PFILE1) + 2);
  234. strcpy(filename, cp);
  235. cp = filename + strlen(filename);
  236. *cp++ = '/';
  237. strcpy(cp, PFILE1);
  238. }
  239. #endif
  240. fparser= tryopen(filename ? filename : PFILE1, "r");
  241. /* JF change from inline attrs file to separate one */
  242. ftmp = tryopen(attrsfile, "w");
  243. rewind(fattrs);
  244. while((c=getc(fattrs))!=EOF) /* Thank god for buffering */
  245. putc(c,ftmp);
  246. fclose(fattrs);
  247. fattrs=ftmp;
  248. fguard = tryopen(guardfile, "w");
  249. }
  250. /* JF to make file opening easier. This func tries to open file
  251. NAME with mode MODE, and prints an error message if it fails. */
  252. FILE *
  253. tryopen(name, mode)
  254. char *name;
  255. char *mode;
  256. {
  257. FILE *ptr;
  258. ptr = fopen(name, mode);
  259. if (ptr == NULL)
  260. {
  261. fprintf(stderr, "bison: ");
  262. perror(name);
  263. done(2);
  264. }
  265. return ptr;
  266. }
  267. void
  268. done(k)
  269. int k;
  270. {
  271. if (faction)
  272. fclose(faction);
  273. if (fattrs)
  274. fclose(fattrs);
  275. if (fguard)
  276. fclose(fguard);
  277. if (finput)
  278. fclose(finput);
  279. if (fparser)
  280. fclose(fparser);
  281. if (foutput)
  282. fclose(foutput);
  283. /* JF write out the output file */
  284. if (k == 0 && ftable)
  285. {
  286. FILE *ftmp;
  287. register int c;
  288. ftmp=tryopen(tabfile, "w");
  289. rewind(ftable);
  290. while((c=getc(ftable)) != EOF)
  291. putc(c,ftmp);
  292. fclose(ftmp);
  293. fclose(ftable);
  294. }
  295. #ifdef VMS
  296. if (faction)
  297. delete(actfile);
  298. if (fattrs)
  299. delete(tmpattrsfile);
  300. if (ftable)
  301. delete(tmptabfile);
  302. if (k==0) sys$exit(SS$_NORMAL);
  303. sys$exit(SS$_ABORT);
  304. #else
  305. #ifdef MSDOS
  306. if (actfile) unlink(actfile);
  307. if (tmpattrsfile) unlink(tmpattrsfile);
  308. if (tmptabfile) unlink(tmptabfile);
  309. #endif /* MSDOS */
  310. exit(k);
  311. #endif /* not VMS */
  312. }