2
* lxc: linux Container library
4
* (C) Copyright IBM Corp. 2007, 2008
7
* Daniel Lezcano <dlezcano at fr.ibm.com>
8
* Michel Normand <normand at fr.ibm.com>
10
* This library is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU Lesser General Public
12
* License as published by the Free Software Foundation; either
13
* version 2.1 of the License, or (at your option) any later version.
15
* This library is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
* Lesser General Public License for more details.
20
* You should have received a copy of the GNU Lesser General Public
21
* License along with this library; if not, write to the Free Software
22
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28
#include <ctype.h> /* for isprint() */
31
#include <sys/types.h>
34
#include "arguments.h"
36
/*---------------------------------------------------------------------------*/
37
static int build_shortopts(const struct option *a_options,
38
char *a_shortopts, size_t a_size)
40
const struct option *opt;
43
if (!a_options || !a_shortopts || !a_size)
46
for (opt = a_options; opt->name; opt++) {
48
if (!isascii(opt->val))
52
a_shortopts[i++] = opt->val;
56
if (opt->has_arg == no_argument)
60
a_shortopts[i++] = ':';
64
if (opt->has_arg == required_argument)
68
a_shortopts[i++] = ':';
74
a_shortopts[i] = '\0';
85
/*---------------------------------------------------------------------------*/
86
static void print_usage(const struct option longopts[],
87
const struct lxc_arguments *a_args)
91
const struct option *opt;
93
fprintf(stderr, "Usage: %s ", a_args->progname);
95
for (opt = longopts, i = 1; opt->name; opt++, i++) {
97
char *uppername = strdup(opt->name);
99
for (j = 0; uppername[j]; j++)
100
uppername[j] = toupper(uppername[j]);
102
fprintf(stderr, "[");
104
if (isprint(opt->val))
105
fprintf(stderr, "-%c|", opt->val);
107
fprintf(stderr, "--%s", opt->name);
109
if (opt->has_arg == required_argument)
110
fprintf(stderr, "=%s", uppername);
112
if (opt->has_arg == optional_argument)
113
fprintf(stderr, "[=%s]", uppername);
115
fprintf(stderr, "] ");
118
fprintf(stderr, "\n\t");
123
fprintf(stderr, "\n");
127
static void print_help(const struct lxc_arguments *args, int code)
133
-o, --logfile=FILE Output log to FILE instead of stderr\n\
134
-l, --logpriority=LEVEL Set log priority to LEVEL\n\
135
-q, --quiet Don't produce any output\n\
136
-?, --help Give this help list\n\
137
--usage Give a short usage message\n\
139
Mandatory or optional arguments to long options are also mandatory or optional\n\
140
for any corresponding short options.\n\
142
See the %s man page for further information.\n\n",
143
args->progname, args->help, args->progname);
148
extern int lxc_arguments_parse(struct lxc_arguments *args,
149
int argc, char * const argv[])
154
ret = build_shortopts(args->options, shortopts, sizeof(shortopts));
156
lxc_error(args, "build_shortopts() failed : %s",
164
c = getopt_long(argc, argv, shortopts, args->options, &index);
168
case 'n': args->name = optarg; break;
169
case 'o': args->log_file = optarg; break;
170
case 'l': args->log_priority = optarg; break;
171
case 'c': args->console = optarg; break;
172
case 'q': args->quiet = 1; break;
173
case OPT_USAGE: print_usage(args->options, args);
174
case '?': print_help(args, 1);
175
case 'h': print_help(args, 0);
178
ret = args->parser(args, c, optarg);
186
* Reclaim the remaining command arguments
188
args->argv = &argv[optind];
189
args->argc = argc - optind;
191
/* Check the command options */
194
lxc_error(args, "missing container name, use --name option");
199
ret = args->checker(args);
202
lxc_error(args, "could not parse command line");
206
extern char **lxc_arguments_dup(const char *file, struct lxc_arguments *args)
209
int opt, nbargs = args->argc + 2;
214
argv = malloc((nbargs + 1) * sizeof(*argv));
220
argv[nbargs++] = strdup(file);
223
argv[nbargs++] = "--quiet";
225
argv[nbargs++] = "--";
227
for (opt = 0; opt < args->argc; opt++)
228
argv[nbargs++] = strdup(args->argv[opt]);
235
int lxc_arguments_str_to_int(struct lxc_arguments *args, const char *str)
241
val = strtol(str, &endptr, 10);
243
lxc_error(args, "invalid statefd '%s' : %m", str);
248
lxc_error(args, "invalid digit for statefd '%s'", str);