sync with en/mplayer.1 rev. 30822
[mplayer.git] / parser-mpcmd.c
blobb03bcfb05ac4b93f3199bca26d17d19320f55d98
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(char *opt, char *param, play_tree_t** ret) {
55 play_tree_t* entry = NULL;
57 *ret = NULL;
59 if(strcasecmp(opt,"playlist") == 0) { // We handle playlist here
60 if(!param)
61 return M_OPT_MISSING_PARAM;
63 entry = parse_playlist_file(param);
64 if(!entry)
65 return -1;
66 else {
67 *ret=entry;
68 return 1;
71 return 0;
74 static inline void add_entry(play_tree_t **last_parentp,
75 play_tree_t **last_entryp, play_tree_t *entry) {
76 if(*last_entryp == NULL)
77 play_tree_set_child(*last_parentp,entry);
78 else
79 play_tree_append_entry(*last_entryp,entry);
80 *last_entryp = entry;
83 /// Setup the \ref Config from command line arguments and build a playtree.
84 /** \ingroup ConfigParsers
86 play_tree_t*
87 m_config_parse_mp_command_line(m_config_t *config, int argc, char **argv)
89 int i,j,start_title=-1,end_title=-1;
90 char *opt,*splitpos=NULL;
91 char entbuf[15];
92 int no_more_opts = 0;
93 int opt_exit = 0; // flag indicating whether mplayer should exit without playing anything
94 play_tree_t *last_parent, *last_entry = NULL, *root;
96 #ifdef MP_DEBUG
97 assert(config != NULL);
98 assert(argv != NULL);
99 assert(argc >= 1);
100 #endif
102 config->mode = M_COMMAND_LINE;
103 mode = GLOBAL;
104 #ifdef CONFIG_MACOSX_FINDER
105 root=macosx_finder_args(config, argc, argv);
106 if(root)
107 return root;
108 #endif
110 last_parent = root = play_tree_new();
111 /* in order to work recursion detection properly in parse_config_file */
112 ++recursion_depth;
114 for (i = 1; i < argc; i++) {
115 //next:
116 opt = argv[i];
117 /* check for -- (no more options id.) except --help! */
118 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) == 0))
120 no_more_opts = 1;
121 if (i+1 >= argc)
123 mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_NoFileGivenOnCommandLine);
124 goto err_out;
126 continue;
128 if((opt[0] == '{') && (opt[1] == '\0'))
130 play_tree_t* entry = play_tree_new();
131 UNSET_GLOBAL;
132 if(last_parent->flags & PLAY_TREE_RND)
133 entry->flags |= PLAY_TREE_RND;
134 if(last_entry == NULL) {
135 play_tree_set_child(last_parent,entry);
136 } else {
137 play_tree_append_entry(last_entry,entry);
138 last_entry = NULL;
140 last_parent = entry;
141 continue;
144 if((opt[0] == '}') && (opt[1] == '\0'))
146 if( ! last_parent || ! last_parent->parent) {
147 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too much }-\n");
148 goto err_out;
150 last_entry = last_parent;
151 last_parent = last_entry->parent;
152 continue;
155 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
157 int tmp = 0;
158 /* remove trailing '-' */
159 opt++;
161 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
162 // We handle here some specific option
163 // Loop option when it apply to a group
164 if(strcasecmp(opt,"loop") == 0 &&
165 (! last_entry || last_entry->child) ) {
166 int l;
167 char* end = NULL;
168 l = (i+1<argc) ? strtol(argv[i+1],&end,0) : 0;
169 if(!end || *end != '\0') {
170 mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_TheLoopOptionMustBeAnInteger, argv[i+1]);
171 tmp = ERR_OUT_OF_RANGE;
172 } else {
173 play_tree_t* pt = last_entry ? last_entry : last_parent;
174 l = l <= 0 ? -1 : l;
175 pt->loop = l;
176 tmp = 1;
178 } else if(strcasecmp(opt,"shuffle") == 0) {
179 if(last_entry && last_entry->child)
180 last_entry->flags |= PLAY_TREE_RND;
181 else
182 last_parent->flags |= PLAY_TREE_RND;
183 } else if(strcasecmp(opt,"noshuffle") == 0) {
184 if(last_entry && last_entry->child)
185 last_entry->flags &= ~PLAY_TREE_RND;
186 else
187 last_parent->flags &= ~PLAY_TREE_RND;
188 } else {
189 const m_option_t* mp_opt = NULL;
190 play_tree_t* entry = NULL;
192 tmp = is_entry_option(opt,(i+1<argc) ? argv[i + 1] : NULL,&entry);
193 if(tmp > 0) { // It's an entry
194 if(entry) {
195 add_entry(&last_parent,&last_entry,entry);
196 if((last_parent->flags & PLAY_TREE_RND) && entry->child)
197 entry->flags |= PLAY_TREE_RND;
198 UNSET_GLOBAL;
199 } else if(mode == LOCAL) // Entry is empty we have to drop his params
200 mode = DROP_LOCAL;
201 } else if(tmp == 0) { // 'normal' options
202 mp_opt = m_config_get_option(config,opt);
203 if (mp_opt != NULL) { // Option exist
204 if(mode == GLOBAL || (mp_opt->flags & M_OPT_GLOBAL))
205 tmp = (i+1<argc) ? m_config_set_option(config, opt, argv[i + 1])
206 : m_config_set_option(config, opt, NULL);
207 else {
208 tmp = m_config_check_option(config, opt, (i+1<argc) ? argv[i + 1] : NULL);
209 if(tmp >= 0 && mode != DROP_LOCAL) {
210 play_tree_t* pt = last_entry ? last_entry : last_parent;
211 play_tree_set_param(pt,opt, argv[i + 1]);
214 } else {
215 tmp = M_OPT_UNKNOWN;
216 mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_UnknownOptionOnCommandLine, opt);
221 if (tmp <= M_OPT_EXIT) {
222 opt_exit = 1;
223 tmp = M_OPT_EXIT - tmp;
224 } else
225 if (tmp < 0) {
226 mp_msg(MSGT_CFGPARSER, MSGL_FATAL, MSGTR_ErrorParsingOptionOnCommandLine, opt);
227 goto err_out;
229 i += tmp;
231 else /* filename */
233 int is_dvdnav = strstr(argv[i],"dvdnav://") != NULL;
234 play_tree_t* entry = play_tree_new();
235 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]);
236 // if required expand DVD filename entries like dvd://1-3 into component titles
237 if ( strstr(argv[i],"dvd://") != NULL || is_dvdnav)
239 int offset = is_dvdnav ? 9 : 6;
240 splitpos=strstr(argv[i]+offset,"-");
241 if(splitpos != NULL)
243 start_title=strtol(argv[i]+offset,NULL,10);
244 if (start_title<0) { //entries like dvd://-2 start title implied 1
245 end_title=abs(start_title);
246 start_title=1;
247 } else {
248 end_title=strtol(splitpos+1,NULL,10);
251 if (dvd_range(start_title) && dvd_range(end_title) && (start_title<end_title))
253 for (j=start_title;j<=end_title;j++)
255 if (j!=start_title)
256 entry=play_tree_new();
257 snprintf(entbuf,sizeof(entbuf),is_dvdnav ? "dvdnav://%d" : "dvd://%d",j);
258 play_tree_add_file(entry,entbuf);
259 add_entry(&last_parent,&last_entry,entry);
260 last_entry = entry;
262 } else {
263 mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_InvalidPlayEntry, argv[i]);
266 } else { // dvd:// or dvd://x entry
267 play_tree_add_file(entry,argv[i]);
269 } else {
270 play_tree_add_file(entry,argv[i]);
273 // Lock stdin if it will be used as input
274 if(strcasecmp(argv[i],"-") == 0)
275 m_config_set_option(config,"noconsolecontrols",NULL);
276 add_entry(&last_parent,&last_entry,entry);
277 UNSET_GLOBAL; // We start entry specific options
282 if (opt_exit)
283 goto err_out;
284 --recursion_depth;
285 if(last_parent != root)
286 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Missing }- ?\n");
287 return root;
289 err_out:
290 --recursion_depth;
291 play_tree_free(root,1);
292 return NULL;