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