]> code.delx.au - pulseaudio/blob - src/daemon/cmdline.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / daemon / cmdline.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <getopt.h>
32 #include <sys/stat.h>
33
34 #include <pulse/xmalloc.h>
35
36 #include <pulsecore/core-util.h>
37 #include <pulsecore/strbuf.h>
38 #include <pulsecore/macro.h>
39
40 #include "cmdline.h"
41
42 /* Argument codes for getopt_long() */
43 enum {
44 ARG_HELP = 256,
45 ARG_VERSION,
46 ARG_DUMP_CONF,
47 ARG_DUMP_MODULES,
48 ARG_DAEMONIZE,
49 ARG_FAIL,
50 ARG_LOG_LEVEL,
51 ARG_HIGH_PRIORITY,
52 ARG_DISALLOW_MODULE_LOADING,
53 ARG_EXIT_IDLE_TIME,
54 ARG_MODULE_IDLE_TIME,
55 ARG_SCACHE_IDLE_TIME,
56 ARG_LOG_TARGET,
57 ARG_LOAD,
58 ARG_FILE,
59 ARG_DL_SEARCH_PATH,
60 ARG_RESAMPLE_METHOD,
61 ARG_KILL,
62 ARG_USE_PID_FILE,
63 ARG_CHECK,
64 ARG_NO_CPU_LIMIT,
65 ARG_DISABLE_SHM,
66 ARG_DUMP_RESAMPLE_METHODS,
67 ARG_SYSTEM,
68 ARG_CLEANUP_SHM
69 };
70
71 /* Tabel for getopt_long() */
72 static struct option long_options[] = {
73 {"help", 0, 0, ARG_HELP},
74 {"version", 0, 0, ARG_VERSION},
75 {"dump-conf", 0, 0, ARG_DUMP_CONF},
76 {"dump-modules", 0, 0, ARG_DUMP_MODULES},
77 {"daemonize", 2, 0, ARG_DAEMONIZE},
78 {"fail", 2, 0, ARG_FAIL},
79 {"verbose", 2, 0, ARG_LOG_LEVEL},
80 {"log-level", 2, 0, ARG_LOG_LEVEL},
81 {"high-priority", 2, 0, ARG_HIGH_PRIORITY},
82 {"disallow-module-loading", 2, 0, ARG_DISALLOW_MODULE_LOADING},
83 {"exit-idle-time", 2, 0, ARG_EXIT_IDLE_TIME},
84 {"module-idle-time", 2, 0, ARG_MODULE_IDLE_TIME},
85 {"scache-idle-time", 2, 0, ARG_SCACHE_IDLE_TIME},
86 {"log-target", 1, 0, ARG_LOG_TARGET},
87 {"load", 1, 0, ARG_LOAD},
88 {"file", 1, 0, ARG_FILE},
89 {"dl-search-path", 1, 0, ARG_DL_SEARCH_PATH},
90 {"resample-method", 1, 0, ARG_RESAMPLE_METHOD},
91 {"kill", 0, 0, ARG_KILL},
92 {"use-pid-file", 2, 0, ARG_USE_PID_FILE},
93 {"check", 0, 0, ARG_CHECK},
94 {"system", 2, 0, ARG_SYSTEM},
95 {"no-cpu-limit", 2, 0, ARG_NO_CPU_LIMIT},
96 {"disable-shm", 2, 0, ARG_DISABLE_SHM},
97 {"dump-resample-methods", 2, 0, ARG_DUMP_RESAMPLE_METHODS},
98 {"cleanup-shm", 2, 0, ARG_CLEANUP_SHM},
99 {NULL, 0, 0, 0}
100 };
101
102 void pa_cmdline_help(const char *argv0) {
103 const char *e;
104
105 pa_assert(argv0);
106
107 if ((e = strrchr(argv0, '/')))
108 e++;
109 else
110 e = argv0;
111
112 printf("%s [options]\n\n"
113 "COMMANDS:\n"
114 " -h, --help Show this help\n"
115 " --version Show version\n"
116 " --dump-conf Dump default configuration\n"
117 " --dump-modules Dump list of available modules\n"
118 " --dump-resample-methods Dump available resample methods\n"
119 " --cleanup-shm Cleanup stale shared memory segments\n"
120 " -k --kill Kill a running daemon\n"
121 " --check Check for a running daemon\n\n"
122
123 "OPTIONS:\n"
124 " --system[=BOOL] Run as system-wide instance\n"
125 " -D, --daemonize[=BOOL] Daemonize after startup\n"
126 " --fail[=BOOL] Quit when startup fails\n"
127 " --high-priority[=BOOL] Try to set high process priority\n"
128 " (only available as root)\n"
129 " --disallow-module-loading[=BOOL] Disallow module loading after startup\n"
130 " --exit-idle-time=SECS Terminate the daemon when idle and this\n"
131 " time passed\n"
132 " --module-idle-time=SECS Unload autoloaded modules when idle and\n"
133 " this time passed\n"
134 " --scache-idle-time=SECS Unload autoloaded samples when idle and\n"
135 " this time passed\n"
136 " --log-level[=LEVEL] Increase or set verbosity level\n"
137 " -v Increase the verbosity level\n"
138 " --log-target={auto,syslog,stderr} Specify the log target\n"
139 " -p, --dl-search-path=PATH Set the search path for dynamic shared\n"
140 " objects (plugins)\n"
141 " --resample-method=[METHOD] Use the specified resampling method\n"
142 " (See --dump-resample-methods for\n"
143 " possible values)\n"
144 " --use-pid-file[=BOOL] Create a PID file\n"
145 " --no-cpu-limit[=BOOL] Do not install CPU load limiter on\n"
146 " platforms that support it.\n"
147 " --disable-shm[=BOOL] Disable shared memory support.\n\n"
148
149 "STARTUP SCRIPT:\n"
150 " -L, --load=\"MODULE ARGUMENTS\" Load the specified plugin module with\n"
151 " the specified argument\n"
152 " -F, --file=FILENAME Run the specified script\n"
153 " -C Open a command line on the running TTY\n"
154 " after startup\n\n"
155
156 " -n Don't load default script file\n", e);
157 }
158
159 int pa_cmdline_parse(pa_daemon_conf *conf, int argc, char *const argv [], int *d) {
160 pa_strbuf *buf = NULL;
161 int c;
162
163 pa_assert(conf);
164 pa_assert(argc > 0);
165 pa_assert(argv);
166
167 buf = pa_strbuf_new();
168
169 if (conf->script_commands)
170 pa_strbuf_puts(buf, conf->script_commands);
171
172 while ((c = getopt_long(argc, argv, "L:F:ChDnp:kv", long_options, NULL)) != -1) {
173 switch (c) {
174 case ARG_HELP:
175 case 'h':
176 conf->cmd = PA_CMD_HELP;
177 break;
178
179 case ARG_VERSION:
180 conf->cmd = PA_CMD_VERSION;
181 break;
182
183 case ARG_DUMP_CONF:
184 conf->cmd = PA_CMD_DUMP_CONF;
185 break;
186
187 case ARG_DUMP_MODULES:
188 conf->cmd = PA_CMD_DUMP_MODULES;
189 break;
190
191 case ARG_DUMP_RESAMPLE_METHODS:
192 conf->cmd = PA_CMD_DUMP_RESAMPLE_METHODS;
193 break;
194
195 case ARG_CLEANUP_SHM:
196 conf->cmd = PA_CMD_CLEANUP_SHM;
197 break;
198
199 case 'k':
200 case ARG_KILL:
201 conf->cmd = PA_CMD_KILL;
202 break;
203
204 case ARG_CHECK:
205 conf->cmd = PA_CMD_CHECK;
206 break;
207
208 case ARG_LOAD:
209 case 'L':
210 pa_strbuf_printf(buf, "load-module %s\n", optarg);
211 break;
212
213 case ARG_FILE:
214 case 'F': {
215 char *p;
216 pa_strbuf_printf(buf, ".include %s\n", p = pa_make_path_absolute(optarg));
217 pa_xfree(p);
218 break;
219 }
220
221 case 'C':
222 pa_strbuf_puts(buf, "load-module module-cli exit_on_eof=1\n");
223 break;
224
225 case ARG_DAEMONIZE:
226 case 'D':
227 if ((conf->daemonize = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
228 pa_log("--daemonize expects boolean argument");
229 goto fail;
230 }
231 break;
232
233 case ARG_FAIL:
234 if ((conf->fail = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
235 pa_log("--fail expects boolean argument");
236 goto fail;
237 }
238 break;
239
240 case 'v':
241 case ARG_LOG_LEVEL:
242
243 if (optarg) {
244 if (pa_daemon_conf_set_log_level(conf, optarg) < 0) {
245 pa_log("--log-level expects log level argument (either numeric in range 0..4 or one of debug, info, notice, warn, error).");
246 goto fail;
247 }
248 } else {
249 if (conf->log_level < PA_LOG_LEVEL_MAX-1)
250 conf->log_level++;
251 }
252
253 break;
254
255 case ARG_HIGH_PRIORITY:
256 if ((conf->high_priority = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
257 pa_log("--high-priority expects boolean argument");
258 goto fail;
259 }
260 break;
261
262 case ARG_DISALLOW_MODULE_LOADING:
263 if ((conf->disallow_module_loading = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
264 pa_log("--disallow-module-loading expects boolean argument");
265 goto fail;
266 }
267 break;
268
269 case ARG_USE_PID_FILE:
270 if ((conf->use_pid_file = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
271 pa_log("--use-pid-file expects boolean argument");
272 goto fail;
273 }
274 break;
275
276 case 'p':
277 case ARG_DL_SEARCH_PATH:
278 pa_xfree(conf->dl_search_path);
279 conf->dl_search_path = *optarg ? pa_xstrdup(optarg) : NULL;
280 break;
281
282 case 'n':
283 pa_xfree(conf->default_script_file);
284 conf->default_script_file = NULL;
285 break;
286
287 case ARG_LOG_TARGET:
288 if (pa_daemon_conf_set_log_target(conf, optarg) < 0) {
289 pa_log("Invalid log target: use either 'syslog', 'stderr' or 'auto'.");
290 goto fail;
291 }
292 break;
293
294 case ARG_EXIT_IDLE_TIME:
295 conf->exit_idle_time = atoi(optarg);
296 break;
297
298 case ARG_MODULE_IDLE_TIME:
299 conf->module_idle_time = atoi(optarg);
300 break;
301
302 case ARG_SCACHE_IDLE_TIME:
303 conf->scache_idle_time = atoi(optarg);
304 break;
305
306 case ARG_RESAMPLE_METHOD:
307 if (pa_daemon_conf_set_resample_method(conf, optarg) < 0) {
308 pa_log("Invalid resample method '%s'.", optarg);
309 goto fail;
310 }
311 break;
312
313 case ARG_SYSTEM:
314 if ((conf->system_instance = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
315 pa_log("--system expects boolean argument");
316 goto fail;
317 }
318 break;
319
320 case ARG_NO_CPU_LIMIT:
321 if ((conf->no_cpu_limit = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
322 pa_log("--no-cpu-limit expects boolean argument");
323 goto fail;
324 }
325 break;
326
327 case ARG_DISABLE_SHM:
328 if ((conf->disable_shm = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
329 pa_log("--disable-shm expects boolean argument");
330 goto fail;
331 }
332 break;
333
334 default:
335 goto fail;
336 }
337 }
338
339 pa_xfree(conf->script_commands);
340 conf->script_commands = pa_strbuf_tostring_free(buf);
341
342 if (!conf->script_commands) {
343 pa_xfree(conf->script_commands);
344 conf->script_commands = NULL;
345 }
346
347 *d = optind;
348
349 return 0;
350
351 fail:
352 if (buf)
353 pa_strbuf_free(buf);
354
355 return -1;
356 }