changeset 336:922efdd65dce

g23sh written, compiles
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 22 Apr 2014 07:21:04 +0000
parents 40b8557b9d04
children a26470040d89
files .hgignore rvinterf/Makefile rvinterf/g23sh/Makefile rvinterf/g23sh/init.c rvinterf/g23sh/main.c rvinterf/g23sh/pktsort.c rvinterf/g23sh/sendsp.c rvinterf/g23sh/usercmd.c
diffstat 8 files changed, 344 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Tue Apr 22 06:51:09 2014 +0000
+++ b/.hgignore	Tue Apr 22 07:21:04 2014 +0000
@@ -23,6 +23,7 @@
 ^miscutil/imei-luhn$
 
 ^rvinterf/etmsync/fc-fsio$
+^rvinterf/g23sh/g23sh$
 ^rvinterf/lowlevel/rvinterf$
 ^rvinterf/lowlevel/rvtdump$
 ^rvinterf/misc/fc-sendsp$
--- a/rvinterf/Makefile	Tue Apr 22 06:51:09 2014 +0000
+++ b/rvinterf/Makefile	Tue Apr 22 07:21:04 2014 +0000
@@ -1,9 +1,10 @@
-PROGDIR=etmsync lowlevel misc tmsh
+PROGDIR=etmsync g23sh lowlevel misc tmsh
 LIBDIR=	libasync libg23
 SUBDIR=	${PROGDIR} ${LIBDIR}
 
 all:	${SUBDIR}
 
