home *** CD-ROM | disk | FTP | other *** search
- /*
- Routines for LightspeedC
-
- (C) Copyright 1985, 1986. THINK Technologies, Inc. All rights reserved.
-
- This file contains:
-
- ungetch, getch, getche.
- */
-
- #ifndef _stdioh_
- #include "stdio.h"
- #endif
-
- #ifndef _EventMgr_
- #include "EventMgr.h"
- #endif
-
- #ifndef _signalh_
- #include "signal.h"
- #endif
-
- #ifndef _Quickdraw_
- #include "QuickDraw.h"
- #endif
-
- extern Boolean _inited;
- extern Boolean _echo;
-
- /* signal handling hooks */
-
- int (*_key_int_sig_func_)() = 0;
-
- /* pdg - 6/18/86 - completely revised command character handling of the
- following function to allow control characters to be
- easily generated from the keyboard */
-
- #ifdef _STD_PASTE_
-
- /* If _STD_PASTE is defined in stdio.h, then characters from the clipboard
- can be pasted into the input stream, unlike TE which pastes into a
- window. This allows all of the standard i/o functions to be passed the
- contents of the paste on a character by character basis. While the
- paste is occurring, the keyboard is constantly monitored for a
- Command-. which will abort the paste. */
-
- static
- AbortEvent()
- {
- EventRecord event;
-
- /* swallow key events looking for Command-. */
-
- while (GetNextEvent(keyDownMask, &event))
- if ((char) event.message == '.' && (event.modifiers & cmdKey))
- return(1);
-
- return(0);
- }
-
- static long paste_offset = 0;
- static int paste_length = 0;
- static Handle scrap_handle = 0;
-
- void _std_setup_paste()
- {
- long scrap_offset;
-
- if (scrap_handle)
- DisposHandle(scrap_handle);
-
- scrap_handle = NewHandle(0);
-
- paste_length = GetScrap(scrap_handle, 'TEXT', &scrap_offset);
- paste_offset = 0;
- }
-
- #endif _STD_PASTE_
-
-
- #line 0 _get_char_from_keyboard()
- int _get_char_from_keyboard()
- {
- register int c;
- EventRecord the_Event; /* holds the event record */
-
- Init_stdio();
-
- #ifdef _STD_PASTE_
-
- /* allow getting keystrokes from the clipboard (scrap) if a paste
- operation has been done */
-
- dopaste:
-
- if (paste_length > 0)
- {
- /* a paste operation is in progress, check for keyboard aborts */
-
- if (AbortEvent())
- paste_length = 0;
- else
- {
- c = * (*scrap_handle + paste_offset);
- paste_length--;
- paste_offset++;
-
- /* change Mac return to a new line */
-
- if (c == '\r') c = '\n';
-
- return (c);
- }
- }
- #endif _STD_PASTE_
-
- nextevent:
-
- while(true)
- {
- while (! GetNextEvent(everyEvent, &the_Event))
- {
- HiliteMenu(0);
- SystemTask();
-
- #ifdef _STD_PASTE_
- if (paste_length > 0) goto dopaste;
- #endif _STD_PASTE_
-
- }
-
- #ifdef _STD_PASTE_
- if (paste_length > 0) goto dopaste;
- #endif _STD_PASTE_
-
- SetCursor(&arrow);
-
- if (the_Event.what == keyDown || the_Event.what == autoKey)
- break;
-
- StdEvent(&the_Event);
- }
-
- /* pick up the character */
-
- c = (unsigned char) the_Event.message;
-
- if (the_Event.modifiers & cmdKey)
- {
-
- /* if signal logic is enabled, then call interrupt procedure */
-
- if (_key_int_sig_func_)
- {register int (*_temp_key_int_sig_func_)() = _key_int_sig_func_;
-
- switch (c)
- {
- case 'c':
- case 'C':
- case '.':
- if ((long)_key_int_sig_func_ == SIG_DFL)
- abort();
-
- _key_int_sig_func_ = (void*) SIG_DFL;
- (*_temp_key_int_sig_func_)(SIGINT);
- goto nextevent;
-
- default: break;
- }
- }
-
- switch (c)
- {
- /* command(fan) D and command Z are considered EOF chars */
-
- case 'd':
- case 'D':
- case 'z':
- case 'Z':
- return (EOF);
-
- /* the key may therefore be some other control key, convert it
- if it is, otherwise ignore the command modifier */
-
- /* NULL is command space, as is command 2 (shift doesn't work
- on for @ since event manager traps command shift combos */
-
- case ' ':
- case '2':
- c = 0;
- break;
-
- /* map command 6 into control-^ */
-
- case '6':
- c = 30;
- break;
-
- /* map command ` into control-~ */
-
- case '`':
- c = 31;
- break;
-
- /* map command backspace into DEL */
-
- case 8:
- c = 0x7F;
- break;
-
- default:
- /* check for standard control chars */
-
- if ((c >= 'A') && (c <= '_')) c-= ('A' - 1);
- else
- if ((c >= 'a') && (c <= '~')) c-= ('a' - 1);
-
- break;
- }
- }
- else
- /* translate any input return to a newline, but not control M! */
- if (c == '\r') c = '\n';
-
- return(c);
- }
-
-
- #line 0 ungetch()
- int ungetch(c)
- int c;
- {
- if (_console->look_full)
- return(EOF);
-
- _console->look_full = true;
- _console->look_ahead = c;
- return(c);
- }
-
-
-
- #line 0 getch_common()
- static int getch_common()
- {
- if (_console->look_full)
- {
- _console->look_full = false;
- return((int)_console->look_ahead);
- }
- return( _get_char_from_keyboard() );
- }
-
-
-
- #line 0 getch()
- int getch()
- {
- return( getch_common() );
- }
-
-
-
- #line 0 getche()
- int getche()
- {
- register int c;
-
- c = getch_common();
- if (c != EOF)
- putch(c);
- return(c);
- }
-
-
-
- #line 0 cgets()
- char *cgets(s)
- register char *s;
- {
- register int c;
- register char *scopy = s;
- register Boolean orig_echo = _echo;
-
- /* handle our own echoing */
-
- Init_stdio();
-
- /*loop until EOF or \n */
-
- while ((c=getch_common())!= EOF)
- {
- if (c == '\b')
- {
- if (scopy != s)
- {
- if (_echo)
- {
- putch('\b');
- putch(' ');
- putch('\b');
- }
-
- *--scopy = ' ';
- }
- }
- else
- {
- if (_echo) putch(c);
-
- if ((*scopy++ = c) == '\n')
- {
- scopy--;
- break;
- }
- }
- }
-
- if ((scopy != s) || (c == '\n'))
- {
- *scopy = '\0';
- }
- else
- s = NULL;
-
- return(s);
- }
-
-