* Improve the handling of BC and UP in t_goto, t_getent now queries

these capabilities and stashes them in "struct tinfo" for t_goto to
  use.  This makes the t_goto call more efficient and plugs a memory
  leak that was present in the original t_goto implementation.
  Thanks to Itojun for spotting this one too!
This commit is contained in:
blymn 2000-06-03 07:14:55 +00:00
parent 16178745d9
commit 63b8f446f6
3 changed files with 46 additions and 31 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: termcap.c,v 1.35 2000/06/02 22:09:01 thorpej Exp $ */
/* $NetBSD: termcap.c,v 1.36 2000/06/03 07:14:55 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.35 2000/06/02 22:09:01 thorpej Exp $");
__RCSID("$NetBSD: termcap.c,v 1.36 2000/06/03 07:14:55 blymn Exp $");
#endif
#endif /* not lint */
@ -81,6 +81,9 @@ static struct tinfo *fbuf; /* untruncated termcap buffer */
int
t_setinfo(struct tinfo **bp, const char *entry)
{
char capability[256], *cap_ptr;
size_t limit;
if ((*bp = malloc(sizeof(struct tinfo))) == NULL)
return -1;
@ -88,6 +91,14 @@ t_setinfo(struct tinfo **bp, const char *entry)
return -1;
strcpy((*bp)->info, entry);
cap_ptr = capability;
limit = 255;
(*bp)->up = t_getstr(*bp, "up", &cap_ptr, &limit);
cap_ptr = capability;
limit = 255;
(*bp)->bc = t_getstr(*bp, "bc", &cap_ptr, &limit);
return 0;
}
@ -107,9 +118,12 @@ t_getent(bp, name)
char **fname;
char *home;
int i, did_getset;
size_t limit;
char pathbuf[PBUFSIZ]; /* holds raw path of filenames */
char *pathvec[PVECSIZ]; /* to point to names in pathbuf */
char *termpath;
char capability[256], *cap_ptr;
_DIAGASSERT(bp != NULL);
_DIAGASSERT(name != NULL);
@ -205,6 +219,20 @@ t_getent(bp, name)
/* no tc reference loop return code in libterm XXX */
if (i == -3)
return (-1);
/* fill in t_goto capabilities - this prevents memory leaks
* and is more efficient than fetching these capabilities
* every time t_goto is called.
*/
if (i >= 0) {
cap_ptr = capability;
limit = 255;
(*bp)->up = t_getstr(*bp, "up", &cap_ptr, &limit);
cap_ptr = capability;
limit = 255;
(*bp)->bc = t_getstr(*bp, "bc", &cap_ptr, &limit);
}
return (i + 1);
}
@ -454,6 +482,10 @@ t_freent(info)
{
_DIAGASSERT(info != NULL);
free(info->info);
if (info->up != NULL)
free(info->up);
if (info->bc != NULL)
free(info->bc);
free(info);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: termcap_private.h,v 1.1 2000/04/18 14:42:42 blymn Exp $ */
/* $NetBSD: termcap_private.h,v 1.2 2000/06/03 07:14:55 blymn Exp $ */
/*-
* Copyright (c) 1998-1999 Brett Lymn
@ -35,6 +35,8 @@
struct tinfo
{
char *info;
char *up; /* for use by tgoto */
char *bc; /* for use by tgoto */
};

View File

@ -1,4 +1,4 @@
/* $NetBSD: tgoto.c,v 1.16 2000/06/02 13:13:12 itojun Exp $ */
/* $NetBSD: tgoto.c,v 1.17 2000/06/03 07:14:55 blymn Exp $ */
/*
* Copyright (c) 1980, 1993
@ -38,21 +38,22 @@
#if 0
static char sccsid[] = "@(#)tgoto.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: tgoto.c,v 1.16 2000/06/02 13:13:12 itojun Exp $");
__RCSID("$NetBSD: tgoto.c,v 1.17 2000/06/03 07:14:55 blymn Exp $");
#endif
#endif /* not lint */
#include <errno.h>
#include <string.h>
#include <termcap.h>
#include <termcap_private.h>
#include <stdlib.h>
#define CTRL(c) ((c) & 037)
#define MAXRETURNSIZE 64
char *UP;
char *BC;
char *UP = NULL;
char *BC = NULL;
/*
* Routine to perform cursor addressing.
@ -109,10 +110,7 @@ t_goto(info, CM, destcol, destline, buffer, limit)
static char added[10];
const char *cp = CM;
char *dp = buffer;
char *old_up = UP, *old_bc = BC;
char new_up[MAXRETURNSIZE], new_bc[MAXRETURNSIZE], *up_ptr, *bc_ptr;
int c;
size_t count = MAXRETURNSIZE;
int oncol = 0;
int which = destline;
@ -120,24 +118,15 @@ t_goto(info, CM, destcol, destline, buffer, limit)
if (info != NULL)
{
up_ptr = new_up;
bc_ptr = new_bc;
UP = t_getstr(info, "up", &up_ptr, &count);
count = MAXRETURNSIZE;
BC = t_getstr(info, "bc", &bc_ptr, &count);
if (!UP)
UP = info->up;
if (!BC)
BC = info->bc;
}
if (cp == 0) {
errno = EINVAL;
toohard:
if (UP != old_up) {
free(UP);
UP = old_up;
}
if (BC != old_bc) {
free(BC);
BC = old_bc;
}
return -1;
}
added[0] = '\0';
@ -299,13 +288,5 @@ setwhich:
}
(void)strcpy(dp, added);
if (UP != old_up) {
free(UP);
UP = old_up;
}
if (BC != old_bc) {
free(BC);
BC = old_bc;
}
return 0;
}