diff --git a/.gitignore b/.gitignore index 1c162a3..9bd7f7a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -bee_getopt.o +mx_getopt.o mx_flock.o mx_log.o mx_util.o diff --git a/Makefile b/Makefile index 1f03389..15a7de3 100644 --- a/Makefile +++ b/Makefile @@ -201,17 +201,17 @@ mxq_job.h += mxq_group.h mxqd.h += mxqd.h -### bee_getopt.h ------------------------------------------------------ +### mx_getopt.h ------------------------------------------------------ -bee_getopt.h += bee_getopt.h +mx_getopt.h += mx_getopt.h ######################################################################## -### bee_getopt.o ------------------------------------------------------- +### mx_getopt.o ------------------------------------------------------- -bee_getopt.o: $(bee_getopt.h) +mx_getopt.o: $(mx_getopt.h) -clean: CLEAN += bee_getopt.o +clean: CLEAN += mx_getopt.o ### mx_log.o ---------------------------------------------------------- @@ -249,7 +249,7 @@ clean: CLEAN += mxq_mysql.o mxqdump.o: $(mx_log.h) mxqdump.o: $(mxq_util.h) mxqdump.o: $(mxq_mysql.h) -mxqdump.o: $(bee_getopt.h) +mxqdump.o: $(mx_getopt.h) mxqdump.o: CFLAGS += $(CFLAGS_MYSQL) mxqdump.o: CFLAGS += $(CFLAGS_MXQ_MYSQL_DEFAULT_FILE) @@ -261,7 +261,7 @@ mxqkill.o: $(mx_log.h) mxqkill.o: $(mx_util.h) mxqkill.o: $(mxq_util.h) mxqkill.o: $(mxq_mysql.h) -mxqkill.o: $(bee_getopt.h) +mxqkill.o: $(mx_getopt.h) mxqkill.o: CFLAGS += $(CFLAGS_MYSQL) mxqkill.o: CFLAGS += $(CFLAGS_MXQ_MYSQL_DEFAULT_FILE) @@ -306,7 +306,7 @@ clean: CLEAN += mxq_job.o ### mxqd.o ------------------------------------------------------------- -mxqd.o: $(bee_getopt.h) +mxqd.o: $(mx_getopt.h) mxqd.o: $(mx_flock.h) mxqd.o: $(mx_util.h) mxqd.o: $(mx_log.h) @@ -323,7 +323,7 @@ clean: CLEAN += mxqd.o ### mxqsub.o ------------------------------------------------------- -mxqsub.o: $(bee_getopt.h) +mxqsub.o: $(mx_getopt.h) mxqsub.o: $(mxq_util.h) mxqsub.o: $(mx_util.h) mxqsub.o: $(mx_log.h) @@ -341,7 +341,7 @@ mxqd: mx_flock.o mxqd: mx_util.o mxqd: mx_log.o mxqd: mxq_log.o -mxqd: bee_getopt.o +mxqd: mx_getopt.o mxqd: mxq_group.o mxqd: mxq_job.o mxqd: mxq_util.o @@ -357,7 +357,7 @@ install:: mxqd ### mxqsub ------------------------------------------------------------ -mxqsub: bee_getopt.o +mxqsub: mx_getopt.o mxqsub: mxq_mysql.o mxqsub: mxq_util.o mxqsub: mx_util.o @@ -377,7 +377,7 @@ mxqdump: mx_log.o mxqdump: mxq_group.o mxqdump: mxq_mysql.o mxqdump: mxq_util.o -mxqdump: bee_getopt.o +mxqdump: mx_getopt.o mxqdump: LDLIBS += $(LDLIBS_MYSQL) build: mxqdump @@ -395,7 +395,7 @@ mxqkill: mxq_util.o mxqkill: mx_util.o mxqkill: mxq_group.o mxqkill: mxq_job.o -mxqkill: bee_getopt.o +mxqkill: mx_getopt.o mxqkill: LDLIBS += $(LDLIBS_MYSQL) build: mxqkill diff --git a/bee_getopt.h b/bee_getopt.h deleted file mode 100644 index 9961ad5..0000000 --- a/bee_getopt.h +++ /dev/null @@ -1,187 +0,0 @@ -/* -** beegetopt - parse options -** -** Copyright (C) 2009-2012 -** Marius Tolzmann -** and other bee developers -** -** This file is part of bee. -** -** bee is free software; you can redistribute it and/or modify -** it under the terms of the GNU General Public License as published by -** the Free Software Foundation, either version 3 of the License, or -** (at your option) any later version. -** -** This program is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with this program; if not, see . -*/ - -#ifndef BEE_GETOPT_H -#define BEE_GETOPT_H 1 - -#define BEE_TYPE_STRING 1 -#define BEE_TYPE_INTEGER 2 -#define BEE_TYPE_FLOAT 3 -#define BEE_TYPE_FLAG 4 -#define BEE_TYPE_NO 5 -#define BEE_TYPE_ENABLE 6 -#define BEE_TYPE_WITH 7 -#define BEE_TYPE_TOGGLE 8 -#define BEE_TYPE_COUNT 9 - -#define BEE_FLAG_SKIPUNKNOWN (1<<0) -#define BEE_FLAG_STOPONNOOPT (1<<1) -#define BEE_FLAG_STOPONUNKNOWN (1<<2) -#define BEE_FLAG_KEEPOPTIONEND (1<<3) - -#define BEE_OPT_LONG(name) .long_opt = (name) -#define BEE_OPT_SHORT(short) .short_opt = (short) -#define BEE_OPT_VALUE(v) .value = (v) -#define BEE_OPT_FLAG(f) .flag = (f) -#define BEE_OPT_TYPE(t) .type = (t) -#define BEE_OPT_OPTIONAL(args) .optional_args = (args) -#define BEE_OPT_REQUIRED(args) .required_args = (args) -#define _BEE_OPT_LEN(l) ._long_len = (l) - -#define BEE_GETOPT_END -1 -#define BEE_GETOPT_ERROR -2 -#define BEE_GETOPT_NOVALUE -3 -#define BEE_GETOPT_NOOPT -4 -#define BEE_GETOPT_AMBIGUOUS -5 -#define BEE_GETOPT_OPTUNKNOWN -6 -#define BEE_GETOPT_NOARG -7 -#define BEE_GETOPT_NOARGS -8 - -#define BEE_OPTION_DEFAULTS \ - BEE_OPT_LONG(NULL), \ - BEE_OPT_SHORT(0), \ - BEE_OPT_VALUE(BEE_GETOPT_NOVALUE), \ - BEE_OPT_FLAG(NULL), \ - BEE_OPT_TYPE(BEE_TYPE_FLAG), \ - BEE_OPT_OPTIONAL(0), \ - BEE_OPT_REQUIRED(0), \ - _BEE_OPT_LEN(0) - - -#define BEE_INIT_OPTION_DEFAULTS(opt) \ - (opt)->long_opt = NULL; \ - (opt)->short_opt = 0; \ - (opt)->value = BEE_GETOPT_NOVALUE; \ - (opt)->flag = NULL; \ - (opt)->type = BEE_TYPE_FLAG; \ - (opt)->optional_args = 0; \ - (opt)->required_args = 0; \ - (opt)->_long_len = 0 - - -#define BEE_INIT_OPTION_END(opt) BEE_INIT_OPTION_DEFAULTS((opt)) - - -#define BEE_OPTION_END { BEE_OPT_LONG(NULL), BEE_OPT_SHORT(0) } - -#define BEE_OPTION(...) { BEE_OPTION_DEFAULTS, ## __VA_ARGS__ } - -#define BEE_OPTION_NO_ARG(name, short, ...) \ - BEE_OPTION(BEE_OPT_LONG(name), \ - BEE_OPT_SHORT(short), \ - BEE_OPT_VALUE(short), \ - ## __VA_ARGS__) - -#define BEE_OPTION_REQUIRED_ARG(name, short, ...) \ - BEE_OPTION(BEE_OPT_LONG(name), \ - BEE_OPT_SHORT(short), \ - BEE_OPT_VALUE(short), \ - BEE_OPT_TYPE(BEE_TYPE_STRING), \ - BEE_OPT_REQUIRED(1), \ - ## __VA_ARGS__ ) - -#define BEE_OPTION_REQUIRED_ARGS(name, short, n, ...) \ - BEE_OPTION(BEE_OPT_LONG(name), \ - BEE_OPT_SHORT(short), \ - BEE_OPT_VALUE(short), \ - BEE_OPT_TYPE(BEE_TYPE_STRING), \ - BEE_OPT_REQUIRED(n), \ - ## __VA_ARGS__ ) - -#define BEE_OPTION_OPTIONAL_ARG(name, short, ...) \ - BEE_OPTION(BEE_OPT_LONG(name), \ - BEE_OPT_SHORT(short), \ - BEE_OPT_VALUE(short), \ - BEE_OPT_TYPE(BEE_TYPE_STRING), \ - BEE_OPT_OPTIONAL(1), \ - ## __VA_ARGS__ ) - -#define BEE_OPTION_OPTIONAL_ARGS(name, short, n, ...) \ - BEE_OPTION(BEE_OPT_LONG(name), \ - BEE_OPT_SHORT(short), \ - BEE_OPT_VALUE(short), \ - BEE_OPT_TYPE(BEE_TYPE_STRING), \ - BEE_OPT_OPTIONAL(n), \ - ## __VA_ARGS__ ) - -#define BEE_OPTION_ARGS(name, short, opt, req, ...) \ - BEE_OPTION(BEE_OPT_LONG(name), \ - BEE_OPT_SHORT(short), \ - BEE_OPT_VALUE(short), \ - BEE_OPT_TYPE(BEE_TYPE_STRING), \ - BEE_OPT_OPTIONAL(opt), \ - BEE_OPT_REQUIRED(req), \ - ## __VA_ARGS__ ) - -#define BEE_GETOPT_FINISH(optctl, argc, argv) \ - do { \ - (argv) = &(optctl).argv[(optctl).optind]; \ - (argc) = (optctl).argc - (optctl).optind; \ - } while(0) - -struct bee_option { - char *long_opt; - char short_opt; - - int value; - int *flag; - - int type; - - int optional_args; - int required_args; - - int _long_len; -}; - -struct bee_getopt_ctl { - int optind; - - char *program; - - char *optarg; - - int optargc; - char **optargv; - - int argc; - char **argv; - - struct bee_option *options; - - int _argc; - int _optcnt; - char *_unhandled_shortopts; - - int flags; -}; - -void bee_getopt_pop_current_argument(struct bee_getopt_ctl *optctl); - -int bee_getopt_init(struct bee_getopt_ctl *ctl, int argc, char **argv, struct bee_option *optv); -int bee_getopt_long(struct bee_getopt_ctl *optctl, int *optindex); -int bee_getopt(struct bee_getopt_ctl *optctl, int *optindex); - -void bee_getopt_print_quoted(char *s); - -#endif diff --git a/bee_getopt.c b/mx_getopt.c similarity index 79% rename from bee_getopt.c rename to mx_getopt.c index 5231bf2..3271663 100644 --- a/bee_getopt.c +++ b/mx_getopt.c @@ -43,9 +43,9 @@ #include #include -#include "bee_getopt.h" +#include "mx_getopt.h" -int bee_getopt_init(struct bee_getopt_ctl *ctl, int argc, char **argv, struct bee_option *optv) +int mx_getopt_init(struct mx_getopt_ctl *ctl, int argc, char **argv, struct mx_option *optv) { int err = 0; @@ -67,8 +67,8 @@ int bee_getopt_init(struct bee_getopt_ctl *ctl, int argc, char **argv, struct be for(ctl->_optcnt=0; optv[ctl->_optcnt].long_opt || optv[ctl->_optcnt].short_opt; ctl->_optcnt++) { if(optv[ctl->_optcnt].long_opt) optv[ctl->_optcnt]._long_len = strlen(optv[ctl->_optcnt].long_opt); - if(optv[ctl->_optcnt].value < 0 && optv[ctl->_optcnt].value != BEE_GETOPT_NOVALUE) { - fprintf(stderr, "*** bee_getopt_init() ERROR: negative value (%d) set for opt %d (long: %s, short: %c)\n", ctl->_optcnt, optv[ctl->_optcnt].value, optv[ctl->_optcnt].long_opt, optv[ctl->_optcnt].short_opt); + if(optv[ctl->_optcnt].value < 0 && optv[ctl->_optcnt].value != MX_GETOPT_NOVALUE) { + fprintf(stderr, "*** mx_getopt_init() ERROR: negative value (%d) set for opt %d (long: %s, short: %c)\n", ctl->_optcnt, optv[ctl->_optcnt].value, optv[ctl->_optcnt].long_opt, optv[ctl->_optcnt].short_opt); err++; } } @@ -81,7 +81,7 @@ int bee_getopt_init(struct bee_getopt_ctl *ctl, int argc, char **argv, struct be return err?0:1; } -static int find_long_option_by_name(struct bee_option *options, char *name, char **optarg) +static int find_long_option_by_name(struct mx_option *options, char *name, char **optarg) { int i = 0; @@ -114,10 +114,10 @@ static int find_long_option_by_name(struct bee_option *options, char *name, char } /* not match found */ - return BEE_GETOPT_OPTUNKNOWN; + return MX_GETOPT_OPTUNKNOWN; } -static int find_short_option(struct bee_option *options, char **name, char **optarg) +static int find_short_option(struct mx_option *options, char **name, char **optarg) { char short_opt; int i; @@ -144,7 +144,7 @@ static int find_short_option(struct bee_option *options, char **name, char **opt if (idx == -1) { /* reset unhandled options */ *name = NULL; - return BEE_GETOPT_OPTUNKNOWN; + return MX_GETOPT_OPTUNKNOWN; } /* skip this short option */ @@ -162,7 +162,7 @@ static int find_short_option(struct bee_option *options, char **name, char **opt return idx; } -static int find_long_option_by_subname(struct bee_option *options, char *name, char **optarg) +static int find_long_option_by_subname(struct mx_option *options, char *name, char **optarg) { int i; int l; @@ -197,7 +197,7 @@ static int find_long_option_by_subname(struct bee_option *options, char *name, c if (idx >= 0) { if (options[i].value != options[idx].value) - return BEE_GETOPT_AMBIGUOUS; + return MX_GETOPT_AMBIGUOUS; } else { idx = i; } @@ -205,7 +205,7 @@ static int find_long_option_by_subname(struct bee_option *options, char *name, c /* return error if option was not found */ if (idx == -1) - return BEE_GETOPT_OPTUNKNOWN; + return MX_GETOPT_OPTUNKNOWN; /* set optarg if assigned */ if (s && optarg) @@ -214,9 +214,9 @@ static int find_long_option_by_subname(struct bee_option *options, char *name, c return idx; } -static int handle_option(struct bee_getopt_ctl *ctl, const int index) +static int handle_option(struct mx_getopt_ctl *ctl, const int index) { - struct bee_option *o = &(ctl->options[index]); + struct mx_option *o = &(ctl->options[index]); int argc; ctl->optargc = 0; @@ -232,7 +232,7 @@ static int handle_option(struct bee_getopt_ctl *ctl, const int index) if (!ctl->optarg) { /* no optarg and no more arguments -> required argument missing */ if (ctl->optind == ctl->_argc) - return (o->required_args == 1)?BEE_GETOPT_NOARG:BEE_GETOPT_NOARGS; + return (o->required_args == 1)?MX_GETOPT_NOARG:MX_GETOPT_NOARGS; /* set optarg to this argument and skip it */ ctl->optarg = ctl->argv[ctl->optind++]; @@ -249,7 +249,7 @@ static int handle_option(struct bee_getopt_ctl *ctl, const int index) if (ctl->optind+argc > ctl->_argc) { /* on error: set optargc to number of args available */ ctl->optargc = ctl->optind - ctl->_argc; - return BEE_GETOPT_NOARGS; + return MX_GETOPT_NOARGS; } /* all required args are available */ @@ -299,7 +299,7 @@ static int handle_option(struct bee_getopt_ctl *ctl, const int index) return o->value; } -void bee_getopt_pop_current_argument(struct bee_getopt_ctl *optctl) +void mx_getopt_pop_current_argument(struct mx_getopt_ctl *optctl) { int i; @@ -316,13 +316,13 @@ void bee_getopt_pop_current_argument(struct bee_getopt_ctl *optctl) optctl->_argc--; } -void bee_getopt_pop_all_arguments(struct bee_getopt_ctl *optctl) +void mx_getopt_pop_all_arguments(struct mx_getopt_ctl *optctl) { while(optctl->optind < optctl->_argc) - bee_getopt_pop_current_argument(optctl); + mx_getopt_pop_current_argument(optctl); } -static int _bee_getopt_long(struct bee_getopt_ctl *optctl, int *optindex) +static int _mx_getopt_long(struct mx_getopt_ctl *optctl, int *optindex) { int this; @@ -332,7 +332,7 @@ static int _bee_getopt_long(struct bee_getopt_ctl *optctl, int *optindex) int idx; if(optctl->optind == optctl->_argc) - return BEE_GETOPT_END; + return MX_GETOPT_END; assert(optctl->optind < optctl->_argc); @@ -352,17 +352,17 @@ static int _bee_getopt_long(struct bee_getopt_ctl *optctl, int *optindex) /* not an option: pop arguement */ if(!maybe_option) { - bee_getopt_pop_current_argument(optctl); - return BEE_GETOPT_NOOPT; + mx_getopt_pop_current_argument(optctl); + return MX_GETOPT_NOOPT; } /* match & skip '--' and pop all remaining arguments */ if(maybe_long && (optctl->argv[this][2] == '\0')) { - if(!(optctl->flags & BEE_FLAG_KEEPOPTIONEND)) + if(!(optctl->flags & MX_FLAG_KEEPOPTIONEND)) optctl->optind++; - bee_getopt_pop_all_arguments(optctl); - return BEE_GETOPT_END; + mx_getopt_pop_all_arguments(optctl); + return MX_GETOPT_END; } /* match long option */ @@ -396,56 +396,56 @@ static int _bee_getopt_long(struct bee_getopt_ctl *optctl, int *optindex) return handle_option(optctl, idx); } -int bee_getopt_long(struct bee_getopt_ctl *optctl, int *optindex) +int mx_getopt_long(struct mx_getopt_ctl *optctl, int *optindex) { - return _bee_getopt_long(optctl, optindex); + return _mx_getopt_long(optctl, optindex); } -int bee_getopt(struct bee_getopt_ctl *optctl, int *optindex) +int mx_getopt(struct mx_getopt_ctl *optctl, int *optindex) { int opt; - while((opt = _bee_getopt_long(optctl, optindex)) != BEE_GETOPT_END) { + while((opt = _mx_getopt_long(optctl, optindex)) != MX_GETOPT_END) { switch(opt) { - case BEE_GETOPT_NOVALUE: - return BEE_GETOPT_NOVALUE; + case MX_GETOPT_NOVALUE: + return MX_GETOPT_NOVALUE; - case BEE_GETOPT_NOOPT: - if (optctl->flags & BEE_FLAG_STOPONNOOPT) { - bee_getopt_pop_all_arguments(optctl); - return BEE_GETOPT_END; + case MX_GETOPT_NOOPT: + if (optctl->flags & MX_FLAG_STOPONNOOPT) { + mx_getopt_pop_all_arguments(optctl); + return MX_GETOPT_END; } break; - case BEE_GETOPT_AMBIGUOUS: + case MX_GETOPT_AMBIGUOUS: if (optctl->program) fprintf(stderr, "%s: ", optctl->program); fprintf(stderr, "option '%s' is ambiguous.\n", optctl->argv[optctl->optind]); - return BEE_GETOPT_ERROR; + return MX_GETOPT_ERROR; - case BEE_GETOPT_OPTUNKNOWN: - if (optctl->flags & BEE_FLAG_STOPONUNKNOWN) { - bee_getopt_pop_all_arguments(optctl); - return BEE_GETOPT_END; + case MX_GETOPT_OPTUNKNOWN: + if (optctl->flags & MX_FLAG_STOPONUNKNOWN) { + mx_getopt_pop_all_arguments(optctl); + return MX_GETOPT_END; } - if (!(optctl->flags & BEE_FLAG_SKIPUNKNOWN)) { + if (!(optctl->flags & MX_FLAG_SKIPUNKNOWN)) { if (optctl->program) fprintf(stderr, "%s: ", optctl->program); fprintf(stderr, "unrecognized option '%s'.\n", optctl->argv[optctl->optind]); - return BEE_GETOPT_ERROR; + return MX_GETOPT_ERROR; } - bee_getopt_pop_current_argument(optctl); + mx_getopt_pop_current_argument(optctl); break; - case BEE_GETOPT_NOARG: - case BEE_GETOPT_NOARGS: + case MX_GETOPT_NOARG: + case MX_GETOPT_NOARGS: if (optctl->program) fprintf(stderr, "%s: ", optctl->program); if (optctl->options[*optindex].long_opt) fprintf(stderr, "option '--%s' requires an argument.\n", optctl->options[*optindex].long_opt); else fprintf(stderr, "option '-%c' requires an argument.\n", optctl->options[*optindex].short_opt); - return BEE_GETOPT_ERROR; + return MX_GETOPT_ERROR; default: assert(opt >= 0); @@ -456,7 +456,7 @@ int bee_getopt(struct bee_getopt_ctl *optctl, int *optindex) return opt; } -void bee_getopt_print_quoted(char *s) +void mx_getopt_print_quoted(char *s) { putchar('\''); while (*s) { diff --git a/mx_getopt.h b/mx_getopt.h new file mode 100644 index 0000000..fe9ef25 --- /dev/null +++ b/mx_getopt.h @@ -0,0 +1,187 @@ +/* +** beegetopt - parse options +** +** Copyright (C) 2009-2012 +** Marius Tolzmann +** and other bee developers +** +** This file is part of bee. +** +** bee is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation, either version 3 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, see . +*/ + +#ifndef MX_GETOPT_H +#define MX_GETOPT_H 1 + +#define MX_TYPE_STRING 1 +#define MX_TYPE_INTEGER 2 +#define MX_TYPE_FLOAT 3 +#define MX_TYPE_FLAG 4 +#define MX_TYPE_NO 5 +#define MX_TYPE_ENABLE 6 +#define MX_TYPE_WITH 7 +#define MX_TYPE_TOGGLE 8 +#define MX_TYPE_COUNT 9 + +#define MX_FLAG_SKIPUNKNOWN (1<<0) +#define MX_FLAG_STOPONNOOPT (1<<1) +#define MX_FLAG_STOPONUNKNOWN (1<<2) +#define MX_FLAG_KEEPOPTIONEND (1<<3) + +#define MX_OPT_LONG(name) .long_opt = (name) +#define MX_OPT_SHORT(short) .short_opt = (short) +#define MX_OPT_VALUE(v) .value = (v) +#define MX_OPT_FLAG(f) .flag = (f) +#define MX_OPT_TYPE(t) .type = (t) +#define MX_OPT_OPTIONAL(args) .optional_args = (args) +#define MX_OPT_REQUIRED(args) .required_args = (args) +#define _MX_OPT_LEN(l) ._long_len = (l) + +#define MX_GETOPT_END -1 +#define MX_GETOPT_ERROR -2 +#define MX_GETOPT_NOVALUE -3 +#define MX_GETOPT_NOOPT -4 +#define MX_GETOPT_AMBIGUOUS -5 +#define MX_GETOPT_OPTUNKNOWN -6 +#define MX_GETOPT_NOARG -7 +#define MX_GETOPT_NOARGS -8 + +#define MX_OPTION_DEFAULTS \ + MX_OPT_LONG(NULL), \ + MX_OPT_SHORT(0), \ + MX_OPT_VALUE(MX_GETOPT_NOVALUE), \ + MX_OPT_FLAG(NULL), \ + MX_OPT_TYPE(MX_TYPE_FLAG), \ + MX_OPT_OPTIONAL(0), \ + MX_OPT_REQUIRED(0), \ + _MX_OPT_LEN(0) + + +#define MX_INIT_OPTION_DEFAULTS(opt) \ + (opt)->long_opt = NULL; \ + (opt)->short_opt = 0; \ + (opt)->value = MX_GETOPT_NOVALUE; \ + (opt)->flag = NULL; \ + (opt)->type = MX_TYPE_FLAG; \ + (opt)->optional_args = 0; \ + (opt)->required_args = 0; \ + (opt)->_long_len = 0 + + +#define MX_INIT_OPTION_END(opt) MX_INIT_OPTION_DEFAULTS((opt)) + + +#define MX_OPTION_END { MX_OPT_LONG(NULL), MX_OPT_SHORT(0) } + +#define MX_OPTION(...) { MX_OPTION_DEFAULTS, ## __VA_ARGS__ } + +#define MX_OPTION_NO_ARG(name, short, ...) \ + MX_OPTION(MX_OPT_LONG(name), \ + MX_OPT_SHORT(short), \ + MX_OPT_VALUE(short), \ + ## __VA_ARGS__) + +#define MX_OPTION_REQUIRED_ARG(name, short, ...) \ + MX_OPTION(MX_OPT_LONG(name), \ + MX_OPT_SHORT(short), \ + MX_OPT_VALUE(short), \ + MX_OPT_TYPE(MX_TYPE_STRING), \ + MX_OPT_REQUIRED(1), \ + ## __VA_ARGS__ ) + +#define MX_OPTION_REQUIRED_ARGS(name, short, n, ...) \ + MX_OPTION(MX_OPT_LONG(name), \ + MX_OPT_SHORT(short), \ + MX_OPT_VALUE(short), \ + MX_OPT_TYPE(MX_TYPE_STRING), \ + MX_OPT_REQUIRED(n), \ + ## __VA_ARGS__ ) + +#define MX_OPTION_OPTIONAL_ARG(name, short, ...) \ + MX_OPTION(MX_OPT_LONG(name), \ + MX_OPT_SHORT(short), \ + MX_OPT_VALUE(short), \ + MX_OPT_TYPE(MX_TYPE_STRING), \ + MX_OPT_OPTIONAL(1), \ + ## __VA_ARGS__ ) + +#define MX_OPTION_OPTIONAL_ARGS(name, short, n, ...) \ + MX_OPTION(MX_OPT_LONG(name), \ + MX_OPT_SHORT(short), \ + MX_OPT_VALUE(short), \ + MX_OPT_TYPE(MX_TYPE_STRING), \ + MX_OPT_OPTIONAL(n), \ + ## __VA_ARGS__ ) + +#define MX_OPTION_ARGS(name, short, opt, req, ...) \ + MX_OPTION(MX_OPT_LONG(name), \ + MX_OPT_SHORT(short), \ + MX_OPT_VALUE(short), \ + MX_OPT_TYPE(MX_TYPE_STRING), \ + MX_OPT_OPTIONAL(opt), \ + MX_OPT_REQUIRED(req), \ + ## __VA_ARGS__ ) + +#define MX_GETOPT_FINISH(optctl, argc, argv) \ + do { \ + (argv) = &(optctl).argv[(optctl).optind]; \ + (argc) = (optctl).argc - (optctl).optind; \ + } while(0) + +struct mx_option { + char *long_opt; + char short_opt; + + int value; + int *flag; + + int type; + + int optional_args; + int required_args; + + int _long_len; +}; + +struct mx_getopt_ctl { + int optind; + + char *program; + + char *optarg; + + int optargc; + char **optargv; + + int argc; + char **argv; + + struct mx_option *options; + + int _argc; + int _optcnt; + char *_unhandled_shortopts; + + int flags; +}; + +void mx_getopt_pop_current_argument(struct mx_getopt_ctl *optctl); + +int mx_getopt_init(struct mx_getopt_ctl *ctl, int argc, char **argv, struct mx_option *optv); +int mx_getopt_long(struct mx_getopt_ctl *optctl, int *optindex); +int mx_getopt(struct mx_getopt_ctl *optctl, int *optindex); + +void mx_getopt_print_quoted(char *s); + +#endif diff --git a/mx_log.c b/mx_log.c index 628838f..8ed9588 100644 --- a/mx_log.c +++ b/mx_log.c @@ -8,6 +8,8 @@ #define MX_LOG_GET (MX_LOG_NONE-1) +int mx_log_errno = 0; + int mx_log_level_set(int level) { static int loglevel = MX_LOG_WARNING; @@ -30,7 +32,7 @@ int mx_log_level_set(int level) return oldloglevel; } - return -(errno=EINVAL); + return -(mx_log_errno=EINVAL); } int mx_log_level_mxlog_to_syslog(int level) @@ -38,7 +40,7 @@ int mx_log_level_mxlog_to_syslog(int level) level = MX_LOG_MXLOG_TO_SYSLOG(level); if (level < LOG_EMERG || level > LOG_DEBUG) - return -(errno=EINVAL); + return -(mx_log_errno=EINVAL); return level; } @@ -46,7 +48,7 @@ int mx_log_level_mxlog_to_syslog(int level) int mx_log_level_syslog_to_mxlog(int level) { if (level < LOG_EMERG || level > LOG_DEBUG) - return -(errno=EINVAL); + return -(mx_log_errno=EINVAL); level = MX_LOG_SYSLOG_TO_MXLOG(level); @@ -71,7 +73,7 @@ int mx_log_printf(const char *fmt, ...) va_end(ap); if (len == -1) - return -(errno=ENOMEM); + return -(mx_log_errno=ENOMEM); if (mx_log_print) return mx_log_print(msg, len); @@ -124,8 +126,9 @@ static int log_log(int level, int loglevel, char *file, unsigned long line, cons break; case MX_LOG_DEBUG: prefix = "DEBUG: "; + break; default: - return -(errno=EINVAL); + return -(mx_log_errno=EINVAL); } if (loglevel >= MX_LOG_DEBUG) @@ -141,18 +144,23 @@ int mx_log_do(int level, char *file, unsigned long line, const char *func, const char *msg = NULL; va_list ap; int res; + int preserved_errno = errno; loglevel = mx_log_level_get(); - if (level > loglevel) + if (level > loglevel) { + errno = preserved_errno; return 0; + } va_start(ap, fmt); len = vasprintf(&msg, fmt, ap); va_end(ap); - if (len == -1) - return -(errno=ENOMEM); + if (len == -1) { + errno = preserved_errno; + return -(mx_log_errno=ENOMEM); + } if (mx_log_log) res = mx_log_log(level, loglevel, file, line, func, msg); @@ -160,6 +168,8 @@ int mx_log_do(int level, char *file, unsigned long line, const char *func, const res = log_log(level, loglevel, file, line, func, msg); mx_free_null(msg); + + errno = preserved_errno; return res; } diff --git a/mx_log.h b/mx_log.h index 1889119..48c6098 100644 --- a/mx_log.h +++ b/mx_log.h @@ -6,9 +6,9 @@ #define MX_LOG_SYSLOG_TO_MXLOG(x) ((x)+1) #define MX_LOG_MXLOG_TO_SYSLOG(x) ((x)-1) -#ifndef mx_free_null -#include -#define mx_free_null(a) do { free((a)); (a) = NULL; } while(0) +#if !defined mx_free_null +# include +# define mx_free_null(a) do { free((a)); (a) = NULL; } while(0) #endif #define MX_LOG_NONE 0 diff --git a/mxq_job.c b/mxq_job.c index 3ab248c..2cef544 100644 --- a/mxq_job.c +++ b/mxq_job.c @@ -20,6 +20,7 @@ #define MXQ_JOB_FIELDS "job_id, " \ "job_status, " \ + "job_flags, " \ "job_priority, " \ "group_id, " \ "job_workdir, " \ @@ -55,6 +56,7 @@ enum mxq_job_columns { MXQ_JOB_COL_JOB_ID, MXQ_JOB_COL_JOB_STATUS, + MXQ_JOB_COL_JOB_FLAGS, MXQ_JOB_COL_JOB_PRIORITY, MXQ_JOB_COL_GROUP_ID, MXQ_JOB_COL_JOB_WORKDIR, @@ -95,6 +97,7 @@ static inline int mxq_job_bind_results(MYSQL_BIND *bind, struct mxq_job *j) MXQ_MYSQL_BIND_UINT64(bind, MXQ_JOB_COL_JOB_ID, &j->job_id); MXQ_MYSQL_BIND_UINT16(bind, MXQ_JOB_COL_JOB_STATUS, &j->job_status); + MXQ_MYSQL_BIND_UINT64(bind, MXQ_JOB_COL_JOB_FLAGS, &j->job_flags); MXQ_MYSQL_BIND_UINT16(bind, MXQ_JOB_COL_JOB_PRIORITY, &j->job_priority); MXQ_MYSQL_BIND_UINT64(bind, MXQ_JOB_COL_GROUP_ID, &j->group_id); diff --git a/mxq_job.h b/mxq_job.h index 86187a8..2b21328 100644 --- a/mxq_job.h +++ b/mxq_job.h @@ -8,7 +8,8 @@ struct mxq_job { uint64_t job_id; - uint16_t job_status; + uint16_t job_status; + uint64_t job_flags; uint16_t job_priority; uint64_t group_id; diff --git a/mxqd.c b/mxqd.c index 0c90eb2..f6985dc 100644 --- a/mxqd.c +++ b/mxqd.c @@ -24,7 +24,7 @@ #include #include -#include "bee_getopt.h" +#include "mx_getopt.h" #include "mx_flock.h" #include "mx_util.h" @@ -209,20 +209,20 @@ int server_init(struct mxq_server *server, int argc, char *argv[]) unsigned long memory_max = 0; int i; - struct bee_getopt_ctl optctl; - struct bee_option opts[] = { - BEE_OPTION_NO_ARG("help", 'h'), - BEE_OPTION_NO_ARG("version", 'V'), - BEE_OPTION_NO_ARG("daemonize", 1), - BEE_OPTION_NO_ARG("no-log", 3), - BEE_OPTION_REQUIRED_ARG("pid-file", 2), - BEE_OPTION_REQUIRED_ARG("slots", 'j'), - BEE_OPTION_REQUIRED_ARG("memory", 'm'), - BEE_OPTION_REQUIRED_ARG("max-memory-per-slot", 'x'), - BEE_OPTION_REQUIRED_ARG("server-id", 'N'), - BEE_OPTION_OPTIONAL_ARG("mysql-default-file", 'M'), - BEE_OPTION_OPTIONAL_ARG("mysql-default-group", 'S'), - BEE_OPTION_END + struct mx_getopt_ctl optctl; + struct mx_option opts[] = { + MX_OPTION_NO_ARG("help", 'h'), + MX_OPTION_NO_ARG("version", 'V'), + MX_OPTION_NO_ARG("daemonize", 1), + MX_OPTION_NO_ARG("no-log", 3), + MX_OPTION_REQUIRED_ARG("pid-file", 2), + MX_OPTION_REQUIRED_ARG("slots", 'j'), + MX_OPTION_REQUIRED_ARG("memory", 'm'), + MX_OPTION_REQUIRED_ARG("max-memory-per-slot", 'x'), + MX_OPTION_REQUIRED_ARG("server-id", 'N'), + MX_OPTION_OPTIONAL_ARG("mysql-default-file", 'M'), + MX_OPTION_OPTIONAL_ARG("mysql-default-group", 'S'), + MX_OPTION_END }; arg_server_id = "main"; @@ -235,13 +235,13 @@ int server_init(struct mxq_server *server, int argc, char *argv[]) if (!arg_mysql_default_file) arg_mysql_default_file = MYSQL_DEFAULT_FILE; - bee_getopt_init(&optctl, argc-1, &argv[1], opts); + mx_getopt_init(&optctl, argc-1, &argv[1], opts); - optctl.flags = BEE_FLAG_STOPONUNKNOWN|BEE_FLAG_STOPONNOOPT; -// optctl.flags = BEE_FLAG_STOPONUNKNOWN; + optctl.flags = MX_FLAG_STOPONUNKNOWN|MX_FLAG_STOPONNOOPT; +// optctl.flags = MX_FLAG_STOPONUNKNOWN; - while ((opt=bee_getopt(&optctl, &i)) != BEE_GETOPT_END) { - if (opt == BEE_GETOPT_ERROR) { + while ((opt=mx_getopt(&optctl, &i)) != MX_GETOPT_END) { + if (opt == MX_GETOPT_ERROR) { exit(EX_USAGE); } @@ -305,7 +305,7 @@ int server_init(struct mxq_server *server, int argc, char *argv[]) } } - BEE_GETOPT_FINISH(optctl, argc, argv); + MX_GETOPT_FINISH(optctl, argc, argv); if (arg_daemonize && arg_nolog) { mx_log_err("Error while using conflicting options --daemonize and --no-log at once."); diff --git a/mxqdump.c b/mxqdump.c index fdda33c..7389ec8 100644 --- a/mxqdump.c +++ b/mxqdump.c @@ -12,7 +12,7 @@ #include "mxq_util.h" #include "mxq_mysql.h" -#include "bee_getopt.h" +#include "mx_getopt.h" #ifndef MXQ_VERSION #define MXQ_VERSION "0.00" @@ -76,14 +76,14 @@ int main(int argc, char *argv[]) char *arg_mysql_default_file; int opt; - struct bee_getopt_ctl optctl; - struct bee_option opts[] = { - BEE_OPTION_NO_ARG("help", 'h'), - BEE_OPTION_NO_ARG("version", 'V'), - - BEE_OPTION_OPTIONAL_ARG("mysql-default-file", 'M'), - BEE_OPTION_OPTIONAL_ARG("mysql-default-group", 'S'), - BEE_OPTION_END + struct mx_getopt_ctl optctl; + struct mx_option opts[] = { + MX_OPTION_NO_ARG("help", 'h'), + MX_OPTION_NO_ARG("version", 'V'), + + MX_OPTION_OPTIONAL_ARG("mysql-default-file", 'M'), + MX_OPTION_OPTIONAL_ARG("mysql-default-group", 'S'), + MX_OPTION_END }; arg_mysql_default_group = getenv("MXQ_MYSQL_DEFAULT_GROUP"); @@ -95,11 +95,11 @@ int main(int argc, char *argv[]) arg_mysql_default_file = MYSQL_DEFAULT_FILE; - bee_getopt_init(&optctl, argc-1, &argv[1], opts); - optctl.flags = BEE_FLAG_STOPONUNKNOWN|BEE_FLAG_STOPONNOOPT; + mx_getopt_init(&optctl, argc-1, &argv[1], opts); + optctl.flags = MX_FLAG_STOPONUNKNOWN|MX_FLAG_STOPONNOOPT; - while ((opt=bee_getopt(&optctl, &i)) != BEE_GETOPT_END) { - if (opt == BEE_GETOPT_ERROR) { + while ((opt=mx_getopt(&optctl, &i)) != MX_GETOPT_END) { + if (opt == MX_GETOPT_ERROR) { exit(EX_USAGE); } @@ -122,7 +122,7 @@ int main(int argc, char *argv[]) } } - BEE_GETOPT_FINISH(optctl, argc, argv); + MX_GETOPT_FINISH(optctl, argc, argv); mmysql.default_file = arg_mysql_default_file; mmysql.default_group = arg_mysql_default_group; diff --git a/mxqkill.c b/mxqkill.c index ff76a1d..8093644 100644 --- a/mxqkill.c +++ b/mxqkill.c @@ -20,7 +20,7 @@ #include "mx_util.h" #include "mxq_util.h" #include "mxq_mysql.h" -#include "bee_getopt.h" +#include "mx_getopt.h" #ifndef MXQ_VERSION #define MXQ_VERSION "0.00" @@ -95,16 +95,16 @@ int main(int argc, char *argv[]) int i; int opt; - struct bee_getopt_ctl optctl; - struct bee_option opts[] = { - BEE_OPTION_NO_ARG("help", 'h'), - BEE_OPTION_NO_ARG("version", 'V'), + struct mx_getopt_ctl optctl; + struct mx_option opts[] = { + MX_OPTION_NO_ARG("help", 'h'), + MX_OPTION_NO_ARG("version", 'V'), - BEE_OPTION_REQUIRED_ARG("group-id", 'g'), + MX_OPTION_REQUIRED_ARG("group-id", 'g'), - BEE_OPTION_OPTIONAL_ARG("mysql-default-file", 'M'), - BEE_OPTION_OPTIONAL_ARG("mysql-default-group", 'S'), - BEE_OPTION_END + MX_OPTION_OPTIONAL_ARG("mysql-default-file", 'M'), + MX_OPTION_OPTIONAL_ARG("mysql-default-group", 'S'), + MX_OPTION_END }; arg_mysql_default_group = getenv("MXQ_MYSQL_DEFAULT_GROUP"); @@ -119,11 +119,11 @@ int main(int argc, char *argv[]) mx_log_level_set(MX_LOG_INFO); - bee_getopt_init(&optctl, argc-1, &argv[1], opts); - optctl.flags = BEE_FLAG_STOPONUNKNOWN|BEE_FLAG_STOPONNOOPT; + mx_getopt_init(&optctl, argc-1, &argv[1], opts); + optctl.flags = MX_FLAG_STOPONUNKNOWN|MX_FLAG_STOPONNOOPT; - while ((opt=bee_getopt(&optctl, &i)) != BEE_GETOPT_END) { - if (opt == BEE_GETOPT_ERROR) { + while ((opt=mx_getopt(&optctl, &i)) != MX_GETOPT_END) { + if (opt == MX_GETOPT_ERROR) { exit(EX_USAGE); } @@ -155,7 +155,7 @@ int main(int argc, char *argv[]) } } - BEE_GETOPT_FINISH(optctl, argc, argv); + MX_GETOPT_FINISH(optctl, argc, argv); mmysql.default_file = arg_mysql_default_file; mmysql.default_group = arg_mysql_default_group; diff --git a/mxqsub.c b/mxqsub.c index f857d82..7bdfc7b 100644 --- a/mxqsub.c +++ b/mxqsub.c @@ -24,7 +24,7 @@ #include "mxq_mysql.h" #include "mxq_util.h" #include "mx_util.h" -#include "bee_getopt.h" +#include "mx_getopt.h" #define MXQ_TASK_JOB_FORCE_APPEND (1<<0) @@ -426,38 +426,38 @@ int main(int argc, char *argv[]) struct group *grp; int opt; - struct bee_getopt_ctl optctl; - struct bee_option opts[] = { - BEE_OPTION_NO_ARG("help", 'h'), - BEE_OPTION_NO_ARG("version", 'V'), + struct mx_getopt_ctl optctl; + struct mx_option opts[] = { + MX_OPTION_NO_ARG("help", 'h'), + MX_OPTION_NO_ARG("version", 'V'), - BEE_OPTION_REQUIRED_ARG("group_id", 1), - BEE_OPTION_REQUIRED_ARG("group_priority", 2), - BEE_OPTION_REQUIRED_ARG("group-id", 3), - BEE_OPTION_REQUIRED_ARG("time", 4), + MX_OPTION_REQUIRED_ARG("group_id", 1), + MX_OPTION_REQUIRED_ARG("group_priority", 2), + MX_OPTION_REQUIRED_ARG("group-id", 3), + MX_OPTION_REQUIRED_ARG("time", 4), - BEE_OPTION_NO_ARG("debug", 5), + MX_OPTION_NO_ARG("debug", 5), - BEE_OPTION_REQUIRED_ARG("group-name", 'N'), - BEE_OPTION_REQUIRED_ARG("group-priority", 'P'), + MX_OPTION_REQUIRED_ARG("group-name", 'N'), + MX_OPTION_REQUIRED_ARG("group-priority", 'P'), - BEE_OPTION_REQUIRED_ARG("command-alias", 'a'), + MX_OPTION_REQUIRED_ARG("command-alias", 'a'), - BEE_OPTION_REQUIRED_ARG("workdir", 'w'), - BEE_OPTION_REQUIRED_ARG("stdout", 'o'), - BEE_OPTION_REQUIRED_ARG("stderr", 'e'), - BEE_OPTION_REQUIRED_ARG("umask", 'u'), - BEE_OPTION_REQUIRED_ARG("priority", 'p'), + MX_OPTION_REQUIRED_ARG("workdir", 'w'), + MX_OPTION_REQUIRED_ARG("stdout", 'o'), + MX_OPTION_REQUIRED_ARG("stderr", 'e'), + MX_OPTION_REQUIRED_ARG("umask", 'u'), + MX_OPTION_REQUIRED_ARG("priority", 'p'), - BEE_OPTION_REQUIRED_ARG("threads", 'j'), - BEE_OPTION_REQUIRED_ARG("memory", 'm'), - BEE_OPTION_REQUIRED_ARG("runtime", 't'), + MX_OPTION_REQUIRED_ARG("threads", 'j'), + MX_OPTION_REQUIRED_ARG("memory", 'm'), + MX_OPTION_REQUIRED_ARG("runtime", 't'), - BEE_OPTION_REQUIRED_ARG("define", 'D'), + MX_OPTION_REQUIRED_ARG("define", 'D'), - BEE_OPTION_OPTIONAL_ARG("mysql-default-file", 'M'), - BEE_OPTION_OPTIONAL_ARG("mysql-default-group", 'S'), - BEE_OPTION_END + MX_OPTION_OPTIONAL_ARG("mysql-default-file", 'M'), + MX_OPTION_OPTIONAL_ARG("mysql-default-group", 'S'), + MX_OPTION_END }; @@ -492,11 +492,11 @@ int main(int argc, char *argv[]) /******************************************************************/ - bee_getopt_init(&optctl, argc-1, &argv[1], opts); - optctl.flags = BEE_FLAG_STOPONUNKNOWN|BEE_FLAG_STOPONNOOPT; + mx_getopt_init(&optctl, argc-1, &argv[1], opts); + optctl.flags = MX_FLAG_STOPONUNKNOWN|MX_FLAG_STOPONNOOPT; - while ((opt=bee_getopt(&optctl, &i)) != BEE_GETOPT_END) { - if (opt == BEE_GETOPT_ERROR) { + while ((opt=mx_getopt(&optctl, &i)) != MX_GETOPT_END) { + if (opt == MX_GETOPT_ERROR) { exit(EX_USAGE); } @@ -613,7 +613,7 @@ int main(int argc, char *argv[]) } } - BEE_GETOPT_FINISH(optctl, argc, argv); + MX_GETOPT_FINISH(optctl, argc, argv); if (argc < 1) { print_usage(); exit(EX_USAGE); diff --git a/mysql/create_tables b/mysql/create_tables index 79223d9..03701cb 100644 --- a/mysql/create_tables +++ b/mysql/create_tables @@ -1,5 +1,7 @@ - +ALTER TABLE mxq_job + ADD COLUMN job_flags INT8 UNSIGNED NOT NULL DEFAULT 0 + AFTER job_status; ALTER TABLE mxq_group ADD COLUMN group_jobs_cancelled INT8 UNSIGNED NOT NULL DEFAULT 0 @@ -49,6 +51,7 @@ DROP TABLE mxq_job; CREATE TABLE IF NOT EXISTS mxq_job ( job_id INT8 UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, job_status INT2 UNSIGNED NOT NULL DEFAULT 0, + job_flags INT8 UNSIGNED NOT NULL DEFAULT 0, job_priority INT2 UNSIGNED NOT NULL DEFAULT 127, group_id INT8 UNSIGNED NOT NULL,