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