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