Interactive mode for para_audioc.
[paraslash.git] / audioc.c
1 /*
2 * Copyright (C) 2005-2011 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file audioc.c The client program used to connect to para_audiod. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11 #include <stdbool.h>
12
13 #include "audioc.cmdline.h"
14 #include "para.h"
15 #include "error.h"
16 #include "net.h"
17 #include "string.h"
18 #include "fd.h"
19 #include "version.h"
20
21 INIT_AUDIOC_ERRLISTS;
22
23 /** The gengetopt structure containing command line args. */
24 static struct audioc_args_info conf;
25 static char *socket_name;
26
27
28 static int loglevel;
29 INIT_STDERR_LOGGING(loglevel);
30
31 static char *concat_args(unsigned argc, char * const *argv)
32 {
33 int i;
34 char *buf = NULL;
35
36 for (i = 0; i < argc; i++) {
37 buf = para_strcat(buf, argv[i]);
38 if (i != argc - 1)
39 buf = para_strcat(buf, "\n");
40 }
41 return buf;
42 }
43
44 #ifdef HAVE_READLINE
45 #include "list.h"
46 #include "sched.h"
47 #include "buffer_tree.h"
48 #include "interactive.h"
49 #include "audiod_completion.h"
50
51 static struct sched sched;
52
53 struct audioc_task {
54 int fd;
55 struct btr_node *btrn;
56 struct task task;
57 };
58
59 static struct i9e_completer audiod_completers[];
60
61 I9E_DUMMY_COMPLETER(cycle);
62 I9E_DUMMY_COMPLETER(off);
63 I9E_DUMMY_COMPLETER(on);
64 I9E_DUMMY_COMPLETER(sb);
65 I9E_DUMMY_COMPLETER(tasks);
66 I9E_DUMMY_COMPLETER(term);
67
68 static void help_completer(struct i9e_completion_info *ci,
69 struct i9e_completion_result *result)
70 {
71 result->matches = i9e_complete_commands(ci->word, audiod_completers);
72 }
73
74 static void stat_completer(struct i9e_completion_info *ci,
75 struct i9e_completion_result *cr)
76 {
77 char *sia[] = {STATUS_ITEM_ARRAY NULL};
78 char *opts[] = {"-p", NULL};
79
80 if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
81 i9e_complete_option(opts, ci, cr);
82 else
83 i9e_extract_completions(ci->word, sia, &cr->matches);
84 }
85
86 static void grab_completer(struct i9e_completion_info *ci,
87 struct i9e_completion_result *cr)
88 {
89 char *opts[] = {"-ms", "-ms", "-ma", "-p=", "-n=", "-o", NULL};
90 i9e_complete_option(opts, ci, cr);
91 }
92
93 static struct i9e_completer audiod_completers[] = {
94 AUDIOD_COMPLETERS
95 {.name = NULL}
96 };
97
98 static void audioc_pre_select(struct sched *s, struct task *t)
99 {
100 struct audioc_task *at = container_of(t, struct audioc_task, task);
101 int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
102
103 if (ret < 0)
104 sched_min_delay(s);
105 para_fd_set(at->fd, &s->rfds, &s->max_fileno);
106 }
107
108 static void audioc_post_select(struct sched *s, struct task *t)
109 {
110 char *buf = NULL;
111 struct audioc_task *at = container_of(t, struct audioc_task, task);
112 int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
113
114 if (ret < 0)
115 goto out;
116 if (!FD_ISSET(at->fd, &s->rfds))
117 return;
118 buf = para_malloc(conf.bufsize_arg);
119 ret = recv_bin_buffer(at->fd, buf, conf.bufsize_arg);
120 PARA_DEBUG_LOG("recv: %d\n", ret);
121 if (ret == 0)
122 ret = -E_AUDIOC_EOF;
123 if (ret < 0)
124 goto out;
125 btr_add_output(buf, ret, at->btrn);
126 return;
127 out:
128 if (ret < 0) {
129 free(buf);
130 btr_remove_node(at->btrn);
131 btr_free_node(at->btrn);
132 at->btrn = NULL;
133 close(at->fd);
134 }
135 t->error = ret;
136 }
137
138 static struct audioc_task audioc_task = {
139 .task = {
140 .pre_select = audioc_pre_select,
141 .post_select = audioc_post_select,
142 .status = "audioc task"
143 },
144 }, *at = &audioc_task;
145
146 static int audioc_i9e_line_handler(char *line)
147 {
148 char *args = NULL;
149 int ret;
150 if (!line || !*line)
151 return 0;
152
153 PARA_DEBUG_LOG("line: %s\n", line);
154 ret = create_argv(line, " ", &conf.inputs);
155 if (ret < 0)
156 return ret;
157 conf.inputs_num = ret;
158 args = concat_args(conf.inputs_num, conf.inputs);
159 free_argv(conf.inputs);
160 conf.inputs_num = 0; /* required for audioc_cmdline_parser_free() */
161 ret = connect_local_socket(socket_name);
162 if (ret < 0)
163 goto out;
164 at->fd = ret;
165 ret = mark_fd_nonblocking(at->fd);
166 if (ret < 0)
167 goto close;
168 ret = send_cred_buffer(at->fd, args);
169 if (ret < 0)
170 goto close;
171 free(args);
172 args = NULL;
173 at->btrn = btr_new_node(&(struct btr_node_description)
174 EMBRACE(.name = "audioc line handler"));
175 at->task.error = 0;
176 register_task(&sched, &at->task);
177 i9e_attach_to_stdout(at->btrn);
178 return 1;
179 close:
180 close(at->fd);
181 out:
182 free(args);
183 return ret;
184 }
185
186 __noreturn static void interactive_session(void)
187 {
188 int ret;
189 char *history_file;
190 struct sigaction act;
191 struct i9e_client_info ici = {
192 .fds = {0, 1, 2},
193 .prompt = "para_audioc> ",
194 .line_handler = audioc_i9e_line_handler,
195 .loglevel = loglevel,
196 .completers = audiod_completers,
197 };
198 PARA_NOTICE_LOG("\n%s\n", VERSION_TEXT("audioc"));
199 if (conf.history_file_given)
200 history_file = para_strdup(conf.history_file_arg);
201 else {
202 char *home = para_homedir();
203 history_file = make_message("%s/.paraslash/audioc.history",
204 home);
205 free(home);
206 }
207 ici.history_file = history_file;
208
209 act.sa_handler = i9e_signal_dispatch;
210 sigemptyset(&act.sa_mask);
211 act.sa_flags = 0;
212 sigaction(SIGINT, &act, NULL);
213 sched.select_function = i9e_select;
214
215 sched.default_timeout.tv_sec = 1;
216 ret = i9e_open(&ici, &sched);
217 if (ret < 0)
218 goto out;
219 para_log = i9e_log;
220 ret = schedule(&sched);
221 i9e_close();
222 para_log = stderr_log;
223 out:
224 free(history_file);
225 audioc_cmdline_parser_free(&conf);
226 if (ret < 0)
227 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
228 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
229 }
230
231 __noreturn static void print_completions(void)
232 {
233 int ret = i9e_print_completions(audiod_completers);
234 exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
235 }
236
237 #else /* HAVE_READLINE */
238
239 __noreturn static void interactive_session(void)
240 {
241 PARA_EMERG_LOG("interactive sessions not available\n");
242 exit(EXIT_FAILURE);
243 }
244
245 __noreturn static void print_completions(void)
246 {
247 PARA_EMERG_LOG("command completion not available\n");
248 exit(EXIT_FAILURE);
249 }
250
251 #endif /* HAVE_READLINE */
252
253 static char *configfile_exists(void)
254 {
255 static char *config_file;
256 struct stat statbuf;
257
258 if (!config_file) {
259 char *home = para_homedir();
260 config_file = make_message("%s/.paraslash/audioc.conf", home);
261 free(home);
262 }
263 if (!stat(config_file, &statbuf))
264 return config_file;
265 return NULL;
266 }
267
268 /**
269 * The client program to connect to para_audiod.
270 *
271 * \param argc Usual argument count.
272 * \param argv Usual argument vector.
273 *
274 * It creates a temporary local socket in order to communicate with para_audiod.
275 * Authentication consists in sending a ucred buffer that contains the user id.
276 *
277 * Any output received through the local socket is sent to stdout.
278 *
279 * \return EXIT_SUCCESS or EXIT_FAILURE.
280 *
281 * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
282 */
283 int main(int argc, char *argv[])
284 {
285 int ret = -E_AUDIOC_SYNTAX, fd;
286 char *cf, *buf = NULL, *args;
287 size_t bufsize;
288
289 if (audioc_cmdline_parser(argc, argv, &conf))
290 goto out;
291 HANDLE_VERSION_FLAG("audioc", conf);
292 cf = configfile_exists();
293 if (cf) {
294 struct audioc_cmdline_parser_params params = {
295 .override = 0,
296 .initialize = 0,
297 .check_required = 0,
298 .check_ambiguity = 0
299 };
300 if (audioc_cmdline_parser_config_file(cf, &conf, &params)) {
301 fprintf(stderr, "parse error in config file\n");
302 exit(EXIT_FAILURE);
303 }
304 }
305 loglevel = get_loglevel_by_name(conf.loglevel_arg);
306 if (conf.socket_given)
307 socket_name = para_strdup(conf.socket_arg);
308 else {
309 char *hn = para_hostname();
310 socket_name = make_message("/var/paraslash/audiod_socket.%s",
311 hn);
312 free(hn);
313 }
314
315 if (conf.complete_given)
316 print_completions();
317
318 if (conf.inputs_num == 0)
319 interactive_session();
320 args = concat_args(conf.inputs_num, conf.inputs);
321
322 ret = connect_local_socket(socket_name);
323 free(socket_name);
324 if (ret < 0) {
325 PARA_EMERG_LOG("failed to connect to local socket\n");
326 goto out;
327 }
328 fd = ret;
329 ret = send_cred_buffer(fd, args);
330 if (ret < 0)
331 goto out;
332 bufsize = conf.bufsize_arg;
333 buf = para_malloc(bufsize);
334 do {
335 size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
336 if (ret <= 0)
337 break;
338 ret = write_all(STDOUT_FILENO, buf, &n);
339 } while (ret >= 0);
340 out:
341 if (ret < 0)
342 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
343 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
344 }