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