Code

Renamed bootstrap.sh to autogen.sh
[ncmpc.git] / libmpdclient.c
1 /* libmpdclient
2  * (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu)
3  * This project's homepage is: http://www.musicpd.org
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
20 #include "libmpdclient.h"
22 #include <errno.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netdb.h>
26 #include <stdio.h>
27 #include <sys/param.h>
28 #include <string.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <unistd.h>
32 #include <stdlib.h>
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #ifndef HAVE_SOCKLEN_T
37 typedef int socklen_t;
38 #endif
39 #endif
41 #ifndef MPD_NO_IPV6
42 #ifdef AF_INET6
43 #define MPD_HAVE_IPV6
44 #endif
45 #endif
47 #ifdef MPD_HAVE_IPV6        
48 int mpd_ipv6Supported() {
49         int s;          
50         s = socket(AF_INET6,SOCK_STREAM,0);
51         if(s == -1) return 0;
52         close(s);       
53         return 1;                       
54 }                       
55 #endif  
58 char * mpd_sanitizeArg(const char * arg) {
59         size_t i;
60         int count=0;
61         char * ret;
63         for(i=0;i<strlen(arg);i++) {
64                 if(arg[i]=='"' || arg[i]=='\\') count++;
65         }
67         ret = malloc(strlen(arg)+count+1);
69         count = 0;
70         for(i=0;i<strlen(arg)+1;i++) {
71                 if(arg[i]=='"' || arg[i]=='\\') {
72                         ret[i+count] = '\\';
73                         count++;
74                 }
75                 ret[i+count] = arg[i];
76         }
78         return ret;
79 }
81 mpd_ReturnElement * mpd_newReturnElement(const char * name, const char * value)
82 {
83         mpd_ReturnElement * ret = malloc(sizeof(mpd_ReturnElement));
85         ret->name = strdup(name);
86         ret->value = strdup(value);
88         return ret;
89 }
91 void mpd_freeReturnElement(mpd_ReturnElement * re) {
92         free(re->name);
93         free(re->value);
94         free(re);
95 }
97 void mpd_setConnectionTimeout(mpd_Connection * connection, float timeout) {
98                 connection->timeout.tv_sec = (int)timeout;
99                 connection->timeout.tv_usec = (int)(timeout*1e6 -
100                                 connection->timeout.tv_sec*1000000+0.5);
103 mpd_Connection * mpd_newConnection(const char * host, int port, float timeout) {
104         int err;
105         struct hostent * he;
106         struct sockaddr * dest;
107         socklen_t destlen;
108         struct sockaddr_in sin;
109         char * rt;
110         char * output;
111         mpd_Connection * connection = malloc(sizeof(mpd_Connection));
112         struct timeval tv;
113         fd_set fds;
114 #ifdef MPD_HAVE_IPV6
115         struct sockaddr_in6 sin6;
116 #endif
117         strcpy(connection->buffer,"");
118         connection->buflen = 0;
119         connection->bufstart = 0;
120         strcpy(connection->errorStr,"");
121         connection->error = 0;
122         connection->doneProcessing = 0;
123         connection->commandList = 0;
124         connection->returnElement = NULL;
126         if(!(he=gethostbyname(host))) {
127                 snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH,
128                                 "host \"%s\" not found",host);
129                 connection->error = MPD_ERROR_UNKHOST;
130                 return connection;
131         }
133         memset(&sin,0,sizeof(struct sockaddr_in));
134         /*dest.sin_family = he->h_addrtype;*/
135         sin.sin_family = AF_INET;
136         sin.sin_port = htons(port);
137 #ifdef MPD_HAVE_IPV6
138         memset(&sin6,0,sizeof(struct sockaddr_in6));
139         sin6.sin6_family = AF_INET6;
140         sin6.sin6_port = htons(port);
141 #endif
142         switch(he->h_addrtype) {
143         case AF_INET:
144                 memcpy((char *)&sin.sin_addr.s_addr,(char *)he->h_addr,
145                                 he->h_length);
146                 dest = (struct sockaddr *)&sin;
147                 destlen = sizeof(struct sockaddr_in);
148                 break;
149 #ifdef MPD_HAVE_IPV6
150         case AF_INET6:
151                 if(!mpd_ipv6Supported()) {
152                         strcpy(connection->errorStr,"no IPv6 suuport but a "
153                                         "IPv6 address found\n");
154                         connection->error = MPD_ERROR_SYSTEM;
155                         return connection;
156                 }
157                 memcpy((char *)&sin6.sin6_addr.s6_addr,(char *)he->h_addr,
158                                 he->h_length);
159                 dest = (struct sockaddr *)&sin6;
160                 destlen = sizeof(struct sockaddr_in6);
161                 break;
162 #endif
163         default:
164                 strcpy(connection->errorStr,"address type is not IPv4 or "
165                                 "IPv6\n");
166                 connection->error = MPD_ERROR_SYSTEM;
167                 return connection;
168                 break;
169         }
170         
171         if((connection->sock = socket(dest->sa_family,SOCK_STREAM,0))<0) {
172                 strcpy(connection->errorStr,"problems creating socket");
173                 connection->error = MPD_ERROR_SYSTEM;
174                 return connection;
175         }
177         /* connect stuff */
178         {
179 #ifdef SO_RCVTIMEO
180                 struct timeval rcvoldto;
181                 struct timeval sndoldto;
182                 socklen_t oldlen = sizeof(struct timeval);
184                 mpd_setConnectionTimeout(connection,timeout);
186                 tv.tv_sec = connection->timeout.tv_sec;
187                 tv.tv_usec = connection->timeout.tv_usec;
189                 if(getsockopt(connection->sock,SOL_SOCKET,SO_RCVTIMEO,&rcvoldto,
190                                         &oldlen)<0 ||
191                                 getsockopt(connection->sock,SOL_SOCKET,
192                                         SO_SNDTIMEO,&sndoldto,&oldlen)<0)
193                 {
194                         strcpy(connection->errorStr,"problems getting socket "
195                                         "timeout\n");
196                         connection->error = MPD_ERROR_SYSTEM;
197                         return connection;
198                 }
199                 if(setsockopt(connection->sock,SOL_SOCKET,SO_RCVTIMEO,&tv,
200                                         sizeof(struct timeval))<0 ||
201                                 setsockopt(connection->sock,SOL_SOCKET,
202                                         SO_SNDTIMEO,&tv,
203                                         sizeof(struct timeval))<0)
204                 {
205                         strcpy(connection->errorStr,"problems setting socket "
206                                         "timeout\n");
207                         connection->error = MPD_ERROR_SYSTEM;
208                         return connection;
209                 }
210 #endif
211                 if(connect(connection->sock,dest,destlen)<0) {
212                         snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH,
213                                         "problems connecting to \"%s\" on port"
214                                         " %i",host,port);
215                         connection->error = MPD_ERROR_CONNPORT;
216                         return connection;
217                 }
218 #ifdef SO_RCVTIMEO
219                 if(setsockopt(connection->sock,SOL_SOCKET,SO_SNDTIMEO,&rcvoldto,
220                                         sizeof(struct timeval))<0 ||
221                                 setsockopt(connection->sock,SOL_SOCKET,
222                                         SO_SNDTIMEO,&sndoldto,
223                                         sizeof(struct timeval))<0)
224                 {
225                         strcpy(connection->errorStr,"problems setting socket "
226                                         "timeout\n");
227                         connection->error = MPD_ERROR_SYSTEM;
228                         return connection;
229                 }
230 #endif
231         }
233         while(!(rt = strstr(connection->buffer,"\n"))) {
234                 tv.tv_sec = connection->timeout.tv_sec;
235                 tv.tv_usec = connection->timeout.tv_usec;
236                 FD_ZERO(&fds);
237                 FD_SET(connection->sock,&fds);
238                 if((err = select(connection->sock+1,&fds,NULL,NULL,&tv)) == 1) {
239                         int readed;
240                         readed = recv(connection->sock,
241                                 &(connection->buffer[connection->buflen]),
242                                 MPD_BUFFER_MAX_LENGTH-connection->buflen,0);
243                         if(readed<=0) {
244                                 snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH,
245                                         "problems getting a response from"
246                                         " \"%s\" on port %i",host,
247                                         port);
248                                 connection->error = MPD_ERROR_NORESPONSE;
249                                 return connection;
250                         }
251                         connection->buflen+=readed;
252                         connection->buffer[connection->buflen] = '\0';
253                         tv.tv_sec = connection->timeout.tv_sec;
254                         tv.tv_usec = connection->timeout.tv_usec;
255                 }
256                 else if(err<0 && errno==EINTR) continue;
257                 else {
258                         snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH,
259                                 "timeout in attempting to get a response from"
260                                  " \"%s\" on port %i",host,port);
261                         connection->error = MPD_ERROR_NORESPONSE;
262                         return connection;
263                 }
264         }
266         *rt = '\0';
267         output = strdup(connection->buffer);
268         strcpy(connection->buffer,rt+1);
269         connection->buflen = strlen(connection->buffer);
271         if(strncmp(output,MPD_WELCOME_MESSAGE,strlen(MPD_WELCOME_MESSAGE))) {
272                 free(output);
273                 snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH,
274                                 "mpd not running on port %i on host \"%s\"",
275                                 port,host);
276                 connection->error = MPD_ERROR_NOTMPD;
277                 return connection;
278         }
280         {
281                 char * test;
282                 char * version[3];
283                 char * tmp = &output[strlen(MPD_WELCOME_MESSAGE)];
284                 char * search = ".";
285                 int i;
287                 for(i=0;i<3;i++) {
288                         char * tok;
289                         if(i==3) search = " ";
290                         version[i] = strtok_r(tmp,search,&tok);
291                         if(!version[i]) {
292                                 free(output);
293                                 snprintf(connection->errorStr,
294                                         MPD_BUFFER_MAX_LENGTH,
295                                         "error parsing version number at "
296                                         "\"%s\"",
297                                         &output[strlen(MPD_WELCOME_MESSAGE)]);
298                                 connection->error = MPD_ERROR_NOTMPD;
299                                 return connection;
300                         }
301                         connection->version[i] = strtol(version[i],&test,10);
302                         if(version[i]==test || *test!='\0') {
303                                 free(output);
304                                 snprintf(connection->errorStr,
305                                         MPD_BUFFER_MAX_LENGTH,
306                                         "error parsing version number at "
307                                         "\"%s\"",
308                                         &output[strlen(MPD_WELCOME_MESSAGE)]);
309                                 connection->error = MPD_ERROR_NOTMPD;
310                                 return connection;
311                         }
312                         tmp = NULL;
313                 }
314         }
316         free(output);
318         connection->doneProcessing = 1;
320         return connection;
323 void mpd_clearError(mpd_Connection * connection) {
324         connection->error = 0;
325         connection->errorStr[0] = '\0';
328 void mpd_closeConnection(mpd_Connection * connection) {
329         close(connection->sock);
330         if(connection->returnElement) free(connection->returnElement);
331         free(connection);
334 void mpd_executeCommand(mpd_Connection * connection, char * command) {
335         int ret;
336         struct timeval tv;
337         fd_set fds;
338         char * commandPtr = command;
339         int commandLen = strlen(command);
341         if(!connection->doneProcessing && !connection->commandList) {
342                 strcpy(connection->errorStr,"not done processing current command");
343                 connection->error = 1;
344                 return;
345         }
347         mpd_clearError(connection);
349         FD_ZERO(&fds);
350         FD_SET(connection->sock,&fds);
351         tv.tv_sec = connection->timeout.tv_sec;
352         tv.tv_usec = connection->timeout.tv_usec;
354         while((ret = select(connection->sock+1,NULL,&fds,NULL,&tv)==1) || 
355                         (ret==-1 && errno==EINTR)) {
356                 ret = send(connection->sock,commandPtr,commandLen,
357                                 MSG_DONTWAIT);
358                 if(ret<=0)
359                 {
360                         if(ret==EAGAIN || ret==EINTR) continue;
361                         snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH,
362                                 "problems giving command \"%s\"",command);
363                         connection->error = MPD_ERROR_SENDING;
364                         return;
365                 }
366                 else {
367                         commandPtr+=ret;
368                         commandLen-=ret;
369                 }
371                 if(commandLen<=0) break;
372         }
374         if(commandLen>0) {
375                 perror("");
376                 snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH,
377                         "timeout sending command \"%s\"",command);
378                 connection->error = MPD_ERROR_TIMEOUT;
379                 return;
380         }
382         if(!connection->commandList) connection->doneProcessing = 0;
385 void mpd_getNextReturnElement(mpd_Connection * connection) {
386         char * output = NULL;
387         char * rt = NULL;
388         char * name;
389         char * value;
390         fd_set fds;
391         struct timeval tv;
392         char * tok;
393         int readed;
394         char * bufferCheck;
395         int err;
397         if(connection->returnElement) mpd_freeReturnElement(connection->returnElement);
398         connection->returnElement = NULL;
400         if(connection->doneProcessing) {
401                 strcpy(connection->errorStr,"already done processing current command");
402                 connection->error = 1;
403                 return;
404         }
406         bufferCheck = connection->buffer+connection->bufstart;
407         while(connection->bufstart>=connection->buflen || 
408                         !(rt = strstr(bufferCheck,"\n"))) {
409                 if(connection->buflen>=MPD_BUFFER_MAX_LENGTH) {
410                         memmove(connection->buffer,
411                                         connection->buffer+
412                                         connection->bufstart,
413                                         connection->buflen-
414                                         connection->bufstart+1);
415                         bufferCheck-=connection->bufstart;
416                         connection->buflen-=connection->bufstart;
417                         connection->bufstart = 0;
418                 }
419                 if(connection->buflen>=MPD_BUFFER_MAX_LENGTH) {
420                         strcpy(connection->errorStr,"buffer overrun");
421                         connection->error = MPD_ERROR_BUFFEROVERRUN;
422                         connection->doneProcessing = 1;
423                         return;
424                 }
425                 bufferCheck+=connection->buflen-connection->bufstart;
426                 tv.tv_sec = connection->timeout.tv_sec;
427                 tv.tv_usec = connection->timeout.tv_usec;
428                 FD_ZERO(&fds);
429                 FD_SET(connection->sock,&fds);
430                 if((err = select(connection->sock+1,&fds,NULL,NULL,&tv) == 1)) {
431                         readed = recv(connection->sock,
432                                 connection->buffer+connection->buflen,
433                                 MPD_BUFFER_MAX_LENGTH-connection->buflen,
434                                 MSG_DONTWAIT);
435                         if(readed<0 && (errno==EAGAIN || errno==EINTR)) {
436                                 continue;
437                         }
438                         if(readed<=0) {
439                                 strcpy(connection->errorStr,"connection"
440                                         " closed");
441                                 connection->error = MPD_ERROR_CONNCLOSED;
442                                 connection->doneProcessing = 1;
443                                 return;
444                         }
445                         connection->buflen+=readed;
446                         connection->buffer[connection->buflen] = '\0';
447                 }
448                 else if(err<0 && errno==EINTR) continue;
449                 else {
450                         strcpy(connection->errorStr,"connection timeout");
451                         connection->error = MPD_ERROR_TIMEOUT;
452                         connection->doneProcessing = 1;
453                         return;
454                 }
455         }
457         *rt = '\0';
458         output = connection->buffer+connection->bufstart;
459         connection->bufstart = rt - connection->buffer + 1;
461         if(strcmp(output,"OK")==0) {
462                 connection->doneProcessing = 1;
463                 return;
464         }
465         if(strncmp(output,"ACK",strlen("ACK"))==0) {
466                 strcpy(connection->errorStr,output);
467                 connection->error = MPD_ERROR_ACK;
468                 connection->doneProcessing = 1;
469                 return;
470         }
472         name = strtok_r(output,":",&tok);
473         if(name && (value = strtok_r(NULL,"",&tok)) && value[0]==' ') {
474                 connection->returnElement = mpd_newReturnElement(name,&(value[1]));
475         }
476         else {
477                 if(!name || !value) {
478                         snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH,
479                                         "error parsing: %s",output);
480                 }
481                 else {
482                         snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH,
483                                         "error parsing: %s:%s",name,value);
484                 }
485                 connection->errorStr[MPD_BUFFER_MAX_LENGTH] = '\0';
486                 connection->error = 1;
487         }
490 void mpd_finishCommand(mpd_Connection * connection) {
491         while(!connection->doneProcessing) mpd_getNextReturnElement(connection);
495 mpd_Status * mpd_getStatus(mpd_Connection * connection) {
496         mpd_Status * status;
498         mpd_executeCommand(connection,"status\n");
499                 
500         if(connection->error) return NULL;
502         status = malloc(sizeof(mpd_Status));
503         status->volume = -1;
504         status->repeat = 0;
505         status->random = 0;
506         status->playlist = -1;
507         status->playlistLength = -1;
508         status->state = -1;
509         status->song = 0;
510         status->elapsedTime = 0;
511         status->totalTime = 0;
512         status->bitRate = 0;
513         status->sampleRate = 0;
514         status->bits = 0;
515         status->channels = 0;
516         status->crossfade = -1;
517         status->error = NULL;
519         mpd_getNextReturnElement(connection);
520         if(connection->error) {
521                 free(status);
522                 return NULL;
523         }
524         while(connection->returnElement) {
525                 mpd_ReturnElement * re = connection->returnElement;
526                 if(strcmp(re->name,"volume")==0) {
527                         status->volume = atoi(re->value);
528                 }
529                 else if(strcmp(re->name,"repeat")==0) {
530                         status->repeat = atoi(re->value);
531                 }
532                 else if(strcmp(re->name,"random")==0) {
533                         status->random = atoi(re->value);
534                 }
535                 else if(strcmp(re->name,"playlist")==0) {
536                         status->playlist = strtol(re->value,NULL,10);
537                 }
538                 else if(strcmp(re->name,"playlistlength")==0) {
539                         status->playlistLength = atoi(re->value);
540                 }
541                 else if(strcmp(re->name,"bitrate")==0) {
542                         status->bitRate = atoi(re->value);
543                 }
544                 else if(strcmp(re->name,"state")==0) {
545                         if(strcmp(re->value,"play")==0) {
546                                 status->state = MPD_STATUS_STATE_PLAY;
547                         }
548                         else if(strcmp(re->value,"stop")==0) {
549                                 status->state = MPD_STATUS_STATE_STOP;
550                         }
551                         else if(strcmp(re->value,"pause")==0) {
552                                 status->state = MPD_STATUS_STATE_PAUSE;
553                         }
554                         else {
555                                 status->state = MPD_STATUS_STATE_UNKNOWN;
556                         }
557                 }
558                 else if(strcmp(re->name,"song")==0) {
559                         status->song = atoi(re->value);
560                 }
561                 else if(strcmp(re->name,"time")==0) {
562                         char * tok;
563                         char * copy;
564                         copy = strdup(re->value);
565                         status->elapsedTime = atoi(strtok_r(copy,":",&tok));
566                         status->totalTime = atoi(strtok_r(NULL,"",&tok));
567                         free(copy);
568                 }
569                 else if(strcmp(re->name,"error")==0) {
570                         status->error = strdup(re->value);
571                 }
572                 else if(strcmp(re->name,"xfade")==0) {
573                         status->crossfade = atoi(re->value);
574                 }
575                 else if(strcmp(re->name,"audio")==0) {
576                         char * tok;
577                         char * copy;
578                         copy = strdup(re->value);
579                         status->sampleRate = atoi(strtok_r(copy,":",&tok));
580                         status->bits = atoi(strtok_r(NULL,":",&tok));
581                         status->channels = atoi(strtok_r(NULL,"",&tok));
582                         free(copy);
583                 }
585                 mpd_getNextReturnElement(connection);
586                 if(connection->error) {
587                         free(status);
588                         return NULL;
589                 }
590         }
592         if(connection->error) {
593                 free(status);
594                 return NULL;
595         }
596         else if(status->state<0) {
597                 strcpy(connection->errorStr,"state not found");
598                 connection->error = 1;
599                 free(status);
600                 return NULL;
601         }
603         return status;
606 void mpd_freeStatus(mpd_Status * status) {
607         if(status->error) free(status->error);
608         free(status);
611 mpd_Stats * mpd_getStats(mpd_Connection * connection) {
612         mpd_Stats * stats;
614         mpd_executeCommand(connection,"stats\n");
615                 
616         if(connection->error) return NULL;
618         stats = malloc(sizeof(mpd_Stats));
619         stats->numberOfArtists = 0;
620         stats->numberOfAlbums = 0;
621         stats->numberOfSongs = 0;
622         stats->uptime = 0;
623         stats->dbUpdateTime = 0;
625         mpd_getNextReturnElement(connection);
626         if(connection->error) {
627                 free(stats);
628                 return NULL;
629         }
630         while(connection->returnElement) {
631                 mpd_ReturnElement * re = connection->returnElement;
632                 if(strcmp(re->name,"artists")==0) {
633                         stats->numberOfArtists = atoi(re->value);
634                 }
635                 else if(strcmp(re->name,"albums")==0) {
636                         stats->numberOfAlbums = atoi(re->value);
637                 }
638                 else if(strcmp(re->name,"songs")==0) {
639                         stats->numberOfSongs = atoi(re->value);
640                 }
641                 else if(strcmp(re->name,"uptime")==0) {
642                         stats->uptime = strtol(re->value,NULL,10);
643                 }
644                 else if(strcmp(re->name,"db_update")==0) {
645                         stats->dbUpdateTime = strtol(re->value,NULL,10);
646                 }
648                 mpd_getNextReturnElement(connection);
649                 if(connection->error) {
650                         free(stats);
651                         return NULL;
652                 }
653         }
655         if(connection->error) {
656                 free(stats);
657                 return NULL;
658         }
660         return stats;
663 void mpd_freeStats(mpd_Stats * stats) {
664         free(stats);
667 void mpd_initSong(mpd_Song * song) {
668         song->file = NULL;
669         song->artist = NULL;
670         song->album = NULL;
671         song->track = NULL;
672         song->title = NULL;
673         song->time = MPD_SONG_NO_TIME;
676 void mpd_finishSong(mpd_Song * song) {
677         if(song->file) free(song->file);
678         if(song->artist) free(song->artist);
679         if(song->album) free(song->album);
680         if(song->title) free(song->title);
681         if(song->track) free(song->track);
684 mpd_Song * mpd_newSong() {
685         mpd_Song * ret = malloc(sizeof(mpd_Song));
687         mpd_initSong(ret);
689         return ret;
692 void mpd_freeSong(mpd_Song * song) {
693         mpd_finishSong(song);
694         free(song);
697 mpd_Song * mpd_songDup(mpd_Song * song) {
698         mpd_Song * ret = mpd_newSong();
700         if(song->file) ret->file = strdup(song->file);
701         if(song->artist) ret->artist = strdup(song->artist);
702         if(song->album) ret->album = strdup(song->album);
703         if(song->title) ret->title = strdup(song->title);
704         if(song->track) ret->track = strdup(song->track);
705         ret->time = song->time;
707         return ret;
710 void mpd_initDirectory(mpd_Directory * directory) {
711         directory->path = NULL;
714 void mpd_finishDirectory(mpd_Directory * directory) {
715         if(directory->path) free(directory->path);
718 mpd_Directory * mpd_newDirectory () {
719         mpd_Directory * directory = malloc(sizeof(mpd_Directory));;
721         mpd_initDirectory(directory);
722         
723         return directory;
726 void mpd_freeDirectory(mpd_Directory * directory) {
727         mpd_finishDirectory(directory);
729         free(directory);
732 mpd_Directory * mpd_directoryDup(mpd_Directory * directory) {
733         mpd_Directory * ret = mpd_newDirectory();
735         if(directory->path) ret->path = strdup(directory->path);
737         return ret;
740 void mpd_initPlaylistFile(mpd_PlaylistFile * playlist) {
741         playlist->path = NULL;
744 void mpd_finishPlaylistFile(mpd_PlaylistFile * playlist) {
745         if(playlist->path) free(playlist->path);
748 mpd_PlaylistFile * mpd_newPlaylistFile() {
749         mpd_PlaylistFile * playlist = malloc(sizeof(mpd_PlaylistFile));
751         mpd_initPlaylistFile(playlist);
753         return playlist;
756 void mpd_freePlaylistFile(mpd_PlaylistFile * playlist) {
757         mpd_finishPlaylistFile(playlist);
758         free(playlist);
761 mpd_PlaylistFile * mpd_playlistFileDup(mpd_PlaylistFile * playlist) {
762         mpd_PlaylistFile * ret = mpd_newPlaylistFile();
764         if(playlist->path) ret->path = strdup(playlist->path);
766         return ret;
769 void mpd_initInfoEntity(mpd_InfoEntity * entity) {
770         entity->info.directory = NULL;
771
773 void mpd_finishInfoEntity(mpd_InfoEntity * entity) {
774         if(entity->info.directory) {
775                 if(entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
776                         mpd_freeDirectory(entity->info.directory);
777                 }
778                 else if(entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
779                         mpd_freeSong(entity->info.song);
780                 }
781                 else if(entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
782                         mpd_freePlaylistFile(entity->info.playlistFile);
783                 }
784         }
787 mpd_InfoEntity * mpd_newInfoEntity() {
788         mpd_InfoEntity * entity = malloc(sizeof(mpd_InfoEntity));
789         
790         mpd_initInfoEntity(entity);
792         return entity;
795 void mpd_freeInfoEntity(mpd_InfoEntity * entity) {
796         mpd_finishInfoEntity(entity);
797         free(entity);
800 void mpd_sendInfoCommand(mpd_Connection * connection, char * command) {
801         mpd_executeCommand(connection,command);
804 mpd_InfoEntity * mpd_getNextInfoEntity(mpd_Connection * connection) {
805         mpd_InfoEntity * entity = NULL;
807         if(connection->doneProcessing) return NULL;
809         if(!connection->returnElement) mpd_getNextReturnElement(connection);
811         if(connection->returnElement) { 
812                 if(strcmp(connection->returnElement->name,"file")==0) {
813                         entity = mpd_newInfoEntity();
814                         entity->type = MPD_INFO_ENTITY_TYPE_SONG;
815                         entity->info.song = mpd_newSong();
816                         entity->info.song->file = 
817                                 strdup(connection->returnElement->value);
818                 }
819                 else if(strcmp(connection->returnElement->name,
820                                         "directory")==0) {
821                         entity = mpd_newInfoEntity();
822                         entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
823                         entity->info.directory = mpd_newDirectory();
824                         entity->info.directory->path = 
825                                 strdup(connection->returnElement->value);
826                 }
827                 else if(strcmp(connection->returnElement->name,"playlist")==0) {
828                         entity = mpd_newInfoEntity();
829                         entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
830                         entity->info.playlistFile = mpd_newPlaylistFile();
831                         entity->info.playlistFile->path = 
832                                 strdup(connection->returnElement->value);
833                 }
834                 else {
835                         connection->error = 1;
836                         strcpy(connection->errorStr,"problem parsing song info");
837                         return NULL;
838                 }
839         }
840         else return NULL;
842         mpd_getNextReturnElement(connection);
843         while(connection->returnElement) {
844                 mpd_ReturnElement * re = connection->returnElement;
846                 if(strcmp(re->name,"file")==0) return entity;
847                 else if(strcmp(re->name,"directory")==0) return entity;
848                 else if(strcmp(re->name,"playlist")==0) return entity;
850                 if(entity->type == MPD_INFO_ENTITY_TYPE_SONG && 
851                                 strlen(re->value)) {
852                         if(!entity->info.song->artist &&
853                                         strcmp(re->name,"Artist")==0) {
854                                 entity->info.song->artist = strdup(re->value);
855                         }
856                         else if(!entity->info.song->album &&
857                                         strcmp(re->name,"Album")==0) {
858                                 entity->info.song->album = strdup(re->value);
859                         }
860                         else if(!entity->info.song->title &&
861                                         strcmp(re->name,"Title")==0) {
862                                 entity->info.song->title = strdup(re->value);
863                         }
864                         else if(!entity->info.song->track &&
865                                         strcmp(re->name,"Track")==0) {
866                                 entity->info.song->track = strdup(re->value);
867                         }
868                         else if(entity->info.song->time==MPD_SONG_NO_TIME &&
869                                         strcmp(re->name,"Time")==0) {
870                                 entity->info.song->time = atoi(re->value);
871                         }
872                 }
873                 else if(entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
874                 }
875                 else if(entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
876                 }
878                 mpd_getNextReturnElement(connection);
879         }
881         return entity;
884 char * mpd_getNextReturnElementNamed(mpd_Connection * connection, 
885                 const char * name) 
887         if(connection->doneProcessing) return NULL;
889         mpd_getNextReturnElement(connection);
890         while(connection->returnElement) {
891                 mpd_ReturnElement * re = connection->returnElement;
893                 if(strcmp(re->name,name)==0) return strdup(re->value);
894                 mpd_getNextReturnElement(connection);
895         }
897         return NULL;
900 char * mpd_getNextArtist(mpd_Connection * connection) {
901         return mpd_getNextReturnElementNamed(connection,"Artist");
904 char * mpd_getNextAlbum(mpd_Connection * connection) {
905         return mpd_getNextReturnElementNamed(connection,"Album");
908 void mpd_sendPlaylistInfoCommand(mpd_Connection * connection, int songNum) {
909         char * string = malloc(strlen("playlistinfo")+25);
910         sprintf(string,"playlistinfo \"%i\"\n",songNum);
911         mpd_sendInfoCommand(connection,string);
912         free(string);
915 void mpd_sendListallCommand(mpd_Connection * connection, const char * dir) {
916         char * sDir = mpd_sanitizeArg(dir);
917         char * string = malloc(strlen("listall")+strlen(sDir)+5);
918         sprintf(string,"listall \"%s\"\n",sDir);
919         mpd_sendInfoCommand(connection,string);
920         free(string);
921         free(sDir);
924 void mpd_sendListallInfoCommand(mpd_Connection * connection, const char * dir) {
925         char * sDir = mpd_sanitizeArg(dir);
926         char * string = malloc(strlen("listallinfo")+strlen(sDir)+5);
927         sprintf(string,"listallinfo \"%s\"\n",sDir);
928         mpd_sendInfoCommand(connection,string);
929         free(string);
930         free(sDir);
933 void mpd_sendLsInfoCommand(mpd_Connection * connection, const char * dir) {
934         char * sDir = mpd_sanitizeArg(dir);
935         char * string = malloc(strlen("lsinfo")+strlen(sDir)+5);
936         sprintf(string,"lsinfo \"%s\"\n",sDir);
937         mpd_sendInfoCommand(connection,string);
938         free(string);
939         free(sDir);
942 void mpd_sendSearchCommand(mpd_Connection * connection, int table, 
943                 const char * str) 
945         char st[10];
946         char * string;
947         char * sanitStr = mpd_sanitizeArg(str);
948         if(table == MPD_TABLE_ARTIST) strcpy(st,"artist");
949         else if(table == MPD_TABLE_ALBUM) strcpy(st,"album");
950         else if(table == MPD_TABLE_TITLE) strcpy(st,"title");
951         else if(table == MPD_TABLE_FILENAME) strcpy(st,"filename");
952         else {
953                 connection->error = 1;
954                 strcpy(connection->errorStr,"unknown table for search");
955                 return;
956         }
957         string = malloc(strlen("search")+strlen(sanitStr)+strlen(st)+6);
958         sprintf(string,"search %s \"%s\"\n",st,sanitStr);
959         mpd_sendInfoCommand(connection,string);
960         free(string);
961         free(sanitStr);
964 void mpd_sendFindCommand(mpd_Connection * connection, int table, 
965                 const char * str) 
967         char st[10];
968         char * string;
969         char * sanitStr = mpd_sanitizeArg(str);
970         if(table == MPD_TABLE_ARTIST) strcpy(st,"artist");
971         else if(table == MPD_TABLE_ALBUM) strcpy(st,"album");
972         else if(table == MPD_TABLE_TITLE) strcpy(st,"title");
973         else {
974                 connection->error = 1;
975                 strcpy(connection->errorStr,"unknown table for find");
976                 return;
977         }
978         string = malloc(strlen("find")+strlen(sanitStr)+strlen(st)+6);
979         sprintf(string,"find %s \"%s\"\n",st,sanitStr);
980         mpd_sendInfoCommand(connection,string);
981         free(string);
982         free(sanitStr);
985 void mpd_sendListCommand(mpd_Connection * connection, int table, 
986                 const char * arg1) 
988         char st[10];
989         char * string;
990         if(table == MPD_TABLE_ARTIST) strcpy(st,"artist");
991         else if(table == MPD_TABLE_ALBUM) strcpy(st,"album");
992         else {
993                 connection->error = 1;
994                 strcpy(connection->errorStr,"unknown table for list");
995                 return;
996         }
997         if(arg1) {
998                 char * sanitArg1 = mpd_sanitizeArg(arg1);
999                 string = malloc(strlen("list")+strlen(sanitArg1)+strlen(st)+6);
1000                 sprintf(string,"list %s \"%s\"\n",st,sanitArg1);
1001                 free(sanitArg1);
1002         }
1003         else {
1004                 string = malloc(strlen("list")+strlen(st)+3);
1005                 sprintf(string,"list %s\n",st);
1006         }
1007         mpd_sendInfoCommand(connection,string);
1008         free(string);
1011 void mpd_sendAddCommand(mpd_Connection * connection, const char * file) {
1012         char * sFile = mpd_sanitizeArg(file);
1013         char * string = malloc(strlen("add")+strlen(sFile)+5);
1014         sprintf(string,"add \"%s\"\n",sFile);
1015         mpd_executeCommand(connection,string);
1016         free(string);
1017         free(sFile);
1020 void mpd_sendDeleteCommand(mpd_Connection * connection, int songNum) {
1021         char * string = malloc(strlen("delete")+25);
1022         sprintf(string,"delete \"%i\"\n",songNum);
1023         mpd_sendInfoCommand(connection,string);
1024         free(string);
1027 void mpd_sendSaveCommand(mpd_Connection * connection, const char * name) {
1028         char * sName = mpd_sanitizeArg(name);
1029         char * string = malloc(strlen("save")+strlen(sName)+5);
1030         sprintf(string,"save \"%s\"\n",sName);
1031         mpd_executeCommand(connection,string);
1032         free(string);
1033         free(sName);
1036 void mpd_sendLoadCommand(mpd_Connection * connection, const char * name) {
1037         char * sName = mpd_sanitizeArg(name);
1038         char * string = malloc(strlen("load")+strlen(sName)+5);
1039         sprintf(string,"load \"%s\"\n",sName);
1040         mpd_executeCommand(connection,string);
1041         free(string);
1042         free(sName);
1045 void mpd_sendRmCommand(mpd_Connection * connection, const char * name) {
1046         char * sName = mpd_sanitizeArg(name);
1047         char * string = malloc(strlen("rm")+strlen(sName)+5);
1048         sprintf(string,"rm \"%s\"\n",sName);
1049         mpd_executeCommand(connection,string);
1050         free(string);
1051         free(sName);
1054 void mpd_sendShuffleCommand(mpd_Connection * connection) {
1055         mpd_executeCommand(connection,"shuffle\n");
1058 void mpd_sendClearCommand(mpd_Connection * connection) {
1059         mpd_executeCommand(connection,"clear\n");
1062 void mpd_sendPlayCommand(mpd_Connection * connection, int songNum) {
1063         char * string = malloc(strlen("play")+25);
1064         sprintf(string,"play \"%i\"\n",songNum);
1065         mpd_sendInfoCommand(connection,string);
1066         free(string);
1069 void mpd_sendStopCommand(mpd_Connection * connection) {
1070         mpd_executeCommand(connection,"stop\n");
1073 void mpd_sendPauseCommand(mpd_Connection * connection) {
1074         mpd_executeCommand(connection,"pause\n");
1077 void mpd_sendNextCommand(mpd_Connection * connection) {
1078         mpd_executeCommand(connection,"next\n");
1081 void mpd_sendMoveCommand(mpd_Connection * connection, int from, int to) {
1082         char * string = malloc(strlen("move")+25);
1083         sprintf(string,"move \"%i\" \"%i\"\n",from,to);
1084         mpd_sendInfoCommand(connection,string);
1085         free(string);
1088 void mpd_sendSwapCommand(mpd_Connection * connection, int song1, int song2) {
1089         char * string = malloc(strlen("swap")+25);
1090         sprintf(string,"swap \"%i\" \"%i\"\n",song1,song2);
1091         mpd_sendInfoCommand(connection,string);
1092         free(string);
1095 void mpd_sendSeekCommand(mpd_Connection * connection, int song, int time) {
1096         char * string = malloc(strlen("seek")+25);
1097         sprintf(string,"seek \"%i\" \"%i\"\n",song,time);
1098         mpd_sendInfoCommand(connection,string);
1099         free(string);
1102 void mpd_sendUpdateCommand(mpd_Connection * connection) {
1103         mpd_executeCommand(connection,"update\n");
1106 void mpd_sendPrevCommand(mpd_Connection * connection) {
1107         mpd_executeCommand(connection,"previous\n");
1110 void mpd_sendRepeatCommand(mpd_Connection * connection, int repeatMode) {
1111         char * string = malloc(strlen("repeat")+25);
1112         sprintf(string,"repeat \"%i\"\n",repeatMode);
1113         mpd_executeCommand(connection,string);
1114         free(string);
1117 void mpd_sendRandomCommand(mpd_Connection * connection, int randomMode) {
1118         char * string = malloc(strlen("random")+25);
1119         sprintf(string,"random \"%i\"\n",randomMode);
1120         mpd_executeCommand(connection,string);
1121         free(string);
1124 void mpd_sendSetvolCommand(mpd_Connection * connection, int volumeChange) {
1125         char * string = malloc(strlen("setvol")+25);
1126         sprintf(string,"setvol \"%i\"\n",volumeChange);
1127         mpd_executeCommand(connection,string);
1128         free(string);
1131 void mpd_sendVolumeCommand(mpd_Connection * connection, int volumeChange) {
1132         char * string = malloc(strlen("volume")+25);
1133         sprintf(string,"volume \"%i\"\n",volumeChange);
1134         mpd_executeCommand(connection,string);
1135         free(string);
1138 void mpd_sendCrossfadeCommand(mpd_Connection * connection, int seconds) {
1139         char * string = malloc(strlen("crossfade")+25);
1140         sprintf(string,"crossfade \"%i\"\n",seconds);
1141         mpd_executeCommand(connection,string);
1142         free(string);
1145 void mpd_sendPasswordCommand(mpd_Connection * connection, const char * pass) {
1146         char * sPass = mpd_sanitizeArg(pass);
1147         char * string = malloc(strlen("password")+strlen(sPass)+5);
1148         sprintf(string,"password \"%s\"\n",sPass);
1149         mpd_executeCommand(connection,string);
1150         free(string);
1151         free(sPass);
1154 void mpd_sendCommandListBegin(mpd_Connection * connection) {
1155         if(connection->commandList) {
1156                 strcpy(connection->errorStr,"already in command list mode");
1157                 connection->error = 1;
1158                 return;
1159         }
1160         connection->commandList = 1;
1161         mpd_executeCommand(connection,"command_list_begin\n");
1164 void mpd_sendCommandListEnd(mpd_Connection * connection) {
1165         if(!connection->commandList) {
1166                 strcpy(connection->errorStr,"not in command list mode");
1167                 connection->error = 1;
1168                 return;
1169         }
1170         connection->commandList = 0;
1171         mpd_executeCommand(connection,"command_list_end\n");