crypt: Rename RSA functions.
[paraslash.git] / play.c
diff --git a/play.c b/play.c
index 108db59..78d53fa 100644 (file)
--- a/play.c
+++ b/play.c
@@ -1,20 +1,18 @@
-/*
- * 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>
 
 /** \file play.c Paraslash's standalone player. */
 
 #include <regex.h>
-#include <fnmatch.h>
 #include <signal.h>
 #include <signal.h>
+#include <lopsub.h>
 
 
+#include "recv_cmd.lsg.h"
+#include "play_cmd.lsg.h"
+#include "write_cmd.lsg.h"
+#include "play.lsg.h"
 #include "para.h"
 #include "list.h"
 #include "para.h"
 #include "list.h"
-#include "play.cmdline.h"
 #include "error.h"
 #include "error.h"
-#include "ggo.h"
 #include "buffer_tree.h"
 #include "version.h"
 #include "string.h"
 #include "buffer_tree.h"
 #include "version.h"
 #include "string.h"
@@ -23,7 +21,6 @@
 #include "afh.h"
 #include "recv.h"
 #include "write.h"
 #include "afh.h"
 #include "recv.h"
 #include "write.h"
-#include "write_common.h"
 #include "fd.h"
 
 /**
 #include "fd.h"
 
 /**
  * Playlist handling is done exclusively in play context.
  */
 
  * Playlist handling is done exclusively in play context.
  */
 
+/** Array of error strings. */
+DEFINE_PARA_ERRLIST;
+
+static struct lls_parse_result *play_lpr;
+
+#define CMD_PTR (lls_cmd(0, play_suite))
+#define OPT_RESULT(_name) \
+       (lls_opt_result(LSG_PLAY_PARA_PLAY_OPT_ ## _name, play_lpr))
+#define OPT_GIVEN(_name) (lls_opt_given(OPT_RESULT(_name)))
+#define OPT_UINT32_VAL(_name) (lls_uint32_val(0, OPT_RESULT(_name)))
+#define OPT_STRING_VAL(_name) (lls_string_val(0, OPT_RESULT(_name)))
+
 /**
  * Describes a request to change the state of para_play.
  *
 /**
  * Describes a request to change the state of para_play.
  *
@@ -72,7 +81,7 @@ struct play_task {
        struct filter_node fn;
        struct writer_node wn;
 
        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;
        enum state_change_request_type rq;
        /* only relevant if rq == CRT_FILE_CHANGE */
        unsigned next_file;
@@ -95,16 +104,14 @@ struct play_task {
        char *afhi_txt;
 };
 
        char *afhi_txt;
 };
 
-/** Initialize the array of errors for para_play. */
-INIT_PLAY_ERRLISTS;
-
-/* Activate the afh receiver. */
-extern void afh_recv_init(struct receiver *r);
-#undef AFH_RECEIVER
-/** Initialization code for a receiver struct. */
-#define AFH_RECEIVER {.name = "afh", .init = afh_recv_init},
-/** This expands to the array of all receivers. */
-DEFINE_RECEIVER_ARRAY;
+typedef int (*play_cmd_handler_t)(struct lls_parse_result *lpr);
+struct play_command_info {
+       play_cmd_handler_t handler;
+};
+#define EXPORT_PLAY_CMD_HANDLER(_cmd) \
+       const struct play_command_info lsg_play_cmd_com_ ## _cmd ## _user_data = { \
+               .handler = com_ ## _cmd \
+       };
 
 static int loglevel = LL_WARNING;
 
 
 static int loglevel = LL_WARNING;
 
@@ -113,90 +120,106 @@ INIT_STDERR_LOGGING(loglevel);
 
 char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
 
 
 char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
 
-/** Iterate over all files in the playlist. */
-#define FOR_EACH_PLAYLIST_FILE(i) for (i = 0; i < conf.inputs_num; i++)
-static struct play_args_info conf;
-
 static struct sched sched = {.max_fileno = 0};
 static struct sched sched = {.max_fileno = 0};
-static struct play_task play_task;
-static struct receiver *afh_recv;
+static struct play_task play_task, *pt = &play_task;
 
 
-static void check_afh_receiver_or_die(void)
-{
-       int i;
+#define AFH_RECV_CMD (lls_cmd(LSG_RECV_CMD_CMD_AFH, recv_cmd_suite))
+#define AFH_RECV ((struct receiver *)lls_user_data(AFH_RECV_CMD))
 
 
-       FOR_EACH_RECEIVER(i) {
-               struct receiver *r = receivers + i;
-               if (strcmp(r->name, "afh"))
-                       continue;
-               afh_recv = r;
-               return;
-       }
-       PARA_EMERG_LOG("fatal: afh receiver not found\n");
-       exit(EXIT_FAILURE);
+static unsigned *shuffle_map;
+
+static const char *get_playlist_file(unsigned idx)
+{
+       return lls_input(shuffle_map[idx], play_lpr);
 }
 
 }
 
-__noreturn static void print_help_and_die(void)
+static void handle_help_flags(void)
 {
 {
-       struct ggo_help help = DEFINE_GGO_HELP(play);
-       unsigned flags = conf.detailed_help_given?
-               GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS;
+       char *help;
 
 
-       ggo_print_help(&help, flags);
-       printf("supported audio formats: %s\n", AUDIO_FORMAT_HANDLERS);
-       exit(0);
+       if (OPT_GIVEN(DETAILED_HELP))
+               help = lls_long_help(CMD_PTR);
+       else if (OPT_GIVEN(HELP) || lls_num_inputs(play_lpr) == 0)
+               help = lls_short_help(CMD_PTR);
+       else
+               return;
+       printf("%s\n", help);
+       free(help);
+       exit(EXIT_SUCCESS);
 }
 
 static void parse_config_or_die(int argc, char *argv[])
 {
 }
 
 static void parse_config_or_die(int argc, char *argv[])
 {
-       int i, ret;
-       char *config_file;
-       struct play_cmdline_parser_params params = {
-               .override = 0,
-               .initialize = 1,
-               .check_required = 0,
-               .check_ambiguity = 0,
-               .print_errors = 1
-       };
+       const struct lls_command *cmd = CMD_PTR;
+       int i, ret, cf_argc;
+       char *cf, *errctx, **cf_argv;
+       struct lls_parse_result *cf_lpr, *merged_lpr;
+       unsigned num_kmas;
+       void *map;
+       size_t sz;
 
 
-       play_cmdline_parser_ext(argc, argv, &conf, &params);
-       loglevel = get_loglevel_by_name(conf.loglevel_arg);
-       version_handle_flag("play", conf.version_given);
-       if (conf.help_given || conf.detailed_help_given)
-               print_help_and_die();
-       if (conf.config_file_given)
-               config_file = para_strdup(conf.config_file_arg);
+       ret = lls(lls_parse(argc, argv, cmd, &play_lpr, &errctx));
+       if (ret < 0)
+               goto fail;
+       loglevel = OPT_UINT32_VAL(LOGLEVEL);
+       version_handle_flag("play", OPT_GIVEN(VERSION));
+       handle_help_flags(); /* also handles the zero-arg case */
+       if (OPT_GIVEN(CONFIG_FILE))
+               cf = para_strdup(OPT_STRING_VAL(CONFIG_FILE));
        else {
                char *home = para_homedir();
        else {
                char *home = para_homedir();
-               config_file = make_message("%s/.paraslash/play.conf", home);
+               cf = make_message("%s/.paraslash/play.conf", home);
                free(home);
        }
                free(home);
        }
-       ret = file_exists(config_file);
-       if (conf.config_file_given && !ret) {
-               PARA_EMERG_LOG("can not read config file %s\n", config_file);
-               goto err;
-       }
-       if (ret) {
-               params.initialize = 0;
-               params.check_required = 1;
-               play_cmdline_parser_config_file(config_file, &conf, &params);
-               loglevel = get_loglevel_by_name(conf.loglevel_arg);
+       ret = mmap_full_file(cf, O_RDONLY, &map, &sz, NULL);
+       if (ret < 0) {
+               if (ret != -E_EMPTY && ret != -ERRNO_TO_PARA_ERROR(ENOENT))
+                       goto free_cf;
+               if (ret == -ERRNO_TO_PARA_ERROR(ENOENT) && OPT_GIVEN(CONFIG_FILE))
+                       goto free_cf;
+               ret = 0;
+               goto setup_keymap;
        }
        }
-       for (i = 0; i < conf.key_map_given; i++) {
-               char *kma = conf.key_map_arg[i];
+       ret = lls(lls_convert_config(map, sz, NULL, &cf_argv, &errctx));
+       para_munmap(map, sz);
+       if (ret < 0)
+               goto free_cf;
+       cf_argc = ret;
+       ret = lls(lls_parse(cf_argc, cf_argv, cmd, &cf_lpr, &errctx));
+       lls_free_argv(cf_argv);
+       if (ret < 0)
+               goto free_cf;
+       ret = lls(lls_merge(play_lpr, cf_lpr, cmd, &merged_lpr, &errctx));
+       lls_free_parse_result(cf_lpr, cmd);
+       if (ret < 0)
+               goto free_cf;
+       lls_free_parse_result(play_lpr, cmd);
+       play_lpr = merged_lpr;
+       loglevel = OPT_UINT32_VAL(LOGLEVEL);
+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));
                if (*kma && strchr(kma + 1, ':'))
                        continue;
                PARA_EMERG_LOG("invalid key map arg: %s\n", kma);
                if (*kma && strchr(kma + 1, ':'))
                        continue;
                PARA_EMERG_LOG("invalid key map arg: %s\n", kma);
-               goto err;
+               goto free_cf;
        }
        }
