src/utils.h


Log

Author Commit Date CI Message
Peter Hutterer 127f8c97 2021-03-30T08:09:37 utils: assert on streq for NULL pointers We have streq_null for that purpose Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Peter Hutterer d3274752 2020-09-07T19:49:36 utils: include unistd.h where we have it MacOS doesn't have eaccess/euidaccess but it does have unistd.h, so let's include it to silence the R_OK redefinition compiler warnings. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Peter Hutterer 41a7c975 2020-07-10T14:57:57 Add asprintf_safe helper function We only ever care about whether we error out or not, so let's wrap this into something more sane. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Peter Hutterer fe886133 2020-06-23T11:07:53 utils: add streq_null() for streq that allows NULL values Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Peter Hutterer 9b1b0c57 2020-06-16T10:34:07 Add a snprintf_safe() helper function Returns true on success or false on error _or_ truncation. Since truncation is almost always an error anyway, we might as well make this easier to check. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Peter Hutterer d075c3e6 2020-06-01T14:16:23 Factor the access check for paths out Easier to re-use without having to duplicate ifdefs. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Ran Benita 0fdd29bb 2019-12-27T13:55:24 utils: move macro defines to before they're used Signed-off-by: Ran Benita <ran@unusedvar.com>
Ran Benita 64137a4a 2019-12-27T13:52:51 utils: fix typo in strndup fallback Fixup 93a1305 - we will have CI for this soon. Signed-off-by: Ran Benita <ran@unusedvar.com>
Adrian Perez de Castro bdff8ebe 2019-08-05T16:18:05 Provide a fallback implementation of [v]asprintf() Some environments (e.g. Windows + MSVC) do not provide asprintf() or vasprintf(). This tries to detect their presence, and provides suitable fallback implementations when not available.
Adrian Perez de Castro 93a13050 2019-08-05T16:07:57 Provide a fallback implementation of strndup() Some environments (e.g. Windows + MSVC) do not provide strndup(), this tries to detect its presence and provide a fallback implementation when not available. [ran: some tweaks]
Ran Benita 34122f9f 2019-12-27T12:34:49 utils: use MIN/MAX instead of min/max min/max symbols conflict on some systems (msvc), so just use the macros. Signed-off-by: Ran Benita <ran@unusedvar.com>
Ran Benita 2af474e8 2019-11-02T13:31:44 parser: get rid of "stealing" atoms This requires (well, at least implemented by) casting away `const` which is undefined behavior, and clang started to warn about it. The micro optimization didn't save too many allocations, anyway. Signed-off-by: Ran Benita <ran@unusedvar.com>
Michael Forney 9d58bbd4 2019-06-04T14:01:02 Use bitwise test instead of popcount to check if one bit is set We don't need to determine the total number of bits set to determine if exactly one is set. Additionally, on x86_64 without any -march=* flag, __builtin_popcount will get compiled to a function call to the compiler runtime (on gcc), or a long sequence of bit operations (on clang). Signed-off-by: Michael Forney <mforney@mforney.org>
Ran Benita b5586a6c 2016-12-02T22:15:19 keysym: fix locale dependence in xkb_keysym_from_name() We currently use strcasecmp, which is locale-dependent. In particular, one well-known surprise even if restricted just ASCII input is found in the tr_TR (Turkish) locale, see e.g. https://msdn.microsoft.com/en-us/library/ms973919.aspx#stringsinnet20_topic5 We have known to avoid locale-dependent functions before, but in this case, we forgot. Fix it by implementing our own simple ASCII-only strcasecmp/strncasecmp. Might have been possible to use strcasecmp_l() with the C locale, but went the easy route. Side advantage is that even this non-optimized version is faster than the optimized libc one (__strcasecmp_l_sse42) since it doesn't need to do the locale stuff. xkb_keysym_from_name(), which uses strcasecmp heavily, becomes faster, and so for example Compose file parsing, which uses xkb_keysym_from_name() heavily, becomes ~20% faster. Resolves https://github.com/xkbcommon/libxkbcommon/issues/42 Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 327364d2 2016-11-14T17:37:35 utils: rename popcount to avoid conflict in NetBSD Resolves https://github.com/xkbcommon/libxkbcommon/issues/41 Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 90611719 2016-02-27T22:29:57 utils: add popcount function Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 2cca0289 2015-11-19T00:44:27 src/utils: change map_file to not take const string argument map_file() uses PROT_READ, so const seems fitting; however unmap_file calls munmap/free, which do not take const, so an UNCONSTIFY is needed. To avoid the UNCONSTIFY hack, which is likely undefined behavior or some such, just remove the const. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita ead816e5 2014-08-09T22:35:24 utils: add a STATIC_ASSERT macro It'd be nicer to use C11's static_assert(), but it's easier to roll our own C99 version using a trick I saw in xv6. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 9c30d6da 2014-06-15T15:30:51 x11: don't iterate on empty batches If count % SIZE == 0 we did a useless iteration where start==stop. It's harmless but strange, so don't do that. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 00f084b6 2014-04-22T14:34:57 utils: detect overflow in memdup() Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita f5465b56 2014-02-08T16:12:09 x11: make msb_pos return unsigned It was initially returning -1 for all-zero arguments, but now it returns 0. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 10643d8a 2014-02-08T12:29:51 Define likely()/unlikely() macros It serves as nice "hotspot" annotations, and can also help things, so why not. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 109fe705 2014-02-04T02:53:05 Use secure_getenv when available We probably don't want to get a privileged process to compile arbitrary keymaps. So we should be careful about the envvars which control include paths or default RMLVOs. But then secure_getenv is more sensible for everything we do. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita eb348255 2013-07-20T23:21:44 x11: add XKB protocol keymap and state creation support These are function to create an xkb_keymap directly from XKB requests to the X server. This opens up the possibility for X clients to use xcb + xcb-xkb + xkbcommon as a proper replacement for Xlib + xkbfile for keyboard support. The X11 support must be enabled with --enable-x11 for now. The functions are in xkbcommon/xkbcommon-x11.h. It depends on a recent libxcb with xkb enabled. The functions are in a new libxkbcommon-x11.so, with a new pkg-config file, etc. so that the packages may be split, and libxkbcommon.so itself remains dependency-free. Why not just use the RMLVO that the server puts in the _XKB_RULES_NAMES property? This does not account for custom keymaps, on-the-fly keymap modifications, remote clients, etc., so is not a proper solution in practice. Also, some servers don't even set it. Now, the client just needs to recreate the keymap in response to a change in the server's keymap (as Xlib clients do with XRefreshKeyboardMapping() and friends). Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 1e6e5669 2013-12-14T17:39:11 ast: pack the ParseCommon struct This shows a measurable improvement in memory and performance for free, on 64bit at least. Packing is (or should be) safe in this case. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita dcdd4e10 2013-10-14T18:59:53 Replace ctype.h functions with ascii ones ctype.h is locale-dependent, so using it in our scanners is not optimal. Let's be deterministic with our own simple functions. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita e91d2653 2013-08-01T23:09:46 scanner: allow empty key name literals Some keymaps actually have this, like the quartz.xkb which is tested. We need to support these. https://bugs.freedesktop.org/show_bug.cgi?id=67654 Reported-By: Gatis Paeglis <gatis.paeglis@digia.com> Signed-off-by: Ran Benita <ran234@gmail.com>
Daniel Stone 17a956d8 2013-05-09T14:47:09 Widen keycode range to 8/255 if possible (bug #63390) If the keycode range is smaller than 8 → 255, artifically widen it when dumping the keymap as not to displease X. Signed-off-by: Daniel Stone <daniel@fooishbar.org>
Ran Benita e4bceec8 2013-03-14T14:33:40 utils: add {un,}map_file to read an entire file This wraps the current mmap call and adds a fallback implementation for systems which do not have mmap (e.g. mingw). Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 17985511 2012-10-16T21:09:33 utils: add and use ARRAY_SIZE macro Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita e44cd2e9 2012-09-24T10:55:20 symbols: move keysyms into LevelInfo Instead of maintaining a syms array in the GroupInfo + sym_index's in the levels. This simplifies the code somewhat. In order not to alloc for every level instead of every group, we only do it if the level has more than one keysym (with a union). Since for now this is a special case, it actually works out better memory-wise. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita b2110705 2012-09-16T14:45:32 Organize src/ and test/ headers - Add context.h and move context-related functions from xkb-priv.h to it. - Move xkb_context definition back to context.c. - Add keysym.h and move keysym upper/lower/keypad from xkb-priv.h to it. - Rename xkb-priv.h to map.h since it only contains keymap-related definitions and declarations now. - Remove unnecessary includes and some and some other small cleanups. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita db45d664 2012-09-10T13:34:36 symbols: add GroupInfo GroupInfo keeps all of the info for a specific group in one struct. This is the old array-of-structures vs. structure-of-arrays, but in this case readability wins. It would also help with lifting the XkbNumKbdGroups limit, because we only have to worry about one array (instead of 6). Signed-off-by: Ran Benita <ran234@gmail.com>
Daniel Stone 14cd8c79 2012-09-11T17:00:38 utils: Replace DEC copyright with Ran's This is not something I do often, but I have good reason here ... utils.h has been totally rewritten since import, and now contains no original DEC content. Everything in here has been added by Ran, and I do not believe that any lingering content from previous iterations is substantial enough as to be copyrightable. Replace DEC's copyright (and license with hostile advertising clause) with Ran's boilerplate copyright and license statement. Signed-off-by: Daniel Stone <daniel@fooishbar.org>
Ran Benita 300f3fb1 2012-08-29T10:12:56 Don't printf NULL strings Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 06d7803a 2012-08-30T12:13:37 state: fix mod_names_are_active This function was always returning -1. Adding a test, we see that test/state.c treat the is_active functions as returning booleans, which would treat -1 as success, so we test for > 0 instead (most users would probably get this wrong as well...). Also update the documentation for the are_active functions, and add a ATTR_NULL_SENTINEL for gcc __attribute__((sentinel)). Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita f410622b 2012-08-15T22:07:37 vmod: remove support for resolving integer to virtual modifier This is only relevant to the virtualModifier= statement in interpret statements in xkb_compat. The current code allows to write e.g. virtualModifier = 4 to get the virtual modifier whose index happens to be 4 (possibly declared in other files or sections, i.e. xkb_types). Doing this is undeterministic, will break without notice, and not used anywhere. Don't allow it. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 9de067aa 2012-08-27T21:31:18 compat: ignore "group" (compatibility) statements Group compatibility statements are like the following: group 3 = AltGr; This currently results in: keymap->groups[2].mask = <real mod mapped from AltGr vmod> And we don't do any thing with this value later. The reason it exists in XKB is to support non-XKB clients (i.e. XKB support disabled entirely in the server), which do not know the concept of "group", and use some modifier to distinguish between the first and second keyboard layouts (usually with the AltGr key). We don't care about all of that, so we can forget about it. One artifact of this removal is that xkb_map_num_groups no longer works, because it counted through keymap->groups (this wasn't entirely correct BTW). Instead we add a new num_groups member to the keymap, which just hold the maximum among the xkb_key's num_groups. This also means we don't have to compute anything just to get the number of groups. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita e5353528 2012-08-13T13:49:17 Move ISEMPTY to utils.h Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 89723b7c 2012-07-24T19:54:14 utils: add/replace string equality macros It's more tidy and less error prone, since we use strcasecmp == 0 a lot. We replace strcmp == 0 by streq, strcasecmp == 0 by istreq, uStrCasePrefix by istreq_prefix and uDupString by strdup_safe. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 2c30fa7a 2012-07-21T16:10:17 Remove old logging leftovers Everything has been converted. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita e037f518 2012-07-21T14:53:49 action: use new log functions Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 8b0e9f92 2012-07-20T13:07:30 utils: remove uTypedAlloc/Calloc Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 71c2f2e0 2012-07-20T17:20:35 utils: replace FATAL by malloc_or_die "Out of memory" is enough in this case. If we want to be OOM-safe this makes it clear where to begin. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 58f8d2c1 2012-07-20T17:09:49 utils: remove Xfuncproto.h and use our own macros Add XKB_EXPORT to replace _X_EXPORT, and copy the definitions of _X_ATTRIBUTE_FOO as ATTR_FOO. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 70f35cfb 2012-07-20T13:10:13 Add logging API Add new public API to provide the library users with some options to control and customize the logging output from the library. It is based upon the skeleton from the libabc demo libray: https://git.kernel.org/?p=linux/kernel/git/kay/libabc.git which is public domain and works pretty well. This requires passing in the context object in every logging call, and thus the conversion is done file by file. We also remove the global warningLevel variable in favor of a verbosity level in the context, which can be set by the user and is silent by default. One issue is the ACTION calls, which, while nice, do not play very well with line- and priority-based logging, and would require some line continuation handling or keeping state or some other compromise. So instead remove these and just inline them with their respective warning/error. So instead of: ERROR("Memory allocation failed\n") ACTION("Removing all files on hardisk\n") its something like that: log_err("Memory allocation failed; Removing all files on harddisk\n") Signed-off-by: Ran Benita <ran234@gmail.com>
Daniel Stone 9308a460 2012-07-17T10:20:15 Run source tree through uncrustify .uncrustify.cfg committed for future reference also, but had to manually fix up a few things: it really likes justifying struct initialisers. Signed-off-by: Daniel Stone <daniel@fooishbar.org>
Ran Benita cd5a8274 2012-06-06T10:44:22 utils: remove unused recalloc and related macros Their use is superseded by darray everywhere now. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita b610b2b9 2012-05-08T14:52:23 Rename XKBcommonint.h to xkb-priv.h and use it Make the files in the src/* directory use their own header or a consilidated private header. This makes the file dependencies clearer. Also drop the pointless "xkb" file name prefix, add split a few declarations to their own files (atom.h and text.h). Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 5eb0a70e 2012-05-07T14:44:30 Don't use typeof clang complains with the xorg-macros warning flags: src/context.c:58:36: error: extension used [-Werror,-pedantic,-Wlanguage-extension-token] typeof(new_paths)); This was not entirely correct, too. So bring back the casts to the results of the allocation macros; might as well make them a bit more type safe. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita a641a185 2012-04-06T03:38:55 Use stdbool.h 'Cause defining your own True and False is so 1990's. Signed-off-by: Ran Benita <ran234@gmail.com> [daniels: Fixed for xkb_desc -> xkb_keymap changes.]
Ran Benita 8fbd44fd 2012-04-06T03:12:50 Implicitly include config.h in all files The definitions in config.h should be available in all files an implementation detail; it can be included through the build system instead of having each file pull it every time. This is especially helpful with AC_USE_SYSTEM_EXTENSIONS, as _GNU_SOURCE and friends can have an effect by merely being defined, which can lead to some confusion if its effective for only half the files. And we don't really support a build _without_ config.h; so, one less thing to worry about. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 813ddf25 2012-03-24T00:29:33 Silence -Wcast-qual warnings There are some cases where we must free a string with a const qualifier. Add a macro UNCONSTIFY to trick the compiler into silencing the warning in the cases where we know what we're doing. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita f08ce9b7 2012-03-24T00:26:12 Use strcasecmp consistently instead of uStrCaseCmp There's no use calling the same thing by a different name. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 3104a8ef 2012-03-24T00:12:08 Move utility macro from XKBcommonint.h to utils.h And merge all the similar ones into the same name. The u* prefix is chosen over the _Xkb prefix because it has more uses throughout the codebase. But It should now be simple to choose a nice prefix and stay consistent. Signed-off-by: Ran Benita <ran234@gmail.com> [daniels: fixed for the case where we have strcasecmp]
Ran Benita 861b0c79 2012-03-23T23:47:26 Rewrite recalloc to the correct type The recalloc function should be expressed in terms of bytes to match its name. However uTypedRecalloc retains its type so nothing is changed. Signed-off-by: Ran Benita <ran234@gmail.com>
Ran Benita 0480f427 2012-03-23T23:28:24 Remove useless stuff from utils Signed-off-by: Ran Benita <ran234@gmail.com> [daniels: fixed conflicts from strcasecmp, added includes to make filecomp build again]
Ran Benita d22b8dbb 2012-03-23T22:25:47 Move utils.{c,h} to be used by the entire project This is a first step for making consistent use of utils.h also outside of xkbcomp/ . Signed-off-by: Ran Benita <ran234@gmail.com>