home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Source / GNU / cc / bison / files.c < prev    next >
C/C++ Source or Header  |  1992-06-24  |  7KB  |  295 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. #define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
  25. #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
  26. #endif
  27.  
  28. #include <stdio.h>
  29. #include "files.h"
  30. #include "new.h"
  31. #include "gram.h"
  32.  
  33. FILE *finput = NULL;
  34. FILE *foutput = NULL;
  35. FILE *fdefines = NULL;
  36. FILE *ftable = NULL;
  37. FILE *fattrs = NULL;
  38. FILE *fguard = NULL;
  39. FILE *faction = NULL;
  40. FILE *fparser = NULL;
  41.  
  42. /* File name specified with -o for the output file, or 0 if no -o.  */
  43. char *spec_outfile;
  44.  
  45. char *infile;
  46. char *outfile;
  47. char *defsfile;
  48. char *tabfile;
  49. char *attrsfile;
  50. char *guardfile;
  51. char *actfile;
  52. char *tmpattrsfile;
  53. char *tmptabfile;
  54.  
  55. extern char    *mktemp();    /* So the compiler won't complain */
  56. extern char    *getenv();
  57. FILE    *tryopen();    /* This might be a good idea */
  58. void done();
  59.  
  60. extern int verboseflag;
  61. extern int definesflag;
  62. int fixed_outfiles = 0;
  63.  
  64.  
  65. char*
  66. stringappend(string1, end1, string2)
  67. char *string1;
  68. int end1;
  69. char *string2;
  70. {
  71.   register char *ostring;
  72.   register char *cp, *cp1;
  73.   register int i;
  74.  
  75.   cp = string2;  i = 0;
  76.   while (*cp++) i++;
  77.  
  78.   ostring = NEW2(i+end1+1, char);
  79.  
  80.   cp = ostring;
  81.   cp1 = string1;
  82.   for (i = 0; i < end1; i++)
  83.     *cp++ = *cp1++;
  84.  
  85.   cp1 = string2;
  86.   while (*cp++ = *cp1++) ;
  87.  
  88.   return ostring;
  89. }
  90.  
  91.  
  92. /* JF this has been hacked to death.  Nowaday it sets up the file names for
  93.    the output files, and opens the tmp files and the parser */
  94. void
  95. openfiles()
  96. {
  97.   char *name_base;
  98.   register char *cp;
  99.   char *filename;
  100.   int base_length;
  101.   int short_base_length;
  102.  
  103. #ifdef VMS
  104.   char *tmp_base = "sys$scratch:b_";
  105. #else
  106.   char *tmp_base = "/tmp/b.";
  107. #endif
  108.   int tmp_len = strlen (tmp_base);
  109.  
  110.   if (spec_outfile)
  111.     {
  112.       /* -o was specified.  The precise -o name will be used for ftable.
  113.      For other output files, remove the ".c" or ".tab.c" suffix.  */
  114.       name_base = spec_outfile;
  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 (!strcmp (name_base + short_base_length - 4, ".tab"))
  122.     short_base_length -= 4;
  123.       else if (!strcmp (name_base + short_base_length - 4, "_tab"))
  124.     short_base_length -= 4;
  125.     }
  126.   else
  127.     {
  128.       /* -o was not specified; compute output file name from input
  129.      or use y.tab.c, etc., if -y was specified.  */
  130.  
  131.       name_base = fixed_outfiles ? "y.y" : infile;
  132.  
  133.       /* Discard any directory names from the input file name
  134.      to make the base of the output.  */
  135.       cp = name_base;
  136.       while (*cp)
  137.     {
  138.       if (*cp == '/')
  139.         name_base = cp+1;
  140.       cp++;
  141.     }
  142.  
  143.       /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any.  */
  144.  
  145.       base_length = strlen (name_base);
  146.       if (!strcmp (name_base + base_length - 2, ".y"))
  147.     base_length -= 2;
  148.       short_base_length = base_length;
  149.  
  150. #ifdef VMS
  151.       name_base = stringappend(name_base, short_base_length, "_tab");
  152. #else
  153.       name_base = stringappend(name_base, short_base_length, ".tab");
  154. #endif
  155.       base_length = short_base_length + 4;
  156.     }
  157.  
  158.   finput = tryopen(infile, "r");
  159.  
  160.   filename = getenv ("BISON_SIMPLE");
  161.   fparser = tryopen(filename ? filename : PFILE, "r");
  162.  
  163.   if (verboseflag)
  164.     {
  165.       outfile = stringappend(name_base, short_base_length, ".output");
  166.       foutput = tryopen(outfile, "w");
  167.     }
  168.  
  169.   if (definesflag)
  170.     {
  171.       defsfile = stringappend(name_base, base_length, ".h");
  172.       fdefines = tryopen(defsfile, "w");
  173.     }
  174.  
  175.   actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  176.   faction = tryopen(actfile, "w+");
  177.   unlink(actfile);
  178.  
  179.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  180.   fattrs = tryopen(tmpattrsfile,"w+");
  181.   unlink(tmpattrsfile);
  182.  
  183.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  184.   ftable = tryopen(tmptabfile, "w+");
  185.   unlink(tmptabfile);
  186.  
  187.     /* These are opened by `done' or `open_extra_files', if at all */
  188.   if (spec_outfile)
  189.     tabfile = spec_outfile;
  190.   else
  191.     tabfile = stringappend(name_base, base_length, ".c");
  192.  
  193. #ifdef VMS
  194.   attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  195.   guardfile = stringappend(name_base, short_base_length, "_guard.c");
  196. #else
  197.   attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  198.   guardfile = stringappend(name_base, short_base_length, ".guard.c");
  199. #endif
  200. }
  201.  
  202.  
  203.  
  204. /* open the output files needed only for the semantic parser.
  205. This is done when %semantic_parser is seen in the declarations section.  */
  206.  
  207. void
  208. open_extra_files()
  209. {
  210.   FILE *ftmp;
  211.   int c;
  212.   char *filename;
  213.         /* JF change open parser file */
  214.   fclose(fparser);
  215.   filename = (char *) getenv ("BISON_HAIRY");
  216.   fparser= tryopen(filename ? filename : PFILE1, "r");
  217.  
  218.         /* JF change from inline attrs file to separate one */
  219.   ftmp = tryopen(attrsfile, "w");
  220.   rewind(fattrs);
  221.   while((c=getc(fattrs))!=EOF)    /* Thank god for buffering */
  222.     putc(c,ftmp);
  223.   fclose(fattrs);
  224.   fattrs=ftmp;
  225.  
  226.   fguard = tryopen(guardfile, "w");
  227.  
  228. }
  229.  
  230.     /* JF to make file opening easier.  This func tries to open file
  231.        NAME with mode MODE, and prints an error message if it fails. */
  232. FILE *
  233. tryopen(name, mode)
  234. char *name;
  235. char *mode;
  236. {
  237.   FILE    *ptr;
  238.  
  239.   ptr = fopen(name, mode);
  240.   if (ptr == NULL)
  241.     {
  242.       fprintf(stderr, "bison: ");
  243.       perror(name);
  244.       done(2);
  245.     }
  246.   return ptr;
  247. }
  248.  
  249. void
  250. done(k)
  251. int k;
  252. {
  253.   if (faction)
  254.     fclose(faction);
  255.  
  256.   if (fattrs)
  257.     fclose(fattrs);
  258.  
  259.   if (fguard)
  260.     fclose(fguard);
  261.  
  262.   if (finput)
  263.     fclose(finput);
  264.  
  265.   if (fparser)
  266.     fclose(fparser);
  267.  
  268.   if (foutput)
  269.     fclose(foutput);
  270.  
  271.     /* JF write out the output file */
  272.   if (k == 0 && ftable)
  273.     {
  274.       FILE *ftmp;
  275.       register int c;
  276.  
  277.       ftmp=tryopen(tabfile, "w");
  278.       rewind(ftable);
  279.       while((c=getc(ftable)) != EOF)
  280.         putc(c,ftmp);
  281.       fclose(ftmp);
  282.       fclose(ftable);
  283.     }
  284.  
  285. #ifdef VMS
  286.   delete(actfile);
  287.   delete(tmpattrsfile);
  288.   delete(tmptabfile);
  289.   if (k==0) sys$exit(SS$_NORMAL);
  290.   sys$exit(SS$_ABORT);
  291. #else
  292.   exit(k);
  293. #endif
  294. }
  295.