1 /* Work-alike for termcap, plus extra features.
2 Copyright (C) 1985, 1986, 1993, 1994 Free Software Foundation, Inc.
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)
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.
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. */
18 /* Emacs config.h may rename various library functions such as malloc. */
23 /* Get the O_* definitions for open et al. */
29 #else /* not HAVE_CONFIG_H */
31 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
32 #define bcopy(s, d, n) memcpy ((d), (s), (n))
51 #endif /* not HAVE_CONFIG_H */
54 #define NULL (char *) 0
61 /* BUFSIZE is the initial size allocated for the buffer
62 for reading the termcap file.
64 Make it large normally for speed.
65 Make it variable when debugging, so can exercise
66 increasing the space dynamically. */
70 #define BUFSIZE bufsize
79 #define TERMCAP_NAME "/etc/termcap"
86 write (2, "virtual memory exhausted\n", 25);
94 register char *tem
= malloc (size
);
106 register char *tem
= realloc (ptr
, size
);
112 #endif /* not emacs */
114 /* Looking up capabilities in the entry already found. */
116 /* The pointer to the data made by tgetent is left here
117 for tgetnum, tgetflag and tgetstr to find. */
118 static char *term_entry
;
120 static char *tgetst1 ();
122 /* Search entry BP for capability CAP.
123 Return a pointer to the capability (in BP) if found,
127 find_capability (bp
, cap
)
128 register char *bp
, *cap
;
142 register char *ptr
= find_capability (term_entry
, cap
);
143 if (!ptr
|| ptr
[-1] != '#')
152 register char *ptr
= find_capability (term_entry
, cap
);
153 return ptr
&& ptr
[-1] == ':';
156 /* Look up a string-valued capability CAP.
157 If AREA is non-null, it points to a pointer to a block in which
158 to store the string. That pointer is advanced over the space used.
159 If AREA is null, space is allocated with `malloc'. */
166 register char *ptr
= find_capability (term_entry
, cap
);
167 if (!ptr
|| (ptr
[-1] != '=' && ptr
[-1] != '~'))
169 return tgetst1 (ptr
, area
);
172 /* Table, indexed by a character in range 0100 to 0140 with 0100 subtracted,
173 gives meaning of character following \, or a space if no special meaning.
174 Eight characters per line within the string. */
177 = " \007\010 \033\014 \
182 /* PTR points to a string value inside a termcap entry.
183 Copy that value, processing \ and ^ abbreviations,
184 into the block that *AREA points to,
185 or to newly allocated storage if AREA is NULL.
186 Return the address to which we copied the value,
187 or NULL if PTR is NULL. */
194 register char *p
, *r
;
203 /* `ret' gets address of where to store the string. */
206 /* Compute size of block needed (may overestimate). */
208 while ((c
= *p
++) && c
!= ':' && c
!= '\n')
210 ret
= (char *) xmalloc (p
- ptr
+ 1);
215 /* Copy the string value, stopping at null or colon.
216 Also process ^ and \ abbreviations. */
219 while ((c
= *p
++) && c
!= ':' && c
!= '\n')
226 if (c
>= '0' && c
<= '7')
231 while (++size
< 3 && (c1
= *p
) >= '0' && c1
<= '7')
238 else if (c
>= 0100 && c
< 0200)
240 c1
= esctab
[(c
& ~040) - 0100];
254 /* Outputting a string with padding. */
257 /* If OSPEED is 0, we use this as the actual baud rate. */
261 /* Actual baud rate if positive;
262 - baud rate / 100 if negative. */
264 static short speeds
[] =
267 0, 50, 75, 110, 134, 150, -3, -6, -12, -18,
268 -20, -24, -36, -48, -72, -96, -192
270 0, 50, 75, 110, 135, 150, -2, -3, -6, -12,
271 -18, -24, -48, -96, -192, -384
276 tputs (str
, nlines
, outfun
)
279 register int (*outfun
) ();
281 register int padcount
= 0;
289 speed
= tputs_baud_rate
;
291 speed
= speeds
[ospeed
];
297 while (*str
>= '0' && *str
<= '9')
299 padcount
+= *str
++ - '0';
305 padcount
+= *str
++ - '0';
315 /* padcount is now in units of tenths of msec. */
316 padcount
*= speeds
[ospeed
];
319 if (speeds
[ospeed
] < 0)
320 padcount
= -padcount
;
327 while (padcount
-- > 0)
331 /* Finding the termcap entry in the termcap data base. */
342 /* Forward declarations of static functions. */
344 static int scan_file ();
345 static char *gobble_line ();
346 static int compare_contin ();
347 static int name_match ();
356 valid_filename_p (fn
)
359 struct FAB fab
= cc$rms_fab
;
360 struct NAM nam
= cc$rms_nam
;
361 char esa
[NAM$C_MAXRSS
];
364 fab
.fab$b_fns
= strlen(fn
);
365 fab
.fab$l_nam
= &nam
;
366 fab
.fab$l_fop
= FAB$M_NAM
;
369 nam
.nam$b_ess
= sizeof esa
;
371 return SYS$
PARSE(&fab
, 0, 0) == RMS$_NORMAL
;
376 #ifdef MSDOS /* MW, May 1993 */
378 valid_filename_p (fn
)
381 return *fn
== '/' || fn
[1] == ':';
384 #define valid_filename_p(fn) (*(fn) == '/')
389 /* Find the termcap entry data for terminal type NAME
390 and store it in the block that BP points to.
391 Record its address for future use.
393 If BP is null, space is dynamically allocated.
395 Return -1 if there is some difficulty accessing the data base
397 0 if the data base is accessible but the type NAME is not defined
398 in it, and some other value otherwise. */
404 register char *termcap_name
;
412 char *tcenv
; /* TERMCAP value, if it contains :tc=. */
413 char *indirect
= NULL
; /* Terminal type in :tc= in TERMCAP value. */
416 #ifdef INTERNAL_TERMINAL
417 /* For the internal terminal we don't want to read any termcap file,
419 if (!strcmp (name
, "internal"))
421 term
= INTERNAL_TERMINAL
;
424 malloc_size
= 1 + strlen (term
);
425 bp
= (char *) xmalloc (malloc_size
);
430 #endif /* INTERNAL_TERMINAL */
432 termcap_name
= getenv ("TERMCAP");
433 if (termcap_name
&& *termcap_name
== '\0')
435 #if defined (MSDOS) && !defined (TEST)
436 if (termcap_name
&& (*termcap_name
== '\\'
437 || *termcap_name
== '/'
438 || termcap_name
[1] == ':'))
439 dostounix_filename(termcap_name
);
442 filep
= termcap_name
&& valid_filename_p (termcap_name
);
444 /* If termcap_name is non-null and starts with / (in the un*x case, that is),
445 it is a file name to use instead of /etc/termcap.
446 If it is non-null and does not start with /,
447 it is the entry itself, but only if
448 the name the caller requested matches the TERM variable. */
450 if (termcap_name
&& !filep
&& !strcmp (name
, getenv ("TERM")))
452 indirect
= tgetst1 (find_capability (termcap_name
, "tc"), (char **) 0);
458 strcpy (bp
, termcap_name
);
462 { /* It has tc=. Need to read /etc/termcap. */
463 tcenv
= termcap_name
;
468 if (!termcap_name
|| !filep
)
469 termcap_name
= TERMCAP_NAME
;
471 /* Here we know we must search a file and termcap_name has its name. */
474 fd
= open (termcap_name
, O_RDONLY
|O_TEXT
, 0);
476 fd
= open (termcap_name
, O_RDONLY
, 0);
482 /* Add 1 to size to ensure room for terminating null. */
483 buf
.beg
= (char *) xmalloc (buf
.size
+ 1);
484 term
= indirect
? indirect
: name
;
488 malloc_size
= indirect
? strlen (tcenv
) + 1 : buf
.size
;
489 bp
= (char *) xmalloc (malloc_size
);
494 /* Copy the data from the environment variable. */
497 bp1
+= strlen (tcenv
);
502 /* Scan the file, reading it via buf, till find start of main entry. */
503 if (scan_file (term
, fd
, &buf
) == 0)
512 /* Free old `term' if appropriate. */
516 /* If BP is malloc'd by us, make sure it is big enough. */
519 malloc_size
= bp1
- bp
+ buf
.size
;
520 termcap_name
= (char *) xrealloc (bp
, malloc_size
);
521 bp1
+= termcap_name
- bp
;
527 /* Copy the line of the entry from buf into bp. */
528 termcap_name
= buf
.ptr
;
529 while ((*bp1
++ = c
= *termcap_name
++) && c
!= '\n')
530 /* Drop out any \ newline sequence. */
531 if (c
== '\\' && *termcap_name
== '\n')
538 /* Does this entry refer to another terminal type's entry?
539 If something is found, copy it into heap and null-terminate it. */
540 term
= tgetst1 (find_capability (bp2
, "tc"), (char **) 0);
547 bp
= (char *) xrealloc (bp
, bp1
- bp
+ 1);
556 /* Given file open on FD and buffer BUFP,
557 scan the file from the beginning until a line is found
558 that starts the entry for terminal type STR.
559 Return 1 if successful, with that line in BUFP,
560 or 0 if no entry is found in the file. */
563 scan_file (str
, fd
, bufp
)
566 register struct buffer
*bufp
;
570 bufp
->ptr
= bufp
->beg
;
579 /* Read a line into the buffer. */
583 /* if it is continued, append another line to it,
584 until a non-continued line ends. */
585 end
= gobble_line (fd
, bufp
, end
);
587 while (!bufp
->ateof
&& end
[-2] == '\\');
589 if (*bufp
->ptr
!= '#'
590 && name_match (bufp
->ptr
, str
))
593 /* Discard the line just processed. */
599 /* Return nonzero if NAME is one of the names specified
600 by termcap entry LINE. */
603 name_match (line
, name
)
608 if (!compare_contin (line
, name
))
610 /* This line starts an entry. Is it the right one? */
611 for (tem
= line
; *tem
&& *tem
!= '\n' && *tem
!= ':'; tem
++)
612 if (*tem
== '|' && !compare_contin (tem
+ 1, name
))
619 compare_contin (str1
, str2
)
620 register char *str1
, *str2
;
627 while (c1
== '\\' && *str1
== '\n')
630 while ((c1
= *str1
++) == ' ' || c1
== '\t');
634 /* End of type being looked up. */
635 if (c1
== '|' || c1
== ':')
636 /* If end of name in data base, we win. */
646 /* Make sure that the buffer <- BUFP contains a full line
647 of the file open on FD, starting at the place BUFP->ptr
648 points to. Can read more of the file, discard stuff before
649 BUFP->ptr, or make the buffer bigger.
651 Return the pointer to after the newline ending the line,
652 or to the end of the file, if there is no newline to end it.
654 Can also merge on continuation lines. If APPEND_END is
655 non-null, it points past the newline of a line that is
656 continued; we add another line onto it and regard the whole
657 thing as one line. The caller decides when a line is continued. */
660 gobble_line (fd
, bufp
, append_end
)
662 register struct buffer
*bufp
;
667 register char *buf
= bufp
->beg
;
671 append_end
= bufp
->ptr
;
676 while (*end
&& *end
!= '\n') end
++;
680 return buf
+ bufp
->full
;
681 if (bufp
->ptr
== buf
)
683 if (bufp
->full
== bufp
->size
)
686 /* Add 1 to size to ensure room for terminating null. */
687 tem
= (char *) xrealloc (buf
, bufp
->size
+ 1);
688 bufp
->ptr
= (bufp
->ptr
- buf
) + tem
;
689 append_end
= (append_end
- buf
) + tem
;
690 bufp
->beg
= buf
= tem
;
695 append_end
-= bufp
->ptr
- buf
;
696 bcopy (bufp
->ptr
, buf
, bufp
->full
-= bufp
->ptr
- buf
);
699 if (!(nread
= read (fd
, buf
+ bufp
->full
, bufp
->size
- bufp
->full
)))
702 buf
[bufp
->full
] = '\0';
723 printf ("TERM: %s\n", term
);
725 buf
= (char *) tgetent (0, term
);
728 printf ("No entry.\n");
732 printf ("Entry: %s\n", buf
);
737 printf ("co: %d\n", tgetnum ("co"));
738 printf ("am: %d\n", tgetflag ("am"));
744 char *x
= tgetstr (cap
, 0);
747 printf ("%s: ", cap
);
751 if (*y
<= ' ' || *y
== 0177)
752 printf ("\\%0o", *y
);