view libcommon/dispatch.c @ 99:97ba63d9361a

scripts/fcsim1-sst: turn off STK & OTA services In the initial unprogrammed state of the cards from Grcard, SST has services 25 through 29 set to allocated and activated. However, these cards appear to not actually support OTA, ENVELOPE commands do nothing (just return SW 9000), and they were never observed issuing any proactive SIM commands, even after a feature-generous TERMINAL PROFILE. Therefore, let's list these STK & OTA services as allocated, but not activated in our FCSIM1 SST.
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 05 May 2021 04:26:07 +0000
parents b7ee2e85686b
children
line wrap: on
line source

/*
 * This module implements common command dispatch for our SIM tools.
 */

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

extern struct cmdtab cmdtab[];

static FILE *
handle_output_redir(str)
	char *str;
{
	char *cp, *fn;
	FILE *outf;

	for (cp = str; isspace(*cp); cp++)
		;
	if (!*cp || *cp == '#') {
		fprintf(stderr, "error: no filename after '>'\n");
		return(0);
	}
	for (fn = cp; *cp && !isspace(*cp); cp++)
		;
	if (*cp)
		*cp++ = '\0';
	while (isspace(*cp))
		cp++;
	if (*cp && *cp != '#') {
		fprintf(stderr, "error: invalid syntax after '>'\n");
		return(0);
	}
	outf = fopen(fn, "w");
	if (!outf)
		perror(fn);
	return outf;
}

simtool_dispatch_cmd(cmd, is_script)
	char *cmd;
{
	char *argv[20];
	char *cp, **ap;
	struct cmdtab *tp;
	FILE *outf;
	int rc;

	for (cp = cmd; isspace(*cp); cp++)
		;
	if (!*cp || *cp == '#')
		return(0);
	if (is_script)
		printf("Script command: %s\n", cp);
	if (*cp == '!')
		return system(cp + 1);
	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 == '#' || *cp == '>')
			break;
		if (ap - argv - 1 >= tp->maxargs) {
			fprintf(stderr, "error: too many arguments\n");
			return(-1);
		}
		if (*cp == '"') {
			*ap++ = ++cp;
			for (;;) {
				if (!*cp) {
unterm_qstring:				fprintf(stderr,
					"error: unterminated quoted string\n");
					return(-1);
				}
				if (*cp == '"')
					break;
				if (*cp++ == '\\') {
					if (!*cp)
						goto unterm_qstring;
					cp++;
				}
			}
			*cp++ = '\0';
		} else {
			*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;
	if (*cp == '>') {
		if (!tp->allow_redir) {
			fprintf(stderr,
			"error: command does not support output redirection\n");
			return(-1);
		}
		outf = handle_output_redir(cp + 1);
		if (!outf)
			return(-1);
	} else
		outf = stdout;
	rc = tp->func(ap - argv, argv, outf);
	if (outf != stdout)
		fclose(outf);
	return rc;
}

dispatch_ready_argv(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: no such command\n");
		return(-1);
	}
	if (argc - 1 > tp->maxargs) {
		fprintf(stderr, "error: too many arguments\n");
		return(-1);
	}
	if (argc - 1 < tp->minargs) {
		fprintf(stderr, "error: too few arguments\n");
		return(-1);
	}
	return tp->func(argc, argv, stdout);
}