home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 4.ddi / BISON.ZIP / FILES.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-30  |  9.8 KB  |  403 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. #ifdef THINK_C
  29. #define unlink    remove
  30. #define XPFILE    "bison.simple"
  31. #define XPFILE1    "bison.hairy"
  32. #endif
  33.  
  34. /* Note: system.h used to define PFILE to XPFILE, PFILE1 to XPFILE1
  35.    Unfortunately, this doesn't seem to work with Zortech's compiler
  36.    so I changed all occurances of PFILE to XPFILE in this file - TCC
  37. */ 
  38.  
  39. #include <stdio.h>
  40. #include "system.h"
  41. #include "files.h"
  42. #include "new.h"
  43. #include "gram.h"
  44.  
  45. FILE *finput = NULL;
  46. FILE *foutput = NULL;
  47. FILE *fdefines = NULL;
  48. FILE *ftable = NULL;
  49. FILE *fattrs = NULL;
  50. FILE *fguard = NULL;
  51. FILE *faction = NULL;
  52. FILE *fparser = NULL;
  53.  
  54. /* File name specified with -o for the output file, or 0 if no -o.  */
  55. char *spec_outfile;
  56.  
  57. char *infile;
  58. char *outfile;
  59. char *defsfile;
  60. char *tabfile;
  61. char *attrsfile;
  62. char *guardfile;
  63. char *actfile;
  64. char *tmpattrsfile;
  65. char *tmptabfile;
  66.  
  67. #ifndef THINK_C
  68. #ifndef ZORTECH
  69. extern char    *mktemp();    /* So the compiler won't complain */
  70. #endif
  71. #endif
  72. extern char    *getenv();
  73. FILE    *tryopen();    /* This might be a good idea */
  74. void done();
  75.  
  76. extern int verboseflag;
  77. extern int definesflag;
  78. int fixed_outfiles = 0;
  79.  
  80. #ifdef THINK_C
  81. char actfile_buffer[FILENAME_MAX];
  82. char attrsfile_buffer[FILENAME_MAX];
  83. char tabfile_buffer[FILENAME_MAX];
  84. #endif
  85.  
  86. #ifdef ZORTECH
  87. char actfile_buffer[FILENAME_MAX];
  88. char attrsfile_buffer[FILENAME_MAX];
  89. char tabfile_buffer[FILENAME_MAX];
  90. #endif
  91.  
  92.  
  93. char*
  94. stringappend(string1, end1, string2)
  95. char *string1;
  96. int end1;
  97. char *string2;
  98. {
  99.   register char *ostring;
  100.   register char *cp, *cp1;
  101.   register int i;
  102.  
  103.   cp = string2;  i = 0;
  104.   while (*cp++) i++;
  105.  
  106.   ostring = NEW2(i+end1+1, char);
  107.  
  108.   cp = ostring;
  109.   cp1 = string1;
  110.   for (i = 0; i < end1; i++)
  111.     *cp++ = *cp1++;
  112.  
  113.   cp1 = string2;
  114.   while (*cp++ = *cp1++) ;
  115.  
  116.   return ostring;
  117. }
  118.  
  119.  
  120. /* JF this has been hacked to death.  Nowaday it sets up the file names for
  121.    the output files, and opens the tmp files and the parser */
  122. void
  123. openfiles()
  124. {
  125.   char *name_base;
  126.   register char *cp;
  127.   char *filename;
  128.   int base_length;
  129.   int short_base_length;
  130.  
  131. #ifdef VMS
  132.   char *tmp_base = "sys$scratch:b_";
  133. #else
  134.   char *tmp_base = "/tmp/b.";
  135. #endif
  136.   int tmp_len;
  137.  
  138. #ifdef MSDOS
  139.   tmp_base = getenv ("TMP");
  140.   if (tmp_base == 0)
  141.     tmp_base = "";
  142.   strlwr (infile);
  143. #endif /* MSDOS */
  144.  
  145.   tmp_len = strlen (tmp_base);
  146.  
  147.   if (spec_outfile)
  148.     {
  149.       /* -o was specified.  The precise -o name will be used for ftable.
  150.      For other output files, remove the ".c" or ".tab.c" suffix.  */
  151.       name_base = spec_outfile;
  152. #ifdef MSDOS
  153.       strlwr (name_base);
  154. #endif /* MSDOS */
  155.       /* BASE_LENGTH includes ".tab" but not ".c".  */
  156.       base_length = strlen (name_base);
  157.       if (!strcmp (name_base + base_length - 2, ".c"))
  158.     base_length -= 2;
  159.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  160.       short_base_length = base_length;
  161.       if (!strcmp (name_base + short_base_length - 4, ".tab"))
  162.     short_base_length -= 4;
  163.       else if (!strcmp (name_base + short_base_length - 4, "_tab"))
  164.     short_base_length -= 4;
  165.     }
  166.   else
  167.     {
  168.       /* -o was not specified; compute output file name from input
  169.      or use y.tab.c, etc., if -y was specified.  */
  170.  
  171.       name_base = fixed_outfiles ? "y.y" : infile;
  172.  
  173.       /* Discard any directory names from the input file name
  174.      to make the base of the output.  */
  175.       cp = name_base;
  176.       while (*cp)
  177.     {
  178.       if (*cp == '/')
  179.         name_base = cp+1;
  180.       cp++;
  181.     }
  182.  
  183.       /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any.  */
  184.  
  185.       base_length = strlen (name_base);
  186.       if (!strcmp (name_base + base_length - 2, ".y"))
  187.     base_length -= 2;
  188.       short_base_length = base_length;
  189.  
  190. #ifdef VMS
  191.       name_base = stringappend(name_base, short_base_length, "_tab");
  192. #else
  193. #ifdef MSDOS
  194.       name_base = stringappend(name_base, short_base_length, "_tab");
  195. #else
  196.       name_base = stringappend(name_base, short_base_length, ".tab");
  197. #endif /* not MSDOS */
  198. #endif
  199.       base_length = short_base_length + 4;
  200.     }
  201.  
  202.   finput = tryopen(infile, "r");
  203.  
  204.   filename = getenv("BISON_SIMPLE");
  205. #ifdef MSDOS
  206.   /* file doesn't exist in curent directory, try in INIT directory */
  207.   cp = getenv("INIT");
  208.   if (filename == 0 && cp != 0)
  209.     {
  210.       filename = malloc(strlen(cp) + strlen(XPFILE) + 2);
  211.       strcpy(filename, cp);
  212.       cp = filename + strlen(filename);
  213.       *cp++ = '/';
  214.       strcpy(cp, XPFILE);
  215.     }
  216. #endif /* MSDOS */
  217.   fparser = tryopen(filename ? filename : XPFILE, "r");
  218.  
  219.   if (verboseflag)
  220.     {
  221. #ifdef MSDOS
  222.       outfile = stringappend(name_base, short_base_length, ".out");
  223. #else
  224.       outfile = stringappend(name_base, short_base_length, ".output");
  225. #endif
  226.       foutput = tryopen(outfile, "w");
  227.     }
  228.  
  229.   if (definesflag)
  230.     {
  231.       defsfile = stringappend(name_base, base_length, ".h");
  232.       fdefines = tryopen(defsfile, "w");
  233.     }
  234.  
  235. #ifdef MSDOS
  236.  #ifdef ZORTECH
  237.   actfile = tmpnam (actfile_buffer);
  238.   tmpattrsfile = tmpnam (attrsfile_buffer);
  239.   tmptabfile = tmpnam (tabfile_buffer);
  240.  #else
  241.   actfile = mktemp(stringappend(tmp_base, tmp_len, "acXXXXXX"));
  242.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "atXXXXXX"));
  243.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "taXXXXXX"));
  244.  #endif /* Not ZORTECH */
  245. #else
  246.  #ifdef THINK_C
  247.   actfile = tmpnam (actfile_buffer);
  248.   tmpattrsfile = tmpnam (attrsfile_buffer);
  249.   tmptabfile = tmpnam (tabfile_buffer);
  250.  #else
  251.   actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  252.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  253.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  254.  #endif /* not THINK_C */
  255. #endif /* not MSDOS */
  256.  
  257.   faction = tryopen(actfile, "w+");
  258.   fattrs = tryopen(tmpattrsfile,"w+");
  259.   ftable = tryopen(tmptabfile, "w+");
  260.  
  261. #ifndef MSDOS
  262.   unlink(actfile);
  263.   unlink(tmpattrsfile);
  264.   unlink(tmptabfile);
  265. #endif
  266.  
  267.     /* These are opened by `done' or `open_extra_files', if at all */
  268.   if (spec_outfile)
  269.     tabfile = spec_outfile;
  270.   else
  271.     tabfile = stringappend(name_base, base_length, ".c");
  272.  
  273. #ifdef VMS
  274.   attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  275.   guardfile = stringappend(name_base, short_base_length, "_guard.c");
  276. #else
  277. #ifdef MSDOS
  278.   attrsfile = stringappend(name_base, short_base_length, ".sth");
  279.   guardfile = stringappend(name_base, short_base_length, ".guc");
  280. #else
  281.   attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  282.   guardfile = stringappend(name_base, short_base_length, ".guard.c");
  283. #endif /* not MSDOS */
  284. #endif /* not VMS */
  285. }
  286.  
  287.  
  288.  
  289. /* open the output files needed only for the semantic parser.
  290. This is done when %semantic_parser is seen in the declarations section.  */
  291.  
  292. void
  293. open_extra_files()
  294. {
  295.   FILE *ftmp;
  296.   int c;
  297.   char *filename, *cp;
  298.  
  299.   fclose(fparser);
  300.  
  301.   filename = (char *) getenv ("BISON_HAIRY");
  302. #ifdef MSDOS
  303.   /* file doesn't exist in curent directory, try in INIT directory */
  304.   cp = getenv("INIT");
  305.   if (filename == 0 && cp != 0)
  306.     {
  307.       filename = malloc(strlen(cp) + strlen(XPFILE1) + 2);
  308.       strcpy(filename, cp);
  309.       cp = filename + strlen(filename);
  310.       *cp++ = '/';
  311.       strcpy(cp, XPFILE1);
  312.     }
  313. #endif
  314.   fparser= tryopen(filename ? filename : XPFILE1, "r");
  315.  
  316.         /* JF change from inline attrs file to separate one */
  317.   ftmp = tryopen(attrsfile, "w");
  318.   rewind(fattrs);
  319.   while((c=getc(fattrs))!=EOF)    /* Thank god for buffering */
  320.     putc(c,ftmp);
  321.   fclose(fattrs);
  322.   fattrs=ftmp;
  323.  
  324.   fguard = tryopen(guardfile, "w");
  325.  
  326. }
  327.  
  328.     /* JF to make file opening easier.  This func tries to open file
  329.        NAME with mode MODE, and prints an error message if it fails. */
  330. FILE *
  331. tryopen(name, mode)
  332. char *name;
  333. char *mode;
  334. {
  335.   FILE    *ptr;
  336.  
  337.   ptr = fopen(name, mode);
  338.   if (ptr == NULL)
  339.     {
  340.       fprintf(stderr, "bison: ");
  341.       perror(name);
  342.       done(2);
  343.     }
  344.   return ptr;
  345. }
  346.  
  347. void
  348. done(k)
  349. int k;
  350. {
  351.   if (faction)
  352.     fclose(faction);
  353.  
  354.   if (fattrs)
  355.     fclose(fattrs);
  356.  
  357.   if (fguard)
  358.     fclose(fguard);
  359.  
  360.   if (finput)
  361.     fclose(finput);
  362.  
  363.   if (fparser)
  364.     fclose(fparser);
  365.  
  366.   if (foutput)
  367.     fclose(foutput);
  368.  
  369.     /* JF write out the output file */
  370.   if (k == 0 && ftable)
  371.     {
  372.       FILE *ftmp;
  373.       register int c;
  374.  
  375.       ftmp=tryopen(tabfile, "w");
  376.       rewind(ftable);
  377.       while((c=getc(ftable)) != EOF)
  378.         putc(c,ftmp);
  379.       fclose(ftmp);
  380.       fclose(ftable);
  381.     }
  382.  
  383. #ifdef VMS
  384.   delete(actfile);
  385.   delete(tmpattrsfile);
  386.   delete(tmptabfile);
  387.   if (k==0) sys$exit(SS$_NORMAL);
  388.   sys$exit(SS$_ABORT);
  389. #else
  390. #ifdef MSDOS
  391.   if (actfile) unlink(actfile);
  392.   if (tmpattrsfile) unlink(tmpattrsfile);
  393.   if (tmptabfile) unlink(tmptabfile);
  394. #endif /* MSDOS */
  395. #ifdef THINK_C
  396.   if (actfile) unlink(actfile);
  397.   if (tmpattrsfile) unlink(tmpattrsfile);
  398.   if (tmptabfile) unlink(tmptabfile);
  399. #endif /* THINK_C */
  400.   exit(k);
  401. #endif /* not VMS */
  402. }
  403.