8164
|
1
|
|
2 #include "config.h"
|
|
3
|
|
4 #ifdef NEW_CONFIG
|
|
5
|
|
6 #include <stdio.h>
|
|
7 #include <stdlib.h>
|
|
8 #include <string.h>
|
|
9 #include <errno.h>
|
|
10
|
|
11 #ifdef MP_DEBUG
|
|
12 #include <assert.h>
|
|
13 #endif
|
|
14
|
|
15 #include "mp_msg.h"
|
|
16 #include "m_option.h"
|
|
17 #include "m_config.h"
|
|
18 #include "playtree.h"
|
|
19
|
|
20 static int recursion_depth = 0;
|
|
21 static int mode = 0;
|
|
22
|
|
23 #define GLOBAL 0
|
|
24 #define LOCAL 1
|
|
25 #define DROP_LOCAL 2
|
|
26
|
|
27 #define UNSET_GLOBAL (mode = LOCAL)
|
|
28 // Use this 1 if you want to have only global option (no per file option)
|
|
29 // #define UNSET_GLOBAL (mode = GLOBAL)
|
|
30
|
|
31
|
|
32 static int is_entry_option(char *opt, char *param, play_tree_t** ret) {
|
|
33 play_tree_t* entry = NULL;
|
|
34
|
|
35 *ret = NULL;
|
|
36
|
|
37 if(strcasecmp(opt,"playlist") == 0) { // We handle playlist here
|
|
38 if(!param)
|
|
39 return M_OPT_MISSING_PARAM;
|
|
40 entry = parse_playlist_file(param);
|
|
41 if(!entry)
|
|
42 return 1;
|
|
43 } else if(strcasecmp(opt,"vcd") == 0) {
|
|
44 char* s;
|
|
45 if(!param)
|
|
46 return M_OPT_MISSING_PARAM;
|
|
47 s = (char*)malloc((strlen(param) + 6 + 1)*sizeof(char));
|
|
48 sprintf(s,"vcd://%s",param);
|
|
49 entry = play_tree_new();
|
|
50 play_tree_add_file(entry,s);
|
|
51 free(s);
|
|
52 } else if(strcasecmp(opt,"dvd") == 0) {
|
|
53 char* s;
|
|
54 if(!param)
|
|
55 return M_OPT_MISSING_PARAM;
|
|
56 s = (char*)malloc((strlen(param) + 6 + 1)*sizeof(char));
|
|
57 sprintf(s,"dvd://%s",param);
|
|
58 entry = play_tree_new();
|
|
59 play_tree_add_file(entry,s);
|
|
60 free(s);
|
|
61 } else if(strcasecmp(opt,"tv") == 0) {
|
|
62 char *s,*pr,*prs;
|
|
63 char *ps,*pe,*channel=NULL;
|
|
64 char *as;
|
|
65 int on=0;
|
|
66
|
|
67 if(!param)
|
|
68 return M_OPT_MISSING_PARAM;
|
|
69 ps = param;
|
|
70 pe = strchr(param,':');
|
|
71 pr = prs = (char*)malloc((strlen(param)+1)*sizeof(char));
|
|
72 pr[0] = '\0';
|
|
73 while(ps) {
|
|
74 if(!pe)
|
|
75 pe = ps + strlen(ps);
|
|
76
|
|
77 as = strchr(ps,'=');
|
|
78 if(as && as[1] != '\0' && pe-as > 0)
|
|
79 as++;
|
|
80 else
|
|
81 as = NULL;
|
|
82 if( !as && pe-ps == 2 && strncasecmp("on",ps,2) == 0 )
|
|
83 on = 1;
|
|
84 else if(as && as-ps == 8 && strncasecmp("channel",ps,6) == 0 && pe-as > 0) {
|
|
85 channel = (char*)realloc(channel,(pe-as+1)*sizeof(char));
|
|
86 strncpy(channel,as,pe-as);
|
|
87 channel[pe-as] = '\0';
|
|
88 } else if(pe-ps > 0) {
|
|
89 if(prs != pr) {
|
|
90 prs[0] = ':';
|
|
91 prs++;
|
|
92 }
|
|
93 strncpy(prs,ps,pe-ps);
|
|
94 prs += pe-ps;
|
|
95 prs[0] = '\0';
|
|
96 }
|
|
97
|
|
98 if(pe[0] != '\0') {
|
|
99 ps = pe+1;
|
|
100 pe = strchr(ps,':');
|
|
101 } else
|
|
102 ps = NULL;
|
|
103 }
|
|
104
|
|
105 if(on) {
|
|
106 int l=5;
|
|
107
|
|
108 if(channel)
|
|
109 l += strlen(channel);
|
|
110 s = (char*) malloc((l+1)*sizeof(char));
|
|
111 if(channel)
|
|
112 sprintf(s,"tv://%s",channel);
|
|
113 else
|
|
114 sprintf(s,"tv://");
|
|
115 entry = play_tree_new();
|
|
116 play_tree_add_file(entry,s);
|
|
117 if(strlen(pr) > 0)
|
|
118 play_tree_set_param(entry,"tv",pr);
|
|
119 free(s);
|
|
120 }
|
|
121 free(pr);
|
|
122 if(channel)
|
|
123 free(channel);
|
|
124
|
|
125 }
|
|
126
|
|
127 if(entry) {
|
|
128 *ret = entry;
|
|
129 return 1;
|
|
130 } else
|
|
131 return 0;
|
|
132 }
|
|
133
|
|
134 play_tree_t*
|
|
135 m_config_parse_mp_command_line(m_config_t *config, int argc, char **argv)
|
|
136 {
|
|
137 int i;
|
|
138 int tmp = 0;
|
|
139 char *opt;
|
|
140 int no_more_opts = 0;
|
|
141 play_tree_t *last_parent, *last_entry = NULL, *root;
|
|
142 void add_entry(play_tree_t *entry) {
|
|
143 if(last_entry == NULL)
|
|
144 play_tree_set_child(last_parent,entry);
|
|
145 else
|
|
146 play_tree_append_entry(last_entry,entry);
|
|
147 last_entry = entry;
|
|
148 }
|
|
149
|
|
150 #ifdef MP_DEBUG
|
|
151 assert(config != NULL);
|
|
152 assert(argv != NULL);
|
|
153 assert(argc >= 1);
|
|
154 #endif
|
|
155
|
|
156 config->mode = M_COMMAND_LINE;
|
|
157 mode = GLOBAL;
|
|
158 last_parent = root = play_tree_new();
|
|
159 /* in order to work recursion detection properly in parse_config_file */
|
|
160 ++recursion_depth;
|
|
161
|
|
162 for (i = 1; i < argc; i++) {
|
|
163 //next:
|
|
164 opt = argv[i];
|
|
165 /* check for -- (no more options id.) except --help! */
|
|
166 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) != 'h'))
|
|
167 {
|
|
168 no_more_opts = 1;
|
|
169 if (i+1 >= argc)
|
|
170 {
|
|
171 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "You added '--' but no filenames presented!\n");
|
|
172 goto err_out;
|
|
173 }
|
|
174 continue;
|
|
175 }
|
|
176 if((opt[0] == '{') && (opt[1] == '\0'))
|
|
177 {
|
|
178 play_tree_t* entry = play_tree_new();
|
|
179 UNSET_GLOBAL;
|
8175
|
180 if(last_parent->flags & PLAY_TREE_RND)
|
|
181 entry->flags |= PLAY_TREE_RND;
|
8164
|
182 if(last_entry == NULL) {
|
|
183 play_tree_set_child(last_parent,entry);
|
|
184 } else {
|
|
185 play_tree_append_entry(last_entry,entry);
|
|
186 last_entry = NULL;
|
|
187 }
|
|
188 last_parent = entry;
|
|
189 continue;
|
|
190 }
|
|
191
|
|
192 if((opt[0] == '}') && (opt[1] == '\0'))
|
|
193 {
|
|
194 if( ! last_parent || ! last_parent->parent) {
|
|
195 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too much }-\n");
|
|
196 goto err_out;
|
|
197 }
|
|
198 last_entry = last_parent;
|
|
199 last_parent = last_entry->parent;
|
|
200 continue;
|
|
201 }
|
|
202
|
|
203 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
|
|
204 {
|
|
205 /* remove trailing '-' */
|
|
206 opt++;
|
|
207
|
|
208 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
|
|
209 // We handle here some specific option
|
|
210 if(strcasecmp(opt,"list-options") == 0) {
|
|
211 m_config_print_option_list(config);
|
|
212 exit(1);
|
|
213 // Loop option when it apply to a group
|
|
214 } else if(strcasecmp(opt,"loop") == 0 &&
|
|
215 (! last_entry || last_entry->child) ) {
|
|
216 int l;
|
|
217 char* end;
|
8400
|
218 l = (i+1<argc) ? strtol(argv[i+1],&end,0) : 0;
|
8164
|
219 if(!end)
|
|
220 tmp = ERR_OUT_OF_RANGE;
|
|
221 else {
|
|
222 play_tree_t* pt = last_entry ? last_entry : last_parent;
|
|
223 l = l <= 0 ? -1 : l;
|
|
224 pt->loop = l;
|
|
225 tmp = 1;
|
|
226 }
|
8175
|
227 } else if(strcasecmp(opt,"rnd") == 0) {
|
|
228 if(last_entry && last_entry->child)
|
|
229 last_entry->flags |= PLAY_TREE_RND;
|
|
230 else
|
|
231 last_parent->flags |= PLAY_TREE_RND;
|
|
232 } else if(strcasecmp(opt,"nornd") == 0) {
|
|
233 if(last_entry && last_entry->child)
|
|
234 last_entry->flags &= ~PLAY_TREE_RND;
|
|
235 else
|
|
236 last_parent->flags &= ~PLAY_TREE_RND;
|
8164
|
237 } else {
|
|
238 m_option_t* mp_opt = NULL;
|
|
239 play_tree_t* entry = NULL;
|
|
240
|
|
241 tmp = is_entry_option(opt,argv[i + 1],&entry);
|
|
242 if(tmp > 0) { // It's an entry
|
|
243 if(entry) {
|
|
244 add_entry(entry);
|
8175
|
245 if((last_parent->flags & PLAY_TREE_RND) && entry->child)
|
|
246 entry->flags |= PLAY_TREE_RND;
|
8164
|
247 UNSET_GLOBAL;
|
|
248 } else if(mode == LOCAL) // Entry is empty we have to drop his params
|
|
249 mode = DROP_LOCAL;
|
|
250 } else if(tmp == 0) { // 'normal' options
|
|
251 mp_opt = m_config_get_option(config,opt);
|
|
252 if (mp_opt != NULL) { // Option exist
|
|
253 if(mode == GLOBAL || (mp_opt->flags & M_OPT_GLOBAL))
|
|
254 tmp = m_config_set_option(config, opt, argv[i + 1]);
|
|
255 else {
|
|
256 tmp = m_config_check_option(config, opt, argv[i + 1]);
|
|
257 if(tmp >= 0 && mode != DROP_LOCAL) {
|
|
258 play_tree_t* pt = last_entry ? last_entry : last_parent;
|
|
259 play_tree_set_param(pt,opt, argv[i + 1]);
|
|
260 }
|
|
261 }
|
|
262 } else {
|
|
263 tmp = M_OPT_UNKNOW;
|
|
264 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Unknow option on the command line: %s\n",opt);
|
|
265 }
|
|
266 }
|
|
267 }
|
|
268
|
|
269 if (tmp < 0)
|
|
270 goto err_out;
|
|
271 i += tmp;
|
|
272 }
|
|
273 else /* filename */
|
|
274 {
|
|
275 play_tree_t* entry = play_tree_new();
|
|
276 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]);
|
|
277 play_tree_add_file(entry,argv[i]);
|
|
278 // Lock stdin if it will be used as input
|
|
279 if(strcasecmp(argv[i],"-") == 0)
|
|
280 m_config_set_option(config,"use-stdin",NULL);
|
|
281 add_entry(entry);
|
|
282 UNSET_GLOBAL; // We start entry specific options
|
|
283
|
|
284 }
|
|
285 }
|
|
286
|
|
287 --recursion_depth;
|
|
288 if(last_parent != root)
|
|
289 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Missing }- ?\n");
|
|
290 return root;
|
|
291
|
|
292 err_out:
|
|
293 --recursion_depth;
|
|
294 play_tree_free(root,1);
|
|
295 return NULL;
|
|
296 }
|
|
297
|
|
298 #endif
|