1#include <linux/ctype.h>
2#include "spk_types.h"
3#include "spk_priv.h"
4#include "speakup.h"
5
6static 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
49static struct st_var_header *var_ptrs[MAXVARS] = { NULL, NULL, NULL };
50
51static 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
61int 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
86void 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]) {
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}
116
117void speakup_unregister_var(enum var_id_t var_id)
118{
119	struct st_var_header *p_header;
120
121	BUG_ON(var_id < 0 || var_id >= MAXVARS);
122	p_header = var_ptrs[var_id];
123	p_header->data = NULL;
124}
125
126struct st_var_header *spk_get_var_header(enum var_id_t var_id)
127{
128	struct st_var_header *p_header;
129
130	if (var_id < 0 || var_id >= MAXVARS)
131		return NULL;
132	p_header = var_ptrs[var_id];
133	if (!p_header->data)
134		return NULL;
135	return p_header;
136}
137
138struct st_var_header *spk_var_header_by_name(const char *name)
139{
140	int i;
141
142	if (!name)
143		return NULL;
144
145	for (i = 0; i < MAXVARS; i++) {
146		if (strcmp(name, var_ptrs[i]->name) == 0)
147			return var_ptrs[i];
148	}
149	return NULL;
150}
151
152struct var_t *spk_get_var(enum var_id_t var_id)
153{
154	BUG_ON(var_id < 0 || var_id >= MAXVARS);
155	BUG_ON(!var_ptrs[var_id]);
156	return var_ptrs[var_id]->data;
157}
158EXPORT_SYMBOL_GPL(spk_get_var);
159
160struct punc_var_t *spk_get_punc_var(enum var_id_t var_id)
161{
162	struct punc_var_t *rv = NULL;
163	struct punc_var_t *where;
164
165	where = punc_vars;
166	while ((where->var_id != -1) && (!rv)) {
167		if (where->var_id == var_id)
168			rv = where;
169		else
170			where++;
171	}
172	return rv;
173}
174
175/* handlers for setting vars */
176int spk_set_num_var(int input, struct st_var_header *var, int how)
177{
178	int val;
179	short ret = 0;
180	int *p_val = var->p_val;
181	int l;
182	char buf[32];
183	char *cp;
184	struct var_t *var_data = var->data;
185
186	if (!var_data)
187		return -ENODATA;
188
189	if (how == E_NEW_DEFAULT) {
190		if (input < var_data->u.n.low || input > var_data->u.n.high)
191			return -ERANGE;
192		var_data->u.n.default_val = input;
193		return 0;
194	}
195	if (how == E_DEFAULT) {
196		val = var_data->u.n.default_val;
197		ret = -ERESTART;
198	} else {
199		if (how == E_SET)
200			val = input;
201		else
202			val = var_data->u.n.value;
203		if (how == E_INC)
204			val += input;
205		else if (how == E_DEC)
206			val -= input;
207		if (val < var_data->u.n.low || val > var_data->u.n.high)
208			return -ERANGE;
209	}
210	var_data->u.n.value = val;
211	if (var->var_type == VAR_TIME && p_val != NULL) {
212		*p_val = msecs_to_jiffies(val);
213		return ret;
214	}
215	if (p_val != NULL)
216		*p_val = val;
217	if (var->var_id == PUNC_LEVEL) {
218		spk_punc_mask = spk_punc_masks[val];
219		return ret;
220	}
221	if (var_data->u.n.multiplier != 0)
222		val *= var_data->u.n.multiplier;
223	val += var_data->u.n.offset;
224	if (var->var_id < FIRST_SYNTH_VAR || !synth)
225		return ret;
226	if (synth->synth_adjust) {
227		int status = synth->synth_adjust(var);
228
229		return (status != 0) ? status : ret;
230	}
231	if (!var_data->u.n.synth_fmt)
232		return ret;
233	if (var->var_id == PITCH)
234		cp = spk_pitch_buff;
235	else
236		cp = buf;
237	if (!var_data->u.n.out_str)
238		l = sprintf(cp, var_data->u.n.synth_fmt, (int)val);
239	else
240		l = sprintf(cp,
241			var_data->u.n.synth_fmt, var_data->u.n.out_str[val]);
242	synth_printf("%s", cp);
243	return ret;
244}
245
246int spk_set_string_var(const char *page, struct st_var_header *var, int len)
247{
248	struct var_t *var_data = var->data;
249
250	if (!var_data)
251		return -ENODATA;
252	if (len > MAXVARLEN)
253		return -E2BIG;
254	if (!len) {
255		if (!var_data->u.s.default_val)
256			return 0;
257		if (!var->p_val)
258			var->p_val = var_data->u.s.default_val;
259		if (var->p_val != var_data->u.s.default_val)
260			strcpy((char *)var->p_val, var_data->u.s.default_val);
261		return -ERESTART;
262	} else if (var->p_val)
263		strcpy((char *)var->p_val, page);
264	else
265		return -E2BIG;
266	return 0;
267}
268
269/* spk_set_mask_bits sets or clears the punc/delim/repeat bits,
270 * if input is null uses the defaults.
271 * values for how: 0 clears bits of chars supplied,
272 * 1 clears allk, 2 sets bits for chars
273 */
274int 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
279	if (how&1) {
280		for (cp = (u_char *)spk_punc_info[3].value; *cp; cp++)
281			spk_chartab[*cp] &= ~mask;
282	}
283	cp = (u_char *)input;
284	if (!cp)
285		cp = spk_punc_info[which].value;
286	else {
287		for (; *cp; cp++) {
288			if (*cp < SPACE)
289				break;
290			if (mask < PUNC) {
291				if (!(spk_chartab[*cp] & PUNC))
292					break;
293			} else if (spk_chartab[*cp]&B_NUM)
294				break;
295		}
296		if (*cp)
297			return -EINVAL;
298		cp = (u_char *)input;
299	}
300	if (how&2) {
301		for (; *cp; cp++)
302			if (*cp > SPACE)
303				spk_chartab[*cp] |= mask;
304	} else {
305		for (; *cp; cp++)
306			if (*cp > SPACE)
307				spk_chartab[*cp] &= ~mask;
308	}
309	return 0;
310}
311
312char *spk_strlwr(char *s)
313{
314	char *p;
315
316	if (!s)
317		return NULL;
318
319	for (p = s; *p; p++)
320		*p = tolower(*p);
321	return s;
322}
323
324char *spk_s2uchar(char *start, char *dest)
325{
326	int val;
327
328	val = simple_strtoul(skip_spaces(start), &start, 10);
329	if (*start == ',')
330		start++;
331	*dest = (u_char)val;
332	return start;
333}
334