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 <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;
43 typedef std::pair<int, std::string> mcpair;
44 typedef std::multimap<int, std::string> mcmap;
47 static void ReplaceChars(char *str)
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);
66 if (!longtext || strchr(longtext, '\n') || strchr(longtext, '('))
71 const char *args_c = args.empty() ? "" : "=";
73 printf("(-%c", i_short);
76 printf("%s", excl.c_str());
78 printf(")--%s%s[%s]", opt.c_str(), args_c, text);
81 printf(":%s:%s", longtext, args.c_str());
83 printf("\"\\\n \"(--%s%s)-%c", opt.c_str(), excl.c_str(), i_short);
86 printf("(%s)", excl.c_str());
87 printf("--%s", opt.c_str());
94 printf( ":%s:%s", longtext, args.c_str());
98 static void ParseOption(const module_config_t *item)
100 std::string excl, args;
102 std::pair<mcmap::iterator, mcmap::iterator> range;
103 std::pair<mumap::iterator, mumap::iterator> range_mod;
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;
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;
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 + "'";
133 case CONFIG_ITEM_LOADFILE:
134 case CONFIG_ITEM_SAVEFILE:
137 case CONFIG_ITEM_DIRECTORY:
141 case CONFIG_ITEM_STRING:
142 case CONFIG_ITEM_INTEGER:
143 if (item->list_count == 0)
146 for (int i = 0; i < item->list_count; i++) {
148 if (item->list_text) {
149 const char *text = item->list_text[i];
150 if (item->i_type == CONFIG_ITEM_INTEGER) {
152 s << item->list.i[i];
153 val = s.str() + "\\:\\\"" + text;
155 if (!item->list.psz[i] || !text)
157 val = item->list.psz[i] + std::string("\\:\\\"") + text;
160 val = std::string("\\\"") + item->list.psz[i];
162 list = val + "\\\" " + list;
166 args = std::string("((") + list + "))";
168 args = std::string("(") + list + ")";
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);
183 case CONFIG_ITEM_KEY:
185 case CONFIG_ITEM_FLOAT:
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"))
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));
211 static void ParseModule(const module_t *mod)
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))
222 int main(int argc, const char **argv)
224 libvlc_instance_t *libvlc = libvlc_new(argc, argv);
231 mod_list = module_list_get(&modules);
232 if (!mod_list || modules == 0)
235 module_t **max = &mod_list[modules];
237 puts("#compdef vlc cvlc rvlc svlc mvlc qvlc nvlc\n"
238 "#This file is autogenerated by zsh.cpp"
239 "typeset -A opt_args"
240 "local context state line ret=1"
243 printf("vlc_modules=\"");
244 for (module_t **mod = mod_list; mod < max; mod++)
248 puts("_arguments -S -s \\");
249 for (module_t **mod = mod_list; mod < max; mod++)
251 puts(" \"(--module)-p[print help on module]:print help on module:($vlc_modules)\"\\");
252 puts(" \"(-p)--module[print help on module]:print help on module:($vlc_modules)\"\\");
253 puts(" \"(--help)-h[print help]\"\\");
254 puts(" \"(-h)--help[print help]\"\\");
255 puts(" \"(--longhelp)-H[print detailed help]\"\\");
256 puts(" \"(-H)--longhelp[print detailed help]\"\\");
257 puts(" \"(--list)-l[print a list of available modules]\"\\");
258 puts(" \"(-l)--list[print a list of available modules]\"\\");
259 puts(" \"--reset-config[reset the current config to the default values]\"\\");
260 puts(" \"--config[use alternate config file]\"\\");
261 puts(" \"--reset-plugins-cache[resets the current plugins cache]\"\\");
262 puts(" \"--version[print version information]\"\\");
263 puts(" \"*:Playlist item:->mrl\" && ret=0\n");
265 puts("case $state in");
267 puts(" _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0");
273 module_list_free(mod_list);
274 libvlc_release(libvlc);