home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1999 March / PCWK3A99.iso / Linux / DDD331 / DDD-3_1_.000 / DDD-3_1_ / ddd-3.1.1 / termcap / termcap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-08  |  14.8 KB  |  713 lines

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