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

  1. /* input_scrub.c - layer between app and the rest of the world
  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. #include "as.h"
  21. #include "read.h"
  22. #include "input-file.h"
  23. #ifdef    VMS
  24. #include <errno.h>    /* Need this to make errno declaration right */
  25. #include <perror.h>    /* Need this to make sys_errlist/sys_nerr right */
  26. #endif /* VMS */
  27. #ifdef NeXT    /* .include feature */
  28. #include <sys/file.h>
  29. #include <sys/param.h>
  30. #endif NeXT    /* .include feature */
  31.  
  32. /*
  33.  * O/S independent module to supply buffers of sanitised source code
  34.  * to rest of assembler. We get raw input data of some length.
  35.  * Also looks after line numbers, for e.g. error messages.
  36.  * This module used to do the sanitising, but now a pre-processor program
  37.  * (app) does that job so this module is degenerate.
  38.  * Now input is pre-sanitised, so we only worry about finding the
  39.  * last partial line. A buffer of full lines is returned to caller.
  40.  * The last partial line begins the next buffer we build and return to caller.
  41.  * The buffer returned to caller is preceeded by BEFORE_STRING and followed
  42.  * by AFTER_STRING. The last character before AFTER_STRING is a newline.
  43.  */
  44.  
  45. /*
  46.  * We expect the following sanitation has already been done.
  47.  *
  48.  * No comments, reduce a comment to a space.
  49.  * Reduce a tab to a space unless it is 1st char of line.
  50.  * All multiple tabs and spaces collapsed into 1 char. Tab only
  51.  *   legal if 1st char of line.
  52.  * # line file statements converted to .line x;.file y; statements.
  53.  * Escaped newlines at end of line: remove them but add as many newlines
  54.  *   to end of statement as you removed in the middle, to synch line numbers.
  55.  */
  56.  
  57. #define BEFORE_STRING ("\n")
  58. #define AFTER_STRING ("\0")    /* bcopy of 0 chars might choke. */
  59. #define BEFORE_SIZE (1)
  60. #define AFTER_SIZE  (1)
  61.  
  62. static char *    buffer_start;    /* -> 1st char of full buffer area. */
  63. static char *    partial_where;    /* -> after last full line in buffer. */
  64. static int    partial_size;    /* >=0. Number of chars in partial line in buffer. */
  65. static char    save_source [AFTER_SIZE];
  66.                 /* Because we need AFTER_STRING just after last */
  67.                 /* full line, it clobbers 1st part of partial */
  68.                 /* line. So we preserve 1st part of partial */
  69.                 /* line here. */
  70. static int    buffer_length;    /* What is the largest size buffer that */
  71.                 /* input_file_give_next_buffer() could */
  72.                 /* return to us? */
  73.  
  74. static void as_1_char ();
  75.  
  76. /*
  77. We never have more than one source file open at once.
  78. We may, however, read more than 1 source file in an assembly.
  79. NULL means we have no file open right now.
  80. */
  81.  
  82.  
  83. /*
  84. We must track the physical file and line number for error messages.
  85. We also track a "logical" file and line number corresponding to (C?)
  86. compiler source line numbers.
  87. Whenever we open a file we must fill in physical_input_file. So if it is NULL
  88. we have not opened any files yet.
  89. */
  90.  
  91. #ifndef NeXT    /* generate stabs for debugging assembly code */
  92. static
  93. #endif NeXT    /* generate stabs for debugging assembly code */
  94. char *        physical_input_file,
  95.      *        logical_input_file;
  96.  
  97. #ifdef NeXT    /* .include feature */
  98. int doing_include = FALSE; /* DJA -- TRUE when we are processing a .include */
  99. #endif NeXT    /* .include feature */
  100.  
  101. typedef unsigned int line_numberT;    /* 1-origin line number in a source file. */
  102.                 /* A line ends in '\n' or eof. */
  103.  
  104. #ifndef NeXT    /* generate stabs for debugging assembly code */
  105. static
  106. #endif NeXT    /* generate stabs for debugging assembly code */
  107. line_numberT    physical_input_line,
  108.         logical_input_line;
  109.  
  110. void
  111. input_scrub_begin ()
  112. {
  113.   know( strlen(BEFORE_STRING) == BEFORE_SIZE );
  114.   know( strlen( AFTER_STRING) ==  AFTER_SIZE );
  115.  
  116.   input_file_begin ();
  117.  
  118.   buffer_length = input_file_buffer_size ();
  119.  
  120.   buffer_start = xmalloc ((long)(BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
  121.   bcopy (BEFORE_STRING, buffer_start, (int)BEFORE_SIZE);
  122.  
  123.   /* Line number things. */
  124.   logical_input_line = 0;
  125.   logical_input_file = (char *)NULL;
  126.   physical_input_file = NULL;    /* No file read yet. */
  127.   do_scrub_begin();
  128. }
  129.  
  130. void
  131. input_scrub_end ()
  132. {
  133.   input_file_end ();
  134. }
  135.  
  136. char *                /* Return start of caller's part of buffer. */
  137. input_scrub_new_file (filename)
  138.      char *    filename;
  139. {
  140.   input_file_open (filename, !flagseen['f']);
  141.   physical_input_file = filename[0] ? filename : "{standard input}";
  142.   physical_input_line = 0;
  143.  
  144.   partial_size = 0;
  145.   return (buffer_start + BEFORE_SIZE);
  146. }
  147.  
  148. char *
  149. input_scrub_next_buffer (bufp)
  150. char **bufp;
  151. {
  152.   register char *    limit;    /* -> just after last char of buffer. */
  153.  
  154. #ifdef DONTDEF
  155.   if(preprocess) {
  156.     if(save_buffer) {
  157.       *bufp = save_buffer;
  158.       save_buffer = 0;
  159.     }
  160.     limit = input_file_give_next_buffer(buffer_start+BEFORE_SIZE);
  161.     if (!limit) {
  162.       partial_where = 0;
  163.       if(partial_size)
  164.         as_warn("Partial line at end of file ignored");
  165.       return partial_where;
  166.     }
  167.  
  168.     if(partial_size)
  169.       bcopy(save_source, partial_where,(int)AFTER_SIZE);
  170.     do_scrub(partial_where,partial_size,buffer_start+BEFORE_SIZE,limit-(buffer_start+BEFORE_SIZE),&out_string,&out_length);
  171.     limit=out_string + out_length;
  172.     for(p=limit;*--p!='\n';)
  173.       ;
  174.     p++;
  175.     if(p<=buffer_start+BEFORE_SIZE)
  176.       as_fatal("Source line too long.  Please change file '%s' and re-make the assembler.",__FILE__);
  177.  
  178.     partial_where = p;
  179.     partial_size = limit-p;
  180.     bcopy(partial_where, save_source,(int)AFTER_SIZE);
  181.     bcopy(AFTER_STRING, partial_where, (int)AFTER_SIZE);
  182.  
  183.     save_buffer = *bufp;
  184.     *bufp = out_string;
  185.  
  186.     return partial_where;
  187.   }
  188.  
  189.   /* We're not preprocessing.  Do the right thing */
  190. #endif
  191.   if (partial_size)
  192.     {
  193.       bcopy (partial_where, buffer_start + BEFORE_SIZE, (int)partial_size);
  194.       bcopy (save_source, buffer_start + BEFORE_SIZE, (int)AFTER_SIZE);
  195.     }
  196.   limit = input_file_give_next_buffer (buffer_start + BEFORE_SIZE + partial_size);
  197.   if (limit)
  198.     {
  199.       register char *    p;    /* Find last newline. */
  200.  
  201.       for (p = limit;   * -- p != '\n';   )
  202.     {
  203.     }
  204.       ++ p;
  205.       if (p <= buffer_start + BEFORE_SIZE)
  206.     {
  207.       as_fatal ("Source line too long. Please change file %s then rebuild assembler.", __FILE__);
  208.     }
  209.       partial_where = p;
  210.       partial_size = limit - p;
  211.       bcopy (partial_where, save_source,  (int)AFTER_SIZE);
  212.       bcopy (AFTER_STRING, partial_where, (int)AFTER_SIZE);
  213.     }
  214.   else
  215.     {
  216.       partial_where = 0;
  217.       if (partial_size > 0)
  218.     {
  219.       as_warn( "Partial line at end of file ignored" );
  220.     }
  221.     }
  222.   return (partial_where);
  223. }
  224.  
  225. /*
  226.  * The remaining part of this file deals with line numbers, error
  227.  * messages and so on.
  228.  */
  229.  
  230.  
  231. int
  232. seen_at_least_1_file ()        /* TRUE if we opened any file. */
  233. {
  234.   return (physical_input_file != NULL);
  235. }
  236.  
  237. void
  238. bump_line_counters ()
  239. {
  240.   ++ physical_input_line;
  241.   ++ logical_input_line;
  242. }
  243.  
  244. /*
  245.  *            new_logical_line()
  246.  *
  247.  * Tells us what the new logical line number and file are.
  248.  * If the line_number is <0, we don't change the current logical line number.
  249.  * If the fname is NULL, we don't change the current logical file name.
  250.  */
  251. void
  252. new_logical_line (fname, line_number)
  253.      char *    fname;        /* DON'T destroy it! We point to it! */
  254.      int    line_number;
  255. {
  256.   if ( fname )
  257.     {
  258.       logical_input_file = fname;
  259.     }
  260.   if ( line_number >= 0 )
  261.     {
  262.       logical_input_line = line_number;
  263.     }
  264. }
  265.  
  266. /*
  267.  *            a s _ w h e r e ( )
  268.  *
  269.  * Write a line to stderr locating where we are in reading
  270.  * input source files.
  271.  * As a sop to the debugger of AS, pretty-print the offending line.
  272.  */
  273. void
  274. as_where()
  275. {
  276.   char *p;
  277.   line_numberT line;
  278.  
  279.   if (physical_input_file)
  280.     {                /* we tried to read SOME source */
  281.       if (input_file_is_open())
  282.     {            /* we can still read lines from source */
  283. #ifdef DONTDEF
  284.       fprintf (stderr," @ physical line %ld., file \"%s\"",
  285.            (long) physical_input_line, physical_input_file);
  286.       fprintf (stderr," @ logical line %ld., file \"%s\"\n",
  287.            (long) logical_input_line, logical_input_file);
  288.       (void)putc(' ', stderr);
  289.       as_howmuch (stderr);
  290.       (void)putc('\n', stderr);
  291. #else
  292.         p = logical_input_file ? logical_input_file : physical_input_file;
  293.         line = logical_input_line ? logical_input_line : physical_input_line;
  294.         fprintf(stderr,"%s:%u:", p, line);
  295. #endif
  296.     }
  297.       else
  298.     {
  299. #ifdef DONTDEF
  300.       fprintf (stderr," After reading source.\n");
  301. #else
  302.     p = logical_input_file ? logical_input_file : physical_input_file;
  303.     line = logical_input_line ? logical_input_line : physical_input_line;
  304.     fprintf (stderr,"%s:unknown:", p);
  305. #endif
  306.     }
  307.     }
  308.   else
  309.     {
  310. #ifdef DONTDEF
  311.       fprintf (stderr," Before reading source.\n");
  312. #else
  313. #endif
  314.     }
  315. }
  316.  
  317.  
  318.  
  319. /*
  320.  *            a s _ p e r r o r
  321.  *
  322.  * Like perror(3), but with more info.
  323.  */
  324. void
  325. as_perror(gripe, filename)
  326.      char *    gripe;        /* Unpunctuated error theme. */
  327.      char *    filename;
  328. {
  329.   extern int errno;        /* See perror(3) for details. */
  330.   extern int sys_nerr;
  331.   extern char * sys_errlist[];
  332.  
  333.   fprintf (stderr,"as:file(%s) %s! ",
  334.        filename, gripe
  335.        );
  336.   if (errno > sys_nerr)
  337.     {
  338.       fprintf (stderr, "Unknown error #%d.", errno);
  339.     }
  340.   else
  341.     {
  342.       fprintf (stderr, "%s.", sys_errlist [errno]);
  343.     }
  344.   (void)putc('\n', stderr);
  345.   errno = 0;            /* After reporting, clear it. */
  346.   if (input_file_is_open())    /* RMS says don't mention line # if not needed. */
  347.     {
  348.       as_where();
  349.     }
  350. }
  351.  
  352. /*
  353.  *            a s _ h o w m u c h ( )
  354.  *
  355.  * Output to given stream how much of line we have scanned so far.
  356.  * Assumes we have scanned up to and including input_line_pointer.
  357.  * No free '\n' at end of line.
  358.  */
  359. void
  360. as_howmuch (stream)
  361.      FILE * stream;        /* Opened for write please. */
  362. {
  363.   register    char *    p;    /* Scan input line. */
  364.   /* register    char    c; JF unused */
  365.  
  366.   for (p = input_line_pointer - 1;   * p != '\n';   --p)
  367.     {
  368.     }
  369.   ++ p;                /* p -> 1st char of line. */
  370.   for (;  p <= input_line_pointer;  p++)
  371.     {
  372.       /* Assume ASCII. EBCDIC & other micro-computer char sets ignored. */
  373.       /* c = *p & 0xFF; JF unused */
  374.       as_1_char (*p, stream);
  375.     }
  376. }
  377.  
  378. static void
  379. as_1_char (c,stream)
  380.      unsigned char c;
  381.      FILE *    stream;
  382. {
  383.   if ( c > 127 )
  384.     {
  385.       (void)putc( '%', stream);
  386.       c -= 128;
  387.     }
  388.   if ( c < 32 )
  389.     {
  390.       (void)putc( '^', stream);
  391.       c += '@';
  392.     }
  393.   (void)putc( c, stream);
  394. }
  395.  
  396. #ifdef NeXT    /* .include feature */
  397. void
  398. read_an_include_file (no_path_name) /* DJA -- added for .include pseudo op support */
  399.      char *    no_path_name;
  400. {
  401.   char                          * buffer;
  402.   int                      buffer_length;
  403.   extern char                      * buffer_limit;
  404.   extern int                  doing_include;
  405.   extern FILE                      * f_in;
  406.   extern char                      * file_name;
  407.   extern char                      * input_line_pointer;
  408.   char                          * last_buffer_limit;
  409.   char                             * last_buffer_start;
  410.   int                      last_doing_include;
  411.   FILE                            * last_f_in;
  412.   char                            * last_file_name;
  413.   char                          * last_input_line_pointer;
  414.   char                             * last_logical_input_file;
  415.   line_numberT                  last_logical_input_line;
  416.   int                       last_partial_size;
  417.   char                            * last_partial_where;
  418.   char                           * last_physical_input_file;
  419.   line_numberT                  last_physical_input_line;
  420.   char                       last_save_source [AFTER_SIZE];
  421. #if 0
  422.   char                    * last_save_buffer;
  423. #endif
  424.   char                      name_buffer [MAXPATHLEN];
  425.   scrub_context_data              scrub_context;
  426.   register struct directory_stack    * the_path_pointer;
  427.   register char                * whole_file_name;
  428.  
  429.   extern struct directory_stack *include;    /* First dir to search */
  430.  
  431.  /*
  432.   * figure out what directory the file name is in.
  433.   */
  434.   whole_file_name = no_path_name;
  435.   if (access(whole_file_name, R_OK))
  436.     {
  437.       whole_file_name = name_buffer;
  438.       the_path_pointer = include;
  439.       while (the_path_pointer)
  440.         {
  441.       if (strlen (the_path_pointer->fname) + (strlen (no_path_name)) >= MAXPATHLEN)
  442.         as_warn ("include file name too long: \"%s%s\"", the_path_pointer->fname, no_path_name);
  443.       else
  444.         {
  445.           *whole_file_name = '\0';
  446.           strcpy (whole_file_name, the_path_pointer->fname);
  447.           strcat (whole_file_name, "/");
  448.           strcat (whole_file_name, no_path_name);
  449.           if (!access(whole_file_name, R_OK))
  450.             goto found;
  451.         }
  452.       the_path_pointer = the_path_pointer->next;
  453.     }
  454.       the_path_pointer = include_defaults;
  455.       while (the_path_pointer->fname != NULL)
  456.         {
  457.       if (strlen (the_path_pointer->fname) + (strlen (no_path_name)) >= MAXPATHLEN)
  458.         as_warn ("include file name too long: \"%s%s\"", the_path_pointer->fname, no_path_name);
  459.       else
  460.         {
  461.           *whole_file_name = '\0';
  462.           strcpy (whole_file_name, the_path_pointer->fname);
  463.           strcat (whole_file_name, "/");
  464.           strcat (whole_file_name, no_path_name);
  465.           if (!access(whole_file_name, R_OK))
  466.             goto found;
  467.         }
  468.       the_path_pointer++;
  469.     }
  470.       as_fatal ("Couldn't find the include file: \"%s\"", no_path_name);
  471.       return;
  472.     }
  473. found:
  474.  /*
  475.   * save a copy of the file state for a recursive call to read a file
  476.   */
  477.   last_buffer_limit = buffer_limit;
  478.   last_buffer_start = buffer_start;
  479.   last_doing_include = doing_include;
  480.   last_f_in = f_in;
  481.   last_file_name = file_name;
  482.   last_input_line_pointer = input_line_pointer;
  483.   last_logical_input_file = logical_input_file;
  484.   last_logical_input_line = logical_input_line;
  485.   last_partial_size = partial_size;
  486.   last_partial_where = partial_where;
  487.   last_physical_input_file = physical_input_file;
  488.   last_physical_input_line = physical_input_line;
  489.   bcopy (save_source, last_save_source, sizeof (save_source));
  490.   save_scrub_context (&scrub_context);
  491.  /*
  492.   * set up for another file
  493.   */
  494.   partial_size = 0;
  495.   doing_include = TRUE;
  496.   input_scrub_begin ();
  497.   buffer = input_scrub_new_file (whole_file_name);
  498.   if (f_in != (FILE *)0)
  499.     read_a_source_file(buffer);
  500.  
  501.   xfree (buffer_start);
  502.  /*
  503.   * restore the file state
  504.   */
  505.   buffer_limit = last_buffer_limit;
  506.   buffer_start = last_buffer_start;
  507.   doing_include = last_doing_include;
  508.   f_in = last_f_in;
  509.   file_name = last_file_name;
  510.   input_line_pointer = last_input_line_pointer;
  511.   logical_input_file = last_logical_input_file;
  512.   logical_input_line = last_logical_input_line;
  513.   partial_size = last_partial_size;
  514.   partial_where = last_partial_where;
  515.   physical_input_file = last_physical_input_file;
  516.   physical_input_line = last_physical_input_line;
  517.   bcopy (last_save_source, save_source, sizeof (save_source));
  518.   restore_scrub_context (&scrub_context);
  519. } /* read_an_include_file */
  520. #endif NeXT    /* .include feature */
  521.  
  522. /* end: input_scrub.c */
  523.