better error messages for wrong config

xkbcommon
Alessandro Mauri 4 years ago
parent 9871f375ed
commit 0486872d78
  1. 53
      hkd.c

53
hkd.c

@ -408,8 +408,7 @@ int key_buffer_remove (struct key_buffer *pb, unsigned short key)
void key_buffer_reset (struct key_buffer *kb) void key_buffer_reset (struct key_buffer *kb)
{ {
kb->size = 0; kb->size = 0;
for (int i = 0; i < KEY_BUFFER_SIZE; i++) memset(kb->buf, 0, KEY_BUFFER_SIZE * sizeof(unsigned short));
kb->buf[i] = 0;
} }
void int_handler (int signum) void int_handler (int signum)
@ -616,20 +615,20 @@ void parse_config_file (void)
wordexp_t result = {0}; wordexp_t result = {0};
FILE *fd; FILE *fd;
int remaining = 0; int remaining = 0;
char block[BLOCK_SIZE + 1] = {0};
char *bb = NULL;
// 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;
char *keys = NULL;
char *cmd = NULL;
int alloc_tmp = 0, alloc_size = 0; int alloc_tmp = 0, alloc_size = 0;
int fuzzy = 0; int fuzzy = 0;
struct key_buffer kb; int i_tmp = 0, done = 0, linenum = 1;
char block[BLOCK_SIZE + 1] = {0};
int i_tmp = 0, done = 0; char *bb = NULL;
char *keys = NULL;
char *cmd = NULL;
char *cp_tmp = NULL; char *cp_tmp = NULL;
struct key_buffer kb;
unsigned short us_tmp = 0; unsigned short us_tmp = 0;
key_buffer_reset(&kb);
if (ext_config_file) { if (ext_config_file) {
switch (wordexp(ext_config_file, &result, 0)) { switch (wordexp(ext_config_file, &result, 0)) {
case 0: case 0:
@ -682,7 +681,7 @@ void parse_config_file (void)
break; break;
bb = block; bb = block;
while (remaining > 0) { while (remaining > 0 && !done) {
switch (state) { switch (state) {
// First state // First state
case 0: case 0:
@ -710,6 +709,7 @@ void parse_config_file (void)
while (*bb != '\n' && remaining > 0) while (*bb != '\n' && remaining > 0)
bb++, remaining--; bb++, remaining--;
bb++, remaining--; bb++, remaining--;
linenum++;
if (remaining > 0) if (remaining > 0)
state = 0; state = 0;
break; break;
@ -723,7 +723,10 @@ void parse_config_file (void)
fuzzy = 1; fuzzy = 1;
break; break;
default: default:
die("invalid line") fprintf(stderr, red("Error at line %d: "
"hotkey definition must start with '-' or '*'\n"),
linenum);
exit(-1);
break; break;
} }
bb++, remaining--; bb++, remaining--;
@ -758,7 +761,10 @@ void parse_config_file (void)
state = 4; state = 4;
break; break;
} else { } else {
die("no command"); fprintf(stderr, red("Error at line %d: "
"no command specified, missing ':' after keys\n"),
linenum);
exit(-1);
} }
break; break;
// Get command // Get command
@ -789,6 +795,7 @@ void parse_config_file (void)
state = 5; state = 5;
bb += alloc_tmp + 1; bb += alloc_tmp + 1;
remaining--; remaining--;
linenum++;
break; break;
} }
break; break;
@ -801,17 +808,29 @@ void parse_config_file (void)
} }
} }
cp_tmp = strtok(keys, ","); cp_tmp = strtok(keys, ",");
if(!cp_tmp) if(!cp_tmp) {
die("no keys"); fprintf(stderr, red("Error at line %d: "
"keys not present\n"), linenum - 1);
exit(-1);
}
do { do {
if (!(us_tmp = key_to_code(cp_tmp))) if (!(us_tmp = key_to_code(cp_tmp))) {
die("key not valid"); fprintf(stderr, red("Error at line %d: "
"%s is not a valid key\n"),
linenum - 1, cp_tmp);
exit(-1);
}
key_buffer_add(&kb, us_tmp); key_buffer_add(&kb, us_tmp);
} while ((cp_tmp = strtok(NULL, ","))); } while ((cp_tmp = strtok(NULL, ",")));
cp_tmp = cmd; cp_tmp = cmd;
while (isblank(*cp_tmp)) while (isblank(*cp_tmp))
cp_tmp++; cp_tmp++;
if (*cp_tmp == '\0') {
fprintf(stderr, red("Error at line %d: "
"command not present\n"), linenum - 1);
exit(-1);
}
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);

Loading…
Cancel
Save