2 * Copyright © 2012 Kristian Høgsberg
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <libweston/config-parser.h>
35 #include "shared/string-helpers.h"
38 handle_option(const struct weston_option *option, char *value)
42 switch (option->type) {
43 case WESTON_OPTION_INTEGER:
44 if (!safe_strtoint(value, option->data))
47 case WESTON_OPTION_UNSIGNED_INTEGER:
49 * (uint32_t *) option->data = strtoul(value, &p, 10);
50 if (errno != 0 || p == value || *p != '\0')
53 case WESTON_OPTION_STRING:
54 * (char **) option->data = strdup(value);
63 long_option(const struct weston_option *options, int count, char *arg)
67 for (k = 0; k < count; k++) {
71 len = strlen(options[k].name);
72 if (strncmp(options[k].name, arg + 2, len) != 0)
75 if (options[k].type == WESTON_OPTION_BOOLEAN) {
77 * (int32_t *) options[k].data = 1;
81 } else if (arg[len+2] == '=') {
82 return handle_option(options + k, arg + len + 3);
90 long_option_with_arg(const struct weston_option *options, int count, char *arg,
95 for (k = 0; k < count; k++) {
99 len = strlen(options[k].name);
100 if (strncmp(options[k].name, arg + 2, len) != 0)
103 /* Since long_option() should handle all booleans, we should
106 assert(options[k].type != WESTON_OPTION_BOOLEAN);
108 return handle_option(options + k, param);
115 short_option(const struct weston_option *options, int count, char *arg)
122 for (k = 0; k < count; k++) {
123 if (options[k].short_name != arg[1])
126 if (options[k].type == WESTON_OPTION_BOOLEAN) {
128 * (int32_t *) options[k].data = 1;
133 return handle_option(options + k, arg + 2);
143 short_option_with_arg(const struct weston_option *options, int count, char *arg, char *param)
150 for (k = 0; k < count; k++) {
151 if (options[k].short_name != arg[1])
154 if (options[k].type == WESTON_OPTION_BOOLEAN)
157 return handle_option(options + k, param);
164 parse_options(const struct weston_option *options,
165 int count, int *argc, char *argv[])
169 for (i = 1, j = 1; i < *argc; i++) {
170 if (argv[i][0] == '-') {
171 if (argv[i][1] == '-') {
172 /* Long option, e.g. --foo or --foo=bar */
173 if (long_option(options, count, argv[i]))
176 /* ...also handle --foo bar */
178 long_option_with_arg(options, count,
179 argv[i], argv[i+1])) {
184 /* Short option, e.g -f or -f42 */
185 if (short_option(options, count, argv[i]))
188 /* ...also handle -f 42 */
190 short_option_with_arg(options, count, argv[i], argv[i+1])) {