view rvinterf/tmsh/usercmd.c @ 73:cd1a0d5689f6

rvinterf/etmsync/fsiomain.c: same "usage" fix as in fc-tmsh
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 27 Oct 2016 00:17:17 +0000
parents 27c41e4b21ae
children 2825d79bb151
line wrap: on
line source

/*
 * This module implements fc-tmsh user command dispatch.
 */

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include "exitcodes.h"

extern char usercmd[];

extern int cmd_abbr();
extern int cmd_abbw();
extern int cmd_audio();
extern int cmd_check_ffs1();
extern int cmd_dieid();
extern int cmd_ffs2();
extern int cmd_omr();
extern int cmd_ping();
extern int cmd_r8();
extern int cmd_r16();
extern int cmd_r32();
extern int cmd_tgtreset();
extern int cmd_tmpkt();
extern int cmd_version();
extern int cmd_w8();
extern int cmd_w16();
extern int cmd_w32();

cmd_exit()
{
	tty_cleanup();
	exit(0);
}

static struct cmdtab {
	char *cmd;
	int minargs;
	int maxargs;
	int (*func)();
} cmdtab[] = {
	{"abbr", 2, 2, cmd_abbr},
	{"abbw", 3, 3, cmd_abbw},
	{"audio", 2, 2, cmd_audio},
	{"check-ffs1", 0, 0, cmd_check_ffs1},
	{"dieid", 0, 0, cmd_dieid},
	{"etmpkt", 1, 253, cmd_tmpkt},
	{"exit", 0, 0, cmd_exit},
	{"ffs2", 1, 3, cmd_ffs2},
	{"omr", 2, 2, cmd_omr},
	{"ping", 0, 2, cmd_ping},
	{"quit", 0, 0, cmd_exit},
	{"r8", 1, 2, cmd_r8},
	{"r16", 1, 2, cmd_r16},
	{"r32", 1, 2, cmd_r32},
	{"tgtreset", 0, 0, cmd_tgtreset},
	{"tmpkt", 1, 253, cmd_tmpkt},
	{"version", 1, 1, cmd_version},
	{"w8", 2, 246, cmd_w8},
	{"w16", 2, 123, cmd_w16},
	{"w32", 2, 62, cmd_w32},
	{0, 0, 0, 0}
};

void
dispatch_user_cmd()
{
	char *argv[257];
	char *cp, **ap;
	struct cmdtab *tp;

	for (cp = usercmd; isspace(*cp); cp++)
		;
	if (!*cp || *cp == '#')
		return;
	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) {
		printf("error: no such command\n");
		return;
	}
	for (ap = argv + 1; ; ) {
		while (isspace(*cp))
			cp++;
		if (!*cp || *cp == '#')
			break;
		if (ap - argv - 1 >= tp->maxargs) {
			printf("error: too many arguments\n");
			return;
		}
		if (*cp == '"') {
			*ap++ = ++cp;
			while (*cp && *cp != '"')
				cp++;
			if (*cp != '"') {
				printf("error: unterminated quoted string\n");
				return;
			}
			*cp++ = '\0';
		} else {
			*ap++ = cp;
			while (*cp && !isspace(*cp))
				cp++;
			if (*cp)
				*cp++ = '\0';
		}
	}
	if (ap - argv - 1 < tp->minargs) {
		printf("error: too few arguments\n");
		return;
	}
	*ap = 0;
	tp->func(ap - argv, argv);
}

dispatch_oneshot_cmd(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 command\n", argv[0]);
		exit(ERROR_USAGE);
	}
	if (argc - 1 > tp->maxargs) {
		fprintf(stderr, "%s: too many arguments\n", tp->cmd);
		exit(ERROR_USAGE);
	}
	if (argc - 1 < tp->minargs) {
		fprintf(stderr, "%s: too few arguments\n", tp->cmd);
		exit(ERROR_USAGE);
	}
	return tp->func(argc, argv);
}