home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / filter.pak / HC312MSG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  5.6 KB  |  272 lines

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