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

  1. /*
  2.    rc2Msg.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. /*
  38.   InitBuffer - allocate memory for filtering the piped output to the IDE
  39. */
  40. void InitBuffer( void )
  41. {
  42.   hBuffer = IDE_memalloc( MEM_MOVEABLE, 8192 );
  43.   Buffer = IDE_memlock( hBuffer );
  44.   bufLen = 0;
  45.   curpos = 0;
  46. }
  47.  
  48. /*
  49.   ReleaseBuffer - cleanup allocated buffers and open file handles
  50. */
  51. void ReleaseBuffer( void )
  52. {
  53.   /* cleanup allocated buffers */
  54.   IDE_memunlock( hBuffer );
  55.   IDE_memfree( hBuffer );
  56.   IDE_Close( Pipefh );
  57.  
  58.   /* delete the pipe */
  59.   IDE_Delete( PIPEID );
  60. }
  61.  
  62. /*
  63.    nextchar - returns the next character from the pipe input
  64.  
  65.    returns: next character from the pipe associated with handle Pipefh
  66. */
  67. char nextchar( void )
  68. {
  69.   if (curpos < bufLen)
  70.   {
  71.     return Buffer[curpos++];
  72.   }
  73.   Buffer[0] = '\0';
  74.   bufLen = IDE_Read( Pipefh, Buffer, 7000 );
  75.   if (bufLen == 0)
  76.     return 0;
  77.   curpos = 0;
  78.   return nextchar();
  79. }
  80.  
  81. /*
  82.   GetLine - get the next line of text from the pipe
  83.  
  84.   returns: far pointer to string containing next line of text from the current opened
  85.        pipe file
  86. */
  87. LPSTR GetLine( void )
  88. {
  89.   char ch;
  90.   int  count;
  91.  
  92.   lineptr = inLine;
  93.   count = 0;
  94.   while (((ch = nextchar()) != '\x0D') && (ch != '\x0A') && (ch != 0) && (count<133))
  95.   {
  96.     *lineptr = ch;
  97.     lineptr++;
  98.      count++;
  99.   }
  100.   if (count == 133)
  101.   {
  102.      strcpy( &inLine[125], "......" );
  103.   }
  104.   if ((lineptr == inLine) && (ch == 0))
  105.   {
  106.     return NULL;
  107.   }
  108.   *lineptr = '\0';
  109.   return inLine;
  110. }
  111.  
  112. /*
  113.   ProcessLine - dissect line of input and post it as a message to the IDE
  114.  
  115.   Input:  LPSTR line    the line to dissect and post
  116. */
  117. char CurFile[MAXPATH];
  118.  
  119. void ProcessLine( LPSTR Line )
  120. {
  121.   static int HavePutFile = FALSE;
  122.   int        HasLineNumber;
  123. //  char       Type;
  124.   unsigned   i;
  125.   char       *s, *LeftParen, *Colon;
  126.   Msg M;
  127.  
  128.    /* if blank line, return */
  129.    while( *Line && ( (*Line == '\r') || (*Line == '\n') ) )
  130.      Line++;
  131.    if( *Line == '\0' )
  132.      return;
  133.  
  134.    /* if line starts with "Microsoft" or "Copyright", it's not
  135.       an error line */
  136.    
  137.    /* also include "RLINK32" */
  138.  
  139.    if( strncmp( Line, "Microsoft", 9 ) == 0 ||
  140.        strncmp( Line, "Copyright", 9 ) == 0  )
  141.    {
  142.      return;
  143.    }
  144.  
  145.  
  146.    HasLineNumber = FALSE;
  147.    LeftParen = strchr( Line, '(' );       /* if '(' in the line */
  148.    if( LeftParen != NULL )                /* Line may contain line number */
  149.      HasLineNumber = TRUE;
  150.  
  151.    if( HasLineNumber ) {
  152.      Colon = strchr(LeftParen, ':' );     /* if no ':' following the '(' */
  153.      if( Colon == NULL )                  /* Line doesn't contain line number */
  154.        HasLineNumber = FALSE;
  155.    }
  156.  
  157.    if( HasLineNumber ) {
  158.      s = LeftParen-1;                       /* position s after last char */
  159.      while( *s == ' ' )                     /* in filename */
  160.        s--;
  161.      s++;
  162.      *s = 0;                                /* and null-terminate */
  163.  
  164.      if( strcmp(Line,CurFile) != 0 )        /* if new filename */
  165.      {
  166.        strcpy(CurFile,Line);
  167.        HavePutFile = TRUE;
  168.      }
  169.  
  170.      s = strchr(LeftParen+1,')');                   /* find the right paren */
  171.      *s = 0;                                 /* and null-terminate line# */
  172.      i = atoi(LeftParen+1);                  /* line# starts after ( */
  173.                          /* convert line# to integer */
  174.  
  175.      s = Colon+1;                            /* position s at first */
  176.      while( *s == ' ' )                      /* non-blank char after : */
  177.        s++;                                  /* Rest of line is message */
  178.       
  179.       M.message = s;
  180.       M.filename = CurFile;
  181.       M.column = 1;
  182.       M.line = i;
  183.  
  184.       IDE_PostMessage( CUR_MSG_GROUP, &M );
  185.       posted++;
  186.    }
  187.    else {                                    /* no line number */
  188.      if( !HavePutFile )
  189.      {
  190.     /* IDE expects the first message to
  191.        be preceded by a filename.  Since
  192.        we don't have one, fake it by
  193.        sending a NULL file before the
  194.        message.
  195.     */
  196.        CurFile[0] = '\0';
  197.        HavePutFile = TRUE;
  198.      }
  199.  
  200.       M.message = Line;
  201.       M.filename = CurFile;
  202.       M.column = 1;
  203.       M.line = 1;
  204.  
  205.       IDE_PostMessage( CUR_MSG_GROUP, &M );
  206.       posted++;
  207.    }
  208.  
  209. }
  210.  
  211. /*
  212.   FilterToIDE - Open the pipe output from the program, read and post each line
  213.         to the IDE
  214. */
  215. void FilterToIDE( void )
  216. {
  217.   LPSTR line;
  218.  
  219.   Pipefh = IDE_Open( PIPEID, READ_WRITE );
  220.   if (Pipefh < 0)
  221.   {
  222.     char error[100];
  223.     LoadString( globInst, IDS_CANNOTFILTER, error, sizeof(error));
  224.     IDE_ErrorBox( error );
  225.     return;
  226.   }
  227.  
  228.   InitBuffer();
  229.  
  230.   while ((line = GetLine()) != NULL)
  231.   {
  232.     ProcessLine( line );
  233.   }
  234.  
  235.   ReleaseBuffer();
  236. }
  237.  
  238. /*
  239.    Run  -  exported entry point to the filter DLL
  240.  
  241.    Input:  pTransferBlock TransBlock    contains information about the program to
  242.                     be run, its command line, and the IDE tool API
  243. */
  244. int far pascal _export Run( pTransferBlock TransBlock )
  245. {
  246.   // Store the IDE tool API
  247.   memcpy( IDE_ToolAPI, TransBlock->IDE_ToolAPI, sizeof(IDE_ToolAPI) );
  248.  
  249.   // Try to run program capturing output to an intermediate file
  250.   IDE_CaptureToPipe( TransBlock->program,
  251.              TransBlock->cmdline,
  252.              PIPEID );
  253.  
  254.   // post the captured output to the IDE
  255.   FilterToIDE();
  256.  
  257.   return posted;
  258. }
  259.  
  260. #pragma argsused
  261. int far pascal LibMain( HINSTANCE hInstance, WORD wDataSegment,
  262.             WORD wHeapSize, LPSTR lpszCmdLine )
  263. {
  264.   globInst = hInstance;
  265.   return 1;
  266. }
  267.  
  268. #pragma argsused
  269. int FAR PASCAL WEP ( int bSystemExit )
  270. {
  271.     return 1;
  272. }
  273.  
  274.