home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / termcap-1.2.2 / termcap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-06  |  17.3 KB  |  828 lines

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