view lcdtest/dispatch.c @ 176:fb2f6497ba53 default tip

doc/Linux-DTR-RTS-flaw: point to new location of this article
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 11 Dec 2023 19:37:20 +0000
parents 0a44549a64c8
children
line wrap: on
line source

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

extern int cmd_cd();
extern int cmd_fc();
extern int cmd_fr();
extern int cmd_rd();
extern int cmd_ri();
extern int cmd_show();
extern int cmd_wd();
extern int cmd_wi();
extern int cmd_wr();

extern int reset_pulse();
extern int init_haoran();
extern int init_startek();

cmd_exit()
{
	exit(0);
}

static struct cmdtab {
	char *cmd;
	int minargs;
	int maxargs;
	int (*func)();
} cmdtab[] = {
	{"cd", 1, 1, cmd_cd},
	{"exit", 0, 0, cmd_exit},
	{"fc", 1, 16, cmd_fc},
	{"fr", 1, 1, cmd_fr},
	{"init-ht", 0, 0, init_haoran},
	{"init-st", 0, 0, init_startek},
	{"quit", 0, 0, cmd_exit},
	{"rd", 0, 0, cmd_rd},
	{"reset", 0, 0, reset_pulse},
	{"ri", 0, 0, cmd_ri},
	{"show", 1, 1, cmd_show},
	{"wd", 1, 1, cmd_wd},
	{"wi", 1, 1, cmd_wi},
	{"wr", 2, 2, cmd_wr},
	{0, 0, 0, 0}
};

dispatch_cmd(cmd)
	char *cmd;
{
	char *argv[18];
	char *cp, **ap;
	struct cmdtab *tp;

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