-       free(config_file);
-       return;
-err:
-       free(config_file);
+       ret = 1;
+free_cf:
+       free(cf);
+       if (ret >= 0)
+               return;
+       lls_free_parse_result(play_lpr, cmd);
+fail:
+       if (errctx)
+               PARA_EMERG_LOG("%s\n", errctx);
+       free(errctx);
+       PARA_EMERG_LOG("%s\n", para_strerror(-ret));
        exit(EXIT_FAILURE);
 }
 
        exit(EXIT_FAILURE);
 }
 
-static char get_playback_state(struct play_task *pt)
+static char get_playback_state(void)
 {
        switch (pt->rq) {
        case CRT_NONE: return pt->playing? 'P' : 'U';
 {
        switch (pt->rq) {
        case CRT_NONE: return pt->playing? 'P' : 'U';
@@ -207,9 +230,9 @@ static char get_playback_state(struct play_task *pt)
        assert(false);
 };
 
        assert(false);
 };
 
-static long unsigned get_play_time(struct play_task *pt)
+static long unsigned get_play_time(void)
 {
 {
-       char state = get_playback_state(pt);
+       char state = get_playback_state();
        long unsigned result;
 
        if (state != 'P' && state != 'U')
        long unsigned result;
 
        if (state != 'P' && state != 'U')
@@ -230,17 +253,18 @@ static long unsigned get_play_time(struct play_task *pt)
        return result;
 }
 
        return result;
 }
 
-static void wipe_receiver_node(struct play_task *pt)
+
+static void wipe_receiver_node(void)
 {
        PARA_NOTICE_LOG("cleaning up receiver node\n");
        btr_remove_node(&pt->rn.btrn);
 {
        PARA_NOTICE_LOG("cleaning up receiver node\n");
        btr_remove_node(&pt->rn.btrn);
-       afh_recv->close(&pt->rn);
-       afh_recv->free_config(pt->rn.conf);
+       AFH_RECV->close(&pt->rn);
+       lls_free_parse_result(pt->rn.lpr, AFH_RECV_CMD);
        memset(&pt->rn, 0, sizeof(struct receiver_node));
 }
 
 /* returns: 0 not eof, 1: eof, < 0: fatal error.  */
        memset(&pt->rn, 0, sizeof(struct receiver_node));
 }
 
 /* returns: 0 not eof, 1: eof, < 0: fatal error.  */
-static int get_playback_error(struct play_task *pt)
+static int get_playback_error(void)
 {
        int err;
 
 {
        int err;
 
@@ -259,22 +283,23 @@ static int get_playback_error(struct play_task *pt)
        return err;
 }
 
        return err;
 }
 
-static int eof_cleanup(struct play_task *pt)
+static int eof_cleanup(void)
 {
 {
-       struct writer *w = writers + DEFAULT_WRITER;
-       const struct filter *decoder = filter_get(pt->fn.filter_num);
+       const struct filter *decoder;
+       const struct writer *w = writer_get(-1); /* default writer */
        int ret;
 
        int ret;
 
-       ret = get_playback_error(pt);
+       ret = get_playback_error();
        if (ret == 0)
                return ret;
        PARA_NOTICE_LOG("cleaning up wn/fn nodes\n");
        task_reap(&pt->wn.task);
        w->close(&pt->wn);
        btr_remove_node(&pt->wn.btrn);
        if (ret == 0)
                return ret;
        PARA_NOTICE_LOG("cleaning up wn/fn nodes\n");
        task_reap(&pt->wn.task);
        w->close(&pt->wn);
        btr_remove_node(&pt->wn.btrn);
-       w->free_config(pt->wn.conf);
+       lls_free_parse_result(pt->wn.lpr, WRITE_CMD(pt->wn.wid));
        memset(&pt->wn, 0, sizeof(struct writer_node));
 
        memset(&pt->wn, 0, sizeof(struct writer_node));
 
+       decoder = filter_get(pt->fn.filter_num);
        task_reap(&pt->fn.task);
        if (decoder->close)
                decoder->close(&pt->fn);
        task_reap(&pt->fn.task);
        if (decoder->close)
                decoder->close(&pt->fn);
@@ -290,7 +315,7 @@ static int eof_cleanup(struct play_task *pt)
         * paused.
         */
        if (ret < 0)
         * paused.
         */
        if (ret < 0)
-               wipe_receiver_node(pt);
+               wipe_receiver_node();
        return ret;
 }
 
        return ret;
 }
 
@@ -299,34 +324,42 @@ static int shuffle_compare(__a_unused const void *a, __a_unused const void *b)
        return para_random(100) - 50;
 }
 
        return para_random(100) - 50;
 }
 
