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