varhandlers.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ctype.h>
  3. #include "spk_types.h"
  4. #include "spk_priv.h"
  5. #include "speakup.h"
  6. static struct st_var_header var_headers[] = {
  7. { "version", VERSION, VAR_PROC, NULL, NULL },
  8. { "synth_name", SYNTH, VAR_PROC, NULL, NULL },
  9. { "keymap", KEYMAP, VAR_PROC, NULL, NULL },
  10. { "silent", SILENT, VAR_PROC, NULL, NULL },
  11. { "punc_some", PUNC_SOME, VAR_PROC, NULL, NULL },
  12. { "punc_most", PUNC_MOST, VAR_PROC, NULL, NULL },
  13. { "punc_all", PUNC_ALL, VAR_PROC, NULL, NULL },
  14. { "delimiters", DELIM, VAR_PROC, NULL, NULL },
  15. { "repeats", REPEATS, VAR_PROC, NULL, NULL },
  16. { "ex_num", EXNUMBER, VAR_PROC, NULL, NULL },
  17. { "characters", CHARS, VAR_PROC, NULL, NULL },
  18. { "synth_direct", SYNTH_DIRECT, VAR_PROC, NULL, NULL },
  19. { "caps_start", CAPS_START, VAR_STRING, spk_str_caps_start, NULL },
  20. { "caps_stop", CAPS_STOP, VAR_STRING, spk_str_caps_stop, NULL },
  21. { "delay_time", DELAY, VAR_TIME, NULL, NULL },
  22. { "trigger_time", TRIGGER, VAR_TIME, NULL, NULL },
  23. { "jiffy_delta", JIFFY, VAR_TIME, NULL, NULL },
  24. { "full_time", FULL, VAR_TIME, NULL, NULL },
  25. { "spell_delay", SPELL_DELAY, VAR_NUM, &spk_spell_delay, NULL },
  26. { "bleeps", BLEEPS, VAR_NUM, &spk_bleeps, NULL },
  27. { "attrib_bleep", ATTRIB_BLEEP, VAR_NUM, &spk_attrib_bleep, NULL },
  28. { "bleep_time", BLEEP_TIME, VAR_TIME, &spk_bleep_time, NULL },
  29. { "cursor_time", CURSOR_TIME, VAR_TIME, NULL, NULL },
  30. { "punc_level", PUNC_LEVEL, VAR_NUM, &spk_punc_level, NULL },
  31. { "reading_punc", READING_PUNC, VAR_NUM, &spk_reading_punc, NULL },
  32. { "say_control", SAY_CONTROL, VAR_NUM, &spk_say_ctrl, NULL },
  33. { "say_word_ctl", SAY_WORD_CTL, VAR_NUM, &spk_say_word_ctl, NULL },
  34. { "no_interrupt", NO_INTERRUPT, VAR_NUM, &spk_no_intr, NULL },
  35. { "key_echo", KEY_ECHO, VAR_NUM, &spk_key_echo, NULL },
  36. { "bell_pos", BELL_POS, VAR_NUM, &spk_bell_pos, NULL },
  37. { "rate", RATE, VAR_NUM, NULL, NULL },
  38. { "pitch", PITCH, VAR_NUM, NULL, NULL },
  39. { "vol", VOL, VAR_NUM, NULL, NULL },
  40. { "tone", TONE, VAR_NUM, NULL, NULL },
  41. { "punct", PUNCT, VAR_NUM, NULL, NULL },
  42. { "voice", VOICE, VAR_NUM, NULL, NULL },
  43. { "freq", FREQUENCY, VAR_NUM, NULL, NULL },
  44. { "lang", LANG, VAR_NUM, NULL, NULL },
  45. { "chartab", CHARTAB, VAR_PROC, NULL, NULL },
  46. { "direct", DIRECT, VAR_NUM, NULL, NULL },
  47. { "pause", PAUSE, VAR_STRING, spk_str_pause, NULL },
  48. };
  49. static struct st_var_header *var_ptrs[MAXVARS] = { NULL, NULL, NULL };
  50. static struct punc_var_t punc_vars[] = {
  51. { PUNC_SOME, 1 },
  52. { PUNC_MOST, 2 },
  53. { PUNC_ALL, 3 },
  54. { DELIM, 4 },
  55. { REPEATS, 5 },
  56. { EXNUMBER, 6 },
  57. { -1, -1 },
  58. };
  59. int spk_chartab_get_value(char *keyword)
  60. {
  61. int value = 0;
  62. if (!strcmp(keyword, "ALPHA"))
  63. value = ALPHA;
  64. else if (!strcmp(keyword, "B_CTL"))
  65. value = B_CTL;
  66. else if (!strcmp(keyword, "WDLM"))
  67. value = WDLM;
  68. else if (!strcmp(keyword, "A_PUNC"))
  69. value = A_PUNC;
  70. else if (!strcmp(keyword, "PUNC"))
  71. value = PUNC;
  72. else if (!strcmp(keyword, "NUM"))
  73. value = NUM;
  74. else if (!strcmp(keyword, "A_CAP"))
  75. value = A_CAP;
  76. else if (!strcmp(keyword, "B_CAPSYM"))
  77. value = B_CAPSYM;
  78. else if (!strcmp(keyword, "B_SYM"))
  79. value = B_SYM;
  80. return value;
  81. }
  82. void speakup_register_var(struct var_t *var)
  83. {
  84. static char nothing[2] = "\0";
  85. int i;
  86. struct st_var_header *p_header;
  87. BUG_ON(!var || var->var_id < 0 || var->var_id >= MAXVARS);
  88. if (!var_ptrs[0]) {
  89. for (i = 0; i < MAXVARS; i++) {
  90. p_header = &var_headers[i];
  91. var_ptrs[p_header->var_id] = p_header;
  92. p_header->data = NULL;
  93. }
  94. }
  95. p_header = var_ptrs[var->var_id];
  96. if (p_header->data)
  97. return;
  98. p_header->data = var;
  99. switch (p_header->var_type) {
  100. case VAR_STRING:
  101. spk_set_string_var(nothing, p_header, 0);
  102. break;
  103. case VAR_NUM:
  104. case VAR_TIME:
  105. spk_set_num_var(0, p_header, E_DEFAULT);
  106. break;
  107. default:
  108. break;
  109. }
  110. }
  111. void speakup_unregister_var(enum var_id_t var_id)
  112. {
  113. struct st_var_header *p_header;
  114. BUG_ON(var_id < 0 || var_id >= MAXVARS);
  115. p_header = var_ptrs[var_id];
  116. p_header->data = NULL;
  117. }
  118. struct st_var_header *spk_get_var_header(enum var_id_t var_id)
  119. {
  120. struct st_var_header *p_header;
  121. if (var_id < 0 || var_id >= MAXVARS)
  122. return NULL;
  123. p_header = var_ptrs[var_id];
  124. if (!p_header->data)
  125. return NULL;
  126. return p_header;
  127. }
  128. struct st_var_header *spk_var_header_by_name(const char *name)
  129. {
  130. int i;
  131. if (!name)
  132. return NULL;
  133. for (i = 0; i < MAXVARS; i++) {
  134. if (strcmp(name, var_ptrs[i]->name) == 0)
  135. return var_ptrs[i];
  136. }
  137. return NULL;
  138. }
  139. struct var_t *spk_get_var(enum var_id_t var_id)
  140. {
  141. BUG_ON(var_id < 0 || var_id >= MAXVARS);
  142. BUG_ON(!var_ptrs[var_id]);
  143. return var_ptrs[var_id]->data;
  144. }
  145. EXPORT_SYMBOL_GPL(spk_get_var);
  146. struct punc_var_t *spk_get_punc_var(enum var_id_t var_id)
  147. {
  148. struct punc_var_t *rv = NULL;
  149. struct punc_var_t *where;
  150. where = punc_vars;
  151. while ((where->var_id != -1) && (!rv)) {
  152. if (where->var_id == var_id)
  153. rv = where;
  154. else
  155. where++;
  156. }
  157. return rv;
  158. }
  159. /* handlers for setting vars */
  160. int spk_set_num_var(int input, struct st_var_header *var, int how)
  161. {
  162. int val;
  163. int *p_val = var->p_val;
  164. char buf[32];
  165. char *cp;
  166. struct var_t *var_data = var->data;
  167. if (!var_data)
  168. return -ENODATA;
  169. val = var_data->u.n.value;
  170. switch (how) {
  171. case E_NEW_DEFAULT:
  172. if (input < var_data->u.n.low || input > var_data->u.n.high)
  173. return -ERANGE;
  174. var_data->u.n.default_val = input;
  175. return 0;
  176. case E_DEFAULT:
  177. val = var_data->u.n.default_val;
  178. break;
  179. case E_SET:
  180. val = input;
  181. break;
  182. case E_INC:
  183. val += input;
  184. break;
  185. case E_DEC:
  186. val -= input;
  187. break;
  188. }
  189. if (val < var_data->u.n.low || val > var_data->u.n.high)
  190. return -ERANGE;
  191. var_data->u.n.value = val;
  192. if (var->var_type == VAR_TIME && p_val) {
  193. *p_val = msecs_to_jiffies(val);
  194. return 0;
  195. }
  196. if (p_val)
  197. *p_val = val;
  198. if (var->var_id == PUNC_LEVEL) {
  199. spk_punc_mask = spk_punc_masks[val];
  200. return 0;
  201. }
  202. if (var_data->u.n.multiplier != 0)
  203. val *= var_data->u.n.multiplier;
  204. val += var_data->u.n.offset;
  205. if (var->var_id < FIRST_SYNTH_VAR || !synth)
  206. return 0;
  207. if (synth->synth_adjust)
  208. return synth->synth_adjust(var);
  209. if (!var_data->u.n.synth_fmt)
  210. return 0;
  211. if (var->var_id == PITCH)
  212. cp = spk_pitch_buff;
  213. else
  214. cp = buf;
  215. if (!var_data->u.n.out_str)
  216. sprintf(cp, var_data->u.n.synth_fmt, (int)val);
  217. else
  218. sprintf(cp, var_data->u.n.synth_fmt, var_data->u.n.out_str[val]);
  219. synth_printf("%s", cp);
  220. return 0;
  221. }
  222. int spk_set_string_var(const char *page, struct st_var_header *var, int len)
  223. {
  224. struct var_t *var_data = var->data;
  225. if (!var_data)
  226. return -ENODATA;
  227. if (len > MAXVARLEN)
  228. return -E2BIG;
  229. if (!len) {
  230. if (!var_data->u.s.default_val)
  231. return 0;
  232. if (!var->p_val)
  233. var->p_val = var_data->u.s.default_val;
  234. if (var->p_val != var_data->u.s.default_val)
  235. strcpy((char *)var->p_val, var_data->u.s.default_val);
  236. return -ERESTART;
  237. } else if (var->p_val) {
  238. strcpy((char *)var->p_val, page);
  239. } else {
  240. return -E2BIG;
  241. }
  242. return 0;
  243. }
  244. /*
  245. * spk_set_mask_bits sets or clears the punc/delim/repeat bits,
  246. * if input is null uses the defaults.
  247. * values for how: 0 clears bits of chars supplied,
  248. * 1 clears allk, 2 sets bits for chars
  249. */
  250. int spk_set_mask_bits(const char *input, const int which, const int how)
  251. {
  252. u_char *cp;
  253. short mask = spk_punc_info[which].mask;
  254. if (how & 1) {
  255. for (cp = (u_char *)spk_punc_info[3].value; *cp; cp++)
  256. spk_chartab[*cp] &= ~mask;
  257. }
  258. cp = (u_char *)input;
  259. if (!cp) {
  260. cp = spk_punc_info[which].value;
  261. } else {
  262. for (; *cp; cp++) {
  263. if (*cp < SPACE)
  264. break;
  265. if (mask < PUNC) {
  266. if (!(spk_chartab[*cp] & PUNC))
  267. break;
  268. } else if (spk_chartab[*cp] & B_NUM) {
  269. break;
  270. }
  271. }
  272. if (*cp)
  273. return -EINVAL;
  274. cp = (u_char *)input;
  275. }
  276. if (how & 2) {
  277. for (; *cp; cp++)
  278. if (*cp > SPACE)
  279. spk_chartab[*cp] |= mask;
  280. } else {
  281. for (; *cp; cp++)
  282. if (*cp > SPACE)
  283. spk_chartab[*cp] &= ~mask;
  284. }
  285. return 0;
  286. }
  287. char *spk_strlwr(char *s)
  288. {
  289. char *p;
  290. if (!s)
  291. return NULL;
  292. for (p = s; *p; p++)
  293. *p = tolower(*p);
  294. return s;
  295. }
  296. char *spk_s2uchar(char *start, char *dest)
  297. {
  298. int val;
  299. val = simple_strtoul(skip_spaces(start), &start, 10);
  300. if (*start == ',')
  301. start++;
  302. *dest = (u_char)val;
  303. return start;
  304. }