Add an option parser
[profile/ivi/weston-ivi-shell.git] / shared / option-parser.c
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
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <assert.h>
28
29 #include "config-parser.h"
30
31 static void
32 handle_option(const struct weston_option *option, char *value)
33 {
34         switch (option->type) {
35         case WESTON_OPTION_INTEGER:
36                 * (int32_t *) option->data = strtol(value, NULL, 0);
37                 return;
38         case WESTON_OPTION_UNSIGNED_INTEGER:
39                 * (uint32_t *) option->data = strtoul(value, NULL, 0);
40                 return;
41         case WESTON_OPTION_STRING:
42                 * (char **) option->data = strdup(value);
43                 return;
44         case WESTON_OPTION_BOOLEAN:
45                 * (int32_t *) option->data = 1;
46                 return;
47         default:
48                 assert(0);
49         }
50 }
51
52 int
53 parse_options(const struct weston_option *options,
54               int count, int argc, char *argv[])
55 {
56         int i, j, k, len = 0;
57
58         for (i = 1, j = 1; i < argc; i++) {
59                 for (k = 0; k < count; k++) {
60                         if (options[k].name)
61                                 len = strlen(options[k].name);
62                         if (options[k].name &&
63                             argv[i][0] == '-' &&
64                             argv[i][1] == '-' &&
65                             strncmp(options[k].name, &argv[i][2], len) == 0 &&
66                             (argv[i][len + 2] == '=' || argv[i][len + 2] == '\0')) {
67                                 handle_option(&options[k], &argv[i][len + 3]);
68                                 break;
69                         } else if (options[k].short_name &&
70                                    argv[i][0] == '-' &&
71                                    options[k].short_name == argv[i][1]) {
72                                 handle_option(&options[k], &argv[i][2]);
73                                 break;
74                         }
75                 }
76                 if (k == count)
77                         argv[j++] = argv[i];
78         }
79         argv[j] = NULL;
80
81         return j;
82 }