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