home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Source / GNU / cctools / as / input-file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-22  |  7.9 KB  |  318 lines

  1. /* input_file.c - Deal with Input Files -
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS 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. GAS 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 GAS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * Confines all details of reading source bytes to this module.
  22.  * All O/S specific crocks should live here.
  23.  * What we lose in "efficiency" we gain in modularity.
  24.  * Note we don't need to #include the "as.h" file. No common coupling!
  25.  */
  26.  
  27. #define NDEBUG        /* JF remove asserts */
  28.  
  29. #ifdef USG
  30. #define index strchr
  31. #define setbuffer(stream, buf, size) setvbuf((stream), (buf), _IOLBF, (size))
  32. #endif
  33.  
  34. #include <stdio.h>
  35. #include <assert.h>
  36. /* #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <sys/file.h>
  39. #include <sys/wait.h> */
  40.  
  41. /* #include "style.h" */
  42. #include "input-file.h"
  43.  
  44. #ifdef NeXT    /* .include feature */
  45. extern int doing_include; /* DJA -- TRUE when we are processing a .include */
  46. #endif NeXT    /* .include feature */
  47.  
  48. /* This variable is non-zero if the file currently being read should be
  49.    preprocessed by app.  It is zero if the file can be read straight in.
  50.  */
  51. int preprocess = 0;
  52.  
  53. void    as_perror();
  54.  
  55. /*
  56.  * This code opens a file, then delivers BUFFER_SIZE character
  57.  * chunks of the file on demand.
  58.  * BUFFER_SIZE is supposed to be a number chosen for speed.
  59.  * The caller only asks once what BUFFER_SIZE is, and asks before
  60.  * the nature of the input files (if any) is known.
  61.  */
  62.  
  63. #define BUFFER_SIZE (32 * 1024)
  64.  
  65. #ifndef NeXT
  66. static char in_buf[BUFFER_SIZE];
  67. #endif /* !defined(NeXT) */
  68.  
  69. /*
  70.  * We use static data: the data area is not sharable.
  71.  */
  72.  
  73. FILE *f_in;    /* JF do things the RIGHT way */
  74. /* static JF remove static so app.c can use file_name */
  75. char *    file_name;
  76.  
  77. /* These hooks accomodate most operating systems. */
  78.  
  79. void
  80. input_file_begin ()
  81. {
  82.   /* file_handle = -1; */
  83.   f_in = (FILE *)0;
  84. }
  85.  
  86. void
  87. input_file_end ()
  88. {
  89. }
  90.  
  91. int                /* Return BUFFER_SIZE. */
  92. input_file_buffer_size ()
  93. {
  94.   return (BUFFER_SIZE);
  95. }
  96.  
  97. int
  98. input_file_is_open ()
  99. {
  100.   /* return (file_handle >= 0); */
  101.   return f_in!=(FILE *)0;
  102. }
  103.  
  104. #ifdef DONTDEF        /* JF save old version in case we need it */
  105. void
  106. input_file_open (filename, preprocess, debugging)
  107.      char *    filename;    /* "" means use stdin. Must not be 0. */
  108.      int    preprocess;    /* TRUE if needs app. */
  109.      int    debugging;    /* TRUE if we are debugging assembler. */
  110. {
  111.   assert( filename != 0 );    /* Filename may not be NULL. */
  112.   if (filename [0])
  113.     {                /* We have a file name. Suck it and see. */
  114.       file_handle = open (filename, O_RDONLY, 0);
  115.       file_name = filename;
  116.     }
  117.   else
  118.     {                /* use stdin for the input file. */
  119.       file_handle = fileno (stdin);
  120.       file_name = "{standard input}"; /* For error messages. */
  121.     }
  122.   if (file_handle < 0)
  123.     {
  124.       as_perror ("Can't open source file for input", file_name);
  125.     }
  126.   if ( preprocess )
  127.     {
  128. /*
  129.  * This code was written in haste for a frobbed BSD 4.2.
  130.  * I have a flight to catch: will someone please do proper
  131.  * error checks? - Dean.
  132.  */
  133.       int    pid;
  134.       char temporary_file_name [12];
  135.       int    fd;
  136.       union wait    status;
  137.       char    *mktemp();
  138.  
  139.       (void)strcpy (temporary_file_name, "#appXXXXXX");
  140.       (void)mktemp (temporary_file_name);
  141.       pid = vfork ();
  142.       if (pid == -1)
  143.     {
  144.       as_perror ("Can't fork to run app", file_name);
  145.       _exit (144);
  146.     }
  147.       if (pid == 0)
  148.     {
  149.       (void)dup2 (file_handle, fileno(stdin));
  150.       fd = open (temporary_file_name, O_WRONLY + O_TRUNC + O_CREAT, 0666);
  151.       if (fd == -1)
  152.         {
  153.           (void)write(2,"Can't open temporary\n",21);
  154.           _exit (99);
  155.         }
  156.       (void)dup2 (fd, fileno(stdout));
  157. /* JF for testing #define PREPROCESSOR "/lib/app" */
  158. #define PREPROCESSOR "./app"
  159.       execl (PREPROCESSOR, PREPROCESSOR, 0);
  160.       execl ("app","app",0);
  161.       (void)write(2,"Exec of app failed.  Get help.\n",31);
  162.       (void)unlink(temporary_file_name);
  163.       _exit (11);
  164.     }
  165.       (void)wait (& status);
  166.       if (status.w_status & 0xFF00)        /* JF was 0xF000, was wrong */
  167.     {
  168.       file_handle = -1;
  169.       as_warn( "Can't preprocess file \"%s\", status = %xx", file_name, status.w_status );
  170.     }
  171.       else
  172.     {
  173.       file_handle = open (temporary_file_name, O_RDONLY, 0);
  174.       if ( ! debugging )
  175.         {
  176.           if (unlink(temporary_file_name))
  177.         {
  178.           as_perror ("Can't delete temporary file, continuing", temporary_file_name);
  179.         }
  180.         }
  181.     }
  182.       if (file_handle == -1)
  183.     {
  184.       as_perror ("Can't retrieve temporary file", temporary_file_name);
  185.     }
  186.     }
  187. }
  188. #else
  189.  
  190. void
  191. input_file_open (filename,pre)
  192.      char *    filename;    /* "" means use stdin. Must not be 0. */
  193.      int pre;
  194. {
  195.     int    c;
  196.     char    buf[80];
  197.  
  198.     preprocess = pre;
  199.  
  200.     assert( filename != 0 );    /* Filename may not be NULL. */
  201.     if (filename [0]) {    /* We have a file name. Suck it and see. */
  202.         f_in=fopen(filename,"r");
  203.         file_name=filename;
  204.     } else {            /* use stdin for the input file. */
  205.         f_in = stdin;
  206.         file_name = "{standard input}"; /* For error messages. */
  207.     }
  208.     if (f_in==(FILE *)0) {
  209.         as_perror ("Can't open source file for input", file_name);
  210.         return;
  211.     }
  212. #ifdef NeXT
  213.     setbuffer(f_in, xmalloc(BUFFER_SIZE), BUFFER_SIZE);
  214. #else /* !defined(NeXT)
  215. #ifndef VMS
  216.     setbuffer(f_in,in_buf,BUFFER_SIZE);
  217. #endif /* VMS */
  218. #endif /* NeXT */
  219.     c=getc(f_in);
  220.     if(c=='#') {    /* Begins with comment, may not want to preprocess */
  221.         c=getc(f_in);
  222.         if(c=='N') {
  223.             fgets(buf,80,f_in);
  224.             if(!strcmp(buf,"O_APP\n"))
  225.                 preprocess=0;
  226.             if(!index(buf,'\n'))
  227.                 ungetc('#',f_in);    /* It was longer */
  228.             else
  229.                 ungetc('\n',f_in);
  230.         } else if(c=='\n')
  231.             ungetc('\n',f_in);
  232.         else
  233.             ungetc('#',f_in);
  234.     } else
  235.         ungetc(c,f_in);
  236.  
  237. #ifdef DONTDEF
  238.     if ( preprocess ) {
  239.         char temporary_file_name [17];
  240.         char    *mktemp();
  241.         FILE    *f_out;
  242.  
  243.         (void)strcpy (temporary_file_name, "/tmp/#appXXXXXX");
  244.         (void)mktemp (temporary_file_name);
  245.         f_out=fopen(temporary_file_name,"w+");
  246.         if(f_out==(FILE *)0) {
  247.             as_perror("Can't open temp file");
  248.         }
  249.             /* JF this will have to be moved on any system that
  250.                does not support removal of open files.  */
  251.         (void)unlink(temporary_file_name);/* JF do it NOW */
  252.         do_scrub(f_in,f_out);
  253.         (void)fclose(f_in);    /* All done with it */
  254.         (void)rewind(f_out);
  255.         f_in=f_out;
  256.     }
  257. #endif
  258. }
  259. #endif
  260.  
  261. char *
  262. input_file_give_next_buffer (where)
  263.      char *        where;    /* Where to place 1st character of new buffer. */
  264. {
  265.   char *    return_value;    /* -> Last char of what we read, + 1. */
  266.   register int    size;
  267.  
  268.   if (f_in == (FILE *)0)
  269.       return 0;
  270.       /*
  271.        * fflush (stdin); could be done here if you want to synchronise
  272.        * stdin and stdout, for the case where our input file is stdin.
  273.        * Since the assembler shouldn't do any output to stdout, we
  274.        * don't bother to synch output and input.
  275.        */
  276.   /* size = read (file_handle, where, BUFFER_SIZE); */
  277.   if(preprocess) {
  278.     char *p;
  279.     int n;
  280.     int ch;
  281.     extern FILE *scrub_file;
  282.     int scrub_from_file();
  283.     void scrub_to_file();
  284.     int do_scrub_next_char();
  285.  
  286.     scrub_file=f_in;
  287.     for(p=where,n=BUFFER_SIZE;n;--n) {
  288.         ch=do_scrub_next_char(scrub_from_file,scrub_to_file);
  289.         if(ch==EOF)
  290.             break;
  291.         *p++=ch;
  292.     }
  293.     size=BUFFER_SIZE-n;
  294.   } else
  295.     size= fread(where,sizeof(char),BUFFER_SIZE,f_in);
  296.   if (size < 0)
  297.     {
  298.       as_perror ("Can't read source file: end-of-file faked.", file_name);
  299.       size = 0;
  300.     }
  301.   if (size)
  302.     return_value = where + size;
  303.   else
  304.     {
  305. #ifdef NeXT    /* .include feature */
  306.       if(doing_include)
  307.     free (f_in->_base);
  308. #endif NeXT    /* .include feature */
  309.       if (fclose (f_in))
  310.     as_perror ("Can't close source file -- continuing", file_name);
  311.       f_in = (FILE *)0;
  312.       return_value = 0;
  313.     }
  314.   return (return_value);
  315. }
  316.  
  317. /* end: input_file.c */
  318.