2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
24 * Registered commands.
26 command_t cmds
[MAX_COMMANDS
];
31 static int active
= 0;
34 * number of registered commands
36 static int registered
= 0;
44 * Global options used by all subcommands
46 struct option command_opts
[MAX_COMMANDS
> MAX_OPTIONS ?
: MAX_OPTIONS
];
49 * Global optstring used by all subcommands
51 char command_optstring
[(MAX_COMMANDS
> MAX_OPTIONS ?
: MAX_OPTIONS
) * 3];
54 * Build command_opts/command_optstr for the active command
56 static void build_opts()
60 memset(command_opts
, 0, sizeof(command_opts
));
61 memset(command_optstring
, 0, sizeof(command_optstring
));
62 if (active
== help_idx
)
64 for (i
= 0; cmds
[i
].cmd
; i
++)
66 command_opts
[i
].name
= cmds
[i
].cmd
;
67 command_opts
[i
].val
= cmds
[i
].op
;
68 command_optstring
[i
] = cmds
[i
].op
;
73 for (i
= 0; cmds
[active
].options
[i
].name
; i
++)
75 command_opts
[i
].name
= cmds
[active
].options
[i
].name
;
76 command_opts
[i
].has_arg
= cmds
[active
].options
[i
].arg
;
77 command_opts
[i
].val
= cmds
[active
].options
[i
].op
;
78 command_optstring
[pos
++] = cmds
[active
].options
[i
].op
;
79 switch (cmds
[active
].options
[i
].arg
)
81 case optional_argument
:
82 command_optstring
[pos
++] = ':';
84 case required_argument
:
85 command_optstring
[pos
++] = ':';
98 void command_register(command_t command
)
100 cmds
[registered
++] = command
;
104 * Print usage text, with an optional error
106 int command_usage(char *error
)
115 fprintf(out
, "Error: %s\n", error
);
117 fprintf(out
, "strongSwan %s PKI tool\n", VERSION
);
118 fprintf(out
, "usage:\n");
119 if (active
== help_idx
)
121 for (i
= 0; cmds
[i
].cmd
; i
++)
123 snprintf(buf
, sizeof(buf
), "--%s (-%c)", cmds
[i
].cmd
, cmds
[i
].op
);
124 fprintf(out
, " pki %-14s %s\n", buf
, cmds
[i
].description
);
129 for (i
= 0; cmds
[active
].line
[i
]; i
++)
133 fprintf(out
, " pki --%s %s\n",
134 cmds
[active
].cmd
, cmds
[active
].line
[i
]);
138 fprintf(out
, " %s\n", cmds
[active
].line
[i
]);
141 for (i
= 0; cmds
[active
].options
[i
].name
; i
++)
143 snprintf(buf
, sizeof(buf
), "--%s (-%c)",
144 cmds
[active
].options
[i
].name
, cmds
[active
].options
[i
].op
);
145 fprintf(out
, " %-15s %s\n",
146 buf
, cmds
[active
].options
[i
].desc
);
149 return error
!= NULL
;
154 * Show usage information
156 static int help(int argc
, char *argv
[])
158 return command_usage(NULL
);
164 int command_dispatch(int argc
, char *argv
[])
168 active
= help_idx
= registered
;
169 command_register((command_t
){help
, 'h', "help", "show usage information"});
172 op
= getopt_long(argc
, argv
, command_optstring
, command_opts
, NULL
);
173 for (i
= 0; cmds
[i
].cmd
; i
++)
175 if (cmds
[i
].op
== op
)
179 return cmds
[i
].call(argc
, argv
);
182 return command_usage("invalid operation");