]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/arguments.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <ctype.h> /* for isprint() */
31 #include <sys/types.h>
34 #include "arguments.h"
38 /*---------------------------------------------------------------------------*/
39 static int build_shortopts(const struct option
*a_options
,
40 char *a_shortopts
, size_t a_size
)
42 const struct option
*opt
;
45 if (!a_options
|| !a_shortopts
|| !a_size
)
48 for (opt
= a_options
; opt
->name
; opt
++) {
50 if (!isascii(opt
->val
))
54 a_shortopts
[i
++] = opt
->val
;
58 if (opt
->has_arg
== no_argument
)
62 a_shortopts
[i
++] = ':';
66 if (opt
->has_arg
== required_argument
)
70 a_shortopts
[i
++] = ':';
76 a_shortopts
[i
] = '\0';
87 /*---------------------------------------------------------------------------*/
88 static void print_usage(const struct option longopts
[],
89 const struct lxc_arguments
*a_args
)
93 const struct option
*opt
;
95 fprintf(stderr
, "Usage: %s ", a_args
->progname
);
97 for (opt
= longopts
, i
= 1; opt
->name
; opt
++, i
++) {
99 char *uppername
= strdup(opt
->name
);
104 for (j
= 0; uppername
[j
]; j
++)
105 uppername
[j
] = toupper(uppername
[j
]);
107 fprintf(stderr
, "[");
109 if (isprint(opt
->val
))
110 fprintf(stderr
, "-%c|", opt
->val
);
112 fprintf(stderr
, "--%s", opt
->name
);
114 if (opt
->has_arg
== required_argument
)
115 fprintf(stderr
, "=%s", uppername
);
117 if (opt
->has_arg
== optional_argument
)
118 fprintf(stderr
, "[=%s]", uppername
);
120 fprintf(stderr
, "] ");
123 fprintf(stderr
, "\n\t");
128 fprintf(stderr
, "\n");
132 static void print_version() {
133 printf("%s\n", LXC_VERSION
);
137 static void print_help(const struct lxc_arguments
*args
, int code
)
143 -o, --logfile=FILE Output log to FILE instead of stderr\n\
144 -l, --logpriority=LEVEL Set log priority to LEVEL\n\
145 -q, --quiet Don't produce any output\n\
146 -P, --lxcpath=PATH Use specified container path\n\
147 -?, --help Give this help list\n\
148 --usage Give a short usage message\n\
149 --version Print the version number\n\
151 Mandatory or optional arguments to long options are also mandatory or optional\n\
152 for any corresponding short options.\n\
154 See the %s man page for further information.\n\n",
155 args
->progname
, args
->help
, args
->progname
);
162 static int lxc_arguments_lxcpath_add(struct lxc_arguments
*args
,
165 if (args
->lxcpath_additional
!= -1 &&
166 args
->lxcpath_cnt
> args
->lxcpath_additional
) {
167 fprintf(stderr
, "This command only accepts %d -P,--lxcpath arguments\n",
168 args
->lxcpath_additional
+ 1);
172 args
->lxcpath
= realloc(args
->lxcpath
, (args
->lxcpath_cnt
+ 1) *
173 sizeof(args
->lxcpath
[0]));
174 if (args
->lxcpath
== NULL
) {
175 lxc_error(args
, "no memory");
178 args
->lxcpath
[args
->lxcpath_cnt
++] = lxcpath
;
182 extern int lxc_arguments_parse(struct lxc_arguments
*args
,
183 int argc
, char * const argv
[])
188 ret
= build_shortopts(args
->options
, shortopts
, sizeof(shortopts
));
190 lxc_error(args
, "build_shortopts() failed : %s",
198 c
= getopt_long(argc
, argv
, shortopts
, args
->options
, &index
);
202 case 'n': args
->name
= optarg
; break;
203 case 'o': args
->log_file
= optarg
; break;
204 case 'l': args
->log_priority
= optarg
; break;
205 case 'q': args
->quiet
= 1; break;
207 remove_trailing_slashes(optarg
);
208 ret
= lxc_arguments_lxcpath_add(args
, optarg
);
212 case OPT_USAGE
: print_usage(args
->options
, args
);
213 case OPT_VERSION
: print_version();
214 case '?': print_help(args
, 1);
215 case 'h': print_help(args
, 0);
218 ret
= args
->parser(args
, c
, optarg
);
226 * Reclaim the remaining command arguments
228 args
->argv
= &argv
[optind
];
229 args
->argc
= argc
- optind
;
231 /* If no lxcpaths were given, use default */
232 if (!args
->lxcpath_cnt
) {
233 ret
= lxc_arguments_lxcpath_add(args
, lxc_global_config_value("lxc.lxcpath"));
238 /* Check the command options */
240 if (!args
->name
&& strcmp(args
->progname
, "lxc-autostart") != 0) {
241 lxc_error(args
, "missing container name, use --name option");
246 ret
= args
->checker(args
);
249 lxc_error(args
, "could not parse command line");
253 int lxc_arguments_str_to_int(struct lxc_arguments
*args
, const char *str
)
259 val
= strtol(str
, &endptr
, 10);
261 lxc_error(args
, "invalid statefd '%s' : %m", str
);
266 lxc_error(args
, "invalid digit for statefd '%s'", str
);