]> code.delx.au - gnu-emacs/blobdiff - src/termcap.c
Emulate `readlink' for DJGPP versions before 2.04.
[gnu-emacs] / src / termcap.c
index f802704383ce939517d1f9fde424e24f61110d1b..69ce56d93b3d1e3fe3f4d30e77d4d7a32bd5e4fa 100644 (file)
@@ -1,5 +1,6 @@
 /* Work-alike for termcap, plus extra features.
-   Copyright (C) 1985, 86, 93, 94, 95, 2000 Free Software Foundation, Inc.
+   Copyright (C) 1985, 1986, 1993, 1994, 1995, 2000, 2001, 2002, 2003,
+                 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -13,59 +14,22 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 /* Emacs config.h may rename various library functions such as malloc.  */
-#ifdef HAVE_CONFIG_H
 #include <config.h>
-#endif
-
-#ifdef emacs
-
-#include <lisp.h>              /* xmalloc is here */
-/* Get the O_* definitions for open et al.  */
+#include <setjmp.h>
 #include <sys/file.h>
-#ifdef HAVE_FCNTL_H
 #include <fcntl.h>
-#endif
-#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif
-
-#else /* not emacs */
 
-#ifdef STDC_HEADERS
-#include <stdlib.h>
-#include <string.h>
-#else
-char *getenv ();
-char *malloc ();
-char *realloc ();
-#endif
-
-/* Do this after the include, in case string.h prototypes bcopy.  */
-#if (defined(HAVE_STRING_H) || defined(STDC_HEADERS)) && !defined(bcopy)
-#define bcopy(s, d, n) memcpy ((d), (s), (n))
-#endif
-
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#ifdef _POSIX_VERSION
-#include <fcntl.h>
-#endif
-
-#endif /* not emacs */
+#include "lisp.h"
 
 #ifndef NULL
 #define NULL (char *) 0
 #endif
 
-#ifndef O_RDONLY
-#define O_RDONLY 0
-#endif
-
 /* BUFSIZE is the initial size allocated for the buffer
    for reading the termcap file.
    It is not a limit.
@@ -87,37 +51,6 @@ int bufsize = 128;
 #define TERMCAP_FILE "/etc/termcap"
 #endif
 
-#ifndef emacs
-static void
-memory_out ()
-{
-  write (2, "virtual memory exhausted\n", 25);
-  exit (1);
-}
-
-static char *
-xmalloc (size)
-     unsigned size;
-{
-  register char *tem = malloc (size);
-
-  if (!tem)
-    memory_out ();
-  return tem;
-}
-
-static char *
-xrealloc (ptr, size)
-     char *ptr;
-     unsigned size;
-{
-  register char *tem = realloc (ptr, size);
-
-  if (!tem)
-    memory_out ();
-  return tem;
-}
-#endif /* not emacs */
 \f
 /* Looking up capabilities in the entry already found.  */
 
@@ -125,15 +58,14 @@ xrealloc (ptr, size)
    for tgetnum, tgetflag and tgetstr to find.  */
 static char *term_entry;
 
-static char *tgetst1 ();
+static char *tgetst1 (char *ptr, char **area);
 
 /* Search entry BP for capability CAP.
    Return a pointer to the capability (in BP) if found,
    0 if not found.  */
 
 static char *
-find_capability (bp, cap)
-     register char *bp, *cap;
+find_capability (register char *bp, register char *cap)
 {
   for (; *bp; bp++)
     if (bp[0] == ':'
@@ -144,8 +76,7 @@ find_capability (bp, cap)
 }
 
 int
-tgetnum (cap)
-     char *cap;
+tgetnum (char *cap)
 {
   register char *ptr = find_capability (term_entry, cap);
   if (!ptr || ptr[-1] != '#')
@@ -154,8 +85,7 @@ tgetnum (cap)
 }
 
 int
-tgetflag (cap)
-     char *cap;
+tgetflag (char *cap)
 {
   register char *ptr = find_capability (term_entry, cap);
   return ptr && ptr[-1] == ':';
@@ -167,9 +97,7 @@ tgetflag (cap)
    If AREA is null, space is allocated with `malloc'.  */
 
 char *
