i9e: Fix typo: s/ie9/i9e.
[paraslash.git] / play.c
diff --git a/play.c b/play.c
index 2155ebf824d45bb578bcb0b782c565be42e349b2..14fac42fd7b6e92566815d4c56b1f4b6b23c88e2 100644 (file)
--- a/play.c
+++ b/play.c
@@ -1,8 +1,4 @@
-/*
- * 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. */
 
@@ -15,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"
@@ -112,10 +109,6 @@ 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;
 
@@ -143,7 +136,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;
@@ -154,76 +147,38 @@ static void handle_help_flags(void)
 
 static void parse_config_or_die(int argc, char *argv[])
 {
-       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;
+       int i, ret;
        unsigned num_kmas;
-       void *map;
-       size_t sz;
+       char *errctx;
 
-       ret = lls(lls_parse(argc, argv, cmd, &play_lpr, &errctx));
-       if (ret < 0)
-               goto fail;
+       ret = lls(lls_parse(argc, argv, CMD_PTR, &play_lpr, &errctx));
+       if (ret < 0) {
+               if (errctx)
+                       PARA_EMERG_LOG("%s\n", errctx);
+               free(errctx);
+               PARA_EMERG_LOG("failed to parse command line options: %s\n",
+                       para_strerror(-ret));
+               exit(EXIT_FAILURE);
+       }
        loglevel = OPT_UINT32_VAL(LOGLEVEL);
        version_handle_flag("play", OPT_GIVEN(VERSION));
-       handle_help_flags();
-       if (OPT_GIVEN(CONFIG_FILE))
-               cf = para_strdup(OPT_STRING_VAL(CONFIG_FILE));
-       else {
-               char *home = para_homedir();
-               cf = make_message("%s/.paraslash/play.conf", home);
-               free(home);
-       }
-       ret = mmap_full_file(cf, O_RDONLY, &map, &sz, NULL);
+       handle_help_flags(); /* also handles the zero-arg case */
+       ret = lsu_merge_config_file_options(OPT_STRING_VAL(CONFIG_FILE),
+               "play.conf", &play_lpr, CMD_PTR, play_suite, 0 /* flags */);
        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 free_cf;
+               PARA_EMERG_LOG("failed to parse config file: %s\n",
+                       para_strerror(-ret));
+               exit(EXIT_FAILURE);
        }
-       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);
-
-       ret = lls(lls_check_arg_count(play_lpr, 1, INT_MAX, &errctx));
-       if (ret < 0)
-               goto free_cf;
        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);
-               goto free_cf;
+               exit(EXIT_FAILURE);
        }
-       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);
 }
 
 static char get_playback_state(void)
@@ -260,7 +215,6 @@ static long unsigned get_play_time(void)
        return result;
 }
 
-
 static void wipe_receiver_node(void)
 {
        PARA_NOTICE_LOG("cleaning up receiver node\n");
@@ -475,6 +429,15 @@ static int next_valid_file(void)
        int i, j = pt->current_file;
        unsigned num_inputs = lls_num_inputs(play_lpr);
 
+       if (j == num_inputs - 1) {
+               switch (OPT_UINT32_VAL(END_OF_PLAYLIST)) {
+               case EOP_LOOP: break;
+               case EOP_STOP:
+                       pt->playing = false;
+                       return 0;
+               case EOP_QUIT: return -E_EOP;
+               }
+       }
        for (i = 0; i < num_inputs; i++) {
                j = (j + 1) % num_inputs;
                if (!pt->invalid[j])
@@ -713,9 +676,15 @@ 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);
 }
 
 static struct i9e_completer pp_completers[] = {
@@ -740,6 +709,11 @@ static void detach_stdout(void)
        btr_remove_node(&pt->btrn);
 }
 
+#define EXPORT_PLAY_CMD_HANDLER(_cmd) \
+       const struct play_command_info lsg_play_cmd_com_ ## _cmd ## _user_data = { \
+               .handler = com_ ## _cmd \
+       };
+
 static int com_quit(__a_unused struct lls_parse_result *lpr)
 {
        pt->rq = CRT_TERM_RQ;
@@ -749,27 +723,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);
@@ -784,18 +746,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);
@@ -1100,9 +1052,16 @@ static void session_open(void)
                history_file = para_strdup(OPT_STRING_VAL(HISTORY_FILE));
        else {
                char *home = para_homedir();
-               history_file = make_message("%s/.paraslash/play.history",
-                       home);
+               char *dot_para = make_message("%s/.paraslash", home);
+
                free(home);
+               ret = para_mkdir(dot_para, 0777);
+               /* warn, but otherwise ignore mkdir error */
+               if (ret < 0 && ret != -ERRNO_TO_PARA_ERROR(EEXIST))
+                       PARA_WARNING_LOG("Can not create %s: %s\n", dot_para,
+                               para_strerror(-ret));
+               history_file = make_message("%s/play.history", dot_para);
+               free(dot_para);
        }
        ici.history_file = history_file;
        ici.loglevel = loglevel;
@@ -1143,7 +1102,7 @@ static void session_update_time_string(char *str, unsigned len)
                if (btr_get_input_queue_size(pt->btrn) > 0)
                        return;
        }
-       ie9_print_status_bar(str, len);
+       i9e_print_status_bar(str, len);
 }
 
 /*
@@ -1295,18 +1254,13 @@ int main(int argc, char *argv[])
        int ret;
        unsigned num_inputs;
 
-       /* needed this early to make help work */
-       recv_init();
-
        sched.default_timeout.tv_sec = 5;
        parse_config_or_die(argc, argv);
-       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->current_file = num_inputs - 1;
        pt->playing = true;
        pt->task = task_register(&(struct task_info){
                .name = "play",