File: termcap.c

package info (click to toggle)
ng 1.5~beta1-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,144 kB
  • sloc: ansic: 43,437; asm: 3,150; sh: 2,539; cpp: 1,234; makefile: 577
file content (361 lines) | stat: -rw-r--r-- 5,716 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* $Id: termcap.c,v 1.1.1.1 2000/06/27 01:48:02 amura Exp $ */
/*
 * termcap.c - termcap library routines for MS-DOS and OS/2
 *
 *	This is nearly public domain termcap library routines,
 *	completely rewritten from the code of Norman Azadian's
 *	termcap routines, which is also not copyrighted.
 *	You may freely copy and distribute this program.
 *
 *				Junn Ohta, September 1989
 */

/*
 * $Log: termcap.c,v $
 * Revision 1.1.1.1  2000/06/27 01:48:02  amura
 * import to CVS
 *
 */

/* 90.07.24  by S.Yoshida: Ng don't need this file.
#include "env.h" */

/* 90.07.24  by S.Yoshida: Ng always need termcap library.
#ifdef TERMCAP */
/* 97.09.12  by amura: Ng don't need termcap when using TurboC conio */
#ifndef TCCONIO

#include <stdio.h>

#define	TBSIZE	1024	/* size of termcap buffer (termcap dependent) */

/*
 * termcap tcbuf; retained for later use of termcap routines
 */
static	char	*tcbuf;

/*
 * dummy variables; for compatibility with UNIX termcap
 */
extern	char	PC;
extern	short	ospeed;

extern	char	*getenv(), *strcpy(), *strchr();

/*
 * error exit
 */
static void
error(s)
	char	*s;
{
	fprintf(stderr, "termcap: %s\n", s);
	exit(1);
}

/*
 * get an entry
 */
static int
getentry(fp, buf, room, name)
	FILE *fp;
	char *buf;
	int room;
	char *name;
{
	register char *p, *q;
	int cont, len, c;
	char line[TBSIZE];

next:
	for (;;) {
		if ((p = fgets(line, TBSIZE, fp)) == NULL) {
			*buf = 0;
			return 0;
		}
		line[TBSIZE-1] = 0;
		if (*p >= 'A' && *p <= 'Z' || *p >= 'a' && *p <= 'z')
			break;
	}
	for (p = line; *p && *p != '\r' && *p != '\n'; p++)
		;
	if (cont = (p > line && p[-1] == '\\'))
		p--;
	*p = 0;
	p = line;
	for (;;) {
		for (q = p; *q && *q != '|' && *q != ':'; q++)
			;
		if (*q == 0)
			goto next;
		c = *q;
		*q = 0;
		if (!strcmp(name, p)) {
			*q = c;
			break;
		}
		*q = c;
		p = q + 1;
	}
	while (*p != ':')
		p++;
	len = strlen(p);
	if (room <= len)
		error("tcbuf too long");
	strcpy(buf, p);
	buf += len;
	room -= len;
	while (cont) {
		if ((p = fgets(line, TBSIZE, fp)) == NULL)
			error("unexpected eof");
		line[TBSIZE-1] = 0;
		for (p = line; *p && *p != '\r' && *p != '\n'; p++)
			;
		if (cont = (p > line && p[-1] == '\\'))
			p--;
		*p = 0;
		for (p = line; *p == ' ' || *p == '\t'; p++)
			;
		if (!*p || *p == '#')
			continue;
		len = strlen(p);
		if (room <= len)
			error("tcbuf too long");
		strcpy(buf, p);
		buf += len;
		room -= len;
	}
	return 1;
}

/*
 * get one capability value
 */
static char *
getcap(name)
	register char *name;
{
	register char *p;

	if (tcbuf == NULL)
		return NULL;

	p = tcbuf;
	while (*p) {
		if (*p++ != ':')
			continue;
		if (*p == *name && p[1] == name[1])
			return p + 2;
	}
	return NULL;
}

/*
 * tgetent
 */
