i18n.c 18.6 KB
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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
// SPDX-License-Identifier: GPL-2.0
/* Internationalization implementation.  Includes definitions of English
 * string arrays, and the i18n pointer.
 */

#include <linux/slab.h>		/* For kmalloc. */
#include <linux/ctype.h>
#include <linux/module.h>
#include <linux/string.h>
#include "speakup.h"
#include "spk_priv.h"

static char *speakup_msgs[MSG_LAST_INDEX];
static char *speakup_default_msgs[MSG_LAST_INDEX] = {
	[MSG_BLANK] = "blank",
	[MSG_IAM_ALIVE] = "I'm aLive!",
	[MSG_YOU_KILLED_SPEAKUP] = "You killed speakup!",
	[MSG_HEY_THATS_BETTER] = "hey. That's better!",
	[MSG_YOU_TURNED_ME_OFF] = "You turned me off!",
	[MSG_PARKED] = "parked!",
	[MSG_UNPARKED] = "unparked!",
	[MSG_MARK] = "mark",
	[MSG_CUT] = "cut",
	[MSG_MARK_CLEARED] = "mark, cleared",
	[MSG_PASTE] = "paste",
	[MSG_BRIGHT] = "bright",
	[MSG_ON_BLINKING] = "on blinking",
	[MSG_OFF] = "off",
	[MSG_ON] = "on",
	[MSG_NO_WINDOW] = "no window",
	[MSG_CURSORING_OFF] = "cursoring off",
	[MSG_CURSORING_ON] = "cursoring on",
	[MSG_HIGHLIGHT_TRACKING] = "highlight tracking",
	[MSG_READ_WINDOW] = "read windo",
	[MSG_READ_ALL] = "read all",
	[MSG_EDIT_DONE] = "edit done",
	[MSG_WINDOW_ALREADY_SET] = "window already set, clear then reset",
	[MSG_END_BEFORE_START] = "error end before start",
	[MSG_WINDOW_CLEARED] = "window cleared",
	[MSG_WINDOW_SILENCED] = "window silenced",
	[MSG_WINDOW_SILENCE_DISABLED] = "window silence disabled",
	[MSG_ERROR] = "error",
	[MSG_GOTO_CANCELED] = "goto canceled",
	[MSG_GOTO] = "go to?",
	[MSG_LEAVING_HELP] = "leaving help",
	[MSG_IS_UNASSIGNED] = "is unassigned",
	[MSG_HELP_INFO] =
	"press space to exit, up or down to scroll, or a letter to go to a command",
	[MSG_EDGE_TOP] = "top,",
	[MSG_EDGE_BOTTOM] = "bottom,",
	[MSG_EDGE_LEFT] = "left,",
	[MSG_EDGE_RIGHT] = "right,",
	[MSG_NUMBER] = "number",
	[MSG_SPACE] = "space",
	[MSG_START] = "start",
	[MSG_END] = "end",
	[MSG_CTRL] = "control-",
	[MSG_DISJUNCTION] = "or",

/* Messages with embedded format specifiers. */
	[MSG_POS_INFO] = "line %ld, col %ld, t t y %d",
	[MSG_CHAR_INFO] = "hex %02x, decimal %d",
	[MSG_REPEAT_DESC] = "times %d .",
	[MSG_REPEAT_DESC2] = "repeated %d .",
	[MSG_WINDOW_LINE] = "window is line %d",
	[MSG_WINDOW_BOUNDARY] = "%s at line %d, column %d",
	[MSG_EDIT_PROMPT] = "edit  %s, press space when done",
	[MSG_NO_COMMAND] = "no commands for %c",
	[MSG_KEYDESC] = "is %s",

	/* Control keys. */
	/* Most of these duplicate the entries in state names. */
	[MSG_CTL_SHIFT] = "shift",
	[MSG_CTL_ALTGR] = "altgr",
	[MSG_CTL_CONTROL] = "control",
	[MSG_CTL_ALT] = "alt",
	[MSG_CTL_LSHIFT] = "l shift",
	[MSG_CTL_SPEAKUP] = "speakup",
	[MSG_CTL_LCONTROL] = "l control",
	[MSG_CTL_RCONTROL] = "r control",
	[MSG_CTL_CAPSSHIFT] = "caps shift",

	/* Color names. */
	[MSG_COLOR_BLACK] = "black",
	[MSG_COLOR_BLUE] = "blue",
	[MSG_COLOR_GREEN] = "green",
	[MSG_COLOR_CYAN] = "cyan",
	[MSG_COLOR_RED] = "red",
	[MSG_COLOR_MAGENTA] = "magenta",
	[MSG_COLOR_YELLOW] = "yellow",
	[MSG_COLOR_WHITE] = "white",
	[MSG_COLOR_GREY] = "grey",

	/* Names of key states. */
	[MSG_STATE_DOUBLE] = "double",
	[MSG_STATE_SPEAKUP] = "speakup",
	[MSG_STATE_ALT] = "alt",
	[MSG_STATE_CONTROL] = "ctrl",
	[MSG_STATE_ALTGR] = "altgr",
	[MSG_STATE_SHIFT] = "shift",

	/* Key names. */
	[MSG_KEYNAME_ESC] = "escape",
	[MSG_KEYNAME_1] = "1",
	[MSG_KEYNAME_2] = "2",
	[MSG_KEYNAME_3] = "3",
	[MSG_KEYNAME_4] = "4",
	[MSG_KEYNAME_5] = "5",
	[MSG_KEYNAME_6] = "6",
	[MSG_KEYNAME_7] = "7",
	[MSG_KEYNAME_8] = "8",
	[MSG_KEYNAME_9] = "9",
	[MSG_KEYNAME_0] = "0",
	[MSG_KEYNAME_DASH] = "minus",
	[MSG_KEYNAME_EQUAL] = "equal",
	[MSG_KEYNAME_BS] = "back space",
	[MSG_KEYNAME_TAB] = "tab",
	[MSG_KEYNAME_Q] = "q",
	[MSG_KEYNAME_W] = "w",
	[MSG_KEYNAME_E] = "e",
	[MSG_KEYNAME_R] = "r",
	[MSG_KEYNAME_T] = "t",
	[MSG_KEYNAME_Y] = "y",
	[MSG_KEYNAME_U] = "u",
	[MSG_KEYNAME_I] = "i",
	[MSG_KEYNAME_O] = "o",
	[MSG_KEYNAME_P] = "p",
	[MSG_KEYNAME_LEFTBRACE] = "left brace",
	[MSG_KEYNAME_RIGHTBRACE] = "right brace",
	[MSG_KEYNAME_ENTER] = "enter",
	[MSG_KEYNAME_LEFTCTRL] = "left control",
	[MSG_KEYNAME_A] = "a",
	[MSG_KEYNAME_S] = "s",
	[MSG_KEYNAME_D] = "d",
	[MSG_KEYNAME_F] = "f",
	[MSG_KEYNAME_G] = "g",
	[MSG_KEYNAME_H] = "h",
	[MSG_KEYNAME_J] = "j",
	[MSG_KEYNAME_K] = "k",
	[MSG_KEYNAME_L] = "l",
	[MSG_KEYNAME_SEMICOLON] = "semicolon",
	[MSG_KEYNAME_SINGLEQUOTE] = "apostrophe",
	[MSG_KEYNAME_GRAVE] = "accent",
	[MSG_KEYNAME_LEFTSHFT] = "left shift",
	[MSG_KEYNAME_BACKSLASH] = "back slash",
	[MSG_KEYNAME_Z] = "z",
	[MSG_KEYNAME_X] = "x",
	[MSG_KEYNAME_C] = "c",
	[MSG_KEYNAME_V] = "v",
	[MSG_KEYNAME_B] = "b",
	[MSG_KEYNAME_N] = "n",
	[MSG_KEYNAME_M] = "m",
	[MSG_KEYNAME_COMMA] = "comma",
	[MSG_KEYNAME_DOT] = "dot",
	[MSG_KEYNAME_SLASH] = "slash",
	[MSG_KEYNAME_RIGHTSHFT] = "right shift",
	[MSG_KEYNAME_KPSTAR] = "keypad asterisk",
	[MSG_KEYNAME_LEFTALT] = "left alt",
	[MSG_KEYNAME_SPACE] = "space",
	[MSG_KEYNAME_CAPSLOCK] = "caps lock",
	[MSG_KEYNAME_F1] = "f1",
	[MSG_KEYNAME_F2] = "f2",
	[MSG_KEYNAME_F3] = "f3",
	[MSG_KEYNAME_F4] = "f4",
	[MSG_KEYNAME_F5] = "f5",
	[MSG_KEYNAME_F6] = "f6",
	[MSG_KEYNAME_F7] = "f7",
	[MSG_KEYNAME_F8] = "f8",
	[MSG_KEYNAME_F9] = "f9",
	[MSG_KEYNAME_F10] = "f10",
	[MSG_KEYNAME_NUMLOCK] = "num lock",
	[MSG_KEYNAME_SCROLLLOCK] = "scroll lock",
	[MSG_KEYNAME_KP7] = "keypad 7",
	[MSG_KEYNAME_KP8] = "keypad 8",
	[MSG_KEYNAME_KP9] = "keypad 9",
	[MSG_KEYNAME_KPMINUS] = "keypad minus",
	[MSG_KEYNAME_KP4] = "keypad 4",
	[MSG_KEYNAME_KP5] = "keypad 5",
	[MSG_KEYNAME_KP6] = "keypad 6",
	[MSG_KEYNAME_KPPLUS] = "keypad plus",
	[MSG_KEYNAME_KP1] = "keypad 1",
	[MSG_KEYNAME_KP2] = "keypad 2",
	[MSG_KEYNAME_KP3] = "keypad 3",
	[MSG_KEYNAME_KP0] = "keypad 0",
	[MSG_KEYNAME_KPDOT] = "keypad dot",
	[MSG_KEYNAME_103RD] = "103rd",
	[MSG_KEYNAME_F13] = "f13",
	[MSG_KEYNAME_102ND] = "102nd",
	[MSG_KEYNAME_F11] = "f11",
	[MSG_KEYNAME_F12] = "f12",
	[MSG_KEYNAME_F14] = "f14",
	[MSG_KEYNAME_F15] = "f15",
	[MSG_KEYNAME_F16] = "f16",
	[MSG_KEYNAME_F17] = "f17",
	[MSG_KEYNAME_F18] = "f18",
	[MSG_KEYNAME_F19] = "f19",
	[MSG_KEYNAME_F20] = "f20",
	[MSG_KEYNAME_KPENTER] = "keypad enter",
	[MSG_KEYNAME_RIGHTCTRL] = "right control",
	[MSG_KEYNAME_KPSLASH] = "keypad slash",
	[MSG_KEYNAME_SYSRQ] = "sysrq",
	[MSG_KEYNAME_RIGHTALT] = "right alt",
	[MSG_KEYNAME_LF] = "line feed",
	[MSG_KEYNAME_HOME] = "home",
	[MSG_KEYNAME_UP] = "up",
	[MSG_KEYNAME_PGUP] = "page up",
	[MSG_KEYNAME_LEFT] = "left",
	[MSG_KEYNAME_RIGHT] = "right",
	[MSG_KEYNAME_END] = "end",
	[MSG_KEYNAME_DOWN] = "down",
	[MSG_KEYNAME_PGDN] = "page down",
	[MSG_KEYNAME_INS] = "insert",
	[MSG_KEYNAME_DEL] = "delete",
	[MSG_KEYNAME_MACRO] = "macro",
	[MSG_KEYNAME_MUTE] = "mute",
	[MSG_KEYNAME_VOLDOWN] = "volume down",
	[MSG_KEYNAME_VOLUP] = "volume up",
	[MSG_KEYNAME_POWER] = "power",
	[MSG_KEYNAME_KPEQUAL] = "keypad equal",
	[MSG_KEYNAME_KPPLUSDASH] = "keypad plusminus",
	[MSG_KEYNAME_PAUSE] = "pause",
	[MSG_KEYNAME_F21] = "f21",
	[MSG_KEYNAME_F22] = "f22",
	[MSG_KEYNAME_F23] = "f23",
	[MSG_KEYNAME_F24] = "f24",
	[MSG_KEYNAME_KPCOMMA] = "keypad comma",
	[MSG_KEYNAME_LEFTMETA] = "left meta",
	[MSG_KEYNAME_RIGHTMETA] = "right meta",
	[MSG_KEYNAME_COMPOSE] = "compose",
	[MSG_KEYNAME_STOP] = "stop",
	[MSG_KEYNAME_AGAIN] = "again",
	[MSG_KEYNAME_PROPS] = "props",
	[MSG_KEYNAME_UNDO] = "undo",
	[MSG_KEYNAME_FRONT] = "front",
	[MSG_KEYNAME_COPY] = "copy",
	[MSG_KEYNAME_OPEN] = "open",
	[MSG_KEYNAME_PASTE] = "paste",
	[MSG_KEYNAME_FIND] = "find",
	[MSG_KEYNAME_CUT] = "cut",
	[MSG_KEYNAME_HELP] = "help",
	[MSG_KEYNAME_MENU] = "menu",
	[MSG_KEYNAME_CALC] = "calc",
	[MSG_KEYNAME_SETUP] = "setup",
	[MSG_KEYNAME_SLEEP] = "sleep",
	[MSG_KEYNAME_WAKEUP] = "wakeup",
	[MSG_KEYNAME_FILE] = "file",
	[MSG_KEYNAME_SENDFILE] = "send file",
	[MSG_KEYNAME_DELFILE] = "delete file",
	[MSG_KEYNAME_XFER] = "transfer",
	[MSG_KEYNAME_PROG1] = "prog1",
	[MSG_KEYNAME_PROG2] = "prog2",
	[MSG_KEYNAME_WWW] = "www",
	[MSG_KEYNAME_MSDOS] = "msdos",
	[MSG_KEYNAME_COFFEE] = "coffee",
	[MSG_KEYNAME_DIRECTION] = "direction",
	[MSG_KEYNAME_CYCLEWINDOWS] = "cycle windows",
	[MSG_KEYNAME_MAIL] = "mail",
	[MSG_KEYNAME_BOOKMARKS] = "bookmarks",
	[MSG_KEYNAME_COMPUTER] = "computer",
	[MSG_KEYNAME_BACK] = "back",
	[MSG_KEYNAME_FORWARD] = "forward",
	[MSG_KEYNAME_CLOSECD] = "close cd",
	[MSG_KEYNAME_EJECTCD] = "eject cd",
	[MSG_KEYNAME_EJECTCLOSE] = "eject close cd",
	[MSG_KEYNAME_NEXTSONG] = "next song",
	[MSG_KEYNAME_PLAYPAUSE] = "play pause",
	[MSG_KEYNAME_PREVSONG] = "previous song",
	[MSG_KEYNAME_STOPCD] = "stop cd",
	[MSG_KEYNAME_RECORD] = "record",
	[MSG_KEYNAME_REWIND] = "rewind",
	[MSG_KEYNAME_PHONE] = "phone",
	[MSG_KEYNAME_ISO] = "iso",
	[MSG_KEYNAME_CONFIG] = "config",
	[MSG_KEYNAME_HOMEPG] = "home page",
	[MSG_KEYNAME_REFRESH] = "refresh",
	[MSG_KEYNAME_EXIT] = "exit",
	[MSG_KEYNAME_MOVE] = "move",
	[MSG_KEYNAME_EDIT] = "edit",
	[MSG_KEYNAME_SCROLLUP] = "scroll up",
	[MSG_KEYNAME_SCROLLDN] = "scroll down",
	[MSG_KEYNAME_KPLEFTPAR] = "keypad left paren",
	[MSG_KEYNAME_KPRIGHTPAR] = "keypad right paren",

	/* Function names. */
	[MSG_FUNCNAME_ATTRIB_BLEEP_DEC] = "attribute bleep decrement",
	[MSG_FUNCNAME_ATTRIB_BLEEP_INC] = "attribute bleep increment",
	[MSG_FUNCNAME_BLEEPS_DEC] = "bleeps decrement",
	[MSG_FUNCNAME_BLEEPS_INC] = "bleeps increment",
	[MSG_FUNCNAME_CHAR_FIRST] = "character, first",
	[MSG_FUNCNAME_CHAR_LAST] = "character, last",
	[MSG_FUNCNAME_CHAR_CURRENT] = "character, say current",
	[MSG_FUNCNAME_CHAR_HEX_AND_DEC] = "character, say hex and decimal",
	[MSG_FUNCNAME_CHAR_NEXT] = "character, say next",
	[MSG_FUNCNAME_CHAR_PHONETIC] = "character, say phonetic",
	[MSG_FUNCNAME_CHAR_PREVIOUS] = "character, say previous",
	[MSG_FUNCNAME_CURSOR_PARK] = "cursor park",
	[MSG_FUNCNAME_CUT] = "cut",
	[MSG_FUNCNAME_EDIT_DELIM] = "edit delimiters",
	[MSG_FUNCNAME_EDIT_EXNUM] = "edit exnum",
	[MSG_FUNCNAME_EDIT_MOST] = "edit most",
	[MSG_FUNCNAME_EDIT_REPEATS] = "edit repeats",
	[MSG_FUNCNAME_EDIT_SOME] = "edit some",
	[MSG_FUNCNAME_GOTO] = "go to",
	[MSG_FUNCNAME_GOTO_BOTTOM] = "go to bottom edge",
	[MSG_FUNCNAME_GOTO_LEFT] = "go to left edge",
	[MSG_FUNCNAME_GOTO_RIGHT] = "go to right edge",
	[MSG_FUNCNAME_GOTO_TOP] = "go to top edge",
	[MSG_FUNCNAME_HELP] = "help",
	[MSG_FUNCNAME_LINE_SAY_CURRENT] = "line, say current",
	[MSG_FUNCNAME_LINE_SAY_NEXT] = "line, say next",
	[MSG_FUNCNAME_LINE_SAY_PREVIOUS] = "line, say previous",
	[MSG_FUNCNAME_LINE_SAY_WITH_INDENT] = "line, say with indent",
	[MSG_FUNCNAME_PASTE] = "paste",
	[MSG_FUNCNAME_PITCH_DEC] = "pitch decrement",
	[MSG_FUNCNAME_PITCH_INC] = "pitch increment",
	[MSG_FUNCNAME_PUNC_DEC] = "punctuation decrement",
	[MSG_FUNCNAME_PUNC_INC] = "punctuation increment",
	[MSG_FUNCNAME_PUNC_LEVEL_DEC] = "punc level decrement",
	[MSG_FUNCNAME_PUNC_LEVEL_INC] = "punc level increment",
	[MSG_FUNCNAME_QUIET] = "quiet",
	[MSG_FUNCNAME_RATE_DEC] = "rate decrement",
	[MSG_FUNCNAME_RATE_INC] = "rate increment",
	[MSG_FUNCNAME_READING_PUNC_DEC] = "reading punctuation decrement",
	[MSG_FUNCNAME_READING_PUNC_INC] = "reading punctuation increment",
	[MSG_FUNCNAME_SAY_ATTRIBUTES] = "say attributes",
	[MSG_FUNCNAME_SAY_FROM_LEFT] = "say from left",
	[MSG_FUNCNAME_SAY_FROM_TOP] = "say from top",
	[MSG_FUNCNAME_SAY_POSITION] = "say position",
	[MSG_FUNCNAME_SAY_SCREEN] = "say screen",
	[MSG_FUNCNAME_SAY_TO_BOTTOM] = "say to bottom",
	[MSG_FUNCNAME_SAY_TO_RIGHT] = "say to right",
	[MSG_FUNCNAME_SPEAKUP] = "speakup",
	[MSG_FUNCNAME_SPEAKUP_LOCK] = "speakup lock",
	[MSG_FUNCNAME_SPEAKUP_OFF] = "speakup off",
	[MSG_FUNCNAME_SPEECH_KILL] = "speech kill",
	[MSG_FUNCNAME_SPELL_DELAY_DEC] = "spell delay decrement",
	[MSG_FUNCNAME_SPELL_DELAY_INC] = "spell delay increment",
	[MSG_FUNCNAME_SPELL_WORD] = "spell word",
	[MSG_FUNCNAME_SPELL_WORD_PHONETICALLY] = "spell word phonetically",
	[MSG_FUNCNAME_TONE_DEC] = "tone decrement",
	[MSG_FUNCNAME_TONE_INC] = "tone increment",
	[MSG_FUNCNAME_VOICE_DEC] = "voice decrement",
	[MSG_FUNCNAME_VOICE_INC] = "voice increment",
	[MSG_FUNCNAME_VOLUME_DEC] = "volume decrement",
	[MSG_FUNCNAME_VOLUME_INC] = "volume increment",
	[MSG_FUNCNAME_WINDOW_CLEAR] = "window, clear",
	[MSG_FUNCNAME_WINDOW_SAY] = "window, say",
	[MSG_FUNCNAME_WINDOW_SET] = "window, set",
	[MSG_FUNCNAME_WINDOW_SILENCE] = "window, silence",
	[MSG_FUNCNAME_WORD_SAY_CURRENT] = "word, say current",
	[MSG_FUNCNAME_WORD_SAY_NEXT] = "word, say next",
	[MSG_FUNCNAME_WORD_SAY_PREVIOUS] = "word, say previous",
};

