Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0+ */
0002 /* utils.h
0003  * originally written by: Kirk Reiser.
0004  *
0005  ** Copyright (C) 2002  Kirk Reiser.
0006  *  Copyright (C) 2003  David Borowski.
0007  */
0008 
0009 #include <stdio.h>
0010 
0011 #define MAXKEYS 512
0012 #define MAXKEYVAL 160
0013 #define HASHSIZE 101
0014 #define is_shift -3
0015 #define is_spk -2
0016 #define is_input -1
0017 
0018 struct st_key {
0019     char *name;
0020     struct st_key *next;
0021     int value, shift;
0022 };
0023 
0024 struct st_key key_table[MAXKEYS];
0025 struct st_key *extra_keys = key_table+HASHSIZE;
0026 char *def_name, *def_val;
0027 FILE *infile;
0028 int lc;
0029 
0030 char filename[256];
0031 
0032 static inline void open_input(const char *dir_name, const char *name)
0033 {
0034     if (dir_name)
0035         snprintf(filename, sizeof(filename), "%s/%s", dir_name, name);
0036     else
0037         snprintf(filename, sizeof(filename), "%s", name);
0038     infile = fopen(filename, "r");
0039     if (infile == 0) {
0040         fprintf(stderr, "can't open %s\n", filename);
0041         exit(1);
0042     }
0043     lc = 0;
0044 }
0045 
0046 static inline int oops(const char *msg, const char *info)
0047 {
0048     if (info == NULL)
0049         info = "";
0050     fprintf(stderr, "error: file %s line %d\n", filename, lc);
0051     fprintf(stderr, "%s %s\n", msg, info);
0052     exit(1);
0053 }
0054 
0055 static inline struct st_key *hash_name(char *name)
0056 {
0057     u_char *pn = (u_char *)name;
0058     int hash = 0;
0059 
0060     while (*pn) {
0061         hash = (hash * 17) & 0xfffffff;
0062         if (isupper(*pn))
0063             *pn = tolower(*pn);
0064         hash += (int)*pn;
0065         pn++;
0066     }
0067     hash %= HASHSIZE;
0068     return &key_table[hash];
0069 }
0070 
0071 static inline struct st_key *find_key(char *name)
0072 {
0073     struct st_key *this = hash_name(name);
0074 
0075     while (this) {
0076         if (this->name && !strcmp(name, this->name))
0077             return this;
0078         this = this->next;
0079     }
0080     return this;
0081 }
0082 
0083 static inline struct st_key *add_key(char *name, int value, int shift)
0084 {
0085     struct st_key *this = hash_name(name);
0086 
0087     if (extra_keys-key_table >= MAXKEYS)
0088         oops("out of key table space, enlarge MAXKEYS", NULL);
0089     if (this->name != NULL) {
0090         while (this->next) {
0091             if (!strcmp(name, this->name))
0092                 oops("attempt to add duplicate key", name);
0093             this = this->next;
0094         }
0095         this->next = extra_keys++;
0096         this = this->next;
0097     }
0098     this->name = strdup(name);
0099     this->value = value;
0100     this->shift = shift;
0101     return this;
0102 }