Code

Added list-format and status-format conf options
[ncmpc.git] / src / options.c
1 /* 
2  * $Id$
3  *
4  * (c) 2004 by Kalle Wallin <kaw@linux.se>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * 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  *
19  */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <ncurses.h>
26 #include <glib.h>
27 #include <popt.h>
29 #include "config.h"
30 #include "ncmpc.h"
31 #include "options.h"
32 #include "command.h"
33 #include "support.h"
35 options_t options;
37 static char *mpd_host     = NULL;
38 static char *mpd_password = NULL;
39 static char *config_file  = NULL;
40 static char *key_file     = NULL;
42 static struct poptOption optionsTable[] = {
43 #ifdef DEBUG
44   { "debug",        'D', 0, 0, 'D', "Enable debug output." },
45 #endif
46   { "version",      'V', 0, 0, 'V', "Display version information." },
47   { "colors",       'c', 0, 0, 'c', "Enable colors." },
48   { "no-colors",    'C', 0, 0, 'C', "Disable colors." },
49   { "exit",         'e', 0, 0, 'e', "Exit on connection errors." },
50   { "port",         'p', POPT_ARG_INT, &options.port, 0, 
51     "Connect to server on port [" DEFAULT_PORT_STR "].", "PORT" },
52   { "host",         'h', POPT_ARG_STRING, &mpd_host, 0, 
53     "Connect to server [" DEFAULT_HOST "].", "HOSTNAME" },
54   { "password",     'P', POPT_ARG_STRING, &mpd_password, 0, 
55     "Connect with password.", "PASSWORD" },
56   { "config",       'f', POPT_ARG_STRING, &config_file, 0, 
57     "Read config from FILE." , "FILE" },
58   { "key-file",     'k', POPT_ARG_STRING, &key_file, 0, 
59     "Read key bindings from FILE." , "FILE" },
61   POPT_AUTOHELP
62   { NULL, 0, 0, NULL, 0 }
63 };
65 static void
66 usage(poptContext optCon, int exitcode, char *error, char *addl) 
67 {
68   poptPrintUsage(optCon, stderr, 0);
69   if (error) 
70     fprintf(stderr, "%s: %s0", error, addl);
71   exit(exitcode);
72 }
74 options_t *
75 options_parse( int argc, const char **argv)
76 {
77   int c;
78   poptContext optCon;   /* context for parsing command-line options */
80   mpd_host = NULL;
81   mpd_password = NULL;
82   config_file = NULL;
83   key_file = NULL;
84   optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
85   while ((c = poptGetNextOpt(optCon)) >= 0) 
86     {
87       switch (c) 
88         {
89 #ifdef DEBUG
90         case 'D':
91           options.debug = 1;
92           break;
93 #endif
94         case 'c':
95           options.enable_colors = 1;
96           break;
97         case 'C':
98           options.enable_colors = 0;
99           break;
100         case 'V':
101           printf("Version " VERSION "\n");
102           exit(EXIT_SUCCESS);
103         case 'e':
104           options.reconnect = 0;
105           break;
106         default:
107           fprintf(stderr, "%s: %s\n",
108                   poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
109                   poptStrerror(c));
110           poptFreeContext(optCon);
111           exit(EXIT_FAILURE);
112           break;
113         }
114     }
115   if (c < -1) 
116     {
117       /* an error occurred during option processing */
118       fprintf(stderr, "%s: %s\n",
119               poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
120               poptStrerror(c));
121       poptFreeContext(optCon);
122       exit(EXIT_FAILURE);
123     }
125   if( mpd_host )
126     {
127       g_free(options.host);
128       options.host = mpd_host;
129     }
130   if( mpd_password )
131     {
132       g_free(options.password);
133       options.password = mpd_password;
134     }
135   if( config_file )
136     {
137       g_free(options.config_file);
138       options.config_file = config_file;
139     }
140   if( key_file )
141     {
142       g_free(options.key_file);
143       options.key_file = key_file;
144     }
146   poptFreeContext(optCon);
147   return &options;
150 options_t *
151 options_init( void )
153   const char *value;
154   char *tmp;
156   memset(&options, 0, sizeof(options_t));
158   if( (value=g_getenv(MPD_HOST_ENV)) )
159     options.host = g_strdup(value);
160   else
161     options.host = g_strdup(DEFAULT_HOST);
162   if( (tmp=g_strstr_len(options.host, strlen(options.host), "@")) )
163     {
164       char *oldhost = options.host;
165       *tmp  = '\0';
166       options.password = locale_to_utf8(oldhost);
167       options.host = g_strdup(tmp+1);
168       g_free(oldhost);
169     }
171   if( (value=g_getenv(MPD_PORT_ENV)) )
172     options.port = atoi(value);
173   else
174     options.port = DEFAULT_PORT;
176   options.list_format = NULL;
177   options.status_format = NULL;
179   options.reconnect = 1;
180   options.find_wrap = 1;
181   options.wide_cursor = 1;
182   options.enable_beep = 1;
185   return &options;
189 options_t *
190 options_get(void)
192   return &options;