2 * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
7 * Copyright (c) 1983 Regents of the University of California.
8 * All rights reserved. The Berkeley software License Agreement
9 * specifies the terms and conditions for redistribution.
12 #pragma ident "%Z%%M% %I% %E% SMI"
15 * remcap - routines for dealing with the remote host data base
17 * derived from termcap
20 #include <sys/types.h>
21 #include <fcntl.h> /* for O_RDONLY */
23 #include <sys/file.h> /* for O_RDONLY */
35 #define MAXHOP 32 /* max number of tc= indirections */
36 #define SYSREMOTE "/etc/remote" /* system remote file */
38 #define tgetent rgetent
39 #define tnchktc rnchktc
40 #define tnamatch rnamatch
41 #define tgetnum rgetnum
42 #define tgetflag rgetflag
43 #define tgetstr rgetstr
44 #define E_TERMCAP RM = SYSREMOTE
45 #define V_TERMCAP "REMOTE"
51 * termcap - routines for dealing with the terminal capability data base
53 * BUG: Should use a "last" pointer in tbuf, so that searching
54 * for capabilities alphabetically would not be a n**2/2
55 * process when large numbers of capabilities are given.
56 * Note: If we add a last pointer now we will screw up the
57 * tc capability. We really should compile termcap.
59 * Essentially all the work here is scanning and decoding escapes
60 * in string capabilities. We don't use stdio because the editor
61 * doesn't, and because living w/o it is not hard.
65 static int hopcount
; /* detect infinite loops in termcap, init 0 */
66 static char *remotefile
;
68 static char *tskip(char *);
69 static char *tdecode(char *, char **);
71 char *tgetstr(char *, char **);
72 int getent(char *, char *, char *, int);
76 extern void myperm(void);
77 extern void userperm(void);
80 * If we use a user specified entry to get the device name,
81 * we need to open the device as the user.
83 int trusted_device
= 0;
86 * Get an entry for terminal name in buffer bp,
87 * from the termcap file. Parse is very rudimentary;
88 * we just notice escaped newlines.
91 tgetent(char *bp
, char *name
, int len
)
93 char lbuf
[BUFSIZ
], *cp
, *p
;
98 remotefile
= cp
= getenv(V_TERMCAP
);
99 if (cp
== (char *)0 || strcmp(cp
, SYSREMOTE
) == 0) {
100 remotefile
= cp
= SYSREMOTE
;
101 return (getent(bp
, name
, cp
, len
));
103 if ((rc1
= getent(bp
, name
, cp
, len
)) != 1)
105 remotefile
= cp
= SYSREMOTE
;
106 rc2
= getent(lbuf
, name
, cp
, sizeof (lbuf
));
107 if (rc1
!= 1 && rc2
!= 1)
114 if (strlen(bp
) + strlen(p
) >= len
) {
115 (void) write(2, "Remcap entry too long\n", 23);
118 (void) strcat(bp
, p
);
126 getent(char *bp
, char *name
, char *cp
, int len
)
130 char ibuf
[BUFSIZ
], *cp2
;
132 int safe
= 1; /* reset only when we open the user's $REMOTE */
137 * TERMCAP can have one of two things in it. It can be the
138 * name of a file to use instead of /etc/termcap. In this
139 * case it better start with a "/". Or it can be an entry to
140 * use so we don't have to read the file. In this case it
141 * has to already have the newlines crunched out.
145 cp2
= getenv(V_TERM
);
146 if (cp2
== (char *)0 || strcmp(name
, cp2
) == 0) {
147 if (strstr(cp
, "dv=") != 0)
149 (void) strncpy(bp
, cp
, len
-1);
153 tf
= open(E_TERMCAP
, O_RDONLY
);
155 /* open SYSREMOTE as uucp, other files as user */
156 safe
= strcmp(cp
, SYSREMOTE
) == 0;
159 tf
= open(RM
= cp
, O_RDONLY
);
165 tf
= open(E_TERMCAP
, O_RDONLY
);
172 cnt
= read(tf
, ibuf
, BUFSIZ
);
181 if (cp
> bp
&& cp
[-1] == '\\') {
188 (void) write(2, "Remcap entry too long\n", 23);
196 * The real work for the match.
198 if (tnamatch(name
)) {
200 * if a dv= entry is obtained from $REMOTE,
201 * switch off trusted_device status
203 if (!safe
&& strstr(bp
, "dv=") != 0)
212 * tnchktc: check the last entry, see if it's tc=xxx. If so,
213 * recursively find xxx and append that entry (minus the names)
214 * to take the place of the tc=xxx entry. This allows termcap
215 * entries to say "like an HP2621 but doesn't turn on the labels".
216 * Note that this works because of the left to right scan.
222 char tcname
[64]; /* name of similar terminal */
224 char *holdtbuf
= tbuf
;
227 p
= tbuf
+ strlen(tbuf
) - 2; /* before the last colon */
230 (void) write(2, "Bad remcap entry\n", 18);
234 /* p now points to beginning of last field */
235 if (p
[0] != 't' || p
[1] != 'c')
237 (void) strlcpy(tcname
, p
+3, sizeof (tcname
));
239 while (*q
&& *q
!= ':')
242 if (++hopcount
> MAXHOP
) {
243 (void) write(2, "Infinite tc= loop\n", 18);
246 if (getent(tcbuf
, tcname
, remotefile
, sizeof (tcbuf
)) != 1) {
247 if (strcmp(remotefile
, SYSREMOTE
) == 0)
249 else if (getent(tcbuf
, tcname
, SYSREMOTE
, sizeof (tcbuf
)) != 1)
252 for (q
= tcbuf
; *q
++ != ':'; )
254 l
= p
- holdtbuf
+ strlen(q
);
256 (void) write(2, "Remcap entry too long\n", 23);
257 q
[BUFSIZ
- (p
-holdtbuf
)] = 0;
265 * Tnamatch deals with name matching. The first field of the termcap
266 * entry is a sequence of names separated by |'s, so we compare
267 * against each such name. The normal : terminator after the last
268 * name (before the first field) stops us.
279 for (Np
= np
; *Np
&& *Bp
== *Np
; Bp
++, Np
++)
281 if (*Np
== 0 && (*Bp
== '|' || *Bp
== ':' || *Bp
== 0))
283 while (*Bp
&& *Bp
!= ':' && *Bp
!= '|')
285 if (*Bp
== 0 || *Bp
== ':')
292 * Skip to the next field. Notice that this is very dumb, not
293 * knowing about \: escapes or any such. If necessary, :'s can be put
294 * into the termcap file in octal.
300 while (*bp
&& *bp
!= ':')
307 } while (*bp
== ':');
313 * Return the (numeric) option id.
314 * Numeric options look like
316 * i.e. the option string is separated from the numeric value by
317 * a # character. If the option is not found we return -1.
318 * Note that we handle octal numbers beginning with 0.
330 if (*bp
++ != id
[0] || *bp
== 0 || *bp
++ != id
[1])
342 i
*= base
, i
+= *bp
++ - '0';
348 * Handle a flag option.
349 * Flag options are given "naked", i.e. followed by a : or the end
350 * of the buffer. Return 1 if we find the option, or 0 if it is
362 if (*bp
++ == id
[0] && *bp
!= 0 && *bp
++ == id
[1]) {
363 if (!*bp
|| *bp
== ':')
372 * Get a string valued option.
375 * Much decoding is done on the strings, and the strings are
376 * placed in area, which is a ref parameter which is updated.
377 * No checking on area overflow.
380 tgetstr(char *id
, char **area
)
388 if (*bp
++ != id
[0] || *bp
== 0 || *bp
++ != id
[1])
395 return (tdecode(bp
, area
));
400 * Tdecode does the grung work to decode the
401 * string capability escapes.
404 tdecode(char *str
, char **area
)
412 while ((c
= *str
++) != 0 && c
!= ':') {
420 dp
= "E\033^^\\\\::n\nr\rt\tb\bf\f";
433 c
<<= 3, c
|= *str
++ - '0';
434 while (--i
&& isdigit(*str
))