1 /*****************************************************************************
2 * zsh.cpp: create zsh completion rule for vlc
3 *****************************************************************************
4 * Copyright (C) 2005 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
> 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( libvlc_int_t
*p_libvlc
, mmap
&mods
, mcmap
&mods2
);
39 void PrintModuleList( libvlc_int_t
*p_libvlc
, 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
);
44 int main( int i_argc
, char **ppsz_argv
)
48 /* Create a libvlc structure */
49 int i_ret
= VLC_Create();
50 libvlc_int_t
*p_libvlc
;
57 /* Initialize libvlc */
58 i_ret
= VLC_Init( 0, i_argc
, ppsz_argv
);
64 p_libvlc
= (libvlc_int_t
*)vlc_object_get( (vlc_object_t
*)NULL
, i_ret
);
65 printf("#compdef vlc\n\n"
67 "#This file is autogenerated by zsh.cpp\n"
68 "typeset -A opt_args\n"
69 "local context state line ret=1\n"
70 "local modules\n\n" );
72 PrintModuleList( p_libvlc
, mods
, mods2
);
74 printf( "_arguments -S -s \\\n" );
75 ParseModules( p_libvlc
, mods
, mods2
);
76 printf( " \"(--module)-p[print help on module]:print help on module:($modules)\"\\\n" );
77 printf( " \"(-p)--module[print help on module]:print help on module:($modules)\"\\\n" );
78 printf( " \"(--help)-h[print help]\"\\\n" );
79 printf( " \"(-h)--help[print help]\"\\\n" );
80 printf( " \"(--longhelp)-H[print detailed help]\"\\\n" );
81 printf( " \"(-H)--longhelp[print detailed help]\"\\\n" );
82 printf( " \"(--list)-l[print a list of available modules]\"\\\n" );
83 printf( " \"(-l)--list[print a list of available modules]\"\\\n" );
84 printf( " \"--save-config[save the current command line options in the config file]\"\\\n" );
85 printf( " \"--reset-config[reset the current config to the default values]\"\\\n" );
86 printf( " \"--config[use alternate config file]\"\\\n" );
87 printf( " \"--reset-plugins-cache[resets the current plugins cache]\"\\\n" );
88 printf( " \"--version[print version information]\"\\\n" );
89 printf( " \"*:Playlist item:->mrl\" && ret=0\n\n" );
91 printf( "case $state in\n" );
93 printf( " _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0\n" );
97 printf( "return ret\n" );
100 /* Finish the threads */
103 /* Destroy the libvlc structure */
110 void ParseModules( libvlc_int_t
*p_libvlc
, mmap
&mods
, mcmap
&mods2
)
112 vlc_list_t
*p_list
= NULL
;;
114 module_config_t
*p_item
;
118 /* List the plugins */
119 p_list
= vlc_list_find( p_libvlc
, 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;
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
->i_config_items
&& p_item
++ );
154 void PrintModuleList( libvlc_int_t
*p_libvlc
, mmap
&mods
, mcmap
&mods2
)
156 vlc_list_t
*p_list
= NULL
;;
161 /* List the plugins */
162 p_list
= vlc_list_find( p_libvlc
, VLC_OBJECT_MODULE
, FIND_ANYWHERE
);
163 if( !p_list
) return;
165 printf( "modules=\"" );
166 for( i_index
= 0; i_index
< p_list
->i_count
; i_index
++ )
168 p_module
= (module_t
*)p_list
->p_values
[i_index
].p_object
;
170 /* Exclude empty plugins (submodules don't have config options, they
171 * are stored in the parent module) */
173 if( strcmp( p_module
->psz_object_name
, "main" ) )
175 mods
.insert( mpair( p_module
->psz_capability
,
176 p_module
->psz_object_name
) );
177 module_config_t
*p_config
= p_module
->p_config
;
181 /* Hack: required subcategory is stored in i_min */
182 if( p_config
->i_type
== CONFIG_SUBCATEGORY
)
184 mods2
.insert( mcpair( p_config
->value
.i
,
185 p_module
->psz_object_name
) );
187 } while( i_items
++ < p_module
->i_config_items
&& p_config
++ );
188 if( p_module
->b_submodule
)
190 printf( "%s ", p_module
->psz_object_name
);
198 void ParseOption( module_config_t
*p_item
, mmap
&mods
, mcmap
&mods2
)
200 char *psz_arguments
= strdup( "" );
207 #define DUP( x ) strdup( x ? x : "" )
209 //Skip deprecated options
210 if( p_item
->psz_current
)
213 switch( p_item
->i_type
)
215 case CONFIG_ITEM_MODULE
:
217 std::pair
<mmap::iterator
, mmap::iterator
> range
= mods
.equal_range( p_item
->psz_type
);
218 std::string list
= (*range
.first
).second
;
220 while( range
.first
!= range
.second
)
222 list
= list
.append( " " );
223 list
= list
.append( range
.first
->second
);
226 asprintf( &psz_arguments
, "(%s)", list
.c_str() );
229 case CONFIG_ITEM_MODULE_CAT
:
231 std::pair
<mcmap::iterator
, mcmap::iterator
> range
=
232 mods2
.equal_range( p_item
->min
.i
);
233 std::string list
= (*range
.first
).second
;
235 while( range
.first
!= range
.second
)
237 list
= list
.append( " " );
238 list
= list
.append( range
.first
->second
);
241 asprintf( &psz_arguments
, "(%s)", list
.c_str() );
244 case CONFIG_ITEM_MODULE_LIST_CAT
:
246 std::pair
<mcmap::iterator
, mcmap::iterator
> range
=
247 mods2
.equal_range( p_item
->min
.i
);
248 std::string list
= "_values -s , ";
249 list
= list
.append( p_item
->psz_name
);
250 while( range
.first
!= range
.second
)
252 list
= list
.append( " '*" );
253 list
= list
.append( range
.first
->second
);
254 list
= list
.append( "'" );
257 asprintf( &psz_arguments
, "%s", list
.c_str() );
261 case CONFIG_ITEM_STRING
:
264 int i
= p_item
->i_list
-1;
266 if( p_item
->ppsz_list_text
)
267 asprintf( &psz_list
, "%s\\:%s", p_item
->ppsz_list
[i
],
268 p_item
->ppsz_list_text
[i
] );
270 psz_list
= strdup(p_item
->ppsz_list
[i
]);
274 if( p_item
->ppsz_list_text
)
275 asprintf( &psz_list2
, "%s\\:%s %s", p_item
->ppsz_list
[i
-1],
276 p_item
->ppsz_list_text
[i
-1], psz_list
);
278 asprintf( &psz_list2
, "%s %s", p_item
->ppsz_list
[i
-1],
282 psz_list
= psz_list2
;
285 if( p_item
->ppsz_list_text
)
286 asprintf( &psz_arguments
, "((%s))", psz_list
);
288 asprintf( &psz_arguments
, "(%s)", psz_list
);
294 case CONFIG_ITEM_FILE
:
295 psz_arguments
= strdup( "_files" );
297 case CONFIG_ITEM_DIRECTORY
:
298 psz_arguments
= strdup( "_files -/" );
301 case CONFIG_ITEM_INTEGER
:
304 int i
= p_item
->i_list
-1;
306 if( p_item
->ppsz_list_text
)
307 asprintf( &psz_list
, "%d\\:%s", p_item
->pi_list
[i
],
308 p_item
->ppsz_list_text
[i
] );
310 psz_list
= strdup(p_item
->ppsz_list
[i
]);
314 if( p_item
->ppsz_list_text
)
315 asprintf( &psz_list2
, "%d\\:%s %s", p_item
->pi_list
[i
-1],
316 p_item
->ppsz_list_text
[i
-1], psz_list
);
318 asprintf( &psz_list2
, "%s %s", p_item
->ppsz_list
[i
-1],
322 psz_list
= psz_list2
;
325 if( p_item
->ppsz_list_text
)
326 asprintf( &psz_arguments
, "((%s))", psz_list
);
328 asprintf( &psz_arguments
, "(%s)", psz_list
);
332 else if( p_item
->min
.i
!= 0 || p_item
->max
.i
!= 0 )
334 // p_control = new RangedIntConfigControl( p_this, p_item, parent );
338 // p_control = new IntegerConfigControl( p_this, p_item, parent );
342 case CONFIG_ITEM_KEY
:
343 // p_control = new KeyConfigControl( p_this, p_item, parent );
346 case CONFIG_ITEM_FLOAT
:
347 // p_control = new FloatConfigControl( p_this, p_item, parent );
350 case CONFIG_ITEM_BOOL
:
351 // p_control = new BoolConfigControl( p_this, p_item, parent );
352 psz_arguments
= NULL
;
353 asprintf( &psz_exclusive
, "--no%s --no-%s", p_item
->psz_name
,
355 psz_name
= DUP( p_item
->psz_name
);
356 psz_text
= DUP( p_item
->psz_text
);
357 psz_longtext
= DUP( p_item
->psz_longtext
);
358 PrintOption( psz_name
, p_item
->i_short
, psz_exclusive
,
359 psz_text
, psz_longtext
, psz_arguments
);
362 free( psz_longtext
);
363 free( psz_exclusive
);
364 asprintf( &psz_exclusive
, "--no%s --%s", p_item
->psz_name
,
366 asprintf( &psz_option
, "no-%s", p_item
->psz_name
);
367 psz_text
= DUP( p_item
->psz_text
);
368 psz_longtext
= DUP( p_item
->psz_longtext
);
369 PrintOption( psz_option
, p_item
->i_short
, psz_exclusive
,
370 psz_text
, psz_longtext
, psz_arguments
);
372 free( psz_longtext
);
373 free( psz_exclusive
);
375 asprintf( &psz_exclusive
, "--no-%s --%s", p_item
->psz_name
,
377 asprintf( &psz_option
, "no%s", p_item
->psz_name
);
378 psz_text
= DUP( p_item
->psz_text
);
379 psz_longtext
= DUP( p_item
->psz_longtext
);
380 PrintOption( psz_option
, p_item
->i_short
, psz_exclusive
,
381 psz_text
, psz_longtext
, psz_arguments
);
383 free( psz_longtext
);
384 free( psz_exclusive
);
389 // p_control = new SectionConfigControl( p_this, p_item, parent );
395 psz_name
= DUP( p_item
->psz_name
);
396 psz_text
= DUP( p_item
->psz_text
);
397 psz_longtext
= DUP( p_item
->psz_longtext
);
398 PrintOption( psz_name
, p_item
->i_short
, NULL
,
399 psz_text
, psz_longtext
, psz_arguments
);
402 free( psz_longtext
);
405 void PrintOption( char *psz_option
, char i_short
, char *psz_exclusive
,
406 char *psz_text
, char *psz_longtext
, char *psz_args
)
411 while( (foo
= strchr( psz_text
, ':' ))) *foo
=';';
412 while( (foo
= strchr( psz_text
, '"' ))) *foo
='\'';
416 while( (foo
= strchr( psz_longtext
, ':' ))) *foo
=';';
417 while( (foo
= strchr( psz_longtext
, '"' ))) *foo
='\'';
420 strchr( psz_longtext
, '\n' ) ||
421 strchr( psz_longtext
, '(' ) ) psz_longtext
= psz_text
;
424 if( !psz_exclusive
) psz_exclusive
= strdup( "" );
425 else asprintf( &psz_exclusive
, " %s", psz_exclusive
);
426 printf( " \"(-%c%s)--%s%s[%s]", i_short
, psz_exclusive
,
427 psz_option
, psz_args
?"=":"", psz_text
);
429 printf( ":%s:%s\"\\\n", psz_longtext
, psz_args
);
432 printf( " \"(--%s%s)-%c[%s]", psz_option
, psz_exclusive
,
435 printf( ":%s:%s\"\\\n", psz_longtext
, psz_args
);
443 printf( " \"(%s)--%s%s[%s]", psz_exclusive
, psz_option
,
444 psz_args
?"=":"", psz_text
);
446 printf( " \"--%s[%s]", psz_option
, psz_text
);
449 printf( ":%s:%s\"\\\n", psz_longtext
, psz_args
);