FILES.C 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
  19. #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
  20. #endif
  21. #ifdef MSDOS
  22. #define XPFILE "C:\\BISON\\BISON.SIM"
  23. #define XPFILE1 "C:\\BISON\\BISON.HAI"
  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 (!strcmp (name_base + short_base_length - 4, ".tab"))
  114. short_base_length -= 4;
  115. else if (!strcmp (name_base + short_base_length - 4, "_tab"))
  116. short_base_length -= 4;
  117. }
  118. else
  119. {
  120. /* -o was not specified; compute output file name from input
  121. or use y.tab.c, etc., if -y was specified. */
  122. name_base = fixed_outfiles ? "y.y" : infile;
  123. /* Discard any directory names from the input file name
  124. to make the base of the output. */
  125. cp = name_base;
  126. while (*cp)
  127. {
  128. if (*cp == '/')
  129. name_base = cp+1;
  130. cp++;
  131. }
  132. /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */
  133. base_length = strlen (name_base);
  134. if (!strcmp (name_base + base_length - 2, ".y"))
  135. base_length -= 2;
  136. short_base_length = base_length;
  137. #ifdef VMS
  138. name_base = stringappend(name_base, short_base_length, "_tab");
  139. #else
  140. #ifdef MSDOS
  141. name_base = stringappend(name_base, short_base_length, "_tab");
  142. #else
  143. name_base = stringappend(name_base, short_base_length, ".tab");
  144. #endif /* not MSDOS */
  145. #endif
  146. base_length = short_base_length + 4;
  147. }
  148. finput = tryopen(infile, "r");
  149. filename = getenv("BISON_SIMPLE");
  150. #ifdef MSDOS
  151. /* file doesn't exist in curent directory, try in INIT directory */
  152. cp = getenv("INIT");
  153. if (filename == 0 && cp != 0)
  154. {
  155. filename = malloc(strlen(cp) + strlen(PFILE) + 2);
  156. strcpy(filename, cp);
  157. cp = filename + strlen(filename);
  158. *cp++ = '/';
  159. strcpy(cp, PFILE);
  160. }
  161. #endif /* MSDOS */
  162. fparser = tryopen(filename ? filename : PFILE, "r");
  163. if (verboseflag)
  164. {
  165. #ifdef MSDOS
  166. outfile = stringappend(name_base, short_base_length, ".out");
  167. #else
  168. outfile = stringappend(name_base, short_base_length, ".output");
  169. #endif
  170. foutput = tryopen(outfile, "w");
  171. }
  172. if (definesflag)
  173. {
  174. defsfile = stringappend(name_base, base_length, ".h");
  175. fdefines = tryopen(defsfile, "w");
  176. }
  177. #ifdef MSDOS
  178. actfile = mktemp(stringappend(tmp_base, tmp_len, "acXXXXXX"));
  179. tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "atXXXXXX"));
  180. tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "taXXXXXX"));
  181. #else
  182. actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  183. tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  184. tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  185. #endif /* not MSDOS */
  186. faction = tryopen(actfile, "w+");
  187. fattrs = tryopen(tmpattrsfile,"w+");
  188. ftable = tryopen(tmptabfile, "w+");
  189. #ifndef MSDOS
  190. unlink(actfile);
  191. unlink(tmpattrsfile);
  192. unlink(tmptabfile);
  193. #endif
  194. /* These are opened by `done' or `open_extra_files', if at all */
  195. if (spec_outfile)
  196. tabfile = spec_outfile;
  197. else
  198. tabfile = stringappend(name_base, base_length, ".c");
  199. #ifdef VMS
  200. attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  201. guardfile = stringappend(name_base, short_base_length, "_guard.c");
  202. #else
  203. #ifdef MSDOS
  204. attrsfile = stringappend(name_base, short_base_length, ".sth");
  205. guardfile = stringappend(name_base, short_base_length, ".guc");
  206. #else
  207. attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  208. guardfile = stringappend(name_base, short_base_length, ".guard.c");
  209. #endif /* not MSDOS */
  210. #endif /* not VMS */
  211. }
  212. /* open the output files needed only for the semantic parser.
  213. This is done when %semantic_parser is seen in the declarations section. */
  214. void
  215. open_extra_files()
  216. {
  217. FILE *ftmp;
  218. int c;
  219. char *filename, *cp;
  220. fclose(fparser);
  221. filename = (char *) getenv ("BISON_HAIRY");
  222. #ifdef MSDOS
  223. /* file doesn't exist in curent directory, try in INIT directory */
  224. cp = getenv("INIT");
  225. if (filename == 0 && cp != 0)
  226. {
  227. filename = malloc(strlen(cp) + strlen(PFILE1) + 2);
  228. strcpy(filename, cp);
  229. cp = filename + strlen(filename);
  230. *cp++ = '/';
  231. strcpy(cp, PFILE1);
  232. }
  233. #endif
  234. fparser= tryopen(filename ? filename : PFILE1, "r");
  235. /* JF change from inline attrs file to separate one */
  236. ftmp = tryopen(attrsfile, "w");
  237. rewind(fattrs);
  238. while((c=getc(fattrs))!=EOF) /* Thank god for buffering */
  239. putc(c,ftmp);
  240. fclose(fattrs);
  241. fattrs=ftmp;
  242. fguard = tryopen(guardfile, "w");
  243. }
  244. /* JF to make file opening easier. This func tries to open file
  245. NAME with mode MODE, and prints an error message if it fails. */
  246. FILE *
  247. tryopen(name, mode)
  248. char *name;
  249. char *mode;
  250. {
  251. FILE *ptr;
  252. ptr = fopen(name, mode);
  253. if (ptr == NULL)
  254. {
  255. fprintf(stderr, "bison: ");
  256. perror(name);
  257. done(2);
  258. }
  259. return ptr;
  260. }
  261. void
  262. done(k)
  263. int k;
  264. {
  265. if (faction)
  266. fclose(faction);
  267. if (fattrs)
  268. fclose(fattrs);
  269. if (fguard)
  270. fclose(fguard);
  271. if (finput)
  272. fclose(finput);
  273. if (fparser)
  274. fclose(fparser);
  275. if (foutput)
  276. fclose(foutput);
  277. /* JF write out the output file */
  278. if (k == 0 && ftable)
  279. {
  280. FILE *ftmp;
  281. register int c;
  282. ftmp=tryopen(tabfile, "w");
  283. rewind(ftable);
  284. while((c=getc(ftable)) != EOF)
  285. putc(c,ftmp);
  286. fclose(ftmp);
  287. fclose(ftable);
  288. }
  289. #ifdef VMS
  290. delete(actfile);
  291. delete(tmpattrsfile);
  292. delete(tmptabfile);
  293. if (k==0) sys$exit(SS$_NORMAL);
  294. sys$exit(SS$_ABORT);
  295. #else
  296. #ifdef MSDOS
  297. if (actfile) unlink(actfile);
  298. if (tmpattrsfile) unlink(tmpattrsfile);
  299. if (tmptabfile) unlink(tmptabfile);
  300. #endif /* MSDOS */
  301. exit(k);
  302. #endif /* not VMS */
  303. }
  304.