Mercurial > mplayer.hg
annotate parser-mpcmd.c @ 12108:06c4780332bc
disallow non-sense type parameter; added support for absolute file path; prefer channels.conf.{sat,ter,cbl} over channels.conf if the file is available
author | nicodvb |
---|---|
date | Sat, 03 Apr 2004 10:30:46 +0000 |
parents | 522afd56703c |
children | 14bdbd687113 |
rev | line source |
---|---|
8164 | 1 #include "config.h" |
2 | |
3 #include <stdio.h> | |
4 #include <stdlib.h> | |
5 #include <string.h> | |
6 #include <errno.h> | |
7 | |
8 #ifdef MP_DEBUG | |
9 #include <assert.h> | |
10 #endif | |
11 | |
12 #include "mp_msg.h" | |
13 #include "m_option.h" | |
14 #include "m_config.h" | |
15 #include "playtree.h" | |
16 | |
17 static int recursion_depth = 0; | |
18 static int mode = 0; | |
19 | |
20 #define GLOBAL 0 | |
21 #define LOCAL 1 | |
22 #define DROP_LOCAL 2 | |
23 | |
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) | |
27 | |
28 | |
29 static int is_entry_option(char *opt, char *param, play_tree_t** ret) { | |
30 play_tree_t* entry = NULL; | |
31 | |
32 *ret = NULL; | |
33 | |
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; | |
40 } | |
41 | |
42 if(entry) { | |
43 *ret = entry; | |
44 return 1; | |
45 } else | |
46 return 0; | |
47 } | |
48 | |
10542
928c02fa9949
fix the bug where only the last file of the command line is found
pl
parents:
10513
diff
changeset
|
49 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
|
50 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
|
51 if(*last_entryp == NULL) |
928c02fa9949
fix the bug where only the last file of the command line is found
pl
parents:
10513
diff
changeset
|
52 play_tree_set_child(*last_parentp,entry); |
10513 | 53 else |
10542
928c02fa9949
fix the bug where only the last file of the command line is found
pl
parents:
10513
diff
changeset
|
54 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
|
55 *last_entryp = entry; |
10513 | 56 } |
57 | |
8164 | 58 play_tree_t* |
59 m_config_parse_mp_command_line(m_config_t *config, int argc, char **argv) | |
60 { | |
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; | |
66 | |
67 #ifdef MP_DEBUG | |
68 assert(config != NULL); | |
69 assert(argv != NULL); | |
70 assert(argc >= 1); | |
71 #endif | |
72 | |
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; | |
78 | |
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')) | |
84 { | |
85 no_more_opts = 1; | |
86 if (i+1 >= argc) | |
87 { | |
88 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "You added '--' but no filenames presented!\n"); | |
89 goto err_out; | |
90 } | |
91 continue; | |
92 } | |
93 if((opt[0] == '{') && (opt[1] == '\0')) | |
94 { | |
95 play_tree_t* entry = play_tree_new(); | |
96 UNSET_GLOBAL; | |
8175 | 97 if(last_parent->flags & PLAY_TREE_RND) |
98 entry->flags |= PLAY_TREE_RND; | |
8164 | 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; | |
104 } | |
105 last_parent = entry; | |
106 continue; | |
107 } | |
108 | |
109 if((opt[0] == '}') && (opt[1] == '\0')) | |
110 { | |
111 if( ! last_parent || ! last_parent->parent) { | |
112 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too much }-\n"); | |
113 goto err_out; | |
114 } | |
115 last_entry = last_parent; | |
116 last_parent = last_entry->parent; | |
117 continue; | |
118 } | |
119 | |
120 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */ | |
121 { | |
122 /* remove trailing '-' */ | |
123 opt++; | |
124 | |
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; | |
8400 | 135 l = (i+1<argc) ? strtol(argv[i+1],&end,0) : 0; |
9106 | 136 if(*end != '\0') { |
137 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "The loop option must be an integer: %s\n",argv[i+1]); | |
8164 | 138 tmp = ERR_OUT_OF_RANGE; |
9106 | 139 } else { |
8164 | 140 play_tree_t* pt = last_entry ? last_entry : last_parent; |
141 l = l <= 0 ? -1 : l; | |
142 pt->loop = l; | |
143 tmp = 1; | |
144 } | |
8452 | 145 } else if(strcasecmp(opt,"shuffle") == 0) { |
8175 | 146 if(last_entry && last_entry->child) |
147 last_entry->flags |= PLAY_TREE_RND; | |
148 else | |
149 last_parent->flags |= PLAY_TREE_RND; | |
8452 | 150 } else if(strcasecmp(opt,"noshuffle") == 0) { |
8175 | 151 if(last_entry && last_entry->child) |
152 last_entry->flags &= ~PLAY_TREE_RND; | |
153 else | |
154 last_parent->flags &= ~PLAY_TREE_RND; | |
8164 | 155 } else { |
156 m_option_t* mp_opt = NULL; | |
157 play_tree_t* entry = NULL; | |
158 | |
8458 | 159 tmp = is_entry_option(opt,(i+1<argc) ? argv[i + 1] : NULL,&entry); |
8164 | 160 if(tmp > 0) { // It's an entry |
161 if(entry) { | |
10542
928c02fa9949
fix the bug where only the last file of the command line is found
pl
parents:
10513
diff
changeset
|
162 add_entry(&last_parent,&last_entry,entry); |
8175 | 163 if((last_parent->flags & PLAY_TREE_RND) && entry->child) |
164 entry->flags |= PLAY_TREE_RND; | |
8164 | 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)) | |
8426 | 172 tmp = (i+1<argc) ? m_config_set_option(config, opt, argv[i + 1]) |
173 : m_config_set_option(config, opt, NULL); | |
8164 | 174 else { |
8458 | 175 tmp = m_config_check_option(config, opt, (i+1<argc) ? argv[i + 1] : NULL); |
8164 | 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]); | |
179 } | |
180 } | |
181 } else { | |
10595
522afd56703c
100l to albeu for his english grammar, and 10l to me becouse I noticed that lately (my backward compatibilty macro uses M_OPT_UNKNOWN)
alex
parents:
10594
diff
changeset
|
182 tmp = M_OPT_UNKNOWN; |
10397 | 183 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Unknown option on the command line: %s\n",opt); |
8164 | 184 } |
185 } | |
186 } | |
187 | |
9792 | 188 if (tmp < 0) { |
189 if (tmp == M_OPT_EXIT) | |
190 exit(0); | |
8164 | 191 goto err_out; |
9792 | 192 } |
8164 | 193 i += tmp; |
194 } | |
195 else /* filename */ | |
196 { | |
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); | |
10542
928c02fa9949
fix the bug where only the last file of the command line is found
pl
parents:
10513
diff
changeset
|
203 add_entry(&last_parent,&last_entry,entry); |
8164 | 204 UNSET_GLOBAL; // We start entry specific options |
205 | |
206 } | |
207 } | |
208 | |
209 --recursion_depth; | |
210 if(last_parent != root) | |
211 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Missing }- ?\n"); | |
212 return root; | |
213 | |
214 err_out: | |
215 --recursion_depth; | |
216 play_tree_free(root,1); | |
217 return NULL; | |
218 } |