static struct msg_group_t all_groups[] = {
	{
		.name = "ctl_keys",
		.start = MSG_CTL_START,
		.end = MSG_CTL_END,
	},
	{
		.name = "colors",
		.start = MSG_COLORS_START,
		.end = MSG_COLORS_END,
	},
	{
		.name = "formatted",
		.start = MSG_FORMATTED_START,
		.end = MSG_FORMATTED_END,
	},
	{
		.name = "function_names",
		.start = MSG_FUNCNAMES_START,
		.end = MSG_FUNCNAMES_END,
	},
	{
		.name = "key_names",
		.start = MSG_KEYNAMES_START,
		.end = MSG_KEYNAMES_END,
	},
	{
		.name = "announcements",
		.start = MSG_ANNOUNCEMENTS_START,
		.end = MSG_ANNOUNCEMENTS_END,
	},
	{
		.name = "states",
		.start = MSG_STATES_START,
		.end = MSG_STATES_END,
	},
};

static const  int num_groups = ARRAY_SIZE(all_groups);

char *spk_msg_get(enum msg_index_t index)
{
	return speakup_msgs[index];
}

/*
 * Function: next_specifier
 * Finds the start of the next format specifier in the argument string.
 * Return value: pointer to start of format
 * specifier, or NULL if no specifier exists.
 */
