-
Notifications
You must be signed in to change notification settings - Fork 0
/
usbiff_config.c
235 lines (201 loc) · 4.71 KB
/
usbiff_config.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <string.h>
#include <stdlib.h>
#include <syslog.h>
#include "usbiff_config.h"
#include "usbiff_mbox.h"
#include "usbiff_signal.h"
#include "usbnotifier.h"
#include "usbiff_common.h"
static char *config_filename = NULL;
static int config_filename_provided = 0;
void
config_set_filename (char *filename)
{
free (config_filename);
config_filename = strdup (filename);
config_filename_provided = 1;
}
const char *
config_get_filename (void)
{
if (!config_filename)
return "<stdin>";
else
return config_filename;
}
struct config *
config_new (void)
{
struct config *res;
if ((res = malloc (sizeof (*res)))) {
res->mailboxes = NULL;
res->signals = NULL;
res->default_settings.color = COLOR_BLUE;
res->default_settings.flash = 1;
res->default_settings.ignore = 0;
res->default_settings.priority = 10;
res->default_settings.toggle = 0;
res->flash_delay.short_delay = 250;
res->flash_delay.long_delay = 1500;
}
return res;
}
struct config *
config_load (void)
{
struct config *res = config_new ();
if (!config_filename)
asprintf (&config_filename, "%s/.usbiffrc", getenv ("HOME"));
if (strcmp ("-", config_filename)) {
yyin = fopen (config_filename, "r");
if (!yyin) {
if (config_filename_provided) {
syslog (LOG_ERR, "Cannot read configuration file \"%s\"", config_filename);
exit (EXIT_FAILURE);
} else
goto no_configuration_file;
}
}
if (yyparse () != 0) {
syslog (LOG_ERR, "Cannot parse configuration file");
exit (EXIT_FAILURE);
}
no_configuration_file:
yyconfigure (res);
parsed_conf_free (prg);
yyclean ();
if (yyin)
fclose (yyin);
return res;
}
void
config_update_flash_delay (struct config *config, int short_delay, int long_delay)
{
config->flash_delay.short_delay = short_delay;
config->flash_delay.long_delay = long_delay;
}
void
config_update_default_settings (struct config *config, int color, int flash, int ignore, int priority, int toggle)
{
if (color >= 0)
config->default_settings.color = color;
if (flash >= 0)
config->default_settings.flash = flash;
if (ignore >= 0)
config->default_settings.ignore = ignore;
if (priority != PRIORITY_UNDEFINED)
config->default_settings.priority = priority;
if (toggle >= 0)
config->default_settings.toggle = toggle;
}
void
config_add_mbox (struct config *config, char *filename)
{
struct mbox **p = &config->mailboxes;
while (*p) {
if (0 == strcmp ((*p)->filename, filename)) {
return;
}
p = &((*p)->next);
}
*p = mbox_new (config, filename);
}
int
config_update_mbox (struct config *config, char *filename, int color, int flash, int ignore, int priority)
{
struct mbox **p = &config->mailboxes;
while (*p) {
if (0 == strcmp ((*p)->filename, filename)) {
if (color >= 0)
(*p)->color = color;
if (flash >= 0)
(*p)->flash = flash;
if (ignore >= 0)
(*p)->ignore = ignore;
if (priority != PRIORITY_UNDEFINED)
(*p)->priority = priority;
return 0;
}
p = &((*p)->next);
}
return -1;
}
void
config_add_signal (struct config *config, int signal)
{
struct signal **p = &config->signals;
while (*p) {
if ((*p)->signal == signal) {
return;
}
p = &((*p)->next);
}
*p = signal_new (config, signal);
}
int
config_update_signal (struct config *config, int signal, int color, int ignore, int toggle)
{
struct signal **p = &config->signals;
while (*p) {
if ((*p)->signal == signal) {
if (color >= 0)
(*p)->color = color;
if (ignore >= 0)
(*p)->ignore = ignore;
if (toggle >= 0)
(*p)->toggle = toggle;
return 0;
}
p = &((*p)->next);
}
return -1;
}
void
config_register (struct config *config, int kq)
{
struct mbox *mbox = config->mailboxes;
while (mbox) {
if (!mbox->ignore)
mbox_register (mbox, kq);
mbox = mbox->next;
}
struct signal *signal = config->signals;
while (signal) {
signal_register (signal, kq);
signal = signal->next;
}
}
void
config_unregister (struct config *config, int kq)
{
struct mbox *mbox = config->mailboxes;
while (mbox) {
if (mbox_unregister (mbox, kq) < 0)
syslog (LOG_WARNING, "Cannot unregister mailbox");
mbox = mbox->next;
}
struct signal *signal = config->signals;
while (signal) {
signal_unregister (signal, kq);
signal = signal->next;
}
}
void
config_free (struct config *config)
{
struct mbox *mbox = config->mailboxes, *mbox_o;
while (mbox) {
mbox_o = mbox;
mbox = mbox->next;
mbox_free (mbox_o);
}
struct signal *signal = config->signals, *signal_o;
while (signal) {
signal_o = signal;
signal = signal->next;
signal_free (signal_o);
}
free (config_filename);
config_filename = NULL;
free (config);
}