-tgetstr (cap, area)
-     char *cap;
-     char **area;
+tgetstr (char *cap, char **area)
 {
   register char *ptr = find_capability (term_entry, cap);
   if (!ptr || (ptr[-1] != '=' && ptr[-1] != '~'))
@@ -182,7 +110,7 @@ tgetstr (cap, area)
    gives meaning of character following \, or a space if no special meaning.
    Sixteen characters per line within the string.  */
 
-static char esctab[]
+static const char esctab[]
   = " \057\026  \047\014         \
      \025   \015      \
    \005 \013          \
@@ -192,7 +120,7 @@ static char esctab[]
    gives meaning of character following \, or a space if no special meaning.
    Eight characters per line within the string.  */
 
-static char esctab[]
+static const char esctab[]
   = " \007\010  \033\014 \
       \012 \
   \015 \011 \013 \
@@ -207,9 +135,7 @@ static char esctab[]
    or NULL if PTR is NULL.  */
 
 static char *
-tgetst1 (ptr, area)
-     char *ptr;
-     char **area;
+tgetst1 (char *ptr, char **area)
 {
   register char *p, *r;
   register int c;
@@ -279,6 +205,52 @@ tgetst1 (ptr, area)
        }
       *r++ = c;
     }
+
+  /* Sometimes entries have "%pN" which means use parameter N in the
+     next %-substitution.  If all such N are continuous in the range
+     [1,9] we can remove each "%pN" because they are redundant, thus
+     reducing bandwidth requirements.  True, Emacs is well beyond the
+     days of 150baud teletypes, but some of its users aren't much so.
+
+     This pass could probably be integrated into the one above but
+     abbreviation expansion makes that effort a little more hairy than
+     its worth; this is cleaner.  */
+  {
+    register int last_p_param = 0;
+    int remove_p_params = 1;
+    struct { char *beg; int len; } cut[11];
+
+    for (cut[0].beg = p = ret; p < r - 3; p++)
+      {
+       if (!remove_p_params)
+         break;
+       if (*p == '%' && *(p + 1) == 'p')
+         {
+           if (*(p + 2) - '0' == 1 + last_p_param)
+             {
+               cut[last_p_param].len = p - cut[last_p_param].beg;
+               last_p_param++;
+               p += 3;
+               cut[last_p_param].beg = p;
+             }
+           else                                /* not continuous: bail */
+             remove_p_params = 0;
+           if (last_p_param > 10)              /* too many: bail */
+             remove_p_params = 0;
+         }
+      }
+    if (remove_p_params && last_p_param)
+      {
+       register int i;
+       char *wp;
+
+       cut[last_p_param].len = r - cut[last_p_param].beg;
+       for (i = 0, wp = ret; i <= last_p_param; wp += cut[i++].len)
+         memcpy (wp, cut[i].beg, cut[i].len);
+       r = wp;
+      }
+  }
+
   *r = '\0';
   /* Update *AREA.  */
   if (area)
@@ -288,34 +260,14 @@ tgetst1 (ptr, area)
 \f
 /* Outputting a string with padding.  */
 
-/* If OSPEED is 0, we use this as the actual baud rate.  */
-int tputs_baud_rate;
 char PC;
 
-/* Actual baud rate if positive;
-   - baud rate / 100 if negative.  */
-
-static int speeds[] =
-  {
-#ifdef VMS
-    0, 50, 75, 110, 134, 150, -3, -6, -12, -18,
-    -20, -24, -36, -48, -72, -96, -192
-#else /* not VMS */
-    0, 50, 75, 110, 135, 150, -2, -3, -6, -12,
-    -18, -24, -48, -96, -192, -288, -384, -576, -1152
-#endif /* not VMS */
-  };
-
 void
