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

  1. /*
  2.    Impl2Msg.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. char NewFileText[] = "File ";
  20.  
  21. int WarnCount = 0;
  22. int ErrorCount = 0;
  23. int FatalCount = 0;
  24.  
  25. int posted = 0;
  26.  
  27. /* Declare an array of function pointers to store the IDE tool API */
  28. IDE_ToolAPIFunc IDE_ToolAPI[IDE_NumFunctions];
  29.  
  30. /* Global variables use to parse program output */
  31. HINSTANCE  globInst;
  32.  
  33. FileHandle Pipefh;
  34.  
  35. HMEM       hBuffer;
  36. LPSTR      Buffer;
  37. WORD       curpos;
  38. WORD       bufLen;
  39.  
  40. char       inLine[133];
  41. LPSTR      lineptr;
  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.   /* cleanup allocated buffers */
  60.   IDE_memunlock( hBuffer );
  61.   IDE_memfree( hBuffer );
  62.   IDE_Close( Pipefh );
  63.  
  64.   /* delete the pipe */
  65.   IDE_Delete( PIPEID );
  66. }
  67.  
  68. /*
  69.    nextchar - returns the next character from the pipe input
  70.  
  71.    returns: next character from the pipe associated with handle Pipefh
  72. */
  73. char nextchar( void )
  74. {
  75.   if (curpos < bufLen)
  76.   {
  77.     return Buffer[curpos++];
  78.   }
  79.   Buffer[0] = '\0';
  80.   bufLen = IDE_Read( Pipefh, Buffer, 7000 );
  81.   if (bufLen == 0)
  82.     return 0;
  83.   curpos = 0;
  84.   return nextchar();
  85. }
  86.  
  87. /*
  88.   GetLine - get the next line of text from the pipe
  89.  
  90.   returns: far pointer to string containing next line of text from the current opened
  91.        pipe file
  92. */
  93. LPSTR GetLine( void )
  94. {
  95.   char ch;
  96.   int  count;
  97.  
  98.   lineptr = inLine;
  99.   count = 0;
  100.   while (((ch = nextchar()) != '\x0D') && (ch != '\x0A') && (ch != 0) && (count<133))
  101.   {
  102.     *lineptr = ch;
  103.     lineptr++;
  104.      count++;
  105.   }
  106.   if (count == 133)
  107.   {
  108.      strcpy( &inLine[125], "......" );
  109.   }
  110.   if ((lineptr == inLine) && (ch == 0))
  111.   {
  112.     return NULL;
  113.   }
  114.   *lineptr = '\0';
  115.   return inLine;
  116. }
  117.  
  118. /*
  119.   ProcessLine - dissect line of input and post it as a message to the IDE
  120.  
  121.   Input:  LPSTR line    the line to dissect and post
  122. */
  123. char CurFile[MAXPATH];
  124.  
  125. void ProcessLine( LPSTR Line )
  126. {
  127.   static int  HavePutFile = FALSE;
  128.   unsigned    HasFileAndLineNumber = FALSE;
  129.   char       *s;
  130.   unsigned    i;
  131.   Msg         M;
  132.  
  133.   if (Line[0] == '\0')                            /* ignore blank line */
  134.     return;
  135.  
  136.   if (Line[0] == '\x0A')
  137.     return;
  138.  
  139.   if( strncmp( Line, "Warning", 7 ) == 0 )      
  140.   {                                             /* we have a warning line */
  141.     Line += 7;                                  /* flush "Warning" */
  142.     if( *Line != ':' )                          /* ':' doesn't follow */
  143.       HasFileAndLineNumber = TRUE;              /* so file, line present */
  144.     
  145.     WarnCount++;
  146.   }
  147.   else if( strncmp( Line, "Error", 5 ) == 0 )   
  148.   {                                             /* we have an error line */
  149.     Line += 5;                                  /* flush "Error" */
  150.     if( *Line != ':' )                          /* ':' doesn't follow */
  151.       HasFileAndLineNumber = TRUE;              /* so file, line present */
  152.     
  153.     ErrorCount++;
  154.   }
  155.   else if( strncmp( Line, "Fatal error", 11 ) == 0 )   
  156.   {                                             /* we have a fatal line */
  157.     Line += 11;                                 /* just post */
  158.     M.message = Line;
  159.     M.filename = NULL;
  160.     M.column = 1;
  161.     M.line = 1;
  162.     IDE_PostMessage( CUR_MSG_GROUP, &M );
  163.     FatalCount++;
  164.     return;    
  165.   }
  166.   else                                          /* not an error line */
  167.     return;
  168.  
  169.   if( HasFileAndLineNumber )
  170.   {                                      /* need to process them */
  171.     while( *Line == ' ' )                /* flush white-space until */
  172.       Line++;                            /*  Line points to the filename */
  173.  
  174.     s = Line;                            
  175.     while( *s != ' ' )                   /* s points to space after */
  176.       s++;                               /*  filename */
  177.     *s = '\0';                           /* Replace space with null */
  178.  
  179.     if (strcmp(Line,CurFile) != 0)       /* if new filename */
  180.     {
  181.        strcpy(CurFile,Line);
  182.        HavePutFile = TRUE;
  183.     }
  184.  
  185.      s++;                                 /* flush white space until */
  186.      while( *s == ' ' )                   /*  s points to line number */
  187.        s++;
  188.      Line = s;                            /* Line points to line number too */
  189.      while( *s != ':' )                   /* position s at ':' after */
  190.        s++;                               /*  line number */
  191.      *s = '\0';                           /* replace ':' with null */
  192.      i = atoi(Line);                      /* convert line number to int */
  193.  
  194.      s++;                                 /* bump past null */
  195.      while( *s == ' ' )                   /* flush white space until */
  196.        s++;                               /*  s points to message */
  197.  
  198.       M.message = s;
  199.       M.filename = CurFile;
  200.       M.column = 1;
  201.       M.line = i;
  202.       IDE_PostMessage( CUR_MSG_GROUP, &M );
  203.       posted++;
  204.    }
  205.    else                                   /* no file or line number */
  206.    {
  207.      Line++;                              /* position Line past ':' */
  208.      while( *Line == ' ' )                /* and at start of message */
  209.        Line++;
  210.  
  211.      if( !HavePutFile )
  212.      {
  213.     /* IDE expects the first message to
  214.        be preceded by a filename.  Since
  215.        we don't have one, fake it by
  216.        sending a NULL file before the
  217.        message.
  218.     */
  219.             CurFile[0] = '\0';
  220.      }
  221.  
  222.      M.message = Line;
  223.      M.filename = CurFile;
  224.      M.column = 1;
  225.      M.line = 1;
  226.      IDE_PostMessage( CUR_MSG_GROUP, &M );
  227.      posted++;
  228.    }
  229. }
  230.  
  231. /*
  232.   FilterToIDE - Open the pipe output from the program, read and post each line
  233.         to the IDE
  234. */
  235. void FilterToIDE( void )
  236. {
  237.   LPSTR line;
  238.  
  239.   Pipefh = IDE_Open( PIPEID, READ_WRITE );
  240.   if (Pipefh < 0)
  241.   {
  242.     char error[100];
  243.     LoadString( globInst, IDS_CANNOTFILTER, error, sizeof(error));
  244.     IDE_ErrorBox( error );
  245.     return;
  246.   }
  247.  
  248.   InitBuffer();
  249.  
  250.   while ((line = GetLine()) != NULL)
  251.   {
  252.     ProcessLine( line );
  253.   }
  254.  
  255.   ReleaseBuffer();
  256. }
  257.  
  258. /*
  259.    Run  -  exported entry point to the filter DLL
  260.  
  261.    Input:  pTransferBlock TransBlock    contains information about the program to
  262.                     be run, its command line, and the IDE tool API
  263. */
  264. int far pascal _export Run( pTransferBlock TransBlock )
  265. {
  266.   // Store the IDE tool API
  267.   memcpy( IDE_ToolAPI, TransBlock->IDE_ToolAPI, sizeof(IDE_ToolAPI) );
  268.  
  269.   // Try to run program capturing output to an intermediate file
  270.   IDE_CaptureToPipe( TransBlock->program,
  271.              TransBlock->cmdline,
  272.              PIPEID );
  273.  
  274.   // post the captured output to the IDE
  275.   FilterToIDE();
  276.  
  277.   // return appropriate code for 
  278.   if (FatalCount)
  279.   {
  280.     return toolFatalError;
  281.   }
  282.   if (ErrorCount)
  283.   {
  284.     return toolErrors;
  285.   }
  286.   if (WarnCount)
  287.   {
  288.     return toolWarnings;
  289.   }
  290.   return toolSuccess;
  291. }
  292.  
  293. #pragma argsused
  294. int far pascal LibMain( HINSTANCE hInstance, WORD wDataSegment,
  295.             WORD wHeapSize, LPSTR lpszCmdLine )
  296. {
  297.   globInst = hInstance;
  298.   return 1;
  299. }
  300.  
  301. #if 0
  302. #pragma argsused
  303. int FAR PASCAL WEP ( int bSystemExit )
  304. {
  305.     return 1;
  306. }
  307. #endif
  308.