-static void shuffle(char **base, size_t num)
+static void init_shuffle_map(void)
 {
 {
+       unsigned n, num_inputs = lls_num_inputs(play_lpr);
+       shuffle_map = para_malloc(num_inputs * sizeof(unsigned));
+       for (n = 0; n < num_inputs; n++)
+               shuffle_map[n] = n;
+       if (!OPT_GIVEN(RANDOMIZE))
+               return;
        srandom(time(NULL));
        srandom(time(NULL));
-       qsort(base, num, sizeof(char *), shuffle_compare);
+       qsort(shuffle_map, num_inputs, sizeof(unsigned), shuffle_compare);
 }
 
 static struct btr_node *new_recv_btrn(struct receiver_node *rn)
 {
        return btr_new_node(&(struct btr_node_description)
 }
 
 static struct btr_node *new_recv_btrn(struct receiver_node *rn)
 {
        return btr_new_node(&(struct btr_node_description)
-               EMBRACE(.name = afh_recv->name, .context = rn,
-                       .handler = afh_recv->execute));
+               EMBRACE(.name = lls_command_name(AFH_RECV_CMD), .context = rn,
+                       .handler = AFH_RECV->execute));
 }
 
 }
 
-static int open_new_file(struct play_task *pt)
+static int open_new_file(void)
 {
        int ret;
 {
        int ret;
-       char *tmp, *path = conf.inputs[pt->next_file], *afh_recv_conf[] =
-               {"play", "-f", path, "-b", "0", NULL};
+       const char *path = get_playlist_file(pt->next_file);
+       char *tmp = para_strdup(path), *errctx;
+       char *argv[] = {"play", "-f", tmp, "-b", "0", NULL};
 
        PARA_NOTICE_LOG("next file: %s\n", path);
 
        PARA_NOTICE_LOG("next file: %s\n", path);
-       wipe_receiver_node(pt);
+       wipe_receiver_node();
        pt->start_chunk = 0;
        pt->rn.btrn = new_recv_btrn(&pt->rn);
        pt->start_chunk = 0;
        pt->rn.btrn = new_recv_btrn(&pt->rn);
-       pt->rn.conf = afh_recv->parse_config(ARRAY_SIZE(afh_recv_conf) - 1,
-               afh_recv_conf);
-       assert(pt->rn.conf);
-       pt->rn.receiver = afh_recv;
-       ret = afh_recv->open(&pt->rn);
+       ret = lls(lls_parse(ARRAY_SIZE(argv) - 1, argv, AFH_RECV_CMD,
+               &pt->rn.lpr, &errctx));
+       free(tmp);
+       assert(ret >= 0);
+       pt->rn.receiver = AFH_RECV;
+       ret = AFH_RECV->open(&pt->rn);
        if (ret < 0) {
                PARA_ERROR_LOG("could not open %s\n", path);
                goto fail;
        if (ret < 0) {
                PARA_ERROR_LOG("could not open %s\n", path);
                goto fail;
@@ -358,20 +391,21 @@ static int open_new_file(struct play_task *pt)
        }
        return 1;
 fail:
        }
        return 1;
 fail:
-       wipe_receiver_node(pt);
+       wipe_receiver_node();
        return ret;
 }
 
        return ret;
 }
 
