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

  1. /*
  2.    EXAMPLE SOURCE CODE FOR GREP FILTER
  3.  
  4.    Grep2Msg.C
  5.    Copyright (c) 1990, 1991 Borland International, Inc.
  6.    All rights reserved.
  7.  
  8.    Grep2Msg - Message filter from Turbo Grep 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 GREP, and
  13.    the output stream is connected to the message window of the IDE.
  14.    This filter is invoked through the IDE transfer mechanism as
  15.  
  16.             grep <commands> | grep2msg | IDE message window
  17.  
  18.     Compile using the LARGE memory model.
  19. */
  20.  
  21. #include <dir.h>
  22. #include <stdlib.h>
  23. #include <fcntl.h>
  24. #include <string.h>
  25. #include <alloc.h>
  26. #include <io.h>
  27. #include <dos.h>
  28. #include "filter.h"
  29.  
  30. #define TRUE  1
  31. #define FALSE 0
  32.  
  33. char     NewFileText[] = "File ";
  34. unsigned BufSize,CurBufLen;
  35. char     *InBuffer,
  36.          *OutBuffer,
  37.          *CurInPtr,
  38.          *CurOutPtr,
  39.          *LinePtr;
  40. char     Line[133];
  41. long int InOff;
  42. char     EndMark;
  43. int      NoLines;
  44.  
  45. /************************************************************************
  46. Function  : NextChar
  47. Parameters: None
  48. Returns   : next character in input buffer or 0 for end of file
  49.  
  50. Input from the standard input stream is buffered in a global buffer InBuffer
  51. which is allocated in function main.  NextChar function will return
  52. the next character in the buffer, reading from the input stream when the
  53. buffer becomes empty.
  54. ************************************************************************/
  55. char NextChar(void)
  56. {
  57.    if (CurInPtr < InBuffer+CurBufLen)   /* if buffer is not empty */
  58.    {
  59.       return *(CurInPtr++);             /* return next information */
  60.    }
  61.    else
  62.    {
  63.       CurInPtr = InBuffer;              /* reset pointer to front of buffer */
  64.       lseek(0,InOff,0);                 /* seek to the next section for read */
  65.       InOff += BufSize;                 /* increment pointer to next block */
  66.       if ((CurBufLen = read(0,InBuffer,BufSize)) !=0)
  67.          return NextChar();             /* recursive call returns first
  68.                                            character in buffer after read */
  69.       return 0;                         /* return 0 on end of file */
  70.    }
  71. }
  72.  
  73. /*************************************************************************
  74. Function  : flushOut
  75. Parameters: Size   The number of characters to be written out
  76. Returns   : nothing
  77.  
  78. Strings to be sent to the message window are placed in a buffer called
  79. OutBuffer.  A call to this function will write Size bytes to the
  80. standard output stream and reset the output buffer pointer to the
  81. beginning of the buffer.  Any additional information in the buffer is
  82. thus lost.
  83. **************************************************************************/
  84. void flushOut(unsigned Size)
  85. {
  86.   if (Size != 0)                 /* don't flush an empty buffer */
  87.   {
  88.     CurOutPtr = OutBuffer;       /* reset pointer to beginning of buffer */
  89.     lseek(1,0,2);                /* seek output stream to end */
  90.     write(1,OutBuffer,Size);     /* write out Size bytes */
  91.   }
  92. }
  93.  
  94. /**************************************************************************
  95. Function  : Put
  96. Parameters: S     pointer to a string of characters
  97.             Len   length of the string of characters
  98. Returns   : Nothing.
  99.  
  100. Put places bytes into OutBuffer so they may be later flushed out into the
  101. standard output stream using flushOut.
  102. *************************************************************************/
  103. void Put(char *S,int Len)
  104. {
  105.   int i;
  106.  
  107.   for (i = 0; i < Len; i++)
  108.   {
  109.     *CurOutPtr++ = S[i];                     /* place byte in buffer */
  110.     if (CurOutPtr >= OutBuffer+BufSize)      /* if buffer overflows */
  111.       flushOut(BufSize);                     /* flush to the stream */
  112.   }
  113. }
  114.  
  115. /**************************************************************************
  116. Function  : ProcessLine
  117. Parameters: Line   a pointer to the character line to be analyzed
  118. Returns   : Nothing.
  119.  
  120. Filters lines output from grep into a format usable in the Turbo C++
  121. environment message window.  Lines are simply sent straight through
  122. with format characters for the message window.
  123. **************************************************************************/
  124. void ProcessLine(char *Line)
  125. {
  126.   char Type;
  127.   unsigned i;
  128.   char *s;
  129.  
  130.   if (Line[0] == 0)                            /* ignore blank line */
  131.     return;
  132.  
  133.   /* check for new file name */
  134.   if (strncmp(Line,NewFileText,strlen(NewFileText)) == 0)
  135.   {
  136.     if (NoLines)                             /* if no lines from last file */
  137.     {
  138.       Type = MsgNewLine;                     /* put some space in window */
  139.       i = 1;
  140.       Put(&Type,1);
  141.       Put((char *)&i,2);
  142.       Put((char *)&i,2);
  143.       Put(" ",2);
  144.     }
  145.     Type = MsgNewFile;                       /* indicate new file */
  146.     Line[strlen(Line)-1] = 0;                /* remove ":" */
  147.     memmove(Line,&Line[strlen(NewFileText)],strlen(Line));
  148.     Put(&Type,1);
  149.     Put(Line,strlen(Line)+1);                /* write filename */
  150.     NoLines = TRUE;
  151.   }
  152.   else
  153.   {
  154.     NoLines = FALSE;                         /* message lines output */
  155.     Type = MsgNewLine;                       /* new line in message window */
  156.     s = strchr(Line,' ');
  157.     if (s != NULL)
  158.     {
  159.       s++;
  160.       if (strncmp(s,"lines match",11) == 0)  /* special case lines matching */
  161.       {
  162.         i = 1;
  163.         Put(&Type,1);                        /* output two newlines */
  164.         Put((char *)&i,2);
  165.         Put((char *)&i,2);                   /* to message window */
  166.         Put(Line,strlen(Line)+1);            /* and the line */
  167.       }
  168.       else
  169.       {
  170.         s--;
  171.         *s = 0;
  172.         i = atoi(Line);
  173.         *s = ' ';
  174.         if (i != 0)
  175.         {
  176.           Put(&Type,1);
  177.           Put((char *)&i,2);                 /* output line number */
  178.           i = 1;                             /* Column */
  179.           Put((char *)&i,2);                 /* set column over */
  180.           s++;
  181.           memmove(Line,s,strlen(s)+1);
  182.           while (Line[0] == ' ' && Line[0] != 0)  /* strip leading spaces */
  183.             memmove(Line,&Line[1],strlen(Line));  /* from remaining line */
  184.           Put(Line,strlen(Line)+1);               /* and put out the line */
  185.         }
  186.       }
  187.     }
  188.   }
  189. }
  190.  
  191. /************************************************************************
  192. Function  : Main
  193.  
  194. Returns   : zero on successful execution
  195.                3 on an error condition
  196.  
  197. The main routine allocates memory for the input and output buffers.
  198. Characters are then read from the input buffer building the line buffer
  199. that will be sent to the filter processor.  Lines are read and filtered
  200. until the end of input is reached.
  201. ************************************************************************/
  202. int main(void)
  203. {
  204.    char c;
  205.    int i, Type;
  206.    unsigned long core;
  207.  
  208.    setmode(1,O_BINARY);               /* set standard out to binary mode */
  209.    NoLines = FALSE;                   /* No lines have been read yet */
  210.    core = farcoreleft();              /* get available memory */
  211.    if (core > 64000U)                 /* limit buffers to total of 64000 */
  212.       BufSize = 64000U;               /* bytes */
  213.    else
  214.       BufSize = (unsigned)core;
  215.    if ((InBuffer = malloc(BufSize)) == NULL) /* allocate buffer space */
  216.       exit(3);                        /* abort if error occured */
  217.    CurInPtr = InBuffer;               /* split buffer */
  218.    BufSize = BufSize/2;               /* between input and output buffers */
  219.    OutBuffer = InBuffer + BufSize;
  220.    CurOutPtr = OutBuffer;
  221.    LinePtr = Line;                    /* set line buffer pointer */
  222.    CurBufLen = 0;                     /* and reset buffer size to zero */
  223.    Put(PipeId,PipeIdLen);             /* Identify process to message window */
  224.    while ((c = NextChar()) != 0)      /* read characters */
  225.    {
  226.       if ((c == 13) || (c == 10))     /* build line until new line is seen */
  227.       {
  228.          *LinePtr = 0;
  229.          ProcessLine(Line);           /* then filter the line */
  230.          LinePtr = Line;
  231.       }
  232.       /* characters are added to line only up to 132 characters */
  233.       else if ((FP_OFF(LinePtr) - FP_OFF(&Line)) < 132)
  234.       {
  235.          *LinePtr = c;
  236.          LinePtr++;
  237.       }
  238.    }
  239.    *LinePtr = 0;
  240.    ProcessLine(Line);                  /* filter last line */
  241.    if (NoLines)                        /* if no lines */
  242.    {
  243.       Type = MsgNewLine;               /* send something to the */
  244.       i = 1;                           /* message window */
  245.       Put((char *)&Type,1);
  246.       Put((char *)&i,2);
  247.       Put((char *)&i,2);
  248.       Put(" ",2);
  249.    }
  250.    EndMark = MsgEoFile;                /* indicate end of input to */
  251.    Put(&EndMark,1);                    /* message window */
  252.    flushOut((unsigned)(CurOutPtr-OutBuffer));  /* flush out remaining buffer */
  253.  
  254.    return  0;                          /* everything went ok */
  255. }
  256.  
  257.