]> code.delx.au - pulseaudio/blob - polyp/cmdline.c
c32d9a0f1c605cfd0f5f05631dddd6d1b07f26e9
[pulseaudio] / polyp / cmdline.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <getopt.h>
31 #include <sys/stat.h>
32
33 #include "cmdline.h"
34 #include "util.h"
35 #include "strbuf.h"
36 #include "xmalloc.h"
37
38 enum {
39 ARG_HELP = 256,
40 ARG_VERSION,
41 ARG_DUMP_CONF,
42 ARG_DUMP_MODULES,
43 ARG_DAEMONIZE,
44 ARG_FAIL,
45 ARG_VERBOSE,
46 ARG_HIGH_PRIORITY,
47 ARG_DISALLOW_MODULE_LOADING,
48 ARG_EXIT_IDLE_TIME,
49 ARG_MODULE_IDLE_TIME,
50 ARG_SCACHE_IDLE_TIME,
51 ARG_LOG_TARGET,
52 ARG_LOAD,
53 ARG_FILE,
54 ARG_DL_SEARCH_PATH,
55 ARG_RESAMPLE_METHOD
56 };
57
58 static struct option long_options[] = {
59 {"help", 0, 0, ARG_HELP},
60 {"version", 0, 0, ARG_VERSION},
61 {"dump-conf", 0, 0, ARG_DUMP_CONF},
62 {"dump-modules", 0, 0, ARG_DUMP_MODULES},
63 {"daemonize", 2, 0, ARG_DAEMONIZE},
64 {"fail", 2, 0, ARG_FAIL},
65 {"verbose", 2, 0, ARG_VERBOSE},
66 {"high-priority", 2, 0, ARG_HIGH_PRIORITY},
67 {"disallow-module-loading", 2, 0, ARG_DISALLOW_MODULE_LOADING},
68 {"exit-idle-time", 2, 0, ARG_EXIT_IDLE_TIME},
69 {"module-idle-time", 2, 0, ARG_MODULE_IDLE_TIME},
70 {"scache-idle-time", 2, 0, ARG_SCACHE_IDLE_TIME},
71 {"log-target", 1, 0, ARG_LOG_TARGET},
72 {"load", 1, 0, ARG_LOAD},
73 {"file", 1, 0, ARG_FILE},
74 {"dl-search-path", 1, 0, ARG_DL_SEARCH_PATH},
75 {"resample-method", 1, 0, ARG_RESAMPLE_METHOD},
76 {NULL, 0, 0, 0}
77 };
78
79 void pa_cmdline_help(const char *argv0) {
80 const char *e;
81
82 if ((e = strrchr(argv0, '/')))
83 e++;
84 else
85 e = argv0;
86
87 printf("%s [options]\n\n"
88 " -h, --help Show this help\n"
89 " --version Show version\n"
90 " --dump-conf Dump default configuration\n"
91 " --dump-modules Dump list of available modules\n\n"
92
93 " -D, --daemonize[=BOOL] Daemonize after startup\n"
94 " --fail[=BOOL] Quit when startup fails\n"
95 " --verbose[=BOOL] Be slightly more verbose\n"
96 " --high-priority[=BOOL] Try to set high process priority (only available as root)\n"
97 " --disallow-module-loading[=BOOL] Disallow module loading after startup\n"
98 " --exit-idle-time=SECS Terminate the daemon when idle and this time passed\n"
99 " --module-idle-time=SECS Unload autoloaded modules when idle and this time passed\n"
100 " --scache-idle-time=SECS Unload autoloaded samples when idle and this time passed\n"
101 " --log-target={auto,syslog,stderr} Specify the log target\n"
102 " -p, --dl-search-path=PATH Set the search path for dynamic shared objects (plugins)\n"
103 " --resample-method=[METHOD] Use the specified resampling method\n\n"
104
105 " -L, --load=\"MODULE ARGUMENTS\" Load the specified plugin module with the specified argument\n"
106 " -F, --file=FILENAME Run the specified script\n"
107 " -C Open a command line on the running TTY after startup\n\n"
108
109 " -n Don't load default script file\n", e);
110 }
111
112 int pa_cmdline_parse(struct pa_daemon_conf *conf, int argc, char *const argv [], int *d) {
113 struct pa_strbuf *buf = NULL;
114 int c;
115 assert(conf && argc && argv);
116
117 buf = pa_strbuf_new();
118
119 if (conf->script_commands)
120 pa_strbuf_puts(buf, conf->script_commands);
121
122 while ((c = getopt_long(argc, argv, "L:F:ChDnp:", long_options, NULL)) != -1) {
123 switch (c) {
124 case ARG_HELP:
125 case 'h':
126 conf->cmd = PA_CMD_HELP;
127 break;
128
129 case ARG_VERSION:
130 conf->cmd = PA_CMD_VERSION;
131 break;
132
133 case ARG_DUMP_CONF:
134 conf->cmd = PA_CMD_DUMP_CONF;
135 break;
136
137 case ARG_DUMP_MODULES:
138 conf->cmd = PA_CMD_DUMP_MODULES;
139 break;
140
141 case ARG_LOAD:
142 case 'L':
143 pa_strbuf_printf(buf, "load-module %s\n", optarg);
144 break;
145
146 case ARG_FILE:
147 case 'F':
148 pa_strbuf_printf(buf, ".include %s\n", optarg);
149 break;
150
151 case 'C':
152 pa_strbuf_puts(buf, "load-module module-cli\n");
153 break;
154
155 case ARG_DAEMONIZE:
156 case 'D':
157 if ((conf->daemonize = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
158 pa_log(__FILE__": --daemonize expects boolean argument\n");
159 goto fail;
160 }
161 break;
162
163 case ARG_FAIL:
164 if ((conf->fail = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
165 pa_log(__FILE__": --fail expects boolean argument\n");
166 goto fail;
167 }
168 break;
169
170 case ARG_VERBOSE:
171 if ((conf->verbose = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
172 pa_log(__FILE__": --verbose expects boolean argument\n");
173 goto fail;
174 }
175 break;
176
177 case ARG_HIGH_PRIORITY:
178 if ((conf->high_priority = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
179 pa_log(__FILE__": --high-priority expects boolean argument\n");
180 goto fail;
181 }
182 break;
183
184 case ARG_DISALLOW_MODULE_LOADING:
185 if ((conf->disallow_module_loading = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
186 pa_log(__FILE__": --disallow-module-loading expects boolean argument\n");
187 goto fail;
188 }
189 break;
190
191 case 'p':
192 case ARG_DL_SEARCH_PATH:
193 pa_xfree(conf->dl_search_path);
194 conf->dl_search_path = *optarg ? pa_xstrdup(optarg) : NULL;
195 break;
196
197 case 'n':
198 pa_xfree(conf->default_script_file);
199 conf->default_script_file = NULL;
200 break;
201
202 case ARG_LOG_TARGET:
203 if (pa_daemon_conf_set_log_target(conf, optarg) < 0) {
204 pa_log(__FILE__": Invalid log target: use either 'syslog', 'stderr' or 'auto'.\n");
205 goto fail;
206 }
207 break;
208
209 case ARG_EXIT_IDLE_TIME:
210 conf->exit_idle_time = atoi(optarg);
211 break;
212
213 case ARG_MODULE_IDLE_TIME:
214 conf->module_idle_time = atoi(optarg);
215 break;
216
217 case ARG_SCACHE_IDLE_TIME:
218 conf->scache_idle_time = atoi(optarg);
219 break;
220
221 case ARG_RESAMPLE_METHOD:
222 if (pa_daemon_conf_set_resample_method(conf, optarg) < 0) {
223 pa_log(__FILE__": Invalid resample method '%s'.\n", optarg);
224 goto fail;
225 }
226 break;
227
228 default:
229 goto fail;
230 }
231 }
232
233 pa_xfree(conf->script_commands);
234 conf->script_commands = pa_strbuf_tostring_free(buf);
235
236 if (!conf->script_commands) {
237 pa_xfree(conf->script_commands);
238 conf->script_commands = NULL;
239 }
240
241 *d = optind;
242
243 return 0;
244
245 fail:
246 if (buf)
247 pa_strbuf_free(buf);
248
249 return -1;
250 }