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

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