3 /// \ingroup ConfigParsers Playtree
21 #include "parser-mpcmd.h"
23 static int recursion_depth
= 0;
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
;
41 if(strcasecmp(opt
,"playlist") == 0) { // We handle playlist here
43 return M_OPT_MISSING_PARAM
;
45 entry
= parse_playlist_file(param
);
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
);
61 play_tree_append_entry(*last_entryp
,entry
);
65 /// Setup the \ref Config from command line arguments and build a playtree.
66 /** \ingroup ConfigParsers
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
;
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 **);
82 assert(config
!= NULL
);
87 config
->mode
= M_COMMAND_LINE
;
89 #ifdef CONFIG_MACOSX_FINDER
90 root
=macosx_finder_args(config
, argc
, argv
);
95 last_parent
= root
= play_tree_new();
96 /* in order to work recursion detection properly in parse_config_file */
99 for (i
= 1; i
< argc
; i
++) {
102 /* check for -- (no more options id.) except --help! */
103 if ((*opt
== '-') && (*(opt
+1) == '-') && (*(opt
+2) == 0))
108 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, MSGTR_NoFileGivenOnCommandLine
);
113 if((opt
[0] == '{') && (opt
[1] == '\0'))
115 play_tree_t
* entry
= play_tree_new();
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
);
122 play_tree_append_entry(last_entry
,entry
);
129 if((opt
[0] == '}') && (opt
[1] == '\0'))
131 if( ! last_parent
|| ! last_parent
->parent
) {
132 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, "too much }-\n");
135 last_entry
= last_parent
;
136 last_parent
= last_entry
->parent
;
140 if ((no_more_opts
== 0) && (*opt
== '-') && (*(opt
+1) != 0)) /* option */
143 /* remove trailing '-' */
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
) ) {
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
;
158 play_tree_t
* pt
= last_entry
? last_entry
: last_parent
;
163 } else if(strcasecmp(opt
,"shuffle") == 0) {
164 if(last_entry
&& last_entry
->child
)
165 last_entry
->flags
|= PLAY_TREE_RND
;
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
;
172 last_parent
->flags
&= ~PLAY_TREE_RND
;
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
180 add_entry(&last_parent
,&last_entry
,entry
);
181 if((last_parent
->flags
& PLAY_TREE_RND
) && entry
->child
)
182 entry
->flags
|= PLAY_TREE_RND
;
184 } else if(mode
== LOCAL
) // Entry is empty we have to drop his params
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
);
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]);
201 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, MSGTR_UnknownOptionOnCommandLine
, opt
);
206 if (tmp
<= M_OPT_EXIT
) {
208 tmp
= M_OPT_EXIT
- tmp
;
211 mp_msg(MSGT_CFGPARSER
, MSGL_FATAL
, MSGTR_ErrorParsingOptionOnCommandLine
, opt
);
218 play_tree_t
* entry
= play_tree_new();
219 mp_msg(MSGT_CFGPARSER
, MSGL_DBG2
,"Adding file %s\n",argv
[i
]);
220 // if required expand DVD filename entries like dvd://1-3 into component titles
221 if ( strstr(argv
[i
],"dvd://") != NULL
)
223 splitpos
=strstr(argv
[i
]+6,"-");
226 start_title
=strtol(argv
[i
]+6,NULL
,10);
227 if (start_title
<0) { //entries like dvd://-2 start title implied 1
228 end_title
=abs(start_title
);
231 end_title
=strtol(splitpos
+1,NULL
,10);
234 if (dvd_range(start_title
) && dvd_range(end_title
) && (start_title
<end_title
))
236 for (j
=start_title
;j
<=end_title
;j
++)
239 entry
=play_tree_new();
240 snprintf(entbuf
,9,"dvd://%d",j
);
241 play_tree_add_file(entry
,entbuf
);
242 add_entry(&last_parent
,&last_entry
,entry
);
246 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
, MSGTR_InvalidPlayEntry
, argv
[i
]);
249 } else { // dvd:// or dvd://x entry
250 play_tree_add_file(entry
,argv
[i
]);
253 play_tree_add_file(entry
,argv
[i
]);
256 // Lock stdin if it will be used as input
257 if(strcasecmp(argv
[i
],"-") == 0)
258 m_config_set_option(config
,"noconsolecontrols",NULL
);
259 add_entry(&last_parent
,&last_entry
,entry
);
260 UNSET_GLOBAL
; // We start entry specific options
268 if(last_parent
!= root
)
269 mp_msg(MSGT_CFGPARSER
, MSGL_ERR
,"Missing }- ?\n");
274 play_tree_free(root
,1);