home *** CD-ROM | disk | FTP | other *** search
- // DOS_Boss.lib - Functions for controlling a windowed DOS session.
- // ver.1
- //
- //
- //***** ReadDOSWindow(): Return contents of DOS window in buffer
- // SYNTAX: string ReadDOSWindow(int DOSWindowHandle[,int DataLen])
- // string ReadDOSWindow(string WindowTitle[,int DataLen])
- // WHERE: DOSWindowHandle: Integer identifier for this window
- // DOSWindowTitle: Partial text title of the window (case-insensitive)
- // DataLen: Length of string returned if not NULL
- // RETURN: Returns NULL if failed to read, else returns string containing the
- // contents of the DOS window.
- //
- //
- //***** CopyDOSBufferToLines(): Convert DOS window string to lines
- // SYNTAX: string[] CopyDOSBufferToLines(Buffer,LineCount)
- // WHERE: Buffer: DOS buffer as returned by ReadDOSWindow()
- // LineCount: Number of lines in the returned array
- // RETURN: Returns an array of strings where each element represents
- // the next row of the DOS screen.
- //
- //
- //***** PasteToDOSWindow(): Send characters to a DOS window's keyboard
- // SYNTAX: void PasteToDOSWindow(int DOSWindowHandle,string String)
- // void PasteToDOSWindow(string WindowTitle,string String)
- // WHERE: DOSWindowHandle: Integer identifier for this window
- // DOSWindowTitle: Partial text title of the window (case-insensitive)
- // String: String of characters to paste into the DOS window
- // NOTE: If this function fails after about 15 calls, and won't work again,
- // then call IBM at 1-800-992-4777 and ask for fix for APAR PJ11939.
- // new report # 3X128,PSZ
- //
- //
- //**** SendDosKey(): Send keystrokes to DOS window running ServeOS2
- // SYNTAX: bool SendDosKey(string ServerSpec,string AsciiBuffer)
- // bool SendDosKey(string ServerSpec,int KeyCode)
- // bool SendDosKey(string ServerSpec,int[] KeyCodes,KeyCount)
- // bool SendDosKey(string ServerSpec,int KeyCode,K_CTRL or K_ALT or K_SHIFT)
- // WHERE: ServerSpec: Unique 8.3 name that ServeOS2 was started with
- // AsciiBuffer: Character string to send, will translate to KeyCode
- // KeyCode: ScanCode is high byte, and CharCode is low byte; if not ScanCode
- // then this function will determine one
- // KeyCodes: An array of ScanCode/CharCode key codes
- // KeyCount: How many keys in the KeyCodes array
- // K_CTRL | K_ALT | K_SHIFT: Use one of these pre-defined values to send code that
- // this key was held while Key is pressed
- // RETURN: Return True if communication with DOS works, else False
- //
-
- #define K_CTRL 1
- #define K_ALT 2
- #define K_SHIFT 3
-
- #define K_F1 0x3B00
- #define K_F2 0x3C00
- #define K_F3 0x3D00
- #define K_F4 0x3E00
- #define K_F5 0x3F00
- #define K_F6 0x4000
- #define K_F7 0x4100
- #define K_F8 0x4200
- #define K_F9 0x4300
- #define K_F10 0x4400
- #define K_INS 0x5200
- #define K_DEL 0x4300
- #define K_UP 0x4800
- #define K_DOWN 0x5000
- #define K_LEFT 0x4B00
- #define K_RIGHT 0x4D00
- #define K_PGUP 0x4900
- #define K_PGDN 0x5100
- #define K_END 0x4F00
- #define K_HOME 0x4700
-
- #define K_ESC 0x011B
- #define K_SHIFT_TAB 0x0F00
-
-
- #include <WinTools.lib>
- #include <ClipBrd.lib>
- #include <WinMsg.lib>
- #include <FileIO.lib>
- #include <NamePipe.lib>
-
- ReadDOSWindow(pWindowSpec,pDataLen)
- {
- DOSWindowData = NULL; // assume failure
- _DataLen = 0;
- if ( (lReadHandle = GetWindowHandle(pWindowSpec)) ) {
-
- // Clear the clipboard, so we'll know when new data is here
- PutClipboardData(NULL);
-
- // Send MENU message to the DOS Window to paste all data into the clipboard
- #define WM_SYSCOMMAND 0x0021
- #define CMDSRC_MENU 2
- #define COPY_ALL_COMMAND 0x9E
- WinSendMsg(lReadHandle,WM_SYSCOMMAND,COPY_ALL_COMMAND,CMDSRC_MENU);
-
- if ( NULL != (DOSWindowData = GetClipboardData(CF_TEXT,_DataLen)) )
- _DataLen = strlen(DOSWindowData);
-
- }
- if ( 1 < va_arg() )
- pDataLen = _DataLen;
- return DOSWindowData;
- }
-
-
- CopyDOSBufferToLines(pBuffer,pLineCount)
- {
- for ( _buf = pBuffer, pLineCount = 0; _buf[0];
- pLineCount++, _buf += lStringLen + 1 ) {
- lStringLen = strcspn(_buf,"\n");
- strncpy(_Lines[pLineCount],_buf,lStringLen);
- }
- return _Lines;
- }
-
-
- PasteToDOSWindow(pWindowSpec,pString)
- {
- if ( (lTypeHandle = GetWindowHandle(pWindowSpec)) ) {
-
- // Put the pString data into the clipboard
- PutClipboardData(pString,1+strlen(pString),CF_TEXT);
-
- // Send menu message to paste from the clipboard
- #define PASTE_COMMAND 0x9F
- WinPostMsg(lTypeHandle,WM_SYSCOMMAND,PASTE_COMMAND,CMDSRC_MENU,False);
- }
- }
-
- AsciiScans = "\x1E\x30\x2E\x20\x12\x21\x22\x23\x17\x24\x25\x26\x32\x31\x18\x19\x10\x13\x1F\x14\x16\x2F\x11\x2D\x15\x2C";
- NumberScans = "\x0B\x02\x03\x04\x05\x06\x07\x08\x09\x0A";
-
- GetScanCode(pCharCode)
- {
- lCharCode = pCharCode;
- if ( 'A' <= toupper(lCharCode) && toupper(lCharCode) <= 'Z' )
- return( AsciiScans[toupper(lCharCode)-'A'] );
- if ( '0' <= lCharCode && lCharCode <= '9' )
- return( NumberScans[lCharCode = '0'] );
- switch ( lCharCode ) {
- case '\r': return 0x1C;
- case '\t': return 0x0F;
- case '\b': return 0x0E;
- case '~':
- case '`': 0x29;
- case '!': return GetScanCode('1');
- case '@': return GetScanCode('2');
- case '#': return GetScanCode('3');
- case '$': return GetScanCode('4');
- case '%': return GetScanCode('5');
- case '^': return GetScanCode('6');
- case '&': return GetScanCode('7');
- case '*': return GetScanCode('8');
- case '(': return GetScanCode('9');
- case ')': return GetScanCode('0');
- case '_':
- case '-': return 0x0C;
- case '+':
- case '=': return 0x0D;
- case '|':
- case '\\': return 0x2B;
- case '{':
- case '[': return 0x1A;
- case '}':
- case ']': return 0x1B;
- case ':'
- case ';' return 0x27;
- case '\"':
- case '\'': return 0x28;
- case '<':
- case ',': return 0x33;
- case '>':
- case '.': return 0x34;
- case '?':
- case '/': return 0x35;
- case '\033': return 1;
- }
- return(0);
- }
-
- SendDosKey(pServerSpec,pKeyCode,pHoldKey)
- {
- // Build GSK_Buffer based on type of input
- HoldAKeyDown = ( 2 < va_arg() );
- GSK_Buffer[0] = '\0'; // initialize GSK_Buffer as a byte buffer
- if ( 1 == DataDimension(pKeyCode) ) {
- if ( CMM_BYTE == DataType(pKeyCode) ) {
- // pKeyCode is an ascii string, for all of string add this key code
- // and look up its scan code too
- for ( GSK_BufferLength = 0; pKeyCode[GSK_BufferLength]; GSK_BufferLength++ ) {
- // char code is written first, then scan code
- GSK_Buffer[GSK_BufferLength*2] = pKeyCode[GSK_BufferLength];
- GSK_Buffer[1+(GSK_BufferLength*2)] = GetScanCode(pKeyCode[GSK_BufferLength]);
- }
- } else {
- // this is an array of pkeycode, scancode and charcode
- for ( GSK_BufferLength = 0; GSK_BufferLength < pHoldKey; GSK_BufferLength++ ) {
- // char code is written first, then scan code
- GSK_Buffer[GSK_BufferLength*2] = pKeyCode[GSK_BufferLength] & 0xFF;
- GSK_Buffer[1+(GSK_BufferLength*2)] = (pKeyCode[GSK_BufferLength] >> 8) & 0xFF;
- }
- }
- } else {
- // pKeyCode is a single key if no scan code then must get it
- GSK_BufferLength = 1;
- GSK_CharCode = pKeyCode & 0xFF;
- GSK_ScanCode = (pKeyCode & 0xFF00) >> 8;
- if ( 0 == GSK_ScanCode )
- GSK_ScanCode = GetScanCode(GSK_CharCode);
- if ( HoldAKeyDown ) {
- // adjust keys for pHoldKey
- switch( pHoldKey ) {
- case V_CTRL:
- if ( 'A' <= toupper(GSK_CharCode) && toupper(GSK_CharCode) <= 'Z' )
- GSK_CharCode -= 'A' - 1;
- else if ( V_F1 <= pKeyCode && pKeyCode <= V_F10 )
- GSK_ScanCode += 0x23;
- else {
- switch ( GSK_ScanCode ) {
- case K_LEFT: GSK_ScanCode = 0x73; break;
- case K_RIGHT: GSK_ScanCode = 0x74; break;
- case K_PGUP: GSK_ScanCode = 0x84; break;
- case K_PGDN: GSK_ScanCode = 0x76; break;
- case K_END: GSK_ScanCode = 0x77; break;
- case K_HOME: GSK_ScanCode = 0x75; break;
- }
- }
- break;
- V_ALT:
- if ( 'A' <= toupper(GSK_CharCode) && toupper(GSK_CharCode) <= 'Z' )
- GSK_CharCode = 0;
- else if ( V_F1 <= pKeyCode && pKeyCode <= V_F10 )
- GSK_ScanCode += 0x2D;
- else if ( 0x02 <= ScanCode && ScanCode <= 0x0D )
- GSK_ScanCode += 0x76, GSK_CharCode = 0;
- break;
- V_SHIFT:
- if ( islower(GSK_CharCode) )
- GSK_CharCode = toupper(GSK_CharCode);
- else if ( V_F1 <= pKeyCode && pKeyCode <= V_F10 )
- GSK_ScanCode += 0x19;
- break;
- default:
- printf("\aHoldKey = %d unknown!\a\n",pHoldKey); abort();
- }
- }
- GSK_Buffer[0] = GSK_CharCode;
- GSK_Buffer[1] = GSK_ScanCode;
- }
- return ( 0 == GSK_BufferLength || 255 < GSK_BufferLength )
- ? False
- : SendKeyBufferToServer(pServerSpec,GSK_Buffer,GSK_BufferLength) ;
- }
-
-
- SendKeyBufferToServer(PipeName,Buffer,BufferLen/*max 255*/)
- {
- bool success = False; // assume failure
- sprintf(GSFullName,"\\PIPE\\%s",PipeName);
-
- if ( !DosCreateNPipe(GSFullName,GSPipeHandle,
- NP_ACCESS_DUPLEX | NP_NOINHERIT,
- NP_NOWAIT | NP_TYPE_BYTE | NP_UNLIMITED_INSTANCES | NP_READMODE_BYTE,
- 4096, 0, 0) ) {
- // Give the DOS program up to 10 seconds to open the file
- for ( GSrepeat = 10 * 10; GSRepeat--; ) {
- suspend(100);
- if ( !DosConnectNPipe(GSPipeHandle) ) {
- success = True;
- break;
- }
- }
- if ( success ) {
-
- // change pipe to blocking state to make sure reads and writes are
- // finished
- DosSetNPHState(GSPipeHandle,NP_WAIT | NP_READMODE_BYTE);
-
- _SendBuf[0] = byte(BufferLen);
- memcpy(_SendBuf+1,Buffer,2*BufferLen);
- if ( DosWrite(GSPipeHandle,_SendBuf,2*BufferLen+1,GSBytesSent)
- || GSBytesSent != 2*BufferLen+1 ) {
- success = False;
- } else {
- // Will be finished when DOS closes the file, which will
- // break the pipe. A READ will hang until pipe is broken.
- GSDummyBuf[0] = '\0';
- DosRead(GSPipeHandle,GSDummyBuf,1,GSByteRead);
- }
- DosDisconnectNPipe(GSPipeHandle);
- }
- DosClose(GSPipeHandle);
- }
- return(success);
- }
-