From 9e387da60551b2716a6a9d272562c823c016e59b Mon Sep 17 00:00:00 2001 From: roy Date: Sun, 5 Apr 2020 12:31:02 +0000 Subject: [PATCH] terminfo: Add guards to optionally build parts of libterminfo Reading from a database is now optional. Compiling terminfo descriptions (including from $TERMINFO) is now optional. Compat support is now optional. This removes 17k on amd64 from the binary size, which allows it to be used again on space constrained ramdisks. --- lib/libterminfo/Makefile | 14 ++++++++++++-- lib/libterminfo/compile.c | 14 ++++++++++++-- lib/libterminfo/term.c | 25 +++++++++++++++++++------ 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/lib/libterminfo/Makefile b/lib/libterminfo/Makefile index d80faf3ab1a8..556703d2da16 100644 --- a/lib/libterminfo/Makefile +++ b/lib/libterminfo/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.22 2012/03/21 05:37:44 matt Exp $ +# $NetBSD: Makefile,v 1.23 2020/04/05 12:31:02 roy Exp $ .include @@ -10,10 +10,20 @@ WARNS?= 5 CPPFLAGS+= -I${.CURDIR} SRCS= term.c ti.c setupterm.c curterm.c tparm.c tputs.c -SRCS+= compile.c hash.c +SRCS+= hash.c INCS= term.h INCSDIR= /usr/include +# For ramdisks there is no database to read from so remove compat +# and the need to read from them. +# While here, remove the ability to compile terminfo descriptions +# from $TERMINFO as well. +# This means the library requires any terminal needed built into it. +.if !defined(SMALLPROG) +CPPFLAGS+= -DTERMINFO_COMPILE -DTERMINFO_DB -DTERMINFO_COMPAT +SRCS+= compile.c +.endif + COPTS.tparm.c = -Wno-format-nonliteral MAN= terminfo.3 terminfo.5 diff --git a/lib/libterminfo/compile.c b/lib/libterminfo/compile.c index fbc8b6c54807..b991eb01b30f 100644 --- a/lib/libterminfo/compile.c +++ b/lib/libterminfo/compile.c @@ -1,4 +1,4 @@ -/* $NetBSD: compile.c,v 1.24 2020/03/30 02:08:11 roy Exp $ */ +/* $NetBSD: compile.c,v 1.25 2020/04/05 12:31:02 roy Exp $ */ /* * Copyright (c) 2009, 2010, 2011, 2020 The NetBSD Foundation, Inc. @@ -32,7 +32,7 @@ #endif #include -__RCSID("$NetBSD: compile.c,v 1.24 2020/03/30 02:08:11 roy Exp $"); +__RCSID("$NetBSD: compile.c,v 1.25 2020/04/05 12:31:02 roy Exp $"); #if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H #include @@ -64,6 +64,7 @@ dowarn(int flags, const char *fmt, ...) } } +#ifdef TERMINFO_COMPAT int _ti_promote(TIC *tic) { @@ -165,6 +166,7 @@ _ti_promote(TIC *tic) return error; } +#endif char * _ti_grow_tbuf(TBUF *tbuf, size_t len) @@ -257,6 +259,7 @@ _ti_find_extra(TIC *tic, TBUF *tbuf, const char *code) char * _ti_getname(int rtype, const char *orig) { +#ifdef TERMINFO_COMPAT const char *delim; char *name; const char *verstr; @@ -286,6 +289,9 @@ _ti_getname(int rtype, const char *orig) memcpy(name, orig, diff); memcpy(name + diff, verstr, vlen + 1); return name; +#else + return strdup(orig); +#endif } size_t @@ -626,7 +632,11 @@ _ti_compile(char *cap, int flags) if (tic == NULL) return NULL; +#ifdef TERMINFO_COMPAT tic->rtype = TERMINFO_RTYPE_O1; /* will promote if needed */ +#else + tic->rtype = TERMINFO_RTYPE; +#endif buf.buf = NULL; buf.buflen = 0; diff --git a/lib/libterminfo/term.c b/lib/libterminfo/term.c index 01aa6fc459d1..251d3d30b7d9 100644 --- a/lib/libterminfo/term.c +++ b/lib/libterminfo/term.c @@ -1,4 +1,4 @@ -/* $NetBSD: term.c,v 1.32 2020/03/27 17:39:53 christos Exp $ */ +/* $NetBSD: term.c,v 1.33 2020/04/05 12:31:02 roy Exp $ */ /* * Copyright (c) 2009, 2010, 2011, 2020 The NetBSD Foundation, Inc. @@ -28,7 +28,7 @@ */ #include -__RCSID("$NetBSD: term.c,v 1.32 2020/03/27 17:39:53 christos Exp $"); +__RCSID("$NetBSD: term.c,v 1.33 2020/04/05 12:31:02 roy Exp $"); #include @@ -50,7 +50,9 @@ __RCSID("$NetBSD: term.c,v 1.32 2020/03/27 17:39:53 christos Exp $"); */ #define _PATH_TERMINFO "/usr/share/misc/terminfo" +#ifdef TERMINFO_DB static char __ti_database[PATH_MAX]; +#endif const char *_ti_database; /* Include a generated list of pre-compiled terminfo descriptions. */ @@ -227,6 +229,7 @@ out: return -1; } +#if defined(TERMINFO_DB) || defined(TERMINFO_COMPILE) static int _ti_checkname(const char *name, const char *termname, const char *termalias) { @@ -259,7 +262,9 @@ _ti_checkname(const char *name, const char *termname, const char *termalias) /* No match. */ return 0; } +#endif +#ifdef TERMINFO_DB static int _ti_dbgetterm(TERMINAL *term, const char *path, const char *name, int flags) { @@ -339,6 +344,7 @@ _ti_dbgettermp(TERMINAL *term, const char *path, const char *name, int flags) } while (*path++ == ':'); return e; } +#endif static int _ti_findterm(TERMINAL *term, const char *name, int flags) @@ -352,12 +358,14 @@ _ti_findterm(TERMINAL *term, const char *name, int flags) _ti_database = NULL; r = 0; - if ((e = getenv("TERMINFO")) != NULL && *e != '\0') { - if (e[0] == '/') - return _ti_dbgetterm(term, e, name, flags); - } + e = getenv("TERMINFO"); +#ifdef TERMINFO_DB + if (e != NULL && *e == '/') + return _ti_dbgetterm(term, e, name, flags); +#endif c = NULL; +#ifdef TERMINFO_COMPILE if (e == NULL && (c = getenv("TERMCAP")) != NULL) { if (*c != '\0' && *c != '/') { c = strdup(c); @@ -402,7 +410,9 @@ _ti_findterm(TERMINAL *term, const char *name, int flags) return r; } } +#endif +#ifdef TERMINFO_DB if ((e = getenv("TERMINFO_DIRS")) != NULL) return _ti_dbgettermp(term, e, name, flags); @@ -414,6 +424,7 @@ _ti_findterm(TERMINAL *term, const char *name, int flags) } if (r != 1) r = _ti_dbgettermp(term, _PATH_TERMINFO, name, flags); +#endif return r; } @@ -424,6 +435,7 @@ _ti_getterm(TERMINAL *term, const char *name, int flags) int r; size_t i; const struct compiled_term *t; +#ifdef TERMINFO_COMPAT char *namev3; namev3 = _ti_getname(TERMINFO_RTYPE, name); @@ -433,6 +445,7 @@ _ti_getterm(TERMINAL *term, const char *name, int flags) if (r == 1) return r; } +#endif r = _ti_findterm(term, name, flags); if (r == 1)