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] = { NULL, NULL, NULL };
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]) {
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
speakup_unregister_var(enum var_id_t var_id)117 void 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
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
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
spk_var_header_by_name(const char * name)138 struct 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
spk_get_var(enum var_id_t var_id)152 struct 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 }
158 EXPORT_SYMBOL_GPL(spk_get_var);
159
spk_get_punc_var(enum var_id_t var_id)160 struct 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 */
spk_set_num_var(int input,struct st_var_header * var,int how)176 int spk_set_num_var(int input, struct st_var_header *var, int how)
177 {
178 int val;
179 int *p_val = var->p_val;
180 int l;
181 char buf[32];
182 char *cp;
183 struct var_t *var_data = var->data;
184
185 if (!var_data)
186 return -ENODATA;
187
188 val = var_data->u.n.value;
189 switch (how) {
190 case E_NEW_DEFAULT:
191 if (input < var_data->u.n.low || input > var_data->u.n.high)
192 return -ERANGE;
193 var_data->u.n.default_val = input;
194 return 0;
195 case E_DEFAULT:
196 val = var_data->u.n.default_val;
197 break;
198 case E_SET:
199 val = input;
200 break;
201 case E_INC:
202 val += input;
203 break;
204 case E_DEC:
205 val -= input;
206 break;
207 }
208
209 if (val < var_data->u.n.low || val > var_data->u.n.high)
210 return -ERANGE;
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 0;
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 0;
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)
227 return 0;
228 if (synth->synth_adjust)
229 return synth->synth_adjust(var);
230
231 if (!var_data->u.n.synth_fmt)
232 return 0;
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 0;
244 }
245
spk_set_string_var(const char * page,struct st_var_header * var,int len)246 int 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 */
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
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
spk_strlwr(char * s)312 char *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
spk_s2uchar(char * start,char * dest)324 char *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