]> git.sesse.net Git - vlc/blob - extras/analyser/zsh.cpp
macosx/framework: libvlc_track_description_release.
[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( "  \"--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     libvlc_release( p_libvlc_instance );
109
110     return 0;
111 }
112
113 void ParseModules( mumap &mods, mcmap &mods2 )
114 {
115     module_t       **p_list;
116     module_t        *p_module;
117     module_config_t *p_item;
118     int              i_index;
119     int              i_items;
120     size_t           i_modules;
121
122     /* List the plugins */
123     p_list = module_list_get(&i_modules);
124     if( !p_list ) return;
125     for( i_index = 0; i_index < i_modules; i_index++ )
126     {
127         p_module = p_list[i_index];
128
129         /* Exclude empty plugins (submodules don't have config options, they
130          * are stored in the parent module) */
131         if( p_module->b_submodule )
132               continue;
133 //            p_item = ((module_t *)p_module->p_parent)->p_config;
134         else
135             p_item = p_module->p_config;
136
137 //        printf( "  #%s\n", p_module->psz_longname );
138         if( !p_item ) continue;
139         i_items = 0;
140         do
141         {
142             if( p_item->i_type == CONFIG_CATEGORY )
143             {
144 //                printf( "  #Category %d\n", p_item->i_value );
145             }
146             else if( p_item->i_type == CONFIG_SUBCATEGORY )
147             {
148 //                printf( "  #Subcategory %d\n", p_item->i_value );
149             }
150             if( p_item->i_type & CONFIG_ITEM )
151                 ParseOption( p_item, mods, mods2 );
152         }
153         while( i_items++ < p_module->i_config_items && p_item++ );
154
155     }
156     module_list_free( p_list );
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         if( range.first != range.second )
227         {
228             while( range.first++ != range.second )
229             {
230                 list = list.append( " " );
231                 list = list.append( range.first->second );
232             }
233             asprintf( &psz_arguments, "(%s)", list.c_str() );
234         }
235     }
236     break;
237     case CONFIG_ITEM_MODULE_CAT:
238     {
239         std::pair<mcmap::iterator, mcmap::iterator> range =
240             mods2.equal_range( p_item->min.i );
241         std::string list = (*range.first).second;
242         if( range.first != range.second )
243         {
244             while( range.first++ != range.second )
245             {
246                 list = list.append( " " );
247                 list = list.append( range.first->second );
248             }
249             asprintf( &psz_arguments, "(%s)", list.c_str() );
250         }
251     }
252     break;
253     case CONFIG_ITEM_MODULE_LIST_CAT:
254     {
255         std::pair<mcmap::iterator, mcmap::iterator> range =
256             mods2.equal_range( p_item->min.i );
257         std::string list = "_values -s , ";
258         list = list.append( p_item->psz_name );
259         while( range.first != range.second )
260         {
261             list = list.append( " '*" );
262             list = list.append( range.first->second );
263             list = list.append( "'" );
264             ++range.first;
265         }
266         psz_arguments = strdup( list.c_str() );
267     }
268     break;
269
270     case CONFIG_ITEM_STRING:
271         if( p_item->i_list )
272         {
273             int i = p_item->i_list -1;
274             char *psz_list;
275             if( p_item->ppsz_list_text )
276                 asprintf( &psz_list, "%s\\:%s", p_item->ppsz_list[i],
277                           p_item->ppsz_list_text[i] );
278             else
279                 psz_list = strdup(p_item->ppsz_list[i]);
280             char *psz_list2;
281             while( i>1 )
282             {
283                 if( p_item->ppsz_list_text )
284                     asprintf( &psz_list2, "%s\\:%s %s", p_item->ppsz_list[i-1],
285                               p_item->ppsz_list_text[i-1], psz_list );
286                 else
287                     asprintf( &psz_list2, "%s %s", p_item->ppsz_list[i-1],
288                               psz_list );
289
290                 free( psz_list );
291                 psz_list = psz_list2;
292                 i--;
293             }
294             if( p_item->ppsz_list_text )
295                 asprintf( &psz_arguments, "((%s))", psz_list );
296             else
297                 asprintf( &psz_arguments, "(%s)", psz_list );
298
299             free( psz_list );
300         }
301         break;
302
303     case CONFIG_ITEM_FILE:
304         psz_arguments = strdup( "_files" );
305         break;
306     case CONFIG_ITEM_DIRECTORY:
307         psz_arguments = strdup( "_files -/" );
308         break;
309
310     case CONFIG_ITEM_INTEGER:
311         if( p_item->i_list )
312         {
313             int i = p_item->i_list -1;
314             char *psz_list;
315             if( p_item->ppsz_list_text )
316                 asprintf( &psz_list, "%d\\:%s", p_item->pi_list[i],
317                           p_item->ppsz_list_text[i] );
318             else
319                 psz_list = strdup(p_item->ppsz_list[i]);
320             char *psz_list2;
321             while( i>1 )
322             {
323                 if( p_item->ppsz_list_text )
324                     asprintf( &psz_list2, "%d\\:%s %s", p_item->pi_list[i-1],
325                               p_item->ppsz_list_text[i-1], psz_list );
326                 else
327                     asprintf( &psz_list2, "%s %s", p_item->ppsz_list[i-1],
328                               psz_list );
329
330                 free( psz_list );
331                 psz_list = psz_list2;
332                 i--;
333             }
334             if( p_item->ppsz_list_text )
335                 asprintf( &psz_arguments, "((%s))", psz_list );
336             else
337                 asprintf( &psz_arguments, "(%s)", psz_list );
338
339             free( psz_list );
340         }
341         else if( p_item->min.i != 0 || p_item->max.i != 0 )
342         {
343 //            p_control = new RangedIntConfigControl( p_this, p_item, parent );
344         }
345         else
346         {
347 //            p_control = new IntegerConfigControl( p_this, p_item, parent );
348         }
349         break;
350
351     case CONFIG_ITEM_KEY:
352 //        p_control = new KeyConfigControl( p_this, p_item, parent );
353         break;
354
355     case CONFIG_ITEM_FLOAT:
356 //        p_control = new FloatConfigControl( p_this, p_item, parent );
357         break;
358
359     case CONFIG_ITEM_BOOL:
360 //        p_control = new BoolConfigControl( p_this, p_item, parent );
361         asprintf( &psz_exclusive, "--no%s --no-%s", p_item->psz_name,
362                  p_item->psz_name );
363         psz_name = DUP( p_item->psz_name );
364         psz_text = DUP( p_item->psz_text );
365         psz_longtext = DUP( p_item->psz_longtext );
366         PrintOption( psz_name, p_item->i_short, psz_exclusive,
367                      psz_text, psz_longtext, psz_arguments );
368         free( psz_name );
369         free( psz_text );
370         free( psz_longtext );
371         free( psz_exclusive );
372         asprintf( &psz_exclusive, "--no%s --%s", p_item->psz_name,
373                  p_item->psz_name );
374         asprintf( &psz_option, "no-%s", p_item->psz_name );
375         psz_text = DUP( p_item->psz_text );
376         psz_longtext = DUP( p_item->psz_longtext );
377         PrintOption( psz_option, p_item->i_short, psz_exclusive,
378                      psz_text, psz_longtext, psz_arguments );
379         free( psz_text );
380         free( psz_longtext );
381         free( psz_exclusive );
382         free( psz_option );
383         asprintf( &psz_exclusive, "--no-%s --%s", p_item->psz_name,
384                  p_item->psz_name );
385         asprintf( &psz_option, "no%s", p_item->psz_name );
386         psz_text = DUP( p_item->psz_text );
387         psz_longtext = DUP( p_item->psz_longtext );
388         PrintOption( psz_option, p_item->i_short, psz_exclusive,
389                      psz_text, psz_longtext, psz_arguments );
390         free( psz_text );
391         free( psz_longtext );
392         free( psz_exclusive );
393         free( psz_option );
394         return;
395
396     case CONFIG_SECTION:
397 //        p_control = new SectionConfigControl( p_this, p_item, parent );
398         break;
399
400     default:
401         break;
402     }
403     psz_name = DUP( p_item->psz_name );
404     psz_text = DUP( p_item->psz_text );
405     psz_longtext = DUP( p_item->psz_longtext );
406     PrintOption( psz_name, p_item->i_short, NULL,
407                  psz_text, psz_longtext, psz_arguments );
408     free( psz_name );
409     free( psz_text );
410     free( psz_longtext );
411     free( psz_arguments );
412 }
413
414 void PrintOption( char *psz_option, char i_short, char *psz_exclusive,
415                    char *psz_text, char *psz_longtext, char *psz_args )
416 {
417     char *foo;
418     if( psz_text )
419     {
420         while( (foo = strchr( psz_text, ':' ))) *foo=';';
421         while( (foo = strchr( psz_text, '"' ))) *foo='\'';
422     }
423     if( psz_longtext )
424     {
425         while( (foo = strchr( psz_longtext, ':' ))) *foo=';';
426         while( (foo = strchr( psz_longtext, '"' ))) *foo='\'';
427     }
428     if( !psz_longtext ||
429         strchr( psz_longtext, '\n' ) ||
430         strchr( psz_longtext, '(' ) ) psz_longtext = psz_text;
431     if( i_short )
432     {
433         if( !psz_exclusive )
434             printf( "  \"(-%c)--%s%s[%s]", i_short,
435                     psz_option, psz_args?"=":"", psz_text );
436         else
437             printf( "  \"(-%c%s)--%s%s[%s]", i_short, psz_exclusive,
438                     psz_option, psz_args?"=":"", psz_text );
439         if( psz_args )
440             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
441         else
442             printf( "\"\\\n" );
443
444         printf( "  \"(--%s%s)-%c[%s]", psz_option, psz_exclusive ? 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