]> code.delx.au - pulseaudio/blob - src/daemon/cmdline.c
Updated catalan po
[pulseaudio] / src / daemon / cmdline.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser 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 PulseAudio 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 Lesser General Public License
17 along with PulseAudio; 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 <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <sys/stat.h>
31
32 #include <pulse/xmalloc.h>
33 #include <pulse/i18n.h>
34
35 #include <pulsecore/core-util.h>
36 #include <pulsecore/strbuf.h>
37 #include <pulsecore/macro.h>
38
39 #include "cmdline.h"
40
41 /* Argument codes for getopt_long() */
42 enum {
43 ARG_HELP = 256,
44 ARG_VERSION,
45 ARG_DUMP_CONF,
46 ARG_DUMP_MODULES,
47 ARG_DAEMONIZE,
48 ARG_FAIL,
49 ARG_LOG_LEVEL,
50 ARG_HIGH_PRIORITY,
51 ARG_REALTIME,
52 ARG_DISALLOW_MODULE_LOADING,
53 ARG_DISALLOW_EXIT,
54 ARG_EXIT_IDLE_TIME,
55 ARG_SCACHE_IDLE_TIME,
56 ARG_LOG_TARGET,
57 ARG_LOG_META,
58 ARG_LOG_TIME,
59 ARG_LOG_BACKTRACE,
60 ARG_LOAD,
61 ARG_FILE,
62 ARG_DL_SEARCH_PATH,
63 ARG_RESAMPLE_METHOD,
64 ARG_KILL,
65 ARG_USE_PID_FILE,
66 ARG_CHECK,
67 ARG_NO_CPU_LIMIT,
68 ARG_DISABLE_SHM,
69 ARG_DUMP_RESAMPLE_METHODS,
70 ARG_SYSTEM,
71 ARG_CLEANUP_SHM,
72 ARG_START
73 };
74
75 /* Tabel for getopt_long() */
76 static const struct option long_options[] = {
77 {"help", 0, 0, ARG_HELP},
78 {"version", 0, 0, ARG_VERSION},
79 {"dump-conf", 0, 0, ARG_DUMP_CONF},
80 {"dump-modules", 0, 0, ARG_DUMP_MODULES},
81 {"daemonize", 2, 0, ARG_DAEMONIZE},
82 {"fail", 2, 0, ARG_FAIL},
83 {"verbose", 2, 0, ARG_LOG_LEVEL},
84 {"log-level", 2, 0, ARG_LOG_LEVEL},
85 {"high-priority", 2, 0, ARG_HIGH_PRIORITY},
86 {"realtime", 2, 0, ARG_REALTIME},
87 {"disallow-module-loading", 2, 0, ARG_DISALLOW_MODULE_LOADING},
88 {"disallow-exit", 2, 0, ARG_DISALLOW_EXIT},
89 {"exit-idle-time", 2, 0, ARG_EXIT_IDLE_TIME},
90 {"scache-idle-time", 2, 0, ARG_SCACHE_IDLE_TIME},
91 {"log-target", 1, 0, ARG_LOG_TARGET},
92 {"log-meta", 2, 0, ARG_LOG_META},
93 {"log-time", 2, 0, ARG_LOG_TIME},
94 {"log-backtrace", 1, 0, ARG_LOG_BACKTRACE},
95 {"load", 1, 0, ARG_LOAD},
96 {"file", 1, 0, ARG_FILE},
97 {"dl-search-path", 1, 0, ARG_DL_SEARCH_PATH},
98 {"resample-method", 1, 0, ARG_RESAMPLE_METHOD},
99 {"kill", 0, 0, ARG_KILL},
100 {"start", 0, 0, ARG_START},
101 {"use-pid-file", 2, 0, ARG_USE_PID_FILE},
102 {"check", 0, 0, ARG_CHECK},
103 {"system", 2, 0, ARG_SYSTEM},
104 {"no-cpu-limit", 2, 0, ARG_NO_CPU_LIMIT},
105 {"disable-shm", 2, 0, ARG_DISABLE_SHM},
106 {"dump-resample-methods", 2, 0, ARG_DUMP_RESAMPLE_METHODS},
107 {"cleanup-shm", 2, 0, ARG_CLEANUP_SHM},
108 {NULL, 0, 0, 0}
109 };
110
111 void pa_cmdline_help(const char *argv0) {
112 const char *e;
113
114 pa_assert(argv0);
115
116 if ((e = strrchr(argv0, '/')))
117 e++;
118 else
119 e = argv0;
120
121 printf(_("%s [options]\n\n"
122 "COMMANDS:\n"
123 " -h, --help Show this help\n"
124 " --version Show version\n"
125 " --dump-conf Dump default configuration\n"
126 " --dump-modules Dump list of available modules\n"
127 " --dump-resample-methods Dump available resample methods\n"
128 " --cleanup-shm Cleanup stale shared memory segments\n"
129 " --start Start the daemon if it is not running\n"
130 " -k --kill Kill a running daemon\n"
131 " --check Check for a running daemon (only returns exit code)\n\n"
132
133 "OPTIONS:\n"
134 " --system[=BOOL] Run as system-wide instance\n"
135 " -D, --daemonize[=BOOL] Daemonize after startup\n"
136 " --fail[=BOOL] Quit when startup fails\n"
137 " --high-priority[=BOOL] Try to set high nice level\n"
138 " (only available as root, when SUID or\n"
139 " with elevated RLIMIT_NICE)\n"
140 " --realtime[=BOOL] Try to enable realtime scheduling\n"
141 " (only available as root, when SUID or\n"
142 " with elevated RLIMIT_RTPRIO)\n"
143 " --disallow-module-loading[=BOOL] Disallow module user requested module\n"
144 " loading/unloading after startup\n"
145 " --disallow-exit[=BOOL] Disallow user requested exit\n"
146 " --exit-idle-time=SECS Terminate the daemon when idle and this\n"
147 " time passed\n"
148 " --module-idle-time=SECS Unload autoloaded modules when idle and\n"
149 " this time passed\n"
150 " --scache-idle-time=SECS Unload autoloaded samples when idle and\n"
151 " this time passed\n"
152 " --log-level[=LEVEL] Increase or set verbosity level\n"
153 " -v Increase the verbosity level\n"
154 " --log-target={auto,syslog,stderr} Specify the log target\n"
155 " --log-meta[=BOOL] Include code location in log messages\n"
156 " --log-time[=BOOL] Include timestamps in log messages\n"
157 " --log-backtrace=FRAMES Include a backtrace in log messages\n"
158 " -p, --dl-search-path=PATH Set the search path for dynamic shared\n"
159 " objects (plugins)\n"
160 " --resample-method=METHOD Use the specified resampling method\n"
161 " (See --dump-resample-methods for\n"
162 " possible values)\n"
163 " --use-pid-file[=BOOL] Create a PID file\n"
164 " --no-cpu-limit[=BOOL] Do not install CPU load limiter on\n"
165 " platforms that support it.\n"
166 " --disable-shm[=BOOL] Disable shared memory support.\n\n"
167
168 "STARTUP SCRIPT:\n"
169 " -L, --load=\"MODULE ARGUMENTS\" Load the specified plugin module with\n"
170 " the specified argument\n"
171 " -F, --file=FILENAME Run the specified script\n"
172 " -C Open a command line on the running TTY\n"
173 " after startup\n\n"
174
175 " -n Don't load default script file\n"), e);
176 }
177
178 int pa_cmdline_parse(pa_daemon_conf *conf, int argc, char *const argv [], int *d) {
179 pa_strbuf *buf = NULL;
180 int c;
181
182 pa_assert(conf);
183 pa_assert(argc > 0);
184 pa_assert(argv);
185
186 buf = pa_strbuf_new();
187
188 if (conf->script_commands)
189 pa_strbuf_puts(buf, conf->script_commands);
190
191 while ((c = getopt_long(argc, argv, "L:F:ChDnp:kv", long_options, NULL)) != -1) {
192 switch (c) {
193 case ARG_HELP:
194 case 'h':
195 conf->cmd = PA_CMD_HELP;
196 break;
197
198 case ARG_VERSION:
199 conf->cmd = PA_CMD_VERSION;
200 break;
201
202 case ARG_DUMP_CONF:
203 conf->cmd = PA_CMD_DUMP_CONF;
204 break;
205
206 case ARG_DUMP_MODULES:
207 conf->cmd = PA_CMD_DUMP_MODULES;
208 break;
209
210 case ARG_DUMP_RESAMPLE_METHODS:
211 conf->cmd = PA_CMD_DUMP_RESAMPLE_METHODS;
212 break;
213
214 case ARG_CLEANUP_SHM:
215 conf->cmd = PA_CMD_CLEANUP_SHM;
216 break;
217
218 case 'k':
219 case ARG_KILL:
220 conf->cmd = PA_CMD_KILL;
221 break;
222
223 case ARG_START:
224 conf->cmd = PA_CMD_START;
225 conf->daemonize = TRUE;
226 break;
227
228 case ARG_CHECK:
229 conf->cmd = PA_CMD_CHECK;
230 break;
231
232 case ARG_LOAD:
233 case 'L':
234 pa_strbuf_printf(buf, "load-module %s\n", optarg);
235 break;
236
237 case ARG_FILE:
238 case 'F': {
239 char *p;
240 pa_strbuf_printf(buf, ".include %s\n", p = pa_make_path_absolute(optarg));
241 pa_xfree(p);
242 break;
243 }
244
245 case 'C':
246 pa_strbuf_puts(buf, "load-module module-cli exit_on_eof=1\n");
247 break;
248
249 case ARG_DAEMONIZE:
250 case 'D':
251 if ((conf->daemonize = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
252 pa_log(_("--daemonize expects boolean argument"));
253 goto fail;
254 }
255 break;
256
257 case ARG_FAIL:
258 if ((conf->fail = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
259 pa_log(_("--fail expects boolean argument"));
260 goto fail;
261 }
262 break;
263
264 case 'v':
265 case ARG_LOG_LEVEL:
266
267 if (optarg) {
268 if (pa_daemon_conf_set_log_level(conf, optarg) < 0) {
269 pa_log(_("--log-level expects log level argument (either numeric in range 0..4 or one of debug, info, notice, warn, error)."));
270 goto fail;
271 }
272 } else {
273 if (conf->log_level < PA_LOG_LEVEL_MAX-1)
274 conf->log_level++;
275 }
276
277 break;
278
279 case ARG_HIGH_PRIORITY:
280 if ((conf->high_priority = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
281 pa_log(_("--high-priority expects boolean argument"));
282 goto fail;
283 }
284 break;
285
286 case ARG_REALTIME:
287 if ((conf->realtime_scheduling = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
288 pa_log(_("--realtime expects boolean argument"));
289 goto fail;
290 }
291 break;
292
293 case ARG_DISALLOW_MODULE_LOADING:
294 if ((conf->disallow_module_loading = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
295 pa_log(_("--disallow-module-loading expects boolean argument"));
296 goto fail;
297 }
298 break;
299
300 case ARG_DISALLOW_EXIT:
301 if ((conf->disallow_exit = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
302 pa_log(_("--disallow-exit expects boolean argument"));
303 goto fail;
304 }
305 break;
306
307 case ARG_USE_PID_FILE:
308 if ((conf->use_pid_file = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
309 pa_log(_("--use-pid-file expects boolean argument"));
310 goto fail;
311 }
312 break;
313
314 case 'p':
315 case ARG_DL_SEARCH_PATH:
316 pa_xfree(conf->dl_search_path);
317 conf->dl_search_path = *optarg ? pa_xstrdup(optarg) : NULL;
318 break;
319
320 case 'n':
321 conf->load_default_script_file = FALSE;
322 break;
323
324 case ARG_LOG_TARGET:
325 if (pa_daemon_conf_set_log_target(conf, optarg) < 0) {
326 pa_log(_("Invalid log target: use either 'syslog', 'stderr' or 'auto'."));
327 goto fail;
328 }
329 break;
330
331 case ARG_LOG_TIME:
332 if ((conf->log_time = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
333 pa_log(_("--log-time expects boolean argument"));
334 goto fail;
335 }
336 break;
337
338 case ARG_LOG_META:
339 if ((conf->log_meta = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
340 pa_log(_("--log-meta expects boolean argument"));
341 goto fail;
342 }
343 break;
344
345 case ARG_LOG_BACKTRACE:
346 conf->log_backtrace = (unsigned) atoi(optarg);
347 break;
348
349 case ARG_EXIT_IDLE_TIME:
350 conf->exit_idle_time = atoi(optarg);
351 break;
352
353 case ARG_SCACHE_IDLE_TIME:
354 conf->scache_idle_time = atoi(optarg);
355 break;
356
357 case ARG_RESAMPLE_METHOD:
358 if (pa_daemon_conf_set_resample_method(conf, optarg) < 0) {
359 pa_log(_("Invalid resample method '%s'."), optarg);
360 goto fail;
361 }
362 break;
363
364 case ARG_SYSTEM:
365 if ((conf->system_instance = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
366 pa_log(_("--system expects boolean argument"));
367 goto fail;
368 }
369 break;
370
371 case ARG_NO_CPU_LIMIT:
372 if ((conf->no_cpu_limit = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
373 pa_log(_("--no-cpu-limit expects boolean argument"));
374 goto fail;
375 }
376 break;
377
378 case ARG_DISABLE_SHM:
379 if ((conf->disable_shm = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
380 pa_log(_("--disable-shm expects boolean argument"));
381 goto fail;
382 }
383 break;
384
385 default:
386 goto fail;
387 }
388 }
389
390 pa_xfree(conf->script_commands);
391 conf->script_commands = pa_strbuf_tostring_free(buf);
392
393 if (!conf->script_commands) {
394 pa_xfree(conf->script_commands);
395 conf->script_commands = NULL;
396 }
397
398 *d = optind;
399
400 return 0;
401
402 fail:
403 if (buf)
404 pa_strbuf_free(buf);
405
406 return -1;
407 }