libcdio
[mplayer.git] / parser-mpcmd.c
blob67b579b644c70cb8c00c18f561fa6c0b4ae6cfc2
1 #include "config.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <errno.h>
8 #ifdef MP_DEBUG
9 #include <assert.h>
10 #endif
12 #include "mp_msg.h"
13 #include "m_option.h"
14 #include "m_config.h"
15 #include "playtree.h"
17 static int recursion_depth = 0;
18 static int mode = 0;
20 #define GLOBAL 0
21 #define LOCAL 1
22 #define DROP_LOCAL 2
24 #define dvd_range(a) (a>0 && a<256)
25 #define UNSET_GLOBAL (mode = LOCAL)
26 // Use this 1 if you want to have only global option (no per file option)
27 // #define UNSET_GLOBAL (mode = GLOBAL)
30 static int is_entry_option(char *opt, char *param, play_tree_t** ret) {
31 play_tree_t* entry = NULL;
33 *ret = NULL;
35 if(strcasecmp(opt,"playlist") == 0) { // We handle playlist here
36 if(!param)
37 return M_OPT_MISSING_PARAM;
39 entry = parse_playlist_file(param);
40 if(!entry)
41 return -1;
42 else {
43 *ret=entry;
44 return 1;
47 return 0;
50 static inline void add_entry(play_tree_t **last_parentp,
51 play_tree_t **last_entryp, play_tree_t *entry) {
52 if(*last_entryp == NULL)
53 play_tree_set_child(*last_parentp,entry);
54 else
55 play_tree_append_entry(*last_entryp,entry);
56 *last_entryp = entry;
59 play_tree_t*
60 m_config_parse_mp_command_line(m_config_t *config, int argc, char **argv)
62 int i,j,start_title=-1,end_title=-1;
63 char *opt,*splitpos=NULL;
64 char entbuf[10];
65 int no_more_opts = 0;
66 int opt_exit = 0; // flag indicating whether mplayer should exit without playing anything
67 play_tree_t *last_parent, *last_entry = NULL, *root;
68 #ifdef MACOSX_FINDER_SUPPORT
69 extern play_tree_t *macosx_finder_args(m_config_t *, int , char **);
70 #endif
72 #ifdef MP_DEBUG
73 assert(config != NULL);
74 assert(argv != NULL);
75 assert(argc >= 1);
76 #endif
78 config->mode = M_COMMAND_LINE;
79 mode = GLOBAL;
80 #ifdef MACOSX_FINDER_SUPPORT
81 root=macosx_finder_args(config, argc, argv);
82 if(root)
83 return root;
84 #endif
86 last_parent = root = play_tree_new();
87 /* in order to work recursion detection properly in parse_config_file */
88 ++recursion_depth;
90 for (i = 1; i < argc; i++) {
91 //next:
92 opt = argv[i];
93 /* check for -- (no more options id.) except --help! */
94 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) == 0))
96 no_more_opts = 1;
97 if (i+1 >= argc)
99 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "'--' indicates no more options, but no filename was given on the command line.\n");
100 goto err_out;
102 continue;
104 if((opt[0] == '{') && (opt[1] == '\0'))
106 play_tree_t* entry = play_tree_new();
107 UNSET_GLOBAL;
108 if(last_parent->flags & PLAY_TREE_RND)
109 entry->flags |= PLAY_TREE_RND;
110 if(last_entry == NULL) {
111 play_tree_set_child(last_parent,entry);
112 } else {
113 play_tree_append_entry(last_entry,entry);
114 last_entry = NULL;
116 last_parent = entry;
117 continue;
120 if((opt[0] == '}') && (opt[1] == '\0'))
122 if( ! last_parent || ! last_parent->parent) {
123 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too much }-\n");
124 goto err_out;
126 last_entry = last_parent;
127 last_parent = last_entry->parent;
128 continue;
131 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
133 int tmp = 0;
134 /* remove trailing '-' */
135 opt++;
137 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
138 // We handle here some specific option
139 if(strcasecmp(opt,"list-options") == 0) {
140 m_config_print_option_list(config);
141 exit(1);
142 // Loop option when it apply to a group
143 } else if(strcasecmp(opt,"loop") == 0 &&
144 (! last_entry || last_entry->child) ) {
145 int l;
146 char* end;
147 l = (i+1<argc) ? strtol(argv[i+1],&end,0) : 0;
148 if(*end != '\0') {
149 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "The loop option must be an integer: %s\n",argv[i+1]);
150 tmp = ERR_OUT_OF_RANGE;
151 } else {
152 play_tree_t* pt = last_entry ? last_entry : last_parent;
153 l = l <= 0 ? -1 : l;
154 pt->loop = l;
155 tmp = 1;
157 } else if(strcasecmp(opt,"shuffle") == 0) {
158 if(last_entry && last_entry->child)
159 last_entry->flags |= PLAY_TREE_RND;
160 else
161 last_parent->flags |= PLAY_TREE_RND;
162 } else if(strcasecmp(opt,"noshuffle") == 0) {
163 if(last_entry && last_entry->child)
164 last_entry->flags &= ~PLAY_TREE_RND;
165 else
166 last_parent->flags &= ~PLAY_TREE_RND;
167 } else {
168 m_option_t* mp_opt = NULL;
169 play_tree_t* entry = NULL;
171 tmp = is_entry_option(opt,(i+1<argc) ? argv[i + 1] : NULL,&entry);
172 if(tmp > 0) { // It's an entry
173 if(entry) {
174 add_entry(&last_parent,&last_entry,entry);
175 if((last_parent->flags & PLAY_TREE_RND) && entry->child)
176 entry->flags |= PLAY_TREE_RND;
177 UNSET_GLOBAL;
178 } else if(mode == LOCAL) // Entry is empty we have to drop his params
179 mode = DROP_LOCAL;
180 } else if(tmp == 0) { // 'normal' options
181 mp_opt = m_config_get_option(config,opt);
182 if (mp_opt != NULL) { // Option exist
183 if(mode == GLOBAL || (mp_opt->flags & M_OPT_GLOBAL))
184 tmp = (i+1<argc) ? m_config_set_option(config, opt, argv[i + 1])
185 : m_config_set_option(config, opt, NULL);
186 else {
187 tmp = m_config_check_option(config, opt, (i+1<argc) ? argv[i + 1] : NULL);
188 if(tmp >= 0 && mode != DROP_LOCAL) {
189 play_tree_t* pt = last_entry ? last_entry : last_parent;
190 play_tree_set_param(pt,opt, argv[i + 1]);
193 } else {
194 tmp = M_OPT_UNKNOWN;
195 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Unknown option on the command line: -%s\n",opt);
200 if (tmp <= M_OPT_EXIT) {
201 opt_exit = 1;
202 tmp = M_OPT_EXIT - tmp;
203 } else
204 if (tmp < 0) {
205 goto err_out;
207 i += tmp;
209 else /* filename */
211 play_tree_t* entry = play_tree_new();
212 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]);
213 // if required expand DVD filename entries like dvd://1-3 into component titles
214 if ( strstr(argv[i],"dvd://") != NULL )
216 splitpos=strstr(argv[i]+6,"-");
217 if(splitpos != NULL)
219 start_title=strtol(argv[i]+6,NULL,10);
220 if (start_title<0) { //entries like dvd://-2 start title implied 1
221 end_title=abs(start_title);
222 start_title=1;
223 } else {
224 end_title=strtol(splitpos+1,NULL,10);
227 if (dvd_range(start_title) && dvd_range(end_title) && (start_title<end_title))
229 for (j=start_title;j<=end_title;j++)
231 if (j!=start_title)
232 entry=play_tree_new();
233 snprintf(entbuf,9,"dvd://%d",j);
234 play_tree_add_file(entry,entbuf);
235 add_entry(&last_parent,&last_entry,entry);
236 last_entry = entry;
238 } else {
239 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Invalid play entry %s\n",argv[i]);
242 } else { // dvd:// or dvd://x entry
243 play_tree_add_file(entry,argv[i]);
245 } else {
246 play_tree_add_file(entry,argv[i]);
249 // Lock stdin if it will be used as input
250 if(strcasecmp(argv[i],"-") == 0)
251 m_config_set_option(config,"noconsolecontrols",NULL);
252 add_entry(&last_parent,&last_entry,entry);
253 UNSET_GLOBAL; // We start entry specific options
258 if (opt_exit)
259 goto err_out;
260 --recursion_depth;
261 if(last_parent != root)
262 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Missing }- ?\n");
263 return root;
265 err_out:
266 --recursion_depth;
267 play_tree_free(root,1);
268 return NULL;