Remove unused headers
[state-utils.git] / src / conn.c
blob69e6bcf3b2f74c7d5bf952330d16bf96845d0c6e
1 /*
2 * state-utils
3 * (c) 2005 by Avuton Olrich <avuton@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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 General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
24 #include "libmpdclient.h"
25 #include "conn.h"
27 #define DEFAULT_HOST "localhost"
28 #define DEFAULT_PORT "6600"
29 #define MPD_TIMEOUT 60
31 /* stolen from mpc */
32 static void parse_password(const char *host, int *password_len, int *parsed_len)
34 /* parse password and host */
35 char *ret = strstr(host, "@");
36 int len = ret - host;
38 if (ret && len == 0) {
39 parsed_len++;
40 } else if (ret) {
41 *password_len = len;
42 *parsed_len += len + 1;
46 /* stolen from mpc */
47 void printErrorAndExit(mpd_Connection * conn)
49 if (conn->error) {
50 fprintf(stderr, "error: %s\n", conn->errorStr);
51 exit(EXIT_FAILURE);
55 /* stolen from mpc */
56 static void send_password(char *host, int password_len, mpd_Connection * conn)
58 host[password_len] = '\0';
60 mpd_sendPasswordCommand(conn, host);
61 printErrorAndExit(conn);
62 mpd_finishCommand(conn);
63 printErrorAndExit(conn);
66 mpd_Connection *setup_connection()
68 char *host = NULL;
69 char *port = NULL;
70 int iport;
71 char *test;
72 int password_len = 0;
73 int parsed_len = 0;
74 mpd_Connection *conn;
76 if ((test = getenv("MPD_HOST")) && test) {
77 host = test;
78 } else {
79 host = DEFAULT_HOST;
82 if ((test = getenv("MPD_PORT")) && test) {
83 port = test;
84 } else {
85 port = DEFAULT_PORT;
88 iport = strtol(port, &test, 10);
90 if (iport <= 0 || *test != '\0') {
91 fprintf(stderr, "MPD_PORT \"%s\" is not a positive integer\n",
92 port);
93 exit(EXIT_FAILURE);
96 parse_password(host, &password_len, &parsed_len);
98 conn = mpd_newConnection(host + parsed_len, iport, MPD_TIMEOUT);
100 printErrorAndExit(conn);
102 if (password_len) {
103 send_password(host, password_len, conn);
106 return conn;