]> git.sesse.net Git - vlc/blob - extras/analyser/zsh.cpp
Reimplemented the way glyphs are rendered in YUVA mode.
[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( 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( ++i_items < p_module->confsize && p_item++ );
149
150     }
151     module_list_free( p_list );
152 }
153
154 void PrintModuleList( mumap &mods, mcmap &mods2 )
155 {
156     module_t       **p_list = NULL;
157     module_t        *p_module;
158     int              i_index;
159     int              i_items;
160     size_t           i_modules;
161
162     /* List the plugins */
163     p_list = module_list_get(&i_modules);
164     if( !p_list ) return;
165
166     printf( "modules=\"" );
167     for( i_index = 0; i_index < i_modules; i_index++ )
168     {
169         p_module = p_list[i_index];
170
171         /* Exclude empty plugins (submodules don't have config options, they
172          * are stored in the parent module) */
173
174         if( strcmp( p_module->psz_object_name, "main" ) )
175         {
176             mods.insert( mpair( p_module->psz_capability,
177                                 p_module->psz_object_name ) );
178             module_config_t *p_config = p_module->p_config;
179             i_items = 0;
180             if( p_config ) do
181             {
182                 /* Hack: required subcategory is stored in i_min */
183                 if( p_config->i_type == CONFIG_SUBCATEGORY )
184                 {
185                     mods2.insert( mcpair( p_config->value.i,
186                                           p_module->psz_object_name ) );
187                 }
188             } while( i_items++ < p_module->i_config_items && p_config++ );
189             if( p_module->parent )
190                 continue;
191             printf( "%s ", p_module->psz_object_name );
192         }
193
194     }
195     printf( "\"\n\n" );
196     module_list_free( p_list );
197     return;
198 }
199
200 void ParseOption( module_config_t *p_item, mumap &mods, mcmap &mods2 )
201 {
202     char *psz_arguments = NULL;
203     char *psz_exclusive;
204     char *psz_option;
205     char *psz_name;
206     char *psz_text;
207     char *psz_longtext;
208
209 #define DUP( x ) strdup( x ? x : "" )
210
211     //Skip deprecated options
212     if( p_item->b_removed )
213         return;
214
215     switch( p_item->i_type )
216     {
217     case CONFIG_ITEM_MODULE:
218     {
219         std::pair<mumap::iterator, mumap::iterator> range = mods.equal_range( p_item->psz_type );
220         std::string list = (*range.first).second;
221         if( range.first != range.second )
222         {
223             while( range.first++ != range.second )
224             {
225                 list = list.append( " " );
226                 list = list.append( range.first->second );
227             }
228             asprintf( &psz_arguments, "(%s)", list.c_str() );
229         }
230     }
231     break;
232     case CONFIG_ITEM_MODULE_CAT:
233     {
234         std::pair<mcmap::iterator, mcmap::iterator> range =
235             mods2.equal_range( p_item->min.i );
236         std::string list = (*range.first).second;
237         if( range.first != range.second )
238         {
239             while( range.first++ != range.second )
240             {
241                 list = list.append( " " );
242                 list = list.append( range.first->second );
243             }
244             asprintf( &psz_arguments, "(%s)", list.c_str() );
245         }
246     }
247     break;
248     case CONFIG_ITEM_MODULE_LIST_CAT:
249     {
250         std::pair<mcmap::iterator, mcmap::iterator> range =
251             mods2.equal_range( p_item->min.i );
252         std::string list = "_values -s , ";
253         list = list.append( p_item->psz_name );
254         while( range.first != range.second )
255         {
256             list = list.append( " '*" );
257             list = list.append( range.first->second );
258             list = list.append( "'" );
259             ++range.first;
260         }
261         psz_arguments = strdup( list.c_str() );
262     }
263     break;
264
265     case CONFIG_ITEM_STRING:
266         if( p_item->i_list )
267         {
268             int i = p_item->i_list -1;
269             char *psz_list;
270             if( p_item->ppsz_list_text )
271                 asprintf( &psz_list, "%s\\:\\\"%s\\\"", p_item->ppsz_list[i],
272                           p_item->ppsz_list_text[i] );
273             else
274                 psz_list = strdup(p_item->ppsz_list[i]);
275             char *psz_list2;
276             while( i>1 )
277             {
278                 if( p_item->ppsz_list_text )
279                     asprintf( &psz_list2, "%s\\:\\\"%s\\\" %s", p_item->ppsz_list[i-1],
280                               p_item->ppsz_list_text[i-1], psz_list );
281                 else
282                     asprintf( &psz_list2, "\\\"%s\\\" %s", p_item->ppsz_list[i-1],
283                               psz_list );
284
285                 free( psz_list );
286                 psz_list = psz_list2;
287                 i--;
288             }
289             if( p_item->ppsz_list_text )
290                 asprintf( &psz_arguments, "((%s))", psz_list );
291             else
292                 asprintf( &psz_arguments, "(%s)", psz_list );
293
294             free( psz_list );
295         }
296         break;
297
298     case CONFIG_ITEM_LOADFILE:
299     case CONFIG_ITEM_SAVEFILE:
300         psz_arguments = strdup( "_files" );
301         break;
302     case CONFIG_ITEM_DIRECTORY:
303         psz_arguments = strdup( "_files -/" );
304         break;
305
306     case CONFIG_ITEM_INTEGER:
307         if( p_item->i_list )
308         {
309             int i = p_item->i_list -1;
310             char *psz_list;
311             if( p_item->ppsz_list_text )
312                 asprintf( &psz_list, "%d\\:\\\"%s\\\"", p_item->pi_list[i],
313                           p_item->ppsz_list_text[i] );
314             else
315                 psz_list = strdup(p_item->ppsz_list[i]);
316             char *psz_list2;
317             while( i > 0 )
318             {
319                 if( p_item->ppsz_list_text )
320                     asprintf( &psz_list2, "%d\\:\\\"%s\\\" %s", p_item->pi_list[i-1],
321                               p_item->ppsz_list_text[i-1], psz_list );
322                 else
323                     asprintf( &psz_list2, "\\\"%s\\\" %s", p_item->ppsz_list[i-1],
324                               psz_list );
325
326                 free( psz_list );
327                 psz_list = psz_list2;
328                 i--;
329             }
330             if( p_item->ppsz_list_text )
331                 asprintf( &psz_arguments, "((%s))", psz_list );
332             else
333                 asprintf( &psz_arguments, "(%s)", psz_list );
334
335             free( psz_list );
336         }
337         else if( p_item->min.i != 0 || p_item->max.i != 0 )
338         {
339 //            p_control = new RangedIntConfigControl( p_this, p_item, parent );
340         }
341         else
342         {
343 //            p_control = new IntegerConfigControl( p_this, p_item, parent );
344         }
345         break;
346
347     case CONFIG_ITEM_KEY:
348 //        p_control = new KeyConfigControl( p_this, p_item, parent );
349         break;
350
351     case CONFIG_ITEM_FLOAT:
352 //        p_control = new FloatConfigControl( p_this, p_item, parent );
353         break;
354
355     case CONFIG_ITEM_BOOL:
356 //        p_control = new BoolConfigControl( p_this, p_item, parent );
357         asprintf( &psz_exclusive, "--no%s --no-%s", p_item->psz_name,
358                  p_item->psz_name );
359         psz_name = DUP( p_item->psz_name );
360         psz_text = DUP( p_item->psz_text );
361         psz_longtext = DUP( p_item->psz_longtext );
362         PrintOption( psz_name, p_item->i_short, psz_exclusive,
363                      psz_text, psz_longtext, psz_arguments );
364         free( psz_name );
365         free( psz_text );
366         free( psz_longtext );
367         free( psz_exclusive );
368         asprintf( &psz_exclusive, "--no%s --%s", p_item->psz_name,
369                  p_item->psz_name );
370         asprintf( &psz_option, "no-%s", p_item->psz_name );
371         psz_text = DUP( p_item->psz_text );
372         psz_longtext = DUP( p_item->psz_longtext );
373         PrintOption( psz_option, p_item->i_short, psz_exclusive,
374                      psz_text, psz_longtext, psz_arguments );
375         free( psz_text );
376         free( psz_longtext );
377         free( psz_exclusive );
378         free( psz_option );
379         asprintf( &psz_exclusive, "--no-%s --%s", p_item->psz_name,
380                  p_item->psz_name );
381         asprintf( &psz_option, "no%s", p_item->psz_name );
382         psz_text = DUP( p_item->psz_text );
383         psz_longtext = DUP( p_item->psz_longtext );
384         PrintOption( psz_option, p_item->i_short, psz_exclusive,
385                      psz_text, psz_longtext, psz_arguments );
386         free( psz_text );
387         free( psz_longtext );
388         free( psz_exclusive );
389         free( psz_option );
390         return;
391
392     case CONFIG_SECTION:
393 //        p_control = new SectionConfigControl( p_this, p_item, parent );
394         break;
395
396     default:
397         break;
398     }
399     psz_name = DUP( p_item->psz_name );
400     psz_text = DUP( p_item->psz_text );
401     psz_longtext = DUP( p_item->psz_longtext );
402     PrintOption( psz_name, p_item->i_short, NULL,
403                  psz_text, psz_longtext, psz_arguments );
404     free( psz_name );
405     free( psz_text );
406     free( psz_longtext );
407     free( psz_arguments );
408 }
409
410 void PrintOption( char *psz_option, char i_short, char *psz_exclusive,
411                    char *psz_text, char *psz_longtext, char *psz_args )
412 {
413     char *foo;
414     if( psz_text )
415     {
416         while( (foo = strchr( psz_text, ':' ))) *foo=';';
417         while( (foo = strchr( psz_text, '"' ))) *foo='\'';
418         while( (foo = strchr( psz_text, '`' ))) *foo='\'';
419     }
420     if( psz_longtext )
421     {
422         while( (foo = strchr( psz_longtext, ':' ))) *foo=';';
423         while( (foo = strchr( psz_longtext, '"' ))) *foo='\'';
424         while( (foo = strchr( psz_longtext, '`' ))) *foo='\'';
425     }
426     if( !psz_longtext ||
427         strchr( psz_longtext, '\n' ) ||
428         strchr( psz_longtext, '(' ) ) psz_longtext = psz_text;
429     if( i_short )
430     {
431         if( !psz_exclusive )
432             printf( "  \"(-%c)--%s%s[%s]", i_short,
433                     psz_option, psz_args?"=":"", psz_text );
434         else
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
442         printf( "  \"(--%s%s)-%c[%s]", psz_option, psz_exclusive ? psz_exclusive : "",
443                 i_short, psz_text );
444         if( psz_args )
445             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
446         else
447             printf( "\"\\\n" );
448
449     }
450     else
451     {
452         if( psz_exclusive )
453             printf( "  \"(%s)--%s%s[%s]", psz_exclusive, psz_option,
454                     psz_args?"=":"", psz_text );
455         else
456             printf( "  \"--%s[%s]", psz_option, psz_text );
457
458         if( psz_args )
459             printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
460         else
461             printf( "\"\\\n" );
462     }
463 }
464