home *** CD-ROM | disk | FTP | other *** search
/ Dream 57 / Amiga_Dream_57.iso / Amiga / Programmation / c / Compilateur / Bison125.lha / Src / files.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-16  |  10.3 KB  |  425 lines

  1. /* Open and close files for bison,
  2.    Copyright (C) 1984, 1986, 1989, 1992 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 2, 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. #if defined (VMS) & !defined (__VMS_POSIX)
  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. char *tmpdefsfile;
  60.  
  61. extern int noparserflag;
  62.  
  63. extern char    *mktemp();    /* So the compiler won't complain */
  64. extern char    *getenv();
  65. extern void    perror();
  66. FILE    *tryopen();    /* This might be a good idea */
  67. void done();
  68.  
  69. #ifdef AMIGA
  70.  #define mktemp tmpnam
  71.  #define unlink remove
  72. #endif
  73.  
  74. extern char *program_name;
  75. extern int verboseflag;
  76. extern int definesflag;
  77. int fixed_outfiles = 0;
  78.  
  79.  
  80. char*
  81. stringappend(string1, end1, string2)
  82. char *string1;
  83. int end1;
  84. char *string2;
  85. {
  86.   register char *ostring;
  87.   register char *cp, *cp1;
  88.   register int i;
  89.  
  90.   cp = string2;  i = 0;
  91.   while (*cp++) i++;
  92.  
  93.   ostring = NEW2(i+end1+1, char);
  94.  
  95.   cp = ostring;
  96.   cp1 = string1;
  97.   for (i = 0; i < end1; i++)
  98.     *cp++ = *cp1++;
  99.  
  100.   cp1 = string2;
  101.   while (*cp++ = *cp1++) ;
  102.  
  103.   return ostring;
  104. }
  105.  
  106.  
  107. /* JF this has been hacked to death.  Nowaday it sets up the file names for
  108.    the output files, and opens the tmp files and the parser */
  109. void
  110. openfiles()
  111. {
  112.   char *name_base;
  113.   register char *cp;
  114.   char *filename;
  115.   int base_length;
  116.   int short_base_length;
  117.  
  118. #if defined (VMS) & !defined (__VMS_POSIX)
  119.   char *tmp_base = "sys$scratch:b_";
  120. #else
  121. #ifdef AMIGA
  122.   char *tmp_base = "T:b.";
  123. #else
  124.   char *tmp_base = "/tmp/b.";
  125. #endif
  126. #endif
  127.   int tmp_len;
  128.  
  129. #ifdef MSDOS
  130.   tmp_base = getenv ("TMP");
  131.   if (tmp_base == 0)
  132.     tmp_base = "";
  133.   strlwr (infile);
  134. #endif /* MSDOS */
  135.  
  136.   tmp_len = strlen (tmp_base);
  137.  
  138.   if (spec_outfile)
  139.     {
  140.       /* -o was specified.  The precise -o name will be used for ftable.
  141.      For other output files, remove the ".c" or ".tab.c" suffix.  */
  142.       name_base = spec_outfile;
  143. #ifdef MSDOS
  144.       strlwr (name_base);
  145. #endif /* MSDOS */
  146.       /* BASE_LENGTH includes ".tab" but not ".c".  */
  147.       base_length = strlen (name_base);
  148.       if (!strcmp (name_base + base_length - 2, ".c"))
  149.     base_length -= 2;
  150.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  151.       short_base_length = base_length;
  152.       if (!strncmp (name_base + short_base_length - 4, ".tab", 4))
  153.     short_base_length -= 4;
  154.       else if (!strncmp (name_base + short_base_length - 4, "_tab", 4))
  155.     short_base_length -= 4;
  156.     }
  157.   else if (spec_file_prefix)
  158.     {
  159.       /* -b was specified.  Construct names from it.  */
  160.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  161.       short_base_length = strlen (spec_file_prefix);
  162.       /* Count room for `.tab'.  */
  163.       base_length = short_base_length + 4;
  164.       name_base = (char *) xmalloc (base_length + 1);
  165.       /* Append `.tab'.  */
  166.       strcpy (name_base, spec_file_prefix);
  167. #ifdef VMS
  168.       strcat (name_base, "_tab");
  169. #else
  170.       strcat (name_base, ".tab");
  171. #endif
  172. #ifdef MSDOS
  173.       strlwr (name_base);
  174. #endif /* MSDOS */
  175.     }
  176.   else
  177.     {
  178.       /* -o was not specified; compute output file name from input
  179.      or use y.tab.c, etc., if -y was specified.  */
  180.  
  181.       name_base = fixed_outfiles ? "y.y" : infile;
  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.   if (! noparserflag) 
  205.     {
  206.       filename = getenv("BISON_SIMPLE");
  207. #ifdef MSDOS
  208.       /* File doesn't exist in current directory; try in INIT directory.  */
  209.       cp = getenv("INIT");
  210.       if (filename == 0 && cp != NULL)
  211.         {
  212.           filename = xmalloc(strlen(cp) + strlen(PFILE) + 2);
  213.           strcpy(filename, cp);
  214.           cp = filename + strlen(filename);
  215.           *cp++ = '/';
  216.           strcpy(cp, PFILE);
  217.         }
  218. #endif /* MSDOS */
  219.       fparser = tryopen(filename ? filename : PFILE, "r");
  220.     }
  221.  
  222.   if (verboseflag)
  223.     {
  224. #ifdef MSDOS
  225.       outfile = stringappend(name_base, short_base_length, ".out");
  226. #else
  227.       /* We used to use just .out if spec_name_prefix (-p) was used,
  228.      but that conflicts with Posix.  */
  229.       outfile = stringappend(name_base, short_base_length, ".output");
  230. #endif
  231.       foutput = tryopen(outfile, "w");
  232.     }
  233.  
  234.   if (noparserflag)
  235.     {
  236.       /* use permanent name for actions file */
  237.       actfile = stringappend(name_base, short_base_length, ".act");
  238.       faction = tryopen(actfile, "w");
  239.     } 
  240.  
  241. #ifdef MSDOS
  242.   if (! noparserflag)
  243.     actfile = mktemp(stringappend(tmp_base, tmp_len, "acXXXXXX"));
  244.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "atXXXXXX"));
  245.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "taXXXXXX"));
  246.   tmpdefsfile = mktemp(stringappend(tmp_base, tmp_len, "deXXXXXX"));
  247. #else
  248.   if (! noparserflag)
  249.     actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  250.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  251.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  252.   tmpdefsfile = mktemp(stringappend(tmp_base, tmp_len, "defs.XXXXXX"));
  253. #endif /* not MSDOS */
  254.  
  255.   if (! noparserflag)
  256.     faction = tryopen(actfile, "w+");
  257.   fattrs = tryopen(tmpattrsfile,"w+");
  258.   ftable = tryopen(tmptabfile, "w+");
  259.  
  260.   if (definesflag)
  261.     {
  262.       defsfile = stringappend(name_base, base_length, ".h");
  263.       fdefines = tryopen(tmpdefsfile, "w+");
  264.     }
  265.  
  266. #ifndef MSDOS
  267.   if (! noparserflag)
  268.     unlink(actfile);
  269.   unlink(tmpattrsfile);
  270.   unlink(tmptabfile);
  271.   unlink(tmpdefsfile);
  272. #endif
  273.  
  274.     /* These are opened by `done' or `open_extra_files', if at all */
  275.   if (spec_outfile)
  276.     tabfile = spec_outfile;
  277.   else
  278.     tabfile = stringappend(name_base, base_length, ".c");
  279.  
  280. #ifdef VMS
  281.   attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  282.   guardfile = stringappend(name_base, short_base_length, "_guard.c");
  283. #else
  284. #ifdef MSDOS
  285.   attrsfile = stringappend(name_base, short_base_length, ".sth");
  286.   guardfile = stringappend(name_base, short_base_length, ".guc");
  287. #else
  288.   attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  289.   guardfile = stringappend(name_base, short_base_length, ".guard.c");
  290. #endif /* not MSDOS */
  291. #endif /* not VMS */
  292. }
  293.  
  294.  
  295.  
  296. /* open the output files needed only for the semantic parser.
  297. This is done when %semantic_parser is seen in the declarations section.  */
  298.  
  299. void
  300. open_extra_files()
  301. {
  302.   FILE *ftmp;
  303.   int c;
  304.   char *filename, *cp;
  305.  
  306.   if (fparser)
  307.     fclose(fparser);
  308.  
  309.   if (! noparserflag) 
  310.     {
  311.       filename = (char *) getenv ("BISON_HAIRY");
  312. #ifdef MSDOS
  313.       /* File doesn't exist in current directory; try in INIT directory.  */
  314.       cp = getenv("INIT");
  315.       if (filename == 0 && cp != NULL)
  316.         {
  317.           filename = xmalloc(strlen(cp) + strlen(PFILE1) + 2);
  318.           strcpy(filename, cp);
  319.           cp = filename + strlen(filename);
  320.           *cp++ = '/';
  321.           strcpy(cp, PFILE1);
  322.         }
  323. #endif
  324.       fparser= tryopen(filename ? filename : PFILE1, "r");
  325.     }
  326.  
  327.         /* JF change from inline attrs file to separate one */
  328.   ftmp = tryopen(attrsfile, "w");
  329.   rewind(fattrs);
  330.   while((c=getc(fattrs))!=EOF)    /* Thank god for buffering */
  331.     putc(c,ftmp);
  332.   fclose(fattrs);
  333.   fattrs=ftmp;
  334.  
  335.   fguard = tryopen(guardfile, "w");
  336.  
  337. }
  338.  
  339.     /* JF to make file opening easier.  This func tries to open file
  340.        NAME with mode MODE, and prints an error message if it fails. */
  341. FILE *
  342. tryopen(name, mode)
  343. char *name;
  344. char *mode;
  345. {
  346.   FILE    *ptr;
  347.  
  348.   ptr = fopen(name, mode);
  349.   if (ptr == NULL)
  350.     {
  351.       fprintf(stderr, "%s: ", program_name);
  352.       perror(name);
  353.       done(2);
  354.     }
  355.   return ptr;
  356. }
  357.  
  358. void
  359. done(k)
  360. int k;
  361. {
  362.   if (faction)
  363.     fclose(faction);
  364.  
  365.   if (fattrs)
  366.     fclose(fattrs);
  367.  
  368.   if (fguard)
  369.     fclose(fguard);
  370.  
  371.   if (finput)
  372.     fclose(finput);
  373.  
  374.   if (fparser)
  375.     fclose(fparser);
  376.  
  377.   if (foutput)
  378.     fclose(foutput);
  379.  
  380.     /* JF write out the output file */
  381.   if (k == 0 && ftable)
  382.     {
  383.       FILE *ftmp;
  384.       register int c;
  385.  
  386.       ftmp=tryopen(tabfile, "w");
  387.       rewind(ftable);
  388.       while((c=getc(ftable)) != EOF)
  389.         putc(c,ftmp);
  390.       fclose(ftmp);
  391.       fclose(ftable);
  392.  
  393.       if (definesflag)
  394.         {
  395.           ftmp = tryopen(defsfile, "w");
  396.           fflush(fdefines);
  397.           rewind(fdefines);
  398.           while((c=getc(fdefines)) != EOF)
  399.             putc(c,ftmp);
  400.           fclose(ftmp);
  401.           fclose(fdefines);
  402.         }
  403.     }
  404.  
  405. #if defined (VMS) & !defined (__VMS_POSIX)
  406.   if (faction && ! noparserflag)
  407.     delete(actfile);
  408.   if (fattrs)
  409.     delete(tmpattrsfile);
  410.   if (ftable)
  411.     delete(tmptabfile);
  412.   if (k==0) sys$exit(SS$_NORMAL);
  413.   sys$exit(SS$_ABORT);
  414. #else
  415. #if defined (MSDOS) | defined (AMIGA)
  416. /*  if (actfile) unlink(actfile); */
  417.   if (actfile && ! noparserflag) unlink(actfile);
  418.   if (tmpattrsfile) unlink(tmpattrsfile);
  419.   if (tmptabfile) unlink(tmptabfile);
  420.   if (tmpdefsfile) unlink(tmpdefsfile);
  421. #endif /* MSDOS | AMIGA */
  422.   exit(k);
  423. #endif /* not VMS, or __VMS_POSIX */
  424. }
  425.