tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: recent terminfo changes



In article <r5jf6e$37j0$1%ciao.gmane.io@localhost>,
Christos Zoulas <christos%astron.com@localhost> wrote:
>In article <20200324144303.E147610756%quasar.astron.com@localhost>,
>Christos Zoulas <christos%quasar.astron.com@localhost> wrote:

Finally here is a complete patch that deletes terminfo2.cdb.
This patch restores compatibility with the old terminfo format
for most entries except the handful ones that need the new entry
format. I did not do the name@v2 part described in the original
proposal because I got lazy, and this is compat code anyway.

I am planning to commit this in a few days if I don't hear otherwise.


Index: distrib/sets/lists/base/mi
===================================================================
RCS file: /cvsroot/src/distrib/sets/lists/base/mi,v
retrieving revision 1.1233
diff -u -u -r1.1233 mi
--- distrib/sets/lists/base/mi	22 Mar 2020 20:21:53 -0000	1.1233
+++ distrib/sets/lists/base/mi	27 Mar 2020 02:02:52 -0000
@@ -4841,7 +4841,8 @@
 ./usr/share/misc/more.help			base-obsolete		obsolete
 ./usr/share/misc/nslookup.help			base-obsolete		obsolete
 ./usr/share/misc/terminfo			base-terminfo-share	share
-./usr/share/misc/terminfo2.cdb			base-terminfo-share	share
+./usr/share/misc/terminfo.cdb			base-terminfo-share	share
+./usr/share/misc/terminfo2.cdb			base-obsolete		obsolete
 ./usr/share/misc/units.lib			base-reference-share	share
 ./usr/share/misc/usb_hid_usages			base-reference-share	share
 ./usr/share/misc/vgrindefs.db			base-groff-share	share
Index: share/terminfo/Makefile
===================================================================
RCS file: /cvsroot/src/share/terminfo/Makefile,v
retrieving revision 1.4
diff -u -u -r1.4 Makefile
--- share/terminfo/Makefile	13 Mar 2020 15:19:25 -0000	1.4
+++ share/terminfo/Makefile	27 Mar 2020 02:02:52 -0000
@@ -1,12 +1,12 @@
 #	$NetBSD: Makefile,v 1.4 2020/03/13 15:19:25 roy Exp $
 #	from: @(#)Makefile	8.1 (Berkeley) 6/8/93
 
-CLEANFILES= terminfo2.cdb
-realall: terminfo2.cdb
-FILES=terminfo2.cdb terminfo
+CLEANFILES= terminfo.cdb
+realall: terminfo.cdb
+FILES=terminfo.cdb terminfo
 FILESDIR=${BINDIR}/misc
 
-terminfo2.cdb: terminfo ${TOOL_TIC}
+terminfo.cdb: terminfo ${TOOL_TIC}
 	${_MKTARGET_CREATE}
 	${TOOL_TIC} -ax -o ${.TARGET} "${.CURDIR}/terminfo" 
 
