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