home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pmtclsh.zip / src.zip / THINTERP.C < prev   
C/C++ Source or Header  |  1996-10-20  |  3KB  |  125 lines

  1. /* PMTclSh for OS/2 ver 1.0 -- tcl shell for OS/2
  2.  * Copyright (C) 1996 Fornari Stefano
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software for any
  5.  * purpose is granted without fee, provided that the above copyright notice
  6.  * and the following paragraph appear in all copies of this software
  7.  * and supporting documentation.
  8.  *
  9.  * This software is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY, even the implied warranty of MERCHANTABILITY
  11.  * or FITNESS FOR A PARTICULAR PURPOSE.  In no event shall the author be
  12.  * liable to any party for direct, indirect, special, incidental, or
  13.  * consequential damages arising out of the use of this software and its
  14.  * documentation.
  15.  */
  16.  
  17. /*-----------------------------------------------------
  18.    thInterp -- tcl 7.4 Interpreter for OS/2
  19.   -----------------------------------------------------*/
  20.   
  21. #define INCL_DOSSEMAPHORES
  22. #define INCL_DOSPROCESS
  23. #define INCL_WINMLE
  24.  
  25. #include <io.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <process.h>
  30.  
  31. #include <os2.h>
  32.  
  33. #include "tcl.h"
  34. #include "pmtclsh.h"
  35.  
  36.  
  37.  
  38. static TID        tidInterp, tidOutput;
  39. static PID        pidInterp;
  40.  
  41. void ThreadWriteOutErr(PVOID pParam)
  42. {
  43.     PTHREADPARAM pthParam = (PTHREADPARAM)pParam;
  44.     LONG         nLen, nCar;
  45.     CHAR         buf[DIMBUF];
  46.     HWND         hwnd;
  47.     HFILE        hPipe;
  48.     HMQ          hmq;
  49.   
  50.     hmq = WinCreateMsgQueue(WinInitialize(0), 0);
  51.  
  52.     hwnd = pthParam->hwnd;
  53.     hPipe = pthParam->param.hPipe;
  54.  
  55.     DosPostEventSem(pthParam->hSem);
  56.         
  57.     while (1)
  58.     {
  59.         if ((nCar = read(hPipe, buf, sizeof(buf)-1)) < 0)
  60.         {
  61.             WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, strerror(errno), "Error!", 0, MB_OK|MB_ERROR);
  62.             return;
  63.         }
  64.         buf[nCar] = '\0';
  65.         
  66.         nLen = (LONG)WinSendMsg(hwnd, MLM_QUERYTEXTLENGTH, 0, 0);
  67.       
  68.         WinSendMsg(hwnd, MLM_SETSEL, (MPARAM)nLen, (MPARAM)nLen);
  69.         WinSendMsg(hwnd, MLM_INSERT, (MPARAM)buf, 0);
  70.     }
  71. }
  72.  
  73.  
  74. void ThreadWaitInterp(PVOID pParam) {
  75.     THREADPARAM thParam;
  76.  
  77.     memcpy(&thParam, pParam, sizeof(thParam));
  78.     DosPostEventSem(thParam.hSem);
  79.     wait(NULL);
  80.  
  81.     WinPostMsg(thParam.hwnd, TCLSHM_EXITINTERP, 0, 0);
  82. }
  83.  
  84.  
  85. BOOL CreateInterp(HWND hwndFrame, HWND hwndOutput, HFILE hPipe)
  86. {
  87.     char               st[100], cmd[] = "tclsh.exe\0\0";
  88.     ULONG              ul;
  89.     RESULTCODES        rc;
  90.     THREADPARAM thParam;
  91.     
  92.  
  93.     
  94.     if (DosCreateEventSem(NULL, &thParam.hSem, 0, 0)) return FALSE;
  95.  
  96.     DosExecPgm(st, sizeof(st)-1, EXEC_ASYNC, cmd, NULL,
  97.         &thParam.param.rc, cmd);
  98.     
  99.     thParam.hwnd = hwndFrame;
  100.     _beginthread(ThreadWaitInterp, NULL, 4096, &thParam);
  101.  
  102.     DosWaitEventSem(thParam.hSem, SEM_INDEFINITE_WAIT);
  103.     DosResetEventSem(thParam.hSem, &ul);
  104.  
  105.     thParam.hwnd = hwndOutput;
  106.     thParam.param.hPipe = hPipe;
  107.     if ((tidOutput = _beginthread(ThreadWriteOutErr, (void*)NULL, 8192, (void*)&thParam)) == -1)
  108.     {
  109.         DosKillProcess(0, thParam.param.rc.codeTerminate);
  110.         return FALSE;
  111.     }
  112.  
  113.     DosWaitEventSem(thParam.hSem, SEM_INDEFINITE_WAIT);
  114.     DosCloseEventSem(thParam.hSem);
  115.  
  116.     return TRUE;
  117. }
  118.  
  119.  
  120. void DestroyInterp(void)
  121. {
  122.     DosKillProcess(0, pidInterp);
  123.     DosKillThread(tidInterp); DosKillThread(tidOutput);
  124. }
  125.