Index: usr.bin/tic/tic.c
===================================================================
RCS file: /cvsroot/src/usr.bin/tic/tic.c,v
retrieving revision 1.32
diff -u -u -r1.32 tic.c
--- usr.bin/tic/tic.c	13 Mar 2020 15:19:25 -0000	1.32
+++ usr.bin/tic/tic.c	27 Mar 2020 02:02:52 -0000
@@ -217,7 +217,8 @@
 static void
 merge(TIC *rtic, TIC *utic, int flags)
 {
-	char *cap, flag, *code, type, *str;
+	char flag, *code, type, *str;
+	const char *cap;
 	short ind, len;
 	int num;
 	size_t n;
@@ -228,7 +229,7 @@
 		cap += sizeof(uint16_t);
 		flag = *cap++;
 		if (VALID_BOOLEAN(flag) &&
-		    _ti_find_cap(&rtic->flags, 'f', ind) == NULL)
+		    _ti_find_cap(rtic, &rtic->flags, 'f', ind) == NULL)
 		{
 			_ti_grow_tbuf(&rtic->flags, sizeof(uint16_t) + 1);
 			le16enc(rtic->flags.buf + rtic->flags.bufpos, ind);
@@ -242,17 +243,15 @@
 	for (n = utic->nums.entries; n > 0; n--) {
 		ind = le16dec(cap);
 		cap += sizeof(uint16_t);
-		num = le32dec(cap);
-		cap += sizeof(uint32_t);
+		num = _ti_decode_num(utic->rtype, &cap);
 		if (VALID_NUMERIC(num) &&
-		    _ti_find_cap(&rtic->nums, 'n', ind) == NULL)
+		    _ti_find_cap(rtic, &rtic->nums, 'n', ind) == NULL)
 		{
 			grow_tbuf(&rtic->nums, sizeof(uint16_t) +
-			    sizeof(uint32_t));
+			    _ti_numsize(rtic));
 			le16enc(rtic->nums.buf + rtic->nums.bufpos, ind);
 			rtic->nums.bufpos += sizeof(uint16_t);
-			le32enc(rtic->nums.buf + rtic->nums.bufpos, num);
-			rtic->nums.bufpos += sizeof(uint32_t);
+			_ti_encode_num(rtic, &rtic->nums, num);
 			rtic->nums.entries++;
 		}
 	}
