view rvinterf/tmsh/usercmd.c @ 964:a96cb97b66a2

ringtools/imy: fix duplicate definition of tdma_durations[] The bug was reported by Vadim Yanitskiy <fixeria@osmocom.org>, although the present fix is slightly different from the contributed patch: because main.c doesn't need this tdma_durations[] array at all, let's simply remove the reference to this array from main.c rather than turn it into an extern. I no longer remember my original thought flow that resulted (by mistake) in tdma_durations[] being multiply defined in main.c and durations.c. My intent might have been to define all globals in main.c and have the reference in durations.c be an extern - and I missed that extern - but without clear memory, I have no certainty. In any case, having this data array defined in the same module that fills it (durations.c) is sensible, so let's make it the new way.
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 31 Aug 2023 19:38:18 +0000
parents b3724fe6c581
children
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_bsim();
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_phone_on();
extern int cmd_ping();
extern int cmd_pwr_key();
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_rvf_dump();
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},
	{"bsim", 1, 2, cmd_bsim},
	{"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},
	{"phone-on", 0, 0, cmd_phone_on},
	{"ping", 0, 2, cmd_ping},
	{"pwr-key", 0, 0, cmd_pwr_key},
	{"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},
	{"rvf-dump", 0, 0, cmd_rvf_dump},
	{"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);
}