]> code.delx.au - pulseaudio/blob - polyp/cmdline.c
rename some stuff
[pulseaudio] / polyp / 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 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 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 "util.h"
35 #include "strbuf.h"
36 #include "xmalloc.h"
37
38 enum {
39 ARG_HELP = 256,
40 ARG_VERSION,
41 ARG_DUMP_CONF,
42 ARG_DUMP_MODULES,
43 ARG_DAEMONIZE,
44 ARG_FAIL,
45 ARG_VERBOSE,
46 ARG_HIGH_PRIORITY,
47 ARG_DISALLOW_MODULE_LOADING,
48 ARG_EXIT_IDLE_TIME,
49 ARG_MODULE_IDLE_TIME,
50 ARG_SCACHE_IDLE_TIME,
51 ARG_LOG_TARGET,
52 ARG_LOAD,
53 ARG_FILE,
54 ARG_DL_SEARCH_PATH,
55 };
56
57 static struct option long_options[] = {
58 {"help", 0, 0, ARG_HELP},
59 {"version", 0, 0, ARG_VERSION},
60 {"dump-conf", 0, 0, ARG_DUMP_CONF},
61 {"dump-modules", 0, 0, ARG_DUMP_MODULES},
62 {"daemonize", 2, 0, ARG_DAEMONIZE},
63 {"fail", 2, 0, ARG_FAIL},
64 {"verbose", 2, 0, ARG_VERBOSE},
65 {"high-priority", 2, 0, ARG_HIGH_PRIORITY},
66 {"disallow-module-loading", 2, 0, ARG_DISALLOW_MODULE_LOADING},
67 {"exit-idle-time", 2, 0, ARG_EXIT_IDLE_TIME},
68 {"module-idle-time", 2, 0, ARG_MODULE_IDLE_TIME},
69 {"scache-idle-time", 2, 0, ARG_SCACHE_IDLE_TIME},
70 {"log-target", 1, 0, ARG_LOG_TARGET},
71 {"load", 1, 0, ARG_LOAD},
72 {"file", 1, 0, ARG_FILE},
73 {"dl-search-path", 1, 0, ARG_DL_SEARCH_PATH},
74 {NULL, 0, 0, 0}
75 };
76
77
78 void pa_cmdline_help(const char *argv0) {
79 const char *e;
80
81 if ((e = strrchr(argv0, '/')))
82 e++;
83 else
84 e = argv0;
85
86 printf("%s [options]\n"
87 " -h, --help Show this help\n"
88 " --version Show version\n"
89 " --dump-conf Dump default configuration\n"
90 " --dump-modules Dump list of available modules\n\n"
91
92 " -D, --daemonize[=BOOL] Daemonize after startup\n"
93 " --fail[=BOOL] Quit when startup fails\n"
94 " --verbose[=BOOL] Be slightly more verbose\n"
95 " --high-priority[=BOOL] Try to set high process priority (only available as root)\n"
96 " --disallow-module-loading[=BOOL] Disallow module loading after startup\n"
97 " --exit-idle-time=SECS Terminate the daemon when idle and this time passed\n"
98 " --module-idle-time=SECS Unload autoloaded modules when idle and this time passed\n"
99 " --scache-idle-time=SECS Unload autoloaded samples when idle and this time passed\n"
100 " --log-target={auto,syslog,stderr} Specify the log target\n"
101 " -p, --dl-search-path=PATH Set the search path for dynamic shared objects (plugins)\n\n"
102
103 " -L, --load=\"MODULE ARGUMENTS\" Load the specified plugin module with the specified argument\n"
104 " -F, --file=FILENAME Run the specified script\n"
105 " -C Open a command line on the running TTY after startup\n\n"
106
107 " -n Don't load default script file\n", e);
108 }
109
110 int pa_cmdline_parse(struct pa_daemon_conf *conf, int argc, char *const argv [], int *d) {
111 struct pa_strbuf *buf = NULL;
112 int c;
113 assert(conf && argc && argv);
114
115 buf = pa_strbuf_new();
116
117 if (conf->script_commands)
118 pa_strbuf_puts(buf, conf->script_commands);
119
120 while ((c = getopt_long(argc, argv, "L:F:ChDnp:", long_options, NULL)) != -1) {
121 switch (c) {
122 case ARG_HELP:
123 case 'h':
124 conf->cmd = PA_CMD_HELP;
125 break;
126
127 case ARG_VERSION:
128 conf->cmd = PA_CMD_VERSION;
129 break;
130
131 case ARG_DUMP_CONF:
132 conf->cmd = PA_CMD_DUMP_CONF;
133 break;
134
135 case ARG_DUMP_MODULES:
136 conf->cmd = PA_CMD_DUMP_MODULES;
137 break;
138
139 case ARG_LOAD:
140 case 'L':
141 pa_strbuf_printf(buf, "load-module %s\n", optarg);
142 break;
143
144 case ARG_FILE:
145 case 'F':
146 pa_strbuf_printf(buf, ".include %s\n", optarg);
147 break;
148
149 case 'C':
150 pa_strbuf_puts(buf, "load-module module-cli\n");
151 break;
152
153 case ARG_DAEMONIZE:
154 case 'D':
155 if ((conf->daemonize = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
156 pa_log(__FILE__": --daemonize expects boolean argument\n");
157 goto fail;
158 }
159 break;
160
161 case ARG_FAIL:
162 if ((conf->fail = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
163 pa_log(__FILE__": --fail expects boolean argument\n");
164 goto fail;
165 }
166 break;
167
168 case ARG_VERBOSE:
169 if ((conf->verbose = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
170 pa_log(__FILE__": --verbose expects boolean argument\n");
171 goto fail;
172 }
173 break;
174
175 case ARG_HIGH_PRIORITY:
176 if ((conf->high_priority = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
177 pa_log(__FILE__": --high-priority expects boolean argument\n");
178 goto fail;
179 }
180 break;
181
182 case ARG_DISALLOW_MODULE_LOADING:
183 if ((conf->disallow_module_loading = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
184 pa_log(__FILE__": --disallow-module-loading expects boolean argument\n");
185 goto fail;
186 }
187 break;
188
189 case 'p':
190 case ARG_DL_SEARCH_PATH:
191 pa_xfree(conf->dl_search_path);
192 conf->dl_search_path = *optarg ? pa_xstrdup(optarg) : NULL;
193 break;
194
195 case 'n':
196 pa_xfree(conf->default_script_file);
197 conf->default_script_file = NULL;
198 break;
199
200 case ARG_LOG_TARGET:
201 if (!strcmp(optarg, "syslog")) {
202 conf->auto_log_target = 0;
203 conf->log_target = PA_LOG_SYSLOG;
204 } else if (!strcmp(optarg, "stderr")) {
205 conf->auto_log_target = 0;
206 conf->log_target = PA_LOG_STDERR;
207 } else if (!strcmp(optarg, "auto"))
208 conf->auto_log_target = 1;
209 else {
210 pa_log(__FILE__": Invalid log target: use either 'syslog', 'stderr' or 'auto'.\n");
211 goto fail;
212 }
213 break;
214
215 case ARG_EXIT_IDLE_TIME:
216 conf->exit_idle_time = atoi(optarg);
217 break;
218
219 case ARG_MODULE_IDLE_TIME:
220 conf->module_idle_time = atoi(optarg);
221 break;
222
223 case ARG_SCACHE_IDLE_TIME:
224 conf->scache_idle_time = atoi(optarg);
225 break;
226
227 default:
228 goto fail;
229 }
230 }
231
232 pa_xfree(conf->script_commands);
233 conf->script_commands = pa_strbuf_tostring_free(buf);
234
235 if (!conf->script_commands) {
236 pa_xfree(conf->script_commands);
237 conf->script_commands = NULL;
238 }
239
240 *d = optind;
241
242 return 0;
243
244 fail:
245 if (buf)
246 pa_strbuf_free(buf);
247
248 return -1;
249 }