Blame


1 3baa2617 2022-02-16 op /*
2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 96233476 2022-06-09 op * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
4 3baa2617 2022-02-16 op *
5 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
6 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
7 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
8 3baa2617 2022-02-16 op *
9 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 3baa2617 2022-02-16 op */
17 3baa2617 2022-02-16 op
18 f36fd90a 2022-07-09 op #include "config.h"
19 f36fd90a 2022-07-09 op
20 3baa2617 2022-02-16 op #include <sys/socket.h>
21 3baa2617 2022-02-16 op #include <sys/un.h>
22 3baa2617 2022-02-16 op
23 3baa2617 2022-02-16 op #include <errno.h>
24 1d673950 2022-03-03 op #include <fcntl.h>
25 3baa2617 2022-02-16 op #include <limits.h>
26 3baa2617 2022-02-16 op #include <stdio.h>
27 3baa2617 2022-02-16 op #include <stdlib.h>
28 3baa2617 2022-02-16 op #include <stdint.h>
29 3baa2617 2022-02-16 op #include <string.h>
30 3baa2617 2022-02-16 op #include <syslog.h>
31 3baa2617 2022-02-16 op #include <unistd.h>
32 3baa2617 2022-02-16 op
33 3baa2617 2022-02-16 op #include "amused.h"
34 3baa2617 2022-02-16 op #include "log.h"
35 bb3f279f 2022-02-16 op #include "playlist.h"
36 3baa2617 2022-02-16 op #include "xmalloc.h"
37 3baa2617 2022-02-16 op
38 96233476 2022-06-09 op static struct imsgbuf *ibuf;
39 96233476 2022-06-09 op char cwd[PATH_MAX];
40 3baa2617 2022-02-16 op
41 2ddcfa40 2022-07-08 op static int ctl_noarg(struct parse_result *, int, char **);
42 2ddcfa40 2022-07-08 op static int ctl_add(struct parse_result *, int, char **);
43 2ddcfa40 2022-07-08 op static int ctl_show(struct parse_result *, int, char **);
44 2ddcfa40 2022-07-08 op static int ctl_load(struct parse_result *, int, char **);
45 2ddcfa40 2022-07-08 op static int ctl_jump(struct parse_result *, int, char **);
46 2ddcfa40 2022-07-08 op static int ctl_repeat(struct parse_result *, int, char **);
47 9fb94242 2022-07-13 op static int ctl_consume(struct parse_result *, int, char **);
48 2ddcfa40 2022-07-08 op static int ctl_monitor(struct parse_result *, int, char **);
49 e5d4e9f7 2022-07-09 op static int ctl_seek(struct parse_result *, int, char **);
50 68e4f29e 2022-07-10 op static int ctl_status(struct parse_result *, int, char **);
51 3baa2617 2022-02-16 op
52 3baa2617 2022-02-16 op struct ctl_command ctl_commands[] = {
53 4b42d3a3 2022-07-13 op { "add", ADD, ctl_add, "files..."},
54 9fb94242 2022-07-13 op { "consume", MODE, ctl_consume, "one|all"},
55 4b42d3a3 2022-07-13 op { "flush", FLUSH, ctl_noarg, ""},
56 4b42d3a3 2022-07-13 op { "jump", JUMP, ctl_jump, "pattern"},
57 4b42d3a3 2022-07-13 op { "load", LOAD, ctl_load, "[file]"},
58 4b42d3a3 2022-07-13 op { "monitor", MONITOR, ctl_monitor, "[events]"},
59 4b42d3a3 2022-07-13 op { "next", NEXT, ctl_noarg, ""},
60 4b42d3a3 2022-07-13 op { "pause", PAUSE, ctl_noarg, ""},
61 4b42d3a3 2022-07-13 op { "play", PLAY, ctl_noarg, ""},
62 4b42d3a3 2022-07-13 op { "prev", PREV, ctl_noarg, ""},
63 9fb94242 2022-07-13 op { "repeat", MODE, ctl_repeat, "one|all on|off"},
64 4b42d3a3 2022-07-13 op { "restart", RESTART, ctl_noarg, ""},
65 4b42d3a3 2022-07-13 op { "seek", SEEK, ctl_seek, "[+-]time[%]"},
66 4b42d3a3 2022-07-13 op { "show", SHOW, ctl_show, "[-p]"},
67 4b42d3a3 2022-07-13 op { "status", STATUS, ctl_status, "[-f fmt]"},
68 4b42d3a3 2022-07-13 op { "stop", STOP, ctl_noarg, ""},
69 4b42d3a3 2022-07-13 op { "toggle", TOGGLE, ctl_noarg, ""},
70 4b42d3a3 2022-07-13 op { NULL, 0, NULL, NULL},
71 3baa2617 2022-02-16 op };
72 3baa2617 2022-02-16 op
73 3baa2617 2022-02-16 op __dead void
74 3baa2617 2022-02-16 op usage(void)
75 3baa2617 2022-02-16 op {
76 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
77 3baa2617 2022-02-16 op exit(1);
78 3baa2617 2022-02-16 op }
79 3baa2617 2022-02-16 op
80 3baa2617 2022-02-16 op static __dead void
81 3baa2617 2022-02-16 op ctl_usage(struct ctl_command *ctl)
82 3baa2617 2022-02-16 op {
83 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
84 3baa2617 2022-02-16 op ctl->name, ctl->usage);
85 3baa2617 2022-02-16 op exit(1);
86 3baa2617 2022-02-16 op }
87 3baa2617 2022-02-16 op
88 96233476 2022-06-09 op /* based on canonpath from kern_pledge.c */
89 3baa2617 2022-02-16 op static int
90 96233476 2022-06-09 op canonpath(const char *input, char *buf, size_t bufsize)
91 96233476 2022-06-09 op {
92 96233476 2022-06-09 op const char *p;
93 96233476 2022-06-09 op char *q, path[PATH_MAX];
94 96233476 2022-06-09 op
95 96233476 2022-06-09 op if (input[0] != '/') {
96 96233476 2022-06-09 op if (snprintf(path, sizeof(path), "%s/%s", cwd, input)
97 96233476 2022-06-09 op >= sizeof(path)) {
98 96233476 2022-06-09 op errno = ENAMETOOLONG;
99 96233476 2022-06-09 op return -1;
100 96233476 2022-06-09 op }
101 96233476 2022-06-09 op input = path;
102 96233476 2022-06-09 op }
103 96233476 2022-06-09 op
104 96233476 2022-06-09 op p = input;
105 96233476 2022-06-09 op q = buf;
106 96233476 2022-06-09 op while (*p && (q - buf < bufsize)) {
107 96233476 2022-06-09 op if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
108 96233476 2022-06-09 op p += 1;
109 96233476 2022-06-09 op
110 96233476 2022-06-09 op } else if (p[0] == '/' && p[1] == '.' &&
111 96233476 2022-06-09 op (p[2] == '/' || p[2] == '\0')) {
112 96233476 2022-06-09 op p += 2;
113 96233476 2022-06-09 op
114 96233476 2022-06-09 op } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
115 96233476 2022-06-09 op (p[3] == '/' || p[3] == '\0')) {
116 96233476 2022-06-09 op p += 3;
117 96233476 2022-06-09 op if (q != buf) /* "/../" at start of buf */
118 96233476 2022-06-09 op while (*--q != '/')
119 96233476 2022-06-09 op continue;
120 96233476 2022-06-09 op
121 96233476 2022-06-09 op } else {
122 96233476 2022-06-09 op *q++ = *p++;
123 96233476 2022-06-09 op }
124 96233476 2022-06-09 op }
125 96233476 2022-06-09 op if ((*p == '\0') && (q - buf < bufsize)) {
126 96233476 2022-06-09 op *q = 0;
127 96233476 2022-06-09 op return 0;
128 96233476 2022-06-09 op } else {
129 96233476 2022-06-09 op errno = ENAMETOOLONG;
130 96233476 2022-06-09 op return -1;
131 96233476 2022-06-09 op }
132 96233476 2022-06-09 op }
133 96233476 2022-06-09 op
134 96233476 2022-06-09 op static int
135 6576093a 2022-07-11 op parse(struct parse_result *res, int argc, char **argv)
136 3baa2617 2022-02-16 op {
137 3baa2617 2022-02-16 op struct ctl_command *ctl = NULL;
138 1d673950 2022-03-03 op const char *argv0;
139 3baa2617 2022-02-16 op int i, status;
140 3baa2617 2022-02-16 op
141 1d673950 2022-03-03 op if ((argv0 = argv[0]) == NULL)
142 1d673950 2022-03-03 op argv0 = "status";
143 1d673950 2022-03-03 op
144 3baa2617 2022-02-16 op for (i = 0; ctl_commands[i].name != NULL; ++i) {
145 1d673950 2022-03-03 op if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
146 3baa2617 2022-02-16 op == 0) {
147 3baa2617 2022-02-16 op if (ctl != NULL) {
148 3baa2617 2022-02-16 op fprintf(stderr,
149 1d673950 2022-03-03 op "ambiguous argument: %s\n", argv0);
150 3baa2617 2022-02-16 op usage();
151 3baa2617 2022-02-16 op }
152 3baa2617 2022-02-16 op ctl = &ctl_commands[i];
153 3baa2617 2022-02-16 op }
154 3baa2617 2022-02-16 op }
155 3baa2617 2022-02-16 op
156 3baa2617 2022-02-16 op if (ctl == NULL) {
157 3baa2617 2022-02-16 op fprintf(stderr, "unknown argument: %s\n", argv[0]);
158 3baa2617 2022-02-16 op usage();
159 3baa2617 2022-02-16 op }
160 3baa2617 2022-02-16 op
161 6576093a 2022-07-11 op res->action = ctl->action;
162 6576093a 2022-07-11 op res->ctl = ctl;
163 3baa2617 2022-02-16 op
164 6576093a 2022-07-11 op status = ctl->main(res, argc, argv);
165 3baa2617 2022-02-16 op close(ibuf->fd);
166 3baa2617 2022-02-16 op free(ibuf);
167 3baa2617 2022-02-16 op return status;
168 3baa2617 2022-02-16 op }
169 3baa2617 2022-02-16 op
170 3baa2617 2022-02-16 op static int
171 fad0fb69 2022-05-28 op load_files(struct parse_result *res, int *ret)
172 7a427ecd 2022-02-17 op {
173 7a427ecd 2022-02-17 op const char *file;
174 7a427ecd 2022-02-17 op char *line = NULL;
175 96233476 2022-06-09 op char path[PATH_MAX];
176 96233476 2022-06-09 op size_t linesize = 0, i = 0;
177 3af93963 2022-03-02 op ssize_t linelen, curr = -1;
178 7a427ecd 2022-02-17 op
179 4b42d3a3 2022-07-13 op while ((linelen = getline(&line, &linesize, res->fp)) != -1) {
180 7a427ecd 2022-02-17 op if (linelen == 0)
181 7a427ecd 2022-02-17 op continue;
182 7a427ecd 2022-02-17 op line[linelen-1] = '\0';
183 7a427ecd 2022-02-17 op file = line;
184 1dd3b054 2022-03-25 op if (!strncmp(file, "> ", 2)) {
185 7a427ecd 2022-02-17 op file += 2;
186 3af93963 2022-03-02 op curr = i;
187 1dd3b054 2022-03-25 op } else if (!strncmp(file, " ", 2))
188 7a427ecd 2022-02-17 op file += 2;
189 1dd3b054 2022-03-25 op
190 18223c28 2022-06-13 op memset(path, 0, sizeof(path));
191 96233476 2022-06-09 op if (canonpath(file, path, sizeof(path)) == -1) {
192 f1beb69a 2022-06-13 op log_warn("canonpath %s", file);
193 7a427ecd 2022-02-17 op continue;
194 7a427ecd 2022-02-17 op }
195 7a427ecd 2022-02-17 op
196 3af93963 2022-03-02 op i++;
197 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
198 7a427ecd 2022-02-17 op path, sizeof(path));
199 7a427ecd 2022-02-17 op }
200 7a427ecd 2022-02-17 op
201 7a427ecd 2022-02-17 op free(line);
202 4b42d3a3 2022-07-13 op if (ferror(res->fp))
203 7a427ecd 2022-02-17 op fatal("getline");
204 4b42d3a3 2022-07-13 op fclose(res->fp);
205 4b42d3a3 2022-07-13 op res->fp = NULL;
206 7a427ecd 2022-02-17 op
207 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
208 3af93963 2022-03-02 op &curr, sizeof(curr));
209 7a427ecd 2022-02-17 op imsg_flush(ibuf);
210 7a427ecd 2022-02-17 op return 0;
211 3baa2617 2022-02-16 op }
212 3baa2617 2022-02-16 op
213 fad0fb69 2022-05-28 op static const char *
214 fad0fb69 2022-05-28 op imsg_strerror(struct imsg *imsg)
215 6b47a39f 2022-02-21 op {
216 fad0fb69 2022-05-28 op size_t datalen;
217 fad0fb69 2022-05-28 op const char *msg;
218 6b47a39f 2022-02-21 op
219 fad0fb69 2022-05-28 op datalen = IMSG_DATA_SIZE(*imsg);
220 fad0fb69 2022-05-28 op msg = imsg->data;
221 fad0fb69 2022-05-28 op if (datalen == 0 || msg[datalen-1] != '\0')
222 fad0fb69 2022-05-28 op fatalx("malformed error message");
223 6b47a39f 2022-02-21 op
224 fad0fb69 2022-05-28 op return msg;
225 fad0fb69 2022-05-28 op }
226 90122a37 2022-05-14 op
227 fad0fb69 2022-05-28 op static const char *
228 232e682b 2022-07-12 op event_name(int type)
229 fad0fb69 2022-05-28 op {
230 6b47a39f 2022-02-21 op switch (type) {
231 6b47a39f 2022-02-21 op case IMSG_CTL_PLAY:
232 fad0fb69 2022-05-28 op return "play";
233 6b47a39f 2022-02-21 op case IMSG_CTL_PAUSE:
234 fad0fb69 2022-05-28 op return "pause";
235 6b47a39f 2022-02-21 op case IMSG_CTL_STOP:
236 fad0fb69 2022-05-28 op return "stop";
237 6b47a39f 2022-02-21 op case IMSG_CTL_NEXT:
238 fad0fb69 2022-05-28 op return "next";
239 6b47a39f 2022-02-21 op case IMSG_CTL_PREV:
240 fad0fb69 2022-05-28 op return "prev";
241 6b47a39f 2022-02-21 op case IMSG_CTL_JUMP:
242 fad0fb69 2022-05-28 op return "jump";
243 6b47a39f 2022-02-21 op case IMSG_CTL_ADD:
244 fad0fb69 2022-05-28 op return "add";
245 6b47a39f 2022-02-21 op case IMSG_CTL_COMMIT:
246 fad0fb69 2022-05-28 op return "load";
247 9fb94242 2022-07-13 op case IMSG_CTL_MODE:
248 9fb94242 2022-07-13 op return "mode";
249 15aecb89 2022-07-12 op case IMSG_CTL_SEEK:
250 15aecb89 2022-07-12 op return "seek";
251 6b47a39f 2022-02-21 op default:
252 fad0fb69 2022-05-28 op return "unknown";
253 6b47a39f 2022-02-21 op }
254 bdc8c4e0 2022-07-08 op }
255 bdc8c4e0 2022-07-08 op
256 bdc8c4e0 2022-07-08 op static void
257 68e4f29e 2022-07-10 op print_time(const char *label, int64_t seconds, const char *suffx)
258 bdc8c4e0 2022-07-08 op {
259 bdc8c4e0 2022-07-08 op int hours, minutes;
260 bdc8c4e0 2022-07-08 op
261 bdc8c4e0 2022-07-08 op if (seconds < 0)
262 bdc8c4e0 2022-07-08 op seconds = 0;
263 bdc8c4e0 2022-07-08 op
264 bdc8c4e0 2022-07-08 op hours = seconds / 3600;
265 bdc8c4e0 2022-07-08 op seconds -= hours * 3600;
266 bdc8c4e0 2022-07-08 op
267 bdc8c4e0 2022-07-08 op minutes = seconds / 60;
268 bdc8c4e0 2022-07-08 op seconds -= minutes * 60;
269 bdc8c4e0 2022-07-08 op
270 68e4f29e 2022-07-10 op printf("%s", label);
271 bdc8c4e0 2022-07-08 op if (hours != 0)
272 bdc8c4e0 2022-07-08 op printf("%02d:", hours);
273 68e4f29e 2022-07-10 op printf("%02d:%02lld%s", minutes, (long long)seconds, suffx);
274 68e4f29e 2022-07-10 op }
275 68e4f29e 2022-07-10 op
276 68e4f29e 2022-07-10 op static void
277 68e4f29e 2022-07-10 op print_status(struct player_status *ps, const char *spec)
278 68e4f29e 2022-07-10 op {
279 68e4f29e 2022-07-10 op const char *status;
280 68e4f29e 2022-07-10 op double percent;
281 68e4f29e 2022-07-10 op char *dup, *tmp, *tok;
282 68e4f29e 2022-07-10 op
283 68e4f29e 2022-07-10 op if (ps->status == STATE_STOPPED)
284 68e4f29e 2022-07-10 op status = "stopped";
285 68e4f29e 2022-07-10 op else if (ps->status == STATE_PLAYING)
286 68e4f29e 2022-07-10 op status = "playing";
287 68e4f29e 2022-07-10 op else if (ps->status == STATE_PAUSED)
288 68e4f29e 2022-07-10 op status = "paused";
289 68e4f29e 2022-07-10 op else
290 68e4f29e 2022-07-10 op status = "unknown";
291 68e4f29e 2022-07-10 op
292 68e4f29e 2022-07-10 op percent = 100.0 * (double)ps->position / (double)ps->duration;
293 68e4f29e 2022-07-10 op
294 68e4f29e 2022-07-10 op tmp = dup = xstrdup(spec);
295 68e4f29e 2022-07-10 op while ((tok = strsep(&tmp, ",")) != NULL) {
296 68e4f29e 2022-07-10 op if (*tok == '\0')
297 68e4f29e 2022-07-10 op continue;
298 68e4f29e 2022-07-10 op
299 68e4f29e 2022-07-10 op if (!strcmp(tok, "path")) {
300 68e4f29e 2022-07-10 op puts(ps->path);
301 9fb94242 2022-07-13 op } else if (!strcmp(tok, "mode:oneline")) {
302 68e4f29e 2022-07-10 op printf("repeat one:%s ",
303 9fb94242 2022-07-13 op ps->mode.repeat_one ? "on" : "off");
304 9fb94242 2022-07-13 op printf("all:%s ", ps->mode.repeat_all ? "on" : "off");
305 9fb94242 2022-07-13 op printf("consume:%s\n", ps->mode.consume ? "on" : "off");
306 e08b784c 2022-07-13 op } else if (!strcmp(tok, "mode")) {
307 68e4f29e 2022-07-10 op printf("repeat all %s\n",
308 9fb94242 2022-07-13 op ps->mode.repeat_all ? "on" : "off");
309 32f74855 2022-07-13 op printf("repeat one %s\n",
310 32f74855 2022-07-13 op ps->mode.repeat_one ? "on" : "off");
311 9fb94242 2022-07-13 op printf("consume %s\n",
312 9fb94242 2022-07-13 op ps->mode.consume ? "on" : "off");
313 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "status")) {
314 68e4f29e 2022-07-10 op printf("%s %s\n", status, ps->path);
315 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "time:oneline")) {
316 68e4f29e 2022-07-10 op print_time("time ", ps->position, " / ");
317 68e4f29e 2022-07-10 op print_time("", ps->duration, "\n");
318 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "time:percentage")) {
319 68e4f29e 2022-07-10 op printf("position %.2f%%\n", percent);
320 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "time:raw")) {
321 68e4f29e 2022-07-10 op printf("position %lld\n", (long long)ps->position);
322 68e4f29e 2022-07-10 op printf("duration %lld\n", (long long)ps->duration);
323 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "time")) {
324 68e4f29e 2022-07-10 op print_time("position ", ps->position, "\n");
325 68e4f29e 2022-07-10 op print_time("duration ", ps->duration, "\n");
326 68e4f29e 2022-07-10 op }
327 68e4f29e 2022-07-10 op }
328 68e4f29e 2022-07-10 op
329 68e4f29e 2022-07-10 op free(dup);
330 6b47a39f 2022-02-21 op }
331 6b47a39f 2022-02-21 op
332 6b47a39f 2022-02-21 op static int
333 3baa2617 2022-02-16 op ctlaction(struct parse_result *res)
334 3baa2617 2022-02-16 op {
335 fad0fb69 2022-05-28 op char path[PATH_MAX];
336 3baa2617 2022-02-16 op struct imsg imsg;
337 fad0fb69 2022-05-28 op struct player_status ps;
338 fad0fb69 2022-05-28 op size_t datalen;
339 3baa2617 2022-02-16 op ssize_t n;
340 fad0fb69 2022-05-28 op int i, type, ret = 0, done = 1;
341 3baa2617 2022-02-16 op
342 4b42d3a3 2022-07-13 op if (pledge("stdio", NULL) == -1)
343 4b42d3a3 2022-07-13 op fatal("pledge");
344 4b42d3a3 2022-07-13 op
345 3baa2617 2022-02-16 op switch (res->action) {
346 3baa2617 2022-02-16 op case PLAY:
347 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
348 00f500ff 2022-02-19 op if (verbose) {
349 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
350 00f500ff 2022-02-19 op NULL, 0);
351 00f500ff 2022-02-19 op done = 0;
352 00f500ff 2022-02-19 op }
353 3baa2617 2022-02-16 op break;
354 3baa2617 2022-02-16 op case PAUSE:
355 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
356 3baa2617 2022-02-16 op break;
357 3baa2617 2022-02-16 op case TOGGLE:
358 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
359 00f500ff 2022-02-19 op if (verbose) {
360 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
361 00f500ff 2022-02-19 op NULL, 0);
362 00f500ff 2022-02-19 op done = 0;
363 00f500ff 2022-02-19 op }
364 3baa2617 2022-02-16 op break;
365 3baa2617 2022-02-16 op case STOP:
366 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
367 3baa2617 2022-02-16 op break;
368 3baa2617 2022-02-16 op case ADD:
369 8ff9231f 2022-02-16 op done = 0;
370 d903ec9a 2022-05-29 op for (i = 0; res->files[i] != NULL; ++i) {
371 18223c28 2022-06-13 op memset(path, 0, sizeof(path));
372 ef593b43 2022-06-09 op if (canonpath(res->files[i], path, sizeof(path))
373 ef593b43 2022-06-09 op == -1) {
374 ef593b43 2022-06-09 op log_warn("canonpath %s", res->files[i]);
375 fad0fb69 2022-05-28 op continue;
376 fad0fb69 2022-05-28 op }
377 fad0fb69 2022-05-28 op
378 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
379 fad0fb69 2022-05-28 op path, sizeof(path));
380 fad0fb69 2022-05-28 op }
381 fad0fb69 2022-05-28 op ret = i == 0;
382 3baa2617 2022-02-16 op break;
383 3baa2617 2022-02-16 op case FLUSH:
384 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
385 3baa2617 2022-02-16 op break;
386 3baa2617 2022-02-16 op case SHOW:
387 3baa2617 2022-02-16 op done = 0;
388 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
389 3baa2617 2022-02-16 op break;
390 bb3f279f 2022-02-16 op case STATUS:
391 bb3f279f 2022-02-16 op done = 0;
392 bb3f279f 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
393 af27e631 2022-02-17 op break;
394 af27e631 2022-02-17 op case NEXT:
395 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
396 00f500ff 2022-02-19 op if (verbose) {
397 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
398 00f500ff 2022-02-19 op NULL, 0);
399 00f500ff 2022-02-19 op done = 0;
400 00f500ff 2022-02-19 op }
401 bb3f279f 2022-02-16 op break;
402 af27e631 2022-02-17 op case PREV:
403 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
404 00f500ff 2022-02-19 op if (verbose) {
405 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
406 00f500ff 2022-02-19 op NULL, 0);
407 00f500ff 2022-02-19 op done = 0;
408 00f500ff 2022-02-19 op }
409 af27e631 2022-02-17 op break;
410 14be015f 2022-02-17 op case LOAD:
411 7a427ecd 2022-02-17 op done = 0;
412 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
413 a913de21 2022-02-17 op break;
414 a913de21 2022-02-17 op case JUMP:
415 a913de21 2022-02-17 op done = 0;
416 fad0fb69 2022-05-28 op memset(path, 0, sizeof(path));
417 4b42d3a3 2022-07-13 op strlcpy(path, res->files[0], sizeof(path));
418 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
419 fad0fb69 2022-05-28 op path, sizeof(path));
420 7a427ecd 2022-02-17 op break;
421 9fb94242 2022-07-13 op case MODE:
422 b72c0765 2022-07-13 op done = 0;
423 9fb94242 2022-07-13 op imsg_compose(ibuf, IMSG_CTL_MODE, 0, 0, -1,
424 9fb94242 2022-07-13 op &res->mode, sizeof(res->mode));
425 b72c0765 2022-07-13 op res->status_format = "mode:oneline";
426 b72c0765 2022-07-13 op if (verbose)
427 b72c0765 2022-07-13 op res->status_format = "mode";
428 310ef57c 2022-02-19 op break;
429 6b47a39f 2022-02-21 op case MONITOR:
430 6b47a39f 2022-02-21 op done = 0;
431 6b47a39f 2022-02-21 op imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
432 6b47a39f 2022-02-21 op NULL, 0);
433 6b47a39f 2022-02-21 op break;
434 8d087670 2022-07-09 op case RESTART:
435 8d087670 2022-07-09 op memset(&res->seek, 0, sizeof(res->seek));
436 8d087670 2022-07-09 op /* fallthrough */
437 e5d4e9f7 2022-07-09 op case SEEK:
438 e5d4e9f7 2022-07-09 op imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
439 e5d4e9f7 2022-07-09 op sizeof(res->seek));
440 e5d4e9f7 2022-07-09 op break;
441 3baa2617 2022-02-16 op case NONE:
442 3baa2617 2022-02-16 op /* action not expected */
443 3baa2617 2022-02-16 op fatalx("invalid action %u", res->action);
444 3baa2617 2022-02-16 op break;
445 3baa2617 2022-02-16 op }
446 3baa2617 2022-02-16 op
447 3baa2617 2022-02-16 op if (ret != 0)
448 3baa2617 2022-02-16 op goto end;
449 3baa2617 2022-02-16 op
450 3baa2617 2022-02-16 op imsg_flush(ibuf);
451 3baa2617 2022-02-16 op
452 fad0fb69 2022-05-28 op i = 0;
453 3baa2617 2022-02-16 op while (!done) {
454 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
455 3baa2617 2022-02-16 op fatalx("imsg_read error");
456 3baa2617 2022-02-16 op if (n == 0)
457 3baa2617 2022-02-16 op fatalx("pipe closed");
458 3baa2617 2022-02-16 op
459 3baa2617 2022-02-16 op while (!done) {
460 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
461 3baa2617 2022-02-16 op fatalx("imsg_get error");
462 3baa2617 2022-02-16 op if (n == 0)
463 3baa2617 2022-02-16 op break;
464 3baa2617 2022-02-16 op
465 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ERR) {
466 fad0fb69 2022-05-28 op log_warnx("%s: %s", res->ctl->name,
467 fad0fb69 2022-05-28 op imsg_strerror(&imsg));
468 fad0fb69 2022-05-28 op ret = 1;
469 fad0fb69 2022-05-28 op done = 1;
470 fad0fb69 2022-05-28 op break;
471 fad0fb69 2022-05-28 op }
472 fad0fb69 2022-05-28 op
473 fad0fb69 2022-05-28 op datalen = IMSG_DATA_SIZE(imsg);
474 fad0fb69 2022-05-28 op
475 3baa2617 2022-02-16 op switch (res->action) {
476 8ff9231f 2022-02-16 op case ADD:
477 d903ec9a 2022-05-29 op if (res->files[i] == NULL)
478 fad0fb69 2022-05-28 op fatalx("received more replies than "
479 fad0fb69 2022-05-28 op "files enqueued.");
480 fad0fb69 2022-05-28 op
481 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
482 d903ec9a 2022-05-29 op log_debug("enqueued %s", res->files[i]);
483 fad0fb69 2022-05-28 op else
484 fad0fb69 2022-05-28 op fatalx("invalid message %d",
485 fad0fb69 2022-05-28 op imsg.hdr.type);
486 fad0fb69 2022-05-28 op i++;
487 d903ec9a 2022-05-29 op done = res->files[i] == NULL;
488 8ff9231f 2022-02-16 op break;
489 3baa2617 2022-02-16 op case SHOW:
490 fad0fb69 2022-05-28 op if (datalen == 0) {
491 fad0fb69 2022-05-28 op done = 1;
492 fad0fb69 2022-05-28 op break;
493 fad0fb69 2022-05-28 op }
494 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
495 fad0fb69 2022-05-28 op fatalx("data size mismatch");
496 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
497 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
498 fad0fb69 2022-05-28 op fatalx("received corrupted data");
499 fad0fb69 2022-05-28 op if (res->pretty) {
500 fad0fb69 2022-05-28 op char c = ' ';
501 fad0fb69 2022-05-28 op if (ps.status == STATE_PLAYING)
502 fad0fb69 2022-05-28 op c = '>';
503 fad0fb69 2022-05-28 op printf("%c ", c);
504 fad0fb69 2022-05-28 op }
505 fad0fb69 2022-05-28 op puts(ps.path);
506 3baa2617 2022-02-16 op break;
507 146307bd 2022-02-17 op case PLAY:
508 146307bd 2022-02-17 op case TOGGLE:
509 bb3f279f 2022-02-16 op case STATUS:
510 146307bd 2022-02-17 op case NEXT:
511 146307bd 2022-02-17 op case PREV:
512 a913de21 2022-02-17 op case JUMP:
513 b72c0765 2022-07-13 op case MODE:
514 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_STATUS)
515 fad0fb69 2022-05-28 op fatalx("invalid message %d",
516 fad0fb69 2022-05-28 op imsg.hdr.type);
517 fad0fb69 2022-05-28 op
518 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
519 fad0fb69 2022-05-28 op fatalx("data size mismatch");
520 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
521 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
522 fad0fb69 2022-05-28 op fatalx("received corrupted data");
523 fad0fb69 2022-05-28 op
524 68e4f29e 2022-07-10 op print_status(&ps, res->status_format);
525 fad0fb69 2022-05-28 op done = 1;
526 bb3f279f 2022-02-16 op break;
527 7a427ecd 2022-02-17 op case LOAD:
528 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
529 fad0fb69 2022-05-28 op break;
530 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_COMMIT) {
531 fad0fb69 2022-05-28 op done = 1;
532 fad0fb69 2022-05-28 op break;
533 fad0fb69 2022-05-28 op }
534 fad0fb69 2022-05-28 op
535 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_BEGIN)
536 fad0fb69 2022-05-28 op fatalx("invalid message %d",
537 fad0fb69 2022-05-28 op imsg.hdr.type);
538 fad0fb69 2022-05-28 op
539 fad0fb69 2022-05-28 op load_files(res, &ret);
540 7a427ecd 2022-02-17 op break;
541 6b47a39f 2022-02-21 op case MONITOR:
542 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_MONITOR)
543 fad0fb69 2022-05-28 op fatalx("invalid message %d",
544 fad0fb69 2022-05-28 op imsg.hdr.type);
545 fad0fb69 2022-05-28 op
546 fad0fb69 2022-05-28 op if (datalen != sizeof(type))
547 fad0fb69 2022-05-28 op fatalx("data size mismatch");
548 fad0fb69 2022-05-28 op
549 fad0fb69 2022-05-28 op memcpy(&type, imsg.data, sizeof(type));
550 fad0fb69 2022-05-28 op if (type < 0 || type > IMSG__LAST)
551 fad0fb69 2022-05-28 op fatalx("received corrupted data");
552 fad0fb69 2022-05-28 op
553 fad0fb69 2022-05-28 op if (!res->monitor[type])
554 fad0fb69 2022-05-28 op break;
555 fad0fb69 2022-05-28 op
556 232e682b 2022-07-12 op puts(event_name(type));
557 fad0fb69 2022-05-28 op fflush(stdout);
558 6b47a39f 2022-02-21 op break;
559 3baa2617 2022-02-16 op default:
560 3baa2617 2022-02-16 op done = 1;
561 3baa2617 2022-02-16 op break;
562 3baa2617 2022-02-16 op }
563 3baa2617 2022-02-16 op
564 3baa2617 2022-02-16 op imsg_free(&imsg);
565 3baa2617 2022-02-16 op }
566 3baa2617 2022-02-16 op }
567 3baa2617 2022-02-16 op
568 3baa2617 2022-02-16 op end:
569 3baa2617 2022-02-16 op return ret;
570 3baa2617 2022-02-16 op }
571 3baa2617 2022-02-16 op
572 2ddcfa40 2022-07-08 op static int
573 3baa2617 2022-02-16 op ctl_noarg(struct parse_result *res, int argc, char **argv)
574 3baa2617 2022-02-16 op {
575 7e29fc4a 2022-03-03 op int ch;
576 7e29fc4a 2022-03-03 op
577 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
578 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
579 7e29fc4a 2022-03-03 op argc -= optind;
580 7e29fc4a 2022-03-03 op argv += optind;
581 7e29fc4a 2022-03-03 op
582 acaf7eb2 2022-03-05 op if (argc > 0)
583 3baa2617 2022-02-16 op ctl_usage(res->ctl);
584 7e29fc4a 2022-03-03 op
585 3baa2617 2022-02-16 op return ctlaction(res);
586 3baa2617 2022-02-16 op }
587 3baa2617 2022-02-16 op
588 2ddcfa40 2022-07-08 op static int
589 3baa2617 2022-02-16 op ctl_add(struct parse_result *res, int argc, char **argv)
590 3baa2617 2022-02-16 op {
591 7e29fc4a 2022-03-03 op int ch;
592 7e29fc4a 2022-03-03 op
593 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
594 3baa2617 2022-02-16 op ctl_usage(res->ctl);
595 7e29fc4a 2022-03-03 op argc -= optind;
596 7e29fc4a 2022-03-03 op argv += optind;
597 7e29fc4a 2022-03-03 op
598 7e29fc4a 2022-03-03 op if (argc == 0)
599 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
600 7e29fc4a 2022-03-03 op res->files = argv;
601 14be015f 2022-02-17 op
602 3baa2617 2022-02-16 op return ctlaction(res);
603 3baa2617 2022-02-16 op }
604 14be015f 2022-02-17 op
605 2ddcfa40 2022-07-08 op static int
606 8e916714 2022-02-17 op ctl_show(struct parse_result *res, int argc, char **argv)
607 8e916714 2022-02-17 op {
608 8e916714 2022-02-17 op int ch;
609 8e916714 2022-02-17 op
610 8e916714 2022-02-17 op while ((ch = getopt(argc, argv, "p")) != -1) {
611 8e916714 2022-02-17 op switch (ch) {
612 8e916714 2022-02-17 op case 'p':
613 8e916714 2022-02-17 op res->pretty = 1;
614 8e916714 2022-02-17 op break;
615 8e916714 2022-02-17 op default:
616 8e916714 2022-02-17 op ctl_usage(res->ctl);
617 8e916714 2022-02-17 op }
618 8e916714 2022-02-17 op }
619 8e916714 2022-02-17 op
620 8e916714 2022-02-17 op return ctlaction(res);
621 8e916714 2022-02-17 op }
622 8e916714 2022-02-17 op
623 2ddcfa40 2022-07-08 op static int
624 14be015f 2022-02-17 op ctl_load(struct parse_result *res, int argc, char **argv)
625 14be015f 2022-02-17 op {
626 7e29fc4a 2022-03-03 op int ch;
627 7e29fc4a 2022-03-03 op
628 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
629 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
630 7e29fc4a 2022-03-03 op argc -= optind;
631 7e29fc4a 2022-03-03 op argv += optind;
632 7e29fc4a 2022-03-03 op
633 4b42d3a3 2022-07-13 op if (argc > 1)
634 14be015f 2022-02-17 op ctl_usage(res->ctl);
635 14be015f 2022-02-17 op
636 4b42d3a3 2022-07-13 op res->fp = stdin;
637 4b42d3a3 2022-07-13 op if (argc == 1) {
638 4b42d3a3 2022-07-13 op if ((res->fp = fopen(argv[0], "r")) == NULL)
639 4b42d3a3 2022-07-13 op fatal("can't open %s", argv[0]);
640 4b42d3a3 2022-07-13 op }
641 14be015f 2022-02-17 op
642 7a427ecd 2022-02-17 op return ctlaction(res);
643 14be015f 2022-02-17 op }
644 14be015f 2022-02-17 op
645 2ddcfa40 2022-07-08 op static int
646 a913de21 2022-02-17 op ctl_jump(struct parse_result *res, int argc, char **argv)
647 a913de21 2022-02-17 op {
648 a913de21 2022-02-17 op int ch;
649 a913de21 2022-02-17 op
650 a913de21 2022-02-17 op while ((ch = getopt(argc, argv, "")) != -1)
651 a913de21 2022-02-17 op ctl_usage(res->ctl);
652 a913de21 2022-02-17 op argc -= optind;
653 a913de21 2022-02-17 op argv += optind;
654 a913de21 2022-02-17 op
655 a913de21 2022-02-17 op if (argc != 1)
656 a913de21 2022-02-17 op ctl_usage(res->ctl);
657 a913de21 2022-02-17 op
658 4b42d3a3 2022-07-13 op res->files = argv;
659 310ef57c 2022-02-19 op return ctlaction(res);
660 1eae758b 2022-07-13 op }
661 1eae758b 2022-07-13 op
662 1eae758b 2022-07-13 op static int
663 1eae758b 2022-07-13 op parse_mode(struct parse_result *res, const char *v)
664 1eae758b 2022-07-13 op {
665 1eae758b 2022-07-13 op if (v == NULL)
666 1eae758b 2022-07-13 op return MODE_TOGGLE;
667 1eae758b 2022-07-13 op if (!strcmp(v, "on"))
668 1eae758b 2022-07-13 op return MODE_ON;
669 1eae758b 2022-07-13 op if (!strcmp(v, "off"))
670 1eae758b 2022-07-13 op return MODE_OFF;
671 1eae758b 2022-07-13 op ctl_usage(res->ctl);
672 310ef57c 2022-02-19 op }
673 310ef57c 2022-02-19 op
674 2ddcfa40 2022-07-08 op static int
675 310ef57c 2022-02-19 op ctl_repeat(struct parse_result *res, int argc, char **argv)
676 310ef57c 2022-02-19 op {
677 1eae758b 2022-07-13 op int ch;
678 310ef57c 2022-02-19 op
679 310ef57c 2022-02-19 op while ((ch = getopt(argc, argv, "")) != -1)
680 310ef57c 2022-02-19 op ctl_usage(res->ctl);
681 310ef57c 2022-02-19 op argc -= optind;
682 310ef57c 2022-02-19 op argv += optind;
683 310ef57c 2022-02-19 op
684 1eae758b 2022-07-13 op if (argc != 1 && argc != 2)
685 310ef57c 2022-02-19 op ctl_usage(res->ctl);
686 310ef57c 2022-02-19 op
687 310ef57c 2022-02-19 op if (!strcmp(argv[0], "one"))
688 1eae758b 2022-07-13 op res->mode.repeat_one = parse_mode(res, argv[1]);
689 310ef57c 2022-02-19 op else if (!strcmp(argv[0], "all"))
690 1eae758b 2022-07-13 op res->mode.repeat_all = parse_mode(res, argv[1]);
691 310ef57c 2022-02-19 op else
692 9fb94242 2022-07-13 op ctl_usage(res->ctl);
693 9fb94242 2022-07-13 op
694 9fb94242 2022-07-13 op return ctlaction(res);
695 9fb94242 2022-07-13 op }
696 9fb94242 2022-07-13 op
697 9fb94242 2022-07-13 op static int
698 9fb94242 2022-07-13 op ctl_consume(struct parse_result *res, int argc, char **argv)
699 9fb94242 2022-07-13 op {
700 9fb94242 2022-07-13 op int ch;
701 9fb94242 2022-07-13 op
702 9fb94242 2022-07-13 op while ((ch = getopt(argc, argv, "")) != -1)
703 9fb94242 2022-07-13 op ctl_usage(res->ctl);
704 9fb94242 2022-07-13 op argc -= optind;
705 9fb94242 2022-07-13 op argv += optind;
706 9fb94242 2022-07-13 op
707 1eae758b 2022-07-13 op if (argc > 1)
708 310ef57c 2022-02-19 op ctl_usage(res->ctl);
709 90122a37 2022-05-14 op
710 1eae758b 2022-07-13 op res->mode.consume = parse_mode(res, argv[0]);
711 90122a37 2022-05-14 op return ctlaction(res);
712 90122a37 2022-05-14 op }
713 90122a37 2022-05-14 op
714 2ddcfa40 2022-07-08 op static int
715 90122a37 2022-05-14 op ctl_monitor(struct parse_result *res, int argc, char **argv)
716 90122a37 2022-05-14 op {
717 15aecb89 2022-07-12 op int ch, n = 0;
718 90122a37 2022-05-14 op const char *events;
719 90122a37 2022-05-14 op char *dup, *tmp, *tok;
720 90122a37 2022-05-14 op
721 90122a37 2022-05-14 op while ((ch = getopt(argc, argv, "")) != -1)
722 90122a37 2022-05-14 op ctl_usage(res->ctl);
723 90122a37 2022-05-14 op argc -= optind;
724 90122a37 2022-05-14 op argv += optind;
725 90122a37 2022-05-14 op
726 90122a37 2022-05-14 op if (argc > 1)
727 90122a37 2022-05-14 op ctl_usage(res->ctl);
728 90122a37 2022-05-14 op
729 9fb94242 2022-07-13 op events = "play,pause,stop,next,prev,jump,mode,add,load,seek";
730 90122a37 2022-05-14 op if (argc == 1)
731 90122a37 2022-05-14 op events = *argv;
732 310ef57c 2022-02-19 op
733 90122a37 2022-05-14 op tmp = dup = xstrdup(events);
734 90122a37 2022-05-14 op while ((tok = strsep(&tmp, ",")) != NULL) {
735 90122a37 2022-05-14 op if (*tok == '\0')
736 90122a37 2022-05-14 op continue;
737 90122a37 2022-05-14 op
738 15aecb89 2022-07-12 op n++;
739 90122a37 2022-05-14 op if (!strcmp(tok, "play"))
740 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PLAY] = 1;
741 90122a37 2022-05-14 op else if (!strcmp(tok, "pause"))
742 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PAUSE] = 1;
743 90122a37 2022-05-14 op else if (!strcmp(tok, "stop"))
744 90122a37 2022-05-14 op res->monitor[IMSG_CTL_STOP] = 1;
745 90122a37 2022-05-14 op else if (!strcmp(tok, "next"))
746 90122a37 2022-05-14 op res->monitor[IMSG_CTL_NEXT] = 1;
747 90122a37 2022-05-14 op else if (!strcmp(tok, "prev"))
748 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PREV] = 1;
749 90122a37 2022-05-14 op else if (!strcmp(tok, "jump"))
750 90122a37 2022-05-14 op res->monitor[IMSG_CTL_JUMP] = 1;
751 9fb94242 2022-07-13 op else if (!strcmp(tok, "mode"))
752 9fb94242 2022-07-13 op res->monitor[IMSG_CTL_MODE] = 1;
753 90122a37 2022-05-14 op else if (!strcmp(tok, "add"))
754 90122a37 2022-05-14 op res->monitor[IMSG_CTL_ADD] = 1;
755 90122a37 2022-05-14 op else if (!strcmp(tok, "load"))
756 90122a37 2022-05-14 op res->monitor[IMSG_CTL_COMMIT] = 1;
757 88519c7b 2022-07-09 op else if (!strcmp(tok, "seek"))
758 88519c7b 2022-07-09 op res->monitor[IMSG_CTL_SEEK] = 1;
759 15aecb89 2022-07-12 op else {
760 15aecb89 2022-07-12 op log_warnx("unknown event \"%s\"", tok);
761 15aecb89 2022-07-12 op n--;
762 15aecb89 2022-07-12 op }
763 90122a37 2022-05-14 op }
764 90122a37 2022-05-14 op
765 90122a37 2022-05-14 op free(dup);
766 15aecb89 2022-07-12 op if (n == 0)
767 15aecb89 2022-07-12 op ctl_usage(res->ctl);
768 e5d4e9f7 2022-07-09 op return ctlaction(res);
769 e5d4e9f7 2022-07-09 op }
770 e5d4e9f7 2022-07-09 op
771 e5d4e9f7 2022-07-09 op static int
772 e5d4e9f7 2022-07-09 op ctl_seek(struct parse_result *res, int argc, char **argv)
773 e5d4e9f7 2022-07-09 op {
774 071167ed 2022-07-09 op const char *n;
775 071167ed 2022-07-09 op char *ep;
776 071167ed 2022-07-09 op int hours = 0, minutes = 0, seconds = 0;
777 071167ed 2022-07-09 op int sign = 1;
778 e5d4e9f7 2022-07-09 op
779 e5d4e9f7 2022-07-09 op if (argc > 0) {
780 e5d4e9f7 2022-07-09 op /* skip the command name */
781 e5d4e9f7 2022-07-09 op argc--;
782 e5d4e9f7 2022-07-09 op argv++;
783 e5d4e9f7 2022-07-09 op }
784 e5d4e9f7 2022-07-09 op
785 e5d4e9f7 2022-07-09 op if (argc > 0 && !strcmp(*argv, "--")) {
786 e5d4e9f7 2022-07-09 op argc--;
787 e5d4e9f7 2022-07-09 op argv++;
788 e5d4e9f7 2022-07-09 op }
789 e5d4e9f7 2022-07-09 op
790 e5d4e9f7 2022-07-09 op if (argc != 1)
791 e5d4e9f7 2022-07-09 op ctl_usage(res->ctl);
792 e5d4e9f7 2022-07-09 op
793 e5d4e9f7 2022-07-09 op n = *argv;
794 e5d4e9f7 2022-07-09 op if (*n == '-' || *n == '+')
795 e5d4e9f7 2022-07-09 op res->seek.relative = 1;
796 071167ed 2022-07-09 op if (*n == '-') {
797 071167ed 2022-07-09 op n++;
798 071167ed 2022-07-09 op sign = -1;
799 071167ed 2022-07-09 op }
800 e5d4e9f7 2022-07-09 op
801 071167ed 2022-07-09 op seconds = strtol(n, &ep, 10);
802 071167ed 2022-07-09 op if (n[0] == '\0' ||
803 071167ed 2022-07-09 op (*ep != '\0' && *ep != ':' && *ep != '%') ||
804 071167ed 2022-07-09 op (*ep == '%' && ep[1] != '\0'))
805 071167ed 2022-07-09 op fatalx("invalid offset: %s", argv[0]);
806 071167ed 2022-07-09 op if (*ep == '\0' || *ep == '%') {
807 071167ed 2022-07-09 op res->seek.percent = *ep == '%';
808 071167ed 2022-07-09 op goto done;
809 071167ed 2022-07-09 op }
810 e5d4e9f7 2022-07-09 op
811 071167ed 2022-07-09 op n = ++ep;
812 071167ed 2022-07-09 op minutes = seconds;
813 071167ed 2022-07-09 op seconds = strtol(n, &ep, 10);
814 071167ed 2022-07-09 op if (n[0] == '\0' || (*ep != '\0' && *ep != ':'))
815 071167ed 2022-07-09 op fatalx("invalid offset: %s", argv[0]);
816 071167ed 2022-07-09 op if (*ep == '\0')
817 071167ed 2022-07-09 op goto done;
818 071167ed 2022-07-09 op
819 071167ed 2022-07-09 op n = ++ep;
820 071167ed 2022-07-09 op hours = minutes;
821 071167ed 2022-07-09 op minutes = seconds;
822 071167ed 2022-07-09 op seconds = strtol(n, &ep, 10);
823 071167ed 2022-07-09 op if (n[0] == '\0' || *ep != '\0')
824 071167ed 2022-07-09 op fatalx("invalid offset: %s", argv[0]);
825 071167ed 2022-07-09 op
826 071167ed 2022-07-09 op done:
827 071167ed 2022-07-09 op res->seek.offset = sign * (hours * 3600 + minutes * 60 + seconds);
828 a913de21 2022-02-17 op return ctlaction(res);
829 a913de21 2022-02-17 op }
830 a913de21 2022-02-17 op
831 fea541a8 2022-02-16 op static int
832 68e4f29e 2022-07-10 op ctl_status(struct parse_result *res, int argc, char **argv)
833 68e4f29e 2022-07-10 op {
834 68e4f29e 2022-07-10 op int ch;
835 68e4f29e 2022-07-10 op
836 68e4f29e 2022-07-10 op while ((ch = getopt(argc, argv, "f:")) != -1) {
837 68e4f29e 2022-07-10 op switch (ch) {
838 68e4f29e 2022-07-10 op case 'f':
839 6576093a 2022-07-11 op res->status_format = optarg;
840 68e4f29e 2022-07-10 op break;
841 68e4f29e 2022-07-10 op default:
842 68e4f29e 2022-07-10 op ctl_usage(res->ctl);
843 68e4f29e 2022-07-10 op }
844 68e4f29e 2022-07-10 op }
845 68e4f29e 2022-07-10 op argc -= optind;
846 68e4f29e 2022-07-10 op argv += optind;
847 68e4f29e 2022-07-10 op
848 68e4f29e 2022-07-10 op if (argc > 0)
849 68e4f29e 2022-07-10 op ctl_usage(res->ctl);
850 68e4f29e 2022-07-10 op
851 68e4f29e 2022-07-10 op return ctlaction(res);
852 68e4f29e 2022-07-10 op }
853 68e4f29e 2022-07-10 op
854 68e4f29e 2022-07-10 op static int
855 1d673950 2022-03-03 op ctl_get_lock(const char *lockfile)
856 3baa2617 2022-02-16 op {
857 1d673950 2022-03-03 op int lockfd;
858 3baa2617 2022-02-16 op
859 1d673950 2022-03-03 op if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
860 1d673950 2022-03-03 op log_debug("open failed: %s", strerror(errno));
861 1d673950 2022-03-03 op return -1;
862 1d673950 2022-03-03 op }
863 3baa2617 2022-02-16 op
864 1d673950 2022-03-03 op if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
865 1d673950 2022-03-03 op log_debug("flock failed: %s", strerror(errno));
866 1d673950 2022-03-03 op if (errno != EAGAIN)
867 1d673950 2022-03-03 op return -1;
868 1d673950 2022-03-03 op while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
869 1d673950 2022-03-03 op /* nop */;
870 1d673950 2022-03-03 op close(lockfd);
871 1d673950 2022-03-03 op return -2;
872 1d673950 2022-03-03 op }
873 1d673950 2022-03-03 op log_debug("flock succeeded");
874 1d673950 2022-03-03 op
875 1d673950 2022-03-03 op return lockfd;
876 1d673950 2022-03-03 op }
877 1d673950 2022-03-03 op
878 1d673950 2022-03-03 op static int
879 1d673950 2022-03-03 op ctl_connect(void)
880 1d673950 2022-03-03 op {
881 1d673950 2022-03-03 op struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
882 1d673950 2022-03-03 op struct sockaddr_un sa;
883 1d673950 2022-03-03 op size_t size;
884 1d673950 2022-03-03 op int fd, lockfd = -1, locked = 0, spawned = 0;
885 1b77ad48 2022-07-08 op int attempt = 0;
886 1d673950 2022-03-03 op char *lockfile = NULL;
887 1d673950 2022-03-03 op
888 1d673950 2022-03-03 op memset(&sa, 0, sizeof(sa));
889 1d673950 2022-03-03 op sa.sun_family = AF_UNIX;
890 1d673950 2022-03-03 op size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
891 1d673950 2022-03-03 op if (size >= sizeof(sa.sun_path)) {
892 1d673950 2022-03-03 op errno = ENAMETOOLONG;
893 fea541a8 2022-02-16 op return -1;
894 fea541a8 2022-02-16 op }
895 3baa2617 2022-02-16 op
896 1d673950 2022-03-03 op retry:
897 1d673950 2022-03-03 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
898 1d673950 2022-03-03 op return -1;
899 1d673950 2022-03-03 op
900 1d673950 2022-03-03 op if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
901 1d673950 2022-03-03 op log_debug("connection failed: %s", strerror(errno));
902 1d673950 2022-03-03 op if (errno != ECONNREFUSED && errno != ENOENT)
903 1d673950 2022-03-03 op goto failed;
904 1b77ad48 2022-07-08 op if (attempt++ == 100)
905 1b77ad48 2022-07-08 op goto failed;
906 1d673950 2022-03-03 op close(fd);
907 1d673950 2022-03-03 op
908 1d673950 2022-03-03 op if (!locked) {
909 1d673950 2022-03-03 op xasprintf(&lockfile, "%s.lock", csock);
910 1d673950 2022-03-03 op if ((lockfd = ctl_get_lock(lockfile)) < 0) {
911 1d673950 2022-03-03 op log_debug("didn't get the lock (%d)", lockfd);
912 1d673950 2022-03-03 op
913 1d673950 2022-03-03 op free(lockfile);
914 1d673950 2022-03-03 op lockfile = NULL;
915 1d673950 2022-03-03 op
916 1d673950 2022-03-03 op if (lockfd == -1)
917 1d673950 2022-03-03 op goto retry;
918 1d673950 2022-03-03 op }
919 1d673950 2022-03-03 op
920 1d673950 2022-03-03 op /*
921 1d673950 2022-03-03 op * Always retry at least once, even if we got
922 1d673950 2022-03-03 op * the lock, because another client could have
923 1d673950 2022-03-03 op * taken the lock, started the server and released
924 1d673950 2022-03-03 op * the lock between our connect() and flock()
925 1d673950 2022-03-03 op */
926 1d673950 2022-03-03 op locked = 1;
927 1d673950 2022-03-03 op goto retry;
928 1d673950 2022-03-03 op }
929 1d673950 2022-03-03 op
930 1d673950 2022-03-03 op if (!spawned) {
931 1d673950 2022-03-03 op log_debug("spawning the daemon");
932 1d673950 2022-03-03 op spawn_daemon();
933 1d673950 2022-03-03 op spawned = 1;
934 1d673950 2022-03-03 op }
935 1d673950 2022-03-03 op
936 1d673950 2022-03-03 op nanosleep(&ts, NULL);
937 1d673950 2022-03-03 op goto retry;
938 1d673950 2022-03-03 op }
939 1d673950 2022-03-03 op
940 1d673950 2022-03-03 op if (locked && lockfd >= 0) {
941 1d673950 2022-03-03 op unlink(lockfile);
942 1d673950 2022-03-03 op free(lockfile);
943 1d673950 2022-03-03 op close(lockfd);
944 1d673950 2022-03-03 op }
945 1d673950 2022-03-03 op return fd;
946 1d673950 2022-03-03 op
947 1d673950 2022-03-03 op failed:
948 1d673950 2022-03-03 op if (locked) {
949 1d673950 2022-03-03 op free(lockfile);
950 1d673950 2022-03-03 op close(lockfd);
951 1d673950 2022-03-03 op }
952 1d673950 2022-03-03 op close(fd);
953 1d673950 2022-03-03 op return -1;
954 fea541a8 2022-02-16 op }
955 fea541a8 2022-02-16 op
956 fea541a8 2022-02-16 op __dead void
957 fea541a8 2022-02-16 op ctl(int argc, char **argv)
958 fea541a8 2022-02-16 op {
959 6576093a 2022-07-11 op struct parse_result res;
960 6576093a 2022-07-11 op const char *fmt;
961 1d673950 2022-03-03 op int ctl_sock;
962 3baa2617 2022-02-16 op
963 6576093a 2022-07-11 op memset(&res, 0, sizeof(res));
964 6576093a 2022-07-11 op if ((fmt = getenv("AMUSED_STATUS_FORMAT")) == NULL)
965 99bf1536 2022-07-13 op fmt = "status,time:oneline,mode:oneline";
966 6576093a 2022-07-11 op res.status_format = fmt;
967 6576093a 2022-07-11 op
968 1eae758b 2022-07-13 op res.mode.consume = MODE_UNDEF;
969 1eae758b 2022-07-13 op res.mode.repeat_all = MODE_UNDEF;
970 1eae758b 2022-07-13 op res.mode.repeat_one = MODE_UNDEF;
971 1eae758b 2022-07-13 op
972 fea541a8 2022-02-16 op log_init(1, LOG_DAEMON);
973 fea541a8 2022-02-16 op log_setverbose(verbose);
974 fea541a8 2022-02-16 op
975 96233476 2022-06-09 op if (getcwd(cwd, sizeof(cwd)) == NULL)
976 96233476 2022-06-09 op fatal("getcwd");
977 96233476 2022-06-09 op
978 1d673950 2022-03-03 op if ((ctl_sock = ctl_connect()) == -1)
979 1d673950 2022-03-03 op fatal("can't connect");
980 fea541a8 2022-02-16 op
981 fea541a8 2022-02-16 op if (ctl_sock == -1)
982 fea541a8 2022-02-16 op fatalx("failed to connect to the daemon");
983 fea541a8 2022-02-16 op
984 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
985 3baa2617 2022-02-16 op imsg_init(ibuf, ctl_sock);
986 3baa2617 2022-02-16 op
987 3baa2617 2022-02-16 op optreset = 1;
988 3baa2617 2022-02-16 op optind = 1;
989 3baa2617 2022-02-16 op
990 4b42d3a3 2022-07-13 op /* we'll drop rpath too later in ctlaction */
991 4b42d3a3 2022-07-13 op if (pledge("stdio rpath", NULL) == -1)
992 4b42d3a3 2022-07-13 op fatal("pledge");
993 4b42d3a3 2022-07-13 op
994 6576093a 2022-07-11 op exit(parse(&res, argc, argv));
995 3baa2617 2022-02-16 op }