Merge svn changes up to r30663
[mplayer/kovensky.git] / parser-mpcmd.c
blob38cac3e5a0ca0d485a0f98f7bb93b1aabdce831e
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 /// \file
20 /// \ingroup ConfigParsers Playtree
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
29 #ifdef MP_DEBUG
30 #include <assert.h>
31 #endif
33 #include "mp_msg.h"
34 #include "help_mp.h"
35 #include "m_option.h"
36 #include "m_config.h"
37 #include "playtree.h"
38 #include "parser-mpcmd.h"
40 static int recursion_depth = 0;
41 static int mode = 0;
43 #define GLOBAL 0
44 #define LOCAL 1
45 #define DROP_LOCAL 2
47 #define dvd_range(a) (a>0 && a<256)
48 #define UNSET_GLOBAL (mode = LOCAL)
49 // Use this 1 if you want to have only global option (no per file option)
50 // #define UNSET_GLOBAL (mode = GLOBAL)
53 static int is_entry_option(struct m_config *mconfig, char *opt, char *param,
54 play_tree_t** ret)
56 play_tree_t* entry = NULL;
58 *ret = NULL;
60 if(strcasecmp(opt,"playlist") == 0) { // We handle playlist here
61 if(!param)
62 return M_OPT_MISSING_PARAM;
64 entry = parse_playlist_file(mconfig, param);
65 if(!entry)
66 return -1;
67 else {
68 *ret=entry;
69 return 1;
72 return 0;
75 static inline void add_entry(play_tree_t **last_parentp,
76 play_tree_t **last_entryp, play_tree_t *entry) {
77 if(*last_entryp == NULL)
78 play_tree_set_child(*last_parentp,entry);
79 else
80 play_tree_append_entry(*last_entryp,entry);
81 *last_entryp = entry;
84 /// Setup the \ref Config from command line arguments and build a playtree.
85 /** \ingroup ConfigParsers
87 play_tree_t*
88 m_config_parse_mp_command_line(m_config_t *config, int argc, char **argv)
90 int i,j,start_title=-1,end_title=-1;
91 char *opt,*splitpos=NULL;
92 char entbuf[15];
93 int no_more_opts = 0;
94 int opt_exit = 0; // flag indicating whether mplayer should exit without playing anything
95 play_tree_t *last_parent, *last_entry = NULL, *root;
96 #ifdef CONFIG_MACOSX_FINDER
97 play_tree_t *macosx_finder_args(m_config_t *, int , char **);
98 #endif
100 #ifdef MP_DEBUG
101 assert(config != NULL);
102 assert(argv != NULL);
103 assert(argc >= 1);
104 #endif
106 config->mode = M_COMMAND_LINE;
107 mode = GLOBAL;
108 #ifdef CONFIG_MACOSX_FINDER
109 root=macosx_finder_args(config, argc, argv);
110 if(root)
111 return root;
112 #endif
114 last_parent = root = play_tree_new();
115 /* in order to work recursion detection properly in parse_config_file */
116 ++recursion_depth;
118 for (i = 1; i < argc; i++) {
119 //next:
120 opt = argv[i];
121 /* check for -- (no more options id.) except --help! */
122 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) == 0))
124 no_more_opts = 1;
125 if (i+1 >= argc)
127 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, "'--' indicates no more options, but no filename was given on the command line.\n");
128 goto err_out;
130 continue;
132 if((opt[0] == '{') && (opt[1] == '\0'))
134 play_tree_t* entry = play_tree_new();
135 UNSET_GLOBAL;
136 if(last_parent->flags & PLAY_TREE_RND)
137 entry->flags |= PLAY_TREE_RND;
138 if(last_entry == NULL) {
139 play_tree_set_child(last_parent,entry);
140 } else {
141 play_tree_append_entry(last_entry,entry);
142 last_entry = NULL;
144 last_parent = entry;
145 continue;
148 if((opt[0] == '}') && (opt[1] == '\0'))
150 if( ! last_parent || ! last_parent->parent) {
151 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too much }-\n");
152 goto err_out;
154 last_entry = last_parent;
155 last_parent = last_entry->parent;
156 continue;
159 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
161 int tmp = 0;
162 /* remove trailing '-' */
163 opt++;
165 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
166 // We handle here some specific option
167 // Loop option when it apply to a group
168 if(strcasecmp(opt,"loop") == 0 &&
169 (! last_entry || last_entry->child) ) {
170 int l;
171 char* end = NULL;
172 l = (i+1<argc) ? strtol(argv[i+1],&end,0) : 0;
173 if(!end || *end != '\0') {
174 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, "The loop option must be an integer: %s\n", argv[i+1]);
175 tmp = ERR_OUT_OF_RANGE;
176 } else {
177 play_tree_t* pt = last_entry ? last_entry : last_parent;
178 l = l <= 0 ? -1 : l;
179 pt->loop = l;
180 tmp = 1;
182 } else if(strcasecmp(opt,"shuffle") == 0) {
183 if(last_entry && last_entry->child)
184 last_entry->flags |= PLAY_TREE_RND;
185 else
186 last_parent->flags |= PLAY_TREE_RND;
187 } else if(strcasecmp(opt,"noshuffle") == 0) {
188 if(last_entry && last_entry->child)
189 last_entry->flags &= ~PLAY_TREE_RND;
190 else
191 last_parent->flags &= ~PLAY_TREE_RND;
192 } else {
193 const m_option_t* mp_opt = NULL;
194 play_tree_t* entry = NULL;
196 tmp = is_entry_option(config, opt,(i+1<argc) ? argv[i + 1] : NULL,&entry);
197 if(tmp > 0) { // It's an entry
198 if(entry) {
199 add_entry(&last_parent,&last_entry,entry);
200 if((last_parent->flags & PLAY_TREE_RND) && entry->child)
201 entry->flags |= PLAY_TREE_RND;
202 UNSET_GLOBAL;
203 } else if(mode == LOCAL) // Entry is empty we have to drop his params
204 mode = DROP_LOCAL;
205 } else if(tmp == 0) { // 'normal' options
206 mp_opt = m_config_get_option(config,opt);
207 if (mp_opt != NULL) { // Option exist
208 if(mode == GLOBAL || (mp_opt->flags & M_OPT_GLOBAL))
209 tmp = (i+1<argc) ? m_config_set_option(config, opt, argv[i + 1])
210 : m_config_set_option(config, opt, NULL);
211 else {
212 tmp = m_config_check_option(config, opt, (i+1<argc) ? argv[i + 1] : NULL);
213 if(tmp >= 0 && mode != DROP_LOCAL) {
214 play_tree_t* pt = last_entry ? last_entry : last_parent;
215 play_tree_set_param(pt,opt, argv[i + 1]);
218 } else {
219 tmp = M_OPT_UNKNOWN;
220 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, "Unknown option on the command line: -%s\n", opt);
225 if (tmp <= M_OPT_EXIT) {
226 opt_exit = 1;
227 tmp = M_OPT_EXIT - tmp;
228 } else
229 if (tmp < 0) {
230 mp_tmsg(MSGT_CFGPARSER, MSGL_FATAL, "Error parsing option on the command line: -%s\n", opt);
231 goto err_out;
233 i += tmp;
235 else /* filename */
237 int is_dvdnav = strstr(argv[i],"dvdnav://") != NULL;
238 play_tree_t* entry = play_tree_new();
239 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]);
240 // if required expand DVD filename entries like dvd://1-3 into component titles
241 if ( strstr(argv[i],"dvd://") != NULL || is_dvdnav)
243 int offset = is_dvdnav ? 9 : 6;
244 splitpos=strstr(argv[i]+offset,"-");
245 if(splitpos != NULL)
247 start_title=strtol(argv[i]+offset,NULL,10);
248 if (start_title<0) { //entries like dvd://-2 start title implied 1
249 end_title=abs(start_title);
250 start_title=1;
251 } else {
252 end_title=strtol(splitpos+1,NULL,10);
255 if (dvd_range(start_title) && dvd_range(end_title) && (start_title<end_title))
257 for (j=start_title;j<=end_title;j++)
259 if (j!=start_title)
260 entry=play_tree_new();
261 snprintf(entbuf,sizeof(entbuf),is_dvdnav ? "dvdnav://%d" : "dvd://%d",j);
262 play_tree_add_file(entry,entbuf);
263 add_entry(&last_parent,&last_entry,entry);
264 last_entry = entry;
266 } else {
267 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, "Invalid play entry %s\n", argv[i]);
270 } else { // dvd:// or dvd://x entry
271 play_tree_add_file(entry,argv[i]);
273 } else {
274 play_tree_add_file(entry,argv[i]);
277 // Lock stdin if it will be used as input
278 if(strcasecmp(argv[i],"-") == 0)
279 m_config_set_option(config,"noconsolecontrols",NULL);
280 add_entry(&last_parent,&last_entry,entry);
281 UNSET_GLOBAL; // We start entry specific options
286 if (opt_exit)
287 goto err_out;
288 --recursion_depth;
289 if(last_parent != root)
290 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Missing }- ?\n");
291 return root;
293 err_out:
294 --recursion_depth;
295 play_tree_free(root,1);
296 return NULL;