1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
|
/*
* This file is part of MPlayer.
*
* MPlayer is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* MPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/// \file
/// \ingroup Config
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include "libmpv/client.h"
#include "talloc.h"
#include "m_config.h"
#include "options/m_option.h"
#include "common/msg.h"
static const union m_option_value default_value;
static const char *replaced_opts;
// Profiles allow to predefine some sets of options that can then
// be applied later on with the internal -profile option.
#define MAX_PROFILE_DEPTH 20
struct m_profile {
struct m_profile *next;
char *name;
char *desc;
int num_opts;
// Option/value pair array.
char **opts;
};
// In the file local case, this contains the old global value.
struct m_opt_backup {
struct m_opt_backup *next;
struct m_config_option *co;
void *backup;
};
static int parse_include(struct m_config *config, struct bstr param, bool set,
int flags)
{
if (param.len == 0)
return M_OPT_MISSING_PARAM;
if (!set)
return 1;
char *filename = bstrdup0(NULL, param);
config->includefunc(config->includefunc_ctx, filename, flags);
talloc_free(filename);
return 1;
}
static int parse_profile(struct m_config *config, const struct m_option *opt,
struct bstr name, struct bstr param, bool set, int flags)
{
if (!bstrcmp0(param, "help")) {
struct m_profile *p;
if (!config->profiles) {
MP_INFO(config, "No profiles have been defined.\n");
return M_OPT_EXIT - 1;
}
MP_INFO(config, "Available profiles:\n");
for (p = config->profiles; p; p = p->next)
MP_INFO(config, "\t%s\t%s\n", p->name, p->desc ? p->desc : "");
MP_INFO(config, "\n");
return M_OPT_EXIT - 1;
}
char **list = NULL;
int r = m_option_type_string_list.parse(config->log, opt, name, param, &list);
if (r < 0)
return r;
if (!list || !list[0])
return M_OPT_INVALID;
for (int i = 0; list[i]; i++) {
struct m_profile *p = m_config_get_profile0(config, list[i]);
if (!p) {
MP_WARN(config, "Unknown profile '%s'.\n", list[i]);
r = M_OPT_INVALID;
} else if (set)
m_config_set_profile(config, p, flags);
}
m_option_free(opt, &list);
return r;
}
static int show_profile(struct m_config *config, bstr param)
{
struct m_profile *p;
int i, j;
if (!param.len)
return M_OPT_MISSING_PARAM;
if (!(p = m_config_get_profile(config, param))) {
MP_ERR(config, "Unknown profile '%.*s'.\n", BSTR_P(param));
return M_OPT_EXIT - 1;
}
if (!config->profile_depth)
MP_INFO(config, "Profile %s: %s\n", p->name,
p->desc ? p->desc : "");
config->profile_depth++;
for (i = 0; i < p->num_opts; i++) {
char spc[config->profile_depth + 1];
for (j = 0; j < config->profile_depth; j++)
spc[j] = ' ';
spc[config->profile_depth] = '\0';
MP_INFO(config, "%s%s=%s\n", spc, p->opts[2 * i], p->opts[2 * i + 1]);
if (config->profile_depth < MAX_PROFILE_DEPTH
&& !strcmp(p->opts[2*i], "profile")) {
char *e, *list = p->opts[2 * i + 1];
while ((e = strchr(list, ','))) {
int l = e - list;
char tmp[l+1];
if (!l)
continue;
memcpy(tmp, list, l);
tmp[l] = '\0';
show_profile(config, bstr0(tmp));
list = e + 1;
}
if (list[0] != '\0')
show_profile(config, bstr0(list));
}
}
config->profile_depth--;
if (!config->profile_depth)
MP_INFO(config, "\n");
return M_OPT_EXIT - 1;
}
static int list_options(struct m_config *config)
{
m_config_print_option_list(config);
return M_OPT_EXIT;
}
// The memcpys are supposed to work around the strict aliasing violation,
// that would result if we just dereferenced a void** (where the void** is
// actually casted from struct some_type* ).
static void *substruct_read_ptr(const void *ptr)
{
void *res;
memcpy(&res, ptr, sizeof(void*));
return res;
}
static void substruct_write_ptr(void *ptr, void *val)
{
memcpy(ptr, &val, sizeof(void*));
}
static void add_options(struct m_config *config,
const char *parent_name,
void *optstruct,
const void *optstruct_def,
const struct m_option *defs);
static void config_destroy(void *p)
{
struct m_config *config = p;
m_config_restore_backups(config);
for (int n = 0; n < config->num_opts; n++)
m_option_free(config->opts[n].opt, config->opts[n].data);
}
struct m_config *m_config_new(void *talloc_ctx, struct mp_log *log,
size_t size, const void *defaults,
const struct m_option *options)
{
struct m_config *config = talloc(talloc_ctx, struct m_config);
talloc_set_destructor(config, config_destroy);
*config = (struct m_config) {.log = log};
// size==0 means a dummy object is created
if (size) {
config->optstruct = talloc_zero_size(config, size);
if (defaults)
memcpy(config->optstruct, defaults, size);
}
if (options)
add_options(config, "", config->optstruct, defaults, options);
return config;
}
struct m_config *m_config_from_obj_desc(void *talloc_ctx, struct mp_log *log,
struct m_obj_desc *desc)
{
return m_config_new(talloc_ctx, log, desc->priv_size, desc->priv_defaults,
desc->options);
}
// Like m_config_from_obj_desc(), but don't allocate option struct.
struct m_config *m_config_from_obj_desc_noalloc(void *talloc_ctx,
struct mp_log *log,
struct m_obj_desc *desc)
{
return m_config_new(talloc_ctx, log, 0, desc->priv_defaults, desc->options);
}
int m_config_set_obj_params(struct m_config *conf, char **args)
{
for (int n = 0; args && args[n * 2 + 0]; n++) {
int r = m_config_set_option(conf, bstr0(args[n * 2 + 0]),
bstr0(args[n * 2 + 1]));
if (r < 0)
return r;
}
return 0;
}
int m_config_apply_defaults(struct m_config *config, const char *name,
struct m_obj_settings *defaults)
{
int r = 0;
for (int n = 0; defaults && defaults[n].name; n++) {
struct m_obj_settings *entry = &defaults[n];
if (name && strcmp(entry->name, name) == 0) {
r = m_config_set_obj_params(config, entry->attribs);
break;
}
}
return r;
}
static void ensure_backup(struct m_config *config, struct m_config_option *co)
{
if (co->opt->type->flags & M_OPT_TYPE_HAS_CHILD)
return;
if (co->opt->flags & M_OPT_GLOBAL)
return;
if (!co->data)
return;
for (struct m_opt_backup *cur = config->backup_opts; cur; cur = cur->next) {
if (cur->co->data == co->data) // comparing data ptr catches aliases
return;
}
struct m_opt_backup *bc = talloc_ptrtype(NULL, bc);
*bc = (struct m_opt_backup) {
.co = co,
.backup = talloc_zero_size(bc, co->opt->type->size),
};
m_option_copy(co->opt, bc->backup, co->data);
bc->next = config->backup_opts;
config->backup_opts = bc;
}
void m_config_restore_backups(struct m_config *config)
{
while (config->backup_opts) {
struct m_opt_backup *bc = config->backup_opts;
config->backup_opts = bc->next;
m_option_copy(bc->co->opt, bc->co->data, bc->backup);
m_option_free(bc->co->opt, bc->backup);
talloc_free(bc);
}
}
void m_config_backup_opt(struct m_config *config, const char *opt)
{
struct m_config_option *co = m_config_get_co(config, bstr0(opt));
if (co) {
ensure_backup(config, co);
} else {
MP_ERR(config, "Option %s not found.\n", opt);
}
}
void m_config_backup_all_opts(struct m_config *config)
{
for (int n = 0; n < config->num_opts; n++)
ensure_backup(config, &config->opts[n]);
}
// Given an option --opt, add --no-opt (if applicable).
static void add_negation_option(struct m_config *config,
struct m_config_option *orig,
const char *parent_name)
{
const struct m_option *opt = orig->opt;
int value;
if (opt->type == CONF_TYPE_FLAG) {
value = opt->min;
} else if (opt->type == CONF_TYPE_CHOICE) {
// Find out whether there's a "no" choice.
// m_option_parse() should be used for this, but it prints
// unsilenceable error messages.
struct m_opt_choice_alternatives *alt = opt->priv;
for ( ; alt->name; alt++) {
if (strcmp(alt->name, "no") == 0)
break;
}
if (!alt->name)
return;
value = alt->value;
} else {
return;
}
struct m_option *no_opt = talloc_ptrtype(config, no_opt);
*no_opt = (struct m_option) {
.name = opt->name,
.type = CONF_TYPE_STORE,
.flags = opt->flags & (M_OPT_NOCFG | M_OPT_GLOBAL | M_OPT_PRE_PARSE),
.is_new_option = opt->is_new_option,
.p = opt->p,
.offset = opt->offset,
.max = value,
};
// Add --no-sub-opt
struct m_config_option co = *orig;
co.name = talloc_asprintf(config, "no-%s", orig->name);
co.opt = no_opt;
co.is_generated = true;
MP_TARRAY_APPEND(config, config->opts, config->num_opts, co);
// Add --sub-no-opt (unfortunately needed for: "--sub=...:no-opt")
if (parent_name[0]) {
co.name = talloc_asprintf(config, "%s-no-%s", parent_name, opt->name);
MP_TARRAY_APPEND(config, config->opts, config->num_opts, co);
}
}
static void m_config_add_option(struct m_config *config,
const char *parent_name,
void *optstruct,
const void *optstruct_def,
const struct m_option *arg);
static void add_options(struct m_config *config,
const char *parent_name,
void *optstruct,
const void *optstruct_def,
const struct m_option *defs)
{
for (int i = 0; defs && defs[i].name; i++)
m_config_add_option(config, parent_name, optstruct, optstruct_def, &defs[i]);
}
static void m_config_add_option(struct m_config *config,
const char *parent_name,
void *optstruct,
const void *optstruct_def,
const struct m_option *arg)
{
assert(config != NULL);
assert(arg != NULL);
struct m_config_option co = {
.opt = arg,
.name = arg->name,
};
if (arg->is_new_option) {
if (optstruct)
co.data = (char *)optstruct + arg->offset;
if (optstruct_def)
co.default_data = (char *)optstruct_def + arg->offset;
} else {
co.data = arg->p;
co.default_data = arg->p;
}
if (arg->defval)
co.default_data = arg->defval;
if (!co.default_data)
co.default_data = &default_value;
// Fill in the full name
if (!co.name[0]) {
co.name = parent_name;
} else if (parent_name[0]) {
co.name = talloc_asprintf(config, "%s-%s", parent_name, co.name);
}
// Option with children -> add them
if (arg->type->flags & M_OPT_TYPE_HAS_CHILD) {
if (arg->type->flags & M_OPT_TYPE_USE_SUBSTRUCT) {
const struct m_sub_options *subopts = arg->priv;
void *new_optstruct = NULL;
if (co.data) {
new_optstruct = m_config_alloc_struct(config, subopts);
substruct_write_ptr(co.data, new_optstruct);
}
const void *new_optstruct_def = substruct_read_ptr(co.default_data);
if (!new_optstruct_def)
new_optstruct_def = subopts->defaults;
add_options(config, co.name, new_optstruct,
new_optstruct_def, subopts->opts);
} else {
const struct m_option *sub = arg->p;
add_options(config, co.name, optstruct, optstruct_def, sub);
}
} else {
// Initialize options
if (co.data && co.default_data) {
if (arg->type->flags & M_OPT_TYPE_DYNAMIC) {
// Would leak memory by overwriting *co.data repeatedly.
for (int i = 0; i < config->num_opts; i++) {
if (co.data == config->opts[i].data)
assert(0);
}
}
// In case this is dynamic data, it has to be allocated and copied.
union m_option_value temp = {0};
memcpy(&temp, co.default_data, arg->type->size);
memset(co.data, 0, arg->type->size);
m_option_copy(arg, co.data, &temp);
}
}
if (arg->name[0]) // no own name -> hidden
MP_TARRAY_APPEND(config, config->opts, config->num_opts, co);
add_negation_option(config, &co, parent_name);
}
struct m_config_option *m_config_get_co(const struct m_config *config,
struct bstr name)
{
for (int n = 0; n < config->num_opts; n++) {
struct m_config_option *co = &config->opts[n];
struct bstr coname = bstr0(co->name);
if ((co->opt->type->flags & M_OPT_TYPE_ALLOW_WILDCARD)
&& bstr_endswith0(coname, "*")) {
coname.len--;
if (bstrcmp(bstr_splice(name, 0, coname.len), coname) == 0)
return co;
} else if (bstrcmp(coname, name) == 0)
return co;
}
return NULL;
}
const char *m_config_get_positional_option(const struct m_config *config, int p)
{
int pos = 0;
for (int n = 0; n < config->num_opts; n++) {
struct m_config_option *co = &config->opts[n];
if (!co->is_generated) {
if (pos == p)
return co->name;
pos++;
}
}
return NULL;
}
// return: <0: M_OPT_ error, 0: skip, 1: check, 2: set
static int handle_set_opt_flags(struct m_config *config,
struct m_config_option *co, int flags)
{
int optflags = co->opt->flags;
bool set = !(flags & M_SETOPT_CHECK_ONLY);
if ((flags & M_SETOPT_PRE_PARSE_ONLY) && !(optflags & M_OPT_PRE_PARSE))
return 0;
if ((flags & M_SETOPT_PRESERVE_CMDLINE) && co->is_set_from_cmdline)
set = false;
if ((flags & M_SETOPT_NO_FIXED) && (optflags & M_OPT_FIXED))
return M_OPT_INVALID;
if ((flags & M_SETOPT_NO_PRE_PARSE) && (optflags & M_OPT_PRE_PARSE))
return M_OPT_INVALID;
// Check if this option isn't forbidden in the current mode
if ((flags & M_SETOPT_FROM_CONFIG_FILE) && (optflags & M_OPT_NOCFG)) {
MP_ERR(config, "The %s option can't be used in a config file.\n",
co->name);
return M_OPT_INVALID;
}
if (flags & M_SETOPT_BACKUP) {
if (optflags & M_OPT_GLOBAL) {
MP_ERR(config, "The %s option is global and can't be set per-file.\n",
co->name);
return M_OPT_INVALID;
}
if (set)
ensure_backup(config, co);
}
return set ? 2 : 1;
}
static void handle_set_from_cmdline(struct m_config *config,
struct m_config_option *co)
{
co->is_set_from_cmdline = true;
// Mark aliases too
if (co->data) {
for (int n = 0; n < config->num_opts; n++) {
struct m_config_option *co2 = &config->opts[n];
if (co2->data == co->data)
co2->is_set_from_cmdline = true;
}
}
}
// The type data points to is as in: m_config_get_co(config, name)->opt
int m_config_set_option_raw(struct m_config *config, struct m_config_option *co,
void *data, int flags)
{
if (!co)
return M_OPT_UNKNOWN;
// This affects some special options like "include", "profile". Maybe these
// should work, or maybe not. For now they would require special code.
if (!co->data)
return M_OPT_UNKNOWN;
int r = handle_set_opt_flags(config, co, flags);
if (r <= 1)
return r;
m_option_copy(co->opt, co->data, data);
if (flags & M_SETOPT_FROM_CMDLINE)
handle_set_from_cmdline(config, co);
return 0;
}
static int parse_subopts(struct m_config *config, char *name, char *prefix,
struct bstr param, int flags);
static int m_config_parse_option(struct m_config *config, struct bstr name,
struct bstr param, int flags)
{
assert(config != NULL);
assert(name.len != 0);
struct m_config_option *co = m_config_get_co(config, name);
if (!co) {
char s[80];
snprintf(s, sizeof(s), "|%.*s#", BSTR_P(name));
char *msg = strstr(replaced_opts, s);
if (msg) {
msg += strlen(s);
char *end = strchr(msg, '|');
MP_FATAL(config, "The --%.*s option was renamed or replaced: %.*s\n",
BSTR_P(name), (int)(end - msg), msg);
}
return M_OPT_UNKNOWN;
}
// This is the only mandatory function
assert(co->opt->type->parse);
int r = handle_set_opt_flags(config, co, flags);
if (r <= 0)
return r;
bool set = r == 2;
if (set) {
MP_VERBOSE(config, "Setting option '%.*s' = '%.*s' (flags = %d)\n",
BSTR_P(name), BSTR_P(param), flags);
}
if (config->includefunc && bstr_equals0(name, "include"))
return parse_include(config, param, set, flags);
if (config->use_profiles && bstr_equals0(name, "profile"))
return parse_profile(config, co->opt, name, param, set, flags);
if (config->use_profiles && bstr_equals0(name, "show-profile"))
return show_profile(config, param);
if (bstr_equals0(name, "list-options"))
return list_options(config);
// Option with children are a bit different to parse
if (co->opt->type->flags & M_OPT_TYPE_HAS_CHILD) {
char prefix[110];
assert(strlen(co->name) < 100);
sprintf(prefix, "%s-", co->name);
return parse_subopts(config, (char *)co->name, prefix, param, flags);
}
r = m_option_parse(config->log, co->opt, name, param, set ? co->data : NULL);
if (r >= 0 && set && (flags & M_SETOPT_FROM_CMDLINE))
handle_set_from_cmdline(config, co);
return r;
}
static int parse_subopts(struct m_config *config, char *name, char *prefix,
struct bstr param, int flags)
{
char **lst = NULL;
// Split the argument into child options
int r = m_option_type_subconfig.parse(config->log, NULL, bstr0(""), param, &lst);
if (r < 0)
return r;
// Parse the child options
for (int i = 0; lst && lst[2 * i]; i++) {
// Build the full name
char n[110];
if (snprintf(n, 110, "%s%s", prefix, lst[2 * i]) > 100)
abort();
r = m_config_parse_option(config,bstr0(n), bstr0(lst[2 * i + 1]), flags);
if (r < 0) {
if (r > M_OPT_EXIT) {
MP_ERR(config, "Error parsing suboption %s/%s (%s)\n",
name, lst[2 * i], m_option_strerror(r));
r = M_OPT_INVALID;
}
break;
}
}
talloc_free(lst);
return r;
}
int m_config_parse_suboptions(struct m_config *config, char *name,
char *subopts)
{
if (!subopts || !*subopts)
return 0;
int r = parse_subopts(config, name, "", bstr0(subopts), 0);
if (r < 0 && r > M_OPT_EXIT) {
MP_ERR(config, "Error parsing suboption %s (%s)\n",
name, m_option_strerror(r));
r = M_OPT_INVALID;
}
return r;
}
int m_config_set_option_ext(struct m_config *config, struct bstr name,
struct bstr param, int flags)
{
int r = m_config_parse_option(config, name, param, flags);
if (r < 0 && r > M_OPT_EXIT) {
MP_ERR(config, "Error parsing option %.*s (%s)\n",
BSTR_P(name), m_option_strerror(r));
r = M_OPT_INVALID;
}
return r;
}
int m_config_set_option(struct m_config *config, struct bstr name,
struct bstr param)
{
return m_config_set_option_ext(config, name, param, 0);
}
int m_config_set_option_node(struct m_config *config, bstr name,
struct mpv_node *data, int flags)
{
struct m_config_option *co = m_config_get_co(config, name);
if (!co)
return M_OPT_UNKNOWN;
int r;
// Do this on an "empty" type to make setting the option strictly overwrite
// the old value, as opposed to e.g. appending to lists.
union m_option_value val = {0};
if (data->format == MPV_FORMAT_STRING) {
bstr param = bstr0(data->u.string);
r = m_option_parse(mp_null_log, co->opt, name, param, &val);
} else {
r = m_option_set_node(co->opt, &val, data);
}
if (r >= 0)
r = m_config_set_option_raw(config, co, &val, flags);
m_option_free(co->opt, &val);
return r;
}
const struct m_option *m_config_get_option(const struct m_config *config,
struct bstr name)
{
assert(config != NULL);
struct m_config_option *co = m_config_get_co(config, name);
return co ? co->opt : NULL;
}
int m_config_option_requires_param(struct m_config *config, bstr name)
{
const struct m_option *opt = m_config_get_option(config, name);
if (opt) {
if (bstr_endswith0(name, "-clr"))
return 0;
return m_option_required_params(opt);
}
return M_OPT_UNKNOWN;
}
static int sort_opt_compare(const void *pa, const void *pb)
{
const struct m_config_option *a = pa;
const struct m_config_option *b = pb;
return strcasecmp(a->name, b->name);
}
void m_config_print_option_list(const struct m_config *config)
{
char min[50], max[50];
int count = 0;
const char *prefix = config->is_toplevel ? "--" : "";
struct m_config_option *sorted =
talloc_memdup(NULL, config->opts, config->num_opts * sizeof(sorted[0]));
if (config->is_toplevel)
qsort(sorted, config->num_opts, sizeof(sorted[0]), sort_opt_compare);
MP_INFO(config, "Options:\n\n");
for (int i = 0; i < config->num_opts; i++) {
struct m_config_option *co = &sorted[i];
const struct m_option *opt = co->opt;
if (opt->type->flags & M_OPT_TYPE_HAS_CHILD)
continue;
if (co->is_generated)
continue;
MP_INFO(config, " %s%-30.30s", prefix, co->name);
if (opt->type == &m_option_type_choice) {
MP_INFO(config, " Choices:");
struct m_opt_choice_alternatives *alt = opt->priv;
for (int n = 0; alt[n].name; n++)
MP_INFO(config, " %s", alt[n].name);
if (opt->flags & (M_OPT_MIN | M_OPT_MAX))
MP_INFO(config, " (or an integer)");
} else {
MP_INFO(config, " %s", co->opt->type->name);
}
if (opt->flags & (M_OPT_MIN | M_OPT_MAX)) {
snprintf(min, sizeof(min), "any");
snprintf(max, sizeof(max), "any");
if (opt->flags & M_OPT_MIN)
snprintf(min, sizeof(min), "%.14g", opt->min);
if (opt->flags & M_OPT_MAX)
snprintf(max, sizeof(max), "%.14g", opt->max);
MP_INFO(config, " (%s to %s)", min, max);
}
char *def = NULL;
if (co->default_data)
def = m_option_print(co->opt, co->default_data);
if (def) {
MP_INFO(config, " (default: %s)", def);
talloc_free(def);
}
if (opt->flags & M_OPT_GLOBAL)
MP_INFO(config, " [global]");
if (opt->flags & M_OPT_NOCFG)
MP_INFO(config, " [nocfg]");
MP_INFO(config, "\n");
count++;
}
MP_INFO(config, "\nTotal: %d options\n", count);
talloc_free(sorted);
}
char **m_config_list_options(void *ta_parent, const struct m_config *config)
{
char **list = talloc_new(ta_parent);
int count = 0;
for (int i = 0; i < config->num_opts; i++) {
struct m_config_option *co = &config->opts[i];
const struct m_option *opt = co->opt;
if (opt->type->flags & M_OPT_TYPE_HAS_CHILD)
continue;
if (co->is_generated)
continue;
// For use with CONF_TYPE_STRING_LIST, it's important not to set list
// as allocation parent.
char *s = talloc_strdup(ta_parent, co->name);
MP_TARRAY_APPEND(ta_parent, list, count, s);
}
MP_TARRAY_APPEND(ta_parent, list, count, NULL);
return list;
}
struct m_profile *m_config_get_profile(const struct m_config *config, bstr name)
{
for (struct m_profile *p = config->profiles; p; p = p->next) {
if (bstr_equals0(name, p->name))
return p;
}
return NULL;
}
struct m_profile *m_config_get_profile0(const struct m_config *config,
char *name)
{
return m_config_get_profile(config, bstr0(name));
}
struct m_profile *m_config_add_profile(struct m_config *config, char *name)
{
if (!name || !name[0] || strcmp(name, "default") == 0)
return NULL; // never a real profile
struct m_profile *p = m_config_get_profile0(config, name);
if (p)
return p;
p = talloc_zero(config, struct m_profile);
p->name = talloc_strdup(p, name);
p->next = config->profiles;
config->profiles = p;
return p;
}
void m_profile_set_desc(struct m_profile *p, bstr desc)
{
talloc_free(p->desc);
p->desc = bstrdup0(p, desc);
}
int m_config_set_profile_option(struct m_config *config, struct m_profile *p,
bstr name, bstr val)
{
int i = m_config_set_option_ext(config, name, val,
|