DOCS/xml/README: improve
[mplayer/glamo.git] / parser-mpcmd.c
blob3d8b4a110f884aeb76eff934fd1f5874ef0b3294
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 "m_option.h"
35 #include "m_config.h"
36 #include "playtree.h"
37 #include "parser-mpcmd.h"
38 #include "osdep/macosx_finder_args.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;
97 #ifdef MP_DEBUG
98 assert(config != NULL);
99 assert(argv != NULL);
100 assert(argc >= 1);
101 #endif
103 config->mode = M_COMMAND_LINE;
104 mode = GLOBAL;
105 #ifdef CONFIG_MACOSX_FINDER
106 root=macosx_finder_args(config, argc, argv);
107 if(root)
108 return root;
109 #endif
111 last_parent = root = play_tree_new();
112 /* in order to work recursion detection properly in parse_config_file */
113 ++recursion_depth;
115 for (i = 1; i < argc; i++) {
116 //next:
117 opt = argv[i];
118 /* check for -- (no more options id.) except --help! */
119 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) == 0))
121 no_more_opts = 1;
122 if (i+1 >= argc)
124 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, "'--' indicates no more options, but no filename was given on the command line.\n");
125 goto err_out;
127 continue;
129 if((opt[0] == '{') && (opt[1] == '\0'))
131 play_tree_t* entry = play_tree_new();
132 UNSET_GLOBAL;
133 if(last_parent->flags & PLAY_TREE_RND)
134 entry->flags |= PLAY_TREE_RND;
135 if(last_entry == NULL) {
136 play_tree_set_child(last_parent,entry);
137 } else {
138 play_tree_append_entry(last_entry,entry);
139 last_entry = NULL;
141 last_parent = entry;
142 continue;
145 if((opt[0] == '}') && (opt[1] == '\0'))
147 if( ! last_parent || ! last_parent->parent) {
148 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too much }-\n");
149 goto err_out;
151 last_entry = last_parent;
152 last_parent = last_entry->parent;
153 continue;
156 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
158 int tmp = 0;
159 /* remove trailing '-' */
160 opt++;
162 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
163 // We handle here some specific option
164 // Loop option when it apply to a group
165 if(strcasecmp(opt,"loop") == 0 &&
166 (! last_entry || last_entry->child) ) {
167 int l;
168 char* end = NULL;
169 l = (i+1<argc) ? strtol(argv[i+1],&end,0) : 0;
170 if(!end || *end != '\0') {
171 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, "The loop option must be an integer: %s\n", argv[i+1]);
172 tmp = ERR_OUT_OF_RANGE;
173 } else {
174 play_tree_t* pt = last_entry ? last_entry : last_parent;
175 l = l <= 0 ? -1 : l;
176 pt->loop = l;
177 tmp = 1;
179 } else if(strcasecmp(opt,"shuffle") == 0) {
180 if(last_entry && last_entry->child)
181 last_entry->flags |= PLAY_TREE_RND;
182 else
183 last_parent->flags |= PLAY_TREE_RND;
184 } else if(strcasecmp(opt,"noshuffle") == 0) {
185 if(last_entry && last_entry->child)
186 last_entry->flags &= ~PLAY_TREE_RND;
187 else
188 last_parent->flags &= ~PLAY_TREE_RND;
189 } else {
190 const m_option_t* mp_opt = NULL;
191 play_tree_t* entry = NULL;
193 tmp = is_entry_option(config, opt,(i+1<argc) ? argv[i + 1] : NULL,&entry);
194 if(tmp > 0) { // It's an entry
195 if(entry) {
196 add_entry(&last_parent,&last_entry,entry);
197 if((last_parent->flags & PLAY_TREE_RND) && entry->child)
198 entry->flags |= PLAY_TREE_RND;
199 UNSET_GLOBAL;
200 } else if(mode == LOCAL) // Entry is empty we have to drop his params
201 mode = DROP_LOCAL;
202 } else if(tmp == 0) { // 'normal' options
203 mp_opt = m_config_get_option(config,opt);
204 if (mp_opt != NULL) { // Option exist
205 if(mode == GLOBAL || (mp_opt->flags & M_OPT_GLOBAL))
206 tmp = (i+1<argc) ? m_config_set_option(config, opt, argv[i + 1])
207 : m_config_set_option(config, opt, NULL);
208 else {
209 tmp = m_config_check_option(config, opt, (i+1<argc) ? argv[i + 1] : NULL);
210 if(tmp >= 0 && mode != DROP_LOCAL) {
211 play_tree_t* pt = last_entry ? last_entry : last_parent;
212 play_tree_set_param(pt,opt, argv[i + 1]);
215 } else {
216 tmp = M_OPT_UNKNOWN;
217 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, "Unknown option on the command line: -%s\n", opt);
222 if (tmp <= M_OPT_EXIT) {
223 opt_exit = 1;
224 tmp = M_OPT_EXIT - tmp;
225 } else
226 if (tmp < 0) {
227 mp_tmsg(MSGT_CFGPARSER, MSGL_FATAL, "Error parsing option on the command line: -%s\n", opt);
228 goto err_out;
230 i += tmp;
232 else /* filename */
234 int is_dvdnav = strstr(argv[i],"dvdnav://") != NULL;
235 play_tree_t* entry = play_tree_new();
236 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]);
237 // if required expand DVD filename entries like dvd://1-3 into component titles
238 if ( strstr(argv[i],"dvd://") != NULL || is_dvdnav)
240 int offset = is_dvdnav ? 9 : 6;
241 splitpos=strstr(argv[i]+offset,"-");
242 if(splitpos != NULL)
244 start_title=strtol(argv[i]+offset,NULL,10);
245 if (start_title<0) { //entries like dvd://-2 start title implied 1
246 end_title=abs(start_title);
247 start_title=1;
248 } else {
249 end_title=strtol(splitpos+1,NULL,10);
252 if (dvd_range(start_title) && dvd_range(end_title) && (start_title<end_title))
254 for (j=start_title;j<=end_title;j++)
256 if (j!=start_title)
257 entry=play_tree_new();
258 snprintf(entbuf,sizeof(entbuf),is_dvdnav ? "dvdnav://%d" : "dvd://%d",j);
259 play_tree_add_file(entry,entbuf);
260 add_entry(&last_parent,&last_entry,entry);
261 last_entry = entry;
263 } else {
264 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, "Invalid play entry %s\n", argv[i]);
267 } else { // dvd:// or dvd://x entry
268 play_tree_add_file(entry,argv[i]);
270 } else {
271 play_tree_add_file(entry,argv[i]);
274 // Lock stdin if it will be used as input
275 if(strcasecmp(argv[i],"-") == 0)
276 m_config_set_option(config,"noconsolecontrols",NULL);
277 add_entry(&last_parent,&last_entry,entry);
278 UNSET_GLOBAL; // We start entry specific options
283 if (opt_exit)
284 goto err_out;
285 --recursion_depth;
286 if(last_parent != root)
287 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Missing }- ?\n");
288 return root;
290 err_out:
291 --recursion_depth;
292 play_tree_free(root,1);
293 return NULL;