~sircmpwn/himitsu

8e19d89f9b041bdf9de808b3528945887d8845ef — Drew DeVault a month ago b69e36f
rpc: implement add command
1 files changed, 22 insertions(+), 2 deletions(-)

M himitsud/cmd.c
M himitsud/cmd.c => himitsud/cmd.c +22 -2
@@ 6,7 6,7 @@
#include "secstore.h"

static void
write_key(const struct hi_key *key, int out)
write_key(int out, const struct hi_key *key)
{
	char resp[] = "key ";
	write(out, resp, sizeof(resp) - 1);


@@ 35,7 35,7 @@ cmd_query(struct hi_secstore *secstore, int out, const char *args)
		if (templ && !hi_key_is_match(index->key, templ)) {
			continue;
		}
		write_key(index->key, out);
		write_key(out, index->key);
		index = index->next;
	}
	char end[] = "end\n";


@@ 44,16 44,36 @@ cmd_query(struct hi_secstore *secstore, int out, const char *args)
	return 0;
}

static int
cmd_add(struct hi_secstore *secstore, int out, const char *args)
{
	struct hi_key *key = hi_key_parse(args);
	if (!key) {
		char error[] = "error invalid key template\n";
		write(out, error, sizeof(error) - 1);
		return 1;
	}
	if (himitsu_secstore_add_key(secstore, key) != 0) {
		char error[] = "error adding key to store failed\n";
		write(out, error, sizeof(error) - 1);
		return 1;
	}
	write_key(out, key);
	return 0;
}

struct {
	char *name;
	int (*fn)(struct hi_secstore *, int, const char *);
} cmds[] = {
	{ "add", cmd_add },
	{ "query", cmd_query },
};

int himitsu_run_command(struct hi_secstore *secstore,
		const char *name, const char *args, int outfd)
{
	/* XXX: could use bsearch */
	for (size_t i = 0; i < sizeof(cmds) / sizeof(cmds[0]); ++i) {
		if (strcmp(cmds[i].name, name) == 0) {
			return cmds[i].fn(secstore, outfd, args);