-static int load_file(struct play_task *pt)
+static int load_file(void)
 {
        const char *af;
        char *tmp, buf[20];
        int ret;
        const struct filter *decoder;
 {
        const char *af;
        char *tmp, buf[20];
        int ret;
        const struct filter *decoder;
+       static struct lls_parse_result *filter_lpr, *writer_lpr;
 
        btr_remove_node(&pt->rn.btrn);
        if (!pt->rn.receiver || pt->next_file != pt->current_file) {
 
        btr_remove_node(&pt->rn.btrn);
        if (!pt->rn.receiver || pt->next_file != pt->current_file) {
-               ret = open_new_file(pt);
+               ret = open_new_file();
                if (ret < 0)
                        return ret;
        } else {
                if (ret < 0)
                        return ret;
        } else {
@@ -387,30 +421,31 @@ static int load_file(struct play_task *pt)
        /* set up decoding filter */
        af = audio_format_name(pt->audio_format_num);
        tmp = make_message("%sdec", af);
        /* set up decoding filter */
        af = audio_format_name(pt->audio_format_num);
        tmp = make_message("%sdec", af);
-       PARA_INFO_LOG("decoder: %s\n", tmp);
-       ret = check_filter_arg(tmp, &pt->fn.conf);
+       ret = filter_setup(tmp, &pt->fn.conf, &filter_lpr);
        freep(&tmp);
        if (ret < 0)
                goto fail;
        pt->fn.filter_num = ret;
        freep(&tmp);
        if (ret < 0)
                goto fail;
        pt->fn.filter_num = ret;
+       pt->fn.lpr = filter_lpr;
        decoder = filter_get(ret);
        pt->fn.btrn = btr_new_node(&(struct btr_node_description)
        decoder = filter_get(ret);
        pt->fn.btrn = btr_new_node(&(struct btr_node_description)
-               EMBRACE(.name = decoder->name, .parent = pt->rn.btrn,
-                       .handler = decoder->execute, .context = &pt->fn));
+               EMBRACE(.name = filter_name(pt->fn.filter_num),
+                       .parent = pt->rn.btrn, .handler = decoder->execute,
+                       .context = &pt->fn));
        if (decoder->open)
                decoder->open(&pt->fn);
        PARA_INFO_LOG("buffer tree:\n");
        btr_log_tree(pt->rn.btrn, LL_INFO);
 
        /* setup default writer */
        if (decoder->open)
                decoder->open(&pt->fn);
        PARA_INFO_LOG("buffer tree:\n");
        btr_log_tree(pt->rn.btrn, LL_INFO);
 
        /* setup default writer */
-       pt->wn.conf = check_writer_arg_or_die(NULL, &pt->wn.writer_num);
-
+       pt->wn.wid = check_writer_arg_or_die(NULL, &writer_lpr);
+       pt->wn.lpr = writer_lpr;
        /* success, register tasks */
        pt->rn.task = task_register(
                &(struct task_info) {
        /* success, register tasks */
        pt->rn.task = task_register(
                &(struct task_info) {
-                       .name = afh_recv->name,
-                       .pre_select = afh_recv->pre_select,
-                       .post_select = afh_recv->post_select,
+                       .name = lls_command_name(AFH_RECV_CMD),
+                       .pre_select = AFH_RECV->pre_select,
+                       .post_select = AFH_RECV->post_select,
                        .context = &pt->rn
                }, &sched);
        sprintf(buf, "%s decoder", af);
                        .context = &pt->rn
                }, &sched);
        sprintf(buf, "%s decoder", af);
@@ -424,36 +459,37 @@ static int load_file(struct play_task *pt)
        register_writer_node(&pt->wn, pt->fn.btrn, &sched);
        return 1;
 fail:
        register_writer_node(&pt->wn, pt->fn.btrn, &sched);
        return 1;
 fail:
-       wipe_receiver_node(pt);
+       wipe_receiver_node();
        return ret;
 }
 
        return ret;
 }
 
-static int next_valid_file(struct play_task *pt)
+static int next_valid_file(void)
 {
        int i, j = pt->current_file;
 {
        int i, j = pt->current_file;
+       unsigned num_inputs = lls_num_inputs(play_lpr);
 
 
-       FOR_EACH_PLAYLIST_FILE(i) {
-               j = (j + 1) % conf.inputs_num;
+       for (i = 0; i < num_inputs; i++) {
+               j = (j + 1) % num_inputs;
                if (!pt->invalid[j])
                        return j;
        }
        return -E_NO_VALID_FILES;
 }
 
                if (!pt->invalid[j])
                        return j;
        }
        return -E_NO_VALID_FILES;
 }
 
-static int load_next_file(struct play_task *pt)
+static int load_next_file(void)
 {
        int ret;
 
 again:
        if (pt->rq == CRT_NONE) {
                pt->start_chunk = 0;
 {
        int ret;
 
 again:
        if (pt->rq == CRT_NONE) {
                pt->start_chunk = 0;
-               ret = next_valid_file(pt);
+               ret = next_valid_file();
                if (ret < 0)
                        return ret;
                pt->next_file = ret;
        } else if (pt->rq == CRT_REPOS)
                pt->next_file = pt->current_file;
                if (ret < 0)
                        return ret;
                pt->next_file = ret;
        } else if (pt->rq == CRT_REPOS)
                pt->next_file = pt->current_file;
-       ret = load_file(pt);
+       ret = load_file();
        if (ret < 0) {
                PARA_ERROR_LOG("%s: marking file as invalid\n",
                        para_strerror(-ret));
        if (ret < 0) {
                PARA_ERROR_LOG("%s: marking file as invalid\n",
                        para_strerror(-ret));
@@ -466,7 +502,7 @@ again:
        return ret;
 }
 
        return ret;
 }
 
-static void kill_stream(struct play_task *pt)
+static void kill_stream(void)
 {
        if (pt->wn.task)
                task_notify(pt->wn.task, E_EOF);
 {
        if (pt->wn.task)
                task_notify(pt->wn.task, E_EOF);
@@ -475,14 +511,15 @@ static void kill_stream(struct play_task *pt)
 #ifdef HAVE_READLINE
 
 /* only called from com_prev(), nec. only if we have readline */
 #ifdef HAVE_READLINE
 
 /* only called from com_prev(), nec. only if we have readline */
-static int previous_valid_file(struct play_task *pt)
+static int previous_valid_file(void)
 {
        int i, j = pt->current_file;
 {
        int i, j = pt->current_file;
+       unsigned num_inputs = lls_num_inputs(play_lpr);
 
 
-       FOR_EACH_PLAYLIST_FILE(i) {
+       for (i = 0; i < num_inputs; i++) {
                j--;
                if (j < 0)
                j--;
                if (j < 0)
-                       j = conf.inputs_num - 1;
+                       j = num_inputs - 1;
                if (!pt->invalid[j])
                        return j;
        }
                if (!pt->invalid[j])
                        return j;
        }
@@ -533,7 +570,7 @@ static const char *default_keyseqs[] = {INTERNAL_KEYMAP_ENTRIES};
 static const char *default_commands[] = {INTERNAL_KEYMAP_ENTRIES};
 #undef KEYMAP_ENTRY
 #define NUM_INTERNALLY_MAPPED_KEYS ARRAY_SIZE(default_commands)
 static const char *default_commands[] = {INTERNAL_KEYMAP_ENTRIES};
 #undef KEYMAP_ENTRY
 #define NUM_INTERNALLY_MAPPED_KEYS ARRAY_SIZE(default_commands)
-#define NUM_MAPPED_KEYS (NUM_INTERNALLY_MAPPED_KEYS + conf.key_map_given)
+#define NUM_MAPPED_KEYS (NUM_INTERNALLY_MAPPED_KEYS + OPT_GIVEN(KEY_MAP))
 #define FOR_EACH_MAPPED_KEY(i) for (i = 0; i < NUM_MAPPED_KEYS; i++)
 
 static inline bool is_internal_key(int key)
 #define FOR_EACH_MAPPED_KEY(i) for (i = 0; i < NUM_MAPPED_KEYS; i++)
 
 static inline bool is_internal_key(int key)
@@ -564,9 +601,9 @@ static inline int get_key_map_idx(int key)
                get_internal_key_map_idx(key) : get_user_key_map_idx(key);
 }
 
                get_internal_key_map_idx(key) : get_user_key_map_idx(key);
 }
 
-static inline char *get_user_key_map_arg(int key)
+static inline const char *get_user_key_map_arg(int key)
 {
 {
-       return conf.key_map_arg[get_user_key_map_idx(key)];
+       return lls_string_val(get_user_key_map_idx(key), OPT_RESULT(KEY_MAP));
 }
 
 static inline char *get_internal_key_map_seq(int key)
 }
 
 static inline char *get_internal_key_map_seq(int key)
@@ -653,24 +690,6 @@ static char **get_mapped_keyseqs(void)
        return result;
 }
 
        return result;
 }
 
-#include "play.command_list.h"
-
-typedef int play_command_handler_t(struct play_task *, int, char**);
-static play_command_handler_t PLAY_COMMAND_HANDLERS;
-
-/* defines one command of para_play */
-struct pp_command {
-       const char *name;
-       play_command_handler_t *handler;
-       const char *description;
-       const char *usage;
-       const char *help;
-};
-
-static struct pp_command pp_cmds[] = {DEFINE_PLAY_CMD_ARRAY};
-#define FOR_EACH_COMMAND(c) for (c = 0; pp_cmds[c].name; c++)
-
-#include "play.completion.h"
 static struct i9e_completer pp_completers[];
 
 I9E_DUMMY_COMPLETER(jmp);
 static struct i9e_completer pp_completers[];
 
 I9E_DUMMY_COMPLETER(jmp);
@@ -682,7 +701,6 @@ I9E_DUMMY_COMPLETER(ls);
 I9E_DUMMY_COMPLETER(info);
 I9E_DUMMY_COMPLETER(play);
 I9E_DUMMY_COMPLETER(pause);
 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);
 I9E_DUMMY_COMPLETER(tasks);
 I9E_DUMMY_COMPLETER(quit);
 I9E_DUMMY_COMPLETER(ff);
@@ -693,9 +711,15 @@ static void help_completer(struct i9e_completion_info *ci,
        result->matches = i9e_complete_commands(ci->word, pp_completers);
 }
 
        result->matches = i9e_complete_commands(ci->word, pp_completers);
 }
 
-static struct i9e_completer pp_completers[] = {PLAY_COMPLETERS {.name = NULL}};
+static struct i9e_completer pp_completers[] = {
+#define LSG_PLAY_CMD_CMD(_name) {.name = #_name, \
+       .completer = _name ## _completer}
+       LSG_PLAY_CMD_SUBCOMMANDS
+#undef LSG_PLAY_CMD_CMD
+       {.name = NULL}
+};
 
 
-static void attach_stdout(struct play_task *pt, const char *name)
+static void attach_stdout(const char *name)
 {
        if (pt->btrn)
                return;
 {
        if (pt->btrn)
                return;
@@ -704,141 +728,137 @@ static void attach_stdout(struct play_task *pt, const char *name)
        i9e_attach_to_stdout(pt->btrn);
 }
 
        i9e_attach_to_stdout(pt->btrn);
 }
 
-static void detach_stdout(struct play_task *pt)
+static void detach_stdout(void)
 {
        btr_remove_node(&pt->btrn);
 }
 
 {
        btr_remove_node(&pt->btrn);
 }
 
-static int com_quit(struct play_task *pt, int argc, __a_unused char **argv)
+static int com_quit(__a_unused struct lls_parse_result *lpr)
 {
 {
-       if (argc != 1)
-               return -E_PLAY_SYNTAX;
        pt->rq = CRT_TERM_RQ;
        return 0;
 }
        pt->rq = CRT_TERM_RQ;
        return 0;
 }
+EXPORT_PLAY_CMD_HANDLER(quit);
 
 
-static int com_help(struct play_task *pt, int argc, char **argv)
+static int com_help(struct lls_parse_result *lpr)
 {
 {
-       int i;
-       char *buf;
+       int i, ret;
+       char *buf, *errctx;
        size_t sz;
        size_t sz;
+       const struct lls_command *cmd;
 
 
-       if (argc > 2)
-               return -E_PLAY_SYNTAX;
-       if (argc < 2) {
-               if (pt->background)
-                       FOR_EACH_COMMAND(i) {
-                               sz = xasprintf(&buf, "%s\t%s\n", pp_cmds[i].name,
-                                       pp_cmds[i].description);
-                               btr_add_output(buf, sz, pt->btrn);
-                       }
-               else {
-                       FOR_EACH_MAPPED_KEY(i) {
-                               bool internal = is_internal_key(i);
-                               int idx = get_key_map_idx(i);
-                               char *seq = get_key_map_seq_safe(i);
-                               char *cmd = get_key_map_cmd(i);
-                               sz = xasprintf(&buf,
-                                       "%s key #%d: %s -> %s\n",
-                                       internal? "internal" : "user-defined",
-                                       idx, seq, cmd);
+       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);
                                btr_add_output(buf, sz, pt->btrn);
-                               free(seq);
-                               free(cmd);
                        }
                        }
+                       return 0;
+               }
+               FOR_EACH_MAPPED_KEY(i) {
+                       bool internal = is_internal_key(i);
+                       int idx = get_key_map_idx(i);
+                       char *seq = get_key_map_seq_safe(i);
+                       char *kmc = get_key_map_cmd(i);
+                       sz = xasprintf(&buf, "%s key #%d: %s -> %s\n",
+                               internal? "internal" : "user-defined",
+                               idx, seq, kmc);
+                       btr_add_output(buf, sz, pt->btrn);
+                       free(seq);
+                       free(kmc);
                }
                return 0;
        }
                }
                return 0;
        }
