1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include "grab-ng.h"
#include "commands.h"
#include "event.h"
#include "parseconfig.h"
/* ----------------------------------------------------------------------- */
static struct event_entry *event_conf_list;
static struct event_entry *event_builtin_list;
/* ----------------------------------------------------------------------- */
static void parse_action(struct event_entry *entry)
{
char *token,*h;
strcpy(entry->argbuf,entry->action);
h = entry->argbuf;
for (;;) {
while (' ' == *h || '\t' == *h)
h++;
if ('\0' == *h)
break;
if ('"' == *h) {
/* quoted string */
h++;
token = h;
while ('\0' != *h && '"' != *h)
h++;
} else {
/* normal string */
token = h;
while ('\0' != *h && ' ' != *h && '\t' != *h)
h++;
}
if ('\0' != *h) {
*h = 0;
h++;
}
entry->argv[entry->argc++] = token;
}
}
/* ----------------------------------------------------------------------- */
int event_register(char *event, char *action)
{
struct event_entry *entry;
entry = malloc(sizeof(*entry));
memset(entry,0,sizeof(*entry));
strncpy(entry->event,event,127);
strncpy(entry->action,action,127);
entry->next = event_conf_list;
event_conf_list = entry;
parse_action(entry);
if (debug)
fprintf(stderr,"ev: reg conf \"%s\" => \"%s\"\n",
entry->event,entry->action);
return 0;
}
int event_register_list(struct event_entry *entry)
{
for (; NULL != entry && 0 != entry->event[0]; entry++) {
entry->next = event_builtin_list;
event_builtin_list = entry;
parse_action(entry);
if (debug)
fprintf(stderr,"ev: reg built-in \"%s\" => \"%s\"\n",
entry->event,entry->action);
}
return 0;
}
void event_readconfig(void)
{
char **list,*val;
list = cfg_list_entries("eventmap");
if (NULL == list)
return;
for (; *list != NULL; list++)
if (NULL != (val = cfg_get_str("eventmap",*list)))
event_register(*list,val);
}
void event_writeconfig(FILE *fp)
{
struct event_entry *entry;
if (NULL == event_conf_list)
return;
fprintf(fp,"[eventmap]\n");
for (entry = event_conf_list; NULL != entry; entry = entry->next)
fprintf(fp,"%s = %s\n",entry->event,entry->action);
fprintf(fp,"\n");
}
/* ----------------------------------------------------------------------- */
int event_dispatch(char *event)
{
struct event_entry *entry = NULL;
char *name,*arg,*h,*argv[EVENT_ARGV_SIZE];
int argc;
/* parse */
if (NULL != (h = strchr(event,'('))) {
name = event;
arg = h+1;
*h = 0;
if (NULL != (h = strchr(arg,')')))
*h = 0;
if (debug)
fprintf(stderr,"ev: dispatch name=%s arg=%s\n",name,arg);
} else {
name = event;
arg = NULL;
if (debug)
fprintf(stderr,"ev: dispatch name=%s\n",name);
}
/* search lists */
if (NULL == entry)
for (entry = event_conf_list; NULL != entry; entry = entry->next)
if (0 == strcasecmp(name,entry->event))
break;
if (NULL == entry)
for (entry = event_builtin_list; NULL != entry; entry = entry->next)
if (0 == strcasecmp(name,entry->event))
break;
if (NULL == entry) {
if (debug)
fprintf(stderr,"ev: 404: %s\n",name);
return 0;
}
/* call action */
memcpy(argv,entry->argv,sizeof(argv));
argc = entry->argc;
if (arg)
argv[argc++] = arg;
do_command(argc,argv);
return 0;
}
|