blob: fb4a3424114577f621904bfeb6f329c6ccee864e [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"
36
Bill Spitzak30114932014-08-19 18:13:09 -070037static int
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040038handle_option(const struct weston_option *option, char *value)
39{
Bill Spitzak30114932014-08-19 18:13:09 -070040 char* p;
41
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040042 switch (option->type) {
43 case WESTON_OPTION_INTEGER:
Bryce Harringtond9779e32016-08-03 17:40:50 -070044 errno = 0;
Bryce Harringtonf6051cb2016-07-08 17:44:10 -070045 * (int32_t *) option->data = strtol(value, &p, 10);
Bryce Harringtond9779e32016-08-03 17:40:50 -070046 if (errno != 0 || p == value || *p != '\0')
47 return 0;
48 return 1;
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')
53 return 0;
54 return 1;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040055 case WESTON_OPTION_STRING:
56 * (char **) option->data = strdup(value);
Bill Spitzak30114932014-08-19 18:13:09 -070057 return 1;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040058 default:
59 assert(0);
60 }
61}
62
Bill Spitzak30114932014-08-19 18:13:09 -070063static int
64long_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
80 return 1;
81 }
82 } else if (arg[len+2] == '=') {
83 return handle_option(options + k, arg + len + 3);
84 }
85 }
86
87 return 0;
88}
89
90static int
91short_option(const struct weston_option *options, int count, char *arg)
92{
93 int k;
94
95 if (!arg[1])
96 return 0;
97
98 for (k = 0; k < count; k++) {
99 if (options[k].short_name != arg[1])
100 continue;
101
102 if (options[k].type == WESTON_OPTION_BOOLEAN) {
103 if (!arg[2]) {
104 * (int32_t *) options[k].data = 1;
105
106 return 1;
107 }
Bryce Harrington38298ec2016-02-11 15:25:56 -0800108 } else if (arg[2]) {
Bill Spitzak30114932014-08-19 18:13:09 -0700109 return handle_option(options + k, arg + 2);
Bryce Harrington38298ec2016-02-11 15:25:56 -0800110 } else {
111 return 0;
Bill Spitzak30114932014-08-19 18:13:09 -0700112 }
113 }
114
115 return 0;
116}
117
Bryce Harrington38298ec2016-02-11 15:25:56 -0800118static int
119short_option_with_arg(const struct weston_option *options, int count, char *arg, char *param)
120{
121 int k;
122
123 if (!arg[1])
124 return 0;
125
126 for (k = 0; k < count; k++) {
127 if (options[k].short_name != arg[1])
128 continue;
129
130 if (options[k].type == WESTON_OPTION_BOOLEAN)
131 continue;
132
133 return handle_option(options + k, param);
134 }
135
136 return 0;
137}
138
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400139int
140parse_options(const struct weston_option *options,
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500141 int count, int *argc, char *argv[])
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400142{
Bill Spitzak30114932014-08-19 18:13:09 -0700143 int i, j;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400144
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500145 for (i = 1, j = 1; i < *argc; i++) {
Bill Spitzak30114932014-08-19 18:13:09 -0700146 if (argv[i][0] == '-') {
147 if (argv[i][1] == '-') {
Bryce Harrington38298ec2016-02-11 15:25:56 -0800148 /* Long option, e.g. --foo or --foo=bar */
Bill Spitzak30114932014-08-19 18:13:09 -0700149 if (long_option(options, count, argv[i]))
150 continue;
Bryce Harrington38298ec2016-02-11 15:25:56 -0800151
152 } else {
153 /* Short option, e.g -f or -f42 */
154 if (short_option(options, count, argv[i]))
155 continue;
156
157 /* ...also handle -f 42 */
158 if (i+1 < *argc &&
159 short_option_with_arg(options, count, argv[i], argv[i+1])) {
160 i++;
161 continue;
162 }
163 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400164 }
Bill Spitzak30114932014-08-19 18:13:09 -0700165 argv[j++] = argv[i];
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400166 }
167 argv[j] = NULL;
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500168 *argc = j;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400169
170 return j;
171}