-       FOR_EACH_COMMAND(i) {
-               if (strcmp(pp_cmds[i].name, argv[1]))
-                       continue;
-               sz = xasprintf(&buf,
-                       "NAME\n\t%s -- %s\n"
-                       "SYNOPSIS\n\t%s\n"
-                       "DESCRIPTION\n%s\n",
-                       argv[1],
-                       pp_cmds[i].description,
-                       pp_cmds[i].usage,
-                       pp_cmds[i].help
-               );
-               btr_add_output(buf, sz, pt->btrn);
-               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;
        }
        }
-       return -E_BAD_PLAY_CMD;
+       cmd = lls_cmd(ret, play_cmd_suite);
+       buf = lls_long_help(cmd);
+       assert(buf);
+       btr_add_output(buf, strlen(buf), pt->btrn);
+       return 0;
 }
 }
+EXPORT_PLAY_CMD_HANDLER(help);
 
 
-static int com_info(struct play_task *pt, int argc, __a_unused char **argv)
+static int com_info(__a_unused struct lls_parse_result *lpr)
 {
        char *buf;
        size_t sz;
        static char dflt[] = "[no information available]";
 
 {
        char *buf;
        size_t sz;
        static char dflt[] = "[no information available]";
 
-       if (argc != 1)
-               return -E_PLAY_SYNTAX;
        sz = xasprintf(&buf, "playlist_pos: %u\npath: %s\n",
        sz = xasprintf(&buf, "playlist_pos: %u\npath: %s\n",
-               pt->current_file, conf.inputs[pt->current_file]);
+               pt->current_file, get_playlist_file(pt->current_file));
        btr_add_output(buf, sz, pt->btrn);
        buf = pt->afhi_txt? pt->afhi_txt : dflt;
        btr_add_output_dont_free(buf, strlen(buf), pt->btrn);
        return 0;
 }
        btr_add_output(buf, sz, pt->btrn);
        buf = pt->afhi_txt? pt->afhi_txt : dflt;
        btr_add_output_dont_free(buf, strlen(buf), pt->btrn);
        return 0;
 }
+EXPORT_PLAY_CMD_HANDLER(info);
 
 
-static void list_file(struct play_task *pt, int num)
+static void list_file(int num)
 {
        char *buf;
        size_t sz;
 
 {
        char *buf;
        size_t sz;
 
-       sz = xasprintf(&buf, "%s %4u %s\n", num == pt->current_file?
-               "*" : " ", num, conf.inputs[num]);
+       sz = xasprintf(&buf, "%s %4d %s\n", num == pt->current_file?
+               "*" : " ", num, get_playlist_file(num));
        btr_add_output(buf, sz, pt->btrn);
 }
 
        btr_add_output(buf, sz, pt->btrn);
 }
 
-static int com_tasks(struct play_task *pt, int argc, __a_unused char **argv)
+static int com_tasks(__a_unused struct lls_parse_result *lpr)
 {
        static char state;
        char *buf;
        size_t sz;
 
 {
        static char state;
        char *buf;
        size_t sz;
 
-       if (argc != 1)
-               return -E_PLAY_SYNTAX;
-
        buf = get_task_list(&sched);
        btr_add_output(buf, strlen(buf), pt->btrn);
        buf = get_task_list(&sched);
        btr_add_output(buf, strlen(buf), pt->btrn);
-       state = get_playback_state(pt);
+       state = get_playback_state();
        sz = xasprintf(&buf, "state: %c\n", state);
        btr_add_output(buf, sz, pt->btrn);
        return 0;
 }
        sz = xasprintf(&buf, "state: %c\n", state);
        btr_add_output(buf, sz, pt->btrn);
        return 0;
 }
+EXPORT_PLAY_CMD_HANDLER(tasks);
 
 
-static int com_ls(struct play_task *pt, int argc, char **argv)
+static int com_ls(__a_unused struct lls_parse_result *lpr)
 {
 {
-       int i, j, ret;
+       int i;
+       unsigned num_inputs = lls_num_inputs(play_lpr);
 
 
-       if (argc == 1) {
-               FOR_EACH_PLAYLIST_FILE(i)
-                       list_file(pt, i);
-               return 0;
-       }
-       for (j = 1; j < argc; j++) {
-               FOR_EACH_PLAYLIST_FILE(i) {
-                       ret = fnmatch(argv[j], conf.inputs[i], 0);
-                       if (ret == 0) /* match */
-                               list_file(pt, i);
-               }
-       }
+       for (i = 0; i < num_inputs; i++)
+               list_file(i);
        return 0;
 }
        return 0;
 }
+EXPORT_PLAY_CMD_HANDLER(ls);
 
 
-static int com_play(struct play_task *pt, int argc, char **argv)
+static int com_play(struct lls_parse_result *lpr)
 {
        int32_t x;
        int ret;
 {
        int32_t x;
        int ret;
-       char state;
+       char state, *errctx;
 
 
-       if (argc > 2)
-               return -E_PLAY_SYNTAX;
-       state = get_playback_state(pt);
-       if (argc == 1) {
+       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;
+       }
+       state = get_playback_state();
+       if (lls_num_inputs(lpr) == 0) {
                if (state == 'P')
                        return 0;
                pt->next_file = pt->current_file;
                if (state == 'P')
                        return 0;
                pt->next_file = pt->current_file;
@@ -846,29 +866,28 @@ static int com_play(struct play_task *pt, int argc, char **argv)
                pt->playing = true;
                return 0;
        }
                pt->playing = true;
                return 0;
        }
-       ret = para_atoi32(argv[1], &x);
+       ret = para_atoi32(lls_input(0, lpr), &x);
        if (ret < 0)
                return ret;
        if (ret < 0)
                return ret;
-       if (x < 0 || x >= conf.inputs_num)
+       if (x < 0 || x >= lls_num_inputs(play_lpr))
                return -ERRNO_TO_PARA_ERROR(EINVAL);
                return -ERRNO_TO_PARA_ERROR(EINVAL);
-       kill_stream(pt);
+       kill_stream();
        pt->next_file = x;
        pt->rq = CRT_FILE_CHANGE;
        return 0;
 }
        pt->next_file = x;
        pt->rq = CRT_FILE_CHANGE;
        return 0;
 }
