Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Kristian Høgsberg |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 5 | * documentation for any purpose is hereby granted without fee, provided that |
| 6 | * the above copyright notice appear in all copies and that both that copyright |
| 7 | * notice and this permission notice appear in supporting documentation, and |
| 8 | * that the name of the copyright holders not be used in advertising or |
| 9 | * publicity pertaining to distribution of the software without specific, |
| 10 | * written prior permission. The copyright holders make no representations |
| 11 | * about the suitability of this software for any purpose. It is provided "as |
| 12 | * is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 15 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 16 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 17 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 18 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 19 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 20 | * OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 23 | #include "config.h" |
| 24 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <stdint.h> |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | #include <assert.h> |
| 30 | |
| 31 | #include "config-parser.h" |
| 32 | |
| 33 | static void |
| 34 | handle_option(const struct weston_option *option, char *value) |
| 35 | { |
| 36 | switch (option->type) { |
| 37 | case WESTON_OPTION_INTEGER: |
| 38 | * (int32_t *) option->data = strtol(value, NULL, 0); |
| 39 | return; |
| 40 | case WESTON_OPTION_UNSIGNED_INTEGER: |
| 41 | * (uint32_t *) option->data = strtoul(value, NULL, 0); |
| 42 | return; |
| 43 | case WESTON_OPTION_STRING: |
| 44 | * (char **) option->data = strdup(value); |
| 45 | return; |
| 46 | case WESTON_OPTION_BOOLEAN: |
| 47 | * (int32_t *) option->data = 1; |
| 48 | return; |
| 49 | default: |
| 50 | assert(0); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | int |
| 55 | parse_options(const struct weston_option *options, |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 56 | int count, int *argc, char *argv[]) |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 57 | { |
| 58 | int i, j, k, len = 0; |
| 59 | |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 60 | for (i = 1, j = 1; i < *argc; i++) { |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 61 | for (k = 0; k < count; k++) { |
| 62 | if (options[k].name) |
| 63 | len = strlen(options[k].name); |
| 64 | if (options[k].name && |
| 65 | argv[i][0] == '-' && |
| 66 | argv[i][1] == '-' && |
| 67 | strncmp(options[k].name, &argv[i][2], len) == 0 && |
| 68 | (argv[i][len + 2] == '=' || argv[i][len + 2] == '\0')) { |
| 69 | handle_option(&options[k], &argv[i][len + 3]); |
| 70 | break; |
| 71 | } else if (options[k].short_name && |
| 72 | argv[i][0] == '-' && |
| 73 | options[k].short_name == argv[i][1]) { |
| 74 | handle_option(&options[k], &argv[i][2]); |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | if (k == count) |
| 79 | argv[j++] = argv[i]; |
| 80 | } |
| 81 | argv[j] = NULL; |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 82 | *argc = j; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 83 | |
| 84 | return j; |
| 85 | } |