view test-voice/user_cmd.c @ 18:f7321b25195e default tip

new build system for ThemWi sw components
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 14 May 2024 18:08:35 -0800
parents 71f01a834820
children
line wrap: on
line source

/*
 * In this module we implement stdin command handling, supporting
 * user-initiated CANCEL and BYE as well as TFO testing commands.
 */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

static void
pcm_fill_cmd(arg)
	char *arg;
{
	char *cp;
	unsigned octet;

	for (cp = arg; isspace(*cp); cp++)
		;
	if (!isxdigit(*cp)) {
inv_syntax:	fprintf(stderr, "error: pcm-fill command invalid syntax\n");
		return;
	}
	octet = strtoul(cp, &cp, 16);
	if (*cp)
		goto inv_syntax;
	if (octet > 0xFF) {
		fprintf(stderr,
			"error: pcm-fill octet argument out of range\n");
		return;
	}
	set_pcm_fill_octet(octet);
}

static void
play_cmd_plain(args)
	char *args;
{
	char *cp, *filename;
	int loop;

	for (cp = args; isspace(*cp); cp++)
		;
	if (!*cp) {
inv_syntax:	fprintf(stderr, "error: play command invalid syntax\n");
		return;
	}
	for (filename = cp; *cp && !isspace(*cp); cp++)
		;
	if (*cp) {
		*cp++ = '\0';
		while (isspace(*cp))
			cp++;
		if (strcmp(cp, "loop"))
			goto inv_syntax;
		loop = 1;
	} else
		loop = 0;
	play_file_cmdop(filename, loop);
}

static void
play_cmd_offset(args)
	char *args;
{
	char *cp, *filename;
	unsigned offset;

	for (cp = args; isspace(*cp); cp++)
		;
	if (!*cp) {
inv_syntax:	fprintf(stderr, "error: play-offset command invalid syntax\n");
		return;
	}
	for (filename = cp; *cp && !isspace(*cp); cp++)
		;
	if (!*cp)
		goto inv_syntax;
	*cp++ = '\0';
	while (isspace(*cp))
		cp++;
	if (!isdigit(*cp))
		goto inv_syntax;
	offset = strtoul(cp, &cp, 0);
	if (*cp && !isspace(*cp))
		goto inv_syntax;
	if (offset < 1 || offset > 159) {
		fprintf(stderr, "error: offset argument out of range\n");
		return;
	}
	while (isspace(*cp))
		cp++;
	if (*cp)
		goto inv_syntax;
	play_file_offset(filename, offset);
}

static void
tfo_req_cmd(args)
	char *args;
{
	char *cp;
	unsigned sig, codec;

	for (cp = args; isspace(*cp); cp++)
		;
	if (!isdigit(*cp)) {
inv_syntax:	fprintf(stderr, "error: tfo-req command invalid syntax\n");
		return;
	}
	sig = strtoul(cp, &cp, 0);
	if (!isspace(*cp))
		goto inv_syntax;
	if (sig > 0xFF) {
		fprintf(stderr, "error: Sig argument out of range\n");
		return;
	}
	while (isspace(*cp))
		cp++;
	if (!isdigit(*cp))
		goto inv_syntax;
	codec = strtoul(cp, &cp, 0);
	if (*cp && !isspace(*cp))
		goto inv_syntax;
	if (codec > 14) {
		fprintf(stderr, "error: Codec_Type argument out of range\n");
		return;
	}
	while (isspace(*cp))
		cp++;
	if (*cp)
		goto inv_syntax;
	send_tfo_req(sig, codec);
}

void
select_stdin()
{
	char buf[256], *cp;

	fgets(buf, sizeof buf, stdin);
	cp = index(buf, '\n');
	if (cp) {
		while (cp > buf && isspace(cp[-1]))
			cp--;
		*cp = '\0';
	}
	for (cp = buf; isspace(*cp); cp++)
		;
	if (!*cp)
		return;
	if (!strcmp(cp, "b") || !strcasecmp(cp, "bye"))
		send_bye_req();
	else if (!strcmp(cp, "c") || !strcasecmp(cp, "cancel"))
		send_cancel_req();
	else if (!strncmp(cp, "pcm-fill", 8) && isspace(cp[8]))
		pcm_fill_cmd(cp + 9);
	else if (!strcmp(cp, "dmw-on"))
		cmd_dmw_on();
	else if (!strcmp(cp, "dmw-off"))
		cmd_dmw_off();
	else if (!strncmp(cp, "play", 4) && isspace(cp[4]))
		play_cmd_plain(cp + 5);
	else if (!strncmp(cp, "play-offset", 11) && isspace(cp[11]))
		play_cmd_offset(cp + 12);
	else if (!strcmp(cp, "play-stop"))
		play_file_stop();
	else if (!strncmp(cp, "tfo-req", 7) && isspace(cp[7]))
		tfo_req_cmd(cp + 8);
	else if (!strcmp(cp, "tfo-stop"))
		stop_tfo_out();
	else
		fprintf(stderr, "error: non-understood stdin command\n");
}