]> git.sesse.net Git - vlc/blob - extras/analyser/zsh.cpp
Source Tree Layout Cleanup: (As decided at videolan-meeting #2)
[vlc] / extras / analyser / 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> 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/vlc.h>
41
42 /* evil hack */
43 #undef __PLUGIN__
44 #undef __BUILTIN__
45 #include <../src/modules/modules.h>
46
47 void ParseModules( libvlc_int_t *p_libvlc, mumap &mods, mcmap &mods2 );
48 void PrintModuleList( libvlc_int_t *p_libvlc, mumap &mods, mcmap &mods2 );
49 void ParseOption( module_config_t *p_item, mumap &mods, mcmap &mods2 );
50 void PrintOption( char *psz_option, char i_short, char *psz_exlusive,
51                    char *psz_text, char *psz_longtext, char *psz_args );
52
53 int main( int i_argc, const char **ppsz_argv )
54 {
55     mumap mods;
56     mcmap mods2;
57     /* Create a libvlc structure */
58     int i_ret = VLC_Create();
59     libvlc_int_t *p_libvlc;
60
61     if( i_ret < 0 )
62     {
63         return i_ret;
64     }
65
66     /* Initialize libvlc */
67     i_ret = VLC_Init( 0, i_argc, ppsz_argv );
68     if( i_ret < 0 )
69     {
70         VLC_Destroy( 0 );
71         return i_ret;
72     }
73     p_libvlc = (libvlc_int_t*)vlc_object_get( (vlc_object_t*)NULL, i_ret );
74     printf("#compdef vlc\n\n"
75
76            "#This file is autogenerated by zsh.cpp\n"
77            "typeset -A opt_args\n"
78            "local context state line ret=1\n"
79            "local modules\n\n" );
80
81     PrintModuleList( p_libvlc, mods, mods2 );
82
83     printf( "_arguments -S -s \\\n" );
84     ParseModules( p_libvlc, mods, mods2 );
85     printf( "  \"(--module)-p[print help on module]:print help on module:($modules)\"\\\n" );
86     printf( "  \"(-p)--module[print help on module]:print help on module:($modules)\"\\\n" );
87     printf( "  \"(--help)-h[print help]\"\\\n" );
88     printf( "  \"(-h)--help[print help]\"\\\n" );
89     printf( "  \"(--longhelp)-H[print detailed help]\"\\\n" );
90     printf( "  \"(-H)--longhelp[print detailed help]\"\\\n" );
91     printf( "  \"(--list)-l[print a list of available modules]\"\\\n" );
92     printf( "  \"(-l)--list[print a list of available modules]\"\\\n" );
93     printf( "  \"--save-config[save the current command line options in the config file]\"\\\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     return 0;
109     /* Finish the threads */
110     VLC_CleanUp( 0 );
111
112     /* Destroy the libvlc structure */
113     VLC_Destroy( 0 );
114
115     return i_ret;
116
117 }
118
119 void ParseModules( libvlc_int_t *p_libvlc, mumap &mods, mcmap &mods2 )
120 {
121     vlc_list_t      *p_list = NULL;;
122     module_t        *p_module;
123     module_config_t *p_item;
124     int              i_index;
125     int              i_items;
126
127     /* List the plugins */
128     p_list = vlc_list_find( p_libvlc, VLC_OBJECT_MODULE, FIND_ANYWHERE );
129     if( !p_list ) return;
130     for( i_index = 0; i_index < p_list->i_count; i_index++ )
131     {
132         p_module = (module_t *)p_list->p_values[i_index].p_object;
133
134         /* Exclude empty plugins (submodules don't have config options, they
135          * are stored in the parent module) */
136         if( p_module->b_submodule )
137               continue;
138 //            p_item = ((module_t *)p_module->p_parent)->p_config;
139         else
140             p_item = p_module->p_config;
141
142 //        printf( "  #%s\n", p_module->psz_longname );
143         if( !p_item ) continue;
144         i_items = 0;
145         do
146         {
147             if( p_item->i_type == CONFIG_CATEGORY )
148             {
149 //                printf( "  #Category %d\n", p_item->i_value );
150             }
151             else if( p_item->i_type == CONFIG_SUBCATEGORY )
152             {
153 //                printf( "  #Subcategory %d\n", p_item->i_value );
154             }
155             if( p_item->i_type & CONFIG_ITEM )
156                 ParseOption( p_item, mods, mods2 );
157         }
158         while( i_items++ < p_module->i_config_items && p_item++ );
159
160     }
161 }
162
163 void PrintModuleList( libvlc_int_t *p_libvlc, mumap &mods, mcmap &mods2 )
164 {
165     vlc_list_t      *p_list = NULL;;
166     module_t        *p_module;
167     int              i_index;
168     int              i_items;
169
170     /* List the plugins */
171     p_list = vlc_list_find( p_libvlc, VLC_OBJECT_MODULE, FIND_ANYWHERE );
172     if( !p_list ) return;
173
174     printf( "modules=\"" );
175     for( i_index = 0; i_index < p_list->i_count; i_index++ )
176     {
177         p_module = (module_t *)p_list->p_values[i_index].p_object;
178
179         /* Exclude empty plugins (submodules don't have config options, they
180          * are stored in the parent module) */
181
182         if( strcmp( p_module->psz_object_name, "main" ) )
183         {
184             mods.insert( mpair( p_module->psz_capability,
185                                 p_module->psz_object_name ) );
186             module_config_t *p_config = p_module->p_config;
187             i_items = 0;
188             if( p_config ) do
189             {
190                 /* Hack: required subcategory is stored in i_min */
191                 if( p_config->i_type == CONFIG_SUBCATEGORY )
192                 {
193                     mods2.insert( mcpair( p_config->value.i,
194                                           p_module->psz_object_name ) );
195                 }
196             } while( i_items++ < p_module->i_config_items && p_config++ );
197             if( p_module->b_submodule )
198                 continue;
199             printf( "%s ", p_module->psz_object_name );
200         }
201
202     }
203     printf( "\"\n\n" );
204     return;
205 }
206
207 void ParseOption( module_config_t *p_item, mumap &mods, mcmap &mods2 )
208 {
209     char *psz_arguments = strdup( "" );
210     char *psz_exclusive;
211     char *psz_option;
212     char *psz_name;
213     char *psz_text;
214     char *psz_longtext;
215
216 #define DUP( x ) strdup( x ? x : "" )
217
218     //Skip deprecated options
219     if( p_item->b_removed )
220         return;
221
222     switch( p_item->i_type )
223     {
224     case CONFIG_ITEM_MODULE:
225     {
226         std::pair<mumap::iterator, mumap::iterator> range = mods.equal_range( p_item->psz_type );
227         std::string list = (*range.first).second;
228         ++range.first;
229         while( range.first != range.second )
230         {
231             list = list.append( " " );
232             list = list.append( range.first->second );
233             ++range.first;
234         }
235         asprintf( &psz_arguments, "(%s)", list.c_str() );
236     }
237     break;
238     case CONFIG_ITEM_MODULE_CAT:
239     {
240         std::pair<mcmap::iterator, mcmap::iterator> range =
241             mods2.equal_range( p_item->min.i );
242         std::string list = (*range.first).second;
243         ++range.first;
244         while( range.first != range.second )
245         {
246             list = list.append( " " );
247             list = list.append( range.first->second );
248             ++range.first;
249         }
250         asprintf( &psz_arguments, "(%s)", list.c_str() );
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         asprintf( &psz_arguments, "%s", 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         psz_arguments = NULL;
362         asprintf( &psz_exclusive, "--no%s --no-%s", p_item->psz_name,
363                  p_item->psz_name );
364         psz_name = DUP( p_item->psz_name );
365         psz_text = DUP( p_item->psz_text );
366         psz_longtext = DUP( p_item->psz_longtext );
367         PrintOption( psz_name, p_item->i_short, psz_exclusive,
368                      psz_text, psz_longtext, psz_arguments );
369         free( psz_name );
370         free( psz_text );
371         free( psz_longtext );
372         free( psz_exclusive );
373         asprintf( &psz_exclusive, "--no%s --%s", p_item->psz_name,
374                  p_item->psz_name );
375         asprintf( &psz_option, "no-%s", p_item->psz_name );
376         psz_text = DUP( p_item->psz_text );
377         psz_longtext = DUP( p_item->psz_longtext );
378         PrintOption( psz_option, p_item->i_short, psz_exclusive,
379                      psz_text, psz_longtext, psz_arguments );
380         free( psz_text );
381         free( psz_longtext );
382         free( psz_exclusive );
383         free( psz_option );
384         asprintf( &psz_exclusive, "--no-%s --%s", p_item->psz_name,
385                  p_item->psz_name );
386         asprintf( &psz_option, "no%s", p_item->psz_name );
387         psz_text = DUP( p_item->psz_text );
388         psz_longtext = DUP( p_item->psz_longtext );
389         PrintOption( psz_option, p_item->i_short, psz_exclusive,
390                      psz_text, psz_longtext, psz_arguments );
391         free( psz_text );
392         free( psz_longtext );
393         free( psz_exclusive );
394         free( psz_option );
395         return;
396
397     case CONFIG_SECTION:
398 //        p_control = new SectionConfigControl( p_this, p_item, parent );
399         break;
400
401     default:
402         break;
403     }
404     psz_name = DUP( p_item->psz_name );
405     psz_text = DUP( p_item->psz_text );
406     psz_longtext = DUP( p_item->psz_longtext );
407     PrintOption( psz_name, p_item->i_short, NULL,
408                  psz_text, psz_longtext, psz_arguments );
409     free( psz_name );
410     free( psz_text );
411     free( psz_longtext );
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 ) psz_exclusive = strdup( "" );
434         else asprintf( &psz_exclusive, " %s", psz_exclusive );
435         printf( "  \"(-%c%s)--%s%s[%s]", i_short, psz_exclusive,
436                 psz_option, psz_args?"=":"", psz_text );
437         if( psz_args )
438             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
439         else
440             printf( "\"\\\n" );
441         printf( "  \"(--%s%s)-%c[%s]", psz_option, psz_exclusive,
442                 i_short, psz_text );
443         if( psz_args )
444             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
445         else
446             printf( "\"\\\n" );
447
448     }
449     else
450     {
451         if( psz_exclusive )
452             printf( "  \"(%s)--%s%s[%s]", psz_exclusive, psz_option,
453                     psz_args?"=":"", psz_text );
454         else
455             printf( "  \"--%s[%s]", psz_option, psz_text );
456
457         if( psz_args )
458             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
459         else
460             printf( "\"\\\n" );
461
462     }
463 }
464