home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 14.ddi / FILTER.PAK / GREP2MSG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  5.2 KB  |  259 lines

  1. /*
  2.    Grep2Msg.C      copyright (c) 1993 Borland International
  3. */
  4.  
  5. #include "ToolApi.H"
  6.  
  7. #include <stdlib.h>
  8. #include <mem.h>
  9. #include <dos.h>
  10. #include <dir.h>
  11. #include <string.h>
  12.  
  13. #include <windows.h>
  14.  
  15. // Name the intermediate "PIPE" through which output will be captured
  16. #define PIPEID "c:\\$$PIPE$$.TC$"
  17.  
  18. char NewFileText[] = "File ";
  19.  
  20. int posted = 0;
  21.  
  22. /* Declare an array of function pointers to store the IDE tool API */
  23. IDE_ToolAPIFunc IDE_ToolAPI[IDE_NumFunctions];
  24.  
  25. /* Global variables use to parse program output */
  26. FileHandle Pipefh;
  27.  
  28. HMEM       hBuffer;
  29. LPSTR      Buffer;
  30. WORD       curpos;
  31. WORD       bufLen;
  32.  
  33. char       inLine[255];
  34. LPSTR      lineptr;
  35.  
  36. /*
  37.   InitBuffer - allocate memory for filtering the piped output from grep to the IDE
  38. */
  39. void InitBuffer( void )
  40. {
  41.   hBuffer = IDE_memalloc( MEM_MOVEABLE, 8192 );
  42.   Buffer = IDE_memlock( hBuffer );
  43.   bufLen = 0;
  44.   curpos = 0;
  45. }
  46.  
  47. /*
  48.   ReleaseBuffer - cleanup allocated buffers and open file handles
  49. */
  50. void ReleaseBuffer( void )
  51. {
  52.   /* cleanup allocated buffers */
  53.   IDE_memunlock( hBuffer );
  54.   IDE_memfree( hBuffer );
  55.   IDE_Close( Pipefh );
  56.  
  57.   /* delete the pipe */
  58.   IDE_Delete( PIPEID );
  59. }
  60.  
  61. /*
  62.    nextchar - returns the next character from the pipe input
  63.  
  64.    returns: next character from the pipe associated with handle Pipefh
  65. */
  66. char nextchar( void )
  67. {
  68.   if (curpos < bufLen)
  69.   {
  70.     return Buffer[curpos++];
  71.   }
  72.   Buffer[0] = '\0';
  73.   bufLen = IDE_Read( Pipefh, Buffer, 7000 );
  74.   if (bufLen == 0)
  75.     return 0;
  76.   curpos = 0;
  77.   return nextchar();
  78. }
  79.  
  80. /*
  81.   GetLine - get the next line of text from the pipe
  82.  
  83.   returns: far pointer to string containing next line of text from the current opened
  84.        pipe file
  85. */
  86. LPSTR GetLine( void )
  87. {
  88.   char ch;
  89.   int  count;
  90.  
  91.   lineptr = inLine;
  92.   count = 0;
  93.   while ( ((ch = nextchar()) != '\x0D') && 
  94.            (ch != '\x0A')               && 
  95.            (ch != 0)                    && 
  96.            (count < 133))
  97.   {
  98.     *lineptr = ch;
  99.     lineptr++;
  100.     count++;
  101.   }
  102.   if (count >= 133)
  103.   {
  104.     strcpy( &inLine[125], "......" );
  105.     lineptr = &inLine[133];
  106.     
  107.     // strip remainder of long line
  108.     while ( ((ch = nextchar()) != '\x0D') && 
  109.              (ch != '\x0A')               && 
  110.              (ch != 0))
  111.            {
  112.            }
  113.   }
  114.   if ((lineptr == inLine) && (ch == 0))
  115.   {
  116.     return NULL;
  117.   }
  118.   *lineptr = '\0';
  119.   return inLine;
  120. }
  121.  
  122. /*
  123.   ProcessLine - dissect line of input and post it as a message to the IDE
  124.  
  125.   Input:  LPSTR line    the line to dissect and post
  126. */
  127. char curFile[MAXPATH];
  128.  
  129. void ProcessLine( LPSTR Line )
  130. {
  131.   unsigned i;
  132.   char *s;
  133.   Msg M;
  134.  
  135.   if (Line[0] == '\0')                            /* ignore blank line */
  136.     return;
  137.  
  138.   if (Line[0] == '\x0A')
  139.     return;
  140.  
  141.   /* check for new file name */
  142.   if (strncmp(Line,NewFileText,strlen(NewFileText)) == 0)
  143.   {
  144.     Line[strlen(Line)-1] = 0;                /* remove ":" */
  145.     memmove(curFile,&Line[strlen(NewFileText)],strlen(Line));
  146.   }
  147.   else
  148.   {
  149.     s = strchr(Line,' ');
  150.     if (s != NULL)
  151.     {
  152.       s++;
  153.       if (strncmp(s,"lines match",11) == 0)  /* special case lines matching */
  154.       {
  155.       }
  156.       else
  157.       {
  158.     s--;
  159.     *s = 0;
  160.     i = atoi(Line);
  161.     *s = ' ';
  162.     if (i != 0)
  163.     {
  164.       s++;
  165.       memmove(Line,s,strlen(s)+1);
  166.       while (Line[0] == ' ' && Line[0] != 0)  /* strip leading spaces */
  167.         memmove(Line,&Line[1],strlen(Line));  /* from remaining line */
  168.  
  169.       M.message = Line;
  170.       M.filename = curFile;
  171.       M.column = 1;
  172.       M.line = i;
  173.  
  174.       IDE_PostMessage( CUR_MSG_GROUP, &M );
  175.       posted++;
  176.     }
  177.       }
  178.     }
  179.   }
  180. }
  181.  
  182. /*
  183.   FilterToIDE - Open the pipe output from the program, read and post each line
  184.         to the IDE
  185. */
  186. void FilterToIDE( void )
  187. {
  188.   LPSTR line;
  189.  
  190.   Pipefh = IDE_Open( PIPEID, READ_WRITE );
  191.   if (Pipefh < 0)
  192.   {
  193.     IDE_ErrorBox( "Grep2Msg.DLL: Cannot filter output." );
  194.     return;
  195.   }
  196.  
  197.   InitBuffer();
  198.  
  199.   while ((line = GetLine()) != NULL)
  200.   {
  201.     ProcessLine( line );
  202.   }
  203.  
  204.   ReleaseBuffer();
  205. }
  206.  
  207. /*
  208.   CheckVersion - Check IDE version
  209. */
  210. BOOL CheckVersion( void )
  211. {
  212.   // If the DLL is specific to a particular version of the IDE
  213.   // or if it only works for DOS platform or Windows Platform
  214.   // this is the place to check.
  215.  
  216.   return 1;
  217. }
  218.  
  219. /*
  220.    Run  -  exported entry point to the filter DLL
  221.  
  222.    Input:  pTransferBlock TransBlock    contains information about the program to
  223.                     be run, its command line, and the IDE tool API
  224. */
  225. int far pascal _export Run( pTransferBlock TransBlock )
  226. {
  227.   // Store the IDE tool API
  228.   memcpy( IDE_ToolAPI, TransBlock->IDE_ToolAPI, sizeof(IDE_ToolAPI) );
  229.  
  230.   if (!CheckVersion())
  231.   {
  232.     return NO_TASK;
  233.   }
  234.  
  235.   // Try to run Grep.COM capturing output to an intermediate file
  236.   IDE_CaptureToPipe( TransBlock->program,
  237.              TransBlock->cmdline,
  238.              PIPEID );
  239.  
  240.   // post the captured output to the IDE
  241.   FilterToIDE();
  242.  
  243.   return 1; // always bring up message window
  244. }
  245.  
  246. #pragma argsused
  247. int far pascal LibMain( HINSTANCE hInstance, WORD wDataSegment,
  248.             WORD wHeapSize, LPSTR lpszCmdLine )
  249. {
  250.   return 1;
  251. }
  252.  
  253. #pragma argsused
  254. int FAR PASCAL WEP ( int bSystemExit )
  255. {
  256.     return 1;
  257. }
  258.  
  259.