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