home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / tput-1.0 / termcap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-10  |  13.9 KB  |  677 lines

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