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