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"
13 options_t options;
15 static struct poptOption optionsTable[] = {
16 #ifdef DEBUG
17 { "debug", 'D', 0, 0, 'D', "Enable debug output." },
18 #endif
19 { "version", 'V', 0, 0, 'V', "Display version information." },
20 { "keys", 'k', 0, 0, 'k', "Display key bindings." },
21 { "colors", 'c', 0, 0, 'c', "Enable colors." },
22 { "no-colors", 'C', 0, 0, 'C', "Disable colors." },
23 { "exit", 'e', 0, 0, 'e', "Exit on connection errors." },
24 { "port", 'p', POPT_ARG_INT, &options.port, 0,
25 "Connect to server on port [" DEFAULT_PORT_STR "].", "PORT" },
26 { "host", 'h', POPT_ARG_STRING, &options.host, 0,
27 "Connect to server [" DEFAULT_HOST "].", "HOSTNAME" },
28 POPT_AUTOHELP
29 { NULL, 0, 0, NULL, 0 }
30 };
32 static void
33 usage(poptContext optCon, int exitcode, char *error, char *addl)
34 {
35 poptPrintUsage(optCon, stderr, 0);
36 if (error)
37 fprintf(stderr, "%s: %s0", error, addl);
38 exit(exitcode);
39 }
41 options_t *
42 options_parse( int argc, const char **argv)
43 {
44 int c;
45 poptContext optCon; /* context for parsing command-line options */
47 optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
48 while ((c = poptGetNextOpt(optCon)) >= 0)
49 {
50 switch (c)
51 {
52 #ifdef DEBUG
53 case 'D':
54 options.debug = 1;
55 break;
56 #endif
57 case 'c':
58 options.enable_colors = 1;
59 break;
60 case 'C':
61 options.enable_colors = 0;
62 break;
63 case 'V':
64 printf("Version " VERSION "\n");
65 exit(EXIT_SUCCESS);
66 case 'k':
67 command_dump_keys();
68 exit(EXIT_SUCCESS);
69 case 'e':
70 options.reconnect = 0;
71 break;
72 default:
73 fprintf(stderr, "%s: %s\n",
74 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
75 poptStrerror(c));
76 poptFreeContext(optCon);
77 exit(EXIT_FAILURE);
78 break;
79 }
80 }
81 if (c < -1)
82 {
83 /* an error occurred during option processing */
84 fprintf(stderr, "%s: %s\n",
85 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
86 poptStrerror(c));
87 poptFreeContext(optCon);
88 exit(EXIT_FAILURE);
89 }
91 poptFreeContext(optCon);
92 return &options;
93 }
95 options_t *
96 options_init( void )
97 {
98 char *value;
100 memset(&options, 0, sizeof(options_t));
101 if( (value=getenv(MPD_HOST_ENV)) )
102 options.host = g_strdup(value);
103 else
104 options.host = g_strdup(DEFAULT_HOST);
105 if( (value=getenv(MPD_PORT_ENV)) )
106 options.port = atoi(value);
107 else
108 options.port = DEFAULT_PORT;
110 options.reconnect = 1;
111 options.find_wrap = 1;
113 options.bg_color = COLOR_BLACK;
114 options.title_color = COLOR_BLUE;
115 options.line_color = COLOR_GREEN;
116 options.list_color = COLOR_YELLOW;
117 options.progress_color = COLOR_GREEN;
118 options.status_color = COLOR_RED;
119 options.alert_color = COLOR_MAGENTA;;
121 return &options;
122 }
125 options_t *
126 options_get(void)
127 {
128 return &options;
129 }