diff rvinterf/asyncshell/oneshot.c @ 965:bd873572ef2c

fc-shell: one-shot command mode implemented
author Mychaela Falconia <falcon@ivan.Harhan.ORG>
date Sat, 07 Nov 2015 00:00:46 +0000
parents
children 93f4fc26b204
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/asyncshell/oneshot.c	Sat Nov 07 00:00:46 2015 +0000
@@ -0,0 +1,49 @@
+/*
+ * This module implements the one-shot command mode of fc-shell.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+
+extern int cmd_poweroff();
+extern int cmd_sp_oneshot();
+extern int cmd_tgtreset();
+
+static struct cmdtab {
+	char *cmd;
+	int minargs;
+	int maxargs;
+	int (*func)();
+} cmdtab[] = {
+	{"poweroff", 0, 0, cmd_poweroff},
+	{"sp", 2, 2, cmd_sp_oneshot},
+	{"tgtreset", 0, 0, cmd_tgtreset},
+	{0, 0, 0, 0}
+};
+
+oneshot_command(argc, argv)
+	char **argv;
+{
+	struct cmdtab *tp;
+
+	for (tp = cmdtab; tp->cmd; tp++)
+		if (!strcmp(tp->cmd, argv[0]))
+			break;
+	if (!tp->func) {
+		fprintf(stderr,
+			"error: \"%s\" is not a valid one-shot command\n",
+			argv[0]);
+		exit(1);
+	}
+	if (argc - 1 > tp->maxargs) {
+		fprintf(stderr, "%s: too many arguments\n", tp->cmd);
+		exit(1);
+	}
+	if (argc - 1 < tp->minargs) {
+		fprintf(stderr, "%s: too few arguments\n", tp->cmd);
+		exit(1);
+	}
+	return tp->func(argc, argv);
+}