10l to Nico for this copy&paste bug
[mplayer/glamo.git] / parser-mpcmd.c
blob137f72cbcaaa2065ca4daf2c3408a6ddc5bc2df3
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 play_tree_t *last_parent, *last_entry = NULL, *root;
67 #ifdef MACOSX_FINDER_SUPPORT
68 extern play_tree_t *macosx_finder_args(m_config_t *, int , char **);
69 #endif
71 #ifdef MP_DEBUG
72 assert(config != NULL);
73 assert(argv != NULL);
74 assert(argc >= 1);
75 #endif
77 config->mode = M_COMMAND_LINE;
78 mode = GLOBAL;
79 #ifdef MACOSX_FINDER_SUPPORT
80 root=macosx_finder_args(config, argc, argv);
81 if(root)
82 return root;
83 #endif
85 last_parent = root = play_tree_new();
86 /* in order to work recursion detection properly in parse_config_file */
87 ++recursion_depth;
89 for (i = 1; i < argc; i++) {
90 //next:
91 opt = argv[i];
92 /* check for -- (no more options id.) except --help! */
93 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) == 0))
95 no_more_opts = 1;
96 if (i+1 >= argc)
98 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "'--' indicates no more options, but no filename was given on the command line.\n");
99 goto err_out;
101 continue;
103 if((opt[0] == '{') && (opt[1] == '\0'))
105 play_tree_t* entry = play_tree_new();
106 UNSET_GLOBAL;
107 if(last_parent->flags & PLAY_TREE_RND)
108 entry->flags |= PLAY_TREE_RND;
109 if(last_entry == NULL) {
110 play_tree_set_child(last_parent,entry);
111 } else {
112 play_tree_append_entry(last_entry,entry);
113 last_entry = NULL;
115 last_parent = entry;
116 continue;
119 if((opt[0] == '}') && (opt[1] == '\0'))
121 if( ! last_parent || ! last_parent->parent) {
122 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too much }-\n");
123 goto err_out;
125 last_entry = last_parent;
126 last_parent = last_entry->parent;
127 continue;
130 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
132 int tmp = 0;
133 /* remove trailing '-' */
134 opt++;
136 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
137 // We handle here some specific option
138 if(strcasecmp(opt,"list-options") == 0) {
139 m_config_print_option_list(config);
140 exit(1);
141 // Loop option when it apply to a group
142 } else if(strcasecmp(opt,"loop") == 0 &&
143 (! last_entry || last_entry->child) ) {
144 int l;
145 char* end;
146 l = (i+1<argc) ? strtol(argv[i+1],&end,0) : 0;
147 if(*end != '\0') {
148 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "The loop option must be an integer: %s\n",argv[i+1]);
149 tmp = ERR_OUT_OF_RANGE;
150 } else {
151 play_tree_t* pt = last_entry ? last_entry : last_parent;
152 l = l <= 0 ? -1 : l;
153 pt->loop = l;
154 tmp = 1;
156 } else if(strcasecmp(opt,"shuffle") == 0) {
157 if(last_entry && last_entry->child)
158 last_entry->flags |= PLAY_TREE_RND;
159 else
160 last_parent->flags |= PLAY_TREE_RND;
161 } else if(strcasecmp(opt,"noshuffle") == 0) {
162 if(last_entry && last_entry->child)
163 last_entry->flags &= ~PLAY_TREE_RND;
164 else
165 last_parent->flags &= ~PLAY_TREE_RND;
166 } else {
167 m_option_t* mp_opt = NULL;
168 play_tree_t* entry = NULL;
170 tmp = is_entry_option(opt,(i+1<argc) ? argv[i + 1] : NULL,&entry);
171 if(tmp > 0) { // It's an entry
172 if(entry) {
173 add_entry(&last_parent,&last_entry,entry);
174 if((last_parent->flags & PLAY_TREE_RND) && entry->child)
175 entry->flags |= PLAY_TREE_RND;
176 UNSET_GLOBAL;
177 } else if(mode == LOCAL) // Entry is empty we have to drop his params
178 mode = DROP_LOCAL;
179 } else if(tmp == 0) { // 'normal' options
180 mp_opt = m_config_get_option(config,opt);
181 if (mp_opt != NULL) { // Option exist
182 if(mode == GLOBAL || (mp_opt->flags & M_OPT_GLOBAL))
183 tmp = (i+1<argc) ? m_config_set_option(config, opt, argv[i + 1])
184 : m_config_set_option(config, opt, NULL);
185 else {
186 tmp = m_config_check_option(config, opt, (i+1<argc) ? argv[i + 1] : NULL);
187 if(tmp >= 0 && mode != DROP_LOCAL) {
188 play_tree_t* pt = last_entry ? last_entry : last_parent;
189 play_tree_set_param(pt,opt, argv[i + 1]);
192 } else {
193 tmp = M_OPT_UNKNOWN;
194 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Unknown option on the command line: -%s\n",opt);
199 if (tmp < 0) {
200 if (tmp == M_OPT_EXIT)
201 exit(0);
202 goto err_out;
204 i += tmp;
206 else /* filename */
208 play_tree_t* entry = play_tree_new();
209 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]);
210 // if required expand DVD filename entries like dvd://1-3 into component titles
211 if ( strstr(argv[i],"dvd://") != NULL )
213 splitpos=strstr(argv[i]+6,"-");
214 if(splitpos != NULL)
216 start_title=strtol(argv[i]+6,NULL,10);
217 if (start_title<0) { //entries like dvd://-2 start title implied 1
218 end_title=abs(start_title);
219 start_title=1;
220 } else {
221 end_title=strtol(splitpos+1,NULL,10);
224 if (dvd_range(start_title) && dvd_range(end_title) && (start_title<end_title))
226 for (j=start_title;j<=end_title;j++)
228 if (j!=start_title)
229 entry=play_tree_new();
230 snprintf(entbuf,9,"dvd://%d",j);
231 play_tree_add_file(entry,entbuf);
232 add_entry(&last_parent,&last_entry,entry);
233 last_entry = entry;
235 } else {
236 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Invalid play entry %s\n",argv[i]);
239 } else { // dvd:// or dvd://x entry
240 play_tree_add_file(entry,argv[i]);
242 } else {
243 play_tree_add_file(entry,argv[i]);
246 // Lock stdin if it will be used as input
247 if(strcasecmp(argv[i],"-") == 0)
248 m_config_set_option(config,"noconsolecontrols",NULL);
249 add_entry(&last_parent,&last_entry,entry);
250 UNSET_GLOBAL; // We start entry specific options
255 --recursion_depth;
256 if(last_parent != root)
257 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Missing }- ?\n");
258 return root;
260 err_out:
261 --recursion_depth;
262 play_tree_free(root,1);
263 return NULL;