static char *next_specifier(char *input)
{
	int found = 0;
	char *next_percent = input;

	while (next_percent && !found) {
		next_percent = strchr(next_percent, '%');
		if (next_percent) {
			/* skip over doubled percent signs */
			while (next_percent[0] == '%' &&
			       next_percent[1] == '%')
				next_percent += 2;
			if (*next_percent == '%')
				found = 1;
			else if (*next_percent == '\0')
				next_percent = NULL;
		}
	}

	return next_percent;
}

/* Skip over 0 or more flags. */
static char *skip_flags(char *input)
{
	while ((*input != '\0') && strchr(" 0+-#", *input))
		input++;
	return input;
}

/* Skip over width.precision, if it exists. */
static char *skip_width(char *input)
{
	while (isdigit(*input))
		input++;
	if (*input == '.') {
		input++;
		while (isdigit(*input))
			input++;
	}
	return input;
}

/*
 * Skip past the end of the conversion part.
 * Note that this code only accepts a handful of conversion specifiers:
 * c d s x and ld.  Not accidental; these are exactly the ones used in
 * the default group of formatted messages.
 */
static char *skip_conversion(char *input)
{
	if ((input[0] == 'l') && (input[1] == 'd'))
		input += 2;
	else if ((*input != '\0') && strchr("cdsx", *input))
		input++;
	return input;
}