@@ -264,7 +263,7 @@
 		len = le16dec(cap);
 		cap += sizeof(uint16_t);
 		if (len > 0 &&
-		    _ti_find_cap(&rtic->strs, 's', ind) == NULL)
+		    _ti_find_cap(rtic, &rtic->strs, 's', ind) == NULL)
 		{
 			grow_tbuf(&rtic->strs, (sizeof(uint16_t) * 2) + len);
 			le16enc(rtic->strs.buf + rtic->strs.bufpos, ind);
@@ -295,8 +294,7 @@
 				continue;
 			break;
 		case 'n':
-			num = le32dec(cap);
-			cap += sizeof(uint32_t);
+			num = _ti_decode_num(utic->rtype, &cap);
 			if (!VALID_NUMERIC(num))
 				continue;
 			break;
@@ -328,7 +326,8 @@
 		if (term->base_term != NULL)
 			continue;
 		rtic = term->tic;
-		while ((cap = _ti_find_extra(&rtic->extras, "use")) != NULL) {
+		while ((cap = _ti_find_extra(rtic, &rtic->extras, "use"))
+		    != NULL) {
 			if (*cap++ != 's') {
 				dowarn("%s: use is not string", rtic->name);
 				break;
@@ -351,15 +350,16 @@
 				dowarn("%s: uses itself", rtic->name);
 				goto remove;
 			}
-			if (_ti_find_extra(&utic->extras, "use") != NULL) {
+			if (_ti_find_extra(utic, &utic->extras, "use")
+			    != NULL) {
 				skipped++;
 				break;
 			}
-			cap = _ti_find_extra(&rtic->extras, "use");
+			cap = _ti_find_extra(rtic, &rtic->extras, "use");
 			merge(rtic, utic, flags);
 	remove:
 			/* The pointers may have changed, find the use again */
-			cap = _ti_find_extra(&rtic->extras, "use");
+			cap = _ti_find_extra(rtic, &rtic->extras, "use");
 			if (cap == NULL)
 				dowarn("%s: use no longer exists - impossible",
 					rtic->name);
Index: lib/libterminfo/compile.c
===================================================================
RCS file: /cvsroot/src/lib/libterminfo/compile.c,v
retrieving revision 1.14
diff -u -u -r1.14 compile.c
--- lib/libterminfo/compile.c	13 Mar 2020 15:19:25 -0000	1.14
+++ lib/libterminfo/compile.c	27 Mar 2020 02:02:52 -0000
@@ -87,7 +87,7 @@
 }
 
 char *
-_ti_find_cap(TBUF *tbuf, char type, short ind)
+_ti_find_cap(TIC *tic, TBUF *tbuf, char type, short ind)
 {
 	size_t n;
 	uint16_t num;
@@ -106,7 +106,7 @@
 			cap++;
 			break;
 		case 'n':
-			cap += sizeof(uint32_t);
+			cap += _ti_numsize(tic);
 			break;
 		case 's':
 			num = le16dec(cap);
@@ -121,7 +121,7 @@
 }
 
 char *
-_ti_find_extra(TBUF *tbuf, const char *code)
+_ti_find_extra(TIC *tic, TBUF *tbuf, const char *code)
 {
 	size_t n;
 	uint16_t num;
@@ -142,7 +142,7 @@
 			cap++;
 			break;
 		case 'n':
-			cap += sizeof(uint32_t);
+			cap += _ti_numsize(tic);
 			break;
 		case 's':
 			num = le16dec(cap);
@@ -156,6 +156,32 @@
 	return NULL;
 }
 
+void
+_ti_encode_num(TIC *tic, TBUF *rbuf, int num)
+{
+	if (_ti_numsize(tic) == sizeof(uint16_t)) {
+		le16enc(rbuf->buf + rbuf->bufpos, (uint16_t)num);
+	} else {
+		le32enc(rbuf->buf + rbuf->bufpos, (uint32_t)num);
+	}
+	rbuf->bufpos += _ti_numsize(tic);
+}
+
+int
+_ti_decode_num(int rtype, const char **cap)
+{
+	int rv;
+
+	if (rtype == TERMINFO_RTYPE_O1) {
+		rv = (int)le16dec(*cap);
+		*cap += sizeof(uint16_t);
+	} else {
+		rv = (int)le32dec(*cap);
+		*cap += sizeof(uint32_t);
+	}
+	return rv;
+}
+
 size_t
 _ti_store_extra(TIC *tic, int wrn, char *id, char type, char flag, int num,
     char *str, size_t strl, int flags)
@@ -165,7 +191,7 @@
 	_DIAGASSERT(tic != NULL);
 
 	if (strcmp(id, "use") != 0) {
-		if (_ti_find_extra(&tic->extras, id) != NULL)
+		if (_ti_find_extra(tic, &tic->extras, id) != NULL)
 			return 0;
 		if (!(flags & TIC_EXTRA)) {
 			if (wrn != 0)
@@ -182,7 +208,7 @@
 	}
 
 	if (!_ti_grow_tbuf(&tic->extras,
-		l + strl + sizeof(uint16_t) + sizeof(uint32_t) + 1))
+		l + strl + sizeof(uint16_t) + _ti_numsize(tic) + 1))
 		return 0;
 	le16enc(tic->extras.buf + tic->extras.bufpos, (uint16_t)l);
 	tic->extras.bufpos += sizeof(uint16_t);
@@ -194,8 +220,7 @@
 		tic->extras.buf[tic->extras.bufpos++] = flag;
 		break;
 	case 'n':
-		le32enc(tic->extras.buf + tic->extras.bufpos, (uint32_t)num);
-		tic->extras.bufpos += sizeof(uint32_t);
+		_ti_encode_num(tic, &tic->extras, num);
 		break;
 	case 's':
 		le16enc(tic->extras.buf + tic->extras.bufpos, (uint16_t)strl);
@@ -239,7 +264,7 @@
 		return -1;
 
 	cap = *buf;
-	*cap++ = TERMINFO_RTYPE; /* Record type 3 */
+	*cap++ = tic->rtype;
 	le16enc(cap, (uint16_t)len);
 	cap += sizeof(uint16_t);
 	memcpy(cap, tic->name, len);
@@ -451,6 +476,17 @@
 	return token;
 }
 
+static int
+_ti_find_rtype(const char *cap)
+{
+	for (const char *ptr = cap; (ptr = strchr(ptr, '#')) != NULL;) {
+		if (strtol(++ptr, NULL, 0) > 0xffff) {
+			return TERMINFO_RTYPE;
+		}
+	}
+	return TERMINFO_RTYPE_O1;
+}
+
 TIC *
 _ti_compile(char *cap, int flags)
 {
@@ -467,7 +503,7 @@
 
 	name = _ti_get_token(&cap, ',');
 	if (name == NULL) {
-		dowarn(flags, "no seperator found: %s", cap);
+		dowarn(flags, "no separator found: %s", cap);
 		return NULL;
 	}
 	desc = strrchr(name, '|');
@@ -494,6 +530,7 @@
 	if (tic == NULL)
 		return NULL;
 
+	tic->rtype = _ti_find_rtype(cap);
 	buf.buf = NULL;
 	buf.buflen = 0;
 
@@ -533,7 +570,7 @@
 			/* Don't use the string if we already have it */
 			ind = (short)_ti_strindex(token);
 			if (ind != -1 &&
-			    _ti_find_cap(&tic->strs, 's', ind) != NULL)
+			    _ti_find_cap(tic, &tic->strs, 's', ind) != NULL)
 				continue;
 
 			/* Encode the string to our scratch buffer */
@@ -579,7 +616,7 @@
 			/* Don't use the number if we already have it */
 			ind = (short)_ti_numindex(token);
 			if (ind != -1 &&
-			    _ti_find_cap(&tic->nums, 'n', ind) != NULL)
+			    _ti_find_cap(tic, &tic->nums, 'n', ind) != NULL)
 				continue;
 
 			cnum = strtol(p, &e, 0);
