genmap.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* genmap.c
  3. * originally written by: Kirk Reiser.
  4. *
  5. ** Copyright (C) 2002 Kirk Reiser.
  6. * Copyright (C) 2003 David Borowski.
  7. */
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <libgen.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include "utils.h"
  14. struct st_key_init {
  15. char *name;
  16. int value, shift;
  17. };
  18. static unsigned char key_data[MAXKEYVAL][16], *kp;
  19. #include "mapdata.h"
  20. static const char delims[] = "\t\n ";
  21. static char *cp;
  22. static int map_ver = 119; /* an arbitrary number so speakup can check */
  23. static int shift_table[17];
  24. static int max_states = 1, flags;
  25. /* flags reserved for later, maybe for individual console maps */
  26. static int get_shift_value(int state)
  27. {
  28. int i;
  29. for (i = 0; shift_table[i] != state; i++) {
  30. if (shift_table[i] == -1) {
  31. if (i >= 16)
  32. oops("too many shift states", NULL);
  33. shift_table[i] = state;
  34. max_states = i+1;
  35. break;
  36. }
  37. }
  38. return i;
  39. }
  40. int
  41. main(int argc, char *argv[])
  42. {
  43. int value, shift_state, i, spk_val = 0, lock_val = 0;
  44. int max_key_used = 0, num_keys_used = 0;
  45. struct st_key *this;
  46. struct st_key_init *p_init;
  47. char buffer[256];
  48. bzero(key_table, sizeof(key_table));
  49. bzero(key_data, sizeof(key_data));
  50. shift_table[0] = 0;
  51. for (i = 1; i <= 16; i++)
  52. shift_table[i] = -1;
  53. if (argc < 2) {
  54. fputs("usage: genmap filename\n", stderr);
  55. exit(1);
  56. }
  57. for (p_init = init_key_data; p_init->name[0] != '.'; p_init++)
  58. add_key(p_init->name, p_init->value, p_init->shift);
  59. open_input(NULL, argv[1]);
  60. while (fgets(buffer, sizeof(buffer), infile)) {
  61. lc++;
  62. value = shift_state = 0;
  63. cp = strtok(buffer, delims);
  64. if (*cp == '#')
  65. continue;
  66. while (cp) {
  67. if (*cp == '=')
  68. break;
  69. this = find_key(cp);
  70. if (this == NULL)
  71. oops("unknown key/modifier", cp);
  72. if (this->shift == is_shift) {
  73. if (value)
  74. oops("modifiers must come first", cp);
  75. shift_state += this->value;
  76. } else if (this->shift == is_input)
  77. value = this->value;
  78. else
  79. oops("bad modifier or key", cp);
  80. cp = strtok(0, delims);
  81. }
  82. if (!cp)
  83. oops("no = found", NULL);
  84. cp = strtok(0, delims);
  85. if (!cp)
  86. oops("no speakup function after =", NULL);
  87. this = find_key(cp);
  88. if (this == NULL || this->shift != is_spk)
  89. oops("invalid speakup function", cp);
  90. i = get_shift_value(shift_state);
  91. if (key_data[value][i]) {
  92. while (--cp > buffer)
  93. if (!*cp)
  94. *cp = ' ';
  95. oops("two functions on same key combination", cp);
  96. }
  97. key_data[value][i] = (char)this->value;
  98. if (value > max_key_used)
  99. max_key_used = value;
  100. }
  101. fclose(infile);
  102. this = find_key("spk_key");
  103. if (this)
  104. spk_val = this->value;
  105. this = find_key("spk_lock");
  106. if (this)
  107. lock_val = this->value;
  108. for (lc = 1; lc <= max_key_used; lc++) {
  109. kp = key_data[lc];
  110. if (!memcmp(key_data[0], kp, 16))
  111. continue;
  112. num_keys_used++;
  113. for (i = 0; i < max_states; i++) {
  114. if (kp[i] != spk_val && kp[i] != lock_val)
  115. continue;
  116. shift_state = shift_table[i];
  117. if (shift_state&16)
  118. continue;
  119. shift_state = get_shift_value(shift_state+16);
  120. kp[shift_state] = kp[i];
  121. /* fill in so we can process the key up, as spk bit will be set */
  122. }
  123. }
  124. printf("\t%d, %d, %d,\n\t", map_ver, num_keys_used, max_states);
  125. for (i = 0; i < max_states; i++)
  126. printf("%d, ", shift_table[i]);
  127. printf("%d,", flags);
  128. for (lc = 1; lc <= max_key_used; lc++) {
  129. kp = key_data[lc];
  130. if (!memcmp(key_data[0], kp, 16))
  131. continue;
  132. printf("\n\t%d,", lc);
  133. for (i = 0; i < max_states; i++)
  134. printf(" %d,", (unsigned int)kp[i]);
  135. }
  136. printf("\n\t0, %d\n", map_ver);
  137. exit(0);
  138. }