home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / language / bison / Old_Bison / C / Files < prev    next >
Encoding:
Text File  |  1990-04-04  |  4.4 KB  |  208 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. /* Convert Unix-ism to ANSI-ism */
  21. #define unlink(f) remove(f)
  22.  
  23. #include <stdio.h>
  24. #include "system.h"
  25. #include "files.h"
  26. #include "new.h"
  27. #include "gram.h"
  28.  
  29. FILE *finput = NULL;
  30. FILE *foutput = NULL;
  31. FILE *fdefines = NULL;
  32. FILE *ftable = NULL;
  33. FILE *fattrs = NULL;
  34. FILE *faction = NULL;
  35. FILE *fparser = NULL;
  36.  
  37. /* File name specified with -o for the output file, or 0 if no -o.  */
  38. char *spec_outfile;
  39.  
  40. char *infile;
  41. char *outfile;
  42. char *defsfile;
  43. char *tabfile;
  44. char *actfile;
  45.  
  46. static char *tmpattrsfile;
  47. static char *tmptabfile;
  48.  
  49. extern int verboseflag;
  50. extern int definesflag;
  51. int fixed_outfiles = 0;
  52.  
  53.  
  54. char *stringappend(char *string1, char *string2)
  55. {
  56.   register char *ostring;
  57.   register char *cp, *cp1;
  58.   register int i, j;
  59.  
  60.   cp = string1;  i = 0;
  61.   while (*cp++) i++;
  62.  
  63.   cp = string2;  j = 0;
  64.   while (*cp++) j++;
  65.  
  66.   ostring = NEW2(i+j+1, char);
  67.  
  68.   cp = ostring;
  69.   cp1 = string1;
  70.   while ((*cp++ = *cp1++) != '\0')
  71.     ;
  72.  
  73.   --cp;  /* Overwrite the trailing null */
  74.   cp1 = string2;
  75.   while ((*cp++ = *cp1++) != '\0')
  76.     ;
  77.  
  78.   return ostring;
  79. }
  80.  
  81.  
  82. /* JF this has been hacked to death.  Nowaday it sets up the file names for
  83.    the output files, and opens the tmp files and the parser */
  84.  
  85. void openfiles(void)
  86. {
  87.   char *full_name;
  88.   char *name_base;
  89.   char *filename;
  90.  
  91.   if (spec_outfile)
  92.     {
  93.       /* -o was specified.  The precise -o name will be used for ftable.
  94.      For other output files, remove the "C" directory.  */
  95.  
  96.       name_base = strrchr (spec_outfile, '.');
  97.       if (!name_base)
  98.         name_base = strchr (spec_outfile, ':');
  99.  
  100.       if (!name_base)
  101.         name_base = spec_outfile;
  102.       else
  103.         ++name_base;
  104.     }
  105.   else
  106.     {
  107.       /* -o was not specified; compute output file name from input
  108.      or use C.Y_Tab, etc., if -y was specified.  */
  109.  
  110.       full_name = fixed_outfiles ? "Y.Y_Tab" : infile;
  111.  
  112.       name_base = strrchr (full_name, '.');
  113.       if (!name_base)
  114.         name_base = strchr (full_name, ':');
  115.  
  116.       if (!name_base)
  117.         name_base = full_name;
  118.       else
  119.         ++name_base;
  120.     }
  121.  
  122.   finput = tryopen(infile, "r");
  123.  
  124.   filename = getenv("Bison$Parser");
  125.   fparser = tryopen(filename ? filename : PARSER, "r");
  126.  
  127.   if (verboseflag)
  128.     {
  129.       outfile = stringappend("Out.", name_base);
  130.       foutput = tryopen(outfile, "w");
  131.     }
  132.  
  133.   if (definesflag)
  134.     {
  135.       defsfile = stringappend("H.", name_base);
  136.       fdefines = tryopen(defsfile, "w");
  137.     }
  138.  
  139.   actfile = mktemp("BisonAct");
  140.   tmpattrsfile = mktemp("BisonAttr");
  141.   tmptabfile = mktemp("BisonTab");
  142.  
  143.   faction = tryopen(actfile, "w+");
  144.   fattrs = tryopen(tmpattrsfile,"w+");
  145.   ftable = tryopen(tmptabfile, "w+");
  146.  
  147.   /* The output file is opened by `done', if at all */
  148.   if (spec_outfile)
  149.     tabfile = spec_outfile;
  150.   else
  151.     tabfile = stringappend("C.", name_base);
  152. }
  153.  
  154.  
  155. /* JF to make file opening easier.  This func tries to open file
  156.    NAME with mode MODE, and prints an error message if it fails. */
  157.  
  158. FILE *tryopen(char *name, char *mode)
  159. {
  160.   FILE    *ptr;
  161.  
  162.   ptr = fopen(name, mode);
  163.   if (ptr == NULL)
  164.     {
  165.       fprintf(stderr, "Bison: cannot open %s (mode \"%s\")\n", name, mode);
  166.       done(2);
  167.     }
  168.   return ptr;
  169. }
  170.  
  171. void done(int k)
  172. {
  173.   if (faction)
  174.     fclose(faction);
  175.  
  176.   if (fattrs)
  177.     fclose(fattrs);
  178.  
  179.   if (finput)
  180.     fclose(finput);
  181.  
  182.   if (fparser)
  183.     fclose(fparser);
  184.  
  185.   if (foutput)
  186.     fclose(foutput);
  187.  
  188.   /* JF write out the output file */
  189.   if (k == 0 && ftable)
  190.     {
  191.       FILE *ftmp;
  192.       register int c;
  193.  
  194.       ftmp=tryopen(tabfile, "w");
  195.       rewind(ftable);
  196.       while((c=getc(ftable)) != EOF)
  197.         putc(c,ftmp);
  198.       fclose(ftmp);
  199.       fclose(ftable);
  200.     }
  201.  
  202.   if (actfile) unlink(actfile);
  203.   if (tmpattrsfile) unlink(tmpattrsfile);
  204.   if (tmptabfile) unlink(tmptabfile);
  205.  
  206.   exit(k);
  207. }
  208.