@@ -600,14 +637,12 @@
 				    num, NULL, 0, flags);
 			else {
 				if (_ti_grow_tbuf(&tic->nums,
-				    sizeof(uint16_t) + sizeof(uint32_t))==NULL)
+				    sizeof(uint16_t) + _ti_numsize(tic))==NULL)
 					goto error;
 				le16enc(tic->nums.buf + tic->nums.bufpos,
 				    (uint16_t)ind);
 				tic->nums.bufpos += sizeof(uint16_t);
-				le32enc(tic->nums.buf + tic->nums.bufpos,
-				    (uint32_t)num);
-				tic->nums.bufpos += sizeof(uint32_t);
+				_ti_encode_num(tic, &tic->nums, num);
 				tic->nums.entries++;
 			}
 			continue;
@@ -622,7 +657,8 @@
 		ind = (short)_ti_flagindex(token);
 		if (ind == -1 && flag == CANCELLED_BOOLEAN) {
 			if ((ind = (short)_ti_numindex(token)) != -1) {
-				if (_ti_find_cap(&tic->nums, 'n', ind) != NULL)
+				if (_ti_find_cap(tic, &tic->nums, 'n', ind)
+				    != NULL)
 					continue;
 				if (_ti_grow_tbuf(&tic->nums,
 					sizeof(uint16_t) * 2) == NULL)
@@ -630,13 +666,13 @@
 				le16enc(tic->nums.buf + tic->nums.bufpos,
 				    (uint16_t)ind);
 				tic->nums.bufpos += sizeof(uint16_t);
-				le32enc(tic->nums.buf + tic->nums.bufpos,
-				    (uint32_t)CANCELLED_NUMERIC);
-				tic->nums.bufpos += sizeof(uint32_t);
+				_ti_encode_num(tic, &tic->nums,
+				    CANCELLED_NUMERIC); 
 				tic->nums.entries++;
 				continue;
 			} else if ((ind = (short)_ti_strindex(token)) != -1) {
-				if (_ti_find_cap(&tic->strs, 's', ind) != NULL)
+				if (_ti_find_cap(tic, &tic->strs, 's', ind)
+				    != NULL)
 					continue;
 				if (_ti_grow_tbuf(&tic->strs,
 				    (sizeof(uint16_t) * 2) + 1) == NULL)
@@ -652,7 +688,7 @@
 		if (ind == -1)
 			_ti_store_extra(tic, 1, token, 'f', flag, 0, NULL, 0,
 			    flags);
-		else if (_ti_find_cap(&tic->flags, 'f', ind) == NULL) {
+		else if (_ti_find_cap(tic, &tic->flags, 'f', ind) == NULL) {
 			if (_ti_grow_tbuf(&tic->flags, sizeof(uint16_t) + 1)
 			    == NULL)
 				goto error;
Index: lib/libterminfo/term.c
===================================================================
RCS file: /cvsroot/src/lib/libterminfo/term.c,v
retrieving revision 1.30
diff -u -u -r1.30 term.c
--- lib/libterminfo/term.c	13 Mar 2020 15:19:25 -0000	1.30
+++ lib/libterminfo/term.c	27 Mar 2020 02:02:52 -0000
@@ -48,7 +48,7 @@
  * Although we can read v1 structure (which includes v2 alias records)
  * we really want a v3 structure to get numerics of type int rather than short.
  */
-#define _PATH_TERMINFO	"/usr/share/misc/terminfo2:/usr/share/misc/terminfo"
+#define _PATH_TERMINFO	"/usr/share/misc/terminfo"
 
 static char __ti_database[PATH_MAX];
 const char *_ti_database;
@@ -150,13 +150,7 @@
 		for (; num != 0; num--) {
 			ind = le16dec(cap);
 			cap += sizeof(uint16_t);
-			if (rtype == TERMINFO_RTYPE_O1) {
-				term->nums[ind] = (int)le16dec(cap);
-				cap += sizeof(uint16_t);
-			} else {
-				term->nums[ind] = (int)le32dec(cap);
-				cap += sizeof(uint32_t);
-			}
+			term->nums[ind] = _ti_decode_num(rtype, &cap);
 			if (flags == 0 && !VALID_NUMERIC(term->nums[ind]))
 				term->nums[ind] = ABSENT_NUMERIC;
 		}
@@ -213,13 +207,7 @@
 				break;
 			case 'n':
 				ud->flag = ABSENT_BOOLEAN;
-				if (rtype == TERMINFO_RTYPE_O1) {
-					ud->num = (int)le16dec(cap);
-					cap += sizeof(uint16_t);
-				} else {
-					ud->num = (int)le32dec(cap);
-					cap += sizeof(uint32_t);
-				}
+				ud->num = _ti_decode_num(rtype, &cap);
 				if (flags == 0 &&
 				    !VALID_NUMERIC(ud->num))
 					ud->num = ABSENT_NUMERIC;
Index: lib/libterminfo/term_private.h
===================================================================
RCS file: /cvsroot/src/lib/libterminfo/term_private.h,v
retrieving revision 1.12
diff -u -u -r1.12 term_private.h
--- lib/libterminfo/term_private.h	13 Mar 2020 15:19:25 -0000	1.12
+++ lib/libterminfo/term_private.h	27 Mar 2020 02:02:52 -0000
@@ -154,18 +154,24 @@
 	char *name;
 	char *alias;
 	char *desc;
+	int  rtype;
 	TBUF flags;
 	TBUF nums;
 	TBUF strs;
 	TBUF extras;
 } TIC;
 
+#define _ti_numsize(tic) \
+    ((tic)->rtype == TERMINFO_RTYPE_O1 ? sizeof(uint16_t) : sizeof(uint32_t))
+
 char *_ti_grow_tbuf(TBUF *, size_t);
 char *_ti_get_token(char **, char);
-char *_ti_find_cap(TBUF *, char,  short);
-char *_ti_find_extra(TBUF *, const char *);
+char *_ti_find_cap(TIC *, TBUF *, char,  short);
+char *_ti_find_extra(TIC *, TBUF *, const char *);
 size_t _ti_store_extra(TIC *, int, char *, char, char, int,
     char *, size_t, int);
+void _ti_encode_num(TIC *, TBUF *, int );
+int _ti_decode_num(int, const char **);
 TIC *_ti_compile(char *, int);
 ssize_t _ti_flatten(uint8_t **, const TIC *);
 void _ti_freetic(TIC *);



Home | Main Index | Thread Index | Old Index