prevent possible segfault when vidmodes is freed and config() calls vo_vm_switch
[mplayer.git] / parser-mpcmd.c
blob59792d9ec983dbcbafe45314f65ecada1c1a1cea
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 UNSET_GLOBAL (mode = LOCAL)
25 // Use this 1 if you want to have only global option (no per file option)
26 // #define UNSET_GLOBAL (mode = GLOBAL)
29 static int is_entry_option(char *opt, char *param, play_tree_t** ret) {
30 play_tree_t* entry = NULL;
32 *ret = NULL;
34 if(strcasecmp(opt,"playlist") == 0) { // We handle playlist here
35 if(!param)
36 return M_OPT_MISSING_PARAM;
37 entry = parse_playlist_file(param);
38 if(!entry)
39 return 1;
42 if(entry) {
43 *ret = entry;
44 return 1;
45 } else
46 return 0;
49 static inline void add_entry(play_tree_t **last_parentp,
50 play_tree_t **last_entryp, play_tree_t *entry) {
51 if(*last_entryp == NULL)
52 play_tree_set_child(*last_parentp,entry);
53 else
54 play_tree_append_entry(*last_entryp,entry);
55 *last_entryp = entry;
58 play_tree_t*
59 m_config_parse_mp_command_line(m_config_t *config, int argc, char **argv)
61 int i;
62 int tmp = 0;
63 char *opt;
64 int no_more_opts = 0;
65 play_tree_t *last_parent, *last_entry = NULL, *root;
67 #ifdef MP_DEBUG
68 assert(config != NULL);
69 assert(argv != NULL);
70 assert(argc >= 1);
71 #endif
73 config->mode = M_COMMAND_LINE;
74 mode = GLOBAL;
75 last_parent = root = play_tree_new();
76 /* in order to work recursion detection properly in parse_config_file */
77 ++recursion_depth;
79 for (i = 1; i < argc; i++) {
80 //next:
81 opt = argv[i];
82 /* check for -- (no more options id.) except --help! */
83 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) != 'h'))
85 no_more_opts = 1;
86 if (i+1 >= argc)
88 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "You added '--' but no filenames presented!\n");
89 goto err_out;
91 continue;
93 if((opt[0] == '{') && (opt[1] == '\0'))
95 play_tree_t* entry = play_tree_new();
96 UNSET_GLOBAL;
97 if(last_parent->flags & PLAY_TREE_RND)
98 entry->flags |= PLAY_TREE_RND;
99 if(last_entry == NULL) {
100 play_tree_set_child(last_parent,entry);
101 } else {
102 play_tree_append_entry(last_entry,entry);
103 last_entry = NULL;
105 last_parent = entry;
106 continue;
109 if((opt[0] == '}') && (opt[1] == '\0'))
111 if( ! last_parent || ! last_parent->parent) {
112 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too much }-\n");
113 goto err_out;
115 last_entry = last_parent;
116 last_parent = last_entry->parent;
117 continue;
120 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
122 /* remove trailing '-' */
123 opt++;
125 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
126 // We handle here some specific option
127 if(strcasecmp(opt,"list-options") == 0) {
128 m_config_print_option_list(config);
129 exit(1);
130 // Loop option when it apply to a group
131 } else if(strcasecmp(opt,"loop") == 0 &&
132 (! last_entry || last_entry->child) ) {
133 int l;
134 char* end;
135 l = (i+1<argc) ? strtol(argv[i+1],&end,0) : 0;
136 if(*end != '\0') {
137 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "The loop option must be an integer: %s\n",argv[i+1]);
138 tmp = ERR_OUT_OF_RANGE;
139 } else {
140 play_tree_t* pt = last_entry ? last_entry : last_parent;
141 l = l <= 0 ? -1 : l;
142 pt->loop = l;
143 tmp = 1;
145 } else if(strcasecmp(opt,"shuffle") == 0) {
146 if(last_entry && last_entry->child)
147 last_entry->flags |= PLAY_TREE_RND;
148 else
149 last_parent->flags |= PLAY_TREE_RND;
150 } else if(strcasecmp(opt,"noshuffle") == 0) {
151 if(last_entry && last_entry->child)
152 last_entry->flags &= ~PLAY_TREE_RND;
153 else
154 last_parent->flags &= ~PLAY_TREE_RND;
155 } else {
156 m_option_t* mp_opt = NULL;
157 play_tree_t* entry = NULL;
159 tmp = is_entry_option(opt,(i+1<argc) ? argv[i + 1] : NULL,&entry);
160 if(tmp > 0) { // It's an entry
161 if(entry) {
162 add_entry(&last_parent,&last_entry,entry);
163 if((last_parent->flags & PLAY_TREE_RND) && entry->child)
164 entry->flags |= PLAY_TREE_RND;
165 UNSET_GLOBAL;
166 } else if(mode == LOCAL) // Entry is empty we have to drop his params
167 mode = DROP_LOCAL;
168 } else if(tmp == 0) { // 'normal' options
169 mp_opt = m_config_get_option(config,opt);
170 if (mp_opt != NULL) { // Option exist
171 if(mode == GLOBAL || (mp_opt->flags & M_OPT_GLOBAL))
172 tmp = (i+1<argc) ? m_config_set_option(config, opt, argv[i + 1])
173 : m_config_set_option(config, opt, NULL);
174 else {
175 tmp = m_config_check_option(config, opt, (i+1<argc) ? argv[i + 1] : NULL);
176 if(tmp >= 0 && mode != DROP_LOCAL) {
177 play_tree_t* pt = last_entry ? last_entry : last_parent;
178 play_tree_set_param(pt,opt, argv[i + 1]);
181 } else {
182 tmp = M_OPT_UNKNOWN;
183 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Unknown option on the command line: %s\n",opt);
188 if (tmp < 0) {
189 if (tmp == M_OPT_EXIT)
190 exit(0);
191 goto err_out;
193 i += tmp;
195 else /* filename */
197 play_tree_t* entry = play_tree_new();
198 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]);
199 play_tree_add_file(entry,argv[i]);
200 // Lock stdin if it will be used as input
201 if(strcasecmp(argv[i],"-") == 0)
202 m_config_set_option(config,"use-stdin",NULL);
203 add_entry(&last_parent,&last_entry,entry);
204 UNSET_GLOBAL; // We start entry specific options
209 --recursion_depth;
210 if(last_parent != root)
211 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Missing }- ?\n");
212 return root;
214 err_out:
215 --recursion_depth;
216 play_tree_free(root,1);
217 return NULL;