Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / 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 <assert.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <sys/stat.h>
34
35 #include <pulse/xmalloc.h>
36
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/strbuf.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_SYSTEM
67 };
68
69 /* Tabel for getopt_long() */
70 static struct option long_options[] = {
71     {"help",                        0, 0, ARG_HELP},
72     {"version",                     0, 0, ARG_VERSION},
73     {"dump-conf",                   0, 0, ARG_DUMP_CONF},
74     {"dump-modules",                0, 0, ARG_DUMP_MODULES},
75     {"daemonize",                   2, 0, ARG_DAEMONIZE},
76     {"fail",                        2, 0, ARG_FAIL},
77     {"verbose",                     2, 0, ARG_LOG_LEVEL},
78     {"log-level",                   2, 0, ARG_LOG_LEVEL},
79     {"high-priority",               2, 0, ARG_HIGH_PRIORITY},
80     {"disallow-module-loading",     2, 0, ARG_DISALLOW_MODULE_LOADING},
81     {"exit-idle-time",              2, 0, ARG_EXIT_IDLE_TIME},
82     {"module-idle-time",            2, 0, ARG_MODULE_IDLE_TIME},
83     {"scache-idle-time",            2, 0, ARG_SCACHE_IDLE_TIME},
84     {"log-target",                  1, 0, ARG_LOG_TARGET},
85     {"load",                        1, 0, ARG_LOAD},
86     {"file",                        1, 0, ARG_FILE},
87     {"dl-search-path",              1, 0, ARG_DL_SEARCH_PATH},
88     {"resample-method",             1, 0, ARG_RESAMPLE_METHOD},
89     {"kill",                        0, 0, ARG_KILL},
90     {"use-pid-file",                2, 0, ARG_USE_PID_FILE},
91     {"check",                       0, 0, ARG_CHECK},
92     {"system",                      2, 0, ARG_SYSTEM},
93     {"no-cpu-limit",                2, 0, ARG_NO_CPU_LIMIT},
94     {"disable-shm",                 2, 0, ARG_DISABLE_SHM},
95     {NULL, 0, 0, 0}
96 };
97
98 void pa_cmdline_help(const char *argv0) {
99     const char *e;
100
101     if ((e = strrchr(argv0, '/')))
102         e++;
103     else
104         e = argv0;
105
106     printf("%s [options]\n\n"
107            "COMMANDS:\n"
108            "  -h, --help                            Show this help\n"
109            "      --version                         Show version\n"
110            "      --dump-conf                       Dump default configuration\n"
111            "      --dump-modules                    Dump list of available modules\n"
112            "  -k  --kill                            Kill a running daemon\n"
113            "      --check                           Check for a running daemon\n\n"
114
115            "OPTIONS:\n"
116            "      --system[=BOOL]                   Run as system-wide instance\n"
117            "  -D, --daemonize[=BOOL]                Daemonize after startup\n"
118            "      --fail[=BOOL]                     Quit when startup fails\n"
119            "      --high-priority[=BOOL]            Try to set high process priority\n"
120            "                                        (only available as root)\n"
121            "      --disallow-module-loading[=BOOL]  Disallow module loading after startup\n"
122            "      --exit-idle-time=SECS             Terminate the daemon when idle and this\n"
123            "                                        time passed\n"
124            "      --module-idle-time=SECS           Unload autoloaded modules when idle and\n"
125            "                                        this time passed\n"
126            "      --scache-idle-time=SECS           Unload autoloaded samples when idle and\n"
127            "                                        this time passed\n"
128            "      --log-level[=LEVEL]               Increase or set verbosity level\n"
129            "  -v                                    Increase the verbosity level\n"
130            "      --log-target={auto,syslog,stderr} Specify the log target\n"
131            "  -p, --dl-search-path=PATH             Set the search path for dynamic shared\n"
132            "                                        objects (plugins)\n"
133            "      --resample-method=[METHOD]        Use the specified resampling method\n"
134            "                                        (one of src-sinc-medium-quality,\n"
135            "                                        src-sinc-best-quality,src-sinc-fastest\n"
136            "                                        src-zero-order-hold,src-linear,trivial)\n"
137            "      --use-pid-file[=BOOL]             Create a PID file\n"
138            "      --no-cpu-limit[=BOOL]             Do not install CPU load limiter on\n"
139            "                                        platforms that support it.\n"
140            "      --disable-shm[=BOOL]              Disable shared memory support.\n\n"
141
142            "STARTUP SCRIPT:\n"
143            "  -L, --load=\"MODULE ARGUMENTS\"         Load the specified plugin module with\n"
144            "                                        the specified argument\n"
145            "  -F, --file=FILENAME                   Run the specified script\n"
146            "  -C                                    Open a command line on the running TTY\n"
147            "                                        after startup\n\n"
148
149            "  -n                                    Don't load default script file\n", e);
150 }
151
152 int pa_cmdline_parse(pa_daemon_conf *conf, int argc, char *const argv [], int *d) {
153     pa_strbuf *buf = NULL;
154     int c;
155     assert(conf && argc && argv);
156
157     buf = pa_strbuf_new();
158
159     if (conf->script_commands)
160         pa_strbuf_puts(buf, conf->script_commands);
161
162     while ((c = getopt_long(argc, argv, "L:F:ChDnp:kv", long_options, NULL)) != -1) {
163         switch (c) {
164             case ARG_HELP:
165             case 'h':
166                 conf->cmd = PA_CMD_HELP;
167                 break;
168
169             case ARG_VERSION:
170                 conf->cmd = PA_CMD_VERSION;
171                 break;
172
173             case ARG_DUMP_CONF:
174                 conf->cmd = PA_CMD_DUMP_CONF;
175                 break;
176
177             case ARG_DUMP_MODULES:
178                 conf->cmd = PA_CMD_DUMP_MODULES;
179                 break;
180
181             case 'k':
182             case ARG_KILL:
183                 conf->cmd = PA_CMD_KILL;
184                 break;
185
186             case ARG_CHECK:
187                 conf->cmd = PA_CMD_CHECK;
188                 break;
189
190             case ARG_LOAD:
191             case 'L':
192                 pa_strbuf_printf(buf, "load-module %s\n", optarg);
193                 break;
194
195             case ARG_FILE:
196             case 'F':
197                 pa_strbuf_printf(buf, ".include %s\n", optarg);
198                 break;
199
200             case 'C':
201                 pa_strbuf_puts(buf, "load-module module-cli exit_on_eof=1\n");
202                 break;
203
204             case ARG_DAEMONIZE:
205             case 'D':
206                 if ((conf->daemonize = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
207                     pa_log("--daemonize expects boolean argument");
208                     goto fail;
209                 }
210                 break;
211
212             case ARG_FAIL:
213                 if ((conf->fail = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
214                     pa_log("--fail expects boolean argument");
215                     goto fail;
216                 }
217                 break;
218
219             case 'v':
220             case ARG_LOG_LEVEL:
221
222                 if (optarg) {
223                     if (pa_daemon_conf_set_log_level(conf, optarg) < 0) {
224                         pa_log("--log-level expects log level argument (either numeric in range 0..4 or one of debug, info, notice, warn, error).");
225                         goto fail;
226                     }
227                 } else {
228                     if (conf->log_level < PA_LOG_LEVEL_MAX-1)
229                         conf->log_level++;
230                 }
231
232                 break;
233
234             case ARG_HIGH_PRIORITY:
235                 if ((conf->high_priority = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
236                     pa_log("--high-priority expects boolean argument");
237                     goto fail;
238                 }
239                 break;
240
241             case ARG_DISALLOW_MODULE_LOADING:
242                 if ((conf->disallow_module_loading = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
243                     pa_log("--disallow-module-loading expects boolean argument");
244                     goto fail;
245                 }
246                 break;
247
248             case ARG_USE_PID_FILE:
249                 if ((conf->use_pid_file = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
250                     pa_log("--use-pid-file expects boolean argument");
251                     goto fail;
252                 }
253                 break;
254
255             case 'p':
256             case ARG_DL_SEARCH_PATH:
257                 pa_xfree(conf->dl_search_path);
258                 conf->dl_search_path = *optarg ? pa_xstrdup(optarg) : NULL;
259                 break;
260
261             case 'n':
262                 pa_xfree(conf->default_script_file);
263                 conf->default_script_file = NULL;
264                 break;
265
266             case ARG_LOG_TARGET:
267                 if (pa_daemon_conf_set_log_target(conf, optarg) < 0) {
268                     pa_log("Invalid log target: use either 'syslog', 'stderr' or 'auto'.");
269                     goto fail;
270                 }
271                 break;
272
273             case ARG_EXIT_IDLE_TIME:
274                 conf->exit_idle_time = atoi(optarg);
275                 break;
276
277             case ARG_MODULE_IDLE_TIME:
278                 conf->module_idle_time = atoi(optarg);
279                 break;
280
281             case ARG_SCACHE_IDLE_TIME:
282                 conf->scache_idle_time = atoi(optarg);
283                 break;
284
285             case ARG_RESAMPLE_METHOD:
286                 if (pa_daemon_conf_set_resample_method(conf, optarg) < 0) {
287                     pa_log("Invalid resample method '%s'.", optarg);
288                     goto fail;
289                 }
290                 break;
291
292             case ARG_SYSTEM:
293                 if ((conf->system_instance = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
294                     pa_log("--system expects boolean argument");
295                     goto fail;
296                 }
297                 break;
298
299             case ARG_NO_CPU_LIMIT:
300                 if ((conf->no_cpu_limit = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
301                     pa_log("--no-cpu-limit expects boolean argument");
302                     goto fail;
303                 }
304                 break;
305
306             case ARG_DISABLE_SHM:
307                 if ((conf->disable_shm = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
308                     pa_log("--disable-shm expects boolean argument");
309                     goto fail;
310                 }
311                 break;
312
313             default:
314                 goto fail;
315         }
316     }
317
318     pa_xfree(conf->script_commands);
319     conf->script_commands = pa_strbuf_tostring_free(buf);
320
321     if (!conf->script_commands) {
322         pa_xfree(conf->script_commands);
323         conf->script_commands = NULL;
324     }
325
326     *d = optind;
327
328     return 0;
329
330 fail:
331     if (buf)
332         pa_strbuf_free(buf);
333
334     return -1;
335 }