splitted PKI tool to a file per command
[strongswan.git] / src / pki / command.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "command.h"
17
18
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdio.h>
22
23 /**
24 * Registered commands.
25 */
26 command_t cmds[CMD_MAX];
27
28 /**
29 * Global options used by all subcommands
30 */
31 struct option command_opts[CMD_MAX > MAX_OPTIONS ?: MAX_OPTIONS];
32
33 /**
34 * Build long_opts for a specific command
35 */
36 static void build_opts(command_type_t cmd)
37 {
38 int i;
39
40 memset(command_opts, 0, sizeof(command_opts));
41 if (cmd == CMD_HELP)
42 {
43 for (i = 0; i < CMD_MAX; i++)
44 {
45 command_opts[i].name = cmds[i].cmd;
46 command_opts[i].val = cmds[i].op;
47 }
48 }
49 else
50 {
51 for (i = 0; cmds[cmd].options[i].name; i++)
52 {
53 command_opts[i].name = cmds[cmd].options[i].name;
54 command_opts[i].has_arg = cmds[cmd].options[i].arg;
55 command_opts[i].val = cmds[cmd].options[i].op;
56 }
57 }
58 }
59
60 /**
61 * Register a command
62 */
63 void command_register(command_type_t type, command_t command)
64 {
65 cmds[type] = command;
66 }
67
68 /**
69 * Print usage text, with an optional error
70 */
71 int command_usage(command_type_t cmd, char *error)
72 {
73 FILE *out = stdout;
74 int i;
75
76 if (error)
77 {
78 out = stderr;
79 fprintf(out, "Error: %s\n", error);
80 }
81 fprintf(out, "strongSwan %s PKI tool\n", VERSION);
82 fprintf(out, "usage:\n");
83 if (cmd == CMD_HELP)
84 {
85 for (i = 0; i < CMD_MAX; i++)
86 {
87 fprintf(out, " pki --%-6s %s\n", cmds[i].cmd, cmds[i].description);
88 }
89 }
90 else
91 {
92 for (i = 0; cmds[cmd].line[i]; i++)
93 {
94 if (i == 0)
95 {
96 fprintf(out, " pki --%s %s\n", cmds[cmd].cmd, cmds[cmd].line[i]);
97 }
98 else
99 {
100 fprintf(out, " %s\n", cmds[cmd].line[i]);
101 }
102 }
103 for (i = 0; cmds[cmd].options[i].name; i++)
104 {
105 fprintf(out, " --%-8s %s\n",
106 cmds[cmd].options[i].name, cmds[cmd].options[i].desc);
107 }
108 }
109 return error != NULL;
110 }
111
112
113
114 /**
115 * Show usage information
116 */
117 static int help(int argc, char *argv[])
118 {
119 return command_usage(CMD_HELP, NULL);
120 }
121
122 /**
123 * Dispatch commands.
124 */
125 int command_dispatch(int argc, char *argv[])
126 {
127 int op, i;
128
129 command_register(CMD_HELP, (command_t) {
130 help, 'h', "help", "show usage information"});
131 build_opts(CMD_HELP);
132 op = getopt_long(argc, argv, "", command_opts, NULL);
133 for (i = 0; i < CMD_MAX; i++)
134 {
135 if (cmds[i].op == op)
136 {
137 build_opts(i);
138 return cmds[i].call(argc, argv);
139 }
140 }
141 return command_usage(CMD_HELP, "invalid operation");
142 }
143