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