/*
 * Function: find_specifier_end
 * Return a pointer to the end of the format specifier.
 */
static char *find_specifier_end(char *input)
{
	input++;		/* Advance over %. */
	input = skip_flags(input);
	input = skip_width(input);
	input = skip_conversion(input);
	return input;
}

/*
 * Function: compare_specifiers
 * Compare the format specifiers pointed to by *input1 and *input2.
 * Return true if they are the same, false otherwise.
 * Advance *input1 and *input2 so that they point to the character following
 * the end of the specifier.
 */
static bool compare_specifiers(char **input1, char **input2)
{
	bool same = false;
	char *end1 = find_specifier_end(*input1);
	char *end2 = find_specifier_end(*input2);
	size_t length1 = end1 - *input1;
	size_t length2 = end2 - *input2;

	if ((length1 == length2) && !memcmp(*input1, *input2, length1))
		same = true;

	*input1 = end1;
	*input2 = end2;
	return same;
}

/*
 * Function: fmt_validate
 * Check that two format strings contain the same number of format specifiers,
 * and that the order of specifiers is the same in both strings.
 * Return true if the condition holds, false if it doesn't.
 */
static bool fmt_validate(char *template, char *user)
{
	bool valid = true;
	bool still_comparing = true;
	char *template_ptr = template;
	char *user_ptr = user;

	while (still_comparing && valid) {
		template_ptr = next_specifier(template_ptr);
		user_ptr = next_specifier(user_ptr);
		if (template_ptr && user_ptr) {
			/* Both have at least one more specifier. */
			valid = compare_specifiers(&template_ptr, &user_ptr);
		} else {
			/* No more format specifiers in one or both strings. */
			still_comparing = false;
			/* See if one has more specifiers than the other. */
			if (template_ptr || user_ptr)
				valid = false;
		}
	}
	return valid;
}

