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