blob: 55be1b5f554e3861d13344e94c191e7bafb3ca22 [file] [log] [blame]
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001/*
2 * Copyright © 2012 Kristian Høgsberg
3 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -07004 * 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:
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040011 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -070012 * 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.
15 *
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
23 * SOFTWARE.
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Eric Engestrom0c30fa52017-05-24 21:23:15 +010028#include <stdbool.h>
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040029#include <stdlib.h>
30#include <stdint.h>
31#include <stdio.h>
32#include <string.h>
33#include <assert.h>
Bryce Harringtond9779e32016-08-03 17:40:50 -070034#include <errno.h>
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040035
Pekka Paalanen91b10102019-04-04 14:27:31 +030036#include <libweston/config-parser.h>
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070037#include "string-helpers.h"
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040038
Eric Engestrom0c30fa52017-05-24 21:23:15 +010039static bool
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040040handle_option(const struct weston_option *option, char *value)
41{
Bill Spitzak30114932014-08-19 18:13:09 -070042 char* p;
43
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040044 switch (option->type) {
45 case WESTON_OPTION_INTEGER:
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070046 if (!safe_strtoint(value, option->data))
Eric Engestrom0c30fa52017-05-24 21:23:15 +010047 return false;
48 return true;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040049 case WESTON_OPTION_UNSIGNED_INTEGER:
Bryce Harringtond9779e32016-08-03 17:40:50 -070050 errno = 0;
Bryce Harringtonf6051cb2016-07-08 17:44:10 -070051 * (uint32_t *) option->data = strtoul(value, &p, 10);
Bryce Harringtond9779e32016-08-03 17:40:50 -070052 if (errno != 0 || p == value || *p != '\0')
Eric Engestrom0c30fa52017-05-24 21:23:15 +010053 return false;
54 return true;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040055 case WESTON_OPTION_STRING:
56 * (char **) option->data = strdup(value);
Eric Engestrom0c30fa52017-05-24 21:23:15 +010057 return true;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040058 default:
59 assert(0);
Marius Vlad6630f2a2019-08-06 20:06:02 +030060 return false;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040061 }
62}
63
Eric Engestrom0c30fa52017-05-24 21:23:15 +010064static bool
Bill Spitzak30114932014-08-19 18:13:09 -070065long_option(const struct weston_option *options, int count, char *arg)
66{
67 int k, len;
68
69 for (k = 0; k < count; k++) {
70 if (!options[k].name)
71 continue;
72
73 len = strlen(options[k].name);
74 if (strncmp(options[k].name, arg + 2, len) != 0)
75 continue;
76
77 if (options[k].type == WESTON_OPTION_BOOLEAN) {
78 if (!arg[len + 2]) {
Daniel Stonedd8219b2019-11-26 00:32:22 +000079 * (bool *) options[k].data = true;
Bill Spitzak30114932014-08-19 18:13:09 -070080
Eric Engestrom0c30fa52017-05-24 21:23:15 +010081 return true;
Bill Spitzak30114932014-08-19 18:13:09 -070082 }
83 } else if (arg[len+2] == '=') {
84 return handle_option(options + k, arg + len + 3);
85 }
86 }
87
Eric Engestrom0c30fa52017-05-24 21:23:15 +010088 return false;
Bill Spitzak30114932014-08-19 18:13:09 -070089}
90
Eric Engestrom0c30fa52017-05-24 21:23:15 +010091static bool
Lyude Paul47bbdc72017-05-08 12:47:55 -040092long_option_with_arg(const struct weston_option *options, int count, char *arg,
93 char *param)
94{
95 int k, len;
96
97 for (k = 0; k < count; k++) {
98 if (!options[k].name)
99 continue;
100
101 len = strlen(options[k].name);
102 if (strncmp(options[k].name, arg + 2, len) != 0)
103 continue;
104
105 /* Since long_option() should handle all booleans, we should
106 * never reach this
107 */
108 assert(options[k].type != WESTON_OPTION_BOOLEAN);
109
110 return handle_option(options + k, param);
111 }
112
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100113 return false;
Lyude Paul47bbdc72017-05-08 12:47:55 -0400114}
115
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100116static bool
Bill Spitzak30114932014-08-19 18:13:09 -0700117short_option(const struct weston_option *options, int count, char *arg)
118{
119 int k;
120
121 if (!arg[1])
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100122 return false;
Bill Spitzak30114932014-08-19 18:13:09 -0700123
124 for (k = 0; k < count; k++) {
125 if (options[k].short_name != arg[1])
126 continue;
127
128 if (options[k].type == WESTON_OPTION_BOOLEAN) {
129 if (!arg[2]) {
Daniel Stonedd8219b2019-11-26 00:32:22 +0000130 * (bool *) options[k].data = true;
Bill Spitzak30114932014-08-19 18:13:09 -0700131
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100132 return true;
Bill Spitzak30114932014-08-19 18:13:09 -0700133 }
Bryce Harrington38298ec2016-02-11 15:25:56 -0800134 } else if (arg[2]) {
Bill Spitzak30114932014-08-19 18:13:09 -0700135 return handle_option(options + k, arg + 2);
Bryce Harrington38298ec2016-02-11 15:25:56 -0800136 } else {
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100137 return false;
Bill Spitzak30114932014-08-19 18:13:09 -0700138 }
139 }
140
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100141 return false;
Bill Spitzak30114932014-08-19 18:13:09 -0700142}
143
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100144static bool
Bryce Harrington38298ec2016-02-11 15:25:56 -0800145short_option_with_arg(const struct weston_option *options, int count, char *arg, char *param)
146{
147 int k;
148
149 if (!arg[1])
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100150 return false;
Bryce Harrington38298ec2016-02-11 15:25:56 -0800151
152 for (k = 0; k < count; k++) {
153 if (options[k].short_name != arg[1])
154 continue;
155
156 if (options[k].type == WESTON_OPTION_BOOLEAN)
157 continue;
158
159 return handle_option(options + k, param);
160 }
161
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100162 return false;
Bryce Harrington38298ec2016-02-11 15:25:56 -0800163}
164
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400165int
166parse_options(const struct weston_option *options,
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500167 int count, int *argc, char *argv[])
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400168{
Bill Spitzak30114932014-08-19 18:13:09 -0700169 int i, j;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400170
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500171 for (i = 1, j = 1; i < *argc; i++) {
Bill Spitzak30114932014-08-19 18:13:09 -0700172 if (argv[i][0] == '-') {
173 if (argv[i][1] == '-') {
Bryce Harrington38298ec2016-02-11 15:25:56 -0800174 /* Long option, e.g. --foo or --foo=bar */
Bill Spitzak30114932014-08-19 18:13:09 -0700175 if (long_option(options, count, argv[i]))
176 continue;
Bryce Harrington38298ec2016-02-11 15:25:56 -0800177
Lyude Paul47bbdc72017-05-08 12:47:55 -0400178 /* ...also handle --foo bar */
179 if (i + 1 < *argc &&
180 long_option_with_arg(options, count,
181 argv[i], argv[i+1])) {
182 i++;
183 continue;
184 }
Bryce Harrington38298ec2016-02-11 15:25:56 -0800185 } else {
186 /* Short option, e.g -f or -f42 */
187 if (short_option(options, count, argv[i]))
188 continue;
189
190 /* ...also handle -f 42 */
191 if (i+1 < *argc &&
192 short_option_with_arg(options, count, argv[i], argv[i+1])) {
193 i++;
194 continue;
195 }
196 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400197 }
Bill Spitzak30114932014-08-19 18:13:09 -0700198 argv[j++] = argv[i];
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400199 }
200 argv[j] = NULL;
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500201 *argc = j;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400202
203 return j;
204}