1 #include <linux/ctype.h>
2 #include "spk_types.h"
3 #include "spk_priv.h"
4 #include "speakup.h"
5 
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 };
48 
49 static struct st_var_header *var_ptrs[MAXVARS] = { 0, 0, 0 };
50 
51 static struct punc_var_t punc_vars[] = {
52 	{ PUNC_SOME, 1 },
53 	{ PUNC_MOST, 2 },
54 	{ PUNC_ALL, 3 },
55 	{ DELIM, 4 },
56 	{ REPEATS, 5 },
57 	{ EXNUMBER, 6 },
58 	{ -1, -1 },
59 };
60 
spk_chartab_get_value(char * keyword)61 int spk_chartab_get_value(char *keyword)
62 {
63 	int value = 0;
64 
65 	if (!strcmp(keyword, "ALPHA"))
66 		value = ALPHA;
67 	else if (!strcmp(keyword, "B_CTL"))
68 		value = B_CTL;
69 	else if (!strcmp(keyword, "WDLM"))
70 		value = WDLM;
71 	else if (!strcmp(keyword, "A_PUNC"))
72 		value = A_PUNC;
73 	else if (!strcmp(keyword, "PUNC"))
74 		value = PUNC;
75 	else if (!strcmp(keyword, "NUM"))
76 		value = NUM;
77 	else if (!strcmp(keyword, "A_CAP"))
78 		value = A_CAP;
79 	else if (!strcmp(keyword, "B_CAPSYM"))
80 		value = B_CAPSYM;
81 	else if (!strcmp(keyword, "B_SYM"))
82 		value = B_SYM;
83 	return value;
84 }
85 
speakup_register_var(struct var_t * var)86 void speakup_register_var(struct var_t *var)
87 {
88 	static char nothing[2] = "\0";
89 	int i;
90 	struct st_var_header *p_header;
91 
92 	BUG_ON(!var || var->var_id < 0 || var->var_id >= MAXVARS);
93 	if (var_ptrs[0] == NULL) {
94 		for (i = 0; i < MAXVARS; i++) {
95 			p_header = &var_headers[i];
96 			var_ptrs[p_header->var_id] = p_header;
97 			p_header->data = NULL;
98 		}
99 	}
100 	p_header = var_ptrs[var->var_id];
101 	if (p_header->data != NULL)
102 		return;
103 	p_header->data = var;
104 	switch (p_header->var_type) {
105 	case VAR_STRING:
106 		spk_set_string_var(nothing, p_header, 0);
107 		break;
108 	case VAR_NUM:
109 	case VAR_TIME:
110 		spk_set_num_var(0, p_header, E_DEFAULT);
111 		break;
112 	default:
113 		break;
114 	}
115 	return;
116 }
117 
speakup_unregister_var(enum var_id_t var_id)118 void speakup_unregister_var(enum var_id_t var_id)
119 {
120 	struct st_var_header *p_header;
121 	BUG_ON(var_id < 0 || var_id >= MAXVARS);
122 	p_header = var_ptrs[var_id];
123 	p_header->data = NULL;
124 }
125 
spk_get_var_header(enum var_id_t var_id)126 struct st_var_header *spk_get_var_header(enum var_id_t var_id)
127 {
128 	struct st_var_header *p_header;
129 	if (var_id < 0 || var_id >= MAXVARS)
130 		return NULL;
131 	p_header = var_ptrs[var_id];
132 	if (p_header->data == NULL)
133 		return NULL;
134 	return p_header;
135 }
136 
spk_var_header_by_name(const char * name)137 struct st_var_header *spk_var_header_by_name(const char *name)
138 {
139 	int i;
140 	struct st_var_header *where = NULL;
141 
142 	if (name != NULL) {
143 		i = 0;
144 		while ((i < MAXVARS) && (where == NULL)) {
145 			if (strcmp(name, var_ptrs[i]->name) == 0)
146 				where = var_ptrs[i];
147 			else
148 				i++;
149 		}
150 	}
151 	return where;
152 }
153 
spk_get_var(enum var_id_t var_id)154 struct var_t *spk_get_var(enum var_id_t var_id)
155 {
156 	BUG_ON(var_id < 0 || var_id >= MAXVARS);
157 	BUG_ON(!var_ptrs[var_id]);
158 	return var_ptrs[var_id]->data;
159 }
160 EXPORT_SYMBOL_GPL(spk_get_var);
161 
spk_get_punc_var(enum var_id_t var_id)162 struct punc_var_t *spk_get_punc_var(enum var_id_t var_id)
163 {
164 	struct punc_var_t *rv = NULL;
165 	struct punc_var_t *where;
166 
167 	where = punc_vars;
168 	while ((where->var_id != -1) && (rv == NULL)) {
169 		if (where->var_id == var_id)
170 			rv = where;
171 		else
172 			where++;
173 	}
174 	return rv;
175 }
176 
177 /* handlers for setting vars */
spk_set_num_var(int input,struct st_var_header * var,int how)178 int spk_set_num_var(int input, struct st_var_header *var, int how)
179 {
180 	int val;
181 	short ret = 0;
182 	int *p_val = var->p_val;
183 	int l;
184 	char buf[32];
185 	char *cp;
186 	struct var_t *var_data = var->data;
187 	if (var_data == NULL)
188 		return E_UNDEF;
189 
190 	if (how == E_NEW_DEFAULT) {
191 		if (input < var_data->u.n.low || input > var_data->u.n.high)
192 			ret = E_RANGE;
193 		else
194 			var_data->u.n.default_val = input;
195 		return ret;
196 	}
197 	if (how == E_DEFAULT) {
198 		val = var_data->u.n.default_val;
199 		ret = SET_DEFAULT;
200 	} else {
201 		if (how == E_SET)
202 			val = input;
203 		else
204 			val = var_data->u.n.value;
205 		if (how == E_INC)
206 			val += input;
207 		else if (how == E_DEC)
208 			val -= input;
209 		if (val < var_data->u.n.low || val > var_data->u.n.high)
210 			return E_RANGE;
211 	}
212 	var_data->u.n.value = val;
213 	if (var->var_type == VAR_TIME && p_val != NULL) {
214 		*p_val = msecs_to_jiffies(val);
215 		return ret;
216 	}
217 	if (p_val != NULL)
218 		*p_val = val;
219 	if (var->var_id == PUNC_LEVEL) {
220 		spk_punc_mask = spk_punc_masks[val];
221 		return ret;
222 	}
223 	if (var_data->u.n.multiplier != 0)
224 		val *= var_data->u.n.multiplier;
225 	val += var_data->u.n.offset;
226 	if (var->var_id < FIRST_SYNTH_VAR || synth == NULL)
227 		return ret;
228 	if (synth->synth_adjust != NULL) {
229 		int status = synth->synth_adjust(var);
230 		return (status != 0) ? status : ret;
231 	}
232 	if (!var_data->u.n.synth_fmt)
233 		return ret;
234 	if (var->var_id == PITCH)
235 		cp = spk_pitch_buff;
236 	else
237 		cp = buf;
238 	if (!var_data->u.n.out_str)
239 		l = sprintf(cp, var_data->u.n.synth_fmt, (int)val);
240 	else
241 		l = sprintf(cp,
242 			var_data->u.n.synth_fmt, var_data->u.n.out_str[val]);
243 	synth_printf("%s", cp);
244 	return ret;
245 }
246 
spk_set_string_var(const char * page,struct st_var_header * var,int len)247 int spk_set_string_var(const char *page, struct st_var_header *var, int len)
248 {
249 	int ret = 0;
250 	struct var_t *var_data = var->data;
251 	if (var_data == NULL)
252 		return E_UNDEF;
253 	if (len > MAXVARLEN)
254 		return -E_TOOLONG;
255 	if (!len) {
256 		if (!var_data->u.s.default_val)
257 			return 0;
258 		ret = SET_DEFAULT;
259 		if (!var->p_val)
260 			var->p_val = var_data->u.s.default_val;
261 		if (var->p_val != var_data->u.s.default_val)
262 			strcpy((char *)var->p_val, var_data->u.s.default_val);
263 	} else if (var->p_val)
264 		strcpy((char *)var->p_val, page);
265 	else
266 		return -E_TOOLONG;
267 	return ret;
268 }
269 
270 /* spk_set_mask_bits sets or clears the punc/delim/repeat bits,
271  * if input is null uses the defaults.
272  * values for how: 0 clears bits of chars supplied,
273  * 1 clears allk, 2 sets bits for chars */
spk_set_mask_bits(const char * input,const int which,const int how)274 int spk_set_mask_bits(const char *input, const int which, const int how)
275 {
276 	u_char *cp;
277 	short mask = spk_punc_info[which].mask;
278 	if (how&1) {
279 		for (cp = (u_char *)spk_punc_info[3].value; *cp; cp++)
280 			spk_chartab[*cp] &= ~mask;
281 	}
282 	cp = (u_char *)input;
283 	if (cp == 0)
284 		cp = spk_punc_info[which].value;
285 	else {
286 		for ( ; *cp; cp++) {
287 			if (*cp < SPACE)
288 				break;
289 			if (mask < PUNC) {
290 				if (!(spk_chartab[*cp]&PUNC))
291 					break;
292 			} else if (spk_chartab[*cp]&B_NUM)
293 				break;
294 		}
295 		if (*cp)
296 			return -EINVAL;
297 		cp = (u_char *)input;
298 	}
299 	if (how&2) {
300 		for ( ; *cp; cp++)
301 			if (*cp > SPACE)
302 				spk_chartab[*cp] |= mask;
303 	} else {
304 		for ( ; *cp; cp++)
305 			if (*cp > SPACE)
306 				spk_chartab[*cp] &= ~mask;
307 	}
308 	return 0;
309 }
310 
spk_strlwr(char * s)311 char *spk_strlwr(char *s)
312 {
313 	char *p;
314 	if (s == NULL)
315 		return NULL;
316 
317 	for (p = s; *p; p++)
318 		*p = tolower(*p);
319 	return s;
320 }
321 
speakup_s2i(char * start,int * dest)322 char *speakup_s2i(char *start, int *dest)
323 {
324 	int val;
325 	char ch = *start;
326 	if (ch == '-' || ch == '+')
327 		start++;
328 	if (*start < '0' || *start > '9')
329 		return start;
330 	val = (*start) - '0';
331 	start++;
332 	while (*start >= '0' && *start <= '9') {
333 		val *= 10;
334 		val += (*start) - '0';
335 		start++;
336 	}
337 	if (ch == '-')
338 		*dest = -val;
339 	else
340 		*dest = val;
341 	return start;
342 }
343 
spk_s2uchar(char * start,char * dest)344 char *spk_s2uchar(char *start, char *dest)
345 {
346 	int val = 0;
347 	while (*start && *start <= SPACE)
348 		start++;
349 	while (*start >= '0' && *start <= '9') {
350 		val *= 10;
351 		val += (*start) - '0';
352 		start++;
353 	}
354 	if (*start == ',')
355 		start++;
356 	*dest = (u_char)val;
357 	return start;
358 }
359 
spk_xlate(char * s)360 char *spk_xlate(char *s)
361 {
362 	static const char finds[] = "nrtvafe";
363 	static const char subs[] = "\n\r\t\013\001\014\033";
364 	static const char hx[] = "0123456789abcdefABCDEF";
365 	char *p = s, *p1, *p2, c;
366 	int num;
367 	while ((p = strchr(p, '\\'))) {
368 		p1 = p+1;
369 		p2 = strchr(finds, *p1);
370 		if (p2) {
371 			*p++ = subs[p2-finds];
372 			p1++;
373 		} else if (*p1 >= '0' && *p1 <= '7') {
374 			num = (*p1++)&7;
375 			while (num < 256 && *p1 >= '0' && *p1 <= '7') {
376 				num <<= 3;
377 				num = (*p1++)&7;
378 			}
379 			*p++ = num;
380 		} else if (*p1 == 'x' &&
381 				strchr(hx, p1[1]) && strchr(hx, p1[2])) {
382 			p1++;
383 			c = *p1++;
384 			if (c > '9')
385 				c = (c - '7') & 0x0f;
386 			else
387 				c -= '0';
388 			num = c << 4;
389 			c = *p1++;
390 			if (c > '9')
391 				c = (c-'7')&0x0f;
392 			else
393 				c -= '0';
394 			num += c;
395 			*p++ = num;
396 		} else
397 			*p++ = *p1++;
398 		p2 = p;
399 		while (*p1)
400 			*p2++ = *p1++;
401 		*p2 = '\0';
402 	}
403 	return s;
404 }
405