]> git.sesse.net Git - vlc/blob - extras/analyser/zsh.cpp
win64: really package npapi plugin
[vlc] / extras / analyser / zsh.cpp
1 /*****************************************************************************
2  * zsh.cpp: create zsh completion rule for vlc
3  *****************************************************************************
4  * Copyright © 2005-2011 the VideoLAN team
5  *
6  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
7             Rafaël Carré <funman@videolanorg>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <stdio.h>
29 #include <map>
30 #include <string>
31 #include <sstream>
32
33 #include <vlc/vlc.h>
34 #include <vlc_common.h>
35 #include <vlc_modules.h>
36 #include "../src/modules/modules.h" /* evil hack */
37
38 typedef std::pair<std::string, std::string> mpair;
39 typedef std::multimap<std::string, std::string> mumap;
40 mumap capabilities;
41
42 typedef std::pair<int, std::string> mcpair;
43 typedef std::multimap<int, std::string> mcmap;
44 mcmap categories;
45
46 static void ReplaceChars(char *str)
47 {
48     if (str) {
49         char *parser;
50         while ((parser = strchr(str, ':'))) *parser=';' ;
51         while ((parser = strchr(str, '"'))) *parser='\'' ;
52         while ((parser = strchr(str, '`'))) *parser='\'' ;
53     }
54 }
55
56 static void PrintOption(const module_config_t *item, const std::string &opt,
57                         const std::string &excl, const std::string &args)
58 {
59     char *longtext = item->psz_longtext;
60     char *text = item->psz_text;
61     char i_short = item->i_short;
62     ReplaceChars(longtext);
63     ReplaceChars(text);
64
65     if (!longtext || strchr(longtext, '\n') || strchr(longtext, '('))
66         longtext = text;
67
68     printf("  \"");
69
70     const char *args_c = args.empty() ? "" : "=";
71     if (i_short) {
72         printf("(-%c", i_short);
73
74         if (!excl.empty())
75             printf("%s", excl.c_str());
76
77         printf(")--%s%s[%s]", opt.c_str(), args_c, text);
78
79         if (!args.empty())
80             printf(":%s:%s", longtext, args.c_str());
81
82         printf("\"\\\n  \"(--%s%s)-%c", opt.c_str(), excl.c_str(), i_short);
83     } else {
84         if (!excl.empty())
85             printf("(%s)", excl.c_str());
86         printf("--%s", opt.c_str());
87         if (!excl.empty())
88             printf("%s", args_c);
89     }
90
91     printf("[%s]", text);
92     if (!args.empty())
93         printf( ":%s:%s", longtext, args.c_str());
94     puts( "\"\\");
95 }
96
97 static void ParseOption(const module_config_t *item)
98 {
99     std::string excl, args;
100     std::string list;
101     std::pair<mcmap::iterator, mcmap::iterator> range;
102     std::pair<mumap::iterator, mumap::iterator> range_mod;
103
104     if (item->b_removed)
105         return;
106
107     switch(item->i_type)
108     {
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;
114         args += ")";
115     break;
116
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;
122         args += ")";
123     break;
124
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 + "'";
130     break;
131
132     case CONFIG_ITEM_LOADFILE:
133     case CONFIG_ITEM_SAVEFILE:
134         args = "_files";
135         break;
136     case CONFIG_ITEM_DIRECTORY:
137         args = "_files -/";
138         break;
139
140     case CONFIG_ITEM_STRING:
141     case CONFIG_ITEM_INTEGER:
142         if (item->i_list == 0)
143             break;
144
145         for (int i = 0; i < item->i_list; i++) {
146             std::string val;
147             if (item->ppsz_list_text) {
148                 const char *text = item->ppsz_list_text[i];
149                 if (item->i_type == CONFIG_ITEM_INTEGER) {
150                     std::stringstream s;
151                     s << item->pi_list[i];
152                     val = s.str() + "\\:\\\"" + text;
153                 } else {
154                     if (!item->ppsz_list[i] || !text)
155                         continue;
156                     val = item->ppsz_list[i] + std::string("\\:\\\"") + text;
157                 }
158             } else
159                 val = std::string("\\\"") + item->ppsz_list[i];
160
161             list = val + "\\\" " + list;
162         }
163
164         if (item->ppsz_list_text)
165             args = std::string("((") + list + "))";
166         else
167             args = std::string("(") + list + ")";
168
169         break;
170
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);
174
175         excl = std::string("--no") + item->psz_name + " --"    + item->psz_name;
176         PrintOption(item, std::string("no-") + item->psz_name,  excl, args);
177
178         excl = std::string("--no-")+ item->psz_name + " --"  + item->psz_name;
179         PrintOption(item, std::string("no") + item->psz_name,   excl, args);
180         return;
181
182     case CONFIG_ITEM_KEY:
183     case CONFIG_SECTION:
184     case CONFIG_ITEM_FLOAT:
185     default:
186         break;
187     }
188
189     PrintOption(item, item->psz_name, "", args);
190 }
191
192 static void PrintModule(const module_t *mod)
193 {
194     const char *name = mod->pp_shortcuts[0];
195     if (!strcmp(name, "main"))
196         return;
197
198     if (mod->psz_capability)
199         capabilities.insert(mpair(mod->psz_capability, name));
200
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));
205
206     if (!mod->parent)
207         printf("%s ", name);
208 }
209
210 static void ParseModule(const module_t *mod)
211 {
212     if (mod->parent)
213         return;
214
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))
218             ParseOption(cfg);
219 }
220
221 int main(int argc, const char **argv)
222 {
223     libvlc_instance_t *libvlc = libvlc_new(argc, argv);
224     if (!libvlc)
225         return 1;
226
227     size_t modules = 0;
228     module_t **mod_list;
229
230     mod_list = module_list_get(&modules);
231     if (!mod_list || modules == 0)
232         return 2;
233
234     module_t **max = &mod_list[modules];
235
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"
240            "local modules\n");
241
242     printf("vlc_modules=\"");
243     for (module_t **mod = mod_list; mod < max; mod++)
244         PrintModule(*mod);
245     puts("\"\n");
246
247     puts("_arguments -S -s \\");
248     for (module_t **mod = mod_list; mod < max; mod++)
249         ParseModule(*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");
263
264     puts("case $state in");
265     puts("  mrl)");
266     puts("    _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0");
267     puts("  ;;");
268     puts("esac\n");
269
270     puts("return ret");
271
272     module_list_free(mod_list);
273     libvlc_release(libvlc);
274     return 0;
275 }