1 /*****************************************************************************
2 * zsh.cpp: create zsh completion rule for vlc
3 *****************************************************************************
4 * Copyright (C) 1998-2005 VideoLAN
7 * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
30 typedef std::multimap<std::string, std::string> mmap;
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;
38 void ParseModules( vlc_t *p_vlc, mmap &mods, mcmap &mods2 );
39 void PrintModuleList( vlc_t *p_vlc, mmap &mods, mcmap &mods2 );
40 void ParseOption( module_config_t *p_item, mmap &mods, mcmap &mods2 );
41 void PrintOption( char *psz_option, char i_short, char *psz_exlusive,
42 char *psz_text, char *psz_longtext, char *psz_args );
43 extern "C"{ vlc_t * vlc_current_object( int i_object );}
45 int main( int i_argc, char **ppsz_argv )
49 /* Create a libvlc structure */
50 int i_ret = VLC_Create();
58 /* Initialize libvlc */
59 i_ret = VLC_Init( 0, i_argc, ppsz_argv );
65 p_vlc = vlc_current_object( i_ret );
66 printf("#compdef vlc\n\n"
68 "#This file is autogenerated by zsh.cpp\n"
69 "typeset -A opt_args\n"
70 "local context state line ret=1\n"
71 "local modules\n\n" );
73 PrintModuleList( p_vlc, mods, mods2 );
75 printf( "_arguments -S -s \\\n" );
76 ParseModules( p_vlc, mods, mods2 );
77 printf( " \"(--module)-p[print help on module]:print help on module:($modules)\"\\\n" );
78 printf( " \"(-p)--module[print help on module]:print help on module:($modules)\"\\\n" );
79 printf( " \"(--help)-h[print help]\"\\\n" );
80 printf( " \"(-h)--help[print help]\"\\\n" );
81 printf( " \"(--longhelp)-H[print detailed help]\"\\\n" );
82 printf( " \"(-H)--longhelp[print detailed help]\"\\\n" );
83 printf( " \"(--list)-l[print a list of available modules]\"\\\n" );
84 printf( " \"(-l)--list[print a list of available modules]\"\\\n" );
85 printf( " \"--save-config[save the current command line options in the config file]\"\\\n" );
86 printf( " \"--reset-config[reset the current config to the default values]\"\\\n" );
87 printf( " \"--config[use alternate config file]\"\\\n" );
88 printf( " \"--reset-plugins-cache[resets the current plugins cache]\"\\\n" );
89 printf( " \"--version[print version information]\"\\\n" );
90 printf( " \"*:Playlist item:->mrl\" && ret=0\n\n" );
92 printf( "case $state in\n" );
94 printf( " _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0\n" );
98 printf( "return ret\n" );
101 /* Finish the threads */
104 /* Destroy the libvlc structure */
111 void ParseModules( vlc_t *p_vlc, mmap &mods, mcmap &mods2 )
113 vlc_list_t *p_list = NULL;;
115 module_config_t *p_item;
118 /* List the plugins */
119 p_list = vlc_list_find( p_vlc, VLC_OBJECT_MODULE, FIND_ANYWHERE );
120 if( !p_list ) return;
121 for( i_index = 0; i_index < p_list->i_count; i_index++ )
123 p_module = (module_t *)p_list->p_values[i_index].p_object;
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;
137 if( p_item->i_type == CONFIG_CATEGORY )
139 // printf( " #Category %d\n", p_item->i_value );
141 else if( p_item->i_type == CONFIG_SUBCATEGORY )
143 // printf( " #Subcategory %d\n", p_item->i_value );
145 if( p_item->i_type & CONFIG_ITEM )
146 ParseOption( p_item, mods, mods2 );
148 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
153 void PrintModuleList( vlc_t *p_vlc, mmap &mods, mcmap &mods2 )
155 vlc_list_t *p_list = NULL;;
159 /* List the plugins */
160 p_list = vlc_list_find( p_vlc, VLC_OBJECT_MODULE, FIND_ANYWHERE );
161 if( !p_list ) return;
163 printf( "modules=\"" );
164 for( i_index = 0; i_index < p_list->i_count; i_index++ )
166 p_module = (module_t *)p_list->p_values[i_index].p_object;
168 /* Exclude empty plugins (submodules don't have config options, they
169 * are stored in the parent module) */
171 if( strcmp( p_module->psz_object_name, "main" ) )
173 mods.insert( mpair( p_module->psz_capability,
174 p_module->psz_object_name ) );
175 module_config_t *p_config = p_module->p_config;
178 /* Hack: required subcategory is stored in i_min */
179 if( p_config->i_type == CONFIG_SUBCATEGORY )
181 mods2.insert( mcpair( p_config->i_value,
182 p_module->psz_object_name ) );
184 } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
185 if( p_module->b_submodule )
187 printf( "%s ", p_module->psz_object_name );
195 void ParseOption( module_config_t *p_item, mmap &mods, mcmap &mods2 )
197 char *psz_arguments = "";
200 switch( p_item->i_type )
202 case CONFIG_ITEM_MODULE:
204 std::pair<mmap::iterator, mmap::iterator> range = mods.equal_range( p_item->psz_type );
205 std::string list = (*range.first).second;
207 while( range.first != range.second )
209 list = list.append( " " );
210 list = list.append( range.first->second );
213 asprintf( &psz_arguments, "(%s)", list.c_str() );
216 case CONFIG_ITEM_MODULE_CAT:
218 std::pair<mcmap::iterator, mcmap::iterator> range =
219 mods2.equal_range( p_item->i_min );
220 std::string list = (*range.first).second;
222 while( range.first != range.second )
224 list = list.append( " " );
225 list = list.append( range.first->second );
228 asprintf( &psz_arguments, "(%s)", list.c_str() );
231 case CONFIG_ITEM_MODULE_LIST_CAT:
233 std::pair<mcmap::iterator, mcmap::iterator> range =
234 mods2.equal_range( p_item->i_min );
235 std::string list = "_values -s , ";
236 list = list.append( p_item->psz_name );
237 while( range.first != range.second )
239 list = list.append( " '*" );
240 list = list.append( range.first->second );
241 list = list.append( "'" );
244 asprintf( &psz_arguments, "%s", list.c_str() );
248 case CONFIG_ITEM_STRING:
251 int i = p_item->i_list -1;
253 if( p_item->ppsz_list_text )
254 asprintf( &psz_list, "%s\\:%s", p_item->ppsz_list[i],
255 p_item->ppsz_list_text[i] );
257 psz_list = strdup(p_item->ppsz_list[i]);
261 if( p_item->ppsz_list_text )
262 asprintf( &psz_list2, "%s\\:%s %s", p_item->ppsz_list[i-1],
263 p_item->ppsz_list_text[i-1], psz_list );
265 asprintf( &psz_list2, "%s %s", p_item->ppsz_list[i-1],
269 psz_list = psz_list2;
272 if( p_item->ppsz_list_text )
273 asprintf( &psz_arguments, "((%s))", psz_list );
275 asprintf( &psz_arguments, "(%s)", psz_list );
281 case CONFIG_ITEM_FILE:
282 psz_arguments = "_files";
284 case CONFIG_ITEM_DIRECTORY:
285 psz_arguments = "_files -/";
288 case CONFIG_ITEM_INTEGER:
291 int i = p_item->i_list -1;
293 if( p_item->ppsz_list_text )
294 asprintf( &psz_list, "%d\\:%s", p_item->pi_list[i],
295 p_item->ppsz_list_text[i] );
297 psz_list = strdup(p_item->ppsz_list[i]);
301 if( p_item->ppsz_list_text )
302 asprintf( &psz_list2, "%d\\:%s %s", p_item->pi_list[i-1],
303 p_item->ppsz_list_text[i-1], psz_list );
305 asprintf( &psz_list2, "%s %s", p_item->ppsz_list[i-1],
309 psz_list = psz_list2;
312 if( p_item->ppsz_list_text )
313 asprintf( &psz_arguments, "((%s))", psz_list );
315 asprintf( &psz_arguments, "(%s)", psz_list );
319 else if( p_item->i_min != 0 || p_item->i_max != 0 )
321 // p_control = new RangedIntConfigControl( p_this, p_item, parent );
325 // p_control = new IntegerConfigControl( p_this, p_item, parent );
329 case CONFIG_ITEM_KEY:
330 // p_control = new KeyConfigControl( p_this, p_item, parent );
333 case CONFIG_ITEM_FLOAT:
334 // p_control = new FloatConfigControl( p_this, p_item, parent );
337 case CONFIG_ITEM_BOOL:
338 // p_control = new BoolConfigControl( p_this, p_item, parent );
339 psz_arguments = NULL;
340 asprintf( &psz_exclusive, "--no%s --no-%s", p_item->psz_name,
342 PrintOption( p_item->psz_name, p_item->i_short, psz_exclusive,
343 p_item->psz_text, p_item->psz_longtext, psz_arguments );
344 free( psz_exclusive );
345 asprintf( &psz_exclusive, "--no%s --%s", p_item->psz_name,
347 asprintf( &psz_option, "no-%s", p_item->psz_name );
348 PrintOption( psz_option, p_item->i_short, psz_exclusive,
349 p_item->psz_text, p_item->psz_longtext, psz_arguments );
350 free( psz_exclusive );
352 asprintf( &psz_exclusive, "--no-%s --%s", p_item->psz_name,
354 asprintf( &psz_option, "no%s", p_item->psz_name );
355 PrintOption( psz_option, p_item->i_short, psz_exclusive,
356 p_item->psz_text, p_item->psz_longtext, psz_arguments );
357 free( psz_exclusive );
362 // p_control = new SectionConfigControl( p_this, p_item, parent );
368 PrintOption( p_item->psz_name, p_item->i_short, NULL,
369 p_item->psz_text, p_item->psz_longtext, psz_arguments );
373 void PrintOption( char *psz_option, char i_short, char *psz_exclusive,
374 char *psz_text, char *psz_longtext, char *psz_args )
378 strchr( psz_longtext, '\n' ) ||
379 strchr( psz_longtext, '(' ) ) psz_longtext = psz_text;
380 while( (foo = strchr( psz_text, '"' ))) *foo='\'';
383 if( !psz_exclusive ) psz_exclusive = "";
384 else asprintf( &psz_exclusive, " %s", psz_exclusive );
385 printf( " \"(-%c%s)--%s%s[%s]", i_short, psz_exclusive,
386 psz_option, psz_args?"=":"", psz_text );
388 printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
391 printf( " \"(--%s%s)-%c[%s]", psz_option, psz_exclusive,
394 printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
402 printf( " \"(%s)--%s%s[%s]", psz_exclusive, psz_option,
403 psz_args?"=":"", psz_text );
405 printf( " \"--%s[%s]", psz_option, psz_text );
408 printf( ":%s:%s\"\\\n", psz_longtext, psz_args );