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 *****************************************************************************/
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
;
44 typedef std::pair
<int, std::string
> mcpair
;
45 typedef std::multimap
<int, std::string
> mcmap
;
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
);
66 if (!longtext
.length() || longtext
.find('\n') != std::string::npos
|| longtext
.find('(') != std::string::npos
)
71 const char *args_c
= args
.empty() ? "" : "=";
73 std::cout
<< "(-" << i_short
;
78 std::cout
<< ")--" << opt
<< args_c
<< "[" << text
<< "]";
81 std::cout
<< ":" << longtext
<< ":" << args
;
83 std::cout
<< "\"\\\n \"(--" << opt
<< excl
<< ")-" << i_short
;
86 std::cout
<< "(" << excl
<< ")";
87 std::cout
<< "--" << opt
;
92 std::cout
<< "[" << text
<< "]";
94 std::cout
<< ":" << longtext
<< ":" << args
;
95 std::cout
<< "\"\\\n";
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
? 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 if (module_is_main(mod
))
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
<< " ";
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
))
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);
247 mod_list
= module_list_get(&modules
);
248 if (!mod_list
|| modules
== 0)
250 libvlc_release(libvlc
);
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"
262 std::cout
<< "vlc_modules=\"";
263 for (module_t
**mod
= mod_list
; mod
< max
; mod
++)
265 std::cout
<< "\"\n\n";
267 std::cout
<< "_arguments -S -s \\\n";
268 for (module_t
**mod
= mod_list
; mod
< max
; 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
);