| 1 |
/* |
| 2 |
* Copyright © 2025 Pierre Le Marre <[email protected]> |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "xkbcommon/xkbcommon.h" |
| 9 |
|
| 10 |
#include "src/darray.h" |
| 11 |
#include "src/scanner-utils.h" |
| 12 |
#include "src/utils.h" |
| 13 |
#include "ast.h" |
| 14 |
|
| 15 |
XKB_EXPORT_PRIVATE const char * |
| 16 |
xkb_file_type_name(enum xkb_file_type type); |
| 17 |
|
| 18 |
XKB_EXPORT_PRIVATE const char * |
| 19 |
xkb_merge_mode_name(enum merge_mode type); |
| 20 |
|
| 21 |
XKB_EXPORT_PRIVATE const char * |
| 22 |
xkb_map_flags_string_iter(unsigned int *index, enum xkb_map_flags flags); |
| 23 |
|
| 24 |
XKB_EXPORT_PRIVATE FILE * |
| 25 |
xkb_resolve_file(struct xkb_context *ctx, |
| 26 |
enum xkb_file_type file_type, |
| 27 |
const char *path, const char *map, |
| 28 |
char *resolved_path, size_t resolved_path_size, |
| 29 |
char *resolved_map, size_t resolved_map_size); |
| 30 |
|
| 31 |
/** An atomic include entry */ |
| 32 |
struct xkb_file_include { |
| 33 |
bool valid:1; |
| 34 |
bool explicit_section:1; |
| 35 |
enum merge_mode merge; |
| 36 |
/* |
| 37 |
* The following are indices from `xkb_file_section::buffer` |
| 38 |
* |
| 39 |
* Use `xkb_file_section_get_string()` to retrieve them. |
| 40 |
*/ |
| 41 |
darray_size_t path; |
| 42 |
darray_size_t file; |
| 43 |
darray_size_t section; |
| 44 |
darray_size_t modifier; |
| 45 |
/* Flags of the included section, only set if valid = true */ |
| 46 |
enum xkb_map_flags flags; |
| 47 |
}; |
| 48 |
|
| 49 |
struct xkb_file_include_group { |
| 50 |
darray_size_t start; |
| 51 |
darray_size_t end; |
| 52 |
}; |
| 53 |
|
| 54 |
/** A file section: `xkb_{keycodes,compat,geometry,symbols,types} */ |
| 55 |
struct xkb_file_section { |
| 56 |
darray_size_t name; |
| 57 |
enum xkb_file_type file_type; |
| 58 |
enum xkb_map_flags flags; |
| 59 |
darray(struct xkb_file_include_group) include_groups; |
| 60 |
darray(struct xkb_file_include) includes; |
| 61 |
/** |
| 62 |
* Char array to store all the strings |
| 63 |
* |
| 64 |
* Use `xkb_file_section_get_string()` to retrieve them. |
| 65 |
*/ |
| 66 |
darray_char buffer; |
| 67 |
}; |
| 68 |
|
| 69 |
XKB_EXPORT_PRIVATE void |
| 70 |
xkb_file_section_init(struct xkb_file_section *section); |
| 71 |
|
| 72 |
XKB_EXPORT_PRIVATE void |
| 73 |
xkb_file_section_free(struct xkb_file_section *section); |
| 74 |
|
| 75 |
enum xkb_file_iterator_flags { |
| 76 |
XKB_FILE_ITERATOR_NO_FLAG = 0, |
| 77 |
/** Include errors are fatal */ |
| 78 |
XKB_FILE_ITERATOR_FAIL_ON_INCLUDE_ERROR = (1 << 0), |
| 79 |
/** Do not list includes */ |
| 80 |
XKB_FILE_ITERATOR_NO_INCLUDES = (1 << 1) |
| 81 |
}; |
| 82 |
|
| 83 |
/** |
| 84 |
* Parse a single keymap file section |
| 85 |
* |
| 86 |
* @param[in] ctx The XKB context |
| 87 |
* @param[in] iterator_flags The flags used to parse the section |
| 88 |
* @param[in] format The keymap format used to parse the keymap file |
| 89 |
* @param[in] compile_flags The flags used to parse the keymap file |
| 90 |
* @param[in] path The path of the keymap file to parse |
| 91 |
* @param[in] map The name of a specific section in the file |
| 92 |
* @param[inout] section The resulting section |
| 93 |
* |
| 94 |
* @returns `true` on success, else `false` |
| 95 |
*/ |
| 96 |
XKB_EXPORT_PRIVATE bool |
| 97 |
xkb_file_section_parse(struct xkb_context *ctx, |
| 98 |
enum xkb_file_iterator_flags iterator_flags, |
| 99 |
enum xkb_keymap_format format, |
| 100 |
enum xkb_keymap_compile_flags compile_flags, |
| 101 |
unsigned int include_depth, |
| 102 |
const char *path, const char *map, |
| 103 |
struct xkb_file_section *section); |
| 104 |
|
| 105 |
XKB_EXPORT_PRIVATE const char * |
| 106 |
xkb_file_section_get_string(const struct xkb_file_section *section, |
| 107 |
darray_size_t idx); |
| 108 |
|
| 109 |
/** Iterator over the sections of a keymap file */ |
| 110 |
struct xkb_file_iterator { |
| 111 |
enum xkb_file_iterator_flags flags; |
| 112 |
bool finished; |
| 113 |
/** File to analyze */ |
| 114 |
const char *path; |
| 115 |
/** Section to analyze; if NULL then iter over all the sections */ |
| 116 |
const char *map; |
| 117 |
/** File type to check/filter */ |
| 118 |
enum xkb_file_type type; |
| 119 |
/** Scanner of the keymap file */ |
| 120 |
struct scanner scanner; |
| 121 |
/** Current section */ |
| 122 |
struct xkb_file_section section; |
| 123 |
/** Pending XKB file */ |
| 124 |
XkbFile *pending_xkb_file; |
| 125 |
/** Pending component of the XKB file */ |
| 126 |
XkbFile *pending_section; |
| 127 |
struct xkb_context *ctx; |
| 128 |
}; |
| 129 |
|
| 130 |
/** |
| 131 |
* Create a keymap file section iterator from a buffer |
| 132 |
* |
| 133 |
* @param ctx The XKB context |
| 134 |
* @param iterator_flags The flags used to parse the section |
| 135 |
* @param format The keymap format used to parse the keymap file |
| 136 |
* @param compile_flags The flags used to parse the keymap file |
| 137 |
* @param path The path of the keymap file parsed (informative) |
| 138 |
* @param map The name of a specific section in the file, else NULL |
| 139 |
* to iterate over all the sections of the file. |
| 140 |
* @param file_type If the input is a keymap, iterate only over the |
| 141 |
* corresponding component; else check that the parsed file type matches. |
| 142 |
* @param string The buffer to parse |
| 143 |
* @param length The length of the buffer to parse |
| 144 |
* |
| 145 |
* @returns A `malloc`ed file iterator on success, else `NULL`. |
| 146 |
*/ |
| 147 |
XKB_EXPORT_PRIVATE struct xkb_file_iterator * |
| 148 |
xkb_file_iterator_new_from_buffer(struct xkb_context *ctx, |
| 149 |
enum xkb_file_iterator_flags iterator_flags, |
| 150 |
enum xkb_keymap_format format, |
| 151 |
enum xkb_keymap_compile_flags compile_flags, |
| 152 |
const char *path, const char *map, |
| 153 |
enum xkb_file_type file_type, |
| 154 |
const char *string, size_t length); |
| 155 |
|
| 156 |
XKB_EXPORT_PRIVATE void |
| 157 |
xkb_file_iterator_free(struct xkb_file_iterator *iter); |
| 158 |
|
| 159 |
XKB_EXPORT_PRIVATE bool |
| 160 |
xkb_file_iterator_next(struct xkb_file_iterator *iter, |
| 161 |
const struct xkb_file_section **section); |