home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / OS2 / gnuinfo.zip / lib / termcap.c < prev    next >
C/C++ Source or Header  |  1997-12-26  |  20KB  |  797 lines

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