contrib: nfs: update to 4.0.0
[vlc.git] / extras / analyser / zsh.cpp
blob7b7b8e00e97c254e3f5904e2bd7f3f952ed1a8b7
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 <algorithm>
29 #include <iostream>
30 #include <map>
31 #include <set>
32 #include <string>
33 #include <sstream>
35 #include <vlc/vlc.h>
36 #include <vlc_common.h>
37 #include <vlc_modules.h>
38 #include <vlc_plugin.h>
40 typedef std::pair<std::string, std::string> mpair;
41 typedef std::multimap<std::string, std::string> mumap;
42 mumap capabilities;
44 typedef std::pair<int, std::string> mcpair;
45 typedef std::multimap<int, std::string> mcmap;
46 mcmap categories;
48 std::set<std::string> mnames;
50 static void ReplaceChars(std::string& str)
52 std::replace(str.begin(), str.end(), ':', ';');
53 std::replace(str.begin(), str.end(), '"', '\'');
54 std::replace(str.begin(), str.end(), '`', '\'');
57 static void PrintOption(const module_config_t *item, const std::string &opt,
58 const std::string &excl, const std::string &args)
60 std::string longtext = item->psz_longtext ? item->psz_longtext : "";
61 std::string text = item->psz_text ? item->psz_text : "";
62 char i_short = item->i_short;
63 ReplaceChars(longtext);
64 ReplaceChars(text);
66 if (!longtext.length() || longtext.find('\n') != std::string::npos || longtext.find('(') != std::string::npos)
67 longtext = text;
69 std::cout << " \"";
71 const char *args_c = args.empty() ? "" : "=";
72 if (i_short) {
73 std::cout << "(-" << i_short;
75 if (!excl.empty())
76 std::cout << excl;
78 std::cout << ")--" << opt << args_c << "[" << text << "]";
80 if (!args.empty())
81 std::cout << ":" << longtext << ":" << args;
83 std::cout << "\"\\\n \"(--" << opt << excl << ")-" << i_short;
84 } else {
85 if (!excl.empty())
86 std::cout << "(" << excl << ")";
87 std::cout << "--" << opt;
88 if (!excl.empty())
89 std::cout << args_c;
92 std::cout << "[" << text << "]";
93 if (!args.empty())
94 std::cout << ":" << longtext << ":" << args;
95 std::cout << "\"\\\n";
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 ? 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 if (module_is_main(mod))
196 return;
198 const char *name = module_get_object(mod);
199 const char *cap = module_get_capability(mod);
201 if (strcmp(cap, "none"))
202 capabilities.insert(mpair(cap, name));
204 unsigned int cfg_size = 0;
205 module_config_t *cfg_list = module_config_get(mod, &cfg_size);
207 for (unsigned int j = 0; j < cfg_size; ++j)
209 const module_config_t *cfg = cfg_list + j;
210 if (cfg->i_type == CONFIG_SUBCATEGORY)
211 categories.insert(mcpair(cfg->value.i, name));
214 module_config_free(cfg_list);
216 if (mnames.find(name) == mnames.end())
218 std::cout << name << " ";
219 mnames.insert(name);
223 static void ParseModule(const module_t *mod)
225 unsigned int cfg_size = 0;
226 module_config_t *cfg_list = module_config_get(mod, &cfg_size);
228 for (unsigned int j = 0; j < cfg_size; ++j)
230 const module_config_t *cfg = cfg_list + j;
231 if (CONFIG_ITEM(cfg->i_type))
232 ParseOption(cfg);
235 module_config_free(cfg_list);
238 int main(int argc, const char **argv)
240 libvlc_instance_t *libvlc = libvlc_new(argc - 1, argv + 1);
241 if (!libvlc)
242 return 1;
244 size_t modules = 0;
245 module_t **mod_list;
247 mod_list = module_list_get(&modules);
248 if (!mod_list || modules == 0)
250 libvlc_release(libvlc);
251 return 2;
254 module_t **max = &mod_list[modules];
256 std::cout << "#compdef vlc cvlc rvlc svlc mvlc qvlc nvlc\n"
257 "#This file is autogenerated by zsh.cpp\n"
258 "typeset -A opt_args\n"
259 "local context state line ret=1\n"
260 "local modules\n\n";
262 std::cout << "vlc_modules=\"";
263 for (module_t **mod = mod_list; mod < max; mod++)
264 PrintModule(*mod);
265 std::cout << "\"\n\n";
267 std::cout << "_arguments -S -s \\\n";
268 for (module_t **mod = mod_list; mod < max; mod++)
269 ParseModule(*mod);
270 std::cout << " \"(--module)-p[print help on module]:print help on module:($vlc_modules)\"\\\n";
271 std::cout << " \"(-p)--module[print help on module]:print help on module:($vlc_modules)\"\\\n";
272 std::cout << " \"(--help)-h[print help]\"\\\n";
273 std::cout << " \"(-h)--help[print help]\"\\\n";
274 std::cout << " \"(--longhelp)-H[print detailed help]\"\\\n";
275 std::cout << " \"(-H)--longhelp[print detailed help]\"\\\n";
276 std::cout << " \"(--list)-l[print a list of available modules]\"\\\n";
277 std::cout << " \"(-l)--list[print a list of available modules]\"\\\n";
278 std::cout << " \"--reset-config[reset the current config to the default values]\"\\\n";
279 std::cout << " \"--config[use alternate config file]\"\\\n";
280 std::cout << " \"--reset-plugins-cache[resets the current plugins cache]\"\\\n";
281 std::cout << " \"--version[print version information]\"\\\n";
282 std::cout << " \"*:Playlist item:->mrl\" && ret=0\n\n";
284 std::cout << "case $state in\n";
285 std::cout << " mrl)\n";
286 std::cout << " _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0\n";
287 std::cout << " ;;\n";
288 std::cout << "esac\n\n";
290 std::cout << "return ret\n";
292 module_list_free(mod_list);
293 libvlc_release(libvlc);
294 return 0;