Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Kristian Høgsberg |
| 3 | * |
Bryce Harrington | 6c6164c | 2015-06-11 14:20:17 -0700 | [diff] [blame] | 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: |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 11 | * |
Bryce Harrington | 6c6164c | 2015-06-11 14:20:17 -0700 | [diff] [blame] | 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. |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 24 | */ |
| 25 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 26 | #include "config.h" |
| 27 | |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 28 | #include <stdbool.h> |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | #include <stdint.h> |
| 31 | #include <stdio.h> |
| 32 | #include <string.h> |
| 33 | #include <assert.h> |
Bryce Harrington | d9779e3 | 2016-08-03 17:40:50 -0700 | [diff] [blame] | 34 | #include <errno.h> |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 35 | |
Pekka Paalanen | 91b1010 | 2019-04-04 14:27:31 +0300 | [diff] [blame] | 36 | #include <libweston/config-parser.h> |
Bryce Harrington | 25a2bdd | 2016-08-03 17:40:52 -0700 | [diff] [blame] | 37 | #include "string-helpers.h" |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 38 | |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 39 | static bool |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 40 | handle_option(const struct weston_option *option, char *value) |
| 41 | { |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 42 | char* p; |
| 43 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 44 | switch (option->type) { |
| 45 | case WESTON_OPTION_INTEGER: |
Bryce Harrington | 25a2bdd | 2016-08-03 17:40:52 -0700 | [diff] [blame] | 46 | if (!safe_strtoint(value, option->data)) |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 47 | return false; |
| 48 | return true; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 49 | case WESTON_OPTION_UNSIGNED_INTEGER: |
Bryce Harrington | d9779e3 | 2016-08-03 17:40:50 -0700 | [diff] [blame] | 50 | errno = 0; |
Bryce Harrington | f6051cb | 2016-07-08 17:44:10 -0700 | [diff] [blame] | 51 | * (uint32_t *) option->data = strtoul(value, &p, 10); |
Bryce Harrington | d9779e3 | 2016-08-03 17:40:50 -0700 | [diff] [blame] | 52 | if (errno != 0 || p == value || *p != '\0') |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 53 | return false; |
| 54 | return true; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 55 | case WESTON_OPTION_STRING: |
| 56 | * (char **) option->data = strdup(value); |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 57 | return true; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 58 | default: |
| 59 | assert(0); |
Marius Vlad | 6630f2a | 2019-08-06 20:06:02 +0300 | [diff] [blame] | 60 | return false; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 64 | static bool |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 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]) { |
Daniel Stone | dd8219b | 2019-11-26 00:32:22 +0000 | [diff] [blame] | 79 | * (bool *) options[k].data = true; |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 80 | |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 81 | return true; |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 82 | } |
| 83 | } else if (arg[len+2] == '=') { |
| 84 | return handle_option(options + k, arg + len + 3); |
| 85 | } |
| 86 | } |
| 87 | |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 88 | return false; |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 91 | static bool |
Lyude Paul | 47bbdc7 | 2017-05-08 12:47:55 -0400 | [diff] [blame] | 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 | |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 113 | return false; |
Lyude Paul | 47bbdc7 | 2017-05-08 12:47:55 -0400 | [diff] [blame] | 114 | } |
| 115 | |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 116 | static bool |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 117 | short_option(const struct weston_option *options, int count, char *arg) |
| 118 | { |
| 119 | int k; |
| 120 | |
| 121 | if (!arg[1]) |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 122 | return false; |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 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]) { |
Daniel Stone | dd8219b | 2019-11-26 00:32:22 +0000 | [diff] [blame] | 130 | * (bool *) options[k].data = true; |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 131 | |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 132 | return true; |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 133 | } |
Bryce Harrington | 38298ec | 2016-02-11 15:25:56 -0800 | [diff] [blame] | 134 | } else if (arg[2]) { |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 135 | return handle_option(options + k, arg + 2); |
Bryce Harrington | 38298ec | 2016-02-11 15:25:56 -0800 | [diff] [blame] | 136 | } else { |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 137 | return false; |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 141 | return false; |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 144 | static bool |
Bryce Harrington | 38298ec | 2016-02-11 15:25:56 -0800 | [diff] [blame] | 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]) |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 150 | return false; |
Bryce Harrington | 38298ec | 2016-02-11 15:25:56 -0800 | [diff] [blame] | 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 | |
Eric Engestrom | 0c30fa5 | 2017-05-24 21:23:15 +0100 | [diff] [blame] | 162 | return false; |
Bryce Harrington | 38298ec | 2016-02-11 15:25:56 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 165 | int |
| 166 | parse_options(const struct weston_option *options, |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 167 | int count, int *argc, char *argv[]) |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 168 | { |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 169 | int i, j; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 170 | |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 171 | for (i = 1, j = 1; i < *argc; i++) { |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 172 | if (argv[i][0] == '-') { |
| 173 | if (argv[i][1] == '-') { |
Bryce Harrington | 38298ec | 2016-02-11 15:25:56 -0800 | [diff] [blame] | 174 | /* Long option, e.g. --foo or --foo=bar */ |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 175 | if (long_option(options, count, argv[i])) |
| 176 | continue; |
Bryce Harrington | 38298ec | 2016-02-11 15:25:56 -0800 | [diff] [blame] | 177 | |
Lyude Paul | 47bbdc7 | 2017-05-08 12:47:55 -0400 | [diff] [blame] | 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 | } |
Bryce Harrington | 38298ec | 2016-02-11 15:25:56 -0800 | [diff] [blame] | 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 | } |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 197 | } |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 198 | argv[j++] = argv[i]; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 199 | } |
| 200 | argv[j] = NULL; |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 201 | *argc = j; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 202 | |
| 203 | return j; |
| 204 | } |