12 //#include "m_config.h"
15 // Don't free for 'production' atm
20 m_option_t
* m_option_list_find(m_option_t
* list
,char* name
) {
23 for(i
= 0 ; list
[i
].name
; i
++) {
24 int l
= strlen(list
[i
].name
) - 1;
25 if((list
[i
].type
->flags
& M_OPT_TYPE_ALLOW_WILDCARD
) &&
26 (l
> 0) && (list
[i
].name
[l
] == '*')) {
27 if(strncasecmp(list
[i
].name
,name
,l
) == 0)
29 } else if(strcasecmp(list
[i
].name
,name
) == 0)
35 // Default function that just does a memcpy
37 static void copy_opt(m_option_t
* opt
,void* dst
,void* src
) {
39 memcpy(dst
,src
,opt
->type
->size
);
42 // Helper for the print funcs (from man printf)
43 static char* dup_printf(const char *fmt
, ...) {
44 /* Guess we need no more than 50 bytes. */
48 if ((p
= malloc (size
)) == NULL
)
51 /* Try to print in the allocated space. */
53 n
= vsnprintf (p
, size
, fmt
, ap
);
55 /* If that worked, return the string. */
56 if (n
> -1 && n
< size
)
58 /* Else try again with more space. */
59 if (n
> -1) /* glibc 2.1 */
60 size
= n
+1; /* precisely what is needed */
62 size
*= 2; /* twice the old size */
63 if ((p
= realloc (p
, size
)) == NULL
)
71 #define VAL(x) (*(int*)(x))
73 static int parse_flag(m_option_t
* opt
,char *name
, char *param
, void* dst
, int src
) {
74 if (src
== M_CONFIG_FILE
) {
75 if(!param
) return M_OPT_MISSING_PARAM
;
76 if (!strcasecmp(param
, "yes") || /* any other language? */
77 !strcasecmp(param
, "on") ||
78 !strcasecmp(param
, "ja") ||
79 !strcasecmp(param
, "si") ||
80 !strcasecmp(param
, "igen") ||
81 !strcasecmp(param
, "y") ||
82 !strcasecmp(param
, "j") ||
83 !strcasecmp(param
, "i") ||
84 !strcmp(param
, "1")) {
85 if(dst
) VAL(dst
) = opt
->max
;
86 } else if (!strcasecmp(param
, "no") ||
87 !strcasecmp(param
, "off") ||
88 !strcasecmp(param
, "nein") ||
89 !strcasecmp(param
, "nicht") ||
90 !strcasecmp(param
, "nem") ||
91 !strcasecmp(param
, "n") ||
92 !strcmp(param
, "0")) {
93 if(dst
) VAL(dst
) = opt
->min
;
95 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Invalid parameter for %s flag: %s\n",name
, param
);
100 if(dst
) VAL(dst
) = opt
->max
;
105 static char* print_flag(m_option_t
* opt
, void* val
) {
106 if(VAL(val
) == opt
->min
)
109 return strdup("yes");
112 m_option_type_t m_option_type_flag
= {
114 "need yes or no in config files",
127 static int parse_int(m_option_t
* opt
,char *name
, char *param
, void* dst
, int src
) {
133 return M_OPT_MISSING_PARAM
;
135 tmp_int
= strtol(param
, &endptr
, 0);
137 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "The %s option must be an integer: %s\n",name
, param
);
138 return M_OPT_INVALID
;
141 if ((opt
->flags
& M_OPT_MIN
) && (tmp_int
< opt
->min
)) {
142 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "The %s option must be >= %d: %s\n", name
, (int) opt
->min
, param
);
143 return M_OPT_OUT_OF_RANGE
;
146 if ((opt
->flags
& M_OPT_MAX
) && (tmp_int
> opt
->max
)) {
147 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "The %s option must be <= %d: %s\n",name
, (int) opt
->max
, param
);
148 return M_OPT_OUT_OF_RANGE
;
151 if(dst
) VAL(dst
) = tmp_int
;
156 static char* print_int(m_option_t
* opt
, void* val
) {
158 return dup_printf("%d",VAL(val
));
161 m_option_type_t m_option_type_int
= {
177 #define VAL(x) (*(float*)(x))
179 static int parse_float(m_option_t
* opt
,char *name
, char *param
, void* dst
, int src
) {
185 return M_OPT_MISSING_PARAM
;
187 tmp_float
= strtod(param
, &endptr
);
192 tmp_float
/= strtod(endptr
+1, &endptr
);
196 /* we also handle floats specified with
197 * non-locale decimal point ::atmos
200 tmp_float
-= 1.0/pow(10,strlen(endptr
+1)) * strtod(endptr
+1, &endptr
);
202 tmp_float
+= 1.0/pow(10,strlen(endptr
+1)) * strtod(endptr
+1, &endptr
);
207 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "The %s option must be a floating point "
208 "number or a ratio (numerator[:/]denominator): %s\n",name
, param
);
209 return M_OPT_INVALID
;
212 if (opt
->flags
& M_OPT_MIN
)
213 if (tmp_float
< opt
->min
) {
214 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "The %s option must be >= %f: %s\n", name
, opt
->min
, param
);
215 return M_OPT_OUT_OF_RANGE
;
218 if (opt
->flags
& M_OPT_MAX
)
219 if (tmp_float
> opt
->max
) {
220 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "The %s option must be <= %f: %s\n", name
, opt
->max
, param
);
221 return M_OPT_OUT_OF_RANGE
;
224 if(dst
) VAL(dst
) = tmp_float
;
228 static char* print_float(m_option_t
* opt
, void* val
) {
230 return dup_printf("%f",VAL(val
));
233 m_option_type_t m_option_type_float
= {
235 "floating point number or ratio (numerator[:/]denominator)",
246 ///////////// Position
248 #define VAL(x) (*(off_t*)(x))
250 static int parse_position(m_option_t
* opt
,char *name
, char *param
, void* dst
, int src
) {
255 return M_OPT_MISSING_PARAM
;
256 if (sscanf(param
, sizeof(off_t
) == sizeof(int) ?
257 "%d%c" : "%lld%c", &tmp_off
, &dummy
) != 1) {
258 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "The %s option must be an integer: %s\n",opt
->name
,param
);
259 return M_OPT_INVALID
;
262 if (opt
->flags
& M_OPT_MIN
)
263 if (tmp_off
< opt
->min
) {
264 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,
265 (sizeof(off_t
) == sizeof(int) ?
266 "The %s option must be >= %d: %s\n" :
267 "The %s option must be >= %lld: %s\n"),
268 name
, (off_t
) opt
->min
, param
);
269 return M_OPT_OUT_OF_RANGE
;
272 if (opt
->flags
& M_OPT_MAX
)
273 if (tmp_off
> opt
->max
) {
274 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,
275 (sizeof(off_t
) == sizeof(int) ?
276 "The %s option must be <= %d: %s\n" :
277 "The %s option must be <= %lld: %s\n"),
278 name
, (off_t
) opt
->max
, param
);
279 return M_OPT_OUT_OF_RANGE
;
287 static char* print_position(m_option_t
* opt
, void* val
) {
288 return dup_printf(sizeof(off_t
) == sizeof(int) ? "%d" : "%lld",VAL(val
));
291 m_option_type_t m_option_type_position
= {
308 #define VAL(x) (*(char**)(x))
310 static int parse_str(m_option_t
* opt
,char *name
, char *param
, void* dst
, int src
) {
314 return M_OPT_MISSING_PARAM
;
316 if ((opt
->flags
& M_OPT_MIN
) && (strlen(param
) < opt
->min
)) {
317 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Parameter must be >= %d chars: %s\n",
318 (int) opt
->min
, param
);
319 return M_OPT_OUT_OF_RANGE
;
322 if ((opt
->flags
& M_OPT_MAX
) && (strlen(param
) > opt
->max
)) {
323 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Parameter must be <= %d chars: %s\n",
324 (int) opt
->max
, param
);
325 return M_OPT_OUT_OF_RANGE
;
331 VAL(dst
) = strdup(param
);
338 static char* print_str(m_option_t
* opt
, void* val
) {
339 return (val
&& VAL(val
) && strlen(VAL(val
)) > 0) ? strdup(VAL(val
)) : NULL
;
342 static void copy_str(m_option_t
* opt
,void* dst
, void* src
) {
345 if(VAL(dst
)) free(VAL(dst
)); //FIXME!!!
347 VAL(dst
) = VAL(src
) ? strdup(VAL(src
)) : NULL
;
351 static void free_str(void* src
) {
354 free(VAL(src
)); //FIXME!!!
360 m_option_type_t m_option_type_string
= {
373 //////////// String list
375 #define LIST_SEPARATOR ','
377 #define VAL(x) (*(char***)(x))
385 static void free_str_list(void* dst
) {
389 if(!dst
|| !VAL(dst
)) return;
394 for(i
= 0 ; d
[i
] != NULL
; i
++)
401 static int str_list_add(char** add
, int n
,void* dst
,int pre
) {
402 char** lst
= VAL(dst
);
405 if(!dst
) return M_OPT_PARSER_ERR
;
408 for(ln
= 0 ; lst
&& lst
[ln
] ; ln
++)
411 lst
= realloc(lst
,(n
+ln
+1)*sizeof(char*));
414 memmove(&lst
[n
],lst
,(ln
+1)*sizeof(char*));
415 memcpy(lst
,add
,n
*sizeof(char*));
417 memcpy(&lst
[ln
],add
,(n
+1)*sizeof(char*));
426 static int str_list_del(char** del
, int n
,void* dst
) {
431 if(!dst
) return M_OPT_PARSER_ERR
;
434 for(ln
= 0 ; lst
&& lst
[ln
] ; ln
++)
438 for(i
= 0 ; del
[i
] != NULL
; i
++) {
439 idx
= strtol(del
[i
], &ep
, 0);
441 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Invalid index: %s\n",del
[i
]);
446 if(idx
< 0 || idx
>= ln
) {
447 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Index %ld is out of range.\n",idx
);
463 d
= calloc(s
+1,sizeof(char*));
464 for(i
= 0, n
= 0 ; i
< ln
; i
++) {
465 if(!lst
[i
]) continue;
478 static int parse_str_list(m_option_t
* opt
,char *name
, char *param
, void* dst
, int src
) {
479 int n
= 0,len
= strlen(opt
->name
);
480 char *ptr
= param
, *last_ptr
, **res
;
483 if(opt
->name
[len
-1] == '*' && ((int)strlen(name
) > len
- 1)) {
484 char* n
= &name
[len
-1];
485 if(strcasecmp(n
,"-add") == 0)
487 else if(strcasecmp(n
,"-pre") == 0)
489 else if(strcasecmp(n
,"-del") == 0)
491 else if(strcasecmp(n
,"-clr") == 0)
494 return M_OPT_UNKNOWN
;
504 // All other ops need a param
505 if (param
== NULL
|| strlen(param
) == 0)
506 return M_OPT_MISSING_PARAM
;
509 while(ptr
[0] != '\0') {
511 ptr
= strchr(ptr
,LIST_SEPARATOR
);
520 return M_OPT_INVALID
;
521 if( ((opt
->flags
& M_OPT_MIN
) && (n
< opt
->min
)) ||
522 ((opt
->flags
& M_OPT_MAX
) && (n
> opt
->max
)) )
523 return M_OPT_OUT_OF_RANGE
;
527 res
= malloc((n
+2)*sizeof(char*));
533 ptr
= strchr(ptr
,LIST_SEPARATOR
);
535 res
[n
] = strdup(last_ptr
);
539 len
= ptr
- last_ptr
;
540 res
[n
] = (char*)malloc(len
+ 1);
541 if(len
) strncpy(res
[n
],last_ptr
,len
);
550 return str_list_add(res
,n
,dst
,0);
552 return str_list_add(res
,n
,dst
,1);
554 return str_list_del(res
,n
,dst
);
564 static void copy_str_list(m_option_t
* opt
,void* dst
, void* src
) {
568 if(!(dst
&& src
)) return;
579 for(n
= 0 ; s
[n
] != NULL
; n
++)
581 d
= (char**)malloc((n
+1)*sizeof(char*));
583 d
[n
] = s
[n
] ? strdup(s
[n
]) : NULL
;
588 static char* print_str_list(m_option_t
* opt
, void* src
) {
590 char *ret
= NULL
,*last
= NULL
;
593 if(!(src
&& VAL(src
))) return NULL
;
596 for(i
= 0 ; lst
[i
] ; i
++) {
598 ret
= dup_printf("%s,%s",last
,lst
[i
]);
601 ret
= strdup(lst
[i
]);
604 if(last
&& last
!= ret
) free(last
);
608 m_option_type_t m_option_type_string_list
= {
610 "A list of strings separated by ','\n"
611 "Option with a name ending in an * permits using the following suffix: \n"
612 "\t-add: Add the given parameters at the end of the list.\n"
613 "\t-pre: Add the given parameters at the begining of the list.\n"
614 "\t-del: Remove the entry at the given indices.\n"
615 "\t-clr: Clear the list.\n"
616 "e.g: -vop-add flip,mirror -vop-del 2,5\n",
618 M_OPT_TYPE_DYNAMIC
| M_OPT_TYPE_ALLOW_WILDCARD
,
628 /////////////////// Func based options
630 // A chained list to save the various calls for func_param and func_full
631 typedef struct m_func_save m_func_save_t
;
639 #define VAL(x) (*(m_func_save_t**)(x))
641 static void free_func_pf(void* src
) {
651 if(s
->param
) free(s
->param
);
658 // Parser for func_param and func_full
659 static int parse_func_pf(m_option_t
* opt
,char *name
, char *param
, void* dst
, int src
) {
665 s
= (m_func_save_t
*)calloc(1,sizeof(m_func_save_t
));
666 s
->name
= strdup(name
);
667 s
->param
= param
? strdup(param
) : NULL
;
671 for( ; p
->next
!= NULL
; p
= p
->next
)
680 static void copy_func_pf(m_option_t
* opt
,void* dst
, void* src
) {
681 m_func_save_t
*d
= NULL
, *s
,* last
= NULL
;
683 if(!(dst
&& src
)) return;
690 d
= (m_func_save_t
*)malloc(sizeof(m_func_save_t
));
691 d
->name
= strdup(s
->name
);
692 d
->param
= s
->param
? strdup(s
->param
) : NULL
;
704 /////////////////// Func_param
706 static void set_func_param(m_option_t
* opt
, void* dst
, void* src
) {
715 if(opt
->priv
) ((m_opt_default_func_t
)opt
->priv
)(opt
,opt
->name
);
716 for( ; s
!= NULL
; s
= s
->next
)
717 ((m_opt_func_param_t
) opt
->p
)(opt
,s
->param
);
720 m_option_type_t m_option_type_func_param
= {
723 sizeof(m_func_save_t
*),
727 NULL
, // Nothing to do on save
733 /////////////////// Func_full
735 static void set_func_full(m_option_t
* opt
, void* dst
, void* src
) {
740 for(s
= VAL(src
) ; s
; s
= s
->next
) {
742 if(opt
->priv
) ((m_opt_default_func_t
)opt
->priv
)(opt
,s
->name
);
743 ((m_opt_func_full_t
) opt
->p
)(opt
,s
->name
,s
->param
);
747 m_option_type_t m_option_type_func_full
= {
750 sizeof(m_func_save_t
*),
751 M_OPT_TYPE_ALLOW_WILDCARD
|M_OPT_TYPE_INDIRECT
,
754 NULL
, // Nothing to do on save
763 #define VAL(x) (*(int*)(x))
765 static int parse_func(m_option_t
* opt
,char *name
, char *param
, void* dst
, int src
) {
771 static void set_func(m_option_t
* opt
,void* dst
, void* src
) {
773 if(opt
->priv
) ((m_opt_default_func_t
)opt
->priv
)(opt
,opt
->name
);
774 for(i
= 0 ; i
< VAL(src
) ; i
++)
775 ((m_opt_func_t
) opt
->p
)(opt
);
778 m_option_type_t m_option_type_func
= {
785 NULL
, // Nothing to do on save
791 /////////////////// Print
793 static int parse_print(m_option_t
* opt
,char *name
, char *param
, void* dst
, int src
) {
794 if(opt
->type
->flags
&M_OPT_TYPE_INDIRECT
)
795 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "%s", *(char **) opt
->p
);
797 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "%s", (char *) opt
->p
);
799 if(opt
->priv
== NULL
)
804 m_option_type_t m_option_type_print
= {
817 m_option_type_t m_option_type_print_indirect
= {
831 /////////////////////// Subconfig
833 #define VAL(x) (*(char***)(x))
835 static int parse_subconf(m_option_t
* opt
,char *name
, char *param
, void* dst
, int src
) {
844 if (param
== NULL
|| strlen(param
) == 0)
845 return M_OPT_MISSING_PARAM
;
847 subparam
= malloc(strlen(param
)+1);
848 subopt
= malloc(strlen(param
)+1);
849 p
= strdup(param
); // In case that param is a static string (cf man strtok)
853 token
= strtok(p
, (char *)&(":"));
858 subopt
[0] = subparam
[0] = 0;
860 sscanf_ret
= sscanf(token
, "%[^=]=%[^:]", subopt
, subparam
);
862 mp_msg(MSGT_CFGPARSER
, MSGL_DBG3
, "token: '%s', subopt='%s', subparam='%s' (ret: %d)\n", token
, subopt
, subparam
, sscanf_ret
);
868 for(i
= 0 ; subopts
[i
].name
; i
++) {
869 if(!strcmp(subopts
[i
].name
,subopt
)) break;
871 if(!subopts
[i
].name
) {
872 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Option %s: Unknown suboption %s\n",name
,subopt
);
873 return M_OPT_UNKNOWN
;
875 r
= m_option_parse(&subopts
[i
],subopt
,
876 subparam
[0] == 0 ? NULL
: subparam
,NULL
,src
);
879 lst
= (char**)realloc(lst
,2 * (nr
+2) * sizeof(char*));
880 lst
[2*nr
] = strdup(subopt
);
881 lst
[2*nr
+1] = subparam
[0] == 0 ? NULL
: strdup(subparam
);
882 memset(&lst
[2*(nr
+1)],0,2*sizeof(char*));
887 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Invalid subconfig argument! ('%s')\n", token
);
888 return M_OPT_INVALID
;
890 token
= strtok(NULL
, (char *)&(":"));
902 m_option_type_t m_option_type_subconfig
= {
904 "The syntax is -option opt1=foo:flag:opt2=blah",
906 M_OPT_TYPE_HAS_CHILD
,
915 #include "libmpcodecs/img_format.h"
917 /* FIXME: snyc with img_format.h */
921 } mp_imgfmt_list
[] = {
922 {"444p", IMGFMT_444P
},
923 {"422p", IMGFMT_422P
},
924 {"411p", IMGFMT_411P
},
925 {"yuy2", IMGFMT_YUY2
},
926 {"uyvy", IMGFMT_UYVY
},
927 {"yvu9", IMGFMT_YVU9
},
928 {"if09", IMGFMT_IF09
},
929 {"yv12", IMGFMT_YV12
},
930 {"i420", IMGFMT_I420
},
931 {"iyuv", IMGFMT_IYUV
},
932 {"clpl", IMGFMT_CLPL
},
933 {"hm12", IMGFMT_NV12
},
934 {"y800", IMGFMT_Y800
},
936 {"nv12", IMGFMT_NV12
},
937 {"nv21", IMGFMT_NV21
},
938 {"bgr24", IMGFMT_BGR24
},
939 {"bgr32", IMGFMT_BGR32
},
940 {"bgr16", IMGFMT_BGR16
},
941 {"bgr15", IMGFMT_BGR15
},
942 {"bgr8", IMGFMT_BGR8
},
943 {"bgr4", IMGFMT_BGR4
},
944 {"bg4b", IMGFMT_BG4B
},
945 {"bgr1", IMGFMT_BGR1
},
946 {"rgb24", IMGFMT_RGB24
},
947 {"rgb32", IMGFMT_RGB32
},
948 {"rgb16", IMGFMT_RGB16
},
949 {"rgb15", IMGFMT_RGB15
},
950 {"rgb8", IMGFMT_RGB8
},
951 {"rgb4", IMGFMT_RGB4
},
952 {"rg4b", IMGFMT_RG4B
},
953 {"rgb1", IMGFMT_RGB1
},
957 static int parse_imgfmt(m_option_t
* opt
,char *name
, char *param
, void* dst
, int src
) {
961 if (param
== NULL
|| strlen(param
) == 0)
962 return M_OPT_MISSING_PARAM
;
964 if(!strcmp(param
,"help")) {
965 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "Available formats:");
966 for(i
= 0 ; mp_imgfmt_list
[i
].name
; i
++)
967 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, " %s",mp_imgfmt_list
[i
].name
);
968 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "\n");
972 if (sscanf(param
, "0x%x", &fmt
) != 1)
974 for(i
= 0 ; mp_imgfmt_list
[i
].name
; i
++) {
975 if(!strcasecmp(param
,mp_imgfmt_list
[i
].name
)) {
976 fmt
=mp_imgfmt_list
[i
].fmt
;
980 if(!mp_imgfmt_list
[i
].name
) {
981 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Option %s: unknown format name: '%s'\n",name
,param
);
982 return M_OPT_INVALID
;
987 *((uint32_t*)dst
) = fmt
;
992 m_option_type_t m_option_type_imgfmt
= {
994 "Please report any missing colorspaces.",
1005 //// Objects (i.e. filters, etc) settings
1007 #include "m_struct.h"
1010 #define VAL(x) (*(m_obj_settings_t**)(x))
1012 static int find_obj_desc(char* name
,m_obj_list_t
* l
,m_struct_t
** ret
) {
1016 for(i
= 0 ; l
->list
[i
] ; i
++) {
1017 n
= M_ST_MB(char*,l
->list
[i
],l
->name_off
);
1018 if(!strcmp(n
,name
)) {
1019 *ret
= M_ST_MB(m_struct_t
*,l
->list
[i
],l
->desc_off
);
1026 static int get_obj_param(char* opt_name
,char* obj_name
, m_struct_t
* desc
,
1027 char* str
,int* nold
,int oldmax
,char** dst
) {
1032 eq
= strchr(str
,'=');
1038 if(p
[0] == '\0') p
= NULL
;
1040 opt
= m_option_list_find(desc
->fields
,str
);
1042 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Option %s: %s doesn't have a %s parameter.\n",opt_name
,obj_name
,str
);
1043 return M_OPT_UNKNOWN
;
1045 r
= m_option_parse(opt
,str
,p
,NULL
,M_CONFIG_FILE
);
1047 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Option %s: Error while parsing %s parameter %s (%s)\n",opt_name
,obj_name
,str
,p
);
1052 dst
[0] = strdup(str
);
1053 dst
[1] = p
? strdup(p
) : NULL
;
1057 if((*nold
) >= oldmax
) {
1058 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Option %s: %s has only %d params, so you can't give more than %d unnamed params.\n",
1059 opt_name
,obj_name
,oldmax
,oldmax
);
1060 return M_OPT_OUT_OF_RANGE
;
1062 opt
= &desc
->fields
[(*nold
)];
1063 r
= m_option_parse(opt
,opt
->name
,str
,NULL
,M_CONFIG_FILE
);
1065 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Option %s: Error while parsing %s parameter %s (%s)\n",opt_name
,obj_name
,opt
->name
,str
);
1069 dst
[0] = strdup(opt
->name
);
1070 dst
[1] = strdup(str
);
1077 static int get_obj_params(char* opt_name
, char* name
,char* params
,
1078 m_struct_t
* desc
,char separator
, char*** _ret
) {
1079 int n
= 0,nold
= 0, nopts
,r
;
1080 char* ptr
,*last_ptr
= params
,*eq
;
1083 if(!strcmp(params
,"help")) { // Help
1084 char min
[50],max
[50];
1086 printf("%s doesn't have any options.\n\n",name
);
1089 printf("\n Name Type Min Max\n\n");
1090 for(n
= 0 ; desc
->fields
[n
].name
; n
++) {
1091 m_option_t
* opt
= &desc
->fields
[n
];
1092 if(opt
->type
->flags
& M_OPT_TYPE_HAS_CHILD
) continue;
1093 if(opt
->flags
& M_OPT_MIN
)
1094 sprintf(min
,"%-8.0f",opt
->min
);
1097 if(opt
->flags
& M_OPT_MAX
)
1098 sprintf(max
,"%-8.0f",opt
->max
);
1101 printf(" %-20.20s %-15.15s %-10.10s %-10.10s\n",
1111 for(nopts
= 0 ; desc
->fields
[nopts
].name
; nopts
++)
1114 // TODO : Check that each opt can be parsed
1116 while(last_ptr
&& last_ptr
[0] != '\0') {
1117 ptr
= strchr(last_ptr
,separator
);
1119 r
= get_obj_param(opt_name
,name
,desc
,last_ptr
,&nold
,nopts
,NULL
);
1123 if(ptr
== last_ptr
) { // Empty field, count it and go on
1129 r
= get_obj_param(opt_name
,name
,desc
,last_ptr
,&nold
,nopts
,NULL
);
1136 if(!_ret
) // Just test
1139 ret
= malloc((n
+2)*2*sizeof(char*));
1143 while(last_ptr
&& last_ptr
[0] != '\0') {
1144 ptr
= strchr(last_ptr
,separator
);
1146 get_obj_param(opt_name
,name
,desc
,last_ptr
,&nold
,nopts
,&ret
[n
*2]);
1150 if(ptr
== last_ptr
) { // Empty field, count it and go on
1156 get_obj_param(opt_name
,name
,desc
,last_ptr
,&nold
,nopts
,&ret
[n
*2]);
1160 ret
[n
*2] = ret
[n
*2+1] = NULL
;
1166 static int parse_obj_params(m_option_t
* opt
,char *name
,
1167 char *param
, void* dst
, int src
) {
1170 m_obj_params_t
* p
= opt
->priv
;
1171 m_struct_t
* desc
= p
->desc
;
1172 char* cpy
= strdup(param
);
1174 // We need the object desc
1176 return M_OPT_INVALID
;
1178 r
= get_obj_params(name
,desc
->name
,cpy
,desc
,p
->separator
,&opts
);
1185 for(r
= 0 ; opts
[r
] ; r
+= 2)
1186 m_struct_set(desc
,dst
,opts
[r
],opts
[r
+1]);
1192 m_option_type_t m_option_type_obj_params
= {
1205 /// Some predefined types as a definition would be quite lengthy
1208 static m_span_t m_span_params_dflts
= { -1, -1 };
1209 static m_option_t m_span_params_fields
[] = {
1210 {"start", M_ST_OFF(m_span_t
,start
), CONF_TYPE_INT
, M_OPT_MIN
, 1 ,0, NULL
},
1211 {"end", M_ST_OFF(m_span_t
,end
), CONF_TYPE_INT
, M_OPT_MIN
, 1 ,0, NULL
},
1212 { NULL
, NULL
, 0, 0, 0, 0, NULL
}
1214 static struct m_struct_st m_span_opts
= {
1217 &m_span_params_dflts
,
1218 m_span_params_fields
1220 m_obj_params_t m_span_params_def
= {
1225 static int parse_obj_settings(char* opt
,char* str
,m_obj_list_t
* list
,
1226 m_obj_settings_t
**_ret
, int ret_n
) {
1228 char *param
,**plist
= NULL
;
1230 m_obj_settings_t
*ret
= _ret
? *_ret
: NULL
;
1233 // Now check that the object exists
1234 param
= strchr(str
,'=');
1238 if(strlen(param
) <= 0)
1243 if(!find_obj_desc(str
,list
,&desc
)) {
1244 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Option %s: %s doesn't exist.\n",opt
,str
);
1245 return M_OPT_INVALID
;
1250 plist
= calloc(4,sizeof(char*));
1251 plist
[0] = strdup("_oldargs_");
1252 plist
[1] = strdup(param
);
1254 r
= get_obj_params(opt
,str
,param
,desc
,':',_ret
? &plist
: NULL
);
1262 ret
= realloc(ret
,(ret_n
+2)*sizeof(m_obj_settings_t
));
1263 memset(&ret
[ret_n
],0,2*sizeof(m_obj_settings_t
));
1264 ret
[ret_n
].name
= strdup(str
);
1265 ret
[ret_n
].attribs
= plist
;
1271 static void free_obj_settings_list(void* dst
);
1273 static int obj_settings_list_del(char *opt_name
,char *param
,void* dst
, int src
) {
1274 char** str_list
= NULL
;
1275 int r
,i
,idx_max
= 0;
1276 char* rem_id
= "_removed_marker_";
1277 m_option_t list_opt
= {opt_name
, NULL
, CONF_TYPE_STRING_LIST
,
1279 m_obj_settings_t
* obj_list
= dst
? VAL(dst
) : NULL
;
1281 if(dst
&& !obj_list
) {
1282 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
, "Option %s: the list is empty.\n",opt_name
);
1284 } else if(obj_list
) {
1285 for(idx_max
= 0 ; obj_list
[idx_max
].name
!= NULL
; idx_max
++)
1289 r
= m_option_parse(&list_opt
,opt_name
,param
,&str_list
,src
);
1290 if(r
< 0 || !str_list
)
1293 for(r
= 0 ; str_list
[r
] ; r
++) {
1296 id
= strtol(str_list
[r
],&endptr
,0);
1297 if(endptr
== str_list
[r
]) {
1298 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
);
1299 m_option_free(&list_opt
,&str_list
);
1300 return M_OPT_INVALID
;
1302 if(!obj_list
) continue;
1303 if(id
>= idx_max
|| id
< -idx_max
) {
1304 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
, "Option %s: Index %d is out of range.\n",opt_name
,id
);
1309 free(obj_list
[id
].name
);
1310 free_str_list(&(obj_list
[id
].attribs
));
1311 obj_list
[id
].name
= rem_id
;
1315 m_option_free(&list_opt
,&str_list
);
1319 for(i
= 0 ; obj_list
[i
].name
; i
++) {
1320 while(obj_list
[i
].name
== rem_id
) {
1321 memmove(&obj_list
[i
],&obj_list
[i
+1],sizeof(m_obj_settings_t
)*(idx_max
- i
));
1325 obj_list
= realloc(obj_list
,sizeof(m_obj_settings_t
)*(idx_max
+1));
1326 VAL(dst
) = obj_list
;
1331 static int parse_obj_settings_list(m_option_t
* opt
,char *name
,
1332 char *param
, void* dst
, int src
) {
1333 int n
= 0,r
,len
= strlen(opt
->name
);
1335 char *ptr
, *last_ptr
;
1336 m_obj_settings_t
*res
= NULL
,*queue
= NULL
,*head
= NULL
;
1339 // We need the objects list
1341 return M_OPT_INVALID
;
1343 if(opt
->name
[len
-1] == '*' && ((int)strlen(name
) > len
- 1)) {
1344 char* n
= &name
[len
-1];
1345 if(strcasecmp(n
,"-add") == 0)
1347 else if(strcasecmp(n
,"-pre") == 0)
1349 else if(strcasecmp(n
,"-del") == 0)
1351 else if(strcasecmp(n
,"-clr") == 0)
1355 strncpy(prefix
,opt
->name
,len
-1);
1356 prefix
[len
-1] = '\0';
1357 mp_msg(MSGT_VFILTER
,MSGL_ERR
, "Option %s: unknown postfix %s\n"
1358 "Supported postfixes are:\n"
1360 " Append the given list to the current list\n\n"
1362 " Prepend the given list to the current list\n\n"
1363 " %3$s-del x,y,...\n"
1364 " Remove the given elements. Take the list element index (starting from 0).\n"
1365 " Negative index can be used (i.e. -1 is the last element)\n\n"
1367 " Clear the current list.\n",name
,n
,prefix
);
1369 return M_OPT_UNKNOWN
;
1373 // Clear the list ??
1376 free_obj_settings_list(dst
);
1380 if (param
== NULL
|| strlen(param
) == 0)
1381 return M_OPT_MISSING_PARAM
;
1385 if(dst
) head
= VAL(dst
);
1388 if(dst
) queue
= VAL(dst
);
1391 return obj_settings_list_del(name
,param
,dst
,src
);
1394 free_obj_settings_list(dst
);
1397 mp_msg(MSGT_VFILTER
,MSGL_ERR
, "Option %s: FIXME\n",name
);
1398 return M_OPT_UNKNOWN
;
1401 if(!strcmp(param
,"help")) {
1402 m_obj_list_t
* ol
= opt
->priv
;
1403 for(n
= 0 ; ol
->list
[n
] ; n
++)
1404 mp_msg(MSGT_VFILTER
,MSGL_INFO
," %-15s: %s\n",
1405 M_ST_MB(char*,ol
->list
[n
],ol
->name_off
),
1406 M_ST_MB(char*,ol
->list
[n
],ol
->info_off
));
1409 ptr
= str
= strdup(param
);
1411 while(ptr
[0] != '\0') {
1413 ptr
= strchr(ptr
,LIST_SEPARATOR
);
1415 r
= parse_obj_settings(name
,last_ptr
,opt
->priv
,dst
? &res
: NULL
,n
);
1424 r
= parse_obj_settings(name
,last_ptr
,opt
->priv
,dst
? &res
: NULL
,n
);
1434 return M_OPT_INVALID
;
1436 if( ((opt
->flags
& M_OPT_MIN
) && (n
< opt
->min
)) ||
1437 ((opt
->flags
& M_OPT_MAX
) && (n
> opt
->max
)) )
1438 return M_OPT_OUT_OF_RANGE
;
1443 for(qsize
= 0 ; queue
[qsize
].name
; qsize
++)
1445 res
= realloc(res
,(qsize
+n
+1)*sizeof(m_obj_settings_t
));
1446 memcpy(&res
[n
],queue
,(qsize
+1)*sizeof(m_obj_settings_t
));
1452 for(hsize
= 0 ; head
[hsize
].name
; hsize
++)
1454 head
= realloc(head
,(hsize
+n
+1)*sizeof(m_obj_settings_t
));
1455 memcpy(&head
[hsize
],res
,(n
+1)*sizeof(m_obj_settings_t
));
1464 static void free_obj_settings_list(void* dst
) {
1466 m_obj_settings_t
*d
;
1468 if(!dst
|| !VAL(dst
)) return;
1472 for(n
= 0 ; d
[n
].name
; n
++) {
1474 free_str_list(&(d
[n
].attribs
));
1481 static void copy_obj_settings_list(m_option_t
* opt
,void* dst
, void* src
) {
1482 m_obj_settings_t
*d
,*s
;
1491 free_obj_settings_list(dst
);
1496 for(n
= 0 ; s
[n
].name
; n
++)
1498 d
= malloc((n
+1)*sizeof(m_obj_settings_t
));
1499 for(n
= 0 ; s
[n
].name
; n
++) {
1500 d
[n
].name
= strdup(s
[n
].name
);
1501 d
[n
].attribs
= NULL
;
1502 copy_str_list(NULL
,&(d
[n
].attribs
),&(s
[n
].attribs
));
1505 d
[n
].attribs
= NULL
;
1509 m_option_type_t m_option_type_obj_settings_list
= {
1510 "Object settings list",
1512 sizeof(m_obj_settings_t
*),
1513 M_OPT_TYPE_DYNAMIC
|M_OPT_TYPE_ALLOW_WILDCARD
,
1514 parse_obj_settings_list
,
1516 copy_obj_settings_list
,
1517 copy_obj_settings_list
,
1518 copy_obj_settings_list
,
1519 free_obj_settings_list
,
1524 static int parse_obj_presets(m_option_t
* opt
,char *name
,
1525 char *param
, void* dst
, int src
) {
1526 m_obj_presets_t
* obj_p
= (m_obj_presets_t
*)opt
->priv
;
1527 m_struct_t
*in_desc
,*out_desc
;
1529 unsigned char* pre
= obj_p
->presets
;
1530 char* pre_name
= NULL
;
1533 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Option %s: Presets need a pointer to a m_obj_presets_t in the priv field.\n",name
);
1534 return M_OPT_PARSER_ERR
;
1538 return M_OPT_MISSING_PARAM
;
1540 in_desc
= obj_p
->in_desc
;
1541 out_desc
= obj_p
->out_desc
? obj_p
->out_desc
: obj_p
->in_desc
;
1544 if(!strcmp(param
,"help")) {
1545 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, "Available presets for %s->%s:",out_desc
->name
,name
);
1546 for(pre
= obj_p
->presets
;(pre_name
= M_ST_MB(char*,pre
,obj_p
->name_off
)) ;
1548 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, " %s",pre_name
);
1549 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "\n");
1553 for(pre_name
= M_ST_MB(char*,pre
,obj_p
->name_off
) ; pre_name
;
1554 pre
+= s
, pre_name
= M_ST_MB(char*,pre
,obj_p
->name_off
)) {
1555 if(!strcmp(pre_name
,param
)) break;
1558 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Option %s: There is no preset named %s\n"
1559 "Available presets are:",name
,param
);
1560 for(pre
= obj_p
->presets
;(pre_name
= M_ST_MB(char*,pre
,obj_p
->name_off
)) ;
1562 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, " %s",pre_name
);
1563 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "\n");
1564 return M_OPT_INVALID
;
1569 for(i
= 0 ; in_desc
->fields
[i
].name
; i
++) {
1570 m_option_t
* out_opt
= m_option_list_find(out_desc
->fields
,
1571 in_desc
->fields
[i
].name
);
1573 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
);
1574 return M_OPT_PARSER_ERR
;
1576 m_option_copy(out_opt
,M_ST_MB_P(dst
,out_opt
->p
),M_ST_MB_P(pre
,in_desc
->fields
[i
].p
));
1582 m_option_type_t m_option_type_obj_presets
= {
1595 static int parse_custom_url(m_option_t
* opt
,char *name
,
1596 char *url
, void* dst
, int src
) {
1598 char *ptr1
=NULL
, *ptr2
=NULL
, *ptr3
=NULL
;
1599 m_struct_t
* desc
= opt
->priv
;
1602 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Option %s: Custom URL needs a pointer to a m_struct_t in the priv field.\n",name
);
1603 return M_OPT_PARSER_ERR
;
1606 // extract the protocol
1607 ptr1
= strstr(url
, "://");
1610 if(m_option_list_find(desc
->fields
,"filename")) {
1611 m_struct_set(desc
,dst
,"filename",url
);
1614 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,"Option %s: URL doesn't have a valid protocol!\n",name
);
1615 return M_OPT_INVALID
;
1618 if(dst
&& m_option_list_find(desc
->fields
,"protocol")) {
1620 r
= m_struct_set(desc
,dst
,"protocol",url
);
1623 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Option %s: Error while setting protocol.\n",name
);
1632 // check if a username:password is given
1633 ptr2
= strstr(ptr1
, "@");
1634 ptr3
= strstr(ptr1
, "/");
1635 if( ptr3
!=NULL
&& ptr3
<ptr2
) {
1636 // it isn't really a username but rather a part of the path
1641 // We got something, at least a username...
1642 int len
= ptr2
-ptr1
;
1643 if(!m_option_list_find(desc
->fields
,"username")) {
1644 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
, "Option %s: This URL doesn't have a username part.\n",name
);
1647 ptr3
= strstr(ptr1
, ":");
1648 if( ptr3
!=NULL
&& ptr3
<ptr2
) {
1649 // We also have a password
1650 int len2
= ptr2
-ptr3
-1;
1651 if(!m_option_list_find(desc
->fields
,"password")) {
1652 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
, "Option %s: This URL doesn't have a password part.\n",name
);
1654 } else { // Username and password
1657 r
= m_struct_set(desc
,dst
,"username",ptr1
);
1660 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Option %s: Error while setting username.\n",name
);
1664 r
= m_struct_set(desc
,dst
,"password",ptr3
+1);
1667 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Option %s: Error while setting password.\n",name
);
1672 } else { // User name only
1674 r
= m_struct_set(desc
,dst
,"username",ptr1
);
1677 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Option %s: Error while setting username.\n",name
);
1686 // before looking for a port number check if we have an IPv6 type numeric address
1687 // in an IPv6 URL the numeric address should be inside square braces.
1688 ptr2
= strstr(ptr1
, "[");
1689 ptr3
= strstr(ptr1
, "]");
1690 if( ptr2
!=NULL
&& ptr3
!=NULL
) {
1691 // we have an IPv6 numeric address
1699 // look if the port is given
1700 ptr2
= strstr(ptr2
, ":");
1701 // If the : is after the first / it isn't the port
1702 ptr3
= strstr(ptr1
, "/");
1703 if(ptr3
&& ptr3
- ptr2
< 0) ptr2
= NULL
;
1706 // Look if a path is given
1709 // So we have an URL like http://www.hostname.com
1712 // We have an URL like http://www.hostname.com/file.txt
1716 // We have an URL beginning like http://www.hostname.com:1212
1717 // Get the port number
1718 if(!m_option_list_find(desc
->fields
,"port")) {
1719 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
, "Option %s: This URL doesn't have a port part.\n",name
);
1723 int p
= atoi(ptr2
+1);
1725 snprintf(tmp
,99,"%d",p
);
1726 r
= m_struct_set(desc
,dst
,"port",tmp
);
1728 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Option %s: Error while setting port.\n",name
);
1735 if( strstr(ptr1
, "]")!=NULL
) pos2
--;
1738 if(!m_option_list_find(desc
->fields
,"hostname")) {
1739 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
, "Option %s: This URL doesn't have a hostname part.\n",name
);
1742 char tmp
[pos2
-pos1
+1];
1743 strncpy(tmp
,ptr1
, pos2
-pos1
);
1744 tmp
[pos2
-pos1
] = '\0';
1745 r
= m_struct_set(desc
,dst
,"hostname",tmp
);
1747 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Option %s: Error while setting hostname.\n",name
);
1752 // Look if a path is given
1753 ptr2
= strstr(ptr1
, "/");
1755 // A path/filename is given
1756 // check if it's not a trailing '/'
1757 if( strlen(ptr2
)>1 ) {
1758 // copy the path/filename in the URL container
1759 if(!m_option_list_find(desc
->fields
,"filename")) {
1760 mp_msg(MSGT_CFGPARSER
, MSGL_WARN
, "Option %s: This URL doesn't have a hostname part.\n",name
);
1764 int l
= strlen(ptr2
+1) + 1;
1765 char* fname
= ptr2
+1;
1768 url_unescape_string(fname
,ptr2
+1);
1770 r
= m_struct_set(desc
,dst
,"filename",fname
);
1774 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "Option %s: Error while setting filename.\n",name
);
1784 /// TODO : Write the other needed funcs for 'normal' options
1785 m_option_type_t m_option_type_custom_url
= {