Merge svn changes up to r30876
[mplayer/kovensky.git] / parser-mpcmd.c
blobc6d3b2daf326b30799429beb8c327ad2eaccaf43
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"
39 #include "osdep/macosx_finder_args.h"
41 static int recursion_depth = 0;
42 static int mode = 0;
44 #define GLOBAL 0
45 #define LOCAL 1
46 #define DROP_LOCAL 2
48 #define dvd_range(a) (a>0 && a<256)
49 #define UNSET_GLOBAL (mode = LOCAL)
50 // Use this 1 if you want to have only global option (no per file option)
51 // #define UNSET_GLOBAL (mode = GLOBAL)
54 static int is_entry_option(struct m_config *mconfig, char *opt, char *param,
55 play_tree_t** ret)
57 play_tree_t* entry = NULL;
59 *ret = NULL;
61 if(strcasecmp(opt,"playlist") == 0) { // We handle playlist here
62 if(!param)
63 return M_OPT_MISSING_PARAM;
65 entry = parse_playlist_file(mconfig, param);
66 if(!entry)
67 return -1;
68 else {
69 *ret=entry;
70 return 1;
73 return 0;
76 static inline void add_entry(play_tree_t **last_parentp,
77 play_tree_t **last_entryp, play_tree_t *entry) {
78 if(*last_entryp == NULL)
79 play_tree_set_child(*last_parentp,entry);
80 else
81 play_tree_append_entry(*last_entryp,entry);
82 *last_entryp = entry;
85 /// Setup the \ref Config from command line arguments and build a playtree.
86 /** \ingroup ConfigParsers
88 play_tree_t*
89 m_config_parse_mp_command_line(m_config_t *config, int argc, char **argv)
91 int i,j,start_title=-1,end_title=-1;
92 char *opt,*splitpos=NULL;
93 char entbuf[15];
94 int no_more_opts = 0;
95 int opt_exit = 0; // flag indicating whether mplayer should exit without playing anything
96 play_tree_t *last_parent, *last_entry = NULL, *root;
98 #ifdef MP_DEBUG
99 assert(config != NULL);
100 assert(argv != NULL);
101 assert(argc >= 1);
102 #endif
104 config->mode = M_COMMAND_LINE;
105 mode = GLOBAL;
106 #ifdef CONFIG_MACOSX_FINDER
107 root=macosx_finder_args(config, argc, argv);
108 if(root)
109 return root;
110 #endif
112 last_parent = root = play_tree_new();
113 /* in order to work recursion detection properly in parse_config_file */
114 ++recursion_depth;
116 for (i = 1; i < argc; i++) {
117 //next:
118 opt = argv[i];
119 /* check for -- (no more options id.) except --help! */
120 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) == 0))
122 no_more_opts = 1;
123 if (i+1 >= argc)
125 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, "'--' indicates no more options, but no filename was given on the command line.\n");
126 goto err_out;
128 continue;
130 if((opt[0] == '{') && (opt[1] == '\0'))
132 play_tree_t* entry = play_tree_new();
133 UNSET_GLOBAL;
134 if(last_parent->flags & PLAY_TREE_RND)
135 entry->flags |= PLAY_TREE_RND;
136 if(last_entry == NULL) {
137 play_tree_set_child(last_parent,entry);
138 } else {
139 play_tree_append_entry(last_entry,entry);
140 last_entry = NULL;
142 last_parent = entry;
143 continue;
146 if((opt[0] == '}') && (opt[1] == '\0'))
148 if( ! last_parent || ! last_parent->parent) {
149 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too much }-\n");
150 goto err_out;
152 last_entry = last_parent;
153 last_parent = last_entry->parent;
154 continue;
157 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
159 int tmp = 0;
160 /* remove trailing '-' */
161 opt++;
163 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
164 // We handle here some specific option
165 // Loop option when it apply to a group
166 if(strcasecmp(opt,"loop") == 0 &&
167 (! last_entry || last_entry->child) ) {
168 int l;
169 char* end = NULL;
170 l = (i+1<argc) ? strtol(argv[i+1],&end,0) : 0;
171 if(!end || *end != '\0') {
172 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, "The loop option must be an integer: %s\n", argv[i+1]);
173 tmp = ERR_OUT_OF_RANGE;
174 } else {
175 play_tree_t* pt = last_entry ? last_entry : last_parent;
176 l = l <= 0 ? -1 : l;
177 pt->loop = l;
178 tmp = 1;
180 } else if(strcasecmp(opt,"shuffle") == 0) {
181 if(last_entry && last_entry->child)
182 last_entry->flags |= PLAY_TREE_RND;
183 else
184 last_parent->flags |= PLAY_TREE_RND;
185 } else if(strcasecmp(opt,"noshuffle") == 0) {
186 if(last_entry && last_entry->child)
187 last_entry->flags &= ~PLAY_TREE_RND;
188 else
189 last_parent->flags &= ~PLAY_TREE_RND;
190 } else {
191 const m_option_t* mp_opt = NULL;
192 play_tree_t* entry = NULL;
194 tmp = is_entry_option(config, opt,(i+1<argc) ? argv[i + 1] : NULL,&entry);
195 if(tmp > 0) { // It's an entry
196 if(entry) {
197 add_entry(&last_parent,&last_entry,entry);
198 if((last_parent->flags & PLAY_TREE_RND) && entry->child)
199 entry->flags |= PLAY_TREE_RND;
200 UNSET_GLOBAL;
201 } else if(mode == LOCAL) // Entry is empty we have to drop his params
202 mode = DROP_LOCAL;
203 } else if(tmp == 0) { // 'normal' options
204 mp_opt = m_config_get_option(config,opt);
205 if (mp_opt != NULL) { // Option exist
206 if(mode == GLOBAL || (mp_opt->flags & M_OPT_GLOBAL))
207 tmp = (i+1<argc) ? m_config_set_option(config, opt, argv[i + 1])
208 : m_config_set_option(config, opt, NULL);
209 else {
210 tmp = m_config_check_option(config, opt, (i+1<argc) ? argv[i + 1] : NULL);
211 if(tmp >= 0 && mode != DROP_LOCAL) {
212 play_tree_t* pt = last_entry ? last_entry : last_parent;
213 play_tree_set_param(pt,opt, argv[i + 1]);
216 } else {
217 tmp = M_OPT_UNKNOWN;
218 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, "Unknown option on the command line: -%s\n", opt);
223 if (tmp <= M_OPT_EXIT) {
224 opt_exit = 1;
225 tmp = M_OPT_EXIT - tmp;
226 } else
227 if (tmp < 0) {
228 mp_tmsg(MSGT_CFGPARSER, MSGL_FATAL, "Error parsing option on the command line: -%s\n", opt);
229 goto err_out;
231 i += tmp;
233 else /* filename */
235 int is_dvdnav = strstr(argv[i],"dvdnav://") != NULL;
236 play_tree_t* entry = play_tree_new();
237 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]);
238 // if required expand DVD filename entries like dvd://1-3 into component titles
239 if ( strstr(argv[i],"dvd://") != NULL || is_dvdnav)
241 int offset = is_dvdnav ? 9 : 6;
242 splitpos=strstr(argv[i]+offset,"-");
243 if(splitpos != NULL)
245 start_title=strtol(argv[i]+offset,NULL,10);
246 if (start_title<0) { //entries like dvd://-2 start title implied 1
247 end_title=abs(start_title);
248 start_title=1;
249 } else {
250 end_title=strtol(splitpos+1,NULL,10);
253 if (dvd_range(start_title) && dvd_range(end_title) && (start_title<end_title))
255 for (j=start_title;j<=end_title;j++)
257 if (j!=start_title)
258 entry=play_tree_new();
259 snprintf(entbuf,sizeof(entbuf),is_dvdnav ? "dvdnav://%d" : "dvd://%d",j);
260 play_tree_add_file(entry,entbuf);
261 add_entry(&last_parent,&last_entry,entry);
262 last_entry = entry;
264 } else {
265 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, "Invalid play entry %s\n", argv[i]);
268 } else { // dvd:// or dvd://x entry
269 play_tree_add_file(entry,argv[i]);
271 } else {
272 play_tree_add_file(entry,argv[i]);
275 // Lock stdin if it will be used as input
276 if(strcasecmp(argv[i],"-") == 0)
277 m_config_set_option(config,"noconsolecontrols",NULL);
278 add_entry(&last_parent,&last_entry,entry);
279 UNSET_GLOBAL; // We start entry specific options
284 if (opt_exit)
285 goto err_out;
286 --recursion_depth;
287 if(last_parent != root)
288 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Missing }- ?\n");
289 return root;
291 err_out:
292 --recursion_depth;
293 play_tree_free(root,1);
294 return NULL;