downstream: terminal: destroy display at the end.
[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 "config.h"
24
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <wayland-util.h>
31
32 #include "config-parser.h"
33 #include "str-util.h"
34
35 static int
36 handle_option(const struct weston_option *option, char *value)
37 {
38         switch (option->type) {
39         case WESTON_OPTION_INTEGER:
40                 return weston_strtoi(value, NULL, 0, (int32_t *)option->data);
41         case WESTON_OPTION_UNSIGNED_INTEGER:
42                 return weston_strtoui(value, NULL, 0, (uint32_t *)option->data);
43         case WESTON_OPTION_STRING:
44                 * (char **) option->data = strdup(value);
45                 return 1;
46         default:
47                 assert(0);
48         }
49 }
50
51 static int
52 long_option(const struct weston_option *options, int count, char *arg)
53 {
54         int k, len;
55
56         for (k = 0; k < count; k++) {
57                 if (!options[k].name)
58                         continue;
59
60                 len = strlen(options[k].name);
61                 if (strncmp(options[k].name, arg + 2, len) != 0)
62                         continue;
63
64                 if (options[k].type == WESTON_OPTION_BOOLEAN) {
65                         if (!arg[len + 2]) {
66                                 * (int32_t *) options[k].data = 1;
67
68                                 return 1;
69                         }
70                 } else if (arg[len+2] == '=') {
71                         return handle_option(options + k, arg + len + 3);
72                 }
73         }
74
75         return 0;
76 }
77
78 static int
79 short_option(const struct weston_option *options, int count, char *arg)
80 {
81         int k;
82
83         if (!arg[1])
84                 return 0;
85
86         for (k = 0; k < count; k++) {
87                 if (options[k].short_name != arg[1])
88                         continue;
89
90                 if (options[k].type == WESTON_OPTION_BOOLEAN) {
91                         if (!arg[2]) {
92                                 * (int32_t *) options[k].data = 1;
93
94                                 return 1;
95                         }
96                 } else {
97                         return handle_option(options + k, arg + 2);
98                 }
99         }
100
101         return 0;
102 }
103
104 int
105 parse_options(const struct weston_option *options,
106               int count, int *argc, char *argv[])
107 {
108         int i, j;
109
110         for (i = 1, j = 1; i < *argc; i++) {
111                 if (argv[i][0] == '-') {
112                         if (argv[i][1] == '-') {
113                                 if (long_option(options, count, argv[i]))
114                                         continue;
115                         } else if (short_option(options, count, argv[i]))
116                                 continue;
117                 }
118                 argv[j++] = argv[i];
119         }
120         argv[j] = NULL;
121         *argc = j;
122
123         return j;
124 }