utils.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /* utils.h
  3. * originally written by: Kirk Reiser.
  4. *
  5. ** Copyright (C) 2002 Kirk Reiser.
  6. * Copyright (C) 2003 David Borowski.
  7. */
  8. #include <stdio.h>
  9. #define MAXKEYS 512
  10. #define MAXKEYVAL 160
  11. #define HASHSIZE 101
  12. #define is_shift -3
  13. #define is_spk -2
  14. #define is_input -1
  15. struct st_key {
  16. char *name;
  17. struct st_key *next;
  18. int value, shift;
  19. };
  20. struct st_key key_table[MAXKEYS];
  21. struct st_key *extra_keys = key_table+HASHSIZE;
  22. char *def_name, *def_val;
  23. FILE *infile;
  24. int lc;
  25. char filename[256];
  26. static inline void open_input(const char *dir_name, const char *name)
  27. {
  28. if (dir_name)
  29. snprintf(filename, sizeof(filename), "%s/%s", dir_name, name);
  30. else
  31. snprintf(filename, sizeof(filename), "%s", name);
  32. infile = fopen(filename, "r");
  33. if (infile == 0) {
  34. fprintf(stderr, "can't open %s\n", filename);
  35. exit(1);
  36. }
  37. lc = 0;
  38. }
  39. static inline int oops(const char *msg, const char *info)
  40. {
  41. if (info == NULL)
  42. info = "";
  43. fprintf(stderr, "error: file %s line %d\n", filename, lc);
  44. fprintf(stderr, "%s %s\n", msg, info);
  45. exit(1);
  46. }
  47. static inline struct st_key *hash_name(char *name)
  48. {
  49. unsigned char *pn = (unsigned char *)name;
  50. int hash = 0;
  51. while (*pn) {
  52. hash = (hash * 17) & 0xfffffff;
  53. if (isupper(*pn))
  54. *pn = tolower(*pn);
  55. hash += (int)*pn;
  56. pn++;
  57. }
  58. hash %= HASHSIZE;
  59. return &key_table[hash];
  60. }
  61. static inline struct st_key *find_key(char *name)
  62. {
  63. struct st_key *this = hash_name(name);
  64. while (this) {
  65. if (this->name && !strcmp(name, this->name))
  66. return this;
  67. this = this->next;
  68. }
  69. return this;
  70. }
  71. static inline struct st_key *add_key(char *name, int value, int shift)
  72. {
  73. struct st_key *this = hash_name(name);
  74. if (extra_keys-key_table >= MAXKEYS)
  75. oops("out of key table space, enlarge MAXKEYS", NULL);
  76. if (this->name != NULL) {
  77. while (this->next) {
  78. if (!strcmp(name, this->name))
  79. oops("attempt to add duplicate key", name);
  80. this = this->next;
  81. }
  82. this->next = extra_keys++;
  83. this = this->next;
  84. }
  85. this->name = strdup(name);
  86. this->value = value;
  87. this->shift = shift;
  88. return this;
  89. }