]> git.sesse.net Git - vlc/blob - extras/zsh.cpp
* src/misc/cpu.c: do not assume SYS_DARWIN means powerpc, so that we can
[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( 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
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     vlc_t *p_vlc;
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_vlc = vlc_current_object( 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_vlc, mods, mods2 );
73
74     printf( "_arguments -S -s \\\n" );
75     ParseModules( p_vlc, 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( vlc_t *p_vlc, 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
117     /* List the plugins */
118     p_list = vlc_list_find( p_vlc, VLC_OBJECT_MODULE, FIND_ANYWHERE );
119     if( !p_list ) return;
120     for( i_index = 0; i_index < p_list->i_count; i_index++ )
121     {
122         p_module = (module_t *)p_list->p_values[i_index].p_object;
123
124         /* Exclude empty plugins (submodules don't have config options, they
125          * are stored in the parent module) */
126         if( p_module->b_submodule )
127               continue;
128 //            p_item = ((module_t *)p_module->p_parent)->p_config;
129         else
130             p_item = p_module->p_config;
131
132 //        printf( "  #%s\n", p_module->psz_longname );
133         if( !p_item ) continue;
134         do
135         {
136             if( p_item->i_type == CONFIG_CATEGORY )
137             {
138 //                printf( "  #Category %d\n", p_item->i_value );
139             }
140             else if( p_item->i_type == CONFIG_SUBCATEGORY )
141             {
142 //                printf( "  #Subcategory %d\n", p_item->i_value );
143             }
144             if( p_item->i_type & CONFIG_ITEM )
145                 ParseOption( p_item, mods, mods2 );
146         }
147         while( p_item->i_type != CONFIG_HINT_END && p_item++ );
148
149     }    
150 }
151
152 void PrintModuleList( vlc_t *p_vlc, mmap &mods, mcmap &mods2 )
153 {
154     vlc_list_t      *p_list = NULL;;
155     module_t        *p_module;
156     int              i_index;
157
158     /* List the plugins */
159     p_list = vlc_list_find( p_vlc, VLC_OBJECT_MODULE, FIND_ANYWHERE );
160     if( !p_list ) return;
161
162     printf( "modules=\"" );
163     for( i_index = 0; i_index < p_list->i_count; i_index++ )
164     {
165         p_module = (module_t *)p_list->p_values[i_index].p_object;
166
167         /* Exclude empty plugins (submodules don't have config options, they
168          * are stored in the parent module) */
169
170         if( strcmp( p_module->psz_object_name, "main" ) )
171         {
172             mods.insert( mpair( p_module->psz_capability,
173                                 p_module->psz_object_name ) );
174             module_config_t *p_config = p_module->p_config;
175             if( p_config ) do
176             {
177                 /* Hack: required subcategory is stored in i_min */
178                 if( p_config->i_type == CONFIG_SUBCATEGORY )
179                 {
180                     mods2.insert( mcpair( p_config->i_value,
181                                           p_module->psz_object_name ) );
182                 }
183             } while( p_config->i_type != CONFIG_HINT_END && p_config++ );
184             if( p_module->b_submodule )
185                 continue;
186             printf( "%s ", p_module->psz_object_name );
187         }
188
189     }
190     printf( "\"\n\n" );
191     return;
192 }
193
194 void ParseOption( module_config_t *p_item, mmap &mods, mcmap &mods2 )
195 {
196     char *psz_arguments = "";
197     char *psz_exclusive;
198     char *psz_option;
199     //Skip deprecated options
200     if( p_item->psz_current )
201         return;
202     
203     switch( p_item->i_type )
204     {
205     case CONFIG_ITEM_MODULE:
206     {
207         std::pair<mmap::iterator, mmap::iterator> range = mods.equal_range( p_item->psz_type );
208         std::string list = (*range.first).second;
209         ++range.first;
210         while( range.first != range.second )
211         {
212             list = list.append( " " );
213             printf("%s",range.first->second.c_str());
214             list = list.append( range.first->second );
215             ++range.first;
216         }
217         asprintf( &psz_arguments, "(%s)", list.c_str() );
218     }
219     break;
220     case CONFIG_ITEM_MODULE_CAT:
221     {
222         std::pair<mcmap::iterator, mcmap::iterator> range =
223             mods2.equal_range( p_item->i_min );
224         std::string list = (*range.first).second;
225         ++range.first;
226         while( range.first != range.second )
227         {
228             list = list.append( " " );
229             list = list.append( range.first->second );
230             ++range.first;
231         }
232         asprintf( &psz_arguments, "(%s)", list.c_str() );
233     }
234     break;
235     case CONFIG_ITEM_MODULE_LIST_CAT:
236     {
237         std::pair<mcmap::iterator, mcmap::iterator> range =
238             mods2.equal_range( p_item->i_min );
239         std::string list = "_values -s , ";
240         list = list.append( p_item->psz_name );
241         while( range.first != range.second )
242         {
243             list = list.append( " '*" );
244             list = list.append( range.first->second );
245             list = list.append( "'" );
246             ++range.first;
247         }
248         asprintf( &psz_arguments, "%s", list.c_str() );
249     }
250     break;
251
252     case CONFIG_ITEM_STRING:
253         if( p_item->i_list )
254         {
255             int i = p_item->i_list -1;
256             char *psz_list;
257             if( p_item->ppsz_list_text )
258                 asprintf( &psz_list, "%s\\:%s", p_item->ppsz_list[i],
259                           p_item->ppsz_list_text[i] );
260             else
261                 psz_list = strdup(p_item->ppsz_list[i]);
262             char *psz_list2;
263             while( i>1 )
264             {
265                 if( p_item->ppsz_list_text )
266                     asprintf( &psz_list2, "%s\\:%s %s", p_item->ppsz_list[i-1],
267                               p_item->ppsz_list_text[i-1], psz_list );
268                 else
269                     asprintf( &psz_list2, "%s %s", p_item->ppsz_list[i-1],
270                               psz_list );
271
272                 free( psz_list );
273                 psz_list = psz_list2;
274                 i--;
275             }
276             if( p_item->ppsz_list_text )
277                 asprintf( &psz_arguments, "((%s))", psz_list );
278             else
279                 asprintf( &psz_arguments, "(%s)", psz_list );
280                 
281             free( psz_list );
282         }
283         break;
284
285     case CONFIG_ITEM_FILE:
286         psz_arguments = "_files";
287         break;
288     case CONFIG_ITEM_DIRECTORY:
289         psz_arguments = "_files -/";
290         break;
291
292     case CONFIG_ITEM_INTEGER:
293         if( p_item->i_list )
294         {
295             int i = p_item->i_list -1;
296             char *psz_list;
297             if( p_item->ppsz_list_text )
298                 asprintf( &psz_list, "%d\\:%s", p_item->pi_list[i],
299                           p_item->ppsz_list_text[i] );
300             else
301                 psz_list = strdup(p_item->ppsz_list[i]);
302             char *psz_list2;
303             while( i>1 )
304             {
305                 if( p_item->ppsz_list_text )
306                     asprintf( &psz_list2, "%d\\:%s %s", p_item->pi_list[i-1],
307                               p_item->ppsz_list_text[i-1], psz_list );
308                 else
309                     asprintf( &psz_list2, "%s %s", p_item->ppsz_list[i-1],
310                               psz_list );
311
312                 free( psz_list );
313                 psz_list = psz_list2;
314                 i--;
315             }
316             if( p_item->ppsz_list_text )
317                 asprintf( &psz_arguments, "((%s))", psz_list );
318             else
319                 asprintf( &psz_arguments, "(%s)", psz_list );
320                 
321             free( psz_list );
322         }
323         else if( p_item->i_min != 0 || p_item->i_max != 0 )
324         {
325 //            p_control = new RangedIntConfigControl( p_this, p_item, parent );
326         }
327         else
328         {
329 //            p_control = new IntegerConfigControl( p_this, p_item, parent );
330         }
331         break;
332
333     case CONFIG_ITEM_KEY:
334 //        p_control = new KeyConfigControl( p_this, p_item, parent );
335         break;
336
337     case CONFIG_ITEM_FLOAT:
338 //        p_control = new FloatConfigControl( p_this, p_item, parent );
339         break;
340
341     case CONFIG_ITEM_BOOL:
342 //        p_control = new BoolConfigControl( p_this, p_item, parent );
343         psz_arguments = NULL;
344         asprintf( &psz_exclusive, "--no%s --no-%s", p_item->psz_name,
345                  p_item->psz_name );
346         PrintOption( p_item->psz_name, p_item->i_short, psz_exclusive,
347                      p_item->psz_text, p_item->psz_longtext, psz_arguments );
348         free( psz_exclusive );
349         asprintf( &psz_exclusive, "--no%s --%s", p_item->psz_name,
350                  p_item->psz_name );
351         asprintf( &psz_option, "no-%s", p_item->psz_name );
352         PrintOption( psz_option, p_item->i_short, psz_exclusive,
353                      p_item->psz_text, p_item->psz_longtext, psz_arguments );
354         free( psz_exclusive );
355         free( psz_option );
356         asprintf( &psz_exclusive, "--no-%s --%s", p_item->psz_name,
357                  p_item->psz_name );
358         asprintf( &psz_option, "no%s", p_item->psz_name );
359         PrintOption( psz_option, p_item->i_short, psz_exclusive,
360                      p_item->psz_text, p_item->psz_longtext, psz_arguments );
361         free( psz_exclusive );
362         free( psz_option );        
363         return;
364
365     case CONFIG_SECTION:
366 //        p_control = new SectionConfigControl( p_this, p_item, parent );
367         break;
368
369     default:
370         break;
371     }
372     PrintOption( p_item->psz_name, p_item->i_short, NULL,
373                  p_item->psz_text, p_item->psz_longtext, psz_arguments );
374
375 }
376
377 void PrintOption( char *psz_option, char i_short, char *psz_exclusive,
378                    char *psz_text, char *psz_longtext, char *psz_args )
379 {
380     char *foo;
381     if( !psz_longtext ||
382         strchr( psz_longtext, '\n' ) ||
383         strchr( psz_longtext, '(' ) ) psz_longtext = psz_text;
384     if( psz_text )
385         while( (foo = strchr( psz_text, '"' ))) *foo='\'';
386     if( i_short )
387     {
388         if( !psz_exclusive ) psz_exclusive = "";
389         else asprintf( &psz_exclusive, " %s", psz_exclusive );
390         printf( "  \"(-%c%s)--%s%s[%s]", i_short, psz_exclusive,
391                 psz_option, psz_args?"=":"", psz_text );
392         if( psz_args )
393             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
394         else
395             printf( "\"\\\n" );
396         printf( "  \"(--%s%s)-%c[%s]", psz_option, psz_exclusive,
397                 i_short, psz_text );        
398         if( psz_args )
399             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
400         else
401             printf( "\"\\\n" );
402                 
403     }
404     else
405     {
406         if( psz_exclusive )
407             printf( "  \"(%s)--%s%s[%s]", psz_exclusive, psz_option,
408                     psz_args?"=":"", psz_text );
409         else
410             printf( "  \"--%s[%s]", psz_option, psz_text );
411             
412         if( psz_args )
413             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
414         else
415             printf( "\"\\\n" );
416         
417     }
418 }
419