1 /*
2 *
3 * libmpdclient
4 * (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com)
5 * This project's homepage is: http://www.musicpd.org
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * - Neither the name of the Music Player Daemon nor the names of its
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35
36 #include "libmpdclient.h"
37 #include "conky.h"
38
39 #include <fcntl.h>
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <unistd.h>
43 #include <cctype>
44 #include <cerrno>
45 #include <climits>
46
47 #ifdef WIN32
48 #include <winsock.h>
49 #include <ws2tcpip.h>
50 #else
51 #include <arpa/inet.h>
52 #include <netdb.h>
53 #include <netinet/in.h>
54 #include <sys/socket.h>
55 #include <sys/un.h>
56 #endif
57
58 #ifndef SUN_LEN
59 #define SUN_LEN(a) sizeof(a)
60 #endif
61
62 #ifndef SOCK_CLOEXEC
63 #define SOCK_CLOEXEC O_CLOEXEC
64 #endif /* SOCK_CLOEXEC */
65
66 /* (bits + 1) / 3 (plus the sign character) */
67 #define INTLEN ((sizeof(int) * CHAR_BIT + 1) / 3 + 1)
68 #define LONGLONGLEN ((sizeof(long long) * CHAR_BIT + 1) / 3 + 1)
69
70 #define COMMAND_LIST 1
71 #define COMMAND_LIST_OK 2
72
73 #ifndef MPD_NO_GAI
74 #ifdef AI_ADDRCONFIG
75 #define MPD_HAVE_GAI
76 #endif
77 #endif
78
79 #ifndef MSG_DONTWAIT
80 #define MSG_DONTWAIT 0
81 #endif
82
83 #ifdef WIN32
84 #define SELECT_ERRNO_IGNORE (errno == WSAEINTR || errno == WSAEINPROGRESS)
85 #define SENDRECV_ERRNO_IGNORE SELECT_ERRNO_IGNORE
86 #else
87 #define SELECT_ERRNO_IGNORE (errno == EINTR)
88 #define SENDRECV_ERRNO_IGNORE (errno == EINTR || errno == EAGAIN)
89 #define winsock_dll_error(c) 0
90 #define closesocket(s) close(s)
91 #define WSACleanup() \
92 do { /* nothing */ \
93 } while (0)
94 #endif
95
96 #ifdef WIN32
winsock_dll_error(mpd_Connection * connection)97 static int winsock_dll_error(mpd_Connection *connection) {
98 WSADATA wsaData;
99
100 if ((WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0 ||
101 LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
102 strncpy(connection->errorStr, "Could not find usable WinSock DLL.",
103 MPD_ERRORSTR_MAX_LENGTH);
104 connection->error = MPD_ERROR_SYSTEM;
105 return 1;
106 }
107 return 0;
108 }
109
do_connect_fail(mpd_Connection * connection,const struct sockaddr * serv_addr,int addrlen)110 static int do_connect_fail(mpd_Connection *connection,
111 const struct sockaddr *serv_addr, int addrlen) {
112 int iMode = 1; /* 0 = blocking, else non-blocking */
113
114 ioctlsocket(connection->sock, FIONBIO, (u_long FAR *)&iMode);
115 return (connect(connection->sock, serv_addr, addrlen) == SOCKET_ERROR &&
116 WSAGetLastError() != WSAEWOULDBLOCK);
117 }
118 #else /* !WIN32 (sane operating systems) */
do_connect_fail(mpd_Connection * connection,const struct sockaddr * serv_addr,int addrlen)119 static int do_connect_fail(mpd_Connection *connection,
120 const struct sockaddr *serv_addr, int addrlen) {
121 int flags = fcntl(connection->sock, F_GETFL, 0);
122
123 fcntl(connection->sock, F_SETFL, flags | O_NONBLOCK);
124 return static_cast<int>(connect(connection->sock, serv_addr, addrlen) < 0 &&
125 errno != EINPROGRESS);
126 }
127 #endif /* !WIN32 */
128
uds_connect(mpd_Connection * connection,const char * host,float timeout)129 static int uds_connect(mpd_Connection *connection, const char *host,
130 float timeout) {
131 struct sockaddr_un addr {};
132
133 strncpy(addr.sun_path, host, sizeof(addr.sun_path) - 1);
134 addr.sun_family = AF_UNIX;
135 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
136 connection->sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
137
138 if (connection->sock < 0) {
139 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
140 "problems creating socket: %s", strerror(errno));
141 connection->error = MPD_ERROR_SYSTEM;
142 return -1;
143 }
144
145 mpd_setConnectionTimeout(connection, timeout);
146
147 /* connect stuff */
148 if (do_connect_fail(connection, reinterpret_cast<struct sockaddr *>(&addr),
149 SUN_LEN(&addr)) != 0) {
150 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
151 "problems connecting socket: %s", strerror(errno));
152 closesocket(connection->sock);
153 connection->sock = -1;
154 connection->error = MPD_ERROR_SYSTEM;
155 return -1;
156 }
157
158 return 0;
159 }
160
161 #ifdef MPD_HAVE_GAI
mpd_connect(mpd_Connection * connection,const char * host,int port,float timeout)162 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
163 float timeout) {
164 int error;
165 char service[INTLEN + 1];
166 struct addrinfo hints {};
167 struct addrinfo *res = nullptr;
168 struct addrinfo *addrinfo = nullptr;
169
170 if (*host == '/') { return uds_connect(connection, host, timeout); }
171
172 /* Setup hints */
173 hints.ai_flags = AI_ADDRCONFIG;
174 hints.ai_family = AF_UNSPEC;
175 hints.ai_socktype = SOCK_STREAM;
176 hints.ai_protocol = IPPROTO_TCP;
177 hints.ai_addrlen = 0;
178 hints.ai_addr = nullptr;
179 hints.ai_canonname = nullptr;
180 hints.ai_next = nullptr;
181
182 snprintf(service, sizeof(service), "%i", port);
183
184 error = getaddrinfo(host, service, &hints, &addrinfo);
185
186 if (error != 0) {
187 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
188 "host \"%s\" not found: %s", host, gai_strerror(error));
189 connection->error = MPD_ERROR_UNKHOST;
190 return -1;
191 }
192
193 for (res = addrinfo; res != nullptr; res = res->ai_next) {
194 /* create socket */
195 if (connection->sock > -1) { closesocket(connection->sock); }
196 connection->sock =
197 socket(res->ai_family, SOCK_STREAM | SOCK_CLOEXEC, res->ai_protocol);
198 if (connection->sock < 0) {
199 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
200 "problems creating socket: %s", strerror(errno));
201 connection->error = MPD_ERROR_SYSTEM;
202 freeaddrinfo(addrinfo);
203 return -1;
204 }
205
206 mpd_setConnectionTimeout(connection, timeout);
207
208 /* connect stuff */
209 if (do_connect_fail(connection, res->ai_addr, res->ai_addrlen) != 0) {
210 /* try the next address family */
211 closesocket(connection->sock);
212 connection->sock = -1;
213 continue;
214 }
215 }
216
217 freeaddrinfo(addrinfo);
218
219 if (connection->sock < 0) {
220 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
221 "problems connecting to \"%s\" on port %i: %s", host, port,
222 strerror(errno));
223 connection->error = MPD_ERROR_CONNPORT;
224
225 return -1;
226 }
227
228 return 0;
229 }
230 #else /* !MPD_HAVE_GAI */
mpd_connect(mpd_Connection * connection,const char * host,int port,float timeout)231 static int mpd_connect(mpd_Connection *connection, const char *host, int port,
232 float timeout) {
233 struct hostent he, *he_res = 0;
234 int he_errno;
235 char hostbuff[2048];
236 struct sockaddr *dest;
237 int destlen;
238 struct sockaddr_in sin;
239
240 if (*host == '/') return uds_connect(connection, host, timeout);
241
242 #ifdef HAVE_GETHOSTBYNAME_R
243 if (gethostbyname_r(host, &he, hostbuff, sizeof(hostbuff), &he_res,
244 &he_errno)) { // get the host info
245 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH, "%s ('%s')",
246 hstrerror(h_errno), host);
247 connection->error = MPD_ERROR_UNKHOST;
248 return -1;
249 }
250 #else /* HAVE_GETHOSTBYNAME_R */
251 if (!(he_res = gethostbyname(host))) {
252 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
253 "host \"%s\" not found", host);
254 connection->error = MPD_ERROR_UNKHOST;
255 return -1;
256 }
257 #endif /* HAVE_GETHOSTBYNAME_R */
258
259 memset(&sin, 0, sizeof(struct sockaddr_in));
260 /* dest.sin_family = he_res->h_addrtype; */
261 sin.sin_family = AF_INET;
262 sin.sin_port = htons(port);
263
264 switch (he_res->h_addrtype) {
265 case AF_INET:
266 memcpy((char *)&sin.sin_addr.s_addr, (char *)he_res->h_addr,
267 he_res->h_length);
268 dest = (struct sockaddr *)&sin;
269 destlen = sizeof(struct sockaddr_in);
270 break;
271 default:
272 strncpy(connection->errorStr, "address type is not IPv4",
273 MPD_ERRORSTR_MAX_LENGTH);
274 connection->error = MPD_ERROR_SYSTEM;
275 return -1;
276 break;
277 }
278
279 if (connection->sock > -1) { closesocket(connection->sock); }
280 if ((connection->sock = socket(dest->sa_family, SOCK_STREAM, 0)) < 0) {
281 strncpy(connection->errorStr, "problems creating socket",
282 MPD_ERRORSTR_MAX_LENGTH);
283 connection->error = MPD_ERROR_SYSTEM;
284 return -1;
285 }
286
287 mpd_setConnectionTimeout(connection, timeout);
288
289 /* connect stuff */
290 if (do_connect_fail(connection, dest, destlen)) {
291 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
292 "problems connecting to \"%s\" on port %i", host, port);
293 connection->error = MPD_ERROR_CONNPORT;
294 return -1;
295 }
296
297 return 0;
298 }
299 #endif /* !MPD_HAVE_GAI */
300
301 const char *mpdTagItemKeys[MPD_TAG_NUM_OF_ITEM_TYPES] = {
302 "Artist", "Album", "Title", "Track", "Name", "Genre", "Date",
303 "Composer", "Performer", "Comment", "Disc", "Filename", "Any"};
304
mpd_sanitizeArg(const char * arg)305 static char *mpd_sanitizeArg(const char *arg) {
306 size_t i;
307 char *ret;
308 const char *c;
309 char *rc;
310
311 /* instead of counting in that loop above,
312 * just use a bit more memory and halve running time */
313 ret = static_cast<char *>(malloc(strlen(arg) * 2 + 1));
314
315 c = arg;
316 rc = ret;
317 for (i = strlen(arg) + 1; i != 0; --i) {
318 if (*c == '"' || *c == '\\') { *rc++ = '\\'; }
319 *(rc++) = *(c++);
320 }
321
322 return ret;
323 }
324
mpd_newReturnElement(const char * name,const char * value)325 static mpd_ReturnElement *mpd_newReturnElement(const char *name,
326 const char *value) {
327 auto *ret =
328 static_cast<mpd_ReturnElement *>(malloc(sizeof(mpd_ReturnElement)));
329
330 ret->name = strndup(name, text_buffer_size.get(*state));
331 ret->value = strndup(value, text_buffer_size.get(*state));
332
333 return ret;
334 }
335
mpd_freeReturnElement(mpd_ReturnElement * re)336 static void mpd_freeReturnElement(mpd_ReturnElement *re) {
337 free(re->name);
338 free(re->value);
339 free(re);
340 }
341
mpd_setConnectionTimeout(mpd_Connection * connection,float timeout)342 void mpd_setConnectionTimeout(mpd_Connection *connection, float timeout) {
343 connection->timeout.tv_sec = static_cast<int>(timeout);
344 connection->timeout.tv_usec =
345 static_cast<int>((timeout - connection->timeout.tv_sec) * 1e6 + 0.5);
346 }
347
mpd_parseWelcome(mpd_Connection * connection,const char * host,int port,char * output)348 static int mpd_parseWelcome(mpd_Connection *connection, const char *host,
349 int port, /* char *rt, */ char *output) {
350 char *tmp;
351 char *test;
352 int i;
353
354 if (static_cast<int>(strncmp(output, MPD_WELCOME_MESSAGE,
355 strlen(MPD_WELCOME_MESSAGE)) != 0) != 0) {
356 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
357 "mpd not running on port %i on host \"%s\"", port, host);
358 connection->error = MPD_ERROR_NOTMPD;
359 return 1;
360 }
361
362 tmp = &output[strlen(MPD_WELCOME_MESSAGE)];
363
364 for (i = 0; i < 3; i++) {
365 if (tmp != nullptr) { connection->version[i] = strtol(tmp, &test, 10); }
366
367 if ((tmp == nullptr) || (test[0] != '.' && test[0] != '\0')) {
368 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
369 "error parsing version number at \"%s\"",
370 &output[strlen(MPD_WELCOME_MESSAGE)]);
371 connection->error = MPD_ERROR_NOTMPD;
372 return 1;
373 }
374 tmp = ++test;
375 }
376
377 return 0;
378 }
379
mpd_newConnection(const char * host,int port,float timeout)380 mpd_Connection *mpd_newConnection(const char *host, int port, float timeout) {
381 int err;
382 char *rt;
383 char *output = nullptr;
384 auto *connection =
385 static_cast<mpd_Connection *>(malloc(sizeof(mpd_Connection)));
386 struct timeval tv {};
387 fd_set fds;
388
389 strncpy(connection->buffer, "", 1);
390 connection->buflen = 0;
391 connection->bufstart = 0;
392 strncpy(connection->errorStr, "", MPD_ERRORSTR_MAX_LENGTH);
393 connection->error = 0;
394 connection->doneProcessing = 0;
395 connection->commandList = 0;
396 connection->listOks = 0;
397 connection->doneListOk = 0;
398 connection->sock = -1;
399 connection->returnElement = nullptr;
400 connection->request = nullptr;
401
402 if (winsock_dll_error(connection)) { return connection; }
403
404 if (mpd_connect(connection, host, port, timeout) < 0) { return connection; }
405
406 while ((rt = strstr(connection->buffer, "\n")) == nullptr) {
407 tv.tv_sec = connection->timeout.tv_sec;
408 tv.tv_usec = connection->timeout.tv_usec;
409 FD_ZERO(&fds);
410 FD_SET(connection->sock, &fds);
411 if ((err = select(connection->sock + 1, &fds, nullptr, nullptr, &tv)) ==
412 1) {
413 int readed;
414
415 readed = recv(connection->sock, &(connection->buffer[connection->buflen]),
416 MPD_BUFFER_MAX_LENGTH - connection->buflen, 0);
417 if (readed <= 0) {
418 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
419 "problems getting a response from \"%s\" on port %i : %s",
420 host, port, strerror(errno));
421 connection->error = MPD_ERROR_NORESPONSE;
422 return connection;
423 }
424 connection->buflen += readed;
425 connection->buffer[connection->buflen] = '\0';
426 } else if (err < 0) {
427 if SELECT_ERRNO_IGNORE { continue; }
428 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
429 "problems connecting to \"%s\" on port %i", host, port);
430 connection->error = MPD_ERROR_CONNPORT;
431 return connection;
432 } else {
433 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
434 "timeout in attempting to get a response from \"%s\" on "
435 "port %i",
436 host, port);
437 connection->error = MPD_ERROR_NORESPONSE;
438 return connection;
439 }
440 }
441
442 *rt = '\0';
443 output = strndup(connection->buffer, text_buffer_size.get(*state));
444 strncpy(connection->buffer, rt + 1, MPD_BUFFER_MAX_LENGTH);
445 connection->buflen = strlen(connection->buffer);
446
447 if (mpd_parseWelcome(connection, host, port, /* rt, */ output) == 0) {
448 connection->doneProcessing = 1;
449 }
450
451 free(output);
452
453 return connection;
454 }
455
456 void mpd_clearError(mpd_Connection *connection) {
457 connection->error = 0;
458 connection->errorStr[0] = '\0';
459 }
460
461 void mpd_closeConnection(mpd_Connection *connection) {
462 if (connection != nullptr) {
463 closesocket(connection->sock);
464 free_and_zero(connection->returnElement);
465 free_and_zero(connection->request);
466 free(connection);
467 }
468 WSACleanup();
469 }
470
471 static void mpd_executeCommand(mpd_Connection *connection,
472 const char *command) {
473 int ret;
474 struct timeval tv {};
475 fd_set fds;
476 const char *commandPtr = command;
477 int commandLen = strlen(command);
478
479 if ((connection->doneProcessing == 0) && (connection->commandList == 0)) {
480 strncpy(connection->errorStr, "not done processing current command",
481 MPD_ERRORSTR_MAX_LENGTH);
482 connection->error = 1;
483 return;
484 }
485
486 mpd_clearError(connection);
487
488 FD_ZERO(&fds);
489 FD_SET(connection->sock, &fds);
490 tv.tv_sec = connection->timeout.tv_sec;
491 tv.tv_usec = connection->timeout.tv_usec;
492
493 do {
494 ret = static_cast<int>(
495 select(connection->sock + 1, nullptr, &fds, nullptr, &tv));
496 if (ret != 1 && !SELECT_ERRNO_IGNORE) { break; }
497 ret = send(connection->sock, commandPtr, commandLen, MSG_DONTWAIT);
498 if (ret <= 0) {
499 if SENDRECV_ERRNO_IGNORE { continue; }
500 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
501 "problems giving command \"%s\"", command);
502 connection->error = MPD_ERROR_SENDING;
503 return;
504 }
505 commandPtr += ret;
506 commandLen -= ret;
507 } while (commandLen > 0);
508
509 if (commandLen > 0) {
510 perror("");
511 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
512 "timeout sending command \"%s\"", command);
513 connection->error = MPD_ERROR_TIMEOUT;
514 return;
515 }
516
517 if (connection->commandList == 0) {
518 connection->doneProcessing = 0;
519 } else if (connection->commandList == COMMAND_LIST_OK) {
520 connection->listOks++;
521 }
522 }
523
524 static void mpd_getNextReturnElement(mpd_Connection *connection) {
525 char *output = nullptr;
526 char *rt = nullptr;
527 char *name = nullptr;
528 char *value = nullptr;
529 fd_set fds;
530 struct timeval tv {};
531 char *tok = nullptr;
532 int readed;
533 char *bufferCheck = nullptr;
534 int err;
535 int pos;
536
537 if (connection->returnElement != nullptr) {
538 mpd_freeReturnElement(connection->returnElement);
539 }
540 connection->returnElement = nullptr;
541
542 if ((connection->doneProcessing != 0) ||
543 ((connection->listOks != 0) && (connection->doneListOk != 0))) {
544 strncpy(connection->errorStr, "already done processing current command",
545 MPD_ERRORSTR_MAX_LENGTH);
546 connection->error = 1;
547 return;
548 }
549
550 bufferCheck = connection->buffer + connection->bufstart;
551 while (connection->bufstart >= connection->buflen ||
552 ((rt = strchr(bufferCheck, '\n')) == nullptr)) {
553 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
554 memmove(connection->buffer, connection->buffer + connection->bufstart,
555 connection->buflen - connection->bufstart + 1);
556 connection->buflen -= connection->bufstart;
557 connection->bufstart = 0;
558 }
559 if (connection->buflen >= MPD_BUFFER_MAX_LENGTH) {
560 strncpy(connection->errorStr, "buffer overrun", MPD_ERRORSTR_MAX_LENGTH);
561 connection->error = MPD_ERROR_BUFFEROVERRUN;
562 connection->doneProcessing = 1;
563 connection->doneListOk = 0;
564 return;
565 }
566 bufferCheck = connection->buffer + connection->buflen;
567 tv.tv_sec = connection->timeout.tv_sec;
568 tv.tv_usec = connection->timeout.tv_usec;
569 FD_ZERO(&fds);
570 FD_SET(connection->sock, &fds);
571 if ((err = static_cast<int>(select(connection->sock + 1, &fds, nullptr,
572 nullptr, &tv) == 1)) != 0) {
573 readed = recv(connection->sock, connection->buffer + connection->buflen,
574 MPD_BUFFER_MAX_LENGTH - connection->buflen, MSG_DONTWAIT);
575 if (readed < 0 && SENDRECV_ERRNO_IGNORE) { continue; }
576 if (readed <= 0) {
577 strncpy(connection->errorStr, "connection closed",
578 MPD_ERRORSTR_MAX_LENGTH);
579 connection->error = MPD_ERROR_CONNCLOSED;
580 connection->doneProcessing = 1;
581 connection->doneListOk = 0;
582 return;
583 }
584 connection->buflen += readed;
585 connection->buffer[connection->buflen] = '\0';
586 } else {
587 strncpy(connection->errorStr, "connection timeout",
588 MPD_ERRORSTR_MAX_LENGTH);
589 connection->error = MPD_ERROR_TIMEOUT;
590 connection->doneProcessing = 1;
591 connection->doneListOk = 0;
592 return;
593 }
594 }
595
596 *rt = '\0';
597 output = connection->buffer + connection->bufstart;
598 connection->bufstart = rt - connection->buffer + 1;
599
600 if (strcmp(output, "OK") == 0) {
601 if (connection->listOks > 0) {
602 strncpy(connection->errorStr, "expected more list_OK's",
603 MPD_ERRORSTR_MAX_LENGTH);
604 connection->error = 1;
605 }
606 connection->listOks = 0;
607 connection->doneProcessing = 1;
608 connection->doneListOk = 0;
609 return;
610 }
611
612 if (strcmp(output, "list_OK") == 0) {
613 if (connection->listOks == 0) {
614 strncpy(connection->errorStr, "got an unexpected list_OK",
615 MPD_ERRORSTR_MAX_LENGTH);
616 connection->error = 1;
617 } else {
618 connection->doneListOk = 1;
619 connection->listOks--;
620 }
621 return;
622 }
623
624 if (strncmp(output, "ACK", strlen("ACK")) == 0) {
625 char *test;
626 char *needle;
627 int val;
628
629 strncpy(connection->errorStr, output, MPD_ERRORSTR_MAX_LENGTH);
630 connection->error = MPD_ERROR_ACK;
631 connection->errorCode = MPD_ACK_ERROR_UNK;
632 connection->errorAt = MPD_ERROR_AT_UNK;
633 connection->doneProcessing = 1;
634 connection->doneListOk = 0;
635
636 needle = strchr(output, '[');
637 if (needle == nullptr) { return; }
638 val = strtol(needle + 1, &test, 10);
639 if (*test != '@') { return; }
640 connection->errorCode = val;
641 val = strtol(test + 1, &test, 10);
642 if (*test != ']') { return; }
643 connection->errorAt = val;
644 return;
645 }
646
647 tok = strchr(output, ':');
648 if (tok == nullptr) { return; }
649 pos = tok - output;
650 value = ++tok;
651 name = output;
652 name[pos] = '\0';
653
654 if (value[0] == ' ') {
655 connection->returnElement = mpd_newReturnElement(name, &(value[1]));
656 } else {
657 snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH,
658 "error parsing: %s:%s", name, value);
659 connection->error = 1;
660 }
661 }
662
663 void mpd_finishCommand(mpd_Connection *connection) {
664 while ((connection != nullptr) && (connection->doneProcessing == 0)) {
665 if (connection->doneListOk != 0) { connection->doneListOk = 0; }
666 mpd_getNextReturnElement(connection);
667 }
668 }
669
670 static void mpd_finishListOkCommand(mpd_Connection *connection) {
671 while ((connection->doneProcessing == 0) && (connection->listOks != 0) &&
672 (connection->doneListOk == 0)) {
673 mpd_getNextReturnElement(connection);
674 }
675 }
676
677 int mpd_nextListOkCommand(mpd_Connection *connection) {
678 mpd_finishListOkCommand(connection);
679 if (connection->doneProcessing == 0) { connection->doneListOk = 0; }
680 if (connection->listOks == 0 || (connection->doneProcessing != 0)) {
681 return -1;
682 }
683 return 0;
684 }
685
686 void mpd_sendStatusCommand(mpd_Connection *connection) {
687 mpd_executeCommand(connection, "status\n");
688 }
689
690 mpd_Status *mpd_getStatus(mpd_Connection *connection) {
691 mpd_Status *status;
692
693 /* mpd_executeCommand(connection, "status\n");
694 if (connection->error) {
695 return nullptr;
696 } */
697
698 if ((connection->doneProcessing != 0) ||
699 ((connection->listOks != 0) && (connection->doneListOk != 0))) {
700 return nullptr;
701 }
702
703 if (connection->returnElement == nullptr) {
704 mpd_getNextReturnElement(connection);
705 }
706
707 status = static_cast<mpd_Status *>(malloc(sizeof(mpd_Status)));
708 status->volume = -1;
709 status->repeat = 0;
710 status->random = 0;
711 status->playlist = -1;
712 status->playlistLength = -1;
713 status->state = -1;
714 status->song = 0;
715 status->songid = 0;
716 status->elapsedTime = 0;
717 status->totalTime = 0;
718 status->bitRate = 0;
719 status->sampleRate = 0;
720 status->bits = 0;
721 status->channels = 0;
722 status->crossfade = -1;
723 status->error = nullptr;
724 status->updatingDb = 0;
725
726 if (connection->error != 0) {
727 free(status);
728 return nullptr;
729 }
730 while (connection->returnElement != nullptr) {
731 mpd_ReturnElement *re = connection->returnElement;
732
733 if (strcmp(re->name, "volume") == 0) {
734 status->volume = atoi(re->value);
735 } else if (strcmp(re->name, "repeat") == 0) {
736 status->repeat = atoi(re->value);
737 } else if (strcmp(re->name, "random") == 0) {
738 status->random = atoi(re->value);
739 } else if (strcmp(re->name, "playlist") == 0) {
740 status->playlist = strtol(re->value, nullptr, 10);
741 } else if (strcmp(re->name, "playlistlength") == 0) {
742 status->playlistLength = atoi(re->value);
743 } else if (strcmp(re->name, "bitrate") == 0) {
744 status->bitRate = atoi(re->value);
745 } else if (strcmp(re->name, "state") == 0) {
746 if (strcmp(re->value, "play") == 0) {
747 status->state = MPD_STATUS_STATE_PLAY;
748 } else if (strcmp(re->value, "stop") == 0) {
749 status->state = MPD_STATUS_STATE_STOP;
750 } else if (strcmp(re->value, "pause") == 0) {
751 status->state = MPD_STATUS_STATE_PAUSE;
752 } else {
753 status->state = MPD_STATUS_STATE_UNKNOWN;
754 }
755 } else if (strcmp(re->name, "song") == 0) {
756 status->song = atoi(re->value);
757 } else if (strcmp(re->name, "songid") == 0) {
758 status->songid = atoi(re->value);
759 } else if (strcmp(re->name, "time") == 0) {
760 char *tok = strchr(re->value, ':');
761
762 /* the second strchr below is a safety check */
763 if ((tok != nullptr) && (strchr(tok, 0) > (tok + 1))) {
764 /* atoi stops at the first non-[0-9] char: */
765 status->elapsedTime = atoi(re->value);
766 status->totalTime = atoi(tok + 1);
767 }
768 } else if (strcmp(re->name, "error") == 0) {
769 status->error = strndup(re->value, text_buffer_size.get(*state));
770 } else if (strcmp(re->name, "xfade") == 0) {
771 status->crossfade = atoi(re->value);
772 } else if (strcmp(re->name, "updating_db") == 0) {
773 status->updatingDb = atoi(re->value);
774 } else if (strcmp(re->name, "audio") == 0) {
775 char *tok = strchr(re->value, ':');
776
777 if ((tok != nullptr) && (strchr(tok, 0) > (tok + 1))) {
778 status->sampleRate = atoi(re->value);
779 status->bits = atoi(++tok);
780 tok = strchr(tok, ':');
781 if ((tok != nullptr) && (strchr(tok, 0) > (tok + 1))) {
782 status->channels = atoi(tok + 1);
783 }
784 }
785 }
786
787 mpd_getNextReturnElement(connection);
788 if (connection->error != 0) {
789 free(status);
790 return nullptr;
791 }
792 }
793
794 if (connection->error != 0) {
795 free(status);
796 return nullptr;
797 }
798 if (status->state < 0) {
799 strncpy(connection->errorStr, "state not found", MPD_ERRORSTR_MAX_LENGTH);
800 connection->error = 1;
801 free(status);
802 return nullptr;
803 }
804
805 return status;
806 }
807
808 void mpd_freeStatus(mpd_Status *status) {
809 free_and_zero(status->error);
810 free(status);
811 }
812
813 void mpd_sendStatsCommand(mpd_Connection *connection) {
814 mpd_executeCommand(connection, "stats\n");
815 }
816
817 mpd_Stats *mpd_getStats(mpd_Connection *connection) {
818 mpd_Stats *stats;
819
820 /* mpd_executeCommand(connection, "stats\n");
821 if (connection->error) {
822 return nullptr;
823 } */
824
825 if ((connection->doneProcessing != 0) ||
826 ((connection->listOks != 0) && (connection->doneListOk != 0))) {
827 return nullptr;
828 }
829
830 if (connection->returnElement == nullptr) {
831 mpd_getNextReturnElement(connection);
832 }
833
834 stats = static_cast<mpd_Stats *>(malloc(sizeof(mpd_Stats)));
835 stats->numberOfArtists = 0;
836 stats->numberOfAlbums = 0;
837 stats->numberOfSongs = 0;
838 stats->uptime = 0;
839 stats->dbUpdateTime = 0;
840 stats->playTime = 0;
841 stats->dbPlayTime = 0;
842
843 if (connection->error != 0) {
844 free(stats);
845 return nullptr;
846 }
847 while (connection->returnElement != nullptr) {
848 mpd_ReturnElement *re = connection->returnElement;
849
850 if (strcmp(re->name, "artists") == 0) {
851 stats->numberOfArtists = atoi(re->value);
852 } else if (strcmp(re->name, "albums") == 0) {
853 stats->numberOfAlbums = atoi(re->value);
854 } else if (strcmp(re->name, "songs") == 0) {
855 stats->numberOfSongs = atoi(re->value);
856 } else if (strcmp(re->name, "uptime") == 0) {
857 stats->uptime = strtol(re->value, nullptr, 10);
858 } else if (strcmp(re->name, "db_update") == 0) {
859 stats->dbUpdateTime = strtol(re->value, nullptr, 10);
860 } else if (strcmp(re->name, "playtime") == 0) {
861 stats->playTime = strtol(re->value, nullptr, 10);
862 } else if (strcmp(re->name, "db_playtime") == 0) {
863 stats->dbPlayTime = strtol(re->value, nullptr, 10);
864 }
865
866 mpd_getNextReturnElement(connection);
867 if (connection->error != 0) {
868 free(stats);
869 return nullptr;
870 }
871 }
872
873 if (connection->error != 0) {
874 free(stats);
875 return nullptr;
876 }
877
878 return stats;
879 }
880
881 void mpd_freeStats(mpd_Stats *stats) { free(stats); }
882
883 mpd_SearchStats *mpd_getSearchStats(mpd_Connection *connection) {
884 mpd_SearchStats *stats;
885 mpd_ReturnElement *re;
886
887 if ((connection->doneProcessing != 0) ||
888 ((connection->listOks != 0) && (connection->doneListOk != 0))) {
889 return nullptr;
890 }
891
892 if (connection->returnElement == nullptr) {
893 mpd_getNextReturnElement(connection);
894 }
895
896 if (connection->error != 0) { return nullptr; }
897
898 stats = static_cast<mpd_SearchStats *>(malloc(sizeof(mpd_SearchStats)));
899 stats->numberOfSongs = 0;
900 stats->playTime = 0;
901
902 while (connection->returnElement != nullptr) {
903 re = connection->returnElement;
904
905 if (strcmp(re->name, "songs") == 0) {
906 stats->numberOfSongs = atoi(re->value);
907 } else if (strcmp(re->name, "playtime") == 0) {
908 stats->playTime = strtol(re->value, nullptr, 10);
909 }
910
911 mpd_getNextReturnElement(connection);
912 if (connection->error != 0) {
913 free(stats);
914 return nullptr;
915 }
916 }
917
918 if (connection->error != 0) {
919 free(stats);
920 return nullptr;
921 }
922
923 return stats;
924 }
925
926 void mpd_freeSearchStats(mpd_SearchStats *stats) { free(stats); }
927
928 static void mpd_initSong(mpd_Song *song) {
929 song->file = nullptr;
930 song->artist = nullptr;
931 song->albumartist = nullptr;
932 song->album = nullptr;
933 song->track = nullptr;
934 song->title = nullptr;
935 song->name = nullptr;
936 song->date = nullptr;
937 /* added by Qball */
938 song->genre = nullptr;
939 song->composer = nullptr;
940 song->performer = nullptr;
941 song->disc = nullptr;
942 song->comment = nullptr;
943
944 song->time = MPD_SONG_NO_TIME;
945 song->pos = MPD_SONG_NO_NUM;
946 song->id = MPD_SONG_NO_ID;
947 }
948
949 static void mpd_finishSong(mpd_Song *song) {
950 free_and_zero(song->file);
951 free_and_zero(song->artist);
952 free_and_zero(song->albumartist);
953 free_and_zero(song->album);
954 free_and_zero(song->title);
955 free_and_zero(song->track);
956 free_and_zero(song->name);
957 free_and_zero(song->date);
958 free_and_zero(song->genre);
959 free_and_zero(song->composer);
960 free_and_zero(song->disc);
961 free_and_zero(song->comment);
962 }
963
964 mpd_Song *mpd_newSong() {
965 auto *ret = static_cast<mpd_Song *>(malloc(sizeof(mpd_Song)));
966
967 mpd_initSong(ret);
968
969 return ret;
970 }
971
972 void mpd_freeSong(mpd_Song *song) {
973 mpd_finishSong(song);
974 free(song);
975 }
976
977 mpd_Song *mpd_songDup(mpd_Song *song) {
978 mpd_Song *ret = mpd_newSong();
979
980 if (song->file != nullptr) {
981 ret->file = strndup(song->file, text_buffer_size.get(*state));
982 }
983 if (song->artist != nullptr) {
984 ret->artist = strndup(song->artist, text_buffer_size.get(*state));
985 }
986 if (song->albumartist != nullptr) {
987 ret->artist = strndup(song->albumartist, text_buffer_size.get(*state));
988 }
989 if (song->album != nullptr) {
990 ret->album = strndup(song->album, text_buffer_size.get(*state));
991 }
992 if (song->title != nullptr) {
993 ret->title = strndup(song->title, text_buffer_size.get(*state));
994 }
995 if (song->track != nullptr) {
996 ret->track = strndup(song->track, text_buffer_size.get(*state));
997 }
998 if (song->name != nullptr) {
999 ret->name = strndup(song->name, text_buffer_size.get(*state));
1000 }
1001 if (song->date != nullptr) {
1002 ret->date = strndup(song->date, text_buffer_size.get(*state));
1003 }
1004 if (song->genre != nullptr) {
1005 ret->genre = strndup(song->genre, text_buffer_size.get(*state));
1006 }
1007 if (song->composer != nullptr) {
1008 ret->composer = strndup(song->composer, text_buffer_size.get(*state));
1009 }
1010 if (song->disc != nullptr) {
1011 ret->disc = strndup(song->disc, text_buffer_size.get(*state));
1012 }
1013 if (song->comment != nullptr) {
1014 ret->comment = strndup(song->comment, text_buffer_size.get(*state));
1015 }
1016 ret->time = song->time;
1017 ret->pos = song->pos;
1018 ret->id = song->id;
1019
1020 return ret;
1021 }
1022
1023 static void mpd_initDirectory(mpd_Directory *directory) {
1024 directory->path = nullptr;
1025 }
1026
1027 static void mpd_finishDirectory(mpd_Directory *directory) {
1028 free_and_zero(directory->path);
1029 }
1030
1031 mpd_Directory *mpd_newDirectory() {
1032 auto *directory = static_cast<mpd_Directory *>(malloc(sizeof(mpd_Directory)));
1033
1034 mpd_initDirectory(directory);
1035
1036 return directory;
1037 }
1038
1039 void mpd_freeDirectory(mpd_Directory *directory) {
1040 mpd_finishDirectory(directory);
1041
1042 free(directory);
1043 }
1044
1045 mpd_Directory *mpd_directoryDup(mpd_Directory *directory) {
1046 mpd_Directory *ret = mpd_newDirectory();
1047
1048 if (directory->path != nullptr) {
1049 ret->path = strndup(directory->path, text_buffer_size.get(*state));
1050 }
1051
1052 return ret;
1053 }
1054
1055 static void mpd_initPlaylistFile(mpd_PlaylistFile *playlist) {
1056 playlist->path = nullptr;
1057 }
1058
1059 static void mpd_finishPlaylistFile(mpd_PlaylistFile *playlist) {
1060 free_and_zero(playlist->path);
1061 }
1062
1063 mpd_PlaylistFile *mpd_newPlaylistFile() {
1064 auto *playlist =
1065 static_cast<mpd_PlaylistFile *>(malloc(sizeof(mpd_PlaylistFile)));
1066
1067 mpd_initPlaylistFile(playlist);
1068
1069 return playlist;
1070 }
1071
1072 void mpd_freePlaylistFile(mpd_PlaylistFile *playlist) {
1073 mpd_finishPlaylistFile(playlist);
1074 free(playlist);
1075 }
1076
1077 mpd_PlaylistFile *mpd_playlistFileDup(mpd_PlaylistFile *playlist) {
1078 mpd_PlaylistFile *ret = mpd_newPlaylistFile();
1079
1080 if (playlist->path != nullptr) {
1081 ret->path = strndup(playlist->path, text_buffer_size.get(*state));
1082 }
1083
1084 return ret;
1085 }
1086
1087 static void mpd_initInfoEntity(mpd_InfoEntity *entity) {
1088 entity->info.directory = nullptr;
1089 }
1090
1091 static void mpd_finishInfoEntity(mpd_InfoEntity *entity) {
1092 if (entity->info.directory != nullptr) {
1093 if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1094 mpd_freeDirectory(entity->info.directory);
1095 } else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
1096 mpd_freeSong(entity->info.song);
1097 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1098 mpd_freePlaylistFile(entity->info.playlistFile);
1099 }
1100 }
1101 }
1102
1103 mpd_InfoEntity *mpd_newInfoEntity() {
1104 auto *entity = static_cast<mpd_InfoEntity *>(malloc(sizeof(mpd_InfoEntity)));
1105
1106 mpd_initInfoEntity(entity);
1107
1108 return entity;
1109 }
1110
1111 void mpd_freeInfoEntity(mpd_InfoEntity *entity) {
1112 mpd_finishInfoEntity(entity);
1113 free(entity);
1114 }
1115
1116 static void mpd_sendInfoCommand(mpd_Connection *connection, char *command) {
1117 mpd_executeCommand(connection, command);
1118 }
1119
1120 mpd_InfoEntity *mpd_getNextInfoEntity(mpd_Connection *connection) {
1121 mpd_InfoEntity *entity = nullptr;
1122
1123 if ((connection->doneProcessing != 0) ||
1124 ((connection->listOks != 0) && (connection->doneListOk != 0))) {
1125 return nullptr;
1126 }
1127
1128 if (connection->returnElement == nullptr) {
1129 mpd_getNextReturnElement(connection);
1130 }
1131
1132 if (connection->returnElement != nullptr) {
1133 if (strcmp(connection->returnElement->name, "file") == 0) {
1134 entity = mpd_newInfoEntity();
1135 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1136 entity->info.song = mpd_newSong();
1137 entity->info.song->file = strndup(connection->returnElement->value,
1138 text_buffer_size.get(*state));
1139 } else if (strcmp(connection->returnElement->name, "directory") == 0) {
1140 entity = mpd_newInfoEntity();
1141 entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
1142 entity->info.directory = mpd_newDirectory();
1143 entity->info.directory->path = strndup(connection->returnElement->value,
1144 text_buffer_size.get(*state));
1145 } else if (strcmp(connection->returnElement->name, "playlist") == 0) {
1146 entity = mpd_newInfoEntity();
1147 entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
1148 entity->info.playlistFile = mpd_newPlaylistFile();
1149 entity->info.playlistFile->path = strndup(
1150 connection->returnElement->value, text_buffer_size.get(*state));
1151 } else if (strcmp(connection->returnElement->name, "cpos") == 0) {
1152 entity = mpd_newInfoEntity();
1153 entity->type = MPD_INFO_ENTITY_TYPE_SONG;
1154 entity->info.song = mpd_newSong();
1155 entity->info.song->pos = atoi(connection->returnElement->value);
1156 } else {
1157 connection->error = 1;
1158 strncpy(connection->errorStr, "problem parsing song info",
1159 MPD_ERRORSTR_MAX_LENGTH);
1160 return nullptr;
1161 }
1162 } else {
1163 return nullptr;
1164 }
1165
1166 mpd_getNextReturnElement(connection);
1167 while (connection->returnElement != nullptr) {
1168 mpd_ReturnElement *re = connection->returnElement;
1169
1170 if (strcmp(re->name, "file") == 0) { return entity; }
1171 if (strcmp(re->name, "directory") == 0) { return entity; }
1172 if (strcmp(re->name, "playlist") == 0) { return entity; }
1173 if (strcmp(re->name, "cpos") == 0) { return entity; }
1174
1175 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG &&
1176 (strlen(re->value) != 0u)) {
1177 if ((entity->info.song->artist == nullptr) &&
1178 strcmp(re->name, "Artist") == 0) {
1179 entity->info.song->artist =
1180 strndup(re->value, text_buffer_size.get(*state));
1181 } else if ((entity->info.song->albumartist == nullptr) &&
1182 strcmp(re->name, "AlbumArtist") == 0) {
1183 entity->info.song->albumartist =
1184 strndup(re->value, text_buffer_size.get(*state));
1185 } else if ((entity->info.song->album == nullptr) &&
1186 strcmp(re->name, "Album") == 0) {
1187 entity->info.song->album =
1188 strndup(re->value, text_buffer_size.get(*state));
1189 } else if ((entity->info.song->title == nullptr) &&
1190 strcmp(re->name, "Title") == 0) {
1191 entity->info.song->title =
1192 strndup(re->value, text_buffer_size.get(*state));
1193 } else if ((entity->info.song->track == nullptr) &&
1194 strcmp(re->name, "Track") == 0) {
1195 entity->info.song->track =
1196 strndup(re->value, text_buffer_size.get(*state));
1197 } else if ((entity->info.song->name == nullptr) &&
1198 strcmp(re->name, "Name") == 0) {
1199 entity->info.song->name =
1200 strndup(re->value, text_buffer_size.get(*state));
1201 } else if (entity->info.song->time == MPD_SONG_NO_TIME &&
1202 strcmp(re->name, "Time") == 0) {
1203 entity->info.song->time = atoi(re->value);
1204 } else if (entity->info.song->pos == MPD_SONG_NO_NUM &&
1205 strcmp(re->name, "Pos") == 0) {
1206 entity->info.song->pos = atoi(re->value);
1207 } else if (entity->info.song->id == MPD_SONG_NO_ID &&
1208 strcmp(re->name, "Id") == 0) {
1209 entity->info.song->id = atoi(re->value);
1210 } else if ((entity->info.song->date == nullptr) &&
1211 strcmp(re->name, "Date") == 0) {
1212 entity->info.song->date =
1213 strndup(re->value, text_buffer_size.get(*state));
1214 } else if ((entity->info.song->genre == nullptr) &&
1215 strcmp(re->name, "Genre") == 0) {
1216 entity->info.song->genre =
1217 strndup(re->value, text_buffer_size.get(*state));
1218 } else if ((entity->info.song->composer == nullptr) &&
1219 strcmp(re->name, "Composer") == 0) {
1220 entity->info.song->composer =
1221 strndup(re->value, text_buffer_size.get(*state));
1222 } else if ((entity->info.song->performer == nullptr) &&
1223 strcmp(re->name, "Performer") == 0) {
1224 entity->info.song->performer =
1225 strndup(re->value, text_buffer_size.get(*state));
1226 } else if ((entity->info.song->disc == nullptr) &&
1227 strcmp(re->name, "Disc") == 0) {
1228 entity->info.song->disc =
1229 strndup(re->value, text_buffer_size.get(*state));
1230 } else if ((entity->info.song->comment == nullptr) &&
1231 strcmp(re->name, "Comment") == 0) {
1232 entity->info.song->comment =
1233 strndup(re->value, text_buffer_size.get(*state));
1234 }
1235 } else if (entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
1236 } else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
1237 }
1238
1239 mpd_getNextReturnElement(connection);
1240 }
1241
1242 return entity;
1243 }
1244
1245 static char *mpd_getNextReturnElementNamed(mpd_Connection *connection,
1246 const char *name) {
1247 if ((connection->doneProcessing != 0) ||
1248 ((connection->listOks != 0) && (connection->doneListOk != 0))) {
1249 return nullptr;
1250 }
1251
1252 mpd_getNextReturnElement(connection);
1253 while (connection->returnElement != nullptr) {
1254 mpd_ReturnElement *re = connection->returnElement;
1255
1256 if (strcmp(re->name, name) == 0) {
1257 return strndup(re->value, text_buffer_size.get(*state));
1258 }
1259 mpd_getNextReturnElement(connection);
1260 }
1261
1262 return nullptr;
1263 }
1264
1265 char *mpd_getNextTag(mpd_Connection *connection, int type) {
1266 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES ||
1267 type == MPD_TAG_ITEM_ANY) {
1268 return nullptr;
1269 }
1270 if (type == MPD_TAG_ITEM_FILENAME) {
1271 return mpd_getNextReturnElementNamed(connection, "file");
1272 }
1273 return mpd_getNextReturnElementNamed(connection, mpdTagItemKeys[type]);
1274 }
1275
1276 char *mpd_getNextArtist(mpd_Connection *connection) {
1277 return mpd_getNextReturnElementNamed(connection, "Artist");
1278 }
1279
1280 char *mpd_getNextAlbum(mpd_Connection *connection) {
1281 return mpd_getNextReturnElementNamed(connection, "Album");
1282 }
1283
1284 void mpd_sendPlaylistInfoCommand(mpd_Connection *connection, int songPos) {
1285 int len = strlen("playlistinfo") + 2 + INTLEN + 3;
1286 auto *string = static_cast<char *>(malloc(len));
1287
1288 snprintf(string, len, "playlistinfo \"%i\"\n", songPos);
1289 mpd_sendInfoCommand(connection, string);
1290 free(string);
1291 }
1292
1293 void mpd_sendPlaylistIdCommand(mpd_Connection *connection, int id) {
1294 int len = strlen("playlistid") + 2 + INTLEN + 3;
1295 auto *string = static_cast<char *>(malloc(len));
1296
1297 snprintf(string, len, "playlistid \"%i\"\n", id);
1298 mpd_sendInfoCommand(connection, string);
1299 free(string);
1300 }
1301
1302 void mpd_sendPlChangesCommand(mpd_Connection *connection, long long playlist) {
1303 int len = strlen("plchanges") + 2 + LONGLONGLEN + 3;
1304 auto *string = static_cast<char *>(malloc(len));
1305
1306 snprintf(string, len, "plchanges \"%lld\"\n", playlist);
1307 mpd_sendInfoCommand(connection, string);
1308 free(string);
1309 }
1310
1311 void mpd_sendPlChangesPosIdCommand(mpd_Connection *connection,
1312 long long playlist) {
1313 int len = strlen("plchangesposid") + 2 + LONGLONGLEN + 3;
1314 auto *string = static_cast<char *>(malloc(len));
1315
1316 snprintf(string, len, "plchangesposid \"%lld\"\n", playlist);
1317 mpd_sendInfoCommand(connection, string);
1318 free(string);
1319 }
1320
1321 void mpd_sendListallCommand(mpd_Connection *connection, const char *dir) {
1322 char *sDir = mpd_sanitizeArg(dir);
1323 int len = strlen("listall") + 2 + strlen(sDir) + 3;
1324 auto *string = static_cast<char *>(malloc(len));
1325
1326 snprintf(string, len, "listall \"%s\"\n", sDir);
1327 mpd_sendInfoCommand(connection, string);
1328 free(string);
1329 free(sDir);
1330 }
1331
1332 void mpd_sendListallInfoCommand(mpd_Connection *connection, const char *dir) {
1333 char *sDir = mpd_sanitizeArg(dir);
1334 int len = strlen("listallinfo") + 2 + strlen(sDir) + 3;
1335 auto *string = static_cast<char *>(malloc(len));
1336
1337 snprintf(string, len, "listallinfo \"%s\"\n", sDir);
1338 mpd_sendInfoCommand(connection, string);
1339 free(string);
1340 free(sDir);
1341 }
1342
1343 void mpd_sendLsInfoCommand(mpd_Connection *connection, const char *dir) {
1344 char *sDir = mpd_sanitizeArg(dir);
1345 int len = strlen("lsinfo") + 2 + strlen(sDir) + 3;
1346 auto *string = static_cast<char *>(malloc(len));
1347
1348 snprintf(string, len, "lsinfo \"%s\"\n", sDir);
1349 mpd_sendInfoCommand(connection, string);
1350 free(string);
1351 free(sDir);
1352 }
1353
1354 void mpd_sendCurrentSongCommand(mpd_Connection *connection) {
1355 mpd_executeCommand(connection, "currentsong\n");
1356 }
1357
1358 void mpd_sendSearchCommand(mpd_Connection *connection, int table,
1359 const char *str) {
1360 mpd_startSearch(connection, 0);
1361 mpd_addConstraintSearch(connection, table, str);
1362 mpd_commitSearch(connection);
1363 }
1364
1365 void mpd_sendFindCommand(mpd_Connection *connection, int table,
1366 const char *str) {
1367 mpd_startSearch(connection, 1);
1368 mpd_addConstraintSearch(connection, table, str);
1369 mpd_commitSearch(connection);
1370 }
1371
1372 void mpd_sendListCommand(mpd_Connection *connection, int table,
1373 const char *arg1) {
1374 char st[7];
1375 int len;
1376 char *string;
1377
1378 if (table == MPD_TABLE_ARTIST) {
1379 strncpy(st, "artist", strlen("artist") + 1);
1380 } else if (table == MPD_TABLE_ALBUM) {
1381 strncpy(st, "album", strlen("album") + 1);
1382 } else {
1383 connection->error = 1;
1384 strncpy(connection->errorStr, "unknown table for list",
1385 MPD_ERRORSTR_MAX_LENGTH);
1386 return;
1387 }
1388 if (arg1 != nullptr) {
1389 char *sanitArg1 = mpd_sanitizeArg(arg1);
1390
1391 len = strlen("list") + 1 + strlen(sanitArg1) + 2 + strlen(st) + 3;
1392 string = static_cast<char *>(malloc(len));
1393 snprintf(string, len, "list %s \"%s\"\n", st, sanitArg1);
1394 free(sanitArg1);
1395 } else {
1396 len = strlen("list") + 1 + strlen(st) + 2;
1397 string = static_cast<char *>(malloc(len));
1398 snprintf(string, len, "list %s\n", st);
1399 }
1400 mpd_sendInfoCommand(connection, string);
1401 free(string);
1402 }
1403
1404 void mpd_sendAddCommand(mpd_Connection *connection, const char *file) {
1405 char *sFile = mpd_sanitizeArg(file);
1406 int len = strlen("add") + 2 + strlen(sFile) + 3;
1407 auto *string = static_cast<char *>(malloc(len));
1408
1409 snprintf(string, len, "add \"%s\"\n", sFile);
1410 mpd_executeCommand(connection, string);
1411 free(string);
1412 free(sFile);
1413 }
1414
1415 int mpd_sendAddIdCommand(mpd_Connection *connection, const char *file) {
1416 int retval = -1;
1417 char *sFile = mpd_sanitizeArg(file);
1418 int len = strlen("addid") + 2 + strlen(sFile) + 3;
1419 auto *string = static_cast<char *>(malloc(len));
1420
1421 snprintf(string, len, "addid \"%s\"\n", sFile);
1422 mpd_sendInfoCommand(connection, string);
1423 free(string);
1424 free(sFile);
1425
1426 string = mpd_getNextReturnElementNamed(connection, "Id");
1427 if (string != nullptr) {
1428 retval = atoi(string);
1429 free(string);
1430 }
1431
1432 return retval;
1433 }
1434
1435 void mpd_sendDeleteCommand(mpd_Connection *connection, int songPos) {
1436 int len = strlen("delete") + 2 + INTLEN + 3;
1437 auto *string = static_cast<char *>(malloc(len));
1438
1439 snprintf(string, len, "delete \"%i\"\n", songPos);
1440 mpd_sendInfoCommand(connection, string);
1441 free(string);
1442 }
1443
1444 void mpd_sendDeleteIdCommand(mpd_Connection *connection, int id) {
1445 int len = strlen("deleteid") + 2 + INTLEN + 3;
1446 auto *string = static_cast<char *>(malloc(len));
1447
1448 snprintf(string, len, "deleteid \"%i\"\n", id);
1449 mpd_sendInfoCommand(connection, string);
1450 free(string);
1451 }
1452
1453 void mpd_sendSaveCommand(mpd_Connection *connection, const char *name) {
1454 char *sName = mpd_sanitizeArg(name);
1455 int len = strlen("save") + 2 + strlen(sName) + 3;
1456 auto *string = static_cast<char *>(malloc(len));
1457
1458 snprintf(string, len, "save \"%s\"\n", sName);
1459 mpd_executeCommand(connection, string);
1460 free(string);
1461 free(sName);
1462 }
1463
1464 void mpd_sendLoadCommand(mpd_Connection *connection, const char *name) {
1465 char *sName = mpd_sanitizeArg(name);
1466 int len = strlen("load") + 2 + strlen(sName) + 3;
1467 auto *string = static_cast<char *>(malloc(len));
1468
1469 snprintf(string, len, "load \"%s\"\n", sName);
1470 mpd_executeCommand(connection, string);
1471 free(string);
1472 free(sName);
1473 }
1474
1475 void mpd_sendRmCommand(mpd_Connection *connection, const char *name) {
1476 char *sName = mpd_sanitizeArg(name);
1477 int len = strlen("rm") + 2 + strlen(sName) + 3;
1478 auto *string = static_cast<char *>(malloc(len));
1479
1480 snprintf(string, len, "rm \"%s\"\n", sName);
1481 mpd_executeCommand(connection, string);
1482 free(string);
1483 free(sName);
1484 }
1485
1486 void mpd_sendRenameCommand(mpd_Connection *connection, const char *from,
1487 const char *to) {
1488 char *sFrom = mpd_sanitizeArg(from);
1489 char *sTo = mpd_sanitizeArg(to);
1490 int len = strlen("rename") + 2 + strlen(sFrom) + 3 + strlen(sTo) + 3;
1491 auto *string = static_cast<char *>(malloc(len));
1492
1493 snprintf(string, len, "rename \"%s\" \"%s\"\n", sFrom, sTo);
1494 mpd_executeCommand(connection, string);
1495 free(string);
1496 free(sFrom);
1497 free(sTo);
1498 }
1499
1500 void mpd_sendShuffleCommand(mpd_Connection *connection) {
1501 mpd_executeCommand(connection, "shuffle\n");
1502 }
1503
1504 void mpd_sendClearCommand(mpd_Connection *connection) {
1505 mpd_executeCommand(connection, "clear\n");
1506 }
1507
1508 void mpd_sendPlayCommand(mpd_Connection *connection, int songPos) {
1509 int len = strlen("play") + 2 + INTLEN + 3;
1510 auto *string = static_cast<char *>(malloc(len));
1511
1512 snprintf(string, len, "play \"%i\"\n", songPos);
1513 mpd_sendInfoCommand(connection, string);
1514 free(string);
1515 }
1516
1517 void mpd_sendPlayIdCommand(mpd_Connection *connection, int id) {
1518 int len = strlen("playid") + 2 + INTLEN + 3;
1519 auto *string = static_cast<char *>(malloc(len));
1520
1521 snprintf(string, len, "playid \"%i\"\n", id);
1522 mpd_sendInfoCommand(connection, string);
1523 free(string);
1524 }
1525
1526 void mpd_sendStopCommand(mpd_Connection *connection) {
1527 mpd_executeCommand(connection, "stop\n");
1528 }
1529
1530 void mpd_sendPauseCommand(mpd_Connection *connection, int pauseMode) {
1531 int len = strlen("pause") + 2 + INTLEN + 3;
1532 auto *string = static_cast<char *>(malloc(len));
1533
1534 snprintf(string, len, "pause \"%i\"\n", pauseMode);
1535 mpd_executeCommand(connection, string);
1536 free(string);
1537 }
1538
1539 void mpd_sendNextCommand(mpd_Connection *connection) {
1540 mpd_executeCommand(connection, "next\n");
1541 }
1542
1543 void mpd_sendMoveCommand(mpd_Connection *connection, int from, int to) {
1544 int len = strlen("move") + 2 + INTLEN + 3 + INTLEN + 3;
1545 auto *string = static_cast<char *>(malloc(len));
1546
1547 snprintf(string, len, "move \"%i\" \"%i\"\n", from, to);
1548 mpd_sendInfoCommand(connection, string);
1549 free(string);
1550 }
1551
1552 void mpd_sendMoveIdCommand(mpd_Connection *connection, int id, int to) {
1553 int len = strlen("moveid") + 2 + INTLEN + 3 + INTLEN + 3;
1554 auto *string = static_cast<char *>(malloc(len));
1555
1556 snprintf(string, len, "moveid \"%i\" \"%i\"\n", id, to);
1557 mpd_sendInfoCommand(connection, string);
1558 free(string);
1559 }
1560
1561 void mpd_sendSwapCommand(mpd_Connection *connection, int song1, int song2) {
1562 int len = strlen("swap") + 2 + INTLEN + 3 + INTLEN + 3;
1563 auto *string = static_cast<char *>(malloc(len));
1564
1565 snprintf(string, len, "swap \"%i\" \"%i\"\n", song1, song2);
1566 mpd_sendInfoCommand(connection, string);
1567 free(string);
1568 }
1569
1570 void mpd_sendSwapIdCommand(mpd_Connection *connection, int id1, int id2) {
1571 int len = strlen("swapid") + 2 + INTLEN + 3 + INTLEN + 3;
1572 auto *string = static_cast<char *>(malloc(len));
1573
1574 snprintf(string, len, "swapid \"%i\" \"%i\"\n", id1, id2);
1575 mpd_sendInfoCommand(connection, string);
1576 free(string);
1577 }
1578
1579 void mpd_sendSeekCommand(mpd_Connection *connection, int song, int seek_time) {
1580 int len = strlen("seek") + 2 + INTLEN + 3 + INTLEN + 3;
1581 auto *string = static_cast<char *>(malloc(len));
1582
1583 snprintf(string, len, "seek \"%i\" \"%i\"\n", song, seek_time);
1584 mpd_sendInfoCommand(connection, string);
1585 free(string);
1586 }
1587
1588 void mpd_sendSeekIdCommand(mpd_Connection *connection, int id, int seek_time) {
1589 int len = strlen("seekid") + 2 + INTLEN + 3 + INTLEN + 3;
1590 auto *string = static_cast<char *>(malloc(len));
1591
1592 snprintf(string, len, "seekid \"%i\" \"%i\"\n", id, seek_time);
1593 mpd_sendInfoCommand(connection, string);
1594 free(string);
1595 }
1596
1597 void mpd_sendUpdateCommand(mpd_Connection *connection, char *path) {
1598 char *sPath = mpd_sanitizeArg(path);
1599 int len = strlen("update") + 2 + strlen(sPath) + 3;
1600 auto *string = static_cast<char *>(malloc(len));
1601
1602 snprintf(string, len, "update \"%s\"\n", sPath);
1603 mpd_sendInfoCommand(connection, string);
1604 free(string);
1605 free(sPath);
1606 }
1607
1608 int mpd_getUpdateId(mpd_Connection *connection) {
1609 char *jobid;
1610 int ret = 0;
1611
1612 jobid = mpd_getNextReturnElementNamed(connection, "updating_db");
1613 if (jobid != nullptr) {
1614 ret = atoi(jobid);
1615 free(jobid);
1616 }
1617
1618 return ret;
1619 }
1620
1621 void mpd_sendPrevCommand(mpd_Connection *connection) {
1622 mpd_executeCommand(connection, "previous\n");
1623 }
1624
1625 void mpd_sendRepeatCommand(mpd_Connection *connection, int repeatMode) {
1626 int len = strlen("repeat") + 2 + INTLEN + 3;
1627 auto *string = static_cast<char *>(malloc(len));
1628
1629 snprintf(string, len, "repeat \"%i\"\n", repeatMode);
1630 mpd_executeCommand(connection, string);
1631 free(string);
1632 }
1633
1634 void mpd_sendRandomCommand(mpd_Connection *connection, int randomMode) {
1635 int len = strlen("random") + 2 + INTLEN + 3;
1636 auto *string = static_cast<char *>(malloc(len));
1637
1638 snprintf(string, len, "random \"%i\"\n", randomMode);
1639 mpd_executeCommand(connection, string);
1640 free(string);
1641 }
1642
1643 void mpd_sendSetvolCommand(mpd_Connection *connection, int volumeChange) {
1644 int len = strlen("setvol") + 2 + INTLEN + 3;
1645 auto *string = static_cast<char *>(malloc(len));
1646
1647 snprintf(string, len, "setvol \"%i\"\n", volumeChange);
1648 mpd_executeCommand(connection, string);
1649 free(string);
1650 }
1651
1652 void mpd_sendVolumeCommand(mpd_Connection *connection, int volumeChange) {
1653 int len = strlen("volume") + 2 + INTLEN + 3;
1654 auto *string = static_cast<char *>(malloc(len));
1655
1656 snprintf(string, len, "volume \"%i\"\n", volumeChange);
1657 mpd_executeCommand(connection, string);
1658 free(string);
1659 }
1660
1661 void mpd_sendCrossfadeCommand(mpd_Connection *connection, int seconds) {
1662 int len = strlen("crossfade") + 2 + INTLEN + 3;
1663 auto *string = static_cast<char *>(malloc(len));
1664
1665 snprintf(string, len, "crossfade \"%i\"\n", seconds);
1666 mpd_executeCommand(connection, string);
1667 free(string);
1668 }
1669
1670 void mpd_sendPasswordCommand(mpd_Connection *connection, const char *pass) {
1671 char *sPass = mpd_sanitizeArg(pass);
1672 int len = strlen("password") + 2 + strlen(sPass) + 3;
1673 auto *string = static_cast<char *>(malloc(len));
1674
1675 snprintf(string, len, "password \"%s\"\n", sPass);
1676 mpd_executeCommand(connection, string);
1677 free(string);
1678 free(sPass);
1679 }
1680
1681 void mpd_sendCommandListBegin(mpd_Connection *connection) {
1682 if (connection->commandList != 0) {
1683 strncpy(connection->errorStr, "already in command list mode",
1684 MPD_ERRORSTR_MAX_LENGTH);
1685 connection->error = 1;
1686 return;
1687 }
1688 connection->commandList = COMMAND_LIST;
1689 mpd_executeCommand(connection, "command_list_begin\n");
1690 }
1691
1692 void mpd_sendCommandListOkBegin(mpd_Connection *connection) {
1693 if (connection->commandList != 0) {
1694 strncpy(connection->errorStr, "already in command list mode",
1695 MPD_ERRORSTR_MAX_LENGTH);
1696 connection->error = 1;
1697 return;
1698 }
1699 connection->commandList = COMMAND_LIST_OK;
1700 mpd_executeCommand(connection, "command_list_ok_begin\n");
1701 connection->listOks = 0;
1702 }
1703
1704 void mpd_sendCommandListEnd(mpd_Connection *connection) {
1705 if (connection->commandList == 0) {
1706 strncpy(connection->errorStr, "not in command list mode",
1707 MPD_ERRORSTR_MAX_LENGTH);
1708 connection->error = 1;
1709 return;
1710 }
1711 connection->commandList = 0;
1712 mpd_executeCommand(connection, "command_list_end\n");
1713 }
1714
1715 void mpd_sendOutputsCommand(mpd_Connection *connection) {
1716 mpd_executeCommand(connection, "outputs\n");
1717 }
1718
1719 mpd_OutputEntity *mpd_getNextOutput(mpd_Connection *connection) {
1720 mpd_OutputEntity *output = nullptr;
1721
1722 if ((connection->doneProcessing != 0) ||
1723 ((connection->listOks != 0) && (connection->doneListOk != 0))) {
1724 return nullptr;
1725 }
1726
1727 if (connection->error != 0) { return nullptr; }
1728
1729 output = static_cast<mpd_OutputEntity *>(malloc(sizeof(mpd_OutputEntity)));
1730 output->id = -10;
1731 output->name = nullptr;
1732 output->enabled = 0;
1733
1734 if (connection->returnElement == nullptr) {
1735 mpd_getNextReturnElement(connection);
1736 }
1737
1738 while (connection->returnElement != nullptr) {
1739 mpd_ReturnElement *re = connection->returnElement;
1740
1741 if (strcmp(re->name, "outputid") == 0) {
1742 if (output != nullptr && output->id >= 0) { return output; }
1743 output->id = atoi(re->value);
1744 } else if (strcmp(re->name, "outputname") == 0) {
1745 output->name = strndup(re->value, text_buffer_size.get(*state));
1746 } else if (strcmp(re->name, "outputenabled") == 0) {
1747 output->enabled = atoi(re->value);
1748 }
1749
1750 mpd_getNextReturnElement(connection);
1751 if (connection->error != 0) {
1752 free(output);
1753 return nullptr;
1754 }
1755 }
1756
1757 return output;
1758 }
1759
1760 void mpd_sendEnableOutputCommand(mpd_Connection *connection, int outputId) {
1761 int len = strlen("enableoutput") + 2 + INTLEN + 3;
1762 auto *string = static_cast<char *>(malloc(len));
1763
1764 snprintf(string, len, "enableoutput \"%i\"\n", outputId);
1765 mpd_executeCommand(connection, string);
1766 free(string);
1767 }
1768
1769 void mpd_sendDisableOutputCommand(mpd_Connection *connection, int outputId) {
1770 int len = strlen("disableoutput") + 2 + INTLEN + 3;
1771 auto *string = static_cast<char *>(malloc(len));
1772
1773 snprintf(string, len, "disableoutput \"%i\"\n", outputId);
1774 mpd_executeCommand(connection, string);
1775 free(string);
1776 }
1777
1778 void mpd_freeOutputElement(mpd_OutputEntity *output) {
1779 free(output->name);
1780 free(output);
1781 }
1782
1783 /** odd naming, but it gets the not allowed commands */
1784 void mpd_sendNotCommandsCommand(mpd_Connection *connection) {
1785 mpd_executeCommand(connection, "notcommands\n");
1786 }
1787
1788 /** odd naming, but it gets the allowed commands */
1789 void mpd_sendCommandsCommand(mpd_Connection *connection) {
1790 mpd_executeCommand(connection, "commands\n");
1791 }
1792
1793 /** Get the next returned command */
1794 char *mpd_getNextCommand(mpd_Connection *connection) {
1795 return mpd_getNextReturnElementNamed(connection, "command");
1796 }
1797
1798 void mpd_sendUrlHandlersCommand(mpd_Connection *connection) {
1799 mpd_executeCommand(connection, "urlhandlers\n");
1800 }
1801
1802 char *mpd_getNextHandler(mpd_Connection *connection) {
1803 return mpd_getNextReturnElementNamed(connection, "handler");
1804 }
1805
1806 void mpd_sendTagTypesCommand(mpd_Connection *connection) {
1807 mpd_executeCommand(connection, "tagtypes\n");
1808 }
1809
1810 char *mpd_getNextTagType(mpd_Connection *connection) {
1811 return mpd_getNextReturnElementNamed(connection, "tagtype");
1812 }
1813
1814 void mpd_startSearch(mpd_Connection *connection, int exact) {
1815 if (connection->request != nullptr) {
1816 strncpy(connection->errorStr, "search already in progress",
1817 MPD_ERRORSTR_MAX_LENGTH);
1818 connection->error = 1;
1819 return;
1820 }
1821
1822 if (exact != 0) {
1823 connection->request = strndup("find", text_buffer_size.get(*state));
1824 } else {
1825 connection->request = strndup("search", text_buffer_size.get(*state));
1826 }
1827 }
1828
1829 void mpd_startStatsSearch(mpd_Connection *connection) {
1830 if (connection->request != nullptr) {
1831 strncpy(connection->errorStr, "search already in progress",
1832 MPD_ERRORSTR_MAX_LENGTH);
1833 connection->error = 1;
1834 return;
1835 }
1836
1837 connection->request = strndup("count", text_buffer_size.get(*state));
1838 }
1839
1840 void mpd_startPlaylistSearch(mpd_Connection *connection, int exact) {
1841 if (connection->request != nullptr) {
1842 strncpy(connection->errorStr, "search already in progress",
1843 MPD_ERRORSTR_MAX_LENGTH);
1844 connection->error = 1;
1845 return;
1846 }
1847
1848 if (exact != 0) {
1849 connection->request = strndup("playlistfind", text_buffer_size.get(*state));
1850 } else {
1851 connection->request =
1852 strndup("playlistsearch", text_buffer_size.get(*state));
1853 }
1854 }
1855
1856 void mpd_startFieldSearch(mpd_Connection *connection, int type) {
1857 const char *strtype;
1858 int len;
1859
1860 if (connection->request != nullptr) {
1861 strncpy(connection->errorStr, "search already in progress",
1862 MPD_ERRORSTR_MAX_LENGTH);
1863 connection->error = 1;
1864 return;
1865 }
1866
1867 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1868 strncpy(connection->errorStr, "invalid type specified",
1869 MPD_ERRORSTR_MAX_LENGTH);
1870 connection->error = 1;
1871 return;
1872 }
1873
1874 strtype = mpdTagItemKeys[type];
1875
1876 len = 5 + strlen(strtype) + 1;
1877 connection->request = static_cast<char *>(malloc(len));
1878
1879 snprintf(connection->request, len, "list %c%s",
1880 tolower(static_cast<unsigned char>(strtype[0])), strtype + 1);
1881 }
1882
1883 void mpd_addConstraintSearch(mpd_Connection *connection, int type,
1884 const char *name) {
1885 const char *strtype;
1886 char *arg;
1887 int len;
1888 char *string;
1889
1890 if (connection->request == nullptr) {
1891 strncpy(connection->errorStr, "no search in progress",
1892 MPD_ERRORSTR_MAX_LENGTH);
1893 connection->error = 1;
1894 return;
1895 }
1896
1897 if (type < 0 || type >= MPD_TAG_NUM_OF_ITEM_TYPES) {
1898 strncpy(connection->errorStr, "invalid type specified",
1899 MPD_ERRORSTR_MAX_LENGTH);
1900 connection->error = 1;
1901 return;
1902 }
1903
1904 if (name == nullptr) {
1905 strncpy(connection->errorStr, "no name specified", MPD_ERRORSTR_MAX_LENGTH);
1906 connection->error = 1;
1907 return;
1908 }
1909
1910 string = strndup(connection->request, text_buffer_size.get(*state));
1911 strtype = mpdTagItemKeys[type];
1912 arg = mpd_sanitizeArg(name);
1913
1914 len = strlen(string) + 1 + strlen(strtype) + 2 + strlen(arg) + 2;
1915 connection->request = static_cast<char *>(realloc(connection->request, len));
1916 snprintf(connection->request, len, "%s %c%s \"%s\"", string,
1917 tolower(static_cast<unsigned char>(strtype[0])), strtype + 1, arg);
1918
1919 free(string);
1920 free(arg);
1921 }
1922
1923 void mpd_commitSearch(mpd_Connection *connection) {
1924 int len;
1925
1926 if (connection->request == nullptr) {
1927 strncpy(connection->errorStr, "no search in progress",
1928 MPD_ERRORSTR_MAX_LENGTH);
1929 connection->error = 1;
1930 return;
1931 }
1932
1933 len = strlen(connection->request) + 2;
1934 connection->request = static_cast<char *>(realloc(connection->request, len));
1935 connection->request[len - 2] = '\n';
1936 connection->request[len - 1] = '\0';
1937 mpd_sendInfoCommand(connection, connection->request);
1938
1939 free_and_zero(connection->request);
1940 }
1941
1942 /**
1943 * @param connection a MpdConnection
1944 * @param path the path to the playlist.
1945 *
1946 * List the content, with full metadata, of a stored playlist. */
1947 void mpd_sendListPlaylistInfoCommand(mpd_Connection *connection, char *path) {
1948 char *arg = mpd_sanitizeArg(path);
1949 int len = strlen("listplaylistinfo") + 2 + strlen(arg) + 3;
1950 auto *query = static_cast<char *>(malloc(len));
1951
1952 snprintf(query, len, "listplaylistinfo \"%s\"\n", arg);
1953 mpd_sendInfoCommand(connection, query);
1954 free(arg);
1955 free(query);
1956 }
1957
1958 /**
1959 * @param connection a MpdConnection
1960 * @param path the path to the playlist.
1961 *
1962 * List the content of a stored playlist. */
1963 void mpd_sendListPlaylistCommand(mpd_Connection *connection, char *path) {
1964 char *arg = mpd_sanitizeArg(path);
1965 int len = strlen("listplaylist") + 2 + strlen(arg) + 3;
1966 auto *query = static_cast<char *>(malloc(len));
1967
1968 snprintf(query, len, "listplaylist \"%s\"\n", arg);
1969 mpd_sendInfoCommand(connection, query);
1970 free(arg);
1971 free(query);
1972 }
1973
1974 void mpd_sendPlaylistClearCommand(mpd_Connection *connection, char *path) {
1975 char *sPath = mpd_sanitizeArg(path);
1976 int len = strlen("playlistclear") + 2 + strlen(sPath) + 3;
1977 auto *string = static_cast<char *>(malloc(len));
1978
1979 snprintf(string, len, "playlistclear \"%s\"\n", sPath);
1980 mpd_executeCommand(connection, string);
1981 free(sPath);
1982 free(string);
1983 }
1984
1985 void mpd_sendPlaylistAddCommand(mpd_Connection *connection, char *playlist,
1986 char *path) {
1987 char *sPlaylist = mpd_sanitizeArg(playlist);
1988 char *sPath = mpd_sanitizeArg(path);
1989 int len =
1990 strlen("playlistadd") + 2 + strlen(sPlaylist) + 3 + strlen(sPath) + 3;
1991 auto *string = static_cast<char *>(malloc(len));
1992
1993 snprintf(string, len, "playlistadd \"%s\" \"%s\"\n", sPlaylist, sPath);
1994 mpd_executeCommand(connection, string);
1995 free(sPlaylist);
1996 free(sPath);
1997 free(string);
1998 }
1999
2000 void mpd_sendPlaylistMoveCommand(mpd_Connection *connection, char *playlist,
2001 int from, int to) {
2002 char *sPlaylist = mpd_sanitizeArg(playlist);
2003 int len = strlen("playlistmove") + 2 + strlen(sPlaylist) + 3 + INTLEN + 3 +
2004 INTLEN + 3;
2005 auto *string = static_cast<char *>(malloc(len));
2006
2007 snprintf(string, len, "playlistmove \"%s\" \"%i\" \"%i\"\n", sPlaylist, from,
2008 to);
2009 mpd_executeCommand(connection, string);
2010 free(sPlaylist);
2011 free(string);
2012 }
2013
2014 void mpd_sendPlaylistDeleteCommand(mpd_Connection *connection, char *playlist,
2015 int pos) {
2016 char *sPlaylist = mpd_sanitizeArg(playlist);
2017 int len = strlen("playlistdelete") + 2 + strlen(sPlaylist) + 3 + INTLEN + 3;
2018 auto *string = static_cast<char *>(malloc(len));
2019
2020 snprintf(string, len, "playlistdelete \"%s\" \"%i\"\n", sPlaylist, pos);
2021 mpd_executeCommand(connection, string);
2022 free(sPlaylist);
2023 free(string);
2024 }
2025