]> git.sesse.net Git - vlc/blob - extras/analyser/zsh.cpp
4f432fac5a14b7ec66f4c4e15ac1d39aa3001d2d
[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 * const argv[] = { "vlc" };
64     libvlc_instance_t *p_libvlc_instance = libvlc_new(1, argv, &ex);
65
66     if( !p_libvlc_instance || libvlc_exception_raised(&ex) )
67     {
68         libvlc_exception_clear(&ex);
69         return 1;
70     }
71
72     printf("#compdef vlc\n\n"
73
74            "#This file is autogenerated by zsh.cpp\n"
75            "typeset -A opt_args\n"
76            "local context state line ret=1\n"
77            "local modules\n\n" );
78
79     PrintModuleList( mods, mods2 );
80
81     printf( "_arguments -S -s \\\n" );
82     ParseModules( mods, mods2 );
83     printf( "  \"(--module)-p[print help on module]:print help on module:($modules)\"\\\n" );
84     printf( "  \"(-p)--module[print help on module]:print help on module:($modules)\"\\\n" );
85     printf( "  \"(--help)-h[print help]\"\\\n" );
86     printf( "  \"(-h)--help[print help]\"\\\n" );
87     printf( "  \"(--longhelp)-H[print detailed help]\"\\\n" );
88     printf( "  \"(-H)--longhelp[print detailed help]\"\\\n" );
89     printf( "  \"(--list)-l[print a list of available modules]\"\\\n" );
90     printf( "  \"(-l)--list[print a list of available modules]\"\\\n" );
91     printf( "  \"--save-config[save the current command line options in the config file]\"\\\n" );
92     printf( "  \"--reset-config[reset the current config to the default values]\"\\\n" );
93     printf( "  \"--config[use alternate config file]\"\\\n" );
94     printf( "  \"--reset-plugins-cache[resets the current plugins cache]\"\\\n" );
95     printf( "  \"--version[print version information]\"\\\n" );
96     printf( "  \"*:Playlist item:->mrl\" && ret=0\n\n" );
97
98     printf( "case $state in\n" );
99     printf( "  mrl)\n" );
100     printf( "    _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0\n" );
101     printf( "  ;;\n" );
102     printf( "esac\n\n" );
103
104     printf( "return ret\n" );
105
106     libvlc_release( p_libvlc_instance );
107
108     return 0;
109 }
110
111 void ParseModules( mumap &mods, mcmap &mods2 )
112 {
113     module_t       **p_list;
114     module_t        *p_module;
115     module_config_t *p_item;
116     int              i_index;
117     int              i_items;
118     size_t           i_modules;
119
120     /* List the plugins */
121     p_list = module_list_get(&i_modules);
122     if( !p_list ) return;
123     for( i_index = 0; i_index < i_modules; i_index++ )
124     {
125         p_module = p_list[i_index];
126
127         /* Exclude empty plugins (submodules don't have config options, they
128          * are stored in the parent module) */
129         if( p_module->b_submodule )
130               continue;
131 //            p_item = ((module_t *)p_module->p_parent)->p_config;
132         else
133             p_item = p_module->p_config;
134
135 //        printf( "  #%s\n", p_module->psz_longname );
136         if( !p_item ) continue;
137         i_items = 0;
138         do
139         {
140             if( p_item->i_type == CONFIG_CATEGORY )
141             {
142 //                printf( "  #Category %d\n", p_item->i_value );
143             }
144             else if( p_item->i_type == CONFIG_SUBCATEGORY )
145             {
146 //                printf( "  #Subcategory %d\n", p_item->i_value );
147             }
148             if( p_item->i_type & CONFIG_ITEM )
149                 ParseOption( p_item, mods, mods2 );
150         }
151         while( i_items++ < p_module->i_config_items && p_item++ );
152
153     }
154 }
155
156 void PrintModuleList( mumap &mods, mcmap &mods2 )
157 {
158     module_t       **p_list = NULL;
159     module_t        *p_module;
160     int              i_index;
161     int              i_items;
162     size_t           i_modules;
163
164     /* List the plugins */
165     p_list = module_list_get(&i_modules);
166     if( !p_list ) return;
167
168     printf( "modules=\"" );
169     for( i_index = 0; i_index < i_modules; i_index++ )
170     {
171         p_module = p_list[i_index];
172
173         /* Exclude empty plugins (submodules don't have config options, they
174          * are stored in the parent module) */
175
176         if( strcmp( p_module->psz_object_name, "main" ) )
177         {
178             mods.insert( mpair( p_module->psz_capability,
179                                 p_module->psz_object_name ) );
180             module_config_t *p_config = p_module->p_config;
181             i_items = 0;
182             if( p_config ) do
183             {
184                 /* Hack: required subcategory is stored in i_min */
185                 if( p_config->i_type == CONFIG_SUBCATEGORY )
186                 {
187                     mods2.insert( mcpair( p_config->value.i,
188                                           p_module->psz_object_name ) );
189                 }
190             } while( i_items++ < p_module->i_config_items && p_config++ );
191             if( p_module->b_submodule )
192                 continue;
193             printf( "%s ", p_module->psz_object_name );
194         }
195
196     }
197     printf( "\"\n\n" );
198     return;
199 }
200
201 void ParseOption( module_config_t *p_item, mumap &mods, mcmap &mods2 )
202 {
203     char *psz_arguments = NULL;
204     char *psz_exclusive;
205     char *psz_option;
206     char *psz_name;
207     char *psz_text;
208     char *psz_longtext;
209
210 #define DUP( x ) strdup( x ? x : "" )
211
212     //Skip deprecated options
213     if( p_item->b_removed )
214         return;
215
216     switch( p_item->i_type )
217     {
218     case CONFIG_ITEM_MODULE:
219     {
220         std::pair<mumap::iterator, mumap::iterator> range = mods.equal_range( p_item->psz_type );
221         std::string list = (*range.first).second;
222         ++range.first;
223         while( range.first != range.second )
224         {
225             list = list.append( " " );
226             list = list.append( range.first->second );
227             ++range.first;
228         }
229         asprintf( &psz_arguments, "(%s)", list.c_str() );
230     }
231     break;
232     case CONFIG_ITEM_MODULE_CAT:
233     {
234         std::pair<mcmap::iterator, mcmap::iterator> range =
235             mods2.equal_range( p_item->min.i );
236         std::string list = (*range.first).second;
237         ++range.first;
238         while( range.first != range.second )
239         {
240             list = list.append( " " );
241             list = list.append( range.first->second );
242             ++range.first;
243         }
244         asprintf( &psz_arguments, "(%s)", list.c_str() );
245     }
246     break;
247     case CONFIG_ITEM_MODULE_LIST_CAT:
248     {
249         std::pair<mcmap::iterator, mcmap::iterator> range =
250             mods2.equal_range( p_item->min.i );
251         std::string list = "_values -s , ";
252         list = list.append( p_item->psz_name );
253         while( range.first != range.second )
254         {
255             list = list.append( " '*" );
256             list = list.append( range.first->second );
257             list = list.append( "'" );
258             ++range.first;
259         }
260         asprintf( &psz_arguments, "%s", list.c_str() );
261     }
262     break;
263
264     case CONFIG_ITEM_STRING:
265         if( p_item->i_list )
266         {
267             int i = p_item->i_list -1;
268             char *psz_list;
269             if( p_item->ppsz_list_text )
270                 asprintf( &psz_list, "%s\\:%s", p_item->ppsz_list[i],
271                           p_item->ppsz_list_text[i] );
272             else
273                 psz_list = strdup(p_item->ppsz_list[i]);
274             char *psz_list2;
275             while( i>1 )
276             {
277                 if( p_item->ppsz_list_text )
278                     asprintf( &psz_list2, "%s\\:%s %s", p_item->ppsz_list[i-1],
279                               p_item->ppsz_list_text[i-1], psz_list );
280                 else
281                     asprintf( &psz_list2, "%s %s", p_item->ppsz_list[i-1],
282                               psz_list );
283
284                 free( psz_list );
285                 psz_list = psz_list2;
286                 i--;
287             }
288             if( p_item->ppsz_list_text )
289                 asprintf( &psz_arguments, "((%s))", psz_list );
290             else
291                 asprintf( &psz_arguments, "(%s)", psz_list );
292
293             free( psz_list );
294         }
295         break;
296
297     case CONFIG_ITEM_FILE:
298         psz_arguments = strdup( "_files" );
299         break;
300     case CONFIG_ITEM_DIRECTORY:
301         psz_arguments = strdup( "_files -/" );
302         break;
303
304     case CONFIG_ITEM_INTEGER:
305         if( p_item->i_list )
306         {
307             int i = p_item->i_list -1;
308             char *psz_list;
309             if( p_item->ppsz_list_text )
310                 asprintf( &psz_list, "%d\\:%s", p_item->pi_list[i],
311                           p_item->ppsz_list_text[i] );
312             else
313                 psz_list = strdup(p_item->ppsz_list[i]);
314             char *psz_list2;
315             while( i>1 )
316             {
317                 if( p_item->ppsz_list_text )
318                     asprintf( &psz_list2, "%d\\:%s %s", p_item->pi_list[i-1],
319                               p_item->ppsz_list_text[i-1], psz_list );
320                 else
321                     asprintf( &psz_list2, "%s %s", p_item->ppsz_list[i-1],
322                               psz_list );
323
324                 free( psz_list );
325                 psz_list = psz_list2;
326                 i--;
327             }
328             if( p_item->ppsz_list_text )
329                 asprintf( &psz_arguments, "((%s))", psz_list );
330             else
331                 asprintf( &psz_arguments, "(%s)", psz_list );
332
333             free( psz_list );
334         }
335         else if( p_item->min.i != 0 || p_item->max.i != 0 )
336         {
337 //            p_control = new RangedIntConfigControl( p_this, p_item, parent );
338         }
339         else
340         {
341 //            p_control = new IntegerConfigControl( p_this, p_item, parent );
342         }
343         break;
344
345     case CONFIG_ITEM_KEY:
346 //        p_control = new KeyConfigControl( p_this, p_item, parent );
347         break;
348
349     case CONFIG_ITEM_FLOAT:
350 //        p_control = new FloatConfigControl( p_this, p_item, parent );
351         break;
352
353     case CONFIG_ITEM_BOOL:
354 //        p_control = new BoolConfigControl( p_this, p_item, parent );
355         asprintf( &psz_exclusive, "--no%s --no-%s", p_item->psz_name,
356                  p_item->psz_name );
357         psz_name = DUP( p_item->psz_name );
358         psz_text = DUP( p_item->psz_text );
359         psz_longtext = DUP( p_item->psz_longtext );
360         PrintOption( psz_name, p_item->i_short, psz_exclusive,
361                      psz_text, psz_longtext, psz_arguments );
362         free( psz_name );
363         free( psz_text );
364         free( psz_longtext );
365         free( psz_exclusive );
366         asprintf( &psz_exclusive, "--no%s --%s", p_item->psz_name,
367                  p_item->psz_name );
368         asprintf( &psz_option, "no-%s", p_item->psz_name );
369         psz_text = DUP( p_item->psz_text );
370         psz_longtext = DUP( p_item->psz_longtext );
371         PrintOption( psz_option, p_item->i_short, psz_exclusive,
372                      psz_text, psz_longtext, psz_arguments );
373         free( psz_text );
374         free( psz_longtext );
375         free( psz_exclusive );
376         free( psz_option );
377         asprintf( &psz_exclusive, "--no-%s --%s", p_item->psz_name,
378                  p_item->psz_name );
379         asprintf( &psz_option, "no%s", p_item->psz_name );
380         psz_text = DUP( p_item->psz_text );
381         psz_longtext = DUP( p_item->psz_longtext );
382         PrintOption( psz_option, p_item->i_short, psz_exclusive,
383                      psz_text, psz_longtext, psz_arguments );
384         free( psz_text );
385         free( psz_longtext );
386         free( psz_exclusive );
387         free( psz_option );
388         return;
389
390     case CONFIG_SECTION:
391 //        p_control = new SectionConfigControl( p_this, p_item, parent );
392         break;
393
394     default:
395         break;
396     }
397     psz_name = DUP( p_item->psz_name );
398     psz_text = DUP( p_item->psz_text );
399     psz_longtext = DUP( p_item->psz_longtext );
400     PrintOption( psz_name, p_item->i_short, NULL,
401                  psz_text, psz_longtext, psz_arguments );
402     free( psz_name );
403     free( psz_text );
404     free( psz_longtext );
405     free( psz_arguments );
406 }
407
408 void PrintOption( char *psz_option, char i_short, char *psz_exclusive,
409                    char *psz_text, char *psz_longtext, char *psz_args )
410 {
411     char *foo;
412     if( psz_text )
413     {
414         while( (foo = strchr( psz_text, ':' ))) *foo=';';
415         while( (foo = strchr( psz_text, '"' ))) *foo='\'';
416     }
417     if( psz_longtext )
418     {
419         while( (foo = strchr( psz_longtext, ':' ))) *foo=';';
420         while( (foo = strchr( psz_longtext, '"' ))) *foo='\'';
421     }
422     if( !psz_longtext ||
423         strchr( psz_longtext, '\n' ) ||
424         strchr( psz_longtext, '(' ) ) psz_longtext = psz_text;
425     if( i_short )
426     {
427         if( !psz_exclusive ) psz_exclusive = strdup( "" );
428         else asprintf( &psz_exclusive, " %s", psz_exclusive );
429         printf( "  \"(-%c%s)--%s%s[%s]", i_short, psz_exclusive,
430                 psz_option, psz_args?"=":"", psz_text );
431         if( psz_args )
432             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
433         else
434             printf( "\"\\\n" );
435         printf( "  \"(--%s%s)-%c[%s]", psz_option, psz_exclusive,
436                 i_short, psz_text );
437         if( psz_args )
438             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
439         else
440             printf( "\"\\\n" );
441
442     }
443     else
444     {
445         if( psz_exclusive )
446             printf( "  \"(%s)--%s%s[%s]", psz_exclusive, psz_option,
447                     psz_args?"=":"", psz_text );
448         else
449             printf( "  \"--%s[%s]", psz_option, psz_text );
450
451         if( psz_args )
452             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
453         else
454             printf( "\"\\\n" );
455
456     }
457 }
458