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