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