Blame
Date:
Sat Feb 19 11:04:46 2022 UTC
Message:
drop AMUSED_VERSION and the undocumented -V flag
001
2022-02-16
op
/*
002
2022-02-16
op
* Copyright (c) 2022 Omar Polo <op@openbsd.org>
003
2022-02-16
op
*
004
2022-02-16
op
* Permission to use, copy, modify, and distribute this software for any
005
2022-02-16
op
* purpose with or without fee is hereby granted, provided that the above
006
2022-02-16
op
* copyright notice and this permission notice appear in all copies.
007
2022-02-16
op
*
008
2022-02-16
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
009
2022-02-16
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
010
2022-02-16
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
011
2022-02-16
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
012
2022-02-16
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
013
2022-02-16
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
014
2022-02-16
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
015
2022-02-16
op
*/
016
2022-02-16
op
017
2022-02-16
op
#include <sys/types.h>
018
2022-02-16
op
#include <sys/socket.h>
019
2022-02-16
op
#include <sys/queue.h>
020
2022-02-16
op
#include <sys/uio.h>
021
2022-02-16
op
#include <sys/un.h>
022
2022-02-16
op
023
2022-02-16
op
#include <errno.h>
024
2022-02-16
op
#include <event.h>
025
2022-02-16
op
#include <limits.h>
026
2022-02-16
op
#include <sndio.h>
027
2022-02-16
op
#include <stdio.h>
028
2022-02-16
op
#include <stdlib.h>
029
2022-02-16
op
#include <stdint.h>
030
2022-02-16
op
#include <string.h>
031
2022-02-16
op
#include <syslog.h>
032
2022-02-16
op
#include <unistd.h>
033
2022-02-16
op
#include <imsg.h>
034
2022-02-16
op
035
2022-02-16
op
#include "amused.h"
036
2022-02-16
op
#include "log.h"
037
2022-02-16
op
#include "playlist.h"
038
2022-02-16
op
#include "xmalloc.h"
039
2022-02-16
op
040
2022-02-16
op
static struct imsgbuf *ibuf;
041
2022-02-16
op
042
2022-02-16
op
int ctl_noarg(struct parse_result *, int, char **);
043
2022-02-16
op
int ctl_add(struct parse_result *, int, char **);
044
2022-02-17
op
int ctl_show(struct parse_result *, int, char **);
045
2022-02-17
op
int ctl_load(struct parse_result *, int, char **);
046
2022-02-17
op
int ctl_jump(struct parse_result *, int, char **);
047
2022-02-19
op
int ctl_repeat(struct parse_result *, int, char **);
048
2022-02-16
op
049
2022-02-16
op
struct ctl_command ctl_commands[] = {
050
2022-02-16
op
{ "play", PLAY, ctl_noarg, "" },
051
2022-02-16
op
{ "pause", PAUSE, ctl_noarg, "" },
052
2022-02-16
op
{ "toggle", TOGGLE, ctl_noarg, "" },
053
2022-02-16
op
{ "stop", STOP, ctl_noarg, "" },
054
2022-02-16
op
{ "restart", RESTART, ctl_noarg, "" },
055
2022-02-16
op
{ "add", ADD, ctl_add, "files...", 1 },
056
2022-02-16
op
{ "flush", FLUSH, ctl_noarg, "" },
057
2022-02-19
op
{ "show", SHOW, ctl_show, "[-p]" },
058
2022-02-16
op
{ "status", STATUS, ctl_noarg, "" },
059
2022-02-17
op
{ "next", NEXT, ctl_noarg, "" },
060
2022-02-17
op
{ "prev", PREV, ctl_noarg, "" },
061
2022-02-17
op
{ "load", LOAD, ctl_load, "[file]", 1 },
062
2022-02-17
op
{ "jump", JUMP, ctl_jump, "pattern" },
063
2022-02-19
op
{ "repeat", REPEAT, ctl_repeat, "one|all on|off" },
064
2022-02-16
op
{ NULL },
065
2022-02-16
op
};
066
2022-02-16
op
067
2022-02-16
op
__dead void
068
2022-02-16
op
usage(void)
069
2022-02-16
op
{
070
2022-02-16
op
fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
071
2022-02-16
op
exit(1);
072
2022-02-16
op
}
073
2022-02-16
op
074
2022-02-16
op
static __dead void
075
2022-02-16
op
ctl_usage(struct ctl_command *ctl)
076
2022-02-16
op
{
077
2022-02-16
op
fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
078
2022-02-16
op
ctl->name, ctl->usage);
079
2022-02-16
op
exit(1);
080
2022-02-16
op
}
081
2022-02-16
op
082
2022-02-16
op
static int
083
2022-02-16
op
parse(int argc, char **argv)
084
2022-02-16
op
{
085
2022-02-16
op
struct ctl_command *ctl = NULL;
086
2022-02-16
op
struct parse_result res;
087
2022-02-16
op
int i, status;
088
2022-02-16
op
089
2022-02-16
op
memset(&res, 0, sizeof(res));
090
2022-02-16
op
091
2022-02-16
op
for (i = 0; ctl_commands[i].name != NULL; ++i) {
092
2022-02-16
op
if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
093
2022-02-16
op
== 0) {
094
2022-02-16
op
if (ctl != NULL) {
095
2022-02-16
op
fprintf(stderr,
096
2022-02-16
op
"ambiguous argument: %s\n", argv[0]);
097
2022-02-16
op
usage();
098
2022-02-16
op
}
099
2022-02-16
op
ctl = &ctl_commands[i];
100
2022-02-16
op
}
101
2022-02-16
op
}
102
2022-02-16
op
103
2022-02-16
op
if (ctl == NULL) {
104
2022-02-16
op
fprintf(stderr, "unknown argument: %s\n", argv[0]);
105
2022-02-16
op
usage();
106
2022-02-16
op
}
107
2022-02-16
op
108
2022-02-16
op
res.action = ctl->action;
109
2022-02-16
op
res.ctl = ctl;
110
2022-02-16
op
111
2022-02-16
op
if (!ctl->has_pledge) {
112
2022-02-16
op
/* pledge(2) default if command doesn't have its own */
113
2022-02-16
op
if (pledge("stdio", NULL) == -1)
114
2022-02-16
op
fatal("pledge");
115
2022-02-16
op
}
116
2022-02-16
op
117
2022-02-16
op
status = ctl->main(&res, argc, argv);
118
2022-02-16
op
close(ibuf->fd);
119
2022-02-16
op
free(ibuf);
120
2022-02-16
op
return status;
121
2022-02-16
op
}
122
2022-02-16
op
123
2022-02-16
op
static int
124
2022-02-16
op
enqueue_tracks(char **files)
125
2022-02-16
op
{
126
2022-02-16
op
char res[PATH_MAX];
127
2022-02-16
op
int enq = 0;
128
2022-02-16
op
129
2022-02-16
op
for (; *files != NULL; ++files) {
130
2022-02-16
op
memset(&res, 0, sizeof(res));
131
2022-02-16
op
if (realpath(*files, res) == NULL) {
132
2022-02-16
op
log_warn("realpath %s", *files);
133
2022-02-16
op
continue;
134
2022-02-16
op
}
135
2022-02-16
op
136
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
137
2022-02-16
op
res, sizeof(res));
138
2022-02-16
op
enq++;
139
2022-02-16
op
}
140
2022-02-16
op
141
2022-02-16
op
return enq == 0;
142
2022-02-17
op
}
143
2022-02-17
op
144
2022-02-17
op
static int
145
2022-02-17
op
jump_req(const char *arg)
146
2022-02-17
op
{
147
2022-02-17
op
char path[PATH_MAX];
148
2022-02-17
op
149
2022-02-17
op
memset(path, 0, sizeof(path));
150
2022-02-17
op
strlcpy(path, arg, sizeof(path));
151
2022-02-17
op
imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1, path, sizeof(path));
152
2022-02-17
op
return 0;
153
2022-02-16
op
}
154
2022-02-16
op
155
2022-02-16
op
static void
156
2022-02-16
op
print_error_message(const char *prfx, struct imsg *imsg)
157
2022-02-16
op
{
158
2022-02-16
op
size_t datalen;
159
2022-02-16
op
char *msg;
160
2022-02-16
op
161
2022-02-16
op
datalen = IMSG_DATA_SIZE(*imsg);
162
2022-02-16
op
if ((msg = calloc(1, datalen)) == NULL)
163
2022-02-16
op
fatal("calloc %zu", datalen);
164
2022-02-16
op
memcpy(msg, imsg->data, datalen);
165
2022-02-16
op
if (datalen == 0 || msg[datalen-1] != '\0')
166
2022-02-16
op
fatalx("malformed error message");
167
2022-02-16
op
168
2022-02-16
op
log_warnx("%s: %s", prfx, msg);
169
2022-02-16
op
free(msg);
170
2022-02-16
op
}
171
2022-02-16
op
172
2022-02-16
op
static int
173
2022-02-16
op
show_add(struct imsg *imsg, int *ret, char ***files)
174
2022-02-16
op
{
175
2022-02-16
op
if (**files == NULL) {
176
2022-02-16
op
log_warnx("received more replies than file sent");
177
2022-02-16
op
*ret = 1;
178
2022-02-16
op
return 1;
179
2022-02-16
op
}
180
2022-02-16
op
181
2022-02-16
op
if (imsg->hdr.type == IMSG_CTL_ERR)
182
2022-02-16
op
print_error_message(**files, imsg);
183
2022-02-16
op
else if (imsg->hdr.type == IMSG_CTL_ADD)
184
2022-02-16
op
log_debug("enqueued %s", **files);
185
2022-02-16
op
else
186
2022-02-16
op
fatalx("got invalid message %d", imsg->hdr.type);
187
2022-02-16
op
188
2022-02-16
op
(*files)++;
189
2022-02-16
op
return (**files) == NULL;
190
2022-02-16
op
}
191
2022-02-16
op
192
2022-02-16
op
static int
193
2022-02-17
op
show_complete(struct parse_result *res, struct imsg *imsg, int *ret)
194
2022-02-16
op
{
195
2022-02-17
op
struct player_status s;
196
2022-02-16
op
size_t datalen;
197
2022-02-16
op
198
2022-02-16
op
if (imsg->hdr.type == IMSG_CTL_ERR) {
199
2022-02-16
op
print_error_message("show failed", imsg);
200
2022-02-16
op
*ret = 1;
201
2022-02-16
op
return 1;
202
2022-02-16
op
}
203
2022-02-16
op
204
2022-02-16
op
datalen = IMSG_DATA_SIZE(*imsg);
205
2022-02-16
op
if (datalen == 0)
206
2022-02-16
op
return 1;
207
2022-02-16
op
208
2022-02-17
op
if (datalen != sizeof(s))
209
2022-02-16
op
fatalx("%s: data size mismatch", __func__);
210
2022-02-17
op
memcpy(&s, imsg->data, sizeof(s));
211
2022-02-17
op
if (s.path[sizeof(s.path)-1] != '\0')
212
2022-02-16
op
fatalx("%s: data corrupted?", __func__);
213
2022-02-16
op
214
2022-02-17
op
if (res->pretty)
215
2022-02-17
op
printf("%c ", s.status == STATE_PLAYING ? '>' : ' ');
216
2022-02-17
op
printf("%s\n", s.path);
217
2022-02-16
op
return 0;
218
2022-02-16
op
}
219
2022-02-16
op
220
2022-02-16
op
static int
221
2022-02-16
op
show_status(struct imsg *imsg, int *ret)
222
2022-02-16
op
{
223
2022-02-16
op
struct player_status s;
224
2022-02-16
op
size_t datalen;
225
2022-02-16
op
226
2022-02-16
op
if (imsg->hdr.type == IMSG_CTL_ERR) {
227
2022-02-16
op
print_error_message("show failed", imsg);
228
2022-02-16
op
*ret = 1;
229
2022-02-16
op
return 1;
230
2022-02-16
op
}
231
2022-02-16
op
232
2022-02-16
op
if (imsg->hdr.type != IMSG_CTL_STATUS)
233
2022-02-16
op
fatalx("%s: got wrong reply", __func__);
234
2022-02-16
op
235
2022-02-16
op
datalen = IMSG_DATA_SIZE(*imsg);
236
2022-02-16
op
if (datalen != sizeof(s))
237
2022-02-16
op
fatalx("%s: data size mismatch", __func__);
238
2022-02-16
op
memcpy(&s, imsg->data, sizeof(s));
239
2022-02-16
op
if (s.path[sizeof(s.path)-1] != '\0')
240
2022-02-16
op
fatalx("%s: data corrupted?", __func__);
241
2022-02-16
op
242
2022-02-16
op
switch (s.status) {
243
2022-02-16
op
case STATE_STOPPED:
244
2022-02-16
op
printf("stopped ");
245
2022-02-16
op
break;
246
2022-02-16
op
case STATE_PLAYING:
247
2022-02-16
op
printf("playing ");
248
2022-02-16
op
break;
249
2022-02-16
op
case STATE_PAUSED:
250
2022-02-16
op
printf("paused ");
251
2022-02-16
op
break;
252
2022-02-16
op
default:
253
2022-02-16
op
printf("unknown ");
254
2022-02-16
op
break;
255
2022-02-16
op
}
256
2022-02-16
op
257
2022-02-16
op
printf("%s\n", s.path);
258
2022-02-19
op
printf("repeat one %s -- repeat all %s\n",
259
2022-02-19
op
s.rp.repeat_one ? "on" : "off",
260
2022-02-19
op
s.rp.repeat_all ? "on" : "off");
261
2022-02-16
op
return 1;
262
2022-02-17
op
}
263
2022-02-17
op
264
2022-02-17
op
static int
265
2022-02-17
op
show_load(struct parse_result *res, struct imsg *imsg, int *ret)
266
2022-02-17
op
{
267
2022-02-17
op
FILE *f;
268
2022-02-17
op
const char *file;
269
2022-02-17
op
char *line = NULL;
270
2022-02-17
op
char path[PATH_MAX];
271
2022-02-17
op
size_t linesize = 0;
272
2022-02-17
op
ssize_t linelen;
273
2022-02-17
op
int any = 0;
274
2022-02-17
op
275
2022-02-17
op
if (imsg->hdr.type == IMSG_CTL_ERR) {
276
2022-02-17
op
print_error_message("load failed", imsg);
277
2022-02-17
op
*ret = 1;
278
2022-02-17
op
return 1;
279
2022-02-17
op
}
280
2022-02-17
op
281
2022-02-17
op
if (imsg->hdr.type == IMSG_CTL_ADD)
282
2022-02-17
op
return 0;
283
2022-02-17
op
284
2022-02-17
op
if (imsg->hdr.type == IMSG_CTL_COMMIT)
285
2022-02-17
op
return 1;
286
2022-02-17
op
287
2022-02-17
op
if (imsg->hdr.type != IMSG_CTL_BEGIN)
288
2022-02-17
op
fatalx("got unexpected message %d", imsg->hdr.type);
289
2022-02-17
op
290
2022-02-17
op
if (res->file == NULL)
291
2022-02-17
op
f = stdin;
292
2022-02-17
op
else if ((f = fopen(res->file, "r")) == NULL) {
293
2022-02-17
op
log_warn("can't open %s", res->file);
294
2022-02-17
op
*ret = 1;
295
2022-02-17
op
return 1;
296
2022-02-17
op
}
297
2022-02-17
op
298
2022-02-17
op
while ((linelen = getline(&line, &linesize, f)) != -1) {
299
2022-02-17
op
if (linelen == 0)
300
2022-02-17
op
continue;
301
2022-02-17
op
line[linelen-1] = '\0';
302
2022-02-17
op
file = line;
303
2022-02-17
op
if (file[0] == '>' && file[1] == ' ')
304
2022-02-17
op
file += 2;
305
2022-02-17
op
if (file[0] == ' ' && file[1] == ' ')
306
2022-02-17
op
file += 2;
307
2022-02-17
op
308
2022-02-17
op
memset(&path, 0, sizeof(path));
309
2022-02-17
op
if (realpath(file, path) == NULL) {
310
2022-02-17
op
log_warn("realpath %s", file);
311
2022-02-17
op
continue;
312
2022-02-17
op
}
313
2022-02-17
op
314
2022-02-17
op
any++;
315
2022-02-17
op
imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
316
2022-02-17
op
path, sizeof(path));
317
2022-02-17
op
}
318
2022-02-17
op
319
2022-02-17
op
free(line);
320
2022-02-17
op
if (ferror(f))
321
2022-02-17
op
fatal("getline");
322
2022-02-17
op
fclose(f);
323
2022-02-17
op
324
2022-02-17
op
if (!any) {
325
2022-02-17
op
*ret = 1;
326
2022-02-17
op
return 1;
327
2022-02-17
op
}
328
2022-02-17
op
329
2022-02-17
op
imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
330
2022-02-17
op
NULL, 0);
331
2022-02-17
op
imsg_flush(ibuf);
332
2022-02-17
op
return 0;
333
2022-02-16
op
}
334
2022-02-16
op
335
2022-02-16
op
static int
336
2022-02-16
op
ctlaction(struct parse_result *res)
337
2022-02-16
op
{
338
2022-02-16
op
struct imsg imsg;
339
2022-02-16
op
ssize_t n;
340
2022-02-16
op
int ret = 0, done = 1;
341
2022-02-16
op
char **files;
342
2022-02-16
op
343
2022-02-16
op
switch (res->action) {
344
2022-02-16
op
case PLAY:
345
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
346
2022-02-19
op
if (verbose) {
347
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
348
2022-02-19
op
NULL, 0);
349
2022-02-19
op
done = 0;
350
2022-02-19
op
}
351
2022-02-16
op
break;
352
2022-02-16
op
case PAUSE:
353
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
354
2022-02-16
op
break;
355
2022-02-16
op
case TOGGLE:
356
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
357
2022-02-19
op
if (verbose) {
358
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
359
2022-02-19
op
NULL, 0);
360
2022-02-19
op
done = 0;
361
2022-02-19
op
}
362
2022-02-16
op
break;
363
2022-02-16
op
case STOP:
364
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
365
2022-02-16
op
break;
366
2022-02-16
op
case RESTART:
367
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
368
2022-02-19
op
if (verbose) {
369
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
370
2022-02-19
op
NULL, 0);
371
2022-02-19
op
done = 0;
372
2022-02-19
op
}
373
2022-02-16
op
break;
374
2022-02-16
op
case ADD:
375
2022-02-16
op
done = 0;
376
2022-02-16
op
files = res->files;
377
2022-02-16
op
ret = enqueue_tracks(res->files);
378
2022-02-16
op
break;
379
2022-02-16
op
case FLUSH:
380
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
381
2022-02-16
op
break;
382
2022-02-16
op
case SHOW:
383
2022-02-16
op
done = 0;
384
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
385
2022-02-16
op
break;
386
2022-02-16
op
case STATUS:
387
2022-02-16
op
done = 0;
388
2022-02-16
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
389
2022-02-17
op
break;
390
2022-02-17
op
case NEXT:
391
2022-02-17
op
imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
392
2022-02-19
op
if (verbose) {
393
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
394
2022-02-19
op
NULL, 0);
395
2022-02-19
op
done = 0;
396
2022-02-19
op
}
397
2022-02-16
op
break;
398
2022-02-17
op
case PREV:
399
2022-02-17
op
imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
400
2022-02-19
op
if (verbose) {
401
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
402
2022-02-19
op
NULL, 0);
403
2022-02-19
op
done = 0;
404
2022-02-19
op
}
405
2022-02-17
op
break;
406
2022-02-17
op
case LOAD:
407
2022-02-17
op
done = 0;
408
2022-02-17
op
imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
409
2022-02-17
op
break;
410
2022-02-17
op
case JUMP:
411
2022-02-17
op
done = 0;
412
2022-02-17
op
ret = jump_req(res->file);
413
2022-02-17
op
break;
414
2022-02-19
op
case REPEAT:
415
2022-02-19
op
imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
416
2022-02-19
op
&res->rep, sizeof(res->rep));
417
2022-02-19
op
break;
418
2022-02-16
op
case NONE:
419
2022-02-16
op
/* action not expected */
420
2022-02-16
op
fatalx("invalid action %u", res->action);
421
2022-02-16
op
break;
422
2022-02-16
op
}
423
2022-02-16
op
424
2022-02-16
op
if (ret != 0)
425
2022-02-16
op
goto end;
426
2022-02-16
op
427
2022-02-16
op
imsg_flush(ibuf);
428
2022-02-16
op
429
2022-02-16
op
while (!done) {
430
2022-02-16
op
if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
431
2022-02-16
op
fatalx("imsg_read error");
432
2022-02-16
op
if (n == 0)
433
2022-02-16
op
fatalx("pipe closed");
434
2022-02-16
op
435
2022-02-16
op
while (!done) {
436
2022-02-16
op
if ((n = imsg_get(ibuf, &imsg)) == -1)
437
2022-02-16
op
fatalx("imsg_get error");
438
2022-02-16
op
if (n == 0)
439
2022-02-16
op
break;
440
2022-02-16
op
441
2022-02-16
op
switch (res->action) {
442
2022-02-16
op
case ADD:
443
2022-02-16
op
done = show_add(&imsg, &ret, &files);
444
2022-02-16
op
break;
445
2022-02-16
op
case SHOW:
446
2022-02-17
op
done = show_complete(res, &imsg, &ret);
447
2022-02-16
op
break;
448
2022-02-17
op
case PLAY:
449
2022-02-17
op
case TOGGLE:
450
2022-02-17
op
case RESTART:
451
2022-02-16
op
case STATUS:
452
2022-02-17
op
case NEXT:
453
2022-02-17
op
case PREV:
454
2022-02-17
op
case JUMP:
455
2022-02-16
op
done = show_status(&imsg, &ret);
456
2022-02-16
op
break;
457
2022-02-17
op
case LOAD:
458
2022-02-17
op
done = show_load(res, &imsg, &ret);
459
2022-02-17
op
break;
460
2022-02-16
op
default:
461
2022-02-16
op
done = 1;
462
2022-02-16
op
break;
463
2022-02-16
op
}
464
2022-02-16
op
465
2022-02-16
op
imsg_free(&imsg);
466
2022-02-16
op
}
467
2022-02-16
op
}
468
2022-02-16
op
469
2022-02-16
op
end:
470
2022-02-16
op
return ret;
471
2022-02-16
op
}
472
2022-02-16
op
473
2022-02-16
op
int
474
2022-02-16
op
ctl_noarg(struct parse_result *res, int argc, char **argv)
475
2022-02-16
op
{
476
2022-02-16
op
if (argc != 1)
477
2022-02-16
op
ctl_usage(res->ctl);
478
2022-02-16
op
return ctlaction(res);
479
2022-02-16
op
}
480
2022-02-16
op
481
2022-02-16
op
int
482
2022-02-16
op
ctl_add(struct parse_result *res, int argc, char **argv)
483
2022-02-16
op
{
484
2022-02-16
op
if (argc < 2)
485
2022-02-16
op
ctl_usage(res->ctl);
486
2022-02-16
op
res->files = argv+1;
487
2022-02-17
op
488
2022-02-17
op
if (pledge("stdio rpath", NULL) == -1)
489
2022-02-17
op
fatal("pledge");
490
2022-02-17
op
491
2022-02-16
op
return ctlaction(res);
492
2022-02-16
op
}
493
2022-02-17
op
494
2022-02-17
op
int
495
2022-02-17
op
ctl_show(struct parse_result *res, int argc, char **argv)
496
2022-02-17
op
{
497
2022-02-17
op
int ch;
498
2022-02-17
op
499
2022-02-17
op
while ((ch = getopt(argc, argv, "p")) != -1) {
500
2022-02-17
op
switch (ch) {
501
2022-02-17
op
case 'p':
502
2022-02-17
op
res->pretty = 1;
503
2022-02-17
op
break;
504
2022-02-17
op
default:
505
2022-02-17
op
ctl_usage(res->ctl);
506
2022-02-17
op
}
507
2022-02-17
op
}
508
2022-02-17
op
509
2022-02-17
op
return ctlaction(res);
510
2022-02-17
op
}
511
2022-02-17
op
512
2022-02-17
op
int
513
2022-02-17
op
ctl_load(struct parse_result *res, int argc, char **argv)
514
2022-02-17
op
{
515
2022-02-17
op
if (argc < 2)
516
2022-02-17
op
res->file = NULL;
517
2022-02-17
op
else if (argc == 2)
518
2022-02-17
op
res->file = argv[1];
519
2022-02-17
op
else
520
2022-02-17
op
ctl_usage(res->ctl);
521
2022-02-17
op
522
2022-02-17
op
if (pledge("stdio rpath", NULL) == -1)
523
2022-02-17
op
fatal("pledge");
524
2022-02-17
op
525
2022-02-17
op
return ctlaction(res);
526
2022-02-17
op
}
527
2022-02-17
op
528
2022-02-17
op
int
529
2022-02-17
op
ctl_jump(struct parse_result *res, int argc, char **argv)
530
2022-02-17
op
{
531
2022-02-17
op
int ch;
532
2022-02-17
op
533
2022-02-17
op
while ((ch = getopt(argc, argv, "")) != -1)
534
2022-02-17
op
ctl_usage(res->ctl);
535
2022-02-17
op
argc -= optind;
536
2022-02-17
op
argv += optind;
537
2022-02-17
op
538
2022-02-17
op
if (argc != 1)
539
2022-02-17
op
ctl_usage(res->ctl);
540
2022-02-17
op
541
2022-02-17
op
res->file = argv[0];
542
2022-02-19
op
return ctlaction(res);
543
2022-02-19
op
}
544
2022-02-19
op
545
2022-02-19
op
int
546
2022-02-19
op
ctl_repeat(struct parse_result *res, int argc, char **argv)
547
2022-02-19
op
{
548
2022-02-19
op
int ch, b;
549
2022-02-19
op
550
2022-02-19
op
while ((ch = getopt(argc, argv, "")) != -1)
551
2022-02-19
op
ctl_usage(res->ctl);
552
2022-02-19
op
argc -= optind;
553
2022-02-19
op
argv += optind;
554
2022-02-19
op
555
2022-02-19
op
if (argc != 2)
556
2022-02-19
op
ctl_usage(res->ctl);
557
2022-02-19
op
558
2022-02-19
op
if (!strcmp(argv[1], "on"))
559
2022-02-19
op
b = 1;
560
2022-02-19
op
else if (!strcmp(argv[1], "off"))
561
2022-02-19
op
b = 0;
562
2022-02-19
op
else
563
2022-02-19
op
ctl_usage(res->ctl);
564
2022-02-19
op
565
2022-02-19
op
res->rep.repeat_one = -1;
566
2022-02-19
op
res->rep.repeat_all = -1;
567
2022-02-19
op
if (!strcmp(argv[0], "one"))
568
2022-02-19
op
res->rep.repeat_one = b;
569
2022-02-19
op
else if (!strcmp(argv[0], "all"))
570
2022-02-19
op
res->rep.repeat_all = b;
571
2022-02-19
op
else
572
2022-02-19
op
ctl_usage(res->ctl);
573
2022-02-19
op
574
2022-02-17
op
return ctlaction(res);
575
2022-02-17
op
}
576
2022-02-17
op
577
2022-02-16
op
static int
578
2022-02-16
op
sockconn(void)
579
2022-02-16
op
{
580
2022-02-16
op
struct sockaddr_un sun;
581
2022-02-16
op
int sock, saved_errno;
582
2022-02-16
op
583
2022-02-16
op
if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
584
2022-02-16
op
fatal("socket");
585
2022-02-16
op
586
2022-02-16
op
memset(&sun, 0, sizeof(sun));
587
2022-02-16
op
sun.sun_family = AF_UNIX;
588
2022-02-16
op
strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
589
2022-02-16
op
if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
590
2022-02-16
op
saved_errno = errno;
591
2022-02-16
op
close(sock);
592
2022-02-16
op
errno = saved_errno;
593
2022-02-16
op
return -1;
594
2022-02-16
op
}
595
2022-02-16
op
596
2022-02-16
op
return sock;
597
2022-02-16
op
}
598
2022-02-16
op
599
2022-02-16
op
__dead void
600
2022-02-16
op
ctl(int argc, char **argv)
601
2022-02-16
op
{
602
2022-02-16
op
int ctl_sock, i = 0;
603
2022-02-16
op
604
2022-02-16
op
log_init(1, LOG_DAEMON);
605
2022-02-16
op
log_setverbose(verbose);
606
2022-02-16
op
607
2022-02-16
op
do {
608
2022-02-16
op
struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
609
2022-02-16
op
610
2022-02-16
op
if ((ctl_sock = sockconn()) != -1)
611
2022-02-16
op
break;
612
2022-02-16
op
if (errno != ENOENT && errno != ECONNREFUSED)
613
2022-02-16
op
fatal("connect %s", csock);
614
2022-02-16
op
615
2022-02-16
op
if (i == 0)
616
2022-02-16
op
spawn_daemon();
617
2022-02-16
op
618
2022-02-16
op
nanosleep(&ts, NULL);
619
2022-02-16
op
} while (++i < 20);
620
2022-02-16
op
621
2022-02-16
op
if (ctl_sock == -1)
622
2022-02-16
op
fatalx("failed to connect to the daemon");
623
2022-02-16
op
624
2022-02-16
op
ibuf = xmalloc(sizeof(*ibuf));
625
2022-02-16
op
imsg_init(ibuf, ctl_sock);
626
2022-02-16
op
627
2022-02-16
op
optreset = 1;
628
2022-02-16
op
optind = 1;
629
2022-02-16
op
630
2022-02-16
op
exit(parse(argc, argv));
631
2022-02-16
op
}
Omar Polo