Get rid of code I neither know nor use anymore.
[mplayer/glamo.git] / parser-mpcmd.c
blob57cdd65ad695ebfcbe717a58775c987180330a71
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 "m_option.h"
18 #include "m_config.h"
19 #include "playtree.h"
21 static int recursion_depth = 0;
22 static int mode = 0;
24 #define GLOBAL 0
25 #define LOCAL 1
26 #define DROP_LOCAL 2
28 #define dvd_range(a) (a>0 && a<256)
29 #define UNSET_GLOBAL (mode = LOCAL)
30 // Use this 1 if you want to have only global option (no per file option)
31 // #define UNSET_GLOBAL (mode = GLOBAL)
34 static int is_entry_option(char *opt, char *param, play_tree_t** ret) {
35 play_tree_t* entry = NULL;
37 *ret = NULL;
39 if(strcasecmp(opt,"playlist") == 0) { // We handle playlist here
40 if(!param)
41 return M_OPT_MISSING_PARAM;
43 entry = parse_playlist_file(param);
44 if(!entry)
45 return -1;
46 else {
47 *ret=entry;
48 return 1;
51 return 0;
54 static inline void add_entry(play_tree_t **last_parentp,
55 play_tree_t **last_entryp, play_tree_t *entry) {
56 if(*last_entryp == NULL)
57 play_tree_set_child(*last_parentp,entry);
58 else
59 play_tree_append_entry(*last_entryp,entry);
60 *last_entryp = entry;
63 /// Setup the \ref Config from command line arguments and build a playtree.
64 /** \ingroup ConfigParsers
66 play_tree_t*
67 m_config_parse_mp_command_line(m_config_t *config, int argc, char **argv)
69 int i,j,start_title=-1,end_title=-1;
70 char *opt,*splitpos=NULL;
71 char entbuf[10];
72 int no_more_opts = 0;
73 int opt_exit = 0; // flag indicating whether mplayer should exit without playing anything
74 play_tree_t *last_parent, *last_entry = NULL, *root;
75 #ifdef MACOSX_FINDER_SUPPORT
76 extern play_tree_t *macosx_finder_args(m_config_t *, int , char **);
77 #endif
79 #ifdef MP_DEBUG
80 assert(config != NULL);
81 assert(argv != NULL);
82 assert(argc >= 1);
83 #endif
85 config->mode = M_COMMAND_LINE;
86 mode = GLOBAL;
87 #ifdef MACOSX_FINDER_SUPPORT
88 root=macosx_finder_args(config, argc, argv);
89 if(root)
90 return root;
91 #endif
93 last_parent = root = play_tree_new();
94 /* in order to work recursion detection properly in parse_config_file */
95 ++recursion_depth;
97 for (i = 1; i < argc; i++) {
98 //next:
99 opt = argv[i];
100 /* check for -- (no more options id.) except --help! */
101 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) == 0))
103 no_more_opts = 1;
104 if (i+1 >= argc)
106 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "'--' indicates no more options, but no filename was given on the command line.\n");
107 goto err_out;
109 continue;
111 if((opt[0] == '{') && (opt[1] == '\0'))
113 play_tree_t* entry = play_tree_new();
114 UNSET_GLOBAL;
115 if(last_parent->flags & PLAY_TREE_RND)
116 entry->flags |= PLAY_TREE_RND;
117 if(last_entry == NULL) {
118 play_tree_set_child(last_parent,entry);
119 } else {
120 play_tree_append_entry(last_entry,entry);
121 last_entry = NULL;
123 last_parent = entry;
124 continue;
127 if((opt[0] == '}') && (opt[1] == '\0'))
129 if( ! last_parent || ! last_parent->parent) {
130 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too much }-\n");
131 goto err_out;
133 last_entry = last_parent;
134 last_parent = last_entry->parent;
135 continue;
138 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
140 int tmp = 0;
141 /* remove trailing '-' */
142 opt++;
144 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
145 // We handle here some specific option
146 // Loop option when it apply to a group
147 if(strcasecmp(opt,"loop") == 0 &&
148 (! last_entry || last_entry->child) ) {
149 int l;
150 char* end = NULL;
151 l = (i+1<argc) ? strtol(argv[i+1],&end,0) : 0;
152 if(!end || *end != '\0') {
153 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "The loop option must be an integer: %s\n",argv[i+1]);
154 tmp = ERR_OUT_OF_RANGE;
155 } else {
156 play_tree_t* pt = last_entry ? last_entry : last_parent;
157 l = l <= 0 ? -1 : l;
158 pt->loop = l;
159 tmp = 1;
161 } else if(strcasecmp(opt,"shuffle") == 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 if(strcasecmp(opt,"noshuffle") == 0) {
167 if(last_entry && last_entry->child)
168 last_entry->flags &= ~PLAY_TREE_RND;
169 else
170 last_parent->flags &= ~PLAY_TREE_RND;
171 } else {
172 m_option_t* mp_opt = NULL;
173 play_tree_t* entry = NULL;
175 tmp = is_entry_option(opt,(i+1<argc) ? argv[i + 1] : NULL,&entry);
176 if(tmp > 0) { // It's an entry
177 if(entry) {
178 add_entry(&last_parent,&last_entry,entry);
179 if((last_parent->flags & PLAY_TREE_RND) && entry->child)
180 entry->flags |= PLAY_TREE_RND;
181 UNSET_GLOBAL;
182 } else if(mode == LOCAL) // Entry is empty we have to drop his params
183 mode = DROP_LOCAL;
184 } else if(tmp == 0) { // 'normal' options
185 mp_opt = m_config_get_option(config,opt);
186 if (mp_opt != NULL) { // Option exist
187 if(mode == GLOBAL || (mp_opt->flags & M_OPT_GLOBAL))
188 tmp = (i+1<argc) ? m_config_set_option(config, opt, argv[i + 1])
189 : m_config_set_option(config, opt, NULL);
190 else {
191 tmp = m_config_check_option(config, opt, (i+1<argc) ? argv[i + 1] : NULL);
192 if(tmp >= 0 && mode != DROP_LOCAL) {
193 play_tree_t* pt = last_entry ? last_entry : last_parent;
194 play_tree_set_param(pt,opt, argv[i + 1]);
197 } else {
198 tmp = M_OPT_UNKNOWN;
199 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Unknown option on the command line: -%s\n",opt);
204 if (tmp <= M_OPT_EXIT) {
205 opt_exit = 1;
206 tmp = M_OPT_EXIT - tmp;
207 } else
208 if (tmp < 0) {
209 mp_msg(MSGT_CFGPARSER, MSGL_FATAL, "Error parsing option on the command line: -%s\n",opt);
210 goto err_out;
212 i += tmp;
214 else /* filename */
216 play_tree_t* entry = play_tree_new();
217 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]);
218 // if required expand DVD filename entries like dvd://1-3 into component titles
219 if ( strstr(argv[i],"dvd://") != NULL )
221 splitpos=strstr(argv[i]+6,"-");
222 if(splitpos != NULL)
224 start_title=strtol(argv[i]+6,NULL,10);
225 if (start_title<0) { //entries like dvd://-2 start title implied 1
226 end_title=abs(start_title);
227 start_title=1;
228 } else {
229 end_title=strtol(splitpos+1,NULL,10);
232 if (dvd_range(start_title) && dvd_range(end_title) && (start_title<end_title))
234 for (j=start_title;j<=end_title;j++)
236 if (j!=start_title)
237 entry=play_tree_new();
238 snprintf(entbuf,9,"dvd://%d",j);
239 play_tree_add_file(entry,entbuf);
240 add_entry(&last_parent,&last_entry,entry);
241 last_entry = entry;
243 } else {
244 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Invalid play entry %s\n",argv[i]);
247 } else { // dvd:// or dvd://x entry
248 play_tree_add_file(entry,argv[i]);
250 } else {
251 play_tree_add_file(entry,argv[i]);
254 // Lock stdin if it will be used as input
255 if(strcasecmp(argv[i],"-") == 0)
256 m_config_set_option(config,"noconsolecontrols",NULL);
257 add_entry(&last_parent,&last_entry,entry);
258 UNSET_GLOBAL; // We start entry specific options
263 if (opt_exit)
264 goto err_out;
265 --recursion_depth;
266 if(last_parent != root)
267 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Missing }- ?\n");
268 return root;
270 err_out:
271 --recursion_depth;
272 play_tree_free(root,1);
273 return NULL;