home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 035 / pmics.zip / sesdos.cc < prev    next >
C/C++ Source or Header  |  1994-12-10  |  5KB  |  162 lines

  1. /*
  2.     PMICS -- PM interface for playing chess on internet chess server
  3.     Copyright (C) 1994  Kevin Nomura
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.     Author can be reached at email: chow@netcom.com
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "pmics.hh"
  25. #define INCL_DOSFILEMGR
  26. #define INCL_DOSDEVIOCTL
  27. #define INCL_DOSDEVICES
  28. #define INCL_DOSPROCESS
  29. #define INCL_DOSQUEUES
  30. #define INCL_DOSEXCEPTIONS
  31. #define INCL_WINMESSAGEMGR
  32. #include <os2.h>
  33. #include <ievtdata.hpp>
  34. #include <iexcbase.hpp>
  35. #include "session.hh"
  36. #include "wcomm.hh"
  37. #include <strstrea.h>
  38.  
  39. extern strstream commStream;
  40.  
  41. #define HF_STDIN 0
  42. #define HF_STDOUT 1
  43. #define PIPESIZE 16383
  44.  
  45. static VOID _System PipeListen(ULONG readPipe)
  46. {
  47.   char          buf[PIPESIZE+1];
  48.   ULONG         cbRead;
  49.  
  50.   IFUNCTRACE_DEVELOP();
  51.   ITRACE_DEVELOP(IString("listening to pipe ") + IString(readPipe));
  52.   if (! IThread::current().isPMInitialized())
  53.     IThread::current().initializePM();
  54.  
  55.   while(1)
  56.     {
  57.       DosRead(readPipe, (PVOID)buf, PIPESIZE, &cbRead);
  58.       buf[cbRead] = 0;        // for debug print
  59.       commStream.clear();
  60.       commStream.write(buf, cbRead);
  61.       ITRACE_DEVELOP(IString("cbRead ") + IString(cbRead) + IString(buf));
  62.  
  63.       hComm.postEvent(MSG_COM_IN);
  64.     }
  65. }
  66.  
  67.  
  68. ADosSession::ADosSession()
  69. {
  70.   // DosExecPgm stuff
  71.   CHAR          LoadError[CCHMAXPATH];
  72.   RESULTCODES   ReturnCodes;
  73.   ULONG         Pid;
  74.  
  75.   // pipe games stuff
  76.   HFILE         hfOSave, hfONew = HF_STDOUT;
  77.   HFILE         hfOR, hfOW;
  78.   HFILE         hfISave, hfINew = HF_STDIN;
  79.   HFILE         hfIR, hfIW;
  80.   APIRET        rc;
  81.  
  82.   IFUNCTRACE_DEVELOP(); fflush(stdout);
  83.  
  84.   isActive = false;        // preset
  85.  
  86.   // redirect stdout
  87.   DosDupHandle(HF_STDOUT, &hfOSave);  // save stdout
  88.   DosCreatePipe(&hfOR, &hfOW, PIPESIZE);   // pipe for redirecting stdout
  89.   DosDupHandle(hfOW, &hfONew);        // change stdout to the pipe
  90.  
  91.   // redirect stdin
  92.   DosDupHandle(HF_STDIN, &hfISave);   // save stdin
  93.   DosCreatePipe(&hfIR, &hfIW, PIPESIZE);   // pipe for redirecting stdin
  94.   DosDupHandle(hfIR, &hfINew);        // switch stdin to the pipe
  95.  
  96.   // kick off a program with the gimmicked file handles
  97.   rc = DosExecPgm(LoadError,            // object name buffer
  98.                   sizeof(LoadError),    // length of object name buffer
  99.                   EXEC_ASYNC,           // async/trace flags
  100.                   (PSZ)"",              // argument string
  101.                   (PSZ)0,               // environment string
  102.                   &ReturnCodes,         // termination codes
  103.                   (PSZ)"D:\\OS2\\CMD.EXE");    // child program
  104.  
  105.   childPID = ReturnCodes.codeTerminate;   // pull out PID of child
  106.   DosClose(hfOW);   // voodoo stuff the manual says to do
  107.   DosClose(hfIR);
  108.  
  109.   hfONew = HF_STDOUT;
  110.   DosDupHandle(hfOSave, &hfONew);    // bring stdout back
  111.   hfINew = HF_STDIN;
  112.   DosDupHandle(hfISave, &hfINew);    // bring stdin back
  113.  
  114.   if (rc != 0) {
  115.     throw IException(IString("DosExecPgm failed for D:\\OS2\\CMD.EXE, rc = ") + IString(rc) + ", LoadError = " + IString(LoadError));
  116.   }
  117.  
  118.   // initiate a task to Listen to the pipe
  119.   pthr = new IThread(&PipeListen, (unsigned long)hfOR);
  120.   writePipe = hfIW;
  121.  
  122. //  write("dir\n");
  123.  
  124.   write("echo NOTE: your keystrokes will not echo until you hit ENTER!\n");
  125.   
  126.   isActive = true;
  127.   return;   // show ok
  128. }
  129.  
  130.  
  131. ADosSession::~ADosSession()
  132. {
  133.   IFUNCTRACE_DEVELOP();
  134.   ITRACE_DEVELOP(IString("kill pid ") + IString((int)childPID));
  135.   if (pthr) delete pthr;
  136.   DosKillProcess(DKP_PROCESS, childPID);
  137. }
  138.  
  139.  
  140. ADosSession::write(char *s)
  141. {
  142.   ULONG cbWritten, n;
  143.  
  144.   IFUNCTRACE_DEVELOP();
  145. //  DosGetDateTime(&keyDt);
  146.   for (n=0; s[n]; n++) {
  147.     ITRACE_DEVELOP(IString("write char: ") + IString((int)s[n]));
  148.     if (s[n] == '\012')
  149.       {
  150.         DosWrite(writePipe, (PVOID)"\015", 1, &cbWritten);
  151.         DosWrite(writePipe, (PVOID)(s+n), 1, &cbWritten);
  152.       }
  153.     else if (s[n] == '\015')
  154.       {
  155.         DosWrite(writePipe, (PVOID)(s+n), 1, &cbWritten);
  156.         DosWrite(writePipe, (PVOID)"\012", 1, &cbWritten);
  157.       }
  158.     else
  159.       DosWrite(writePipe, (PVOID)(s+n), 1, &cbWritten);
  160.   }
  161. }
  162.