/drivers/staging/speakup/i18n.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2 · C · 628 lines · 532 code · 34 blank · 62 comment · 57 complexity · f159400f9cbae4f92994a9cea0054f66 MD5 · raw file

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