blob: eee75468e2b8053b06a8620d42b995c1c164caa9 [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
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040028#include <stdlib.h>
29#include <stdint.h>
30#include <stdio.h>
31#include <string.h>
32#include <assert.h>
Bryce Harringtond9779e32016-08-03 17:40:50 -070033#include <errno.h>
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040034
35#include "config-parser.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070036#include "string-helpers.h"
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040037
Bill Spitzak30114932014-08-19 18:13:09 -070038static int
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040039handle_option(const struct weston_option *option, char *value)
40{
Bill Spitzak30114932014-08-19 18:13:09 -070041 char* p;
42
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040043 switch (option->type) {
44 case WESTON_OPTION_INTEGER:
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070045 if (!safe_strtoint(value, option->data))
Bryce Harringtond9779e32016-08-03 17:40:50 -070046 return 0;
47 return 1;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040048 case WESTON_OPTION_UNSIGNED_INTEGER:
Bryce Harringtond9779e32016-08-03 17:40:50 -070049 errno = 0;
Bryce Harringtonf6051cb2016-07-08 17:44:10 -070050 * (uint32_t *) option->data = strtoul(value, &p, 10);
Bryce Harringtond9779e32016-08-03 17:40:50 -070051 if (errno != 0 || p == value || *p != '\0')
52 return 0;
53 return 1;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040054 case WESTON_OPTION_STRING:
55 * (char **) option->data = strdup(value);
Bill Spitzak30114932014-08-19 18:13:09 -070056 return 1;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040057 default:
58 assert(0);
59 }
60}
61
Bill Spitzak30114932014-08-19 18:13:09 -070062static int
63long_option(const struct weston_option *options, int count, char *arg)
64{
65 int k, len;
66
67 for (k = 0; k < count; k++) {
68 if (!options[k].name)
69 continue;
70
71 len = strlen(options[k].name);
72 if (strncmp(options[k].name, arg + 2, len) != 0)
73 continue;
74
75 if (options[k].type == WESTON_OPTION_BOOLEAN) {
76 if (!arg[len + 2]) {
77 * (int32_t *) options[k].data = 1;
78
79 return 1;
80 }
81 } else if (arg[len+2] == '=') {
82 return handle_option(options + k, arg + len + 3);
83 }
84 }
85
86 return 0;
87}
88
89static int
90short_option(const struct weston_option *options, int count, char *arg)
91{
92 int k;
93
94 if (!arg[1])
95 return 0;
96
97 for (k = 0; k < count; k++) {
98 if (options[k].short_name != arg[1])
99 continue;
100
101 if (options[k].type == WESTON_OPTION_BOOLEAN) {
102 if (!arg[2]) {
103 * (int32_t *) options[k].data = 1;
104
105 return 1;
106 }
Bryce Harrington38298ec2016-02-11 15:25:56 -0800107 } else if (arg[2]) {
Bill Spitzak30114932014-08-19 18:13:09 -0700108 return handle_option(options + k, arg + 2);
Bryce Harrington38298ec2016-02-11 15:25:56 -0800109 } else {
110 return 0;
Bill Spitzak30114932014-08-19 18:13:09 -0700111 }
112 }
113
114 return 0;
115}
116
Bryce Harrington38298ec2016-02-11 15:25:56 -0800117static int
118short_option_with_arg(const struct weston_option *options, int count, char *arg, char *param)
119{
120 int k;
121
122 if (!arg[1])
123 return 0;
124
125 for (k = 0; k < count; k++) {
126 if (options[k].short_name != arg[1])
127 continue;
128
129 if (options[k].type == WESTON_OPTION_BOOLEAN)
130 continue;
131
132 return handle_option(options + k, param);
133 }
134
135 return 0;
136}
137
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400138int
139parse_options(const struct weston_option *options,
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500140 int count, int *argc, char *argv[])
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400141{
Bill Spitzak30114932014-08-19 18:13:09 -0700142 int i, j;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400143
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500144 for (i = 1, j = 1; i < *argc; i++) {
Bill Spitzak30114932014-08-19 18:13:09 -0700145 if (argv[i][0] == '-') {
146 if (argv[i][1] == '-') {
Bryce Harrington38298ec2016-02-11 15:25:56 -0800147 /* Long option, e.g. --foo or --foo=bar */
Bill Spitzak30114932014-08-19 18:13:09 -0700148 if (long_option(options, count, argv[i]))
149 continue;
Bryce Harrington38298ec2016-02-11 15:25:56 -0800150
151 } else {
152 /* Short option, e.g -f or -f42 */
153 if (short_option(options, count, argv[i]))
154 continue;
155
156 /* ...also handle -f 42 */
157 if (i+1 < *argc &&
158 short_option_with_arg(options, count, argv[i], argv[i+1])) {
159 i++;
160 continue;
161 }
162 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400163 }
Bill Spitzak30114932014-08-19 18:13:09 -0700164 argv[j++] = argv[i];
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400165 }
166 argv[j] = NULL;
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500167 *argc = j;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400168
169 return j;
170}