Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /* genmap.c
0003  * originally written by: Kirk Reiser.
0004  *
0005  ** Copyright (C) 2002  Kirk Reiser.
0006  *  Copyright (C) 2003  David Borowski.
0007  */
0008 
0009 #include <stdlib.h>
0010 #include <stdio.h>
0011 #include <libgen.h>
0012 #include <string.h>
0013 #include <linux/version.h>
0014 #include <ctype.h>
0015 #include "utils.h"
0016 
0017 struct st_key_init {
0018     char *name;
0019     int value, shift;
0020 };
0021 
0022 static unsigned char key_data[MAXKEYVAL][16], *kp;
0023 
0024 #include "mapdata.h"
0025 
0026 static const char delims[] = "\t\n ";
0027 static char *cp;
0028 static int map_ver = 119; /* an arbitrary number so speakup can check */
0029 static int shift_table[17];
0030 static int max_states = 1, flags;
0031 /* flags reserved for later, maybe for individual console maps */
0032 
0033 static int get_shift_value(int state)
0034 {
0035     int i;
0036 
0037     for (i = 0; shift_table[i] != state; i++) {
0038         if (shift_table[i] == -1) {
0039             if (i >= 16)
0040                 oops("too many shift states", NULL);
0041             shift_table[i] = state;
0042             max_states = i+1;
0043         break;
0044     }
0045     }
0046     return i;
0047 }
0048 
0049 int
0050 main(int argc, char *argv[])
0051 {
0052     int value, shift_state, i, spk_val = 0, lock_val = 0;
0053     int max_key_used = 0, num_keys_used = 0;
0054     struct st_key *this;
0055     struct st_key_init *p_init;
0056     char buffer[256];
0057 
0058     bzero(key_table, sizeof(key_table));
0059     bzero(key_data, sizeof(key_data));
0060 
0061     shift_table[0] = 0;
0062     for (i = 1; i <= 16; i++)
0063         shift_table[i] = -1;
0064 
0065     if (argc < 2) {
0066         fputs("usage: genmap filename\n", stderr);
0067         exit(1);
0068     }
0069 
0070     for (p_init = init_key_data; p_init->name[0] != '.'; p_init++)
0071         add_key(p_init->name, p_init->value, p_init->shift);
0072 
0073     open_input(NULL, argv[1]);
0074     while (fgets(buffer, sizeof(buffer), infile)) {
0075         lc++;
0076         value = shift_state = 0;
0077 
0078         cp = strtok(buffer, delims);
0079         if (*cp == '#')
0080             continue;
0081 
0082         while (cp) {
0083             if (*cp == '=')
0084                 break;
0085             this = find_key(cp);
0086             if (this == NULL)
0087                 oops("unknown key/modifier", cp);
0088             if (this->shift == is_shift) {
0089                 if (value)
0090                     oops("modifiers must come first", cp);
0091                 shift_state += this->value;
0092             } else if (this->shift == is_input)
0093                 value = this->value;
0094             else
0095                 oops("bad modifier or key", cp);
0096             cp = strtok(0, delims);
0097         }
0098         if (!cp)
0099             oops("no = found", NULL);
0100 
0101         cp = strtok(0, delims);
0102         if (!cp)
0103             oops("no speakup function after =", NULL);
0104 
0105         this = find_key(cp);
0106         if (this == NULL || this->shift != is_spk)
0107             oops("invalid speakup function", cp);
0108 
0109         i = get_shift_value(shift_state);
0110         if (key_data[value][i]) {
0111             while (--cp > buffer)
0112                 if (!*cp)
0113                     *cp = ' ';
0114             oops("two functions on same key combination", cp);
0115         }
0116         key_data[value][i] = (char)this->value;
0117         if (value > max_key_used)
0118             max_key_used = value;
0119     }
0120     fclose(infile);
0121 
0122     this = find_key("spk_key");
0123     if (this)
0124         spk_val = this->value;
0125 
0126     this = find_key("spk_lock");
0127     if (this)
0128         lock_val = this->value;
0129 
0130     for (lc = 1; lc <= max_key_used; lc++) {
0131         kp = key_data[lc];
0132         if (!memcmp(key_data[0], kp, 16))
0133             continue;
0134         num_keys_used++;
0135         for (i = 0; i < max_states; i++) {
0136             if (kp[i] != spk_val && kp[i] != lock_val)
0137                 continue;
0138             shift_state = shift_table[i];
0139             if (shift_state&16)
0140                 continue;
0141             shift_state = get_shift_value(shift_state+16);
0142             kp[shift_state] = kp[i];
0143             /* fill in so we can process the key up, as spk bit will be set */
0144         }
0145     }
0146 
0147     printf("\t%d, %d, %d,\n\t", map_ver, num_keys_used, max_states);
0148     for (i = 0; i < max_states; i++)
0149         printf("%d, ", shift_table[i]);
0150     printf("%d,", flags);
0151     for (lc = 1; lc <= max_key_used; lc++) {
0152         kp = key_data[lc];
0153         if (!memcmp(key_data[0], kp, 16))
0154             continue;
0155         printf("\n\t%d,", lc);
0156         for (i = 0; i < max_states; i++)
0157             printf(" %d,", (unsigned int)kp[i]);
0158     }
0159     printf("\n\t0, %d\n", map_ver);
0160 
0161     exit(0);
0162 }