Debian Codegraph @

libsrc/tw/tvget.c

raw
1 /* @(#)tvget.c 19.1 (ES0-DMD) 02/25/03 13:58:14 */
2 /*===========================================================================
3 Copyright (C) 1995 European Southern Observatory (ESO)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License, or (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
16 License along with this program; if not, write to the Free
17 Software Foundation, Inc., 675 Massachusetss Ave, Cambridge,
18 MA 02139, USA.
19
20 Corresponding concerning ESO-MIDAS should be addressed as follows:
21 Internet e-mail: [email protected]
22 Postal address: European Southern Observatory
23 Data Management Division
24 Karl-Schwarzschild-Strasse 2
25 D 85748 Garching bei Muenchen
26 GERMANY
27 ===========================================================================*/
28
29 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
31 .TYPE Module
32 .IDENTIFICATION tvget.c
33 .AUTHOR Francois Ochsenbein [ESO-IPG]
34 .LANGUAGE C
35 .KEYWORDS Terminal Independant i/o Package.
36 .ENVIRONMENT TermWindows
37 .COMMENTS Only Input Functions are here.
38 \begin{TeX}
39 Used Capabilities:
40 $$\begin{tabular}{|lp{37em}|} \hline
41 {\tt ...} & and all input keys, \ie
42 {\tt ku kd kr kl {\it or} kb kh} for arrows,
43 {\tt k1}\dots{\tt k9} for PF keys,
44 {\tt K0}\dots{\tt K9}, {\tt K. K, K- KE} for keypad,
45 extended with {\tt Kf, Ki, Kr}\dots for VT200,
46 {\tt F0}\dots {\tt F}$n$ for function keys. \\
47 \hline
48 \end{tabular}$$
49 \end{TeX}
50 .VERSION 1.0 07-Jul-1986: Creation
51 .VERSION 1.1 12-Aug-1986: Added tv_wa, writing a string with attributes.
52
53 .VERSION 1.2 13-Oct-1986 Some terminals (e.g. HP) need to resend the
54 attribute on each new line.
55 This feature incorporated with ChangedLine flag
56 in standout part of TERM structure.
57 .VERSION 2.0 03-Dec-1986 New terminal-independant with output buffering.
58 .VERSION 2.1 11-Jun-1987 Version '2' of TermWindows ---
59 Defaut input char is Control_Z
60 Redefined interfaces to xv routines, allowing
61 a batch mode, or no terminal connected at all.
62 .VERSION 2.2 23-Jun-1987 Adapted to UNIX and Minimal Implementation.
63 .VERSION 2.3 02-Dec-1987 Use of external function while waiting for input
64 .VERSION 3.0 20-Mar-1988 Version '3' of TermWindows.
65 ----------------------------------------------------------------------------*/
66
67 #define DEBUG 0 /* For debugging */
68
69 #define PM_LEVEL LEVEL_TV
70
71 #define TW_MACROS 0 /* Don't use TermWindows Macros */
72 #define TW_import 0
73 #define TW_STRUCT 0 /* Do not use window Structures */
74 #include <twset.h>
75
76 MID_EXTERN TERM *terms ;
77
78 MID_STATIC short int *low = NULL_PTR(short int);
79 MID_STATIC short int *high= NULL_PTR(short int);/* To scan input sequences */
80 MID_STATIC int TICK = 5; /* Check input every 5 sec.*/
81
82 #if DEBUG
83 # define ENTER_DEBUG(x) ENTER(x)
84 # define EXIT_DEBUG(x) EXIT(x)
85 # define TERMTEST if(!terms->version) tv_gopen();\
86 tv_ed(terms)
87 #else
88 # define TERMTEST if(!terms->version) tv_gopen()
89 # define ENTER_DEBUG(x)
90 # define EXIT_DEBUG(x) return(x)
91 #endif
92
93 #define FINISH goto FIN
94 #define ENTER_TV(x) int state_buffer; \
95 ENTER(x); TERMTEST; \
96 state_buffer = tv_buffer(1);
97 #define EXIT_TV(f) FIN: \
98 tv_buffer(state_buffer); \
99 EXIT(f)
100
101 /* Macros just to improve readibility */
102 #define curl (terms->pos[0])
103 #define curc (terms->pos[1])
104 #define diml (terms->dim[0])
105 #define dimc (terms->dim[1])
106
107 #define BUFR (terms->bufr)
108 #define bufp (terms->ir)
109 #define buftop (terms->irtop)
110 #define BUFS (terms->bufs)
111 #define CAPLIST (terms->caplist)
112 #define RAW_MODE (terms->flags & TERM_RAW)
113 #define KEYPAD (terms->flags & TERM_KPAD)
114
115
116 MONITOR (TVGET);
117
118 /*==========================================================================
119 * tv_gto
120 *==========================================================================*/
121 int tv_gto(sec)
122 /*++++++++++
123 .PURPOSE Defines the Time-out
124 .RETURNS Previous time-out
125 .REMARKS
126 --------------*/
127 int sec; /* IN: The asked time-out (seconds) */
128 {
129 int old;
130
131 old = TICK;
132 TICK = MAX(sec, 1); /* At least 1 second... */
133
134 return(old);
135 }
136
137 /*==========================================================================
138 * tv_gaj
139 *==========================================================================*/
140 static int tv_gaj(ch, index)
141 /*++++++++++
142 .PURPOSE Modifies low and high limits to match an input sequence.
143 .RETURNS Length of the lower matching sequence; 0 if no matching.
144 .REMARKS For internal use (tv_getc) only.
145 --------------*/
146 char *ch; /* IN: Character to match */
147 int index; /* IN: Position of the char in the caplist */
148 {
149 register char *capl, *capu;
150 register int i;
151 #if DEBUG
152 short int *m; char *cap;
153
154 TRACE_ED_STR2("Trying to match char ",ch,1);
155 TRACE_ED_I(".... at offset: ", index);
156 for (m = low; m <= high; m++)
157 { cap = *m + CAPLIST;
158 TRACE_ED_STR2("...sequence", cap, 10);
159 }
160 #endif
161
162 i = index+4;
163
164 capl = *low + CAPLIST;
165
166 if(*ch < *(capl+i)) return(0); /* Match impossible */
167
168 capu = *high + CAPLIST;
169
170 #if DEBUG
171 TRACE_ED_STR2("Up sequence is:", capu, 10);
172 TRACE_ED_STR2("Low sequence is:", capl, 10);
173 #endif
174
175 if (*ch > *(capu+i)) return(0); /* Match impossible */
176
177 while (*ch > *(capl+i)) /* Increment the lower limit until match */
178 capl = *(++low) + CAPLIST;
179 if (*ch != *(capl+i)) return(0);
180
181 while (*ch < *(capu+i)) /* Decrement the upper limit until match */
182 capu = *(--high) + CAPLIST;
183
184 #if DEBUG
185 TRACE_ED_STR2("Low sequence is:", capl, 10);
186 TRACE_ED_STR2("Up sequence is:", capu, 10);
187 #endif
188
189 if (*ch != *(capu+i)) return(0);
190
191 return(*capl);
192 }
193
194 /*==========================================================================
195 * tv_in
196 *==========================================================================*/
197 static int tv_in()
198 /*++++++++++++
199 .PURPOSE Read input from terminal
200 .RETURNS Length of read string / EOF
201 .REMARKS While the program is waiting for input, the function defined
202 by tv_fw is called and executed.
203 ---------------*/
204 {
205 int len, count;
206 TVSAVE saved;
207
208
209 ENTER_DEBUG("+tv_in");
210
211 for (count = 32767; (buftop == 0) && (count > 0); )
212 { if (terms->term_type)
213 len = osaread(0, BUFR+buftop, terms->buf_size-buftop);
214 /* Read from File */
215 else len = ostread(BUFR+buftop, terms->buf_size-buftop, TICK);
216 /* Read Terminal */
217 if (len) break;
218 if (terms->fct) /* Call fw routine */
219 { tv_cus(&saved); /* .. save cursor */
220 count = (*(terms->fct))(count);
221 if (count > 0) tv_cur(&saved); /* .. restore cursor */
222 }
223 else count -= TICK;
224 }
225
226 if (len == 0) len = buftop, buftop=0; /* Was got via tv_supply */
227 if (len < 0) ERROR(osmsg());
228 else if (terms->term_type) /* Read from File */
229 { if (len >= terms->buf_size)
230 len = terms->buf_size, ERROR("Input truncated");
231 else *(BUFR + len++) = '\r';
232 }
233
234 TRACE_STR2(BUFR, len);
235 EXIT_DEBUG(len);
236 }
237
238 /*==========================================================================
239 * tv_inw
240 *==========================================================================*/
241 static int tv_inw(s)
242 /*+++++++++++
243 .PURPOSE Read input from terminal with time_out
244 .RETURNS Length / EOF
245 .REMARKS Unique interface to xv_cin
246 -------------*/
247 int s; /* IN: time-out in seconds */
248 {
249 register int len;
250
251 ENTER_DEBUG("+tv_inw");
252
253 if (terms->term_type == 0) /* Actual terminal */
254 { len = ostread(BUFR, terms->buf_size, s);
255 if (len < 0) ERROR(osmsg());
256 else if (len == 0) len = buftop; /* Was got via tv_supply */
257 TRACE_STR2(BUFR, len);
258 EXIT_DEBUG(len);
259 }
260 EXIT_DEBUG(tv_in()); /* No terminal */
261 }
262
263 /*==========================================================================
264 * tv_getc
265 *==========================================================================*/
266 int tv_getc(ch)
267 /*+++++++++++++
268 .PURPOSE Single-character input routine from the terminal; the character is not
269 echoed on the screen. The meaning of the returned ch depends on
270 type returned.
271 .RETURNS EOF / NOK / _STANDARD_ / _ARROW_ / _KEYPAD_ / _PF_ / _FK_
272 \begin{TeX}
273 \begin{itemize}
274 \item (NOK) if an error occured, or a very long time-out period expired.
275 \item (\_EOF\_) if input file terminated (in batch mode);
276 ch contains the control char (0 in Batch)
277 \item (\_STANDARD\_) for a normal (ASCII printable or control character)
278 \item (\_ARROW\_) for arrows; ch contains the following:
279 \begin{itemize}
280 \item (\_UP\_) up arrow key
281 \item (\_DOWN\_) down arrow key
282 \item (\_RIGHT\_) right arrow key
283 \item (\_LEFT\_) left arrow key
284 \end{itemize}
285 Note that the return is also \_ARROW\_ if some control characters
286 were mapped to cursor movements (see tv\_open)
287 \item (\_KEYPAD\_) for a character from the keypad; ch contains `0' \dots `9'
288 \quad `-' \quad `.' \quad `,' \quad `\b r' (Enter)
289 \item (\_PF\_) for Programmable Function keys: ch contains
290 1 for PF1, 2 for PF2, etc\dots
291 \item (\_FK\_) for other function keys: ch contains 1 for FK1, etc\dots
292 \end{itemize}
293 \end{TeX}
294 .REMARKS The cursor position remains unchanged.
295 The default char (no input) is null.
296 ------------------------*/
297 char *ch; /* OUT: Character typed at keyboard */
298 {
299 register int index, llow;
300 register char *p;
301 int type, seq;
302
303 ENTER("tv_getc");
304
305 #if DEBUG
306 TRACE_ED_STR2("In Buffer: ", BUFR + bufp, buftop-bufp);
307 #endif
308
309 if (terms->last_type) /* Something already exists */
310 { *ch = terms->last_char;
311 type = terms->last_type;
312 FINISH;
313 }
314
315 if(bufp >= buftop ) /* The buffer is empty */
316 { bufp = 0, buftop = 0;
317 type = tv_in();
318 if (type < 0) FINISH; /* Nothing more */
319 buftop += type;
320 type = NOK;
321 if(buftop == 0) FINISH; /* ... still empty! */
322 }
323
324 *ch = BUFR[bufp++];
325 type= _STANDARD_;
326
327 /* Testing the char; Raw mode does not check the incoming
328 * char. Note that the raw flag is set by the TW_cc_RAW
329 * input character */
330
331 if (RAW_MODE) FINISH;
332
333 /* Look in the Control Char Definitions if the typed char
334 * is mapped to an arrow, the EOF or RAW. If yes, transform type
335 */
336
337 index = *(unsigned char *)ch;
338 if (index < 32)
339 { switch(terms->tc[index])
340 { case TW_cc_RET: *ch = '\r'; FINISH;
341 case TW_cc_EOF: type = EOF; FINISH;
342 case TW_cc_UP: case TW_cc_DOWN: case TW_cc_HOME:
343 case TW_cc_LEFT: case TW_cc_RIGHT:
344 type = _ARROW_;
345 *ch = terms->tc[index] - TW_cc_UP;
346 FINISH;
347 }
348
349 /* Test if the read char can be the starting byte
350 * of an input sequence: corresponding bit is set to 1 */
351
352 if (!(terms->specials & MASK(index))) FINISH;
353 }
354 else FINISH;
355
356
357 /* Recognition of an escape sequence: a complete matching
358 * occurs when lower and higher limits of partial match
359 * become identical. Remember that the escape sequence is
360 * coded as (length) (2 chars cap-code) `=' (sequence) */
361
362 low = (short int *)(CAPLIST + terms->indexr);
363 high = low + (terms->nsr-1);
364 index = 0;
365
366 for (seq=0; ;seq++)
367 { llow = tv_gaj(ch,index);
368 index++;
369 if(llow < index)
370 { /* The sequence cannot be understood ... */
371 p = *low + 4 + CAPLIST;
372 index--;
373 oscopy(BUFS, p, index);
374 *(BUFS + index++) = *ch;
375 ERR_ED_STR2("I can't understand sequence:", BUFS, index);
376 EXIT(NOK);
377 }
378
379 if ((high == low) && (index == llow)) break;
380
381 /* Read Next character - if nothing after some delay,
382 assume not an escape sequence */
383
384 if (bufp >= buftop )
385 { bufp = 0, buftop = 0;
386 type = tv_inw(2);
387 if (type < 0) FINISH;
388 buftop += type;
389 if (seq == 0) type = _STANDARD_;
390 if (buftop == 0) /* still empty... */
391 FINISH;
392 }
393 *ch = BUFR[bufp++];
394 }
395
396 /* The sequence was recognized - Set the correct char
397 * and correct type */
398
399 p = *low + CAPLIST; /* Address of escape sequence */
400
401 *ch = *(p+2); /* 2nd char of capability */
402
403 switch (*(p+1)) /* First char of capability */
404
405 { case 'k': /* PF or arrow keys */
406 type = _ARROW_;
407 switch(*ch)
408 { case 'u': *ch = _UP_; break;
409 case 'd': *ch = _DOWN_; break;
410 case 'b': /* in some termcapfiles... */
411 case 'l': *ch = _LEFT_; break;
412 case 'r': *ch = _RIGHT_; break;
413 case 'h': *ch = _HOME_; break; /* home */
414 default : type = _PF_;
415 if (isdigit(*ch)) *ch -= '0';
416 else *ch -= 'A'-10;
417 }
418 break;
419
420 case 'K': /* Keypad keys */
421 type = _KEYPAD_;
422 if (*ch == 'E') *ch = '\r'; /* ENTER key */
423 break;
424 case 'F': /* Other function keys */
425 type = _FK_;
426 if (isdigit(*ch)) *ch -= '0';
427 else *ch -= 'A'-10;
428 break;
429 }
430
431 FIN:
432 terms->last_char = 0;
433 terms->last_type = 0;
434 EXIT (type);
435 }
436
437 /*==========================================================================*/
438 int tv_getb(buf, lbuf)
439 /*+++++++++++++
440 .PURPOSE Raw input mode. Up to lbuf bytes are read, but at least 1.
441 .RETURNS Number of bytes
442 .REMARKS
443 ---------------*/
444 char *buf; /* OUT: What was typed at the terminal */
445 int lbuf; /* IN: Max number of bytes */
446 {
447 char *p, *pe, *pc;
448 int i;
449 char tc[2];
450 MID_RSTATIC char arrows[] = {'u', 'd', 'l', 'r', 'h'};
451
452 ENTER("tv_getb");
453
454 p = buf, pe = p + lbuf;
455
456 if (terms->last_type) /* Something already exists */
457 { pc = NULL_PTR(char);
458 tc[1] = terms->last_char;
459 switch(terms->last_type)
460 { case _ARROW_:
461 tc[1] = arrows[tc[1]];
462 case _PF_:
463 tc[0] = 'k';
464 break;
465 case _KEYPAD_:
466 tc[0] = 'K';
467 break;
468 case _FK_:
469 tc[0] = 'F';
470 tc[1] += (tc[1] > 9 ? 'A'-10 : '0');
471 break;
472 default:
473 pc = &(terms->last_char);
474 }
475 if (pc) *(p++) = *pc;
476 else if (pc = SearchCap(tc)) p += oscopy(p, pc+4, *pc);
477 if(bufp >= buftop ) /* The buffer is empty */
478 FINISH;
479 }
480
481 if(bufp >= buftop ) /* The buffer is empty */
482 { bufp = 0, buftop = 0;
483 if ((i = tv_in()) < 0) FINISH; /* Nothing more */
484 buftop += i;
485 if(buftop == 0) FINISH; /* ... still empty! */
486 }
487
488 i = MIN(pe-p, buftop - bufp);
489 p += oscopy(p, &(BUFR[bufp]), i);
490 bufp += i;
491
492 FIN:
493 terms->last_char = 0;
494 terms->last_type = 0;
495 EXIT (p - buf);
496 }
497
498 /*==========================================================================
499 * tv_inc
500 *==========================================================================*/
501 int tv_inc()
502 /*+++++++++++++
503 .PURPOSE Check if input characters are waiting.
504 .RETURNS Number of input chars waiting, 0 if none.
505 ---------------*/
506 {
507 register int n;
508
509 ENTER("+tv_inc");
510
511 n = buftop - bufp;
512 if (n <= 0) n = ostin();
513
514 EXIT(n);
515 }
516
517 /*==========================================================================
518 * tv_push
519 *==========================================================================*/
520 int tv_push(type, ch)
521 /*+++++++++++++
522 .PURPOSE Supply (will be the next input) the read character.
523 .RETURNS OK
524 .REMARKS
525 ---------------*/
526 int type; /* IN: Class of character */
527 char ch; /* IN: Character in class */
528 {
529 terms->last_char = ch;
530 terms->last_type = type;
531
532 return(OK);
533 }
534
535 /*==========================================================================
536 * tv_supply
537 *==========================================================================*/
538 int tv_supply(str, len, queue)
539 /*+++++++++++++
540 .PURPOSE Supply (will be the next input) or queue a response,
541 as if it were read from the terminal.
542 May be used e.g. in the function used while waiting for input
543 (cf tv_fw).
544 .RETURNS OK / NOK (string too long, truncated)
545 .REMARKS A length of zero indicates an EOS-terminated string,
546 and terminate the string with the carriage-return.
547 ---------------*/
548 char *str; /* IN: String to supply as if read from terminal */
549 int len; /* IN: Length of String (0 for standard string) */
550 int queue; /* IN: 0 for top, 1 for queue */
551 {
552 register int l;
553 char *p;
554
555 ENTER("tv_supply");
556
557 if (len <= 0)
558 len = strlen(str), p = str + len++, *p = '\r';
559 else p = NULL_PTR(char);
560
561 TRACE_STR2(str, len);
562
563 /* Suppress what's already read... */
564
565 if (bufp) buftop = oscopy(BUFR, BUFR+bufp, buftop - bufp),
566 bufp = 0;
567
568 /* If it's a queue, be sure that all type ahead buffer is
569 * exhausted */
570
571 if (queue) buftop += tv_in();
572
573 l = terms->buf_size - buftop; /* Remaining bytes */
574
575 if (len > l) status = NOK, ERROR("Input truncated");
576 else status = OK , l = len;
577
578 if (queue) oscopy(BUFR+buftop, str, l);
579 else oscopy(BUFR+l, BUFR, buftop),
580 oscopy(BUFR, str, l);
581 buftop += l;
582
583 if (p) *p = EOS;
584
585 EXIT(status);
586 }
587
588 /*==========================================================================
589 * tv_fw
590 *==========================================================================*/
591 int tv_fw (f)
592 /*+++
593 .PURPOSE Specify the function to execute while the terminal is
594 waiting for input from keyboard.
595 .RETURNS OK
596 .REMARKS This entry acts as a declaration.
597 The function f has one parameter, the remaining count in s before
598 the time-out, and should return the modified time-out.
599 ---*/
600 int (*f)(); /* IN: Function to execute (NULL_FCT to do nothing) */
601 {
602 ENTER("tv_fw");
603 TRACE_ED_I("Function: ", (long)f);
604 terms->fct = f;
605 EXIT(OK);
606 }
607