Convert audiod commands to lopsub.
[paraslash.git] / audioc.c
index 42b4628b46b03a5416f35a908c159c4ede42c66e..dabc2f77eb18d8b705241acc0753fd2fbbe34736 100644 (file)
--- a/audioc.c
+++ b/audioc.c
@@ -1,25 +1,33 @@
 /*
- * Copyright (C) 2005-2013 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
 /** \file audioc.c The client program used to connect to para_audiod. */
 
+#include <netinet/in.h>
+#include <sys/socket.h>
 #include <regex.h>
 #include <sys/types.h>
-#include <stdbool.h>
+#include <arpa/inet.h>
+#include <sys/un.h>
+#include <netdb.h>
 #include <signal.h>
 
 #include "audioc.cmdline.h"
+#include "audiod_cmd.lsg.h"
+
 #include "para.h"
 #include "error.h"
 #include "net.h"
 #include "string.h"
 #include "fd.h"
+#include "ggo.h"
 #include "version.h"
 
-INIT_AUDIOC_ERRLISTS;
+/** Array of error strings. */
+DEFINE_PARA_ERRLIST;
 
 /** The gengetopt structure containing command line args. */
 static struct audioc_args_info conf;
@@ -42,19 +50,37 @@ static char *concat_args(unsigned argc, char * const *argv)
        return buf;
 }
 
+static int connect_audiod(const char *sname, char *args)
+{
+       int fd = -1, ret;
+
+       ret = connect_local_socket(sname);
+       if (ret < 0)
+               goto fail;
+       fd = ret;
+       ret = send_cred_buffer(fd, args);
+       if (ret < 0)
+               goto fail;
+       return fd;
+fail:
+       PARA_ERROR_LOG("could not connect %s\n", sname);
+       if (fd >= 0)
+               close(fd);
+       return ret;
+}
+
 #ifdef HAVE_READLINE
 #include "list.h"
 #include "sched.h"
 #include "buffer_tree.h"
 #include "interactive.h"
-#include "audiod_completion.h"
 
 static struct sched sched;
 
 struct audioc_task {
        int fd;
        struct btr_node *btrn;
-       struct task task;
+       struct task *task;
 };
 
 static struct i9e_completer audiod_completers[];
@@ -72,11 +98,20 @@ static void help_completer(struct i9e_completion_info *ci,
        result->matches = i9e_complete_commands(ci->word, audiod_completers);
 }
 
