home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 5.ddi / EXAMPLES.ZIP / TASM2MSG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  17.2 KB  |  428 lines

  1. /*
  2.    EXAMPLE SOURCE CODE FOR TASM FILTER
  3.  
  4.    Tasm2Msg.C
  5.    Copyright (c) 1990, 1991 Borland International, Inc.
  6.    All rights reserved.
  7.  
  8.    Tasm2Msg - assembler output filter to the IDE message window.
  9.  
  10.    This filter accepts input through the standard input stream, converts
  11.    it and outputs it to the standard output stream.  The streams are linked
  12.    through pipes, such that the input stream is the output from the assembler
  13.    being invoked, and the output stream is connected to the message window
  14.    of the IDE, ie.
  15.  
  16.           tasm fname | tasm2msg | IDE message window
  17.  
  18.    Input can come from TASM, MASM 5.1 or OPTASM.  The type of assembler is
  19.    determined from analysing the lines of output from the assembler.
  20.  
  21.    Compile using the LARGE memory model.
  22. */
  23.  
  24. #include <dir.h>
  25. #include <dos.h>
  26. #include <stdlib.h>
  27. #include <fcntl.h>
  28. #include <string.h>
  29. #include <alloc.h>
  30. #include <io.h>
  31. #include <ctype.h>
  32. #include "filter.h"
  33.  
  34. #define TRUE (1 == 1)
  35. #define FALSE !(TRUE)
  36.  
  37. /* Turbo Assembler text for conversion */
  38. char TasmWarningTxt[] = "*Warning* ";
  39. char TasmErrorTxt[]   = "**Error** ";
  40. char TasmFatalTxt[]   = "**Fatal** ";
  41.  
  42. /* Microsoft Assembler and OPTASM text for conversion */
  43. char MasmWarningText[] = "warning";
  44. char MasmErrorText[]   = "error";
  45.  
  46. char     CurFile[MAXPATH];       /* Current file in message window */
  47. unsigned BufSize,                /* Size of internal working buffer */
  48.          CurBufLen;              /* Buffer space in use */
  49. char     *InBuffer,              /* Input buffer */
  50.          *OutBuffer,             /* Output buffer */
  51.          *CurInPtr,              /* current character in input buffer */
  52.          *CurOutPtr,             /* current character in output */
  53.          *LinePtr;               /* pointer to the current character in
  54.                                     the input buffer */
  55. int      (*processor)(char *);   /* function pointer used to call the
  56.                                     appropriate conversion routine */
  57. char     Line[133];              /* static buffer to store line most recently
  58.                                     input */
  59. long int InOff;                  /* position in actual input stream */
  60. char     EndMark;                /* Used to output end of data to message
  61.                                     window */
  62.  
  63. /*************************************************************************
  64. Function  : NextChar
  65. Parameters: none
  66. Returns   : next character in input buffer or 0 on end of file
  67.  
  68. Input from the standard input stream is buffered in a global buffer InBuffer
  69. which is allocated in function main.  The function will return
  70. the next character in the buffer, reading from the input stream when the
  71. buffer becomes empty.
  72. **************************************************************************/
  73. char NextChar(void)
  74. {
  75.   if (CurInPtr < InBuffer+CurBufLen)  /* if buffer is not empty */
  76.   {
  77.     return *(CurInPtr++);             /* return next character */
  78.   }
  79.   else
  80.   {
  81.     CurInPtr = InBuffer;              /* reset pointer to front of buffer */
  82.     lseek(0,InOff,0);                 /* seek to the next section for read */
  83.     InOff += BufSize;                 /* increment pointer to next block */
  84.     if ((CurBufLen = read(0,InBuffer,BufSize)) !=0)
  85.       return NextChar();              /* recursive call merely returns
  86.                                          first character in buffer after read */
  87.     return 0;                         /* return 0 on end of file */
  88.   }
  89. }
  90.  
  91. /*************************************************************************
  92. Function  : flushOut
  93. Parameter : Size   The number of characters to be written out
  94. Returns   : nothing
  95.  
  96. Strings to be sent to the message window are buffered in a buffer called
  97. OutBuffer.  A call to this function will write out Size bytes from OutBuffer
  98. to the standard output stream and resets the output buffer pointer to the
  99. beginning of the buffer.  The output buffer is considered empty after a call
  100. to this function.
  101. *************************************************************************/
  102. void flushOut(unsigned Size)
  103. {
  104.    if (Size != 0)                /* don't flush an empty buffer */
  105.    {
  106.       CurOutPtr = OutBuffer;     /* reset pointer to beginning of buffer */
  107.       lseek(1,0,2);              /* seek output stream to end */
  108.       write(1,OutBuffer,Size);   /* write out Size bytes */
  109.    }
  110. }
  111.  
  112. /************************************************************************
  113. Function  : Put
  114. Parameters: S    pointer to bytes being put into output buffer
  115.             Len  number of bytes to be put in output buffer
  116. Returns   : nothing
  117.  
  118. Put places bytes into OutBuffer so they may be later flushed out into
  119. the standard output stream.
  120. ************************************************************************/
  121. void Put(char *S,int Len)
  122. {
  123.    int i;
  124.  
  125.    for (i = 0; i < Len; i++)
  126.    {
  127.       *CurOutPtr++ = S[i];                   /* place byte in buffer */
  128.       if (CurOutPtr >= OutBuffer+BufSize)    /* if buffer overflows */
  129.          flushOut(BufSize);                  /* flush the buffer */
  130.    }
  131. }
  132.  
  133.  
  134. /************************************************************************
  135. Function  : ProcessNonTasmLine
  136. Parameters: Line  a pointer to the current line of characters to process
  137. Returns   : 1 if line is Microsoft Assembler line
  138.             0 if line is not
  139.  
  140. Analyze line to determine if it is output from a MASM or OPTASM compatible
  141. assembler. In the case of a recognizable line, output relevant information
  142. to the message window indicating message and line number.
  143. -*----------------------------------------------------------------------*-
  144. Microsoft assembler lines which are in need of conversion are of the form:
  145.  
  146.    source-file(LINE #): message-kind message-num : message text
  147.  
  148. where message-kind is one of:  warning, error
  149. -*----------------------------------------------------------------------*-
  150. OPTASM assembler lines which are in need of conversion are of the form:
  151.  
  152.    source-file(LINE #) : message-kind message-num : message text
  153.  
  154. where message-kind is one of: Warning, Error
  155. -*----------------------------------------------------------------------*-
  156. Masm and Optasm lines differ from Tasm lines in that half the line must be
  157. scanned in order to determine if the line is valid.  For this reason all
  158. output information is stored and sent at the end when the determination of
  159. a valid line is made.
  160. ************************************************************************/
  161. int ProcessNonTasmLine(char *Line)
  162. {
  163.    char     Type;
  164.    unsigned i;
  165.    char     *s;
  166.    char     fn[MAXPATH];
  167.    int      NewFile;
  168.  
  169.    if (Line[0] == 0)                         /* Empty line, no action */
  170.       return 0;
  171.  
  172.    s = strchr(Line,'(');                     /* find ( */
  173.    if (s != NULL)                            /* if no (, invalid line */
  174.    {
  175.       memmove(fn,Line,(unsigned)(s-Line));    /* store filename */
  176.       fn[(unsigned)(s-Line)] = 0;             /* null terminate name */
  177.       memmove(Line,++s,strlen(Line));         /* shift line left */
  178.       if (strcmp(fn,CurFile) != 0)            /* if new filename */
  179.       {
  180.          NewFile = TRUE;                      /* indicate by setting flag */
  181.          strcpy(CurFile,fn);                  /* store new name */
  182.       }
  183.       else NewFile = FALSE;
  184.       s = strchr(Line,')');                   /* find the close paren */
  185.       if (s != NULL)                          /* if no ) invalid line */
  186.       {
  187.          *s = 0;                               /* isolate the line number */
  188.          i = atoi(Line);                       /* if number is found convert
  189.                                                   string to integer */
  190.          if (i != 0)
  191.          {
  192.             s++;
  193.             while (*s == ' ')                  /* optasm has space here */
  194.                s++;
  195.             if (*s != ':')                     /* strip : from line */
  196.                return 0;                       /* no :, not MASM line */
  197.             s++;
  198.             memmove(Line,s,strlen(s)+1);        /* shift line */
  199.             while (Line[0] == ' ' && Line[0] != 0)  /* strip spaces from line */
  200.                memmove(Line,&Line[1],strlen(Line));
  201.             Line[0] = tolower(Line[0]);        /* optasm uses upper case */
  202.             /* check for warning or error text from MASM, shift line if
  203.                needed. */
  204.             if ((strncmp(Line, MasmWarningText, strlen(MasmWarningText)) != 0) &&
  205.                 (strncmp(Line, MasmErrorText, strlen(MasmErrorText)) != 0))
  206.                return 0; /* not error or warning, not MASM line */
  207.  
  208.             /* strip spaces from beginning of line */
  209.             while (Line[0] == ' ' && Line[0] != 0)  /* strip spaces from line */
  210.                memmove(Line,&Line[1],strlen(Line));
  211.          }
  212.          else return 0;                   /* no line number, not MASM line */
  213.       }
  214.       else return 0;                      /* no ), not MASM line */
  215.       if (NewFile)
  216.       {
  217.          Type = MsgNewFile;               /* send new file indicated */
  218.          Put(&Type,1);                    /* Put info to output stream */
  219.          Put(CurFile,strlen(CurFile)+1);  /* along with the new name */
  220.       }
  221.       Type = MsgNewLine;                  /* set type to new line */
  222.       Put(&Type,1);                       /* indicate need for new line */
  223.       Put((char *)&i,2);                  /* put the number out */
  224.       i = 1;                              /* set column in message box */
  225.       Put((char *)&i,2);                  /* tab over to put message */
  226.       Put(Line,strlen(Line)+1);           /* output the message */
  227.    }
  228.    else return 0;                         /* no ( on line, not MASM line */
  229.  
  230.    return 1;                              /* MASM line */
  231. }
  232.  
  233. /*************************************************************************
  234. Function  : ProcessTasmLine
  235. Parameters: Line  a pointer to the current line of characters to process
  236. Returns   : 1 if line is a Turbo Assembler line
  237.             0 if line is not
  238.  
  239. Process through a line of input to determine if it is a Turbo Assembler
  240. output line and convert it to information for the Turbo C++ message window.
  241.  
  242. Turbo Assembler lines which are in need of conversion are of the form:
  243.  
  244.     message-type source-file(LINE #) message-text
  245.  
  246. where type is one of: *Warning*, **Error**, **Fatal**
  247.  
  248. TASM lines are identified by the first portion of text.  If warning,
  249. error or fatal is determined at the outset, text is output from there
  250. as it is scanned.  Any incorrect configuration will simply abort the
  251. scanning of the rest of the line.
  252. *************************************************************************/
  253. int ProcessTasmLine(char *Line)
  254. {
  255.    static int HavePutFile = FALSE;
  256.    char     Type;
  257.    unsigned i;
  258.    char     *s;
  259.    char     fn[MAXPATH];
  260.  
  261.    /* don't try to process a NULL line */
  262.    if (Line[0] == 0)
  263.       return 0;
  264.  
  265.    /* check for tasm type tags */
  266.    if ((strncmp(Line,TasmWarningTxt,strlen(TasmWarningTxt)) == 0) ||
  267.       (strncmp(Line,TasmErrorTxt,  strlen(TasmErrorTxt)) == 0) ||
  268.       (strncmp(Line,TasmFatalTxt,  strlen(TasmFatalTxt)) == 0))
  269.  
  270.    {
  271.       /* skip over type by moving string left */
  272.       memmove(Line,&Line[strlen(TasmFatalTxt)],strlen(Line));
  273.  
  274.       /* locate the first open paren '(' filename will be characters to
  275.      to the left, line number will be characters to the right up to the
  276.      close paren ')' */
  277.       s = strchr(Line,'(');                      /* find ( */
  278.       if (s != NULL)                             /* if no (, invalid line */
  279.       {
  280.      memmove(fn,Line,(unsigned)(s-Line));    /* save filename */
  281.      fn[(unsigned)(s-Line)] = 0;             /* null terminate name */
  282.      memmove(Line,++s,strlen(Line));         /* shift line left */
  283.      if (strcmp(fn,CurFile) != 0)            /* if new filename */
  284.      {
  285.             Type = MsgNewFile;                   /* indicate by sending type
  286.                             out to message window */
  287.         strcpy(CurFile,fn);
  288.         Put(&Type,1);
  289.             Put(CurFile,strlen(CurFile)+1);      /* along with the new name */
  290.         HavePutFile = TRUE;
  291.      }
  292.      Type = MsgNewLine;                      /* set type to new line */
  293.      s = strchr(Line,')');                   /* find the close paren */
  294.      if (s != NULL)
  295.      {
  296.             *s = 0;                              /* isolate number in string */
  297.             i = atoi(Line);                      /* if number is found convert
  298.                             string to integer */
  299.         if (i != 0)
  300.         {
  301.            Put(&Type,1);                       /* indicate need for new line */
  302.            Put((char *)&i,2);                  /* put the number out */
  303.            i = 1;                              /* set column in message box */
  304.            Put((char *)&i,2);                  /* tab over to put message */
  305.            s++;
  306.            memmove(Line,s,strlen(s)+1);        /* shift line */
  307.            while (Line[0] == ' ' && Line[0] != 0)  /* strip spaces from line */
  308.           memmove(Line,&Line[1],strlen(Line));
  309.            Put(Line,strlen(Line)+1);           /* output the message */
  310.                return 1;              /* TASM line */
  311.         }
  312.             return 0;                 /* invalid line number, not TASM line */
  313.      }
  314.          return 0;                    /* no ) in line, not TASM line */
  315.       }
  316.       else                            /* Fatal error, no line # or filename */
  317.       {
  318.         if( !HavePutFile )
  319.         {
  320.        /* IDE expects the first message to
  321.           be preceded by a filename.  Since
  322.           we don't have one, fake it by
  323.           sending a NULL file before the
  324.           message.
  325.        */
  326.        Type = MsgNewFile;                  /* indicate by sending type
  327.                               out to message window */
  328.        *CurFile = '\0';
  329.        Put(&Type,1);
  330.        Put(CurFile,1);                     /* along with null filename */
  331.            HavePutFile = TRUE;
  332.         }
  333.                        
  334.     Type = MsgNewLine;            /* Fake line # etc.                   */
  335.     i    = 1;
  336.     Put(&Type,1);
  337.     Put((char *)&i,2);
  338.     Put((char *)&i,2);
  339.     while (Line[0] == ' ' && Line[0] != 0)
  340.       memmove(Line,&Line[1],strlen(Line));
  341.     Put(Line,strlen(Line)+1);
  342.     return 1;
  343.       }
  344.    }
  345.    else return 0;                     /* no line start message, not TASM line */
  346. }
  347.  
  348. /**************************************************************************
  349. Function  : ProcessLine
  350. Parameters: Line    character pointer to line of input data
  351. Returns   : nothing
  352.  
  353. If line type has been determined, call correct line processor through
  354. a function pointer.  Else try testing for both Tasm style line and Masm
  355. style line to determine if either one is the input to the filter.
  356. **************************************************************************/
  357. void ProcessLine(char *Line)
  358. {
  359.     if (processor == NULL)
  360.     {
  361.        if (ProcessTasmLine(Line))            /* check for TASM line */
  362.           processor = ProcessTasmLine;
  363.        else
  364.           if (ProcessNonTasmLine(Line))      /* check MASM or OPTASM style */
  365.              processor = ProcessNonTasmLine;
  366.     }
  367.     else
  368.        processor(Line);                      /* type already determined */
  369. }
  370.  
  371.  
  372. /***********************************************************************
  373. Function  : main
  374.  
  375. Returns   : zero for successful execution
  376.             3    if an error is encountered
  377.  
  378. The main routine allocates buffers for the input and output buffer.
  379. Characters are then read from the input buffer building the line buffer
  380. that will be sent to the filter processor.  Lines are read and filtered
  381. until the end of input is reached.
  382. ***********************************************************************/
  383. int main( void )
  384. {
  385.    char c;
  386.    unsigned long core;
  387.  
  388.    setmode(1,O_BINARY);               /* set output stream to binary mode */
  389.    core = farcoreleft();
  390.    if (core > 64000U)
  391.       BufSize = 64000U;
  392.    else BufSize = (unsigned)core;     /* get available memory */
  393.                                       /* stay under 64K */
  394.    if ((CurInPtr = malloc(BufSize)) == NULL) /* allocate buffer space */
  395.       exit(3);
  396.    processor = NULL;                  /* set current processor to none */
  397.    InBuffer = CurInPtr;               /* input buffer is first half of space */
  398.    BufSize = BufSize/2;               /* output buffer is 2nd half */
  399.    OutBuffer = InBuffer + BufSize;
  400.    CurOutPtr = OutBuffer;             /* set buffer pointers */
  401.    LinePtr = Line;
  402.    CurBufLen = 0;
  403.    Put(PipeId,PipeIdLen);             /* send ID string to message window */
  404.    while ((c = NextChar()) != 0)      /* read characters */
  405.    {
  406.       if ((c == 13) || (c == 10))     /* build until line end */
  407.       {
  408.          *LinePtr = 0;
  409.          ProcessLine(Line);           /* filter the line */
  410.          LinePtr = Line;
  411.       }
  412.       /* characters are added to buffer up to 132 characters max */
  413.       else if ((FP_OFF(LinePtr) - FP_OFF(&Line)) < 132)
  414.       {
  415.          *LinePtr = c;                /* add to line buffer */
  416.          LinePtr++;
  417.       }
  418.    }
  419.    *LinePtr = 0;
  420.    ProcessLine(Line);                 /* filter last line */
  421.    EndMark = MsgEoFile;
  422.    Put(&EndMark,1);                   /* indicate end of input to
  423.                                          the message window */
  424.    flushOut((unsigned)(CurOutPtr-OutBuffer));     /* flush the buffer */
  425.    return 0;                          /* return OK */
  426. }
  427.  
  428.