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