changeset 926:6a0aa8d36d06

rvinterf backslash escape: introduce libprint The new helper function library named libprint is meant to replace the badly misnamed libg23, and will soon contain functions for printing all of the same kinds of GPF TST packets that are now handled in libg23. However, we are also moving safe_print_trace() from libasync to this new library, and changing it to emit our new backslash escape format.
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 23 May 2023 03:47:46 +0000
parents 85d144f9fe56
children 4e243402f453
files rvinterf/Makefile rvinterf/asyncshell/Makefile rvinterf/libasync/rvtrace.c rvinterf/libprint/Makefile rvinterf/libprint/back_esc.c rvinterf/tmsh/Makefile
diffstat 6 files changed, 68 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/rvinterf/Makefile	Tue May 23 03:21:02 2023 +0000
+++ b/rvinterf/Makefile	Tue May 23 03:47:46 2023 +0000
@@ -1,7 +1,7 @@
 CC=	gcc
 CFLAGS=	-O2
 PROGDIR=asyncshell ctracedec etmsync l1filter lowlevel rvtat tmsh
-LIBDIR=	libasync libg23 libinterf
+LIBDIR=	libasync libg23 libinterf libprint
 SUBDIR=	${PROGDIR} ${LIBDIR}
 
 INSTALL_PREFIX=	/opt/freecalypso
@@ -9,12 +9,12 @@
 
 all:	${SUBDIR}
 
-asyncshell:	libasync libg23 libinterf
+asyncshell:	libasync libg23 libinterf libprint
 etmsync:	libinterf
 l1filter:	libinterf
-lowlevel:	libg23
+lowlevel:	libg23 libprint
 rvtat:		libinterf
-tmsh:		libasync libinterf
+tmsh:		libasync libinterf libprint
 
 ${SUBDIR}: FRC
 	cd $@; ${MAKE} ${MFLAGS} CC=${CC} CFLAGS="${CFLAGS}"
--- a/rvinterf/asyncshell/Makefile	Tue May 23 03:21:02 2023 +0000
+++ b/rvinterf/asyncshell/Makefile	Tue May 23 03:47:46 2023 +0000
@@ -5,7 +5,8 @@
 OBJS=	at.o battery.o help.o init.o keypress.o main.o oneshot.o parse.o \
 	pktsort.o poweroff.o rxctl.o sendarb.o sendsp.o tchcmd.o tchplay.o \
 	tchrec.o usercmd.o
-LIBS=	../libasync/libasync.a ../libg23/libg23.a ../libinterf/libinterf.a
+LIBS=	../libasync/libasync.a ../libg23/libg23.a ../libprint/libprint.a \
+	../libinterf/libinterf.a
 
 INSTALL_PREFIX=	/opt/freecalypso
 
--- a/rvinterf/libasync/rvtrace.c	Tue May 23 03:21:02 2023 +0000
+++ b/rvinterf/libasync/rvtrace.c	Tue May 23 03:47:46 2023 +0000
@@ -15,34 +15,6 @@
 extern int rvi_msg_len;
 
 void
-safe_print_trace(src, srclen, dest)
-	u_char *src;
-	char *dest;
-{
-	int i, c;
-	char *dp;
-
-	dp = dest;
-	for (i = 0; i < srclen; i++) {
-		c = src[i];
-		if (c & 0x80) {
-			*dp++ = 'M';
-			*dp++ = '-';
-			c &= 0x7F;
-		}
-		if (c < 0x20) {
-			*dp++ = '^';
-			*dp++ = c + '@';
-		} else if (c == 0x7F) {
-			*dp++ = '^';
-			*dp++ = '?';
-		} else
-			*dp++ = c;
-	}
-	*dp = '\0';
-}
-
-void
 handle_useid_0()
 {
 	char buf[MAX_PKT_FROM_TARGET*4];
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/libprint/Makefile	Tue May 23 03:47:46 2023 +0000
@@ -0,0 +1,13 @@
+CC=	gcc
+CFLAGS=	-O2
+OBJS=	back_esc.o
+LIB=	libprint.a
+
+all:	${LIB}
+
+${LIB}:	${OBJS}
+	ar rcu $@ ${OBJS}
+	ranlib $@
+
+clean:
+	rm -f *.[oa] errs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/libprint/back_esc.c	Tue May 23 03:47:46 2023 +0000
@@ -0,0 +1,48 @@
+/*
+ * The function implemented in this module prints a potentially-dirty string
+ * into a buffer with C-style backslash escapes, including doubling of any
+ * already-present backslashes.  The output buffer must be sized for up to
+ * 4x expansion, plus the terminating NUL.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdio.h>
+
+void
+safe_print_trace(src, srclen, dest)
+	u_char *src;
+	char *dest;
+{
+	int i, c;
+	char *dp;
+
+	dp = dest;
+	for (i = 0; i < srclen; i++) {
+		c = src[i];
+		switch (c) {
+		case '\\':
+			*dp++ = '\\';
+			*dp++ = '\\';
+			continue;
+		case '\r':
+			*dp++ = '\\';
+			*dp++ = 'r';
+			continue;
+		case '\n':
+			*dp++ = '\\';
+			*dp++ = 'n';
+			continue;
+		}
+		if (c >= ' ' && c <= '~')
+			*dp++ = c;
+		else if (c <= 7 && (i+1 == srclen || !isdigit(src[i+1]))) {
+			sprintf(dp, "\\%d", c);
+			dp += 2;
+		} else {
+			sprintf(dp, "\\x%02X", c);
+			dp += 4;
+		}
+	}
+	*dp = '\0';
+}
--- a/rvinterf/tmsh/Makefile	Tue May 23 03:21:02 2023 +0000
+++ b/rvinterf/tmsh/Makefile	Tue May 23 03:47:46 2023 +0000
@@ -5,7 +5,7 @@
 OBJS=	abb.o abbtm3.o audiocmd.o audioresp.o bsimcmd.o bsimresp.o etmbasic.o \
 	ffs2.o ffs2resp.o init.o l1cmd.o l1resp.o main.o misc.o omr.o omw.o \
 	oneshot.o pktsort.o rftablechk.o saverftab.o tmcore.o usercmd.o
-LIBS=	../libasync/libasync.a ../libinterf/libinterf.a \
+LIBS=	../libasync/libasync.a ../libprint/libprint.a ../libinterf/libinterf.a \
 	../../librftab/librftab.a
 
 INSTALL_PREFIX=	/opt/freecalypso