home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool_main.zip / ool / source / Xcons.CPP < prev    next >
C/C++ Source or Header  |  1998-03-22  |  5KB  |  187 lines

  1. #include "xcons.h"
  2. #include "xpmthr.h"
  3. #include "xpipe.h"
  4. #include "xrect.h"
  5. #include <stdlib.h>
  6. #include "xuserbtn.h"
  7. #include "xbitmap.h"
  8. #include "oolres.rh"
  9. #include "xreslib.h"
  10. XConsole * window;
  11.  
  12. #include<iostream.h>
  13.  
  14. void XConsole :: CheckConsole(int argc, void ** argv)
  15. {
  16.    XString arguments;
  17.    BOOL console = FALSE;
  18.    for(int i = 1; i < argc; i++)
  19.    {
  20.       XString s = (char*) argv[i];
  21.       if(s == "-console")
  22.       {
  23.          console = TRUE;
  24.       } /* end if */
  25.       else
  26.       {
  27.          arguments += " ";
  28.          arguments += s;
  29.       }
  30.    } /* end for */
  31.    if( console )
  32.    {
  33.       window = new XConsole((char*) argv[0], arguments);
  34.       XApplication::GetApplication()->Start();
  35.       exit(0);
  36.    } /* end if */
  37. }
  38.  
  39.  
  40. class ConsoleThread: public XPMThread
  41. {
  42.       XString file;
  43.       XString command;
  44.    public:
  45.       PID pid;
  46.       ConsoleThread( const char * c, const char * f): XPMThread(256000) { file = f; command = c; }
  47.       void Init( void );
  48. };
  49.  
  50.  
  51. XConsole :: XConsole(char * f, char *  v):XFrameWindow( 100, "XConsole", XFrameWindow::defaultStyle | FRM_TASKLIST)
  52. {
  53.    titleBtn = NULL;
  54.    window = this,
  55.    mle = new XMultiLineEdit(this, XRect(), 0, MLE_HORZSCROLL|MLE_VERTSCROLL|WIN_VISIBLE, "", "8.Helv");
  56.  
  57.    SetClient(mle);
  58.    XRect r (10,10,400,300);
  59.    SetSize(&r);
  60.  
  61.    Show();
  62.  
  63.    HWND cons = QueryWindow (WIN_PARENT)->GetHandle ();
  64.    titleBar = WinWindowFromID (cons, FID_TITLEBAR);
  65.    SWP swp;
  66.    WinQueryWindowPos (titleBar, &swp);
  67.    WinSetWindowPos (titleBar, 0L, swp.x + swp.cy, swp.y, swp.cx - swp.cy, swp.cy, SWP_MOVE | SWP_SIZE);
  68.    XFrameWindow * owner = (XFrameWindow*) WinQueryWindowULong( cons, 0);
  69.    titleBtn = new XUserButton( owner, XRect(swp.x, swp.y, swp.cy, swp.cy), 9999, BS_NOPOINTERFOCUS | BS_NOBORDER | BS_NOCURSORSELECT);
  70.    XBitmap bmp;
  71.    XResourceLibrary lib("OOLRES");
  72.    XResource res(IDR_RESTART, &lib);
  73.    bmp.Load(&res);
  74.    titleBtn->SetBitmap(&bmp);
  75.    titleBtn->Show();
  76.    thread = new ConsoleThread(fn = f, va = v);
  77.    thread->Run();
  78. }
  79.  
  80. void XConsole :: DoSize (XSize* s)
  81. {
  82.    SWP swp;
  83.    WinQueryWindowPos (titleBar, &swp);
  84.    WinSetWindowPos (titleBar, 0L, swp.x + swp.cy, swp.y, swp.cx - swp.cy, swp.cy, SWP_MOVE | SWP_SIZE);
  85.    if(titleBtn)
  86.       titleBtn->SetSize(swp.x, swp.y, swp.cy, swp.cy);//WinSetWindowPos (titleBtn, 0L, swp.x, swp.y, 0, 0, SWP_MOVE);
  87. }
  88.  
  89. BOOL XConsole :: DoCommand (LONG msg) {
  90.    switch (msg) {
  91.    case 9999:
  92.       if (NULL == thread) {
  93.          thread = new ConsoleThread(fn, va);
  94.          thread->Run();
  95.       }
  96.       break;
  97.    }
  98.    return TRUE;
  99. }
  100.  
  101. void ConsoleThread :: Init( void )
  102. {
  103.    #define PIPESIZE 4096
  104.    ULONG cbRead;
  105.    char achBuf[PIPESIZE];
  106.    HFILE hfSave = -1, hfSave2 = -1, hfNew = XPIPE_STDOUT, hfNew2=XPIPE_STDERROR;
  107.  
  108.    XString string = "XConsole (c)opyright Stefan von Brauk 1998\n\nStarting:  ";
  109.    string += command;
  110.    string += " ";
  111.    string += file;
  112.    string += "\r\n\r\n";
  113.  
  114.    window->mle->SetText(string);
  115.    window->mle->ShowFirstChar();
  116.    LONG l = window->mle->GetTextLength();
  117.    window->mle->SelectText( l,l);
  118.  
  119.    //save stdout-handle
  120.    XPipe::DuplicateHandle( XPIPE_STDOUT, hfSave);
  121.    //save stderror-handle
  122.    XPipe::DuplicateHandle( XPIPE_STDERROR, hfSave2);
  123.  
  124.    XPipe pipe;
  125.    //open a pipe
  126.    pipe.Open();
  127.    //set the write-handle as stdout
  128.    XPipe::DuplicateHandle( pipe.GetWriteHandle(), hfNew);
  129.  
  130.    //set the write-handle as stderror
  131.    XPipe::DuplicateHandle( pipe.GetWriteHandle(), hfNew2);
  132.  
  133.    LONG result;
  134.  
  135.    ULONG li;
  136.  
  137.    if( command.Find( li, ".cmd") || command.Find( li, ".CMD"))
  138.    {
  139.       XString commandLine = "/C ";
  140.       commandLine += command;
  141.       commandLine += " ";
  142.       commandLine += file;
  143.       pid = XProcess::ExecuteProg( (char*) "CMD.EXE", (char*) commandLine, NULL, EXE_ASYNC, &result);
  144.    }
  145.    else
  146.       pid = XProcess::ExecuteProg( command, (char*) file, NULL, EXEC_ASYNCRESULT, &result);
  147.  
  148.    //close write-handle
  149.    XPipe::CloseHandle(pipe.GetWriteHandle() );
  150.  
  151.    //bring the saved handles back to stdout and stderror
  152.    XPipe::DuplicateHandle( hfSave, hfNew);
  153.  
  154.    XPipe::DuplicateHandle( hfSave2, hfNew2);
  155.  
  156.    //close stdout/stderror
  157.    XPipe::CloseHandle(hfSave);
  158.    XPipe::CloseHandle(hfSave2);
  159.  
  160.    do
  161.    {
  162.       //read data
  163.       cbRead = pipe.Read( achBuf, PIPESIZE);
  164.       achBuf[cbRead] = 0;
  165.       if(cbRead)
  166.       {
  167.          int l = window->mle->GetTextLength();
  168.          window->mle->SelectText( l,l);
  169.          window->mle->InsertString( (char*) achBuf );
  170.       }
  171.    } while(cbRead);
  172.  
  173.    pipe.Close();
  174.  
  175.    XString end = "\n\rAction complete, RC=";
  176.    end += (int) result;
  177.  
  178.    window->mle->InsertString( end );
  179.  
  180.    window->thread = NULL;
  181.  
  182.    pid = 0;
  183.    window->thread = NULL;
  184.    Terminate();
  185. }
  186.  
  187.