7306
|
1 /* Work-alike for termcap, plus extra features.
|
|
2 Copyright (C) 1985, 1986, 1993, 1994 Free Software Foundation, Inc.
|
|
3
|
|
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)
|
|
7 any later version.
|
|
8
|
|
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.
|
|
13
|
|
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. */
|
|
17
|
|
18 /* Emacs config.h may rename various library functions such as malloc. */
|
|
19 #ifdef HAVE_CONFIG_H
|
|
20 #include <config.h>
|
|
21 #else /* not HAVE_CONFIG_H */
|
|
22
|
|
23 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
|
|
24 #define bcopy(s, d, n) memcpy ((d), (s), (n))
|
|
25 #endif
|
|
26
|
|
27 #ifdef STDC_HEADERS
|
|
28 #include <stdlib.h>
|
|
29 #include <string.h>
|
|
30 #else
|
|
31 char *getenv ();
|
|
32 char *malloc ();
|
|
33 char *realloc ();
|
|
34 #endif
|
|
35
|
|
36 #ifdef HAVE_UNISTD_H
|
|
37 #include <unistd.h>
|
|
38 #endif
|
|
39 #ifdef _POSIX_VERSION
|
|
40 #include <fcntl.h>
|
|
41 #endif
|
|
42
|
|
43 #endif /* not HAVE_CONFIG_H */
|
|
44
|
|
45 #ifndef NULL
|
|
46 #define NULL (char *) 0
|
|
47 #endif
|
|
48
|
7685
|
49 #ifndef O_RDONLY
|
|
50 #define O_RDONLY 0
|
|
51 #endif
|
|
52
|
7306
|
53 /* BUFSIZE is the initial size allocated for the buffer
|
|
54 for reading the termcap file.
|
|
55 It is not a limit.
|
|
56 Make it large normally for speed.
|
|
57 Make it variable when debugging, so can exercise
|
|
58 increasing the space dynamically. */
|
|
59
|
|
60 #ifndef BUFSIZE
|
|
61 #ifdef DEBUG
|
|
62 #define BUFSIZE bufsize
|
|
63
|
|
64 int bufsize = 128;
|
|
65 #else
|
|
66 #define BUFSIZE 2048
|
|
67 #endif
|
|
68 #endif
|
|
69
|
|
70 #ifndef TERMCAP_NAME
|
|
71 #define TERMCAP_NAME "/etc/termcap"
|
|
72 #endif
|
|
73
|
|
74 #ifndef emacs
|
|
75 static void
|
|
76 memory_out ()
|
|
77 {
|
|
78 write (2, "virtual memory exhausted\n", 25);
|
|
79 exit (1);
|
|
80 }
|
|
81
|
|
82 static char *
|
|
83 xmalloc (size)
|
|
84 unsigned size;
|
|
85 {
|
|
86 register char *tem = malloc (size);
|
|
87
|
|
88 if (!tem)
|
|
89 memory_out ();
|
|
90 return tem;
|
|
91 }
|
|
92
|
|
93 static char *
|
|
94 xrealloc (ptr, size)
|
|
95 char *ptr;
|
|
96 unsigned size;
|
|
97 {
|
|
98 register char *tem = realloc (ptr, size);
|
|
99
|
|
100 if (!tem)
|
|
101 memory_out ();
|
|
102 return tem;
|
|
103 }
|
|
104 #endif /* not emacs */
|
|
105
|
|
106 /* Looking up capabilities in the entry already found. */
|
|
107
|
|
108 /* The pointer to the data made by tgetent is left here
|
|
109 for tgetnum, tgetflag and tgetstr to find. */
|
|
110 static char *term_entry;
|
|
111
|
|
112 static char *tgetst1 ();
|
|
113
|
|
114 /* Search entry BP for capability CAP.
|
|
115 Return a pointer to the capability (in BP) if found,
|
|
116 0 if not found. */
|
|
117
|
|
118 static char *
|
|
119 find_capability (bp, cap)
|
|
120 register char *bp, *cap;
|
|
121 {
|
|
122 for (; *bp; bp++)
|
|
123 if (bp[0] == ':'
|
|
124 && bp[1] == cap[0]
|
|
125 && bp[2] == cap[1])
|
|
126 return &bp[4];
|
|
127 return NULL;
|
|
128 }
|
|
129
|
|
130 int
|
|
131 tgetnum (cap)
|
|
132 char *cap;
|
|
133 {
|
|
134 register char *ptr = find_capability (term_entry, cap);
|
|
135 if (!ptr || ptr[-1] != '#')
|
|
136 return -1;
|
|
137 return atoi (ptr);
|
|
138 }
|
|
139
|
|
140 int
|
|
141 tgetflag (cap)
|
|
142 char *cap;
|
|
143 {
|
|
144 register char *ptr = find_capability (term_entry, cap);
|
|
145 return ptr && ptr[-1] == ':';
|
|
146 }
|
|
147
|
|
148 /* Look up a string-valued capability CAP.
|
|
149 If AREA is non-null, it points to a pointer to a block in which
|
|
150 to store the string. That pointer is advanced over the space used.
|
|
151 If AREA is null, space is allocated with `malloc'. */
|
|
152
|
|
153 char *
|
|
154 tgetstr (cap, area)
|
|
155 char *cap;
|
|
156 char **area;
|
|
157 {
|
|
158 register char *ptr = find_capability (term_entry, cap);
|
|
159 if (!ptr || (ptr[-1] != '=' && ptr[-1] != '~'))
|
|
160 return NULL;
|
|
161 return tgetst1 (ptr, area);
|
|
162 }
|
|
163
|
|
164 /* Table, indexed by a character in range 0100 to 0140 with 0100 subtracted,
|
|
165 gives meaning of character following \, or a space if no special meaning.
|
|
166 Eight characters per line within the string. */
|
|
167
|
|
168 static char esctab[]
|
|
169 = " \007\010 \033\014 \
|
|
170 \012 \
|
|
171 \015 \011 \013 \
|
|
172 ";
|
|
173
|
|
174 /* PTR points to a string value inside a termcap entry.
|
|
175 Copy that value, processing \ and ^ abbreviations,
|
|
176 into the block that *AREA points to,
|
|
177 or to newly allocated storage if AREA is NULL.
|
|
178 Return the address to which we copied the value,
|
|
179 or NULL if PTR is NULL. */
|
|
180
|
|
181 static char *
|
|
182 tgetst1 (ptr, area)
|
|
183 char *ptr;
|
|
184 char **area;
|
|
185 {
|
|
186 register char *p, *r;
|
|
187 register int c;
|
|
188 register int size;
|
|
189 char *ret;
|
|
190 register int c1;
|
|
191
|
|
192 if (!ptr)
|
|
193 return NULL;
|
|
194
|
|
195 /* `ret' gets address of where to store the string. */
|
|
196 if (!area)
|
|
197 {
|
|
198 /* Compute size of block needed (may overestimate). */
|
|
199 p = ptr;
|
|
200 while ((c = *p++) && c != ':' && c != '\n')
|
|
201 ;
|
|
202 ret = (char *) xmalloc (p - ptr + 1);
|
|
203 }
|
|
204 else
|
|
205 ret = *area;
|
|
206
|
|
207 /* Copy the string value, stopping at null or colon.
|
|
208 Also process ^ and \ abbreviations. */
|
|
209 p = ptr;
|
|
210 r = ret;
|
|
211 while ((c = *p++) && c != ':' && c != '\n')
|
|
212 {
|
|
213 if (c == '^')
|
|
214 c = *p++ & 037;
|
|
215 else if (c == '\\')
|
|
216 {
|
|
217 c = *p++;
|
|
218 if (c >= '0' && c <= '7')
|
|
219 {
|
|
220 c -= '0';
|
|
221 size = 0;
|
|
222
|
|
223 while (++size < 3 && (c1 = *p) >= '0' && c1 <= '7')
|
|
224 {
|
|
225 c *= 8;
|
|
226 c += c1 - '0';
|
|
227 p++;
|
|
228 }
|
|
229 }
|
|
230 else if (c >= 0100 && c < 0200)
|
|
231 {
|
|
232 c1 = esctab[(c & ~040) - 0100];
|
|
233 if (c1 != ' ')
|
|
234 c = c1;
|
|
235 }
|
|
236 }
|
|
237 *r++ = c;
|
|
238 }
|
|
239 *r = '\0';
|
|
240 /* Update *AREA. */
|
|
241 if (area)
|
|
242 *area = r + 1;
|
|
243 return ret;
|
|
244 }
|
|
245
|
|
246 /* Outputting a string with padding. */
|
|
247
|
|
248 short ospeed;
|
|
249 /* If OSPEED is 0, we use this as the actual baud rate. */
|
|
250 int tputs_baud_rate;
|
|
251 char PC;
|
|
252
|
|
253 /* Actual baud rate if positive;
|
|
254 - baud rate / 100 if negative. */
|
|
255
|
|
256 static short speeds[] =
|
|
257 {
|
|
258 #ifdef VMS
|
|
259 0, 50, 75, 110, 134, 150, -3, -6, -12, -18,
|
|
260 -20, -24, -36, -48, -72, -96, -192
|
|
261 #else /* not VMS */
|
|
262 0, 50, 75, 110, 135, 150, -2, -3, -6, -12,
|
|
263 -18, -24, -48, -96, -192, -384
|
|
264 #endif /* not VMS */
|
|
265 };
|
|
266
|
|
267 void
|
|
268 tputs (str, nlines, outfun)
|
|
269 register char *str;
|
|
270 int nlines;
|
|
271 register int (*outfun) ();
|
|
272 {
|
|
273 register int padcount = 0;
|
|
274 register int speed;
|
|
275
|
|
276 #ifdef emacs
|
|
277 extern baud_rate;
|
|
278 speed = baud_rate;
|
|
279 #else
|
|
280 if (ospeed == 0)
|
|
281 speed = tputs_baud_rate;
|
|
282 else
|
|
283 speed = speeds[ospeed];
|
|
284 #endif
|
|
285
|
|
286 if (!str)
|
|
287 return;
|
|
288
|
|
289 while (*str >= '0' && *str <= '9')
|
|
290 {
|
|
291 padcount += *str++ - '0';
|
|
292 padcount *= 10;
|
|
293 }
|
|
294 if (*str == '.')
|
|
295 {
|
|
296 str++;
|
|
297 padcount += *str++ - '0';
|
|
298 }
|
|
299 if (*str == '*')
|
|
300 {
|
|
301 str++;
|
|
302 padcount *= nlines;
|
|
303 }
|
|
304 while (*str)
|
|
305 (*outfun) (*str++);
|
|
306
|
|
307 /* padcount is now in units of tenths of msec. */
|
|
308 padcount *= speeds[ospeed];
|
|
309 padcount += 500;
|
|
310 padcount /= 1000;
|
|
311 if (speeds[ospeed] < 0)
|
|
312 padcount = -padcount;
|
|
313 else
|
|
314 {
|
|
315 padcount += 50;
|
|
316 padcount /= 100;
|
|
317 }
|
|
318
|
|
319 while (padcount-- > 0)
|
|
320 (*outfun) (PC);
|
|
321 }
|
|
322
|
|
323 /* Finding the termcap entry in the termcap data base. */
|
|
324
|
|
325 struct buffer
|
|
326 {
|
|
327 char *beg;
|
|
328 int size;
|
|
329 char *ptr;
|
|
330 int ateof;
|
|
331 int full;
|
|
332 };
|
|
333
|
|
334 /* Forward declarations of static functions. */
|
|
335
|
|
336 static int scan_file ();
|
|
337 static char *gobble_line ();
|
|
338 static int compare_contin ();
|
|
339 static int name_match ();
|
|
340
|
|
341 #ifdef VMS
|
|
342
|
|
343 #include <rmsdef.h>
|
|
344 #include <fab.h>
|
|
345 #include <nam.h>
|
|
346
|
|
347 static int
|
|
348 valid_filename_p (fn)
|
|
349 char *fn;
|
|
350 {
|
|
351 struct FAB fab = cc$rms_fab;
|
|
352 struct NAM nam = cc$rms_nam;
|
|
353 char esa[NAM$C_MAXRSS];
|
|
354
|
|
355 fab.fab$l_fna = fn;
|
|
356 fab.fab$b_fns = strlen(fn);
|
|
357 fab.fab$l_nam = &nam;
|
|
358 fab.fab$l_fop = FAB$M_NAM;
|
|
359
|
|
360 nam.nam$l_esa = esa;
|
|
361 nam.nam$b_ess = sizeof esa;
|
|
362
|
|
363 return SYS$PARSE(&fab, 0, 0) == RMS$_NORMAL;
|
|
364 }
|
|
365
|
|
366 #else /* !VMS */
|
|
367
|
|
368 #ifdef MSDOS /* MW, May 1993 */
|
|
369 static int
|
|
370 valid_filename_p (fn)
|
|
371 char *fn;
|
|
372 {
|
|
373 return *fn == '/' || fn[1] == ':';
|
|
374 }
|
|
375 #else
|
|
376 #define valid_filename_p(fn) (*(fn) == '/')
|
|
377 #endif
|
|
378
|
|
379 #endif /* !VMS */
|
|
380
|
|
381 /* Find the termcap entry data for terminal type NAME
|
|
382 and store it in the block that BP points to.
|
|
383 Record its address for future use.
|
|
384
|
|
385 If BP is null, space is dynamically allocated.
|
|
386
|
|
387 Return -1 if there is some difficulty accessing the data base
|
|
388 of terminal types,
|
|
389 0 if the data base is accessible but the type NAME is not defined
|
|
390 in it, and some other value otherwise. */
|
|
391
|
|
392 int
|
|
393 tgetent (bp, name)
|
|
394 char *bp, *name;
|
|
395 {
|
|
396 register char *termcap_name;
|
|
397 register int fd;
|
|
398 struct buffer buf;
|
|
399 register char *bp1;
|
|
400 char *bp2;
|
|
401 char *term;
|
|
402 int malloc_size = 0;
|
|
403 register int c;
|
|
404 char *tcenv; /* TERMCAP value, if it contains :tc=. */
|
|
405 char *indirect = NULL; /* Terminal type in :tc= in TERMCAP value. */
|
|
406 int filep;
|
|
407
|
|
408 #ifdef INTERNAL_TERMINAL
|
|
409 /* For the internal terminal we don't want to read any termcap file,
|
|
410 so fake it. */
|
|
411 if (!strcmp (name, "internal"))
|
|
412 {
|
|
413 term = INTERNAL_TERMINAL;
|
|
414 if (!bp)
|
|
415 {
|
|
416 malloc_size = 1 + strlen (term);
|
|
417 bp = (char *) xmalloc (malloc_size);
|
|
418 }
|
|
419 strcpy (bp, term);
|
|
420 goto ret;
|
|
421 }
|
|
422 #endif /* INTERNAL_TERMINAL */
|
|
423
|
|
424 termcap_name = getenv ("TERMCAP");
|
|
425 if (termcap_name && *termcap_name == '\0')
|
|
426 termcap_name = NULL;
|
|
427 #if defined (MSDOS) && !defined (TEST)
|
|
428 if (termcap_name && (*termcap_name == '\\'
|
|
429 || *termcap_name == '/'
|
|
430 || termcap_name[1] == ':'))
|
|
431 dostounix_filename(termcap_name);
|
|
432 #endif
|
|
433
|
|
434 filep = termcap_name && valid_filename_p (termcap_name);
|
|
435
|
|
436 /* If termcap_name is non-null and starts with / (in the un*x case, that is),
|
|
437 it is a file name to use instead of /etc/termcap.
|
|
438 If it is non-null and does not start with /,
|
|
439 it is the entry itself, but only if
|
|
440 the name the caller requested matches the TERM variable. */
|
|
441
|
|
442 if (termcap_name && !filep && !strcmp (name, getenv ("TERM")))
|
|
443 {
|
|
444 indirect = tgetst1 (find_capability (termcap_name, "tc"), (char **) 0);
|
|
445 if (!indirect)
|
|
446 {
|
|
447 if (!bp)
|
|
448 bp = termcap_name;
|
|
449 else
|
|
450 strcpy (bp, termcap_name);
|
|
451 goto ret;
|
|
452 }
|
|
453 else
|
|
454 { /* It has tc=. Need to read /etc/termcap. */
|
|
455 tcenv = termcap_name;
|
|
456 termcap_name = NULL;
|
|
457 }
|
|
458 }
|
|
459
|
|
460 if (!termcap_name || !filep)
|
|
461 termcap_name = TERMCAP_NAME;
|
|
462
|
|
463 /* Here we know we must search a file and termcap_name has its name. */
|
|
464
|
|
465 #ifdef MSDOS
|
7685
|
466 fd = open (termcap_name, O_RDONLY|O_TEXT, 0);
|
7306
|
467 #else
|
7685
|
468 fd = open (termcap_name, O_RDONLY, 0);
|
7306
|
469 #endif
|
|
470 if (fd < 0)
|
|
471 return -1;
|
|
472
|
|
473 buf.size = BUFSIZE;
|
|
474 /* Add 1 to size to ensure room for terminating null. */
|
|
475 buf.beg = (char *) xmalloc (buf.size + 1);
|
|
476 term = indirect ? indirect : name;
|
|
477
|
|
478 if (!bp)
|
|
479 {
|
|
480 malloc_size = indirect ? strlen (tcenv) + 1 : buf.size;
|
|
481 bp = (char *) xmalloc (malloc_size);
|
|
482 }
|
|
483 bp1 = bp;
|
|
484
|
|
485 if (indirect)
|
|
486 /* Copy the data from the environment variable. */
|
|
487 {
|
|
488 strcpy (bp, tcenv);
|
|
489 bp1 += strlen (tcenv);
|
|
490 }
|
|
491
|
|
492 while (term)
|
|
493 {
|
|
494 /* Scan the file, reading it via buf, till find start of main entry. */
|
|
495 if (scan_file (term, fd, &buf) == 0)
|
|
496 {
|
|
497 close (fd);
|
|
498 free (buf.beg);
|
|
499 if (malloc_size)
|
|
500 free (bp);
|
|
501 return 0;
|
|
502 }
|
|
503
|
|
504 /* Free old `term' if appropriate. */
|
|
505 if (term != name)
|
|
506 free (term);
|
|
507
|
|
508 /* If BP is malloc'd by us, make sure it is big enough. */
|
|
509 if (malloc_size)
|
|
510 {
|
|
511 malloc_size = bp1 - bp + buf.size;
|
|
512 termcap_name = (char *) xrealloc (bp, malloc_size);
|
|
513 bp1 += termcap_name - bp;
|
|
514 bp = termcap_name;
|
|
515 }
|
|
516
|
|
517 bp2 = bp1;
|
|
518
|
|
519 /* Copy the line of the entry from buf into bp. */
|
|
520 termcap_name = buf.ptr;
|
|
521 while ((*bp1++ = c = *termcap_name++) && c != '\n')
|
|
522 /* Drop out any \ newline sequence. */
|
|
523 if (c == '\\' && *termcap_name == '\n')
|
|
524 {
|
|
525 bp1--;
|
|
526 termcap_name++;
|
|
527 }
|
|
528 *bp1 = '\0';
|
|
529
|
|
530 /* Does this entry refer to another terminal type's entry?
|
|
531 If something is found, copy it into heap and null-terminate it. */
|
|
532 term = tgetst1 (find_capability (bp2, "tc"), (char **) 0);
|
|
533 }
|
|
534
|
|
535 close (fd);
|
|
536 free (buf.beg);
|
|
537
|
|
538 if (malloc_size)
|
|
539 bp = (char *) xrealloc (bp, bp1 - bp + 1);
|
|
540
|
|
541 ret:
|
|
542 term_entry = bp;
|
|
543 if (malloc_size)
|
|
544 return (int) bp;
|
|
545 return 1;
|
|
546 }
|
|
547
|
|
548 /* Given file open on FD and buffer BUFP,
|
|
549 scan the file from the beginning until a line is found
|
|
550 that starts the entry for terminal type STR.
|
|
551 Return 1 if successful, with that line in BUFP,
|
|
552 or 0 if no entry is found in the file. */
|
|
553
|
|
554 static int
|
|
555 scan_file (str, fd, bufp)
|
|
556 char *str;
|
|
557 int fd;
|
|
558 register struct buffer *bufp;
|
|
559 {
|
|
560 register char *end;
|
|
561
|
|
562 bufp->ptr = bufp->beg;
|
|
563 bufp->full = 0;
|
|
564 bufp->ateof = 0;
|
|
565 *bufp->ptr = '\0';
|
|
566
|
|
567 lseek (fd, 0L, 0);
|
|
568
|
|
569 while (!bufp->ateof)
|
|
570 {
|
|
571 /* Read a line into the buffer. */
|
|
572 end = NULL;
|
|
573 do
|
|
574 {
|
|
575 /* if it is continued, append another line to it,
|
|
576 until a non-continued line ends. */
|
|
577 end = gobble_line (fd, bufp, end);
|
|
578 }
|
|
579 while (!bufp->ateof && end[-2] == '\\');
|
|
580
|
|
581 if (*bufp->ptr != '#'
|
|
582 && name_match (bufp->ptr, str))
|
|
583 return 1;
|
|
584
|
|
585 /* Discard the line just processed. */
|
|
586 bufp->ptr = end;
|
|
587 }
|
|
588 return 0;
|
|
589 }
|
|
590
|
|
591 /* Return nonzero if NAME is one of the names specified
|
|
592 by termcap entry LINE. */
|
|
593
|
|
594 static int
|
|
595 name_match (line, name)
|
|
596 char *line, *name;
|
|
597 {
|
|
598 register char *tem;
|
|
599
|
|
600 if (!compare_contin (line, name))
|
|
601 return 1;
|
|
602 /* This line starts an entry. Is it the right one? */
|
|
603 for (tem = line; *tem && *tem != '\n' && *tem != ':'; tem++)
|
|
604 if (*tem == '|' && !compare_contin (tem + 1, name))
|
|
605 return 1;
|
|
606
|
|
607 return 0;
|
|
608 }
|
|
609
|
|
610 static int
|
|
611 compare_contin (str1, str2)
|
|
612 register char *str1, *str2;
|
|
613 {
|
|
614 register int c1, c2;
|
|
615 while (1)
|
|
616 {
|
|
617 c1 = *str1++;
|
|
618 c2 = *str2++;
|
|
619 while (c1 == '\\' && *str1 == '\n')
|
|
620 {
|
|
621 str1++;
|
|
622 while ((c1 = *str1++) == ' ' || c1 == '\t');
|
|
623 }
|
|
624 if (c2 == '\0')
|
|
625 {
|
|
626 /* End of type being looked up. */
|
|
627 if (c1 == '|' || c1 == ':')
|
|
628 /* If end of name in data base, we win. */
|
|
629 return 0;
|
|
630 else
|
|
631 return 1;
|
|
632 }
|
|
633 else if (c1 != c2)
|
|
634 return 1;
|
|
635 }
|
|
636 }
|
|
637
|
|
638 /* Make sure that the buffer <- BUFP contains a full line
|
|
639 of the file open on FD, starting at the place BUFP->ptr
|
|
640 points to. Can read more of the file, discard stuff before
|
|
641 BUFP->ptr, or make the buffer bigger.
|
|
642
|
|
643 Return the pointer to after the newline ending the line,
|
|
644 or to the end of the file, if there is no newline to end it.
|
|
645
|
|
646 Can also merge on continuation lines. If APPEND_END is
|
|
647 non-null, it points past the newline of a line that is
|
|
648 continued; we add another line onto it and regard the whole
|
|
649 thing as one line. The caller decides when a line is continued. */
|
|
650
|
|
651 static char *
|
|
652 gobble_line (fd, bufp, append_end)
|
|
653 int fd;
|
|
654 register struct buffer *bufp;
|
|
655 char *append_end;
|
|
656 {
|
|
657 register char *end;
|
|
658 register int nread;
|
|
659 register char *buf = bufp->beg;
|
|
660 register char *tem;
|
|
661
|
|
662 if (!append_end)
|
|
663 append_end = bufp->ptr;
|
|
664
|
|
665 while (1)
|
|
666 {
|
|
667 end = append_end;
|
|
668 while (*end && *end != '\n') end++;
|
|
669 if (*end)
|
|
670 break;
|
|
671 if (bufp->ateof)
|
|
672 return buf + bufp->full;
|
|
673 if (bufp->ptr == buf)
|
|
674 {
|
|
675 if (bufp->full == bufp->size)
|
|
676 {
|
|
677 bufp->size *= 2;
|
|
678 /* Add 1 to size to ensure room for terminating null. */
|
|
679 tem = (char *) xrealloc (buf, bufp->size + 1);
|
|
680 bufp->ptr = (bufp->ptr - buf) + tem;
|
|
681 append_end = (append_end - buf) + tem;
|
|
682 bufp->beg = buf = tem;
|
|
683 }
|
|
684 }
|
|
685 else
|
|
686 {
|
|
687 append_end -= bufp->ptr - buf;
|
|
688 bcopy (bufp->ptr, buf, bufp->full -= bufp->ptr - buf);
|
|
689 bufp->ptr = buf;
|
|
690 }
|
|
691 if (!(nread = read (fd, buf + bufp->full, bufp->size - bufp->full)))
|
|
692 bufp->ateof = 1;
|
|
693 bufp->full += nread;
|
|
694 buf[bufp->full] = '\0';
|
|
695 }
|
|
696 return end + 1;
|
|
697 }
|
|
698
|
|
699 #ifdef TEST
|
|
700
|
|
701 #ifdef NULL
|
|
702 #undef NULL
|
|
703 #endif
|
|
704
|
|
705 #include <stdio.h>
|
|
706
|
|
707 main (argc, argv)
|
|
708 int argc;
|
|
709 char **argv;
|
|
710 {
|
|
711 char *term;
|
|
712 char *buf;
|
|
713
|
|
714 term = argv[1];
|
|
715 printf ("TERM: %s\n", term);
|
|
716
|
|
717 buf = (char *) tgetent (0, term);
|
|
718 if ((int) buf <= 0)
|
|
719 {
|
|
720 printf ("No entry.\n");
|
|
721 return 0;
|
|
722 }
|
|
723
|
|
724 printf ("Entry: %s\n", buf);
|
|
725
|
|
726 tprint ("cm");
|
|
727 tprint ("AL");
|
|
728
|
|
729 printf ("co: %d\n", tgetnum ("co"));
|
|
730 printf ("am: %d\n", tgetflag ("am"));
|
|
731 }
|
|
732
|
|
733 tprint (cap)
|
|
734 char *cap;
|
|
735 {
|
|
736 char *x = tgetstr (cap, 0);
|
|
737 register char *y;
|
|
738
|
|
739 printf ("%s: ", cap);
|
|
740 if (x)
|
|
741 {
|
|
742 for (y = x; *y; y++)
|
|
743 if (*y <= ' ' || *y == 0177)
|
|
744 printf ("\\%0o", *y);
|
|
745 else
|
|
746 putchar (*y);
|
|
747 free (x);
|
|
748 }
|
|
749 else
|
|
750 printf ("none");
|
|
751 putchar ('\n');
|
|
752 }
|
|
753
|
|
754 #endif /* TEST */
|
|
755
|