Blame


1 3baa2617 2022-02-16 op /*
2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 3baa2617 2022-02-16 op *
4 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
5 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
6 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
7 3baa2617 2022-02-16 op *
8 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 3baa2617 2022-02-16 op */
16 3baa2617 2022-02-16 op
17 3baa2617 2022-02-16 op #include <sys/types.h>
18 3baa2617 2022-02-16 op #include <sys/socket.h>
19 3baa2617 2022-02-16 op #include <sys/queue.h>
20 3baa2617 2022-02-16 op #include <sys/uio.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 3baa2617 2022-02-16 op #include <event.h>
25 3baa2617 2022-02-16 op #include <limits.h>
26 3baa2617 2022-02-16 op #include <sndio.h>
27 3baa2617 2022-02-16 op #include <stdio.h>
28 3baa2617 2022-02-16 op #include <stdlib.h>
29 3baa2617 2022-02-16 op #include <stdint.h>
30 3baa2617 2022-02-16 op #include <string.h>
31 3baa2617 2022-02-16 op #include <syslog.h>
32 3baa2617 2022-02-16 op #include <unistd.h>
33 3baa2617 2022-02-16 op #include <imsg.h>
34 3baa2617 2022-02-16 op
35 3baa2617 2022-02-16 op #include "amused.h"
36 3baa2617 2022-02-16 op #include "log.h"
37 bb3f279f 2022-02-16 op #include "playlist.h"
38 3baa2617 2022-02-16 op #include "xmalloc.h"
39 3baa2617 2022-02-16 op
40 3baa2617 2022-02-16 op static struct imsgbuf *ibuf;
41 3baa2617 2022-02-16 op
42 3baa2617 2022-02-16 op int ctl_noarg(struct parse_result *, int, char **);
43 3baa2617 2022-02-16 op int ctl_add(struct parse_result *, int, char **);
44 8e916714 2022-02-17 op int ctl_show(struct parse_result *, int, char **);
45 14be015f 2022-02-17 op int ctl_load(struct parse_result *, int, char **);
46 3baa2617 2022-02-16 op
47 3baa2617 2022-02-16 op struct ctl_command ctl_commands[] = {
48 3baa2617 2022-02-16 op { "play", PLAY, ctl_noarg, "" },
49 3baa2617 2022-02-16 op { "pause", PAUSE, ctl_noarg, "" },
50 3baa2617 2022-02-16 op { "toggle", TOGGLE, ctl_noarg, "" },
51 3baa2617 2022-02-16 op { "stop", STOP, ctl_noarg, "" },
52 3baa2617 2022-02-16 op { "restart", RESTART, ctl_noarg, "" },
53 3baa2617 2022-02-16 op { "add", ADD, ctl_add, "files...", 1 },
54 3baa2617 2022-02-16 op { "flush", FLUSH, ctl_noarg, "" },
55 8e916714 2022-02-17 op { "show", SHOW, ctl_show, "" },
56 bb3f279f 2022-02-16 op { "status", STATUS, ctl_noarg, "" },
57 af27e631 2022-02-17 op { "next", NEXT, ctl_noarg, "" },
58 af27e631 2022-02-17 op { "prev", PREV, ctl_noarg, "" },
59 14be015f 2022-02-17 op { "load", LOAD, ctl_load, "[file]", 1 },
60 3baa2617 2022-02-16 op { NULL },
61 3baa2617 2022-02-16 op };
62 3baa2617 2022-02-16 op
63 3baa2617 2022-02-16 op __dead void
64 3baa2617 2022-02-16 op usage(void)
65 3baa2617 2022-02-16 op {
66 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
67 3baa2617 2022-02-16 op fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
68 3baa2617 2022-02-16 op exit(1);
69 3baa2617 2022-02-16 op }
70 3baa2617 2022-02-16 op
71 3baa2617 2022-02-16 op static __dead void
72 3baa2617 2022-02-16 op ctl_usage(struct ctl_command *ctl)
73 3baa2617 2022-02-16 op {
74 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
75 3baa2617 2022-02-16 op ctl->name, ctl->usage);
76 3baa2617 2022-02-16 op exit(1);
77 3baa2617 2022-02-16 op }
78 3baa2617 2022-02-16 op
79 3baa2617 2022-02-16 op static int
80 3baa2617 2022-02-16 op parse(int argc, char **argv)
81 3baa2617 2022-02-16 op {
82 3baa2617 2022-02-16 op struct ctl_command *ctl = NULL;
83 3baa2617 2022-02-16 op struct parse_result res;
84 3baa2617 2022-02-16 op int i, status;
85 3baa2617 2022-02-16 op
86 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
87 3baa2617 2022-02-16 op
88 3baa2617 2022-02-16 op for (i = 0; ctl_commands[i].name != NULL; ++i) {
89 3baa2617 2022-02-16 op if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
90 3baa2617 2022-02-16 op == 0) {
91 3baa2617 2022-02-16 op if (ctl != NULL) {
92 3baa2617 2022-02-16 op fprintf(stderr,
93 3baa2617 2022-02-16 op "ambiguous argument: %s\n", argv[0]);
94 3baa2617 2022-02-16 op usage();
95 3baa2617 2022-02-16 op }
96 3baa2617 2022-02-16 op ctl = &ctl_commands[i];
97 3baa2617 2022-02-16 op }
98 3baa2617 2022-02-16 op }
99 3baa2617 2022-02-16 op
100 3baa2617 2022-02-16 op if (ctl == NULL) {
101 3baa2617 2022-02-16 op fprintf(stderr, "unknown argument: %s\n", argv[0]);
102 3baa2617 2022-02-16 op usage();
103 3baa2617 2022-02-16 op }
104 3baa2617 2022-02-16 op
105 3baa2617 2022-02-16 op res.action = ctl->action;
106 3baa2617 2022-02-16 op res.ctl = ctl;
107 3baa2617 2022-02-16 op
108 3baa2617 2022-02-16 op if (!ctl->has_pledge) {
109 3baa2617 2022-02-16 op /* pledge(2) default if command doesn't have its own */
110 3baa2617 2022-02-16 op if (pledge("stdio", NULL) == -1)
111 3baa2617 2022-02-16 op fatal("pledge");
112 3baa2617 2022-02-16 op }
113 3baa2617 2022-02-16 op
114 3baa2617 2022-02-16 op status = ctl->main(&res, argc, argv);
115 3baa2617 2022-02-16 op close(ibuf->fd);
116 3baa2617 2022-02-16 op free(ibuf);
117 3baa2617 2022-02-16 op return status;
118 3baa2617 2022-02-16 op }
119 3baa2617 2022-02-16 op
120 3baa2617 2022-02-16 op static int
121 3baa2617 2022-02-16 op enqueue_tracks(char **files)
122 3baa2617 2022-02-16 op {
123 3baa2617 2022-02-16 op char res[PATH_MAX];
124 3baa2617 2022-02-16 op int enq = 0;
125 3baa2617 2022-02-16 op
126 3baa2617 2022-02-16 op for (; *files != NULL; ++files) {
127 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
128 3baa2617 2022-02-16 op if (realpath(*files, res) == NULL) {
129 3baa2617 2022-02-16 op log_warn("realpath %s", *files);
130 3baa2617 2022-02-16 op continue;
131 3baa2617 2022-02-16 op }
132 3baa2617 2022-02-16 op
133 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
134 3baa2617 2022-02-16 op res, sizeof(res));
135 3baa2617 2022-02-16 op enq++;
136 3baa2617 2022-02-16 op }
137 3baa2617 2022-02-16 op
138 3baa2617 2022-02-16 op return enq == 0;
139 3baa2617 2022-02-16 op }
140 3baa2617 2022-02-16 op
141 8ff9231f 2022-02-16 op static void
142 8ff9231f 2022-02-16 op print_error_message(const char *prfx, struct imsg *imsg)
143 8ff9231f 2022-02-16 op {
144 8ff9231f 2022-02-16 op size_t datalen;
145 8ff9231f 2022-02-16 op char *msg;
146 8ff9231f 2022-02-16 op
147 8ff9231f 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
148 8ff9231f 2022-02-16 op if ((msg = calloc(1, datalen)) == NULL)
149 8ff9231f 2022-02-16 op fatal("calloc %zu", datalen);
150 8ff9231f 2022-02-16 op memcpy(msg, imsg->data, datalen);
151 8ff9231f 2022-02-16 op if (datalen == 0 || msg[datalen-1] != '\0')
152 8ff9231f 2022-02-16 op fatalx("malformed error message");
153 8ff9231f 2022-02-16 op
154 8ff9231f 2022-02-16 op log_warnx("%s: %s", prfx, msg);
155 8ff9231f 2022-02-16 op free(msg);
156 8ff9231f 2022-02-16 op }
157 8ff9231f 2022-02-16 op
158 3baa2617 2022-02-16 op static int
159 8ff9231f 2022-02-16 op show_add(struct imsg *imsg, int *ret, char ***files)
160 8ff9231f 2022-02-16 op {
161 8ff9231f 2022-02-16 op if (**files == NULL) {
162 8ff9231f 2022-02-16 op log_warnx("received more replies than file sent");
163 8ff9231f 2022-02-16 op *ret = 1;
164 8ff9231f 2022-02-16 op return 1;
165 8ff9231f 2022-02-16 op }
166 8ff9231f 2022-02-16 op
167 8ff9231f 2022-02-16 op if (imsg->hdr.type == IMSG_CTL_ERR)
168 8ff9231f 2022-02-16 op print_error_message(**files, imsg);
169 8ff9231f 2022-02-16 op else if (imsg->hdr.type == IMSG_CTL_ADD)
170 8ff9231f 2022-02-16 op log_debug("enqueued %s", **files);
171 8ff9231f 2022-02-16 op else
172 8ff9231f 2022-02-16 op fatalx("got invalid message %d", imsg->hdr.type);
173 8ff9231f 2022-02-16 op
174 8ff9231f 2022-02-16 op (*files)++;
175 8ff9231f 2022-02-16 op return (**files) == NULL;
176 8ff9231f 2022-02-16 op }
177 8ff9231f 2022-02-16 op
178 8ff9231f 2022-02-16 op static int
179 8e916714 2022-02-17 op show_complete(struct parse_result *res, struct imsg *imsg, int *ret)
180 3baa2617 2022-02-16 op {
181 63dd8e70 2022-02-17 op struct player_status s;
182 3baa2617 2022-02-16 op size_t datalen;
183 3baa2617 2022-02-16 op
184 8ff9231f 2022-02-16 op if (imsg->hdr.type == IMSG_CTL_ERR) {
185 8ff9231f 2022-02-16 op print_error_message("show failed", imsg);
186 8ff9231f 2022-02-16 op *ret = 1;
187 8ff9231f 2022-02-16 op return 1;
188 8ff9231f 2022-02-16 op }
189 8ff9231f 2022-02-16 op
190 3baa2617 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
191 3baa2617 2022-02-16 op if (datalen == 0)
192 3baa2617 2022-02-16 op return 1;
193 3baa2617 2022-02-16 op
194 63dd8e70 2022-02-17 op if (datalen != sizeof(s))
195 3baa2617 2022-02-16 op fatalx("%s: data size mismatch", __func__);
196 63dd8e70 2022-02-17 op memcpy(&s, imsg->data, sizeof(s));
197 63dd8e70 2022-02-17 op if (s.path[sizeof(s.path)-1] != '\0')
198 3baa2617 2022-02-16 op fatalx("%s: data corrupted?", __func__);
199 3baa2617 2022-02-16 op
200 8e916714 2022-02-17 op if (res->pretty)
201 8e916714 2022-02-17 op printf("%c ", s.status == STATE_PLAYING ? '>' : ' ');
202 8e916714 2022-02-17 op printf("%s\n", s.path);
203 3baa2617 2022-02-16 op return 0;
204 bb3f279f 2022-02-16 op }
205 bb3f279f 2022-02-16 op
206 bb3f279f 2022-02-16 op static int
207 bb3f279f 2022-02-16 op show_status(struct imsg *imsg, int *ret)
208 bb3f279f 2022-02-16 op {
209 bb3f279f 2022-02-16 op struct player_status s;
210 bb3f279f 2022-02-16 op size_t datalen;
211 bb3f279f 2022-02-16 op
212 bb3f279f 2022-02-16 op if (imsg->hdr.type == IMSG_CTL_ERR) {
213 bb3f279f 2022-02-16 op print_error_message("show failed", imsg);
214 bb3f279f 2022-02-16 op *ret = 1;
215 bb3f279f 2022-02-16 op return 1;
216 bb3f279f 2022-02-16 op }
217 bb3f279f 2022-02-16 op
218 bb3f279f 2022-02-16 op if (imsg->hdr.type != IMSG_CTL_STATUS)
219 bb3f279f 2022-02-16 op fatalx("%s: got wrong reply", __func__);
220 bb3f279f 2022-02-16 op
221 bb3f279f 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
222 bb3f279f 2022-02-16 op if (datalen != sizeof(s))
223 bb3f279f 2022-02-16 op fatalx("%s: data size mismatch", __func__);
224 bb3f279f 2022-02-16 op memcpy(&s, imsg->data, sizeof(s));
225 bb3f279f 2022-02-16 op if (s.path[sizeof(s.path)-1] != '\0')
226 bb3f279f 2022-02-16 op fatalx("%s: data corrupted?", __func__);
227 bb3f279f 2022-02-16 op
228 bb3f279f 2022-02-16 op switch (s.status) {
229 bb3f279f 2022-02-16 op case STATE_STOPPED:
230 bb3f279f 2022-02-16 op printf("stopped ");
231 bb3f279f 2022-02-16 op break;
232 bb3f279f 2022-02-16 op case STATE_PLAYING:
233 bb3f279f 2022-02-16 op printf("playing ");
234 bb3f279f 2022-02-16 op break;
235 bb3f279f 2022-02-16 op case STATE_PAUSED:
236 bb3f279f 2022-02-16 op printf("paused ");
237 bb3f279f 2022-02-16 op break;
238 bb3f279f 2022-02-16 op default:
239 bb3f279f 2022-02-16 op printf("unknown ");
240 bb3f279f 2022-02-16 op break;
241 bb3f279f 2022-02-16 op }
242 bb3f279f 2022-02-16 op
243 bb3f279f 2022-02-16 op printf("%s\n", s.path);
244 bb3f279f 2022-02-16 op return 1;
245 7a427ecd 2022-02-17 op }
246 7a427ecd 2022-02-17 op
247 7a427ecd 2022-02-17 op static int
248 7a427ecd 2022-02-17 op show_load(struct parse_result *res, struct imsg *imsg, int *ret)
249 7a427ecd 2022-02-17 op {
250 ec1fb0c7 2022-02-17 op FILE *f;
251 7a427ecd 2022-02-17 op const char *file;
252 7a427ecd 2022-02-17 op char *line = NULL;
253 7a427ecd 2022-02-17 op char path[PATH_MAX];
254 7a427ecd 2022-02-17 op size_t linesize = 0;
255 7a427ecd 2022-02-17 op ssize_t linelen;
256 7a427ecd 2022-02-17 op int any = 0;
257 7a427ecd 2022-02-17 op
258 7a427ecd 2022-02-17 op if (imsg->hdr.type == IMSG_CTL_ERR) {
259 7a427ecd 2022-02-17 op print_error_message("load failed", imsg);
260 7a427ecd 2022-02-17 op *ret = 1;
261 7a427ecd 2022-02-17 op return 1;
262 7a427ecd 2022-02-17 op }
263 7a427ecd 2022-02-17 op
264 7a427ecd 2022-02-17 op if (imsg->hdr.type == IMSG_CTL_ADD)
265 7a427ecd 2022-02-17 op return 0;
266 7a427ecd 2022-02-17 op
267 7a427ecd 2022-02-17 op if (imsg->hdr.type == IMSG_CTL_COMMIT)
268 7a427ecd 2022-02-17 op return 1;
269 7a427ecd 2022-02-17 op
270 7a427ecd 2022-02-17 op if (imsg->hdr.type != IMSG_CTL_BEGIN)
271 7a427ecd 2022-02-17 op fatalx("got unexpected message %d", imsg->hdr.type);
272 7a427ecd 2022-02-17 op
273 ec1fb0c7 2022-02-17 op if (res->file == NULL)
274 ec1fb0c7 2022-02-17 op f = stdin;
275 ec1fb0c7 2022-02-17 op else if ((f = fopen(res->file, "r")) == NULL) {
276 ec1fb0c7 2022-02-17 op log_warn("can't open %s", res->file);
277 ec1fb0c7 2022-02-17 op *ret = 1;
278 ec1fb0c7 2022-02-17 op return 1;
279 ec1fb0c7 2022-02-17 op }
280 ec1fb0c7 2022-02-17 op
281 ec1fb0c7 2022-02-17 op while ((linelen = getline(&line, &linesize, f)) != -1) {
282 7a427ecd 2022-02-17 op if (linelen == 0)
283 7a427ecd 2022-02-17 op continue;
284 7a427ecd 2022-02-17 op line[linelen-1] = '\0';
285 7a427ecd 2022-02-17 op file = line;
286 7a427ecd 2022-02-17 op if (file[0] == '>' && file[1] == ' ')
287 7a427ecd 2022-02-17 op file += 2;
288 7a427ecd 2022-02-17 op if (file[0] == ' ' && file[1] == ' ')
289 7a427ecd 2022-02-17 op file += 2;
290 7a427ecd 2022-02-17 op
291 7a427ecd 2022-02-17 op memset(&path, 0, sizeof(path));
292 7a427ecd 2022-02-17 op if (realpath(file, path) == NULL) {
293 7a427ecd 2022-02-17 op log_warn("realpath %s", file);
294 7a427ecd 2022-02-17 op continue;
295 7a427ecd 2022-02-17 op }
296 7a427ecd 2022-02-17 op
297 7a427ecd 2022-02-17 op any++;
298 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
299 7a427ecd 2022-02-17 op path, sizeof(path));
300 7a427ecd 2022-02-17 op }
301 7a427ecd 2022-02-17 op
302 7a427ecd 2022-02-17 op free(line);
303 ec1fb0c7 2022-02-17 op if (ferror(f))
304 7a427ecd 2022-02-17 op fatal("getline");
305 ec1fb0c7 2022-02-17 op fclose(f);
306 7a427ecd 2022-02-17 op
307 7a427ecd 2022-02-17 op if (!any) {
308 7a427ecd 2022-02-17 op *ret = 1;
309 7a427ecd 2022-02-17 op return 1;
310 7a427ecd 2022-02-17 op }
311 7a427ecd 2022-02-17 op
312 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
313 7a427ecd 2022-02-17 op NULL, 0);
314 7a427ecd 2022-02-17 op imsg_flush(ibuf);
315 7a427ecd 2022-02-17 op return 0;
316 3baa2617 2022-02-16 op }
317 3baa2617 2022-02-16 op
318 3baa2617 2022-02-16 op static int
319 3baa2617 2022-02-16 op ctlaction(struct parse_result *res)
320 3baa2617 2022-02-16 op {
321 3baa2617 2022-02-16 op struct imsg imsg;
322 3baa2617 2022-02-16 op ssize_t n;
323 3baa2617 2022-02-16 op int ret = 0, done = 1;
324 8ff9231f 2022-02-16 op char **files;
325 3baa2617 2022-02-16 op
326 3baa2617 2022-02-16 op switch (res->action) {
327 3baa2617 2022-02-16 op case PLAY:
328 146307bd 2022-02-17 op done = 0;
329 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
330 146307bd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
331 3baa2617 2022-02-16 op break;
332 3baa2617 2022-02-16 op case PAUSE:
333 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
334 3baa2617 2022-02-16 op break;
335 3baa2617 2022-02-16 op case TOGGLE:
336 146307bd 2022-02-17 op done = 0;
337 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
338 146307bd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
339 3baa2617 2022-02-16 op break;
340 3baa2617 2022-02-16 op case STOP:
341 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
342 3baa2617 2022-02-16 op break;
343 3baa2617 2022-02-16 op case RESTART:
344 146307bd 2022-02-17 op done = 0;
345 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
346 146307bd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
347 3baa2617 2022-02-16 op break;
348 3baa2617 2022-02-16 op case ADD:
349 8ff9231f 2022-02-16 op done = 0;
350 8ff9231f 2022-02-16 op files = res->files;
351 3baa2617 2022-02-16 op ret = enqueue_tracks(res->files);
352 3baa2617 2022-02-16 op break;
353 3baa2617 2022-02-16 op case FLUSH:
354 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
355 3baa2617 2022-02-16 op break;
356 3baa2617 2022-02-16 op case SHOW:
357 3baa2617 2022-02-16 op done = 0;
358 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
359 3baa2617 2022-02-16 op break;
360 bb3f279f 2022-02-16 op case STATUS:
361 bb3f279f 2022-02-16 op done = 0;
362 bb3f279f 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
363 af27e631 2022-02-17 op break;
364 af27e631 2022-02-17 op case NEXT:
365 146307bd 2022-02-17 op done = 0;
366 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
367 146307bd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
368 bb3f279f 2022-02-16 op break;
369 af27e631 2022-02-17 op case PREV:
370 146307bd 2022-02-17 op done = 0;
371 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
372 146307bd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
373 af27e631 2022-02-17 op break;
374 14be015f 2022-02-17 op case LOAD:
375 7a427ecd 2022-02-17 op done = 0;
376 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
377 7a427ecd 2022-02-17 op break;
378 3baa2617 2022-02-16 op case NONE:
379 3baa2617 2022-02-16 op /* action not expected */
380 3baa2617 2022-02-16 op fatalx("invalid action %u", res->action);
381 3baa2617 2022-02-16 op break;
382 3baa2617 2022-02-16 op }
383 3baa2617 2022-02-16 op
384 3baa2617 2022-02-16 op if (ret != 0)
385 3baa2617 2022-02-16 op goto end;
386 3baa2617 2022-02-16 op
387 3baa2617 2022-02-16 op imsg_flush(ibuf);
388 3baa2617 2022-02-16 op
389 3baa2617 2022-02-16 op while (!done) {
390 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
391 3baa2617 2022-02-16 op fatalx("imsg_read error");
392 3baa2617 2022-02-16 op if (n == 0)
393 3baa2617 2022-02-16 op fatalx("pipe closed");
394 3baa2617 2022-02-16 op
395 3baa2617 2022-02-16 op while (!done) {
396 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
397 3baa2617 2022-02-16 op fatalx("imsg_get error");
398 3baa2617 2022-02-16 op if (n == 0)
399 3baa2617 2022-02-16 op break;
400 3baa2617 2022-02-16 op
401 3baa2617 2022-02-16 op switch (res->action) {
402 8ff9231f 2022-02-16 op case ADD:
403 8ff9231f 2022-02-16 op done = show_add(&imsg, &ret, &files);
404 8ff9231f 2022-02-16 op break;
405 3baa2617 2022-02-16 op case SHOW:
406 8e916714 2022-02-17 op done = show_complete(res, &imsg, &ret);
407 3baa2617 2022-02-16 op break;
408 146307bd 2022-02-17 op case PLAY:
409 146307bd 2022-02-17 op case TOGGLE:
410 146307bd 2022-02-17 op case RESTART:
411 bb3f279f 2022-02-16 op case STATUS:
412 146307bd 2022-02-17 op case NEXT:
413 146307bd 2022-02-17 op case PREV:
414 bb3f279f 2022-02-16 op done = show_status(&imsg, &ret);
415 bb3f279f 2022-02-16 op break;
416 7a427ecd 2022-02-17 op case LOAD:
417 7a427ecd 2022-02-17 op done = show_load(res, &imsg, &ret);
418 7a427ecd 2022-02-17 op break;
419 3baa2617 2022-02-16 op default:
420 3baa2617 2022-02-16 op done = 1;
421 3baa2617 2022-02-16 op break;
422 3baa2617 2022-02-16 op }
423 3baa2617 2022-02-16 op
424 3baa2617 2022-02-16 op imsg_free(&imsg);
425 3baa2617 2022-02-16 op }
426 3baa2617 2022-02-16 op }
427 3baa2617 2022-02-16 op
428 3baa2617 2022-02-16 op end:
429 3baa2617 2022-02-16 op return ret;
430 3baa2617 2022-02-16 op }
431 3baa2617 2022-02-16 op
432 3baa2617 2022-02-16 op int
433 3baa2617 2022-02-16 op ctl_noarg(struct parse_result *res, int argc, char **argv)
434 3baa2617 2022-02-16 op {
435 3baa2617 2022-02-16 op if (argc != 1)
436 3baa2617 2022-02-16 op ctl_usage(res->ctl);
437 3baa2617 2022-02-16 op return ctlaction(res);
438 3baa2617 2022-02-16 op }
439 3baa2617 2022-02-16 op
440 3baa2617 2022-02-16 op int
441 3baa2617 2022-02-16 op ctl_add(struct parse_result *res, int argc, char **argv)
442 3baa2617 2022-02-16 op {
443 3baa2617 2022-02-16 op if (argc < 2)
444 3baa2617 2022-02-16 op ctl_usage(res->ctl);
445 3baa2617 2022-02-16 op res->files = argv+1;
446 14be015f 2022-02-17 op
447 14be015f 2022-02-17 op if (pledge("stdio rpath", NULL) == -1)
448 14be015f 2022-02-17 op fatal("pledge");
449 14be015f 2022-02-17 op
450 3baa2617 2022-02-16 op return ctlaction(res);
451 3baa2617 2022-02-16 op }
452 14be015f 2022-02-17 op
453 14be015f 2022-02-17 op int
454 8e916714 2022-02-17 op ctl_show(struct parse_result *res, int argc, char **argv)
455 8e916714 2022-02-17 op {
456 8e916714 2022-02-17 op int ch;
457 8e916714 2022-02-17 op
458 8e916714 2022-02-17 op while ((ch = getopt(argc, argv, "p")) != -1) {
459 8e916714 2022-02-17 op switch (ch) {
460 8e916714 2022-02-17 op case 'p':
461 8e916714 2022-02-17 op res->pretty = 1;
462 8e916714 2022-02-17 op break;
463 8e916714 2022-02-17 op default:
464 8e916714 2022-02-17 op ctl_usage(res->ctl);
465 8e916714 2022-02-17 op }
466 8e916714 2022-02-17 op }
467 8e916714 2022-02-17 op
468 8e916714 2022-02-17 op return ctlaction(res);
469 8e916714 2022-02-17 op }
470 8e916714 2022-02-17 op
471 8e916714 2022-02-17 op int
472 14be015f 2022-02-17 op ctl_load(struct parse_result *res, int argc, char **argv)
473 14be015f 2022-02-17 op {
474 14be015f 2022-02-17 op if (argc < 2)
475 ec1fb0c7 2022-02-17 op res->file = NULL;
476 ec1fb0c7 2022-02-17 op else if (argc == 2)
477 ec1fb0c7 2022-02-17 op res->file = argv[1];
478 ec1fb0c7 2022-02-17 op else
479 14be015f 2022-02-17 op ctl_usage(res->ctl);
480 14be015f 2022-02-17 op
481 14be015f 2022-02-17 op if (pledge("stdio rpath", NULL) == -1)
482 14be015f 2022-02-17 op fatal("pledge");
483 14be015f 2022-02-17 op
484 7a427ecd 2022-02-17 op return ctlaction(res);
485 14be015f 2022-02-17 op }
486 14be015f 2022-02-17 op
487 fea541a8 2022-02-16 op static int
488 fea541a8 2022-02-16 op sockconn(void)
489 3baa2617 2022-02-16 op {
490 fea541a8 2022-02-16 op struct sockaddr_un sun;
491 fea541a8 2022-02-16 op int sock, saved_errno;
492 3baa2617 2022-02-16 op
493 fea541a8 2022-02-16 op if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
494 3baa2617 2022-02-16 op fatal("socket");
495 3baa2617 2022-02-16 op
496 3baa2617 2022-02-16 op memset(&sun, 0, sizeof(sun));
497 3baa2617 2022-02-16 op sun.sun_family = AF_UNIX;
498 3baa2617 2022-02-16 op strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
499 fea541a8 2022-02-16 op if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
500 fea541a8 2022-02-16 op saved_errno = errno;
501 fea541a8 2022-02-16 op close(sock);
502 fea541a8 2022-02-16 op errno = saved_errno;
503 fea541a8 2022-02-16 op return -1;
504 fea541a8 2022-02-16 op }
505 3baa2617 2022-02-16 op
506 fea541a8 2022-02-16 op return sock;
507 fea541a8 2022-02-16 op }
508 fea541a8 2022-02-16 op
509 fea541a8 2022-02-16 op __dead void
510 fea541a8 2022-02-16 op ctl(int argc, char **argv)
511 fea541a8 2022-02-16 op {
512 fea541a8 2022-02-16 op int ctl_sock, i = 0;
513 3baa2617 2022-02-16 op
514 fea541a8 2022-02-16 op log_init(1, LOG_DAEMON);
515 fea541a8 2022-02-16 op log_setverbose(verbose);
516 fea541a8 2022-02-16 op
517 fea541a8 2022-02-16 op do {
518 fea541a8 2022-02-16 op struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
519 fea541a8 2022-02-16 op
520 fea541a8 2022-02-16 op if ((ctl_sock = sockconn()) != -1)
521 fea541a8 2022-02-16 op break;
522 fea541a8 2022-02-16 op if (errno != ENOENT && errno != ECONNREFUSED)
523 fea541a8 2022-02-16 op fatal("connect %s", csock);
524 fea541a8 2022-02-16 op
525 fea541a8 2022-02-16 op if (i == 0)
526 fea541a8 2022-02-16 op spawn_daemon();
527 fea541a8 2022-02-16 op
528 fea541a8 2022-02-16 op nanosleep(&ts, NULL);
529 fea541a8 2022-02-16 op } while (++i < 20);
530 fea541a8 2022-02-16 op
531 fea541a8 2022-02-16 op if (ctl_sock == -1)
532 fea541a8 2022-02-16 op fatalx("failed to connect to the daemon");
533 fea541a8 2022-02-16 op
534 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
535 3baa2617 2022-02-16 op imsg_init(ibuf, ctl_sock);
536 3baa2617 2022-02-16 op
537 3baa2617 2022-02-16 op optreset = 1;
538 3baa2617 2022-02-16 op optind = 1;
539 3baa2617 2022-02-16 op
540 3baa2617 2022-02-16 op exit(parse(argc, argv));
541 3baa2617 2022-02-16 op }