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