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