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

  1. /*
  2.    HC312Msg.C      copyright (c) 1993 Borland International
  3. */
  4.  
  5. #include "ToolApi.H"  // Borland IDE tool dll interface
  6.  
  7. #include <stdlib.h>
  8. #include <ctype.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. /* Declare an array of function pointers to store the IDE tool API */
  20. IDE_ToolAPIFunc IDE_ToolAPI[IDE_NumFunctions];
  21.  
  22. /* Global variables use to parse program output */
  23.  
  24. char WarningText[] = "Warning";
  25. char ErrorText[]   = "Error";
  26.  
  27. char     CurFile[MAXPATH];       /* Current file in message window */
  28.  
  29. FileHandle Pipefh;
  30.  
  31. HMEM       hBuffer;
  32. LPSTR      Buffer;
  33. WORD       curpos;
  34. WORD       bufLen;
  35.  
  36. char       inLine[150];
  37. LPSTR      lineptr;
  38.  
  39. int numFatals   = 0;
  40. int numErrors   = 0;
  41. int numWarnings = 0;
  42.  
  43. /*
  44.   InitBuffer - allocate memory for filtering the piped output to the IDE
  45. */
  46. void InitBuffer( void )
  47. {
  48.   hBuffer = IDE_memalloc( MEM_MOVEABLE, 8192 );
  49.   Buffer = IDE_memlock( hBuffer );
  50.   bufLen = 0;
  51.   curpos = 0;
  52. }
  53.  
  54. /*
  55.   ReleaseBuffer - cleanup allocated buffers and open file handles
  56. */
  57. void ReleaseBuffer( void )
  58. {
  59.   IDE_memunlock( hBuffer );
  60.   IDE_memfree( hBuffer );
  61.   IDE_Close( Pipefh );
  62.   IDE_Delete( PIPEID );
  63. }
  64.  
  65. /*
  66.    nextchar - returns the next character from the pipe input
  67.  
  68.    returns: next character from the pipe associated with handle Pipefh
  69. */
  70. char nextchar( void )
  71. {
  72.   if (curpos < bufLen)
  73.   {
  74.     return Buffer[curpos++];
  75.   }
  76.   Buffer[0] = '\0';
  77.   bufLen = IDE_Read( Pipefh, Buffer, 7000 );
  78.   if (bufLen == 0)
  79.     return 0;
  80.   curpos = 0;
  81.   return nextchar();
  82. }
  83.  
  84. /*
  85.   GetLine - get the next line of text from the pipe
  86.  
  87.   returns: far pointer to string containing next line of text from the current opened
  88.        pipe file
  89. */
  90. LPSTR GetLine( void )
  91. {
  92.   char ch;
  93.   int  count;
  94.  
  95.   lineptr = inLine;
  96.   count = 0;
  97.   while (((ch = nextchar()) != '\x0D') && (ch != '\x0A') && (ch != 0) && (count<133))
  98.   {
  99.     *lineptr = ch;
  100.     lineptr++;
  101.      count++;
  102.   }
  103.   if (count == 133)
  104.   {
  105.      strcpy( &inLine[125], "......" );
  106.   }
  107.   if ((lineptr == inLine) && (ch == 0))
  108.   {
  109.     return NULL;
  110.   }
  111.   *lineptr = '\0';
  112.   return inLine;
  113. }
  114.  
  115. /*
  116. HC31 error and warning lines are of the form:
  117.  
  118.   Warning: ####: line...## of file.nam : Message text
  119.  
  120. */
  121. void ProcessLine( LPSTR Line )
  122. {
  123.   char *s;
  124.   char *p;
  125.   int havePost = 0;
  126.   long lineno;
  127.   Msg M;
  128.   
  129.   // first trick is that the output is garbled due to conio type output
  130.   // being captured.  Problems can be fixed if null terminate at the text
  131.   // "Microsoft"
  132.   if ((s = strstr(Line, "Microsoft")) != NULL)
  133.   {
  134.     *s='\0';
  135.   }
  136.   if (strncmp( Line, ErrorText, strlen(ErrorText)) == 0)
  137.   {
  138.     // it is an error
  139.     numErrors++;
  140.     havePost = 1;
  141.   }
  142.   if (strncmp( Line, WarningText, strlen(WarningText)) == 0)
  143.   {
  144.     // it is a warning
  145.     
  146.     numWarnings++;
  147.     havePost = 1;
  148.   }  
  149.   if (havePost)
  150.   {
  151.     // have a line to parse
  152.     if ((s = strchr(Line, ':')) != NULL)
  153.     {
  154.       s++;
  155.       // located : following error number
  156.       if (!strncmp( s, " line", 5 ))
  157.       {
  158.         // found the " line " text
  159.         s+=5;
  160.         while (*s == '.')
  161.         {
  162.           // get past any .'s
  163.           s++;
  164.         }
  165.         if ((p = strstr(s, " of")) != NULL)
  166.         {
  167.           // found the " of" text, now s points to line number text
  168.           *p = '\0';
  169.           // convert to long
  170.           lineno = atol( s );
  171.           p+=4;
  172.           s = strstr(p, " : ");
  173.           *s = '\0';
  174.           s+=2;
  175.         }
  176.       }
  177.       else
  178.       {
  179.         // error or warning, but no line and fname
  180.         p = NULL;    // set fname ptr to NULL
  181.         lineno = 0;  // line to 0
  182.       }
  183.       // OK, now lineno == line
  184.       // p == filename
  185.       // s == error
  186.       M.message  = s;
  187.       M.filename = p;
  188.       M.column = 1;
  189.       M.line = (unsigned)lineno;
  190.       IDE_PostMessage(CUR_MSG_GROUP, &M);
  191.     }
  192.   }
  193. }
  194.  
  195. /*
  196.   FilterToIDE - Open the pipe output from the program, read and post each line
  197.         to the IDE
  198. */
  199. void FilterToIDE( void )
  200. {
  201.   LPSTR line;
  202.  
  203.   Pipefh = IDE_Open( PIPEID, READ_WRITE );
  204.   if (Pipefh < 0)
  205.   {
  206.     IDE_ErrorBox( "HC312Msg.DLL: Cannot filter output." );
  207.     return;
  208.   }
  209.  
  210.   InitBuffer();
  211.  
  212.   while ((line = GetLine()) != NULL)
  213.   {
  214.     ProcessLine( line );
  215.   }
  216.   ReleaseBuffer();
  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.   // Try to run program capturing output to an intermediate file
  231.   IDE_CaptureToPipe( TransBlock->program,
  232.              TransBlock->cmdline,
  233.              PIPEID );
  234.  
  235.   // post the captured output to the IDE
  236.   FilterToIDE();
  237.  
  238.   // return appropriate code for 
  239.   if (numFatals)
  240.   {
  241.     return toolFatalError;
  242.   }
  243.   if (numErrors)
  244.   {
  245.     return toolErrors;
  246.   }
  247.   if (numWarnings)
  248.   {
  249.     return toolWarnings;
  250.   }
  251.   return toolSuccess;
  252. }
  253.  
  254. #pragma argsused
  255. int far pascal LibMain( HINSTANCE hInstance, WORD wDataSegment,
  256.             WORD wHeapSize, LPSTR lpszCmdLine )
  257. {
  258.   return 1;
  259. }
  260.  
  261. #pragma argsused
  262. int FAR PASCAL WEP ( int bSystemExit )
  263. {
  264.     return 1;
  265. }
  266.  
  267.