home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnubison.zip / files.c < prev    next >
C/C++ Source or Header  |  1996-06-20  |  13KB  |  509 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. #ifdef OS2
  21. #define XPFILE   "bison.simple"
  22. #define XPFILE1  "bison.hairy"
  23. #endif
  24.  
  25. #if defined (VMS) & !defined (__VMS_POSIX)
  26. #include <ssdef.h>
  27. #define unlink delete
  28. #ifndef XPFILE
  29. #define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
  30. #endif
  31. #ifndef XPFILE1
  32. #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
  33. #endif
  34. #endif
  35.  
  36. #include <stdio.h>
  37. #include "system.h"
  38. #include "files.h"
  39. #include "new.h"
  40. #include "gram.h"
  41.  
  42. FILE *finput = NULL;
  43. FILE *foutput = NULL;
  44. FILE *fdefines = NULL;
  45. FILE *ftable = NULL;
  46. FILE *fattrs = NULL;
  47. FILE *fguard = NULL;
  48. FILE *faction = NULL;
  49. FILE *fparser = NULL;
  50.  
  51. /* File name specified with -o for the output file, or 0 if no -o.  */
  52. char *spec_outfile;
  53.  
  54. char *infile;
  55. char *outfile;
  56. char *defsfile;
  57. char *tabfile;
  58. char *attrsfile;
  59. char *guardfile;
  60. char *actfile;
  61. char *tmpattrsfile;
  62. char *tmptabfile;
  63. char *tmpdefsfile;
  64.  
  65. extern int noparserflag;
  66.  
  67. extern char    *mktemp();    /* So the compiler won't complain */
  68. extern char    *getenv();
  69. extern void    perror();
  70. FILE    *tryopen();    /* This might be a good idea */
  71. void done();
  72.  
  73. extern char *program_name;
  74. extern int verboseflag;
  75. extern int definesflag;
  76. int fixed_outfiles = 0;
  77.  
  78.  
  79. char*
  80. stringappend(string1, end1, string2)
  81. char *string1;
  82. int end1;
  83. char *string2;
  84. {
  85.   register char *ostring;
  86.   register char *cp, *cp1;
  87.   register int i;
  88.  
  89.   cp = string2;  i = 0;
  90.   while (*cp++) i++;
  91.  
  92.   ostring = NEW2(i+end1+1, char);
  93.  
  94.   cp = ostring;
  95.   cp1 = string1;
  96.   for (i = 0; i < end1; i++)
  97.     *cp++ = *cp1++;
  98.  
  99.   cp1 = string2;
  100.   while (*cp++ = *cp1++) ;
  101.  
  102.   return ostring;
  103. }
  104.  
  105.  
  106. #ifdef OS2
  107. char*
  108. makename(string1, end1, string2a, string2b)
  109. char *string1;
  110. int end1;
  111. char *string2a, *string2b;
  112. {
  113.   register char *ostring = stringappend(string1, end1, string2a);
  114.  
  115.   if (!IsFileNameValid(ostring)) {
  116.     free(ostring);
  117.     ostring = stringappend(string1, end1, string2b);
  118.     if (!IsFileNameValid(ostring))
  119.       ChangeNameForFAT(ostring);
  120.   }
  121.  
  122.   return ostring;
  123. }
  124.  
  125. char *
  126. search_template(char *name, char *envvar)
  127. {
  128.   char filename[256], env[1024], *ptr, *last;
  129.  
  130.   if ( access(name, 0) == 0 )
  131.     return name;
  132.  
  133.   if ( (ptr = getenv(envvar)) == NULL )
  134.     return NULL;
  135.  
  136.   strcpy(env, ptr);
  137.  
  138.   for ( ptr = strtok(env, ";"); ptr != NULL; ptr = strtok(NULL, ";") )
  139.   {
  140.     strcpy(filename, ptr);
  141.     last = strchr(filename, 0) - 1;
  142.     if ( *last != '/' && *last != '\\' && *last != ':' )
  143.       strcat(filename, "/");
  144.     strcat(filename, name);
  145.     
  146.     if (!IsFileNameValid(filename))
  147.       ChangeNameForFAT(filename);
  148.  
  149.     if ( access(filename, 0) == 0 )
  150.       return strdup(filename);
  151.   }
  152.  
  153.   return NULL;
  154. }
  155.  
  156. char *
  157. find_template(char *name)
  158. {
  159.   char *path;
  160.  
  161.   if ( (path = search_template(name, "INIT")) == NULL )
  162.     if ( (path = search_template(name, "PATH")) == NULL )
  163.       return name;
  164.  
  165.   return path;
  166. }
  167. #endif
  168.  
  169.  
  170. /* JF this has been hacked to death.  Nowaday it sets up the file names for
  171.    the output files, and opens the tmp files and the parser */
  172. void
  173. openfiles()
  174. {
  175.   char *name_base;
  176.   register char *cp;
  177.   char *filename;
  178.   int base_length;
  179.   int short_base_length;
  180.  
  181. #if defined (VMS) & !defined (__VMS_POSIX)
  182.   char *tmp_base = "sys$scratch:b_";
  183. #else
  184.   char *tmp_base = "/tmp/b.";
  185. #endif
  186.   int tmp_len;
  187.  
  188. #if defined(MSDOS) || defined(OS2)
  189.   tmp_base = getenv ("TMP");
  190.   if (tmp_base == 0)
  191.     tmp_base = "";
  192.   strlwr (infile);
  193. #endif /* MSDOS */
  194.  
  195.   tmp_len = strlen (tmp_base);
  196.  
  197.   if (spec_outfile)
  198.     {
  199.       /* -o was specified.  The precise -o name will be used for ftable.
  200.      For other output files, remove the ".c" or ".tab.c" suffix.  */
  201.       name_base = spec_outfile;
  202. #ifdef MSDOS
  203.       strlwr (name_base);
  204. #endif /* MSDOS */
  205.       /* BASE_LENGTH includes ".tab" but not ".c".  */
  206.       base_length = strlen (name_base);
  207.       if (!strcmp (name_base + base_length - 2, ".c"))
  208.     base_length -= 2;
  209.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  210.       short_base_length = base_length;
  211.       if (!strncmp (name_base + short_base_length - 4, ".tab", 4))
  212.     short_base_length -= 4;
  213.       else if (!strncmp (name_base + short_base_length - 4, "_tab", 4))
  214.     short_base_length -= 4;
  215.     }
  216.   else if (spec_file_prefix)
  217.     {
  218.       /* -b was specified.  Construct names from it.  */
  219.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  220.       short_base_length = strlen (spec_file_prefix);
  221.       /* Count room for `.tab'.  */
  222.       base_length = short_base_length + 4;
  223.       name_base = (char *) xmalloc (base_length + 1);
  224.       /* Append `.tab'.  */
  225.       strcpy (name_base, spec_file_prefix);
  226. #ifdef VMS
  227.       strcat (name_base, "_tab");
  228. #else
  229.       strcat (name_base, ".tab");
  230. #endif
  231. #ifdef MSDOS
  232.       strlwr (name_base);
  233. #endif /* MSDOS */
  234.     }
  235.   else
  236.     {
  237.       /* -o was not specified; compute output file name from input
  238.      or use y.tab.c, etc., if -y was specified.  */
  239.  
  240.       name_base = fixed_outfiles ? "y.y" : infile;
  241.  
  242.       /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any.  */
  243.  
  244.       base_length = strlen (name_base);
  245.       if (!strcmp (name_base + base_length - 2, ".y"))
  246.     base_length -= 2;
  247.       short_base_length = base_length;
  248.  
  249. #ifdef VMS
  250.       name_base = stringappend(name_base, short_base_length, "_tab");
  251.       base_length = short_base_length + 4;
  252. #else
  253. #ifdef OS2
  254.       name_base = makename(name_base, short_base_length, ".tab.c", "_tab.c");
  255.       name_base[strlen(name_base) - 2] = 0;
  256.       base_length = strlen(name_base);
  257.       /* grrr, what a hack */
  258. #else
  259. #ifdef MSDOS
  260.       name_base = stringappend(name_base, short_base_length, "_tab");
  261.       base_length = short_base_length + 4;
  262. #else
  263.       name_base = stringappend(name_base, short_base_length, ".tab");
  264.       base_length = short_base_length + 4;
  265. #endif /* not MSDOS */
  266. #endif
  267. #endif
  268.     }
  269.  
  270.   finput = tryopen(infile, "r");
  271.  
  272.   if (! noparserflag) 
  273.     {
  274.       filename = getenv("BISON_SIMPLE");
  275. #ifdef MSDOS
  276.       /* File doesn't exist in current directory; try in INIT directory.  */
  277.       cp = getenv("INIT");
  278.       if (filename == 0 && cp != NULL)
  279.         {
  280.           filename = xmalloc(strlen(cp) + strlen(PFILE) + 2);
  281.           strcpy(filename, cp);
  282.           cp = filename + strlen(filename);
  283.           *cp++ = '/';
  284.           strcpy(cp, PFILE);
  285.         }
  286. #endif /* MSDOS */
  287. #ifdef OS2
  288.       if (filename == 0)
  289.          filename = find_template(PFILE);
  290. #endif /* OS2 */
  291.       fparser = tryopen(filename ? filename : PFILE, "r");
  292.     }
  293.  
  294.   if (verboseflag)
  295.     {
  296. #ifdef MSDOS
  297.       outfile = stringappend(name_base, short_base_length, ".out");
  298. #else
  299.       /* We used to use just .out if spec_name_prefix (-p) was used,
  300.      but that conflicts with Posix.  */
  301. #ifdef OS2
  302.       outfile = makename(name_base, short_base_length, ".output", ".out");
  303. #else
  304.       outfile = stringappend(name_base, short_base_length, ".output");
  305. #endif
  306. #endif
  307.       foutput = tryopen(outfile, "w");
  308.     }
  309.  
  310.   if (noparserflag)
  311.     {
  312.       /* use permanent name for actions file */
  313.       actfile = stringappend(name_base, short_base_length, ".act");
  314.       faction = tryopen(actfile, "w");
  315.     } 
  316.  
  317. #if defined(MSDOS) || defined(OS2)
  318.   if (! noparserflag)
  319.     actfile = mktemp(stringappend(tmp_base, tmp_len, "/acXXXXXX"));
  320.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "/atXXXXXX"));
  321.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "/taXXXXXX"));
  322.   tmpdefsfile = mktemp(stringappend(tmp_base, tmp_len, "/deXXXXXX"));
  323. #else
  324.   if (! noparserflag)
  325.     actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  326.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  327.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  328.   tmpdefsfile = mktemp(stringappend(tmp_base, tmp_len, "defs.XXXXXX"));
  329. #endif /* not MSDOS */
  330.  
  331.   if (! noparserflag)
  332.     faction = tryopen(actfile, "w+");
  333.   fattrs = tryopen(tmpattrsfile,"w+");
  334.   ftable = tryopen(tmptabfile, "w+");
  335.  
  336.   if (definesflag)
  337.     {
  338.       defsfile = stringappend(name_base, base_length, ".h");
  339.       fdefines = tryopen(tmpdefsfile, "w+");
  340.     }
  341.  
  342. #if !defined(MSDOS) && !defined(OS2)
  343.   if (! noparserflag)
  344.     unlink(actfile);
  345.   unlink(tmpattrsfile);
  346.   unlink(tmptabfile);
  347.   unlink(tmpdefsfile);
  348. #endif
  349.  
  350.     /* These are opened by `done' or `open_extra_files', if at all */
  351.   if (spec_outfile)
  352.     tabfile = spec_outfile;
  353.   else
  354.     tabfile = stringappend(name_base, base_length, ".c");
  355.  
  356. #ifdef VMS
  357.   attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  358.   guardfile = stringappend(name_base, short_base_length, "_guard.c");
  359. #else
  360. #ifdef OS2
  361.   attrsfile = makename(name_base, short_base_length, ".stype.h", ".sth");
  362.   guardfile = makename(name_base, short_base_length, ".guard.c", ".guc");
  363. #else
  364. #ifdef MSDOS
  365.   attrsfile = stringappend(name_base, short_base_length, ".sth");
  366.   guardfile = stringappend(name_base, short_base_length, ".guc");
  367. #else
  368.   attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  369.   guardfile = stringappend(name_base, short_base_length, ".guard.c");
  370. #endif /* not MSDOS */
  371. #endif /* not OS2 */
  372. #endif /* not VMS */
  373. }
  374.  
  375.  
  376.  
  377. /* open the output files needed only for the semantic parser.
  378. This is done when %semantic_parser is seen in the declarations section.  */
  379.  
  380. void
  381. open_extra_files()
  382. {
  383.   FILE *ftmp;
  384.   int c;
  385.   char *filename, *cp;
  386.  
  387.   if (fparser)
  388.     fclose(fparser);
  389.  
  390.   if (! noparserflag) 
  391.     {
  392.       filename = (char *) getenv ("BISON_HAIRY");
  393. #ifdef MSDOS
  394.       /* File doesn't exist in current directory; try in INIT directory.  */
  395.       cp = getenv("INIT");
  396.       if (filename == 0 && cp != NULL)
  397.         {
  398.           filename = xmalloc(strlen(cp) + strlen(PFILE1) + 2);
  399.           strcpy(filename, cp);
  400.           cp = filename + strlen(filename);
  401.           *cp++ = '/';
  402.           strcpy(cp, PFILE1);
  403.         }
  404. #endif
  405. #ifdef OS2
  406.   if (filename == 0)
  407.     filename = find_template(PFILE1);
  408. #endif /* OS2 */
  409.       fparser= tryopen(filename ? filename : PFILE1, "r");
  410.     }
  411.  
  412.         /* JF change from inline attrs file to separate one */
  413.   ftmp = tryopen(attrsfile, "w");
  414.   rewind(fattrs);
  415.   while((c=getc(fattrs))!=EOF)    /* Thank god for buffering */
  416.     putc(c,ftmp);
  417.   fclose(fattrs);
  418.   fattrs=ftmp;
  419.  
  420.   fguard = tryopen(guardfile, "w");
  421.  
  422. }
  423.  
  424.     /* JF to make file opening easier.  This func tries to open file
  425.        NAME with mode MODE, and prints an error message if it fails. */
  426. FILE *
  427. tryopen(name, mode)
  428. char *name;
  429. char *mode;
  430. {
  431.   FILE    *ptr;
  432.  
  433.   ptr = fopen(name, mode);
  434.   if (ptr == NULL)
  435.     {
  436.       fprintf(stderr, "%s: ", program_name);
  437.       perror(name);
  438.       done(2);
  439.     }
  440.   return ptr;
  441. }
  442.  
  443. void
  444. done(k)
  445. int k;
  446. {
  447.   if (faction)
  448.     fclose(faction);
  449.  
  450.   if (fattrs)
  451.     fclose(fattrs);
  452.  
  453.   if (fguard)
  454.     fclose(fguard);
  455.  
  456.   if (finput)
  457.     fclose(finput);
  458.  
  459.   if (fparser)
  460.     fclose(fparser);
  461.  
  462.   if (foutput)
  463.     fclose(foutput);
  464.  
  465.     /* JF write out the output file */
  466.   if (k == 0 && ftable)
  467.     {
  468.       FILE *ftmp;
  469.       register int c;
  470.  
  471.       ftmp=tryopen(tabfile, "w");
  472.       rewind(ftable);
  473.       while((c=getc(ftable)) != EOF)
  474.         putc(c,ftmp);
  475.       fclose(ftmp);
  476.       fclose(ftable);
  477.  
  478.       if (definesflag)
  479.         {
  480.           ftmp = tryopen(defsfile, "w");
  481.           fflush(fdefines);
  482.           rewind(fdefines);
  483.           while((c=getc(fdefines)) != EOF)
  484.             putc(c,ftmp);
  485.           fclose(ftmp);
  486.           fclose(fdefines);
  487.         }
  488.     }
  489.  
  490. #if defined (VMS) & !defined (__VMS_POSIX)
  491.   if (faction && ! noparserflag)
  492.     delete(actfile);
  493.   if (fattrs)
  494.     delete(tmpattrsfile);
  495.   if (ftable)
  496.     delete(tmptabfile);
  497.   if (k==0) sys$exit(SS$_NORMAL);
  498.   sys$exit(SS$_ABORT);
  499. #else
  500. #if defined(MSDOS) || defined(OS2)
  501.   if (actfile && ! noparserflag) unlink(actfile);
  502.   if (tmpattrsfile) unlink(tmpattrsfile);
  503.   if (tmptabfile) unlink(tmptabfile);
  504.   if (tmpdefsfile) unlink(tmpdefsfile);
  505. #endif /* MSDOS */
  506.   exit(k);
  507. #endif /* not VMS, or __VMS_POSIX */
  508. }
  509.