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         name_max = pathconf(path, _PC_NAME_MAX);
58         if (name_max < 0)
59                 return -errno;
60
61         len = offsetof(struct dirent, d_name) + name_max + 1;
62         tmp = malloc(len);
63         if (!tmp)
64                 return -ENOMEM;
65
66         *ent = tmp;
67         return 0;
68 }
69
70 static inline int shl_strtou(const char *input, unsigned int *output)
71 {
72         unsigned long val;
73         unsigned int res;
74         char *tmp = NULL;
75
76         if (!input || !*input)
77                 return -EINVAL;
78
79         errno = 0;
80         val = strtoul(input, &tmp, 0);
81
82         res = val;
83         if (!tmp || *tmp || errno || (unsigned long)res != val)
84                 return -EINVAL;
85
86         if (output)
87                 *output = res;
88         return 0;
89 }
90
91 static inline int shl_dup(void **out, const void *data, size_t size)
92 {
93         void *cpy;
94
95         if (!data || !size)
96                 return -EINVAL;
97
98         cpy = malloc(size);
99         if (!cpy)
100                 return -ENOMEM;
101
102         memcpy(cpy, data, size);
103         *out = cpy;
104         return 0;
105 }
106
107 static inline bool shl_ends_with(const char *str, const char *suffix)
108 {
109         size_t len, slen;
110
111         len = strlen(str);
112         slen = strlen(suffix);
113
114         if (len < slen)
115                 return false;
116
117         return !memcmp(str + len - slen, suffix, slen);
118 }
119
120 static inline unsigned long shl_next_pow2(unsigned long num)
121 {
122         unsigned int i;
123
124         if (!num)
125                 return 0;
126
127         --num;
128         for (i = 1; i < sizeof(unsigned long) * CHAR_BIT; i <<= 1)
129                 num = num | num >> i;
130
131         return num + 1;
132 }
133
134 /* This parses \arg and splits the string into a new allocated array. The array
135  * is stored in \out and is NULL terminated. Empty entries are removed from the
136  * array if \keep_empty is false. \out_num is the number of entries in the
137  * array. You can set it to NULL to not retrieve this value.
138  * \sep is the separator character which must be a valid ASCII character,
139  * otherwise this will not be UTF8 safe. */
140 static inline int shl_split_string(const char *arg, char ***out,
141                                    unsigned int *out_num, char sep,
142                                    bool keep_empty)
143 {
144         unsigned int i;
145         unsigned int num, len, size, pos;
146         char **list, *off;
147
148         if (!arg || !out || !sep)
149                 return -EINVAL;
150
151         num = 0;
152         size = 0;
153         len = 0;
154         for (i = 0; arg[i]; ++i) {
155                 if (arg[i] != sep) {
156                         ++len;
157                         continue;
158                 }
159
160                 if (keep_empty || len) {
161                         ++num;
162                         size += len + 1;
163                         len = 0;
164                 }
165         }
166
167         if (len > 0 || (keep_empty && (!i || arg[i - 1] == sep))) {
168                 ++num;
169                 size += len + 1;
170         }
171
172         list = malloc(sizeof(char*) * (num + 1) + size);
173         if (!list)
174                 return -ENOMEM;
175
176         off = (void*)(((char*)list) + (sizeof(char*) * (num + 1)));
177         i = 0;
178         for (pos = 0; pos < num; ) {
179                 list[pos] = off;
180                 while (arg[i] && arg[i] != sep)
181                         *off++ = arg[i++];
182                 if (arg[i])
183                         ++i;
184                 if (list[pos] == off && !keep_empty)
185                         continue;
186                 *off++ = 0;
187                 pos++;
188         }
189         list[pos] = NULL;
190
191         *out = list;
192         if (out_num)
193                 *out_num = num;
194         return 0;
195 }
196
197 static inline int shl_dup_array_size(char ***out, char **argv, size_t len)
198 {
199         char **t, *off;
200         unsigned int size, i;
201
202         if (!out || !argv)
203                 return -EINVAL;
204
205         size = 0;
206         for (i = 0; i < len; ++i) {
207                 ++size;
208                 if (argv[i])
209                         size += strlen(argv[i]);
210         }
211         ++i;
212
213         size += i * sizeof(char*);
214
215         t = malloc(size);
216         if (!t)
217                 return -ENOMEM;
218         *out = t;
219
220         off = (char*)t + i * sizeof(char*);
221         while (len--) {
222                 *t++ = off;
223                 for (i = 0; *argv && argv[0][i]; ++i)
224                         *off++ = argv[0][i];
225                 *off++ = 0;
226                 argv++;
227         }
228         *t = NULL;
229
230         return 0;
231 }
232
233 static inline int shl_dup_array(char ***out, char **argv)
234 {
235         unsigned int i;
236
237         if (!out || !argv)
238                 return -EINVAL;
239
240         for (i = 0; argv[i]; ++i)
241                 /* empty */ ;
242
243         return shl_dup_array_size(out, argv, i);
244 }
245
246 /* returns true if the string-list contains only a single entry \entry */
247 static inline bool shl_string_list_is(char **list, const char *entry)
248 {
249         if (!list || !entry)
250                 return false;
251         if (!list[0] || list[1])
252                 return false;
253         return !strcmp(list[0], entry);
254 }
255
256 static inline unsigned int shl_string_list_count(char **list, bool ignore_empty)
257 {
258         unsigned int num;
259
260         if (!list)
261                 return 0;
262
263         for (num = 0; *list; ++list)
264                 if (**list || !ignore_empty)
265                         ++num;
266
267         return num;
268 }
269
270 /* reads a whole file into a buffer with 0-termination */
271 static inline int shl_read_file(const char *path, char **out, size_t *size)
272 {
273         FILE *ffile;
274         ssize_t len;
275         char *buf;
276         int ret;
277
278         if (!path || !out)
279                 return -EINVAL;
280
281         errno = 0;
282
283         ffile = fopen(path, "rb");
284         if (!ffile)
285                 return -errno;
286
287         if (fseek(ffile, 0, SEEK_END) != 0) {
288                 ret = -errno;
289                 goto err_close;
290         }
291
292         len = ftell(ffile);
293         if (len < 0) {
294                 ret = -errno;
295                 goto err_close;
296         }
297
298         rewind(ffile);
299
300         buf = malloc(len + 1);
301         if (!buf) {
302                 ret = -ENOMEM;
303                 goto err_close;
304         }
305
306         errno = 0;
307         if (len && len != fread(buf, 1, len, ffile)) {
308                 ret = errno ? -errno : -EFAULT;
309                 goto err_free;
310         }
311
312         buf[len] = 0;
313         *out = buf;
314         if (size)
315                 *size = len;
316         ret = 0;
317         goto err_close;
318
319 err_free:
320         free(buf);
321 err_close:
322         fclose(ffile);
323         return ret;
324 }
325
326 /* TODO: xkbcommon should provide these flags!
327  * We currently copy them into each library API we use so we need  to keep
328  * them in sync. Currently, they're used in uterm-input and tsm-vte. */
329 enum shl_xkb_mods {
330         SHL_SHIFT_MASK          = (1 << 0),
331         SHL_LOCK_MASK           = (1 << 1),
332         SHL_CONTROL_MASK        = (1 << 2),
333         SHL_ALT_MASK            = (1 << 3),
334         SHL_LOGO_MASK           = (1 << 4),
335 };
336
337 static inline unsigned int shl_get_xkb_mods(struct xkb_state *state)
338 {
339         unsigned int mods = 0;
340
341         if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_SHIFT,
342                                          XKB_STATE_MODS_EFFECTIVE) > 0)
343                 mods |= SHL_SHIFT_MASK;
344         if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
345                                          XKB_STATE_MODS_EFFECTIVE) > 0)
346                 mods |= SHL_LOCK_MASK;
347         if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
348                                          XKB_STATE_MODS_EFFECTIVE) > 0)
349                 mods |= SHL_CONTROL_MASK;
350         if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
351                                          XKB_STATE_MODS_EFFECTIVE) > 0)
352                 mods |= SHL_ALT_MASK;
353         if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_LOGO,
354                                          XKB_STATE_MODS_EFFECTIVE) > 0)
355                 mods |= SHL_LOGO_MASK;
356
357         return mods;
358 }
359
360 static inline uint32_t shl_get_ascii(struct xkb_state *state, uint32_t keycode,
361                                      const uint32_t *keysyms,
362                                      unsigned int num_keysyms)
363 {
364         struct xkb_keymap *keymap;
365         xkb_layout_index_t num_layouts;
366         xkb_layout_index_t layout;
367         xkb_level_index_t level;
368         const xkb_keysym_t *syms;
369         int num_syms;
370
371         if (num_keysyms == 1 && keysyms[0] < 128)
372                 return keysyms[0];
373
374         keymap = xkb_state_get_keymap(state);
375         num_layouts = xkb_keymap_num_layouts_for_key(keymap, keycode);
376
377         for (layout = 0; layout < num_layouts; layout++) {
378                 level = xkb_state_key_get_level(state, keycode, layout);
379                 num_syms = xkb_keymap_key_get_syms_by_level(keymap, keycode,
380                                                         layout, level, &syms);
381                 if (num_syms != 1)
382                         continue;
383
384                 if (syms[0] < 128)
385                         return syms[0];
386         }
387
388         return XKB_KEY_NoSymbol;
389 }
390
391 static inline bool shl_grab_matches(unsigned int ev_mods,
392                                     unsigned int ev_num_syms,
393                                     const uint32_t *ev_syms,
394                                     unsigned int grab_mods,
395                                     unsigned int grab_num_syms,
396                                     const uint32_t *grab_syms)
397 {
398         if (!SHL_HAS_BITS(ev_mods, grab_mods))
399                 return false;
400
401         if (grab_num_syms != 0) {
402                 if (ev_num_syms != grab_num_syms)
403                         return false;
404                 if (memcmp(ev_syms, grab_syms, sizeof(uint32_t) * ev_num_syms))
405                         return false;
406         }
407
408         return true;
409 }
410
411 static inline bool shl_grab_has_match(unsigned int ev_mods,
412                                       unsigned int ev_num_syms,
413                                       const uint32_t *ev_syms,
414                                       unsigned int grab_num,
415                                       const unsigned int *grab_mods,
416                                       const unsigned int *grab_num_syms,
417                                       uint32_t **grab_syms)
418 {
419         unsigned int i;
420
421         for (i = 0; i < grab_num; ++i) {
422                 if (shl_grab_matches(ev_mods, ev_num_syms, ev_syms,
423                                      grab_mods[i], grab_num_syms[i],
424                                      grab_syms[i]))
425                         return true;
426         }
427
428         return false;
429 }
430
431 #endif /* SHL_MISC_H */