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