home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / trmcap11.zip / termcap-1.1 / termcap.c < prev    next >
C/C++ Source or Header  |  1992-10-01  |  15KB  |  694 lines

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