+g23sh:		libasync libg23
 lowlevel:	libg23
 tmsh:		libasync
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/g23sh/Makefile	Tue Apr 22 07:21:04 2014 +0000
@@ -0,0 +1,18 @@
+CC=	gcc
+CFLAGS=	-O2 -I../include
+PROG=	g23sh
+OBJS=	init.o main.o pktsort.o sendsp.o usercmd.o
+LIBS=	../libasync/libasync.a ../libg23/libg23.a
+INSTBIN=/usr/local/bin
+
+all:	${PROG}
+
+${PROG}: ${OBJS} ${LIBS}
+	${CC} ${CFLAGS} -o $@ ${OBJS} ${LIBS}
+
+install:	${PROG}
+	mkdir -p ${INSTBIN}
+	install -c ${PROG} ${INSTBIN}
+
+clean:
+	rm -f *.o *.out *errs ${PROG}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/g23sh/init.c	Tue Apr 22 07:21:04 2014 +0000
@@ -0,0 +1,27 @@
+/*
+ * This module contains the initialization code for g23sh.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include "pktmux.h"
+#include "localsock.h"
+
+extern int sock;
+
+init()
+{
+	static u_char want_rvt_lost[9] = {CLI2RVI_WANT_RVTRACE,
+					  0xFF, 0xFF, 0xFF, 0xFF,
+					  0x00, 0x00, 0x00, 0x00};
+	static u_char want_l23_mux[2] = {CLI2RVI_WANT_MUXPROTO, RVT_L23_HEADER};
+
+	if (!sock)
+		connect_local_socket();
+	localsock_prep_for_length_rx();
+	send_init_command(want_rvt_lost, 9);
+	send_init_command(want_l23_mux, 2);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/g23sh/main.c	Tue Apr 22 07:21:04 2014 +0000
@@ -0,0 +1,86 @@
+/*
+ * This module contains the main() function for g23sh.
+ */
+
+#include <sys/types.h>
+#include <sys/errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+char *socket_pathname = "/tmp/rvinterf_socket";
+int ttyhacks, dflag;
+
+int sock;
+
+extern char *rvinterf_Bopt, *rvinterf_lopt, *rvinterf_wopt;
+
+main(argc, argv)
+	char **argv;
+{
+	extern int optind;
+	extern char *optarg;
+	int c;
+	fd_set fds;
+
+	while ((c = getopt(argc, argv, "B:dl:s:w:")) != EOF)
+		switch (c) {
+		case 'B':
+			rvinterf_Bopt = optarg;
+			continue;
+		case 'd':
+			dflag++;
+			continue;
+		case 'l':
+			rvinterf_lopt = optarg;
+			continue;
+		case 's':
+			socket_pathname = optarg;
+			continue;
+		case 'w':
+			rvinterf_wopt = optarg;
+			continue;
+		case '?':
+		default:
+usage:			fprintf(stderr,
+				"usage: %s [options] [ttyport]\n", argv[0]);
+			exit(1);
+		}
+	switch (argc - optind) {
+	case 0:
+		if (rvinterf_Bopt || rvinterf_lopt || rvinterf_wopt) {
+			fprintf(stderr,
+      "%s: -B, -l and -w options are meaningful only when launching rvinterf\n",
+				argv[0]);
+			exit(1);
+		}
+		break;
+	case 1:
+		launch_rvinterf(argv[optind]);
+		break;
+	default:
+		goto usage;
+	}
+
+	ttyhacks = isatty(0) && !dflag;
+	init();
+	tty_init();
+	for (;;) {
+		FD_ZERO(&fds);
+		FD_SET(0, &fds);
+		FD_SET(sock, &fds);
+		c = select(sock+1, &fds, 0, 0, 0);
+		if (c < 0) {
+			if (errno == EINTR)
+				continue;
+			tty_cleanup();
+			perror("select");
+			exit(1);
+		}
+		if (FD_ISSET(0, &fds))
+			handle_tty_input();
+		if (FD_ISSET(sock, &fds))
+			handle_rvinterf_input();
+		fflush(stdout);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/g23sh/pktsort.c	Tue Apr 22 07:21:04 2014 +0000
@@ -0,0 +1,67 @@
+/*
+ * Here we sort out incoming packets from the target relayed via rvinterf.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include "pktmux.h"
+#include "limits.h"
+#include "localsock.h"
+#include "localtypes.h"
+
+extern u_char rvi_msg[];
+extern int rvi_msg_len;
+
+static void
+process_rvt()
+{
+	u32 useid;
+
+	if (rvi_msg_len < 7) {
+		tty_cleanup();
+		fprintf(stderr, "Error: rvinterf sent us an invalid RVT msg\n");
+		exit(1);
+	}
+	useid = rvi_msg[2] << 24 | rvi_msg[3] << 16 | rvi_msg[4] << 8
+		| rvi_msg[5];
+	switch (useid) {
+	case 0:
+		handle_useid_0();
+		return;
+	default:
+		tty_cleanup();
+		fprintf(stderr, "unexpected fwd of USEID %08X from rvinterf\n",
+			useid);
+		exit(1);
+	}
+}
+
+void
+g23_packet_rx()
+{
+	char fmtbuf[MAX_PKT_FROM_TARGET*8];	/* size it generously */
+
+	format_g23_packet(rvi_msg + 1, rvi_msg_len - 1, fmtbuf);
+	async_msg_output(fmtbuf);
+}
+
+void
+process_pkt_from_target()
+{
+	switch (rvi_msg[1]) {
+	case RVT_RV_HEADER:
+		process_rvt();
+		return;
+	case RVT_L23_HEADER:
+		g23_packet_rx();
+		return;
+	default:
+		tty_cleanup();
+		fprintf(stderr, "unexpected fwd of MUX %02X from rvinterf\n",
+			rvi_msg[1]);
+		exit(1);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/g23sh/sendsp.c	Tue Apr 22 07:21:04 2014 +0000
@@ -0,0 +1,53 @@
+/*
+ * g23sh system primitive sending command
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include "pktmux.h"
+#include "limits.h"
+
+void
+cmd_sendsp(argc, argv)
+	char **argv;
+{
+	char *stackdest, *primarg;
+	unsigned intlen;
+	u_char sendpkt[MAX_PKT_TO_TARGET+1];
+	unsigned pktlen;
+
+	stackdest = argv[1];
+	primarg = argv[2];
+	if (strlen(stackdest) > 4) {
+		printf(
+		  "error: stack destination arg may not exceed 4 characters\n");
+		return;
+	}
+	intlen = 12 + strlen(primarg);
+	pktlen = intlen + 4;
+	if (pktlen > MAX_PKT_TO_TARGET) {
+		printf("error: max pkt to target limit exceeded\n");
+		return;
+	}
+	/* fill out the packet */
+	sendpkt[0] = RVT_L23_HEADER;
+	sendpkt[1] = 0xB7;	/* system prim */
+	sendpkt[2] = intlen;
+	sendpkt[3] = intlen >> 8;
+	/* send zeros for the timestamp */
+	sendpkt[4] = 0;
+	sendpkt[5] = 0;
+	sendpkt[6] = 0;
+	sendpkt[7] = 0;
+	/* as far as TI's sw is concerned, we are PCO */
+	sendpkt[8] = 'P';
+	sendpkt[9] = 'C';
+	sendpkt[10] = 'O';
+	sendpkt[11] = ' ';
+	sprintf(sendpkt + 12, "%-4s%s", stackdest, primarg);
+	/* send it! */
+	send_pkt_to_target(sendpkt, pktlen);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/g23sh/usercmd.c	Tue Apr 22 07:21:04 2014 +0000
@@ -0,0 +1,90 @@
+/*
+ * This module implements g23sh user command dispatch.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+
+extern char usercmd[];
+
+extern void cmd_sendsp();
+
+void
+cmd_exit()
+{
+	tty_cleanup();
+	exit(0);
+}
+
+static struct cmdtab {
+	char *cmd;
+	int minargs;
+	int maxargs;
+	void (*func)();
+} cmdtab[] = {
+	{"exit", 0, 0, cmd_exit},
+	{"quit", 0, 0, cmd_exit},
+	{"sp", 2, 2, cmd_sendsp},
+	{0, 0, 0, 0}
+};
+
+void
+dispatch_user_cmd()
+{
+	char *argv[10];
+	char *cp, **ap;
+	struct cmdtab *tp;
+
+	for (cp = usercmd; isspace(*cp); cp++)
+		;
+	if (!*cp || *cp == '#')
+		return;
+	argv[0] = cp;
+	while (*cp && !isspace(*cp))
+		cp++;
+	if (*cp)
+		*cp++ = '\0';
+	for (tp = cmdtab; tp->cmd; tp++)
+		if (!strcmp(tp->cmd, argv[0]))
+			break;
+	if (!tp->func) {
+		printf("error: no such command\n");
+		return;
+	}
+	for (ap = argv + 1; ; ) {
+		while (isspace(*cp))
+			cp++;
+		if (!*cp || *cp == '#')
+			break;
+		if (ap - argv - 1 >= tp->maxargs) {
+			printf("error: too many arguments\n");
+			return;
+		}
+		if (*cp == '"') {
+			*ap++ = ++cp;
+			while (*cp && *cp != '"')
+				cp++;
+			if (*cp != '"') {
+				printf("error: unterminated quoted string\n");
+				return;
+			}
+			*cp++ = '\0';
+		} else {
+			*ap++ = cp;
+			while (*cp && !isspace(*cp))
+				cp++;
+			if (*cp)
+				*cp++ = '\0';
+		}
+	}
+	if (ap - argv - 1 < tp->minargs) {
+		printf("error: too few arguments\n");
+		return;
+	}
+	*ap = 0;
+	tp->func(ap - argv, argv);
+}