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>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
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));
diff --git a/src/text.c b/src/text.c
index 57f065f..a579699 100644
--- a/src/text.c
+++ b/src/text.c
@@ -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);
}
diff --git a/src/text.h b/src/text.h
index 98945fb..29f73ab 100644
--- a/src/text.h
+++ b/src/text.h
@@ -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);