home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / nasm20b / nasm_src / io.c < prev    next >
C/C++ Source or Header  |  1993-01-19  |  5KB  |  194 lines

  1. /* ---------------------------------------------------------------------- */
  2. /*                   Copyright (C) 1991 by Natürlich!                     */
  3. /*                      This file is copyrighted!                         */
  4. /*                Refer to the documentation for details.                 */
  5. /* ---------------------------------------------------------------------- */
  6. /*                   'Trusted' I/O functions                    */
  7. /* ------------------------------------------------------------ */
  8. #define  __IO__            /* for WHAT ?? */
  9. #include <stdio.h>
  10. #include "defines.h"
  11. #include "nasm.h"
  12. #include OSBIND
  13. #include "debug.h"
  14. #include NMALLOC_H
  15. #include "buffer.h"
  16.  
  17. #if __NSTDC__
  18. extern int     (*gettoken)( void);
  19. #else
  20. extern int     (*gettoken)();
  21. #endif
  22.  
  23. extern buffer huge   *bp;
  24.  
  25. extern int     show_expansion;
  26. extern char    c, header[], *realname;
  27. static char    noread[] = "Couldn't read-access file";
  28.  
  29.  
  30. static int      tabval;
  31.  
  32. void    tab()
  33. {               
  34.    int     i;
  35.         
  36.    for( i = tabval; i; i--)
  37.       putchar( ' ');
  38. }
  39.  
  40. void    tabin()
  41. {
  42.    if( ! tabval--)
  43.       nierror("Tabbing goes negative");
  44.    tab();
  45. }
  46.  
  47.  
  48. void    tabout()
  49. {
  50.    tab();
  51.    ++tabval;
  52. }
  53.  
  54.         
  55. void next_buffer( p)
  56. register buffer huge *p;
  57. {
  58.    ENTER("next_buffer");
  59.    p->buflist = p->p = (byte huge *) p + sizeof( buffer) + 2;
  60.    if( (long) (p->remain = Fread( p->_aux1, NBUFSIZ, p->buflist)) < 0)
  61.       nferror( noread);
  62.    LEAVE();
  63. }
  64.  
  65.  
  66. void file_done( p)
  67. register buffer huge *p;
  68. {
  69.    register buffer huge *q;
  70.  
  71.    ENTER("file_done");
  72.    Fclose( p->_aux1);      /* MUST WORK and if it doesn't ... */
  73.    if( q = p->before)
  74.    {
  75.       q->line--;
  76.       if( ! (q->type & BUF_TOKEN))
  77.       {
  78.          *--q->p = q->_aux2;     /* † */
  79.          q->remain++;
  80.       }
  81.    }
  82.    if( show_expansion)
  83.    {
  84.         tabin();
  85.         printf( "Closed file: \"%s\"\n", p->name);
  86.    }
  87.    LEAVE();
  88. }
  89.  
  90.  
  91.  
  92. void include( afile, ext)
  93. char huge *afile,
  94.      huge *ext;
  95. {
  96.    register buffer huge *p;
  97.    int                  i;
  98.    register char huge   *x;
  99.  
  100.    ENTER("include");
  101. #if LOWERFILE
  102.    downcase( afile);
  103. #endif
  104.  
  105.    for( x = afile; *x; x++)                        /* if it doesn't have an extension */
  106.       if( *x == DIRSLASH || *x == '.')
  107.       {
  108.          IMESS( "Trying to open \"%s\" ", (lword) afile, 4);
  109.          if( (i = (int) Fopen( x = afile, OPEN_R)) >= 0)
  110.             goto opened;
  111.          break;
  112.       }
  113.  
  114.    {
  115.       int   off = (int) strlen( header);
  116.  
  117.       MESS("Open failed");
  118.       strcpy( x = nmalloc( (long) (strlen( afile) + off + 5L)), header);
  119.       strcat( x, afile);
  120.       complete( x, ext, 1);
  121.       afile = x + off;
  122. oncemore:
  123.       IMESS( "Trying to open \"%s\" ", (lword) x, 4);
  124.       if( (i = (int) Fopen( x, OPEN_R)) < 0)
  125.       {
  126.          if( ! bp)                              /* source file ? */
  127.             nferror("Source file does not exist");
  128.          if( i == -35)
  129.             nferror("Out of GEMDOS file handles (too many .includes ?)");
  130.          if( bp && afile != x)
  131.          {
  132.             x = afile;                  /* Include files try with header */
  133.             goto oncemore;
  134.          }                              /* V-- this pig's coding is 'safe' */
  135.          sprintf( header, "Include file \"%s\" does not exist", x);
  136.          nferror( header);
  137.       }
  138.    }
  139.    
  140. opened:
  141.    p         = (buffer huge *) nmalloc( (long) (sizeof( buffer) + NBUFSIZ + 2));
  142.    p->buflist= p->p = (byte huge *) p + sizeof( buffer) + 2;
  143.    if( (long) (p->remain = Fread( i, NBUFSIZ, p->buflist)) < 0)
  144.       nferror("Couldn't read-access file");
  145.    if( ! p->remain)
  146.    {
  147.       Fclose( i);
  148.       nwarning("File is completely empty");
  149.       nfree( (void huge *) p);
  150.       LEAVE();
  151.       return;
  152.    }
  153.    IMESS("Open worked! p->remain = %ld", p->remain, 4);
  154.    if( p->before = bp)              /* should be the nil_buffer for */
  155.    {                                /* the source file              */
  156.       p->name         = get_filename( x);
  157.       p->before->next = p;
  158.       if( ! (bp->type & BUF_TOKEN))
  159.          bp->_aux2  = c;
  160.    }
  161.    else
  162.       p->name    = realname ? get_filename( realname) : get_filename( x);
  163.    c             = to_upper( *p->p++); /* what's this ? v1.2 puzzlement */
  164.    p->remain--;
  165.    p->_aux1      = i;
  166.    p->type       = BUF_FILE | BUF_FREE | BUF_ALLOC;
  167.    p->line       = 0;
  168.    p->next       = 0;
  169.    p->multi.fill = next_buffer;
  170.    p->done       = file_done;
  171.    p->get        = (int (*)()) (gettoken = lexer); /* **-PORT #1-**/
  172.    bp            = p;
  173.    if( show_expansion)
  174.    {
  175.         tabout();
  176.         if( bp->before)
  177.                 printf( "line %4.4d, ", bp->before->line);
  178.         printf( "opening file: \"%s\"\n", x);
  179.    }
  180.    LEAVE();
  181. }
  182.  
  183. /* †
  184.    But .. but. that's TERRIBLE!! How can you do such a thing!
  185.    Think of the consequences. Maybe you never read a thing from the
  186.    old buffer may be the buffer just got refilled. You could be over-
  187.    writing good memory. Ohhh!
  188.  
  189.    Not to worry. We allocate *two* extrabytes for each file inclosure
  190.    to have space just before the actual buffer starts as ...undo...
  191.    storage. OK ?? O.K. ??????
  192. */
  193.  
  194.