diff options
author | Ran Benita <ran234@gmail.com> | 2013-03-01 18:33:40 +0200 |
---|---|---|
committer | Daniel Stone <daniel@fooishbar.org> | 2013-03-18 22:20:04 +0000 |
commit | 4e8dcca86dfbd27f6a0e2247813d53069c438295 (patch) | |
tree | 29e90d0e50c7b2320b0355ca881096e6794d60ce | |
parent | 540feef3bbfe8fc74caea306eed3af90ca9517ea (diff) | |
download | libxkbcommon-4e8dcca86dfbd27f6a0e2247813d53069c438295.tar.gz libxkbcommon-4e8dcca86dfbd27f6a0e2247813d53069c438295.tar.bz2 libxkbcommon-4e8dcca86dfbd27f6a0e2247813d53069c438295.zip |
text: clean up and fix the *MaskText functions
The snprintf trick that LedStateText and ControlMaskText do cannot work,
because you can't use the buffer as an argument to write to itself!
(posix at least has 'restrict' there). So those two actually never
worked for more than one value (i.e. with a +).
Fix that, and do the same cleanup to ModMaskText. Now we have 3
functions which look exactly the same, oh well.
Also increase the context text buffer size, you never know.
Signed-off-by: Ran Benita <ran234@gmail.com>
-rw-r--r-- | src/context.c | 2 | ||||
-rw-r--r-- | src/keymap-dump.c | 4 | ||||
-rw-r--r-- | src/text.c | 126 | ||||
-rw-r--r-- | src/text.h | 2 |
4 files changed, 57 insertions, 77 deletions
diff --git a/src/context.c b/src/context.c index a846b8f..ded10b2 100644 --- a/src/context.c +++ b/src/context.c @@ -53,7 +53,7 @@ struct xkb_context { struct atom_table *atom_table; /* Buffer for the *Text() functions. */ - char text_buffer[1024]; + char text_buffer[2048]; size_t text_next; }; diff --git a/src/keymap-dump.c b/src/keymap-dump.c index 9be65d0..0ab228b 100644 --- a/src/keymap-dump.c +++ b/src/keymap-dump.c @@ -243,7 +243,7 @@ write_led_map(struct xkb_keymap *keymap, struct buf *buf, if (led->which_groups) { if (led->which_groups != XKB_STATE_LAYOUT_EFFECTIVE) { write_buf(buf, "\t\twhichGroupState= %s;\n", - LedStateText(keymap->ctx, led->which_groups)); + LedStateMaskText(keymap->ctx, led->which_groups)); } write_buf(buf, "\t\tgroups= 0x%02x;\n", led->groups); @@ -252,7 +252,7 @@ write_led_map(struct xkb_keymap *keymap, struct buf *buf, if (led->which_mods) { if (led->which_mods != XKB_STATE_MODS_EFFECTIVE) { write_buf(buf, "\t\twhichModState= %s;\n", - LedStateText(keymap->ctx, led->which_mods)); + LedStateMaskText(keymap->ctx, led->which_mods)); } write_buf(buf, "\t\tmodifiers= %s;\n", ModMaskText(keymap, led->mods.mods)); @@ -211,48 +211,6 @@ const LookupEntry symInterpretMatchMaskNames[] = { }; const char * -ModMaskText(const struct xkb_keymap *keymap, xkb_mod_mask_t mask) -{ - xkb_mod_index_t i; - size_t len; - ssize_t rem; - char *str; - char buf[1024]; - const struct xkb_mod *mod; - - if (mask == 0) - return "none"; - - if (mask == MOD_REAL_MASK_ALL) - return "all"; - - str = buf; - buf[0] = '\0'; - rem = sizeof(buf); - darray_enumerate(i, mod, keymap->mods) { - if (!(mask & (1 << i))) - continue; - - len = snprintf(str, rem, "%s%s", - (str != buf) ? "+" : "", - xkb_atom_text(keymap->ctx, mod->name)); - rem -= len; - str += len; - - if (rem <= 1) - break; - } - str = buf; - - len = strlen(str); - if (len >= sizeof(buf)) - len = sizeof(buf) - 1; - - return strcpy(xkb_context_get_buffer(keymap->ctx, len + 1), str); - -} - -const char * ModIndexText(const struct xkb_keymap *keymap, xkb_mod_index_t ndx) { if (ndx == XKB_MOD_INVALID) @@ -309,49 +267,72 @@ SIMatchText(enum xkb_match_operation type) return LookupValue(symInterpretMatchMaskNames, type); } -#define GET_TEXT_BUF_SIZE 512 +const char * +ModMaskText(const struct xkb_keymap *keymap, xkb_mod_mask_t mask) +{ + char buf[1024]; + size_t pos = 0; + xkb_mod_index_t i; + const struct xkb_mod *mod; + + if (mask == 0) + return "none"; -#define append_get_text(...) do { \ - int _size = snprintf(ret, GET_TEXT_BUF_SIZE, __VA_ARGS__); \ - if (_size >= GET_TEXT_BUF_SIZE) \ - return NULL; \ -} while (0) + if (mask == MOD_REAL_MASK_ALL) + return "all"; + + darray_enumerate(i, mod, keymap->mods) { + int ret; + + if (!(mask & (1 << i))) + continue; + + ret = snprintf(buf + pos, sizeof(buf) - pos, "%s%s", + pos == 0 ? "" : "+", + xkb_atom_text(keymap->ctx, mod->name)); + if (ret <= 0 || pos + ret >= sizeof(buf)) + break; + else + pos += ret; + } + + return strcpy(xkb_context_get_buffer(keymap->ctx, pos + 1), buf); +} const char * -LedStateText(struct xkb_context *ctx, enum xkb_state_component mask) +LedStateMaskText(struct xkb_context *ctx, enum xkb_state_component mask) { - unsigned int i; - char *ret; + char buf[1024]; + size_t pos = 0; if (mask == 0) return "0"; - ret = xkb_context_get_buffer(ctx, GET_TEXT_BUF_SIZE); - ret[0] = '\0'; - - for (i = 0; mask; i++) { - const char *name; + for (unsigned i = 0; mask; i++) { + int ret; if (!(mask & (1 << i))) continue; mask &= ~(1 << i); - name = LookupValue(modComponentMaskNames, 1 << i); - if (ret[0] != '\0') - append_get_text("%s+%s", ret, name); + ret = snprintf(buf + pos, sizeof(buf) - pos, "%s%s", + pos == 0 ? "" : "+", + LookupValue(modComponentMaskNames, 1 << i)); + if (ret <= 0 || pos + ret >= sizeof(buf)) + break; else - append_get_text("%s", name); + pos += ret; } - return ret; + return strcpy(xkb_context_get_buffer(ctx, pos + 1), buf); } const char * ControlMaskText(struct xkb_context *ctx, enum xkb_action_controls mask) { - unsigned int i; - char *ret; + char buf[1024]; + size_t pos = 0; if (mask == 0) return "none"; @@ -359,23 +340,22 @@ ControlMaskText(struct xkb_context *ctx, enum xkb_action_controls mask) if (mask == CONTROL_ALL) return "all"; - ret = xkb_context_get_buffer(ctx, GET_TEXT_BUF_SIZE); - ret[0] = '\0'; - - for (i = 0; mask; i++) { - const char *name; + for (unsigned i = 0; mask; i++) { + int ret; if (!(mask & (1 << i))) continue; mask &= ~(1 << i); - name = LookupValue(ctrlMaskNames, 1 << i); - if (ret[0] != '\0') - append_get_text("%s+%s", ret, name); + ret = snprintf(buf + pos, sizeof(buf) - pos, "%s%s", + pos == 0 ? "" : "+", + LookupValue(ctrlMaskNames, 1 << i)); + if (ret <= 0 || pos + ret >= sizeof(buf)) + break; else - append_get_text("%s", name); + pos += ret; } - return ret; + return strcpy(xkb_context_get_buffer(ctx, pos + 1), buf); } @@ -70,7 +70,7 @@ const char * SIMatchText(enum xkb_match_operation type); const char * -LedStateText(struct xkb_context *ctx, enum xkb_state_component mask); +LedStateMaskText(struct xkb_context *ctx, enum xkb_state_component mask); const char * ControlMaskText(struct xkb_context *ctx, enum xkb_action_controls mask); |