blob: d5fee8ec44ecef8ccbe3815065b355747b525c53 [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>
33
34#include "config-parser.h"
35
Bill Spitzak30114932014-08-19 18:13:09 -070036static int
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040037handle_option(const struct weston_option *option, char *value)
38{
Bill Spitzak30114932014-08-19 18:13:09 -070039 char* p;
40
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040041 switch (option->type) {
42 case WESTON_OPTION_INTEGER:
Bill Spitzak30114932014-08-19 18:13:09 -070043 * (int32_t *) option->data = strtol(value, &p, 0);
44 return *value && !*p;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040045 case WESTON_OPTION_UNSIGNED_INTEGER:
Bill Spitzak30114932014-08-19 18:13:09 -070046 * (uint32_t *) option->data = strtoul(value, &p, 0);
47 return *value && !*p;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040048 case WESTON_OPTION_STRING:
49 * (char **) option->data = strdup(value);
Bill Spitzak30114932014-08-19 18:13:09 -070050 return 1;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -040051 default:
52 assert(0);
53 }
54}
55
Bill Spitzak30114932014-08-19 18:13:09 -070056static int
57long_option(const struct weston_option *options, int count, char *arg)
58{
59 int k, len;
60
61 for (k = 0; k < count; k++) {
62 if (!options[k].name)
63 continue;
64
65 len = strlen(options[k].name);
66 if (strncmp(options[k].name, arg + 2, len) != 0)
67 continue;
68
69 if (options[k].type == WESTON_OPTION_BOOLEAN) {
70 if (!arg[len + 2]) {
71 * (int32_t *) options[k].data = 1;
72
73 return 1;
74 }
75 } else if (arg[len+2] == '=') {
76 return handle_option(options + k, arg + len + 3);
77 }
78 }
79
80 return 0;
81}
82
83static int
84short_option(const struct weston_option *options, int count, char *arg)
85{
86 int k;
87
88 if (!arg[1])
89 return 0;
90
91 for (k = 0; k < count; k++) {
92 if (options[k].short_name != arg[1])
93 continue;
94
95 if (options[k].type == WESTON_OPTION_BOOLEAN) {
96 if (!arg[2]) {
97 * (int32_t *) options[k].data = 1;
98
99 return 1;
100 }
Bryce Harrington38298ec2016-02-11 15:25:56 -0800101 } else if (arg[2]) {
Bill Spitzak30114932014-08-19 18:13:09 -0700102 return handle_option(options + k, arg + 2);
Bryce Harrington38298ec2016-02-11 15:25:56 -0800103 } else {
104 return 0;
Bill Spitzak30114932014-08-19 18:13:09 -0700105 }
106 }
107
108 return 0;
109}
110
Bryce Harrington38298ec2016-02-11 15:25:56 -0800111static int
112short_option_with_arg(const struct weston_option *options, int count, char *arg, char *param)
113{
114 int k;
115
116 if (!arg[1])
117 return 0;
118
119 for (k = 0; k < count; k++) {
120 if (options[k].short_name != arg[1])
121 continue;
122
123 if (options[k].type == WESTON_OPTION_BOOLEAN)
124 continue;
125
126 return handle_option(options + k, param);
127 }
128
129 return 0;
130}
131
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400132int
133parse_options(const struct weston_option *options,
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500134 int count, int *argc, char *argv[])
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400135{
Bill Spitzak30114932014-08-19 18:13:09 -0700136 int i, j;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400137
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500138 for (i = 1, j = 1; i < *argc; i++) {
Bill Spitzak30114932014-08-19 18:13:09 -0700139 if (argv[i][0] == '-') {
140 if (argv[i][1] == '-') {
Bryce Harrington38298ec2016-02-11 15:25:56 -0800141 /* Long option, e.g. --foo or --foo=bar */
Bill Spitzak30114932014-08-19 18:13:09 -0700142 if (long_option(options, count, argv[i]))
143 continue;
Bryce Harrington38298ec2016-02-11 15:25:56 -0800144
145 } else {
146 /* Short option, e.g -f or -f42 */
147 if (short_option(options, count, argv[i]))
148 continue;
149
150 /* ...also handle -f 42 */
151 if (i+1 < *argc &&
152 short_option_with_arg(options, count, argv[i], argv[i+1])) {
153 i++;
154 continue;
155 }
156 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400157 }
Bill Spitzak30114932014-08-19 18:13:09 -0700158 argv[j++] = argv[i];
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400159 }
160 argv[j] = NULL;
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500161 *argc = j;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400162
163 return j;
164}