Debian Codegraph @

libsrc/tw/tvinit.c

raw
1 /* @(#)tvinit.c 19.1 (ES0-DMD) 02/25/03 13:58:15 */
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 tvinit.c
33 .AUTHOR Francois Ochsenbein [ESO-IPG]
34 .LANGUAGE C
35 .KEYWORDS Terminal Independant i/o Package
36 .ENVIRONMENT TermWindows
37 .COMMENTS This module provides functions to open/close the
38 terminal.
39 PhysiCal output makes use of ost.
40 *** NOTE *** This version includes the OS-dependant
41 fi_date routine ***
42 \begin{TeX}
43 Used Capabilities:
44 $$\begin{tabular}{|lp{30em}|}\hline
45 {\tt .M=} & Switch on automatic margin \\
46 {\tt .m=} & Switch off automatic margin \\
47 {\tt am} & Terminal has automatic margins \\
48 {\tt co\#} & Number of columns on the screen \\
49 {\tt is=} & Initialization sequence \\
50 {\tt ti=} & Initialization sequence for special modes \\
51 {\tt te=} & Undo what's done by {\tt ti} \\
52 {\tt ks=} & Start keypad mode \\
53 {\tt ke=} & Exit keypad mode \\
54 {\tt li\#} & Number of lines on the screen \\
55 {\tt rs=} & Reset terminal sequence \\
56 {\tt se=} & Exit Stand-out mode (normal attributes) \\
57 {\tt Va=} & Set keyboard to ASCII \\
58 {\tt Vn=} & Set normal Video attributes \\
59 {\tt up=} & Move Cursor up \\
60 {\tt do=} & Move Cursor down \\
61 {\tt bc=} & Move Cursor left \\
62 {\tt nd=} & Move Cursor right \\
63 {\tt nl=} & Write a newline \\
64 {\tt cm=} & Cursor absolute positionning \\
65 \hline
66 \end{tabular}$$
67 The default TERMCAP file the logical {\tt TERMCAPFILE}
68 (or some other definition in {\tt stdef:terminal.h}).
69 However, the login directory is searched for an existing (binary)
70 copy of terminal capabilities, named from terminal name with extension
71 {\tt .tty}.\\[0.5ex]
72 {\bf Notes}:
73 \begin{enumerate}
74 \item if the terminal has automatic margins (the cursor moves to
75 the beginning of the next line when the right margin is reached),
76 the number of columns is decremented to avoid wrapping problems.
77 \item if no terminal is available (\eg in batch mode),
78 what should appear on the terminal is simply directed to stdout.
79 \end{enumerate}
80
81 \end{TeX}
82
83 .VERSION 1.0 07-Jul-1986: Creation
84 .VERSION 1.1 02-Sep-1986: The default directory is searched
85 for a binary copy of terminal capabilities.
86 The TERMCAPFILE is only used if such a binary copy does not
87 exist.
88 .VERSION 1.2 26-Sep-1986: At initialisation, set scrolling region
89 to whole screen.
90 .VERSION 1.3 10-Oct-1986: At closing, use of newly defined
91 tt_close function.
92 .VERSION 2.0 03-Dec-1986: New terminal-independant version.
93 Buffering capabilities.
94 .VERSION 2.1 12-Jun-1987: Possibility of Batch Mode.
95 Modified bug in ztinit.
96 Version '2' of TermCap: check if Termcapfile
97 was not modified.
98 .VERSION 2.2 23-Jun-1987: Minimal implementation in Version 2,
99 with UNIX compatibility.
100 This minimal implementation has the restrictions:
101 \begin{TeX}
102 \begin{itemize}
103 \item no batch mode
104 \item a binary copy of the termcapabilities for the specific terminal
105 exist: no need to parse the TermCapFile and to encode the
106 capabilities.
107 \item no batch possibility
108 \item no insert/delete/scrolling
109 \end{itemize}
110 \end{TeX}
111
112 .VERSION 2.3 02-Dec-1987 Use of external function while waiting for input
113 .VERSION 3.0 15-Mar-1988 Adapted on top of OS routines. Version 3
114 which allows definition of control characters.
115 .VERSION 3.1 21-Feb-1989 Added tv_setcc
116 .VERSION 3.2 28-Mar-1989 Added DEFAULT_TERMINAL = ansi
117 .VERSION 3.3 17-May-1989 Check minimal capabilities
118 .VERSION 3.4 19-Jun-1989 Allow :nl=\n capability
119 .VERSION 3.5 29-Jun-1989 Check correctly the version...
120 .VERSION 3.6 25-Jul-1989 Reset Automatic Margins at Close...
121 .VERSION 3.7 23-Aug-1989 `ti' cap is required for SunView...
122 Also added (for Unix) a lookup in Local /etc/termcap
123 .VERSION 3.8 28-May-1990 Allow hard terminals...
124 .VERSION 3.9 27-Aug-1990 Leave Int = ^C as default in Unix
125 .VERSION 3.91 07-Sep-1990 Let write a message if a problem occurs...
126 .VERSION 3.92 09-Oct-1990 Allow file output (Unix background)
127 .VERSION 3.93 03-Dec-1990 tv_setsc allows the change of Control Char Set
128 .VERSION 3.94 26-Feb-1991 Was calling tv_open recursively in batch...
129 Also added socket option in tv_setsc
130 .VERSION 4.0 28-Jan-1994 Create the binary copy in MID_WORK vs HOME. CG.
131 -------------------------------------------------------*/
132
133 #define DEBUG 0 /* For debugging only */
134 #define PM_LEVEL LEVEL_TV
135
136 #define Closing_Wait 500 /* ms to wait at Closing */
137
138 #define DEFAULT_TERMINAL "ansi"
139
140 #define TW_MACROS 0 /* Don't use TermWindows Macros */
141 #define TW_import 0
142 #define TW_STRUCT 0 /* Do not use window Structures */
143 #include <osterm.h> /* OS Terminal Definitions */
144 #include <twset.h>
145
146 #ifndef NULL
147 #define NULL NULL_PTR(char)
148 #endif
149
150 #define EXT_TTY ".tty"
151
152 #if MID_OS_MSDOS /* --- For Ibm-PC (M. Albrecht) */
153 MID_STATIC char *DIR_TTY = "\\"; /* Default Directory */
154 # define DIR_TTY_TRAILER ""
155 #else
156 #if MID_OS_VMS
157 MID_STATIC char *DIR_TTY = "SYS$LOGIN:";
158 # define DIR_TTY_TRAILER ""
159 #else /* UNIX */
160 MID_STATIC char *DIR_TTY = NULL; /* Default Directory */
161 # define DIR_TTY_TRAILER "/"
162 # define local_termcapfile "/etc/termcap"
163 #endif
164 #endif
165
166 MID_STATIC TERM sterm = { /* The Terminal Definition */
167 1, /* No Terminal */
168 0 /* The version id */
169 };
170 static struct termstatus tst;
171
172 MID_GLOBAL TERM *terms = &sterm;
173 /* The terminal structure common to TV routines */
174
175 static short int old_pos[2];
176
177 MID_STATIC char ccdef[4][33] = { /* Control Characters */
178 { TW_cc_NONE, TW_cc_MODE, TW_cc_HOME, TW_cc_INT, /* User */
179 TW_cc_DOWN, TW_cc_EOL, TW_cc_REFR, TW_cc_NONE, /* ^D ^E ^F ^G */
180 TW_cc_HELP, TW_cc_NW, TW_cc_RET , TW_cc_HELP, /* ^H ^I ^J ^K */
181 TW_cc_LEFT, TW_cc_RET, TW_cc_NW, TW_cc_NONE, /* ^L ^M ^N ^O */
182 TW_cc_NONE, TW_cc_NONE, TW_cc_RIGHT,TW_cc_NONE, /* ^P ^Q ^R ^S */
183 TW_cc_NONE, TW_cc_UP, TW_cc_DELE, TW_cc_DELW, /* ^T ^U ^V ^W */
184 TW_cc_DELL, TW_cc_QUIT, TW_cc_EOF, TW_cc_NONE, /* ^X ^Y ^Z ^[ */
185 TW_cc_RAW, TW_cc_NONE, TW_cc_MODE, TW_cc_NONE, /* ^\ ^] ^^ ^_ */
186 TW_cc_DELC}, /* Delete */
187 { TW_cc_NONE, TW_cc_MODE, TW_cc_UP, TW_cc_INT, /* VMS */
188 TW_cc_LEFT, TW_cc_EOL, TW_cc_RIGHT,TW_cc_HELP, /* ^D ^E ^F ^G */
189 TW_cc_HOME, TW_cc_NW, TW_cc_RET , TW_cc_HELP, /* ^H ^I ^J ^K */
190 TW_cc_NONE, TW_cc_RET, TW_cc_DOWN, TW_cc_NONE, /* ^L ^M ^N ^O */
191 TW_cc_NONE, TW_cc_NONE, TW_cc_REFR, TW_cc_NONE, /* ^P ^Q ^R ^S */
192 TW_cc_NONE, TW_cc_DELL, TW_cc_DELE, TW_cc_DELW, /* ^T ^U ^V ^W */
193 TW_cc_NONE, TW_cc_QUIT, TW_cc_EOF, TW_cc_NONE, /* ^X ^Y ^Z ^[ */
194 TW_cc_RAW, TW_cc_NONE, TW_cc_MODE, TW_cc_NONE, /* ^\ ^] ^^ ^_ */
195 TW_cc_DELC}, /* Delete */
196 { TW_cc_NONE, TW_cc_MODE, TW_cc_LEFT, TW_cc_INT, /* Unix */
197 TW_cc_EOF, TW_cc_EOL, TW_cc_RIGHT,TW_cc_NONE, /* ^D ^E ^F ^G */
198 TW_cc_DELC, TW_cc_NW, TW_cc_RET, TW_cc_HELP, /* ^H ^I ^J ^K */
199 TW_cc_HOME, TW_cc_RET, TW_cc_DOWN, TW_cc_NONE, /* ^L ^M ^N ^O */
200 TW_cc_UP, TW_cc_NONE, TW_cc_REFR, TW_cc_NONE, /* ^P ^Q ^R ^S */
201 TW_cc_NONE, TW_cc_DELL, TW_cc_RAW, TW_cc_DELW, /* ^T ^U ^V ^W */
202 TW_cc_DELE, TW_cc_NONE, TW_cc_NONE, TW_cc_NONE, /* ^X ^Y ^Z ^[ */
203 TW_cc_QUIT, TW_cc_NONE, TW_cc_MODE, TW_cc_NONE, /* ^\ ^] ^^ ^_ */
204 TW_cc_DELC}, /* Delete */
205 { TW_cc_NONE, TW_cc_NONE, TW_cc_NONE, TW_cc_NONE, /* Socket */
206 TW_cc_EOF, TW_cc_EOL, TW_cc_REFR, TW_cc_NONE, /* ^D ^E ^F ^G */
207 TW_cc_HELP, TW_cc_NONE, TW_cc_RET , TW_cc_HELP, /* ^H ^I ^J ^K */
208 TW_cc_RET, TW_cc_RET, TW_cc_NONE, TW_cc_NONE, /* ^L ^M ^N ^O */
209 TW_cc_NONE, TW_cc_NONE, TW_cc_NONE, TW_cc_NONE, /* ^P ^Q ^R ^S */
210 TW_cc_NONE, TW_cc_NONE, TW_cc_RAW, TW_cc_NONE, /* ^T ^U ^V ^W */
211 TW_cc_NONE, TW_cc_NONE, TW_cc_EOF, TW_cc_NONE, /* ^X ^Y ^Z ^[ */
212 TW_cc_NONE, TW_cc_NONE, TW_cc_MODE, TW_cc_NONE, /* ^\ ^] ^^ ^_ */
213 TW_cc_DELC}, /* Delete */
214 };
215
216 /* Macros just to improve readibility */
217
218 #define SEND(p) tv_send(p,1)
219 #define curl (terms->pos[0])
220 #define curc (terms->pos[1])
221 #define diml (terms->dim[0])
222 #define dimc (terms->dim[1])
223 #define oldl old_pos[0]
224 #define oldc old_pos[1]
225 #define KEYPAD (terms->flags & TERM_KPAD)
226
227 #define TTattr terms->tt_attr
228 #define Vn 0
229 #define VB 1
230 #define Vu 2
231 #define Vb 3
232 #define Vr 4
233 #define Vg 5
234 #define Va 6
235
236 /* MONITOR(TVINIT); */ /* CG. static is defined locally */
237
238
239 /*==========================================================================
240 * tv_setcc
241 *==========================================================================*/
242 int tv_setcc(c, action)
243 /*+++++++
244 .PURPOSE Define the meaning of Control Characters for TermWindow Usage.
245 .RETURNS Actual action of cc (differs if could not be changed)
246 .REMARKS If cc is alphabetic, it's converted to control character.
247 ----------*/
248 int c; /* IN: The control character */
249 int action; /* IN: Action TW_cc_xxx as defined in twdef.h */
250 {
251 register char *p;
252 register int k;
253 static char cc;
254
255 ENTER("+tv_setcc");
256
257 p = NULL_PTR(char);
258 cc = (iscntrl(c) ? c : Control_(c));
259 k = (cc < 32 ? cc : 32); /* Position in terms->tc */
260 switch(action)
261 { case TW_cc_INT: p = &(tst.cc_INT); break;
262 case TW_cc_QUIT: p = &(tst.cc_QUIT); break;
263 default: if ((action < 0) || (action > TW_cc_max))
264 FINISH;
265 }
266
267 /* Check it's not an escape sequence */
268 if (terms->specials & MASK(k)) FINISH;
269
270 if (p) /* Must reset terminal... */
271 { if (ostinfo(&tst)) FINISH; /* Be sure interrupt chars are right */
272 if (*p == cc) { terms->tc[k] = action; FINISH;} /* No change */
273 *p = cc; /* Change it .. */
274 ostset(&tst); /* Change terminal ... */
275 ostinfo(&tst); /* Verify it's done */
276 if (*p != cc) terms->standout |= ChangedControl,
277 terms->tc[(*p < 32 ? *p : 32)] = action;
278 }
279 if (terms->tc[k] != action) terms->standout |= ChangedControl;
280 terms->tc[k] = action;
281
282 FIN:
283 k = terms->tc[k];
284 EXIT(k);
285 }
286
287 int tv_setsc(option)
288 /*+++++++
289 .PURPOSE Define the meaning of Control Characters for TermWindow Usage.
290 .RETURNS Previous definitions
291 .REMARKS
292 ----------*/
293 int option; /* IN: TW_cc_NONE (no control mapping) / TW_cc_VMS / \
294 TW_cc_UNIX / TW_cc_SET */
295 {
296 int old_option, i, k;
297 char cc;
298
299 ENTER("+tv_setsc");
300
301 old_option = (terms->flags & TERM_NOMAP ? TW_cc_NONE : TW_cc_SET);
302 k = -1;
303 switch(option) {
304 case TW_cc_NONE:
305 terms->flags |= TERM_NOMAP; break;
306 case TW_cc_SET:
307 terms->flags &= ~TERM_NOMAP; break;
308 case -1: k = TW_cc_default; break;
309 case TW_cc_USER: k = 0; break;
310 case TW_cc_VMS: case TW_cc_UNIX:
311 k = option; break;
312 case TW_cc_SOCKET:
313 k = option-1; break;
314 }
315
316 if (k >= 0) {
317 for (i = 32, cc = DEL; i >= 0; cc = --i)
318 tv_setcc(cc, ccdef[k][i]);
319 terms->flags &= ~TERM_NOMAP;
320 }
321
322 EXIT(old_option);
323 }
324
325 /*==========================================================================
326 * tv_getcc
327 *==========================================================================*/
328 int tv_getcc(action)
329 /*+++++++
330 .PURPOSE Retrieve the Control Character corresponding to a TermWindow Action.
331 .RETURNS The Control Character corresponding to the action (-1 for None)
332 .REMARKS If several control characters are mapped to an action,
333 the first encountered is returned.
334 ----------*/
335 int action; /* IN: Action TW_cc_xxx as defined in twdef.h */
336 {
337 int i;
338
339 for (i = 0; i < sizeof(terms->tc); i++)
340 if (terms->tc[i] == action) break;
341
342 if (i == sizeof(terms->tc) - 1) i = DEL;
343 else if (i >= sizeof(terms->tc)) i = -1;
344
345 return(i);
346 }
347
348 /*==========================================================================
349 * tv_aload
350 *==========================================================================*/
351 static int tv_aload()
352 /*+++
353 .PURPOSE Load attributes and graphic chars in the TERM structure from
354 TERMCAPabilities.
355 .RETURNS OK
356 .REMARKS Some termcap files use `me' `mb' `md' `mr' for
357 exit mode, blink, bold, reverse respectively.
358 ----------*/
359 {
360 register char *p, *pso;
361 register int i;
362 int k;
363 MID_RSTATIC char gcap[] = "GvGhG0G1G2G3G4GuGdGlGrGg";
364 MID_RSTATIC char video[] = "VnmeVBmdVuusVbmbVrmr";/* Video attr. */
365
366
367 /* If TTattr not yet loaded, do it now
368 * Note that no Stand-Out mode implies Overstrike or ChangeCase */
369
370 if(terms->standout&7) return(OK); /* Already done */
371
372 k = 1; /* Last 3 bits of terms->standout */
373
374 /* Load the defaults graphic characters */
375 terms->gchars[_VERTICAL_] = '|';
376 terms->gchars[_HORIZONTAL_] = '-';
377 for (i = ITEMS(terms->gchars); --i>=2; )
378 terms->gchars[i] = '+';
379 terms->gchars[_RUBBISH_] = '~';
380
381 pso = SearchCap("so"); /* Default stand-out mode */
382
383 p = SearchCap("Vg"); /* Graphic */
384 /* Some Termcap files use "as" */
385 if (!p) p = SearchCap("as");
386 TTattr[Vg] = p;
387
388 /* Load now the graphic chars */
389 for (i=0; i < ITEMS(terms->gchars); i++)
390 { p = SearchCap(&gcap[i*2]);
391 if (p) terms->gchars[i] = *(p+4);
392 }
393
394 TRACE_ED_STR2("Graphic chars: ", terms->gchars, sizeof(terms->gchars));
395
396 p = SearchCap("Va"); /* ASCII */
397 /* Some Termcap files use "ae" */
398 if (!p) p = SearchCap("ae");
399 TTattr[Va] = p;
400
401 /* Load now Video Attributes */
402
403 for (i = 0; i < sizeof(video)/4; i++)
404 { p = SearchCap(&video[i*4]); /* V. */
405 if (!p) p = SearchCap(&video[i*4+2]); /* m. */
406 if (!p) p = pso;
407 TTattr[i] = p;
408 TRACE_ED_STR2("TTattr is: ", TTattr[i], 12);
409 }
410
411 p = TTattr[Vn]; /* Normal */
412 if ( (!p) || (p == pso) )
413 p = SearchCap("se");
414 TTattr[Vn] = p;
415
416 if (!p) /* No existing attribute */
417 k = (SearchCap("os") ?
418 2: /* Use Overstrike */
419 3); /* Use Case Change */
420
421 terms->standout |= k;
422 return(OK);
423 }
424
425 /*==========================================================================
426 * tv_close
427 *==========================================================================*/
428 int tv_close ()
429 /*+++
430 .PURPOSE Close the opened terminal
431 .RETURNS OK
432 .REMARKS To be called at the end of program using tv_... functions.
433 ---*/
434 {
435 register char *p;
436
437 ENTER("tv_close");
438
439 if(!terms->version) FINISH; /* Was not opened ! */
440 if (terms->term_type) FINISH; /* Is NOT a terminal! */
441
442 tv_where(old_pos);
443 /* Reset the scrolling */
444 tv_attr(terms->attr_init);
445 tv_sr0();
446 tv_goto(oldl,oldc);
447
448 /* Go to newline if cursor not positioned at the left margin */
449 if (curc) tv_nl();
450
451 /* Reset Automatic Margins */
452
453 if (SearchCap("am"))
454 { p = SearchCap(".M");
455 if (p) SEND(p);
456 }
457
458 /* Clear the lower part of the screen */
459
460 if(!(terms->term_type & TERM_hard)) tv_clear(_DOWN_);
461
462 /* Reset the original attributes */
463 tv_attr(terms->attr_init);
464
465 /* Reset the terminal to init --- use `rs' capability */
466 SendCap("te"); /* Undo ti */
467 SendCap("rs"); /* Reset */
468
469 #if MID_OS_VMS
470 tv_wms(Closing_Wait); /* Wait a bit ... */
471 #endif
472
473 ostclose();
474
475 FIN: terms->version = 0;
476 EXIT(OK);
477 }
478
479 /*==========================================================================
480 * tv_gterm
481 *==========================================================================*/
482 char *tv_gterm()
483 /*+++
484 .PURPOSE Retrieve terminal name
485 .RETURNS Pointer to terminal name / NULL if not opened
486 .REMARKS
487 ---*/
488 {
489 tv_open(NULL_PTR(char), NULL_PTR(char), -1); /* Default Open */
490
491 return(terms->term_name);
492 }
493
494 /*==========================================================================
495 * tv_kpad
496 *==========================================================================*/
497 static int tv_kpad (k)
498 /*+++
499 .PURPOSE Enable or disables the keypad.
500 .RETURNS OK if possible, NOK if not possible
501 .REMARKS
502 ---*/
503 int k; /* IN: non-zero to enable, zero to disable */
504 {
505 register char *p;
506 register int s;
507
508 ENTER("tv_kpad");
509
510 s = OK;
511 if(k) /* Trying to enable ... */
512 { if (KEYPAD) FINISH;
513 p = SearchCap("ks");
514 if (p) terms->flags |= TERM_KPAD;
515 }
516 else /* Trying to disable .. */
517 { if (!KEYPAD) FINISH;
518 p = SearchCap("ke");
519 terms->flags &= (~TERM_KPAD);
520 }
521 if (!p) s = NOK;
522 else SEND(p);
523
524 FIN:
525 EXIT(s);
526 }
527
528 /*==========================================================================
529 * tv_init
530 *==========================================================================*/
531 int tv_reset()
532 /*+++
533 .PURPOSE Reset the Terminal: Get new number of lines/cols.
534 .RETURNS OK / NOK
535 .REMARKS
536 ---*/
537 {
538 short cur_pos[2];
539
540 cur_pos[0] = terms->pos[0]; cur_pos[1] = terms->pos[1];
541
542 if (ostinfo(&tst)) return(NOK); /* Get Characteristics */
543 if (tst.lines <= 0) return(NOK); /* Bad lines count */
544
545 if (tst.columns > 0)
546 terms->dim[1] = terms->dim_init[1] = tst.columns;
547
548 if ((tst.lines > 0) && (tst.lines != terms->dim_init[0])) {
549 terms->dim[0] = terms->dim_init[0] = tst.lines;
550 tv_sr0(); /* Reset scrolling region */
551 }
552 tv_goto (cur_pos[0], cur_pos[1]);
553 return(OK);
554 }
555
556 static int resize ()
557 /*+++
558 .PURPOSE Function called when window resized.
559 .RETURNS OK / NOK
560 ----*/
561 {
562 ostwinch(resize); /* Reset function for next Window Resize */
563 return(tv_reset());
564 }
565
566
567 static int tv_init (opt)
568 /*+++
569 .PURPOSE Initializes the terminal (number of lines, col, etc)
570 .RETURNS OK / NOK
571 .REMARKS List of capabilities is assumed to be loaded in the Minimal Version.
572 If the terminal has automatic margin (am), number of
573 columns is decremented.
574 ---*/
575 int opt; /* IN: Option for control characters */
576 {
577 register char *p;
578 register int i;
579 MID_RSTATIC char motions[] = "updobcndnlcm" ; /* Cursor seq. */
580 MID_RSTATIC char swidth[] = "lico" ; /* Screen dim. */
581 MID_RSTATIC char no_cap[] = "";
582
583 ENTER("tv_init");
584
585 terms->baud = MAX(tst.baud_rate, 20000);
586 terms->attr_init = 0;
587 terms->attr = 0;
588 terms->dim_init[0] = tst.lines; /* Default number of lines */
589 terms->dim_init[1] = tst.columns; /* Default number of columns */
590 terms->pos[0] = tst.lines-1; /* Default position */
591 terms->pos[1] = 0; /* Default position */
592 terms->scregion[0] = 0; /* Default top scrolling region */
593 terms->scregion[1] = 0; /* Default top scrolling region */
594 terms->flags = 0; /* No flag */
595 terms->pad_char = 0; /* Default padding character */
596 terms->standout = 0; /* Unloaded attribute sequences */
597 terms->ir = 0; /* Read buffer */
598 terms->irtop = 0; /* Read buffer */
599 terms->iw = 0; /* Write buffer */
600 terms->last_char = 0;
601 terms->last_type = 0;
602
603 /* Number of lines / columns */
604 for (i = 0; i < 2; i++)
605 { if (terms->dim_init[i]) continue;
606 p = SearchCap(&swidth[i*2]);
607 if (p) terms->dim_init[i] = atoi(p+4);
608 }
609
610 /* Padding char, if known */
611 p = SearchCap("pc");
612 if (p) terms->pad_char = *(p+4);
613
614 /* Set the control characters */
615 if (terms->term_type == 0) {
616 if (ostopen() < 0)
617 opt = TW_cc_SOCKET, ERROR(osmsg());
618 tv_setsc(opt);
619 }
620
621 /* Determine basic motions sequences */
622
623 for (i = 0; i < ITEMS(terms->tt_ptr); i++)
624 {
625 p = SearchCap(&motions[i*2]);
626 if_not(p) p = no_cap;
627 terms->tt_ptr[i] = p;
628 }
629
630 /* Check the NewLine: can't be only \n */
631
632 p = terms->tt_ptr[_NEWLINE_];
633 if ( (*p == '\01') && (*(p+4) == '\n'))
634 terms->tt_ptr[_NEWLINE_] = "\02nl=\r\n";
635
636 if (*terms->tt_ptr[_LEFT_] == 0) /* No left motion, try bs */
637 if_not (SearchCap("bs"))
638 terms->tt_ptr[_LEFT_] = SearchCap("_H");
639
640
641 #if (TW_LEVEL == 0) /* Test all cursor movements exist */
642 for (i = 0; i < ITEMS(terms->tt_ptr); i++)
643 { if(*(terms->tt_ptr[i]) == 0)
644 { ERR_ED_STR2("Missing Capability: ", terms->tt_ptr[i]+1, 2);
645 EXIT(NOK);
646 }
647 }
648 #else /* If no up capability, set terminal to `hard' */
649 if(*(terms->tt_ptr[_UP_]) == 0)
650 terms->flags |= TERM_hard;
651 #endif
652
653 /* Clear Capability */
654 p = SearchCap("cl");
655 if (!p) p = SearchCap("ff");
656 terms->tt_clear = p;
657
658 /* Get Graphic Chars and Change Attribute sequences */
659 tv_aload();
660
661 /* Send init sequence if necessary */
662 p = SearchCap("is");
663 if (p) SEND(p);
664 else /* Some TERMCAP files have i1, i2, i3 cap's */
665 { SendCap("i1");
666 SendCap("i2");
667 SendCap("i3");
668 }
669 SendCap("ti"); /* Special Initialisation */
670
671 /* Look for automatic margins. If possible to disable, do it. */
672
673 if (SearchCap("am"))
674 { p = SearchCap(".m");
675 if (p) SEND(p), (terms->flags) |= TERM_am;
676 else (terms->dim_init[1])--;
677 }
678
679 terms->dim[0] = terms->dim_init[0];
680 terms->dim[1] = terms->dim_init[1];
681
682 /* Set the default attributes and char set */
683 SEND(terms->tt_attr[Vn]);
684 SEND(terms->tt_attr[Va]);
685
686 /* Enable keypad mode */
687 tv_kpad(1);
688
689 /* Set the scrolling region to whole screen ... (superseedes init) */
690 tv_sr0();
691
692 EXIT(OK);
693 }
694
695 /*==========================================================================
696 * tv_open
697 *==========================================================================*/
698 int tv_open (device, term_file, option)
699 /*+++++
700 .PURPOSE Open a specified device, reading specifications in the term_file
701 (list of TERMCAPabilities)
702 terminal descriptions.
703 .RETURNS OK / NOK
704 .REMARKS In Minimal Implementation, Binary file MUST exist,
705 and No batch possibilities.
706 The option allows a choice of control characters, as TW_cc_VMS,
707 TW_cc_UNIX or TW_cc_USER. The control characters to use
708 may be set BEFORE calling tv_open.
709 \begin{TeX}
710
711 The actions of the Control Characters are defined as follows:
712 $$\begin{tabular}{|l|ccc|l|}
713 \hline
714 Mnemonic & User & VMS & UNIX & Action \\
715 \hline
716 {\tt TW\_cc\_INT} & \^{}C & \^{}C & {\em Del.} & Interrupt \\
717 {\tt TW\_cc\_QUIT} & \^{}Y & \^{}Y & \^{}\b & Quit (Stop) \\
718 {\tt TW\_cc\_RET} & \^{}M & \^{}M & \^{}M & Return Key \\
719 {\tt TW\_cc\_EOF} & \^{}Z & \^{}Z & \^{}D & End of File character \\
720 {\tt TW\_cc\_RAW} & \^{}\b & \^{}\b & \^{}V & No interpretation of next
721 character \\
722 {\tt TW\_cc\_MODE} & \^{}A & \^{}A & \^{}A & Switch Insert / Replace Modes \\
723 {\tt TW\_cc\_REFRESH} & \^{}F & \^{}R & \^{}R & Refresh Screen \\
724 {\tt TW\_cc\_HELP} & \^{}H & \^{}G & \^{}K & Help Window \\
725 {\tt TW\_cc\_UP} & \^{}U & \^{}B & \^{}P & Move Up \\
726 {\tt TW\_cc\_DOWN} & \^{}D & \^{}N & \^{}N & Move Down \\
727 {\tt TW\_cc\_LEFT} & \^{}L & \^{}D & \^{}B & Move Left (Backwards) \\
728 {\tt TW\_cc\_RIGHT} & \^{}R & \^{}F & \^{}F & Move Right (Forwards) \\
729 {\tt TW\_cc\_HOME} & \^{}B & \^{}H & \^{}L & Move to Left Margin \\
730 {\tt TW\_cc\_EOL} & \^{}E & \^{}E & \^{}E & Move to End of Line \\
731 {\tt TW\_cc\_NW} & \^{}I & \^{}I & \^{}I & Move to Next Word \\
732 {\tt TW\_cc\_DELC} & {\em Del.} & {\em Del.}& \^{}H & Delete
733 character at left of cursor \\
734 {\tt TW\_cc\_DELW} & \^{}W & \^{}W & \^{}W & Delete Word at Left of
735 cursor\\
736 {\tt TW\_cc\_DELE} & \^{}J & \^{}J & \^{}X & Delete to End of Line \\
737 {\tt TW\_cc\_DELL} & \^{}X & \^{}X & \^{}U & Delete Complete Line \\
738 \hline
739 \end{tabular}$$
740 \end{TeX}
741 -----*/
742 char *device; /* IN: The type of device; NULL gets from environment */
743 char *term_file;/* IN: The Binary file; from device by default */
744 int option; /* IN: Option for control characters */
745 {
746 register char *p, *p1, *p2;
747 static int bfile;
748 #if (TW_LEVEL > 0) /* Full Implementation */
749 TERM *term2;
750 long int t;
751 int status;
752 MID_STATIC char binput = 0;
753 MID_STATIC char *termcapfile = NULL;
754 #endif
755
756 ENTER("tv_open");
757
758 if (sterm.version == TW_VERSION) EXIT(OK); /* Already done */
759
760 #if (TW_LEVEL > 0) /* Full Implementation */
761
762 /* Define first the TermCap filename, using defaults,
763 * and retrieve date of creation in case of modifs */
764
765 termcapfile = (term_file ? term_file : TERMCAPFILE);
766 if (!termcapfile) termcapfile = oshenv("TERMCAP",NULL_PTR(char));
767 if (!termcapfile)
768 #if MID_OS_MSDOS
769 termcapfile = "\\termcap.dat";
770 #else
771 { ERROR("TERMCAPFILE not defined"); goto BAD; }
772 #endif
773 t = osfdate(termcapfile);
774 #endif
775 /* Get first Terminal Status.
776 * Note that, if no terminal is associated with the process
777 * (eg batch run, indicated by a bad return code
778 * in ostinfo), stdin/stdout are assumed */
779
780 if (ostinfo(&tst) < 0) /* No terminal... */
781 terms->term_type = 1, /* Indicates NOT a terminal */
782 ERROR(osmsg());
783 else terms->term_type = 0; /* Indicates a true terminal */
784 terms->version = TW_VERSION; /* Corrected 26-Feb-1991 */
785 terms->term_name = tst.termname;
786
787 p = (device ? device : terms->term_name); /* Terminal name */
788 /* if (!(*p)) p = terms->term_name; */
789 if (!p) p = DEFAULT_TERMINAL;
790 /* if (!(*p)) p = DEFAULT_TERMINAL; */
791
792 TRACE_ED_STRING("Terminal id: ", p);
793
794 /* Copy terminal name and termcapfile to a new buffer */
795
796 if (!DIR_TTY) DIR_TTY = oshenv("MID_WORK", (char *)0);
797 if (!DIR_TTY) DIR_TTY = oshenv("HOME", (char *)0);
798
799 terms->term_name = MEM_GET(char, 2*strlen(p) + strlen(DIR_TTY) +
800 1 + strlen(termcapfile) +
801 sizeof(DIR_TTY_TRAILER) + sizeof(EXT_TTY));
802 for (p2 = terms->term_name, p1 = p; *p1; p1++, p2++)
803 #if MID_OS_VMS /* Convert to lower case for VAX... */
804 *p2 = tolower(*p1);
805 #else
806 *p2 = *p1;
807 #endif
808 *p2 = EOS;
809 terms->save_file = ++p2;
810
811 /* Transform device name to file name */
812 for (p1 = DIR_TTY; *p1; p1++, p2++) *p2 = *p1;
813 for (p1 = DIR_TTY_TRAILER; *p1; p1++, p2++) *p2 = *p1;
814 for (p1 = p; *p1; p1++)
815 #if MID_OS_VMS /* VMS doesn't like hyphens in filenames... */
816 if (isalnum(*p1))
817 #endif
818 *(p2++) = *p1;
819
820 for (p1 = EXT_TTY; *p1; p1++, p2++) *p2 = *p1;
821 *p2 = EOS;
822 p1 = termcapfile, termcapfile = ++p2;
823 for (; *p1; p1++, p2++) *p2 = *p1;
824 *p2 = EOS;
825
826 #if (TW_LEVEL > 0) /* ------------- F U L L Implementation ----- */
827 /* Look if a binary file with loaded capabilities
828 * already exists; but don't use it if its date
829 * is earlier than revision date of TermCap file!
830 * *** Note that, if TermCap file doesn't exist,
831 * the check will always be OK. */
832
833 bfile = 0;
834 if (osfdate(terms->save_file) > t)
835 bfile = osdopen(terms->save_file, READ);
836 binput = (bfile > 0);
837
838 if (binput)
839 { term2 = MEM_GET(TERM,1);
840 *term2 = *terms; /* Save */
841 status = osdread(bfile, (char *)terms, sizeof(TERM));
842 TRACE_ED_STR2("Read TERM: ", (char *)terms, sizeof(TERM));
843 if ( (status != sizeof(TERM)) || (terms->version != TW_VERSION) )
844 {
845 binput = 0;
846 *terms = *term2;
847 MEM_FREE(term2);
848 }
849 }
850
851 if (binput)
852 { terms->save_file = term2->save_file;
853 terms->term_name = term2->term_name;
854 MEM_FREE(term2);
855 terms->caplist = MEM_GET(char,terms->size);
856 status = osdread(bfile, terms->caplist, terms->size);
857 TRACE_ED_STR2("Read capl: ", terms->caplist, status);
858 if (status != terms->size) binput = 0;
859 }
860 if (bfile>0) osdclose(bfile);
861
862 /* If no binary input, acquire the terminal
863 * capabilities directly from TermCapFile */
864 if (!binput)
865 { eh_class(_WARNING_); /* Next message is a Warning */
866 ERR_ED_STRING("Obsolete version (recreated): ", terms->save_file);
867 ERR_CLEAR();
868 bfile = osdopen(terms->save_file, WRITE);
869 status = tu_load(termcapfile, terms->term_name, terms);
870 #ifdef local_termcapfile
871 if (status != OK)
872 status = tu_load(local_termcapfile, terms->term_name, terms);
873 #endif
874 if (status != OK) {
875 if (GetLog() > 2) /* There is a Log File. Issue also the message to
876 the terminal, to let the user be aware of the problem... */
877
878 { p = ERR_LOC(); /* Retrieve Error ... */
879 ostwrite("**** ", 5); /* and display */
880 ostwrite(p, strlen(p)); /* and display */
881 ostwrite("\r\n", 2);
882 ospwait(4); /* Just wait, to give some time to read
883 the so important above message ... */
884 }
885 goto BAD;
886 }
887
888 /* Copy structure to the binary file */
889 terms->size = terms->captop; /* Limit the size */
890 TRACE_ED_STR2("Writ TERM: ", (char *)terms, sizeof(TERM));
891 osdwrite(bfile, (char *)terms, sizeof(TERM));
892 TRACE_ED_STR2("Writ capl: ", terms->caplist, terms->size);
893 osdwrite(bfile, terms->caplist, terms->size);
894 osdclose(bfile);
895 }
896 #else /* ---------- Minimal Implementation -------- */
897 /* Load the binary file with loaded capabilities */
898
899 p2 = (term_file ? term_file : terms->save_file);
900 bfile = osdopen(p2, READ);
901 if (bfile <= 0)
902 { ERR_ED_STRING("Can't find ", p2);
903 goto BAD;
904 }
905 p1 = terms->term_name; /* Save */
906 status = osdread(bfile, terms, sizeof(TERM));
907 if ( (status != sizeof(TERM)) || (terms->version != TW_VERSION) )
908 { ERR_ED_STRING("Bad Version of ", p2);
909 goto BAD;
910 }
911 terms->term_name = p1; /* Reset addresses */
912 terms->save_file = p1 + strlen(p1) + 1;
913
914 /* Read the CapList */
915 if (!(terms->caplist = MEM_GET(char,terms->size))) goto BAD;
916 if (osdread(bfile, terms->caplist, terms->size) != terms->size)
917 { ERR_ED_STRING("Bad ", p2);
918 goto BAD;
919 }
920 osdclose(bfile);
921 #endif
922
923 terms->buf_size = TVBUF;
924 if (!(terms->bufr = MEM_GET(char, 3*TVBUF))) goto BAD;
925 terms->bufw = terms->bufr + terms->buf_size;
926 terms->bufs = terms->bufw + terms->buf_size;
927
928 /* Initialize the terminal */
929
930 if (tv_init(option) != OK) goto BAD;
931 ostwinch(resize); /* Set function for Window Resize */
932
933 status = OK;
934 FINISH;
935
936 BAD:
937 status = NOK, ostclose();
938
939 FIN:
940 EXIT(status);
941 }
942
943 /*==========================================================================
944 * tv_stop
945 *==========================================================================*/
946 int tv_stop (stop)
947 /*+++
948 .PURPOSE Stop (stop!=0) or Start (stop==0) the terminal
949 .RETURNS The previous state, i.e. 0 if terminal was active, not-zero
950 if terminal was already stopped.
951 (always 0 in Minimal Implementation)
952 .REMARKS What is in the buffer is NOT sent to the terminal (immediate stop)
953 ---*/
954 int stop; /* IN: 0 to start, 1 to stop */
955
956 {
957 #if (TW_LEVEL == 0)
958 return(0);
959 #else
960 register int returned;
961
962 ENTER("tv_stop");
963
964 returned = (terms->flags) & (TERM_NOWRITE);
965
966 if(stop) terms->flags |= TERM_NOWRITE;
967 else terms->flags &= ~(TERM_NOWRITE);
968
969 EXIT(returned);
970 #endif
971 }
972
973 /*==========================================================================
974 * tv_wait
975 *==========================================================================*/
976 int tv_wait (ms)
977 /*+++
978 .PURPOSE Set a waiting time (ms) after each output to screen.
979 .RETURNS The previous waiting time (always 0 in Minimal Implementation)
980 .REMARKS Useful to have an idea about the speed of a program
981 as seen from a remote terminal...
982 The waiting time is limited to 30000 (0.5 mn).
983 ---*/
984 int ms; /* IN: milliseconds to wait */
985 {
986 #if (TW_LEVEL == 0)
987 return(0);
988 #else
989 register int old_ms;
990
991 ENTER("+tv_wait");
992
993 if (terms)
994 old_ms = terms->msw,
995 terms->msw = (ms < 30000 ? ms : 30000);
996 else old_ms = 0;
997
998 EXIT(old_ms);
999 #endif
1000 }
1001
1002 /*==========================================================================
1003 * tv_dim
1004 *==========================================================================*/
1005 int tv_dim(coo)
1006 /*+++++++++++
1007 .PURPOSE Returns the current screen dimensions
1008 .RETURNS OK
1009 .REMARKS The position is adjusted within the screen limits.
1010 Not traced.
1011 ------------*/
1012 short int coo[2]; /* OUT: screen dimensions: line (0=top), col (0=left) */
1013 {
1014 tv_open(NULL_PTR(char), NULL_PTR(char), -1); /* Default Open */
1015
1016 coo[0] = terms->dim[0];
1017 coo[1] = terms->dim[1];
1018 return(OK);
1019 }
1020
1021 #if 0
1022 /*==========================================================================
1023 * tv_width
1024 *==========================================================================*/
1025 int tv_width (width)
1026 /*+++
1027 .PURPOSE Set the width (number of columns) to a specified value
1028 .RETURNS OK (was done, new dimension loaded), NOK (not possible, always
1029 in Minimal Implementation)
1030 .REMARKS Only enlarge or reduce width. Enlarge is `.S' with width `.W#',
1031 reduce is `.s' with width `.w#'
1032 * Note that, in case of output to a file, the width may always
1033 be changed!
1034 ---*/
1035 int width; /* IN: New column width */
1036 {
1037 #if (TW_LEVEL < 2)
1038 return(NOK);
1039 #else
1040 register char *p, *pw;
1041
1042 ENTER("tv_width");
1043
1044 TRACE_ED_I("Present Width: ",terms->dim[1]);
1045 TRACE_ED_I("Asked Width: ",width);
1046
1047 if ( (width == terms->dim[1]) || (terms->term_type) )
1048 { terms->dim[1] = width;
1049 EXIT(OK);
1050 }
1051
1052 status = OK;
1053 if (width > terms->dim[1]) p = ".S", pw = ".W"; /* Enlarge */
1054 else p = ".s", pw = ".w"; /* Reduce */
1055
1056 p = SearchCap(p), pw = SearchCap(pw);
1057 if ((p) && (pw)) SEND(p), terms->dim[1] = atoi(pw+4);
1058 else status = NOK;
1059
1060 EXIT(status);
1061 #endif
1062 }
1063 #endif
1064
1065 /*==========================================================================
1066 * Internal Routines
1067 *==========================================================================*/
1068
1069 #if DEBUG
1070 /*==========================================================================
1071 * tv_ed
1072 *==========================================================================*/
1073 int tv_ed ()
1074 /*+++
1075 .PURPOSE Edit the terminal characteristics on the log file.
1076 .RETURNS OK
1077 .REMARKS Only used for debugging.
1078 ---*/
1079 {
1080 register char *p, *pw;
1081 MID_STATIC char msg[80] = "";
1082
1083 sprintf(msg,"....Device %s[%d] of dim. [%d,%d] -- Cursor at [%d,%d]",
1084 terms->term_name, terms->term_type,
1085 terms->dim[0], terms->dim[1],
1086 terms->pos[0], terms->pos[1]);
1087 LOG(msg);
1088 sprintf(msg,".... attr=%X, flags=%X",
1089 terms->attr, terms->flags);
1090 LOG(msg);
1091 return(OK);
1092 }
1093 #endif