sync with en/mplayer.1 r30336
[mplayer/glamo.git] / parser-mpcmd.c
blobc16148bc8b734f7c8ec292fbce3b8b9106d1e346
2 /// \file
3 /// \ingroup ConfigParsers Playtree
5 #include "config.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <errno.h>
12 #ifdef MP_DEBUG
13 #include <assert.h>
14 #endif
16 #include "mp_msg.h"
17 #include "help_mp.h"
18 #include "m_option.h"
19 #include "m_config.h"
20 #include "playtree.h"
21 #include "parser-mpcmd.h"
23 static int recursion_depth = 0;
24 static int mode = 0;
26 #define GLOBAL 0
27 #define LOCAL 1
28 #define DROP_LOCAL 2
30 #define dvd_range(a) (a>0 && a<256)
31 #define UNSET_GLOBAL (mode = LOCAL)
32 // Use this 1 if you want to have only global option (no per file option)
33 // #define UNSET_GLOBAL (mode = GLOBAL)
36 static int is_entry_option(char *opt, char *param, play_tree_t** ret) {
37 play_tree_t* entry = NULL;
39 *ret = NULL;
41 if(strcasecmp(opt,"playlist") == 0) { // We handle playlist here
42 if(!param)
43 return M_OPT_MISSING_PARAM;
45 entry = parse_playlist_file(param);
46 if(!entry)
47 return -1;
48 else {
49 *ret=entry;
50 return 1;
53 return 0;
56 static inline void add_entry(play_tree_t **last_parentp,
57 play_tree_t **last_entryp, play_tree_t *entry) {
58 if(*last_entryp == NULL)
59 play_tree_set_child(*last_parentp,entry);
60 else
61 play_tree_append_entry(*last_entryp,entry);
62 *last_entryp = entry;
65 /// Setup the \ref Config from command line arguments and build a playtree.
66 /** \ingroup ConfigParsers
68 play_tree_t*
69 m_config_parse_mp_command_line(m_config_t *config, int argc, char **argv)
71 int i,j,start_title=-1,end_title=-1;
72 char *opt,*splitpos=NULL;
73 char entbuf[15];
74 int no_more_opts = 0;
75 int opt_exit = 0; // flag indicating whether mplayer should exit without playing anything
76 play_tree_t *last_parent, *last_entry = NULL, *root;
77 #ifdef CONFIG_MACOSX_FINDER
78 play_tree_t *macosx_finder_args(m_config_t *, int , char **);
79 #endif
81 #ifdef MP_DEBUG
82 assert(config != NULL);
83 assert(argv != NULL);
84 assert(argc >= 1);
85 #endif
87 config->mode = M_COMMAND_LINE;
88 mode = GLOBAL;
89 #ifdef CONFIG_MACOSX_FINDER
90 root=macosx_finder_args(config, argc, argv);
91 if(root)
92 return root;
93 #endif
95 last_parent = root = play_tree_new();
96 /* in order to work recursion detection properly in parse_config_file */
97 ++recursion_depth;
99 for (i = 1; i < argc; i++) {
100 //next:
101 opt = argv[i];
102 /* check for -- (no more options id.) except --help! */
103 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) == 0))
105 no_more_opts = 1;
106 if (i+1 >= argc)
108 mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_NoFileGivenOnCommandLine);
109 goto err_out;
111 continue;
113 if((opt[0] == '{') && (opt[1] == '\0'))
115 play_tree_t* entry = play_tree_new();
116 UNSET_GLOBAL;
117 if(last_parent->flags & PLAY_TREE_RND)
118 entry->flags |= PLAY_TREE_RND;
119 if(last_entry == NULL) {
120 play_tree_set_child(last_parent,entry);
121 } else {
122 play_tree_append_entry(last_entry,entry);
123 last_entry = NULL;
125 last_parent = entry;
126 continue;
129 if((opt[0] == '}') && (opt[1] == '\0'))
131 if( ! last_parent || ! last_parent->parent) {
132 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too much }-\n");
133 goto err_out;
135 last_entry = last_parent;
136 last_parent = last_entry->parent;
137 continue;
140 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
142 int tmp = 0;
143 /* remove trailing '-' */
144 opt++;
146 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
147 // We handle here some specific option
148 // Loop option when it apply to a group
149 if(strcasecmp(opt,"loop") == 0 &&
150 (! last_entry || last_entry->child) ) {
151 int l;
152 char* end = NULL;
153 l = (i+1<argc) ? strtol(argv[i+1],&end,0) : 0;
154 if(!end || *end != '\0') {
155 mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_TheLoopOptionMustBeAnInteger, argv[i+1]);
156 tmp = ERR_OUT_OF_RANGE;
157 } else {
158 play_tree_t* pt = last_entry ? last_entry : last_parent;
159 l = l <= 0 ? -1 : l;
160 pt->loop = l;
161 tmp = 1;
163 } else if(strcasecmp(opt,"shuffle") == 0) {
164 if(last_entry && last_entry->child)
165 last_entry->flags |= PLAY_TREE_RND;
166 else
167 last_parent->flags |= PLAY_TREE_RND;
168 } else if(strcasecmp(opt,"noshuffle") == 0) {
169 if(last_entry && last_entry->child)
170 last_entry->flags &= ~PLAY_TREE_RND;
171 else
172 last_parent->flags &= ~PLAY_TREE_RND;
173 } else {
174 const m_option_t* mp_opt = NULL;
175 play_tree_t* entry = NULL;
177 tmp = is_entry_option(opt,(i+1<argc) ? argv[i + 1] : NULL,&entry);
178 if(tmp > 0) { // It's an entry
179 if(entry) {
180 add_entry(&last_parent,&last_entry,entry);
181 if((last_parent->flags & PLAY_TREE_RND) && entry->child)
182 entry->flags |= PLAY_TREE_RND;
183 UNSET_GLOBAL;
184 } else if(mode == LOCAL) // Entry is empty we have to drop his params
185 mode = DROP_LOCAL;
186 } else if(tmp == 0) { // 'normal' options
187 mp_opt = m_config_get_option(config,opt);
188 if (mp_opt != NULL) { // Option exist
189 if(mode == GLOBAL || (mp_opt->flags & M_OPT_GLOBAL))
190 tmp = (i+1<argc) ? m_config_set_option(config, opt, argv[i + 1])
191 : m_config_set_option(config, opt, NULL);
192 else {
193 tmp = m_config_check_option(config, opt, (i+1<argc) ? argv[i + 1] : NULL);
194 if(tmp >= 0 && mode != DROP_LOCAL) {
195 play_tree_t* pt = last_entry ? last_entry : last_parent;
196 play_tree_set_param(pt,opt, argv[i + 1]);
199 } else {
200 tmp = M_OPT_UNKNOWN;
201 mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_UnknownOptionOnCommandLine, opt);
206 if (tmp <= M_OPT_EXIT) {
207 opt_exit = 1;
208 tmp = M_OPT_EXIT - tmp;
209 } else
210 if (tmp < 0) {
211 mp_msg(MSGT_CFGPARSER, MSGL_FATAL, MSGTR_ErrorParsingOptionOnCommandLine, opt);
212 goto err_out;
214 i += tmp;
216 else /* filename */
218 int is_dvdnav = strstr(argv[i],"dvdnav://") != NULL;
219 play_tree_t* entry = play_tree_new();
220 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]);
221 // if required expand DVD filename entries like dvd://1-3 into component titles
222 if ( strstr(argv[i],"dvd://") != NULL || is_dvdnav)
224 int offset = is_dvdnav ? 9 : 6;
225 splitpos=strstr(argv[i]+offset,"-");
226 if(splitpos != NULL)
228 start_title=strtol(argv[i]+offset,NULL,10);
229 if (start_title<0) { //entries like dvd://-2 start title implied 1
230 end_title=abs(start_title);
231 start_title=1;
232 } else {
233 end_title=strtol(splitpos+1,NULL,10);
236 if (dvd_range(start_title) && dvd_range(end_title) && (start_title<end_title))
238 for (j=start_title;j<=end_title;j++)
240 if (j!=start_title)
241 entry=play_tree_new();
242 snprintf(entbuf,sizeof(entbuf),is_dvdnav ? "dvdnav://%d" : "dvd://%d",j);
243 play_tree_add_file(entry,entbuf);
244 add_entry(&last_parent,&last_entry,entry);
245 last_entry = entry;
247 } else {
248 mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_InvalidPlayEntry, argv[i]);
251 } else { // dvd:// or dvd://x entry
252 play_tree_add_file(entry,argv[i]);
254 } else {
255 play_tree_add_file(entry,argv[i]);
258 // Lock stdin if it will be used as input
259 if(strcasecmp(argv[i],"-") == 0)
260 m_config_set_option(config,"noconsolecontrols",NULL);
261 add_entry(&last_parent,&last_entry,entry);
262 UNSET_GLOBAL; // We start entry specific options
267 if (opt_exit)
268 goto err_out;
269 --recursion_depth;
270 if(last_parent != root)
271 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Missing }- ?\n");
272 return root;
274 err_out:
275 --recursion_depth;
276 play_tree_free(root,1);
277 return NULL;