changeset 276:909f00c15f27

more fc-fsio foundation
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 23 Feb 2014 21:31:30 +0000
parents cedf09b6b5ac
children e23fc1228efd
files rvinterf/etmsync/Makefile rvinterf/etmsync/cmdtab.h rvinterf/etmsync/connect.c rvinterf/etmsync/dispatch.c rvinterf/etmsync/exitcodes.h rvinterf/etmsync/fscmdtab.c rvinterf/etmsync/fsiomain.c rvinterf/etmsync/launchrvif.c
diffstat 8 files changed, 144 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/rvinterf/etmsync/Makefile	Sun Feb 23 20:27:15 2014 +0000
+++ b/rvinterf/etmsync/Makefile	Sun Feb 23 21:31:30 2014 +0000
@@ -3,7 +3,7 @@
 PROGS=	fc-fsio
 INSTBIN=/usr/local/bin
 
-FSIO_OBJS=	connect.o fsiomain.o launchrvif.o
+FSIO_OBJS=	connect.o dispatch.o fscmdtab.o fsiomain.o launchrvif.o
 
 all:	${PROGS}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/etmsync/cmdtab.h	Sun Feb 23 21:31:30 2014 +0000
@@ -0,0 +1,8 @@
+struct cmdtab {
+	char *cmd;
+	int minargs;
+	int maxargs;
+	int (*func)();
+};
+
+#define	MAX_CMD_ARGS	16
--- a/rvinterf/etmsync/connect.c	Sun Feb 23 20:27:15 2014 +0000
+++ b/rvinterf/etmsync/connect.c	Sun Feb 23 21:31:30 2014 +0000
@@ -12,6 +12,7 @@
 #include <unistd.h>
 #include "pktmux.h"
 #include "localsock.h"
+#include "exitcodes.h"
 
 extern char *socket_pathname;
 extern int sock;
@@ -26,7 +27,7 @@
 	sock = socket(AF_UNIX, SOCK_STREAM, 0);
 	if (sock < 0) {
 		perror("socket(AF_UNIX, SOCK_STREAM, 0)");
-		exit(1);
+		exit(ERROR_UNIX);
 	}
 
 	local.sun_family = AF_UNIX;
@@ -48,7 +49,7 @@
 	rc = connect(sock, (struct sockaddr *) &local, namelen);
 	if (rc != 0) {
 		perror(socket_pathname);
-		exit(1);
+		exit(ERROR_RVINTERF);
 	}
 
 	return(0);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/etmsync/dispatch.c	Sun Feb 23 21:31:30 2014 +0000
