Debian Codegraph @

libsrc/tw/termcap.c

raw
1 /* @(#)termcap.c 19.1 (ES0-DMD) 02/25/03 13:58:13 */
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 termcap.c
33 .AUTHOR Francois Ochsenbein [ESO-IPG]
34 .LANGUAGE C
35 .KEYWORDS Termcap
36 .ENVIRONMENT TermWindows
37 .VERSION 1.0 01-Jul-1986: Creation
38 .VERSION 2.0 10-Jun-1987:
39 For version '2', only functions required to CREATE the
40 TERMCAP Area are here.
41 The characters may be 8 bits.
42 .VERSION 2.2 23-Jun-1987: Adapted UNIX
43 .VERSION 2.3 23-Nov-1987: Removed Bug which occured in tu_x1,
44 and caused crashes for some terminals (due to reallocation
45 of memory)
46 .VERSION 3.0 15-May-1988: Version '3'. Use stritem function to locate
47 the device name in the list.
48 .VERSION 3.1 28-Mar-1989: Removed bug in stritem (if 2nd string is of length 0)
49 .VERSION 3.2 09-May-1989: Modified f_gets to eliminate bad messages in
50 case of unknown terminal.
51 .VERSION 3.3 14-Jul-1989: Modified error logging in tu_load.
52 .VERSION 3.4 24-Aug-1989: Don't modify term structure when terminal not
53 found (may be looked in a new file)
54
55 .COMMENTS The main program loads the TermCapFile into a binary
56 structure in SYS$LOGIN.
57
58 This module provides tools to read the TERMCAP file and to
59 prepare a TERM structure (defined in TERMINAL.H) which
60 receives the TermCapabilities in a form usable by ZT Libraries.
61 For usage in TermWindows, this module is NOT required
62 if it is assumed that a binary copy of the TERM already exists.
63
64 \begin{TeX}
65
66 \bigskip
67 \bigskip
68
69 {\bf Description of TERMCAPS}
70
71
72 TERMCAP is the file EMACS uses to acquire the terminal specific
73 commands and escape sequences for updating the display. This file is
74 an ASCII text file that can be modified with any text editor.
75
76 The TERMCAP method of updating a display was developed by William
77 Joy at the University of California at Berkeley for UNIX
78 \footnote{UNIX is a trademark of Bell Laboratories}
79 It is
80 employed by many UNIX programs, some of which run on VMS. Because
81 EMACS uses a nearly identical method of reading the TERMCAP file as
82 other programs, the same TERMCAP file can be shared between several
83 screen manipulation programs.
84
85 The TERMCAP file contains a series of entries that describe the
86 capabilities of various terminals. Each entry is composed of fields
87 separated by colons (`:'). There can be several lines per entry (by
88 placing a `\b' at the end of each line, except the last line of the
89 entry).
90
91 The first field is the name of the terminal to which the ensuing
92 fields apply. This field can specify several synonyms for the
93 terminal; the synonyms are separated by vertical bars (`$|$'). The
94 field, as with all fields, are terminated by a colon. Note that EMACS
95 distinguishes between upper and lower case alphabetics in this field
96 (and in all fields; hence the reason for quotes on the DEFINE
97 EMACS\_TERM command.)
98
99 The remainder of the fields are specified as two letter field
100 names. These may be followed by a field value. For instance:
101
102 \begin{quote}
103 {\tt up=\b Eu:}\\
104 {\tt li\#24:}\\
105 {\tt ul:}
106 \end{quote}
107
108 are all valid fields. The field name can be separated from the field
109 value by either a `=' (the equal-sign designates a string), or `\#'
110 (the sharp designates a number), or a `:' (the column designates
111 a booloean value).
112 {\em Note added by Fran\c cois Ochsenbein:}
113 The `@' instead of `=' or `\#' means that
114 {\em the capability does not exist.}
115
116 The two letter field names that EMACS uses are described in the
117 following table. Note that some TERMCAP entries may have more (which
118 are ignore by EMACS).
119
120 {\em Note added by Fran\c cois Ochsenbein:}
121 All capabilities are not described here; the
122 usage of various other capabilities are explained in each
123 module making use of TERMCAP.\\
124 Let's just mention here {\tt V}$x$ for video attributes,
125 {\tt G}$x$ for graphic characters,
126 {\tt F}$x$ for function keys,
127 {\tt k}$x$ for keypad keys,
128 and {\tt K}$x$ for special keys.
129
130
131 $$\begin{tabular}{|llp{33em}|} \hline
132 Field &Padding &Meaning \\
133 \hline
134 {\tt al=} &P* &Adds a new line to the screen, pushing
135 lines below it down.\\
136 {\tt am} & &Boolean value (automatic margin, \ie
137 cursor wraps from right margin to next line).\\
138 {\tt bc=} & &Moves the cursor left (typically
139 backspace or \^{ }H).\\
140 {\tt cb= } &P &Erase text from the left margin to the cursor.\\
141 {\tt cd=} &P &Erases text from the cursor to the
142 end of the display.\\
143 {\tt ce=} &P &Erases text from the cursor to the
144 rightmost column in the display.\\
145 {\tt cl=} &P* &Erases all text on all lines on the
146 display.\\
147 {\tt cm=} &P &Moves the cursor; see below.\\
148 {\tt co\#} & &Specifies the number of columns per
149 line.\\
150 {\tt cs= } &P &Defines a scrolling region.\\
151 {\tt cu= } &P &Erase text from the top of the display to the cursor.\\
152 {\tt dc= } &P* &Deletes the current character, moving
153 all characters at the right of the
154 deleted character left by one.\\
155 {\tt dl=} &P* &Deletes the current line, moving all
156 lines below it up by one.\\
157 {\tt dm=} & &Enters Delete Character mode.\\
158 {\tt do=} & &Moves the cursor down (line-feed on hard
159 terminals).\\
160 {\tt ed=} & &Exits Delete Character mode.\\
161 {\tt ei=} & &Exits Insert Character mode. (Each
162 character sent to the terminal will
163 replace the character under (or to the
164 right of) the cursor.\\
165 {\tt ho=} &P &Move the cursor to home position
166 ( the top left corner).\\
167 {\tt ic=} &P &Inserts one character space, moving all
168 characters to the right of the cursor
169 right by one space.\\
170 {\tt im=} &P &Enter Insert Character mode. (Each
171 character sent to the terminal will move
172 all characters to the right of the
173 cursor right by one.\\
174 {\tt ip=} &P* &Inserts padding after the character
175 being inserted.\\
176 {\tt is=} &P &Initialize the display.\\
177 {\tt ke=} & &Exit keypad mode (disable the keypad).\\
178 {\tt kn\#} & &Number of keypad keys.
179 Cursor arrows are {\tt kl=}(left),
180 {\tt kr=}(right), {\tt ku=}(up),
181 {\tt kd=}(down), {\tt kh=}(home).\\
182 {\tt ks=} & &Start keypad mode (enable the keypad).\\
183 {\tt li\#} & &Specifies the number of lines on the display.\\
184 {\tt nd=} & &Moves the cursor right by one character.\\
185 {\tt pc=} & &Definition of the padding char (NUL is default).\\
186 {\tt rc=} & &Restore saved cursor position and attributes.\\
187 {\tt sc=} & &Save cursor position and attributes.\\
188 {\tt se=} & &Exit {\sl stand-out} mode. (All subsequent
189 characters will appear normally.)\\
190 {\tt sf=} &P &Scroll forward (up) the display. The cursor
191 remains at the same position.\\
192 {\tt so=} & &Enter {\sl stand-out} mode. (All subsequent
193 characters will appear highlighted in
194 some fashion. This may be blinking,
195 bold, reverse video, ...)\\
196 {\tt sr=} &P &Scroll Reverse (down) the display.\\
197 {\tt tc=} & &This field means that sequences are
198 to be found with the name provided after the `='.\\
199 {\tt te=} & &This string is sent to the terminal when
200 EMACS exits or redraws the display.\\
201 {\tt ti=} & &This string is sent to the terminal when
202 EMACS starts up or redraws the display.\\
203 {\tt ue= } & &Exit {\sl underscore} mode (synonym is {\tt Vu}).\\
204 {\tt ul } & &Terminal underlines without
205 overstriking.\\
206 {\tt up=} &P* &Moves the cursor up one line,
207 maintaining the same column.\\
208 {\tt us= } & &Enter {\sl underscore} mode.\\
209 {\tt vb=} & &Flashes the screen (for a visible bell).\\
210 \hline
211 \end{tabular}$$
212
213 For each of the field names mentioned in the table
214 that end with `=', you should
215 include the sequence of characters that effect the behavior listed to
216 the right of the name. Fields with P or P* next to them can have a
217 time delay whenever the field is output (to permit terminals to do
218 internal processing). If a delay is needed then you may put a number
219 (indicating milliseconds) immediately to the right of the `=', but
220 before the first character of the string. Additionally, for `dc',
221 `dl', `al', `cl', `ip', and `up', you can put an `*' after the number
222 but before the first character of the string. The `*' states that the
223 delay is to be repeated for each time the string is sent to the
224 terminal. (For example: if EMACS needs to insert 3 blank lines, then
225 it will `execute the delay' between each of the issuances of the `al'
226 string.)
227
228
229 The string values for each name can have the following special
230 character sequences:
231
232 $$\begin{tabular}{|ll|} \hline
233
234 {\tt \b E } & ASCII Escape character.\\
235 {\tt \^{ }x } & The ASCII control character `x'.\\
236 {\tt \b nnn } & The ASCII character represented by octal nnn.\\
237 {\tt \b n } & The Control-J (linefeed) character.\\
238 {\tt \b r } & Control-M (Carriage-return).\\
239 {\tt \b t } & Tab (Control-I).\\
240 {\tt \b b } & Backspace (Control-H).\\
241 {\tt \b f } & Formfeed (Control-L).\\
242 {\tt \b\^\ } & A single caret (\^{ }).\\
243 {\tt \b\b } & A single backslash (\b).\\
244 {\tt \b: } & The column character (:).\\
245 \hline
246 \end{tabular}$$
247
248
249
250 Note: You must use \b072 or \b: to represent a colon (`:')
251 in a field.
252
253
254 The value of the `cm' field has a special interpretation. EMACS
255 treats it as a function that is called with two parameters: first the
256 line, and then the column that the cursor is to go to. Within the
257 string, the following character sequences have a special meaning:
258
259 $$\begin{tabular}{|lp{33em}|} \hline
260
261 {\tt \%d } & Replaced with the line or column number.\\
262 {\tt \%2 } & Replaced with the line or column number using a
263 two digit, zero filled field.\\
264 {\tt \%3 } & Replaced with the line or column number using a
265 three digit, zero filled field.\\
266 {\tt \%. } & Use the ASCII character corresponding to the line
267 or column number (line or column 1 would produce a
268 Control-A).\\
269 {\tt \%+x } & Use the ASCII character corresponding to the line
270 or column number plus the ASCII number for
271 character `x'.\\
272 {\tt \%>xy } & If the line or column number is greater than an
273 ASCII `x', then add an ASCII `y' to it. (`x' and
274 `y' are characters.)\\
275 {\tt \%r } & Output the column, then line. (Emacs defaults to
276 line and then column.)\\
277 {\tt \%i } & Add one to both the columns and line numbers
278 (Emacs defaults to zero biased lines and columns).\\
279 {\tt \%\% } & Output a single `\%'.\\
280 {\tt \%n } & Exclusive-or the line and column number with octal
281 140.\\
282 \hline
283 \end{tabular}$$
284
285
286 For example:
287
288 \begin{center}
289 {\tt cm=\b E[\%d;\%dm}
290 \end{center}
291
292 translates to ESCAPE, left bracket, the line number, a semicolon, the
293 column number, then a lowercase M.
294
295
296
297 {\bf \^{ }S And \^{ }Q Handling}
298
299 If your terminal (or communications link) uses \^{ }Q and \^{ }S
300 (X-on,
301 X-off) for synchronization, then you should set xon-protocol as
302 follows:
303
304 \begin{quote}
305 {\tt (set-terminal-characteristic "xon-protocol" 1) }
306 \end{quote}
307
308
309 This will tell Emacs to intercept and act on these characters;
310 \^{ }S will be recognized as telling Emacs to stop sending characters, and
311 \^{ }Q will tell Emacs to continue sending them. Any bindings that these
312 characters have will behave unpredictably. Any MLisp functions you
313 write can use the global variable xon-mode to determine if it should
314 ignore or act on \^{ }Q and \^{ }S.
315
316
317 If you set xon-mode to 1, then you must also have TTSYNC and
318 HOSTSYNC set with the Set Terminal VMS command.
319
320
321 When xon-mode is set, then you will have to rebind any functions
322 that are bound to the \^{ }S and \^{ }Q keys (typically search-forward an
323 quote-character). The conventional rebindings are \^{ }\^{ } -s and
324 \^{ }\^{ }-
325 (Control-uparrow-s, and Control-uparrow-q), although some of the
326 oltimers use Control-\b\ and Control-H.
327 \end{TeX}
328
329 ----------------------------------------------------------------------------*/
330
331 #define DEBUG 0 /* For debugging only */
332
333 #define PM_LEVEL LEVEL_TU
334
335 #define TW_MACROS 0 /* Don't use TermWindows Macros */
336 #define TW_import 0
337 #define TW_STRUCT 0 /* Do not use window Structures */
338 #include <twset.h>
339
340 #define ESC 033 /* Code for ASCII escape character */
341 #define strbspan_(str,mask,table) oscbspan(str,strlen(str),mask,table)
342 #define strbspan(str,mask) strbspan_(str,mask,main_ascii)
343
344 #define FINISH goto FIN
345
346 #ifdef call
347 #undef call
348 #endif
349 #define call(x) if (!(status = x)) FINISH \
350 /* Exit on error */
351
352 MID_STATIC INDEX ind = {0};
353 static int fn; /* TermCap File Number */
354 static TERM *terms;
355 char *osmsg();
356
357 /* MID_RSTATIC char defaults[]=":bc:nd:do:up:cm:nl=\r\n:ff=\f:_H=^H:li#60:co#132:";*/
358 MID_RSTATIC char defaults[]=":nd= :nl=\r\n:ff=\f:_H=^H:li#60:co#132:";
359
360 /* Macros just to improve readibility */
361
362 #define NCAPS (terms->ns)
363 #define CAPLIST (terms->caplist)
364 #define CAPSIZE (terms->size)
365 #define CAPTOP (terms->captop)
366
367
368 /* MONITOR(TERMCAP); */ /* CG. status is defined locally*/
369
370 /*===========================================================================*/
371 static int strloc(str, c)
372 /*+++++++
373 .PURPOSE Locate the first occurence of character `c'
374 .RETURNS Index within str of char c; length of str if not found.
375 ---------*/
376 char *str; /* IN: string to scan */
377 char c; /* IN: char to locate */
378 {
379 register char *p;
380
381 for (p=str; *p; p++)
382 if (*p == c) break;
383 return(p-str);
384 }
385
386 /*===========================================================================*/
387 static int stritem( s1, s2, sep)
388 /*++++++++++++++++++++++++
389 .PURPOSE Locates a substring within a string, which must be followed / preceded
390 by separator(s) characters
391 .RETURNS Index within first string of second string. The index
392 is the length of the first string for mismatch.
393 .REMARKS Locate is made case-sensitive.
394 .METHOD Use index, then check preceding / following char.
395 -----------*/
396 char *s1; /* IN: address of first string (source) */
397 char *s2; /* IN: address of 2nd string (object to find) */
398 char *sep; /* IN: list of separators */
399 {
400 register char *p1, *pe;
401 register int i;
402
403 i = strlen(s2);
404 if (i == 0) i = 1;
405
406 for (p1 = s1, pe = p1 + strlen(p1); *p1; )
407 { p1 += oscindex(p1, pe-p1, s2, i);
408 if_not(*p1) break; /* Not Found */
409 if (p1 > s1) /* Check preceding char */
410 { p1--;
411 if_not (*(sep + strloc(sep, *(p1++))))
412 { p1 += i;
413 continue;
414 }
415 }
416 p1 += i; /* Check following char */
417 if_not(*p1) break; /* Located at end : OK */
418 if(*(sep + strloc(sep, *p1))) /* Separator found */
419 break;
420 }
421
422 return(p1-s1);
423 }
424
425 /*==========================================================================
426 * f_gets
427 *==========================================================================*/
428 static int f_gets (buf, len)
429 /*+++
430 .PURPOSE Interface to file reading
431 .RETURNS OK / NOK / EOF
432 .REMARKS On return, always terminated by EOS.
433 ---------*/
434 char *buf; /* OUT: text, EOS-terminated */
435 int len; /* IN: Size of buffer */
436 {
437 register int status;
438
439 status = osaread(fn, buf, len);
440 if (status >= len)
441 ERR_ED_STR2("Too long TermCapFile record: ", buf, 30);
442 if (status >= 0) status = OK;
443 #if DEBUG
444 printf("Read from TermCapFile: %s\n",buf);
445 #endif
446
447 return(status);
448 }
449
450 /*==========================================================================
451 * tu_encode
452 *==========================================================================*/
453 static int tu_encode (str)
454 /*++++++++++++++
455 .PURPOSE Encode capabilities followed by the `=' sign, i.e.
456 \begin{TeX}
457 \begin{itemize}
458 \item interpretation of delays into number of ms
459 (limited to 255)
460 \item interpretation of \b ooo (octal codes)
461 \item replacement of the \% format by FORMAT char (0x`FE')
462 \item replacement of the starting `:' by the length of the coded
463 capability.
464 \end{itemize}
465 \end{TeX}
466 .RETURNS Length of capability
467 .REMARKS An existing delay is flagged by `d' (no repetition),
468 or `D' (repeated delay indicated by P*) in place of the `='.
469 -----------------------*/
470 char *str; /* MOD: The cap string to encode */
471 {
472 register char *p1, *p2;
473 register int delay;
474 MID_STATIC char delay_flag = '=';
475
476 ENTER("+tu_encode");
477
478 TRACE_ED_STR2("Coding capability ",str,4);
479
480 p1 = str;
481 if (*p1 != ':') { ERR_ED_STR2("Bad TermCapability=>",str,4);
482 FINISH;
483 }
484 p1 += 1;
485 if (*p1) p1 += 2; /* Not Followed by EOS */
486
487 *str = 0;
488 switch(*(p1)) /* Test char following the cap_code */
489 { case '@' : /* Nothing to code */
490 p1++;
491 case ':' : case EOS: /* Nothing to code */
492 goto FIN;
493 case '=' : break;
494 default : /* Do not code, but compute length */
495 delay = oscloc(p1, 80, ':'); /* Length */
496 *str = delay - 1, p1 += delay;
497 goto FIN;
498 }
499
500 /* Here, only if cap_code followed by `='. p1 and p2 are used
501 as pointers to input / output string */
502 p1 = str+4, p2 = p1;
503
504 /* Compute the delays, i.e. digits followed by optional `*' */
505 delay_flag = '=';
506 for( delay = 0; isdigit(*p1); p1++)
507 { delay *= 10;
508 delay += (*p1 - '0');
509 delay_flag = 'd';
510 }
511 if(*p1 == '*') p1++, delay_flag = 'D';
512
513 for(; *p1 != ':'; p2++) /* The `:' indicates the end */
514 { switch(*p1)
515 { case '^': /* Control Char */
516 *p2 = (*(++p1)) & 037;
517 p1++; break;
518 case '\\': /* Escape - Look next char */
519 p1++, *p2 = 0;
520 switch(*p1)
521 { case 'E': case 'e': *p2 = ESC; p1++; break;
522 case 'n': *p2 = '\n'; p1++; break; /* Newline */
523 case 'r': *p2 = '\r'; p1++; break; /* <Return> */
524 case 't': *p2 = '\t'; p1++; break; /* Horiz. Tab */
525 case 'b': *p2 = '\b'; p1++; break; /* Backspace */
526 case 'f': *p2 = '\f'; p1++; break; /* Form Feed */
527 case '0': case '1': case '2': /* Octal number */
528 case '3':
529 while(isdigit(*p1))
530 { *(unsigned char *)p2 *= 8;
531 *(unsigned char *)p2 += *(p1++) - '0';
532 }
533 /* *p2 &= 0177; */ /* Convert to ASCII */
534 break;
535 default: /* case '^': case '\\': and others */
536 *p2 = *(p1++); break;
537 } break;
538 case '%': /* May be a format */
539 if (*(++p1) == '%') *p2 = *(p1++); /* `%' sign */
540 else *p2 = FORMAT;
541 break;
542 default: /* Normal char */
543 *p2 = *(p1++);
544 }
545 }
546
547 /* Write the length of the coded string */
548 *str = p2 - str - 4;
549
550 /* Insert the delay --- limited to 255 ms ! */
551 if (delay)
552 { *(str+3) = delay_flag;
553 *(unsigned char *)p2 = MIN(delay, 255);
554 }
555
556 #if DEBUG
557 TRACE_ED_STR2("Coded:",str+4,*str);
558 if(*(str+3) != '=') TRACE_ED_I("Delay is ",*(str + 4 + *str));
559 #endif
560
561 FIN:
562 EXIT(p1 - str);
563 }
564
565 /*==========================================================================
566 * tu_append
567 *==========================================================================*/
568 static int tu_append (buffer, len)
569 /*+++++++++
570 .PURPOSE Append into the TERMCAP list the specified buffer.
571 .RETURNS OK / NOK
572 .REMARKS Not traced.
573 ---------*/
574 char *buffer; /* IN: Buffer to copy */
575 int len; /* IN: Length of buffer */
576 {
577 register int n;
578
579 if (len <= 0) return(OK);
580
581 /* Test for 2 consecutive : */
582 if ( (*buffer == ':') && (CAPTOP > 0))
583 if (*(CAPLIST + CAPTOP -1) == ':') len--, buffer++;
584
585 /* Test if area large enough, with one spare byte */
586 if ( (CAPTOP + len) >= CAPSIZE) /* Reallocate */
587 { /* TRACE("Reallocation of Termcap Area");*/
588 n = CAPSIZE + MAX (MEM_AMOUNT,len); /* Required memory */
589 CAPLIST = MEM_EXP(char, CAPLIST, n);
590 if (!CAPLIST) return(NOK); /* Allocation failed */
591 CAPSIZE = n;
592 }
593 /* Copy caplist to area */
594 CAPTOP += oscopy(CAPLIST + CAPTOP, buffer, len);
595
596 return(OK);
597 }
598
599 /*==========================================================================
600 * tu_x1
601 *==========================================================================*/
602 static int tu_x1 ()
603 /*++++++++++
604 .PURPOSE Encodes the capabilities and generates the index by capability
605 .RETURNS OK/NOK
606 .REMARKS The first encountered capability wins if duplicates
607 exist --- however, input sequences are stored several times
608 when duplicates exist.
609 -----------------*/
610 {
611 register int i,j;
612 INDEX *idx; /* Starting address of index by capability */
613 int flag, status;
614
615 #if DEBUG
616 MID_STATIC char cap3[] = "xx: ";
617 #endif
618
619 ENTER("tu_x1");
620
621 status = NOK;
622 NCAPS = 0; /* No sequences */
623
624 /* Scan the caplist - each cap starts and ends with `:' */
625
626 for ( i = 0, j = 0; isgraph(*(CAPLIST+i)) ; i += j)
627 { idx = (INDEX *) (CAPLIST + terms->index);
628 /* May have been reallocated... */
629 j = i + 1;
630 ind.cap[0] = *(CAPLIST+j++); /* First char of capcode */
631 ind.cap[1] = *(CAPLIST+j); /* Second char of capcode */
632 ind.icap = i; /* Index of capcode */
633
634 flag = 0; /* Check if entry aleady exists */
635 if( (ind.cap[0] != 'k') && (ind.cap[0] != 'K') && (ind.cap[0] != 'F'))
636 { for (j=0; (j<NCAPS) && (flag == 0); j++)
637 flag = ((idx[j].cap[0] == ind.cap[0]) &&
638 (idx[j].cap[1] == ind.cap[1]));
639 }
640 j = tu_encode(i + CAPLIST);
641 if (j < 3) /* Don't add to index */
642 { flag = 1;
643 if (j <= 0) FINISH;
644 }
645 if(flag) continue; /* Already exists */
646
647 NCAPS++; /* Add a new entry */
648 call(tu_append((char *)&ind,sizeof(ind)));
649 /* Encode the capabilities */
650 }
651
652 /* Sorting via a simple interchange */
653 #if DEBUG
654 TRACE_ED_I("Sorting capabilities ",NCAPS);
655 #endif
656 status = OK;
657 if (NCAPS < 2) FINISH;
658
659 flag = NCAPS-1;
660
661 idx = (INDEX *) (CAPLIST + terms->index);
662
663 for (i=0; i<flag; i++)
664 { for (j=i+1; j < NCAPS; j++)
665 { if (idx[i].cap[0] < idx[j].cap[0]) continue;
666 if (idx[i].cap[0] == idx[j].cap[0])
667 if (idx[i].cap[1] <= idx[j].cap[1]) continue;
668 ind = idx[i]; idx[i] = idx[j]; idx[j] = ind;
669 }
670 }
671
672 #if DEBUG
673 TRACE("Sorted Capabilities");
674 for (i=0; i<NCAPS; i++)
675 { cap3[0] = idx[i].cap[0], cap3[1] = idx[i].cap[1];
676 TRACE_ED_STR2(cap3, 4+CAPLIST+idx[i].icap, *(CAPLIST+idx[i].icap));
677 }
678 #endif
679
680 FIN:
681 EXIT(status);
682 }
683
684 /*==========================================================================
685 * tu_x2
686 *==========================================================================*/
687 static int tu_x2 ()
688 /*+++++++++++
689 .PURPOSE Generates the index by sequences (made with short integers)
690 .RETURNS OK/NOK
691 .REMARKS To be meaningful, capabilities should be coded.
692 Only those caps beginning with `k', `K' or `F' are retained
693 (functions that are read on the terminal)
694 -----------*/
695 {
696 register int i;
697 register char *p1, *p2;
698 INDEX *idx1; /* Starting address of index by capability */
699 short int *idx; /* Starting address of index by sequence */
700 short int temp;
701 char flag;
702 int status;
703
704 ENTER("tu_x2");
705
706 status = NOK;
707
708 /* Simply use the index by capcodes ---
709 Keep only capcodes starting with 'k', 'K' or 'F' */
710
711 terms->nsr = 0; /* No readable sequence */
712 terms->specials = 0;
713
714 for (i=0; i<NCAPS; i++)
715 { idx1 = (INDEX *) (CAPLIST + terms->index);
716 flag = idx1[i].cap[0];
717 if (flag == 'k' || flag == 'K' || flag == 'F')
718 { call(tu_append((char *)&idx1[i].icap, sizeof(short int)));
719 (terms->nsr)++;
720 }
721 }
722
723 /* Sort ... */
724 #if DEBUG
725 TRACE_ED_I("Sorting capabilities: ",(terms->nsr));
726 #endif
727
728 status = OK;
729 if ((terms->nsr) < 2) FINISH;
730
731 flag = 1; /* Indicates sort not complete */
732 idx = (short int *) (CAPLIST + terms->indexr);
733
734 while(flag)
735 { flag = 0;
736 for (i=0; i<(terms->nsr) -1; i++)
737 { p1 = CAPLIST + idx[i] + 4;
738 p2 = CAPLIST + idx[i+1] + 4;
739 while (*p1 == *p2) p1++, p2++;
740 if (*p1 <= *p2) continue;
741 temp = idx[i]; idx[i] = idx[i+1]; idx[i+1] = temp;
742 flag = 1;
743 }
744 }
745
746 /* Flag in `specials' chars that start escape sequences */
747
748 for (i=0; i<(terms->nsr); i++)
749 { p1 = CAPLIST + idx[i] + 4;
750 if (*(unsigned char *)p1 < 32) terms->specials |=
751 MASK(*(unsigned char *)p1);
752 }
753
754 #if DEBUG
755 for (i=0; i<(terms->nsr); i++)
756 TRACE_ED_STR2("Caps sorted by sequence: ",
757 CAPLIST + idx[i] + 1 ,2);
758 #endif
759
760 FIN:
761 EXIT(status);
762 }
763
764 /*==========================================================================
765 * tu_build
766 *==========================================================================*/
767 static int tu_build ()
768 /*++++++++
769 .PURPOSE Generates the 2 indexes (by capcode - by sequences), and
770 encodes the caps.
771 .RETURNS OK/NOK
772 .REMARKS
773 ----------*/
774 {
775 int status;
776
777 ENTER("tu_build");
778 status = NOK;
779
780 *(CAPLIST + CAPTOP++) = EOS; /* Terminate the string */
781
782 /* Adjust the captop to a correct boundary */
783
784 CAPTOP = ((CAPTOP + sizeof(char *)-1)/sizeof(char *)) * sizeof(char *);
785
786 /* Save this position in term.index */
787 terms->index = CAPTOP;
788
789 /* Encode capabilities and generate the index by capabilities */
790 call(tu_x1());
791
792 /* Generate the index by sequences */
793 CAPTOP = ((CAPTOP + sizeof(char *)-1)/sizeof(char *)) * sizeof(char *);
794 terms->indexr = CAPTOP;
795 call(tu_x2());
796 status = OK;
797
798 FIN:
799 EXIT(status);
800 }
801
802 /*==========================================================================
803 * tu_fetch
804 *==========================================================================*/
805 static int tu_fetch (device)
806 /*++++++++++++++
807 .PURPOSE Search the TERMCAP file for a specified device, and append the
808 caplist into CAPLIST.
809 .RETURNS NOK if device not found, OK otherwise
810 .REMARKS
811 ------------------*/
812 char *device; /* IN: EOS-terminated device name */
813 {
814 int i, j, l, status;
815 char buffer[1024];
816
817 ENTER("tu_fetch");
818
819 TRACE_ED_STRING("Looking for Device=>",device);
820
821 while ((status = f_gets(buffer,sizeof(buffer))) == OK)
822 { if (buffer[0] == '#')
823 continue; /* Comment line */
824 if (isspace(buffer[0])) continue; /* Not useful */
825 i = stritem(buffer,device,":|");
826 /* Followed / preceded by : or | */
827 if (buffer[i]) break;
828 }
829 if (status != OK) FINISH; /* Device not found */
830
831 /* Now, load the caplist, which starts right now, at the :
832 * Note that the `\' indicates a continuation on the next line */
833
834 i = strloc(buffer, ':'); /* Starting scanning point */
835
836 for (; status == OK; status = f_gets(buffer,sizeof(buffer)))
837 { if (buffer[0] == '#') /* Line commented out */
838 continue;
839 if (i == 0) i = oscspan(buffer,sizeof(buffer), _SPACE_, main_ascii);
840 if (i<=0) break; /* List must start with blank or so */
841 j = strbspan(buffer,_SPACE_); /* Offset of last char */
842 l = j-i; /* Length of the termlist */
843 if (buffer[j] != '\\') l++;
844 call(tu_append(&buffer[i],l));
845 /* Test for continuation char \ */
846 if (buffer[j] != '\\') break;
847 i = 0;
848 }
849 if (status == NOK) FINISH;
850
851 status = OK;
852 TRACE_ED_STR2("CapList: ",CAPLIST,CAPTOP);
853
854 FIN:
855 if (status != OK) status = NOK;
856 EXIT(status);
857 }
858
859 /*==========================================================================
860 * tu_scan
861 *==========================================================================*/
862 static int tu_scan (device)
863 /*+++++++++++
864 .PURPOSE Open the specified TERMCAP file for the named device, expanding
865 the ``tc='' references.
866 .RETURNS NOK if device not found, OK otherwise
867 .REMARKS
868 ---------------*/
869 char *device; /* IN: EOS-terminated device name */
870 {
871 char *p, *tc_device;
872 int status;
873
874 /* ENTER("tu_scan"); */
875
876 TRACE_ED_STRING("Look for device: ", device);
877 status = NOK;
878
879 /* Now, fetch the successive entries */
880 tc_device = device;
881
882 while(1)
883 { if (tu_fetch(tc_device) != OK)
884 { ERR_ED_STRING("This device is unknown: ",tc_device);
885 FINISH;;
886 }
887 *(CAPLIST + CAPTOP) = EOS; /* Terminate the string */
888 /* Locate last field */
889 p = CAPLIST + oscbloc(CAPLIST, CAPTOP, '=');
890 tc_device = p;
891 if (*(--p) != 'c') break; /* Should match "tc=" */
892 if (*(--p) != 't') break; /* Should match "tc=" */
893 /* An expansion is required - Reset the captop size in
894 * the term, and get the alternate device name */
895 tc_device++; /* Starting char of alternate device name */
896 *(CAPLIST + CAPTOP - 1) = EOS; /* Replace the : */
897 CAPTOP = p - CAPLIST; /* Last field is deleted */
898 osaseek(fn, 0L, 0);
899 }
900
901 status = OK;
902 osaclose(fn);
903
904 FIN:
905 return(status);
906 }
907
908 /*==========================================================================
909 * tu_load
910 *==========================================================================*/
911 int tu_load (file_name, device, term)
912 /*++++++++++++
913 .PURPOSE Load to TERM structure the specified device.
914 .RETURNS NOK if device or file not found, OK otherwise
915 .REMARKS Appends some defautt capabilities.
916 ----------------*/
917 char *file_name; /* IN: EOS-terminated TERMCAP file name */
918 char *device; /* IN: EOS-terminated device name */
919 TERM *term; /* MOD: Area to receive capabilities */
920 {
921 int status;
922
923 ENTER("tu_load");
924
925 TRACE_ED_STRING("TermCapFile: ", file_name);
926 status = NOK;
927
928 terms = term;
929
930 /* First, open the TERMCAP file */
931
932 fn = osaopen(file_name,READ);
933 if (fn <= 0) ERR_ED_STRING("Bad TermCap File: ", file_name);
934 else status = tu_scan(device);
935 if (!status) FINISH;
936
937 tu_append(defaults, sizeof(defaults)-1);
938
939 status &= tu_build();
940
941 #if DEBUG
942 TRACE_ED_STR2("CapList ",terms->caplist, terms->captop);
943 #endif
944
945 FIN:
946 EXIT(status);
947 }
948