demux: adaptive: fix cross category es recycling
[vlc.git] / extras / analyser / zsh.cpp
blob0954a72dc1a6e8ed5be26ebf56d281d14a1df890
1 /*****************************************************************************
2 * zsh.cpp: create zsh completion rule for vlc
3 *****************************************************************************
4 * Copyright © 2005-2011 the VideoLAN team
6 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
7 Rafaël Carré <funman@videolanorg>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <stdio.h>
29 #include <map>
30 #include <string>
31 #include <sstream>
33 #include <vlc/vlc.h>
34 #include <vlc_common.h>
35 #include <vlc_modules.h>
36 #include <vlc_plugin.h>
37 #include "../src/modules/modules.h" /* evil hack */
39 typedef std::pair<std::string, std::string> mpair;
40 typedef std::multimap<std::string, std::string> mumap;
41 mumap capabilities;
43 typedef std::pair<int, std::string> mcpair;
44 typedef std::multimap<int, std::string> mcmap;
45 mcmap categories;
47 static void ReplaceChars(char *str)
49 if (str) {
50 char *parser;
51 while ((parser = strchr(str, ':'))) *parser=';' ;
52 while ((parser = strchr(str, '"'))) *parser='\'' ;
53 while ((parser = strchr(str, '`'))) *parser='\'' ;
57 static void PrintOption(const module_config_t *item, const std::string &opt,
58 const std::string &excl, const std::string &args)
60 char *longtext = item->psz_longtext;
61 char *text = item->psz_text;
62 char i_short = item->i_short;
63 ReplaceChars(longtext);
64 ReplaceChars(text);
66 if (!longtext || strchr(longtext, '\n') || strchr(longtext, '('))
67 longtext = text;
69 printf(" \"");
71 const char *args_c = args.empty() ? "" : "=";
72 if (i_short) {
73 printf("(-%c", i_short);
75 if (!excl.empty())
76 printf("%s", excl.c_str());
78 printf(")--%s%s[%s]", opt.c_str(), args_c, text);
80 if (!args.empty())
81 printf(":%s:%s", longtext, args.c_str());
83 printf("\"\\\n \"(--%s%s)-%c", opt.c_str(), excl.c_str(), i_short);
84 } else {
85 if (!excl.empty())
86 printf("(%s)", excl.c_str());
87 printf("--%s", opt.c_str());
88 if (!excl.empty())
89 printf("%s", args_c);
92 printf("[%s]", text);
93 if (!args.empty())
94 printf( ":%s:%s", longtext, args.c_str());
95 puts( "\"\\");
98 static void ParseOption(const module_config_t *item)
100 std::string excl, args;
101 std::string list;
102 std::pair<mcmap::iterator, mcmap::iterator> range;
103 std::pair<mumap::iterator, mumap::iterator> range_mod;
105 if (item->b_removed)
106 return;
108 switch(item->i_type)
110 case CONFIG_ITEM_MODULE:
111 range_mod = capabilities.equal_range(item->psz_type);
112 args = "(" + (*range_mod.first).second;
113 while (range_mod.first++ != range_mod.second)
114 args += " " + range_mod.first->second;
115 args += ")";
116 break;
118 case CONFIG_ITEM_MODULE_CAT:
119 range = categories.equal_range(item->min.i);
120 args = "(" + (*range.first).second;
121 while (range.first++ != range.second)
122 args += " " + range.first->second;
123 args += ")";
124 break;
126 case CONFIG_ITEM_MODULE_LIST_CAT:
127 range = categories.equal_range(item->min.i);
128 args = std::string("_values -s , ") + item->psz_name;
129 while (range.first != range.second)
130 args += " '*" + range.first++->second + "'";
131 break;
133 case CONFIG_ITEM_LOADFILE:
134 case CONFIG_ITEM_SAVEFILE:
135 args = "_files";
136 break;
137 case CONFIG_ITEM_DIRECTORY:
138 args = "_files -/";
139 break;
141 case CONFIG_ITEM_STRING:
142 case CONFIG_ITEM_INTEGER:
143 if (item->list_count == 0)
144 break;
146 for (int i = 0; i < item->list_count; i++) {
147 std::string val;
148 if (item->list_text) {
149 const char *text = item->list_text[i];
150 if (item->i_type == CONFIG_ITEM_INTEGER) {
151 std::stringstream s;
152 s << item->list.i[i];
153 val = s.str() + "\\:\\\"" + text;
154 } else {
155 if (!item->list.psz[i] || !text)
156 continue;
157 val = item->list.psz[i] + std::string("\\:\\\"") + text;
159 } else
160 val = std::string("\\\"") + item->list.psz[i];
162 list = val + "\\\" " + list;
165 if (item->list_text)
166 args = std::string("((") + list + "))";
167 else
168 args = std::string("(") + list + ")";
170 break;
172 case CONFIG_ITEM_BOOL:
173 excl = std::string("--no") + item->psz_name + " --no-" + item->psz_name;
174 PrintOption(item, item->psz_name, excl, args);
176 excl = std::string("--no") + item->psz_name + " --" + item->psz_name;
177 PrintOption(item, std::string("no-") + item->psz_name, excl, args);
179 excl = std::string("--no-")+ item->psz_name + " --" + item->psz_name;
180 PrintOption(item, std::string("no") + item->psz_name, excl, args);
181 return;
183 case CONFIG_ITEM_KEY:
184 case CONFIG_SECTION:
185 case CONFIG_ITEM_FLOAT:
186 default:
187 break;
190 PrintOption(item, item->psz_name, "", args);
193 static void PrintModule(const module_t *mod)
195 const char *name = mod->pp_shortcuts[0];
196 if (!strcmp(name, "main"))
197 return;
199 if (mod->psz_capability)
200 capabilities.insert(mpair(mod->psz_capability, name));
202 module_config_t *max = &mod->p_config[mod->i_config_items];
203 for (module_config_t *cfg = mod->p_config; cfg && cfg < max; cfg++)
204 if (cfg->i_type == CONFIG_SUBCATEGORY)
205 categories.insert(mcpair(cfg->value.i, name));
207 if (!mod->parent)
208 printf("%s ", name);
211 static void ParseModule(const module_t *mod)
213 if (mod->parent)
214 return;
216 module_config_t *max = mod->p_config + mod->confsize;
217 for (module_config_t *cfg = mod->p_config; cfg && cfg < max; cfg++)
218 if (CONFIG_ITEM(cfg->i_type))
219 ParseOption(cfg);
222 int main(int argc, const char **argv)
224 libvlc_instance_t *libvlc = libvlc_new(argc - 1, argv + 1);
225 if (!libvlc)
226 return 1;
228 size_t modules = 0;
229 module_t **mod_list;
231 mod_list = module_list_get(&modules);
232 if (!mod_list || modules == 0)
234 libvlc_release(libvlc);
235 return 2;
238 module_t **max = &mod_list[modules];
240 puts("#compdef vlc cvlc rvlc svlc mvlc qvlc nvlc\n"
241 "#This file is autogenerated by zsh.cpp\n"
242 "typeset -A opt_args\n"
243 "local context state line ret=1\n"
244 "local modules\n");
246 printf("vlc_modules=\"");
247 for (module_t **mod = mod_list; mod < max; mod++)
248 PrintModule(*mod);
249 puts("\"\n");
251 puts("_arguments -S -s \\");
252 for (module_t **mod = mod_list; mod < max; mod++)
253 ParseModule(*mod);
254 puts(" \"(--module)-p[print help on module]:print help on module:($vlc_modules)\"\\");
255 puts(" \"(-p)--module[print help on module]:print help on module:($vlc_modules)\"\\");
256 puts(" \"(--help)-h[print help]\"\\");
257 puts(" \"(-h)--help[print help]\"\\");
258 puts(" \"(--longhelp)-H[print detailed help]\"\\");
259 puts(" \"(-H)--longhelp[print detailed help]\"\\");
260 puts(" \"(--list)-l[print a list of available modules]\"\\");
261 puts(" \"(-l)--list[print a list of available modules]\"\\");
262 puts(" \"--reset-config[reset the current config to the default values]\"\\");
263 puts(" \"--config[use alternate config file]\"\\");
264 puts(" \"--reset-plugins-cache[resets the current plugins cache]\"\\");
265 puts(" \"--version[print version information]\"\\");
266 puts(" \"*:Playlist item:->mrl\" && ret=0\n");
268 puts("case $state in");
269 puts(" mrl)");
270 puts(" _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0");
271 puts(" ;;");
272 puts("esac\n");
274 puts("return ret");
276 module_list_free(mod_list);
277 libvlc_release(libvlc);
278 return 0;