Module Name:    src
Committed By:   roy
Date:           Thu Mar  4 15:16:39 UTC 2010

Modified Files:
        src/lib/libterminfo: termcap.c

Log Message:
Convert padding and give terminfo some default assumptions about termcap.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/lib/libterminfo/termcap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libterminfo/termcap.c
diff -u src/lib/libterminfo/termcap.c:1.5 src/lib/libterminfo/termcap.c:1.6
--- src/lib/libterminfo/termcap.c:1.5	Tue Mar  2 14:11:11 2010
+++ src/lib/libterminfo/termcap.c	Thu Mar  4 15:16:39 2010
@@ -1,4 +1,4 @@
-/* $NetBSD: termcap.c,v 1.5 2010/03/02 14:11:11 roy Exp $ */
+/* $NetBSD: termcap.c,v 1.6 2010/03/04 15:16:39 roy Exp $ */
 
 /*
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: termcap.c,v 1.5 2010/03/02 14:11:11 roy Exp $");
+__RCSID("$NetBSD: termcap.c,v 1.6 2010/03/04 15:16:39 roy Exp $");
 
 #include <assert.h>
 #include <ctype.h>
@@ -229,6 +229,7 @@
 strval(const char *val)
 {
 	char *info, *ip, c;
+	const char *ps, *pe;
 	int p;
 	size_t len, l, n;
 
@@ -237,6 +238,22 @@
 	if (info == NULL)
 		return 0;
 
+	/* Move the = */
+	*ip++ = *val++;
+
+	/* Set ps and pe to point to the start and end of the padding */
+	if (isdigit((unsigned char)*val)) {
+		for (ps = pe = val;
+		     isdigit((unsigned char)*val) || *val == '.';
+		     val++)
+			pe++;
+		if (*val == '*') {
+			val++;
+			pe++;
+		}
+	} else
+		ps = pe  = NULL;
+
 	l = 0;
 	p = 1;
 	for (; *val != '\0'; val++) {
@@ -287,6 +304,19 @@
 		*ip++ = '\\';
 	}
 
+	/* Add our padding at the end. */
+	if (ps != NULL) {
+		n = pe - ps;
+		if (l + n + 4 > len)
+			goto elen;
+		*ip++ = '$';
+		*ip++ = '<';
+		strncpy(ip, ps, n);
+		ip += n;
+		*ip++ = '/';
+		*ip++ = '>';
+	}
+
 	*ip = '\0';
 	return info;
 
@@ -296,20 +326,38 @@
 	return NULL;
 }
 
+static struct def_info {
+	const char *name;
+	const char *cap;
+} def_infos[] = {
+	{ "bel",	"^G" },
+	{ "cr",		"^M" },
+	{ "cud1",	"^J" },
+	{ "ht",		"^I" },
+	{ "ind",	"^J" },
+	{ "kbs",	"^H" },
+	{ "kcub1",	"^H" },
+	{ "kcud1",	"^J" },
+	{ "nel",	"^M^J" }
+};
+
 char *
 captoinfo(char *cap)
 {
 	char *info, *ip, *token, *val, *p, tok[3];
 	const char *name;
 	size_t len, lp, nl, vl, rl;
+	int defs[__arraycount(def_infos)];
 
 	_DIAGASSERT(cap != NULL);
 
 	len = strlen(cap) * 2;
+	len += __arraycount(def_infos) * (5 + 4 + 3); /* reserve for defs */
 	info = ip = malloc(len);
 	if (info == NULL)
 		return NULL;
 
+	memset(defs, 0, sizeof(defs));
 	lp = 0;
 	tok[2] = '\0';
 	for (token = _ti_get_token(&cap, ':');
@@ -334,6 +382,16 @@
 				val = strval(token + 2);
 			}
 		}
+
+		/* See if this sets a default. */
+		for (nl = 0; nl < __arraycount(def_infos); nl++) {
+			if (strcmp(name, def_infos[nl].name) == 0) {
+				printf ("matched %s\n", name);
+				defs[nl] = 1;
+				break;
+			}
+		}
+
 		nl = strlen(name);
 		if (val == NULL)
 			vl = 0;
@@ -367,6 +425,19 @@
 		}
 	}
 
+	/* Add any defaults not set above. */
+	for (nl = 0; nl < __arraycount(def_infos); nl++) {
+		if (defs[nl] == 0) {
+			*ip++ = ',';
+			*ip++ = ' ';
+			strcpy(ip, def_infos[nl].name);
+			ip += strlen(def_infos[nl].name);
+			*ip++ = '=';
+			strcpy(ip, def_infos[nl].cap);
+			ip += strlen(def_infos[nl].cap);
+		}
+	}
+
 	*ip = '\0';
 	return info;
 }

Reply via email to