X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/3eca4629203c3038f04db312999ca32d8e292e50..20e94fdd8ec2a82d0789fc4ea354a803a14e2ec3:/src/termcap.c diff --git a/src/termcap.c b/src/termcap.c index 27a20a67ae..d1b05e8df9 100644 --- a/src/termcap.c +++ b/src/termcap.c @@ -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. + 2004, 2005, 2006, 2007, 2008, 2011 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 @@ -30,10 +30,6 @@ Boston, MA 02110-1301, USA. */ #include "msdos.h" #endif -#ifndef NULL -#define NULL (char *) 0 -#endif - /* BUFSIZE is the initial size allocated for the buffer for reading the termcap file. It is not a limit. @@ -157,7 +153,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; @@ -323,10 +319,10 @@ tputs (register const char *str, int nlines, int (*outfun) (int)) struct termcap_buffer { char *beg; - int size; + ptrdiff_t size; char *ptr; int ateof; - int full; + ptrdiff_t full; }; /* Forward declarations of static functions. */ @@ -338,8 +334,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] == ':'; } @@ -367,7 +362,7 @@ tgetent (char *bp, const 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. */ @@ -382,7 +377,7 @@ tgetent (char *bp, const char *name) if (!bp) { malloc_size = 1 + strlen (term); - bp = (char *) xmalloc (malloc_size); + bp = xmalloc (malloc_size); } strcpy (bp, term); goto ret; @@ -401,7 +396,7 @@ tgetent (char *bp, const 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); @@ -445,13 +440,13 @@ tgetent (char *bp, const char *name) buf.size = BUFSIZE; /* Add 1 to size to ensure room for terminating null. */ - buf.beg = (char *) xmalloc (buf.size + 1); + 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; @@ -468,22 +463,22 @@ tgetent (char *bp, const 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; } @@ -506,10 +501,10 @@ tgetent (char *bp, const 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; @@ -620,7 +615,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; @@ -637,12 +631,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 @@ -661,15 +657,31 @@ gobble_line (int fd, register struct termcap_buffer *bufp, char *append_end) #ifdef TEST -#ifdef NULL -#undef NULL -#endif - #include -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; @@ -691,28 +703,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 */ -