+EXPORT_PLAY_CMD_HANDLER(play);
 
 
-static int com_pause(struct play_task *pt, int argc, __a_unused char **argv)
+static int com_pause(__a_unused struct lls_parse_result *lpr)
 {
        char state;
        long unsigned seconds, ss;
 
 {
        char state;
        long unsigned seconds, ss;
 
-       if (argc != 1)
-               return -E_PLAY_SYNTAX;
-       state = get_playback_state(pt);
+       state = get_playback_state();
        pt->playing = false;
        if (state != 'P')
                return 0;
        pt->playing = false;
        if (state != 'P')
                return 0;
-       seconds = get_play_time(pt);
+       seconds = get_play_time();
        pt->playing = false;
        ss = 0;
        if (pt->seconds > 0)
        pt->playing = false;
        ss = 0;
        if (pt->seconds > 0)
@@ -876,96 +895,105 @@ static int com_pause(struct play_task *pt, int argc, __a_unused char **argv)
        ss = PARA_MAX(ss, 0UL);
        ss = PARA_MIN(ss, pt->num_chunks);
        pt->start_chunk = ss;
        ss = PARA_MAX(ss, 0UL);
        ss = PARA_MIN(ss, pt->num_chunks);
        pt->start_chunk = ss;
-       kill_stream(pt);
+       kill_stream();
        return 0;
 }
        return 0;
 }
+EXPORT_PLAY_CMD_HANDLER(pause);
 
 
-static int com_prev(struct play_task *pt, int argc, __a_unused char **argv)
-
+static int com_prev(__a_unused struct lls_parse_result *lpr)
 {
        int ret;
 
 {
        int ret;
 
-       if (argc != 1)
-               return -E_PLAY_SYNTAX;
-       ret = previous_valid_file(pt);
+       ret = previous_valid_file();
        if (ret < 0)
                return ret;
        if (ret < 0)
                return ret;
-       kill_stream(pt);
+       kill_stream();
        pt->next_file = ret;
        pt->rq = CRT_FILE_CHANGE;
        pt->start_chunk = 0;
        return 0;
 }
        pt->next_file = ret;
        pt->rq = CRT_FILE_CHANGE;
        pt->start_chunk = 0;
        return 0;
 }
+EXPORT_PLAY_CMD_HANDLER(prev);
 
 
-static int com_next(struct play_task *pt, int argc, __a_unused char **argv)
+static int com_next(__a_unused struct lls_parse_result *lpr)
 {
        int ret;
 
 {
        int ret;
 
-       if (argc != 1)
-               return -E_PLAY_SYNTAX;
-       ret = next_valid_file(pt);
+       ret = next_valid_file();
        if (ret < 0)
                return ret;
        if (ret < 0)
                return ret;
-       kill_stream(pt);
+       kill_stream();
        pt->next_file = ret;
        pt->rq = CRT_FILE_CHANGE;
        pt->start_chunk = 0;
        return 0;
 }
        pt->next_file = ret;
        pt->rq = CRT_FILE_CHANGE;
        pt->start_chunk = 0;
        return 0;
 }
+EXPORT_PLAY_CMD_HANDLER(next);
 
 
-static int com_fg(struct play_task *pt, int argc, __a_unused char **argv)
+static int com_fg(__a_unused struct lls_parse_result *lpr)
 {
 {
-       if (argc != 1)
-               return -E_PLAY_SYNTAX;
        pt->background = false;
        return 0;
 }
        pt->background = false;
        return 0;
 }
+EXPORT_PLAY_CMD_HANDLER(fg);
 
 
-static int com_bg(struct play_task *pt, int argc, __a_unused char **argv)
+static int com_bg(__a_unused struct lls_parse_result *lpr)
 {
 {
-       if (argc != 1)
-               return -E_PLAY_SYNTAX;
        pt->background = true;
        return 0;
 }
        pt->background = true;
        return 0;
 }
+EXPORT_PLAY_CMD_HANDLER(bg);
 
 
-static int com_jmp(struct play_task *pt, int argc, char **argv)
+static int com_jmp(struct lls_parse_result *lpr)
 {
        int32_t percent;
        int ret;
 {
        int32_t percent;
        int ret;
+       char *errctx;
 
 
-       if (argc != 2)
-               return -E_PLAY_SYNTAX;
-       ret = para_atoi32(argv[1], &percent);
+       ret = lls(lls_check_arg_count(lpr, 1, 1, &errctx));
+       if (ret < 0) {
+               if (errctx)
+                       PARA_ERROR_LOG("%s\n", errctx);
+               free(errctx);
+               return ret;
+       }
+       ret = para_atoi32(lls_input(0, lpr), &percent);
        if (ret < 0)
                return ret;
        if (percent < 0 || percent > 100)
                return -ERRNO_TO_PARA_ERROR(EINVAL);
        if (percent == 100)
        if (ret < 0)
                return ret;
        if (percent < 0 || percent > 100)
                return -ERRNO_TO_PARA_ERROR(EINVAL);
        if (percent == 100)
-               return com_next(pt, 1, (char *[]){"next", NULL});
+               return com_next(NULL);
        if (pt->playing && !pt->fn.btrn)
                return 0;
        pt->start_chunk = percent * pt->num_chunks / 100;
        if (!pt->playing)
                return 0;
        pt->rq = CRT_REPOS;
        if (pt->playing && !pt->fn.btrn)
                return 0;
        pt->start_chunk = percent * pt->num_chunks / 100;
        if (!pt->playing)
                return 0;
        pt->rq = CRT_REPOS;
-       kill_stream(pt);
+       kill_stream();
        return 0;
 }
        return 0;
 }
+EXPORT_PLAY_CMD_HANDLER(jmp);
 
 
-static int com_ff(struct play_task *pt, int argc, char **argv)
+static int com_ff(struct lls_parse_result *lpr)
 {
        int32_t seconds;
 {
        int32_t seconds;
+       char *errctx;
        int ret;
 
        int ret;
 
-       if (argc != 2)
-               return -E_PLAY_SYNTAX;
-       ret = para_atoi32(argv[1], &seconds);
+       ret = lls(lls_check_arg_count(lpr, 1, 1, &errctx));
+       if (ret < 0) {
+               if (errctx)
+                       PARA_ERROR_LOG("%s\n", errctx);
+               free(errctx);
+               return ret;
+       }
+       ret = para_atoi32(lls_input(0, lpr), &seconds);
        if (ret < 0)
                return ret;
        if (pt->playing && !pt->fn.btrn)
                return 0;
        if (ret < 0)
                return ret;
        if (pt->playing && !pt->fn.btrn)
                return 0;
-       seconds += get_play_time(pt);
+       seconds += get_play_time();
        seconds = PARA_MIN(seconds, (typeof(seconds))pt->seconds - 4);
        seconds = PARA_MAX(seconds, 0);
        pt->start_chunk = pt->num_chunks * seconds / pt->seconds;
        seconds = PARA_MIN(seconds, (typeof(seconds))pt->seconds - 4);
        seconds = PARA_MAX(seconds, 0);
        pt->start_chunk = pt->num_chunks * seconds / pt->seconds;
@@ -974,49 +1002,52 @@ static int com_ff(struct play_task *pt, int argc, char **argv)
        if (!pt->playing)
                return 0;
        pt->rq = CRT_REPOS;
        if (!pt->playing)
                return 0;
        pt->rq = CRT_REPOS;
-       kill_stream(pt);
+       kill_stream();
        return 0;
 }
        return 0;
 }
