client: Remove sb-compatibility code.
[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 #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                 close(at->fd);
133         }
134         t->error = ret;
135 }
136
137 static struct audioc_task audioc_task = {
138         .task = {
139                 .pre_select = audioc_pre_select,
140                 .post_select = audioc_post_select,
141                 .status = "audioc task"
142         },
143 }, *at = &audioc_task;
144
145 static int audioc_i9e_line_handler(char *line)
146 {
147         char *args = NULL;
148         int ret;
149         if (!line || !*line)
150                 return 0;
151
152         PARA_DEBUG_LOG("line: %s\n", line);
153         ret = create_argv(line, " ", &conf.inputs);
154         if (ret < 0)
155                 return ret;
156         conf.inputs_num = ret;
157         args = concat_args(conf.inputs_num, conf.inputs);
158         free_argv(conf.inputs);
159         conf.inputs_num = 0; /* required for audioc_cmdline_parser_free() */
160         ret = connect_local_socket(socket_name);
161         if (ret < 0)
162                 goto out;
163         at->fd = ret;
164         ret = mark_fd_nonblocking(at->fd);
165         if (ret < 0)
166                 goto close;
167         ret = send_cred_buffer(at->fd, args);
168         if (ret < 0)
169                 goto close;
170         free(args);
171         args = NULL;
172         at->btrn = btr_new_node(&(struct btr_node_description)
173                 EMBRACE(.name = "audioc line handler"));
174         at->task.error = 0;
175         register_task(&sched, &at->task);
176         i9e_attach_to_stdout(at->btrn);
177         return 1;
178 close:
179         close(at->fd);
180 out:
181         free(args);
182         return ret;
183 }
184
185 __noreturn static void interactive_session(void)
186 {
187         int ret;
188         char *history_file;
189         struct sigaction act;
190         struct i9e_client_info ici = {
191                 .fds = {0, 1, 2},
192                 .prompt = "para_audioc> ",
193                 .line_handler = audioc_i9e_line_handler,
194                 .loglevel = loglevel,
195                 .completers = audiod_completers,
196         };
197         PARA_NOTICE_LOG("\n%s\n", VERSION_TEXT("audioc"));
198         if (conf.history_file_given)
199                 history_file = para_strdup(conf.history_file_arg);
200         else {
201                 char *home = para_homedir();
202                 history_file = make_message("%s/.paraslash/audioc.history",
203                         home);
204                 free(home);
205         }
206         ici.history_file = history_file;
207
208         act.sa_handler = i9e_signal_dispatch;
209         sigemptyset(&act.sa_mask);
210         act.sa_flags = 0;
211         sigaction(SIGINT, &act, NULL);
212         sched.select_function = i9e_select;
213
214         sched.default_timeout.tv_sec = 1;
215         ret = i9e_open(&ici, &sched);
216         if (ret < 0)
217                 goto out;
218         para_log = i9e_log;
219         ret = schedule(&sched);
220         i9e_close();
221         para_log = stderr_log;
222 out:
223         free(history_file);
224         audioc_cmdline_parser_free(&conf);
225         if (ret < 0)
226                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
227         exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
228 }
229
230 __noreturn static void print_completions(void)
231 {
232         int ret = i9e_print_completions(audiod_completers);
233         exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
234 }
235
236 #else /* HAVE_READLINE */
237
238 __noreturn static void interactive_session(void)
239 {
240         PARA_EMERG_LOG("interactive sessions not available\n");
241         exit(EXIT_FAILURE);
242 }
243
244 __noreturn static void print_completions(void)
245 {
246         PARA_EMERG_LOG("command completion not available\n");
247         exit(EXIT_FAILURE);
248 }
249
250 #endif /* HAVE_READLINE */
251
252 static char *configfile_exists(void)
253 {
254         char *config_file;
255         struct stat statbuf;
256         char *home = para_homedir();
257
258         config_file = make_message("%s/.paraslash/audioc.conf", home);
259         free(home);
260         if (!stat(config_file, &statbuf))
261                 return config_file;
262         free(config_file);
263         return NULL;
264 }
265
266 /**
267  * The client program to connect to para_audiod.
268  *
269  * \param argc Usual argument count.
270  * \param argv Usual argument vector.
271  *
272  * It connects to the "well-known" local socket to communicate with
273  * para_audiod. Authentication is performed by sending a ucred buffer
274  * containing the user id to the local socket.
275  *
276  * Any data received from the socket is written to stdout.
277  *
278  * \return EXIT_SUCCESS or EXIT_FAILURE.
279  *
280  * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
281  */
282 int main(int argc, char *argv[])
283 {
284         int ret = -E_AUDIOC_SYNTAX, fd;
285         char *cf, *buf = NULL, *args = NULL;
286         size_t bufsize;
287
288         if (audioc_cmdline_parser(argc, argv, &conf))
289                 goto out;
290         HANDLE_VERSION_FLAG("audioc", conf);
291         cf = configfile_exists();
292         if (cf) {
293                 struct audioc_cmdline_parser_params params = {
294                         .override = 0,
295                         .initialize = 0,
296                         .check_required = 0,
297                         .check_ambiguity = 0
298                 };
299                 ret = audioc_cmdline_parser_config_file(cf, &conf, &params);
300                 free(cf);
301                 if (ret) {
302                         fprintf(stderr, "parse error in config file\n");
303                         exit(EXIT_FAILURE);
304                 }
305         }
306         loglevel = get_loglevel_by_name(conf.loglevel_arg);
307         if (conf.socket_given)
308                 socket_name = para_strdup(conf.socket_arg);
309         else {
310                 char *hn = para_hostname();
311                 socket_name = make_message("/var/paraslash/audiod_socket.%s",
312                         hn);
313                 free(hn);
314         }
315
316         if (conf.complete_given)
317                 print_completions();
318
319         if (conf.inputs_num == 0)
320                 interactive_session();
321         args = concat_args(conf.inputs_num, conf.inputs);
322
323         ret = connect_local_socket(socket_name);
324         free(socket_name);
325         if (ret < 0) {
326                 PARA_EMERG_LOG("failed to connect to local socket\n");
327                 goto out;
328         }
329         fd = ret;
330         ret = send_cred_buffer(fd, args);
331         if (ret < 0)
332                 goto out;
333         bufsize = conf.bufsize_arg;
334         buf = para_malloc(bufsize);
335         ret = mark_fd_blocking(STDOUT_FILENO);
336         if (ret < 0)
337                 goto out;
338         do {
339                 size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
340                 if (ret <= 0)
341                         break;
342                 ret = write_all(STDOUT_FILENO, buf, n);
343         } while (ret >= 0);
344 out:
345         free(buf);
346         free(args);
347         if (ret < 0)
348                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
349         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
350 }