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 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <stdint.h> |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | #include <assert.h> |
Bryce Harrington | d9779e3 | 2016-08-03 17:40:50 -0700 | [diff] [blame] | 33 | #include <errno.h> |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 34 | |
| 35 | #include "config-parser.h" |
Bryce Harrington | 25a2bdd | 2016-08-03 17:40:52 -0700 | [diff] [blame] | 36 | #include "string-helpers.h" |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 37 | |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 38 | static int |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 39 | handle_option(const struct weston_option *option, char *value) |
| 40 | { |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 41 | char* p; |
| 42 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 43 | switch (option->type) { |
| 44 | case WESTON_OPTION_INTEGER: |
Bryce Harrington | 25a2bdd | 2016-08-03 17:40:52 -0700 | [diff] [blame] | 45 | if (!safe_strtoint(value, option->data)) |
Bryce Harrington | d9779e3 | 2016-08-03 17:40:50 -0700 | [diff] [blame] | 46 | return 0; |
| 47 | return 1; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 48 | case WESTON_OPTION_UNSIGNED_INTEGER: |
Bryce Harrington | d9779e3 | 2016-08-03 17:40:50 -0700 | [diff] [blame] | 49 | errno = 0; |
Bryce Harrington | f6051cb | 2016-07-08 17:44:10 -0700 | [diff] [blame] | 50 | * (uint32_t *) option->data = strtoul(value, &p, 10); |
Bryce Harrington | d9779e3 | 2016-08-03 17:40:50 -0700 | [diff] [blame] | 51 | if (errno != 0 || p == value || *p != '\0') |
| 52 | return 0; |
| 53 | return 1; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 54 | case WESTON_OPTION_STRING: |
| 55 | * (char **) option->data = strdup(value); |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 56 | return 1; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 57 | default: |
| 58 | assert(0); |
| 59 | } |
| 60 | } |
| 61 | |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 62 | static int |
| 63 | long_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 | |
| 89 | static int |
Lyude Paul | 47bbdc7 | 2017-05-08 12:47:55 -0400 | [diff] [blame] | 90 | long_option_with_arg(const struct weston_option *options, int count, char *arg, |
| 91 | char *param) |
| 92 | { |
| 93 | int k, len; |
| 94 | |
| 95 | for (k = 0; k < count; k++) { |
| 96 | if (!options[k].name) |
| 97 | continue; |
| 98 | |
| 99 | len = strlen(options[k].name); |
| 100 | if (strncmp(options[k].name, arg + 2, len) != 0) |
| 101 | continue; |
| 102 | |
| 103 | /* Since long_option() should handle all booleans, we should |
| 104 | * never reach this |
| 105 | */ |
| 106 | assert(options[k].type != WESTON_OPTION_BOOLEAN); |
| 107 | |
| 108 | return handle_option(options + k, param); |
| 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 115 | short_option(const struct weston_option *options, int count, char *arg) |
| 116 | { |
| 117 | int k; |
| 118 | |
| 119 | if (!arg[1]) |
| 120 | return 0; |
| 121 | |
| 122 | for (k = 0; k < count; k++) { |
| 123 | if (options[k].short_name != arg[1]) |
| 124 | continue; |
| 125 | |
| 126 | if (options[k].type == WESTON_OPTION_BOOLEAN) { |
| 127 | if (!arg[2]) { |
| 128 | * (int32_t *) options[k].data = 1; |
| 129 | |
| 130 | return 1; |
| 131 | } |
Bryce Harrington | 38298ec | 2016-02-11 15:25:56 -0800 | [diff] [blame] | 132 | } else if (arg[2]) { |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 133 | return handle_option(options + k, arg + 2); |
Bryce Harrington | 38298ec | 2016-02-11 15:25:56 -0800 | [diff] [blame] | 134 | } else { |
| 135 | return 0; |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Bryce Harrington | 38298ec | 2016-02-11 15:25:56 -0800 | [diff] [blame] | 142 | static int |
| 143 | short_option_with_arg(const struct weston_option *options, int count, char *arg, char *param) |
| 144 | { |
| 145 | int k; |
| 146 | |
| 147 | if (!arg[1]) |
| 148 | return 0; |
| 149 | |
| 150 | for (k = 0; k < count; k++) { |
| 151 | if (options[k].short_name != arg[1]) |
| 152 | continue; |
| 153 | |
| 154 | if (options[k].type == WESTON_OPTION_BOOLEAN) |
| 155 | continue; |
| 156 | |
| 157 | return handle_option(options + k, param); |
| 158 | } |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 163 | int |
| 164 | parse_options(const struct weston_option *options, |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 165 | int count, int *argc, char *argv[]) |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 166 | { |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 167 | int i, j; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 168 | |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 169 | for (i = 1, j = 1; i < *argc; i++) { |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 170 | if (argv[i][0] == '-') { |
| 171 | if (argv[i][1] == '-') { |
Bryce Harrington | 38298ec | 2016-02-11 15:25:56 -0800 | [diff] [blame] | 172 | /* Long option, e.g. --foo or --foo=bar */ |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 173 | if (long_option(options, count, argv[i])) |
| 174 | continue; |
Bryce Harrington | 38298ec | 2016-02-11 15:25:56 -0800 | [diff] [blame] | 175 | |
Lyude Paul | 47bbdc7 | 2017-05-08 12:47:55 -0400 | [diff] [blame] | 176 | /* ...also handle --foo bar */ |
| 177 | if (i + 1 < *argc && |
| 178 | long_option_with_arg(options, count, |
| 179 | argv[i], argv[i+1])) { |
| 180 | i++; |
| 181 | continue; |
| 182 | } |
Bryce Harrington | 38298ec | 2016-02-11 15:25:56 -0800 | [diff] [blame] | 183 | } else { |
| 184 | /* Short option, e.g -f or -f42 */ |
| 185 | if (short_option(options, count, argv[i])) |
| 186 | continue; |
| 187 | |
| 188 | /* ...also handle -f 42 */ |
| 189 | if (i+1 < *argc && |
| 190 | short_option_with_arg(options, count, argv[i], argv[i+1])) { |
| 191 | i++; |
| 192 | continue; |
| 193 | } |
| 194 | } |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 195 | } |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 196 | argv[j++] = argv[i]; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 197 | } |
| 198 | argv[j] = NULL; |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 199 | *argc = j; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 200 | |
| 201 | return j; |
| 202 | } |