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

  1. /*
  2.    ovct2Msg.C      copyright (c) 1992 Borland International
  3. */
  4.  
  5. #include "StdTypes.H"
  6. #include "TranType.H"
  7. #include "ToolApi.H"
  8. #include "filtrc.h"
  9.  
  10. #include <stdlib.h>
  11. #include <mem.h>
  12. #include <dos.h>
  13. #include <dir.h>
  14. #include <string.h>
  15.  
  16. #include <windows.h>
  17.  
  18. // Name the intermediate "PIPE" through which output will be captured
  19. #define PIPEID "c:\\$$PIPE$$.TC$"
  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[133];
  37. LPSTR      lineptr;
  38.  
  39. /*
  40.   InitBuffer - allocate memory for filtering the piped output from ovct 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') && (ch != '\x0A') && (ch != 0) && (count<133))
  97.   {
  98.     *lineptr = ch;
  99.     lineptr++;
  100.      count++;
  101.   }
  102.   if (count == 133)
  103.   {
  104.      strcpy( &inLine[129], "......" );
  105.   }
  106.   if ((lineptr == inLine) && (ch == 0))
  107.   {
  108.     return NULL;
  109.   }
  110.   *lineptr = '\0';
  111.   return inLine;
  112. }
  113.  
  114. /*
  115.   ProcessLine - dissect line of input and post it as a message to the IDE
  116.  
  117.   Input:  LPSTR line    the line to dissect and post
  118. */
  119. char CurFile[MAXPATH];
  120.  
  121. void ProcessLine( LPSTR Line )
  122. {
  123.   unsigned   i;
  124.   char       *s, *file, *lineno;
  125.   
  126.   Msg M;
  127.  
  128.   /* if blank line, return */
  129.   while( *Line && ( (*Line == '\r') || (*Line == '\n') ) )\
  130.   {
  131.     Line++;
  132.   }
  133.   
  134.   if( *Line == '\0' )
  135.   {
  136.     return;
  137.   }
  138.  
  139.   // If line begins with Error or Warning or Fatal we want it
  140.   if (!strncmp( Line, "Error", 5 ) ||      
  141.       !strncmp( Line, "Fatal", 5 ) ||
  142.       !strncmp( Line, "Warning", 7 ))
  143.   {
  144.     // get by the beginning
  145.     s = Line;
  146.     while ((*s != '\0') && 
  147.            (*s != ' '))
  148.            {
  149.              s++;
  150.            }
  151.     if (!*s)
  152.     {
  153.       return;  // no file name
  154.     }
  155.     s++;
  156.     // should be pointing at a filename
  157.     file = s;
  158.   
  159.     // get to next space
  160.     while ((*s != '\0') && 
  161.            (*s != ' '))
  162.            {
  163.              s++;
  164.            }
  165.     if (*s==' ')
  166.     {
  167.       // found end of name
  168.       *s = '\0';  // delimit filename 
  169.       s++;
  170.     
  171.       // should be line number
  172.       lineno = s;
  173.     
  174.       // find end of number
  175.       while ((*s != '\0') && 
  176.              (*s != ':'))
  177.              {
  178.                s++;
  179.              }
  180.       if (*s == ':')
  181.       {
  182.         // found end of number
  183.         *s='\0';  // delimit line number
  184.         s++;
  185.         // set the line number
  186.         i = atoi( lineno );
  187.       }
  188.       else
  189.       {
  190.         // didn't find a number, send 0
  191.         i=0;
  192.       }
  193.     }
  194.     
  195.     // whatever is left must be the error
  196.     M.message = s;
  197.     M.filename = file;
  198.     M.column = 1;
  199.     M.line = i;
  200.  
  201.     IDE_PostMessage( CUR_MSG_GROUP, &M );
  202.     posted++;
  203.   }
  204. }
  205.  
  206. /*
  207.   FilterToIDE - Open the pipe output from the program, read and post each line
  208.         to the IDE
  209. */
  210. void FilterToIDE( void )
  211. {
  212.   LPSTR line;
  213.  
  214.   Pipefh = IDE_Open( PIPEID, READ_WRITE );
  215.   if (Pipefh < 0)
  216.   {
  217.     char error[100];
  218.     LoadString( globInst, IDS_CANNOTFILTER, error, sizeof(error));
  219.     IDE_ErrorBox( error );
  220.     return;
  221.   }
  222.  
  223.   InitBuffer();
  224.  
  225.   while ((line = GetLine()) != NULL)
  226.   {
  227.     ProcessLine( line );
  228.   }
  229.  
  230.   ReleaseBuffer();
  231. }
  232.  
  233. /*
  234.   CheckVersion - Check IDE version
  235. */
  236. BOOL CheckVersion( void )
  237. {
  238.   // If the DLL is specific to a particular version of the IDE
  239.   // or if it only works for DOS platform or Windows Platform
  240.   // this is the place to check.
  241.  
  242.   return 1;
  243. }
  244.  
  245. /*
  246.    Run  -  exported entry point to the filter DLL
  247.  
  248.    Input:  pTransferBlock TransBlock    contains information about the program to
  249.                     be run, its command line, and the IDE tool API
  250. */
  251. int far pascal _export Run( pTransferBlock TransBlock )
  252. {
  253.   // Store the IDE tool API
  254.   memcpy( IDE_ToolAPI, TransBlock->IDE_ToolAPI, sizeof(IDE_ToolAPI) );
  255.  
  256.   if (!CheckVersion())
  257.   {
  258.     return NO_TASK;
  259.   }
  260.  
  261.   // Try to run ovct.COM capturing output to an intermediate file
  262.   IDE_CaptureToPipe( TransBlock->program,
  263.              TransBlock->cmdline,
  264.              PIPEID );
  265.  
  266.   // post the captured output to the IDE
  267.   FilterToIDE();
  268.  
  269.   return posted;
  270. }
  271.  
  272. #pragma argsused
  273. int far pascal LibMain( HINSTANCE hInstance, WORD wDataSegment,
  274.             WORD wHeapSize, LPSTR lpszCmdLine )
  275. {
  276.   globInst = hInstance;
  277.   return 1;
  278. }
  279.  
  280. #pragma argsused
  281. int FAR PASCAL WEP ( int bSystemExit )
  282. {
  283.     return 1;
  284. }
  285.  
  286.