2
0
mirror of git://github.com/lxc/lxc synced 2025-09-01 13:31:38 +00:00

arguments: non-functional changes

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
This commit is contained in:
Christian Brauner
2017-08-25 07:17:20 +02:00
parent 77b0073a8c
commit ea0eb48b3c
2 changed files with 74 additions and 54 deletions

View File

@@ -21,26 +21,26 @@
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <limits.h>
#include <string.h> #include <string.h>
#include <ctype.h> /* for isprint() */ #include <unistd.h>
#include <errno.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h>
#include "arguments.h" #include "arguments.h"
#include "utils.h" #include "utils.h"
#include "version.h" #include "version.h"
/*---------------------------------------------------------------------------*/ static int build_shortopts(const struct option *a_options, char *a_shortopts,
static int build_shortopts(const struct option *a_options, size_t a_size)
char *a_shortopts, size_t a_size)
{ {
const struct option *opt;
size_t i = 0; size_t i = 0;
const struct option *opt;
if (!a_options || !a_shortopts || !a_size) if (!a_options || !a_shortopts || !a_size)
return -1; return -1;
@@ -79,12 +79,11 @@ static int build_shortopts(const struct option *a_options,
return 0; return 0;
is2big: is2big:
errno = E2BIG; errno = E2BIG;
return -1; return -1;
} }
/*---------------------------------------------------------------------------*/
static void print_usage(const struct option longopts[], static void print_usage(const struct option longopts[],
const struct lxc_arguments *a_args) const struct lxc_arguments *a_args)
@@ -96,8 +95,9 @@ static void print_usage(const struct option longopts[],
for (opt = longopts, i = 1; opt->name; opt++, i++) { for (opt = longopts, i = 1; opt->name; opt++, i++) {
int j; int j;
char *uppername = strdup(opt->name); char *uppername;
uppername = strdup(opt->name);
if (!uppername) if (!uppername)
exit(-ENOMEM); exit(-ENOMEM);
@@ -129,7 +129,8 @@ static void print_usage(const struct option longopts[],
exit(0); exit(0);
} }
static void print_version() { static void print_version()
{
printf("%s\n", LXC_VERSION); printf("%s\n", LXC_VERSION);
exit(0); exit(0);
} }
@@ -164,13 +165,14 @@ static int lxc_arguments_lxcpath_add(struct lxc_arguments *args,
{ {
if (args->lxcpath_additional != -1 && if (args->lxcpath_additional != -1 &&
args->lxcpath_cnt > args->lxcpath_additional) { args->lxcpath_cnt > args->lxcpath_additional) {
fprintf(stderr, "This command only accepts %d -P,--lxcpath arguments\n", fprintf(stderr,
"This command only accepts %d -P,--lxcpath arguments\n",
args->lxcpath_additional + 1); args->lxcpath_additional + 1);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
args->lxcpath = realloc(args->lxcpath, (args->lxcpath_cnt + 1) * args->lxcpath = realloc(
sizeof(args->lxcpath[0])); args->lxcpath, (args->lxcpath_cnt + 1) * sizeof(args->lxcpath[0]));
if (args->lxcpath == NULL) { if (args->lxcpath == NULL) {
lxc_error(args, "no memory"); lxc_error(args, "no memory");
return -ENOMEM; return -ENOMEM;
@@ -179,11 +181,11 @@ static int lxc_arguments_lxcpath_add(struct lxc_arguments *args,
return 0; return 0;
} }
extern int lxc_arguments_parse(struct lxc_arguments *args, extern int lxc_arguments_parse(struct lxc_arguments *args, int argc,
int argc, char * const argv[]) char *const argv[])
{ {
int ret = 0;
char shortopts[256]; char shortopts[256];
int ret = 0;
ret = build_shortopts(args->options, shortopts, sizeof(shortopts)); ret = build_shortopts(args->options, shortopts, sizeof(shortopts));
if (ret < 0) { if (ret < 0) {
@@ -192,28 +194,43 @@ extern int lxc_arguments_parse(struct lxc_arguments *args,
return ret; return ret;
} }
while (1) { while (true) {
int c, index = 0; int c;
int index = 0;
c = getopt_long(argc, argv, shortopts, args->options, &index); c = getopt_long(argc, argv, shortopts, args->options, &index);
if (c == -1) if (c == -1)
break; break;
switch (c) { switch (c) {
case 'n': args->name = optarg; break; case 'n':
case 'o': args->log_file = optarg; break; args->name = optarg;
case 'l': args->log_priority = optarg; break; break;
case 'q': args->quiet = 1; break; case 'o':
case OPT_RCFILE: args->rcfile = optarg; break; args->log_file = optarg;
break;
case 'l':
args->log_priority = optarg;
break;
case 'q':
args->quiet = 1;
break;
case OPT_RCFILE:
args->rcfile = optarg;
break;
case 'P': case 'P':
remove_trailing_slashes(optarg); remove_trailing_slashes(optarg);
ret = lxc_arguments_lxcpath_add(args, optarg); ret = lxc_arguments_lxcpath_add(args, optarg);
if (ret < 0) if (ret < 0)
return ret; return ret;
break; break;
case OPT_USAGE: print_usage(args->options, args); case OPT_USAGE:
case OPT_VERSION: print_version(); print_usage(args->options, args);
case '?': print_help(args, 1); case OPT_VERSION:
case 'h': print_help(args, 0); print_version();
case '?':
print_help(args, 1);
case 'h':
print_help(args, 0);
default: default:
if (args->parser) { if (args->parser) {
ret = args->parser(args, c, optarg); ret = args->parser(args, c, optarg);
@@ -231,7 +248,8 @@ extern int lxc_arguments_parse(struct lxc_arguments *args,
/* If no lxcpaths were given, use default */ /* If no lxcpaths were given, use default */
if (!args->lxcpath_cnt) { if (!args->lxcpath_cnt) {
ret = lxc_arguments_lxcpath_add(args, lxc_global_config_value("lxc.lxcpath")); ret = lxc_arguments_lxcpath_add(
args, lxc_global_config_value("lxc.lxcpath"));
if (ret < 0) if (ret < 0)
return ret; return ret;
} }

View File

@@ -32,14 +32,14 @@
struct lxc_arguments; struct lxc_arguments;
typedef int (*lxc_arguments_parser_t) (struct lxc_arguments *, int, char*); typedef int (*lxc_arguments_parser_t)(struct lxc_arguments *, int, char *);
typedef int (*lxc_arguments_checker_t) (const struct lxc_arguments *); typedef int (*lxc_arguments_checker_t)(const struct lxc_arguments *);
struct lxc_arguments { struct lxc_arguments {
const char *help; const char *help;
void(*helpfn)(const struct lxc_arguments *); void (*helpfn)(const struct lxc_arguments *);
const char *progname; const char *progname;
const struct option* options; const struct option *options;
lxc_arguments_parser_t parser; lxc_arguments_parser_t parser;
lxc_arguments_checker_t checker; lxc_arguments_checker_t checker;
@@ -143,29 +143,31 @@ struct lxc_arguments {
void *data; void *data;
}; };
#define LXC_COMMON_OPTIONS \ #define LXC_COMMON_OPTIONS \
{"name", required_argument, 0, 'n'}, \ { "name", required_argument, 0, 'n' }, \
{"help", no_argument, 0, 'h'}, \ { "help", no_argument, 0, 'h' }, \
{"usage", no_argument, 0, OPT_USAGE}, \ { "usage", no_argument, 0, OPT_USAGE }, \
{"version", no_argument, 0, OPT_VERSION}, \ { "version", no_argument, 0, OPT_VERSION }, \
{"quiet", no_argument, 0, 'q'}, \ { "quiet", no_argument, 0, 'q' }, \
{"logfile", required_argument, 0, 'o'}, \ { "logfile", required_argument, 0, 'o' }, \
{"logpriority", required_argument, 0, 'l'}, \ { "logpriority", required_argument, 0, 'l' }, \
{"lxcpath", required_argument, 0, 'P'}, \ { "lxcpath", required_argument, 0, 'P' }, \
{"rcfile", required_argument, 0, OPT_RCFILE}, \ { "rcfile", required_argument, 0, OPT_RCFILE }, \
{0, 0, 0, 0} { 0, 0, 0, 0 }
/* option keys for long only options */ /* option keys for long only options */
#define OPT_USAGE 0x1000 #define OPT_USAGE 0x1000
#define OPT_VERSION OPT_USAGE-1 #define OPT_VERSION OPT_USAGE - 1
#define OPT_RCFILE OPT_USAGE-2 #define OPT_RCFILE OPT_USAGE - 2
extern int lxc_arguments_parse(struct lxc_arguments *args, extern int lxc_arguments_parse(struct lxc_arguments *args, int argc,
int argc, char *const argv[]); char *const argv[]);
extern int lxc_arguments_str_to_int(struct lxc_arguments *args, const char *str); extern int lxc_arguments_str_to_int(struct lxc_arguments *args,
const char *str);
#define lxc_error(arg, fmt, args...) if (!(arg)->quiet) \ #define lxc_error(arg, fmt, args...) \
fprintf(stderr, "%s: " fmt "\n", (arg)->progname, ## args) if (!(arg)->quiet) \
fprintf(stderr, "%s: " fmt "\n", (arg)->progname, ##args)
#endif #endif /* __LXC_ARGUMENTS_H */