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

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