summaryrefslogtreecommitdiff
path: root/src/utils.h
Commit message (Collapse)AuthorAgeFilesLines
* utils: fix printf format warnings on mingwRan Benita2023-05-061-1/+3
| | | | | | | | | See: https://github.com/mesonbuild/wrapdb/pull/819 https://github.com/Exiv2/exiv2/blob/c86ae6acf597304db37246434ebc393d732c22c2/src/image_int.hpp#L15 https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/ Signed-off-by: Ran Benita <ran@unusedvar.com>
* Possible fix for non-MSVC windows compilersRan Benita2023-04-111-1/+1
| | | | | | | | `_MSC_VER` is specific to MSVC, but there can be other compilers targeting windows. Hopefully they do define `_WIN32`, so let's use that. Refs: https://github.com/xkbcommon/libxkbcommon/issues/305 Signed-off-by: Ran Benita <ran@unusedvar.com>
* utils: move some MSVC compat stuff to common placeRan Benita2022-09-241-5/+13
| | | | Signed-off-by: Ran Benita <ran@unusedvar.com>
* utils: assert on streq for NULL pointersPeter Hutterer2021-03-301-0/+1
| | | | | | We have streq_null for that purpose Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
* utils: include unistd.h where we have itPeter Hutterer2020-09-071-1/+1
| | | | | | | 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>
* Add asprintf_safe helper functionPeter Hutterer2020-07-131-0/+32
| | | | | | | 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>
* utils: add streq_null() for streq that allows NULL valuesPeter Hutterer2020-07-061-0/+8
| | | | Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
* Add a snprintf_safe() helper functionPeter Hutterer2020-07-061-0/+15
| | | | | | | 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>
* Factor the access check for paths outPeter Hutterer2020-07-061-0/+23
| | | | | | Easier to re-use without having to duplicate ifdefs. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
* utils: move macro defines to before they're usedRan Benita2019-12-271-8/+8
| | | | Signed-off-by: Ran Benita <ran@unusedvar.com>
* utils: fix typo in strndup fallbackRan Benita2019-12-271-1/+1
| | | | | | Fixup 93a1305 - we will have CI for this soon. Signed-off-by: Ran Benita <ran@unusedvar.com>
* Provide a fallback implementation of [v]asprintf()Adrian Perez de Castro2019-12-271-0/+8
| | | | | | 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.
* Provide a fallback implementation of strndup()Adrian Perez de Castro2019-12-271-0/+15
| | | | | | | | 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]
* utils: use MIN/MAX instead of min/maxRan Benita2019-12-271-14/+0
| | | | | | min/max symbols conflict on some systems (msvc), so just use the macros. Signed-off-by: Ran Benita <ran@unusedvar.com>
* parser: get rid of "stealing" atomsRan Benita2019-11-081-7/+0
| | | | | | | | | 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>
* Use bitwise test instead of popcount to check if one bit is setMichael Forney2019-06-101-10/+2
| | | | | | | | | | | 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>
* keysym: fix locale dependence in xkb_keysym_from_name()Ran Benita2016-12-021-3/+11
| | | | | | | | | | | | | | | | | | | | | | | 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>
* utils: rename popcount to avoid conflict in NetBSDRan Benita2016-11-141-1/+2
| | | | | Resolves https://github.com/xkbcommon/libxkbcommon/issues/41 Signed-off-by: Ran Benita <ran234@gmail.com>
* utils: add popcount functionRan Benita2016-10-221-0/+13
| | | | Signed-off-by: Ran Benita <ran234@gmail.com>
* src/utils: change map_file to not take const string argumentRan Benita2015-11-191-2/+2
| | | | | | | | | 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>
* utils: add a STATIC_ASSERT macroRan Benita2014-08-091-0/+4
| | | | | | | 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>
* x11: don't iterate on empty batchesRan Benita2014-06-151-0/+3
| | | | | | | 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>
* utils: detect overflow in memdup()Ran Benita2014-04-221-1/+1
| | | | Signed-off-by: Ran Benita <ran234@gmail.com>
* x11: make msb_pos return unsignedRan Benita2014-02-081-3/+3
| | | | | | | It was initially returning -1 for all-zero arguments, but now it returns 0. Signed-off-by: Ran Benita <ran234@gmail.com>
* Define likely()/unlikely() macrosRan Benita2014-02-081-0/+8
| | | | | | | It serves as nice "hotspot" annotations, and can also help things, so why not. Signed-off-by: Ran Benita <ran234@gmail.com>
* Use secure_getenv when availableRan Benita2014-02-041-0/+8
| | | | | | | | | 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>
* x11: add XKB protocol keymap and state creation supportRan Benita2014-02-021-0/+16
| | | | | | | | | | | | | | | | | | | | | | 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>
* ast: pack the ParseCommon structRan Benita2013-12-141-0/+6
| | | | | | | 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>
* Replace ctype.h functions with ascii onesRan Benita2013-10-141-0/+41
| | | | | | | 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>
* scanner: allow empty key name literalsRan Benita2013-08-021-0/+12
| | | | | | | | | | 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>
* Widen keycode range to 8/255 if possible (bug #63390)Daniel Stone2013-05-091-0/+12
| | | | | | | 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>
* utils: add {un,}map_file to read an entire fileRan Benita2013-04-011-0/+8
| | | | | | | 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>
* utils: add and use ARRAY_SIZE macroRan Benita2012-10-161-0/+2
| | | | Signed-off-by: Ran Benita <ran234@gmail.com>
* symbols: move keysyms into LevelInfoRan Benita2012-09-241-0/+9
| | | | | | | | | | 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>
* Organize src/ and test/ headersRan Benita2012-09-161-0/+4
| | | | | | | | | | | | - 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>
* symbols: add GroupInfoRan Benita2012-09-131-0/+2
| | | | | | | | | | 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>
* utils: Replace DEC copyright with Ran'sDaniel Stone2012-09-111-25/+22
| | | | | | | | | | | | | | 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>
* Don't printf NULL stringsRan Benita2012-09-031-0/+6
| | | | Signed-off-by: Ran Benita <ran234@gmail.com>
* state: fix mod_names_are_activeRan Benita2012-09-021-0/+6
| | | | | | | | | | | 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>
* vmod: remove support for resolving integer to virtual modifierRan Benita2012-09-021-0/+8
| | | | | | | | | | | | 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>
* compat: ignore "group" (compatibility) statementsRan Benita2012-09-011-0/+3
| | | | | | | | | | | | | | | | | | | | | | 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>
* Move ISEMPTY to utils.hRan Benita2012-09-011-0/+6
| | | | Signed-off-by: Ran Benita <ran234@gmail.com>
* utils: add/replace string equality macrosRan Benita2012-07-271-3/+25
| | | | | | | | 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>
* Remove old logging leftoversRan Benita2012-07-231-6/+0
| | | | | | Everything has been converted. Signed-off-by: Ran Benita <ran234@gmail.com>
* action: use new log functionsRan Benita2012-07-231-1/+0
| | | | Signed-off-by: Ran Benita <ran234@gmail.com>
* Add logging APIRan Benita2012-07-231-32/+5
| | | | | | | | | | | | | | | | | | | | | | | | | 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>
* utils: replace FATAL by malloc_or_dieRan Benita2012-07-231-5/+6
| | | | | | | "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>
* utils: remove Xfuncproto.h and use our own macrosRan Benita2012-07-231-9/+28
| | | | | | | 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>
* utils: remove uTypedAlloc/CallocRan Benita2012-07-231-3/+0
| | | | Signed-off-by: Ran Benita <ran234@gmail.com>
* Run source tree through uncrustifyDaniel Stone2012-07-171-34/+34
| | | | | | | .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>