]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - fade.c
Convert para_fade to lopsub.
[paraslash.git] / fade.c
diff --git a/fade.c b/fade.c
index 67dc4d5494c13ea8d6d167d56502b5818146bcff..34237c6ba3cb339c5118e5a7636fb4742383136e 100644 (file)
--- a/fade.c
+++ b/fade.c
@@ -7,27 +7,33 @@
 /** \file fade.c A volume fader and alarm clock for OSS. */
 
 #include <regex.h>
+#include <lopsub.h>
 
-#include "fade.cmdline.h"
+#include "fade.lsg.h"
 #include "para.h"
 #include "fd.h"
 #include "string.h"
 #include "mix.h"
 #include "error.h"
-#include "ggo.h"
 #include "version.h"
 
 /** Array of error strings. */
 DEFINE_PARA_ERRLIST;
 
-static struct fade_args_info conf;
-
 enum mixer_id {MIXER_ENUM};
 static char *mixer_name[] = {MIXER_NAMES};
 DECLARE_MIXER_INITS;
 static struct mixer supported_mixer[] = {MIXER_ARRAY};
 #define FOR_EACH_MIXER(i) for ((i) = 0; (i) < NUM_SUPPORTED_MIXERS; (i)++)
 
+static struct lls_parse_result *lpr;
+
+#define CMD_PTR (lls_cmd(0, fade_suite))
+#define OPT_RESULT(_name) (lls_opt_result(LSG_FADE_PARA_FADE_OPT_ ## _name, lpr))
+#define OPT_GIVEN(_name) (lls_opt_given(OPT_RESULT(_name)))
+#define OPT_STRING_VAL(_name) (lls_string_val(0, OPT_RESULT(_name)))
+#define OPT_UINT32_VAL(_name) (lls_uint32_val(0, OPT_RESULT(_name)))
+
 static int loglevel;
 static __printf_2_3 void date_log(int ll, const char *fmt, ...)
 {
@@ -70,7 +76,7 @@ static int fade(struct mixer *m, struct mixer_handle *h, int new_vol, int fade_t
                goto out;
        vol = ret;
        PARA_NOTICE_LOG("fading %s from %d to %d in %u seconds\n",
-               conf.mixer_channel_arg, vol, new_vol, secs);
+               OPT_STRING_VAL(MIXER_CHANNEL), vol, new_vol, secs);
        diff = new_vol - vol;
        if (!diff) {
                sleep(secs);
@@ -123,7 +129,7 @@ fail:
        exit(EXIT_FAILURE);
 }
 
-static void change_afs_mode(char *afs_mode)
+static void change_afs_mode(const char *afs_mode)
 {
        char *cmd;
 
@@ -139,8 +145,9 @@ static int set_initial_volume(struct mixer *m, struct mixer_handle *h)
 {
        int i, ret;
 
-       for (i = 0; i < conf.ivol_given; i++) {
-               char *p, *ch, *arg = para_strdup(conf.ivol_arg[i]);
+       for (i = 0; i < OPT_GIVEN(IVOL); i++) {
+               const char *val = lls_string_val(i, OPT_RESULT(IVOL));
+               char *p, *ch, *arg = para_strdup(val);
                int32_t iv;
                p = strchr(arg, ':');
                if (p) {
@@ -178,19 +185,19 @@ static int sweet_dreams(struct mixer *m, struct mixer_handle *h)
        time_t t1, wake_time_epoch;
        unsigned int delay;
        struct tm *tm;
-       int ret, min = conf.wake_min_arg;
-       char *fo_mood = conf.fo_mood_arg;
-       char *fi_mood = conf.fi_mood_arg;
-       char *sleep_mood = conf.sleep_mood_arg;
-       int fit = conf.fi_time_arg;
-       int fot = conf.fo_time_arg;
-       int fiv = conf.fi_vol_arg;
-       int fov = conf.fo_vol_arg;
+       int ret, min = OPT_UINT32_VAL(WAKE_MIN);
+       const char *fo_mood = OPT_STRING_VAL(FO_MOOD);
+       const char *fi_mood = OPT_STRING_VAL(FI_MOOD);
+       const char *sleep_mood = OPT_STRING_VAL(SLEEP_MOOD);
+       int fit = OPT_UINT32_VAL(FI_TIME);
+       int fot = OPT_UINT32_VAL(FO_TIME);
+       int fiv = OPT_UINT32_VAL(FI_VOL);
+       int fov = OPT_UINT32_VAL(FO_VOL);
 
        /* calculate wake time */
        time(&t1);
-       if (conf.wake_hour_given) {
-               int hour = conf.wake_hour_arg;
+       if (OPT_GIVEN(WAKE_HOUR)) {
+               int hour = OPT_UINT32_VAL(WAKE_HOUR);
                tm = localtime(&t1);
                if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
                        t1 += 86400; /* wake time is tomorrow */
@@ -214,7 +221,7 @@ static int sweet_dreams(struct mixer *m, struct mixer_handle *h)
                        return ret;
                change_afs_mode(fo_mood);
                client_cmd("play");
-               ret = set_channel(m, h, conf.mixer_channel_arg);
+               ret = set_channel(m, h, OPT_STRING_VAL(MIXER_CHANNEL));
                if (ret < 0)
                        return ret;
                ret = fade(m, h, fov, fot);
@@ -225,7 +232,7 @@ static int sweet_dreams(struct mixer *m, struct mixer_handle *h)
                if (ret < 0)
                        return ret;
        }
-       if (conf.sleep_mood_given) {
+       if (OPT_GIVEN(SLEEP_MOOD)) {
                change_afs_mode(sleep_mood);
                client_cmd("play");
        } else
@@ -253,37 +260,25 @@ static int snooze(struct mixer *m, struct mixer_handle *h)
 {
        int ret, val;
 
-       if (conf.so_time_arg <= 0)
+       if (OPT_UINT32_VAL(SO_TIME) == 0)
                return 1;
        ret = m->get(h);
        if (ret < 0)
                return ret;
        val = ret;
-       if (val < conf.so_vol_arg)
-               ret = m->set(h, conf.so_vol_arg);
+       if (val < OPT_UINT32_VAL(SO_VOL))
+               ret = m->set(h, OPT_UINT32_VAL(SO_VOL));
        else
-               ret = fade(m, h, conf.so_vol_arg, conf.so_time_arg);
+               ret = fade(m, h, OPT_UINT32_VAL(SO_VOL),
+                       OPT_UINT32_VAL(SO_TIME));
        if (ret < 0)
                return ret;
        client_cmd("pause");
-       PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
-       sleep(conf.snooze_time_arg);
+       PARA_NOTICE_LOG("%" PRIu32 " seconds snooze time...\n",
+               OPT_UINT32_VAL(SNOOZE_TIME));
+       sleep(OPT_UINT32_VAL(SNOOZE_TIME));
        client_cmd("play");
-       return fade(m, h, conf.si_vol_arg, conf.si_time_arg);
-}
-
-static int configfile_exists(void)
-{
-       static char *config_file;
-
-       if (!conf.config_file_given) {
-               char *home = para_homedir();
-               free(config_file);
-               config_file = make_message("%s/.paraslash/fade.conf", home);
-               free(home);
-               conf.config_file_arg = config_file;
-       }
-       return file_exists(conf.config_file_arg);
+       return fade(m, h, OPT_UINT32_VAL(SI_VOL), OPT_UINT32_VAL(SI_TIME));
 }
 
 static void init_mixers(void)
@@ -300,18 +295,18 @@ static void init_mixers(void)
 
 static int set_val(struct mixer *m, struct mixer_handle *h)
 {
-       return m->set(h, conf.val_arg);
+       return m->set(h, OPT_UINT32_VAL(VAL));
 }
 
 static struct mixer *get_mixer_or_die(void)
 {
        int i;
 
-       if (!conf.mixer_api_given)
+       if (!OPT_GIVEN(MIXER_API))
                i = DEFAULT_MIXER;
        else
                FOR_EACH_MIXER(i)
-                       if (!strcmp(mixer_name[i], conf.mixer_api_arg))
+                       if (!strcmp(mixer_name[i], OPT_STRING_VAL(MIXER_API)))
                                break;
        if (i < NUM_SUPPORTED_MIXERS) {
                PARA_NOTICE_LOG("using %s mixer API\n", mixer_name[i]);
@@ -326,13 +321,19 @@ static struct mixer *get_mixer_or_die(void)
        exit(EXIT_FAILURE);
 }
 
-__noreturn static void print_help_and_die(void)
+static void handle_help_flags(void)
 {
-       struct ggo_help h = DEFINE_GGO_HELP(fade);
-       bool d = conf.detailed_help_given;
+       char *help;
 
-       ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
-       exit(0);
+       if (OPT_GIVEN(DETAILED_HELP))
+               help = lls_long_help(CMD_PTR);
+       else if (OPT_GIVEN(HELP))
+               help = lls_short_help(CMD_PTR);
+       else
+               return;
+       printf("%s\n", help);
+       free(help);
+       exit(EXIT_SUCCESS);
 }
 
 /**
@@ -348,63 +349,93 @@ __noreturn static void print_help_and_die(void)
  */
 int main(int argc, char *argv[])
 {
+       const struct lls_command *cmd = CMD_PTR;
        int ret;
+       char *cf, *errctx;
        struct mixer *m;
-       struct mixer_handle *h = NULL;
-
-       fade_cmdline_parser(argc, argv, &conf);
-       loglevel = get_loglevel_by_name(conf.loglevel_arg);
-       version_handle_flag("fade", conf.version_given);
-       if (conf.help_given || conf.detailed_help_given)
-               print_help_and_die();
-       ret = configfile_exists();
-       if (!ret && conf.config_file_given) {
-               PARA_EMERG_LOG("can not read config file %s\n",
-                       conf.config_file_arg);
-               exit(EXIT_FAILURE);
+       struct mixer_handle *h;
+       void *map;
+       size_t sz;
+
+       ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
+       if (ret < 0)
+               goto fail;
+       loglevel = OPT_UINT32_VAL(LOGLEVEL);
+       version_handle_flag("fade", 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/fade.conf", home);
+               free(home);
        }
-       if (ret) {
-               struct fade_cmdline_parser_params params = {
-                       .override = 0,
-                       .initialize = 0,
-                       .check_required = 0,
-                       .check_ambiguity = 0,
-                       .print_errors = 1
-               };
-               fade_cmdline_parser_config_file(conf.config_file_arg,
-                       &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;
+       } else {
+               int cf_argc;
+               char **cf_argv;
+               struct lls_parse_result *cf_lpr, *merged_lpr;
+               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_PTR, &cf_lpr, &errctx));
+               lls_free_argv(cf_argv);
+               if (ret < 0)
+                       goto free_cf;
+               ret = lls(lls_merge(lpr, cf_lpr, CMD_PTR, &merged_lpr,
+                       &errctx));
+               lls_free_parse_result(cf_lpr, CMD_PTR);
+               if (ret < 0)
+                       goto free_cf;
+               lls_free_parse_result(lpr, CMD_PTR);
+               lpr = merged_lpr;
+               loglevel = OPT_UINT32_VAL(LOGLEVEL);
        }
        init_mixers();
        m = get_mixer_or_die();
-       ret = m->open(conf.mixer_device_arg, &h);
+       ret = m->open(OPT_STRING_VAL(MIXER_DEVICE), &h);
        if (ret < 0)
-               goto out;
-       ret = set_channel(m, h, conf.mixer_channel_arg);
+               goto free_cf;
+       ret = set_channel(m, h, OPT_STRING_VAL(MIXER_CHANNEL));
        if (ret == -E_BAD_CHANNEL) {
                char *channels = m->get_channels(h);
                printf("Available channels: %s\n", channels);
                free(channels);
        }
        if (ret < 0)
-               goto out;
-       switch (conf.mode_arg) {
-       case mode_arg_fade:
-               ret = fade(m, h, conf.fade_vol_arg, conf.fade_time_arg);
-               break;
-       case mode_arg_snooze:
+               goto close_mixer;
+       if (strcmp(OPT_STRING_VAL(MODE), "fade") == 0)
+               ret = fade(m, h, OPT_UINT32_VAL(FADE_VOL),
+                       OPT_UINT32_VAL(FADE_TIME));
+       else if (strcmp(OPT_STRING_VAL(MODE), "snooze") == 0)
                ret = snooze(m, h);
-               break;
-       case mode_arg_set:
+       else if (strcmp(OPT_STRING_VAL(MODE), "set") == 0)
                ret = set_val(m, h);
-               break;
-       default: /* sleep mode */
+       else if (strcmp(OPT_STRING_VAL(MODE), "sleep") == 0)
                ret = sweet_dreams(m, h);
-               break;
+       else {
+               PARA_ERROR_LOG("invalid mode: %s\n", OPT_STRING_VAL(MODE));
+               ret = -ERRNO_TO_PARA_ERROR(EINVAL);
        }
-out:
+close_mixer:
        m->close(&h);
-       if (ret < 0)
-               PARA_EMERG_LOG("%s\n", para_strerror(-ret));
-       return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
+free_cf:
+       free(cf);
+       lls_free_parse_result(lpr, CMD_PTR);
+       if (ret >= 0)
+               return EXIT_SUCCESS;
+fail:
+       if (errctx)
+               PARA_ERROR_LOG("%s\n", errctx);
+       free(errctx);
+       PARA_EMERG_LOG("%s\n", para_strerror(-ret));
+       return EXIT_FAILURE;
 }