]> git.sesse.net Git - vlc/blob - extras/analyser/zsh.cpp
zsh completion: add all known aliases
[vlc] / extras / analyser / zsh.cpp
1 /*****************************************************************************
2  * zsh.cpp: create zsh completion rule for vlc
3  *****************************************************************************
4  * Copyright © 2005-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include <stdio.h>
25 #include <map>
26 #include <string>
27 #include <utility>
28 #include <iostream>
29 #include <algorithm>
30 typedef std::multimap<std::string, std::string> mumap;
31 typedef std::multimap<int, std::string> mcmap;
32
33 typedef std::pair<std::string, std::string> mpair;
34 typedef std::pair<int, std::string> mcpair;
35
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <vlc_common.h>
41 #include <vlc/vlc.h>
42
43 /* evil hack */
44 #undef __PLUGIN__
45 #undef __BUILTIN__
46 #include <../src/modules/modules.h>
47
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 );
53
54 int main( int i_argc, const char **ppsz_argv )
55 {
56     mumap mods;
57     mcmap mods2;
58     /* Create a libvlc structure */
59
60     libvlc_exception_t ex;
61     libvlc_exception_init(&ex);
62
63     const char *argv[i_argc + 1];
64     argv[0] = "vlc";
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);
68
69     if( !p_libvlc_instance || libvlc_exception_raised(&ex) )
70     {
71         libvlc_exception_clear(&ex);
72         return 1;
73     }
74
75     printf("#compdef vlc cvlc rvlc svlc mvlc qvlc nvlc\n\n"
76
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" );
81
82     PrintModuleList( mods, mods2 );
83
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" );
100
101     printf( "case $state in\n" );
102     printf( "  mrl)\n" );
103     printf( "    _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0\n" );
104     printf( "  ;;\n" );
105     printf( "esac\n\n" );
106
107     printf( "return ret\n" );
108
109     libvlc_release( p_libvlc_instance );
110
111     return 0;
112 }
113
114 void ParseModules( mumap &mods, mcmap &mods2 )
115 {
116     module_t       **p_list;
117     module_t        *p_module;
118     module_config_t *p_item;
119     int              i_index;
120     int              i_items;
121     size_t           i_modules;
122
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++ )
127     {
128         p_module = p_list[i_index];
129
130         /* Exclude empty plugins (submodules don't have config options, they
131          * are stored in the parent module) */
132         if( p_module->b_submodule )
133               continue;
134 //            p_item = ((module_t *)p_module->p_parent)->p_config;
135         else
136             p_item = p_module->p_config;
137
138 //        printf( "  #%s\n", p_module->psz_longname );
139         if( !p_item ) continue;
140         i_items = 0;
141         do
142         {
143             if( p_item->i_type == CONFIG_CATEGORY )
144             {
145 //                printf( "  #Category %d\n", p_item->i_value );
146             }
147             else if( p_item->i_type == CONFIG_SUBCATEGORY )
148             {
149 //                printf( "  #Subcategory %d\n", p_item->i_value );
150             }
151             if( p_item->i_type & CONFIG_ITEM )
152                 ParseOption( p_item, mods, mods2 );
153         }
154         while( i_items++ < p_module->i_config_items && p_item++ );
155
156     }
157     module_list_free( p_list );
158 }
159
160 void PrintModuleList( mumap &mods, mcmap &mods2 )
161 {
162     module_t       **p_list = NULL;
163     module_t        *p_module;
164     int              i_index;
165     int              i_items;
166     size_t           i_modules;
167
168     /* List the plugins */
169     p_list = module_list_get(&i_modules);
170     if( !p_list ) return;
171
172     printf( "modules=\"" );
173     for( i_index = 0; i_index < i_modules; i_index++ )
174     {
175         p_module = p_list[i_index];
176
177         /* Exclude empty plugins (submodules don't have config options, they
178          * are stored in the parent module) */
179
180         if( strcmp( p_module->psz_object_name, "main" ) )
181         {
182             mods.insert( mpair( p_module->psz_capability,
183                                 p_module->psz_object_name ) );
184             module_config_t *p_config = p_module->p_config;
185             i_items = 0;
186             if( p_config ) do
187             {
188                 /* Hack: required subcategory is stored in i_min */
189                 if( p_config->i_type == CONFIG_SUBCATEGORY )
190                 {
191                     mods2.insert( mcpair( p_config->value.i,
192                                           p_module->psz_object_name ) );
193                 }
194             } while( i_items++ < p_module->i_config_items && p_config++ );
195             if( p_module->b_submodule )
196                 continue;
197             printf( "%s ", p_module->psz_object_name );
198         }
199
200     }
201     printf( "\"\n\n" );
202     module_list_free( p_list );
203     return;
204 }
205
206 void ParseOption( module_config_t *p_item, mumap &mods, mcmap &mods2 )
207 {
208     char *psz_arguments = NULL;
209     char *psz_exclusive;
210     char *psz_option;
211     char *psz_name;
212     char *psz_text;
213     char *psz_longtext;
214
215 #define DUP( x ) strdup( x ? x : "" )
216
217     //Skip deprecated options
218     if( p_item->b_removed )
219         return;
220
221     switch( p_item->i_type )
222     {
223     case CONFIG_ITEM_MODULE:
224     {
225         std::pair<mumap::iterator, mumap::iterator> range = mods.equal_range( p_item->psz_type );
226         std::string list = (*range.first).second;
227         if( range.first != range.second )
228         {
229             while( range.first++ != range.second )
230             {
231                 list = list.append( " " );
232                 list = list.append( range.first->second );
233             }
234             asprintf( &psz_arguments, "(%s)", list.c_str() );
235         }
236     }
237     break;
238     case CONFIG_ITEM_MODULE_CAT:
239     {
240         std::pair<mcmap::iterator, mcmap::iterator> range =
241             mods2.equal_range( p_item->min.i );
242         std::string list = (*range.first).second;
243         if( range.first != range.second )
244         {
245             while( range.first++ != range.second )
246             {
247                 list = list.append( " " );
248                 list = list.append( range.first->second );
249             }
250             asprintf( &psz_arguments, "(%s)", list.c_str() );
251         }
252     }
253     break;
254     case CONFIG_ITEM_MODULE_LIST_CAT:
255     {
256         std::pair<mcmap::iterator, mcmap::iterator> range =
257             mods2.equal_range( p_item->min.i );
258         std::string list = "_values -s , ";
259         list = list.append( p_item->psz_name );
260         while( range.first != range.second )
261         {
262             list = list.append( " '*" );
263             list = list.append( range.first->second );
264             list = list.append( "'" );
265             ++range.first;
266         }
267         psz_arguments = strdup( list.c_str() );
268     }
269     break;
270
271     case CONFIG_ITEM_STRING:
272         if( p_item->i_list )
273         {
274             int i = p_item->i_list -1;
275             char *psz_list;
276             if( p_item->ppsz_list_text )
277                 asprintf( &psz_list, "%s\\:%s", p_item->ppsz_list[i],
278                           p_item->ppsz_list_text[i] );
279             else
280                 psz_list = strdup(p_item->ppsz_list[i]);
281             char *psz_list2;
282             while( i>1 )
283             {
284                 if( p_item->ppsz_list_text )
285                     asprintf( &psz_list2, "%s\\:%s %s", p_item->ppsz_list[i-1],
286                               p_item->ppsz_list_text[i-1], psz_list );
287                 else
288                     asprintf( &psz_list2, "%s %s", p_item->ppsz_list[i-1],
289                               psz_list );
290
291                 free( psz_list );
292                 psz_list = psz_list2;
293                 i--;
294             }
295             if( p_item->ppsz_list_text )
296                 asprintf( &psz_arguments, "((%s))", psz_list );
297             else
298                 asprintf( &psz_arguments, "(%s)", psz_list );
299
300             free( psz_list );
301         }
302         break;
303
304     case CONFIG_ITEM_FILE:
305         psz_arguments = strdup( "_files" );
306         break;
307     case CONFIG_ITEM_DIRECTORY:
308         psz_arguments = strdup( "_files -/" );
309         break;
310
311     case CONFIG_ITEM_INTEGER:
312         if( p_item->i_list )
313         {
314             int i = p_item->i_list -1;
315             char *psz_list;
316             if( p_item->ppsz_list_text )
317                 asprintf( &psz_list, "%d\\:%s", p_item->pi_list[i],
318                           p_item->ppsz_list_text[i] );
319             else
320                 psz_list = strdup(p_item->ppsz_list[i]);
321             char *psz_list2;
322             while( i>1 )
323             {
324                 if( p_item->ppsz_list_text )
325                     asprintf( &psz_list2, "%d\\:%s %s", p_item->pi_list[i-1],
326                               p_item->ppsz_list_text[i-1], psz_list );
327                 else
328                     asprintf( &psz_list2, "%s %s", p_item->ppsz_list[i-1],
329                               psz_list );
330
331                 free( psz_list );
332                 psz_list = psz_list2;
333                 i--;
334             }
335             if( p_item->ppsz_list_text )
336                 asprintf( &psz_arguments, "((%s))", psz_list );
337             else
338                 asprintf( &psz_arguments, "(%s)", psz_list );
339
340             free( psz_list );
341         }
342         else if( p_item->min.i != 0 || p_item->max.i != 0 )
343         {
344 //            p_control = new RangedIntConfigControl( p_this, p_item, parent );
345         }
346         else
347         {
348 //            p_control = new IntegerConfigControl( p_this, p_item, parent );
349         }
350         break;
351
352     case CONFIG_ITEM_KEY:
353 //        p_control = new KeyConfigControl( p_this, p_item, parent );
354         break;
355
356     case CONFIG_ITEM_FLOAT:
357 //        p_control = new FloatConfigControl( p_this, p_item, parent );
358         break;
359
360     case CONFIG_ITEM_BOOL:
361 //        p_control = new BoolConfigControl( p_this, p_item, parent );
362         asprintf( &psz_exclusive, "--no%s --no-%s", p_item->psz_name,
363                  p_item->psz_name );
364         psz_name = DUP( p_item->psz_name );
365         psz_text = DUP( p_item->psz_text );
366         psz_longtext = DUP( p_item->psz_longtext );
367         PrintOption( psz_name, p_item->i_short, psz_exclusive,
368                      psz_text, psz_longtext, psz_arguments );
369         free( psz_name );
370         free( psz_text );
371         free( psz_longtext );
372         free( psz_exclusive );
373         asprintf( &psz_exclusive, "--no%s --%s", p_item->psz_name,
374                  p_item->psz_name );
375         asprintf( &psz_option, "no-%s", p_item->psz_name );
376         psz_text = DUP( p_item->psz_text );
377         psz_longtext = DUP( p_item->psz_longtext );
378         PrintOption( psz_option, p_item->i_short, psz_exclusive,
379                      psz_text, psz_longtext, psz_arguments );
380         free( psz_text );
381         free( psz_longtext );
382         free( psz_exclusive );
383         free( psz_option );
384         asprintf( &psz_exclusive, "--no-%s --%s", p_item->psz_name,
385                  p_item->psz_name );
386         asprintf( &psz_option, "no%s", p_item->psz_name );
387         psz_text = DUP( p_item->psz_text );
388         psz_longtext = DUP( p_item->psz_longtext );
389         PrintOption( psz_option, p_item->i_short, psz_exclusive,
390                      psz_text, psz_longtext, psz_arguments );
391         free( psz_text );
392         free( psz_longtext );
393         free( psz_exclusive );
394         free( psz_option );
395         return;
396
397     case CONFIG_SECTION:
398 //        p_control = new SectionConfigControl( p_this, p_item, parent );
399         break;
400
401     default:
402         break;
403     }
404     psz_name = DUP( p_item->psz_name );
405     psz_text = DUP( p_item->psz_text );
406     psz_longtext = DUP( p_item->psz_longtext );
407     PrintOption( psz_name, p_item->i_short, NULL,
408                  psz_text, psz_longtext, psz_arguments );
409     free( psz_name );
410     free( psz_text );
411     free( psz_longtext );
412     free( psz_arguments );
413 }
414
415 void PrintOption( char *psz_option, char i_short, char *psz_exclusive,
416                    char *psz_text, char *psz_longtext, char *psz_args )
417 {
418     char *foo;
419     if( psz_text )
420     {
421         while( (foo = strchr( psz_text, ':' ))) *foo=';';
422         while( (foo = strchr( psz_text, '"' ))) *foo='\'';
423     }
424     if( psz_longtext )
425     {
426         while( (foo = strchr( psz_longtext, ':' ))) *foo=';';
427         while( (foo = strchr( psz_longtext, '"' ))) *foo='\'';
428     }
429     if( !psz_longtext ||
430         strchr( psz_longtext, '\n' ) ||
431         strchr( psz_longtext, '(' ) ) psz_longtext = psz_text;
432     if( i_short )
433     {
434         if( !psz_exclusive )
435             printf( "  \"(-%c)--%s%s[%s]", i_short,
436                     psz_option, psz_args?"=":"", psz_text );
437         else
438             printf( "  \"(-%c%s)--%s%s[%s]", i_short, psz_exclusive,
439                     psz_option, psz_args?"=":"", psz_text );
440         if( psz_args )
441             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
442         else
443             printf( "\"\\\n" );
444
445         printf( "  \"(--%s%s)-%c[%s]", psz_option, psz_exclusive ? psz_exclusive : "",
446                 i_short, psz_text );
447         if( psz_args )
448             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
449         else
450             printf( "\"\\\n" );
451
452     }
453     else
454     {
455         if( psz_exclusive )
456             printf( "  \"(%s)--%s%s[%s]", psz_exclusive, psz_option,
457                     psz_args?"=":"", psz_text );
458         else
459             printf( "  \"--%s[%s]", psz_option, psz_text );
460
461         if( psz_args )
462             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
463         else
464             printf( "\"\\\n" );
465     }
466 }
467