@@ -0,0 +1,96 @@
+/*
+ * This module implements the command dispatch for fc-fsio
+ * and possibly other similar utilities in the future.
+ */
+
+#include <ctype.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include "cmdtab.h"
+#include "exitcodes.h"
+
+extern struct cmdtab cmdtab[];
+
+parse_and_dispatch_cmd(cmd, is_script)
+	char *cmd;
+{
+	char *argv[MAX_CMD_ARGS+2];
+	char *cp, **ap;
+	struct cmdtab *tp;
+
+	for (cp = cmd; isspace(*cp); cp++)
+		;
+	if (!*cp || *cp == '#')
+		return(0);
+	if (is_script)
+		printf("Script command: %s\n", cp);
+	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) {
+		fprintf(stderr, "error: no such command\n");
+		return(ERROR_USAGE);
+	}
+	for (ap = argv + 1; ; ) {
+		while (isspace(*cp))
+			cp++;
+		if (!*cp || *cp == '#')
+			break;
+		if (ap - argv - 1 >= tp->maxargs) {
+			fprintf(stderr, "error: too many arguments\n");
+			return(ERROR_USAGE);
+		}
+		*ap++ = cp;
+		while (*cp && !isspace(*cp))
+			cp++;
+		if (*cp)
+			*cp++ = '\0';
+	}
+	if (ap - argv - 1 < tp->minargs) {
+		fprintf(stderr, "error: too few arguments\n");
+		return(ERROR_USAGE);
+	}
+	*ap = 0;
+	return tp->func(ap - argv, argv);
+}
+
+cmd_exec(argc, argv)
+	char **argv;
+{
+	FILE *f;
+	char linebuf[512], *cp;
+	int lineno, retval = 0;
+
+	f = fopen(argv[1], "r");
+	if (!f) {
+		perror(argv[1]);
+		return(ERROR_USAGE);
+	}
+	for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) {
+		cp = index(linebuf, '\n');
+		if (!cp) {
+			fprintf(stderr, "%s line %d: missing newline\n",
+				argv[1], lineno);
+			fclose(f);
+			return(ERROR_USAGE);
+		}
+		*cp = '\0';
+		retval = parse_and_dispatch_cmd(linebuf, 1);
+		if (retval)
+			break;
+	}
+	fclose(f);
+	return(retval);
+}
+
+cmd_exit()
+{
+	exit(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/etmsync/exitcodes.h	Sun Feb 23 21:31:30 2014 +0000
@@ -0,0 +1,4 @@
+#define	ERROR_USAGE	1
+#define	ERROR_TARGET	2
+#define	ERROR_RVINTERF	3
+#define	ERROR_UNIX	4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/etmsync/fscmdtab.c	Sun Feb 23 21:31:30 2014 +0000
@@ -0,0 +1,14 @@
+/*
+ * fc-fsio command dispatch table
+ */
+
+#include "cmdtab.h"
+
+extern int cmd_exec();
+extern int cmd_exit();
+
+struct cmdtab cmdtab[] = {
+	{"exec", 1, 1, cmd_exec},
+	{"exit", 0, 0, cmd_exit},
+	{0, 0, 0, 0}
+};
--- a/rvinterf/etmsync/fsiomain.c	Sun Feb 23 20:27:15 2014 +0000
+++ b/rvinterf/etmsync/fsiomain.c	Sun Feb 23 21:31:30 2014 +0000
@@ -4,6 +4,8 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
+#include "exitcodes.h"
 
 char *socket_pathname = "/tmp/rvinterf_socket";
 int sock;
@@ -16,6 +18,7 @@
 	extern int optind;
 	extern char *optarg;
 	int c, sopt = 0;
+	char command[512];
 
 	while ((c = getopt(argc, argv, "B:l:p:s:w:")) != EOF)
 		switch (c) {
@@ -39,14 +42,14 @@
 		default:
 usage:			fprintf(stderr,
 				"usage: %s [options] [command]\n", argv[0]);
-			exit(1);
+			exit(ERROR_USAGE);
 		}
 	if (rvinterf_ttyport) {
 		if (sopt) {
 			fprintf(stderr,
 			"%s error: -p and -s options are mutually exclusive\n",
 				argv[0]);
-			exit(1);
+			exit(ERROR_USAGE);
 		}
 		launch_rvinterf();
 	} else {
@@ -54,10 +57,18 @@
 			fprintf(stderr,
 "%s error: -B, -l and -w options are meaningful only when launching rvinterf\n",
 				argv[0]);
-			exit(1);
+			exit(ERROR_USAGE);
 		}
 		connect_local_socket();
 	}
 
-
+	for (;;) {
+		if (isatty(0)) {
+			fputs("fsio> ", stdout);
+			fflush(stdout);
+		}
+		if (!fgets(command, sizeof command, stdin))
+			exit(0);
+		parse_and_dispatch_cmd(command, 0);
+	}
 }
--- a/rvinterf/etmsync/launchrvif.c	Sun Feb 23 20:27:15 2014 +0000
+++ b/rvinterf/etmsync/launchrvif.c	Sun Feb 23 21:31:30 2014 +0000
@@ -8,6 +8,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include "exitcodes.h"
 
 static char rvinterf_pathname[] = "/usr/local/bin/rvinterf";
 
@@ -23,7 +24,7 @@
 	rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sp);
 	if (rc < 0) {
 		perror("socketpair");
-		exit(1);
+		exit(ERROR_UNIX);
 	}
 	sock = sp[0];
 	sprintf(Sarg, "-S%d", sp[1]);
@@ -48,7 +49,7 @@
 	rc = vfork();
 	if (rc < 0) {
 		perror("vfork for launching rvinterf");
-		exit(1);
+		exit(ERROR_UNIX);
 	}
 	if (!rc) {
 		/* we are in the child - do the exec */