/*
 * Function: msg_set
 * Description: Add a user-supplied message to the user_messages array.
 * The message text is copied to a memory area allocated with kmalloc.
 * If the function fails, then user_messages is untouched.
 * Arguments:
 * - index: a message number, as found in i18n.h.
 * - text:  text of message.  Not NUL-terminated.
 * - length: number of bytes in text.
 * Failure conditions:
 * -EINVAL -  Invalid format specifiers in formatted message or illegal index.
 * -ENOMEM -  Unable to allocate memory.
 */
ssize_t spk_msg_set(enum msg_index_t index, char *text, size_t length)
{
	char *newstr = NULL;
	unsigned long flags;

	if ((index < MSG_FIRST_INDEX) || (index >= MSG_LAST_INDEX))
		return -EINVAL;

	newstr = kmalloc(length + 1, GFP_KERNEL);
	if (!newstr)
		return -ENOMEM;

	memcpy(newstr, text, length);
	newstr[length] = '\0';
	if (index >= MSG_FORMATTED_START &&
	    index <= MSG_FORMATTED_END &&
	    !fmt_validate(speakup_default_msgs[index], newstr)) {
		kfree(newstr);
		return -EINVAL;
	}
	spin_lock_irqsave(&speakup_info.spinlock, flags);
	if (speakup_msgs[index] != speakup_default_msgs[index])
		kfree(speakup_msgs[index]);
	speakup_msgs[index] = newstr;
	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
	return 0;
}

