Code

Fixed som spelling errors.
[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;
624         stats->playTime = 0;
625         stats->dbPlayTime = 0;
627         mpd_getNextReturnElement(connection);
628         if(connection->error) {
629                 free(stats);
630                 return NULL;
631         }
632         while(connection->returnElement) {
633                 mpd_ReturnElement * re = connection->returnElement;
634                 if(strcmp(re->name,"artists")==0) {
635                         stats->numberOfArtists = atoi(re->value);
636                 }
637                 else if(strcmp(re->name,"albums")==0) {
638                         stats->numberOfAlbums = atoi(re->value);
639                 }
640                 else if(strcmp(re->name,"songs")==0) {
641                         stats->numberOfSongs = atoi(re->value);
642                 }
643                 else if(strcmp(re->name,"uptime")==0) {
644                         stats->uptime = strtol(re->value,NULL,10);
645                 }
646                 else if(strcmp(re->name,"db_update")==0) {
647                         stats->dbUpdateTime = strtol(re->value,NULL,10);
648                 }
649                 else if(strcmp(re->name,"playtime")==0) {
650                         stats->playTime = strtol(re->value,NULL,10);
651                 }
652                 else if(strcmp(re->name,"db_playtime")==0) {
653                         stats->dbPlayTime = strtol(re->value,NULL,10);
654                 }
656                 mpd_getNextReturnElement(connection);
657                 if(connection->error) {
658                         free(stats);
659                         return NULL;
660                 }
661         }
663         if(connection->error) {
664                 free(stats);
665                 return NULL;
666         }
668         return stats;
671 void mpd_freeStats(mpd_Stats * stats) {
672         free(stats);
675 void mpd_initSong(mpd_Song * song) {
676         song->file = NULL;
677         song->artist = NULL;
678         song->album = NULL;
679         song->track = NULL;
680         song->title = NULL;
681         song->time = MPD_SONG_NO_TIME;
684 void mpd_finishSong(mpd_Song * song) {
685         if(song->file) free(song->file);
686         if(song->artist) free(song->artist);
687         if(song->album) free(song->album);
688         if(song->title) free(song->title);
689         if(song->track) free(song->track);
692 mpd_Song * mpd_newSong() {
693         mpd_Song * ret = malloc(sizeof(mpd_Song));
695         mpd_initSong(ret);
697         return ret;
700 void mpd_freeSong(mpd_Song * song) {
701         mpd_finishSong(song);
702         free(song);
705 mpd_Song * mpd_songDup(mpd_Song * song) {
706         mpd_Song * ret = mpd_newSong();
708         if(song->file) ret->file = strdup(song->file);
709         if(song->artist) ret->artist = strdup(song->artist);
710         if(song->album) ret->album = strdup(song->album);
711         if(song->title) ret->title = strdup(song->title);
712         if(song->track) ret->track = strdup(song->track);
713         ret->time = song->time;
715         return ret;
718 void mpd_initDirectory(mpd_Directory * directory) {
719         directory->path = NULL;
722 void mpd_finishDirectory(mpd_Directory * directory) {
723         if(directory->path) free(directory->path);
726 mpd_Directory * mpd_newDirectory () {
727         mpd_Directory * directory = malloc(sizeof(mpd_Directory));;
729         mpd_initDirectory(directory);
730         
731         return directory;
734 void mpd_freeDirectory(mpd_Directory * directory) {
735         mpd_finishDirectory(directory);
737         free(directory);
740 mpd_Directory * mpd_directoryDup(mpd_Directory * directory) {
741         mpd_Directory * ret = mpd_newDirectory();
743         if(directory->path) ret->path = strdup(directory->path);
745         return ret;
748 void mpd_initPlaylistFile(mpd_PlaylistFile * playlist) {
749         playlist->path = NULL;
752 void mpd_finishPlaylistFile(mpd_PlaylistFile * playlist) {
753         if(playlist->path) free(playlist->path);
756 mpd_PlaylistFile * mpd_newPlaylistFile() {
757         mpd_PlaylistFile * playlist = malloc(sizeof(mpd_PlaylistFile));
759         mpd_initPlaylistFile(playlist);
761         return playlist;
764 void mpd_freePlaylistFile(mpd_PlaylistFile * playlist) {
765         mpd_finishPlaylistFile(playlist);
766         free(playlist);
769 mpd_PlaylistFile * mpd_playlistFileDup(mpd_PlaylistFile * playlist) {
770         mpd_PlaylistFile * ret = mpd_newPlaylistFile();
772         if(playlist->path) ret->path = strdup(playlist->path);
774         return ret;
777 void mpd_initInfoEntity(mpd_InfoEntity * entity) {
778         entity->info.directory = NULL;
779
781 void mpd_finishInfoEntity(mpd_InfoEntity * entity) {
782         if(entity->info.directory) {
783                 if(entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
784                         mpd_freeDirectory(entity->info.directory);
785                 }
786                 else if(entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
787                         mpd_freeSong(entity->info.song);
788                 }
789                 else if(entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
790                         mpd_freePlaylistFile(entity->info.playlistFile);
791                 }
792         }
795 mpd_InfoEntity * mpd_newInfoEntity() {
796         mpd_InfoEntity * entity = malloc(sizeof(mpd_InfoEntity));
797         
798         mpd_initInfoEntity(entity);
800         return entity;
803 void mpd_freeInfoEntity(mpd_InfoEntity * entity) {
804         mpd_finishInfoEntity(entity);
805         free(entity);
808 void mpd_sendInfoCommand(mpd_Connection * connection, char * command) {
809         mpd_executeCommand(connection,command);
812 mpd_InfoEntity * mpd_getNextInfoEntity(mpd_Connection * connection) {
813         mpd_InfoEntity * entity = NULL;
815         if(connection->doneProcessing) return NULL;
817         if(!connection->returnElement) mpd_getNextReturnElement(connection);
819         if(connection->returnElement) { 
820                 if(strcmp(connection->returnElement->name,"file")==0) {
821                         entity = mpd_newInfoEntity();
822                         entity->type = MPD_INFO_ENTITY_TYPE_SONG;
823                         entity->info.song = mpd_newSong();
824                         entity->info.song->file = 
825                                 strdup(connection->returnElement->value);
826                 }
827                 else if(strcmp(connection->returnElement->name,
828                                         "directory")==0) {
829                         entity = mpd_newInfoEntity();
830                         entity->type = MPD_INFO_ENTITY_TYPE_DIRECTORY;
831                         entity->info.directory = mpd_newDirectory();
832                         entity->info.directory->path = 
833                                 strdup(connection->returnElement->value);
834                 }
835                 else if(strcmp(connection->returnElement->name,"playlist")==0) {
836                         entity = mpd_newInfoEntity();
837                         entity->type = MPD_INFO_ENTITY_TYPE_PLAYLISTFILE;
838                         entity->info.playlistFile = mpd_newPlaylistFile();
839                         entity->info.playlistFile->path = 
840                                 strdup(connection->returnElement->value);
841                 }
842                 else {
843                         connection->error = 1;
844                         strcpy(connection->errorStr,"problem parsing song info");
845                         return NULL;
846                 }
847         }
848         else return NULL;
850         mpd_getNextReturnElement(connection);
851         while(connection->returnElement) {
852                 mpd_ReturnElement * re = connection->returnElement;
854                 if(strcmp(re->name,"file")==0) return entity;
855                 else if(strcmp(re->name,"directory")==0) return entity;
856                 else if(strcmp(re->name,"playlist")==0) return entity;
858                 if(entity->type == MPD_INFO_ENTITY_TYPE_SONG && 
859                                 strlen(re->value)) {
860                         if(!entity->info.song->artist &&
861                                         strcmp(re->name,"Artist")==0) {
862                                 entity->info.song->artist = strdup(re->value);
863                         }
864                         else if(!entity->info.song->album &&
865                                         strcmp(re->name,"Album")==0) {
866                                 entity->info.song->album = strdup(re->value);
867                         }
868                         else if(!entity->info.song->title &&
869                                         strcmp(re->name,"Title")==0) {
870                                 entity->info.song->title = strdup(re->value);
871                         }
872                         else if(!entity->info.song->track &&
873                                         strcmp(re->name,"Track")==0) {
874                                 entity->info.song->track = strdup(re->value);
875                         }
876                         else if(entity->info.song->time==MPD_SONG_NO_TIME &&
877                                         strcmp(re->name,"Time")==0) {
878                                 entity->info.song->time = atoi(re->value);
879                         }
880                 }
881                 else if(entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
882                 }
883                 else if(entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) {
884                 }
886                 mpd_getNextReturnElement(connection);
887         }
889         return entity;
892 char * mpd_getNextReturnElementNamed(mpd_Connection * connection, 
893                 const char * name) 
895         if(connection->doneProcessing) return NULL;
897         mpd_getNextReturnElement(connection);
898         while(connection->returnElement) {
899                 mpd_ReturnElement * re = connection->returnElement;
901                 if(strcmp(re->name,name)==0) return strdup(re->value);
902                 mpd_getNextReturnElement(connection);
903         }
905         return NULL;
908 char * mpd_getNextArtist(mpd_Connection * connection) {
909         return mpd_getNextReturnElementNamed(connection,"Artist");
912 char * mpd_getNextAlbum(mpd_Connection * connection) {
913         return mpd_getNextReturnElementNamed(connection,"Album");
916 void mpd_sendPlaylistInfoCommand(mpd_Connection * connection, int songNum) {
917         char * string = malloc(strlen("playlistinfo")+25);
918         sprintf(string,"playlistinfo \"%i\"\n",songNum);
919         mpd_sendInfoCommand(connection,string);
920         free(string);
923 void mpd_sendListallCommand(mpd_Connection * connection, const char * dir) {
924         char * sDir = mpd_sanitizeArg(dir);
925         char * string = malloc(strlen("listall")+strlen(sDir)+5);
926         sprintf(string,"listall \"%s\"\n",sDir);
927         mpd_sendInfoCommand(connection,string);
928         free(string);
929         free(sDir);
932 void mpd_sendListallInfoCommand(mpd_Connection * connection, const char * dir) {
933         char * sDir = mpd_sanitizeArg(dir);
934         char * string = malloc(strlen("listallinfo")+strlen(sDir)+5);
935         sprintf(string,"listallinfo \"%s\"\n",sDir);
936         mpd_sendInfoCommand(connection,string);
937         free(string);
938         free(sDir);
941 void mpd_sendLsInfoCommand(mpd_Connection * connection, const char * dir) {
942         char * sDir = mpd_sanitizeArg(dir);
943         char * string = malloc(strlen("lsinfo")+strlen(sDir)+5);
944         sprintf(string,"lsinfo \"%s\"\n",sDir);
945         mpd_sendInfoCommand(connection,string);
946         free(string);
947         free(sDir);
950 void mpd_sendSearchCommand(mpd_Connection * connection, int table, 
951                 const char * str) 
953         char st[10];
954         char * string;
955         char * sanitStr = mpd_sanitizeArg(str);
956         if(table == MPD_TABLE_ARTIST) strcpy(st,"artist");
957         else if(table == MPD_TABLE_ALBUM) strcpy(st,"album");
958         else if(table == MPD_TABLE_TITLE) strcpy(st,"title");
959         else if(table == MPD_TABLE_FILENAME) strcpy(st,"filename");
960         else {
961                 connection->error = 1;
962                 strcpy(connection->errorStr,"unknown table for search");
963                 return;
964         }
965         string = malloc(strlen("search")+strlen(sanitStr)+strlen(st)+6);
966         sprintf(string,"search %s \"%s\"\n",st,sanitStr);
967         mpd_sendInfoCommand(connection,string);
968         free(string);
969         free(sanitStr);
972 void mpd_sendFindCommand(mpd_Connection * connection, int table, 
973                 const char * str) 
975         char st[10];
976         char * string;
977         char * sanitStr = mpd_sanitizeArg(str);
978         if(table == MPD_TABLE_ARTIST) strcpy(st,"artist");
979         else if(table == MPD_TABLE_ALBUM) strcpy(st,"album");
980         else if(table == MPD_TABLE_TITLE) strcpy(st,"title");
981         else {
982                 connection->error = 1;
983                 strcpy(connection->errorStr,"unknown table for find");
984                 return;
985         }
986         string = malloc(strlen("find")+strlen(sanitStr)+strlen(st)+6);
987         sprintf(string,"find %s \"%s\"\n",st,sanitStr);
988         mpd_sendInfoCommand(connection,string);
989         free(string);
990         free(sanitStr);
993 void mpd_sendListCommand(mpd_Connection * connection, int table, 
994                 const char * arg1) 
996         char st[10];
997         char * string;
998         if(table == MPD_TABLE_ARTIST) strcpy(st,"artist");
999         else if(table == MPD_TABLE_ALBUM) strcpy(st,"album");
1000         else {
1001                 connection->error = 1;
1002                 strcpy(connection->errorStr,"unknown table for list");
1003                 return;
1004         }
1005         if(arg1) {
1006                 char * sanitArg1 = mpd_sanitizeArg(arg1);
1007                 string = malloc(strlen("list")+strlen(sanitArg1)+strlen(st)+6);
1008                 sprintf(string,"list %s \"%s\"\n",st,sanitArg1);
1009                 free(sanitArg1);
1010         }
1011         else {
1012                 string = malloc(strlen("list")+strlen(st)+3);
1013                 sprintf(string,"list %s\n",st);
1014         }
1015         mpd_sendInfoCommand(connection,string);
1016         free(string);
1019 void mpd_sendAddCommand(mpd_Connection * connection, const char * file) {
1020         char * sFile = mpd_sanitizeArg(file);
1021         char * string = malloc(strlen("add")+strlen(sFile)+5);
1022         sprintf(string,"add \"%s\"\n",sFile);
1023         mpd_executeCommand(connection,string);
1024         free(string);
1025         free(sFile);
1028 void mpd_sendDeleteCommand(mpd_Connection * connection, int songNum) {
1029         char * string = malloc(strlen("delete")+25);
1030         sprintf(string,"delete \"%i\"\n",songNum);
1031         mpd_sendInfoCommand(connection,string);
1032         free(string);
1035 void mpd_sendSaveCommand(mpd_Connection * connection, const char * name) {
1036         char * sName = mpd_sanitizeArg(name);
1037         char * string = malloc(strlen("save")+strlen(sName)+5);
1038         sprintf(string,"save \"%s\"\n",sName);
1039         mpd_executeCommand(connection,string);
1040         free(string);
1041         free(sName);
1044 void mpd_sendLoadCommand(mpd_Connection * connection, const char * name) {
1045         char * sName = mpd_sanitizeArg(name);
1046         char * string = malloc(strlen("load")+strlen(sName)+5);
1047         sprintf(string,"load \"%s\"\n",sName);
1048         mpd_executeCommand(connection,string);
1049         free(string);
1050         free(sName);
1053 void mpd_sendRmCommand(mpd_Connection * connection, const char * name) {
1054         char * sName = mpd_sanitizeArg(name);
1055         char * string = malloc(strlen("rm")+strlen(sName)+5);
1056         sprintf(string,"rm \"%s\"\n",sName);
1057         mpd_executeCommand(connection,string);
1058         free(string);
1059         free(sName);
1062 void mpd_sendShuffleCommand(mpd_Connection * connection) {
1063         mpd_executeCommand(connection,"shuffle\n");
1066 void mpd_sendClearCommand(mpd_Connection * connection) {
1067         mpd_executeCommand(connection,"clear\n");
1070 void mpd_sendPlayCommand(mpd_Connection * connection, int songNum) {
1071         char * string = malloc(strlen("play")+25);
1072         sprintf(string,"play \"%i\"\n",songNum);
1073         mpd_sendInfoCommand(connection,string);
1074         free(string);
1077 void mpd_sendStopCommand(mpd_Connection * connection) {
1078         mpd_executeCommand(connection,"stop\n");
1081 void mpd_sendPauseCommand(mpd_Connection * connection) {
1082         mpd_executeCommand(connection,"pause\n");
1085 void mpd_sendNextCommand(mpd_Connection * connection) {
1086         mpd_executeCommand(connection,"next\n");
1089 void mpd_sendMoveCommand(mpd_Connection * connection, int from, int to) {
1090         char * string = malloc(strlen("move")+25);
1091         sprintf(string,"move \"%i\" \"%i\"\n",from,to);
1092         mpd_sendInfoCommand(connection,string);
1093         free(string);
1096 void mpd_sendSwapCommand(mpd_Connection * connection, int song1, int song2) {
1097         char * string = malloc(strlen("swap")+25);
1098         sprintf(string,"swap \"%i\" \"%i\"\n",song1,song2);
1099         mpd_sendInfoCommand(connection,string);
1100         free(string);
1103 void mpd_sendSeekCommand(mpd_Connection * connection, int song, int time) {
1104         char * string = malloc(strlen("seek")+25);
1105         sprintf(string,"seek \"%i\" \"%i\"\n",song,time);
1106         mpd_sendInfoCommand(connection,string);
1107         free(string);
1110 void mpd_sendUpdateCommand(mpd_Connection * connection) {
1111         mpd_executeCommand(connection,"update\n");
1114 void mpd_sendPrevCommand(mpd_Connection * connection) {
1115         mpd_executeCommand(connection,"previous\n");
1118 void mpd_sendRepeatCommand(mpd_Connection * connection, int repeatMode) {
1119         char * string = malloc(strlen("repeat")+25);
1120         sprintf(string,"repeat \"%i\"\n",repeatMode);
1121         mpd_executeCommand(connection,string);
1122         free(string);
1125 void mpd_sendRandomCommand(mpd_Connection * connection, int randomMode) {
1126         char * string = malloc(strlen("random")+25);
1127         sprintf(string,"random \"%i\"\n",randomMode);
1128         mpd_executeCommand(connection,string);
1129         free(string);
1132 void mpd_sendSetvolCommand(mpd_Connection * connection, int volumeChange) {
1133         char * string = malloc(strlen("setvol")+25);
1134         sprintf(string,"setvol \"%i\"\n",volumeChange);
1135         mpd_executeCommand(connection,string);
1136         free(string);
1139 void mpd_sendVolumeCommand(mpd_Connection * connection, int volumeChange) {
1140         char * string = malloc(strlen("volume")+25);
1141         sprintf(string,"volume \"%i\"\n",volumeChange);
1142         mpd_executeCommand(connection,string);
1143         free(string);
1146 void mpd_sendCrossfadeCommand(mpd_Connection * connection, int seconds) {
1147         char * string = malloc(strlen("crossfade")+25);
1148         sprintf(string,"crossfade \"%i\"\n",seconds);
1149         mpd_executeCommand(connection,string);
1150         free(string);
1153 void mpd_sendPasswordCommand(mpd_Connection * connection, const char * pass) {
1154         char * sPass = mpd_sanitizeArg(pass);
1155         char * string = malloc(strlen("password")+strlen(sPass)+5);
1156         sprintf(string,"password \"%s\"\n",sPass);
1157         mpd_executeCommand(connection,string);
1158         free(string);
1159         free(sPass);
1162 void mpd_sendCommandListBegin(mpd_Connection * connection) {
1163         if(connection->commandList) {
1164                 strcpy(connection->errorStr,"already in command list mode");
1165                 connection->error = 1;
1166                 return;
1167         }
1168         connection->commandList = 1;
1169         mpd_executeCommand(connection,"command_list_begin\n");
1172 void mpd_sendCommandListEnd(mpd_Connection * connection) {
1173         if(!connection->commandList) {
1174                 strcpy(connection->errorStr,"not in command list mode");
1175                 connection->error = 1;
1176                 return;
1177         }
1178         connection->commandList = 0;
1179         mpd_executeCommand(connection,"command_list_end\n");