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 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_modules.h>
36 #include "../src/modules/modules.h" /* evil hack */
38 typedef std::pair<std::string, std::string> mpair;
39 typedef std::multimap<std::string, std::string> mumap;
42 typedef std::pair<int, std::string> mcpair;
43 typedef std::multimap<int, std::string> mcmap;
46 static void ReplaceChars(char *str)
50 while ((parser = strchr(str, ':'))) *parser=';' ;
51 while ((parser = strchr(str, '"'))) *parser='\'' ;
52 while ((parser = strchr(str, '`'))) *parser='\'' ;
56 static void PrintOption(const module_config_t *item, const std::string &opt,
57 const std::string &excl, const std::string &args)
59 char *longtext = item->psz_longtext;
60 char *text = item->psz_text;
61 char i_short = item->i_short;
62 ReplaceChars(longtext);
65 if (!longtext || strchr(longtext, '\n') || strchr(longtext, '('))
70 const char *args_c = args.empty() ? "" : "=";
72 printf("(-%c", i_short);
75 printf("%s", excl.c_str());
77 printf(")--%s%s[%s]", opt.c_str(), args_c, text);
80 printf(":%s:%s", longtext, args.c_str());
82 printf("\"\\\n \"(--%s%s)-%c", opt.c_str(), excl.c_str(), i_short);
85 printf("(%s)", excl.c_str());
86 printf("--%s", opt.c_str());
93 printf( ":%s:%s", longtext, args.c_str());
97 static void ParseOption(const module_config_t *item)
99 std::string excl, args;
101 std::pair<mcmap::iterator, mcmap::iterator> range;
102 std::pair<mumap::iterator, mumap::iterator> range_mod;
109 case CONFIG_ITEM_MODULE:
110 range_mod = capabilities.equal_range(item->psz_type);
111 args = "(" + (*range_mod.first).second;
112 while (range_mod.first++ != range_mod.second)
113 args += " " + range_mod.first->second;
117 case CONFIG_ITEM_MODULE_CAT:
118 range = categories.equal_range(item->min.i);
119 args = "(" + (*range.first).second;
120 while (range.first++ != range.second)
121 args += " " + range.first->second;
125 case CONFIG_ITEM_MODULE_LIST_CAT:
126 range = categories.equal_range(item->min.i);
127 args = std::string("_values -s , ") + item->psz_name;
128 while (range.first != range.second)
129 args += " '*" + range.first++->second + "'";
132 case CONFIG_ITEM_LOADFILE:
133 case CONFIG_ITEM_SAVEFILE:
136 case CONFIG_ITEM_DIRECTORY:
140 case CONFIG_ITEM_STRING:
141 case CONFIG_ITEM_INTEGER:
142 if (item->i_list == 0)
145 for (int i = 0; i < item->i_list; i++) {
147 if (item->ppsz_list_text) {
148 const char *text = item->ppsz_list_text[i];
149 if (item->i_type == CONFIG_ITEM_INTEGER) {
151 s << item->pi_list[i];
152 val = s.str() + "\\:\\\"" + text;
154 if (!item->ppsz_list[i] || !text)
156 val = item->ppsz_list[i] + std::string("\\:\\\"") + text;
159 val = std::string("\\\"") + item->ppsz_list[i];
161 list = val + "\\\" " + list;
164 if (item->ppsz_list_text)
165 args = std::string("((") + list + "))";
167 args = std::string("(") + list + ")";
171 case CONFIG_ITEM_BOOL:
172 excl = std::string("--no") + item->psz_name + " --no-" + item->psz_name;
173 PrintOption(item, item->psz_name, excl, args);
175 excl = std::string("--no") + item->psz_name + " --" + item->psz_name;
176 PrintOption(item, std::string("no-") + item->psz_name, excl, args);
178 excl = std::string("--no-")+ item->psz_name + " --" + item->psz_name;
179 PrintOption(item, std::string("no") + item->psz_name, excl, args);
182 case CONFIG_ITEM_KEY:
184 case CONFIG_ITEM_FLOAT:
189 PrintOption(item, item->psz_name, "", args);
192 static void PrintModule(const module_t *mod)
194 const char *name = mod->pp_shortcuts[0];
195 if (!strcmp(name, "main"))
198 if (mod->psz_capability)
199 capabilities.insert(mpair(mod->psz_capability, name));
201 module_config_t *max = &mod->p_config[mod->i_config_items];
202 for (module_config_t *cfg = mod->p_config; cfg && cfg < max; cfg++)
203 if (cfg->i_type == CONFIG_SUBCATEGORY)
204 categories.insert(mcpair(cfg->value.i, name));
210 static void ParseModule(const module_t *mod)
215 module_config_t *max = mod->p_config + mod->confsize;
216 for (module_config_t *cfg = mod->p_config; cfg && cfg < max; cfg++)
217 if (CONFIG_ITEM(cfg->i_type))
221 int main(int argc, const char **argv)
223 libvlc_instance_t *libvlc = libvlc_new(argc, argv);
230 mod_list = module_list_get(&modules);
231 if (!mod_list || modules == 0)
234 module_t **max = &mod_list[modules];
236 puts("#compdef vlc cvlc rvlc svlc mvlc qvlc nvlc\n"
237 "#This file is autogenerated by zsh.cpp"
238 "typeset -A opt_args"
239 "local context state line ret=1"
242 printf("vlc_modules=\"");
243 for (module_t **mod = mod_list; mod < max; mod++)
247 puts("_arguments -S -s \\");
248 for (module_t **mod = mod_list; mod < max; mod++)
250 puts(" \"(--module)-p[print help on module]:print help on module:($vlc_modules)\"\\");
251 puts(" \"(-p)--module[print help on module]:print help on module:($vlc_modules)\"\\");
252 puts(" \"(--help)-h[print help]\"\\");
253 puts(" \"(-h)--help[print help]\"\\");
254 puts(" \"(--longhelp)-H[print detailed help]\"\\");
255 puts(" \"(-H)--longhelp[print detailed help]\"\\");
256 puts(" \"(--list)-l[print a list of available modules]\"\\");
257 puts(" \"(-l)--list[print a list of available modules]\"\\");
258 puts(" \"--reset-config[reset the current config to the default values]\"\\");
259 puts(" \"--config[use alternate config file]\"\\");
260 puts(" \"--reset-plugins-cache[resets the current plugins cache]\"\\");
261 puts(" \"--version[print version information]\"\\");
262 puts(" \"*:Playlist item:->mrl\" && ret=0\n");
264 puts("case $state in");
266 puts(" _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0");
272 module_list_free(mod_list);
273 libvlc_release(libvlc);