home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / bison / bison1~1.zoo / files.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-25  |  9.4 KB  |  387 lines

  1. /* Open and close files for bison,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #ifdef VMS
  22. #include <ssdef.h>
  23. #define unlink delete
  24. #ifndef XPFILE
  25. #define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
  26. #endif
  27. #ifndef XPFILE1
  28. #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
  29. #endif
  30. #endif
  31.  
  32. #include <stdio.h>
  33. #include "system.h"
  34. #include "files.h"
  35. #include "new.h"
  36. #include "gram.h"
  37.  
  38. FILE *finput = NULL;
  39. FILE *foutput = NULL;
  40. FILE *fdefines = NULL;
  41. FILE *ftable = NULL;
  42. FILE *fattrs = NULL;
  43. FILE *fguard = NULL;
  44. FILE *faction = NULL;
  45. FILE *fparser = NULL;
  46.  
  47. /* File name specified with -o for the output file, or 0 if no -o.  */
  48. char *spec_outfile;
  49.  
  50. char *infile;
  51. char *outfile;
  52. char *defsfile;
  53. char *tabfile;
  54. char *attrsfile;
  55. char *guardfile;
  56. char *actfile;
  57. char *tmpattrsfile;
  58. char *tmptabfile;
  59.  
  60. extern char    *mktemp();    /* So the compiler won't complain */
  61. extern char    *getenv();
  62. FILE    *tryopen();    /* This might be a good idea */
  63. void done();
  64.  
  65. extern char *program_name;
  66. extern int verboseflag;
  67. extern int definesflag;
  68. int fixed_outfiles = 0;
  69. #ifdef atarist
  70. long _stksize = -1L;
  71. #endif
  72.  
  73.  
  74. char*
  75. stringappend(string1, end1, string2)
  76. char *string1;
  77. int end1;
  78. char *string2;
  79. {
  80.   register char *ostring;
  81.   register char *cp, *cp1;
  82.   register int i;
  83.  
  84.   cp = string2;  i = 0;
  85.   while (*cp++) i++;
  86.  
  87.   ostring = NEW2(i+end1+1, char);
  88.  
  89.   cp = ostring;
  90.   cp1 = string1;
  91.   for (i = 0; i < end1; i++)
  92.     *cp++ = *cp1++;
  93.  
  94.   cp1 = string2;
  95.   while (*cp++ = *cp1++) ;
  96.  
  97.   return ostring;
  98. }
  99.  
  100.  
  101. /* JF this has been hacked to death.  Nowaday it sets up the file names for
  102.    the output files, and opens the tmp files and the parser */
  103. void
  104. openfiles()
  105. {
  106.   char *name_base;
  107.   register char *cp;
  108.   char *filename;
  109.   int base_length;
  110.   int short_base_length;
  111.  
  112. #ifdef VMS
  113.   char *tmp_base = "sys$scratch:b_";
  114. #else
  115.   char *tmp_base = "/tmp/b.";
  116. #endif
  117.   int tmp_len;
  118.  
  119. #if defined(MSDOS) || defined(atarist)
  120.   tmp_base = getenv ("TEMP");
  121.   if(tmp_base == 0) tmp_base = getenv("TMP");
  122.   if (tmp_base == 0)
  123.     tmp_base = "";
  124.   strlwr (infile);
  125. #endif /* MSDOS || atarist */
  126.  
  127.   tmp_len = strlen (tmp_base);
  128.  
  129.   if (spec_outfile)
  130.     {
  131.       /* -o was specified.  The precise -o name will be used for ftable.
  132.      For other output files, remove the ".c" or ".tab.c" suffix.  */
  133.       name_base = spec_outfile;
  134. #if defined(MSDOS) || defined(atarist)
  135.       strlwr (name_base);
  136. #endif /* MSDOS || atarist */
  137.       /* BASE_LENGTH includes ".tab" but not ".c".  */
  138.       base_length = strlen (name_base);
  139.       if (!strcmp (name_base + base_length - 2, ".c"))
  140.     base_length -= 2;
  141.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  142.       short_base_length = base_length;
  143.       if (!strncmp (name_base + short_base_length - 4, ".tab", 4))
  144.     short_base_length -= 4;
  145.       else if (!strncmp (name_base + short_base_length - 4, "_tab", 4))
  146.     short_base_length -= 4;
  147.     }
  148.   else if (spec_file_prefix)
  149.     {
  150.       /* -b was specified.  Construct names from it.  */
  151.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  152.       short_base_length = strlen (spec_file_prefix);
  153.       /* Count room for `.tab'.  */
  154.       base_length = short_base_length + 4;
  155.       name_base = (char *) mallocate (base_length + 1);
  156.       /* Append `.tab'.  */
  157.       strcpy (name_base, spec_file_prefix);
  158.       strcat (name_base, ".tab");
  159. #if defined(MSDOS) || defined(atarist)
  160.       strlwr (name_base);
  161. #endif /* MSDOS */
  162.     }
  163.   else
  164.     {
  165.       /* -o was not specified; compute output file name from input
  166.      or use y.tab.c, etc., if -y was specified.  */
  167.  
  168.       name_base = fixed_outfiles ? "y.y" : infile;
  169.  
  170.       /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any.  */
  171.  
  172.       base_length = strlen (name_base);
  173.       if (!strcmp (name_base + base_length - 2, ".y"))
  174.     base_length -= 2;
  175.       short_base_length = base_length;
  176.  
  177. #ifdef VMS
  178.       name_base = stringappend(name_base, short_base_length, "_tab");
  179. #else
  180. #if defined(MSDOS) || defined(atarist)
  181.       name_base = stringappend(name_base, short_base_length, "_tab");
  182. #else
  183.       name_base = stringappend(name_base, short_base_length, ".tab");
  184. #endif /* not MSDOS || atarist */
  185. #endif
  186.       base_length = short_base_length + 4;
  187.     }
  188.  
  189.   finput = tryopen(infile, "r");
  190.  
  191.   filename = getenv("BISON_SIMPLE");
  192. #if defined(MSDOS) || defined(atarist)
  193.   /* file doesn't exist in curent directory, try in INIT directory */
  194. #ifdef atarist
  195.   cp = getenv("GNULIB");
  196. #else
  197.   cp = getenv("INIT");
  198. #endif
  199.   if (filename == 0 && cp != 0)
  200.     {
  201.       filename = malloc(strlen(cp) + strlen(PFILE) + 2);
  202.       strcpy(filename, cp);
  203.       cp = filename + strlen(filename);
  204.       *cp++ = '/';
  205.       strcpy(cp, PFILE);
  206.     }
  207. #endif /* MSDOS */
  208.   fparser = tryopen(filename ? filename : PFILE, "r");
  209.  
  210.   if (verboseflag)
  211.     {
  212. #if defined(MSDOS) || defined(atarist)
  213.       outfile = stringappend(name_base, short_base_length, ".out");
  214. #else
  215.       if (spec_name_prefix)
  216.     outfile = stringappend(name_base, short_base_length, ".out");
  217.       else
  218.     outfile = stringappend(name_base, short_base_length, ".output");
  219. #endif
  220.       foutput = tryopen(outfile, "w");
  221.     }
  222.  
  223.   if (definesflag)
  224.     {
  225.       defsfile = stringappend(name_base, base_length, ".h");
  226.       fdefines = tryopen(defsfile, "w");
  227.     }
  228.  
  229. #if defined(MSDOS) || defined(atarist)
  230.   actfile = mktemp(stringappend(tmp_base, tmp_len, "acXXXXXX"));
  231.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "atXXXXXX"));
  232.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "taXXXXXX"));
  233. #else
  234.   actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  235.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  236.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  237. #endif /* not MSDOS */
  238.  
  239.   faction = tryopen(actfile, "w+");
  240.   fattrs = tryopen(tmpattrsfile,"w+");
  241.   ftable = tryopen(tmptabfile, "w+");
  242.  
  243. #if !(defined(MSDOS) || defined(atarist))
  244.   unlink(actfile);
  245.   unlink(tmpattrsfile);
  246.   unlink(tmptabfile);
  247. #endif
  248.  
  249.     /* These are opened by `done' or `open_extra_files', if at all */
  250.   if (spec_outfile)
  251.     tabfile = spec_outfile;
  252.   else
  253.     tabfile = stringappend(name_base, base_length, ".c");
  254.  
  255. #ifdef VMS
  256.   attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  257.   guardfile = stringappend(name_base, short_base_length, "_guard.c");
  258. #else
  259. #if defined(MSDOS) || defined(atarist)
  260.   attrsfile = stringappend(name_base, short_base_length, ".sth");
  261.   guardfile = stringappend(name_base, short_base_length, ".guc");
  262. #else
  263.   attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  264.   guardfile = stringappend(name_base, short_base_length, ".guard.c");
  265. #endif /* not MSDOS */
  266. #endif /* not VMS */
  267. }
  268.  
  269.  
  270.  
  271. /* open the output files needed only for the semantic parser.
  272. This is done when %semantic_parser is seen in the declarations section.  */
  273.  
  274. void
  275. open_extra_files()
  276. {
  277.   FILE *ftmp;
  278.   int c;
  279.   char *filename, *cp;
  280.  
  281.   fclose(fparser);
  282.  
  283.   filename = (char *) getenv ("BISON_HAIRY");
  284. #if defined(MSDOS) || defined(atarist)
  285.   /* file doesn't exist in curent directory, try in INIT directory */
  286. #ifdef atarist
  287.   cp = getenv("GNULIB");
  288. #else
  289.   cp = getenv("INIT");
  290. #endif
  291.   if (filename == 0 && cp != 0)
  292.     {
  293.       filename = malloc(strlen(cp) + strlen(PFILE1) + 2);
  294.       strcpy(filename, cp);
  295.       cp = filename + strlen(filename);
  296.       *cp++ = '/';
  297.       strcpy(cp, PFILE1);
  298.     }
  299. #endif
  300.   fparser= tryopen(filename ? filename : PFILE1, "r");
  301.  
  302.         /* JF change from inline attrs file to separate one */
  303.   ftmp = tryopen(attrsfile, "w");
  304.   rewind(fattrs);
  305.   while((c=getc(fattrs))!=EOF)    /* Thank god for buffering */
  306.     putc(c,ftmp);
  307.   fclose(fattrs);
  308.   fattrs=ftmp;
  309.  
  310.   fguard = tryopen(guardfile, "w");
  311.  
  312. }
  313.  
  314.     /* JF to make file opening easier.  This func tries to open file
  315.        NAME with mode MODE, and prints an error message if it fails. */
  316. FILE *
  317. tryopen(name, mode)
  318. char *name;
  319. char *mode;
  320. {
  321.   FILE    *ptr;
  322.  
  323.   ptr = fopen(name, mode);
  324.   if (ptr == NULL)
  325.     {
  326.       fprintf(stderr, "%s: ", program_name);
  327.       perror(name);
  328.       done(2);
  329.     }
  330.   return ptr;
  331. }
  332.  
  333. void
  334. done(k)
  335. int k;
  336. {
  337.   if (faction)
  338.     fclose(faction);
  339.  
  340.   if (fattrs)
  341.     fclose(fattrs);
  342.  
  343.   if (fguard)
  344.     fclose(fguard);
  345.  
  346.   if (finput)
  347.     fclose(finput);
  348.  
  349.   if (fparser)
  350.     fclose(fparser);
  351.  
  352.   if (foutput)
  353.     fclose(foutput);
  354.  
  355.     /* JF write out the output file */
  356.   if (k == 0 && ftable)
  357.     {
  358.       FILE *ftmp;
  359.       register int c;
  360.  
  361.       ftmp=tryopen(tabfile, "w");
  362.       rewind(ftable);
  363.       while((c=getc(ftable)) != EOF)
  364.         putc(c,ftmp);
  365.       fclose(ftmp);
  366.       fclose(ftable);
  367.     }
  368.  
  369. #ifdef VMS
  370.   if (faction)
  371.     delete(actfile);
  372.   if (fattrs)
  373.     delete(tmpattrsfile);
  374.   if (ftable)
  375.     delete(tmptabfile);
  376.   if (k==0) sys$exit(SS$_NORMAL);
  377.   sys$exit(SS$_ABORT);
  378. #else
  379. #if defined(MSDOS) || defined(atarist)
  380.   if (actfile) unlink(actfile);
  381.   if (tmpattrsfile) unlink(tmpattrsfile);
  382.   if (tmptabfile) unlink(tmptabfile);
  383. #endif /* MSDOS */
  384.   exit(k);
  385. #endif /* not VMS */
  386. }
  387.