home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d583 / aroff.lha / ARoff / Sources / fichiers.c < prev    next >
C/C++ Source or Header  |  1992-01-04  |  6KB  |  254 lines

  1. /*
  2.  * Fonctions d'acces aux fichiers
  3.  * (c)1991 par Denis GOUNELLE
  4.  */
  5.  
  6. #include "aroff.h"
  7. #include "pile.h"
  8.  
  9. extern struct TeteListe TMac ;
  10. extern struct Macro *CurrentMacro ;
  11. extern struct String *CurrentString ;
  12. extern unsigned char OutputBuf[], Arg[] ;
  13. extern struct InputFile *CurrentInputFile ;
  14. extern long InputMode, OutputLen, TmpLineLen, ArgLen, Flg, EmptyToWrite ;
  15.  
  16. /***********************************************************************/
  17.  
  18. static long GetFromMac()
  19. {
  20.   register unsigned long c ;
  21.   register struct Macro *m ;
  22.  
  23.   m = CurrentMacro ;
  24.  
  25.   if ( (m->m_NextL) && (m->m_NextC >= m->m_NextL->ml_Len) )
  26.   {
  27.     m->m_NextC = 0 ;
  28.     m->m_NextL = (struct MLine *)m->m_NextL->ml_Node.el_Suivant ;
  29.     return( '\n' ) ;
  30.   }
  31.  
  32.   if ( m->m_NextL )
  33.   {
  34.     c = m->m_NextL->ml_Text[m->m_NextC] ;
  35.     m->m_NextC++ ;
  36.     return( c ) ;
  37.   }
  38.  
  39.   return( EOF ) ;
  40. }
  41.  
  42. /***********************************************************************/
  43.  
  44. long GetChar()
  45. {
  46.   char *p ;
  47.   struct InputFile *f ;
  48.   register unsigned long c ;
  49.   register struct Macro *m ;
  50.   register struct String *s ;
  51.  
  52. _restart:
  53.  
  54.   if ( InputMode & IM_EXMACRO )
  55.   {
  56.     for (;;)
  57.     {
  58.       c = GetFromMac() ;
  59.       if ( c != EOF ) return( c ) ;
  60.  
  61.       BCLR( Flg , F_TRAP ) ;
  62.       InputMode = Pop( TE_INMODE , 1 ) ;
  63.  
  64.       if ( InputMode & IM_STRING )
  65.       {
  66.     s = (struct String *)Pop( TE_STRING , 1 ) ;
  67.     CurrentString = s ;
  68.       }
  69.       if (! (InputMode & IM_EXMACRO)) break ;
  70.  
  71.       m = (struct Macro *)Pop( TE_MACRO , 1 ) ;
  72.  
  73.       CurrentMacro = m ;
  74.       if ( m->m_Flag & MF_TRAP ) BSET( Flg , F_TRAP ) ;
  75.       if ( m->m_Flag & MF_WAIT )
  76.       {
  77.     BCLR( m->m_Flag , MF_WAIT ) ;
  78.     p = (char *)Pop( TE_OUTLINE , 1 ) ;
  79.     c = Pop( TE_TOWRITE , 0 ) ;
  80.     if ( c != -1 ) EmptyToWrite = c ;
  81.     LigneSuiv( p ) ;
  82.     free( p ) ;
  83.       }
  84.     }
  85.  
  86.     while (! (Flg & F_TRAP))
  87.     {
  88.       p = (char *)Pop( TE_OUTLINE , 0 ) ;
  89.       if ( (! p) || (p == (char *)-1) ) break ;
  90.       c = Pop( TE_TOWRITE , 0 ) ;
  91.       if ( c != -1 ) EmptyToWrite = c ;
  92.       LigneSuiv( p ) ;
  93.       free( p ) ;
  94.     }
  95.  
  96.     if ( Flg & F_TRAP ) goto _restart ;
  97.     BCLR( InputMode , IM_EXMACRO ) ;
  98.     CurrentMacro = NULL ;
  99.   }
  100.  
  101.   if ( InputMode & IM_STRING )
  102.   {
  103.     s = CurrentString ;
  104.     if ( s->s_pos >= s->s_len )
  105.     {
  106.       free( s ) ;
  107.       InputMode = Pop( TE_INMODE , 1 ) ;
  108.       if ( InputMode & IM_STRING )
  109.       {
  110.     s = (struct String *)Pop( TE_STRING , 1 ) ;
  111.     CurrentString = s ;
  112.       }
  113.       else if ( InputMode & IM_EXMACRO )
  114.       {
  115.     m = (struct Macro *)Pop( TE_MACRO , 1 ) ;
  116.     CurrentMacro = m ;
  117.       }
  118.       goto _restart ;
  119.     }
  120.     c = s->s_val[s->s_pos] ;
  121.     s->s_pos++ ;
  122.     return( c ) ;
  123.   }
  124.  
  125.   f = CurrentInputFile ;
  126.  
  127.   /* si fin du tampon, lit LGMAXBUF octets dans le fichier */
  128.  
  129.   if ( f->if_NextC >= f->if_BufLen )
  130.   {
  131.     if ( f->if_Flag & IFF_LOADED ) return( EOF ) ;
  132.  
  133.     f->if_NextC = 0 ;
  134.     f->if_BufLen = read( f->if_Desc , f->if_Buf , LGMAXBUF ) ;
  135.     if (f->if_BufLen == -1 ) Fatal( ERR_READ ) ;
  136.     if (! f->if_BufLen) return( EOF ) ;
  137.   }
  138.  
  139.   /* lecture normale */
  140.  
  141.   c = f->if_Buf[f->if_NextC] ;
  142.   f->if_NextC++ ;
  143.   return( c ) ;
  144. }
  145.  
  146. /***********************************************************************/
  147.  
  148. long PutChar( c )
  149. unsigned char c ;
  150.  
  151. {
  152.   register struct MLine *l ;
  153.  
  154.   if ( InputMode & IM_RDMACRO )
  155.   {
  156.     if ( c == '\n' ) CurrentMacro->m_NextL = NULL ;
  157.  
  158.     l = CurrentMacro->m_NextL ;
  159.     if ( ! l )
  160.     {
  161.       l = (struct MLine *)myalloc( sizeof(struct MLine) , 1 ) ;
  162.       InsereQueue( &(CurrentMacro->m_Def) , l ) ;
  163.       CurrentMacro->m_NextL = l ;
  164.       CurrentMacro->m_NextC = 0 ;
  165.     }
  166.  
  167.     if ( c == '\n' ) return( 0 ) ;
  168.  
  169.     if ( l->ml_Len >= LGMAXSTR ) Fatal( ERR_OVERFLOW ) ;
  170.     l->ml_Text[l->ml_Len] = c ;
  171.     l->ml_Len++ ;
  172.     return( 0 ) ;
  173.   }
  174.  
  175.   if ( InputMode & IM_RDARGS )
  176.   {
  177.     if ( ArgLen >= LGMAXSTR ) Fatal( ERR_OVERFLOW ) ;
  178.     Arg[ArgLen] = c ;
  179.     ArgLen++ ;
  180.     return( 0 ) ;
  181.   }
  182.  
  183.   if ( OutputLen >= LGMAXBUF ) Fatal( ERR_OVERFLOW ) ;
  184.   OutputBuf[OutputLen] = c ;
  185.   OutputLen++ ;
  186.   return( OutputLen > TmpLineLen ) ;
  187. }
  188.  
  189. /***********************************************************************/
  190.  
  191. struct InputFile *NewFile( name )
  192. char *name ;
  193.  
  194. {
  195.   long lg ;
  196.   struct InputFile *f ;
  197.  
  198.   f = (struct InputFile *)myalloc( sizeof(struct InputFile) , 1 ) ;
  199.  
  200.   if (! strcmp( name , "-" ))
  201.   {
  202.     f->if_Desc = 0 ;
  203.     f->if_Name = "stdin" ;
  204.     BSET( f->if_Flag , IFF_STDIN ) ;
  205.   }
  206.   else
  207.   {
  208.     f->if_Name = (char *)myalloc( strlen(name)+1 ) ;
  209.     strcpy( f->if_Name , name ) ;
  210.     f->if_Desc = open( name , O_RDONLY ) ;
  211.   }
  212.  
  213.   if ( f->if_Desc == -1 ) Fatal( ERR_OPEN ) ;
  214.  
  215.   /* charge le fichier en memoire */
  216.  
  217.   if ( Flg & F_LOADED )
  218.   {
  219.     lg = lseek( f->if_Desc , 0 , 2 ) ;
  220.     if ( lg < 1 ) Fatal( ERR_EMPTY ) ;
  221.     if ( lg == -1 ) Fatal( ERR_SEEK ) ;
  222.     if ( lseek( f->if_Desc , 0 , 0 ) == -1 ) Fatal( ERR_SEEK ) ;
  223.     f->if_Buf = (unsigned char *)myalloc( lg , 0 ) ;
  224.     f->if_BufLen = lg ;
  225.     if ( read( f->if_Desc , f->if_Buf , lg ) != lg ) Fatal( ERR_READ ) ;
  226.     close( f->if_Desc ) ;
  227.     BSET( f->if_Flag , IFF_LOADED ) ;
  228.   }
  229.   else f->if_Buf = (unsigned char *)myalloc( LGMAXBUF , 0 ) ;
  230.  
  231.   f->if_Line = 1 ;
  232.   return( f ) ;
  233. }
  234.  
  235. /**************************************************************************/
  236.  
  237. void CloseFile( f )
  238. struct InputFile *f ;
  239.  
  240. {
  241.   if ( (f != NULL) &&
  242.        (f != (struct InputFile *)-1) &&
  243.        (f->if_Desc > 0) )
  244.   {
  245.     if ( f->if_Buf ) free( f->if_Buf ) ;
  246.     if ( f->if_Flag & (IFF_STDIN|IFF_LOADED) ) return ;
  247.  
  248.     close( f->if_Desc ) ;
  249.     free( f->if_Name ) ;
  250.     f->if_Desc = -1 ;
  251.   }
  252. }
  253.  
  254.