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