Annotation of acopm/src/options.c, Revision 1.1.1.1
1.1 bountyht 1: /*
2: * Copyright (C) 2017 Aaron M. D. Jones <aaronmdjones@gmail.com>
3: *
4: * Redistribution and use in source and binary forms, with or without
5: * modification, are permitted provided that the following
6: * conditions are met:
7: *
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: *
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: *
15: * 3. Neither the name of the copyright holder nor the names of its
16: * contributors may be used to endorse or promote products derived from
17: * this software without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22: * PARTICULAR PURPOSE ARE DISCLAIMED.
23: *
24: * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
25: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31: * POSSIBILITY OF SUCH DAMAGE.
32: */
33:
34: #include "acopm-common.h"
35:
36: #include <assert.h> /* assert() */
37: #include <errno.h> /* errno */
38: #include <getopt.h> /* extern char *optarg, struct option, getopt_long() */
39: #include <string.h> /* memset() */
40:
41: #include "daemon.h" /* acopm_daemon_set() */
42: #include "options.h" /* decls for own functions */
43: #include "vars.h" /* global variable definitions */
44:
45: #include "utils/log.h" /* acopm_log_set_mask(), ACOPM_LOGLVL_* */
46:
47: static const struct option long_opts[] = {
48: { "help", no_argument, 0, 'h' },
49: { "version", no_argument, 0, 'v' },
50: { "daemon", no_argument, 0, 'D' },
51: { "config", required_argument, 0, 'c' },
52: { "logfile", required_argument, 0, 'L' },
53: { "logmask", required_argument, 0, 'l' },
54: { NULL, 0, 0, '\0' }
55: };
56:
57: static inline void acopm_attr_inline
58: acopm_usage_info(void)
59: {
60: static const char *const name = PACKAGE_TARNAME;
61:
62: (void) fprintf(stderr, "Usage: %s [-hv] [-D] [-c file] [-L file] [-l mask]\n\n", name);
63: (void) fprintf(stderr, "\t--help / -h: display this usage information and exit\n");
64: (void) fprintf(stderr, "\t--version / -v: display program version and exit\n");
65: (void) fprintf(stderr, "\t--daemon / -D: daemonise after connecting to server\n");
66: (void) fprintf(stderr, "\t--config / -c: configuration file to use\n");
67: (void) fprintf(stderr, "\t--logfile / -L: set value for logfile (overrides config file)\n");
68: (void) fprintf(stderr, "\t--logmask / -l: set value for logmask (overrides config file)\n");
69: }
70:
71: static inline void acopm_attr_inline
72: acopm_version_info(void)
73: {
74: #ifdef PACKAGE_VERSION
75: (void) fprintf(stderr, "This is %s version %s", PACKAGE_NAME, PACKAGE_VERSION);
76: # ifdef PACKAGE_URL
77: (void) fprintf(stderr, " <%s>", PACKAGE_URL);
78: # endif
79: (void) fprintf(stderr, "\n");
80: #endif
81:
82: #ifdef BUILD_WITH_DEBUG
83: static const char *const debug = "Yes";
84: #else
85: static const char *const debug = "No";
86: #endif
87:
88: #ifdef BUILD_WITH_MBEDTLS
89: static const char *const tls = "Yes";
90: #else
91: static const char *const tls = "No";
92: #endif
93:
94: (void) fprintf(stderr, " Debug Build: %s\n", debug);
95: (void) fprintf(stderr, " TLS Support: %s\n", tls);
96: }
97:
98: static inline bool acopm_attr_inline
99: acopm_parse_logmask(void)
100: {
101: char *endptr = NULL;
102: const unsigned long int mask = strtoul(optarg, &endptr, 0);
103:
104: assert(endptr != NULL);
105:
106: if (! endptr || *endptr)
107: {
108: (void) fprintf(stderr, "%s: invalid value for log mask (at '%s')",
109: __func__, ((endptr) ? endptr : "unknown"));
110: return false;
111: }
112: if (mask > ACOPM_LOGLVL_RANGE_MAX)
113: {
114: (void) fprintf(stderr, "%s: invalid value for log mask (too large)\n", __func__);
115: return false;
116: }
117:
118: (void) acopm_log_set_mask((unsigned int) mask);
119: return true;
120: }
121:
122: enum acopm_opt_parse_result
123: acopm_parse_options(const int argc, char **const restrict argv)
124: {
125: (void) memset(config_file, 0x00, sizeof config_file);
126:
127: for (;;)
128: {
129: const int c = getopt_long(argc, argv, "hvDc:L:l:", long_opts, NULL);
130:
131: if (c == -1)
132: break;
133:
134: switch (c)
135: {
136: case 'h':
137: (void) acopm_usage_info();
138: return ACOPM_OPT_PARSE_EXIT_SUCCESS;
139:
140: case 'v':
141: (void) acopm_version_info();
142: return ACOPM_OPT_PARSE_EXIT_SUCCESS;
143:
144: case 'D':
145: (void) acopm_daemon_set();
146: break;
147:
148: case 'c':
149: if (! acopm_strset_buf(config_file, optarg))
150: (void) acopm_log_warning("%s: config file '%s' too long, truncated",
151: __func__, optarg);
152: break;
153:
154: case 'L':
155: if (! acopm_log_set_file(optarg))
156: return ACOPM_OPT_PARSE_EXIT_FAILURE;
157:
158: break;
159:
160: case 'l':
161: if (! acopm_parse_logmask())
162: return ACOPM_OPT_PARSE_EXIT_FAILURE;
163:
164: break;
165: }
166: }
167:
168: return ACOPM_OPT_PARSE_CONTINUE;
169: }
CVSweb