Fix crash with partial fribidi conversion.
[mplayer/greg.git] / m_option.c
blobdb45e3c242ffe5c6ce87fa229721cb0e0d8f9a33
2 /// \file
3 /// \ingroup Options
5 #include "config.h"
7 #include <stdlib.h>
8 #include <string.h>
9 #include <math.h>
10 #include <stdio.h>
11 #include <stdarg.h>
12 #include <inttypes.h>
13 #include <unistd.h>
15 #include "m_option.h"
16 //#include "m_config.h"
17 #include "mp_msg.h"
18 #include "stream/url.h"
19 #include "libavutil/avstring.h"
21 // Don't free for 'production' atm
22 #ifndef MP_DEBUG
23 //#define NO_FREE
24 #endif
26 const m_option_t* m_option_list_find(const m_option_t* list,const char* name) {
27 int i;
29 for(i = 0 ; list[i].name ; i++) {
30 int l = strlen(list[i].name) - 1;
31 if((list[i].type->flags & M_OPT_TYPE_ALLOW_WILDCARD) &&
32 (l > 0) && (list[i].name[l] == '*')) {
33 if(strncasecmp(list[i].name,name,l) == 0)
34 return &list[i];
35 } else if(strcasecmp(list[i].name,name) == 0)
36 return &list[i];
38 return NULL;
41 // Default function that just does a memcpy
43 static void copy_opt(const m_option_t* opt,void* dst,void* src) {
44 if(dst && src)
45 memcpy(dst,src,opt->type->size);
48 // Helper for the print funcs (from man printf)
49 static char* dup_printf(const char *fmt, ...) {
50 /* Guess we need no more than 50 bytes. */
51 int n, size = 50;
52 char *p;
53 va_list ap;
54 if ((p = malloc (size)) == NULL)
55 return NULL;
56 while (1) {
57 /* Try to print in the allocated space. */
58 va_start(ap, fmt);
59 n = vsnprintf (p, size, fmt, ap);
60 va_end(ap);
61 /* If that worked, return the string. */
62 if (n > -1 && n < size)
63 return p;
64 /* Else try again with more space. */
65 if (n > -1) /* glibc 2.1 */
66 size = n+1; /* precisely what is needed */
67 else /* glibc 2.0 */
68 size *= 2; /* twice the old size */
69 if ((p = realloc (p, size)) == NULL)
70 return NULL;
75 // Flag
77 #define VAL(x) (*(int*)(x))
79 static int parse_flag(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
80 if (src == M_CONFIG_FILE) {
81 if(!param) return M_OPT_MISSING_PARAM;
82 if (!strcasecmp(param, "yes") || /* any other language? */
83 !strcasecmp(param, "on") ||
84 !strcasecmp(param, "ja") ||
85 !strcasecmp(param, "si") ||
86 !strcasecmp(param, "igen") ||
87 !strcasecmp(param, "y") ||
88 !strcasecmp(param, "j") ||
89 !strcasecmp(param, "i") ||
90 !strcasecmp(param, "tak") ||
91 !strcasecmp(param, "ja") ||
92 !strcasecmp(param, "true") ||
93 !strcmp(param, "1")) {
94 if(dst) VAL(dst) = opt->max;
95 } else if (!strcasecmp(param, "no") ||
96 !strcasecmp(param, "off") ||
97 !strcasecmp(param, "nein") ||
98 !strcasecmp(param, "nicht") ||
99 !strcasecmp(param, "nem") ||
100 !strcasecmp(param, "n") ||
101 !strcasecmp(param, "nie") ||
102 !strcasecmp(param, "nej") ||
103 !strcasecmp(param, "false") ||
104 !strcmp(param, "0")) {
105 if(dst) VAL(dst) = opt->min;
106 } else {
107 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Invalid parameter for %s flag: %s\n",name, param);
108 return M_OPT_INVALID;
110 return 1;
111 } else {
112 if(dst) VAL(dst) = opt->max;
113 return 0;
117 static char* print_flag(const m_option_t* opt, const void* val) {
118 if(VAL(val) == opt->min)
119 return strdup("no");
120 else
121 return strdup("yes");
124 const m_option_type_t m_option_type_flag = {
125 "Flag",
126 "need yes or no in config files",
127 sizeof(int),
129 parse_flag,
130 print_flag,
131 copy_opt,
132 copy_opt,
133 NULL,
134 NULL
137 // Integer
139 static int parse_int(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
140 long long tmp_int;
141 char *endptr;
142 src = 0;
144 if (param == NULL)
145 return M_OPT_MISSING_PARAM;
147 tmp_int = strtoll(param, &endptr, 10);
148 if (*endptr)
149 tmp_int = strtoll(param, &endptr, 0);
150 if (*endptr) {
151 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "The %s option must be an integer: %s\n",name, param);
152 return M_OPT_INVALID;
155 if ((opt->flags & M_OPT_MIN) && (tmp_int < opt->min)) {
156 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "The %s option must be >= %d: %s\n", name, (int) opt->min, param);
157 return M_OPT_OUT_OF_RANGE;
160 if ((opt->flags & M_OPT_MAX) && (tmp_int > opt->max)) {
161 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "The %s option must be <= %d: %s\n",name, (int) opt->max, param);
162 return M_OPT_OUT_OF_RANGE;
165 if(dst) {
166 if (opt->type->size == sizeof(int64_t))
167 *(int64_t *)dst = tmp_int;
168 else
169 VAL(dst) = tmp_int;
172 return 1;
175 static char* print_int(const m_option_t* opt, const void* val) {
176 if (opt->type->size == sizeof(int64_t))
177 return dup_printf("%"PRId64, *(const int64_t *)val);
178 return dup_printf("%d",VAL(val));
181 const m_option_type_t m_option_type_int = {
182 "Integer",
184 sizeof(int),
186 parse_int,
187 print_int,
188 copy_opt,
189 copy_opt,
190 NULL,
191 NULL
194 const m_option_type_t m_option_type_int64 = {
195 "Integer64",
197 sizeof(int64_t),
199 parse_int,
200 print_int,
201 copy_opt,
202 copy_opt,
203 NULL,
204 NULL
207 // Float
209 #undef VAL
210 #define VAL(x) (*(double*)(x))
212 static int parse_double(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
213 double tmp_float;
214 char* endptr;
215 src = 0;
217 if (param == NULL)
218 return M_OPT_MISSING_PARAM;
220 tmp_float = strtod(param, &endptr);
222 switch(*endptr) {
223 case ':':
224 case '/':
225 tmp_float /= strtod(endptr+1, &endptr);
226 break;
227 case '.':
228 case ',':
229 /* we also handle floats specified with
230 * non-locale decimal point ::atmos
232 if(tmp_float<0)
233 tmp_float -= 1.0/pow(10,strlen(endptr+1)) * strtod(endptr+1, &endptr);
234 else
235 tmp_float += 1.0/pow(10,strlen(endptr+1)) * strtod(endptr+1, &endptr);
236 break;
239 if (*endptr) {
240 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "The %s option must be a floating point "
241 "number or a ratio (numerator[:/]denominator): %s\n",name, param);
242 return M_OPT_INVALID;
245 if (opt->flags & M_OPT_MIN)
246 if (tmp_float < opt->min) {
247 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "The %s option must be >= %f: %s\n", name, opt->min, param);
248 return M_OPT_OUT_OF_RANGE;
251 if (opt->flags & M_OPT_MAX)
252 if (tmp_float > opt->max) {
253 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "The %s option must be <= %f: %s\n", name, opt->max, param);
254 return M_OPT_OUT_OF_RANGE;
257 if(dst) VAL(dst) = tmp_float;
258 return 1;
261 static char* print_double(const m_option_t* opt, const void* val) {
262 opt = NULL;
263 return dup_printf("%f",VAL(val));
266 const m_option_type_t m_option_type_double = {
267 "Double",
268 "double precission floating point number or ratio (numerator[:/]denominator)",
269 sizeof(double),
271 parse_double,
272 print_double,
273 copy_opt,
274 copy_opt,
275 NULL,
276 NULL
279 #undef VAL
280 #define VAL(x) (*(float*)(x))
282 static int parse_float(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
283 double tmp;
284 int r= parse_double(opt, name, param, &tmp, src);
285 if(r==1 && dst) VAL(dst) = tmp;
286 return r;
289 static char* print_float(const m_option_t* opt, const void* val) {
290 opt = NULL;
291 return dup_printf("%f",VAL(val));
294 const m_option_type_t m_option_type_float = {
295 "Float",
296 "floating point number or ratio (numerator[:/]denominator)",
297 sizeof(float),
299 parse_float,
300 print_float,
301 copy_opt,
302 copy_opt,
303 NULL,
304 NULL
307 ///////////// Position
308 #undef VAL
309 #define VAL(x) (*(off_t*)(x))
311 static int parse_position(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
312 off_t tmp_off;
313 char dummy;
315 if (param == NULL)
316 return M_OPT_MISSING_PARAM;
317 if (sscanf(param, sizeof(off_t) == sizeof(int) ?
318 "%d%c" : "%"PRId64"%c", &tmp_off, &dummy) != 1) {
319 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "The %s option must be an integer: %s\n",opt->name,param);
320 return M_OPT_INVALID;
323 if (opt->flags & M_OPT_MIN)
324 if (tmp_off < opt->min) {
325 mp_msg(MSGT_CFGPARSER, MSGL_ERR,
326 "The %s option must be >= %"PRId64": %s\n",
327 name, (int64_t) opt->min, param);
328 return M_OPT_OUT_OF_RANGE;
331 if (opt->flags & M_OPT_MAX)
332 if (tmp_off > opt->max) {
333 mp_msg(MSGT_CFGPARSER, MSGL_ERR,
334 "The %s option must be <= %"PRId64": %s\n",
335 name, (int64_t) opt->max, param);
336 return M_OPT_OUT_OF_RANGE;
339 if(dst)
340 VAL(dst) = tmp_off;
341 return 1;
344 static char* print_position(const m_option_t* opt, const void* val) {
345 return dup_printf("%"PRId64,(int64_t)VAL(val));
348 const m_option_type_t m_option_type_position = {
349 "Position",
350 "Integer (off_t)",
351 sizeof(off_t),
353 parse_position,
354 print_position,
355 copy_opt,
356 copy_opt,
357 NULL,
358 NULL
362 ///////////// String
364 #undef VAL
365 #define VAL(x) (*(char**)(x))
367 static int parse_str(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
370 if (param == NULL)
371 return M_OPT_MISSING_PARAM;
373 if ((opt->flags & M_OPT_MIN) && (strlen(param) < opt->min)) {
374 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Parameter must be >= %d chars: %s\n",
375 (int) opt->min, param);
376 return M_OPT_OUT_OF_RANGE;
379 if ((opt->flags & M_OPT_MAX) && (strlen(param) > opt->max)) {
380 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Parameter must be <= %d chars: %s\n",
381 (int) opt->max, param);
382 return M_OPT_OUT_OF_RANGE;
385 if(dst) {
386 if(VAL(dst))
387 free(VAL(dst));
388 VAL(dst) = strdup(param);
391 return 1;
395 static char* print_str(const m_option_t* opt, const void* val) {
396 return (val && VAL(val) && strlen(VAL(val)) > 0) ? strdup(VAL(val)) : NULL;
399 static void copy_str(const m_option_t* opt,void* dst, void* src) {
400 if(dst && src) {
401 #ifndef NO_FREE
402 if(VAL(dst)) free(VAL(dst)); //FIXME!!!
403 #endif
404 VAL(dst) = VAL(src) ? strdup(VAL(src)) : NULL;
408 static void free_str(void* src) {
409 if(src && VAL(src)){
410 #ifndef NO_FREE
411 free(VAL(src)); //FIXME!!!
412 #endif
413 VAL(src) = NULL;
417 const m_option_type_t m_option_type_string = {
418 "String",
420 sizeof(char*),
421 M_OPT_TYPE_DYNAMIC,
422 parse_str,
423 print_str,
424 copy_str,
425 copy_str,
426 copy_str,
427 free_str
430 //////////// String list
432 #define LIST_SEPARATOR ','
433 #undef VAL
434 #define VAL(x) (*(char***)(x))
436 #define OP_NONE 0
437 #define OP_ADD 1
438 #define OP_PRE 2
439 #define OP_DEL 3
440 #define OP_CLR 4
442 static void free_str_list(void* dst) {
443 char** d;
444 int i;
446 if(!dst || !VAL(dst)) return;
447 d = VAL(dst);
449 // FIXME!!!
450 #ifndef NO_FREE
451 for(i = 0 ; d[i] != NULL ; i++)
452 free(d[i]);
453 free(d);
454 #endif
455 VAL(dst) = NULL;
458 static int str_list_add(char** add, int n,void* dst,int pre) {
459 char** lst = VAL(dst);
460 int ln;
462 if(!dst) return M_OPT_PARSER_ERR;
463 lst = VAL(dst);
465 for(ln = 0 ; lst && lst[ln] ; ln++)
466 /**/;
468 lst = realloc(lst,(n+ln+1)*sizeof(char*));
470 if(pre) {
471 memmove(&lst[n],lst,(ln+1)*sizeof(char*));
472 memcpy(lst,add,n*sizeof(char*));
473 } else
474 memcpy(&lst[ln],add,(n+1)*sizeof(char*));
476 free(add);
478 VAL(dst) = lst;
480 return 1;
483 static int str_list_del(char** del, int n,void* dst) {
484 char **lst,*ep,**d;
485 int i,ln,s;
486 long idx;
488 if(!dst) return M_OPT_PARSER_ERR;
489 lst = VAL(dst);
491 for(ln = 0 ; lst && lst[ln] ; ln++)
492 /**/;
493 s = ln;
495 for(i = 0 ; del[i] != NULL ; i++) {
496 idx = strtol(del[i], &ep, 0);
497 if(*ep) {
498 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Invalid index: %s\n",del[i]);
499 free(del[i]);
500 continue;
502 free(del[i]);
503 if(idx < 0 || idx >= ln) {
504 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Index %ld is out of range.\n",idx);
505 continue;
506 } else if(!lst[idx])
507 continue;
508 free(lst[idx]);
509 lst[idx] = NULL;
510 s--;
512 free(del);
514 if(s == 0) {
515 if(lst) free(lst);
516 VAL(dst) = NULL;
517 return 1;
520 d = calloc(s+1,sizeof(char*));
521 for(i = 0, n = 0 ; i < ln ; i++) {
522 if(!lst[i]) continue;
523 d[n] = lst[i];
524 n++;
526 d[s] = NULL;
528 if(lst) free(lst);
529 VAL(dst) = d;
531 return 1;
534 static char *get_nextsep(char *ptr, char sep, int modify) {
535 char *last_ptr = ptr;
536 for(;;){
537 ptr = strchr(ptr, sep);
538 if(ptr && ptr>last_ptr && ptr[-1]=='\\'){
539 if (modify) memmove(ptr-1, ptr, strlen(ptr)+1);
540 else ptr++;
541 }else
542 break;
544 return ptr;
547 static int parse_str_list(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
548 int n = 0,len = strlen(opt->name);
549 char *str;
550 char *ptr = param, *last_ptr, **res;
551 int op = OP_NONE;
553 if(opt->name[len-1] == '*' && ((int)strlen(name) > len - 1)) {
554 const char* n = &name[len-1];
555 if(strcasecmp(n,"-add") == 0)
556 op = OP_ADD;
557 else if(strcasecmp(n,"-pre") == 0)
558 op = OP_PRE;
559 else if(strcasecmp(n,"-del") == 0)
560 op = OP_DEL;
561 else if(strcasecmp(n,"-clr") == 0)
562 op = OP_CLR;
563 else
564 return M_OPT_UNKNOWN;
567 // Clear the list ??
568 if(op == OP_CLR) {
569 if(dst)
570 free_str_list(dst);
571 return 0;
574 // All other ops need a param
575 if (param == NULL || strlen(param) == 0)
576 return M_OPT_MISSING_PARAM;
579 while(ptr[0] != '\0') {
580 ptr = get_nextsep(ptr, LIST_SEPARATOR, 0);
581 if(!ptr) {
582 n++;
583 break;
585 ptr++;
586 n++;
588 if(n == 0)
589 return M_OPT_INVALID;
590 if( ((opt->flags & M_OPT_MIN) && (n < opt->min)) ||
591 ((opt->flags & M_OPT_MAX) && (n > opt->max)) )
592 return M_OPT_OUT_OF_RANGE;
594 if(!dst) return 1;
596 res = malloc((n+2)*sizeof(char*));
597 ptr = str = strdup(param);
598 n = 0;
600 while(1) {
601 last_ptr = ptr;
602 ptr = get_nextsep(ptr, LIST_SEPARATOR, 1);
603 if(!ptr) {
604 res[n] = strdup(last_ptr);
605 n++;
606 break;
608 len = ptr - last_ptr;
609 res[n] = malloc(len + 1);
610 if(len) strncpy(res[n],last_ptr,len);
611 res[n][len] = '\0';
612 ptr++;
613 n++;
615 res[n] = NULL;
616 free(str);
618 switch(op) {
619 case OP_ADD:
620 return str_list_add(res,n,dst,0);
621 case OP_PRE:
622 return str_list_add(res,n,dst,1);
623 case OP_DEL:
624 return str_list_del(res,n,dst);
627 if(VAL(dst))
628 free_str_list(dst);
629 VAL(dst) = res;
631 return 1;
634 static void copy_str_list(const m_option_t* opt,void* dst, void* src) {
635 int n;
636 char **d,**s;
638 if(!(dst && src)) return;
639 s = VAL(src);
641 if(VAL(dst))
642 free_str_list(dst);
644 if(!s) {
645 VAL(dst) = NULL;
646 return;
649 for(n = 0 ; s[n] != NULL ; n++)
650 /* NOTHING */;
651 d = malloc((n+1)*sizeof(char*));
652 for( ; n >= 0 ; n--)
653 d[n] = s[n] ? strdup(s[n]) : NULL;
655 VAL(dst) = d;
658 static char* print_str_list(const m_option_t* opt, const void* src) {
659 char **lst = NULL;
660 char *ret = NULL,*last = NULL;
661 int i;
663 if(!(src && VAL(src))) return NULL;
664 lst = VAL(src);
666 for(i = 0 ; lst[i] ; i++) {
667 if(last) {
668 ret = dup_printf("%s,%s",last,lst[i]);
669 free(last);
670 } else
671 ret = strdup(lst[i]);
672 last = ret;
674 if(last && last != ret) free(last);
675 return ret;
678 const m_option_type_t m_option_type_string_list = {
679 "String list",
680 "A list of strings separated by ','\n"
681 "Option with a name ending in an * permits using the following suffix: \n"
682 "\t-add: Add the given parameters at the end of the list.\n"
683 "\t-pre: Add the given parameters at the beginning of the list.\n"
684 "\t-del: Remove the entry at the given indices.\n"
685 "\t-clr: Clear the list.\n"
686 "e.g: -vf-add flip,mirror -vf-del 2,5\n",
687 sizeof(char**),
688 M_OPT_TYPE_DYNAMIC | M_OPT_TYPE_ALLOW_WILDCARD,
689 parse_str_list,
690 print_str_list,
691 copy_str_list,
692 copy_str_list,
693 copy_str_list,
694 free_str_list
698 /////////////////// Func based options
700 // A chained list to save the various calls for func_param and func_full
701 typedef struct m_func_save m_func_save_t;
702 struct m_func_save {
703 m_func_save_t* next;
704 char* name;
705 char* param;
708 #undef VAL
709 #define VAL(x) (*(m_func_save_t**)(x))
711 static void free_func_pf(void* src) {
712 m_func_save_t *s,*n;
714 if(!src) return;
716 s = VAL(src);
718 while(s) {
719 n = s->next;
720 free(s->name);
721 if(s->param) free(s->param);
722 free(s);
723 s = n;
725 VAL(src) = NULL;
728 // Parser for func_param and func_full
729 static int parse_func_pf(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
730 m_func_save_t *s,*p;
732 if(!dst)
733 return 1;
735 s = calloc(1,sizeof(m_func_save_t));
736 s->name = strdup(name);
737 s->param = param ? strdup(param) : NULL;
739 p = VAL(dst);
740 if(p) {
741 for( ; p->next != NULL ; p = p->next)
742 /**/;
743 p->next = s;
744 } else
745 VAL(dst) = s;
747 return 1;
750 static void copy_func_pf(const m_option_t* opt,void* dst, void* src) {
751 m_func_save_t *d = NULL, *s,* last = NULL;
753 if(!(dst && src)) return;
754 s = VAL(src);
756 if(VAL(dst))
757 free_func_pf(dst);
759 while(s) {
760 d = calloc(1,sizeof(m_func_save_t));
761 d->name = strdup(s->name);
762 d->param = s->param ? strdup(s->param) : NULL;
763 if(last)
764 last->next = d;
765 else
766 VAL(dst) = d;
767 last = d;
768 s = s->next;
774 /////////////////// Func_param
776 static void set_func_param(const m_option_t* opt, void* dst, void* src) {
777 m_func_save_t* s;
779 if(!src) return;
780 s = VAL(src);
782 if(!s) return;
784 // Revert if needed
785 if(opt->priv) ((m_opt_default_func_t)opt->priv)(opt,opt->name);
786 for( ; s != NULL ; s = s->next)
787 ((m_opt_func_param_t) opt->p)(opt,s->param);
790 const m_option_type_t m_option_type_func_param = {
791 "Func param",
793 sizeof(m_func_save_t*),
794 M_OPT_TYPE_INDIRECT,
795 parse_func_pf,
796 NULL,
797 NULL, // Nothing to do on save
798 set_func_param,
799 copy_func_pf,
800 free_func_pf
803 /////////////////// Func_full
805 static void set_func_full(const m_option_t* opt, void* dst, void* src) {
806 m_func_save_t* s;
808 if(!src) return;
810 for(s = VAL(src) ; s ; s = s->next) {
811 // Revert if needed
812 if(opt->priv) ((m_opt_default_func_t)opt->priv)(opt,s->name);
813 ((m_opt_func_full_t) opt->p)(opt,s->name,s->param);
817 const m_option_type_t m_option_type_func_full = {
818 "Func full",
820 sizeof(m_func_save_t*),
821 M_OPT_TYPE_ALLOW_WILDCARD|M_OPT_TYPE_INDIRECT,
822 parse_func_pf,
823 NULL,
824 NULL, // Nothing to do on save
825 set_func_full,
826 copy_func_pf,
827 free_func_pf
830 /////////////// Func
832 #undef VAL
833 #define VAL(x) (*(int*)(x))
835 static int parse_func(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
836 if(dst)
837 VAL(dst) += 1;
838 return 0;
841 static void set_func(const m_option_t* opt,void* dst, void* src) {
842 int i;
843 if(opt->priv) ((m_opt_default_func_t)opt->priv)(opt,opt->name);
844 for(i = 0 ; i < VAL(src) ; i++)
845 ((m_opt_func_t) opt->p)(opt);
848 const m_option_type_t m_option_type_func = {
849 "Func",
851 sizeof(int),
852 M_OPT_TYPE_INDIRECT,
853 parse_func,
854 NULL,
855 NULL, // Nothing to do on save
856 set_func,
857 NULL,
858 NULL
861 /////////////////// Print
863 static int parse_print(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
864 if(opt->type == CONF_TYPE_PRINT_INDIRECT)
865 mp_msg(MSGT_CFGPARSER, MSGL_INFO, "%s", *(char **) opt->p);
866 else if(opt->type == CONF_TYPE_PRINT_FUNC)
867 return ((m_opt_func_full_t) opt->p)(opt,name,param);
868 else
869 mp_msg(MSGT_CFGPARSER, MSGL_INFO, "%s", (char *) opt->p);
871 if(opt->priv == NULL)
872 return M_OPT_EXIT;
873 return 1;
876 const m_option_type_t m_option_type_print = {
877 "Print",
881 parse_print,
882 NULL,
883 NULL,
884 NULL,
885 NULL,
886 NULL
889 const m_option_type_t m_option_type_print_indirect = {
890 "Print",
894 parse_print,
895 NULL,
896 NULL,
897 NULL,
898 NULL,
899 NULL
902 const m_option_type_t m_option_type_print_func = {
903 "Print",
906 M_OPT_TYPE_ALLOW_WILDCARD,
907 parse_print,
908 NULL,
909 NULL,
910 NULL,
911 NULL,
912 NULL
916 /////////////////////// Subconfig
917 #undef VAL
918 #define VAL(x) (*(char***)(x))
920 static int parse_subconf(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
921 char *subparam;
922 char *subopt;
923 int nr = 0,i,r;
924 const m_option_t *subopts;
925 const char *p;
926 char** lst = NULL;
928 if (param == NULL || strlen(param) == 0)
929 return M_OPT_MISSING_PARAM;
931 subparam = malloc(strlen(param)+1);
932 subopt = malloc(strlen(param)+1);
933 p = param;
935 subopts = opt->p;
937 while(p[0])
939 int sscanf_ret = 1;
940 int optlen = strcspn(p, ":=");
941 /* clear out */
942 subopt[0] = subparam[0] = 0;
943 av_strlcpy(subopt, p, optlen + 1);
944 p = &p[optlen];
945 if (p[0] == '=') {
946 sscanf_ret = 2;
947 p = &p[1];
948 if (p[0] == '"') {
949 p = &p[1];
950 optlen = strcspn(p, "\"");
951 av_strlcpy(subparam, p, optlen + 1);
952 p = &p[optlen];
953 if (p[0] != '"') {
954 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Terminating '\"' missing for '%s'\n", subopt);
955 return M_OPT_INVALID;
957 p = &p[1];
958 } else if (p[0] == '%') {
959 p = &p[1];
960 optlen = (int)strtol(p, (char**)&p, 0);
961 if (!p || p[0] != '%' || (optlen > strlen(p) - 1)) {
962 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Invalid length %i for '%s'\n", optlen, subopt);
963 return M_OPT_INVALID;
965 p = &p[1];
966 av_strlcpy(subparam, p, optlen + 1);
967 p = &p[optlen];
968 } else {
969 optlen = strcspn(p, ":");
970 av_strlcpy(subparam, p, optlen + 1);
971 p = &p[optlen];
974 if (p[0] == ':')
975 p = &p[1];
976 else if (p[0]) {
977 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Incorrect termination for '%s'\n", subopt);
978 return M_OPT_INVALID;
981 switch(sscanf_ret)
983 case 1:
984 subparam[0] = 0;
985 case 2:
986 for(i = 0 ; subopts[i].name ; i++) {
987 if(!strcmp(subopts[i].name,subopt)) break;
989 if(!subopts[i].name) {
990 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: Unknown suboption %s\n",name,subopt);
991 return M_OPT_UNKNOWN;
993 r = m_option_parse(&subopts[i],subopt,
994 subparam[0] == 0 ? NULL : subparam,NULL,src);
995 if(r < 0) return r;
996 if(dst) {
997 lst = (char**)realloc(lst,2 * (nr+2) * sizeof(char*));
998 lst[2*nr] = strdup(subopt);
999 lst[2*nr+1] = subparam[0] == 0 ? NULL : strdup(subparam);
1000 memset(&lst[2*(nr+1)],0,2*sizeof(char*));
1001 nr++;
1003 break;
1007 free(subparam);
1008 free(subopt);
1009 if(dst)
1010 VAL(dst) = lst;
1012 return 1;
1015 const m_option_type_t m_option_type_subconfig = {
1016 "Subconfig",
1017 "The syntax is -option opt1=foo:flag:opt2=blah",
1018 sizeof(int),
1019 M_OPT_TYPE_HAS_CHILD,
1020 parse_subconf,
1021 NULL,
1022 NULL,
1023 NULL,
1024 NULL,
1025 NULL
1028 #include "libmpcodecs/img_format.h"
1030 /* FIXME: snyc with img_format.h */
1031 static struct {
1032 const char* name;
1033 unsigned int fmt;
1034 } mp_imgfmt_list[] = {
1035 {"444p", IMGFMT_444P},
1036 {"422p", IMGFMT_422P},
1037 {"411p", IMGFMT_411P},
1038 {"yuy2", IMGFMT_YUY2},
1039 {"uyvy", IMGFMT_UYVY},
1040 {"yvu9", IMGFMT_YVU9},
1041 {"if09", IMGFMT_IF09},
1042 {"yv12", IMGFMT_YV12},
1043 {"i420", IMGFMT_I420},
1044 {"iyuv", IMGFMT_IYUV},
1045 {"clpl", IMGFMT_CLPL},
1046 {"hm12", IMGFMT_HM12},
1047 {"y800", IMGFMT_Y800},
1048 {"y8", IMGFMT_Y8},
1049 {"nv12", IMGFMT_NV12},
1050 {"nv21", IMGFMT_NV21},
1051 {"bgr24", IMGFMT_BGR24},
1052 {"bgr32", IMGFMT_BGR32},
1053 {"bgr16", IMGFMT_BGR16},
1054 {"bgr15", IMGFMT_BGR15},
1055 {"bgr8", IMGFMT_BGR8},
1056 {"bgr4", IMGFMT_BGR4},
1057 {"bg4b", IMGFMT_BG4B},
1058 {"bgr1", IMGFMT_BGR1},
1059 {"rgb48be", IMGFMT_RGB48BE},
1060 {"rgb48le", IMGFMT_RGB48LE},
1061 {"rgb48ne", IMGFMT_RGB48NE},
1062 {"rgb24", IMGFMT_RGB24},
1063 {"rgb32", IMGFMT_RGB32},
1064 {"rgb16", IMGFMT_RGB16},
1065 {"rgb15", IMGFMT_RGB15},
1066 {"rgb8", IMGFMT_RGB8},
1067 {"rgb4", IMGFMT_RGB4},
1068 {"rg4b", IMGFMT_RG4B},
1069 {"rgb1", IMGFMT_RGB1},
1070 {"rgba", IMGFMT_RGBA},
1071 {"argb", IMGFMT_ARGB},
1072 {"bgra", IMGFMT_BGRA},
1073 {"abgr", IMGFMT_ABGR},
1074 {"mjpeg", IMGFMT_MJPEG},
1075 {"mjpg", IMGFMT_MJPEG},
1076 { NULL, 0 }
1079 static int parse_imgfmt(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
1080 uint32_t fmt = 0;
1081 int i;
1083 if (param == NULL || strlen(param) == 0)
1084 return M_OPT_MISSING_PARAM;
1086 if(!strcmp(param,"help")) {
1087 mp_msg(MSGT_CFGPARSER, MSGL_INFO, "Available formats:");
1088 for(i = 0 ; mp_imgfmt_list[i].name ; i++)
1089 mp_msg(MSGT_CFGPARSER, MSGL_INFO, " %s",mp_imgfmt_list[i].name);
1090 mp_msg(MSGT_CFGPARSER, MSGL_INFO, "\n");
1091 return M_OPT_EXIT - 1;
1094 if (sscanf(param, "0x%x", &fmt) != 1)
1096 for(i = 0 ; mp_imgfmt_list[i].name ; i++) {
1097 if(!strcasecmp(param,mp_imgfmt_list[i].name)) {
1098 fmt=mp_imgfmt_list[i].fmt;
1099 break;
1102 if(!mp_imgfmt_list[i].name) {
1103 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: unknown format name: '%s'\n",name,param);
1104 return M_OPT_INVALID;
1108 if(dst)
1109 *((uint32_t*)dst) = fmt;
1111 return 1;
1114 const m_option_type_t m_option_type_imgfmt = {
1115 "Image format",
1116 "Please report any missing colorspaces.",
1117 sizeof(uint32_t),
1119 parse_imgfmt,
1120 NULL,
1121 copy_opt,
1122 copy_opt,
1123 NULL,
1124 NULL
1127 #include "libaf/af_format.h"
1129 /* FIXME: snyc with af_format.h */
1130 static struct {
1131 const char* name;
1132 unsigned int fmt;
1133 } mp_afmt_list[] = {
1134 // SPECIAL
1135 {"mulaw", AF_FORMAT_MU_LAW},
1136 {"alaw", AF_FORMAT_A_LAW},
1137 {"mpeg2", AF_FORMAT_MPEG2},
1138 {"ac3", AF_FORMAT_AC3},
1139 {"imaadpcm", AF_FORMAT_IMA_ADPCM},
1140 // ORIDNARY
1141 {"u8", AF_FORMAT_U8},
1142 {"s8", AF_FORMAT_S8},
1143 {"u16le", AF_FORMAT_U16_LE},
1144 {"u16be", AF_FORMAT_U16_BE},
1145 {"u16ne", AF_FORMAT_U16_NE},
1146 {"s16le", AF_FORMAT_S16_LE},
1147 {"s16be", AF_FORMAT_S16_BE},
1148 {"s16ne", AF_FORMAT_S16_NE},
1149 {"u24le", AF_FORMAT_U24_LE},
1150 {"u24be", AF_FORMAT_U24_BE},
1151 {"u24ne", AF_FORMAT_U24_NE},
1152 {"s24le", AF_FORMAT_S24_LE},
1153 {"s24be", AF_FORMAT_S24_BE},
1154 {"s24ne", AF_FORMAT_S24_NE},
1155 {"u32le", AF_FORMAT_U32_LE},
1156 {"u32be", AF_FORMAT_U32_BE},
1157 {"u32ne", AF_FORMAT_U32_NE},
1158 {"s32le", AF_FORMAT_S32_LE},
1159 {"s32be", AF_FORMAT_S32_BE},
1160 {"s32ne", AF_FORMAT_S32_NE},
1161 {"floatle", AF_FORMAT_FLOAT_LE},
1162 {"floatbe", AF_FORMAT_FLOAT_BE},
1163 {"floatne", AF_FORMAT_FLOAT_NE},
1164 { NULL, 0 }
1167 static int parse_afmt(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
1168 uint32_t fmt = 0;
1169 int i;
1171 if (param == NULL || strlen(param) == 0)
1172 return M_OPT_MISSING_PARAM;
1174 if(!strcmp(param,"help")) {
1175 mp_msg(MSGT_CFGPARSER, MSGL_INFO, "Available formats:");
1176 for(i = 0 ; mp_afmt_list[i].name ; i++)
1177 mp_msg(MSGT_CFGPARSER, MSGL_INFO, " %s",mp_afmt_list[i].name);
1178 mp_msg(MSGT_CFGPARSER, MSGL_INFO, "\n");
1179 return M_OPT_EXIT - 1;
1182 if (sscanf(param, "0x%x", &fmt) != 1)
1184 for(i = 0 ; mp_afmt_list[i].name ; i++) {
1185 if(!strcasecmp(param,mp_afmt_list[i].name)) {
1186 fmt=mp_afmt_list[i].fmt;
1187 break;
1190 if(!mp_afmt_list[i].name) {
1191 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: unknown format name: '%s'\n",name,param);
1192 return M_OPT_INVALID;
1196 if(dst)
1197 *((uint32_t*)dst) = fmt;
1199 return 1;
1202 const m_option_type_t m_option_type_afmt = {
1203 "Audio format",
1204 "Please report any missing formats.",
1205 sizeof(uint32_t),
1207 parse_afmt,
1208 NULL,
1209 copy_opt,
1210 copy_opt,
1211 NULL,
1212 NULL
1216 static double parse_timestring(const char *str)
1218 int a, b;
1219 double d;
1220 if (sscanf(str, "%d:%d:%lf", &a, &b, &d) == 3)
1221 return 3600*a + 60*b + d;
1222 else if (sscanf(str, "%d:%lf", &a, &d) == 2)
1223 return 60*a + d;
1224 else if (sscanf(str, "%lf", &d) == 1)
1225 return d;
1226 return -1e100;
1230 static int parse_time(const m_option_t* opt,const char *name, char *param, void* dst, int src)
1232 double time;
1234 if (param == NULL || strlen(param) == 0)
1235 return M_OPT_MISSING_PARAM;
1237 time = parse_timestring(param);
1238 if (time == -1e100) {
1239 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: invalid time: '%s'\n",
1240 name,param);
1241 return M_OPT_INVALID;
1244 if (dst)
1245 *(double *)dst = time;
1246 return 1;
1249 const m_option_type_t m_option_type_time = {
1250 "Time",
1252 sizeof(double),
1254 parse_time,
1255 print_double,
1256 copy_opt,
1257 copy_opt,
1258 NULL,
1259 NULL
1263 // Time or size (-endpos)
1265 static int parse_time_size(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
1266 m_time_size_t ts;
1267 char unit[4];
1268 double end_at;
1270 if (param == NULL || strlen(param) == 0)
1271 return M_OPT_MISSING_PARAM;
1273 ts.pos=0;
1274 /* End at size parsing */
1275 if(sscanf(param, "%lf%3s", &end_at, unit) == 2) {
1276 ts.type = END_AT_SIZE;
1277 if(!strcasecmp(unit, "b"))
1279 else if(!strcasecmp(unit, "kb"))
1280 end_at *= 1024;
1281 else if(!strcasecmp(unit, "mb"))
1282 end_at *= 1024*1024;
1283 else if(!strcasecmp(unit, "gb"))
1284 end_at *= 1024*1024*1024;
1285 else
1286 ts.type = END_AT_NONE;
1288 if (ts.type == END_AT_SIZE) {
1289 ts.pos = end_at;
1290 goto out;
1294 /* End at time parsing. This has to be last because the parsing accepts
1295 * even a number followed by garbage */
1296 if ((end_at = parse_timestring(param)) == -1e100) {
1297 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: invalid time or size: '%s'\n",
1298 name,param);
1299 return M_OPT_INVALID;
1302 ts.type = END_AT_TIME;
1303 ts.pos = end_at;
1304 out:
1305 if(dst)
1306 *(m_time_size_t *)dst = ts;
1307 return 1;
1310 const m_option_type_t m_option_type_time_size = {
1311 "Time or size",
1313 sizeof(m_time_size_t),
1315 parse_time_size,
1316 NULL,
1317 copy_opt,
1318 copy_opt,
1319 NULL,
1320 NULL
1324 //// Objects (i.e. filters, etc) settings
1326 #include "m_struct.h"
1328 #undef VAL
1329 #define VAL(x) (*(m_obj_settings_t**)(x))
1331 static int find_obj_desc(const char* name,const m_obj_list_t* l,const m_struct_t** ret) {
1332 int i;
1333 char* n;
1335 for(i = 0 ; l->list[i] ; i++) {
1336 n = M_ST_MB(char*,l->list[i],l->name_off);
1337 if(!strcmp(n,name)) {
1338 *ret = M_ST_MB(m_struct_t*,l->list[i],l->desc_off);
1339 return 1;
1342 return 0;
1345 static int get_obj_param(const char* opt_name,const char* obj_name, const m_struct_t* desc,
1346 char* str,int* nold,int oldmax,char** dst) {
1347 char* eq;
1348 const m_option_t* opt;
1349 int r;
1351 eq = strchr(str,'=');
1352 if(eq && eq == str)
1353 eq = NULL;
1355 if(eq) {
1356 char* p = eq + 1;
1357 if(p[0] == '\0') p = NULL;
1358 eq[0] = '\0';
1359 opt = m_option_list_find(desc->fields,str);
1360 if(!opt) {
1361 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: %s doesn't have a %s parameter.\n",opt_name,obj_name,str);
1362 return M_OPT_UNKNOWN;
1364 r = m_option_parse(opt,str,p,NULL,M_CONFIG_FILE);
1365 if(r < 0) {
1366 if(r > M_OPT_EXIT)
1367 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: Error while parsing %s parameter %s (%s)\n",opt_name,obj_name,str,p);
1368 eq[0] = '=';
1369 return r;
1371 if(dst) {
1372 dst[0] = strdup(str);
1373 dst[1] = p ? strdup(p) : NULL;
1375 eq[0] = '=';
1376 } else {
1377 if((*nold) >= oldmax) {
1378 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: %s has only %d params, so you can't give more than %d unnamed params.\n",
1379 opt_name,obj_name,oldmax,oldmax);
1380 return M_OPT_OUT_OF_RANGE;
1382 opt = &desc->fields[(*nold)];
1383 r = m_option_parse(opt,opt->name,str,NULL,M_CONFIG_FILE);
1384 if(r < 0) {
1385 if(r > M_OPT_EXIT)
1386 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: Error while parsing %s parameter %s (%s)\n",opt_name,obj_name,opt->name,str);
1387 return r;
1389 if(dst) {
1390 dst[0] = strdup(opt->name);
1391 dst[1] = strdup(str);
1393 (*nold)++;
1395 return 1;
1398 static int get_obj_params(const char* opt_name, const char* name,char* params,
1399 const m_struct_t* desc,char separator, char*** _ret) {
1400 int n = 0,nold = 0, nopts,r;
1401 char* ptr,*last_ptr = params;
1402 char** ret;
1404 if(!strcmp(params,"help")) { // Help
1405 char min[50],max[50];
1406 if(!desc->fields) {
1407 printf("%s doesn't have any options.\n\n",name);
1408 return M_OPT_EXIT - 1;
1410 printf("\n Name Type Min Max\n\n");
1411 for(n = 0 ; desc->fields[n].name ; n++) {
1412 const m_option_t* opt = &desc->fields[n];
1413 if(opt->type->flags & M_OPT_TYPE_HAS_CHILD) continue;
1414 if(opt->flags & M_OPT_MIN)
1415 sprintf(min,"%-8.0f",opt->min);
1416 else
1417 strcpy(min,"No");
1418 if(opt->flags & M_OPT_MAX)
1419 sprintf(max,"%-8.0f",opt->max);
1420 else
1421 strcpy(max,"No");
1422 printf(" %-20.20s %-15.15s %-10.10s %-10.10s\n",
1423 opt->name,
1424 opt->type->name,
1425 min,
1426 max);
1428 printf("\n");
1429 return M_OPT_EXIT - 1;
1432 for(nopts = 0 ; desc->fields[nopts].name ; nopts++)
1433 /* NOP */;
1435 // TODO : Check that each opt can be parsed
1436 r = 1;
1437 while(last_ptr && last_ptr[0] != '\0') {
1438 ptr = strchr(last_ptr,separator);
1439 if(!ptr) {
1440 r = get_obj_param(opt_name,name,desc,last_ptr,&nold,nopts,NULL);
1441 n++;
1442 break;
1444 if(ptr == last_ptr) { // Empty field, count it and go on
1445 nold++;
1446 last_ptr = ptr+1;
1447 continue;
1449 ptr[0] = '\0';
1450 r = get_obj_param(opt_name,name,desc,last_ptr,&nold,nopts,NULL);
1451 ptr[0] = separator;
1452 if(r < 0) break;
1453 n++;
1454 last_ptr = ptr+1;
1456 if(r < 0) return r;
1457 if (!last_ptr[0]) // count an empty field at the end, too
1458 nold++;
1459 if (nold > nopts) {
1460 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Too many options for %s\n", name);
1461 return M_OPT_OUT_OF_RANGE;
1463 if(!_ret) // Just test
1464 return 1;
1465 if (n == 0) // No options or only empty options
1466 return 1;
1468 ret = malloc((n+2)*2*sizeof(char*));
1469 n = nold = 0;
1470 last_ptr = params;
1472 while(last_ptr && last_ptr[0] != '\0') {
1473 ptr = strchr(last_ptr,separator);
1474 if(!ptr) {
1475 get_obj_param(opt_name,name,desc,last_ptr,&nold,nopts,&ret[n*2]);
1476 n++;
1477 break;
1479 if(ptr == last_ptr) { // Empty field, count it and go on
1480 last_ptr = ptr+1;
1481 nold++;
1482 continue;
1484 ptr[0] = '\0';
1485 get_obj_param(opt_name,name,desc,last_ptr,&nold,nopts,&ret[n*2]);
1486 n++;
1487 last_ptr = ptr+1;
1489 ret[n*2] = ret[n*2+1] = NULL;
1490 *_ret = ret;
1492 return 1;
1495 static int parse_obj_params(const m_option_t* opt,const char *name,
1496 char *param, void* dst, int src) {
1497 char** opts;
1498 int r;
1499 m_obj_params_t* p = opt->priv;
1500 const m_struct_t* desc;
1501 char* cpy;
1503 // We need the object desc
1504 if(!p)
1505 return M_OPT_INVALID;
1507 desc = p->desc;
1508 cpy = strdup(param);
1509 r = get_obj_params(name,desc->name,cpy,desc,p->separator,dst ? &opts : NULL);
1510 free(cpy);
1511 if(r < 0)
1512 return r;
1513 if(!dst)
1514 return 1;
1515 if (!opts) // no arguments given
1516 return 1;
1518 for(r = 0 ; opts[r] ; r += 2)
1519 m_struct_set(desc,dst,opts[r],opts[r+1]);
1521 return 1;
1525 const m_option_type_t m_option_type_obj_params = {
1526 "Object params",
1530 parse_obj_params,
1531 NULL,
1532 NULL,
1533 NULL,
1534 NULL,
1535 NULL
1538 /// Some predefined types as a definition would be quite lengthy
1540 /// Span arguments
1541 static const m_span_t m_span_params_dflts = { -1, -1 };
1542 static const m_option_t m_span_params_fields[] = {
1543 {"start", M_ST_OFF(m_span_t,start), CONF_TYPE_INT, M_OPT_MIN, 1 ,0, NULL},
1544 {"end", M_ST_OFF(m_span_t,end), CONF_TYPE_INT, M_OPT_MIN , 1 ,0, NULL},
1545 { NULL, NULL, 0, 0, 0, 0, NULL }
1547 static const struct m_struct_st m_span_opts = {
1548 "m_span",
1549 sizeof(m_span_t),
1550 &m_span_params_dflts,
1551 m_span_params_fields
1553 const m_obj_params_t m_span_params_def = {
1554 &m_span_opts,
1558 static int parse_obj_settings(const char* opt,char* str,const m_obj_list_t* list,
1559 m_obj_settings_t **_ret, int ret_n) {
1560 int r;
1561 char *param,**plist = NULL;
1562 const m_struct_t* desc;
1563 m_obj_settings_t *ret = _ret ? *_ret : NULL;
1566 // Now check that the object exists
1567 param = strchr(str,'=');
1568 if(param) {
1569 param[0] = '\0';
1570 param++;
1571 if(strlen(param) <= 0)
1572 param = NULL;
1576 if(!find_obj_desc(str,list,&desc)) {
1577 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: %s doesn't exist.\n",opt,str);
1578 return M_OPT_INVALID;
1581 if(param) {
1582 if(!desc && _ret) {
1583 if(!strcmp(param,"help")) {
1584 mp_msg(MSGT_CFGPARSER, MSGL_INFO, "Option %s: %s have no option description.\n",opt,str);
1585 return M_OPT_EXIT - 1;
1587 plist = calloc(4,sizeof(char*));
1588 plist[0] = strdup("_oldargs_");
1589 plist[1] = strdup(param);
1590 } else if(desc) {
1591 r = get_obj_params(opt,str,param,desc,':',_ret ? &plist : NULL);
1592 if(r < 0)
1593 return r;
1596 if(!_ret)
1597 return 1;
1599 ret = realloc(ret,(ret_n+2)*sizeof(m_obj_settings_t));
1600 memset(&ret[ret_n],0,2*sizeof(m_obj_settings_t));
1601 ret[ret_n].name = strdup(str);
1602 ret[ret_n].attribs = plist;
1604 *_ret = ret;
1605 return 1;
1608 static void free_obj_settings_list(void* dst);
1610 static int obj_settings_list_del(const char *opt_name,char *param,void* dst, int src) {
1611 char** str_list = NULL;
1612 int r,i,idx_max = 0;
1613 char* rem_id = "_removed_marker_";
1614 const m_option_t list_opt = {opt_name , NULL, CONF_TYPE_STRING_LIST,
1615 0, 0, 0, NULL };
1616 m_obj_settings_t* obj_list = dst ? VAL(dst) : NULL;
1618 if(dst && !obj_list) {
1619 mp_msg(MSGT_CFGPARSER, MSGL_WARN, "Option %s: the list is empty.\n",opt_name);
1620 return 1;
1621 } else if(obj_list) {
1622 for(idx_max = 0 ; obj_list[idx_max].name != NULL ; idx_max++)
1623 /* NOP */;
1626 r = m_option_parse(&list_opt,opt_name,param,&str_list,src);
1627 if(r < 0 || !str_list)
1628 return r;
1630 for(r = 0 ; str_list[r] ; r++) {
1631 int id;
1632 char* endptr;
1633 id = strtol(str_list[r],&endptr,0);
1634 if(endptr == str_list[r]) {
1635 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: invalid parameter. We need a list of integers which are the indices of the elements to remove.\n",opt_name);
1636 m_option_free(&list_opt,&str_list);
1637 return M_OPT_INVALID;
1639 if(!obj_list) continue;
1640 if(id >= idx_max || id < -idx_max) {
1641 mp_msg(MSGT_CFGPARSER, MSGL_WARN, "Option %s: Index %d is out of range.\n",opt_name,id);
1642 continue;
1644 if(id < 0)
1645 id = idx_max + id;
1646 free(obj_list[id].name);
1647 free_str_list(&(obj_list[id].attribs));
1648 obj_list[id].name = rem_id;
1651 if(!dst) {
1652 m_option_free(&list_opt,&str_list);
1653 return 1;
1656 for(i = 0 ; obj_list[i].name ; i++) {
1657 while(obj_list[i].name == rem_id) {
1658 memmove(&obj_list[i],&obj_list[i+1],sizeof(m_obj_settings_t)*(idx_max - i));
1659 idx_max--;
1662 obj_list = realloc(obj_list,sizeof(m_obj_settings_t)*(idx_max+1));
1663 VAL(dst) = obj_list;
1665 return 1;
1668 static int parse_obj_settings_list(const m_option_t* opt,const char *name,
1669 char *param, void* dst, int src) {
1670 int n = 0,r,len = strlen(opt->name);
1671 char *str;
1672 char *ptr, *last_ptr;
1673 m_obj_settings_t *res = NULL,*queue = NULL,*head = NULL;
1674 int op = OP_NONE;
1676 // We need the objects list
1677 if(!opt->priv)
1678 return M_OPT_INVALID;
1680 if(opt->name[len-1] == '*' && ((int)strlen(name) > len - 1)) {
1681 const char* n = &name[len-1];
1682 if(strcasecmp(n,"-add") == 0)
1683 op = OP_ADD;
1684 else if(strcasecmp(n,"-pre") == 0)
1685 op = OP_PRE;
1686 else if(strcasecmp(n,"-del") == 0)
1687 op = OP_DEL;
1688 else if(strcasecmp(n,"-clr") == 0)
1689 op = OP_CLR;
1690 else {
1691 char prefix[len];
1692 strncpy(prefix,opt->name,len-1);
1693 prefix[len-1] = '\0';
1694 mp_msg(MSGT_VFILTER,MSGL_ERR, "Option %s: unknown postfix %s\n"
1695 "Supported postfixes are:\n"
1696 " %s-add\n"
1697 " Append the given list to the current list\n\n"
1698 " %s-pre\n"
1699 " Prepend the given list to the current list\n\n"
1700 " %s-del x,y,...\n"
1701 " Remove the given elements. Take the list element index (starting from 0).\n"
1702 " Negative index can be used (i.e. -1 is the last element)\n\n"
1703 " %s-clr\n"
1704 " Clear the current list.\n",name,n,prefix,prefix,prefix,prefix);
1706 return M_OPT_UNKNOWN;
1710 // Clear the list ??
1711 if(op == OP_CLR) {
1712 if(dst)
1713 free_obj_settings_list(dst);
1714 return 0;
1717 if (param == NULL || strlen(param) == 0)
1718 return M_OPT_MISSING_PARAM;
1720 switch(op) {
1721 case OP_ADD:
1722 if(dst) head = VAL(dst);
1723 break;
1724 case OP_PRE:
1725 if(dst) queue = VAL(dst);
1726 break;
1727 case OP_DEL:
1728 return obj_settings_list_del(name,param,dst,src);
1729 case OP_NONE:
1730 if(dst && VAL(dst))
1731 free_obj_settings_list(dst);
1732 break;
1733 default:
1734 mp_msg(MSGT_VFILTER,MSGL_ERR, "Option %s: FIXME\n",name);
1735 return M_OPT_UNKNOWN;
1738 if(!strcmp(param,"help")) {
1739 m_obj_list_t* ol = opt->priv;
1740 mp_msg(MSGT_VFILTER,MSGL_INFO,"Available video filters:\n");
1741 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_FILTERS\n");
1742 for(n = 0 ; ol->list[n] ; n++)
1743 mp_msg(MSGT_VFILTER,MSGL_INFO," %-15s: %s\n",
1744 M_ST_MB(char*,ol->list[n],ol->name_off),
1745 M_ST_MB(char*,ol->list[n],ol->info_off));
1746 mp_msg(MSGT_VFILTER,MSGL_INFO,"\n");
1747 return M_OPT_EXIT - 1;
1749 ptr = str = strdup(param);
1751 while(ptr[0] != '\0') {
1752 last_ptr = ptr;
1753 ptr = get_nextsep(ptr, LIST_SEPARATOR, 1);
1755 if(!ptr) {
1756 r = parse_obj_settings(name,last_ptr,opt->priv,dst ? &res : NULL,n);
1757 if(r < 0) {
1758 free(str);
1759 return r;
1761 n++;
1762 break;
1764 ptr[0] = '\0';
1765 r = parse_obj_settings(name,last_ptr,opt->priv,dst ? &res : NULL,n);
1766 if(r < 0) {
1767 free(str);
1768 return r;
1770 ptr++;
1771 n++;
1773 free(str);
1774 if(n == 0)
1775 return M_OPT_INVALID;
1777 if( ((opt->flags & M_OPT_MIN) && (n < opt->min)) ||
1778 ((opt->flags & M_OPT_MAX) && (n > opt->max)) )
1779 return M_OPT_OUT_OF_RANGE;
1781 if(dst) {
1782 if(queue) {
1783 int qsize;
1784 for(qsize = 0 ; queue[qsize].name ; qsize++)
1785 /* NOP */;
1786 res = realloc(res,(qsize+n+1)*sizeof(m_obj_settings_t));
1787 memcpy(&res[n],queue,(qsize+1)*sizeof(m_obj_settings_t));
1788 n += qsize;
1789 free(queue);
1791 if(head) {
1792 int hsize;
1793 for(hsize = 0 ; head[hsize].name ; hsize++)
1794 /* NOP */;
1795 head = realloc(head,(hsize+n+1)*sizeof(m_obj_settings_t));
1796 memcpy(&head[hsize],res,(n+1)*sizeof(m_obj_settings_t));
1797 free(res);
1798 res = head;
1800 VAL(dst) = res;
1802 return 1;
1805 static void free_obj_settings_list(void* dst) {
1806 int n;
1807 m_obj_settings_t *d;
1809 if(!dst || !VAL(dst)) return;
1811 d = VAL(dst);
1812 #ifndef NO_FREE
1813 for(n = 0 ; d[n].name ; n++) {
1814 free(d[n].name);
1815 free_str_list(&(d[n].attribs));
1817 free(d);
1818 #endif
1819 VAL(dst) = NULL;
1822 static void copy_obj_settings_list(const m_option_t* opt,void* dst, void* src) {
1823 m_obj_settings_t *d,*s;
1824 int n;
1826 if(!(dst && src))
1827 return;
1829 s = VAL(src);
1831 if(VAL(dst))
1832 free_obj_settings_list(dst);
1833 if(!s) return;
1837 for(n = 0 ; s[n].name ; n++)
1838 /* NOP */;
1839 d = malloc((n+1)*sizeof(m_obj_settings_t));
1840 for(n = 0 ; s[n].name ; n++) {
1841 d[n].name = strdup(s[n].name);
1842 d[n].attribs = NULL;
1843 copy_str_list(NULL,&(d[n].attribs),&(s[n].attribs));
1845 d[n].name = NULL;
1846 d[n].attribs = NULL;
1847 VAL(dst) = d;
1850 const m_option_type_t m_option_type_obj_settings_list = {
1851 "Object settings list",
1853 sizeof(m_obj_settings_t*),
1854 M_OPT_TYPE_DYNAMIC|M_OPT_TYPE_ALLOW_WILDCARD,
1855 parse_obj_settings_list,
1856 NULL,
1857 copy_obj_settings_list,
1858 copy_obj_settings_list,
1859 copy_obj_settings_list,
1860 free_obj_settings_list,
1865 static int parse_obj_presets(const m_option_t* opt,const char *name,
1866 char *param, void* dst, int src) {
1867 m_obj_presets_t* obj_p = (m_obj_presets_t*)opt->priv;
1868 m_struct_t *in_desc,*out_desc;
1869 int s,i;
1870 unsigned char* pre;
1871 char* pre_name = NULL;
1873 if(!obj_p) {
1874 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: Presets need a pointer to a m_obj_presets_t in the priv field.\n",name);
1875 return M_OPT_PARSER_ERR;
1878 if(!param)
1879 return M_OPT_MISSING_PARAM;
1881 pre = obj_p->presets;
1882 in_desc = obj_p->in_desc;
1883 out_desc = obj_p->out_desc ? obj_p->out_desc : obj_p->in_desc;
1884 s = in_desc->size;
1886 if(!strcmp(param,"help")) {
1887 mp_msg(MSGT_CFGPARSER, MSGL_INFO, "Available presets for %s->%s:",out_desc->name,name);
1888 for(pre = obj_p->presets;(pre_name = M_ST_MB(char*,pre,obj_p->name_off)) ;
1889 pre += s)
1890 mp_msg(MSGT_CFGPARSER, MSGL_ERR, " %s",pre_name);
1891 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "\n");
1892 return M_OPT_EXIT - 1;
1895 for(pre_name = M_ST_MB(char*,pre,obj_p->name_off) ; pre_name ;
1896 pre += s, pre_name = M_ST_MB(char*,pre,obj_p->name_off)) {
1897 if(!strcmp(pre_name,param)) break;
1899 if(!pre_name) {
1900 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: There is no preset named %s\n"
1901 "Available presets are:",name,param);
1902 for(pre = obj_p->presets;(pre_name = M_ST_MB(char*,pre,obj_p->name_off)) ;
1903 pre += s)
1904 mp_msg(MSGT_CFGPARSER, MSGL_ERR, " %s",pre_name);
1905 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "\n");
1906 return M_OPT_INVALID;
1909 if(!dst) return 1;
1911 for(i = 0 ; in_desc->fields[i].name ; i++) {
1912 const m_option_t* out_opt = m_option_list_find(out_desc->fields,
1913 in_desc->fields[i].name);
1914 if(!out_opt) {
1915 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: Unable to find the target option for field %s.\nPlease report this to the developers.\n",name,in_desc->fields[i].name);
1916 return M_OPT_PARSER_ERR;
1918 m_option_copy(out_opt,M_ST_MB_P(dst,out_opt->p),M_ST_MB_P(pre,in_desc->fields[i].p));
1920 return 1;
1924 const m_option_type_t m_option_type_obj_presets = {
1925 "Object presets",
1929 parse_obj_presets,
1930 NULL,
1931 NULL,
1932 NULL,
1933 NULL,
1934 NULL
1937 static int parse_custom_url(const m_option_t* opt,const char *name,
1938 char *url, void* dst, int src) {
1939 int pos1, pos2, r, v6addr = 0;
1940 char *ptr1=NULL, *ptr2=NULL, *ptr3=NULL, *ptr4=NULL;
1941 m_struct_t* desc = opt->priv;
1943 if(!desc) {
1944 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: Custom URL needs a pointer to a m_struct_t in the priv field.\n",name);
1945 return M_OPT_PARSER_ERR;
1948 // extract the protocol
1949 ptr1 = strstr(url, "://");
1950 if( ptr1==NULL ) {
1951 // Filename only
1952 if(m_option_list_find(desc->fields,"filename")) {
1953 m_struct_set(desc,dst,"filename",url);
1954 return 1;
1956 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Option %s: URL doesn't have a valid protocol!\n",name);
1957 return M_OPT_INVALID;
1959 if(m_option_list_find(desc->fields,"string")) {
1960 if(strlen(ptr1)>3) {
1961 m_struct_set(desc,dst,"string",ptr1+3);
1962 return 1;
1965 pos1 = ptr1-url;
1966 if(dst && m_option_list_find(desc->fields,"protocol")) {
1967 ptr1[0] = '\0';
1968 r = m_struct_set(desc,dst,"protocol",url);
1969 ptr1[0] = ':';
1970 if(r < 0) {
1971 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: Error while setting protocol.\n",name);
1972 return r;
1976 // jump the "://"
1977 ptr1 += 3;
1978 pos1 += 3;
1980 // check if a username:password is given
1981 ptr2 = strstr(ptr1, "@");
1982 ptr3 = strstr(ptr1, "/");
1983 if( ptr3!=NULL && ptr3<ptr2 ) {
1984 // it isn't really a username but rather a part of the path
1985 ptr2 = NULL;
1987 if( ptr2!=NULL ) {
1989 // We got something, at least a username...
1990 if(!m_option_list_find(desc->fields,"username")) {
1991 mp_msg(MSGT_CFGPARSER, MSGL_WARN, "Option %s: This URL doesn't have a username part.\n",name);
1992 // skip
1993 } else {
1994 ptr3 = strstr(ptr1, ":");
1995 if( ptr3!=NULL && ptr3<ptr2 ) {
1996 // We also have a password
1997 if(!m_option_list_find(desc->fields,"password")) {
1998 mp_msg(MSGT_CFGPARSER, MSGL_WARN, "Option %s: This URL doesn't have a password part.\n",name);
1999 // skip
2000 } else { // Username and password
2001 if(dst) {
2002 ptr3[0] = '\0';
2003 r = m_struct_set(desc,dst,"username",ptr1);
2004 ptr3[0] = ':';
2005 if(r < 0) {
2006 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: Error while setting username.\n",name);
2007 return r;
2009 ptr2[0] = '\0';
2010 r = m_struct_set(desc,dst,"password",ptr3+1);
2011 ptr2[0] = '@';
2012 if(r < 0) {
2013 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: Error while setting password.\n",name);
2014 return r;
2018 } else { // User name only
2019 ptr2[0] = '\0';
2020 r = m_struct_set(desc,dst,"username",ptr1);
2021 ptr2[0] = '@';
2022 if(r < 0) {
2023 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: Error while setting username.\n",name);
2024 return r;
2028 ptr1 = ptr2+1;
2029 pos1 = ptr1-url;
2032 // before looking for a port number check if we have an IPv6 type numeric address
2033 // in an IPv6 URL the numeric address should be inside square braces.
2034 ptr2 = strstr(ptr1, "[");
2035 ptr3 = strstr(ptr1, "]");
2036 // If the [] is after the first it isn't the hostname
2037 ptr4 = strstr(ptr1, "/");
2038 if( ptr2!=NULL && ptr3!=NULL && (ptr2 < ptr3) && (!ptr4 || ptr4 > ptr3)) {
2039 // we have an IPv6 numeric address
2040 ptr1++;
2041 pos1++;
2042 ptr2 = ptr3;
2043 v6addr = 1;
2044 } else {
2045 ptr2 = ptr1;
2048 // look if the port is given
2049 ptr2 = strstr(ptr2, ":");
2050 // If the : is after the first / it isn't the port
2051 ptr3 = strstr(ptr1, "/");
2052 if(ptr3 && ptr3 - ptr2 < 0) ptr2 = NULL;
2053 if( ptr2==NULL ) {
2054 // No port is given
2055 // Look if a path is given
2056 if( ptr3==NULL ) {
2057 // No path/filename
2058 // So we have an URL like http://www.hostname.com
2059 pos2 = strlen(url);
2060 } else {
2061 // We have an URL like http://www.hostname.com/file.txt
2062 pos2 = ptr3-url;
2064 } else {
2065 // We have an URL beginning like http://www.hostname.com:1212
2066 // Get the port number
2067 if(!m_option_list_find(desc->fields,"port")) {
2068 mp_msg(MSGT_CFGPARSER, MSGL_WARN, "Option %s: This URL doesn't have a port part.\n",name);
2069 // skip
2070 } else {
2071 if(dst) {
2072 int p = atoi(ptr2+1);
2073 char tmp[100];
2074 snprintf(tmp,99,"%d",p);
2075 r = m_struct_set(desc,dst,"port",tmp);
2076 if(r < 0) {
2077 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: Error while setting port.\n",name);
2078 return r;
2082 pos2 = ptr2-url;
2084 if( v6addr ) pos2--;
2085 // Get the hostname
2086 if(pos2-pos1 > 0) {
2087 if(!m_option_list_find(desc->fields,"hostname")) {
2088 mp_msg(MSGT_CFGPARSER, MSGL_WARN, "Option %s: This URL doesn't have a hostname part.\n",name);
2089 // skip
2090 } else {
2091 char tmp[pos2-pos1+1];
2092 strncpy(tmp,ptr1, pos2-pos1);
2093 tmp[pos2-pos1] = '\0';
2094 r = m_struct_set(desc,dst,"hostname",tmp);
2095 if(r < 0) {
2096 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: Error while setting hostname.\n",name);
2097 return r;
2101 // Look if a path is given
2102 ptr2 = strstr(ptr1, "/");
2103 if( ptr2!=NULL ) {
2104 // A path/filename is given
2105 // check if it's not a trailing '/'
2106 if( strlen(ptr2)>1 ) {
2107 // copy the path/filename in the URL container
2108 if(!m_option_list_find(desc->fields,"filename")) {
2109 mp_msg(MSGT_CFGPARSER, MSGL_WARN, "Option %s: This URL doesn't have a hostname part.\n",name);
2110 // skip
2111 } else {
2112 if(dst) {
2113 int l = strlen(ptr2+1) + 1;
2114 char* fname = ptr2+1;
2115 if(l > 1) {
2116 fname = malloc(l);
2117 url_unescape_string(fname,ptr2+1);
2119 r = m_struct_set(desc,dst,"filename",fname);
2120 if(fname != ptr2+1)
2121 free(fname);
2122 if(r < 0) {
2123 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: Error while setting filename.\n",name);
2124 return r;
2130 return 1;
2133 /// TODO : Write the other needed funcs for 'normal' options
2134 const m_option_type_t m_option_type_custom_url = {
2135 "Custom URL",
2139 parse_custom_url,
2140 NULL,
2141 NULL,
2142 NULL,
2143 NULL,
2144 NULL