]> git.sesse.net Git - vlc/blob - extras/analyser/zsh.cpp
svg module: fix memleak.
[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/vlc.h>
41
42 /* evil hack */
43 #undef __PLUGIN__
44 #undef __BUILTIN__
45 #include <../src/modules/modules.h>
46
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 );
52
53 int main( int i_argc, const char **ppsz_argv )
54 {
55     mumap mods;
56     mcmap mods2;
57     /* Create a libvlc structure */
58     int i_ret = VLC_Create();
59     libvlc_int_t *p_libvlc;
60
61     if( i_ret < 0 )
62     {
63         return i_ret;
64     }
65
66     /* Initialize libvlc */
67     i_ret = VLC_Init( 0, i_argc, ppsz_argv );
68     if( i_ret < 0 )
69     {
70         VLC_Destroy( 0 );
71         return i_ret;
72     }
73     p_libvlc = (libvlc_int_t*)vlc_object_get( i_ret );
74     printf("#compdef vlc\n\n"
75
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" );
80
81     PrintModuleList( p_libvlc, mods, mods2 );
82
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" );
99
100     printf( "case $state in\n" );
101     printf( "  mrl)\n" );
102     printf( "    _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0\n" );
103     printf( "  ;;\n" );
104     printf( "esac\n\n" );
105
106     printf( "return ret\n" );
107
108     /* Exit early since we did not release all the objects we used,
109      * but we don't care, our task is over */
110     return 0;
111
112     /* Finish the threads */
113     VLC_CleanUp( 0 );
114
115     /* Destroy the libvlc structure */
116     VLC_Destroy( 0 );
117
118     return i_ret;
119
120 }
121
122 void ParseModules( libvlc_int_t *p_libvlc, mumap &mods, mcmap &mods2 )
123 {
124     vlc_list_t      *p_list = NULL;;
125     module_t        *p_module;
126     module_config_t *p_item;
127     int              i_index;
128     int              i_items;
129
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++ )
134     {
135         p_module = (module_t *)p_list->p_values[i_index].p_object;
136
137         /* Exclude empty plugins (submodules don't have config options, they
138          * are stored in the parent module) */
139         if( p_module->b_submodule )
140               continue;
141 //            p_item = ((module_t *)p_module->p_parent)->p_config;
142         else
143             p_item = p_module->p_config;
144
145 //        printf( "  #%s\n", p_module->psz_longname );
146         if( !p_item ) continue;
147         i_items = 0;
148         do
149         {
150             if( p_item->i_type == CONFIG_CATEGORY )
151             {
152 //                printf( "  #Category %d\n", p_item->i_value );
153             }
154             else if( p_item->i_type == CONFIG_SUBCATEGORY )
155             {
156 //                printf( "  #Subcategory %d\n", p_item->i_value );
157             }
158             if( p_item->i_type & CONFIG_ITEM )
159                 ParseOption( p_item, mods, mods2 );
160         }
161         while( i_items++ < p_module->i_config_items && p_item++ );
162
163     }
164 }
165
166 void PrintModuleList( libvlc_int_t *p_libvlc, mumap &mods, mcmap &mods2 )
167 {
168     vlc_list_t      *p_list = NULL;;
169     module_t        *p_module;
170     int              i_index;
171     int              i_items;
172
173     /* List the plugins */
174     p_list = vlc_list_find( p_libvlc, VLC_OBJECT_MODULE, FIND_ANYWHERE );
175     if( !p_list ) return;
176
177     printf( "modules=\"" );
178     for( i_index = 0; i_index < p_list->i_count; i_index++ )
179     {
180         p_module = (module_t *)p_list->p_values[i_index].p_object;
181
182         /* Exclude empty plugins (submodules don't have config options, they
183          * are stored in the parent module) */
184
185         if( strcmp( p_module->psz_object_name, "main" ) )
186         {
187             mods.insert( mpair( p_module->psz_capability,
188                                 p_module->psz_object_name ) );
189             module_config_t *p_config = p_module->p_config;
190             i_items = 0;
191             if( p_config ) do
192             {
193                 /* Hack: required subcategory is stored in i_min */
194                 if( p_config->i_type == CONFIG_SUBCATEGORY )
195                 {
196                     mods2.insert( mcpair( p_config->value.i,
197                                           p_module->psz_object_name ) );
198                 }
199             } while( i_items++ < p_module->i_config_items && p_config++ );
200             if( p_module->b_submodule )
201                 continue;
202             printf( "%s ", p_module->psz_object_name );
203         }
204
205     }
206     printf( "\"\n\n" );
207     return;
208 }
209
210 void ParseOption( module_config_t *p_item, mumap &mods, mcmap &mods2 )
211 {
212     char *psz_arguments = strdup( "" );
213     char *psz_exclusive;
214     char *psz_option;
215     char *psz_name;
216     char *psz_text;
217     char *psz_longtext;
218
219 #define DUP( x ) strdup( x ? x : "" )
220
221     //Skip deprecated options
222     if( p_item->b_removed )
223         return;
224
225     switch( p_item->i_type )
226     {
227     case CONFIG_ITEM_MODULE:
228     {
229         std::pair<mumap::iterator, mumap::iterator> range = mods.equal_range( p_item->psz_type );
230         std::string list = (*range.first).second;
231         ++range.first;
232         while( range.first != range.second )
233         {
234             list = list.append( " " );
235             list = list.append( range.first->second );
236             ++range.first;
237         }
238         asprintf( &psz_arguments, "(%s)", list.c_str() );
239     }
240     break;
241     case CONFIG_ITEM_MODULE_CAT:
242     {
243         std::pair<mcmap::iterator, mcmap::iterator> range =
244             mods2.equal_range( p_item->min.i );
245         std::string list = (*range.first).second;
246         ++range.first;
247         while( range.first != range.second )
248         {
249             list = list.append( " " );
250             list = list.append( range.first->second );
251             ++range.first;
252         }
253         asprintf( &psz_arguments, "(%s)", list.c_str() );
254     }
255     break;
256     case CONFIG_ITEM_MODULE_LIST_CAT:
257     {
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 )
263         {
264             list = list.append( " '*" );
265             list = list.append( range.first->second );
266             list = list.append( "'" );
267             ++range.first;
268         }
269         asprintf( &psz_arguments, "%s", list.c_str() );
270     }
271     break;
272
273     case CONFIG_ITEM_STRING:
274         if( p_item->i_list )
275         {
276             int i = p_item->i_list -1;
277             char *psz_list;
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] );
281             else
282                 psz_list = strdup(p_item->ppsz_list[i]);
283             char *psz_list2;
284             while( i>1 )
285             {
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 );
289                 else
290                     asprintf( &psz_list2, "%s %s", p_item->ppsz_list[i-1],
291                               psz_list );
292
293                 free( psz_list );
294                 psz_list = psz_list2;
295                 i--;
296             }
297             if( p_item->ppsz_list_text )
298                 asprintf( &psz_arguments, "((%s))", psz_list );
299             else
300                 asprintf( &psz_arguments, "(%s)", psz_list );
301
302             free( psz_list );
303         }
304         break;
305
306     case CONFIG_ITEM_FILE:
307         psz_arguments = strdup( "_files" );
308         break;
309     case CONFIG_ITEM_DIRECTORY:
310         psz_arguments = strdup( "_files -/" );
311         break;
312
313     case CONFIG_ITEM_INTEGER:
314         if( p_item->i_list )
315         {
316             int i = p_item->i_list -1;
317             char *psz_list;
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] );
321             else
322                 psz_list = strdup(p_item->ppsz_list[i]);
323             char *psz_list2;
324             while( i>1 )
325             {
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 );
329                 else
330                     asprintf( &psz_list2, "%s %s", p_item->ppsz_list[i-1],
331                               psz_list );
332
333                 free( psz_list );
334                 psz_list = psz_list2;
335                 i--;
336             }
337             if( p_item->ppsz_list_text )
338                 asprintf( &psz_arguments, "((%s))", psz_list );
339             else
340                 asprintf( &psz_arguments, "(%s)", psz_list );
341
342             free( psz_list );
343         }
344         else if( p_item->min.i != 0 || p_item->max.i != 0 )
345         {
346 //            p_control = new RangedIntConfigControl( p_this, p_item, parent );
347         }
348         else
349         {
350 //            p_control = new IntegerConfigControl( p_this, p_item, parent );
351         }
352         break;
353
354     case CONFIG_ITEM_KEY:
355 //        p_control = new KeyConfigControl( p_this, p_item, parent );
356         break;
357
358     case CONFIG_ITEM_FLOAT:
359 //        p_control = new FloatConfigControl( p_this, p_item, parent );
360         break;
361
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,
366                  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 );
372         free( psz_name );
373         free( psz_text );
374         free( psz_longtext );
375         free( psz_exclusive );
376         asprintf( &psz_exclusive, "--no%s --%s", p_item->psz_name,
377                  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 );
383         free( psz_text );
384         free( psz_longtext );
385         free( psz_exclusive );
386         free( psz_option );
387         asprintf( &psz_exclusive, "--no-%s --%s", p_item->psz_name,
388                  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 );
394         free( psz_text );
395         free( psz_longtext );
396         free( psz_exclusive );
397         free( psz_option );
398         return;
399
400     case CONFIG_SECTION:
401 //        p_control = new SectionConfigControl( p_this, p_item, parent );
402         break;
403
404     default:
405         break;
406     }
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 );
412     free( psz_name );
413     free( psz_text );
414     free( psz_longtext );
415 }
416
417 void PrintOption( char *psz_option, char i_short, char *psz_exclusive,
418                    char *psz_text, char *psz_longtext, char *psz_args )
419 {
420     char *foo;
421     if( psz_text )
422     {
423         while( (foo = strchr( psz_text, ':' ))) *foo=';';
424         while( (foo = strchr( psz_text, '"' ))) *foo='\'';
425     }
426     if( psz_longtext )
427     {
428         while( (foo = strchr( psz_longtext, ':' ))) *foo=';';
429         while( (foo = strchr( psz_longtext, '"' ))) *foo='\'';
430     }
431     if( !psz_longtext ||
432         strchr( psz_longtext, '\n' ) ||
433         strchr( psz_longtext, '(' ) ) psz_longtext = psz_text;
434     if( i_short )
435     {
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 );
440         if( psz_args )
441             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
442         else
443             printf( "\"\\\n" );
444         printf( "  \"(--%s%s)-%c[%s]", psz_option, psz_exclusive,
445                 i_short, psz_text );
446         if( psz_args )
447             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
448         else
449             printf( "\"\\\n" );
450
451     }
452     else
453     {
454         if( psz_exclusive )
455             printf( "  \"(%s)--%s%s[%s]", psz_exclusive, psz_option,
456                     psz_args?"=":"", psz_text );
457         else
458             printf( "  \"--%s[%s]", psz_option, psz_text );
459
460         if( psz_args )
461             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
462         else
463             printf( "\"\\\n" );
464
465     }
466 }
467