view rvinterf/tmsh/usercmd.c @ 777:0cffc53991f9

simagent: bugfix in hex string arg parsing function
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 12 Mar 2021 23:58:15 +0000
parents 0f2db8baf8db
children 4c7f3778dc24
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_aul();
extern int cmd_aur();
extern int cmd_aus();
extern int cmd_auw();
extern int cmd_auw_fir();
extern int cmd_check_ffs1();
extern int cmd_dieid();
extern int cmd_ffs2();
extern int cmd_me();
extern int cmd_mpr();
extern int cmd_mpw();
extern int cmd_oabbr();
extern int cmd_oabbw();
extern int cmd_omr();
extern int cmd_omw();
extern int cmd_ping();
extern int cmd_r8();
extern int cmd_r16();
extern int cmd_r32();
extern int cmd_rfe();
extern int cmd_rfpr();
extern int cmd_rfpw();
extern int cmd_rftr();
extern int cmd_rftw();
extern int cmd_rxpr();
extern int cmd_rxpw();
extern int cmd_save_rf_table();
extern int cmd_save_rf_table_raw();
extern int cmd_save_tx_ramp();
extern int cmd_scr();
extern int cmd_scw();
extern int cmd_sr();
extern int cmd_tgtreset();
extern int cmd_tm3ver();
extern int cmd_tminit();
extern int cmd_tmpkt();
extern int cmd_tms();
extern int cmd_ttr();
extern int cmd_ttw();
extern int cmd_txpr();
extern int cmd_txpw();
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},
	{"aul", 1, 1, cmd_aul},
	{"aur", 1, 1, cmd_aur},
	{"aus", 1, 1, cmd_aus},
	{"auw", 2, 126, cmd_auw},
	{"auw-fir", 2, 2, cmd_auw_fir},
	{"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},
	{"me", 1, 1, cmd_me},
	{"mpr", 1, 1, cmd_mpr},
	{"mpw", 2, 2, cmd_mpw},
	{"oabbr", 2, 2, cmd_oabbr},
	{"oabbw", 3, 3, cmd_oabbw},
	{"omr", 2, 2, cmd_omr},
	{"omw", 2, 129, cmd_omw},
	{"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},
	{"rfe", 1, 1, cmd_rfe},
	{"rfpr", 1, 1, cmd_rfpr},
	{"rfpw", 2, 3, cmd_rfpw},
	{"rftr", 1, 1, cmd_rftr},
	{"rftw", 2, 3, cmd_rftw},
	{"rxpr", 1, 1, cmd_rxpr},
	{"rxpw", 2, 2, cmd_rxpw},
	{"save-rf-table", 1, 1, cmd_save_rf_table},
	{"save-rf-table-raw", 1, 1, cmd_save_rf_table_raw},
	{"save-tx-ramp", 1, 1, cmd_save_tx_ramp},
	{"scr", 1, 1, cmd_scr},
	{"scw", 2, 2, cmd_scw},
	{"sr", 2, 2, cmd_sr},
	{"tgtreset", 0, 0, cmd_tgtreset},
	{"tm3ver", 1, 1, cmd_tm3ver},
	{"tminit", 0, 0, cmd_tminit},
	{"tmpkt", 1, 253, cmd_tmpkt},
	{"tms", 1, 1, cmd_tms},
	{"ttr", 1, 1, cmd_ttr},
	{"ttw", 2, 2, cmd_ttw},
	{"txpr", 1, 1, cmd_txpr},
	{"txpw", 2, 2, cmd_txpw},
	{"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);
}