Compare commits
4 Commits
122ba9b44b
...
213db3cee5
Author | SHA1 | Date | |
---|---|---|---|
213db3cee5 | |||
5308b31abe | |||
684127c6fe | |||
699ddd057c |
4
hkd.1
4
hkd.1
@ -6,6 +6,7 @@ hkd \- simple HotKey Daemon
|
||||
.SH SYNOPSIS
|
||||
.SY hkd
|
||||
.OP \-v
|
||||
.OP \-d
|
||||
.OP \-c file
|
||||
.YS
|
||||
|
||||
@ -23,6 +24,9 @@ correctly.
|
||||
.IP \-v
|
||||
verbose, prints debug information and the internal pressed key buffer upon
|
||||
change acting as a crude keylogger :^)
|
||||
.IP \-d
|
||||
dump, used for debugging, it prints the whole hotkey list along with the
|
||||
information about the hotkeys (matching type, keys and associated command).
|
||||
.IP "\-c file"
|
||||
override default configuration file location, instead using the specified
|
||||
.I file
|
||||
|
137
hkd.c
137
hkd.c
@ -235,7 +235,7 @@ char *ext_config_file = NULL;
|
||||
/* Flags */
|
||||
int vflag = 0;
|
||||
int dead = 0; // exit flag
|
||||
|
||||
int dump = 0;
|
||||
/* key buffer operations */
|
||||
int key_buffer_add (struct key_buffer*, unsigned short);
|
||||
int key_buffer_remove (struct key_buffer*, unsigned short);
|
||||
@ -251,6 +251,7 @@ void remove_lock (void);
|
||||
void die (const char *, ...);
|
||||
int prepare_epoll (int *, int, int);
|
||||
unsigned short key_to_code (char *);
|
||||
const char * code_to_name (unsigned int);
|
||||
/* hotkey list operations */
|
||||
void hotkey_list_add (struct hotkey_list_e *, struct key_buffer *, char *, int);
|
||||
void hotkey_list_destroy (struct hotkey_list_e *);
|
||||
@ -288,7 +289,7 @@ int main (int argc, char *argv[])
|
||||
sigaction(SIGINT, &action, NULL);
|
||||
|
||||
/* Parse command line arguments */
|
||||
while ((opc = getopt(argc, argv, "vc:")) != -1) {
|
||||
while ((opc = getopt(argc, argv, "vc:d")) != -1) {
|
||||
switch (opc) {
|
||||
case 'v':
|
||||
vflag = 1;
|
||||
@ -298,6 +299,10 @@ int main (int argc, char *argv[])
|
||||
if (!ext_config_file)
|
||||
die("malloc in main():");
|
||||
strcpy(ext_config_file, optarg);
|
||||
break;
|
||||
case 'd':
|
||||
dump = 1;
|
||||
break;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -305,6 +310,20 @@ int main (int argc, char *argv[])
|
||||
/* Parse config file */
|
||||
parse_config_file();
|
||||
|
||||
/* If a dump is requested print the hotkey list then exit */
|
||||
if (dump) {
|
||||
printf("DUMPING HOTKEY LIST\n\n");
|
||||
for (struct hotkey_list_e *tmp = hotkey_list; tmp; tmp = tmp->next) {
|
||||
printf("Hotkey\n");
|
||||
printf("\tKeys: ");
|
||||
for (unsigned int i = 0; i < tmp->kb.size; i++)
|
||||
printf("%s ", code_to_name(tmp->kb.buf[i]));
|
||||
printf("\n\tMatching: %s\n", tmp->fuzzy ? "fuzzy" : "ordered");
|
||||
printf("\tCommand: %s\n\n", tmp->command);
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
/* Load descriptors */
|
||||
update_descriptors_list(&fds, &fd_num);
|
||||
|
||||
@ -375,17 +394,9 @@ int main (int argc, char *argv[])
|
||||
continue;
|
||||
|
||||
if (vflag) {
|
||||
int ci;
|
||||
printf("Pressed keys: ");
|
||||
for (unsigned int i = 0; i < pb.size; i++) {
|
||||
ci = i;
|
||||
while (pb.buf[i] != key_conversion_table[ci + 1].value) {
|
||||
if (ci >= array_size_const(key_conversion_table) - 2)
|
||||
break;
|
||||
ci++;
|
||||
}
|
||||
printf("%s ", key_conversion_table[ci + 1].name);
|
||||
}
|
||||
for (unsigned int i = 0; i < pb.size; i++)
|
||||
printf("%s ", code_to_name(pb.buf[i]));
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
@ -651,12 +662,13 @@ void parse_config_file (void)
|
||||
{
|
||||
wordexp_t result = {0};
|
||||
FILE *fd;
|
||||
int remaining = 0;
|
||||
// 0: normal, 1: skip line 2: get directive 3: get keys 4: get command 5: output
|
||||
int state = 0;
|
||||
int alloc_tmp = 0, alloc_size = 0;
|
||||
int fuzzy = 0;
|
||||
int i_tmp = 0, done = 0, linenum = 1;
|
||||
int i_tmp = 0, linenum = 1;
|
||||
// 0: aok, 1: request block, 2: last block, -1: exit
|
||||
int exit_state = 0;
|
||||
char block[BLOCK_SIZE + 1] = {0};
|
||||
char *bb = NULL;
|
||||
char *keys = NULL;
|
||||
@ -672,7 +684,7 @@ void parse_config_file (void)
|
||||
break;
|
||||
case WRDE_NOSPACE:
|
||||
/* If the error was WRDE_NOSPACE,
|
||||
* then perhaps part of the result was allocated */
|
||||
* then perhaps part of the result was allocated */
|
||||
wordfree (&result);
|
||||
die("Not enough space:");
|
||||
default:
|
||||
@ -692,7 +704,7 @@ void parse_config_file (void)
|
||||
break;
|
||||
case WRDE_NOSPACE:
|
||||
/* If the error was WRDE_NOSPACE,
|
||||
* then perhaps part of the result was allocated */
|
||||
* then perhaps part of the result was allocated */
|
||||
wordfree (&result);
|
||||
die("Not enough space:");
|
||||
default:
|
||||
@ -709,31 +721,37 @@ void parse_config_file (void)
|
||||
if (!fd)
|
||||
die("Could not open any config files, check the log for more details");
|
||||
}
|
||||
while (!done) {
|
||||
memset(block, 0, BLOCK_SIZE);
|
||||
remaining = fread(block, sizeof(char), BLOCK_SIZE, fd);
|
||||
if (!remaining)
|
||||
while (exit_state >= 0) {
|
||||
int tmp = 0;
|
||||
memset(block, 0, BLOCK_SIZE + 1);
|
||||
tmp = fread(block, sizeof(char), BLOCK_SIZE, fd);
|
||||
if (!tmp)
|
||||
break;
|
||||
if (tmp < BLOCK_SIZE || feof(fd))
|
||||
exit_state = 2;
|
||||
else
|
||||
exit_state = 0;
|
||||
bb = block;
|
||||
|
||||
while (remaining > 0 && !done) {
|
||||
while (exit_state == 0 || exit_state == 2) {
|
||||
switch (state) {
|
||||
// First state
|
||||
case 0:
|
||||
// remove whitespaces
|
||||
while (isblank(*bb) && remaining > 0)
|
||||
bb++, remaining--;
|
||||
if (remaining <= 0)
|
||||
break;
|
||||
while (isblank(*bb))
|
||||
bb++;
|
||||
// get state
|
||||
switch (*bb) {
|
||||
case EOF:
|
||||
case '\0':
|
||||
// If it is the end of the last block exit
|
||||
if (exit_state > 1)
|
||||
exit_state = -1;
|
||||
break;
|
||||
case '\n':
|
||||
case '#':
|
||||
state = 1;
|
||||
break;
|
||||
case '\0':
|
||||
done = 1;
|
||||
break;
|
||||
default:
|
||||
state = 2;
|
||||
break;
|
||||
@ -741,12 +759,15 @@ void parse_config_file (void)
|
||||
break;
|
||||
// Skip line (comment)
|
||||
case 1:
|
||||
while (*bb != '\n' && remaining > 0)
|
||||
bb++, remaining--;
|
||||
bb++, remaining--;
|
||||
linenum++;
|
||||
if (remaining > 0)
|
||||
while (*bb != '\n' && *bb)
|
||||
bb++;
|
||||
if (*bb) {
|
||||
bb++;
|
||||
linenum++;
|
||||
state = 0;
|
||||
} else {
|
||||
exit_state = 1;
|
||||
}
|
||||
break;
|
||||
// Get compairson method
|
||||
case 2:
|
||||
@ -763,7 +784,7 @@ void parse_config_file (void)
|
||||
linenum);
|
||||
break;
|
||||
}
|
||||
bb++, remaining--;
|
||||
bb++;
|
||||
state = 3;
|
||||
break;
|
||||
// Get keys
|
||||
@ -772,26 +793,27 @@ void parse_config_file (void)
|
||||
if (!(keys = malloc(alloc_size = (sizeof(char) * 64))))
|
||||
die("malloc for keys in parse_config_file():");
|
||||
memset(keys, 0, alloc_size);
|
||||
alloc_tmp = 0;
|
||||
} else if (alloc_tmp >= alloc_size) {
|
||||
if (!(keys = realloc(keys, alloc_size *= 2)))
|
||||
if (!(keys = realloc(keys, alloc_size = alloc_size * 2)))
|
||||
die("realloc for keys in parse_config_file():");
|
||||
memset(&keys[alloc_size / 2], 0, alloc_size / 2);
|
||||
}
|
||||
|
||||
for (; remaining > 0 &&
|
||||
(bb[alloc_tmp] != ':' && bb[alloc_tmp] != '\n') &&
|
||||
alloc_tmp < alloc_size;
|
||||
remaining--, alloc_tmp++);
|
||||
for (alloc_tmp = 0; bb[alloc_tmp] &&
|
||||
bb[alloc_tmp] != ':' && bb[alloc_tmp] != '\n' &&
|
||||
alloc_tmp < alloc_size; alloc_tmp++);
|
||||
|
||||
if (remaining <= 0 || alloc_tmp == alloc_size) {
|
||||
if (!bb[alloc_tmp] || alloc_tmp == alloc_size) {
|
||||
strncat(keys, bb, alloc_tmp);
|
||||
bb += alloc_tmp;
|
||||
if (exit_state > 1)
|
||||
die("Keys not finished before end of file");
|
||||
else
|
||||
exit_state = 1;
|
||||
break;
|
||||
} else if (bb[alloc_tmp] == ':') {
|
||||
strncat(keys, bb, alloc_tmp);
|
||||
bb += alloc_tmp + 1;
|
||||
remaining--;
|
||||
state = 4;
|
||||
break;
|
||||
} else {
|
||||
@ -806,33 +828,35 @@ void parse_config_file (void)
|
||||
if (!(cmd = malloc(alloc_size = (sizeof(char) * 128))))
|
||||
die("malloc for cmd in parse_config_file():");
|
||||
memset(cmd, 0, alloc_size);
|
||||
alloc_tmp = 0;
|
||||
} else if (alloc_tmp >= alloc_size) {
|
||||
if (!(cmd = realloc(cmd, alloc_size *= 2)))
|
||||
if (!(cmd = realloc(cmd, alloc_size = alloc_size * 2)))
|
||||
die("realloc for cmd in parse_config_file():");
|
||||
memset(&cmd[alloc_size / 2], 0, alloc_size / 2);
|
||||
}
|
||||
|
||||
for (; remaining > 0 &&
|
||||
bb[alloc_tmp] != '\n' &&
|
||||
alloc_tmp < alloc_size;
|
||||
remaining--, alloc_tmp++);
|
||||
for (alloc_tmp = 0; bb[alloc_tmp] && bb[alloc_tmp] != '\n' &&
|
||||
alloc_tmp < alloc_size; alloc_tmp++);
|
||||
|
||||
if (remaining <= 0 || alloc_tmp == alloc_size) {
|
||||
if (!bb[alloc_tmp] || alloc_tmp == alloc_size) {
|
||||
strncat(cmd, bb, alloc_tmp);
|
||||
bb += alloc_tmp;
|
||||
if (exit_state > 1)
|
||||
die("Command not finished before end of file");
|
||||
else
|
||||
exit_state = 1;
|
||||
break;
|
||||
} else {
|
||||
strncat(cmd, bb, alloc_tmp);
|
||||
if (!(bb[alloc_tmp - 1] == '\\'))
|
||||
state = 5;
|
||||
bb += alloc_tmp + 1;
|
||||
remaining--;
|
||||
linenum++;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
if (!keys)
|
||||
die("error");
|
||||
i_tmp = strlen(keys);
|
||||
for (int i = 0; i < i_tmp; i++) {
|
||||
if (isblank(keys[i])) {
|
||||
@ -864,13 +888,13 @@ void parse_config_file (void)
|
||||
|
||||
hotkey_list_add(hotkey_list, &kb, cp_tmp, fuzzy);
|
||||
hotkey_size_mask |= 1 << (kb.size - 1);
|
||||
// DO STUFF
|
||||
|
||||
key_buffer_reset(&kb);
|
||||
free(keys);
|
||||
free(cmd);
|
||||
cp_tmp = keys = cmd = NULL;
|
||||
i_tmp = state = 0;
|
||||
i_tmp = 0;
|
||||
state = 0;
|
||||
break;
|
||||
default:
|
||||
die("Unknown state in parse_config_file");
|
||||
@ -914,3 +938,12 @@ void die(const char *fmt, ...)
|
||||
va_end(ap);
|
||||
exit(errno ? errno : 1);
|
||||
}
|
||||
|
||||
const char * code_to_name (unsigned int code)
|
||||
{
|
||||
for (int i = 0; i < array_size_const(key_conversion_table); i++) {
|
||||
if (key_conversion_table[i].value == code)
|
||||
return key_conversion_table[i].name;
|
||||
}
|
||||
return "Key not recognized";
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user