Allocate struct demuxer with talloc
[mplayer.git] / parser-mpcmd.c
blobb0562fb86c000e38bf14de375666126ce29fb782
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(struct m_config *mconfig, char *opt, char *param,
37 play_tree_t** ret)
39 play_tree_t* entry = NULL;
41 *ret = NULL;
43 if(strcasecmp(opt,"playlist") == 0) { // We handle playlist here
44 if(!param)
45 return M_OPT_MISSING_PARAM;
47 entry = parse_playlist_file(mconfig, param);
48 if(!entry)
49 return -1;
50 else {
51 *ret=entry;
52 return 1;
55 return 0;
58 static inline void add_entry(play_tree_t **last_parentp,
59 play_tree_t **last_entryp, play_tree_t *entry) {
60 if(*last_entryp == NULL)
61 play_tree_set_child(*last_parentp,entry);
62 else
63 play_tree_append_entry(*last_entryp,entry);
64 *last_entryp = entry;
67 /// Setup the \ref Config from command line arguments and build a playtree.
68 /** \ingroup ConfigParsers
70 play_tree_t*
71 m_config_parse_mp_command_line(m_config_t *config, int argc, char **argv)
73 int i,j,start_title=-1,end_title=-1;
74 char *opt,*splitpos=NULL;
75 char entbuf[10];
76 int no_more_opts = 0;
77 int opt_exit = 0; // flag indicating whether mplayer should exit without playing anything
78 play_tree_t *last_parent, *last_entry = NULL, *root;
79 #ifdef CONFIG_MACOSX_FINDER
80 play_tree_t *macosx_finder_args(m_config_t *, int , char **);
81 #endif
83 #ifdef MP_DEBUG
84 assert(config != NULL);
85 assert(argv != NULL);
86 assert(argc >= 1);
87 #endif
89 config->mode = M_COMMAND_LINE;
90 mode = GLOBAL;
91 #ifdef CONFIG_MACOSX_FINDER
92 root=macosx_finder_args(config, argc, argv);
93 if(root)
94 return root;
95 #endif
97 last_parent = root = play_tree_new();
98 /* in order to work recursion detection properly in parse_config_file */
99 ++recursion_depth;
101 for (i = 1; i < argc; i++) {
102 //next:
103 opt = argv[i];
104 /* check for -- (no more options id.) except --help! */
105 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) == 0))
107 no_more_opts = 1;
108 if (i+1 >= argc)
110 mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_NoFileGivenOnCommandLine);
111 goto err_out;
113 continue;
115 if((opt[0] == '{') && (opt[1] == '\0'))
117 play_tree_t* entry = play_tree_new();
118 UNSET_GLOBAL;
119 if(last_parent->flags & PLAY_TREE_RND)
120 entry->flags |= PLAY_TREE_RND;
121 if(last_entry == NULL) {
122 play_tree_set_child(last_parent,entry);
123 } else {
124 play_tree_append_entry(last_entry,entry);
125 last_entry = NULL;
127 last_parent = entry;
128 continue;
131 if((opt[0] == '}') && (opt[1] == '\0'))
133 if( ! last_parent || ! last_parent->parent) {
134 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too much }-\n");
135 goto err_out;
137 last_entry = last_parent;
138 last_parent = last_entry->parent;
139 continue;
142 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
144 int tmp = 0;
145 /* remove trailing '-' */
146 opt++;
148 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
149 // We handle here some specific option
150 // Loop option when it apply to a group
151 if(strcasecmp(opt,"loop") == 0 &&
152 (! last_entry || last_entry->child) ) {
153 int l;
154 char* end = NULL;
155 l = (i+1<argc) ? strtol(argv[i+1],&end,0) : 0;
156 if(!end || *end != '\0') {
157 mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_TheLoopOptionMustBeAnInteger, argv[i+1]);
158 tmp = ERR_OUT_OF_RANGE;
159 } else {
160 play_tree_t* pt = last_entry ? last_entry : last_parent;
161 l = l <= 0 ? -1 : l;
162 pt->loop = l;
163 tmp = 1;
165 } else if(strcasecmp(opt,"shuffle") == 0) {
166 if(last_entry && last_entry->child)
167 last_entry->flags |= PLAY_TREE_RND;
168 else
169 last_parent->flags |= PLAY_TREE_RND;
170 } else if(strcasecmp(opt,"noshuffle") == 0) {
171 if(last_entry && last_entry->child)
172 last_entry->flags &= ~PLAY_TREE_RND;
173 else
174 last_parent->flags &= ~PLAY_TREE_RND;
175 } else {
176 const m_option_t* mp_opt = NULL;
177 play_tree_t* entry = NULL;
179 tmp = is_entry_option(config, opt,(i+1<argc) ? argv[i + 1] : NULL,&entry);
180 if(tmp > 0) { // It's an entry
181 if(entry) {
182 add_entry(&last_parent,&last_entry,entry);
183 if((last_parent->flags & PLAY_TREE_RND) && entry->child)
184 entry->flags |= PLAY_TREE_RND;
185 UNSET_GLOBAL;
186 } else if(mode == LOCAL) // Entry is empty we have to drop his params
187 mode = DROP_LOCAL;
188 } else if(tmp == 0) { // 'normal' options
189 mp_opt = m_config_get_option(config,opt);
190 if (mp_opt != NULL) { // Option exist
191 if(mode == GLOBAL || (mp_opt->flags & M_OPT_GLOBAL))
192 tmp = (i+1<argc) ? m_config_set_option(config, opt, argv[i + 1])
193 : m_config_set_option(config, opt, NULL);
194 else {
195 tmp = m_config_check_option(config, opt, (i+1<argc) ? argv[i + 1] : NULL);
196 if(tmp >= 0 && mode != DROP_LOCAL) {
197 play_tree_t* pt = last_entry ? last_entry : last_parent;
198 play_tree_set_param(pt,opt, argv[i + 1]);
201 } else {
202 tmp = M_OPT_UNKNOWN;
203 mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_UnknownOptionOnCommandLine, opt);
208 if (tmp <= M_OPT_EXIT) {
209 opt_exit = 1;
210 tmp = M_OPT_EXIT - tmp;
211 } else
212 if (tmp < 0) {
213 mp_msg(MSGT_CFGPARSER, MSGL_FATAL, MSGTR_ErrorParsingOptionOnCommandLine, opt);
214 goto err_out;
216 i += tmp;
218 else /* filename */
220 play_tree_t* entry = play_tree_new();
221 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]);
222 // if required expand DVD filename entries like dvd://1-3 into component titles
223 if ( strstr(argv[i],"dvd://") != NULL )
225 splitpos=strstr(argv[i]+6,"-");
226 if(splitpos != NULL)
228 start_title=strtol(argv[i]+6,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,9,"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;