home *** CD-ROM | disk | FTP | other *** search
/ Resource for Source: C/C++ / Resource for Source - C-C++.iso / misc_src / link2msg / link2msg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-01  |  8.2 KB  |  222 lines

  1. /***************************************************************\
  2. **                                                             **
  3. **  link2msg - Tlink output filter to the IDE message window.  **
  4. **                                                             **
  5. **  Copyright (c) 1993 by Dale Nurden                          **
  6. **                                                             **
  7. **  Modified from Borland International's Tasm2msg             **
  8. **                                                             **
  9. \***************************************************************/
  10.  
  11.  
  12. /*
  13.    This filter accepts input through the standard input stream, converts
  14.    it and outputs it to the standard output stream.  The streams are linked
  15.    through pipes, such that the input stream is the output from Tlink being
  16.    invoked, and the output stream is connected to the message window of the
  17.    IDE, ie.
  18.  
  19.           tlink fname | link2msg | IDE message window
  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. /* Tlink text for non-conversion */
  38. char HeaderMessage[]  = "Turbo Link";
  39.  
  40. char     CurFile[MAXPATH];       /* Current file in message window */
  41. unsigned BufSize,                /* Size of internal working buffer */
  42.          CurBufLen;              /* Buffer space in use */
  43. char     *InBuffer,              /* Input buffer */
  44.          *OutBuffer,             /* Output buffer */
  45.          *CurInPtr,              /* current character in input buffer */
  46.          *CurOutPtr,             /* current character in output */
  47.          *LinePtr;               /* pointer to the current character in
  48.                                     the input buffer */
  49. char     Line[133];              /* static buffer to store line most recently
  50.                                     input */
  51. long int InOff;                  /* position in actual input stream */
  52. char     EndMark;                /* Used to output end of data to message
  53.                                     window */
  54.  
  55. /*************************************************************************
  56. Function  : NextChar
  57. Parameters: none
  58. Returns   : next character in input buffer or 0 on end of file
  59.  
  60. Input from the standard input stream is buffered in a global buffer InBuffer
  61. which is allocated in function main.  The function will return
  62. the next character in the buffer, reading from the input stream when the
  63. buffer becomes empty.
  64. **************************************************************************/
  65. char NextChar(void)
  66. {
  67.   if (CurInPtr < InBuffer+CurBufLen)  /* if buffer is not empty */
  68.   {
  69.     return *(CurInPtr++);             /* return next character */
  70.   }
  71.   else
  72.   {
  73.     CurInPtr = InBuffer;              /* reset pointer to front of buffer */
  74.     lseek(0,InOff,0);                 /* seek to the next section for read */
  75.     InOff += BufSize;                 /* increment pointer to next block */
  76.     if ((CurBufLen = read(0,InBuffer,BufSize)) !=0)
  77.       return NextChar();              /* recursive call merely returns
  78.                                          first character in buffer after read */
  79.     return 0;                         /* return 0 on end of file */
  80.   }
  81. }
  82.  
  83. /*************************************************************************
  84. Function  : flushOut
  85. Parameter : Size   The number of characters to be written out
  86. Returns   : nothing
  87.  
  88. Strings to be sent to the message window are buffered in a buffer called
  89. OutBuffer.  A call to this function will write out Size bytes from OutBuffer
  90. to the standard output stream and resets the output buffer pointer to the
  91. beginning of the buffer.  The output buffer is considered empty after a call
  92. to this function.
  93. *************************************************************************/
  94. void flushOut(unsigned Size)
  95. {
  96.    if (Size != 0)                /* don't flush an empty buffer */
  97.    {
  98.       CurOutPtr = OutBuffer;     /* reset pointer to beginning of buffer */
  99.       lseek(1,0,2);              /* seek output stream to end */
  100.       write(1,OutBuffer,Size);   /* write out Size bytes */
  101.    }
  102. }
  103.  
  104. /************************************************************************
  105. Function  : Put
  106. Parameters: S    pointer to bytes being put into output buffer
  107.             Len  number of bytes to be put in output buffer
  108. Returns   : nothing
  109.  
  110. Put places bytes into OutBuffer so they may be later flushed out into
  111. the standard output stream.
  112. ************************************************************************/
  113. void Put(char *S,int Len)
  114. {
  115.    int i;
  116.  
  117.    for (i = 0; i < Len; i++)
  118.    {
  119.       *CurOutPtr++ = S[i];                   /* place byte in buffer */
  120.       if (CurOutPtr >= OutBuffer+BufSize)    /* if buffer overflows */
  121.          flushOut(BufSize);                  /* flush the buffer */
  122.    }
  123. }
  124.  
  125.  
  126. /*************************************************************************
  127. Function  : ProcessLine
  128. Parameters: Line  a pointer to the current line of characters to process
  129. Returns   : 1 if line is a Tlink line
  130.             0 if line is not
  131. *************************************************************************/
  132. int ProcessLine(char *Line)
  133. {
  134.    char     Type;
  135.    unsigned i;
  136.  
  137.    /* don't try to process a NULL line */
  138.    if (Line[0] == 0)
  139.       return 0;
  140.  
  141.    if (strnicmp(Line,HeaderMessage,strlen(HeaderMessage)))
  142.    {
  143.      /* IDE expects the first message to
  144.        be preceded by a filename.  Since
  145.         we don't have one, fake it by
  146.        sending a NULL file before the
  147.        message.
  148.     */
  149.      Type = MsgNewFile;                  /* indicate by sending type out to message window */
  150.     *CurFile = '\0';
  151.     Put(&Type,1);
  152.     Put(CurFile,1);                     /* along with null filename */
  153.  
  154.     Type = MsgNewLine;            /* Fake line # etc.                   */
  155.     i    = 0;
  156.     Put(&Type,1);
  157.     Put((char *)&i,2);
  158.     Put((char *)&i,2);
  159.     while (Line[0] == ' ' && Line[0] != 0)
  160.       memmove(Line,&Line[1],strlen(Line));
  161.     Put(Line,strlen(Line)+1);
  162.    }
  163.   return 1;
  164. }
  165.  
  166.  
  167. /***********************************************************************
  168. Function  : main
  169.  
  170. Returns   : zero for successful execution
  171.             3    if an error is encountered
  172.  
  173. The main routine allocates buffers for the input and output buffer.
  174. Characters are then read from the input buffer building the line buffer
  175. that will be sent to the filter processor.  Lines are read and filtered
  176. until the end of input is reached.
  177. ***********************************************************************/
  178. int main( void )
  179. {
  180.    char c;
  181.    unsigned long core;
  182.  
  183.    setmode(1,O_BINARY);               /* set output stream to binary mode */
  184.    core = farcoreleft();
  185.    if (core > 64000U)
  186.       BufSize = 64000U;
  187.    else BufSize = (unsigned)core;     /* get available memory */
  188.                                       /* stay under 64K */
  189.    if ((CurInPtr = malloc(BufSize)) == NULL) /* allocate buffer space */
  190.       exit(3);
  191.    InBuffer = CurInPtr;               /* input buffer is first half of space */
  192.    BufSize = BufSize/2;               /* output buffer is 2nd half */
  193.    OutBuffer = InBuffer + BufSize;
  194.    CurOutPtr = OutBuffer;             /* set buffer pointers */
  195.    LinePtr = Line;
  196.    CurBufLen = 0;
  197.    Put(PipeId,PipeIdLen);             /* send ID string to message window */
  198.    while ((c = NextChar()) != 0)      /* read characters */
  199.    {
  200.       if ((c == 13) || (c == 10))     /* build until line end */
  201.       {
  202.          *LinePtr = 0;
  203.          ProcessLine(Line);           /* filter the line */
  204.          LinePtr = Line;
  205.       }
  206.       /* characters are added to buffer up to 132 characters max */
  207.       else if ((FP_OFF(LinePtr) - FP_OFF(&Line)) < 132)
  208.       {
  209.          *LinePtr = c;                /* add to line buffer */
  210.          LinePtr++;
  211.       }
  212.    }
  213.    *LinePtr = 0;
  214.    ProcessLine(Line);                 /* filter last line */
  215.    EndMark = MsgEoFile;
  216.    Put(&EndMark,1);                   /* indicate end of input to
  217.                                          the message window */
  218.    flushOut((unsigned)(CurOutPtr-OutBuffer));     /* flush the buffer */
  219.    return 0;                          /* return OK */
  220. }
  221.  
  222.