+EXPORT_PLAY_CMD_HANDLER(ff);
 
 
-static int run_command(char *line, struct play_task *pt)
+static int run_command(char *line)
 {
 {
-       int i, ret, argc;
+       int ret, argc;
        char **argv = NULL;
        char **argv = NULL;
+       char *errctx = NULL;
+       const struct play_command_info *pci;
+       struct lls_parse_result *lpr;
+       const struct lls_command *cmd;
 
 
-       attach_stdout(pt, __FUNCTION__);
+       attach_stdout(__FUNCTION__);
        ret = create_argv(line, " ", &argv);
        ret = create_argv(line, " ", &argv);
-       if (ret < 0) {
-               PARA_ERROR_LOG("parse error: %s\n", para_strerror(-ret));
-               return 0;
-       }
+       if (ret < 0)
+               goto out;
        if (ret == 0)
                goto out;
        argc = ret;
        if (ret == 0)
                goto out;
        argc = ret;
-       FOR_EACH_COMMAND(i) {
-               if (strcmp(pp_cmds[i].name, argv[0]))
-                       continue;
-               ret = pp_cmds[i].handler(pt, argc, argv);
-               if (ret < 0)
-                       PARA_WARNING_LOG("%s: %s\n", pt->background?
-                               "" : argv[0], para_strerror(-ret));
-               ret = 1;
+       ret = lls(lls_lookup_subcmd(argv[0], play_cmd_suite, &errctx));
+       if (ret < 0)
                goto out;
                goto out;
-       }
-       PARA_WARNING_LOG("invalid command: %s\n", argv[0]);
-       ret = 0;
+       cmd = lls_cmd(ret, play_cmd_suite);
+       ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
+       if (ret < 0)
+               goto out;
+       pci = lls_user_data(cmd);
+       ret = pci->handler(lpr);
+       lls_free_parse_result(lpr, cmd);
 out:
 out:
+       if (errctx)
+               PARA_ERROR_LOG("%s\n", errctx);
+       free(errctx);
        free_argv(argv);
        return ret;
 }
 
 static int play_i9e_line_handler(char *line)
 {
        free_argv(argv);
        return ret;
 }
 
 static int play_i9e_line_handler(char *line)
 {
-       return run_command(line, &play_task);
+       return run_command(line);
 }
 
 static int play_i9e_key_handler(int key)
 {
 }
 
 static int play_i9e_key_handler(int key)
 {
-       struct play_task *pt = &play_task;
        int idx = get_key_map_idx(key);
        char *seq = get_key_map_seq(key);
        char *cmd = get_key_map_cmd(key);
        int idx = get_key_map_idx(key);
        char *seq = get_key_map_seq(key);
        char *cmd = get_key_map_cmd(key);
@@ -1025,7 +1056,7 @@ static int play_i9e_key_handler(int key)
        PARA_NOTICE_LOG("pressed %d: %s key #%d (%s -> %s)\n",
                key, internal? "internal" : "user-defined",
                idx, seq, cmd);
        PARA_NOTICE_LOG("pressed %d: %s key #%d (%s -> %s)\n",
                key, internal? "internal" : "user-defined",
                idx, seq, cmd);
-       run_command(cmd, pt);
+       run_command(cmd);
        free(seq);
        free(cmd);
        pt->next_update = *now;
        free(seq);
        free(cmd);
        pt->next_update = *now;
@@ -1042,7 +1073,7 @@ static struct i9e_client_info ici = {
 
 static void sigint_handler(int sig)
 {
 
 static void sigint_handler(int sig)
 {
-       play_task.background = true;
+       pt->background = true;
        i9e_signal_dispatch(sig);
 }
 
        i9e_signal_dispatch(sig);
 }
 
@@ -1051,15 +1082,15 @@ static void sigint_handler(int sig)
  * stderr. Once the i9e subsystem has been initialized, we switch to the i9e
  * log facility.
  */
  * stderr. Once the i9e subsystem has been initialized, we switch to the i9e
  * log facility.
  */
-static void session_open(struct play_task *pt)
+static void session_open(void)
 {
        int ret;
        char *history_file;
        struct sigaction act;
 
        PARA_NOTICE_LOG("\n%s\n", version_text("play"));
 {
        int ret;
        char *history_file;
        struct sigaction act;
 
        PARA_NOTICE_LOG("\n%s\n", version_text("play"));
-       if (conf.history_file_given)
-               history_file = para_strdup(conf.history_file_arg);
+       if (OPT_GIVEN(HISTORY_FILE))
+               history_file = para_strdup(OPT_STRING_VAL(HISTORY_FILE));
        else {
                char *home = para_homedir();
                history_file = make_message("%s/.paraslash/play.history",
        else {
                char *home = para_homedir();
                history_file = make_message("%s/.paraslash/play.history",
@@ -1095,7 +1126,7 @@ out:
        exit(EXIT_FAILURE);
 }
 
        exit(EXIT_FAILURE);
 }
 
-static void session_update_time_string(struct play_task *pt, char *str, unsigned len)
+static void session_update_time_string(char *str, unsigned len)
 {
        if (pt->background)
                return;
 {
        if (pt->background)
                return;
@@ -1120,30 +1151,30 @@ static void session_update_time_string(struct play_task *pt, char *str, unsigned
  * terminates. Subsequent calls to i9e_get_error() then return negative and we
  * are allowed to call i9e_close() and terminate as well.
  */
  * terminates. Subsequent calls to i9e_get_error() then return negative and we
  * are allowed to call i9e_close() and terminate as well.
  */
-static int session_post_select(__a_unused struct sched *s, struct play_task *pt)
+static int session_post_select(__a_unused struct sched *s)
 {
        int ret;
 
        if (pt->background)
 {
        int ret;
 
        if (pt->background)
-               detach_stdout(pt);
+               detach_stdout();
        else
        else
-               attach_stdout(pt, __FUNCTION__);
+               attach_stdout(__FUNCTION__);
        ret = i9e_get_error();
        if (ret < 0) {
        ret = i9e_get_error();
        if (ret < 0) {
-               kill_stream(pt);
+               kill_stream();
                i9e_close();
                para_log = stderr_log;
                free(ici.history_file);
                return ret;
        }
                i9e_close();
                para_log = stderr_log;
                free(ici.history_file);
                return ret;
        }
-       if (get_playback_state(pt) == 'X')
+       if (get_playback_state() == 'X')
                i9e_signal_dispatch(SIGTERM);
        return 0;
 }
 
 #else /* HAVE_READLINE */
 
                i9e_signal_dispatch(SIGTERM);
        return 0;
 }
 
 #else /* HAVE_READLINE */
 
-static int session_post_select(struct sched *s, struct play_task *pt)
+static int session_post_select(struct sched *s)
 {
        char c;
 
 {
        char c;
 
@@ -1151,38 +1182,36 @@ static int session_post_select(struct sched *s, struct play_task *pt)
                return 0;
        if (read(STDIN_FILENO, &c, 1))
                do_nothing;
                return 0;
        if (read(STDIN_FILENO, &c, 1))
                do_nothing;
-       kill_stream(pt);
+       kill_stream();
        return 1;
 }
 
        return 1;
 }
 
-static void session_open(__a_unused struct play_task *pt)
+static void session_open(void)
 {
 }
 
 {
 }
 
-static void session_update_time_string(__a_unused struct play_task *pt,
-               char *str, __a_unused unsigned len)
+static void session_update_time_string(char *str, __a_unused unsigned len)
 {
        printf("\r%s     ", str);
        fflush(stdout);
 }
 #endif /* HAVE_READLINE */
 
 {
        printf("\r%s     ", str);
        fflush(stdout);
 }
 #endif /* HAVE_READLINE */
 
-static void play_pre_select(struct sched *s, void *context)
+static void play_pre_select(struct sched *s, __a_unused void *context)
 {
 {
-       struct play_task *pt = context;
        char state;
 
        para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
        char state;
 
        para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
-       state = get_playback_state(pt);
+       state = get_playback_state();
        if (state == 'R' || state == 'F' || state == 'X')
                return sched_min_delay(s);
        sched_request_barrier_or_min_delay(&pt->next_update, s);
 }
 
        if (state == 'R' || state == 'F' || state == 'X')
                return sched_min_delay(s);
        sched_request_barrier_or_min_delay(&pt->next_update, s);
 }
 
-static unsigned get_time_string(struct play_task *pt, char **result)
+static unsigned get_time_string(char **result)
 {
        int seconds, length;
 {
        int seconds, length;
-       char state = get_playback_state(pt);
+       char state = get_playback_state();
 
        /* do not return anything if things are about to change */
        if (state != 'P' && state != 'U') {
 
        /* do not return anything if things are about to change */
        if (state != 'P' && state != 'U') {
@@ -1192,7 +1221,7 @@ static unsigned get_time_string(struct play_task *pt, char **result)
        length = pt->seconds;
        if (length == 0)
                return xasprintf(result, "0:00 [0:00] (0%%/0:00)");
        length = pt->seconds;
        if (length == 0)
                return xasprintf(result, "0:00 [0:00] (0%%/0:00)");
-       seconds = get_play_time(pt);
+       seconds = get_play_time();
        return xasprintf(result, "#%u: %d:%02d [%d:%02d] (%d%%/%d:%02d) %s",
                pt->current_file,
                seconds / 60,
        return xasprintf(result, "#%u: %d:%02d [%d:%02d] (%d%%/%d:%02d) %s",
                pt->current_file,
                seconds / 60,
@@ -1202,28 +1231,27 @@ static unsigned get_time_string(struct play_task *pt, char **result)
                length? (seconds * 100 + length / 2) / length : 0,
                length / 60,
                length % 60,
                length? (seconds * 100 + length / 2) / length : 0,
                length / 60,
                length % 60,
-               conf.inputs[pt->current_file]
+               get_playlist_file(pt->current_file)
        );
 }
 
        );
 }
 
-static int play_post_select(struct sched *s, void *context)
+static int play_post_select(struct sched *s, __a_unused void *context)
 {
 {
-       struct play_task *pt = context;
        int ret;
 
        int ret;
 
-       ret = eof_cleanup(pt);
+       ret = eof_cleanup();
        if (ret < 0) {
                pt->rq = CRT_TERM_RQ;
                return 0;
        }
        if (ret < 0) {
                pt->rq = CRT_TERM_RQ;
                return 0;
        }
-       ret = session_post_select(s, pt);
+       ret = session_post_select(s);
        if (ret < 0)
                goto out;
        if (!pt->wn.btrn && !pt->fn.btrn) {
        if (ret < 0)
                goto out;
        if (!pt->wn.btrn && !pt->fn.btrn) {
-               char state = get_playback_state(pt);
+               char state = get_playback_state();
                if (state == 'P' || state == 'R' || state == 'F') {
                        PARA_NOTICE_LOG("state: %c\n", state);
                if (state == 'P' || state == 'R' || state == 'F') {
                        PARA_NOTICE_LOG("state: %c\n", state);
-                       ret = load_next_file(pt);
+                       ret = load_next_file();
                        if (ret < 0) {
                                PARA_ERROR_LOG("%s\n", para_strerror(-ret));
                                pt->rq = CRT_TERM_RQ;
                        if (ret < 0) {
                                PARA_ERROR_LOG("%s\n", para_strerror(-ret));
                                pt->rq = CRT_TERM_RQ;
@@ -1235,10 +1263,10 @@ static int play_post_select(struct sched *s, void *context)
        }
        if (tv_diff(now, &pt->next_update, NULL) >= 0) {
                char *str;
        }
        if (tv_diff(now, &pt->next_update, NULL) >= 0) {
                char *str;
-               unsigned len = get_time_string(pt, &str);
+               unsigned len = get_time_string(&str);
                struct timeval delay = {.tv_sec = 0, .tv_usec = 100 * 1000};
                if (str && len > 0)
                struct timeval delay = {.tv_sec = 0, .tv_usec = 100 * 1000};
                if (str && len > 0)
-                       session_update_time_string(pt, str, len);
+                       session_update_time_string(str, len);
                free(str);
                tv_add(now, &delay, &pt->next_update);
        }
                free(str);
                tv_add(now, &delay, &pt->next_update);
        }
@@ -1258,26 +1286,20 @@ out:
 int main(int argc, char *argv[])
 {
        int ret;
 int main(int argc, char *argv[])
 {
        int ret;
-       struct play_task *pt = &play_task;
+       unsigned num_inputs;
 
        /* needed this early to make help work */
        recv_init();
 
        /* needed this early to make help work */
        recv_init();
-       filter_init();
-       writer_init();
 
        sched.default_timeout.tv_sec = 5;
 
        sched.default_timeout.tv_sec = 5;
-
        parse_config_or_die(argc, argv);
        parse_config_or_die(argc, argv);
-       if (conf.inputs_num == 0)
-               print_help_and_die();
-       check_afh_receiver_or_die();
-
-       session_open(pt);
-       if (conf.randomize_given)
-               shuffle(conf.inputs, conf.inputs_num);
-       pt->invalid = para_calloc(sizeof(*pt->invalid) * conf.inputs_num);
+       AFH_RECV->init();
+       session_open();
+       num_inputs = lls_num_inputs(play_lpr);
+       init_shuffle_map();
+       pt->invalid = para_calloc(sizeof(*pt->invalid) * num_inputs);
        pt->rq = CRT_FILE_CHANGE;
        pt->rq = CRT_FILE_CHANGE;
-       pt->current_file = conf.inputs_num - 1;
+       pt->current_file = num_inputs - 1;
        pt->playing = true;
        pt->task = task_register(&(struct task_info){
                .name = "play",
        pt->playing = true;
        pt->task = task_register(&(struct task_info){
                .name = "play",