]> code.delx.au - gnu-emacs/blobdiff - src/termcap.c
Merge latest Org fixes (commit 7524ef2).
[gnu-emacs] / src / termcap.c
index ea4ef244f641303879beb55c6c08bc9073f2e652..82c2b1fda0734ded92a77b0fc86056568bb7abc6 100644 (file)
@@ -1,6 +1,6 @@
 /* Work-alike for termcap, plus extra features.
-   Copyright (C) 1985, 1986, 1993, 1994, 1995, 2000, 2001, 2002, 2003,
-                 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+   Copyright (C) 1985-1986, 1993-1995, 2000-2008, 2011, 2013 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,29 +13,18 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 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., 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA.  */
+along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 /* Emacs config.h may rename various library functions such as malloc.  */
 #include <config.h>
-#include <setjmp.h>
-#include <lisp.h>              /* xmalloc is here */
-/* Get the O_* definitions for open et al.  */
 #include <sys/file.h>
-#ifdef HAVE_FCNTL_H
 #include <fcntl.h>
-#endif
-#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif
-
-#ifndef NULL
-#define NULL (char *) 0
-#endif
 
-#ifndef O_RDONLY
-#define O_RDONLY 0
+#include "lisp.h"
+#include "tparam.h"
+#ifdef MSDOS
+#include "msdos.h"
 #endif
 
 /* BUFSIZE is the initial size allocated for the buffer
@@ -73,7 +62,7 @@ static char *tgetst1 (char *ptr, char **area);
    0 if not found.  */
 
 static char *
