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