Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rochester!ur-tut!ur-cvsvax!srs!dan From: dan@srs.UUCP (Dan Kegel) Newsgroups: comp.sys.ibm.pc Subject: Raw Mode and how to set it (part 2) Message-ID: <116@srs.UUCP> Date: Tue, 3-Feb-87 10:11:12 EST Article-I.D.: srs.116 Posted: Tue Feb 3 10:11:12 1987 Date-Received: Sat, 7-Feb-87 04:08:34 EST References: <3281@hplabs.hplabs.UUCP> <1342@kontron.UUCP> Organization: S.R. Systems, Rochester NY Lines: 125 Here is the subroutine promised in my last posting: #!/bin/sh # # shar archiver, delete everything above the #!/bin/sh line # and run through sh (not csh) # echo 'shar: extracting "rawmode.c" (3326 characters)' sed 's/^XX //' > rawmode.c << 'XXX_EOF_XXX' XX /*--- rawmode.c ------------------------------------------------- XX Routines to set and reset raw mode for Microsoft C. XX Stolen from the sources to the mighty game Hack. XX Thanks to Mark Zbikowski (markz@microsoft.UUCP). XX XX Applications should set stdout to raw mode when they start up, and set XX it back to the old mode when terminating. XX Programs which rely on cooked mode will appear to hang in raw mode because, XX in raw mode, keyboard reads don't terminate when the user hits ENTER; they XX continue until the line buffer is full. XX ------------------------------------------------------------------*/ XX #include XX #include XX XX #define DEVICE 0x80 XX #define RAW 0x20 XX #define IOCTL 0x44 XX #define STDIN fileno(stdin) XX #define STDOUT fileno(stdout) XX #define GETBITS 0 XX #define SETBITS 1 XX XX static unsigned old_stdin, old_stdout, ioctl(); XX XX #define BREAKCHECK 0x33 XX XX static unsigned old_breakchk, breakctl(); XX XX /*---- SetHandleRaw ----------------------------------------------------- XX Given any file handle, sets it into raw mode. Useful when opening handles XX to printers, COM ports, etc., as it lets you output ^Z chars without fear. XX -------------------------------------------------------------------------*/ XX void XX SetHandleRaw(handle) XX short handle; XX { XX int temp; XX temp = ioctl(handle, GETBITS, 0); XX if (temp & DEVICE) XX ioctl(handle, SETBITS, temp | RAW); XX } XX XX /*---- rawgetchar --------------------------------------------------- XX Read a character from keyboard with neither echo nor ^C-checking. XX Used by editors, etc., which can't afford to let themselves be killed by the XX user, or who want to use ^C for a function key. XX ----------------------------------------------------------------------*/ XX int XX rawgetchar() XX { XX return (0xff & bdos(7, 0, 0)); XX } XX XX /*--- set_raw --------------------------------------------------------- XX Call this to set raw mode; call restore_raw() later to restore XX console to old rawness state. (Don't strand user in RAW mode!) XX Not only sets raw mode, but also turns off ^C trapping on random DOS calls. XX All character input from stdin should be done with rawgetchar() to XX avoid ^C trapping on input. XX -----------------------------------------------------------------------*/ XX set_raw() XX { XX old_stdin = ioctl(STDIN, GETBITS, 0); XX old_stdout = ioctl(STDOUT, GETBITS, 0); XX old_breakchk = breakctl(GETBITS, 0); XX if (old_stdin & DEVICE) XX ioctl(STDIN, SETBITS, old_stdin | RAW); XX if (old_stdout & DEVICE) XX ioctl(STDOUT, SETBITS, old_stdout | RAW); XX (void) breakctl(SETBITS, 0); XX } XX XX restore_raw() XX { XX if (old_stdin) XX (void) ioctl(STDIN, SETBITS, old_stdin); XX if (old_stdout) XX (void) ioctl(STDOUT, SETBITS, old_stdout); XX if (old_breakchk) XX (void) breakctl(SETBITS, old_breakchk); XX } XX XX static unsigned XX ioctl(handle, mode, setvalue) XX unsigned setvalue; XX { XX union REGS regs; XX XX regs.h.ah = IOCTL; XX regs.h.al = mode; XX regs.x.bx = handle; XX regs.h.dl = setvalue; XX regs.h.dh = 0; /* Zero out dh */ XX intdos(®s, ®s); XX return (regs.x.dx); XX } XX XX /* Controls ^C - checking during everything except keyboard reads */ XX static unsigned XX breakctl(mode, setvalue) XX unsigned setvalue; XX { XX union REGS regs; XX XX regs.h.ah = BREAKCHECK; XX regs.h.al = mode; XX regs.h.dl = setvalue; XX intdos(®s, ®s); XX return (regs.x.dx & 0xff); XX } XX XX /*------ end of rawmode.c ----------------------------------------------*/ XXX_EOF_XXX if test 3326 -ne "`wc -c < rawmode.c`" then echo 'shar: transmission error on "rawmode.c"' fi