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