diff loadtools/initscript.c @ 45:16315ed6401a

init-script logic implemented in fc-xram
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Mon, 17 Jun 2013 08:15:30 +0000
parents
children 50b652bc3a4f
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/loadtools/initscript.c	Mon Jun 17 08:15:30 2013 +0000
@@ -0,0 +1,121 @@
+/*
+ * This module has been copied from ltscript.c, ltdispatch.c and ltpassthru.c:
+ * here we implement the init-script functionality for fc-xram.
+ */
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+
+extern char default_helpers_dir[];
+
+static
+loadagent_cmd(argc, argv)
+	char **argv;
+{
+	if (tpinterf_make_cmd(argv) < 0) {
+		fprintf(stderr, "error: unable to form target command\n");
+		return(-1);
+	}
+	if (tpinterf_send_cmd() < 0)
+		return(-1);
+	return tpinterf_pass_output(1);
+}
+
+static struct cmdtab {
+	char *cmd;
+	int minargs;
+	int maxargs;
+	int (*func)();
+} cmdtab[] = {
+	{"w8", 2, 2, loadagent_cmd},
+	{"w16", 2, 2, loadagent_cmd},
+	{"w32", 2, 2, loadagent_cmd},
+	{0, 0, 0, 0}
+};
+
+static
+dispatch_cmd(cmd)
+	char *cmd;
+{
+	char *argv[10];
+	char *cp, **ap;
+	struct cmdtab *tp;
+
+	for (cp = cmd; isspace(*cp); cp++)
+		;
+	if (!*cp || *cp == '#')
+		return(0);
+	printf("init-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(-1);
+	}
+	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(-1);
+		}
+		*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(-1);
+	}
+	*ap = 0;
+	return tp->func(ap - argv, argv);
+}
+
+exec_init_script(script_name)
+	char *script_name;
+{
+	char pathbuf[MAXPATHLEN], *openfname;
+	FILE *f;
+	char linebuf[512], *cp;
+	int lineno, retval = 0;
+
+	if (index(script_name, '/'))
+		openfname = script_name;
+	else {
+		sprintf(pathbuf, "%s/%s", default_helpers_dir, script_name);
+		openfname = pathbuf;
+	}
+	f = fopen(openfname, "r");
+	if (!f) {
+		perror(openfname);
+		return(-1);
+	}
+	for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) {
+		cp = index(linebuf, '\n');
+		if (!cp) {
+			fprintf(stderr, "%s line %d: missing newline\n",
+				openfname, lineno);
+			fclose(f);
+			return(-1);
+		}
+		*cp = '\0';
+		retval = dispatch_cmd(linebuf);
+		if (retval)
+			break;
+	}
+	fclose(f);
+	return(retval);
+}