add dvb channels list .conf as filetype.
[vlc/solaris.git] / extras / analyser / zsh.cpp
blob65870424757ea3ea2fb0c705eb3f9e2a809632ba
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 <stdio.h>
29 #include <map>
30 #include <string>
31 #include <sstream>
33 #include <vlc/vlc.h>
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;
40 mumap capabilities;
42 typedef std::pair<int, std::string> mcpair;
43 typedef std::multimap<int, std::string> mcmap;
44 mcmap categories;
46 static void ReplaceChars(char *str)
48 if (str) {
49 char *parser;
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);
63 ReplaceChars(text);
65 if (!longtext || strchr(longtext, '\n') || strchr(longtext, '('))
66 longtext = text;
68 printf(" \"");
70 const char *args_c = args.empty() ? "" : "=";
71 if (i_short) {
72 printf("(-%c", i_short);
74 if (!excl.empty())
75 printf("%s", excl.c_str());
77 printf(")--%s%s[%s]", opt.c_str(), args_c, text);
79 if (!args.empty())
80 printf(":%s:%s", longtext, args.c_str());
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);
91 printf("[%s]", text);
92 if (!args.empty())
93 printf( ":%s:%s", longtext, args.c_str());
94 puts( "\"\\");
97 static void ParseOption(const module_config_t *item)
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;
104 if (item->b_removed)
105 return;
107 switch(item->i_type)
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;
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;
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;
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;
140 case CONFIG_ITEM_STRING:
141 case CONFIG_ITEM_INTEGER:
142 if (item->i_list == 0)
143 break;
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;
158 } else
159 val = std::string("\\\"") + item->ppsz_list[i];
161 list = val + "\\\" " + list;
164 if (item->ppsz_list_text)
165 args = std::string("((") + list + "))";
166 else
167 args = std::string("(") + list + ")";
169 break;
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);
180 return;
182 case CONFIG_ITEM_KEY:
183 case CONFIG_SECTION:
184 case CONFIG_ITEM_FLOAT:
185 default:
186 break;
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"))
196 return;
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));
206 if (!mod->parent)
207 printf("%s ", name);
210 static void ParseModule(const module_t *mod)
212 if (mod->parent)
213 return;
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);
221 int main(int argc, const char **argv)
223 libvlc_instance_t *libvlc = libvlc_new(argc, argv);
224 if (!libvlc)
225 return 1;
227 size_t modules = 0;
228 module_t **mod_list;
230 mod_list = module_list_get(&modules);
231 if (!mod_list || modules == 0)
232 return 2;
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"
240 "local modules\n");
242 printf("vlc_modules=\"");
243 for (module_t **mod = mod_list; mod < max; mod++)
244 PrintModule(*mod);
245 puts("\"\n");
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");
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");
270 puts("return ret");
272 module_list_free(mod_list);
273 libvlc_release(libvlc);
274 return 0;