Path: blob/master/drivers/accessibility/speakup/utils.h
26282 views
/* SPDX-License-Identifier: GPL-2.0+ */1/* utils.h2* originally written by: Kirk Reiser.3*4** Copyright (C) 2002 Kirk Reiser.5* Copyright (C) 2003 David Borowski.6*/78#include <stdio.h>910#define MAXKEYS 51211#define MAXKEYVAL 16012#define HASHSIZE 10113#define is_shift -314#define is_spk -215#define is_input -11617struct st_key {18char *name;19struct st_key *next;20int value, shift;21};2223struct st_key key_table[MAXKEYS];24struct st_key *extra_keys = key_table+HASHSIZE;25char *def_name, *def_val;26FILE *infile;27int lc;2829char filename[256];3031static inline void open_input(const char *dir_name, const char *name)32{33if (dir_name)34snprintf(filename, sizeof(filename), "%s/%s", dir_name, name);35else36snprintf(filename, sizeof(filename), "%s", name);37infile = fopen(filename, "r");38if (infile == 0) {39fprintf(stderr, "can't open %s\n", filename);40exit(1);41}42lc = 0;43}4445static inline int oops(const char *msg, const char *info)46{47if (info == NULL)48info = "";49fprintf(stderr, "error: file %s line %d\n", filename, lc);50fprintf(stderr, "%s %s\n", msg, info);51exit(1);52}5354static inline struct st_key *hash_name(char *name)55{56unsigned char *pn = (unsigned char *)name;57int hash = 0;5859while (*pn) {60hash = (hash * 17) & 0xfffffff;61if (isupper(*pn))62*pn = tolower(*pn);63hash += (int)*pn;64pn++;65}66hash %= HASHSIZE;67return &key_table[hash];68}6970static inline struct st_key *find_key(char *name)71{72struct st_key *this = hash_name(name);7374while (this) {75if (this->name && !strcmp(name, this->name))76return this;77this = this->next;78}79return this;80}8182static inline struct st_key *add_key(char *name, int value, int shift)83{84struct st_key *this = hash_name(name);8586if (extra_keys-key_table >= MAXKEYS)87oops("out of key table space, enlarge MAXKEYS", NULL);88if (this->name != NULL) {89while (this->next) {90if (!strcmp(name, this->name))91oops("attempt to add duplicate key", name);92this = this->next;93}94this->next = extra_keys++;95this = this->next;96}97this->name = strdup(name);98this->value = value;99this->shift = shift;100return this;101}102103104