Code

Removed extra source - langinfo.c
[ncmpc.git] / options.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <ncurses.h>
6 #include <glib.h>
7 #include <popt.h>
9 #include "config.h"
10 #include "options.h"
11 #include "command.h"
12 #include "support.h"
14 options_t options;
16 static char *mpd_host     = NULL;
17 static char *mpd_password = NULL;
19 static struct poptOption optionsTable[] = {
20 #ifdef DEBUG
21   { "debug",        'D', 0, 0, 'D', "Enable debug output." },
22 #endif
23   { "version",      'V', 0, 0, 'V', "Display version information." },
24   { "keys",         'k', 0, 0, 'k', "Display key bindings." },
25   { "colors",       'c', 0, 0, 'c', "Enable colors." },
26   { "no-colors",    'C', 0, 0, 'C', "Disable colors." },
27   { "exit",         'e', 0, 0, 'e', "Exit on connection errors." },
28   { "port",         'p', POPT_ARG_INT, &options.port, 0, 
29     "Connect to server on port [" DEFAULT_PORT_STR "].", "PORT" },
30   { "host",         'h', POPT_ARG_STRING, &mpd_host, 0, 
31     "Connect to server [" DEFAULT_HOST "].", "HOSTNAME" },
32   { "passwd",       'P', POPT_ARG_STRING, &mpd_password, 0, 
33     "Connect with password.", "PASSWORD" },
34   POPT_AUTOHELP
35   { NULL, 0, 0, NULL, 0 }
36 };
38 static void
39 usage(poptContext optCon, int exitcode, char *error, char *addl) 
40 {
41   poptPrintUsage(optCon, stderr, 0);
42   if (error) 
43     fprintf(stderr, "%s: %s0", error, addl);
44   exit(exitcode);
45 }
47 options_t *
48 options_parse( int argc, const char **argv)
49 {
50   int c;
51   poptContext optCon;   /* context for parsing command-line options */
53   mpd_host = NULL;
54   mpd_password = NULL;
55   optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
56   while ((c = poptGetNextOpt(optCon)) >= 0) 
57     {
58       switch (c) 
59         {
60 #ifdef DEBUG
61         case 'D':
62           options.debug = 1;
63           break;
64 #endif
65         case 'c':
66           options.enable_colors = 1;
67           break;
68         case 'C':
69           options.enable_colors = 0;
70           break;
71         case 'V':
72           printf("Version " VERSION "\n");
73           exit(EXIT_SUCCESS);
74         case 'k':
75           command_dump_keys();
76           exit(EXIT_SUCCESS);
77         case 'e':
78           options.reconnect = 0;
79           break;
80         default:
81           fprintf(stderr, "%s: %s\n",
82                   poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
83                   poptStrerror(c));
84           poptFreeContext(optCon);
85           exit(EXIT_FAILURE);
86           break;
87         }
88     }
89   if (c < -1) 
90     {
91       /* an error occurred during option processing */
92       fprintf(stderr, "%s: %s\n",
93               poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
94               poptStrerror(c));
95       poptFreeContext(optCon);
96       exit(EXIT_FAILURE);
97     }
99   if( mpd_host )
100     {
101       g_free(options.host);
102       options.host = mpd_host;
103     }
104   if( mpd_password )
105     {
106       g_free(options.password);
107       options.password = mpd_password;
108     }
109   poptFreeContext(optCon);
110   return &options;
113 options_t *
114 options_init( void )
116   const char *value;
117   char *tmp;
119   memset(&options, 0, sizeof(options_t));
121   if( (value=g_getenv(MPD_HOST_ENV)) )
122     options.host = g_strdup(value);
123   else
124     options.host = g_strdup(DEFAULT_HOST);
125   if( (tmp=g_strstr_len(options.host, strlen(options.host), "@")) )
126     {
127       char *oldhost = options.host;
128       *tmp  = '\0';
129       options.password = locale_to_utf8(oldhost);
130       options.host = g_strdup(tmp+1);
131       g_free(oldhost);
132     }
134   if( (value=g_getenv(MPD_PORT_ENV)) )
135     options.port = atoi(value);
136   else
137     options.port = DEFAULT_PORT;
139   options.reconnect = 1;
140   options.find_wrap = 1;
142   options.bg_color       = COLOR_BLACK;
143   options.title_color    = COLOR_YELLOW;
144   options.line_color     = COLOR_WHITE;
145   options.list_color     = COLOR_GREEN;
146   options.progress_color = COLOR_WHITE;
147   options.status_color   = COLOR_YELLOW;
148   options.alert_color    = COLOR_RED;
150   return &options;
154 options_t *
155 options_get(void)
157   return &options;