/*
* Copyright (C) 2017 Aaron M. D. Jones <aaronmdjones@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "acopm-common.h"
#include <assert.h> /* assert() */
#include <errno.h> /* errno */
#include <getopt.h> /* extern char *optarg, struct option, getopt_long() */
#include <string.h> /* memset() */
#include "daemon.h" /* acopm_daemon_set() */
#include "options.h" /* decls for own functions */
#include "vars.h" /* global variable definitions */
#include "utils/log.h" /* acopm_log_set_mask(), ACOPM_LOGLVL_* */
static const struct option long_opts[] = {
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'v' },
{ "daemon", no_argument, 0, 'D' },
{ "config", required_argument, 0, 'c' },
{ "logfile", required_argument, 0, 'L' },
{ "logmask", required_argument, 0, 'l' },
{ NULL, 0, 0, '\0' }
};
static inline void acopm_attr_inline
acopm_usage_info(void)
{
static const char *const name = PACKAGE_TARNAME;
(void) fprintf(stderr, "Usage: %s [-hv] [-D] [-c file] [-L file] [-l mask]\n\n", name);
(void) fprintf(stderr, "\t--help / -h: display this usage information and exit\n");
(void) fprintf(stderr, "\t--version / -v: display program version and exit\n");
(void) fprintf(stderr, "\t--daemon / -D: daemonise after connecting to server\n");
(void) fprintf(stderr, "\t--config / -c: configuration file to use\n");
(void) fprintf(stderr, "\t--logfile / -L: set value for logfile (overrides config file)\n");
(void) fprintf(stderr, "\t--logmask / -l: set value for logmask (overrides config file)\n");
}
static inline void acopm_attr_inline
acopm_version_info(void)
{
#ifdef PACKAGE_VERSION
(void) fprintf(stderr, "This is %s version %s", PACKAGE_NAME, PACKAGE_VERSION);
# ifdef PACKAGE_URL
(void) fprintf(stderr, " <%s>", PACKAGE_URL);
# endif
(void) fprintf(stderr, "\n");
#endif
#ifdef BUILD_WITH_DEBUG
static const char *const debug = "Yes";
#else
static const char *const debug = "No";
#endif
#ifdef BUILD_WITH_MBEDTLS
static const char *const tls = "Yes";
#else
static const char *const tls = "No";
#endif
(void) fprintf(stderr, " Debug Build: %s\n", debug);
(void) fprintf(stderr, " TLS Support: %s\n", tls);
}
static inline bool acopm_attr_inline
acopm_parse_logmask(void)
{
char *endptr = NULL;
const unsigned long int mask = strtoul(optarg, &endptr, 0);
assert(endptr != NULL);
if (! endptr || *endptr)
{
(void) fprintf(stderr, "%s: invalid value for log mask (at '%s')",
__func__, ((endptr) ? endptr : "unknown"));
return false;
}
if (mask > ACOPM_LOGLVL_RANGE_MAX)
{
(void) fprintf(stderr, "%s: invalid value for log mask (too large)\n", __func__);
return false;
}
(void) acopm_log_set_mask((unsigned int) mask);
return true;
}
enum acopm_opt_parse_result
acopm_parse_options(const int argc, char **const restrict argv)
{
(void) memset(config_file, 0x00, sizeof config_file);
for (;;)
{
const int c = getopt_long(argc, argv, "hvDc:L:l:", long_opts, NULL);
if (c == -1)
break;
switch (c)
{
case 'h':
(void) acopm_usage_info();
return ACOPM_OPT_PARSE_EXIT_SUCCESS;
case 'v':
(void) acopm_version_info();
return ACOPM_OPT_PARSE_EXIT_SUCCESS;
case 'D':
(void) acopm_daemon_set();
break;
case 'c':
if (! acopm_strset_buf(config_file, optarg))
(void) acopm_log_warning("%s: config file '%s' too long, truncated",
__func__, optarg);
break;
case 'L':
if (! acopm_log_set_file(optarg))
return ACOPM_OPT_PARSE_EXIT_FAILURE;
break;
case 'l':
if (! acopm_parse_logmask())
return ACOPM_OPT_PARSE_EXIT_FAILURE;
break;
}
}
return ACOPM_OPT_PARSE_CONTINUE;
}