-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_notify.c
273 lines (233 loc) · 6.81 KB
/
cli_notify.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright (c) 2008, 2011 Pelle Johansson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <string.h>
#include <sys/queue.h>
#include <sys/uio.h>
#include <unistd.h>
#include <event.h>
#include <syslog.h>
#include "cli.h"
#include "base64.h"
#include "complete.h"
typedef void (*notify_cb_t)(int fd, const char *name, const char *code, const char *arg, size_t len);
struct notify_data
{
TAILQ_ENTRY(notify_data) link;
const char *name;
const char *code;
notify_cb_t cb;
int once;
};
TAILQ_HEAD(, notify_data) notifies = TAILQ_HEAD_INITIALIZER(notifies);
static void
start_notify(int fd, const char *name, const char *code, notify_cb_t cb, int once) {
struct notify_data *data = malloc(sizeof (*data));
struct iovec vecs[3];
if (!data)
err(1, "malloc");
data->name = name;
data->code = code;
data->cb = cb;
data->once = once;
TAILQ_INSERT_TAIL(¬ifies, data, link);
vecs[0].iov_base = (void*)"STRT";
vecs[0].iov_len = 4;
vecs[1].iov_base = (void*)code;
vecs[1].iov_len = 4;
vecs[2].iov_base = (void*)"\n";
vecs[2].iov_len = 1;
if (writev(fd, vecs, 3) == -1)
err(1, "writev");
}
static void
stop_notify(int fd, struct notify_data *data) {
struct notify_data *d;
TAILQ_REMOVE(¬ifies, data, link);
TAILQ_FOREACH(d, ¬ifies, link) {
if (strcmp(d->code, data->code) == 0)
break;
}
if (!d) {
struct iovec vecs[3];
vecs[0].iov_base = (void*)"STOP";
vecs[0].iov_len = 4;
vecs[1].iov_base = (void*)data->code;
vecs[1].iov_len = 4;
vecs[2].iov_base = (void*)"\n";
vecs[2].iov_len = 1;
if (writev(fd, vecs, 3) == -1)
err(1, "writev");
}
free(data);
}
#define ESTART(type) \
void notify_ ## type ## _cb (int fd, const char *n, const char *code, const char *val, size_t len) { \
if (len != 4) \
return; \
\
int v = debase64_int24(val); \
switch (v) {
#define EV(type, name, val) \
case val: \
printf("%s " #name "\n", n); \
fflush(stdout); \
break;
#define EEND(type) \
default: \
printf("%s unknown:0x%x\n", n, v); \
fflush(stdout); \
} \
}
#include "status_enums.h"
#undef ESTART
#undef EV
#undef EEND
void
notify_int_cb (int fd, const char *n, const char *code, const char *val, size_t len) {
if (len != 4)
return;
printf ("%s %d\n", n, debase64_int24(val));
fflush(stdout);
}
void
notify_string_cb (int fd, const char *n, const char *code, const char *val, size_t len) {
printf ("%s %.*s\n", n, (int)len, val);
fflush(stdout);
}
struct notify_code {
const char *name;
const char *code;
notify_cb_t cb;
} notify_codes[] = {
#define NOTIFY(name, code, type) { #name, code, notify_ ## type ## _cb },
#define STATUS(name, code, type) { #name, code, notify_ ## type ## _cb },
#include "all_notify.h"
#undef NOTIFY
#undef STATUS
{ NULL }
};
static int
filter_notifies (int fd, struct complete_candidate **cands) {
struct complete_candidate *cand, **pcand;
struct evbuffer *buf = evbuffer_new();
char *line;
evbuffer_add(buf, "QSTS", 4);
for (cand = *cands ; cand ; cand = cand->next)
evbuffer_add(buf, ((struct notify_code*)cand->aux)->code, 4);
//syslog(LOG_DEBUG, "QSTS fd: %d query: %.*s", fd, (int)EVBUFFER_LENGTH(buf), EVBUFFER_DATA(buf));
evbuffer_add(buf, "\n", 1);
write (fd, EVBUFFER_DATA(buf), EVBUFFER_LENGTH(buf));
evbuffer_drain(buf, EVBUFFER_LENGTH(buf));
while (!(line = evbuffer_readline(buf)))
evbuffer_read(buf, fd, 1024);
if (strncmp(line, "EDIS", 4) == 0)
return 1;
if (strncmp(line, "QSTS", 4) != 0)
errx(1, "Unexpected reply, not QSTS: %s", line);
line += 4;
/* Reply should be in same order as query. */
pcand = cands;
while ((cand = *pcand)) {
if (strncmp(((struct notify_code*)cand->aux)->code, line, 4) == 0) {
pcand = &cand->next;
line += 4;
} else {
*pcand = cand->next;
free(cand);
}
}
return 0;
}
int
cli_notify (int fd, int argc, char *argv[], int once) {
int num = 0;
struct evbuffer *data;
struct complete_candidate *candidates = NULL, *cand;
struct notify_code *nc;
int argi = 0;
do {
candidates = NULL;
for (nc = notify_codes ; nc->name ; nc++) {
cand = malloc (sizeof (*cand));
if (!cand)
err (1, "malloc");
cand->name = nc->name;
cand->name_off = 0;
cand->next = candidates;
cand->aux = nc;
candidates = cand;
}
//syslog(LOG_DEBUG, "complete at %d '%s'", argi, argv[argi]);
argi += complete(&candidates, argc - argi, (const char**)argv + argi, (void(*)(struct complete_candidate*))free);
//syslog(LOG_DEBUG, "after complete at %d '%s'", argi, argv[argi]);
if (candidates) {
if (filter_notifies(fd, &candidates))
errx(1, "server disabled");
}
if (!candidates)
errx (1, "No matching notification");
if (candidates->next) {
fprintf (stderr, "Multiple matches:\n");
for (cand = candidates; cand; cand = cand->next) {
fprintf (stderr, "%s\n", cand->name);
}
return 1;
}
nc = candidates->aux;
start_notify(fd, nc->name, nc->code, nc->cb, once);
num++;
free(candidates);
} while (argi < argc);
data = evbuffer_new();
while (!TAILQ_EMPTY(¬ifies)) {
int res = evbuffer_read(data, fd, 1024);
char *line;
if (res < 0)
err (1, "evbuffer_read");
if (res == 0)
errx (1, "EOF");
while ((line = evbuffer_readline(data))) {
int len = strlen(line);
if (len >= 8 && strncmp(line, "STAT", 4) == 0) {
const char *l = line + 4;
const char *v = line + 8;
struct notify_data *d;
TAILQ_FOREACH(d, ¬ifies, link) {
if (strncmp(l, d->code, 4) == 0) {
d->cb(fd, d->name, d->code, v, len - 8);
if (d->once) {
stop_notify(fd, d);
break; /* XXX multiple with same code? */
}
}
}
}
}
}
return 0;
}