-find_capability (register char *bp, register char *cap)
+find_capability (register char *bp, register const char *cap)
 {
   for (; *bp; bp++)
     if (bp[0] == ':'
@@ -84,7 +73,7 @@ find_capability (register char *bp, register char *cap)
 }
 
 int
-tgetnum (char *cap)
+tgetnum (const char *cap)
 {
   register char *ptr = find_capability (term_entry, cap);
   if (!ptr || ptr[-1] != '#')
@@ -93,7 +82,7 @@ tgetnum (char *cap)
 }
 
 int
-tgetflag (char *cap)
+tgetflag (const char *cap)
 {
   register char *ptr = find_capability (term_entry, cap);
   return ptr && ptr[-1] == ':';
@@ -105,7 +94,7 @@ tgetflag (char *cap)
    If AREA is null, space is allocated with `malloc'.  */
 
 char *
-tgetstr (char *cap, char **area)
+tgetstr (const char *cap, char **area)
 {
   register char *ptr = find_capability (term_entry, cap);
   if (!ptr || (ptr[-1] != '=' && ptr[-1] != '~'))
@@ -161,7 +150,7 @@ tgetst1 (char *ptr, char **area)
       p = ptr;
       while ((c = *p++) && c != ':' && c != '\n')
        ;
-      ret = (char *) xmalloc (p - ptr + 1);
+      ret = xmalloc (p - ptr + 1);
     }
   else
     ret = *area;
@@ -268,45 +257,19 @@ tgetst1 (char *ptr, char **area)
 \f
 /* Outputting a string with padding.  */
 
-#ifndef emacs
-short ospeed;
-/* If OSPEED is 0, we use this as the actual baud rate.  */
-int tputs_baud_rate;
-#endif
-
 char PC;
 
-#ifndef emacs
-/* Actual baud rate if positive;
-   - baud rate / 100 if negative.  */
-
-static const int speeds[] =
-  {
-    0, 50, 75, 110, 135, 150, -2, -3, -6, -12,
-    -18, -24, -48, -96, -192, -288, -384, -576, -1152
-  };
-
-#endif /* not emacs */
-
 void
-tputs (register char *str, int nlines, register int (*outfun) (/* ??? */))
+tputs (register const char *str, int nlines, int (*outfun) (int))
 {
   register int padcount = 0;
   register int speed;
 
-#ifdef emacs
-  extern EMACS_INT baud_rate;
   speed = baud_rate;
   /* For quite high speeds, convert to the smaller
      units to avoid overflow.  */
   if (speed > 10000)
     speed = - speed / 100;
-#else
-  if (ospeed == 0)
-    speed = tputs_baud_rate;
-  else
-    speed = speeds[ospeed];
-#endif
 
   if (!str)
     return;
@@ -353,10 +316,10 @@ tputs (register char *str, int nlines, register int (*outfun) (/* ??? */))
 struct termcap_buffer
   {
     char *beg;
-    int size;
+    ptrdiff_t size;
     char *ptr;
     int ateof;
-    int full;
+    ptrdiff_t full;
   };
 
 /* Forward declarations of static functions.  */
@@ -368,8 +331,7 @@ static int name_match (char *line, char *name);
 
 #ifdef MSDOS /* MW, May 1993 */
 static int
-valid_filename_p (fn)
-     char *fn;
+valid_filename_p (char *fn)
 {
   return *fn == '/' || fn[1] == ':';
 }
@@ -389,7 +351,7 @@ valid_filename_p (fn)
    in it, and some other value otherwise.  */
 
 int
-tgetent (char *bp, char *name)
+tgetent (char *bp, const char *name)
 {
   register char *termcap_name;
   register int fd;
@@ -397,7 +359,7 @@ tgetent (char *bp, char *name)
   register char *bp1;
   char *tc_search_point;
   char *term;
-  int malloc_size = 0;
+  ptrdiff_t malloc_size = 0;
   register int c;
   char *tcenv = NULL;          /* TERMCAP value, if it contains :tc=.  */
   char *indirect = NULL;       /* Terminal type in :tc= in TERMCAP value.  */
@@ -412,7 +374,7 @@ tgetent (char *bp, char *name)
       if (!bp)
        {
          malloc_size = 1 + strlen (term);
-         bp = (char *) xmalloc (malloc_size);
+         bp = xmalloc (malloc_size);
        }
       strcpy (bp, term);
       goto ret;
@@ -431,7 +393,7 @@ tgetent (char *bp, char *name)
   if (termcap_name && (*termcap_name == '\\'
                       || *termcap_name == '/'
                       || termcap_name[1] == ':'))
-    dostounix_filename(termcap_name);
+    dostounix_filename (termcap_name);
 #endif
 
   filep = termcap_name && valid_filename_p (termcap_name);
@@ -475,13 +437,13 @@ tgetent (char *bp, char *name)
 
   buf.size = BUFSIZE;
   /* Add 1 to size to ensure room for terminating null.  */
-  buf.beg = (char *) xmalloc (buf.size + 1);
-  term = indirect ? indirect : name;
+  buf.beg = xmalloc (buf.size + 1);
+  term = indirect ? indirect : (char *)name;
 
   if (!bp)
     {
       malloc_size = indirect ? strlen (tcenv) + 1 : buf.size;
-      bp = (char *) xmalloc (malloc_size);
+      bp = xmalloc (malloc_size);
     }
   tc_search_point = bp1 = bp;
 
@@ -498,22 +460,22 @@ tgetent (char *bp, char *name)
       if (scan_file (term, fd, &buf) == 0)
        {
          close (fd);
-         free (buf.beg);
+         xfree (buf.beg);
          if (malloc_size)
-           free (bp);
+           xfree (bp);
          return 0;
        }
 
       /* Free old `term' if appropriate.  */
       if (term != name)
-       free (term);
+       xfree (term);
 
       /* If BP is malloc'd by us, make sure it is big enough.  */
       if (malloc_size)
        {
-         int offset1 = bp1 - bp, offset2 = tc_search_point - bp;
+         ptrdiff_t offset1 = bp1 - bp, offset2 = tc_search_point - bp;
          malloc_size = offset1 + buf.size;
-         bp = termcap_name = (char *) xrealloc (bp, malloc_size);
+         bp = termcap_name = xrealloc (bp, malloc_size);
          bp1 = termcap_name + offset1;
          tc_search_point = termcap_name + offset2;
        }
@@ -536,10 +498,10 @@ tgetent (char *bp, char *name)
     }
 
   close (fd);
-  free (buf.beg);
+  xfree (buf.beg);
 
   if (malloc_size)
-    bp = (char *) xrealloc (bp, bp1 - bp + 1);
+    bp = xrealloc (bp, bp1 - bp + 1);
 
  ret:
   term_entry = bp;
@@ -650,7 +612,6 @@ gobble_line (int fd, register struct termcap_buffer *bufp, char *append_end)
   register char *end;
   register int nread;
   register char *buf = bufp->beg;
-  register char *tem;
 
   if (!append_end)
     append_end = bufp->ptr;
@@ -667,12 +628,14 @@ gobble_line (int fd, register struct termcap_buffer *bufp, char *append_end)
        {
          if (bufp->full == bufp->size)
            {
-             bufp->size *= 2;
+             ptrdiff_t ptr_offset = bufp->ptr - buf;
+             ptrdiff_t append_end_offset = append_end - buf;
              /* Add 1 to size to ensure room for terminating null.  */
-             tem = (char *) xrealloc (buf, bufp->size + 1);
-             bufp->ptr = (bufp->ptr - buf) + tem;
-             append_end = (append_end - buf) + tem;
-             bufp->beg = buf = tem;
+             ptrdiff_t size = bufp->size + 1;
+             bufp->beg = buf = xpalloc (buf, &size, 1, -1, 1);
+             bufp->size = size - 1;
+             bufp->ptr = buf + ptr_offset;
+             append_end = buf + append_end_offset;
            }
        }
       else
@@ -691,15 +654,31 @@ gobble_line (int fd, register struct termcap_buffer *bufp, char *append_end)
 \f
 #ifdef TEST
 
-#ifdef NULL
-#undef NULL
-#endif
-
 #include <stdio.h>
 
-main (argc, argv)
-     int argc;
-     char **argv;
+static void
+tprint (char *cap)
+{
+  char *x = tgetstr (cap, 0);
+  register char *y;
+
+  printf ("%s: ", cap);
+  if (x)
+    {
+      for (y = x; *y; y++)
+       if (*y <= ' ' || *y == 0177)
+         printf ("\\%0o", *y);
+       else
+         putchar (*y);
+      free (x);
+    }
+  else
+    printf ("none");
+  putchar ('\n');
+}
+
+int
+main (int argc, char **argv)
 {
   char *term;
   char *buf;
@@ -721,30 +700,8 @@ main (argc, argv)
 
   printf ("co: %d\n", tgetnum ("co"));
   printf ("am: %d\n", tgetflag ("am"));
-}
 
-tprint (cap)
-     char *cap;
-{
-  char *x = tgetstr (cap, 0);
-  register char *y;
-
-  printf ("%s: ", cap);
-  if (x)
-    {
-      for (y = x; *y; y++)
-       if (*y <= ' ' || *y == 0177)
-         printf ("\\%0o", *y);
-       else
-         putchar (*y);
-      free (x);
-    }
-  else
-    printf ("none");
-  putchar ('\n');
+  return 0;
 }
 
 #endif /* TEST */
-
-/* arch-tag: c2e8d427-2271-4fac-95fe-411857238b80
-   (do not change this comment) */