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

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