]> git.sesse.net Git - vlc/blob - extras/analyser/zsh.cpp
decoder: factor some common code into DecoderUpdateFormatLocked()
[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 <vlc_plugin.h>
37 #include "../src/modules/modules.h" /* evil hack */
38
39 typedef std::pair<std::string, std::string> mpair;
40 typedef std::multimap<std::string, std::string> mumap;
41 mumap capabilities;
42
43 typedef std::pair<int, std::string> mcpair;
44 typedef std::multimap<int, std::string> mcmap;
45 mcmap categories;
46
47 static void ReplaceChars(char *str)
48 {
49     if (str) {
50         char *parser;
51         while ((parser = strchr(str, ':'))) *parser=';' ;
52         while ((parser = strchr(str, '"'))) *parser='\'' ;
53         while ((parser = strchr(str, '`'))) *parser='\'' ;
54     }
55 }
56
57 static void PrintOption(const module_config_t *item, const std::string &opt,
58                         const std::string &excl, const std::string &args)
59 {
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);
65
66     if (!longtext || strchr(longtext, '\n') || strchr(longtext, '('))
67         longtext = text;
68
69     printf("  \"");
70
71     const char *args_c = args.empty() ? "" : "=";
72     if (i_short) {
73         printf("(-%c", i_short);
74
75         if (!excl.empty())
76             printf("%s", excl.c_str());
77
78         printf(")--%s%s[%s]", opt.c_str(), args_c, text);
79
80         if (!args.empty())
81             printf(":%s:%s", longtext, args.c_str());
82
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);
90     }
91
92     printf("[%s]", text);
93     if (!args.empty())
94         printf( ":%s:%s", longtext, args.c_str());
95     puts( "\"\\");
96 }
97
98 static void ParseOption(const module_config_t *item)
99 {
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;
104
105     if (item->b_removed)
106         return;
107
108     switch(item->i_type)
109     {
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;
117
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;
125
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;
132
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;
140
141     case CONFIG_ITEM_STRING:
142     case CONFIG_ITEM_INTEGER:
143         if (item->list_count == 0)
144             break;
145
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;
158                 }
159             } else
160                 val = std::string("\\\"") + item->list.psz[i];
161
162             list = val + "\\\" " + list;
163         }
164
165         if (item->list_text)
166             args = std::string("((") + list + "))";
167         else
168             args = std::string("(") + list + ")";
169
170         break;
171
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);
175
176         excl = std::string("--no") + item->psz_name + " --"    + item->psz_name;
177         PrintOption(item, std::string("no-") + item->psz_name,  excl, args);
178
179         excl = std::string("--no-")+ item->psz_name + " --"  + item->psz_name;
180         PrintOption(item, std::string("no") + item->psz_name,   excl, args);
181         return;
182
183     case CONFIG_ITEM_KEY:
184     case CONFIG_SECTION:
185     case CONFIG_ITEM_FLOAT:
186     default:
187         break;
188     }
189
190     PrintOption(item, item->psz_name, "", args);
191 }
192
193 static void PrintModule(const module_t *mod)
194 {
195     const char *name = mod->pp_shortcuts[0];
196     if (!strcmp(name, "main"))
197         return;
198
199     if (mod->psz_capability)
200         capabilities.insert(mpair(mod->psz_capability, name));
201
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));
206
207     if (!mod->parent)
208         printf("%s ", name);
209 }
210
211 static void ParseModule(const module_t *mod)
212 {
213     if (mod->parent)
214         return;
215
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);
220 }
221
222 int main(int argc, const char **argv)
223 {
224     libvlc_instance_t *libvlc = libvlc_new(argc, argv);
225     if (!libvlc)
226         return 1;
227
228     size_t modules = 0;
229     module_t **mod_list;
230
231     mod_list = module_list_get(&modules);
232     if (!mod_list || modules == 0)
233         return 2;
234
235     module_t **max = &mod_list[modules];
236
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"
241            "local modules\n");
242
243     printf("vlc_modules=\"");
244     for (module_t **mod = mod_list; mod < max; mod++)
245         PrintModule(*mod);
246     puts("\"\n");
247
248     puts("_arguments -S -s \\");
249     for (module_t **mod = mod_list; mod < max; mod++)
250         ParseModule(*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");
264
265     puts("case $state in");
266     puts("  mrl)");
267     puts("    _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0");
268     puts("  ;;");
269     puts("esac\n");
270
271     puts("return ret");
272
273     module_list_free(mod_list);
274     libvlc_release(libvlc);
275     return 0;
276 }