/*
 * Find a message group, given its name.  Return a pointer to the structure
 * if found, or NULL otherwise.
 */
struct msg_group_t *spk_find_msg_group(const char *group_name)
{
	struct msg_group_t *group = NULL;
	int i;

	for (i = 0; i < num_groups; i++) {
		if (!strcmp(all_groups[i].name, group_name)) {
			group = &all_groups[i];
			break;
		}
	}
	return group;
}

void spk_reset_msg_group(struct msg_group_t *group)
{
	unsigned long flags;
	enum msg_index_t i;

	spin_lock_irqsave(&speakup_info.spinlock, flags);

	for (i = group->start; i <= group->end; i++) {
		if (speakup_msgs[i] != speakup_default_msgs[i])
			kfree(speakup_msgs[i]);
		speakup_msgs[i] = speakup_default_msgs[i];
	}
	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
}

/* Called at initialization time, to establish default messages. */
void spk_initialize_msgs(void)
{
	memcpy(speakup_msgs, speakup_default_msgs,
	       sizeof(speakup_default_msgs));
}

/* Free user-supplied strings when module is unloaded: */
void spk_free_user_msgs(void)
{
	enum msg_index_t index;
	unsigned long flags;

	spin_lock_irqsave(&speakup_info.spinlock, flags);
	for (index = MSG_FIRST_INDEX; index < MSG_LAST_INDEX; index++) {
		if (speakup_msgs[index] != speakup_default_msgs[index]) {
			kfree(speakup_msgs[index]);
			speakup_msgs[index] = speakup_default_msgs[index];
		}
	}
	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
}