1 // SPDX-License-Identifier: GPL-2.0
2 /* Internationalization implementation. Includes definitions of English
3 * string arrays, and the i18n pointer.
4 */
5
6 #include <linux/slab.h> /* For kmalloc. */
7 #include <linux/ctype.h>
8 #include <linux/module.h>
9 #include <linux/string.h>
10 #include "speakup.h"
11 #include "spk_priv.h"
12
13 static char *speakup_msgs[MSG_LAST_INDEX];
14 static char *speakup_default_msgs[MSG_LAST_INDEX] = {
15 [MSG_BLANK] = "blank",
16 [MSG_IAM_ALIVE] = "I'm aLive!",
17 [MSG_YOU_KILLED_SPEAKUP] = "You killed speakup!",
18 [MSG_HEY_THATS_BETTER] = "hey. That's better!",
19 [MSG_YOU_TURNED_ME_OFF] = "You turned me off!",
20 [MSG_PARKED] = "parked!",
21 [MSG_UNPARKED] = "unparked!",
22 [MSG_MARK] = "mark",
23 [MSG_CUT] = "cut",
24 [MSG_MARK_CLEARED] = "mark, cleared",
25 [MSG_PASTE] = "paste",
26 [MSG_BRIGHT] = "bright",
27 [MSG_ON_BLINKING] = "on blinking",
28 [MSG_OFF] = "off",
29 [MSG_ON] = "on",
30 [MSG_NO_WINDOW] = "no window",
31 [MSG_CURSORING_OFF] = "cursoring off",
32 [MSG_CURSORING_ON] = "cursoring on",
33 [MSG_HIGHLIGHT_TRACKING] = "highlight tracking",
34 [MSG_READ_WINDOW] = "read windo",
35 [MSG_READ_ALL] = "read all",
36 [MSG_EDIT_DONE] = "edit done",
37 [MSG_WINDOW_ALREADY_SET] = "window already set, clear then reset",
38 [MSG_END_BEFORE_START] = "error end before start",
39 [MSG_WINDOW_CLEARED] = "window cleared",
40 [MSG_WINDOW_SILENCED] = "window silenced",
41 [MSG_WINDOW_SILENCE_DISABLED] = "window silence disabled",
42 [MSG_ERROR] = "error",
43 [MSG_GOTO_CANCELED] = "goto canceled",
44 [MSG_GOTO] = "go to?",
45 [MSG_LEAVING_HELP] = "leaving help",
46 [MSG_IS_UNASSIGNED] = "is unassigned",
47 [MSG_HELP_INFO] =
48 "press space to exit, up or down to scroll, or a letter to go to a command",
49 [MSG_EDGE_TOP] = "top,",
50 [MSG_EDGE_BOTTOM] = "bottom,",
51 [MSG_EDGE_LEFT] = "left,",
52 [MSG_EDGE_RIGHT] = "right,",
53 [MSG_NUMBER] = "number",
54 [MSG_SPACE] = "space",
55 [MSG_START] = "start",
56 [MSG_END] = "end",
57 [MSG_CTRL] = "control-",
58 [MSG_DISJUNCTION] = "or",
59
60 /* Messages with embedded format specifiers. */
61 [MSG_POS_INFO] = "line %ld, col %ld, t t y %d",
62 [MSG_CHAR_INFO] = "hex %02x, decimal %d",
63 [MSG_REPEAT_DESC] = "times %d .",
64 [MSG_REPEAT_DESC2] = "repeated %d .",
65 [MSG_WINDOW_LINE] = "window is line %d",
66 [MSG_WINDOW_BOUNDARY] = "%s at line %d, column %d",
67 [MSG_EDIT_PROMPT] = "edit %s, press space when done",
68 [MSG_NO_COMMAND] = "no commands for %c",
69 [MSG_KEYDESC] = "is %s",
70
71 /* Control keys. */
72 /* Most of these duplicate the entries in state names. */
73 [MSG_CTL_SHIFT] = "shift",
74 [MSG_CTL_ALTGR] = "altgr",
75 [MSG_CTL_CONTROL] = "control",
76 [MSG_CTL_ALT] = "alt",
77 [MSG_CTL_LSHIFT] = "l shift",
78 [MSG_CTL_SPEAKUP] = "speakup",
79 [MSG_CTL_LCONTROL] = "l control",
80 [MSG_CTL_RCONTROL] = "r control",
81 [MSG_CTL_CAPSSHIFT] = "caps shift",
82
83 /* Color names. */
84 [MSG_COLOR_BLACK] = "black",
85 [MSG_COLOR_BLUE] = "blue",
86 [MSG_COLOR_GREEN] = "green",
87 [MSG_COLOR_CYAN] = "cyan",
88 [MSG_COLOR_RED] = "red",
89 [MSG_COLOR_MAGENTA] = "magenta",
90 [MSG_COLOR_YELLOW] = "yellow",
91 [MSG_COLOR_WHITE] = "white",
92 [MSG_COLOR_GREY] = "grey",
93 [MSG_COLOR_BRIGHTBLUE] = "bright blue",
94 [MSG_COLOR_BRIGHTGREEN] = "bright green",
95 [MSG_COLOR_BRIGHTCYAN] = "bright cyan",
96 [MSG_COLOR_BRIGHTRED] = "bright red",
97 [MSG_COLOR_BRIGHTMAGENTA] = "bright magenta",
98 [MSG_COLOR_BRIGHTYELLOW] = "bright yellow",
99 [MSG_COLOR_BRIGHTWHITE] = "bright white",
100
101 /* Names of key states. */
102 [MSG_STATE_DOUBLE] = "double",
103 [MSG_STATE_SPEAKUP] = "speakup",
104 [MSG_STATE_ALT] = "alt",
105 [MSG_STATE_CONTROL] = "ctrl",
106 [MSG_STATE_ALTGR] = "altgr",
107 [MSG_STATE_SHIFT] = "shift",
108
109 /* Key names. */
110 [MSG_KEYNAME_ESC] = "escape",
111 [MSG_KEYNAME_1] = "1",
112 [MSG_KEYNAME_2] = "2",
113 [MSG_KEYNAME_3] = "3",
114 [MSG_KEYNAME_4] = "4",
115 [MSG_KEYNAME_5] = "5",
116 [MSG_KEYNAME_6] = "6",
117 [MSG_KEYNAME_7] = "7",
118 [MSG_KEYNAME_8] = "8",
119 [MSG_KEYNAME_9] = "9",
120 [MSG_KEYNAME_0] = "0",
121 [MSG_KEYNAME_DASH] = "minus",
122 [MSG_KEYNAME_EQUAL] = "equal",
123 [MSG_KEYNAME_BS] = "back space",
124 [MSG_KEYNAME_TAB] = "tab",
125 [MSG_KEYNAME_Q] = "q",
126 [MSG_KEYNAME_W] = "w",
127 [MSG_KEYNAME_E] = "e",
128 [MSG_KEYNAME_R] = "r",
129 [MSG_KEYNAME_T] = "t",
130 [MSG_KEYNAME_Y] = "y",
131 [MSG_KEYNAME_U] = "u",
132 [MSG_KEYNAME_I] = "i",
133 [MSG_KEYNAME_O] = "o",
134 [MSG_KEYNAME_P] = "p",
135 [MSG_KEYNAME_LEFTBRACE] = "left brace",
136 [MSG_KEYNAME_RIGHTBRACE] = "right brace",
137 [MSG_KEYNAME_ENTER] = "enter",
138 [MSG_KEYNAME_LEFTCTRL] = "left control",
139 [MSG_KEYNAME_A] = "a",
140 [MSG_KEYNAME_S] = "s",
141 [MSG_KEYNAME_D] = "d",
142 [MSG_KEYNAME_F] = "f",
143 [MSG_KEYNAME_G] = "g",
144 [MSG_KEYNAME_H] = "h",
145 [MSG_KEYNAME_J] = "j",
146 [MSG_KEYNAME_K] = "k",
147 [MSG_KEYNAME_L] = "l",
148 [MSG_KEYNAME_SEMICOLON] = "semicolon",
149 [MSG_KEYNAME_SINGLEQUOTE] = "apostrophe",
150 [MSG_KEYNAME_GRAVE] = "accent",
151 [MSG_KEYNAME_LEFTSHFT] = "left shift",
152 [MSG_KEYNAME_BACKSLASH] = "back slash",
153 [MSG_KEYNAME_Z] = "z",
154 [MSG_KEYNAME_X] = "x",
155 [MSG_KEYNAME_C] = "c",
156 [MSG_KEYNAME_V] = "v",
157 [MSG_KEYNAME_B] = "b",
158 [MSG_KEYNAME_N] = "n",
159 [MSG_KEYNAME_M] = "m",
160 [MSG_KEYNAME_COMMA] = "comma",
161 [MSG_KEYNAME_DOT] = "dot",
162 [MSG_KEYNAME_SLASH] = "slash",
163 [MSG_KEYNAME_RIGHTSHFT] = "right shift",
164 [MSG_KEYNAME_KPSTAR] = "keypad asterisk",
165 [MSG_KEYNAME_LEFTALT] = "left alt",
166 [MSG_KEYNAME_SPACE] = "space",
167 [MSG_KEYNAME_CAPSLOCK] = "caps lock",
168 [MSG_KEYNAME_F1] = "f1",
169 [MSG_KEYNAME_F2] = "f2",
170 [MSG_KEYNAME_F3] = "f3",
171 [MSG_KEYNAME_F4] = "f4",
172 [MSG_KEYNAME_F5] = "f5",
173 [MSG_KEYNAME_F6] = "f6",
174 [MSG_KEYNAME_F7] = "f7",
175 [MSG_KEYNAME_F8] = "f8",
176 [MSG_KEYNAME_F9] = "f9",
177 [MSG_KEYNAME_F10] = "f10",
178 [MSG_KEYNAME_NUMLOCK] = "num lock",
179 [MSG_KEYNAME_SCROLLLOCK] = "scroll lock",
180 [MSG_KEYNAME_KP7] = "keypad 7",
181 [MSG_KEYNAME_KP8] = "keypad 8",
182 [MSG_KEYNAME_KP9] = "keypad 9",
183 [MSG_KEYNAME_KPMINUS] = "keypad minus",
184 [MSG_KEYNAME_KP4] = "keypad 4",
185 [MSG_KEYNAME_KP5] = "keypad 5",
186 [MSG_KEYNAME_KP6] = "keypad 6",
187 [MSG_KEYNAME_KPPLUS] = "keypad plus",
188 [MSG_KEYNAME_KP1] = "keypad 1",
189 [MSG_KEYNAME_KP2] = "keypad 2",
190 [MSG_KEYNAME_KP3] = "keypad 3",
191 [MSG_KEYNAME_KP0] = "keypad 0",
192 [MSG_KEYNAME_KPDOT] = "keypad dot",
193 [MSG_KEYNAME_103RD] = "103rd",
194 [MSG_KEYNAME_F13] = "f13",
195 [MSG_KEYNAME_102ND] = "102nd",
196 [MSG_KEYNAME_F11] = "f11",
197 [MSG_KEYNAME_F12] = "f12",
198 [MSG_KEYNAME_F14] = "f14",
199 [MSG_KEYNAME_F15] = "f15",
200 [MSG_KEYNAME_F16] = "f16",
201 [MSG_KEYNAME_F17] = "f17",
202 [MSG_KEYNAME_F18] = "f18",
203 [MSG_KEYNAME_F19] = "f19",
204 [MSG_KEYNAME_F20] = "f20",
205 [MSG_KEYNAME_KPENTER] = "keypad enter",
206 [MSG_KEYNAME_RIGHTCTRL] = "right control",
207 [MSG_KEYNAME_KPSLASH] = "keypad slash",
208 [MSG_KEYNAME_SYSRQ] = "sysrq",
209 [MSG_KEYNAME_RIGHTALT] = "right alt",
210 [MSG_KEYNAME_LF] = "line feed",
211 [MSG_KEYNAME_HOME] = "home",
212 [MSG_KEYNAME_UP] = "up",
213 [MSG_KEYNAME_PGUP] = "page up",
214 [MSG_KEYNAME_LEFT] = "left",
215 [MSG_KEYNAME_RIGHT] = "right",
216 [MSG_KEYNAME_END] = "end",
217 [MSG_KEYNAME_DOWN] = "down",
218 [MSG_KEYNAME_PGDN] = "page down",
219 [MSG_KEYNAME_INS] = "insert",
220 [MSG_KEYNAME_DEL] = "delete",
221 [MSG_KEYNAME_MACRO] = "macro",
222 [MSG_KEYNAME_MUTE] = "mute",
223 [MSG_KEYNAME_VOLDOWN] = "volume down",
224 [MSG_KEYNAME_VOLUP] = "volume up",
225 [MSG_KEYNAME_POWER] = "power",
226 [MSG_KEYNAME_KPEQUAL] = "keypad equal",
227 [MSG_KEYNAME_KPPLUSDASH] = "keypad plusminus",
228 [MSG_KEYNAME_PAUSE] = "pause",
229 [MSG_KEYNAME_F21] = "f21",
230 [MSG_KEYNAME_F22] = "f22",
231 [MSG_KEYNAME_F23] = "f23",
232 [MSG_KEYNAME_F24] = "f24",
233 [MSG_KEYNAME_KPCOMMA] = "keypad comma",
234 [MSG_KEYNAME_LEFTMETA] = "left meta",
235 [MSG_KEYNAME_RIGHTMETA] = "right meta",
236 [MSG_KEYNAME_COMPOSE] = "compose",
237 [MSG_KEYNAME_STOP] = "stop",
238 [MSG_KEYNAME_AGAIN] = "again",
239 [MSG_KEYNAME_PROPS] = "props",
240 [MSG_KEYNAME_UNDO] = "undo",
241 [MSG_KEYNAME_FRONT] = "front",
242 [MSG_KEYNAME_COPY] = "copy",
243 [MSG_KEYNAME_OPEN] = "open",
244 [MSG_KEYNAME_PASTE] = "paste",
245 [MSG_KEYNAME_FIND] = "find",
246 [MSG_KEYNAME_CUT] = "cut",
247 [MSG_KEYNAME_HELP] = "help",
248 [MSG_KEYNAME_MENU] = "menu",
249 [MSG_KEYNAME_CALC] = "calc",
250 [MSG_KEYNAME_SETUP] = "setup",
251 [MSG_KEYNAME_SLEEP] = "sleep",
252 [MSG_KEYNAME_WAKEUP] = "wakeup",
253 [MSG_KEYNAME_FILE] = "file",
254 [MSG_KEYNAME_SENDFILE] = "send file",
255 [MSG_KEYNAME_DELFILE] = "delete file",
256 [MSG_KEYNAME_XFER] = "transfer",
257 [MSG_KEYNAME_PROG1] = "prog1",
258 [MSG_KEYNAME_PROG2] = "prog2",
259 [MSG_KEYNAME_WWW] = "www",
260 [MSG_KEYNAME_MSDOS] = "msdos",
261 [MSG_KEYNAME_COFFEE] = "coffee",
262 [MSG_KEYNAME_DIRECTION] = "direction",
263 [MSG_KEYNAME_CYCLEWINDOWS] = "cycle windows",
264 [MSG_KEYNAME_MAIL] = "mail",
265 [MSG_KEYNAME_BOOKMARKS] = "bookmarks",
266 [MSG_KEYNAME_COMPUTER] = "computer",
267 [MSG_KEYNAME_BACK] = "back",
268 [MSG_KEYNAME_FORWARD] = "forward",
269 [MSG_KEYNAME_CLOSECD] = "close cd",
270 [MSG_KEYNAME_EJECTCD] = "eject cd",
271 [MSG_KEYNAME_EJECTCLOSE] = "eject close cd",
272 [MSG_KEYNAME_NEXTSONG] = "next song",
273 [MSG_KEYNAME_PLAYPAUSE] = "play pause",
274 [MSG_KEYNAME_PREVSONG] = "previous song",
275 [MSG_KEYNAME_STOPCD] = "stop cd",
276 [MSG_KEYNAME_RECORD] = "record",
277 [MSG_KEYNAME_REWIND] = "rewind",
278 [MSG_KEYNAME_PHONE] = "phone",
279 [MSG_KEYNAME_ISO] = "iso",
280 [MSG_KEYNAME_CONFIG] = "config",
281 [MSG_KEYNAME_HOMEPG] = "home page",
282 [MSG_KEYNAME_REFRESH] = "refresh",
283 [MSG_KEYNAME_EXIT] = "exit",
284 [MSG_KEYNAME_MOVE] = "move",
285 [MSG_KEYNAME_EDIT] = "edit",
286 [MSG_KEYNAME_SCROLLUP] = "scroll up",
287 [MSG_KEYNAME_SCROLLDN] = "scroll down",
288 [MSG_KEYNAME_KPLEFTPAR] = "keypad left paren",
289 [MSG_KEYNAME_KPRIGHTPAR] = "keypad right paren",
290
291 /* Function names. */
292 [MSG_FUNCNAME_ATTRIB_BLEEP_DEC] = "attribute bleep decrement",
293 [MSG_FUNCNAME_ATTRIB_BLEEP_INC] = "attribute bleep increment",
294 [MSG_FUNCNAME_BLEEPS_DEC] = "bleeps decrement",
295 [MSG_FUNCNAME_BLEEPS_INC] = "bleeps increment",
296 [MSG_FUNCNAME_CHAR_FIRST] = "character, first",
297 [MSG_FUNCNAME_CHAR_LAST] = "character, last",
298 [MSG_FUNCNAME_CHAR_CURRENT] = "character, say current",
299 [MSG_FUNCNAME_CHAR_HEX_AND_DEC] = "character, say hex and decimal",
300 [MSG_FUNCNAME_CHAR_NEXT] = "character, say next",
301 [MSG_FUNCNAME_CHAR_PHONETIC] = "character, say phonetic",
302 [MSG_FUNCNAME_CHAR_PREVIOUS] = "character, say previous",
303 [MSG_FUNCNAME_CURSOR_PARK] = "cursor park",
304 [MSG_FUNCNAME_CUT] = "cut",
305 [MSG_FUNCNAME_EDIT_DELIM] = "edit delimiters",
306 [MSG_FUNCNAME_EDIT_EXNUM] = "edit exnum",
307 [MSG_FUNCNAME_EDIT_MOST] = "edit most",
308 [MSG_FUNCNAME_EDIT_REPEATS] = "edit repeats",
309 [MSG_FUNCNAME_EDIT_SOME] = "edit some",
310 [MSG_FUNCNAME_GOTO] = "go to",
311 [MSG_FUNCNAME_GOTO_BOTTOM] = "go to bottom edge",
312 [MSG_FUNCNAME_GOTO_LEFT] = "go to left edge",
313 [MSG_FUNCNAME_GOTO_RIGHT] = "go to right edge",
314 [MSG_FUNCNAME_GOTO_TOP] = "go to top edge",
315 [MSG_FUNCNAME_HELP] = "help",
316 [MSG_FUNCNAME_LINE_SAY_CURRENT] = "line, say current",
317 [MSG_FUNCNAME_LINE_SAY_NEXT] = "line, say next",
318 [MSG_FUNCNAME_LINE_SAY_PREVIOUS] = "line, say previous",
319 [MSG_FUNCNAME_LINE_SAY_WITH_INDENT] = "line, say with indent",
320 [MSG_FUNCNAME_PASTE] = "paste",
321 [MSG_FUNCNAME_PITCH_DEC] = "pitch decrement",
322 [MSG_FUNCNAME_PITCH_INC] = "pitch increment",
323 [MSG_FUNCNAME_PUNC_DEC] = "punctuation decrement",
324 [MSG_FUNCNAME_PUNC_INC] = "punctuation increment",
325 [MSG_FUNCNAME_PUNC_LEVEL_DEC] = "punc level decrement",
326 [MSG_FUNCNAME_PUNC_LEVEL_INC] = "punc level increment",
327 [MSG_FUNCNAME_QUIET] = "quiet",
328 [MSG_FUNCNAME_RATE_DEC] = "rate decrement",
329 [MSG_FUNCNAME_RATE_INC] = "rate increment",
330 [MSG_FUNCNAME_READING_PUNC_DEC] = "reading punctuation decrement",
331 [MSG_FUNCNAME_READING_PUNC_INC] = "reading punctuation increment",
332 [MSG_FUNCNAME_SAY_ATTRIBUTES] = "say attributes",
333 [MSG_FUNCNAME_SAY_FROM_LEFT] = "say from left",
334 [MSG_FUNCNAME_SAY_FROM_TOP] = "say from top",
335 [MSG_FUNCNAME_SAY_POSITION] = "say position",
336 [MSG_FUNCNAME_SAY_SCREEN] = "say screen",
337 [MSG_FUNCNAME_SAY_TO_BOTTOM] = "say to bottom",
338 [MSG_FUNCNAME_SAY_TO_RIGHT] = "say to right",
339 [MSG_FUNCNAME_SPEAKUP] = "speakup",
340 [MSG_FUNCNAME_SPEAKUP_LOCK] = "speakup lock",
341 [MSG_FUNCNAME_SPEAKUP_OFF] = "speakup off",
342 [MSG_FUNCNAME_SPEECH_KILL] = "speech kill",
343 [MSG_FUNCNAME_SPELL_DELAY_DEC] = "spell delay decrement",
344 [MSG_FUNCNAME_SPELL_DELAY_INC] = "spell delay increment",
345 [MSG_FUNCNAME_SPELL_WORD] = "spell word",
346 [MSG_FUNCNAME_SPELL_WORD_PHONETICALLY] = "spell word phonetically",
347 [MSG_FUNCNAME_TONE_DEC] = "tone decrement",
348 [MSG_FUNCNAME_TONE_INC] = "tone increment",
349 [MSG_FUNCNAME_VOICE_DEC] = "voice decrement",
350 [MSG_FUNCNAME_VOICE_INC] = "voice increment",
351 [MSG_FUNCNAME_VOLUME_DEC] = "volume decrement",
352 [MSG_FUNCNAME_VOLUME_INC] = "volume increment",
353 [MSG_FUNCNAME_WINDOW_CLEAR] = "window, clear",
354 [MSG_FUNCNAME_WINDOW_SAY] = "window, say",
355 [MSG_FUNCNAME_WINDOW_SET] = "window, set",
356 [MSG_FUNCNAME_WINDOW_SILENCE] = "window, silence",
357 [MSG_FUNCNAME_WORD_SAY_CURRENT] = "word, say current",
358 [MSG_FUNCNAME_WORD_SAY_NEXT] = "word, say next",
359 [MSG_FUNCNAME_WORD_SAY_PREVIOUS] = "word, say previous",
360 };
361
362 static struct msg_group_t all_groups[] = {
363 {
364 .name = "ctl_keys",
365 .start = MSG_CTL_START,
366 .end = MSG_CTL_END,
367 },
368 {
369 .name = "colors",
370 .start = MSG_COLORS_START,
371 .end = MSG_COLORS_END,
372 },
373 {
374 .name = "formatted",
375 .start = MSG_FORMATTED_START,
376 .end = MSG_FORMATTED_END,
377 },
378 {
379 .name = "function_names",
380 .start = MSG_FUNCNAMES_START,
381 .end = MSG_FUNCNAMES_END,
382 },
383 {
384 .name = "key_names",
385 .start = MSG_KEYNAMES_START,
386 .end = MSG_KEYNAMES_END,
387 },
388 {
389 .name = "announcements",
390 .start = MSG_ANNOUNCEMENTS_START,
391 .end = MSG_ANNOUNCEMENTS_END,
392 },
393 {
394 .name = "states",
395 .start = MSG_STATES_START,
396 .end = MSG_STATES_END,
397 },
398 };
399
400 static const int num_groups = ARRAY_SIZE(all_groups);
401
spk_msg_get(enum msg_index_t index)402 char *spk_msg_get(enum msg_index_t index)
403 {
404 return speakup_msgs[index];
405 }
406
407 /*
408 * Function: next_specifier
409 * Finds the start of the next format specifier in the argument string.
410 * Return value: pointer to start of format
411 * specifier, or NULL if no specifier exists.
412 */
next_specifier(char * input)413 static char *next_specifier(char *input)
414 {
415 int found = 0;
416 char *next_percent = input;
417
418 while (next_percent && !found) {
419 next_percent = strchr(next_percent, '%');
420 if (next_percent) {
421 /* skip over doubled percent signs */
422 while (next_percent[0] == '%' &&
423 next_percent[1] == '%')
424 next_percent += 2;
425 if (*next_percent == '%')
426 found = 1;
427 else if (*next_percent == '\0')
428 next_percent = NULL;
429 }
430 }
431
432 return next_percent;
433 }
434
435 /* Skip over 0 or more flags. */
skip_flags(char * input)436 static char *skip_flags(char *input)
437 {
438 while ((*input != '\0') && strchr(" 0+-#", *input))
439 input++;
440 return input;
441 }
442
443 /* Skip over width.precision, if it exists. */
skip_width(char * input)444 static char *skip_width(char *input)
445 {
446 while (isdigit(*input))
447 input++;
448 if (*input == '.') {
449 input++;
450 while (isdigit(*input))
451 input++;
452 }
453 return input;
454 }
455
456 /*
457 * Skip past the end of the conversion part.
458 * Note that this code only accepts a handful of conversion specifiers:
459 * c d s x and ld. Not accidental; these are exactly the ones used in
460 * the default group of formatted messages.
461 */
skip_conversion(char * input)462 static char *skip_conversion(char *input)
463 {
464 if ((input[0] == 'l') && (input[1] == 'd'))
465 input += 2;
466 else if ((*input != '\0') && strchr("cdsx", *input))
467 input++;
468 return input;
469 }
470
471 /*
472 * Function: find_specifier_end
473 * Return a pointer to the end of the format specifier.
474 */
find_specifier_end(char * input)475 static char *find_specifier_end(char *input)
476 {
477 input++; /* Advance over %. */
478 input = skip_flags(input);
479 input = skip_width(input);
480 input = skip_conversion(input);
481 return input;
482 }
483
484 /*
485 * Function: compare_specifiers
486 * Compare the format specifiers pointed to by *input1 and *input2.
487 * Return true if they are the same, false otherwise.
488 * Advance *input1 and *input2 so that they point to the character following
489 * the end of the specifier.
490 */
compare_specifiers(char ** input1,char ** input2)491 static bool compare_specifiers(char **input1, char **input2)
492 {
493 bool same = false;
494 char *end1 = find_specifier_end(*input1);
495 char *end2 = find_specifier_end(*input2);
496 size_t length1 = end1 - *input1;
497 size_t length2 = end2 - *input2;
498
499 if ((length1 == length2) && !memcmp(*input1, *input2, length1))
500 same = true;
501
502 *input1 = end1;
503 *input2 = end2;
504 return same;
505 }
506
507 /*
508 * Function: fmt_validate
509 * Check that two format strings contain the same number of format specifiers,
510 * and that the order of specifiers is the same in both strings.
511 * Return true if the condition holds, false if it doesn't.
512 */
fmt_validate(char * template,char * user)513 static bool fmt_validate(char *template, char *user)
514 {
515 bool valid = true;
516 bool still_comparing = true;
517 char *template_ptr = template;
518 char *user_ptr = user;
519
520 while (still_comparing && valid) {
521 template_ptr = next_specifier(template_ptr);
522 user_ptr = next_specifier(user_ptr);
523 if (template_ptr && user_ptr) {
524 /* Both have at least one more specifier. */
525 valid = compare_specifiers(&template_ptr, &user_ptr);
526 } else {
527 /* No more format specifiers in one or both strings. */
528 still_comparing = false;
529 /* See if one has more specifiers than the other. */
530 if (template_ptr || user_ptr)
531 valid = false;
532 }
533 }
534 return valid;
535 }
536
537 /*
538 * Function: msg_set
539 * Description: Add a user-supplied message to the user_messages array.
540 * The message text is copied to a memory area allocated with kmalloc.
541 * If the function fails, then user_messages is untouched.
542 * Arguments:
543 * - index: a message number, as found in i18n.h.
544 * - text: text of message. Not NUL-terminated.
545 * - length: number of bytes in text.
546 * Failure conditions:
547 * -EINVAL - Invalid format specifiers in formatted message or illegal index.
548 * -ENOMEM - Unable to allocate memory.
549 */
spk_msg_set(enum msg_index_t index,char * text,size_t length)550 ssize_t spk_msg_set(enum msg_index_t index, char *text, size_t length)
551 {
552 char *newstr = NULL;
553 unsigned long flags;
554
555 if ((index < MSG_FIRST_INDEX) || (index >= MSG_LAST_INDEX))
556 return -EINVAL;
557
558 newstr = kmemdup_nul(text, length, GFP_KERNEL);
559 if (!newstr)
560 return -ENOMEM;
561
562 if (index >= MSG_FORMATTED_START &&
563 index <= MSG_FORMATTED_END &&
564 !fmt_validate(speakup_default_msgs[index], newstr)) {
565 kfree(newstr);
566 return -EINVAL;
567 }
568 spin_lock_irqsave(&speakup_info.spinlock, flags);
569 if (speakup_msgs[index] != speakup_default_msgs[index])
570 kfree(speakup_msgs[index]);
571 speakup_msgs[index] = newstr;
572 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
573 return 0;
574 }
575
576 /*
577 * Find a message group, given its name. Return a pointer to the structure
578 * if found, or NULL otherwise.
579 */
spk_find_msg_group(const char * group_name)580 struct msg_group_t *spk_find_msg_group(const char *group_name)
581 {
582 struct msg_group_t *group = NULL;
583 int i;
584
585 for (i = 0; i < num_groups; i++) {
586 if (!strcmp(all_groups[i].name, group_name)) {
587 group = &all_groups[i];
588 break;
589 }
590 }
591 return group;
592 }
593
spk_reset_msg_group(struct msg_group_t * group)594 void spk_reset_msg_group(struct msg_group_t *group)
595 {
596 unsigned long flags;
597 enum msg_index_t i;
598
599 spin_lock_irqsave(&speakup_info.spinlock, flags);
600
601 for (i = group->start; i <= group->end; i++) {
602 if (speakup_msgs[i] != speakup_default_msgs[i])
603 kfree(speakup_msgs[i]);
604 speakup_msgs[i] = speakup_default_msgs[i];
605 }
606 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
607 }
608
609 /* Called at initialization time, to establish default messages. */
spk_initialize_msgs(void)610 void spk_initialize_msgs(void)
611 {
612 memcpy(speakup_msgs, speakup_default_msgs,
613 sizeof(speakup_default_msgs));
614 }
615
616 /* Free user-supplied strings when module is unloaded: */
spk_free_user_msgs(void)617 void spk_free_user_msgs(void)
618 {
619 enum msg_index_t index;
620 unsigned long flags;
621
622 spin_lock_irqsave(&speakup_info.spinlock, flags);
623 for (index = MSG_FIRST_INDEX; index < MSG_LAST_INDEX; index++) {
624 if (speakup_msgs[index] != speakup_default_msgs[index]) {
625 kfree(speakup_msgs[index]);
626 speakup_msgs[index] = speakup_default_msgs[index];
627 }
628 }
629 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
630 }
631