blob: 795195f5c119417049e783db36a944f75721a343 [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);
60 }
61}
62
Eric Engestrom0c30fa52017-05-24 21:23:15 +010063static bool
Bill Spitzak30114932014-08-19 18:13:09 -070064long_option(const struct weston_option *options, int count, char *arg)
65{
66 int k, len;
67
68 for (k = 0; k < count; k++) {
69 if (!options[k].name)
70 continue;
71
72 len = strlen(options[k].name);
73 if (strncmp(options[k].name, arg + 2, len) != 0)
74 continue;
75
76 if (options[k].type == WESTON_OPTION_BOOLEAN) {
77 if (!arg[len + 2]) {
78 * (int32_t *) options[k].data = 1;
79
Eric Engestrom0c30fa52017-05-24 21:23:15 +010080 return true;
Bill Spitzak30114932014-08-19 18:13:09 -070081 }
82 } else if (arg[len+2] == '=') {
83 return handle_option(options + k, arg + len + 3);
84 }
85 }
86
Eric Engestrom0c30fa52017-05-24 21:23:15 +010087 return false;
Bill Spitzak30114932014-08-19 18:13:09 -070088}
89
Eric Engestrom0c30fa52017-05-24 21:23:15 +010090static bool
Lyude Paul47bbdc72017-05-08 12:47:55 -040091long_option_with_arg(const struct weston_option *options, int count, char *arg,
92 char *param)
93{
94 int k, len;
95
96 for (k = 0; k < count; k++) {
97 if (!options[k].name)
98 continue;
99
100 len = strlen(options[k].name);
101 if (strncmp(options[k].name, arg + 2, len) != 0)
102 continue;
103
104 /* Since long_option() should handle all booleans, we should
105 * never reach this
106 */
107 assert(options[k].type != WESTON_OPTION_BOOLEAN);
108
109 return handle_option(options + k, param);
110 }
111
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100112 return false;
Lyude Paul47bbdc72017-05-08 12:47:55 -0400113}
114
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100115static bool
Bill Spitzak30114932014-08-19 18:13:09 -0700116short_option(const struct weston_option *options, int count, char *arg)
117{
118 int k;
119
120 if (!arg[1])
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100121 return false;
Bill Spitzak30114932014-08-19 18:13:09 -0700122
123 for (k = 0; k < count; k++) {
124 if (options[k].short_name != arg[1])
125 continue;
126
127 if (options[k].type == WESTON_OPTION_BOOLEAN) {
128 if (!arg[2]) {
129 * (int32_t *) options[k].data = 1;
130
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100131 return true;
Bill Spitzak30114932014-08-19 18:13:09 -0700132 }
Bryce Harrington38298ec2016-02-11 15:25:56 -0800133 } else if (arg[2]) {
Bill Spitzak30114932014-08-19 18:13:09 -0700134 return handle_option(options + k, arg + 2);
Bryce Harrington38298ec2016-02-11 15:25:56 -0800135 } else {
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100136 return false;
Bill Spitzak30114932014-08-19 18:13:09 -0700137 }
138 }
139
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100140 return false;
Bill Spitzak30114932014-08-19 18:13:09 -0700141}
142
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100143static bool
Bryce Harrington38298ec2016-02-11 15:25:56 -0800144short_option_with_arg(const struct weston_option *options, int count, char *arg, char *param)
145{
146 int k;
147
148 if (!arg[1])
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100149 return false;
Bryce Harrington38298ec2016-02-11 15:25:56 -0800150
151 for (k = 0; k < count; k++) {
152 if (options[k].short_name != arg[1])
153 continue;
154
155 if (options[k].type == WESTON_OPTION_BOOLEAN)
156 continue;
157
158 return handle_option(options + k, param);
159 }
160
Eric Engestrom0c30fa52017-05-24 21:23:15 +0100161 return false;
Bryce Harrington38298ec2016-02-11 15:25:56 -0800162}
163
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400164int
165parse_options(const struct weston_option *options,
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500166 int count, int *argc, char *argv[])
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400167{
Bill Spitzak30114932014-08-19 18:13:09 -0700168 int i, j;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400169
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500170 for (i = 1, j = 1; i < *argc; i++) {
Bill Spitzak30114932014-08-19 18:13:09 -0700171 if (argv[i][0] == '-') {
172 if (argv[i][1] == '-') {
Bryce Harrington38298ec2016-02-11 15:25:56 -0800173 /* Long option, e.g. --foo or --foo=bar */
Bill Spitzak30114932014-08-19 18:13:09 -0700174 if (long_option(options, count, argv[i]))
175 continue;
Bryce Harrington38298ec2016-02-11 15:25:56 -0800176
Lyude Paul47bbdc72017-05-08 12:47:55 -0400177 /* ...also handle --foo bar */
178 if (i + 1 < *argc &&
179 long_option_with_arg(options, count,
180 argv[i], argv[i+1])) {
181 i++;
182 continue;
183 }
Bryce Harrington38298ec2016-02-11 15:25:56 -0800184 } else {
185 /* Short option, e.g -f or -f42 */
186 if (short_option(options, count, argv[i]))
187 continue;
188
189 /* ...also handle -f 42 */
190 if (i+1 < *argc &&
191 short_option_with_arg(options, count, argv[i], argv[i+1])) {
192 i++;
193 continue;
194 }
195 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400196 }
Bill Spitzak30114932014-08-19 18:13:09 -0700197 argv[j++] = argv[i];
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400198 }
199 argv[j] = NULL;
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500200 *argc = j;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400201
202 return j;
203}