+static void version_completer(struct i9e_completion_info *ci,
+               struct i9e_completion_result *cr)
+{
+       char *opts[] = {LSG_AUDIOD_CMD_VERSION_OPTS, NULL};
+
+       if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
+               i9e_complete_option(opts, ci, cr);
+}
+
 static void stat_completer(struct i9e_completion_info *ci,
                struct i9e_completion_result *cr)
 {
        char *sia[] = {STATUS_ITEM_ARRAY NULL};
-       char *opts[] = {"-p", NULL};
+       char *opts[] = {LSG_AUDIOD_CMD_STAT_OPTS, NULL};
 
        if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
                i9e_complete_option(opts, ci, cr);
@@ -87,18 +122,22 @@ static void stat_completer(struct i9e_completion_info *ci,
 static void grab_completer(struct i9e_completion_info *ci,
                struct i9e_completion_result *cr)
 {
-       char *opts[] = {"-ms", "-ms", "-ma", "-p=", "-n=", "-o", NULL};
+       char *opts[] = {LSG_AUDIOD_CMD_GRAB_OPTS, NULL};
        i9e_complete_option(opts, ci, cr);
 }
 
+I9E_DUMMY_COMPLETER(SUPERCOMMAND_UNAVAILABLE);
 static struct i9e_completer audiod_completers[] = {
-       AUDIOD_COMPLETERS
+#define LSG_AUDIOD_CMD_CMD(_name) {.name = #_name, \
+       .completer = _name ## _completer}
+       LSG_AUDIOD_CMD_SUBCOMMANDS
+#undef LSG_AUDIOD_CMD_CMD
        {.name = NULL}
 };
 
-static void audioc_pre_select(struct sched *s, struct task *t)
+static void audioc_pre_select(struct sched *s, void *context)
 {
-       struct audioc_task *at = container_of(t, struct audioc_task, task);
+       struct audioc_task *at = context;
        int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
 
        if (ret < 0)
@@ -106,10 +145,10 @@ static void audioc_pre_select(struct sched *s, struct task *t)
        para_fd_set(at->fd, &s->rfds, &s->max_fileno);
 }
 
-static int audioc_post_select(struct sched *s, struct task *t)
+static int audioc_post_select(struct sched *s, void *context)
 {
        char *buf = NULL;
-       struct audioc_task *at = container_of(t, struct audioc_task, task);
+       struct audioc_task *at = context;
        int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
 
        if (ret < 0)
@@ -134,20 +173,12 @@ out:
        return ret;
 }
 
-static struct audioc_task audioc_task = {
-       .task = {
-               .pre_select = audioc_pre_select,
-               .post_select = audioc_post_select,
-               .status = "audioc task"
-       },
-}, *at = &audioc_task;
+static struct audioc_task audioc_task, *at = &audioc_task;
 
 static int audioc_i9e_line_handler(char *line)
 {
        char *args = NULL;
        int ret;
-       if (!line || !*line)
-               return 0;
 
        PARA_DEBUG_LOG("line: %s\n", line);
        ret = create_argv(line, " ", &conf.inputs);
@@ -156,23 +187,26 @@ static int audioc_i9e_line_handler(char *line)
        conf.inputs_num = ret;
        args = concat_args(conf.inputs_num, conf.inputs);
        free_argv(conf.inputs);
+       if (!args)
+               return 0;
        conf.inputs_num = 0; /* required for audioc_cmdline_parser_free() */
-       ret = connect_local_socket(socket_name);
+       ret = connect_audiod(socket_name, args);
        if (ret < 0)
                goto out;
        at->fd = ret;
        ret = mark_fd_nonblocking(at->fd);
-       if (ret < 0)
-               goto close;
-       ret = send_cred_buffer(at->fd, args);
        if (ret < 0)
                goto close;
        free(args);
        args = NULL;
        at->btrn = btr_new_node(&(struct btr_node_description)
                EMBRACE(.name = "audioc line handler"));
-       at->task.error = 0;
-       register_task(&sched, &at->task);
+       at->task = task_register(&(struct task_info) {
+               .name = "audioc",
+               .pre_select = audioc_pre_select,
+               .post_select = audioc_post_select,
+               .context = at,
+       }, &sched);
        i9e_attach_to_stdout(at->btrn);
        return 1;
 close:
@@ -194,7 +228,7 @@ __noreturn static void interactive_session(void)
                .loglevel = loglevel,
                .completers = audiod_completers,
        };
-       PARA_NOTICE_LOG("\n%s\n", VERSION_TEXT("audioc"));
+       PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
        if (conf.history_file_given)
                history_file = para_strdup(conf.history_file_arg);
        else {
@@ -217,6 +251,7 @@ __noreturn static void interactive_session(void)
                goto out;
        para_log = i9e_log;
        ret = schedule(&sched);
+       sched_shutdown(&sched);
        i9e_close();
        para_log = stderr_log;
 out:
@@ -263,6 +298,15 @@ static char *configfile_exists(void)
        return NULL;
 }
 
+__noreturn static void print_help_and_die(void)
+{
+       struct ggo_help h = DEFINE_GGO_HELP(audioc);
+       bool d = conf.detailed_help_given;
+
+       ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
+       exit(0);
+}
+
 /**
  * The client program to connect to para_audiod.
  *
@@ -281,13 +325,15 @@ static char *configfile_exists(void)
  */
 int main(int argc, char *argv[])
 {
-       int ret = -E_AUDIOC_SYNTAX, fd;
+       int ret, fd;
        char *cf, *buf = NULL, *args = NULL;
        size_t bufsize;
 
-       if (audioc_cmdline_parser(argc, argv, &conf))
-               goto out;
-       HANDLE_VERSION_FLAG("audioc", conf);
+       audioc_cmdline_parser(argc, argv, &conf);
+       loglevel = get_loglevel_by_name(conf.loglevel_arg);
+       version_handle_flag("audioc", conf.version_given);
+       if (conf.help_given || conf.detailed_help_given)
+               print_help_and_die();
        cf = configfile_exists();
        if (cf) {
                struct audioc_cmdline_parser_params params = {
@@ -298,14 +344,10 @@ int main(int argc, char *argv[])
                        .print_errors = 1,
 
                };
-               ret = audioc_cmdline_parser_config_file(cf, &conf, &params);
+               audioc_cmdline_parser_config_file(cf, &conf, &params);
                free(cf);
-               if (ret) {
-                       fprintf(stderr, "parse error in config file\n");
-                       exit(EXIT_FAILURE);
-               }
+               loglevel = get_loglevel_by_name(conf.loglevel_arg);
        }
-       loglevel = get_loglevel_by_name(conf.loglevel_arg);
        if (conf.socket_given)
                socket_name = para_strdup(conf.socket_arg);
        else {
@@ -322,21 +364,16 @@ int main(int argc, char *argv[])
                interactive_session();
        args = concat_args(conf.inputs_num, conf.inputs);
 
-       ret = connect_local_socket(socket_name);
+       ret = connect_audiod(socket_name, args);
        free(socket_name);
-       if (ret < 0) {
-               PARA_EMERG_LOG("failed to connect to local socket\n");
+       if (ret < 0)
                goto out;
-       }
        fd = ret;
-       ret = send_cred_buffer(fd, args);
+       ret = mark_fd_blocking(STDOUT_FILENO);
        if (ret < 0)
                goto out;
        bufsize = conf.bufsize_arg;
        buf = para_malloc(bufsize);
-       ret = mark_fd_blocking(STDOUT_FILENO);
-       if (ret < 0)
-               goto out;
        do {
                size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
                if (ret <= 0)