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