audiod: Use lsu_merge_config_file_options().
[paraslash.git] / play.c
diff --git a/play.c b/play.c
index b54a46966355757a49c8a540d4b958b0696de688..233da70099b6cdc2bf441bb1bc9cce44fc8da701 100644 (file)
--- a/play.c
+++ b/play.c
@@ -1,14 +1,9 @@
-/*
- * Copyright (C) 2012 Andre Noll <maan@tuebingen.mpg.de>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 2012 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
 
 /** \file play.c Paraslash's standalone player. */
 
 #include <regex.h>
 #include <signal.h>
-#include <inttypes.h>
 #include <lopsub.h>
 
 #include "recv_cmd.lsg.h"
@@ -16,6 +11,7 @@
 #include "write_cmd.lsg.h"
 #include "play.lsg.h"
 #include "para.h"
+#include "lsu.h"
 #include "list.h"
 #include "error.h"
 #include "buffer_tree.h"
@@ -86,7 +82,7 @@ struct play_task {
        struct filter_node fn;
        struct writer_node wn;
 
-       /* See comment to enum state_change_request_type above */
+       /* See comment to enum \ref state_change_request_type above. */
        enum state_change_request_type rq;
        /* only relevant if rq == CRT_FILE_CHANGE */
        unsigned next_file;
@@ -144,7 +140,7 @@ static void handle_help_flags(void)
 
        if (OPT_GIVEN(DETAILED_HELP))
                help = lls_long_help(CMD_PTR);
-       else if (OPT_GIVEN(HELP))
+       else if (OPT_GIVEN(HELP) || lls_num_inputs(play_lpr) == 0)
                help = lls_short_help(CMD_PTR);
        else
                return;
@@ -168,7 +164,7 @@ static void parse_config_or_die(int argc, char *argv[])
                goto fail;
        loglevel = OPT_UINT32_VAL(LOGLEVEL);
        version_handle_flag("play", OPT_GIVEN(VERSION));
-       handle_help_flags();
+       handle_help_flags(); /* also handles the zero-arg case */
        if (OPT_GIVEN(CONFIG_FILE))
                cf = para_strdup(OPT_STRING_VAL(CONFIG_FILE));
        else {
@@ -183,7 +179,7 @@ static void parse_config_or_die(int argc, char *argv[])
                if (ret == -ERRNO_TO_PARA_ERROR(ENOENT) && OPT_GIVEN(CONFIG_FILE))
                        goto free_cf;
                ret = 0;
-               goto free_cf;
+               goto setup_keymap;
        }
        ret = lls(lls_convert_config(map, sz, NULL, &cf_argv, &errctx));
        para_munmap(map, sz);
@@ -201,10 +197,7 @@ static void parse_config_or_die(int argc, char *argv[])
        lls_free_parse_result(play_lpr, cmd);
        play_lpr = merged_lpr;
        loglevel = OPT_UINT32_VAL(LOGLEVEL);
-
-       ret = lls(lls_check_arg_count(play_lpr, 1, INT_MAX, &errctx));
-       if (ret < 0)
-               goto free_cf;
+setup_keymap:
        num_kmas = OPT_GIVEN(KEY_MAP);
        for (i = 0; i < num_kmas; i++) {
                const char *kma = lls_string_val(i, OPT_RESULT(KEY_MAP));
@@ -709,18 +702,22 @@ I9E_DUMMY_COMPLETER(ls);
 I9E_DUMMY_COMPLETER(info);
 I9E_DUMMY_COMPLETER(play);
 I9E_DUMMY_COMPLETER(pause);
-I9E_DUMMY_COMPLETER(stop);
 I9E_DUMMY_COMPLETER(tasks);
 I9E_DUMMY_COMPLETER(quit);
 I9E_DUMMY_COMPLETER(ff);
 
 static void help_completer(struct i9e_completion_info *ci,
-               struct i9e_completion_result *result)
+               struct i9e_completion_result *cr)
 {
-       result->matches = i9e_complete_commands(ci->word, pp_completers);
+       char *opts[] = {LSG_PLAY_CMD_HELP_OPTS, NULL};
+
+       if (ci->word[0] == '-') {
+               i9e_complete_option(opts, ci, cr);
+               return;
+       }
+       cr->matches = i9e_complete_commands(ci->word, pp_completers);
 }
 
-I9E_DUMMY_COMPLETER(SUPERCOMMAND_UNAVAILABLE);
 static struct i9e_completer pp_completers[] = {
 #define LSG_PLAY_CMD_CMD(_name) {.name = #_name, \
        .completer = _name ## _completer}
@@ -752,27 +749,15 @@ EXPORT_PLAY_CMD_HANDLER(quit);
 
 static int com_help(struct lls_parse_result *lpr)
 {
-       int i, ret;
-       char *buf, *errctx;
+       int i;
+       char *buf;
        size_t sz;
-       const struct lls_command *cmd;
+       unsigned n;
+       const struct lls_opt_result *r =
+               lls_opt_result(LSG_PLAY_CMD_HELP_OPT_LONG, lpr);
+       bool long_help = lls_opt_given(r);
 
-       ret = lls(lls_check_arg_count(lpr, 0, 1, &errctx));
-       if (ret < 0) {
-               if (errctx)
-                       PARA_ERROR_LOG("%s\n", errctx);
-               free(errctx);
-               return ret;
-       }
-       if (lls_num_inputs(lpr) == 0) {
-               if (pt->background) {
-                       for (i = 1; (cmd = lls_cmd(i, play_cmd_suite)); i++) {
-                               sz = xasprintf(&buf, "%s\t%s\n",
-                                       lls_command_name(cmd), lls_purpose(cmd));
-                               btr_add_output(buf, sz, pt->btrn);
-                       }
-                       return 0;
-               }
+       if (!pt->background) {
                FOR_EACH_MAPPED_KEY(i) {
                        bool internal = is_internal_key(i);
                        int idx = get_key_map_idx(i);
@@ -787,18 +772,8 @@ static int com_help(struct lls_parse_result *lpr)
                }
                return 0;
        }
-       ret = lls(lls_lookup_subcmd(lls_input(0, lpr), play_cmd_suite,
-               &errctx));
-       if (ret < 0) {
-               if (errctx)
-                       PARA_ERROR_LOG("%s\n", errctx);
-               free(errctx);
-               return ret;
-       }
-       cmd = lls_cmd(ret, play_cmd_suite);
-       buf = lls_long_help(cmd);
-       assert(buf);
-       btr_add_output(buf, strlen(buf), pt->btrn);
+       lsu_com_help(long_help, lpr, play_cmd_suite, NULL, &buf, &n);
+       btr_add_output(buf, n, pt->btrn);
        return 0;
 }
 EXPORT_PLAY_CMD_HANDLER(help);