Mercurial > mplayer.hg
annotate parser-mpcmd.c @ 10564:2345addedd01
sync
author | gabucino |
---|---|
date | Mon, 11 Aug 2003 07:28:25 +0000 |
parents | 928c02fa9949 |
children | 57bdcdb061d7 |
rev | line source |
---|---|
8164 | 1 |
2 #include "config.h" | |
3 | |
4 #ifdef NEW_CONFIG | |
5 | |
6 #include <stdio.h> | |
7 #include <stdlib.h> | |
8 #include <string.h> | |
9 #include <errno.h> | |
10 | |
11 #ifdef MP_DEBUG | |
12 #include <assert.h> | |
13 #endif | |
14 | |
15 #include "mp_msg.h" | |
16 #include "m_option.h" | |
17 #include "m_config.h" | |
18 #include "playtree.h" | |
19 | |
20 static int recursion_depth = 0; | |
21 static int mode = 0; | |
22 | |
23 #define GLOBAL 0 | |
24 #define LOCAL 1 | |
25 #define DROP_LOCAL 2 | |
26 | |
27 #define UNSET_GLOBAL (mode = LOCAL) | |
28 // Use this 1 if you want to have only global option (no per file option) | |
29 // #define UNSET_GLOBAL (mode = GLOBAL) | |
30 | |
31 | |
32 static int is_entry_option(char *opt, char *param, play_tree_t** ret) { | |
33 play_tree_t* entry = NULL; | |
34 | |
35 *ret = NULL; | |
36 | |
37 if(strcasecmp(opt,"playlist") == 0) { // We handle playlist here | |
38 if(!param) | |
39 return M_OPT_MISSING_PARAM; | |
40 entry = parse_playlist_file(param); | |
41 if(!entry) | |
42 return 1; | |
43 } | |
44 | |
45 if(entry) { | |
46 *ret = entry; | |
47 return 1; | |
48 } else | |
49 return 0; | |
50 } | |
51 | |
10542
928c02fa9949
fix the bug where only the last file of the command line is found
pl
parents:
10513
diff
changeset
|
52 static inline void add_entry(play_tree_t **last_parentp, |
928c02fa9949
fix the bug where only the last file of the command line is found
pl
parents:
10513
diff
changeset
|
53 play_tree_t **last_entryp, play_tree_t *entry) { |
928c02fa9949
fix the bug where only the last file of the command line is found
pl
parents:
10513
diff
changeset
|
54 if(*last_entryp == NULL) |
928c02fa9949
fix the bug where only the last file of the command line is found
pl
parents:
10513
diff
changeset
|
55 play_tree_set_child(*last_parentp,entry); |
10513 | 56 else |
10542
928c02fa9949
fix the bug where only the last file of the command line is found
pl
parents:
10513
diff
changeset
|
57 play_tree_append_entry(*last_entryp,entry); |
928c02fa9949
fix the bug where only the last file of the command line is found
pl
parents:
10513
diff
changeset
|
58 *last_entryp = entry; |
10513 | 59 } |
60 | |
8164 | 61 play_tree_t* |
62 m_config_parse_mp_command_line(m_config_t *config, int argc, char **argv) | |
63 { | |
64 int i; | |
65 int tmp = 0; | |
66 char *opt; | |
67 int no_more_opts = 0; | |
68 play_tree_t *last_parent, *last_entry = NULL, *root; | |
69 | |
70 #ifdef MP_DEBUG | |
71 assert(config != NULL); | |
72 assert(argv != NULL); | |
73 assert(argc >= 1); | |
74 #endif | |
75 | |
76 config->mode = M_COMMAND_LINE; | |
77 mode = GLOBAL; | |
78 last_parent = root = play_tree_new(); | |
79 /* in order to work recursion detection properly in parse_config_file */ | |
80 ++recursion_depth; | |
81 | |
82 for (i = 1; i < argc; i++) { | |
83 //next: | |
84 opt = argv[i]; | |
85 /* check for -- (no more options id.) except --help! */ | |
86 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) != 'h')) | |
87 { | |
88 no_more_opts = 1; | |
89 if (i+1 >= argc) | |
90 { | |
91 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "You added '--' but no filenames presented!\n"); | |
92 goto err_out; | |
93 } | |
94 continue; | |
95 } | |
96 if((opt[0] == '{') && (opt[1] == '\0')) | |
97 { | |
98 play_tree_t* entry = play_tree_new(); | |
99 UNSET_GLOBAL; | |
8175 | 100 if(last_parent->flags & PLAY_TREE_RND) |
101 entry->flags |= PLAY_TREE_RND; | |
8164 | 102 if(last_entry == NULL) { |
103 play_tree_set_child(last_parent,entry); | |
104 } else { | |
105 play_tree_append_entry(last_entry,entry); | |
106 last_entry = NULL; | |
107 } | |
108 last_parent = entry; | |
109 continue; | |
110 } | |
111 | |
112 if((opt[0] == '}') && (opt[1] == '\0')) | |
113 { | |
114 if( ! last_parent || ! last_parent->parent) { | |
115 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too much }-\n"); | |
116 goto err_out; | |
117 } | |
118 last_entry = last_parent; | |
119 last_parent = last_entry->parent; | |
120 continue; | |
121 } | |
122 | |
123 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */ | |
124 { | |
125 /* remove trailing '-' */ | |
126 opt++; | |
127 | |
128 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt); | |
129 // We handle here some specific option | |
130 if(strcasecmp(opt,"list-options") == 0) { | |
131 m_config_print_option_list(config); | |
132 exit(1); | |
133 // Loop option when it apply to a group | |
134 } else if(strcasecmp(opt,"loop") == 0 && | |
135 (! last_entry || last_entry->child) ) { | |
136 int l; | |
137 char* end; | |
8400 | 138 l = (i+1<argc) ? strtol(argv[i+1],&end,0) : 0; |
9106 | 139 if(*end != '\0') { |
140 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "The loop option must be an integer: %s\n",argv[i+1]); | |
8164 | 141 tmp = ERR_OUT_OF_RANGE; |
9106 | 142 } else { |
8164 | 143 play_tree_t* pt = last_entry ? last_entry : last_parent; |
144 l = l <= 0 ? -1 : l; | |
145 pt->loop = l; | |
146 tmp = 1; | |
147 } | |
8452 | 148 } else if(strcasecmp(opt,"shuffle") == 0) { |
8175 | 149 if(last_entry && last_entry->child) |
150 last_entry->flags |= PLAY_TREE_RND; | |
151 else | |
152 last_parent->flags |= PLAY_TREE_RND; | |
8452 | 153 } else if(strcasecmp(opt,"noshuffle") == 0) { |
8175 | 154 if(last_entry && last_entry->child) |
155 last_entry->flags &= ~PLAY_TREE_RND; | |
156 else | |
157 last_parent->flags &= ~PLAY_TREE_RND; | |
8164 | 158 } else { |
159 m_option_t* mp_opt = NULL; | |
160 play_tree_t* entry = NULL; | |
161 | |
8458 | 162 tmp = is_entry_option(opt,(i+1<argc) ? argv[i + 1] : NULL,&entry); |
8164 | 163 if(tmp > 0) { // It's an entry |
164 if(entry) { | |
10542
928c02fa9949
fix the bug where only the last file of the command line is found
pl
parents:
10513
diff
changeset
|
165 add_entry(&last_parent,&last_entry,entry); |
8175 | 166 if((last_parent->flags & PLAY_TREE_RND) && entry->child) |
167 entry->flags |= PLAY_TREE_RND; | |
8164 | 168 UNSET_GLOBAL; |
169 } else if(mode == LOCAL) // Entry is empty we have to drop his params | |
170 mode = DROP_LOCAL; | |
171 } else if(tmp == 0) { // 'normal' options | |
172 mp_opt = m_config_get_option(config,opt); | |
173 if (mp_opt != NULL) { // Option exist | |
174 if(mode == GLOBAL || (mp_opt->flags & M_OPT_GLOBAL)) | |
8426 | 175 tmp = (i+1<argc) ? m_config_set_option(config, opt, argv[i + 1]) |
176 : m_config_set_option(config, opt, NULL); | |
8164 | 177 else { |
8458 | 178 tmp = m_config_check_option(config, opt, (i+1<argc) ? argv[i + 1] : NULL); |
8164 | 179 if(tmp >= 0 && mode != DROP_LOCAL) { |
180 play_tree_t* pt = last_entry ? last_entry : last_parent; | |
181 play_tree_set_param(pt,opt, argv[i + 1]); | |
182 } | |
183 } | |
184 } else { | |
185 tmp = M_OPT_UNKNOW; | |
10397 | 186 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Unknown option on the command line: %s\n",opt); |
8164 | 187 } |
188 } | |
189 } | |
190 | |
9792 | 191 if (tmp < 0) { |
192 if (tmp == M_OPT_EXIT) | |
193 exit(0); | |
8164 | 194 goto err_out; |
9792 | 195 } |
8164 | 196 i += tmp; |
197 } | |
198 else /* filename */ | |
199 { | |
200 play_tree_t* entry = play_tree_new(); | |
201 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]); | |
202 play_tree_add_file(entry,argv[i]); | |
203 // Lock stdin if it will be used as input | |
204 if(strcasecmp(argv[i],"-") == 0) | |
205 m_config_set_option(config,"use-stdin",NULL); | |
10542
928c02fa9949
fix the bug where only the last file of the command line is found
pl
parents:
10513
diff
changeset
|
206 add_entry(&last_parent,&last_entry,entry); |
8164 | 207 UNSET_GLOBAL; // We start entry specific options |
208 | |
209 } | |
210 } | |
211 | |
212 --recursion_depth; | |
213 if(last_parent != root) | |
214 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Missing }- ?\n"); | |
215 return root; | |
216 | |
217 err_out: | |
218 --recursion_depth; | |
219 play_tree_free(root,1); | |
220 return NULL; | |
221 } | |
222 | |
223 #endif |