1 /*****************************************************************************
2 * zsh.cpp: create zsh completion rule for vlc
3 *****************************************************************************
4 * Copyright © 2005-2008 the VideoLAN team
7 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
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 *****************************************************************************/
30 typedef std::multimap<std::string, std::string> mumap;
31 typedef std::multimap<int, std::string> mcmap;
33 typedef std::pair<std::string, std::string> mpair;
34 typedef std::pair<int, std::string> mcpair;
40 #include <vlc_common.h>
42 #include <vlc_modules.h>
47 #include <../src/modules/modules.h>
49 void ParseModules( mumap &mods, mcmap &mods2 );
50 void PrintModuleList( mumap &mods, mcmap &mods2 );
51 void ParseOption( module_config_t *p_item, mumap &mods, mcmap &mods2 );
52 void PrintOption( char *psz_option, char i_short, char *psz_exlusive,
53 char *psz_text, char *psz_longtext, char *psz_args );
55 int main( int i_argc, const char **ppsz_argv )
59 /* Create a libvlc structure */
60 const char *argv[i_argc + 1];
62 for( int i = 0; i < i_argc; i++ )
63 argv[i+1] = ppsz_argv[i];
64 libvlc_instance_t *p_libvlc_instance = libvlc_new(i_argc+1, argv);
66 if( !p_libvlc_instance )
71 printf("#compdef vlc cvlc rvlc svlc mvlc qvlc nvlc\n\n"
73 "#This file is autogenerated by zsh.cpp\n"
74 "typeset -A opt_args\n"
75 "local context state line ret=1\n"
76 "local modules\n\n" );
78 PrintModuleList( mods, mods2 );
80 printf( "_arguments -S -s \\\n" );
81 ParseModules( mods, mods2 );
82 printf( " \"(--module)-p[print help on module]:print help on module:($modules)\"\\\n" );
83 printf( " \"(-p)--module[print help on module]:print help on module:($modules)\"\\\n" );
84 printf( " \"(--help)-h[print help]\"\\\n" );
85 printf( " \"(-h)--help[print help]\"\\\n" );
86 printf( " \"(--longhelp)-H[print detailed help]\"\\\n" );
87 printf( " \"(-H)--longhelp[print detailed help]\"\\\n" );
88 printf( " \"(--list)-l[print a list of available modules]\"\\\n" );
89 printf( " \"(-l)--list[print a list of available modules]\"\\\n" );
90 printf( " \"--reset-config[reset the current config to the default values]\"\\\n" );
91 printf( " \"--config[use alternate config file]\"\\\n" );
92 printf( " \"--reset-plugins-cache[resets the current plugins cache]\"\\\n" );
93 printf( " \"--version[print version information]\"\\\n" );
94 printf( " \"*:Playlist item:->mrl\" && ret=0\n\n" );
96 printf( "case $state in\n" );
98 printf( " _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0\n" );
100 printf( "esac\n\n" );
102 printf( "return ret\n" );
104 libvlc_release( p_libvlc_instance );
109 void ParseModules( mumap &mods, mcmap &mods2 )
113 module_config_t *p_item;
118 /* List the plugins */
119 p_list = module_list_get(&i_modules);
120 if( !p_list ) return;
121 for( i_index = 0; i_index < i_modules; i_index++ )
123 p_module = p_list[i_index];
125 /* Exclude empty plugins (submodules don't have config options, they
126 * are stored in the parent module) */
127 if( p_module->b_submodule )
129 // p_item = ((module_t *)p_module->p_parent)->p_config;
131 p_item = p_module->p_config;
133 // printf( " #%s\n", p_module->psz_longname );
134 if( !p_item ) continue;
138 if( p_item->i_type == CONFIG_CATEGORY )
140 // printf( " #Category %d\n", p_item->i_value );
142 else if( p_item->i_type == CONFIG_SUBCATEGORY )
144 // printf( " #Subcategory %d\n", p_item->i_value );
146 if( p_item->i_type & CONFIG_ITEM )
147 ParseOption( p_item, mods, mods2 );
149 while( ++i_items < p_module->confsize && p_item++ );
152 module_list_free( p_list );
155 void PrintModuleList( mumap &mods, mcmap &mods2 )
157 module_t **p_list = NULL;
163 /* List the plugins */
164 p_list = module_list_get(&i_modules);
165 if( !p_list ) return;
167 printf( "modules=\"" );
168 for( i_index = 0; i_index < i_modules; i_index++ )
170 p_module = p_list[i_index];
172 /* Exclude empty plugins (submodules don't have config options, they
173 * are stored in the parent module) */
175 if( strcmp( p_module->psz_object_name, "main" ) )
177 mods.insert( mpair( p_module->psz_capability,
178 p_module->psz_object_name ) );
179 module_config_t *p_config = p_module->p_config;
183 /* Hack: required subcategory is stored in i_min */
184 if( p_config->i_type == CONFIG_SUBCATEGORY )
186 mods2.insert( mcpair( p_config->value.i,
187 p_module->psz_object_name ) );
189 } while( i_items++ < p_module->i_config_items && p_config++ );
190 if( p_module->b_submodule )
192 printf( "%s ", p_module->psz_object_name );
197 module_list_free( p_list );
201 void ParseOption( module_config_t *p_item, mumap &mods, mcmap &mods2 )
203 char *psz_arguments = NULL;
210 #define DUP( x ) strdup( x ? x : "" )
212 //Skip deprecated options
213 if( p_item->b_removed )
216 switch( p_item->i_type )
218 case CONFIG_ITEM_MODULE:
220 std::pair<mumap::iterator, mumap::iterator> range = mods.equal_range( p_item->psz_type );
221 std::string list = (*range.first).second;
222 if( range.first != range.second )
224 while( range.first++ != range.second )
226 list = list.append( " " );
227 list = list.append( range.first->second );
229 asprintf( &psz_arguments, "(%s)", list.c_str() );
233 case CONFIG_ITEM_MODULE_CAT:
235 std::pair<mcmap::iterator, mcmap::iterator> range =
236 mods2.equal_range( p_item->min.i );
237 std::string list = (*range.first).second;
238 if( range.first != range.second )
240 while( range.first++ != range.second )
242 list = list.append( " " );
243 list = list.append( range.first->second );
245 asprintf( &psz_arguments, "(%s)", list.c_str() );
249 case CONFIG_ITEM_MODULE_LIST_CAT:
251 std::pair<mcmap::iterator, mcmap::iterator> range =
252 mods2.equal_range( p_item->min.i );
253 std::string list = "_values -s , ";
254 list = list.append( p_item->psz_name );
255 while( range.first != range.second )
257 list = list.append( " '*" );
258 list = list.append( range.first->second );
259 list = list.append( "'" );
262 psz_arguments = strdup( list.c_str() );
266 case CONFIG_ITEM_STRING:
269 int i = p_item->i_list -1;
271 if( p_item->ppsz_list_text )
272 asprintf( &psz_list, "%s\\:\\\"%s\\\"", p_item->ppsz_list[i],
273 p_item->ppsz_list_text[i] );
275 psz_list = strdup(p_item->ppsz_list[i]);
279 if( p_item->ppsz_list_text )
280 asprintf( &psz_list2, "%s\\:\\\"%s\\\" %s", p_item->ppsz_list[i-1],
281 p_item->ppsz_list_text[i-1], psz_list );
283 asprintf( &psz_list2, "\\\"%s\\\" %s", p_item->ppsz_list[i-1],
287 psz_list = psz_list2;
290 if( p_item->ppsz_list_text )
291 asprintf( &psz_arguments, "((%s))", psz_list );
293 asprintf( &psz_arguments, "(%s)", psz_list );
299 case CONFIG_ITEM_FILE:
300 psz_arguments = strdup( "_files" );
302 case CONFIG_ITEM_DIRECTORY:
303 psz_arguments = strdup( "_files -/" );
306 case CONFIG_ITEM_INTEGER:
309 int i = p_item->i_list -1;
311 if( p_item->ppsz_list_text )
312 asprintf( &psz_list, "%d\\:\\\"%s\\\"", p_item->pi_list[i],
313 p_item->ppsz_list_text[i] );
315 psz_list = strdup(p_item->ppsz_list[i]);
319 if( p_item->ppsz_list_text )
320 asprintf( &psz_list2, "%d\\:\\\"%s\\\" %s", p_item->pi_list[i-1],
321 p_item->ppsz_list_text[i-1], psz_list );
323 asprintf( &psz_list2, "\\\"%s\\\" %s", p_item->ppsz_list[i-1],
327 psz_list = psz_list2;
330 if( p_item->ppsz_list_text )
331 asprintf( &psz_arguments, "((%s))", psz_list );
333 asprintf( &psz_arguments, "(%s)", psz_list );
337 else if( p_item->min.i != 0 || p_item->max.i != 0 )
339 // p_control = new RangedIntConfigControl( p_this, p_item, parent );
343 // p_control = new IntegerConfigControl( p_this, p_item, parent );
347 case CONFIG_ITEM_KEY:
348 // p_control = new KeyConfigControl( p_this, p_item, parent );
351 case CONFIG_ITEM_FLOAT:
352 // p_control = new FloatConfigControl( p_this, p_item, parent );
355 case CONFIG_ITEM_BOOL:
356 // p_control = new BoolConfigControl( p_this, p_item, parent );
357 asprintf( &psz_exclusive, "--no%s --no-%s", p_item->psz_name,
359 psz_name = DUP( p_item->psz_name );
360 psz_text = DUP( p_item->psz_text );
361 psz_longtext = DUP( p_item->psz_longtext );
362 PrintOption( psz_name, p_item->i_short, psz_exclusive,
363 psz_text, psz_longtext, psz_arguments );
366 free( psz_longtext );
367 free( psz_exclusive );
368 asprintf( &psz_exclusive, "--no%s --%s", p_item->psz_name,
370 asprintf( &psz_option, "no-%s", p_item->psz_name );
371 psz_text = DUP( p_item->psz_text );
372 psz_longtext = DUP( p_item->psz_longtext );
373 PrintOption( psz_option, p_item->i_short, psz_exclusive,
374 psz_text, psz_longtext, psz_arguments );
376 free( psz_longtext );
377 free( psz_exclusive );
379 asprintf( &psz_exclusive, "--no-%s --%s", p_item->psz_name,
381 asprintf( &psz_option, "no%s", p_item->psz_name );
382 psz_text = DUP( p_item->psz_text );
383 psz_longtext = DUP( p_item->psz_longtext );
384 PrintOption( psz_option, p_item->i_short, psz_exclusive,
385 psz_text, psz_longtext, psz_arguments );
387 free( psz_longtext );
388 free( psz_exclusive );
393 // p_control = new SectionConfigControl( p_this, p_item, parent );
399 psz_name = DUP( p_item->psz_name );
400 psz_text = DUP( p_item->psz_text );
401 psz_longtext = DUP( p_item->psz_longtext );
402 PrintOption( psz_name, p_item->i_short, NULL,
403 psz_text, psz_longtext, psz_arguments );
406 free( psz_longtext );
407 free( psz_arguments );
410 void PrintOption( char *psz_option, char i_short, char *psz_exclusive,
411 char *psz_text, char *psz_longtext, char *psz_args )
416 while( (foo = strchr( psz_text, ':' ))) *foo=';';
417 while( (foo = strchr( psz_text, '"' ))) *foo='\'';
421 while( (foo = strchr( psz_longtext, ':' ))) *foo=';';
422 while( (foo = strchr( psz_longtext, '"' ))) *foo='\'';
425 strchr( psz_longtext, '\n' ) ||
426 strchr( psz_longtext, '(' ) ) psz_longtext = psz_text;
430 printf( " \"(-%c)--%s%s[%s]", i_short,
431 psz_option, psz_args?"=":"", psz_text );
433 printf( " \"(-%c%s)--%s%s[%s]", i_short, psz_exclusive,
434 psz_option, psz_args?"=":"", psz_text );
436 printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
440 printf( " \"(--%s%s)-%c[%s]", psz_option, psz_exclusive ? psz_exclusive : "",
443 printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
451 printf( " \"(%s)--%s%s[%s]", psz_exclusive, psz_option,
452 psz_args?"=":"", psz_text );
454 printf( " \"--%s[%s]", psz_option, psz_text );
457 printf( ":%s:%s\"\\\n", psz_longtext, psz_args );