home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / filter.pak / GREP2MSG.C < prev    next >
C/C++ Source or Header  |  1997-07-23  |  5KB  |  265 lines

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