• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2012 Kristian Høgsberg
3  *
4  * 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:
11  *
12  * 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.
24  */
25 
26 #include "config.h"
27 
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <assert.h>
34 #include <errno.h>
35 
36 #include <libweston/config-parser.h>
37 #include "string-helpers.h"
38 
39 static bool
handle_option(const struct weston_option * option,char * value)40 handle_option(const struct weston_option *option, char *value)
41 {
42 	char* p;
43 
44 	switch (option->type) {
45 	case WESTON_OPTION_INTEGER:
46 		if (!safe_strtoint(value, option->data))
47 			return false;
48 		return true;
49 	case WESTON_OPTION_UNSIGNED_INTEGER:
50 		errno = 0;
51 		* (uint32_t *) option->data = strtoul(value, &p, 10);
52 		if (errno != 0 || p == value || *p != '\0')
53 			return false;
54 		return true;
55 	case WESTON_OPTION_STRING:
56 		* (char **) option->data = strdup(value);
57 		return true;
58 	default:
59 		assert(0);
60 		return false;
61 	}
62 }
63 
64 static bool
long_option(const struct weston_option * options,int count,char * arg)65 long_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]) {
79 				* (bool *) options[k].data = true;
80 
81 				return true;
82 			}
83 		} else if (arg[len+2] == '=') {
84 			return handle_option(options + k, arg + len + 3);
85 		}
86 	}
87 
88 	return false;
89 }
90 
91 static bool
long_option_with_arg(const struct weston_option * options,int count,char * arg,char * param)92 long_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 
113 	return false;
114 }
115 
116 static bool
short_option(const struct weston_option * options,int count,char * arg)117 short_option(const struct weston_option *options, int count, char *arg)
118 {
119 	int k;
120 
121 	if (!arg[1])
122 		return false;
123 
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]) {
130 				* (bool *) options[k].data = true;
131 
132 				return true;
133 			}
134 		} else if (arg[2]) {
135 			return handle_option(options + k, arg + 2);
136 		} else {
137 			return false;
138 		}
139 	}
140 
141 	return false;
142 }
143 
144 static bool
short_option_with_arg(const struct weston_option * options,int count,char * arg,char * param)145 short_option_with_arg(const struct weston_option *options, int count, char *arg, char *param)
146 {
147 	int k;
148 
149 	if (!arg[1])
150 		return false;
151 
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 
162 	return false;
163 }
164 
165 int
parse_options(const struct weston_option * options,int count,int * argc,char * argv[])166 parse_options(const struct weston_option *options,
167 	      int count, int *argc, char *argv[])
168 {
169 	int i, j;
170 
171 	for (i = 1, j = 1; i < *argc; i++) {
172 		if (argv[i][0] == '-') {
173 			if (argv[i][1] == '-') {
174 				/* Long option, e.g. --foo or --foo=bar */
175 				if (long_option(options, count, argv[i]))
176 					continue;
177 
178 				/* ...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 				}
185 			} 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 			}
197 		}
198 		argv[j++] = argv[i];
199 	}
200 	argv[j] = NULL;
201 	*argc = j;
202 
203 	return j;
204 }
205