fixed bug in parse_config_file

there was a bug where parsing would ignore the very last valid hotkey in the
config file
xkbcommon
Alessandro Mauri 4 years ago
parent 684127c6fe
commit 5308b31abe
  1. 72
      hkd.c

72
hkd.c

@ -44,7 +44,7 @@
/* Value defines */ /* Value defines */
#define FILE_NAME_MAX_LENGTH 255 #define FILE_NAME_MAX_LENGTH 255
#define KEY_BUFFER_SIZE 16 #define KEY_BUFFER_SIZE 16
#define BLOCK_SIZE 512 #define BLOCK_SIZE 1024
/* ANSI colors escape codes */ /* ANSI colors escape codes */
#define ANSI_COLOR_RED "\x1b[31m" #define ANSI_COLOR_RED "\x1b[31m"
@ -662,12 +662,12 @@ void parse_config_file (void)
{ {
wordexp_t result = {0}; wordexp_t result = {0};
FILE *fd; FILE *fd;
int remaining = 0;
// 0: normal, 1: skip line 2: get directive 3: get keys 4: get command 5: output // 0: normal, 1: skip line 2: get directive 3: get keys 4: get command 5: output
int state = 0; int state = 0;
int alloc_tmp = 0, alloc_size = 0; int alloc_tmp = 0, alloc_size = 0;
int fuzzy = 0; int fuzzy = 0;
int i_tmp = 0, done = 0, linenum = 1; int i_tmp = 0, linenum = 1;
int exit_state = 0; /* 0: continue, 1: last block, 2: exit */
char block[BLOCK_SIZE + 1] = {0}; char block[BLOCK_SIZE + 1] = {0};
char *bb = NULL; char *bb = NULL;
char *keys = NULL; char *keys = NULL;
@ -720,31 +720,37 @@ void parse_config_file (void)
if (!fd) if (!fd)
die("Could not open any config files, check the log for more details"); die("Could not open any config files, check the log for more details");
} }
while (!done) { while (exit_state < 2) {
memset(block, 0, BLOCK_SIZE); int tmp = 0;
remaining = fread(block, sizeof(char), BLOCK_SIZE, fd); if (!exit_state) {
if (!remaining) memset(block, 0, BLOCK_SIZE + 1);
tmp = fread(block, sizeof(char), BLOCK_SIZE, fd);
if (!tmp)
break; break;
if (tmp < BLOCK_SIZE || feof(fd))
exit_state = 1;
}
bb = block; bb = block;
while (remaining > 0 && !done) { while (exit_state < 2) {
switch (state) { switch (state) {
// First state // First state
case 0: case 0:
// remove whitespaces // remove whitespaces
while (isblank(*bb) && remaining > 0) while (isblank(*bb))
bb++, remaining--; bb++;
if (remaining <= 0)
break;
// get state // get state
switch (*bb) { switch (*bb) {
case EOF:
case '\0':
// If it is the end of the last block exit
if (exit_state)
exit_state = 2;
break;
case '\n': case '\n':
case '#': case '#':
state = 1; state = 1;
break; break;
case '\0':
done = 1;
break;
default: default:
state = 2; state = 2;
break; break;
@ -752,12 +758,13 @@ void parse_config_file (void)
break; break;
// Skip line (comment) // Skip line (comment)
case 1: case 1:
while (*bb != '\n' && remaining > 0) while (*bb != '\n' && *bb)
bb++, remaining--; bb++;
bb++, remaining--; if (*bb) {
bb++;
linenum++; linenum++;
if (remaining > 0)
state = 0; state = 0;
}
break; break;
// Get compairson method // Get compairson method
case 2: case 2:
@ -774,7 +781,7 @@ void parse_config_file (void)
linenum); linenum);
break; break;
} }
bb++, remaining--; bb++;
state = 3; state = 3;
break; break;
// Get keys // Get keys
@ -790,19 +797,17 @@ void parse_config_file (void)
memset(&keys[alloc_size / 2], 0, alloc_size / 2); memset(&keys[alloc_size / 2], 0, alloc_size / 2);
} }
for (; remaining > 0 && for (; bb[alloc_tmp] &&
(bb[alloc_tmp] != ':' && bb[alloc_tmp] != '\n') && bb[alloc_tmp] != ':' && bb[alloc_tmp] != '\n' &&
alloc_tmp < alloc_size; alloc_tmp < alloc_size; alloc_tmp++);
remaining--, alloc_tmp++);
if (remaining <= 0 || alloc_tmp == alloc_size) { if (!bb[alloc_tmp] || alloc_tmp == alloc_size) {
strncat(keys, bb, alloc_tmp); strncat(keys, bb, alloc_tmp);
bb += alloc_tmp; bb += alloc_tmp;
break; break;
} else if (bb[alloc_tmp] == ':') { } else if (bb[alloc_tmp] == ':') {
strncat(keys, bb, alloc_tmp); strncat(keys, bb, alloc_tmp);
bb += alloc_tmp + 1; bb += alloc_tmp + 1;
remaining--;
state = 4; state = 4;
break; break;
} else { } else {
@ -824,12 +829,10 @@ void parse_config_file (void)
memset(&cmd[alloc_size / 2], 0, alloc_size / 2); memset(&cmd[alloc_size / 2], 0, alloc_size / 2);
} }
for (; remaining > 0 && for (; bb[alloc_tmp] && bb[alloc_tmp] != '\n' &&
bb[alloc_tmp] != '\n' && alloc_tmp < alloc_size; alloc_tmp++);
alloc_tmp < alloc_size;
remaining--, alloc_tmp++);
if (remaining <= 0 || alloc_tmp == alloc_size) { if (!bb[alloc_tmp] || alloc_tmp == alloc_size) {
strncat(cmd, bb, alloc_tmp); strncat(cmd, bb, alloc_tmp);
bb += alloc_tmp; bb += alloc_tmp;
break; break;
@ -838,12 +841,13 @@ void parse_config_file (void)
if (!(bb[alloc_tmp - 1] == '\\')) if (!(bb[alloc_tmp - 1] == '\\'))
state = 5; state = 5;
bb += alloc_tmp + 1; bb += alloc_tmp + 1;
remaining--;
linenum++; linenum++;
break; break;
} }
break; break;
case 5: case 5:
if (!keys)
die("error");
i_tmp = strlen(keys); i_tmp = strlen(keys);
for (int i = 0; i < i_tmp; i++) { for (int i = 0; i < i_tmp; i++) {
if (isblank(keys[i])) { if (isblank(keys[i])) {
@ -875,13 +879,13 @@ void parse_config_file (void)
hotkey_list_add(hotkey_list, &kb, cp_tmp, fuzzy); hotkey_list_add(hotkey_list, &kb, cp_tmp, fuzzy);
hotkey_size_mask |= 1 << (kb.size - 1); hotkey_size_mask |= 1 << (kb.size - 1);
// DO STUFF
key_buffer_reset(&kb); key_buffer_reset(&kb);
free(keys); free(keys);
free(cmd); free(cmd);
cp_tmp = keys = cmd = NULL; cp_tmp = keys = cmd = NULL;
i_tmp = state = 0; i_tmp = 0;
state = 0;
break; break;
default: default:
die("Unknown state in parse_config_file"); die("Unknown state in parse_config_file");

Loading…
Cancel
Save