Add context flag to inhibit default include paths
[platform/upstream/libxkbcommon.git] / src / context.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Author: Daniel Stone <daniel@fooishbar.org>
24  */
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <unistd.h>
30
31 #include "xkb-priv.h"
32
33 struct xkb_context {
34     int refcnt;
35     char **include_paths;
36     int num_include_paths;
37     int size_include_paths;
38 };
39
40 /**
41  * Append one directory to the context's include path.
42  */
43 _X_EXPORT int
44 xkb_context_include_path_append(struct xkb_context *context, const char *path)
45 {
46     struct stat stat_buf;
47     int err;
48
49     if (context->size_include_paths <= context->num_include_paths) {
50         int new_size;
51         char **new_paths;
52         new_size = context->size_include_paths + 2;
53         new_paths = uTypedRecalloc(context->include_paths,
54                                    context->size_include_paths,
55                                    new_size,
56                                    char *);
57         if (!new_paths)
58             return 0;
59         context->include_paths = new_paths;
60         context->size_include_paths = new_size;
61     }
62
63     err = stat(path, &stat_buf);
64     if (err != 0)
65         return 0;
66     if (!S_ISDIR(stat_buf.st_mode))
67         return 0;
68     if (eaccess(path, R_OK | X_OK) != 0)
69         return 0;
70
71     context->include_paths[context->num_include_paths] = strdup(path);
72     if (!context->include_paths[context->num_include_paths])
73         return 0;
74     context->num_include_paths++;
75
76     return 1;
77 }
78
79 /**
80  * Append the default include directories to the context.
81  */
82 _X_EXPORT int
83 xkb_context_include_path_append_default(struct xkb_context *context)
84 {
85     const char *home = getenv("HOME");
86     char *user_path;
87     int err;
88
89     (void) xkb_context_include_path_append(context, DFLT_XKB_CONFIG_ROOT);
90
91     home = getenv("HOME");
92     if (!home)
93         return 1;
94     err = asprintf(&user_path, "%s/.xkb", home);
95     if (err <= 0)
96         return 1;
97     (void) xkb_context_include_path_append(context, user_path);
98     free(user_path);
99
100     return 1;
101 }
102
103 /**
104  * Remove all entries in the context's include path.
105  */
106 _X_EXPORT void
107 xkb_context_include_path_clear(struct xkb_context *context)
108 {
109     int i;
110
111     for (i = 0; i < context->num_include_paths; i++) {
112         free(context->include_paths[i]);
113         context->include_paths[i] = NULL;
114     }
115     free(context->include_paths);
116     context->include_paths = NULL;
117     context->num_include_paths = 0;
118 }
119
120 /**
121  * xkb_context_include_path_clear() + xkb_context_include_path_append_default()
122  */
123 _X_EXPORT int
124 xkb_context_include_path_reset_defaults(struct xkb_context *context)
125 {
126     xkb_context_include_path_clear(context);
127     return xkb_context_include_path_append_default(context);
128 }
129
130 /**
131  * Returns the number of entries in the context's include path.
132  */
133 _X_EXPORT unsigned int
134 xkb_context_num_include_paths(struct xkb_context *context)
135 {
136     return context->num_include_paths;
137 }
138
139 /**
140  * Returns the given entry in the context's include path, or NULL if an
141  * invalid index is passed.
142  */
143 _X_EXPORT const char *
144 xkb_context_include_path_get(struct xkb_context *context, unsigned int idx)
145 {
146     if (idx >= xkb_context_num_include_paths(context))
147         return NULL;
148
149     return context->include_paths[idx];
150 }
151
152 /**
153  * Take a new reference on the context.
154  */
155 _X_EXPORT struct xkb_context *
156 xkb_context_ref(struct xkb_context *context)
157 {
158     context->refcnt++;
159     return context;
160 }
161
162 /**
163  * Drop an existing reference on the context, and free it if the refcnt is
164  * now 0.
165  */
166 _X_EXPORT void
167 xkb_context_unref(struct xkb_context *context)
168 {
169     if (--context->refcnt > 0)
170         return;
171
172     xkb_context_include_path_clear(context);
173     free(context);
174 }
175
176 /**
177  * Create a new context.
178  */
179 _X_EXPORT struct xkb_context *
180 xkb_context_new(enum xkb_context_flags flags)
181 {
182     struct xkb_context *context = calloc(1, sizeof(*context));
183
184     if (!context)
185         return NULL;
186
187     context->refcnt = 1;
188
189     if (!(flags & XKB_CONTEXT_NO_DEFAULT_INCLUDES) &&
190         !xkb_context_include_path_append_default(context)) {
191         xkb_context_unref(context);
192         return NULL;
193     }
194
195     return context;
196 }