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 */
59 const char *argv
[i_argc
+ 1];
61 for( int i
= 0; i
< i_argc
; i
++ )
62 argv
[i
+1] = ppsz_argv
[i
];
63 libvlc_instance_t
*p_libvlc_instance
= libvlc_new(i_argc
+1, argv
);
65 if( !p_libvlc_instance
)
70 printf("#compdef vlc cvlc rvlc svlc mvlc qvlc nvlc\n\n"
72 "#This file is autogenerated by zsh.cpp\n"
73 "typeset -A opt_args\n"
74 "local context state line ret=1\n"
75 "local modules\n\n" );
77 PrintModuleList( mods
, mods2
);
79 printf( "_arguments -S -s \\\n" );
80 ParseModules( mods
, mods2
);
81 printf( " \"(--module)-p[print help on module]:print help on module:($modules)\"\\\n" );
82 printf( " \"(-p)--module[print help on module]:print help on module:($modules)\"\\\n" );
83 printf( " \"(--help)-h[print help]\"\\\n" );
84 printf( " \"(-h)--help[print help]\"\\\n" );
85 printf( " \"(--longhelp)-H[print detailed help]\"\\\n" );
86 printf( " \"(-H)--longhelp[print detailed help]\"\\\n" );
87 printf( " \"(--list)-l[print a list of available modules]\"\\\n" );
88 printf( " \"(-l)--list[print a list of available modules]\"\\\n" );
89 printf( " \"--reset-config[reset the current config to the default values]\"\\\n" );
90 printf( " \"--config[use alternate config file]\"\\\n" );
91 printf( " \"--reset-plugins-cache[resets the current plugins cache]\"\\\n" );
92 printf( " \"--version[print version information]\"\\\n" );
93 printf( " \"*:Playlist item:->mrl\" && ret=0\n\n" );
95 printf( "case $state in\n" );
97 printf( " _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0\n" );
101 printf( "return ret\n" );
103 libvlc_release( p_libvlc_instance
);
108 void ParseModules( mumap
&mods
, mcmap
&mods2
)
112 module_config_t
*p_item
;
117 /* List the plugins */
118 p_list
= module_list_get(&i_modules
);
119 if( !p_list
) return;
120 for( i_index
= 0; i_index
< i_modules
; i_index
++ )
122 p_module
= p_list
[i_index
];
124 /* Exclude empty plugins (submodules don't have config options, they
125 * are stored in the parent module) */
126 if( p_module
->b_submodule
)
128 // p_item = ((module_t *)p_module->p_parent)->p_config;
130 p_item
= p_module
->p_config
;
132 // printf( " #%s\n", p_module->psz_longname );
133 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( i_items
++ < p_module
->i_config_items
&& p_item
++ );
151 module_list_free( p_list
);
154 void PrintModuleList( mumap
&mods
, mcmap
&mods2
)
156 module_t
**p_list
= NULL
;
162 /* List the plugins */
163 p_list
= module_list_get(&i_modules
);
164 if( !p_list
) return;
166 printf( "modules=\"" );
167 for( i_index
= 0; i_index
< i_modules
; i_index
++ )
169 p_module
= p_list
[i_index
];
171 /* Exclude empty plugins (submodules don't have config options, they
172 * are stored in the parent module) */
174 if( strcmp( p_module
->psz_object_name
, "main" ) )
176 mods
.insert( mpair( p_module
->psz_capability
,
177 p_module
->psz_object_name
) );
178 module_config_t
*p_config
= p_module
->p_config
;
182 /* Hack: required subcategory is stored in i_min */
183 if( p_config
->i_type
== CONFIG_SUBCATEGORY
)
185 mods2
.insert( mcpair( p_config
->value
.i
,
186 p_module
->psz_object_name
) );
188 } while( i_items
++ < p_module
->i_config_items
&& p_config
++ );
189 if( p_module
->b_submodule
)
191 printf( "%s ", p_module
->psz_object_name
);
196 module_list_free( p_list
);
200 void ParseOption( module_config_t
*p_item
, mumap
&mods
, mcmap
&mods2
)
202 char *psz_arguments
= NULL
;
209 #define DUP( x ) strdup( x ? x : "" )
211 //Skip deprecated options
212 if( p_item
->b_removed
)
215 switch( p_item
->i_type
)
217 case CONFIG_ITEM_MODULE
:
219 std::pair
<mumap::iterator
, mumap::iterator
> range
= mods
.equal_range( p_item
->psz_type
);
220 std::string list
= (*range
.first
).second
;
221 if( range
.first
!= range
.second
)
223 while( range
.first
++ != range
.second
)
225 list
= list
.append( " " );
226 list
= list
.append( range
.first
->second
);
228 asprintf( &psz_arguments
, "(%s)", list
.c_str() );
232 case CONFIG_ITEM_MODULE_CAT
:
234 std::pair
<mcmap::iterator
, mcmap::iterator
> range
=
235 mods2
.equal_range( p_item
->min
.i
);
236 std::string list
= (*range
.first
).second
;
237 if( range
.first
!= range
.second
)
239 while( range
.first
++ != range
.second
)
241 list
= list
.append( " " );
242 list
= list
.append( range
.first
->second
);
244 asprintf( &psz_arguments
, "(%s)", list
.c_str() );
248 case CONFIG_ITEM_MODULE_LIST_CAT
:
250 std::pair
<mcmap::iterator
, mcmap::iterator
> range
=
251 mods2
.equal_range( p_item
->min
.i
);
252 std::string list
= "_values -s , ";
253 list
= list
.append( p_item
->psz_name
);
254 while( range
.first
!= range
.second
)
256 list
= list
.append( " '*" );
257 list
= list
.append( range
.first
->second
);
258 list
= list
.append( "'" );
261 psz_arguments
= strdup( list
.c_str() );
265 case CONFIG_ITEM_STRING
:
268 int i
= p_item
->i_list
-1;
270 if( p_item
->ppsz_list_text
)
271 asprintf( &psz_list
, "%s\\:%s", p_item
->ppsz_list
[i
],
272 p_item
->ppsz_list_text
[i
] );
274 psz_list
= strdup(p_item
->ppsz_list
[i
]);
278 if( p_item
->ppsz_list_text
)
279 asprintf( &psz_list2
, "%s\\:%s %s", p_item
->ppsz_list
[i
-1],
280 p_item
->ppsz_list_text
[i
-1], psz_list
);
282 asprintf( &psz_list2
, "%s %s", p_item
->ppsz_list
[i
-1],
286 psz_list
= psz_list2
;
289 if( p_item
->ppsz_list_text
)
290 asprintf( &psz_arguments
, "((%s))", psz_list
);
292 asprintf( &psz_arguments
, "(%s)", psz_list
);
298 case CONFIG_ITEM_FILE
:
299 psz_arguments
= strdup( "_files" );
301 case CONFIG_ITEM_DIRECTORY
:
302 psz_arguments
= strdup( "_files -/" );
305 case CONFIG_ITEM_INTEGER
:
308 int i
= p_item
->i_list
-1;
310 if( p_item
->ppsz_list_text
)
311 asprintf( &psz_list
, "%d\\:%s", p_item
->pi_list
[i
],
312 p_item
->ppsz_list_text
[i
] );
314 psz_list
= strdup(p_item
->ppsz_list
[i
]);
318 if( p_item
->ppsz_list_text
)
319 asprintf( &psz_list2
, "%d\\:%s %s", p_item
->pi_list
[i
-1],
320 p_item
->ppsz_list_text
[i
-1], psz_list
);
322 asprintf( &psz_list2
, "%s %s", p_item
->ppsz_list
[i
-1],
326 psz_list
= psz_list2
;
329 if( p_item
->ppsz_list_text
)
330 asprintf( &psz_arguments
, "((%s))", psz_list
);
332 asprintf( &psz_arguments
, "(%s)", psz_list
);
336 else if( p_item
->min
.i
!= 0 || p_item
->max
.i
!= 0 )
338 // p_control = new RangedIntConfigControl( p_this, p_item, parent );
342 // p_control = new IntegerConfigControl( p_this, p_item, parent );
346 case CONFIG_ITEM_KEY
:
347 // p_control = new KeyConfigControl( p_this, p_item, parent );
350 case CONFIG_ITEM_FLOAT
:
351 // p_control = new FloatConfigControl( p_this, p_item, parent );
354 case CONFIG_ITEM_BOOL
:
355 // p_control = new BoolConfigControl( p_this, p_item, parent );
356 asprintf( &psz_exclusive
, "--no%s --no-%s", p_item
->psz_name
,
358 psz_name
= DUP( p_item
->psz_name
);
359 psz_text
= DUP( p_item
->psz_text
);
360 psz_longtext
= DUP( p_item
->psz_longtext
);
361 PrintOption( psz_name
, p_item
->i_short
, psz_exclusive
,
362 psz_text
, psz_longtext
, psz_arguments
);
365 free( psz_longtext
);
366 free( psz_exclusive
);
367 asprintf( &psz_exclusive
, "--no%s --%s", p_item
->psz_name
,
369 asprintf( &psz_option
, "no-%s", p_item
->psz_name
);
370 psz_text
= DUP( p_item
->psz_text
);
371 psz_longtext
= DUP( p_item
->psz_longtext
);
372 PrintOption( psz_option
, p_item
->i_short
, psz_exclusive
,
373 psz_text
, psz_longtext
, psz_arguments
);
375 free( psz_longtext
);
376 free( psz_exclusive
);
378 asprintf( &psz_exclusive
, "--no-%s --%s", p_item
->psz_name
,
380 asprintf( &psz_option
, "no%s", p_item
->psz_name
);
381 psz_text
= DUP( p_item
->psz_text
);
382 psz_longtext
= DUP( p_item
->psz_longtext
);
383 PrintOption( psz_option
, p_item
->i_short
, psz_exclusive
,
384 psz_text
, psz_longtext
, psz_arguments
);
386 free( psz_longtext
);
387 free( psz_exclusive
);
392 // p_control = new SectionConfigControl( p_this, p_item, parent );
398 psz_name
= DUP( p_item
->psz_name
);
399 psz_text
= DUP( p_item
->psz_text
);
400 psz_longtext
= DUP( p_item
->psz_longtext
);
401 PrintOption( psz_name
, p_item
->i_short
, NULL
,
402 psz_text
, psz_longtext
, psz_arguments
);
405 free( psz_longtext
);
406 free( psz_arguments
);
409 void PrintOption( char *psz_option
, char i_short
, char *psz_exclusive
,
410 char *psz_text
, char *psz_longtext
, char *psz_args
)
415 while( (foo
= strchr( psz_text
, ':' ))) *foo
=';';
416 while( (foo
= strchr( psz_text
, '"' ))) *foo
='\'';
420 while( (foo
= strchr( psz_longtext
, ':' ))) *foo
=';';
421 while( (foo
= strchr( psz_longtext
, '"' ))) *foo
='\'';
424 strchr( psz_longtext
, '\n' ) ||
425 strchr( psz_longtext
, '(' ) ) psz_longtext
= psz_text
;
429 printf( " \"(-%c)--%s%s[%s]", i_short
,
430 psz_option
, psz_args
?"=":"", psz_text
);
432 printf( " \"(-%c%s)--%s%s[%s]", i_short
, psz_exclusive
,
433 psz_option
, psz_args
?"=":"", psz_text
);
435 printf( ":%s:%s\"\\\n", psz_longtext
, psz_args
);
439 printf( " \"(--%s%s)-%c[%s]", psz_option
, psz_exclusive
? psz_exclusive
: "",
442 printf( ":%s:%s\"\\\n", psz_longtext
, psz_args
);
450 printf( " \"(%s)--%s%s[%s]", psz_exclusive
, psz_option
,
451 psz_args
?"=":"", psz_text
);
453 printf( " \"--%s[%s]", psz_option
, psz_text
);
456 printf( ":%s:%s\"\\\n", psz_longtext
, psz_args
);