options: Move ass_enabled to options struct
[mplayer/glamo.git] / parser-mpcmd.c
blob4b46d1cc0216c51926633751e8de1a0c571b6779
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[15];
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_tmsg(MSGT_CFGPARSER, MSGL_ERR, "'--' indicates no more options, but no filename was given on the command line.\n");
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_tmsg(MSGT_CFGPARSER, MSGL_ERR, "The loop option must be an integer: %s\n", 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_tmsg(MSGT_CFGPARSER, MSGL_ERR, "Unknown option on the command line: -%s\n", 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_tmsg(MSGT_CFGPARSER, MSGL_FATAL, "Error parsing option on the command line: -%s\n", opt);
214 goto err_out;
216 i += tmp;
218 else /* filename */
220 int is_dvdnav = strstr(argv[i],"dvdnav://") != NULL;
221 play_tree_t* entry = play_tree_new();
222 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]);
223 // if required expand DVD filename entries like dvd://1-3 into component titles
224 if ( strstr(argv[i],"dvd://") != NULL || is_dvdnav)
226 int offset = is_dvdnav ? 9 : 6;
227 splitpos=strstr(argv[i]+offset,"-");
228 if(splitpos != NULL)
230 start_title=strtol(argv[i]+offset,NULL,10);
231 if (start_title<0) { //entries like dvd://-2 start title implied 1
232 end_title=abs(start_title);
233 start_title=1;
234 } else {
235 end_title=strtol(splitpos+1,NULL,10);
238 if (dvd_range(start_title) && dvd_range(end_title) && (start_title<end_title))
240 for (j=start_title;j<=end_title;j++)
242 if (j!=start_title)
243 entry=play_tree_new();
244 snprintf(entbuf,sizeof(entbuf),is_dvdnav ? "dvdnav://%d" : "dvd://%d",j);
245 play_tree_add_file(entry,entbuf);
246 add_entry(&last_parent,&last_entry,entry);
247 last_entry = entry;
249 } else {
250 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, "Invalid play entry %s\n", argv[i]);
253 } else { // dvd:// or dvd://x entry
254 play_tree_add_file(entry,argv[i]);
256 } else {
257 play_tree_add_file(entry,argv[i]);
260 // Lock stdin if it will be used as input
261 if(strcasecmp(argv[i],"-") == 0)
262 m_config_set_option(config,"noconsolecontrols",NULL);
263 add_entry(&last_parent,&last_entry,entry);
264 UNSET_GLOBAL; // We start entry specific options
269 if (opt_exit)
270 goto err_out;
271 --recursion_depth;
272 if(last_parent != root)
273 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Missing }- ?\n");
274 return root;
276 err_out:
277 --recursion_depth;
278 play_tree_free(root,1);
279 return NULL;