view rvinterf/etm/usercmd.c @ 254:4eeab025b502

doc/TIFFS-old-description: the original description from SE 52 Mes 16
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 02 Feb 2014 00:02:26 +0000
parents c9ea2f7441a5
children c413e791595a
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>

extern char usercmd[];

extern void cmd_abbr();
extern void cmd_abbw();
extern void cmd_dieid();
extern void cmd_etmpkt();
extern void cmd_ping();
extern void cmd_r8();
extern void cmd_r16();
extern void cmd_r32();
extern void cmd_tgtreset();
extern void cmd_version();
extern void cmd_w8();
extern void cmd_w16();
extern void cmd_w32();

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

static struct cmdtab {
	char *cmd;
	int minargs;
	int maxargs;
	void (*func)();
} cmdtab[] = {
	{"abbr", 2, 2, cmd_abbr},
	{"abbw", 3, 3, cmd_abbw},
	{"dieid", 0, 0, cmd_dieid},
	{"etmpkt", 1, 253, cmd_etmpkt},
	{"exit", 0, 0, cmd_exit},
	{"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},
	{"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;
		}
		*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);
}