Partly revert "shl: handle pathconf() errors"
[platform/upstream/kmscon.git] / src / shl_misc.h
1 /*
2  * shl - Miscellaneous small helpers
3  *
4  * Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
5  * Copyright (c) 2011 University of Tuebingen
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files
9  * (the "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26
27 /*
28  * Miscellaneous helpers
29  */
30
31 #ifndef SHL_MISC_H
32 #define SHL_MISC_H
33
34 #include <dirent.h>
35 #include <errno.h>
36 #include <limits.h>
37 #include <stdbool.h>
38 #include <stddef.h>
39 #include <stdint.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <xkbcommon/xkbcommon.h>
45
46 #define SHL_EXPORT __attribute__((visibility("default")))
47 #define SHL_HAS_BITS(_bitmask, _bits) (((_bitmask) & (_bits)) == (_bits))
48 #define SHL_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
49 #define SHL_ULONG_BITS (sizeof(unsigned long) * 8)
50
51 static inline int shl_dirent(const char *path, struct dirent **ent)
52 {
53         size_t len;
54         struct dirent *tmp;
55         long name_max;
56
57         /* errno may be left unchanged, see pathconf(3p) */
58         errno = 0;
59         name_max = pathconf(path, _PC_NAME_MAX);
60         if (name_max < 0) {
61                 if (errno)
62                         return -errno;
63                 else
64                         return -EINVAL;
65         }
66
67         len = offsetof(struct dirent, d_name) + name_max + 1;
68         tmp = malloc(len);
69         if (!tmp)
70                 return -ENOMEM;
71
72         *ent = tmp;
73         return 0;
74 }
75
76 static inline int shl_strtou(const char *input, unsigned int *output)
77 {
78         unsigned long val;
79         unsigned int res;
80         char *tmp = NULL;
81
82         if (!input || !*input)
83                 return -EINVAL;
84
85         errno = 0;
86         val = strtoul(input, &tmp, 0);
87
88         res = val;
89         if (!tmp || *tmp || errno || (unsigned long)res != val)
90                 return -EINVAL;
91
92         if (output)
93                 *output = res;
94         return 0;
95 }
96
97 static inline int shl_dup(void **out, const void *data, size_t size)
98 {
99         void *cpy;
100
101         if (!data || !size)
102                 return -EINVAL;
103
104         cpy = malloc(size);
105         if (!cpy)
106                 return -ENOMEM;
107
108         memcpy(cpy, data, size);
109         *out = cpy;
110         return 0;
111 }
112
113 static inline bool shl_ends_with(const char *str, const char *suffix)
114 {
115         size_t len, slen;
116
117         len = strlen(str);
118         slen = strlen(suffix);
119
120         if (len < slen)
121                 return false;
122
123         return !memcmp(str + len - slen, suffix, slen);
124 }
125
126 static inline unsigned long shl_next_pow2(unsigned long num)
127 {
128         unsigned int i;
129
130         if (!num)
131                 return 0;
132
133         --num;
134         for (i = 1; i < sizeof(unsigned long) * CHAR_BIT; i <<= 1)
135                 num = num | num >> i;
136
137         return num + 1;
138 }
139
140 /* This parses \arg and splits the string into a new allocated array. The array
141  * is stored in \out and is NULL terminated. Empty entries are removed from the
142  * array if \keep_empty is false. \out_num is the number of entries in the
143  * array. You can set it to NULL to not retrieve this value.
144  * \sep is the separator character which must be a valid ASCII character,
145  * otherwise this will not be UTF8 safe. */
146 static inline int shl_split_string(const char *arg, char ***out,
147                                    unsigned int *out_num, char sep,
148                                    bool keep_empty)
149 {
150         unsigned int i;
151         unsigned int num, len, size, pos;
152         char **list, *off;
153
154         if (!arg || !out || !sep)
155                 return -EINVAL;
156
157         num = 0;
158         size = 0;
159         len = 0;
160         for (i = 0; arg[i]; ++i) {
161                 if (arg[i] != sep) {
162                         ++len;
163                         continue;
164                 }
165
166                 if (keep_empty || len) {
167                         ++num;
168                         size += len + 1;
169                         len = 0;
170                 }
171         }
172
173         if (len > 0 || (keep_empty && (!i || arg[i - 1] == sep))) {
174                 ++num;
175                 size += len + 1;
176         }
177
178         list = malloc(sizeof(char*) * (num + 1) + size);
179         if (!list)
180                 return -ENOMEM;
181
182         off = (void*)(((char*)list) + (sizeof(char*) * (num + 1)));
183         i = 0;
184         for (pos = 0; pos < num; ) {
185                 list[pos] = off;
186                 while (arg[i] && arg[i] != sep)
187                         *off++ = arg[i++];
188                 if (arg[i])
189                         ++i;
190                 if (list[pos] == off && !keep_empty)
191                         continue;
192                 *off++ = 0;
193                 pos++;
194         }
195         list[pos] = NULL;
196
197         *out = list;
198         if (out_num)
199                 *out_num = num;
200         return 0;
201 }
202
203 static inline int shl_dup_array_size(char ***out, char **argv, size_t len)
204 {
205         char **t, *off;
206         unsigned int size, i;
207
208         if (!out || !argv)
209                 return -EINVAL;
210
211         size = 0;
212         for (i = 0; i < len; ++i) {
213                 ++size;
214                 if (argv[i])
215                         size += strlen(argv[i]);
216         }
217         ++i;
218
219         size += i * sizeof(char*);
220
221         t = malloc(size);
222         if (!t)
223                 return -ENOMEM;
224         *out = t;
225
226         off = (char*)t + i * sizeof(char*);
227         while (len--) {
228                 *t++ = off;
229                 for (i = 0; *argv && argv[0][i]; ++i)
230                         *off++ = argv[0][i];
231                 *off++ = 0;
232                 argv++;
233         }
234         *t = NULL;
235
236         return 0;
237 }
238
239 static inline int shl_dup_array(char ***out, char **argv)
240 {
241         unsigned int i;
242
243         if (!out || !argv)
244                 return -EINVAL;
245
246         for (i = 0; argv[i]; ++i)
247                 /* empty */ ;
248
249         return shl_dup_array_size(out, argv, i);
250 }
251
252 /* returns true if the string-list contains only a single entry \entry */
253 static inline bool shl_string_list_is(char **list, const char *entry)
254 {
255         if (!list || !entry)
256                 return false;
257         if (!list[0] || list[1])
258                 return false;
259         return !strcmp(list[0], entry);
260 }
261
262 static inline unsigned int shl_string_list_count(char **list, bool ignore_empty)
263 {
264         unsigned int num;
265
266         if (!list)
267                 return 0;
268
269         for (num = 0; *list; ++list)
270                 if (**list || !ignore_empty)
271                         ++num;
272
273         return num;
274 }
275
276 /* reads a whole file into a buffer with 0-termination */
277 static inline int shl_read_file(const char *path, char **out, size_t *size)
278 {
279         FILE *ffile;
280         ssize_t len;
281         char *buf;
282         int ret;
283
284         if (!path || !out)
285                 return -EINVAL;
286
287         errno = 0;
288
289         ffile = fopen(path, "rb");
290         if (!ffile)
291                 return -errno;
292
293         if (fseek(ffile, 0, SEEK_END) != 0) {
294                 ret = -errno;
295                 goto err_close;
296         }
297
298         len = ftell(ffile);
299         if (len < 0) {
300                 ret = -errno;
301                 goto err_close;
302         }
303
304         rewind(ffile);
305
306         buf = malloc(len + 1);
307         if (!buf) {
308                 ret = -ENOMEM;
309                 goto err_close;
310         }
311
312         errno = 0;
313         if (len && len != fread(buf, 1, len, ffile)) {
314                 ret = errno ? -errno : -EFAULT;
315                 goto err_free;
316         }
317
318         buf[len] = 0;
319         *out = buf;
320         if (size)
321                 *size = len;
322         ret = 0;
323         goto err_close;
324
325 err_free:
326         free(buf);
327 err_close:
328         fclose(ffile);
329         return ret;
330 }
331
332 /* TODO: xkbcommon should provide these flags!
333  * We currently copy them into each library API we use so we need  to keep
334  * them in sync. Currently, they're used in uterm-input and tsm-vte. */
335 enum shl_xkb_mods {
336         SHL_SHIFT_MASK          = (1 << 0),
337         SHL_LOCK_MASK           = (1 << 1),
338         SHL_CONTROL_MASK        = (1 << 2),
339         SHL_ALT_MASK            = (1 << 3),
340         SHL_LOGO_MASK           = (1 << 4),
341 };
342
343 static inline unsigned int shl_get_xkb_mods(struct xkb_state *state)
344 {
345         unsigned int mods = 0;
346
347         if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_SHIFT,
348                                          XKB_STATE_MODS_EFFECTIVE) > 0)
349                 mods |= SHL_SHIFT_MASK;
350         if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
351                                          XKB_STATE_MODS_EFFECTIVE) > 0)
352                 mods |= SHL_LOCK_MASK;
353         if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
354                                          XKB_STATE_MODS_EFFECTIVE) > 0)
355                 mods |= SHL_CONTROL_MASK;
356         if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
357                                          XKB_STATE_MODS_EFFECTIVE) > 0)
358                 mods |= SHL_ALT_MASK;
359         if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_LOGO,
360                                          XKB_STATE_MODS_EFFECTIVE) > 0)
361                 mods |= SHL_LOGO_MASK;
362
363         return mods;
364 }
365
366 static inline uint32_t shl_get_ascii(struct xkb_state *state, uint32_t keycode,
367                                      const uint32_t *keysyms,
368                                      unsigned int num_keysyms)
369 {
370         struct xkb_keymap *keymap;
371         xkb_layout_index_t num_layouts;
372         xkb_layout_index_t layout;
373         xkb_level_index_t level;
374         const xkb_keysym_t *syms;
375         int num_syms;
376
377         if (num_keysyms == 1 && keysyms[0] < 128)
378                 return keysyms[0];
379
380         keymap = xkb_state_get_keymap(state);
381         num_layouts = xkb_keymap_num_layouts_for_key(keymap, keycode);
382
383         for (layout = 0; layout < num_layouts; layout++) {
384                 level = xkb_state_key_get_level(state, keycode, layout);
385                 num_syms = xkb_keymap_key_get_syms_by_level(keymap, keycode,
386                                                         layout, level, &syms);
387                 if (num_syms != 1)
388                         continue;
389
390                 if (syms[0] < 128)
391                         return syms[0];
392         }
393
394         return XKB_KEY_NoSymbol;
395 }
396
397 static inline bool shl_grab_matches(unsigned int ev_mods,
398                                     unsigned int ev_num_syms,
399                                     const uint32_t *ev_syms,
400                                     unsigned int grab_mods,
401                                     unsigned int grab_num_syms,
402                                     const uint32_t *grab_syms)
403 {
404         if (!SHL_HAS_BITS(ev_mods, grab_mods))
405                 return false;
406
407         if (grab_num_syms != 0) {
408                 if (ev_num_syms != grab_num_syms)
409                         return false;
410                 if (memcmp(ev_syms, grab_syms, sizeof(uint32_t) * ev_num_syms))
411                         return false;
412         }
413
414         return true;
415 }
416
417 static inline bool shl_grab_has_match(unsigned int ev_mods,
418                                       unsigned int ev_num_syms,
419                                       const uint32_t *ev_syms,
420                                       unsigned int grab_num,
421                                       const unsigned int *grab_mods,
422                                       const unsigned int *grab_num_syms,
423                                       uint32_t **grab_syms)
424 {
425         unsigned int i;
426
427         for (i = 0; i < grab_num; ++i) {
428                 if (shl_grab_matches(ev_mods, ev_num_syms, ev_syms,
429                                      grab_mods[i], grab_num_syms[i],
430                                      grab_syms[i]))
431                         return true;
432         }
433
434         return false;
435 }
436
437 #endif /* SHL_MISC_H */