aboutsummaryrefslogtreecommitdiffstats
path: root/src/pki/command.c
blob: 9b02596dbdcd7a64d2ef74589c07a834f675259b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright (C) 2009 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "command.h"


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

/**
 * Registered commands.
 */
command_t cmds[CMD_MAX];

/**
 * Global options used by all subcommands
 */
struct option command_opts[CMD_MAX > MAX_OPTIONS ?: MAX_OPTIONS];

/**
 * Build long_opts for a specific command
 */
static void build_opts(command_type_t cmd)
{
	int i;

	memset(command_opts, 0, sizeof(command_opts));
	if (cmd == CMD_HELP)
	{
		for (i = 0; i < CMD_MAX; i++)
		{
			command_opts[i].name = cmds[i].cmd;
			command_opts[i].val = cmds[i].op;
		}
	}
	else
	{
		for (i = 0; cmds[cmd].options[i].name; i++)
		{
			command_opts[i].name = cmds[cmd].options[i].name;
			command_opts[i].has_arg = cmds[cmd].options[i].arg;
			command_opts[i].val = cmds[cmd].options[i].op;
		}
	}
}

/**
 * Register a command
 */
void command_register(command_type_t type, command_t command)
{
	cmds[type] = command;
}

/**
 * Print usage text, with an optional error
 */
int command_usage(command_type_t cmd, char *error)
{
	FILE *out = stdout;
	int i;

	if (error)
	{
		out = stderr;
		fprintf(out, "Error: %s\n", error);
	}
	fprintf(out, "strongSwan %s PKI tool\n", VERSION);
	fprintf(out, "usage:\n");
	if (cmd == CMD_HELP)
	{
		for (i = 0; i < CMD_MAX; i++)
		{
			fprintf(out, "  pki --%-6s %s\n", cmds[i].cmd, cmds[i].description);
		}
	}
	else
	{
		for (i = 0; cmds[cmd].line[i]; i++)
		{
			if (i == 0)
			{
				fprintf(out, "  pki --%s %s\n", cmds[cmd].cmd, cmds[cmd].line[i]);
			}
			else
			{
				fprintf(out, "               %s\n", cmds[cmd].line[i]);
			}
		}
		for (i = 0; cmds[cmd].options[i].name; i++)
		{
			fprintf(out, "        --%-8s %s\n",
					cmds[cmd].options[i].name, cmds[cmd].options[i].desc);
		}
	}
	return error != NULL;
}



/**
 * Show usage information
 */
static int help(int argc, char *argv[])
{
	return command_usage(CMD_HELP, NULL);
}

/**
 * Dispatch commands.
 */
int command_dispatch(int argc, char *argv[])
{
	int op, i;

	command_register(CMD_HELP, (command_t) {
					 help, 'h', "help", "show usage information"});
	build_opts(CMD_HELP);
	op = getopt_long(argc, argv, "", command_opts, NULL);
	for (i = 0; i < CMD_MAX; i++)
	{
		if (cmds[i].op == op)
		{
			build_opts(i);
			return cmds[i].call(argc, argv);
		}
	}
	return command_usage(CMD_HELP, "invalid operation");
}