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