-tputs (str, nlines, outfun)
-     register char *str;
-     int nlines;
-     register int (*outfun) ();
+tputs (register char *str, int nlines, register int (*outfun) (/* ??? */))
 {
   register int padcount = 0;
   register int speed;
 
-  extern int baud_rate;
   speed = baud_rate;
   /* For quite high speeds, convert to the smaller
      units to avoid overflow.  */
@@ -375,37 +327,10 @@ struct termcap_buffer
 
 /* Forward declarations of static functions.  */
 
-static int scan_file ();
-static char *gobble_line ();
-static int compare_contin ();
-static int name_match ();
-
-#ifdef VMS
-
-#include <rmsdef.h>
-#include <fab.h>
-#include <nam.h>
-
-static int
-valid_filename_p (fn)
-     char *fn;
-{
-  struct FAB fab = cc$rms_fab;
-  struct NAM nam = cc$rms_nam;
-  char esa[NAM$C_MAXRSS];
-
-  fab.fab$l_fna = fn;
-  fab.fab$b_fns = strlen(fn);
-  fab.fab$l_nam = &nam;
-  fab.fab$l_fop = FAB$M_NAM;
-
-  nam.nam$l_esa = esa;
-  nam.nam$b_ess = sizeof esa;
-
-  return SYS$PARSE(&fab, 0, 0) == RMS$_NORMAL;
-}
-
-#else /* !VMS */
+static int scan_file (char *str, int fd, register struct termcap_buffer *bufp);
+static char *gobble_line (int fd, register struct termcap_buffer *bufp, char *append_end);
+static int compare_contin (register char *str1, register char *str2);
+static int name_match (char *line, char *name);
 
 #ifdef MSDOS /* MW, May 1993 */
 static int
@@ -418,8 +343,6 @@ valid_filename_p (fn)
 #define valid_filename_p(fn) (*(fn) == '/')
 #endif
 
-#endif /* !VMS */
-
 /* Find the termcap entry data for terminal type NAME
    and store it in the block that BP points to.
    Record its address for future use.
@@ -432,8 +355,7 @@ valid_filename_p (fn)
    in it, and some other value otherwise.  */
 
 int
-tgetent (bp, name)
-     char *bp, *name;
+tgetent (char *bp, char *name)
 {
   register char *termcap_name;
   register int fd;
@@ -555,11 +477,11 @@ tgetent (bp, name)
       /* If BP is malloc'd by us, make sure it is big enough.  */
       if (malloc_size)
        {
-         malloc_size = bp1 - bp + buf.size;
-         termcap_name = (char *) xrealloc (bp, malloc_size);
-         bp1 += termcap_name - bp;
-         tc_search_point += termcap_name - bp;
-         bp = termcap_name;
+         int offset1 = bp1 - bp, offset2 = tc_search_point - bp;
+         malloc_size = offset1 + buf.size;
+         bp = termcap_name = (char *) xrealloc (bp, malloc_size);
+         bp1 = termcap_name + offset1;
+         tc_search_point = termcap_name + offset2;
        }
 
       /* Copy the line of the entry from buf into bp.  */
@@ -597,10 +519,7 @@ tgetent (bp, name)
    or 0 if no entry is found in the file.  */
 
 static int
-scan_file (str, fd, bufp)
-     char *str;
-     int fd;
-     register struct termcap_buffer *bufp;
+scan_file (char *str, int fd, register struct termcap_buffer *bufp)
 {
   register char *end;
 
@@ -637,8 +556,7 @@ scan_file (str, fd, bufp)
    by termcap entry LINE.  */
 
 static int
-name_match (line, name)
-     char *line, *name;
+name_match (char *line, char *name)
 {
   register char *tem;
 
@@ -653,8 +571,7 @@ name_match (line, name)
 }
 
 static int
-compare_contin (str1, str2)
-     register char *str1, *str2;
+compare_contin (register char *str1, register char *str2)
 {
   register int c1, c2;
   while (1)
@@ -694,10 +611,7 @@ compare_contin (str1, str2)
    thing as one line.  The caller decides when a line is continued.  */
 
 static char *
-gobble_line (fd, bufp, append_end)
-     int fd;
-     register struct termcap_buffer *bufp;
-     char *append_end;
+gobble_line (int fd, register struct termcap_buffer *bufp, char *append_end)
 {
   register char *end;
   register int nread;
@@ -730,7 +644,7 @@ gobble_line (fd, bufp, append_end)
       else
        {
          append_end -= bufp->ptr - buf;
-         bcopy (bufp->ptr, buf, bufp->full -= bufp->ptr - buf);
+         memcpy (buf, bufp->ptr, bufp->full -= bufp->ptr - buf);
          bufp->ptr = buf;
        }
       if (!(nread = read (fd, buf + bufp->full, bufp->size - bufp->full)))
@@ -797,3 +711,4 @@ tprint (cap)
 }
 
 #endif /* TEST */
+