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 }
|
|
44
|
|
45 if(entry) {
|
|
46 *ret = entry;
|
|
47 return 1;
|
|
48 } else
|
|
49 return 0;
|
|
50 }
|
|
51
|
|
52 play_tree_t*
|
|
53 m_config_parse_mp_command_line(m_config_t *config, int argc, char **argv)
|
|
54 {
|
|
55 int i;
|
|
56 int tmp = 0;
|
|
57 char *opt;
|
|
58 int no_more_opts = 0;
|
|
59 play_tree_t *last_parent, *last_entry = NULL, *root;
|
|
60 void add_entry(play_tree_t *entry) {
|
|
61 if(last_entry == NULL)
|
|
62 play_tree_set_child(last_parent,entry);
|
|
63 else
|
|
64 play_tree_append_entry(last_entry,entry);
|
|
65 last_entry = entry;
|
|
66 }
|
|
67
|
|
68 #ifdef MP_DEBUG
|
|
69 assert(config != NULL);
|
|
70 assert(argv != NULL);
|
|
71 assert(argc >= 1);
|
|
72 #endif
|
|
73
|
|
74 config->mode = M_COMMAND_LINE;
|
|
75 mode = GLOBAL;
|
|
76 last_parent = root = play_tree_new();
|
|
77 /* in order to work recursion detection properly in parse_config_file */
|
|
78 ++recursion_depth;
|
|
79
|
|
80 for (i = 1; i < argc; i++) {
|
|
81 //next:
|
|
82 opt = argv[i];
|
|
83 /* check for -- (no more options id.) except --help! */
|
|
84 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) != 'h'))
|
|
85 {
|
|
86 no_more_opts = 1;
|
|
87 if (i+1 >= argc)
|
|
88 {
|
|
89 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "You added '--' but no filenames presented!\n");
|
|
90 goto err_out;
|
|
91 }
|
|
92 continue;
|
|
93 }
|
|
94 if((opt[0] == '{') && (opt[1] == '\0'))
|
|
95 {
|
|
96 play_tree_t* entry = play_tree_new();
|
|
97 UNSET_GLOBAL;
|
8175
|
98 if(last_parent->flags & PLAY_TREE_RND)
|
|
99 entry->flags |= PLAY_TREE_RND;
|
8164
|
100 if(last_entry == NULL) {
|
|
101 play_tree_set_child(last_parent,entry);
|
|
102 } else {
|
|
103 play_tree_append_entry(last_entry,entry);
|
|
104 last_entry = NULL;
|
|
105 }
|
|
106 last_parent = entry;
|
|
107 continue;
|
|
108 }
|
|
109
|
|
110 if((opt[0] == '}') && (opt[1] == '\0'))
|
|
111 {
|
|
112 if( ! last_parent || ! last_parent->parent) {
|
|
113 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too much }-\n");
|
|
114 goto err_out;
|
|
115 }
|
|
116 last_entry = last_parent;
|
|
117 last_parent = last_entry->parent;
|
|
118 continue;
|
|
119 }
|
|
120
|
|
121 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
|
|
122 {
|
|
123 /* remove trailing '-' */
|
|
124 opt++;
|
|
125
|
|
126 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
|
|
127 // We handle here some specific option
|
|
128 if(strcasecmp(opt,"list-options") == 0) {
|
|
129 m_config_print_option_list(config);
|
|
130 exit(1);
|
|
131 // Loop option when it apply to a group
|
|
132 } else if(strcasecmp(opt,"loop") == 0 &&
|
|
133 (! last_entry || last_entry->child) ) {
|
|
134 int l;
|
|
135 char* end;
|
8400
|
136 l = (i+1<argc) ? strtol(argv[i+1],&end,0) : 0;
|
9106
|
137 if(*end != '\0') {
|
|
138 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "The loop option must be an integer: %s\n",argv[i+1]);
|
8164
|
139 tmp = ERR_OUT_OF_RANGE;
|
9106
|
140 } else {
|
8164
|
141 play_tree_t* pt = last_entry ? last_entry : last_parent;
|
|
142 l = l <= 0 ? -1 : l;
|
|
143 pt->loop = l;
|
|
144 tmp = 1;
|
|
145 }
|
8452
|
146 } else if(strcasecmp(opt,"shuffle") == 0) {
|
8175
|
147 if(last_entry && last_entry->child)
|
|
148 last_entry->flags |= PLAY_TREE_RND;
|
|
149 else
|
|
150 last_parent->flags |= PLAY_TREE_RND;
|
8452
|
151 } else if(strcasecmp(opt,"noshuffle") == 0) {
|
8175
|
152 if(last_entry && last_entry->child)
|
|
153 last_entry->flags &= ~PLAY_TREE_RND;
|
|
154 else
|
|
155 last_parent->flags &= ~PLAY_TREE_RND;
|
8164
|
156 } else {
|
|
157 m_option_t* mp_opt = NULL;
|
|
158 play_tree_t* entry = NULL;
|
|
159
|
8458
|
160 tmp = is_entry_option(opt,(i+1<argc) ? argv[i + 1] : NULL,&entry);
|
8164
|
161 if(tmp > 0) { // It's an entry
|
|
162 if(entry) {
|
|
163 add_entry(entry);
|
8175
|
164 if((last_parent->flags & PLAY_TREE_RND) && entry->child)
|
|
165 entry->flags |= PLAY_TREE_RND;
|
8164
|
166 UNSET_GLOBAL;
|
|
167 } else if(mode == LOCAL) // Entry is empty we have to drop his params
|
|
168 mode = DROP_LOCAL;
|
|
169 } else if(tmp == 0) { // 'normal' options
|
|
170 mp_opt = m_config_get_option(config,opt);
|
|
171 if (mp_opt != NULL) { // Option exist
|
|
172 if(mode == GLOBAL || (mp_opt->flags & M_OPT_GLOBAL))
|
8426
|
173 tmp = (i+1<argc) ? m_config_set_option(config, opt, argv[i + 1])
|
|
174 : m_config_set_option(config, opt, NULL);
|
8164
|
175 else {
|
8458
|
176 tmp = m_config_check_option(config, opt, (i+1<argc) ? argv[i + 1] : NULL);
|
8164
|
177 if(tmp >= 0 && mode != DROP_LOCAL) {
|
|
178 play_tree_t* pt = last_entry ? last_entry : last_parent;
|
|
179 play_tree_set_param(pt,opt, argv[i + 1]);
|
|
180 }
|
|
181 }
|
|
182 } else {
|
|
183 tmp = M_OPT_UNKNOW;
|
|
184 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Unknow option on the command line: %s\n",opt);
|
|
185 }
|
|
186 }
|
|
187 }
|
|
188
|
9792
|
189 if (tmp < 0) {
|
|
190 if (tmp == M_OPT_EXIT)
|
|
191 exit(0);
|
8164
|
192 goto err_out;
|
9792
|
193 }
|
8164
|
194 i += tmp;
|
|
195 }
|
|
196 else /* filename */
|
|
197 {
|
|
198 play_tree_t* entry = play_tree_new();
|
|
199 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]);
|
|
200 play_tree_add_file(entry,argv[i]);
|
|
201 // Lock stdin if it will be used as input
|
|
202 if(strcasecmp(argv[i],"-") == 0)
|
|
203 m_config_set_option(config,"use-stdin",NULL);
|
|
204 add_entry(entry);
|
|
205 UNSET_GLOBAL; // We start entry specific options
|
|
206
|
|
207 }
|
|
208 }
|
|
209
|
|
210 --recursion_depth;
|
|
211 if(last_parent != root)
|
|
212 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Missing }- ?\n");
|
|
213 return root;
|
|
214
|
|
215 err_out:
|
|
216 --recursion_depth;
|
|
217 play_tree_free(root,1);
|
|
218 return NULL;
|
|
219 }
|
|
220
|
|
221 #endif
|