]> code.delx.au - pulseaudio/blob - polyp/cmdline.c
b4d58f1fb75c7fbdeab608c068fd28f09dc076fd
[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 #define ENV_CONFIG_FILE "POLYP_CONFIG"
39
40 char* config_file(void) {
41 char *p, *h;
42
43 if ((p = getenv(ENV_CONFIG_FILE)))
44 return pa_xstrdup(p);
45
46 if ((h = getenv("HOME"))) {
47 struct stat st;
48 p = pa_sprintf_malloc("%s/.polypaudio", h);
49 if (stat(p, &st) >= 0)
50 return p;
51
52 pa_xfree(p);
53 }
54
55 return pa_xstrdup(DEFAULT_CONFIG_FILE);
56 }
57
58 void pa_cmdline_help(const char *argv0) {
59 const char *e;
60 char *cfg = config_file();
61
62 if ((e = strrchr(argv0, '/')))
63 e++;
64 else
65 e = argv0;
66
67 printf("%s [options]\n"
68 " -r Try to set high process priority (only available as root)\n"
69 " -R Don't drop root if SETUID root\n"
70 " -L MODULE Load the specified plugin module with the specified argument\n"
71 " -F FILE Run the specified script\n"
72 " -C Open a command line on the running TTY\n"
73 " -n Don't load configuration file (%s)\n"
74 " -D Daemonize after loading the modules\n"
75 " -d Disallow module loading after startup\n"
76 " -f Dont quit when the startup fails\n"
77 " -v Verbose startup\n"
78 " -X SECS Terminate the daemon after the last client quit and this time passed\n"
79 " -h Show this help\n"
80 " -l TARGET Specify the log target (syslog, stderr, auto)\n"
81 " -p DIR Append a directory to the search path for dynamic modules\n"
82 " -V Show version\n", e, cfg);
83
84 pa_xfree(cfg);
85 }
86
87 struct pa_cmdline* pa_cmdline_parse(int argc, char * const argv []) {
88 char c, *cfg;
89 struct pa_cmdline *cmdline = NULL;
90 struct pa_strbuf *buf = NULL;
91 int no_default_config_file = 0;
92 assert(argc && argv);
93
94 cmdline = pa_xmalloc(sizeof(struct pa_cmdline));
95 cmdline->daemonize =
96 cmdline->help =
97 cmdline->verbose =
98 cmdline->high_priority =
99 cmdline->stay_root =
100 cmdline->version =
101 cmdline->disallow_module_loading = 0;
102 cmdline->fail = cmdline->auto_log_target = 1;
103 cmdline->quit_after_last_client_time = -1;
104 cmdline->log_target = -1;
105 cmdline->dl_search_path = NULL;
106
107 buf = pa_strbuf_new();
108 assert(buf);
109
110 while ((c = getopt(argc, argv, "L:F:CDhfvrRVndX:l:p:")) != -1) {
111 switch (c) {
112 case 'L':
113 pa_strbuf_printf(buf, "load %s\n", optarg);
114 break;
115 case 'F':
116 pa_strbuf_printf(buf, ".include %s\n", optarg);
117 break;
118 case 'C':
119 pa_strbuf_puts(buf, "load module-cli\n");
120 break;
121 case 'D':
122 cmdline->daemonize = 1;
123 break;
124 case 'h':
125 cmdline->help = 1;
126 break;
127 case 'f':
128 cmdline->fail = 0;
129 break;
130 case 'v':
131 cmdline->verbose = 1;
132 break;
133 case 'r':
134 cmdline->high_priority = 1;
135 break;
136 case 'R':
137 cmdline->stay_root = 1;
138 break;
139 case 'V':
140 cmdline->version = 1;
141 break;
142 case 'n':
143 no_default_config_file = 1;
144 break;
145 case 'd':
146 cmdline->disallow_module_loading = 1;
147 break;
148 case 'X':
149 cmdline->quit_after_last_client_time = atoi(optarg);
150 break;
151 case 'p':
152 if (cmdline->dl_search_path)
153 pa_xfree(cmdline->dl_search_path);
154 cmdline->dl_search_path = pa_xstrdup(optarg);
155 break;
156 case 'l':
157 if (!strcmp(optarg, "syslog")) {
158 cmdline->auto_log_target = 0;
159 cmdline->log_target = PA_LOG_SYSLOG;
160 } else if (!strcmp(optarg, "stderr")) {
161 cmdline->auto_log_target = 0;
162 cmdline->log_target = PA_LOG_STDERR;
163 } else if (!strcmp(optarg, "auto"))
164 cmdline->auto_log_target = 1;
165 else {
166 pa_log(__FILE__": Invalid log target: use either 'syslog', 'stderr' or 'auto'.\n");
167 goto fail;
168 }
169 break;
170 default:
171 goto fail;
172 }
173 }
174
175 if (!no_default_config_file) {
176 cfg = config_file();
177 pa_strbuf_printf(buf, ".include %s\n", cfg);
178 pa_xfree(cfg);
179 }
180
181 cmdline->cli_commands = pa_strbuf_tostring_free(buf);
182 return cmdline;
183
184 fail:
185 if (cmdline)
186 pa_cmdline_free(cmdline);
187 if (buf)
188 pa_strbuf_free(buf);
189 return NULL;
190 }
191
192 void pa_cmdline_free(struct pa_cmdline *cmd) {
193 assert(cmd);
194 pa_xfree(cmd->cli_commands);
195 pa_xfree(cmd->dl_search_path);
196 pa_xfree(cmd);
197 }