view rvinterf/etmsync/dispatch.c @ 497:74610c4f10f7

target-utils: added 10 ms delay at the end of abb_power_off() The deosmification of the ABB access code (replacement of osmo_delay_ms() bogus delays with correctly-timed ones, which are significantly shorter) had one annoying side effect: when executing the poweroff command from any of the programs, one last '=' prompt character was being sent (and received by the x86 host) as the Calypso board powers off. With delays being shorter now, the abb_power_off() function was returning and the standalone program's main loop was printing its prompt before the Iota chip fully executed the switch-off sequence! I thought about inserting an endless tight loop at the end of the abb_power_off() function, but the implemented solution of a 10 ms delay is a little nicer IMO because if the DEVOFF operation doesn't happen for some reason in a manual hacking scenario, there won't be an artificial blocker in the form of a tight loop keeping us from further poking around.
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 25 May 2019 20:44:05 +0000
parents e7502631a0f9
children
line wrap: on
line source

/*
 * 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);
		}
		if (*cp == '"') {
			*ap++ = ++cp;
			while (*cp && *cp != '"')
				cp++;
			if (*cp != '"') {
				fprintf(stderr,
					"error: unterminated quoted string\n");
				return(ERROR_USAGE);
			}
			*cp++ = '\0';
		} else {
			*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);
}

dispatch_ready_argv(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: no such command\n");
		return(ERROR_USAGE);
	}
	if (argc - 1 > tp->maxargs) {
		fprintf(stderr, "error: too many arguments\n");
		return(ERROR_USAGE);
	}
	if (argc - 1 < tp->minargs) {
		fprintf(stderr, "error: too few arguments\n");
		return(ERROR_USAGE);
	}
	return tp->func(argc, 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);
}