]> git.sesse.net Git - vlc/blob - extras/analyser/zsh.cpp
zsh: fix a memory leak (this is usefull as this program seems to fail on OOM).
[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\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 }
158
159 void PrintModuleList( mumap &mods, mcmap &mods2 )
160 {
161     module_t       **p_list = NULL;
162     module_t        *p_module;
163     int              i_index;
164     int              i_items;
165     size_t           i_modules;
166
167     /* List the plugins */
168     p_list = module_list_get(&i_modules);
169     if( !p_list ) return;
170
171     printf( "modules=\"" );
172     for( i_index = 0; i_index < i_modules; i_index++ )
173     {
174         p_module = p_list[i_index];
175
176         /* Exclude empty plugins (submodules don't have config options, they
177          * are stored in the parent module) */
178
179         if( strcmp( p_module->psz_object_name, "main" ) )
180         {
181             mods.insert( mpair( p_module->psz_capability,
182                                 p_module->psz_object_name ) );
183             module_config_t *p_config = p_module->p_config;
184             i_items = 0;
185             if( p_config ) do
186             {
187                 /* Hack: required subcategory is stored in i_min */
188                 if( p_config->i_type == CONFIG_SUBCATEGORY )
189                 {
190                     mods2.insert( mcpair( p_config->value.i,
191                                           p_module->psz_object_name ) );
192                 }
193             } while( i_items++ < p_module->i_config_items && p_config++ );
194             if( p_module->b_submodule )
195                 continue;
196             printf( "%s ", p_module->psz_object_name );
197         }
198
199     }
200     printf( "\"\n\n" );
201     module_list_free( p_list );
202     return;
203 }
204
205 void ParseOption( module_config_t *p_item, mumap &mods, mcmap &mods2 )
206 {
207     char *psz_arguments = NULL;
208     char *psz_exclusive;
209     char *psz_option;
210     char *psz_name;
211     char *psz_text;
212     char *psz_longtext;
213
214 #define DUP( x ) strdup( x ? x : "" )
215
216     //Skip deprecated options
217     if( p_item->b_removed )
218         return;
219
220     switch( p_item->i_type )
221     {
222     case CONFIG_ITEM_MODULE:
223     {
224         std::pair<mumap::iterator, mumap::iterator> range = mods.equal_range( p_item->psz_type );
225         std::string list = (*range.first).second;
226         ++range.first;
227         while( range.first != range.second )
228         {
229             list = list.append( " " );
230             list = list.append( range.first->second );
231             ++range.first;
232         }
233         asprintf( &psz_arguments, "(%s)", list.c_str() );
234     }
235     break;
236     case CONFIG_ITEM_MODULE_CAT:
237     {
238         std::pair<mcmap::iterator, mcmap::iterator> range =
239             mods2.equal_range( p_item->min.i );
240         std::string list = (*range.first).second;
241         ++range.first;
242         while( range.first != range.second )
243         {
244             list = list.append( " " );
245             list = list.append( range.first->second );
246             ++range.first;
247         }
248         asprintf( &psz_arguments, "(%s)", list.c_str() );
249     }
250     break;
251     case CONFIG_ITEM_MODULE_LIST_CAT:
252     {
253         std::pair<mcmap::iterator, mcmap::iterator> range =
254             mods2.equal_range( p_item->min.i );
255         std::string list = "_values -s , ";
256         list = list.append( p_item->psz_name );
257         while( range.first != range.second )
258         {
259             list = list.append( " '*" );
260             list = list.append( range.first->second );
261             list = list.append( "'" );
262             ++range.first;
263         }
264         psz_arguments = strdup( list.c_str() );
265     }
266     break;
267
268     case CONFIG_ITEM_STRING:
269         if( p_item->i_list )
270         {
271             int i = p_item->i_list -1;
272             char *psz_list;
273             if( p_item->ppsz_list_text )
274                 asprintf( &psz_list, "%s\\:%s", p_item->ppsz_list[i],
275                           p_item->ppsz_list_text[i] );
276             else
277                 psz_list = strdup(p_item->ppsz_list[i]);
278             char *psz_list2;
279             while( i>1 )
280             {
281                 if( p_item->ppsz_list_text )
282                     asprintf( &psz_list2, "%s\\:%s %s", p_item->ppsz_list[i-1],
283                               p_item->ppsz_list_text[i-1], psz_list );
284                 else
285                     asprintf( &psz_list2, "%s %s", p_item->ppsz_list[i-1],
286                               psz_list );
287
288                 free( psz_list );
289                 psz_list = psz_list2;
290                 i--;
291             }
292             if( p_item->ppsz_list_text )
293                 asprintf( &psz_arguments, "((%s))", psz_list );
294             else
295                 asprintf( &psz_arguments, "(%s)", psz_list );
296
297             free( psz_list );
298         }
299         break;
300
301     case CONFIG_ITEM_FILE:
302         psz_arguments = strdup( "_files" );
303         break;
304     case CONFIG_ITEM_DIRECTORY:
305         psz_arguments = strdup( "_files -/" );
306         break;
307
308     case CONFIG_ITEM_INTEGER:
309         if( p_item->i_list )
310         {
311             int i = p_item->i_list -1;
312             char *psz_list;
313             if( p_item->ppsz_list_text )
314                 asprintf( &psz_list, "%d\\:%s", p_item->pi_list[i],
315                           p_item->ppsz_list_text[i] );
316             else
317                 psz_list = strdup(p_item->ppsz_list[i]);
318             char *psz_list2;
319             while( i>1 )
320             {
321                 if( p_item->ppsz_list_text )
322                     asprintf( &psz_list2, "%d\\:%s %s", p_item->pi_list[i-1],
323                               p_item->ppsz_list_text[i-1], psz_list );
324                 else
325                     asprintf( &psz_list2, "%s %s", p_item->ppsz_list[i-1],
326                               psz_list );
327
328                 free( psz_list );
329                 psz_list = psz_list2;
330                 i--;
331             }
332             if( p_item->ppsz_list_text )
333                 asprintf( &psz_arguments, "((%s))", psz_list );
334             else
335                 asprintf( &psz_arguments, "(%s)", psz_list );
336
337             free( psz_list );
338         }
339         else if( p_item->min.i != 0 || p_item->max.i != 0 )
340         {
341 //            p_control = new RangedIntConfigControl( p_this, p_item, parent );
342         }
343         else
344         {
345 //            p_control = new IntegerConfigControl( p_this, p_item, parent );
346         }
347         break;
348
349     case CONFIG_ITEM_KEY:
350 //        p_control = new KeyConfigControl( p_this, p_item, parent );
351         break;
352
353     case CONFIG_ITEM_FLOAT:
354 //        p_control = new FloatConfigControl( p_this, p_item, parent );
355         break;
356
357     case CONFIG_ITEM_BOOL:
358 //        p_control = new BoolConfigControl( p_this, p_item, parent );
359         asprintf( &psz_exclusive, "--no%s --no-%s", p_item->psz_name,
360                  p_item->psz_name );
361         psz_name = DUP( p_item->psz_name );
362         psz_text = DUP( p_item->psz_text );
363         psz_longtext = DUP( p_item->psz_longtext );
364         PrintOption( psz_name, p_item->i_short, psz_exclusive,
365                      psz_text, psz_longtext, psz_arguments );
366         free( psz_name );
367         free( psz_text );
368         free( psz_longtext );
369         free( psz_exclusive );
370         asprintf( &psz_exclusive, "--no%s --%s", p_item->psz_name,
371                  p_item->psz_name );
372         asprintf( &psz_option, "no-%s", p_item->psz_name );
373         psz_text = DUP( p_item->psz_text );
374         psz_longtext = DUP( p_item->psz_longtext );
375         PrintOption( psz_option, p_item->i_short, psz_exclusive,
376                      psz_text, psz_longtext, psz_arguments );
377         free( psz_text );
378         free( psz_longtext );
379         free( psz_exclusive );
380         free( psz_option );
381         asprintf( &psz_exclusive, "--no-%s --%s", p_item->psz_name,
382                  p_item->psz_name );
383         asprintf( &psz_option, "no%s", p_item->psz_name );
384         psz_text = DUP( p_item->psz_text );
385         psz_longtext = DUP( p_item->psz_longtext );
386         PrintOption( psz_option, p_item->i_short, psz_exclusive,
387                      psz_text, psz_longtext, psz_arguments );
388         free( psz_text );
389         free( psz_longtext );
390         free( psz_exclusive );
391         free( psz_option );
392         return;
393
394     case CONFIG_SECTION:
395 //        p_control = new SectionConfigControl( p_this, p_item, parent );
396         break;
397
398     default:
399         break;
400     }
401     psz_name = DUP( p_item->psz_name );
402     psz_text = DUP( p_item->psz_text );
403     psz_longtext = DUP( p_item->psz_longtext );
404     PrintOption( psz_name, p_item->i_short, NULL,
405                  psz_text, psz_longtext, psz_arguments );
406     free( psz_name );
407     free( psz_text );
408     free( psz_longtext );
409     free( psz_arguments );
410 }
411
412 void PrintOption( char *psz_option, char i_short, char *psz_exclusive,
413                    char *psz_text, char *psz_longtext, char *psz_args )
414 {
415     char *foo;
416     if( psz_text )
417     {
418         while( (foo = strchr( psz_text, ':' ))) *foo=';';
419         while( (foo = strchr( psz_text, '"' ))) *foo='\'';
420     }
421     if( psz_longtext )
422     {
423         while( (foo = strchr( psz_longtext, ':' ))) *foo=';';
424         while( (foo = strchr( psz_longtext, '"' ))) *foo='\'';
425     }
426     if( !psz_longtext ||
427         strchr( psz_longtext, '\n' ) ||
428         strchr( psz_longtext, '(' ) ) psz_longtext = psz_text;
429     if( i_short )
430     {
431         if( !psz_exclusive ) psz_exclusive = strdup( "" );
432         else asprintf( &psz_exclusive, " %s", psz_exclusive );
433         printf( "  \"(-%c%s)--%s%s[%s]", i_short, psz_exclusive,
434                 psz_option, psz_args?"=":"", psz_text );
435         if( psz_args )
436             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
437         else
438             printf( "\"\\\n" );
439         printf( "  \"(--%s%s)-%c[%s]", psz_option, psz_exclusive,
440                 i_short, psz_text );
441         if( psz_args )
442             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
443         else
444             printf( "\"\\\n" );
445
446     }
447     else
448     {
449         if( psz_exclusive )
450             printf( "  \"(%s)--%s%s[%s]", psz_exclusive, psz_option,
451                     psz_args?"=":"", psz_text );
452         else
453             printf( "  \"--%s[%s]", psz_option, psz_text );
454
455         if( psz_args )
456             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
457         else
458             printf( "\"\\\n" );
459
460     }
461 }
462