int
tgetent(buf, name)
	char *buf;
	char *name;
{
	register char *p, *q;
	char *file;
	FILE *fp;
	int ret;

	if ((file = getenv("TERMCAP")) == NULL)
		file = "/etc/termcap";
	if ((fp = fopen(file, "r")) == NULL)
		return -1;
	p = buf;
	if ((ret = getentry(fp, p, TBSIZE, name)) == 1)
		tcbuf = buf;
	fclose(fp);
	while (p = getcap("tc")) {
		for (q = p; *q && *q != ':'; q++)
			;
		*q = 0;
		p -= 2;
		if ((fp = fopen(file, "r")) == NULL)
			return -1;
		ret = getentry(fp, p, TBSIZE-(int)(p-tcbuf), p+3);
		fclose(fp);
	}
	return ret;
}

/*
 * tgetflag
 */
int
tgetflag(name)
	char *name;
{
	register char *p;

	return ((p = getcap(name)) != NULL && *p == ':');
}

/*
 * tgetnum
 */
int
tgetnum(name)
	char *name;
{
	register char *p;
	int num;

	if ((p = getcap(name)) == NULL || *p++ != '#')
		return -1;
	num = 0;
	while (*p && *p != ':')
		num = num * 10 + (*p++ - '0');
	return num;
}

/*
 * tgetstr
 */
char *
tgetstr(name, bufp)
	char *name;
	char **bufp;
{
	register char *p, *q;
	char *ret;
	int c;

	if ((p = getcap(name)) == NULL || *p++ != '=')
		return NULL;
	ret = q = *bufp;
	while (*p && *p != ':') {
		if (*p == '^') {
			p++;
			*q++ = *p++ & 0x1f;
			continue;
		}
		if (*p != '\\') {
			*q++ = *p++;
			continue;
		}
		p++;
		if (*p >= '0' && *p <= '7') {
			c = *p++ - '0';
			if (*p >= '0' && *p <= '7')
				c = (c << 3) + *p++ - '0';
			if (*p >= '0' && *p <= '7')
				c = (c << 3) + *p++ - '0';
			*q++ = c;
			continue;
		}
		switch (c = *p++) {
		case 'E': c = '\033'; break;
		case 'n': c = '\n';   break;
		case 'r': c = '\r';   break;
		case 'f': c = '\f';   break;
		case 't': c = '\t';   break;
		case 'b': c = '\b';   break;
		}
		*q++ = c;
	}
	*q++ = 0;
	*bufp = q;
	return ret;
}

/*
 * tgoto
 */
char *
tgoto(cm, col, row)
	char *cm;
	int col, row;
{
	register char *p, *q;
	char *fmt;
	int *val, tmp;
	static char buf[20];

	val = &row;
	for (p = cm, q = buf; *p; p++) {
		if (*p != '%') {
			*q++ = *p;
			continue;
		}
		switch (*++p) {
		case 'd':
			fmt = "%dX";
			goto num;
		case '2':
			fmt = "%02dX";
			goto num;
		case '3':
			fmt = "%03dX";
		num:
			sprintf(q, fmt, *val);
			while (*q != 'X')
				q++;
			val = &col;
			break;
		case '.':
			*q++ = *val;
			val = &col;
			break;
		case '+':
			*q++ = *val + *++p;
			val = &col;
			break;
		case '>':
			p++;
			if (*val > *p++)
				*val += *p;
			break;
		case 'r':
			tmp = row;
			row = col;
			col = tmp;
			break;
		case 'i':
			row++;
			col++;
			break;
		case 'n':
			row ^= 0140;
			col ^= 0140;
			break;
		case 'B':
			*val += 6 * (*val / 10);
			break;
		case 'D':
			*val -= 2 * (*val % 16);
			break;
		case '%':
			*q++ = '%';
			break;
		default:
			return "OOPS";
		}
	}
	*q = 0;
	return buf;
}

/*
 * tputs
 */
void
tputs(p, lines, outc)
	register char *p;
	int lines;
	int (*outc)();
{
	while (*p == '.' || *p == '*' || *p >= '0' && *p <= '9')
		p++;
	while (*p)
		(*outc)(*p++);
}

#endif /* TCCONIO */
/* 90.07.24  by S.Yoshida: Ng always need termcap library.
#endif /*TERMCAP*/