Mercurial > hg > xemacs-beta
annotate src/termcap.c @ 5276:dd2976af8783
Add some missing #includes, termcap.c, hopefully fixing Adam Sjoegren's build.
2010-09-18 Aidan Kehoe <kehoea@parhasard.net>
* termcap.c:
Add a couple of missing includes here, which should fix builds
that use this file. (I have no access to such builds, but Mats'
buildbot shows output that indicates they fail at link time since
DEVICE_BAUD_RATE and IS_DIRECTORY_SEP are available.)
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Sat, 18 Sep 2010 15:03:54 +0100 |
parents | 16112448d484 |
children | 308d34e9f07d |
rev | line source |
---|---|
428 | 1 /* Work-alike for termcap, plus extra features. |
2 Copyright (C) 1985, 1986, 1993 Free Software Foundation, Inc. | |
771 | 3 Copyright (C) 2001 Ben Wing. |
428 | 4 |
5 This file is part of XEmacs. | |
6 | |
7 XEmacs is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 2, or (at your option) any | |
10 later version. | |
11 | |
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with XEmacs; see the file COPYING. If not, write to | |
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 Boston, MA 02111-1307, USA. */ | |
21 | |
22 /* Synched up with: Not synched with FSF. */ | |
23 | |
24 /* config.h may rename various library functions such as malloc. */ | |
25 #ifdef emacs | |
26 #include <config.h> | |
27 #include "lisp.h" /* For encapsulated open, close, read */ | |
5276
dd2976af8783
Add some missing #includes, termcap.c, hopefully fixing Adam Sjoegren's build.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4976
diff
changeset
|
28 #include "device.h" |
dd2976af8783
Add some missing #includes, termcap.c, hopefully fixing Adam Sjoegren's build.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4976
diff
changeset
|
29 #include "device-impl.h" /* For DEVICE_BAUD_RATE */ |
dd2976af8783
Add some missing #includes, termcap.c, hopefully fixing Adam Sjoegren's build.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4976
diff
changeset
|
30 #include "sysfile.h" |
dd2976af8783
Add some missing #includes, termcap.c, hopefully fixing Adam Sjoegren's build.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4976
diff
changeset
|
31 #include "process.h" |
428 | 32 #else /* not emacs */ |
33 | |
34 #include <stdlib.h> | |
35 #include <string.h> | |
36 | |
37 #ifdef HAVE_UNISTD_H | |
38 #include <unistd.h> | |
39 #endif | |
40 #ifdef _POSIX_VERSION | |
41 #include <fcntl.h> | |
42 #endif | |
43 | |
44 #endif /* not emacs */ | |
45 | |
46 /* BUFSIZE is the initial size allocated for the buffer | |
47 for reading the termcap file. | |
48 It is not a limit. | |
49 Make it large normally for speed. | |
50 Make it variable when debugging, so can exercise | |
51 increasing the space dynamically. */ | |
52 | |
53 #ifndef BUFSIZE | |
54 #ifdef DEBUG | |
55 #define BUFSIZE bufsize | |
56 | |
57 int bufsize = 128; | |
58 #else | |
59 #define BUFSIZE 2048 | |
60 #endif | |
61 #endif | |
62 | |
63 #ifndef emacs | |
64 static void | |
65 memory_out () | |
66 { | |
771 | 67 retry_write (2, "virtual memory exhausted\n", 25); |
428 | 68 exit (1); |
69 } | |
70 | |
71 static char * | |
72 xmalloc (size) | |
73 unsigned int size; | |
74 { | |
75 char *tem = malloc (size); | |
76 | |
77 if (!tem) | |
78 memory_out (); | |
79 return tem; | |
80 } | |
81 | |
82 static char * | |
83 xrealloc (ptr, size) | |
84 char *ptr; | |
85 unsigned size; | |
86 { | |
87 char *tem = realloc (ptr, size); | |
88 | |
89 if (!tem) | |
90 memory_out (); | |
91 return tem; | |
92 } | |
93 #endif /* not emacs */ | |
94 | |
95 /* Looking up capabilities in the entry already found. */ | |
96 | |
97 /* The pointer to the data made by tgetent is left here | |
98 for tgetnum, tgetflag and tgetstr to find. */ | |
99 static char *term_entry; | |
100 | |
442 | 101 static const char *tgetst1 (const char *ptr, char **area); |
428 | 102 |
103 /* Search entry BP for capability CAP. | |
104 Return a pointer to the capability (in BP) if found, | |
105 0 if not found. */ | |
106 | |
442 | 107 static const char * |
428 | 108 find_capability (bp, cap) |
442 | 109 const char *bp; |
110 const char *cap; | |
428 | 111 { |
112 for (; *bp; bp++) | |
113 if (bp[0] == ':' | |
114 && bp[1] == cap[0] | |
115 && bp[2] == cap[1]) | |
116 return &bp[4]; | |
117 return 0; | |
118 } | |
119 | |
120 int | |
121 tgetnum (cap) | |
442 | 122 const char *cap; |
428 | 123 { |
442 | 124 const char *ptr = find_capability (term_entry, cap); |
428 | 125 if (!ptr || ptr[-1] != '#') |
126 return -1; | |
127 return atoi (ptr); | |
128 } | |
129 | |
130 int | |
131 tgetflag (cap) | |
442 | 132 const char *cap; |
428 | 133 { |
442 | 134 const char *ptr = find_capability (term_entry, cap); |
428 | 135 return 0 != ptr && ptr[-1] == ':'; |
136 } | |
137 | |
138 /* Look up a string-valued capability CAP. | |
139 If AREA is nonzero, it points to a pointer to a block in which | |
140 to store the string. That pointer is advanced over the space used. | |
141 If AREA is zero, space is allocated with `malloc'. */ | |
142 | |
442 | 143 const char * |
428 | 144 tgetstr (cap, area) |
442 | 145 const char *cap; |
428 | 146 char **area; |
147 { | |
442 | 148 const char *ptr = find_capability (term_entry, cap); |
428 | 149 if (!ptr || (ptr[-1] != '=' && ptr[-1] != '~')) |
150 return 0; | |
151 return tgetst1 (ptr, area); | |
152 } | |
153 | |
154 /* Table, indexed by a character in range 0100 to 0140 with 0100 subtracted, | |
155 gives meaning of character following \, or a space if no special meaning. | |
156 Eight characters per line within the string. */ | |
157 | |
158 static char esctab[] | |
159 = " \007\010 \033\014 " | |
160 " \012 " | |
161 " \015 \011 \013 " | |
162 " "; | |
163 | |
164 /* PTR points to a string value inside a termcap entry. | |
165 Copy that value, processing \ and ^ abbreviations, | |
166 into the block that *AREA points to, | |
167 or to newly allocated storage if AREA is 0. */ | |
168 | |
442 | 169 static const char * |
428 | 170 tgetst1 (ptr, area) |
442 | 171 const char *ptr; |
428 | 172 char **area; |
173 { | |
442 | 174 const char *p; |
428 | 175 char *r; |
176 int c; | |
177 int size; | |
178 char *ret; | |
179 int c1; | |
180 | |
181 if (!ptr) | |
182 return 0; | |
183 | |
184 /* `ret' gets address of where to store the string. */ | |
185 if (!area) | |
186 { | |
187 /* Compute size of block needed (may overestimate). */ | |
188 p = ptr; | |
189 while ((c = *p++) && c != ':' && c != '\n') | |
190 ; | |
191 ret = (char *) xmalloc (p - ptr + 1); | |
192 } | |
193 else | |
194 ret = *area; | |
195 | |
196 /* Copy the string value, stopping at null or colon. | |
197 Also process ^ and \ abbreviations. */ | |
198 p = ptr; | |
199 r = ret; | |
200 while ((c = *p++) && c != ':' && c != '\n') | |
201 { | |
202 if (c == '^') | |
203 c = *p++ & 037; | |
204 else if (c == '\\') | |
205 { | |
206 c = *p++; | |
207 if (c >= '0' && c <= '7') | |
208 { | |
209 c -= '0'; | |
210 size = 0; | |
211 | |
212 while (++size < 3 && (c1 = *p) >= '0' && c1 <= '7') | |
213 { | |
214 c *= 8; | |
215 c += c1 - '0'; | |
216 p++; | |
217 } | |
218 } | |
219 else if (c >= 0100 && c < 0200) | |
220 { | |
221 c1 = esctab[(c & ~040) - 0100]; | |
222 if (c1 != ' ') | |
223 c = c1; | |
224 } | |
225 } | |
226 *r++ = c; | |
227 } | |
228 *r = 0; | |
229 /* Update *AREA. */ | |
230 if (area) | |
231 *area = r + 1; | |
232 return ret; | |
233 } | |
234 | |
235 /* Outputting a string with padding. */ | |
236 | |
237 #ifdef LINUX | |
238 speed_t ospeed; | |
239 #else | |
240 short ospeed; | |
241 #endif | |
242 /* If `ospeed' is 0, we use `tputs_baud_rate' as the actual baud rate. */ | |
243 int tputs_baud_rate; | |
244 char PC; | |
245 | |
246 /* Actual baud rate if positive; | |
247 - baud rate / 100 if negative. */ | |
248 | |
249 static short speeds[] = | |
250 { | |
251 0, 50, 75, 110, 135, 150, -2, -3, -6, -12, | |
252 -18, -24, -48, -96, -192, -288, -384, -576, -1152 | |
253 }; | |
254 | |
255 void | |
256 tputs (string, nlines, outfun) | |
442 | 257 const char *string; |
428 | 258 int nlines; |
259 void (*outfun) (int); | |
260 { | |
261 int padcount = 0; | |
262 int speed; | |
263 | |
264 #ifdef emacs | |
265 speed = DEVICE_BAUD_RATE (XDEVICE (Fselected_device (Qnil))); | |
266 #else | |
267 if (ospeed == 0) | |
268 speed = tputs_baud_rate; | |
269 else | |
270 speed = speeds[ospeed]; | |
271 #endif | |
272 | |
273 if (string == (char *) 0) | |
274 return; | |
275 | |
442 | 276 while (isdigit (* (const unsigned char *) string)) |
428 | 277 { |
278 padcount += *string++ - '0'; | |
279 padcount *= 10; | |
280 } | |
281 if (*string == '.') | |
282 { | |
283 string++; | |
284 padcount += *string++ - '0'; | |
285 } | |
286 if (*string == '*') | |
287 { | |
288 string++; | |
289 padcount *= nlines; | |
290 } | |
291 while (*string) | |
292 (*outfun) (*string++); | |
293 | |
294 /* padcount is now in units of tenths of msec. */ | |
295 padcount *= speeds[ospeed]; | |
296 padcount += 500; | |
297 padcount /= 1000; | |
298 if (speeds[ospeed] < 0) | |
299 padcount = -padcount; | |
300 else | |
301 { | |
302 padcount += 50; | |
303 padcount /= 100; | |
304 } | |
305 | |
306 while (padcount-- > 0) | |
307 (*outfun) (PC); | |
308 } | |
309 | |
310 /* Finding the termcap entry in the termcap data base. */ | |
311 | |
312 struct buffer | |
313 { | |
314 char *beg; | |
315 int size; | |
316 char *ptr; | |
317 int ateof; | |
318 int full; | |
319 }; | |
320 | |
321 /* Forward declarations of static functions. */ | |
322 | |
323 static int scan_file (); | |
324 static char *gobble_line (); | |
325 static int compare_contin (); | |
326 static int name_match (); | |
327 | |
328 | |
329 /* Find the termcap entry data for terminal type NAME | |
330 and store it in the block that BP points to. | |
331 Record its address for future use. | |
332 | |
333 If BP is zero, space is dynamically allocated. */ | |
334 | |
335 int | |
336 tgetent (bp, name) | |
337 char *bp; | |
442 | 338 const char *name; |
428 | 339 { |
340 char *tem; | |
341 int fd; | |
342 struct buffer buf; | |
343 char *bp1; | |
344 char *bp2; | |
442 | 345 const char *term; |
428 | 346 int malloc_size = 0; |
347 int c; | |
442 | 348 char *tcenv; /* TERMCAP value, if it contains :tc=. */ |
349 const char *indirect = 0; /* Terminal type in :tc= in TERMCAP value. */ | |
428 | 350 |
771 | 351 tem = egetenv ("TERMCAP"); |
428 | 352 if (tem && *tem == 0) tem = 0; |
353 | |
354 | |
355 /* If tem is non-null and starts with / (in the un*x case, that is), | |
356 it is a file name to use instead of /etc/termcap. | |
357 If it is non-null and does not start with /, | |
358 it is the entry itself, but only if | |
359 the name the caller requested matches the TERM variable. */ | |
360 | |
771 | 361 if (tem && !IS_DIRECTORY_SEP (*tem) && !strcmp (name, egetenv ("TERM"))) |
428 | 362 { |
363 indirect = tgetst1 (find_capability (tem, "tc"), 0); | |
364 if (!indirect) | |
365 { | |
366 if (!bp) | |
367 bp = tem; | |
368 else | |
369 strcpy (bp, tem); | |
370 goto ret; | |
371 } | |
372 else | |
373 { /* We will need to read /etc/termcap. */ | |
374 tcenv = tem; | |
375 tem = 0; | |
376 } | |
377 } | |
378 else | |
379 indirect = (char *) 0; | |
380 | |
381 if (!tem) | |
382 tem = "/etc/termcap"; | |
383 | |
384 /* Here we know we must search a file and tem has its name. */ | |
385 | |
867 | 386 fd = qxe_open ((Ibyte *) tem, 0, 0); |
428 | 387 if (fd < 0) |
388 return -1; | |
389 | |
390 buf.size = BUFSIZE; | |
391 /* Add 1 to size to ensure room for terminating null. */ | |
392 buf.beg = (char *) xmalloc (buf.size + 1); | |
393 term = indirect ? indirect : name; | |
394 | |
395 if (!bp) | |
396 { | |
397 malloc_size = indirect ? strlen (tcenv) + 1 : buf.size; | |
398 bp = (char *) xmalloc (malloc_size); | |
399 } | |
400 bp1 = bp; | |
401 | |
402 if (indirect) | |
403 /* Copy the data from the environment variable. */ | |
404 { | |
405 strcpy (bp, tcenv); | |
406 bp1 += strlen (tcenv); | |
407 } | |
408 | |
409 while (term) | |
410 { | |
411 /* Scan the file, reading it via buf, till find start of main entry. */ | |
412 if (scan_file (term, fd, &buf) == 0) | |
413 return 0; | |
414 | |
415 /* Free old `term' if appropriate. */ | |
416 if (term != name) | |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
1726
diff
changeset
|
417 xfree (term); |
428 | 418 |
419 /* If BP is malloc'd by us, make sure it is big enough. */ | |
420 if (malloc_size) | |
421 { | |
422 malloc_size = bp1 - bp + buf.size; | |
423 tem = (char *) xrealloc (bp, malloc_size); | |
424 bp1 += tem - bp; | |
425 bp = tem; | |
426 } | |
427 | |
428 bp2 = bp1; | |
429 | |
430 /* Copy the line of the entry from buf into bp. */ | |
431 tem = buf.ptr; | |
432 while ((*bp1++ = c = *tem++) && c != '\n') | |
433 /* Drop out any \ newline sequence. */ | |
434 if (c == '\\' && *tem == '\n') | |
435 { | |
436 bp1--; | |
437 tem++; | |
438 } | |
439 *bp1 = 0; | |
440 | |
441 /* Does this entry refer to another terminal type's entry? | |
442 If something is found, copy it into heap and null-terminate it. */ | |
443 term = tgetst1 (find_capability (bp2, "tc"), 0); | |
444 } | |
445 | |
771 | 446 retry_close (fd); |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
1726
diff
changeset
|
447 xfree (buf.beg); |
428 | 448 |
449 if (malloc_size) | |
450 { | |
451 bp = (char *) xrealloc (bp, bp1 - bp + 1); | |
452 } | |
453 | |
454 ret: | |
455 term_entry = bp; | |
456 if (malloc_size) | |
457 /* #### yuck, why the hell are we casting a pointer to an int? */ | |
458 return (int) (long) bp; | |
459 return 1; | |
460 } | |
461 | |
462 /* Given file open on FD and buffer BUFP, | |
463 scan the file from the beginning until a line is found | |
464 that starts the entry for terminal type STRING. | |
465 Returns 1 if successful, with that line in BUFP, | |
466 or returns 0 if no entry found in the file. */ | |
467 | |
468 static int | |
469 scan_file (string, fd, bufp) | |
470 char *string; | |
471 int fd; | |
472 struct buffer *bufp; | |
473 { | |
474 char *end; | |
475 | |
476 bufp->ptr = bufp->beg; | |
477 bufp->full = 0; | |
478 bufp->ateof = 0; | |
479 *bufp->ptr = 0; | |
480 | |
481 lseek (fd, 0L, 0); | |
482 | |
483 while (!bufp->ateof) | |
484 { | |
485 /* Read a line into the buffer. */ | |
486 end = 0; | |
487 do | |
488 { | |
489 /* if it is continued, append another line to it, | |
490 until a non-continued line ends. */ | |
491 end = gobble_line (fd, bufp, end); | |
492 } | |
493 while (!bufp->ateof && end[-2] == '\\'); | |
494 | |
495 if (*bufp->ptr != '#' | |
496 && name_match (bufp->ptr, string)) | |
497 return 1; | |
498 | |
499 /* Discard the line just processed. */ | |
500 bufp->ptr = end; | |
501 } | |
502 return 0; | |
503 } | |
504 | |
505 /* Return nonzero if NAME is one of the names specified | |
506 by termcap entry LINE. */ | |
507 | |
508 static int | |
509 name_match (line, name) | |
510 char *line, *name; | |
511 { | |
512 char *tem; | |
513 | |
514 if (!compare_contin (line, name)) | |
515 return 1; | |
516 /* This line starts an entry. Is it the right one? */ | |
517 for (tem = line; *tem && *tem != '\n' && *tem != ':'; tem++) | |
518 if (*tem == '|' && !compare_contin (tem + 1, name)) | |
519 return 1; | |
520 | |
521 return 0; | |
522 } | |
523 | |
524 static int | |
525 compare_contin (str1, str2) | |
526 char *str1, *str2; | |
527 { | |
528 int c1, c2; | |
529 while (1) | |
530 { | |
531 c1 = *str1++; | |
532 c2 = *str2++; | |
533 while (c1 == '\\' && *str1 == '\n') | |
534 { | |
535 str1++; | |
536 while ((c1 = *str1++) == ' ' || c1 == '\t'); | |
537 } | |
538 if (c2 == '\0') | |
539 { | |
540 /* End of type being looked up. */ | |
541 if (c1 == '|' || c1 == ':') | |
542 /* If end of name in data base, we win. */ | |
543 return 0; | |
544 else | |
545 return 1; | |
546 } | |
547 else if (c1 != c2) | |
548 return 1; | |
549 } | |
550 } | |
551 | |
552 /* Make sure that the buffer <- BUFP contains a full line | |
553 of the file open on FD, starting at the place BUFP->ptr | |
554 points to. Can read more of the file, discard stuff before | |
555 BUFP->ptr, or make the buffer bigger. | |
556 | |
557 Returns the pointer to after the newline ending the line, | |
558 or to the end of the file, if there is no newline to end it. | |
559 | |
560 Can also merge on continuation lines. If APPEND_END is | |
561 nonzero, it points past the newline of a line that is | |
562 continued; we add another line onto it and regard the whole | |
563 thing as one line. The caller decides when a line is continued. */ | |
564 | |
565 static char * | |
566 gobble_line (fd, bufp, append_end) | |
567 int fd; | |
568 struct buffer *bufp; | |
569 char *append_end; | |
570 { | |
571 char *end; | |
572 int nread; | |
573 char *buf = bufp->beg; | |
574 char *tem; | |
575 | |
576 if (append_end == 0) | |
577 append_end = bufp->ptr; | |
578 | |
579 while (1) | |
580 { | |
581 end = append_end; | |
582 while (*end && *end != '\n') end++; | |
583 if (*end) | |
584 break; | |
585 if (bufp->ateof) | |
586 return buf + bufp->full; | |
587 if (bufp->ptr == buf) | |
588 { | |
589 if (bufp->full == bufp->size) | |
590 { | |
591 bufp->size *= 2; | |
592 /* Add 1 to size to ensure room for terminating null. */ | |
593 tem = (char *) xrealloc (buf, bufp->size + 1); | |
594 bufp->ptr = (bufp->ptr - buf) + tem; | |
595 append_end = (append_end - buf) + tem; | |
596 bufp->beg = buf = tem; | |
597 } | |
598 } | |
599 else | |
600 { | |
601 append_end -= bufp->ptr - buf; | |
602 memcpy (buf, bufp->ptr, bufp->full -= bufp->ptr - buf); | |
603 bufp->ptr = buf; | |
604 } | |
771 | 605 if (!(nread = retry_read (fd, buf + bufp->full, bufp->size - bufp->full))) |
428 | 606 bufp->ateof = 1; |
607 bufp->full += nread; | |
608 buf[bufp->full] = 0; | |
609 } | |
610 return end + 1; | |
611 } | |
612 | |
613 #ifdef TEST | |
614 | |
615 #include <stdio.h> | |
616 | |
617 main (argc, argv) | |
618 int argc; | |
619 char **argv; | |
620 { | |
621 char *term; | |
622 char *buf; | |
623 | |
624 term = argv[1]; | |
625 printf ("TERM: %s\n", term); | |
626 | |
627 buf = (char *) tgetent (0, term); | |
628 if ((int) buf <= 0) | |
629 { | |
630 printf ("No entry.\n"); | |
631 return 0; | |
632 } | |
633 | |
634 printf ("Entry: %s\n", buf); | |
635 | |
636 tprint ("cm"); | |
637 tprint ("AL"); | |
638 | |
639 printf ("co: %d\n", tgetnum ("co")); | |
640 printf ("am: %d\n", tgetflag ("am")); | |
641 } | |
642 | |
643 tprint (cap) | |
442 | 644 const char *cap; |
428 | 645 { |
646 char *x = tgetstr (cap, 0); | |
647 char *y; | |
648 | |
649 printf ("%s: ", cap); | |
650 if (x) | |
651 { | |
652 for (y = x; *y; y++) | |
653 if (*y <= ' ' || *y == 0177) | |
654 printf ("\\%0o", *y); | |
655 else | |
656 putchar (*y); | |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
1726
diff
changeset
|
657 xfree (x); |
428 | 658 } |
659 else | |
660 printf ("none"); | |
661 putchar ('\n'); | |
662 } | |
663 | |
664 #endif /* TEST */ | |
665 |