home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / utilities / print / aroff / sources / fichiers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-28  |  4.6 KB  |  227 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 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.   register long c ;
  48.   struct InputFile *f ;
  49.   register struct Macro *m ;
  50.   register struct String *s ;
  51.  
  52.   if ( InputMode & IM_EXMACRO )
  53.   {
  54.  
  55. _restart:
  56.  
  57.     for (;;)
  58.     {
  59.       c = GetFromMac() ;
  60.       if ( c != EOF ) return( c ) ;
  61.  
  62.       BCLR( Flg , F_TRAP ) ;
  63.       InputMode = Pop( TE_INMODE , 1 ) ;
  64.      
  65.       if ( InputMode & IM_STRING )
  66.       {
  67.         s = (struct String *)Pop( TE_STRING , 1 ) ;
  68.         CurrentString = s ;
  69.       }
  70.       if (! (InputMode & IM_EXMACRO)) break ;
  71.  
  72.       m = (struct Macro *)Pop( TE_MACRO , 1 ) ;
  73.  
  74.       CurrentMacro = m ;
  75.       if ( m->m_Flag & MF_TRAP ) BSET( Flg , F_TRAP ) ;
  76.       if ( m->m_Flag & MF_WAIT )
  77.       {
  78.     BCLR( m->m_Flag , MF_WAIT ) ;
  79.     p = (char *)Pop( TE_OUTLINE , 1 ) ;
  80.     c = Pop( TE_TOWRITE , 0 ) ;
  81.     if ( c != -1 ) EmptyToWrite = c ;
  82.     LigneSuiv( p ) ;
  83.     free( p ) ;
  84.       }
  85.     }
  86.  
  87.     while (! (Flg & F_TRAP))
  88.     {
  89.       p = (char *)Pop( TE_OUTLINE , 0 ) ;
  90.       if ( (! p) || (p == (char *)-1) ) break ;
  91.       c = Pop( TE_TOWRITE , 0 ) ;
  92.       if ( c != -1 ) EmptyToWrite = c ;
  93.       LigneSuiv( p ) ;
  94.       free( p ) ;
  95.     }
  96.  
  97.     if ( Flg & F_TRAP ) goto _restart ;
  98.     BCLR( InputMode , IM_EXMACRO ) ;
  99.     CurrentMacro = NULL ;
  100.   }
  101.  
  102.   if ( InputMode & IM_STRING ) 
  103.   {
  104.     s = CurrentString ;
  105.     c = s->s_val[s->s_pos] ;
  106.     s->s_pos++ ;
  107.     if ( s->s_pos >= s->s_len )
  108.     { 
  109.       InputMode = Pop( TE_INMODE , 1 ) ;
  110.       if ( InputMode & IM_STRING ) 
  111.       {
  112.         s = (struct String *)Pop( TE_STRING , 1 ) ;
  113.         CurrentString = s ;
  114.       }
  115.       else if ( InputMode & IM_EXMACRO )
  116.       {
  117.         m = (struct Macro *)Pop( TE_MACRO , 1 ) ;
  118.         CurrentMacro = m ;
  119.       }
  120.     }
  121.     return( c ) ;
  122.   }
  123.  
  124.   f = CurrentInputFile ;
  125.  
  126.   /* si fin du tampon, lit LGMAXBUF octets dans le fichier */
  127.   if ( f->if_NextC >= f->if_BufLen )
  128.   {
  129.     f->if_NextC = 0 ;
  130.     f->if_BufLen = read( f->if_Desc , f->if_Buf , LGMAXBUF ) ;
  131.     if (! f->if_BufLen) return( EOF ) ;
  132.   }
  133.  
  134.   /* lecture normale */
  135.   c = f->if_Buf[f->if_NextC] ;
  136.   f->if_NextC++ ;
  137.   return( c ) ;
  138. }
  139.  
  140. /***********************************************************************/
  141.  
  142. long PutChar( c )
  143. char c ;
  144.  
  145. {
  146.   register struct MLine *l ;
  147.  
  148.   if ( InputMode & IM_RDMACRO )
  149.   {
  150.     if ( c == '\n' ) CurrentMacro->m_NextL = NULL ;
  151.  
  152.     l = CurrentMacro->m_NextL ;
  153.     if ( ! l )
  154.     {
  155.       l = (struct MLine *)myalloc( sizeof(struct MLine) , 1 ) ;
  156.       InsereQueue( &(CurrentMacro->m_Def) , l ) ;
  157.       CurrentMacro->m_NextL = l ;
  158.       CurrentMacro->m_NextC = 0 ;
  159.     }
  160.  
  161.     if ( c == '\n' ) return( 0 ) ;
  162.  
  163.     if ( l->ml_Len >= LGMAXSTR ) Fatal( ERR_OVERFLOW ) ;
  164.     l->ml_Text[l->ml_Len] = c ;
  165.     l->ml_Len++ ;
  166.     return( 0 ) ;
  167.   }
  168.  
  169.   if ( InputMode & IM_RDARGS )
  170.   {
  171.     if ( ArgLen >= LGMAXSTR ) Fatal( ERR_OVERFLOW ) ;
  172.     Arg[ArgLen] = c ;
  173.     ArgLen++ ;
  174.     return( 0 ) ;
  175.   }
  176.  
  177.   if ( OutputLen >= LGMAXBUF ) Fatal( ERR_OVERFLOW ) ;
  178.   OutputBuf[OutputLen] = c ;
  179.   OutputLen++ ;
  180.   return( OutputLen > TmpLineLen ) ;
  181. }
  182.  
  183. /***********************************************************************/
  184.  
  185. struct InputFile *NewFile( name )
  186. char *name ;
  187.  
  188. {
  189.   struct InputFile *f ;
  190.  
  191.   f = (struct InputFile *)myalloc( sizeof(struct InputFile) , 1 ) ;
  192.  
  193.   if (! strcmp( name , "-" ))
  194.   {
  195.     strcpy( f->if_Name , "stdin" ) ;
  196.     f->if_Desc = 0 ;
  197.     BSET( f->if_Flag , IFF_STDIN ) ;
  198.   }
  199.   else
  200.   {
  201.     strcpy( f->if_Name , name ) ;
  202.     f->if_Desc = open( name , O_RDONLY ) ;
  203.   }
  204.  
  205.   if ( f->if_Desc == -1 ) Fatal( ERR_OPEN ) ;
  206.   f->if_Line = 1 ;
  207.   return( f ) ;
  208. }
  209.  
  210. /**************************************************************************/
  211.  
  212. void CloseFile( f )
  213. struct InputFile *f ;
  214.  
  215. {
  216.   if ( (f != NULL) &&
  217.        (f != (struct InputFile *)-1) &&
  218.        (f->if_Desc > 0) &&
  219.        (! (f->if_Flag & IFF_STDIN)) )
  220.   {
  221.     close( f->if_Desc ) ;
  222.     f->if_Desc = -1 ;
  223.   }
  224.  
  225. }
  226.  
  227.