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>
46 #include <../src/modules/modules.h>
48 void ParseModules( mumap &mods, mcmap &mods2 );
49 void PrintModuleList( mumap &mods, mcmap &mods2 );
50 void ParseOption( module_config_t *p_item, mumap &mods, mcmap &mods2 );
51 void PrintOption( char *psz_option, char i_short, char *psz_exlusive,
52 char *psz_text, char *psz_longtext, char *psz_args );
54 int main( int i_argc, const char **ppsz_argv )
58 /* Create a libvlc structure */
60 libvlc_exception_t ex;
61 libvlc_exception_init(&ex);
63 const char *argv[i_argc + 1];
65 for( int i = 0; i < i_argc; i++ )
66 argv[i+1] = ppsz_argv[i];
67 libvlc_instance_t *p_libvlc_instance = libvlc_new(i_argc+1, argv, &ex);
69 if( !p_libvlc_instance || libvlc_exception_raised(&ex) )
71 libvlc_exception_clear(&ex);
75 printf("#compdef vlc\n\n"
77 "#This file is autogenerated by zsh.cpp\n"
78 "typeset -A opt_args\n"
79 "local context state line ret=1\n"
80 "local modules\n\n" );
82 PrintModuleList( mods, mods2 );
84 printf( "_arguments -S -s \\\n" );
85 ParseModules( mods, mods2 );
86 printf( " \"(--module)-p[print help on module]:print help on module:($modules)\"\\\n" );
87 printf( " \"(-p)--module[print help on module]:print help on module:($modules)\"\\\n" );
88 printf( " \"(--help)-h[print help]\"\\\n" );
89 printf( " \"(-h)--help[print help]\"\\\n" );
90 printf( " \"(--longhelp)-H[print detailed help]\"\\\n" );
91 printf( " \"(-H)--longhelp[print detailed help]\"\\\n" );
92 printf( " \"(--list)-l[print a list of available modules]\"\\\n" );
93 printf( " \"(-l)--list[print a list of available modules]\"\\\n" );
94 printf( " \"--save-config[save the current command line options in the config file]\"\\\n" );
95 printf( " \"--reset-config[reset the current config to the default values]\"\\\n" );
96 printf( " \"--config[use alternate config file]\"\\\n" );
97 printf( " \"--reset-plugins-cache[resets the current plugins cache]\"\\\n" );
98 printf( " \"--version[print version information]\"\\\n" );
99 printf( " \"*:Playlist item:->mrl\" && ret=0\n\n" );
101 printf( "case $state in\n" );
103 printf( " _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0\n" );
105 printf( "esac\n\n" );
107 printf( "return ret\n" );
109 libvlc_release( p_libvlc_instance );
114 void ParseModules( mumap &mods, mcmap &mods2 )
118 module_config_t *p_item;
123 /* List the plugins */
124 p_list = module_list_get(&i_modules);
125 if( !p_list ) return;
126 for( i_index = 0; i_index < i_modules; i_index++ )
128 p_module = p_list[i_index];
130 /* Exclude empty plugins (submodules don't have config options, they
131 * are stored in the parent module) */
132 if( p_module->b_submodule )
134 // p_item = ((module_t *)p_module->p_parent)->p_config;
136 p_item = p_module->p_config;
138 // printf( " #%s\n", p_module->psz_longname );
139 if( !p_item ) continue;
143 if( p_item->i_type == CONFIG_CATEGORY )
145 // printf( " #Category %d\n", p_item->i_value );
147 else if( p_item->i_type == CONFIG_SUBCATEGORY )
149 // printf( " #Subcategory %d\n", p_item->i_value );
151 if( p_item->i_type & CONFIG_ITEM )
152 ParseOption( p_item, mods, mods2 );
154 while( i_items++ < p_module->i_config_items && p_item++ );
157 module_list_free( p_list );
160 void PrintModuleList( mumap &mods, mcmap &mods2 )
162 module_t **p_list = NULL;
168 /* List the plugins */
169 p_list = module_list_get(&i_modules);
170 if( !p_list ) return;
172 printf( "modules=\"" );
173 for( i_index = 0; i_index < i_modules; i_index++ )
175 p_module = p_list[i_index];
177 /* Exclude empty plugins (submodules don't have config options, they
178 * are stored in the parent module) */
180 if( strcmp( p_module->psz_object_name, "main" ) )
182 mods.insert( mpair( p_module->psz_capability,
183 p_module->psz_object_name ) );
184 module_config_t *p_config = p_module->p_config;
188 /* Hack: required subcategory is stored in i_min */
189 if( p_config->i_type == CONFIG_SUBCATEGORY )
191 mods2.insert( mcpair( p_config->value.i,
192 p_module->psz_object_name ) );
194 } while( i_items++ < p_module->i_config_items && p_config++ );
195 if( p_module->b_submodule )
197 printf( "%s ", p_module->psz_object_name );
202 module_list_free( p_list );
206 void ParseOption( module_config_t *p_item, mumap &mods, mcmap &mods2 )
208 char *psz_arguments = NULL;
215 #define DUP( x ) strdup( x ? x : "" )
217 //Skip deprecated options
218 if( p_item->b_removed )
221 switch( p_item->i_type )
223 case CONFIG_ITEM_MODULE:
225 std::pair<mumap::iterator, mumap::iterator> range = mods.equal_range( p_item->psz_type );
226 std::string list = (*range.first).second;
228 while( range.first != range.second )
230 list = list.append( " " );
231 list = list.append( range.first->second );
234 asprintf( &psz_arguments, "(%s)", list.c_str() );
237 case CONFIG_ITEM_MODULE_CAT:
239 std::pair<mcmap::iterator, mcmap::iterator> range =
240 mods2.equal_range( p_item->min.i );
241 std::string list = (*range.first).second;
243 while( range.first != range.second )
245 list = list.append( " " );
246 list = list.append( range.first->second );
249 asprintf( &psz_arguments, "(%s)", list.c_str() );
252 case CONFIG_ITEM_MODULE_LIST_CAT:
254 std::pair<mcmap::iterator, mcmap::iterator> range =
255 mods2.equal_range( p_item->min.i );
256 std::string list = "_values -s , ";
257 list = list.append( p_item->psz_name );
258 while( range.first != range.second )
260 list = list.append( " '*" );
261 list = list.append( range.first->second );
262 list = list.append( "'" );
265 psz_arguments = strdup( list.c_str() );
269 case CONFIG_ITEM_STRING:
272 int i = p_item->i_list -1;
274 if( p_item->ppsz_list_text )
275 asprintf( &psz_list, "%s\\:%s", p_item->ppsz_list[i],
276 p_item->ppsz_list_text[i] );
278 psz_list = strdup(p_item->ppsz_list[i]);
282 if( p_item->ppsz_list_text )
283 asprintf( &psz_list2, "%s\\:%s %s", p_item->ppsz_list[i-1],
284 p_item->ppsz_list_text[i-1], psz_list );
286 asprintf( &psz_list2, "%s %s", p_item->ppsz_list[i-1],
290 psz_list = psz_list2;
293 if( p_item->ppsz_list_text )
294 asprintf( &psz_arguments, "((%s))", psz_list );
296 asprintf( &psz_arguments, "(%s)", psz_list );
302 case CONFIG_ITEM_FILE:
303 psz_arguments = strdup( "_files" );
305 case CONFIG_ITEM_DIRECTORY:
306 psz_arguments = strdup( "_files -/" );
309 case CONFIG_ITEM_INTEGER:
312 int i = p_item->i_list -1;
314 if( p_item->ppsz_list_text )
315 asprintf( &psz_list, "%d\\:%s", p_item->pi_list[i],
316 p_item->ppsz_list_text[i] );
318 psz_list = strdup(p_item->ppsz_list[i]);
322 if( p_item->ppsz_list_text )
323 asprintf( &psz_list2, "%d\\:%s %s", p_item->pi_list[i-1],
324 p_item->ppsz_list_text[i-1], psz_list );
326 asprintf( &psz_list2, "%s %s", p_item->ppsz_list[i-1],
330 psz_list = psz_list2;
333 if( p_item->ppsz_list_text )
334 asprintf( &psz_arguments, "((%s))", psz_list );
336 asprintf( &psz_arguments, "(%s)", psz_list );
340 else if( p_item->min.i != 0 || p_item->max.i != 0 )
342 // p_control = new RangedIntConfigControl( p_this, p_item, parent );
346 // p_control = new IntegerConfigControl( p_this, p_item, parent );
350 case CONFIG_ITEM_KEY:
351 // p_control = new KeyConfigControl( p_this, p_item, parent );
354 case CONFIG_ITEM_FLOAT:
355 // p_control = new FloatConfigControl( p_this, p_item, parent );
358 case CONFIG_ITEM_BOOL:
359 // p_control = new BoolConfigControl( p_this, p_item, parent );
360 asprintf( &psz_exclusive, "--no%s --no-%s", p_item->psz_name,
362 psz_name = DUP( p_item->psz_name );
363 psz_text = DUP( p_item->psz_text );
364 psz_longtext = DUP( p_item->psz_longtext );
365 PrintOption( psz_name, p_item->i_short, psz_exclusive,
366 psz_text, psz_longtext, psz_arguments );
369 free( psz_longtext );
370 free( psz_exclusive );
371 asprintf( &psz_exclusive, "--no%s --%s", p_item->psz_name,
373 asprintf( &psz_option, "no-%s", p_item->psz_name );
374 psz_text = DUP( p_item->psz_text );
375 psz_longtext = DUP( p_item->psz_longtext );
376 PrintOption( psz_option, p_item->i_short, psz_exclusive,
377 psz_text, psz_longtext, psz_arguments );
379 free( psz_longtext );
380 free( psz_exclusive );
382 asprintf( &psz_exclusive, "--no-%s --%s", p_item->psz_name,
384 asprintf( &psz_option, "no%s", p_item->psz_name );
385 psz_text = DUP( p_item->psz_text );
386 psz_longtext = DUP( p_item->psz_longtext );
387 PrintOption( psz_option, p_item->i_short, psz_exclusive,
388 psz_text, psz_longtext, psz_arguments );
390 free( psz_longtext );
391 free( psz_exclusive );
396 // p_control = new SectionConfigControl( p_this, p_item, parent );
402 psz_name = DUP( p_item->psz_name );
403 psz_text = DUP( p_item->psz_text );
404 psz_longtext = DUP( p_item->psz_longtext );
405 PrintOption( psz_name, p_item->i_short, NULL,
406 psz_text, psz_longtext, psz_arguments );
409 free( psz_longtext );
410 free( psz_arguments );
413 void PrintOption( char *psz_option, char i_short, char *psz_exclusive,
414 char *psz_text, char *psz_longtext, char *psz_args )
419 while( (foo = strchr( psz_text, ':' ))) *foo=';';
420 while( (foo = strchr( psz_text, '"' ))) *foo='\'';
424 while( (foo = strchr( psz_longtext, ':' ))) *foo=';';
425 while( (foo = strchr( psz_longtext, '"' ))) *foo='\'';
428 strchr( psz_longtext, '\n' ) ||
429 strchr( psz_longtext, '(' ) ) psz_longtext = psz_text;
433 printf( " \"(-%c)--%s%s[%s]", i_short,
434 psz_option, psz_args?"=":"", psz_text );
436 printf( " \"(-%c%s)--%s%s[%s]", i_short, psz_exclusive,
437 psz_option, psz_args?"=":"", psz_text );
439 printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
442 printf( " \"(--%s%s)-%c[%s]", psz_option, psz_exclusive,
445 printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
453 printf( " \"(%s)--%s%s[%s]", psz_exclusive, psz_option,
454 psz_args?"=":"", psz_text );
456 printf( " \"--%s[%s]", psz_option, psz_text );
459 printf( ":%s:%s\"\\\n", psz_longtext, psz_args );