From 22a94f704157dfab5ccecf1a5d6b722ed51f357e Mon Sep 17 00:00:00 2001 From: blymn Date: Sat, 20 May 2000 13:55:10 +0000 Subject: [PATCH] * Removed variable names from ansi style prototype * Add -I. to CPPFLAGS * Added new function t_agetstr which dynamically allocates area buffer. --- lib/libterm/Makefile | 4 +-- lib/libterm/shlib_version | 4 +-- lib/libterm/termcap.3 | 58 +++++++++++++++++++++++++++++++++++++-- lib/libterm/termcap.c | 51 ++++++++++++++++++++++++++++++++-- lib/libterm/termcap.h | 5 ++-- 5 files changed, 112 insertions(+), 10 deletions(-) diff --git a/lib/libterm/Makefile b/lib/libterm/Makefile index ca494c36d338..6a1f97675603 100644 --- a/lib/libterm/Makefile +++ b/lib/libterm/Makefile @@ -1,8 +1,8 @@ -# $NetBSD: Makefile,v 1.21 1999/08/15 10:59:01 blymn Exp $ +# $NetBSD: Makefile,v 1.22 2000/05/20 13:55:10 blymn Exp $ # @(#)Makefile 8.1 (Berkeley) 6/4/93 LIB= termcap -CPPFLAGS+=-DCM_N -DCM_GT -DCM_B -DCM_D +CPPFLAGS+=-I. -DCM_N -DCM_GT -DCM_B -DCM_D SRCS= termcap.c tgoto.c tputs.c MAN= termcap.3 diff --git a/lib/libterm/shlib_version b/lib/libterm/shlib_version index 2200f8f79cee..af7a6737d673 100644 --- a/lib/libterm/shlib_version +++ b/lib/libterm/shlib_version @@ -1,5 +1,5 @@ -# $NetBSD: shlib_version,v 1.5 2000/04/19 13:41:28 blymn Exp $ +# $NetBSD: shlib_version,v 1.6 2000/05/20 13:55:10 blymn Exp $ # Remember to update distrib/sets/lists/base/shl.* when changing # major=0 -minor=2 +minor=3 diff --git a/lib/libterm/termcap.3 b/lib/libterm/termcap.3 index 30e3302263f5..93e75466f991 100644 --- a/lib/libterm/termcap.3 +++ b/lib/libterm/termcap.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: termcap.3,v 1.18 2000/05/10 11:13:35 blymn Exp $ +.\" $NetBSD: termcap.3,v 1.19 2000/05/20 13:55:10 blymn Exp $ .\" .\" Copyright (c) 1980, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -73,6 +73,8 @@ .Fn t_getflag "struct tinfo *info" "char *id" .Ft char * .Fn t_getstr "struct tinfo *info" "char *id" "char **area" "size_t *limit" +.Ft char * +.Fn t_agetstr "struct tinfo *info" "char *id" "char **buffer" "char **area" .Ft int .Fn t_getterm "struct tinfo *info" "char **area" "size_t *limit" .Ft int @@ -293,7 +295,59 @@ When the information pointed to by is no longer required any storage associated with the object can be released by calling .Fn t_freent . -The functions +The function +.Fn t_agetstr +performs the same function as +.Fn t_getstr +except sufficient memory is first allocated to hold the desired string +argument before adding it to the buffer. To initialise the buffer for +.Fn t_agetstr +the first call must be done with the contents of +.Fa buffer +set to NULL. Subsequent calls to +.Fn t_agetstr +can pass +.Fa buffer +and +.Fa area +in unmodified and the requested string argument will be appended to +the buffer after sufficient memory has been allocated. Modifying either +.Fa buffer +or +.Fa area +between calls to +.Fn t_agetstr +will certainly lead to the function misbehaving. When the storage +allocated by +.Fn t_agetstr +is no longer required it can be freed by passing the pointer contained +in +.Fa buffer +to +.Fn free . +If an error occurs within +.Fn t_agetstr +a NULL will be returned and +.Fa buffer +and +.Fa area +will remain unmodified. +.Em NOTE +.Fn t_agetstr +uses +.Fn realloc +to reallocate the buffer memory so saving a copy of either +.Fa buffer +or +.Fa area +for use after subsequent calls to +.Fn t_agetstr +is likely to fail. It is best to keep offsets from +.Fa buffer +to the desired string and calculate pointer addresses only after all +the calls to +.Fn t_agetstr +have been done. The functions .Fn t_getnum and .Fn t_getflag diff --git a/lib/libterm/termcap.c b/lib/libterm/termcap.c index f3bc524e1b1f..495de5027930 100644 --- a/lib/libterm/termcap.c +++ b/lib/libterm/termcap.c @@ -1,4 +1,4 @@ -/* $NetBSD: termcap.c,v 1.29 2000/05/14 01:14:29 lukem Exp $ */ +/* $NetBSD: termcap.c,v 1.30 2000/05/20 13:55:10 blymn Exp $ */ /* * Copyright (c) 1980, 1993 @@ -38,7 +38,7 @@ #if 0 static char sccsid[] = "@(#)termcap.c 8.1 (Berkeley) 6/4/93"; #else -__RCSID("$NetBSD: termcap.c,v 1.29 2000/05/14 01:14:29 lukem Exp $"); +__RCSID("$NetBSD: termcap.c,v 1.30 2000/05/20 13:55:10 blymn Exp $"); #endif #endif /* not lint */ @@ -375,6 +375,53 @@ tgetstr(id, area) return t_getstr(fbuf, ids, area, NULL); } +/* + * Get a string valued option specified by id and append it to the + * given buffer. If bufptr is NULL then a new buffer is allocated, if + * bufptr is non-NULL then extend the memory allocation to allow the + * new string to be appended to the buffer. The pointer area is + * updated to point to the start of the new entry and this address is + * also returned to the caller. If the string is not found or a + * memory allocation fails then NULL is returned and bufptr and area + * are unchanged. + */ +char * +t_agetstr(struct tinfo *info, const char *id, char **bufptr, char **area) +{ + size_t new_size, offset; + char *new_buf; + + _DIAGASSERT(info != NULL); + _DIAGASSERT(id != NULL); + _DIAGASSERT(bufptr != NULL); + _DIAGASSERT(area != NULL); + _DIAGASSERT(size != NULL); + + t_getstr(info, id, NULL, &new_size); + + /* either the string is empty or the capability does not exist. */ + if (new_size == 0) + return NULL; + + /* check if we have a buffer, if not malloc one and fill it in. */ + if (*bufptr == NULL) { + if ((new_buf = (char *) malloc(new_size)) == NULL) + return NULL; + *bufptr = new_buf; + *area = new_buf; + } else { + offset = *area - *bufptr; + if ((new_buf = realloc(*bufptr, offset + new_size)) == NULL) + return NULL; + + *bufptr = new_buf; + *area = *bufptr + offset; /* we need to do this just in case + realloc shifted the buffer. */ + } + + return t_getstr(info, id, area, NULL); +} + /* * Free the buffer allocated by t_getent * diff --git a/lib/libterm/termcap.h b/lib/libterm/termcap.h index e44a3455877f..8a41dce9302d 100644 --- a/lib/libterm/termcap.h +++ b/lib/libterm/termcap.h @@ -1,4 +1,4 @@ -/* $NetBSD: termcap.h,v 1.10 2000/04/19 13:41:29 blymn Exp $ */ +/* $NetBSD: termcap.h,v 1.11 2000/05/20 13:55:11 blymn Exp $ */ /*- * Copyright (c) 1992, 1993 @@ -60,7 +60,8 @@ int t_getent __P((struct tinfo **, const char *)); int t_getnum __P((struct tinfo *, const char *)); int t_getflag __P((struct tinfo *, const char *)); char *t_getstr __P((struct tinfo *, const char *, char **, size_t *)); -int t_getterm(struct tinfo *info, char **area, size_t *limit); +char *t_agetstr(struct tinfo *, const char *, char **, char **); +int t_getterm(struct tinfo *, char **, size_t *); int t_goto __P((struct tinfo *, const char *, int, int, char *, size_t)); int t_puts __P((struct tinfo *, const char *, int, void (*)(char, void *), void *));