struct bstr bstr_strip_linebreaks(struct bstr str) { if (bstr_endswith0(str, "\r\n")) { str = bstr_splice(str, 0, str.len - 2); } else if (bstr_endswith0(str, "\n")) { str = bstr_splice(str, 0, str.len - 1); } return str; }
static int parse_pls(struct pl_parser *p) { bstr line = {0}; while (!line.len && !pl_eof(p)) line = bstr_strip(pl_get_line(p)); if (bstrcasecmp0(line, "[playlist]") != 0) return -1; if (p->probing) return 0; while (!pl_eof(p)) { line = bstr_strip(pl_get_line(p)); bstr key, value; if (bstr_split_tok(line, "=", &key, &value) && bstr_case_startswith(key, bstr0("File"))) { value = bstr_strip(value); if (bstr_startswith0(value, "\"") && bstr_endswith0(value, "\"")) value = bstr_splice(value, 1, -1); pl_add(p, value); } } return 0; }
// Returns 0 if a valid option/file is available, <0 on error, 1 on end of args. static int split_opt_silent(struct parse_state *p) { assert(!p->error); if (p->argc < 1) return 1; p->mp_opt = NULL; p->arg = bstr0(p->argv[0]); p->param = bstr0(NULL); p->argc--; p->argv++; if (p->no_more_opts || !bstr_startswith0(p->arg, "-") || p->arg.len == 1) return 0; if (bstrcmp0(p->arg, "--") == 0) { p->no_more_opts = true; return split_opt_silent(p); } bool old_syntax = !bstr_startswith0(p->arg, "--"); if (old_syntax) { p->arg = bstr_cut(p->arg, 1); } else { p->arg = bstr_cut(p->arg, 2); int idx = bstrchr(p->arg, '='); if (idx > 0) { p->param = bstr_cut(p->arg, idx + 1); p->arg = bstr_splice(p->arg, 0, idx); } } p->mp_opt = m_config_get_option(p->config, p->arg); if (!p->mp_opt) { // Automagic "no-" arguments: "--no-bla" turns into "--bla=no". if (!bstr_startswith0(p->arg, "no-")) return -1; struct bstr s = bstr_cut(p->arg, 3); p->mp_opt = m_config_get_option(p->config, s); if (!p->mp_opt || p->mp_opt->type != &m_option_type_flag) return -1; // Avoid allowing "--no-no-bla". if (bstr_startswith(bstr0(p->mp_opt->name), bstr0("no-"))) return -1; // Flag options never have parameters. old_syntax = false; if (p->param.len) return -2; p->arg = s; p->param = bstr0("no"); } if (bstr_endswith0(p->arg, "-clr")) old_syntax = false; if (old_syntax && !(p->mp_opt->type->flags & M_OPT_TYPE_OLD_SYNTAX_NO_PARAM)) { if (p->argc < 1) return -3; p->param = bstr0(p->argv[0]); p->argc--; p->argv++; } return 0; }