BSD 1 development
[unix-history] / s7 / ttycap.c
CommitLineData
8c2df56f
BJ
1/*
2 * ttycap - routines for dealing with the teletype capability data base
3 * Bill Joy UCB September 25, 1977
4 */
5
6char ttycap[] "/etc/ttycap";
7
8extern char *tbuf;
9
10tgetent(bp, name)
11 char *bp, *name;
12{
13 register char *cp;
14 register int cap, c;
15 int ibuf[259];
16
17 tbuf = bp;
18 cap = fopen(ttycap, ibuf);
19 if (cap < 0)
20 return (-1);
21 for (;;) {
22 for (cp = bp, c = getc(ibuf); c != -1; c = getc(ibuf)) {
23 if (c == '\n') {
24 if (cp > bp && cp[-1] == '\\'){
25 cp--;
26 continue;
27 }
28 break;
29 }
30 *cp++ = c;
31 }
32 *cp = 0;
33 if (c == -1) {
34 close(ibuf[0]);
35 return (0);
36 }
37 if (tnamatch(name)) {
38 close(ibuf[0]);
39 return (1);
40 }
41 }
42}
43
44tnamatch(np)
45 char *np;
46{
47 register char *Np, *Bp;
48
49 for (Bp = tbuf; *Bp && *Bp != ':'; Bp++) {
50 for (Np = np; *Np && *Bp != '|' && *Bp != ':' && *Bp == *Np; Bp++, Np++)
51 continue;
52 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
53 return (1);
54 while (*Bp && *Bp != ':' && *Bp != '|')
55 Bp++;
56 if (*Bp == 0)
57 break;
58 }
59 return (0);
60}
61
62tskip(bp, i)
63 register char *bp;
64 register int i;
65{
66
67 for (; i > 0; i--) {
68 while (*bp && *bp != ':')
69 bp++;
70 if (*bp == ':')
71 bp++;
72 }
73 return (bp);
74}
75
76tgetnum(id)
77 char *id;
78{
79 register int i, base;
80 register char *bp;
81
82 for (bp = tskip(tbuf, 3); *bp != 0; bp = tskip(bp, 1)) {
83 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
84 continue;
85 if (*bp != '#')
86 continue;
87 bp++;
88 base = 10;
89 if (*bp == '0')
90 base = 8;
91 i = 0;
92 while (*bp >= '0' && *bp <= '9')
93 i =* base, i =+ *bp++ - '0';
94 return (i);
95 }
96 return (-1);
97}
98
99tgetflag(id)
100 char *id;
101{
102 register char *bp;
103
104 for (bp = tskip(tbuf, 3); *bp; bp = tskip(bp, 1))
105 if (*bp++ == id[0] && *bp != 0 && *bp == id[1])
106 return (1);
107 return (0);
108}
109
110tgetstr(id, area)
111 char *id, **area;
112{
113 register char *cp;
114 register int i;
115 register char *bp;
116
117 for (bp = tskip(tbuf, 3); *bp != 0; bp = tskip(bp, 1)) {
118 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
119 continue;
120 if (*bp != '=')
121 continue;
122 bp++;
123 return (tdecode(bp, area));
124 }
125 return (0);
126}
127
128tdecode(str, area)
129 register char *str;
130 char **area;
131{
132 register char *cp;
133 register int c;
134 int i;
135
136 cp = *area;
137 while ((c = *str++) && c != ':') {
138 switch (c) {
139 case '^':
140 if (*str >= 'a' && *str <= 'z')
141 c = *str++ & 037;
142 break;
143 case '\\':
144 switch (*str) {
145 case 'E':
146 str++;
147 c = 033;
148 break;
149 case '^':
150 case '\\':
151 case ':':
152 c = *str++;
153 break;
154 case 'n':
155 c = '\n';
156 str++;
157 break;
158 case 'r':
159 c = '\r';
160 str++;
161 break;
162 case 't':
163 c = '\t';
164 str++;
165 break;
166 case 'b':
167 c = '\b';
168 str++;
169 break;
170 default:
171 if (*str >= '0' && *str <= '9') {
172 c = 0, i = 3;
173 do
174 c =<< 3, c=| *str++ - '0';
175 while (--i && *str >= '0' && *str <= '9');
176 } else if (*str)
177 c = *str++;
178 break;
179 }
180 }
181 *cp++ = c;
182 }
183 *cp++ = 0;
184 str = *area;
185 *area = cp;
186 return (str);
187}