comparison parser-mpcmd.c @ 8164:487cfc28525d

New config system + cleanup of header inter dependency
author albeu
date Tue, 12 Nov 2002 01:56:42 +0000
parents
children bf5d1da56ecd
comparison
equal deleted inserted replaced
8163:51e5033ee687 8164:487cfc28525d
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;
180 if(last_entry == NULL) {
181 play_tree_set_child(last_parent,entry);
182 } else {
183 play_tree_append_entry(last_entry,entry);
184 last_entry = NULL;
185 }
186 last_parent = entry;
187 continue;
188 }
189
190 if((opt[0] == '}') && (opt[1] == '\0'))
191 {
192 if( ! last_parent || ! last_parent->parent) {
193 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too much }-\n");
194 goto err_out;
195 }
196 last_entry = last_parent;
197 last_parent = last_entry->parent;
198 continue;
199 }
200
201 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
202 {
203 /* remove trailing '-' */
204 opt++;
205
206 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
207 // We handle here some specific option
208 if(strcasecmp(opt,"list-options") == 0) {
209 m_config_print_option_list(config);
210 exit(1);
211 // Loop option when it apply to a group
212 } else if(strcasecmp(opt,"loop") == 0 &&
213 (! last_entry || last_entry->child) ) {
214 int l;
215 char* end;
216 l = strtol(argv[i+1],&end,0);
217 if(!end)
218 tmp = ERR_OUT_OF_RANGE;
219 else {
220 play_tree_t* pt = last_entry ? last_entry : last_parent;
221 l = l <= 0 ? -1 : l;
222 pt->loop = l;
223 tmp = 1;
224 }
225 } else {
226 m_option_t* mp_opt = NULL;
227 play_tree_t* entry = NULL;
228
229 tmp = is_entry_option(opt,argv[i + 1],&entry);
230 if(tmp > 0) { // It's an entry
231 if(entry) {
232 add_entry(entry);
233 UNSET_GLOBAL;
234 } else if(mode == LOCAL) // Entry is empty we have to drop his params
235 mode = DROP_LOCAL;
236 } else if(tmp == 0) { // 'normal' options
237 mp_opt = m_config_get_option(config,opt);
238 if (mp_opt != NULL) { // Option exist
239 if(mode == GLOBAL || (mp_opt->flags & M_OPT_GLOBAL))
240 tmp = m_config_set_option(config, opt, argv[i + 1]);
241 else {
242 tmp = m_config_check_option(config, opt, argv[i + 1]);
243 if(tmp >= 0 && mode != DROP_LOCAL) {
244 play_tree_t* pt = last_entry ? last_entry : last_parent;
245 play_tree_set_param(pt,opt, argv[i + 1]);
246 }
247 }
248 } else {
249 tmp = M_OPT_UNKNOW;
250 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Unknow option on the command line: %s\n",opt);
251 }
252 }
253 }
254
255 if (tmp < 0)
256 goto err_out;
257 i += tmp;
258 }
259 else /* filename */
260 {
261 play_tree_t* entry = play_tree_new();
262 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]);
263 play_tree_add_file(entry,argv[i]);
264 // Lock stdin if it will be used as input
265 if(strcasecmp(argv[i],"-") == 0)
266 m_config_set_option(config,"use-stdin",NULL);
267 add_entry(entry);
268 UNSET_GLOBAL; // We start entry specific options
269
270 }
271 }
272
273 --recursion_depth;
274 if(last_parent != root)
275 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Missing }- ?\n");
276 return root;
277
278 err_out:
279 --recursion_depth;
280 play_tree_free(root,1);
281 return NULL;
282 }
283
284 #endif