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