home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 19 Printer / 19-Printer.zip / cpostsrc.zip / CPOSTP2.C < prev    next >
C/C++ Source or Header  |  1993-04-12  |  12KB  |  337 lines

  1. /*------------------------------------------------------------------
  2.  * cpostp2.c : pass 2 of cPost
  3.  *------------------------------------------------------------------
  4.  * 12-02-91 originally by Patrick J. Mueller
  5.  * 12-03-92 converted from cBook to cPost
  6.  *------------------------------------------------------------------*/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. #include "ctok.h"
  13. #include "cpost.h"
  14.  
  15. /*------------------------------------------------------------------
  16.  * static buffer
  17.  *------------------------------------------------------------------*/
  18. #define BUFFER_LEN  8000
  19. #define BRACKET_LEN  500
  20.  
  21. static unsigned char Buffer    [BUFFER_LEN];
  22. static unsigned char FuncBuff  [MAX_IDENT_LEN+1];
  23. static unsigned char CurrFunc  [MAX_IDENT_LEN+1];
  24. static unsigned char BrackInfo [BRACKET_LEN];
  25.  
  26. /*------------------------------------------------------------------
  27.  * global variables
  28.  *------------------------------------------------------------------*/
  29. static FILE           *oFile;
  30. static PageEject      *pageJ;
  31. static char           *Xchars[256];
  32. static unsigned char   currFont;
  33. static unsigned long   lineNo;
  34. static int             col;
  35.  
  36. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  37. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  38.  
  39. /*------------------------------------------------------------------
  40.  * write out the buffer, translating as we go
  41.  *------------------------------------------------------------------*/
  42. static void WriteOut(
  43.    unsigned char    *buffer,
  44.    int               len
  45.    )
  46.    {
  47.    int         i;
  48.    char       *xlate;
  49.  
  50.    /*---------------------------------------------------------------
  51.     * print each character in buffer
  52.     *---------------------------------------------------------------*/
  53.    for (i=0; i<len; i++)
  54.       {
  55.       xlate = Xchars[buffer[i]];
  56.  
  57.       /*------------------------------------------------------------
  58.        * check for end of line
  59.        *------------------------------------------------------------*/
  60.       if ('\n' == buffer[i])
  61.          {
  62.          lineNo++;
  63.  
  64.          /*---------------------------------------------------------
  65.           * print end of line info
  66.           *---------------------------------------------------------*/
  67.          fprintf(oFile,")] showLine %s\n",BrackInfo);
  68.          BrackInfo[0] = 0;
  69.  
  70.          /*---------------------------------------------------------
  71.           * see if there is a new function definition
  72.           *---------------------------------------------------------*/
  73.          if (*CurrFunc)
  74.             {
  75.             fprintf(oFile,"/currFunc (%s) def\n",CurrFunc);
  76.             *CurrFunc = 0;
  77.             }
  78.  
  79.          /*---------------------------------------------------------
  80.           * write page eject if we need to
  81.           *---------------------------------------------------------*/
  82.          if (info.oBreak && pageJ && (pageJ->lineNo == lineNo))
  83.             {
  84.             if (pageJ->next)
  85.                {
  86.                int lines;
  87.  
  88.                lines = pageJ->next->lineNo - pageJ->lineNo;
  89.                fprintf(oFile,"%d linesFit\n",lines);
  90.  
  91.                pageJ = pageJ->next;
  92.                }
  93.             }
  94.  
  95.          /*---------------------------------------------------------
  96.           * print start of line info
  97.           *---------------------------------------------------------*/
  98.          fprintf(oFile,"[%c (",currFont);
  99.  
  100.          col = 0;
  101.          }
  102.  
  103.       /*------------------------------------------------------------
  104.        * plain old character
  105.        *------------------------------------------------------------*/
  106.       else if (NULL == xlate)
  107.          {
  108.          col++;
  109.  
  110. /*       fwrite(&(buffer[i]),1,1,oFile); */
  111.          fputc(buffer[i],oFile);
  112.          }
  113.  
  114.       /*------------------------------------------------------------
  115.        * character to translate
  116.        *------------------------------------------------------------*/
  117.       else
  118.          {
  119.          char *bChar;
  120.          char  nBuff[20];
  121.  
  122.          col++;
  123.  
  124.          /*---------------------------------------------------------
  125.           * check for bracket character
  126.           *---------------------------------------------------------*/
  127.          bChar = NULL;
  128.          switch(buffer[i])
  129.             {
  130.             case '\x01' : bChar = "u"; break;
  131.             case '\x02' : bChar = "m"; break;
  132.             case '\x03' : bChar = "l"; break;
  133.             }
  134.  
  135.          if (bChar && (strlen(BrackInfo) < BRACKET_LEN - 30))
  136.             {
  137.             sprintf(nBuff,"%d",col);
  138.  
  139.  
  140.             strcat(BrackInfo," ");
  141.             strcat(BrackInfo,nBuff);
  142.             strcat(BrackInfo," ");
  143.             strcat(BrackInfo,bChar);
  144.             strcat(BrackInfo,"B");
  145.             }
  146.  
  147.          /*---------------------------------------------------------
  148.           * print translation
  149.           *---------------------------------------------------------*/
  150. /*       fwrite(xlate,1,strlen(xlate),oFile); */
  151.          fputs(xlate,oFile);
  152.          }
  153.       }
  154.    }
  155.  
  156. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  157. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  158.  
  159. /*------------------------------------------------------------------
  160.  * pass 2 - add formatting tags
  161.  *------------------------------------------------------------------*/
  162. int Pass2(
  163.    File     *file,
  164.    Info     *info
  165.    )
  166.    {
  167.    Tok           *next;
  168.    unsigned long  offs;
  169.    unsigned long  len;
  170.    FILE          *iFile;
  171.    int            i;
  172.  
  173.    /*---------------------------------------------------------------
  174.     * initialize some stuff
  175.     *---------------------------------------------------------------*/
  176.    oFile = info->oFile;
  177.  
  178.    for (i=0; i<256; i++)
  179.       {
  180.       Xchars[i] = NULL;
  181.       }
  182.  
  183.    Xchars['(']  = "\\(";    /* ( -> \( */
  184.    Xchars[')']  = "\\)";    /* ) -> \) */
  185.    Xchars['\\'] = "\\\\";   /* \ -> \\ */
  186.  
  187.    Xchars['\x01'] = " ";
  188.    Xchars['\x02'] = " ";
  189.    Xchars['\x03'] = " ";
  190.  
  191.    /*---------------------------------------------------------------
  192.     * print status and read file
  193.     *---------------------------------------------------------------*/
  194.    fprintf(stderr,"   Reading file %s\n",file->name);
  195.    cParse(file,info);
  196.  
  197.    /*---------------------------------------------------------------
  198.     * write header
  199.     *---------------------------------------------------------------*/
  200.    fprintf(info->oFile,"\n%%----------------------------------------------\n");
  201.  
  202.    if (file->date && file->time && *file->date && *file->time)
  203.       fprintf(info->oFile,"(%s) (%s   %s) startFile\n",file->name,
  204.                           file->date,file->time);
  205.    else
  206.       fprintf(info->oFile,"(%s) () startFile\n",file->name);
  207.  
  208.    /*---------------------------------------------------------------
  209.     * first line processing
  210.     *---------------------------------------------------------------*/
  211.    CurrFunc[0]  = 0;
  212.    BrackInfo[0] = 0;
  213.    fprintf(oFile,"[n (");
  214.  
  215.    /*---------------------------------------------------------------
  216.     * open the input file
  217.     *---------------------------------------------------------------*/
  218.    if (file->tempName)
  219.       iFile = fopen(file->tempName,"r");
  220.    else
  221.       iFile = fopen(file->pathName,"r");
  222.  
  223.    if (!iFile)
  224.       cPostError(1,"error opening file for reading");
  225.  
  226.    /*---------------------------------------------------------------
  227.     * initialize token processing
  228.     *---------------------------------------------------------------*/
  229.    next     = file->tokList;
  230.    pageJ    = file->breakList;
  231.    offs     = 0;
  232.    lineNo   = 0;
  233.    col      = 0;
  234.    currFont = 'n';
  235.  
  236.    /*---------------------------------------------------------------
  237.     * loop through tokens
  238.     *---------------------------------------------------------------*/
  239.    while (next)
  240.       {
  241.       /*---------------------------------------------------------
  242.        * read boring stuff up to first token
  243.        *---------------------------------------------------------*/
  244.       if (next->tok.offs != offs)
  245.          {
  246.          /*---------------------------------------------------------
  247.           * read and write it
  248.           *---------------------------------------------------------*/
  249.          len = next->tok.offs - offs;
  250.          while (len > BUFFER_LEN)
  251.             {
  252.             fread(Buffer,1,BUFFER_LEN,iFile);
  253.             WriteOut(Buffer,BUFFER_LEN);
  254.             len -= BUFFER_LEN;
  255.             }
  256.  
  257.          fread(Buffer,1,(int)len,iFile);
  258.          WriteOut(Buffer,(int)len);
  259.  
  260.          /*------------------------------------------------------
  261.           * update the pointer
  262.           *------------------------------------------------------*/
  263.          offs = next->tok.offs;
  264.          }
  265.  
  266.       /*---------------------------------------------------------
  267.        * write the token
  268.        *---------------------------------------------------------*/
  269.       switch (next->extType)
  270.          {
  271.          /*---------------------------------------------------------
  272.           * get it's font character
  273.           *---------------------------------------------------------*/
  274.          case TOKEN_RESER     : currFont = 'k'; break;
  275.          case TOKEN_PREPROC   : currFont = 'p'; break;
  276.          case TOKEN_COMMENT   : currFont = 'c'; break;
  277.          case TOKEN_IDENT     : currFont = 'i'; break;
  278.          case TOKEN_FUNDEF    : currFont = 'd'; break;
  279.          case TOKEN_FUNPRO    : currFont = 'f'; break;
  280.          case TOKEN_FUNUSE    : currFont = 'f'; break;
  281.          default :              currFont = 'n'; break;
  282.          }
  283.  
  284.       /*------------------------------------------------------------
  285.        * copy function name into buffer for function definitions
  286.        *------------------------------------------------------------*/
  287.       if (TOKEN_FUNDEF == next->extType)
  288.          strcpy(CurrFunc,next->str);
  289.  
  290.       /*------------------------------------------------------
  291.        * read and write token
  292.        *------------------------------------------------------*/
  293.       if (currFont != 'n')
  294.          fprintf(oFile,") %c (",currFont);
  295.  
  296.       len = next->tok.len;
  297.       while (len > BUFFER_LEN)
  298.          {
  299.          fread(Buffer,1,(int)BUFFER_LEN,iFile);
  300.          WriteOut(Buffer,(int)BUFFER_LEN);
  301.          len -= BUFFER_LEN;
  302.          }
  303.  
  304.       fread(Buffer,1,(int)len,iFile);
  305.       WriteOut(Buffer,(int)len);
  306.  
  307.       if (currFont != 'n')
  308.          fprintf(info->oFile,") n (");
  309.  
  310.       currFont = 'n';
  311.  
  312.       /*------------------------------------------------------------
  313.        * prepare for next token
  314.        *------------------------------------------------------------*/
  315.       offs += next->tok.len;
  316.       next = next->next;
  317.       }
  318.  
  319.    /*---------------------------------------------------------------
  320.     * last line processing
  321.     *---------------------------------------------------------------*/
  322.    fprintf(oFile,")] showLine %s\nendFile",BrackInfo);
  323.  
  324.    /*---------------------------------------------------------------
  325.     * close file
  326.     *---------------------------------------------------------------*/
  327.    fclose(iFile);
  328.  
  329.    /*---------------------------------------------------------------
  330.     * free parser storage
  331.     *---------------------------------------------------------------*/
  332.    cParseDone(file,info);
  333.  
  334.    return 0;
  335.    }
  336.  
  337.