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

  1. /*
  2.    borl2Msg.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. int posted = 0;
  20.  
  21. /* Declare an array of function pointers to store the IDE tool API */
  22. IDE_ToolAPIFunc IDE_ToolAPI[IDE_NumFunctions];
  23.  
  24. /* Global variables use to parse program output */
  25. HINSTANCE  globInst;
  26.  
  27. FileHandle Pipefh;
  28.  
  29. HMEM       hBuffer;
  30. LPSTR      Buffer;
  31. WORD       curpos;
  32. WORD       bufLen;
  33.  
  34. char       inLine[133];
  35. LPSTR      lineptr;
  36.  
  37. unsigned  errorCount = 0;
  38. unsigned  fatalCount = 0;
  39. unsigned  warnCount  = 0;
  40.  
  41. /*
  42.   InitBuffer - allocate memory for filtering the piped output to the IDE
  43. */
  44. void InitBuffer( void )
  45. {
  46.   hBuffer = IDE_memalloc( MEM_MOVEABLE, 8192 );
  47.   Buffer = IDE_memlock( hBuffer );
  48.   bufLen = 0;
  49.   curpos = 0;
  50. }
  51.  
  52. /*
  53.   ReleaseBuffer - cleanup allocated buffers and open file handles
  54. */
  55. void ReleaseBuffer( void )
  56. {
  57.   /* cleanup allocated buffers */
  58.   IDE_memunlock( hBuffer );
  59.   IDE_memfree( hBuffer );
  60.   IDE_Close( Pipefh );
  61.  
  62.   /* delete the pipe */
  63.   IDE_Delete( PIPEID );
  64. }
  65.  
  66. /*
  67.    nextchar - returns the next character from the pipe input
  68.  
  69.    returns: next character from the pipe associated with handle Pipefh
  70. */
  71. char nextchar( void )
  72. {
  73.   if (curpos < bufLen)
  74.   {
  75.     return Buffer[curpos++];
  76.   }
  77.   Buffer[0] = '\0';
  78.   bufLen = IDE_Read( Pipefh, Buffer, 7000 );
  79.   if (bufLen == 0)
  80.     return 0;
  81.   curpos = 0;
  82.   return nextchar();
  83. }
  84.  
  85. /*
  86.   GetLine - get the next line of text from the pipe
  87.  
  88.   returns: far pointer to string containing next line of text from the current opened
  89.        pipe file
  90. */
  91. LPSTR GetLine( void )
  92. {
  93.   char ch;
  94.   int  count;
  95.  
  96.   lineptr = inLine;
  97.   count = 0;
  98.   while (((ch = nextchar()) != '\x0D') && (ch != '\x0A') && (ch != 0) && (count<133))
  99.   {
  100.     *lineptr = ch;
  101.     lineptr++;
  102.      count++;
  103.   }
  104.   if (count == 133)
  105.   {
  106.      strcpy( &inLine[125], "....." );
  107.   }
  108.   if ((lineptr == inLine) && (ch == 0))
  109.   {
  110.     return NULL;
  111.   }
  112.   *lineptr = '\0';
  113.   return inLine;
  114. }
  115.  
  116. /*
  117.   ProcessLine - dissect line of input and post it as a message to the IDE
  118.  
  119.   Input:  LPSTR line    the line to dissect and post
  120. */
  121. char CurFile[MAXPATH];
  122.  
  123. void ProcessLine( LPSTR Line )
  124. {
  125.   unsigned   i;
  126.   char       *s, *file, *lineno;
  127.   
  128.   int haveALine = 0;
  129.   
  130.   Msg M;
  131.  
  132.   /* if blank line, return */
  133.   while( *Line && ( (*Line == '\r') || (*Line == '\n') ) )\
  134.   {
  135.     Line++;
  136.   }
  137.   
  138.   if( *Line == '\0' )
  139.   {
  140.     return;
  141.   }
  142.   
  143.   if (strncmp( Line, "   WINDPMI.386", 14 ) == 0)
  144.   {
  145.     M.message = Line;
  146.     M.filename = NULL;
  147.     M.column = 1;
  148.     M.line = 1;
  149.     IDE_PostMessage( CUR_MSG_GROUP, &M );
  150.     s = GetLine();
  151.     s = GetLine();
  152.     if (s != NULL)
  153.     {
  154.       M.message = s;
  155.       IDE_PostMessage( CUR_MSG_GROUP, &M );
  156.     }
  157.     fatalCount++;
  158.     return;    
  159.   }
  160.   
  161.   // If line begins with Error or Warning or Fatal we want it
  162.   if (!strncmp( Line, "Error", 5 ))
  163.   {
  164.     errorCount++;
  165.     haveALine = 1;
  166.   }
  167.   
  168.   if (!strncmp( Line, "Fatal", 5 ))
  169.   {
  170.     fatalCount++;
  171.     haveALine = 1;
  172.   }
  173.   if (!strncmp( Line, "Warning", 7 ))
  174.   {
  175.     warnCount++;    
  176.     haveALine = 1;
  177.   }
  178.   
  179.   if (haveALine)
  180.   {
  181.     // get by the beginning
  182.     s = Line;
  183.     while ((*s != '\0') && 
  184.            (*s != ' '))
  185.            {
  186.              s++;
  187.            }
  188.     if (!*s)
  189.     {
  190.       return;  // no file name
  191.     }
  192.     if (*(s-1) == ':')
  193.     {
  194.       file = NULL;
  195.       i = 0;
  196.       s++;
  197.     }
  198.      
  199.     else
  200.     {
  201.       s++;
  202.       // should be pointing at a filename
  203.       file = s;
  204.   
  205.       // get to next space
  206.       while ((*s != '\0') && 
  207.              (*s != ' '))
  208.              {
  209.                s++;
  210.              }
  211.       if (*s==' ')
  212.       {
  213.         // found end of name
  214.         *s = '\0';  // delimit filename 
  215.         s++;
  216.     
  217.         // should be line number
  218.         lineno = s;
  219.       }
  220.         
  221.       // find end of number
  222.       while ((*s != '\0') && 
  223.              (*s != ':'))
  224.              {
  225.                s++;
  226.              }
  227.       if (*s == ':')
  228.       {
  229.         // found end of number
  230.         *s='\0';  // delimit line number
  231.         s++;
  232.         // set the line number
  233.         i = atoi( lineno );
  234.       }
  235.       else
  236.       {
  237.         // didn't find a number, send 0
  238.         i=0;
  239.         // and reset message to lineno
  240.         s = lineno;
  241.       }
  242.     }
  243.   
  244.     
  245.     // whatever is left must be the error
  246.     M.message = s;
  247.     M.filename = file;
  248.     M.column = 1;
  249.     M.line = i;
  250.     
  251.     IDE_PostMessage( CUR_MSG_GROUP, &M );
  252.     posted++;
  253.   }
  254. }
  255.  
  256. /*
  257.   FilterToIDE - Open the pipe output from the program, read and post each line
  258.         to the IDE
  259. */
  260. void FilterToIDE( void )
  261. {
  262.   LPSTR line;
  263.  
  264.   Pipefh = IDE_Open( PIPEID, 0 );
  265.   if (Pipefh < 0)
  266.   {
  267.     char error[100];
  268.     LoadString( globInst, IDS_CANNOTFILTER, error, sizeof(error));
  269.     IDE_ErrorBox( error );
  270.     fatalCount++;
  271.     return;
  272.   }
  273.  
  274.   InitBuffer();
  275.  
  276.   while ((line = GetLine()) != NULL)
  277.   {
  278.     ProcessLine( line );
  279.   }
  280.  
  281.   ReleaseBuffer();
  282. }
  283.  
  284. /*
  285.    Run  -  exported entry point to the filter DLL
  286.  
  287.    Input:  pTransferBlock TransBlock    contains information about the program to
  288.                     be run, its command line, and the IDE tool API
  289. */
  290. int far pascal _export Run( pTransferBlock TransBlock )
  291. {
  292.   // Store the IDE tool API
  293.   memcpy( IDE_ToolAPI, TransBlock->IDE_ToolAPI, sizeof(IDE_ToolAPI) );
  294.  
  295.   // Try to run program capturing output to an intermediate file
  296.   IDE_CaptureToPipe( TransBlock->program,
  297.              TransBlock->cmdline,
  298.              PIPEID );
  299.  
  300.   // post the captured output to the IDE
  301.   FilterToIDE();
  302.  
  303.   // return appropriate code for 
  304.   if (fatalCount)
  305.   {
  306.     return toolFatalError;
  307.   }
  308.   if (errorCount)
  309.   {
  310.     return toolErrors;
  311.   }
  312.   if (warnCount)
  313.   {
  314.     return toolWarnings;
  315.   }
  316.   return toolSuccess;
  317. }
  318.  
  319. #pragma argsused
  320. int far pascal LibMain( HINSTANCE hInstance, WORD wDataSegment,
  321.             WORD wHeapSize, LPSTR lpszCmdLine )
  322. {
  323.   globInst = hInstance;
  324.   return 1;
  325. }
  326.  
  327. #pragma argsused
  328. int FAR PASCAL WEP ( int bSystemExit )
  329. {
  330.     return 1;
  331. }
  332.  
  333.