Mercurial > emacs
annotate src/termcap.c @ 13156:a398dbd2b945
Indentation change.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 08 Oct 1995 19:38:53 +0000 |
parents | bd38619285f7 |
children | ad4eada50462 |
rev | line source |
---|---|
7306 | 1 /* Work-alike for termcap, plus extra features. |
12675
8c9369149a9d
Move #define of bcopy to after #include <string.h>.
David J. MacKenzie <djm@gnu.org>
parents:
11264
diff
changeset
|
2 Copyright (C) 1985, 86, 93, 94, 95 Free Software Foundation, Inc. |
7306 | 3 |
4 This program is free software; you can redistribute it and/or modify | |
5 it under the terms of the GNU General Public License as published by | |
6 the Free Software Foundation; either version 2, or (at your option) | |
7 any later version. | |
8 | |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
15 along with this program; see the file COPYING. If not, write to | |
16 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
17 | |
18 /* Emacs config.h may rename various library functions such as malloc. */ | |
19 #ifdef HAVE_CONFIG_H | |
12994
bd38619285f7
Don't assume that HAVE_CONFIG_H implies emacs.
David J. MacKenzie <djm@gnu.org>
parents:
12907
diff
changeset
|
20 #include <config.h> |
bd38619285f7
Don't assume that HAVE_CONFIG_H implies emacs.
David J. MacKenzie <djm@gnu.org>
parents:
12907
diff
changeset
|
21 #endif |
7785 | 22 |
12994
bd38619285f7
Don't assume that HAVE_CONFIG_H implies emacs.
David J. MacKenzie <djm@gnu.org>
parents:
12907
diff
changeset
|
23 #ifdef emacs |
7785 | 24 |
25 /* Get the O_* definitions for open et al. */ | |
26 #include <sys/file.h> | |
27 #ifdef USG5 | |
28 #include <fcntl.h> | |
29 #endif | |
30 | |
12994
bd38619285f7
Don't assume that HAVE_CONFIG_H implies emacs.
David J. MacKenzie <djm@gnu.org>
parents:
12907
diff
changeset
|
31 #else /* not emacs */ |
7306 | 32 |
33 #ifdef STDC_HEADERS | |
34 #include <stdlib.h> | |
35 #include <string.h> | |
36 #else | |
37 char *getenv (); | |
38 char *malloc (); | |
39 char *realloc (); | |
40 #endif | |
41 | |
12675
8c9369149a9d
Move #define of bcopy to after #include <string.h>.
David J. MacKenzie <djm@gnu.org>
parents:
11264
diff
changeset
|
42 /* Do this after the include, in case string.h prototypes bcopy. */ |
8c9369149a9d
Move #define of bcopy to after #include <string.h>.
David J. MacKenzie <djm@gnu.org>
parents:
11264
diff
changeset
|
43 #if (defined(HAVE_STRING_H) || defined(STDC_HEADERS)) && !defined(bcopy) |
8c9369149a9d
Move #define of bcopy to after #include <string.h>.
David J. MacKenzie <djm@gnu.org>
parents:
11264
diff
changeset
|
44 #define bcopy(s, d, n) memcpy ((d), (s), (n)) |
8c9369149a9d
Move #define of bcopy to after #include <string.h>.
David J. MacKenzie <djm@gnu.org>
parents:
11264
diff
changeset
|
45 #endif |
8c9369149a9d
Move #define of bcopy to after #include <string.h>.
David J. MacKenzie <djm@gnu.org>
parents:
11264
diff
changeset
|
46 |
7306 | 47 #ifdef HAVE_UNISTD_H |
48 #include <unistd.h> | |
49 #endif | |
50 #ifdef _POSIX_VERSION | |
51 #include <fcntl.h> | |
52 #endif | |
53 | |
12994
bd38619285f7
Don't assume that HAVE_CONFIG_H implies emacs.
David J. MacKenzie <djm@gnu.org>
parents:
12907
diff
changeset
|
54 #endif /* not emacs */ |
7306 | 55 |
56 #ifndef NULL | |
57 #define NULL (char *) 0 | |
58 #endif | |
59 | |
7685 | 60 #ifndef O_RDONLY |
61 #define O_RDONLY 0 | |
62 #endif | |
63 | |
7306 | 64 /* BUFSIZE is the initial size allocated for the buffer |
65 for reading the termcap file. | |
66 It is not a limit. | |
67 Make it large normally for speed. | |
68 Make it variable when debugging, so can exercise | |
69 increasing the space dynamically. */ | |
70 | |
71 #ifndef BUFSIZE | |
72 #ifdef DEBUG | |
73 #define BUFSIZE bufsize | |
74 | |
75 int bufsize = 128; | |
76 #else | |
77 #define BUFSIZE 2048 | |
78 #endif | |
79 #endif | |
80 | |
12679
a14b26e55f25
TERMCAP_NAME -> TERMCAP_FILE.
David J. MacKenzie <djm@gnu.org>
parents:
12675
diff
changeset
|
81 #ifndef TERMCAP_FILE |
a14b26e55f25
TERMCAP_NAME -> TERMCAP_FILE.
David J. MacKenzie <djm@gnu.org>
parents:
12675
diff
changeset
|
82 #define TERMCAP_FILE "/etc/termcap" |
7306 | 83 #endif |
84 | |
85 #ifndef emacs | |
86 static void | |
87 memory_out () | |
88 { | |
89 write (2, "virtual memory exhausted\n", 25); | |
90 exit (1); | |
91 } | |
92 | |
93 static char * | |
94 xmalloc (size) | |
95 unsigned size; | |
96 { | |
97 register char *tem = malloc (size); | |
98 | |
99 if (!tem) | |
100 memory_out (); | |
101 return tem; | |
102 } | |
103 | |
104 static char * | |
105 xrealloc (ptr, size) | |
106 char *ptr; | |
107 unsigned size; | |
108 { | |
109 register char *tem = realloc (ptr, size); | |
110 | |
111 if (!tem) | |
112 memory_out (); | |
113 return tem; | |
114 } | |
115 #endif /* not emacs */ | |
116 | |
117 /* Looking up capabilities in the entry already found. */ | |
118 | |
119 /* The pointer to the data made by tgetent is left here | |
120 for tgetnum, tgetflag and tgetstr to find. */ | |
121 static char *term_entry; | |
122 | |
123 static char *tgetst1 (); | |
124 | |
125 /* Search entry BP for capability CAP. | |
126 Return a pointer to the capability (in BP) if found, | |
127 0 if not found. */ | |
128 | |
129 static char * | |
130 find_capability (bp, cap) | |
131 register char *bp, *cap; | |
132 { | |
133 for (; *bp; bp++) | |
134 if (bp[0] == ':' | |
135 && bp[1] == cap[0] | |
136 && bp[2] == cap[1]) | |
137 return &bp[4]; | |
138 return NULL; | |
139 } | |
140 | |
141 int | |
142 tgetnum (cap) | |
143 char *cap; | |
144 { | |
145 register char *ptr = find_capability (term_entry, cap); | |
146 if (!ptr || ptr[-1] != '#') | |
147 return -1; | |
148 return atoi (ptr); | |
149 } | |
150 | |
151 int | |
152 tgetflag (cap) | |
153 char *cap; | |
154 { | |
155 register char *ptr = find_capability (term_entry, cap); | |
156 return ptr && ptr[-1] == ':'; | |
157 } | |
158 | |
159 /* Look up a string-valued capability CAP. | |
160 If AREA is non-null, it points to a pointer to a block in which | |
161 to store the string. That pointer is advanced over the space used. | |
162 If AREA is null, space is allocated with `malloc'. */ | |
163 | |
164 char * | |
165 tgetstr (cap, area) | |
166 char *cap; | |
167 char **area; | |
168 { | |
169 register char *ptr = find_capability (term_entry, cap); | |
170 if (!ptr || (ptr[-1] != '=' && ptr[-1] != '~')) | |
171 return NULL; | |
172 return tgetst1 (ptr, area); | |
173 } | |
174 | |
175 /* Table, indexed by a character in range 0100 to 0140 with 0100 subtracted, | |
176 gives meaning of character following \, or a space if no special meaning. | |
177 Eight characters per line within the string. */ | |
178 | |
179 static char esctab[] | |
180 = " \007\010 \033\014 \ | |
181 \012 \ | |
182 \015 \011 \013 \ | |
183 "; | |
184 | |
185 /* PTR points to a string value inside a termcap entry. | |
186 Copy that value, processing \ and ^ abbreviations, | |
187 into the block that *AREA points to, | |
188 or to newly allocated storage if AREA is NULL. | |
189 Return the address to which we copied the value, | |
190 or NULL if PTR is NULL. */ | |
191 | |
192 static char * | |
193 tgetst1 (ptr, area) | |
194 char *ptr; | |
195 char **area; | |
196 { | |
197 register char *p, *r; | |
198 register int c; | |
199 register int size; | |
200 char *ret; | |
201 register int c1; | |
202 | |
203 if (!ptr) | |
204 return NULL; | |
205 | |
206 /* `ret' gets address of where to store the string. */ | |
207 if (!area) | |
208 { | |
209 /* Compute size of block needed (may overestimate). */ | |
210 p = ptr; | |
211 while ((c = *p++) && c != ':' && c != '\n') | |
212 ; | |
213 ret = (char *) xmalloc (p - ptr + 1); | |
214 } | |
215 else | |
216 ret = *area; | |
217 | |
218 /* Copy the string value, stopping at null or colon. | |
219 Also process ^ and \ abbreviations. */ | |
220 p = ptr; | |
221 r = ret; | |
222 while ((c = *p++) && c != ':' && c != '\n') | |
223 { | |
224 if (c == '^') | |
10186
af0b61d21a8f
(tgetst1): Let ^? stand for DEL character.
Richard M. Stallman <rms@gnu.org>
parents:
7785
diff
changeset
|
225 { |
af0b61d21a8f
(tgetst1): Let ^? stand for DEL character.
Richard M. Stallman <rms@gnu.org>
parents:
7785
diff
changeset
|
226 c = *p++; |
af0b61d21a8f
(tgetst1): Let ^? stand for DEL character.
Richard M. Stallman <rms@gnu.org>
parents:
7785
diff
changeset
|
227 if (c == '?') |
af0b61d21a8f
(tgetst1): Let ^? stand for DEL character.
Richard M. Stallman <rms@gnu.org>
parents:
7785
diff
changeset
|
228 c = 0177; |
af0b61d21a8f
(tgetst1): Let ^? stand for DEL character.
Richard M. Stallman <rms@gnu.org>
parents:
7785
diff
changeset
|
229 else |
af0b61d21a8f
(tgetst1): Let ^? stand for DEL character.
Richard M. Stallman <rms@gnu.org>
parents:
7785
diff
changeset
|
230 c &= 037; |
af0b61d21a8f
(tgetst1): Let ^? stand for DEL character.
Richard M. Stallman <rms@gnu.org>
parents:
7785
diff
changeset
|
231 } |
7306 | 232 else if (c == '\\') |
233 { | |
234 c = *p++; | |
235 if (c >= '0' && c <= '7') | |
236 { | |
237 c -= '0'; | |
238 size = 0; | |
239 | |
240 while (++size < 3 && (c1 = *p) >= '0' && c1 <= '7') | |
241 { | |
242 c *= 8; | |
243 c += c1 - '0'; | |
244 p++; | |
245 } | |
246 } | |
247 else if (c >= 0100 && c < 0200) | |
248 { | |
249 c1 = esctab[(c & ~040) - 0100]; | |
250 if (c1 != ' ') | |
251 c = c1; | |
252 } | |
253 } | |
254 *r++ = c; | |
255 } | |
256 *r = '\0'; | |
257 /* Update *AREA. */ | |
258 if (area) | |
259 *area = r + 1; | |
260 return ret; | |
261 } | |
262 | |
263 /* Outputting a string with padding. */ | |
264 | |
265 short ospeed; | |
266 /* If OSPEED is 0, we use this as the actual baud rate. */ | |
267 int tputs_baud_rate; | |
268 char PC; | |
269 | |
270 /* Actual baud rate if positive; | |
271 - baud rate / 100 if negative. */ | |
272 | |
10739
97096cdf6e55
(speeds): Make it ints. Add some higher speeds.
Richard M. Stallman <rms@gnu.org>
parents:
10186
diff
changeset
|
273 static int speeds[] = |
7306 | 274 { |
275 #ifdef VMS | |
276 0, 50, 75, 110, 134, 150, -3, -6, -12, -18, | |
277 -20, -24, -36, -48, -72, -96, -192 | |
278 #else /* not VMS */ | |
279 0, 50, 75, 110, 135, 150, -2, -3, -6, -12, | |
10739
97096cdf6e55
(speeds): Make it ints. Add some higher speeds.
Richard M. Stallman <rms@gnu.org>
parents:
10186
diff
changeset
|
280 -18, -24, -48, -96, -192, -288, -384, -576, -1152 |
7306 | 281 #endif /* not VMS */ |
282 }; | |
283 | |
284 void | |
285 tputs (str, nlines, outfun) | |
286 register char *str; | |
287 int nlines; | |
288 register int (*outfun) (); | |
289 { | |
290 register int padcount = 0; | |
291 register int speed; | |
292 | |
293 #ifdef emacs | |
294 extern baud_rate; | |
295 speed = baud_rate; | |
10739
97096cdf6e55
(speeds): Make it ints. Add some higher speeds.
Richard M. Stallman <rms@gnu.org>
parents:
10186
diff
changeset
|
296 /* For quite high speeds, convert to the smaller |
97096cdf6e55
(speeds): Make it ints. Add some higher speeds.
Richard M. Stallman <rms@gnu.org>
parents:
10186
diff
changeset
|
297 units to avoid overflow. */ |
97096cdf6e55
(speeds): Make it ints. Add some higher speeds.
Richard M. Stallman <rms@gnu.org>
parents:
10186
diff
changeset
|
298 if (speed > 10000) |
97096cdf6e55
(speeds): Make it ints. Add some higher speeds.
Richard M. Stallman <rms@gnu.org>
parents:
10186
diff
changeset
|
299 speed = - speed / 100; |
7306 | 300 #else |
301 if (ospeed == 0) | |
302 speed = tputs_baud_rate; | |
303 else | |
304 speed = speeds[ospeed]; | |
305 #endif | |
306 | |
307 if (!str) | |
308 return; | |
309 | |
310 while (*str >= '0' && *str <= '9') | |
311 { | |
312 padcount += *str++ - '0'; | |
313 padcount *= 10; | |
314 } | |
315 if (*str == '.') | |
316 { | |
317 str++; | |
318 padcount += *str++ - '0'; | |
319 } | |
320 if (*str == '*') | |
321 { | |
322 str++; | |
323 padcount *= nlines; | |
324 } | |
325 while (*str) | |
326 (*outfun) (*str++); | |
327 | |
10739
97096cdf6e55
(speeds): Make it ints. Add some higher speeds.
Richard M. Stallman <rms@gnu.org>
parents:
10186
diff
changeset
|
328 /* PADCOUNT is now in units of tenths of msec. |
10753 | 329 SPEED is measured in characters per 10 seconds |
10739
97096cdf6e55
(speeds): Make it ints. Add some higher speeds.
Richard M. Stallman <rms@gnu.org>
parents:
10186
diff
changeset
|
330 or in characters per .1 seconds (if negative). |
97096cdf6e55
(speeds): Make it ints. Add some higher speeds.
Richard M. Stallman <rms@gnu.org>
parents:
10186
diff
changeset
|
331 We use the smaller units for larger speeds to avoid overflow. */ |
97096cdf6e55
(speeds): Make it ints. Add some higher speeds.
Richard M. Stallman <rms@gnu.org>
parents:
10186
diff
changeset
|
332 padcount *= speed; |
7306 | 333 padcount += 500; |
334 padcount /= 1000; | |
10739
97096cdf6e55
(speeds): Make it ints. Add some higher speeds.
Richard M. Stallman <rms@gnu.org>
parents:
10186
diff
changeset
|
335 if (speed < 0) |
7306 | 336 padcount = -padcount; |
337 else | |
338 { | |
339 padcount += 50; | |
340 padcount /= 100; | |
341 } | |
342 | |
343 while (padcount-- > 0) | |
344 (*outfun) (PC); | |
345 } | |
346 | |
347 /* Finding the termcap entry in the termcap data base. */ | |
348 | |
349 struct buffer | |
350 { | |
351 char *beg; | |
352 int size; | |
353 char *ptr; | |
354 int ateof; | |
355 int full; | |
356 }; | |
357 | |
358 /* Forward declarations of static functions. */ | |
359 | |
360 static int scan_file (); | |
361 static char *gobble_line (); | |
362 static int compare_contin (); | |
363 static int name_match (); | |
364 | |
365 #ifdef VMS | |
366 | |
367 #include <rmsdef.h> | |
368 #include <fab.h> | |
369 #include <nam.h> | |
370 | |
371 static int | |
372 valid_filename_p (fn) | |
373 char *fn; | |
374 { | |
375 struct FAB fab = cc$rms_fab; | |
376 struct NAM nam = cc$rms_nam; | |
377 char esa[NAM$C_MAXRSS]; | |
378 | |
379 fab.fab$l_fna = fn; | |
380 fab.fab$b_fns = strlen(fn); | |
381 fab.fab$l_nam = &nam; | |
382 fab.fab$l_fop = FAB$M_NAM; | |
383 | |
384 nam.nam$l_esa = esa; | |
385 nam.nam$b_ess = sizeof esa; | |
386 | |
387 return SYS$PARSE(&fab, 0, 0) == RMS$_NORMAL; | |
388 } | |
389 | |
390 #else /* !VMS */ | |
391 | |
392 #ifdef MSDOS /* MW, May 1993 */ | |
393 static int | |
394 valid_filename_p (fn) | |
395 char *fn; | |
396 { | |
397 return *fn == '/' || fn[1] == ':'; | |
398 } | |
399 #else | |
400 #define valid_filename_p(fn) (*(fn) == '/') | |
401 #endif | |
402 | |
403 #endif /* !VMS */ | |
404 | |
405 /* Find the termcap entry data for terminal type NAME | |
406 and store it in the block that BP points to. | |
407 Record its address for future use. | |
408 | |
409 If BP is null, space is dynamically allocated. | |
410 | |
411 Return -1 if there is some difficulty accessing the data base | |
412 of terminal types, | |
413 0 if the data base is accessible but the type NAME is not defined | |
414 in it, and some other value otherwise. */ | |
415 | |
416 int | |
417 tgetent (bp, name) | |
418 char *bp, *name; | |
419 { | |
420 register char *termcap_name; | |
421 register int fd; | |
422 struct buffer buf; | |
423 register char *bp1; | |
424 char *bp2; | |
425 char *term; | |
426 int malloc_size = 0; | |
427 register int c; | |
428 char *tcenv; /* TERMCAP value, if it contains :tc=. */ | |
429 char *indirect = NULL; /* Terminal type in :tc= in TERMCAP value. */ | |
430 int filep; | |
431 | |
432 #ifdef INTERNAL_TERMINAL | |
433 /* For the internal terminal we don't want to read any termcap file, | |
434 so fake it. */ | |
435 if (!strcmp (name, "internal")) | |
436 { | |
437 term = INTERNAL_TERMINAL; | |
438 if (!bp) | |
439 { | |
440 malloc_size = 1 + strlen (term); | |
441 bp = (char *) xmalloc (malloc_size); | |
442 } | |
443 strcpy (bp, term); | |
444 goto ret; | |
445 } | |
446 #endif /* INTERNAL_TERMINAL */ | |
447 | |
12907
6cae53a06172
default to user-supplied buffer.
David J. MacKenzie <djm@gnu.org>
parents:
12679
diff
changeset
|
448 /* For compatibility with programs like `less' that want to |
6cae53a06172
default to user-supplied buffer.
David J. MacKenzie <djm@gnu.org>
parents:
12679
diff
changeset
|
449 put data in the termcap buffer themselves as a fallback. */ |
6cae53a06172
default to user-supplied buffer.
David J. MacKenzie <djm@gnu.org>
parents:
12679
diff
changeset
|
450 if (bp) |
6cae53a06172
default to user-supplied buffer.
David J. MacKenzie <djm@gnu.org>
parents:
12679
diff
changeset
|
451 term_entry = bp; |
6cae53a06172
default to user-supplied buffer.
David J. MacKenzie <djm@gnu.org>
parents:
12679
diff
changeset
|
452 |
7306 | 453 termcap_name = getenv ("TERMCAP"); |
454 if (termcap_name && *termcap_name == '\0') | |
455 termcap_name = NULL; | |
456 #if defined (MSDOS) && !defined (TEST) | |
457 if (termcap_name && (*termcap_name == '\\' | |
458 || *termcap_name == '/' | |
459 || termcap_name[1] == ':')) | |
460 dostounix_filename(termcap_name); | |
461 #endif | |
462 | |
463 filep = termcap_name && valid_filename_p (termcap_name); | |
464 | |
465 /* If termcap_name is non-null and starts with / (in the un*x case, that is), | |
466 it is a file name to use instead of /etc/termcap. | |
467 If it is non-null and does not start with /, | |
468 it is the entry itself, but only if | |
469 the name the caller requested matches the TERM variable. */ | |
470 | |
471 if (termcap_name && !filep && !strcmp (name, getenv ("TERM"))) | |
472 { | |
473 indirect = tgetst1 (find_capability (termcap_name, "tc"), (char **) 0); | |
474 if (!indirect) | |
475 { | |
476 if (!bp) | |
477 bp = termcap_name; | |
478 else | |
479 strcpy (bp, termcap_name); | |
480 goto ret; | |
481 } | |
482 else | |
483 { /* It has tc=. Need to read /etc/termcap. */ | |
484 tcenv = termcap_name; | |
485 termcap_name = NULL; | |
486 } | |
487 } | |
488 | |
489 if (!termcap_name || !filep) | |
12679
a14b26e55f25
TERMCAP_NAME -> TERMCAP_FILE.
David J. MacKenzie <djm@gnu.org>
parents:
12675
diff
changeset
|
490 termcap_name = TERMCAP_FILE; |
7306 | 491 |
492 /* Here we know we must search a file and termcap_name has its name. */ | |
493 | |
494 #ifdef MSDOS | |
7685 | 495 fd = open (termcap_name, O_RDONLY|O_TEXT, 0); |
7306 | 496 #else |
7685 | 497 fd = open (termcap_name, O_RDONLY, 0); |
7306 | 498 #endif |
499 if (fd < 0) | |
500 return -1; | |
501 | |
502 buf.size = BUFSIZE; | |
503 /* Add 1 to size to ensure room for terminating null. */ | |
504 buf.beg = (char *) xmalloc (buf.size + 1); | |
505 term = indirect ? indirect : name; | |
506 | |
507 if (!bp) | |
508 { | |
509 malloc_size = indirect ? strlen (tcenv) + 1 : buf.size; | |
510 bp = (char *) xmalloc (malloc_size); | |
511 } | |
512 bp1 = bp; | |
513 | |
514 if (indirect) | |
515 /* Copy the data from the environment variable. */ | |
516 { | |
517 strcpy (bp, tcenv); | |
518 bp1 += strlen (tcenv); | |
519 } | |
520 | |
521 while (term) | |
522 { | |
523 /* Scan the file, reading it via buf, till find start of main entry. */ | |
524 if (scan_file (term, fd, &buf) == 0) | |
525 { | |
526 close (fd); | |
527 free (buf.beg); | |
528 if (malloc_size) | |
529 free (bp); | |
530 return 0; | |
531 } | |
532 | |
533 /* Free old `term' if appropriate. */ | |
534 if (term != name) | |
535 free (term); | |
536 | |
537 /* If BP is malloc'd by us, make sure it is big enough. */ | |
538 if (malloc_size) | |
539 { | |
540 malloc_size = bp1 - bp + buf.size; | |
541 termcap_name = (char *) xrealloc (bp, malloc_size); | |
542 bp1 += termcap_name - bp; | |
543 bp = termcap_name; | |
544 } | |
545 | |
546 bp2 = bp1; | |
547 | |
548 /* Copy the line of the entry from buf into bp. */ | |
549 termcap_name = buf.ptr; | |
550 while ((*bp1++ = c = *termcap_name++) && c != '\n') | |
551 /* Drop out any \ newline sequence. */ | |
552 if (c == '\\' && *termcap_name == '\n') | |
553 { | |
554 bp1--; | |
555 termcap_name++; | |
556 } | |
557 *bp1 = '\0'; | |
558 | |
559 /* Does this entry refer to another terminal type's entry? | |
560 If something is found, copy it into heap and null-terminate it. */ | |
561 term = tgetst1 (find_capability (bp2, "tc"), (char **) 0); | |
562 } | |
563 | |
564 close (fd); | |
565 free (buf.beg); | |
566 | |
567 if (malloc_size) | |
568 bp = (char *) xrealloc (bp, bp1 - bp + 1); | |
569 | |
570 ret: | |
571 term_entry = bp; | |
572 return 1; | |
573 } | |
574 | |
575 /* Given file open on FD and buffer BUFP, | |
576 scan the file from the beginning until a line is found | |
577 that starts the entry for terminal type STR. | |
578 Return 1 if successful, with that line in BUFP, | |
579 or 0 if no entry is found in the file. */ | |
580 | |
581 static int | |
582 scan_file (str, fd, bufp) | |
583 char *str; | |
584 int fd; | |
585 register struct buffer *bufp; | |
586 { | |
587 register char *end; | |
588 | |
589 bufp->ptr = bufp->beg; | |
590 bufp->full = 0; | |
591 bufp->ateof = 0; | |
592 *bufp->ptr = '\0'; | |
593 | |
594 lseek (fd, 0L, 0); | |
595 | |
596 while (!bufp->ateof) | |
597 { | |
598 /* Read a line into the buffer. */ | |
599 end = NULL; | |
600 do | |
601 { | |
602 /* if it is continued, append another line to it, | |
603 until a non-continued line ends. */ | |
604 end = gobble_line (fd, bufp, end); | |
605 } | |
606 while (!bufp->ateof && end[-2] == '\\'); | |
607 | |
608 if (*bufp->ptr != '#' | |
609 && name_match (bufp->ptr, str)) | |
610 return 1; | |
611 | |
612 /* Discard the line just processed. */ | |
613 bufp->ptr = end; | |
614 } | |
615 return 0; | |
616 } | |
617 | |
618 /* Return nonzero if NAME is one of the names specified | |
619 by termcap entry LINE. */ | |
620 | |
621 static int | |
622 name_match (line, name) | |
623 char *line, *name; | |
624 { | |
625 register char *tem; | |
626 | |
627 if (!compare_contin (line, name)) | |
628 return 1; | |
629 /* This line starts an entry. Is it the right one? */ | |
630 for (tem = line; *tem && *tem != '\n' && *tem != ':'; tem++) | |
631 if (*tem == '|' && !compare_contin (tem + 1, name)) | |
632 return 1; | |
633 | |
634 return 0; | |
635 } | |
636 | |
637 static int | |
638 compare_contin (str1, str2) | |
639 register char *str1, *str2; | |
640 { | |
641 register int c1, c2; | |
642 while (1) | |
643 { | |
644 c1 = *str1++; | |
645 c2 = *str2++; | |
646 while (c1 == '\\' && *str1 == '\n') | |
647 { | |
648 str1++; | |
649 while ((c1 = *str1++) == ' ' || c1 == '\t'); | |
650 } | |
651 if (c2 == '\0') | |
652 { | |
653 /* End of type being looked up. */ | |
654 if (c1 == '|' || c1 == ':') | |
655 /* If end of name in data base, we win. */ | |
656 return 0; | |
657 else | |
658 return 1; | |
659 } | |
660 else if (c1 != c2) | |
661 return 1; | |
662 } | |
663 } | |
664 | |
665 /* Make sure that the buffer <- BUFP contains a full line | |
666 of the file open on FD, starting at the place BUFP->ptr | |
667 points to. Can read more of the file, discard stuff before | |
668 BUFP->ptr, or make the buffer bigger. | |
669 | |
670 Return the pointer to after the newline ending the line, | |
671 or to the end of the file, if there is no newline to end it. | |
672 | |
673 Can also merge on continuation lines. If APPEND_END is | |
674 non-null, it points past the newline of a line that is | |
675 continued; we add another line onto it and regard the whole | |
676 thing as one line. The caller decides when a line is continued. */ | |
677 | |
678 static char * | |
679 gobble_line (fd, bufp, append_end) | |
680 int fd; | |
681 register struct buffer *bufp; | |
682 char *append_end; | |
683 { | |
684 register char *end; | |
685 register int nread; | |
686 register char *buf = bufp->beg; | |
687 register char *tem; | |
688 | |
689 if (!append_end) | |
690 append_end = bufp->ptr; | |
691 | |
692 while (1) | |
693 { | |
694 end = append_end; | |
695 while (*end && *end != '\n') end++; | |
696 if (*end) | |
697 break; | |
698 if (bufp->ateof) | |
699 return buf + bufp->full; | |
700 if (bufp->ptr == buf) | |
701 { | |
702 if (bufp->full == bufp->size) | |
703 { | |
704 bufp->size *= 2; | |
705 /* Add 1 to size to ensure room for terminating null. */ | |
706 tem = (char *) xrealloc (buf, bufp->size + 1); | |
707 bufp->ptr = (bufp->ptr - buf) + tem; | |
708 append_end = (append_end - buf) + tem; | |
709 bufp->beg = buf = tem; | |
710 } | |
711 } | |
712 else | |
713 { | |
714 append_end -= bufp->ptr - buf; | |
715 bcopy (bufp->ptr, buf, bufp->full -= bufp->ptr - buf); | |
716 bufp->ptr = buf; | |
717 } | |
718 if (!(nread = read (fd, buf + bufp->full, bufp->size - bufp->full))) | |
719 bufp->ateof = 1; | |
720 bufp->full += nread; | |
721 buf[bufp->full] = '\0'; | |
722 } | |
723 return end + 1; | |
724 } | |
725 | |
726 #ifdef TEST | |
727 | |
728 #ifdef NULL | |
729 #undef NULL | |
730 #endif | |
731 | |
732 #include <stdio.h> | |
733 | |
734 main (argc, argv) | |
735 int argc; | |
736 char **argv; | |
737 { | |
738 char *term; | |
739 char *buf; | |
740 | |
741 term = argv[1]; | |
742 printf ("TERM: %s\n", term); | |
743 | |
744 buf = (char *) tgetent (0, term); | |
745 if ((int) buf <= 0) | |
746 { | |
747 printf ("No entry.\n"); | |
748 return 0; | |
749 } | |
750 | |
751 printf ("Entry: %s\n", buf); | |
752 | |
753 tprint ("cm"); | |
754 tprint ("AL"); | |
755 | |
756 printf ("co: %d\n", tgetnum ("co")); | |
757 printf ("am: %d\n", tgetflag ("am")); | |
758 } | |
759 | |
760 tprint (cap) | |
761 char *cap; | |
762 { | |
763 char *x = tgetstr (cap, 0); | |
764 register char *y; | |
765 | |
766 printf ("%s: ", cap); | |
767 if (x) | |
768 { | |
769 for (y = x; *y; y++) | |
770 if (*y <= ' ' || *y == 0177) | |
771 printf ("\\%0o", *y); | |
772 else | |
773 putchar (*y); | |
774 free (x); | |
775 } | |
776 else | |
777 printf ("none"); | |
778 putchar ('\n'); | |
779 } | |
780 | |
781 #endif /* TEST */ | |
782 |