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> |
| 33 | |
| 34 | #include "config-parser.h" |
| 35 | |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 36 | static int |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 37 | handle_option(const struct weston_option *option, char *value) |
| 38 | { |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 39 | char* p; |
| 40 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 41 | switch (option->type) { |
| 42 | case WESTON_OPTION_INTEGER: |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 43 | * (int32_t *) option->data = strtol(value, &p, 0); |
| 44 | return *value && !*p; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 45 | case WESTON_OPTION_UNSIGNED_INTEGER: |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 46 | * (uint32_t *) option->data = strtoul(value, &p, 0); |
| 47 | return *value && !*p; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 48 | case WESTON_OPTION_STRING: |
| 49 | * (char **) option->data = strdup(value); |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 50 | return 1; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 51 | default: |
| 52 | assert(0); |
| 53 | } |
| 54 | } |
| 55 | |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 56 | static int |
| 57 | long_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 | |
| 83 | static int |
| 84 | short_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 | } |
| 101 | } else { |
| 102 | return handle_option(options + k, arg + 2); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 109 | int |
| 110 | parse_options(const struct weston_option *options, |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 111 | int count, int *argc, char *argv[]) |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 112 | { |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 113 | int i, j; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 114 | |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 115 | for (i = 1, j = 1; i < *argc; i++) { |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 116 | if (argv[i][0] == '-') { |
| 117 | if (argv[i][1] == '-') { |
| 118 | if (long_option(options, count, argv[i])) |
| 119 | continue; |
| 120 | } else if (short_option(options, count, argv[i])) |
| 121 | continue; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 122 | } |
Bill Spitzak | 3011493 | 2014-08-19 18:13:09 -0700 | [diff] [blame] | 123 | argv[j++] = argv[i]; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 124 | } |
| 125 | argv[j] = NULL; |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 126 | *argc = j; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 127 | |
| 128 | return j; |
| 129 | } |