FILES.C 9.9 KB

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