2
0
mirror of git://github.com/lxc/lxc synced 2025-08-31 08:19:32 +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
* 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 <stdlib.h>
#include <limits.h>
#include <string.h>
#include <ctype.h> /* for isprint() */
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "arguments.h"
#include "utils.h"
#include "version.h"
/*---------------------------------------------------------------------------*/
static int build_shortopts(const struct option *a_options,
char *a_shortopts, size_t a_size)
static int build_shortopts(const struct option *a_options, char *a_shortopts,
size_t a_size)
{
const struct option *opt;
size_t i = 0;
const struct option *opt;
if (!a_options || !a_shortopts || !a_size)
return -1;
@@ -79,12 +79,11 @@ static int build_shortopts(const struct option *a_options,
return 0;
is2big:
is2big:
errno = E2BIG;
return -1;
}
/*---------------------------------------------------------------------------*/
static void print_usage(const struct option longopts[],
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++) {
int j;
char *uppername = strdup(opt->name);
char *uppername;
uppername = strdup(opt->name);
if (!uppername)
exit(-ENOMEM);
@@ -129,7 +129,8 @@ static void print_usage(const struct option longopts[],
exit(0);
}
static void print_version() {
static void print_version()
{
printf("%s\n", LXC_VERSION);
exit(0);
}
@@ -164,13 +165,14 @@ static int lxc_arguments_lxcpath_add(struct lxc_arguments *args,
{
if (args->lxcpath_additional != -1 &&
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);
exit(EXIT_FAILURE);
}
args->lxcpath = realloc(args->lxcpath, (args->lxcpath_cnt + 1) *
sizeof(args->lxcpath[0]));
args->lxcpath = realloc(
args->lxcpath, (args->lxcpath_cnt + 1) * sizeof(args->lxcpath[0]));
if (args->lxcpath == NULL) {
lxc_error(args, "no memory");
return -ENOMEM;
@@ -179,11 +181,11 @@ static int lxc_arguments_lxcpath_add(struct lxc_arguments *args,
return 0;
}
extern int lxc_arguments_parse(struct lxc_arguments *args,
int argc, char * const argv[])
extern int lxc_arguments_parse(struct lxc_arguments *args, int argc,
char *const argv[])
{
int ret = 0;
char shortopts[256];
int ret = 0;
ret = build_shortopts(args->options, shortopts, sizeof(shortopts));
if (ret < 0) {
@@ -192,28 +194,43 @@ extern int lxc_arguments_parse(struct lxc_arguments *args,
return ret;
}
while (1) {
int c, index = 0;
while (true) {
int c;
int index = 0;
c = getopt_long(argc, argv, shortopts, args->options, &index);
if (c == -1)
break;
switch (c) {
case 'n': args->name = optarg; break;
case 'o': 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 'n':
args->name = optarg;
break;
case 'o':
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':
remove_trailing_slashes(optarg);
ret = lxc_arguments_lxcpath_add(args, optarg);
if (ret < 0)
return ret;
break;
case OPT_USAGE: print_usage(args->options, args);
case OPT_VERSION: print_version();
case '?': print_help(args, 1);
case 'h': print_help(args, 0);
case OPT_USAGE:
print_usage(args->options, args);
case OPT_VERSION:
print_version();
case '?':
print_help(args, 1);
case 'h':
print_help(args, 0);
default:
if (args->parser) {
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 (!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)
return ret;
}

View File

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