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