home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / utilities / print / aroff / sources / fonctions.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-28  |  10.3 KB  |  487 lines

  1. /*
  2.  * Diverses fonctions de base
  3.  * (c)1991 par Denis GOUNELLE
  4.  */
  5.  
  6. #include "aroff.h"
  7. #include "pile.h"
  8.  
  9. static unsigned char tmp[1+LGMAXSTR<<1], aux[LGMAXSTR+1] ;
  10.  
  11. extern struct Contexte *LastContext ;
  12. extern struct String *CurrentString ;
  13. extern unsigned char Arg[], OutputBuf[] ;
  14. extern char *ARoff_Version, *ARoff_Usage ;
  15. extern struct InputFile *CurrentInputFile ;
  16. extern struct Macro *CurrentMacro, *FindVar() ;
  17. extern struct TeteListe TReg, TStr, TMac, TTrp ;
  18. extern long ArgLen, OutputLen, TotalIndent, LineSpacing, LineNumber,
  19.         NumInterv, NumSpace, NumIndent, PageOffset, Flg, TmpNoNum,
  20.         InputMode, TmpIndent, OutputLine, PageLen, PageNumber,
  21.         EmptyToWrite, NewPageNumber ;
  22.  
  23. extern void do_ab(), do_ad(), do_am(), do_as(), do_bp(), do_br(), do_ce(),
  24.         do_de(), do_ds(), do_ex(), do_fi(), do_fs(), do_fl(), do_in(),
  25.         do_ll(), do_ls(), do_lt(), do_na(), do_ne(), do_nf(), do_nm(),
  26.         do_nn(), do_nr(), do_pl(), do_pm(), do_pn(), do_po(), do_rm(),
  27.         do_rr(), do_rs(), do_rt(), do_so(), do_sp(), do_ta(), do_tc(),
  28.         do_ti(), do_tl(), do_tm(), do_tr(), do_wh() ;
  29.  
  30. /**************************************************************************/
  31.  
  32. void Termine()
  33. {
  34.   struct Macro *m ;
  35.  
  36.   CloseFile( CurrentInputFile ) ;
  37.  
  38.   VideListe( &TReg ) ;
  39.   VideListe( &TStr ) ;
  40.   VideListe( &TTrp ) ;
  41.  
  42.   for ( m = (struct Macro *)TMac.tl_Premier ;
  43.     m ;
  44.     m = (struct Macro *)m->m_Node.el_Suivant ) VideListe( &(m->m_Def) ) ;
  45.   VideListe( &TMac ) ;
  46.  
  47.   exit( 0 ) ;
  48. }
  49.  
  50. /**************************************************************************/
  51.  
  52. void Abort( indic )
  53. long indic ;
  54.  
  55. {
  56.   if ( (indic) && (OutputLen) )
  57.   {
  58.     OutputBuf[OutputLen] = '\0' ;
  59.     OutputLen = '\0' ;
  60.     LigneSuiv( OutputBuf ) ;
  61.   }
  62.   Termine() ;
  63. }
  64.  
  65. /**************************************************************************/
  66.  
  67. void FatalError( code , line , file )
  68. long code, line ;
  69. char *file ;
  70.  
  71. {
  72.   if ( code == ERR_ARGS )
  73.     fprintf( stderr , "%s\n%s\n" , ARoff_Version , ARoff_Usage ) ;
  74.   else
  75.   {
  76.     fprintf( stderr , "Error %d in file %s at line %d\n" , code , file , line ) ;
  77.     if ( CurrentInputFile )
  78.       fprintf( stderr , "current file: %s, line: %d\n" ,
  79.            CurrentInputFile->if_Name , CurrentInputFile->if_Line ) ;
  80.   }
  81.  
  82.   Abort( 0 ) ;
  83. }
  84.  
  85. /**************************************************************************/
  86.  
  87. char *myalloc( len , indic )
  88. long len, indic ;
  89.  
  90. {
  91.   char *p ;
  92.  
  93.   p = (char *)malloc( len ) ;
  94.   if ( ! p ) Fatal( ERR_MEMORY ) ;
  95.   if ( indic ) bzero( p , len ) ;
  96.   return( p ) ;
  97. }
  98.  
  99. /**************************************************************************/
  100.  
  101. void SetMacArgs()
  102. {
  103.   register long k ;
  104.   register struct Macro *m ;
  105.   register unsigned char *p, c ;
  106.  
  107.   m = CurrentMacro ;
  108.   m->m_NbArg = k = 0 ;
  109.  
  110.   while ( Arg[k] )
  111.   {
  112.     p = m->m_Arg[m->m_NbArg] ;
  113.     while ( (c = Arg[k]) && (! isspace( c )) )
  114.     {
  115.       if ( c == SC_FIXSPC ) c = ' ' ;
  116.       *p = c ;
  117.       k++ ;
  118.       p++ ;
  119.     }
  120.     *p = '\0' ;
  121.     while ( isspace( Arg[k] ) ) k++ ;
  122.     m->m_NbArg++ ;
  123.   }
  124.  
  125.   SetReg( ".$" , m->m_NbArg , 0 ) ;
  126. }
  127.  
  128. /**************************************************************************/
  129.  
  130. struct Command CmdTable[] =
  131. {
  132.   "ab",do_ab,
  133.   "ad",do_ad,
  134.   "am",do_am,
  135.   "as",do_as,
  136.   "bp",do_bp,
  137.   "br",do_br,
  138.   "ce",do_ce,
  139.   "de",do_de,
  140.   "ds",do_ds,
  141.   "ex",do_ex,
  142.   "fi",do_fi,
  143. /*"fl",do_fl, inaccessible pour l'utilisateur */
  144.   "fs",do_fs,
  145.   "in",do_in,
  146.   "ll",do_ll,
  147.   "ls",do_ls,
  148.   "lt",do_lt,
  149.   "na",do_na,
  150.   "ne",do_ne,
  151.   "nf",do_nf,
  152.   "nm",do_nm,
  153.   "nn",do_nn,
  154.   "nr",do_nr,
  155.   "pl",do_pl,
  156.   "pm",do_pm,
  157.   "pn",do_pn,
  158.   "po",do_po,
  159.   "rm",do_rm,
  160.   "rr",do_rr,
  161.   "rs",do_rs,
  162.   "rt",do_rt,
  163.   "so",do_so,
  164.   "sp",do_sp,
  165.   "ta",do_ta,
  166.   "tc",do_tc,
  167.   "ti",do_ti,
  168.   "tl",do_tl,
  169.   "tm",do_tm,
  170.   "tr",do_tr,
  171.   "wh",do_wh,
  172.   ""  ,NULL,
  173. } ;
  174.  
  175. void ExecCmd( cmd )
  176. char *cmd ;
  177.  
  178. {
  179.   register struct Macro *m ;
  180.   register struct Command *p ;
  181.  
  182.   Arg[ArgLen] = '\0' ;
  183.  
  184.   /* est-ce une requete ? */
  185.  
  186.   for ( p = CmdTable ; p->c_func ; p++ )
  187.     if (! strcmp( p->c_name , cmd ))
  188.     {
  189.       (*p->c_func)() ;
  190.       goto _end ;
  191.     }
  192.  
  193.   /* est-ce une macro ? */
  194.  
  195.   m = FindVar( &TMac , cmd ) ;
  196.   if ( m )
  197.   {
  198.     if ( InputMode & IM_EXMACRO ) Push( TE_MACRO , CurrentMacro ) ;
  199.     if ( InputMode & IM_STRING ) Push( TE_STRING , CurrentString ) ;
  200.     Push( TE_INMODE , InputMode ) ;
  201.     BCLR( InputMode , (IM_STRING|IM_EXMACRO) ) ;
  202.  
  203.     m->m_NextC = 0 ;
  204.     m->m_NextL = (struct MLine *)m->m_Def.tl_Premier ;
  205.     CurrentMacro = m ;
  206.     SetMacArgs() ;
  207.     BSET( InputMode , IM_EXMACRO ) ;
  208.   }
  209.  
  210. _end:
  211.  
  212.   BCLR( Flg , F_NOBRK ) ;
  213.   BSET( Flg , F_MACREQ|F_WASNL ) ;
  214. }
  215.  
  216. /**************************************************************************/
  217.  
  218. long GetName( dst )
  219. char *dst ;
  220.  
  221. {
  222.   long c ;
  223.  
  224.   if ( (c = GetChar()) == EOF ) return( 0 ) ;
  225.   dst[0] = c ;
  226.   if ( (c = GetChar()) == EOF ) return( 0 ) ;
  227.   dst[1] = c ;
  228.   dst[2] = '\0' ;
  229.   return( 1 ) ;
  230. }
  231.  
  232. /**************************************************************************/
  233.  
  234. long myatoi()
  235.  
  236. {
  237.   long k ;
  238.   unsigned char *p ;
  239.  
  240.   k = strtol( Arg , &p , 10 ) ;
  241.   if ( (*p != '\0') && (! isspace( *p )) ) Fatal( ERR_SYNTAX ) ;
  242.   return( k ) ;
  243. }
  244.  
  245. /**************************************************************************/
  246.  
  247. char *GetArg( pstr , pval )
  248. unsigned char *pstr ;
  249. long *pval ;
  250.  
  251. {
  252.   while ( isspace( *pstr ) ) pstr++ ;
  253.   if ( isdigit( *pstr ) ) *pval = atoi( pstr ) ;
  254.   while ( (*pstr != '\0') && (! isspace( *pstr)) ) pstr++ ;
  255.   return( pstr ) ;
  256. }
  257.  
  258. /**************************************************************************/
  259.  
  260. long ChangeValue( val )
  261. long *val ;
  262.  
  263. {
  264.   register char c ;
  265.   register long k ;
  266.  
  267.   k = 0 ;
  268.   c = '\0' ;
  269.   if ( (Arg[k] == '+') || (Arg[k] == '-') )
  270.   {
  271.     c = Arg[k] ;
  272.     k++ ;
  273.   }
  274.  
  275.   if (! isdigit( Arg[k] )) return( 0 ) ;
  276.   k = atoi( &Arg[k] ) ;
  277.  
  278.   switch ( c )
  279.   {
  280.     case '+' : *val += k ; break ;
  281.     case '-' : *val -= k ; break ;
  282.     default  : *val  = k ; break ;
  283.   }
  284.  
  285.   return( 1 ) ;
  286. }
  287.  
  288. /**************************************************************************/
  289.  
  290. void ChangePageNumber( number )
  291. long number ;
  292.  
  293. {
  294.    PageNumber = number ;
  295.    SetReg( "pn" , PageNumber , 0 ) ;
  296.    NewPageNumber = number + 1 ;
  297. }
  298.  
  299. /**************************************************************************/
  300.  
  301. long LigneSuiv( str )
  302. char *str ;
  303.  
  304. {
  305.   register long k ;
  306.   register char *p ;
  307.   register struct Macro *m ;
  308.  
  309.   m = CurrentMacro ;
  310.   k = strlen( str ) ;
  311.  
  312.   if ( TestTrp( OutputLine ) )
  313.   {
  314.     BSET( Flg , F_TRAP ) ;
  315.     BSET( CurrentMacro->m_Flag , MF_TRAP ) ;
  316.     p = myalloc( k , 0 ) ;
  317.     strcpy( p , str ) ;
  318.     Push( TE_OUTLINE , p ) ;
  319.     Push( TE_TOWRITE , EmptyToWrite ) ;
  320.     if ( m ) BSET( m->m_Flag , MF_WAIT ) ;
  321.     EmptyToWrite = 0 ;
  322.     return ;
  323.   }
  324.  
  325.   if ( k ) do_fl( str ) ;
  326.   OutputLine++ ;
  327.   if ( OutputLine > PageLen )
  328.   {
  329.     OutputLine = 1 ;
  330.     ChangePageNumber( NewPageNumber ) ;
  331.   }
  332.  
  333. }
  334.  
  335. /**************************************************************************/
  336.  
  337. void WriteLine()
  338.  
  339. {
  340.   register long k ;
  341.   register unsigned char *p ;
  342.  
  343.   /* ajoute le decalage de page */
  344.  
  345.   p = tmp ;
  346.   for ( k = 0 ; k < PageOffset ; k++ , p++ ) *p = ' ' ;
  347.  
  348.   /* effectue la numerotation */
  349.  
  350.   for ( k = 0 ; k < OutputLen ; k++ ) if (! isspace( OutputBuf[k] )) break ;
  351.   if ( (Flg & F_NUMBER) && (k < OutputLen) && (! TmpNoNum) )
  352.   {
  353.     for ( k = 0 ; k < NumIndent ; k++ , p++ ) *p = ' ' ;
  354.     sprintf( aux , "%d" , LineNumber ) ;
  355.     LineNumber++ ;
  356.     SetReg( "ol" , LineNumber , 0 ) ;
  357.     if ( (NumInterv != 1) && ((LineNumber % NumInterv) != 1) )
  358.       for ( k = 0 ; aux[k] != '\0' ; k++ ) aux[k] = ' ' ;
  359.     strcpy( p , aux ) ;
  360.     p += strlen( aux ) ;
  361.     for ( k = 0 ; k < NumSpace ; k++ , p++ ) *p = ' ' ;
  362.   }
  363.  
  364.   /* ajoute l'indentation */
  365.  
  366.   for ( k = 0 ; k < TotalIndent ; k++ , p++ ) *p = ' ' ;
  367.   if ( Flg & F_NOTI ) TmpIndent = 0 ;
  368.  
  369.   /* ecrit la ligne finale */
  370.  
  371.   bcopy( OutputBuf , p , OutputLen ) ;
  372.   p += OutputLen ;
  373.   OutputLen = 0 ;
  374.   *p = '\n' ;
  375.   p[1] = '\0' ;
  376.  
  377.   EmptyToWrite += LineSpacing - 1 ;
  378.   LigneSuiv( tmp ) ;
  379. }
  380.  
  381. /**************************************************************************/
  382.  
  383. void SauveContexte( indic )
  384. long indic ;
  385.  
  386. {
  387.   register long k ;
  388.   register struct Macro *m ;
  389.   register struct Contexte *p ;
  390.  
  391.   p = (struct Contexte *)myalloc( sizeof(struct Contexte) , 0 ) ;
  392.  
  393.   if ( InputMode & IM_EXMACRO )
  394.   {
  395.     m = CurrentMacro ;
  396.     k = (long)&(m->m_Arg[m->m_NbArg][0]) - (long)m ;
  397.     bcopy( m , &(p->src.c_macro) , k ) ;
  398.     p->c_ptr = (long)m ;
  399.     p->c_len = k ;
  400.   }
  401.   else if ( InputMode & IM_STRING )
  402.   {
  403.     k = 9 + strlen( CurrentString->s_val ) ;
  404.     bcopy( CurrentString , &(p->src.c_string) , k ) ;
  405.     p->c_ptr = (long)CurrentString ;
  406.     p->c_len = k ;
  407.   }
  408.   else
  409.   {
  410.     bcopy( CurrentInputFile , &(p->src.c_file) , sizeof(struct InputFile) ) ;
  411.     p->c_seek = lseek( p->src.c_file.if_Desc , 0 , 1 ) ;
  412.     p->c_ptr = (long)CurrentInputFile ;
  413.   }
  414.  
  415.   bcopy( OutputBuf , p->c_obuf , OutputLen ) ;
  416.   p->c_olen = OutputLen ;
  417.   p->c_imod = InputMode ;
  418.   p->c_flag = Flg ;
  419.  
  420.   if ( indic )
  421.   {
  422.     if ( LastContext ) free( LastContext ) ;
  423.     LastContext = p ;
  424.   }
  425.   else Push( TE_CONTEXT , p ) ;
  426. }
  427.  
  428. /**************************************************************************/
  429.  
  430. void RestoreContexte( indic )
  431. long indic ;
  432.  
  433. {
  434.   register struct Contexte *p ;
  435.  
  436.   if ( indic ) p = LastContext ;
  437.   else p = (struct Contexte *) Pop( TE_CONTEXT , 1 ) ;
  438.  
  439.   Flg = p->c_flag ;
  440.   InputMode = p->c_imod ;
  441.   if ( InputMode & IM_EXMACRO )
  442.   {
  443.     CurrentMacro = (struct Macro *)p->c_ptr ;
  444.     bcopy( &(p->src.c_macro) , CurrentMacro , p->c_len ) ;
  445.   }
  446.   else if ( InputMode & IM_STRING )
  447.   {
  448.     CurrentString = (struct String *)p->c_ptr ;
  449.     bcopy( &(p->src.c_string) , CurrentString , p->c_len ) ;
  450.   }
  451.   else
  452.   {
  453.     CurrentInputFile = (struct InputFile *)p->c_ptr ;
  454.     bcopy( &(p->src.c_file) , CurrentInputFile, sizeof(struct InputFile) ) ;
  455.     lseek( p->src.c_file.if_Desc , p->c_seek , 0 ) ;
  456.   }
  457.  
  458.   bcopy( p->c_obuf , OutputBuf , OutputLen ) ;
  459.   OutputLen = p->c_olen ;
  460.   free( p ) ;
  461.   if ( indic ) LastContext = NULL ;
  462. }
  463.  
  464. /**************************************************************************/
  465.  
  466. void NewString( str )
  467. char *str ;
  468.  
  469. {
  470.   register struct String *s ;
  471.  
  472.   if ( ! *str ) return ;
  473.  
  474.   if ( InputMode & IM_STRING ) Push( TE_STRING , CurrentString ) ;
  475.   if ( InputMode & IM_EXMACRO ) Push( TE_MACRO , CurrentMacro ) ;
  476.   Push( TE_INMODE , InputMode ) ;
  477.   BCLR( InputMode , (IM_STRING|IM_EXMACRO) ) ;
  478.  
  479.   s = (struct String *)myalloc( sizeof(struct String) , 0 ) ;
  480.   strcpy( s->s_val , str ) ;
  481.   s->s_len = strlen( str ) ;
  482.   s->s_pos = 0 ;
  483.   CurrentString = s ;
  484.   BSET( InputMode , IM_STRING ) ;
  485. }
  486.  
  487.