From 98dc8206ae61a857db6017e7244a9af0e578adc6 Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 20 Feb 2014 14:46:23 +0100 Subject: options: handle escape sequences in e.g. --playing-msg differently M_OPT_PARSE_ESCAPES was pretty stupid, and broke the (useful) assumption that string variables contain exactly the same value as set by the option. Simplify it, and move escape handling to the place where it's used. Escape handling itself is not terribly useful, but still allows useful things like multiline custom OSD with "\n". --- options/m_option.c | 67 +++++++----------------------------------------------- options/m_option.h | 3 --- options/options.c | 6 ++--- 3 files changed, 11 insertions(+), 65 deletions(-) (limited to 'options') diff --git a/options/m_option.c b/options/m_option.c index e0ab4ed6c8..7cfd518eb1 100644 --- a/options/m_option.c +++ b/options/m_option.c @@ -745,36 +745,6 @@ const m_option_type_t m_option_type_float = { #undef VAL #define VAL(x) (*(char **)(x)) -static char *unescape_string(void *talloc_ctx, bstr str) -{ - bstr dst = {0}; - while (str.len) { - if (!mp_append_escaped_string(talloc_ctx, &dst, &str)) { - talloc_free(dst.start); - return NULL; - } - if (!bstr_eatstart0(&str, "\"")) - break; - bstr_xappend(talloc_ctx, &dst, bstr0("\"")); - } - return dst.start; -} - -static char *escape_string(char *str0) -{ - char *res = talloc_strdup(NULL, ""); - bstr str = bstr0(str0); - while (str.len) { - bstr rest; - bool esc = bstr_split_tok(str, "\\", &str, &rest); - res = talloc_strndup_append_buffer(res, str.start, str.len); - if (esc) - res = talloc_strdup_append_buffer(res, "\\\\"); - str = rest; - } - return res; -} - static int clamp_str(const m_option_t *opt, void *val) { char *v = VAL(val); @@ -789,43 +759,26 @@ static int clamp_str(const m_option_t *opt, void *val) static int parse_str(struct mp_log *log, const m_option_t *opt, struct bstr name, struct bstr param, void *dst) { - int r = 1; - void *tmp = talloc_new(NULL); - - if (param.start == NULL) { - r = M_OPT_MISSING_PARAM; - goto exit; - } + if (param.start == NULL) + return M_OPT_MISSING_PARAM; m_opt_string_validate_fn validate = opt->priv; if (validate) { - r = validate(log, opt, name, param); + int r = validate(log, opt, name, param); if (r < 0) - goto exit; - } - - if (opt->flags & M_OPT_PARSE_ESCAPES) { - char *res = unescape_string(tmp, param); - if (!res) { - mp_err(log, "Parameter has broken escapes: %.*s\n", BSTR_P(param)); - r = M_OPT_INVALID; - goto exit; - } - param = bstr0(res); + return r; } if ((opt->flags & M_OPT_MIN) && (param.len < opt->min)) { mp_err(log, "Parameter must be >= %d chars: %.*s\n", (int) opt->min, BSTR_P(param)); - r = M_OPT_OUT_OF_RANGE; - goto exit; + return M_OPT_OUT_OF_RANGE; } if ((opt->flags & M_OPT_MAX) && (param.len > opt->max)) { mp_err(log, "Parameter must be <= %d chars: %.*s\n", (int) opt->max, BSTR_P(param)); - r = M_OPT_OUT_OF_RANGE; - goto exit; + return M_OPT_OUT_OF_RANGE; } if (dst) { @@ -833,16 +786,12 @@ static int parse_str(struct mp_log *log, const m_option_t *opt, VAL(dst) = bstrdup0(NULL, param); } -exit: - talloc_free(tmp); - return r; + return 0; } static char *print_str(const m_option_t *opt, const void *val) { - bool need_escape = opt->flags & M_OPT_PARSE_ESCAPES; - char *s = val ? VAL(val) : NULL; - return s ? (need_escape ? escape_string(s) : talloc_strdup(NULL, s)) : NULL; + return talloc_strdup(NULL, val ? VAL(val) : NULL); } static void copy_str(const m_option_t *opt, void *dst, const void *src) diff --git a/options/m_option.h b/options/m_option.h index 9178231b2b..fc46599483 100644 --- a/options/m_option.h +++ b/options/m_option.h @@ -338,9 +338,6 @@ struct m_option { // See M_OPT_TYPE_OPTIONAL_PARAM. #define M_OPT_OPTIONAL_PARAM (1 << 10) -// Parse C-style escapes like "\n" (for CONF_TYPE_STRING only) -#define M_OPT_PARSE_ESCAPES (1 << 11) - // These are kept for compatibility with older code. #define CONF_MIN M_OPT_MIN #define CONF_MAX M_OPT_MAX diff --git a/options/options.c b/options/options.c index 700415a443..4bac77c7b7 100644 --- a/options/options.c +++ b/options/options.c @@ -602,9 +602,9 @@ const m_option_t mp_opts[] = { OPT_FLAG("term-osd-bar", term_osd_bar, 0), OPT_STRING("term-osd-bar-chars", term_osd_bar_chars, 0), - OPT_STRING("playing-msg", playing_msg, M_OPT_PARSE_ESCAPES), - OPT_STRING("status-msg", status_msg, M_OPT_PARSE_ESCAPES), - OPT_STRING("osd-status-msg", osd_status_msg, M_OPT_PARSE_ESCAPES), + OPT_STRING("playing-msg", playing_msg, 0), + OPT_STRING("status-msg", status_msg, 0), + OPT_STRING("osd-status-msg", osd_status_msg, 0), OPT_FLAG("slave-broken", slave_mode, CONF_GLOBAL), OPT_FLAG("idle", player_idle_mode, CONF_GLOBAL), -- cgit v1.2.3