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
;
45 #include <../src/modules/modules.h>
47 void ParseModules( libvlc_int_t
*p_libvlc
, mumap
&mods
, mcmap
&mods2
);
48 void PrintModuleList( libvlc_int_t
*p_libvlc
, mumap
&mods
, mcmap
&mods2
);
49 void ParseOption( module_config_t
*p_item
, mumap
&mods
, mcmap
&mods2
);
50 void PrintOption( char *psz_option
, char i_short
, char *psz_exlusive
,
51 char *psz_text
, char *psz_longtext
, char *psz_args
);
53 int main( int i_argc
, const char **ppsz_argv
)
57 /* Create a libvlc structure */
58 int i_ret
= VLC_Create();
59 libvlc_int_t
*p_libvlc
;
66 /* Initialize libvlc */
67 i_ret
= VLC_Init( 0, i_argc
, ppsz_argv
);
73 p_libvlc
= (libvlc_int_t
*)vlc_object_get( i_ret
);
74 printf("#compdef vlc\n\n"
76 "#This file is autogenerated by zsh.cpp\n"
77 "typeset -A opt_args\n"
78 "local context state line ret=1\n"
79 "local modules\n\n" );
81 PrintModuleList( p_libvlc
, mods
, mods2
);
83 printf( "_arguments -S -s \\\n" );
84 ParseModules( p_libvlc
, mods
, mods2
);
85 printf( " \"(--module)-p[print help on module]:print help on module:($modules)\"\\\n" );
86 printf( " \"(-p)--module[print help on module]:print help on module:($modules)\"\\\n" );
87 printf( " \"(--help)-h[print help]\"\\\n" );
88 printf( " \"(-h)--help[print help]\"\\\n" );
89 printf( " \"(--longhelp)-H[print detailed help]\"\\\n" );
90 printf( " \"(-H)--longhelp[print detailed help]\"\\\n" );
91 printf( " \"(--list)-l[print a list of available modules]\"\\\n" );
92 printf( " \"(-l)--list[print a list of available modules]\"\\\n" );
93 printf( " \"--save-config[save the current command line options in the config file]\"\\\n" );
94 printf( " \"--reset-config[reset the current config to the default values]\"\\\n" );
95 printf( " \"--config[use alternate config file]\"\\\n" );
96 printf( " \"--reset-plugins-cache[resets the current plugins cache]\"\\\n" );
97 printf( " \"--version[print version information]\"\\\n" );
98 printf( " \"*:Playlist item:->mrl\" && ret=0\n\n" );
100 printf( "case $state in\n" );
102 printf( " _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0\n" );
104 printf( "esac\n\n" );
106 printf( "return ret\n" );
108 /* Exit early since we did not release all the objects we used,
109 * but we don't care, our task is over */
112 /* Finish the threads */
115 /* Destroy the libvlc structure */
122 void ParseModules( libvlc_int_t
*p_libvlc
, mumap
&mods
, mcmap
&mods2
)
124 vlc_list_t
*p_list
= NULL
;;
126 module_config_t
*p_item
;
130 /* List the plugins */
131 p_list
= vlc_list_find( p_libvlc
, VLC_OBJECT_MODULE
, FIND_ANYWHERE
);
132 if( !p_list
) return;
133 for( i_index
= 0; i_index
< p_list
->i_count
; i_index
++ )
135 p_module
= (module_t
*)p_list
->p_values
[i_index
].p_object
;
137 /* Exclude empty plugins (submodules don't have config options, they
138 * are stored in the parent module) */
139 if( p_module
->b_submodule
)
141 // p_item = ((module_t *)p_module->p_parent)->p_config;
143 p_item
= p_module
->p_config
;
145 // printf( " #%s\n", p_module->psz_longname );
146 if( !p_item
) continue;
150 if( p_item
->i_type
== CONFIG_CATEGORY
)
152 // printf( " #Category %d\n", p_item->i_value );
154 else if( p_item
->i_type
== CONFIG_SUBCATEGORY
)
156 // printf( " #Subcategory %d\n", p_item->i_value );
158 if( p_item
->i_type
& CONFIG_ITEM
)
159 ParseOption( p_item
, mods
, mods2
);
161 while( i_items
++ < p_module
->i_config_items
&& p_item
++ );
166 void PrintModuleList( libvlc_int_t
*p_libvlc
, mumap
&mods
, mcmap
&mods2
)
168 vlc_list_t
*p_list
= NULL
;;
173 /* List the plugins */
174 p_list
= vlc_list_find( p_libvlc
, VLC_OBJECT_MODULE
, FIND_ANYWHERE
);
175 if( !p_list
) return;
177 printf( "modules=\"" );
178 for( i_index
= 0; i_index
< p_list
->i_count
; i_index
++ )
180 p_module
= (module_t
*)p_list
->p_values
[i_index
].p_object
;
182 /* Exclude empty plugins (submodules don't have config options, they
183 * are stored in the parent module) */
185 if( strcmp( p_module
->psz_object_name
, "main" ) )
187 mods
.insert( mpair( p_module
->psz_capability
,
188 p_module
->psz_object_name
) );
189 module_config_t
*p_config
= p_module
->p_config
;
193 /* Hack: required subcategory is stored in i_min */
194 if( p_config
->i_type
== CONFIG_SUBCATEGORY
)
196 mods2
.insert( mcpair( p_config
->value
.i
,
197 p_module
->psz_object_name
) );
199 } while( i_items
++ < p_module
->i_config_items
&& p_config
++ );
200 if( p_module
->b_submodule
)
202 printf( "%s ", p_module
->psz_object_name
);
210 void ParseOption( module_config_t
*p_item
, mumap
&mods
, mcmap
&mods2
)
212 char *psz_arguments
= strdup( "" );
219 #define DUP( x ) strdup( x ? x : "" )
221 //Skip deprecated options
222 if( p_item
->b_removed
)
225 switch( p_item
->i_type
)
227 case CONFIG_ITEM_MODULE
:
229 std::pair
<mumap::iterator
, mumap::iterator
> range
= mods
.equal_range( p_item
->psz_type
);
230 std::string list
= (*range
.first
).second
;
232 while( range
.first
!= range
.second
)
234 list
= list
.append( " " );
235 list
= list
.append( range
.first
->second
);
238 asprintf( &psz_arguments
, "(%s)", list
.c_str() );
241 case CONFIG_ITEM_MODULE_CAT
:
243 std::pair
<mcmap::iterator
, mcmap::iterator
> range
=
244 mods2
.equal_range( p_item
->min
.i
);
245 std::string list
= (*range
.first
).second
;
247 while( range
.first
!= range
.second
)
249 list
= list
.append( " " );
250 list
= list
.append( range
.first
->second
);
253 asprintf( &psz_arguments
, "(%s)", list
.c_str() );
256 case CONFIG_ITEM_MODULE_LIST_CAT
:
258 std::pair
<mcmap::iterator
, mcmap::iterator
> range
=
259 mods2
.equal_range( p_item
->min
.i
);
260 std::string list
= "_values -s , ";
261 list
= list
.append( p_item
->psz_name
);
262 while( range
.first
!= range
.second
)
264 list
= list
.append( " '*" );
265 list
= list
.append( range
.first
->second
);
266 list
= list
.append( "'" );
269 asprintf( &psz_arguments
, "%s", list
.c_str() );
273 case CONFIG_ITEM_STRING
:
276 int i
= p_item
->i_list
-1;
278 if( p_item
->ppsz_list_text
)
279 asprintf( &psz_list
, "%s\\:%s", p_item
->ppsz_list
[i
],
280 p_item
->ppsz_list_text
[i
] );
282 psz_list
= strdup(p_item
->ppsz_list
[i
]);
286 if( p_item
->ppsz_list_text
)
287 asprintf( &psz_list2
, "%s\\:%s %s", p_item
->ppsz_list
[i
-1],
288 p_item
->ppsz_list_text
[i
-1], psz_list
);
290 asprintf( &psz_list2
, "%s %s", p_item
->ppsz_list
[i
-1],
294 psz_list
= psz_list2
;
297 if( p_item
->ppsz_list_text
)
298 asprintf( &psz_arguments
, "((%s))", psz_list
);
300 asprintf( &psz_arguments
, "(%s)", psz_list
);
306 case CONFIG_ITEM_FILE
:
307 psz_arguments
= strdup( "_files" );
309 case CONFIG_ITEM_DIRECTORY
:
310 psz_arguments
= strdup( "_files -/" );
313 case CONFIG_ITEM_INTEGER
:
316 int i
= p_item
->i_list
-1;
318 if( p_item
->ppsz_list_text
)
319 asprintf( &psz_list
, "%d\\:%s", p_item
->pi_list
[i
],
320 p_item
->ppsz_list_text
[i
] );
322 psz_list
= strdup(p_item
->ppsz_list
[i
]);
326 if( p_item
->ppsz_list_text
)
327 asprintf( &psz_list2
, "%d\\:%s %s", p_item
->pi_list
[i
-1],
328 p_item
->ppsz_list_text
[i
-1], psz_list
);
330 asprintf( &psz_list2
, "%s %s", p_item
->ppsz_list
[i
-1],
334 psz_list
= psz_list2
;
337 if( p_item
->ppsz_list_text
)
338 asprintf( &psz_arguments
, "((%s))", psz_list
);
340 asprintf( &psz_arguments
, "(%s)", psz_list
);
344 else if( p_item
->min
.i
!= 0 || p_item
->max
.i
!= 0 )
346 // p_control = new RangedIntConfigControl( p_this, p_item, parent );
350 // p_control = new IntegerConfigControl( p_this, p_item, parent );
354 case CONFIG_ITEM_KEY
:
355 // p_control = new KeyConfigControl( p_this, p_item, parent );
358 case CONFIG_ITEM_FLOAT
:
359 // p_control = new FloatConfigControl( p_this, p_item, parent );
362 case CONFIG_ITEM_BOOL
:
363 // p_control = new BoolConfigControl( p_this, p_item, parent );
364 psz_arguments
= NULL
;
365 asprintf( &psz_exclusive
, "--no%s --no-%s", p_item
->psz_name
,
367 psz_name
= DUP( p_item
->psz_name
);
368 psz_text
= DUP( p_item
->psz_text
);
369 psz_longtext
= DUP( p_item
->psz_longtext
);
370 PrintOption( psz_name
, p_item
->i_short
, psz_exclusive
,
371 psz_text
, psz_longtext
, psz_arguments
);
374 free( psz_longtext
);
375 free( psz_exclusive
);
376 asprintf( &psz_exclusive
, "--no%s --%s", p_item
->psz_name
,
378 asprintf( &psz_option
, "no-%s", p_item
->psz_name
);
379 psz_text
= DUP( p_item
->psz_text
);
380 psz_longtext
= DUP( p_item
->psz_longtext
);
381 PrintOption( psz_option
, p_item
->i_short
, psz_exclusive
,
382 psz_text
, psz_longtext
, psz_arguments
);
384 free( psz_longtext
);
385 free( psz_exclusive
);
387 asprintf( &psz_exclusive
, "--no-%s --%s", p_item
->psz_name
,
389 asprintf( &psz_option
, "no%s", p_item
->psz_name
);
390 psz_text
= DUP( p_item
->psz_text
);
391 psz_longtext
= DUP( p_item
->psz_longtext
);
392 PrintOption( psz_option
, p_item
->i_short
, psz_exclusive
,
393 psz_text
, psz_longtext
, psz_arguments
);
395 free( psz_longtext
);
396 free( psz_exclusive
);
401 // p_control = new SectionConfigControl( p_this, p_item, parent );
407 psz_name
= DUP( p_item
->psz_name
);
408 psz_text
= DUP( p_item
->psz_text
);
409 psz_longtext
= DUP( p_item
->psz_longtext
);
410 PrintOption( psz_name
, p_item
->i_short
, NULL
,
411 psz_text
, psz_longtext
, psz_arguments
);
414 free( psz_longtext
);
417 void PrintOption( char *psz_option
, char i_short
, char *psz_exclusive
,
418 char *psz_text
, char *psz_longtext
, char *psz_args
)
423 while( (foo
= strchr( psz_text
, ':' ))) *foo
=';';
424 while( (foo
= strchr( psz_text
, '"' ))) *foo
='\'';
428 while( (foo
= strchr( psz_longtext
, ':' ))) *foo
=';';
429 while( (foo
= strchr( psz_longtext
, '"' ))) *foo
='\'';
432 strchr( psz_longtext
, '\n' ) ||
433 strchr( psz_longtext
, '(' ) ) psz_longtext
= psz_text
;
436 if( !psz_exclusive
) psz_exclusive
= strdup( "" );
437 else asprintf( &psz_exclusive
, " %s", psz_exclusive
);
438 printf( " \"(-%c%s)--%s%s[%s]", i_short
, psz_exclusive
,
439 psz_option
, psz_args
?"=":"", psz_text
);
441 printf( ":%s:%s\"\\\n", psz_longtext
, psz_args
);
444 printf( " \"(--%s%s)-%c[%s]", psz_option
, psz_exclusive
,
447 printf( ":%s:%s\"\\\n", psz_longtext
, psz_args
);
455 printf( " \"(%s)--%s%s[%s]", psz_exclusive
, psz_option
,
456 psz_args
?"=":"", psz_text
);
458 printf( " \"--%s[%s]", psz_option
, psz_text
);
461 printf( ":%s:%s\"\\\n", psz_longtext
, psz_args
);