home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume2 / ms-termcap / TERMCAP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-07  |  18.4 KB  |  776 lines

  1. /* Work-alike for termcap, plus extra features.
  2.    Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  3.  
  4.    Hacked for MS-DOS by Paul Shields, shields@yunccn.UUCP,
  5.    Dec 1987 - Mar 1988.
  6.  
  7.                NO WARRANTY
  8.  
  9.    BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  10. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  11. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  12. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  13. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  14. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  15. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  16. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  17. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  18. CORRECTION.
  19.  
  20.    IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  21. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  22. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  23. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  24. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  25. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  26. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  27. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  28. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  29. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  30.  
  31.         GENERAL PUBLIC LICENSE TO COPY
  32.  
  33.    1. You may copy and distribute verbatim copies of this source file
  34. as you receive it, in any medium, provided that you conspicuously and
  35. appropriately publish on each copy a valid copyright notice "Copyright
  36. (C) 1986 Free Software Foundation, Inc."; and include following the
  37. copyright notice a verbatim copy of the above disclaimer of warranty
  38. and of this License.  You may charge a distribution fee for the
  39. physical act of transferring a copy.
  40.  
  41.    2. You may modify your copy or copies of this source file or
  42. any portion of it, and copy and distribute such modifications under
  43. the terms of Paragraph 1 above, provided that you also do the following:
  44.  
  45.       a) cause the modified files to carry prominent notices stating
  46.       that you changed the files and the date of any change; and
  47.  
  48.       b) cause the whole of any work that you distribute or publish,
  49.       that in whole or in part contains or is a derivative of this
  50.       program or any part thereof, to be licensed at no charge to all
  51.       third parties on terms identical to those contained in this
  52.       License Agreement (except that you may choose to grant more
  53.       extensive warranty protection to third parties, at your option).
  54.  
  55.       c) You may charge a distribution fee for the physical act of
  56.       transferring a copy, and you may at your option offer warranty
  57.       protection in exchange for a fee.
  58.  
  59.     3. You may copy and distribute this program or any portion of it in
  60. compiled, executable or object code form under the terms of Paragraphs
  61. 1 and 2 above provided that you do the following:
  62.  
  63.       a) cause each such copy to be accompanied by the
  64.       corresponding machine-readable source code, which must
  65.       be distributed under the terms of Paragraphs 1 and 2 above; or,
  66.  
  67.       b) cause each such copy to be accompanied by a
  68.       written offer, with no time limit, to give any third party
  69.       free (except for a nominal shipping charge) a machine readable
  70.       copy of the corresponding source code, to be distributed
  71.       under the terms of Paragraphs 1 and 2 above; or,
  72.  
  73.       c) in the case of a recipient of this program in compiled, executable
  74.       or object code form (without the corresponding source code) you
  75.       shall cause copies you distribute to be accompanied by a copy
  76.       of the written offer of source code which you received along
  77.       with the copy you received.
  78.  
  79.     4. You may not copy, sublicense, distribute or transfer this program
  80. except as expressly provided under this License Agreement.  Any attempt
  81. otherwise to copy, sublicense, distribute or transfer this program is void and
  82. your rights to use the program under this License agreement shall be
  83. automatically terminated.  However, parties who have received computer
  84. software programs from you with this License Agreement will not have
  85. their licenses terminated so long as such parties remain in full compliance.
  86.  
  87.     5. If you wish to incorporate parts of this program into other free
  88. programs whose distribution conditions are different, write to the Free
  89. Software Foundation at 1000 Mass Ave, Cambridge, MA 02138.  We have not yet
  90. worked out a simple rule that can be stated here, but we will often permit
  91. this.  We will be guided by the two goals of preserving the free status of
  92. all derivatives of our free software and of promoting the sharing and reuse of
  93. software.
  94.  
  95.  
  96. In other words, you are welcome to use, share and improve this program.
  97. You are forbidden to forbid anyone else to use, share and improve
  98. what you give them.   Help stamp out software-hoarding!  */
  99.  
  100.  
  101.  
  102. /* BUFSIZE is the initial size allocated for the buffer
  103.    for reading the termcap file.
  104.    It is not a limit.
  105.    Make it large normally for speed.
  106.    Make it variable when debugging, so can exercise
  107.    increasing the space dynamically.  */
  108.  
  109. #ifdef emacs
  110. #include "config.h"
  111. #else
  112. #include <fcntl.h>
  113. #endif
  114.  
  115. /* MODS for MS-DOS by Paul Shields, December 1987. */
  116. #ifdef MSDOS
  117. #include <stdlib.h>
  118. #endif
  119.  
  120. #if defined(MSDOS) || defined(SYSV)
  121. #define  bcopy(b,a,n) memcpy(a,b,n)
  122. #endif
  123.  
  124. #ifndef BUFSIZE
  125. # ifdef DEBUG
  126. #  include <stdio.h>
  127. #  define  BUFSIZE bufsize
  128.    int bufsize = 2048;
  129.    extern int  debuglevel;
  130. # else
  131. #  define BUFSIZE 2048
  132. # endif
  133. #endif
  134.  
  135. #ifndef emacs
  136. static void
  137. memory_out ()
  138. {
  139.     write (2, "memory exhausted\n", 25);
  140.     exit (1);
  141. }
  142.  
  143. static char *
  144. xmalloc (size)
  145.        int size;
  146. {
  147.     register char *tem = malloc (size);
  148.     if (!tem)
  149.         memory_out ();
  150.     return tem;
  151. }
  152.  
  153. static char *
  154. xrealloc (ptr, size)
  155.        int ptr;
  156.        int size;
  157. {
  158.     register char *tem = realloc (ptr, size);
  159.     if (!tem)
  160.         memory_out ();
  161.     return tem;
  162. }
  163. #endif /* not emacs */
  164.  
  165. /* Looking up capabilities in the entry already found */
  166.  
  167. /* The pointer to the data made by tgetent is left here
  168.      for tgetnum, tgetflag and tgetstr to find.  */
  169.  
  170. static char *term_entry;
  171.  
  172. static char *tgetst1 ();
  173.  
  174. /* This is the main subroutine that is used to search
  175.      an entry for a particular capability */
  176.  
  177. static char *
  178. find_capability (bp, cap)
  179.        register char *bp, *cap;
  180. {
  181.     for (; *bp; bp++)
  182.         if (bp[0] == ':' && bp[1] == cap[0] && bp[2] == cap[1])
  183.             return &bp[4];
  184.     return 0;
  185. }
  186.  
  187. int
  188. tgetnum (cap)
  189.        char *cap;
  190. {
  191.     register char *ptr = find_capability (term_entry, cap);
  192. #ifdef DEBUG
  193.     if(debuglevel & 4)
  194.         fprintf(stderr,"cap: %s, num: %d\n", cap, atoi(ptr));
  195. #endif
  196.     if (!ptr || ptr[-1] != '#')
  197.         return -1;
  198.     return atoi (ptr);
  199. }
  200.  
  201. int
  202. tgetflag (cap)
  203.        char *cap;
  204. {
  205.     register char *ptr = find_capability (term_entry, cap);
  206.     return 0 != ptr && ptr[-1] == ':';
  207. }
  208.  
  209. /* Look up a string-valued capability `cap'.
  210.      If `area' is nonzero, it points to a pointer to a block in which
  211.      to store the string.  That pointer is advanced over the space used.
  212.      If `area' is zero, space is allocated with `malloc'.  */
  213.  
  214. char *
  215. tgetstr (cap, area)
  216.        char *cap;
  217.        char **area;
  218. {
  219.     register char *ptr = find_capability (term_entry, cap);
  220.     if (!ptr || (ptr[-1] != '=' && ptr[-1] != '~'))
  221.         return 0;
  222.     return tgetst1 (ptr, area);
  223. }
  224.  
  225. /* Table, indexed by a character in range 0100 to 0140 with 0100 subtracted,
  226.    gives meaning of character following \, or a space if no special meaning.
  227.    Eight characters per line within the string.  */
  228.  
  229. static char esctab[] = "\
  230. \040\007\010\040\040\033\014\040\
  231. \040\040\040\040\040\040\012\040\
  232. \040\040\015\040\011\040\013\040\
  233. \040\040\040\040\040\040\040\040";
  234.  
  235. /* Given a pointer to a string value inside a termcap entry (`ptr'),
  236.    copy the value and process \ and ^ abbreviations.
  237.    Copy into block that *area points to,
  238.    or to newly allocated storage if area is 0.  */
  239.  
  240. static char *
  241. tgetst1 (ptr, area)
  242.        char *ptr;
  243.        char **area;
  244. {
  245.     register char *p, *r;
  246.     register int c;
  247.     register int size;
  248.     char *ret;
  249.     register int c1;
  250.  
  251.     if (!ptr)
  252.       return 0;
  253.  
  254.     /* `ret' gets address of where to store the string */
  255.     if (!area)
  256.       {
  257.         /* Compute size of block needed (may overestimate) */
  258.         p = ptr;
  259.         while ((c = *p++) && c != ':');
  260.         ret = xmalloc (p - ptr + 1);
  261.       }
  262.     else
  263.       ret = *area;
  264.  
  265.     /* Copy the string value, stopping at null or colon.  */
  266.     /* Also process ^ and \ abbreviations.  */
  267.     p = ptr;
  268.     r = ret;
  269.     while ((c = *p++) && c != ':')
  270.       {
  271.         if (c == '^')
  272.     c = *p++ & 037;
  273.         else if (c == '\\')
  274.     {
  275.       c = *p++;
  276.       if (c >= '0' && c <= '7')
  277.         {
  278.           c -= '0';
  279.           size = 0;
  280.  
  281.           while (++size < 3 && (c1 = *p) >= '0' && c1 <= '7')
  282.         {
  283.           c *= 8;
  284.           c += c1 - '0';
  285.           p++;
  286.         }
  287.         }
  288.       else if (c >= 0100 && c < 0200)
  289.         {
  290.           c1 = esctab[(c & ~040) - 0100];
  291.           if (c1 != ' ')
  292.         c = c1;
  293.         }
  294.     }
  295.         *r++ = c;
  296.       }
  297.     *r = 0;
  298.     /* Update *area */
  299.     if (area)
  300.       *area = r + 1;
  301.     return ret;
  302. }
  303.  
  304. /* Outputting a string with padding */
  305. extern short ospeed;
  306. extern char PC;
  307.  
  308. /* Actual baud rate if positive;
  309.      - baud rate / 100 if negative.  */
  310.  
  311. static short speeds[] =
  312.     {
  313. #ifdef VMS
  314.       0, 50, 75, 110, 134, 150, -3, -6, -12, -18,
  315.       -20, -24, -36, -48, -72, -96, -192
  316. #else /* not VMS */
  317.       0, 50, 75, 110, 135, 150, -2, -3, -6, -12,
  318.       -18, -24, -48, -96, -192, -384
  319. #endif /* not VMS */
  320.     };
  321.  
  322. void
  323. tputs (string, nlines, outfun)
  324.        register char *string;
  325.        int nlines;
  326.        register int (*outfun) ();
  327. {
  328.     register int padcount = 0;
  329.  
  330.     if (string == (char *) 0)
  331.       return;
  332.     while (*string >= '0' && *string <= '9')
  333.       {
  334.         padcount += *string++ - '0';
  335.         padcount *= 10;
  336.       }
  337.     if (*string == '.')
  338.       {
  339.         string++;
  340.         padcount += *string++ - '0';
  341.       }
  342.     if (*string == '*')
  343.       {
  344.         string++;
  345.         padcount *= nlines;
  346.       }
  347.     while (*string)
  348.       (*outfun) (*string++);
  349.  
  350.     /* padcount is now in units of tenths of msec.  */
  351.     padcount *= speeds[ospeed];
  352.     padcount += 500;
  353.     padcount /= 1000;
  354.     if (speeds[ospeed] < 0)
  355.       padcount = -padcount;
  356.     else
  357.       {
  358.         padcount += 50;
  359.         padcount /= 100;
  360.       }
  361.  
  362.     while (padcount-- > 0)
  363.       (*outfun) (PC);
  364. }
  365.  
  366. /* Finding the termcap entry in the termcap data base */
  367.  
  368. struct buffer
  369.     {
  370.       char *beg;
  371.       int size;
  372.       char *ptr;
  373.       int ateof;
  374.       int full;
  375.     };
  376.  
  377. /* Forward declarations of static functions */
  378.  
  379. static int scan_file ();
  380. static char *gobble_line ();
  381. static int compare_contin ();
  382. static int name_match ();
  383.  
  384. #ifdef VMS
  385.  
  386. #include <rmsdef.h>
  387. #include <fab.h>
  388. #include <nam.h>
  389.  
  390. static int
  391. legal_filename_p (fn)
  392.        char *fn;
  393. {
  394.     struct FAB fab = cc$rms_fab;
  395.     struct NAM nam = cc$rms_nam;
  396.     char esa[NAM$C_MAXRSS];
  397.  
  398.     fab.fab$l_fna = fn;
  399.     fab.fab$b_fns = strlen(fn);
  400.     fab.fab$l_nam = &nam;
  401.     fab.fab$l_fop = FAB$M_NAM;
  402.  
  403.     nam.nam$l_esa = esa;
  404.     nam.nam$b_ess = sizeof esa;
  405.  
  406.     return SYS$PARSE(&fab, 0, 0) == RMS$_NORMAL;
  407. }
  408.  
  409. #endif /* VMS */
  410.  
  411. /* Find the termcap entry data for terminal type `name'
  412.      and store it in the block that `bp' points to.
  413.      Record its address for future use.
  414.  
  415.      If `bp' is zero, space is dynamically allocated.  */
  416.  
  417. int
  418. tgetent (bp, name)
  419.        char *bp, *name;
  420. {
  421.     register char *tem;
  422.     register int fd;
  423.     struct buffer buf;
  424.     register char *bp1;
  425.     char *bp2;
  426.     char *term;
  427.     int malloc_size = 0;
  428.     register int c;
  429.     char *tcenv;            /* TERMCAP value, if it contains :tc=.  */
  430.     char *indirect = 0;        /* Terminal type in :tc= in TERMCAP value.  */
  431.     int filep;
  432.  
  433. #ifdef DEBUG
  434.     if(debuglevel & 4)
  435.         fprintf(stderr,"tgetent(buf, %s)\n", name);
  436. #endif
  437.  
  438.     tem = (char *) getenv ("TERMCAP");
  439.     if (tem && *tem == 0) tem = 0;
  440.  
  441. #ifdef VMS
  442.     filep = (tem && legal_filename_p (tem));
  443. #else
  444.     filep = (tem && (*tem == '/'));
  445. #endif /* VMS */
  446.  
  447.     /* If tem is non-null and starts with / (in the un*x case, that is),
  448.        it is a file name to use instead of /etc/termcap.
  449.        If it is non-null and does not start with /,
  450.        it is the entry itself, but only if
  451.        the name the caller requested matches the TERM variable.  */
  452.  
  453.     if (tem && !filep && !strcmp (name, getenv ("TERM")))
  454.      {
  455.         indirect = tgetst1 (find_capability (tem, "tc"), 0);
  456.         if (!indirect)
  457.            {
  458.            if (!bp)
  459.              bp = tem;
  460.            else
  461.              strcpy (bp, tem);
  462.            goto ret;
  463.          }
  464.         else
  465.          {            /* we will need to read /etc/termcap */
  466.            tcenv = tem;
  467.            tem = 0;
  468.          }
  469.      }
  470.     else
  471.         indirect = (char *) 0;
  472.  
  473.     if (!tem)
  474. #ifdef VMS
  475.       tem = "emacs_library:[etc]termcap.dat";
  476. #else
  477.       tem = "/etc/termcap";
  478. #endif
  479.  
  480.     /* Here we know we must search a file and tem has its name.  */
  481.  
  482. #ifdef DEBUG
  483.     if(debuglevel & 4)
  484.         fprintf(stderr,"%s: ", tem);
  485. #endif
  486.     fd = open (tem, 0, 0);
  487.     if (fd < 0)
  488.       return -1;
  489.  
  490.     buf.size = BUFSIZE;
  491.     buf.beg = xmalloc (buf.size);
  492.     term = indirect ? indirect : name;
  493.  
  494. #ifdef DEBUG
  495.     if(debuglevel & 4)
  496.         fprintf(stderr,"...opened\n");
  497. #endif
  498.     if (!bp)
  499.       {
  500.         malloc_size = indirect ? strlen (tcenv) + 1 : buf.size;
  501.         bp = xmalloc (malloc_size);
  502.       }
  503.     bp1 = bp;
  504.  
  505.     if (indirect)            /* copy the data from the environment variable */
  506.       {
  507.         strcpy (bp, tcenv);
  508.         bp1 += strlen (tcenv);
  509.       }
  510.  
  511.     while (term)
  512.       {
  513. #ifdef DEBUG
  514.     if(debuglevel & 4)
  515.         fprintf(stderr,"term = %s\n", term);
  516. #endif
  517.         /* Scan file, reading it via buf, till find start of main entry */
  518.         if (scan_file (term, fd, &buf) == 0)
  519.             return 0;
  520.  
  521.         /* Free old `term' if appropriate.  */
  522.         if (term != name)
  523.             free (term);
  524.  
  525.         /* If `bp' is malloc'd by us, make sure it is big enough.  */
  526.         if (malloc_size)
  527.          {
  528.             malloc_size = bp1 - bp + buf.size;
  529.             tem = xrealloc (bp, malloc_size);
  530.             bp1 += tem - bp;
  531.             bp = tem;
  532.          }
  533.  
  534.         bp2 = bp1;
  535.  
  536.         /* Copy the line of the entry from buf into bp.  */
  537.         tem = buf.ptr;
  538.         while ((*bp1++ = c = *tem++) && c != '\n') {
  539.             /* Drop out any \ newline sequence, and following whitespace */
  540.             if (c == '\\' && *tem == '\n')
  541.              {
  542.                 bp1--;
  543.                 tem++;
  544.                 while ((c = *tem++) == ' ' || c == '\t')
  545.                     ;
  546.                 tem--;
  547.              }
  548.         }
  549.         *bp1 = 0;
  550.  
  551.         /* Does this entry refer to another terminal type's entry?  */
  552.         /* If something is found, copy it into heap and null-terminate it */
  553.         term = tgetst1 (find_capability (bp2, "tc"), 0);
  554.       }
  555.  
  556.     close (fd);
  557.     free (buf.beg);
  558.  
  559.     if (malloc_size)
  560.       {
  561.         bp = xrealloc (bp, bp1 - bp + 1);
  562.       }
  563.  
  564.  ret:
  565.     term_entry = bp;
  566.  
  567. #ifdef DEBUG
  568.     if(debuglevel & 4)
  569.         fprintf(stderr,"term_entry = %s\n", term_entry);
  570. #endif
  571.     if (malloc_size)
  572.       return (int) bp;
  573.     return 1;
  574. }
  575.  
  576. /* Given file open on `fd' and buffer `bufp',
  577.    scan the file from the beginning until a line is found
  578.    that starts the entry for terminal type `string'.
  579.    Returns 1 if successful, with that line in `bufp',
  580.    or returns 0 if no entry found in the file.  */
  581.  
  582. static int
  583. scan_file (string, fd, bufp)
  584.        char *string;
  585.        int fd;
  586.        register struct buffer *bufp;
  587. {
  588.     register char *end;
  589.  
  590.     bufp->ptr = bufp->beg;
  591.     bufp->full = 0;
  592.     bufp->ateof = 0;
  593.     *bufp->ptr = 0;
  594.  
  595.     lseek (fd, 0L, 0);
  596.  
  597.     while (!bufp->ateof)
  598.       {
  599.         /* Read a line into the buffer */
  600.         end = 0;
  601.         do
  602.     {
  603.       /* if it is continued, append another line to it,
  604.          until a non-continued line ends */
  605.       end = gobble_line (fd, bufp, end);
  606.     }
  607.         while (!bufp->ateof && end[-2] == '\\');
  608.  
  609.         if (*bufp->ptr != '#'
  610.       && name_match (bufp->ptr, string))
  611.     return 1;
  612.  
  613.         /* Discard the line just processed */
  614.         bufp->ptr = end;
  615.       }
  616.     return 0;
  617. }
  618.  
  619. /* Return nonzero if NAME is one of the names specified
  620.      by termcap entry LINE.  */
  621.  
  622. static int
  623. name_match (line, name)
  624.        char *line, *name;
  625. {
  626.     register char *tem;
  627.  
  628.     if (!compare_contin (line, name))
  629.       return 1;
  630.     /* This line starts an entry.  Is it the right one?  */
  631.     for (tem = line; *tem && *tem != '\n' && *tem != ':'; tem++)
  632.       if (*tem == '|' && !compare_contin (tem + 1, name))
  633.         return 1;
  634.  
  635.     return 0;
  636. }
  637.  
  638. static int
  639. compare_contin (str1, str2)
  640.        register char *str1, *str2;
  641. {
  642.     register int c1, c2;
  643.     while (1)
  644.       {
  645.         c1 = *str1++;
  646.         c2 = *str2++;
  647.         while (c1 == '\\' && *str1 == '\n')
  648.     {
  649.       str1++;
  650.       while ((c1 = *str1++) == ' ' || c1 == '\t');
  651.     }
  652.         if (c2 == '\0')        /* end of type being looked up */
  653.     {
  654.       if (c1 == '|' || c1 == ':') /* If end of name in data base, */
  655.         return 0;        /* we win. */
  656.       else
  657.         return 1;
  658.           }
  659.         else if (c1 != c2)
  660.     return 1;
  661.       }
  662. }
  663.  
  664. /* Make sure that the buffer <- `bufp' contains a full line
  665.      of the file open on `fd', starting at the place `bufp->ptr'
  666.      points to.  Can read more of the file, discard stuff before
  667.      `bufp->ptr', or make the buffer bigger.
  668.  
  669.      Returns the pointer to after the newline ending the line,
  670.      or to the end of the file, if there is no newline to end it.
  671.  
  672.      Can also merge on continuation lines.  If `append_end' is
  673.      nonzero, it points past the newline of a line that is
  674.      continued; we add another line onto it and regard the whole
  675.      thing as one line.  The caller decides when a line is continued.  */
  676.  
  677. static char *
  678. gobble_line (fd, bufp, append_end)
  679.        int fd;
  680.        register struct buffer *bufp;
  681.        char *append_end;
  682. {
  683.     register char *end;
  684.     register int nread;
  685.     register char *buf = bufp->beg;
  686.     register char *tem;
  687.  
  688.     if (append_end == 0)
  689.       append_end = bufp->ptr;
  690.  
  691.     while (1)
  692.       {
  693.         end = append_end;
  694.         while (*end && *end != '\n') end++;
  695.         if (*end)
  696.           break;
  697.         if (bufp->ateof)
  698.     return buf + bufp->full;
  699.         if (bufp->ptr == buf)
  700.     {
  701.       if (bufp->full == bufp->size)
  702.         {
  703.           bufp->size *= 2;
  704.           tem = xrealloc (buf, bufp->size);
  705.           bufp->ptr += tem - buf;
  706.           append_end += tem - buf;
  707.           bufp->beg = buf = tem;
  708.         }
  709.     }
  710.         else
  711.     {
  712.       append_end -= bufp->ptr - buf;
  713.       bcopy (bufp->ptr, buf, bufp->full -= bufp->ptr - buf);
  714.       bufp->ptr = buf;
  715.     }
  716.         if (!(nread = read (fd, buf + bufp->full, bufp->size - bufp->full)))
  717.     bufp->ateof = 1;
  718.         bufp->full += nread;
  719.         if (bufp->full != bufp->size)
  720.     buf[bufp->full] = 0;
  721.       }
  722.     return end + 1;
  723. }
  724.  
  725. #ifdef TEST
  726. #include <stdio.h>
  727.  
  728. main (argc, argv)
  729.        int argc;
  730.        char **argv;
  731. {
  732.     char *term;
  733.     char *buf;
  734.  
  735.     term = argv[1];
  736.     printf ("TERM: %s\n", term);
  737.  
  738.     buf = (char *) tgetent (0, term);
  739.     if ((int) buf <= 0)
  740.       {
  741.         printf ("No entry.\n");
  742.         return 0;
  743.       }
  744.  
  745.     printf ("Entry: %s\n", buf);
  746.  
  747.     tprint ("cm");
  748.     tprint ("AL");
  749.  
  750.     printf ("co: %d\n", tgetnum ("co"));
  751.     printf ("am: %d\n", tgetflag ("am"));
  752. }
  753.  
  754. tprint (cap)
  755.        char *cap;
  756. {
  757.     char *x = tgetstr (cap, 0);
  758.     register char *y;
  759.  
  760.     printf ("%s: ", cap);
  761.     if (x)
  762.       {
  763.         for (y = x; *y; y++)
  764.     if (*y <= ' ' || *y == 0177)
  765.       printf ("\\%0o", *y);
  766.     else
  767.       putchar (*y);
  768.         free (x);
  769.       }
  770.     else
  771.       printf ("none");
  772.     putchar ('\n');
  773. }
  774.  
  775. #endif /* TEST */
  776.