view loadtools/initscript.c @ 995:74024eb17e04

fc-loadtool help: improve language regarding 16 MiB flash chips In FC project history, 16 MiB flash originally meant Pirelli DP-L10. Then we got FCDEV3B with the same flash (our own design), but now we are discovering more Calypso devices that used such large flash, both late Calypso era (Sony Ericsson K2x0) as well as much earlier ones (FIC FLUID devices.txt file with 2004 dates, Leonardo+ rev 5). Hence we need to migrate to more generic or neutral language in associated documentation, without giving elevated status to specific examples that drove our early project history.
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 03 Dec 2023 21:11:12 +0000
parents e7502631a0f9
children
line wrap: on
line source

/*
 * 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[];

extern int cmd_baud();

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[] = {
	{"baud", 1, 1, cmd_baud},
	{"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);
}