|
|
1.1 root 1: /* $Header: /newbits/usr/lib/libcurses/RCS/getkey.c,v 1.2 91/09/30 13:05:46 bin Exp Locker: bin $
2: *
3: * The information contained herein is a trade secret of INETCO
4: * Systems, and is confidential information. It is provided under
5: * a license agreement, and may be copied or disclosed only under
6: * the terms of that agreement. Any reproduction or disclosure of
7: * this material without the express written authorization of
8: * INETCO Systems or persuant to the license agreement is unlawful.
9: *
10: * Copyright (c) 1989
11: * An unpublished work by INETCO Systems, Ltd.
12: * All rights reserved.
13: */
14:
15: /*
16: * Keyboard Escape Sequence Mapping Routine.
17: *
18: * int getkey();
19: *
20: * FUTURE: Support escape sequences split across multiple reads.
21: */
22:
23: #include <stdio.h>
24: #include "curses.ext"
25:
26: /*
27: * Get character from terminal, recognizing escape sequences.
28: */
29: int
30: getkey()
31: {
32: register tkeyent_t * kp;
33: register int len;
34: tkeyent_t * sav_kp;
35: int sav_len;
36: uchar * str;
37: static unsigned char buf[64];
38: static int bufoff;
39: static int buflen;
40:
41: #if DEBUG > 0
42: fprintf( outf, "getkey: bufoff=%d buflen=%d\n", bufoff, buflen );
43: #endif
44:
45: while ( 1 ) {
46:
47: while ( bufoff >= buflen ) {
48:
49: buflen = read( 0, buf, sizeof(buf) - 1 );
50: bufoff = 0;
51:
52: #if DEBUG > 0
53: fprintf( outf, "getkey: %d bytes read\n", buflen );
54: if ( buflen > 0 ) {
55: fprintf( outf, "\t'" );
56: for ( bufoff = 0; bufoff < buflen; bufoff++ )
57: fprintf(outf, "%s",
58: unctrl(buf[bufoff]) );
59: fprintf( outf, "'\n" );
60: bufoff = 0;
61: }
62: #endif
63:
64: if ( buflen <= 0 )
65: return (0);
66:
67: buf[buflen] = '\0';
68: }
69:
70: /*
71: * Search for matching escape sequences.
72: * 'kp' is a pointer into the keymap array.
73: * 'kp->cpp' is a pointer to the capability variable.
74: * '*(kp->cpp)' is a pointer to the capability string.
75: * '**(kp->cpp)'is the first character in the capability string.
76: * Remember the longest capability string found.
77: */
78: for ( sav_kp = NULL, kp = tkeymap; kp->id != 0; kp++ ) {
79:
80: /*
81: * Capability is not defined.
82: */
83: if ( kp->cpp == NULL )
84: continue;
85:
86: /*
87: * Capability is not supported on current terminal.
88: */
89: if ( (str = *(kp->cpp)) == NULL )
90: continue;
91:
92: /*
93: * Compute length of string.
94: */
95: len = strlen(str);
96:
97: /*
98: * Ignore zero-length strings.
99: */
100: if ( len == 0 )
101: continue;
102:
103: #if DEBUG > 0
104: {
105: uchar * cp;
106: fprintf( outf, "getkey:0%03o = '", kp->id );
107: for ( cp = str; *cp; cp++ )
108: fprintf( outf, "%s", unctrl(*cp) );
109: fprintf( outf, "'\n" );
110: }
111: #endif
112: /*
113: * Ignore strings which are longer than the input.
114: */
115: if ( bufoff + len > buflen )
116: continue;
117:
118: /*
119: * Match not found.
120: */
121: if ( memcmp( str, &buf[bufoff], len ) != 0 )
122: continue;
123:
124: #if DEBUG > 0
125: fprintf(outf, "getkey:0%03o match of len %d found!\n",
126: kp->id, len );
127: #endif
128: /*
129: * Match is longer than previous one - remember it.
130: */
131: if ( (sav_kp == NULL) || (sav_len < len) ) {
132: sav_kp = kp;
133: sav_len = len;
134: }
135: #if DEBUG > 0
136: else {
137: fprintf(outf, "getkey:0%03o match discarded\n",
138: kp->id );
139: }
140: #endif
141: }
142:
143: /*
144: * Match was found.
145: */
146: if ( sav_kp != NULL ) {
147: #if DEBUG > 0
148: fprintf(outf,"getkey: match token = 0%o\n",sav_kp->id);
149: #endif
150: bufoff += sav_len;
151:
152: return (sav_kp->id);
153: }
154:
155: #if DEBUG > 0
156: fprintf( outf, "getkey: char = %s\n", unctrl(buf[bufoff]) );
157: #endif
158: /*
159: * Return next char.
160: */
161: return (buf[ bufoff++ ]);
162: }
163: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.