zsh.cpp 15 KB
Newer Older
1
2
3
/*****************************************************************************
 * zsh.cpp: create zsh completion rule for vlc
 *****************************************************************************
4
 * Copyright © 2005-2008 the VideoLAN team
5
6
 * $Id$
 *
7
 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
24
25
26
27
28
29
 *****************************************************************************/

#include <stdio.h>
#include <map>
#include <string>
#include <utility>
#include <iostream>
#include <algorithm>
Rafaël Carré's avatar
Rafaël Carré committed
30
typedef std::multimap<std::string, std::string> mumap;
31
32
typedef std::multimap<int, std::string> mcmap;

33
typedef std::pair<std::string, std::string> mpair;
34
typedef std::pair<int, std::string> mcpair;
35

36
37
38
39
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

Rafaël Carré's avatar
Rafaël Carré committed
40
#include <vlc_common.h>
41
#include <vlc/vlc.h>
Rafaël Carré's avatar
Rafaël Carré committed
42
#include <vlc_modules.h>
43

Rafaël Carré's avatar
Rafaël Carré committed
44
45
46
47
48
/* evil hack */
#undef __PLUGIN__
#undef __BUILTIN__
#include <../src/modules/modules.h>

Rafaël Carré's avatar
Rafaël Carré committed
49
50
void ParseModules( mumap &mods, mcmap &mods2 );
void PrintModuleList( mumap &mods, mcmap &mods2 );
Rafaël Carré's avatar
Rafaël Carré committed
51
void ParseOption( module_config_t *p_item, mumap &mods, mcmap &mods2 );
52
53
54
void PrintOption( char *psz_option, char i_short, char *psz_exlusive,
                   char *psz_text, char *psz_longtext, char *psz_args );

Rafaël Carré's avatar
Rafaël Carré committed
55
int main( int i_argc, const char **ppsz_argv )
56
{
Rafaël Carré's avatar
Rafaël Carré committed
57
    mumap mods;
58
    mcmap mods2;
59
    /* Create a libvlc structure */
60
61
62
63
    const char *argv[i_argc + 1];
    argv[0] = "vlc";
    for( int i = 0; i < i_argc; i++ )
        argv[i+1] = ppsz_argv[i];
ivoire's avatar
ivoire committed
64
    libvlc_instance_t *p_libvlc_instance = libvlc_new(i_argc+1, argv);
65

ivoire's avatar
ivoire committed
66
    if( !p_libvlc_instance )
67
    {
Rafaël Carré's avatar
Rafaël Carré committed
68
        return 1;
69
    }
Rafaël Carré's avatar
Rafaël Carré committed
70

71
    printf("#compdef vlc cvlc rvlc svlc mvlc qvlc nvlc\n\n"
72

73
           "#This file is autogenerated by zsh.cpp\n"
74
           "typeset -A opt_args\n"
75
           "local context state line ret=1\n"
76
77
           "local modules\n\n" );

Rafaël Carré's avatar
Rafaël Carré committed
78
    PrintModuleList( mods, mods2 );
79
80

    printf( "_arguments -S -s \\\n" );
Rafaël Carré's avatar
Rafaël Carré committed
81
    ParseModules( mods, mods2 );
82
83
84
85
86
87
88
89
90
91
92
93
    printf( "  \"(--module)-p[print help on module]:print help on module:($modules)\"\\\n" );
    printf( "  \"(-p)--module[print help on module]:print help on module:($modules)\"\\\n" );
    printf( "  \"(--help)-h[print help]\"\\\n" );
    printf( "  \"(-h)--help[print help]\"\\\n" );
    printf( "  \"(--longhelp)-H[print detailed help]\"\\\n" );
    printf( "  \"(-H)--longhelp[print detailed help]\"\\\n" );
    printf( "  \"(--list)-l[print a list of available modules]\"\\\n" );
    printf( "  \"(-l)--list[print a list of available modules]\"\\\n" );
    printf( "  \"--reset-config[reset the current config to the default values]\"\\\n" );
    printf( "  \"--config[use alternate config file]\"\\\n" );
    printf( "  \"--reset-plugins-cache[resets the current plugins cache]\"\\\n" );
    printf( "  \"--version[print version information]\"\\\n" );
94
    printf( "  \"*:Playlist item:->mrl\" && ret=0\n\n" );
95

96
97
98
99
100
    printf( "case $state in\n" );
    printf( "  mrl)\n" );
    printf( "    _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0\n" );
    printf( "  ;;\n" );
    printf( "esac\n\n" );
101

102
103
    printf( "return ret\n" );

Rafaël Carré's avatar
Rafaël Carré committed
104
    libvlc_release( p_libvlc_instance );
105

Rafaël Carré's avatar
Rafaël Carré committed
106
    return 0;
107
108
}

Rafaël Carré's avatar
Rafaël Carré committed
109
void ParseModules( mumap &mods, mcmap &mods2 )
110
{
Rafaël Carré's avatar
Rafaël Carré committed
111
    module_t       **p_list;
112
113
114
    module_t        *p_module;
    module_config_t *p_item;
    int              i_index;
Rafaël Carré's avatar
Rafaël Carré committed
115
    int              i_items;
Rafaël Carré's avatar
Rafaël Carré committed
116
    size_t           i_modules;
117
118

    /* List the plugins */
Rafaël Carré's avatar
Rafaël Carré committed
119
    p_list = module_list_get(&i_modules);
120
    if( !p_list ) return;
Rafaël Carré's avatar
Rafaël Carré committed
121
    for( i_index = 0; i_index < i_modules; i_index++ )
122
    {
Rafaël Carré's avatar
Rafaël Carré committed
123
        p_module = p_list[i_index];
124
125
126
127
128
129
130
131
132
133
134

        /* Exclude empty plugins (submodules don't have config options, they
         * are stored in the parent module) */
        if( p_module->b_submodule )
              continue;
//            p_item = ((module_t *)p_module->p_parent)->p_config;
        else
            p_item = p_module->p_config;

//        printf( "  #%s\n", p_module->psz_longname );
        if( !p_item ) continue;
Rafaël Carré's avatar
Rafaël Carré committed
135
        i_items = 0;
136
137
138
139
140
141
142
143
144
145
146
        do
        {
            if( p_item->i_type == CONFIG_CATEGORY )
            {
//                printf( "  #Category %d\n", p_item->i_value );
            }
            else if( p_item->i_type == CONFIG_SUBCATEGORY )
            {
//                printf( "  #Subcategory %d\n", p_item->i_value );
            }
            if( p_item->i_type & CONFIG_ITEM )
147
                ParseOption( p_item, mods, mods2 );
148
        }
Rafaël Carré's avatar
Rafaël Carré committed
149
        while( ++i_items < p_module->confsize && p_item++ );
150

151
    }
ivoire's avatar
ivoire committed
152
    module_list_free( p_list );
153
154
}

Rafaël Carré's avatar
Rafaël Carré committed
155
void PrintModuleList( mumap &mods, mcmap &mods2 )
156
{
Rafaël Carré's avatar
Rafaël Carré committed
157
    module_t       **p_list = NULL;
158
159
    module_t        *p_module;
    int              i_index;
Rafaël Carré's avatar
Rafaël Carré committed
160
    int              i_items;
Rafaël Carré's avatar
Rafaël Carré committed
161
    size_t           i_modules;
162
163

    /* List the plugins */
Rafaël Carré's avatar
Rafaël Carré committed
164
    p_list = module_list_get(&i_modules);
165
    if( !p_list ) return;
166
167

    printf( "modules=\"" );
Rafaël Carré's avatar
Rafaël Carré committed
168
    for( i_index = 0; i_index < i_modules; i_index++ )
169
    {
Rafaël Carré's avatar
Rafaël Carré committed
170
        p_module = p_list[i_index];
171
172
173
174
175
176

        /* Exclude empty plugins (submodules don't have config options, they
         * are stored in the parent module) */

        if( strcmp( p_module->psz_object_name, "main" ) )
        {
177
178
179
            mods.insert( mpair( p_module->psz_capability,
                                p_module->psz_object_name ) );
            module_config_t *p_config = p_module->p_config;
Rafaël Carré's avatar
Rafaël Carré committed
180
            i_items = 0;
181
182
183
184
185
            if( p_config ) do
            {
                /* Hack: required subcategory is stored in i_min */
                if( p_config->i_type == CONFIG_SUBCATEGORY )
                {
Rafaël Carré's avatar
Rafaël Carré committed
186
                    mods2.insert( mcpair( p_config->value.i,
187
188
                                          p_module->psz_object_name ) );
                }
Rafaël Carré's avatar
Rafaël Carré committed
189
            } while( i_items++ < p_module->i_config_items && p_config++ );
190
191
            if( p_module->b_submodule )
                continue;
192
193
194
195
196
            printf( "%s ", p_module->psz_object_name );
        }

    }
    printf( "\"\n\n" );
197
    module_list_free( p_list );
198
    return;
199
200
}

Rafaël Carré's avatar
Rafaël Carré committed
201
void ParseOption( module_config_t *p_item, mumap &mods, mcmap &mods2 )
202
{
ivoire's avatar
ivoire committed
203
    char *psz_arguments = NULL;
204
205
    char *psz_exclusive;
    char *psz_option;
Rafaël Carré's avatar
Rafaël Carré committed
206
207
208
209
210
211
    char *psz_name;
    char *psz_text;
    char *psz_longtext;

#define DUP( x ) strdup( x ? x : "" )

212
    //Skip deprecated options
Rafaël Carré's avatar
Rafaël Carré committed
213
    if( p_item->b_removed )
214
        return;
215

216
217
218
219
    switch( p_item->i_type )
    {
    case CONFIG_ITEM_MODULE:
    {
Rafaël Carré's avatar
Rafaël Carré committed
220
        std::pair<mumap::iterator, mumap::iterator> range = mods.equal_range( p_item->psz_type );
221
        std::string list = (*range.first).second;
ivoire's avatar
ivoire committed
222
        if( range.first != range.second )
223
        {
ivoire's avatar
ivoire committed
224
225
226
227
228
229
            while( range.first++ != range.second )
            {
                list = list.append( " " );
                list = list.append( range.first->second );
            }
            asprintf( &psz_arguments, "(%s)", list.c_str() );
230
231
232
233
        }
    }
    break;
    case CONFIG_ITEM_MODULE_CAT:
234
235
    {
        std::pair<mcmap::iterator, mcmap::iterator> range =
Rafaël Carré's avatar
Rafaël Carré committed
236
            mods2.equal_range( p_item->min.i );
237
        std::string list = (*range.first).second;
ivoire's avatar
ivoire committed
238
        if( range.first != range.second )
239
        {
ivoire's avatar
ivoire committed
240
241
242
243
244
245
            while( range.first++ != range.second )
            {
                list = list.append( " " );
                list = list.append( range.first->second );
            }
            asprintf( &psz_arguments, "(%s)", list.c_str() );
246
247
248
        }
    }
    break;
249
    case CONFIG_ITEM_MODULE_LIST_CAT:
250
251
    {
        std::pair<mcmap::iterator, mcmap::iterator> range =
Rafaël Carré's avatar
Rafaël Carré committed
252
            mods2.equal_range( p_item->min.i );
253
254
255
256
257
258
259
260
261
        std::string list = "_values -s , ";
        list = list.append( p_item->psz_name );
        while( range.first != range.second )
        {
            list = list.append( " '*" );
            list = list.append( range.first->second );
            list = list.append( "'" );
            ++range.first;
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
262
        psz_arguments = strdup( list.c_str() );
263
264
    }
    break;
265
266
267
268
269
270
271

    case CONFIG_ITEM_STRING:
        if( p_item->i_list )
        {
            int i = p_item->i_list -1;
            char *psz_list;
            if( p_item->ppsz_list_text )
Rafaël Carré's avatar
Rafaël Carré committed
272
                asprintf( &psz_list, "%s\\:\\\"%s\\\"", p_item->ppsz_list[i],
273
274
275
276
277
278
279
                          p_item->ppsz_list_text[i] );
            else
                psz_list = strdup(p_item->ppsz_list[i]);
            char *psz_list2;
            while( i>1 )
            {
                if( p_item->ppsz_list_text )
Rafaël Carré's avatar
Rafaël Carré committed
280
                    asprintf( &psz_list2, "%s\\:\\\"%s\\\" %s", p_item->ppsz_list[i-1],
281
282
                              p_item->ppsz_list_text[i-1], psz_list );
                else
Rafaël Carré's avatar
Rafaël Carré committed
283
                    asprintf( &psz_list2, "\\\"%s\\\" %s", p_item->ppsz_list[i-1],
284
285
286
287
288
289
290
291
292
293
                              psz_list );

                free( psz_list );
                psz_list = psz_list2;
                i--;
            }
            if( p_item->ppsz_list_text )
                asprintf( &psz_arguments, "((%s))", psz_list );
            else
                asprintf( &psz_arguments, "(%s)", psz_list );
294

295
296
297
298
299
            free( psz_list );
        }
        break;

    case CONFIG_ITEM_FILE:
300
        psz_arguments = strdup( "_files" );
301
302
        break;
    case CONFIG_ITEM_DIRECTORY:
303
        psz_arguments = strdup( "_files -/" );
304
305
306
307
308
309
310
311
        break;

    case CONFIG_ITEM_INTEGER:
        if( p_item->i_list )
        {
            int i = p_item->i_list -1;
            char *psz_list;
            if( p_item->ppsz_list_text )
Rafaël Carré's avatar
Rafaël Carré committed
312
                asprintf( &psz_list, "%d\\:\\\"%s\\\"", p_item->pi_list[i],
313
314
315
316
                          p_item->ppsz_list_text[i] );
            else
                psz_list = strdup(p_item->ppsz_list[i]);
            char *psz_list2;
Rafaël Carré's avatar
Rafaël Carré committed
317
            while( i > 0 )
318
319
            {
                if( p_item->ppsz_list_text )
Rafaël Carré's avatar
Rafaël Carré committed
320
                    asprintf( &psz_list2, "%d\\:\\\"%s\\\" %s", p_item->pi_list[i-1],
321
322
                              p_item->ppsz_list_text[i-1], psz_list );
                else
Rafaël Carré's avatar
Rafaël Carré committed
323
                    asprintf( &psz_list2, "\\\"%s\\\" %s", p_item->ppsz_list[i-1],
324
325
326
327
328
329
330
331
332
333
                              psz_list );

                free( psz_list );
                psz_list = psz_list2;
                i--;
            }
            if( p_item->ppsz_list_text )
                asprintf( &psz_arguments, "((%s))", psz_list );
            else
                asprintf( &psz_arguments, "(%s)", psz_list );
334

335
336
            free( psz_list );
        }
Rafaël Carré's avatar
Rafaël Carré committed
337
        else if( p_item->min.i != 0 || p_item->max.i != 0 )
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
        {
//            p_control = new RangedIntConfigControl( p_this, p_item, parent );
        }
        else
        {
//            p_control = new IntegerConfigControl( p_this, p_item, parent );
        }
        break;

    case CONFIG_ITEM_KEY:
//        p_control = new KeyConfigControl( p_this, p_item, parent );
        break;

    case CONFIG_ITEM_FLOAT:
//        p_control = new FloatConfigControl( p_this, p_item, parent );
        break;

    case CONFIG_ITEM_BOOL:
//        p_control = new BoolConfigControl( p_this, p_item, parent );
        asprintf( &psz_exclusive, "--no%s --no-%s", p_item->psz_name,
                 p_item->psz_name );
Rafaël Carré's avatar
Rafaël Carré committed
359
360
361
362
363
364
365
366
        psz_name = DUP( p_item->psz_name );
        psz_text = DUP( p_item->psz_text );
        psz_longtext = DUP( p_item->psz_longtext );
        PrintOption( psz_name, p_item->i_short, psz_exclusive,
                     psz_text, psz_longtext, psz_arguments );
        free( psz_name );
        free( psz_text );
        free( psz_longtext );
367
368
369
370
        free( psz_exclusive );
        asprintf( &psz_exclusive, "--no%s --%s", p_item->psz_name,
                 p_item->psz_name );
        asprintf( &psz_option, "no-%s", p_item->psz_name );
Rafaël Carré's avatar
Rafaël Carré committed
371
372
        psz_text = DUP( p_item->psz_text );
        psz_longtext = DUP( p_item->psz_longtext );
373
        PrintOption( psz_option, p_item->i_short, psz_exclusive,
Rafaël Carré's avatar
Rafaël Carré committed
374
375
376
                     psz_text, psz_longtext, psz_arguments );
        free( psz_text );
        free( psz_longtext );
377
378
379
380
381
        free( psz_exclusive );
        free( psz_option );
        asprintf( &psz_exclusive, "--no-%s --%s", p_item->psz_name,
                 p_item->psz_name );
        asprintf( &psz_option, "no%s", p_item->psz_name );
Rafaël Carré's avatar
Rafaël Carré committed
382
383
        psz_text = DUP( p_item->psz_text );
        psz_longtext = DUP( p_item->psz_longtext );
384
        PrintOption( psz_option, p_item->i_short, psz_exclusive,
Rafaël Carré's avatar
Rafaël Carré committed
385
386
387
                     psz_text, psz_longtext, psz_arguments );
        free( psz_text );
        free( psz_longtext );
388
        free( psz_exclusive );
389
        free( psz_option );
390
391
392
393
394
395
396
397
398
        return;

    case CONFIG_SECTION:
//        p_control = new SectionConfigControl( p_this, p_item, parent );
        break;

    default:
        break;
    }
Rafaël Carré's avatar
Rafaël Carré committed
399
400
401
402
403
404
405
406
    psz_name = DUP( p_item->psz_name );
    psz_text = DUP( p_item->psz_text );
    psz_longtext = DUP( p_item->psz_longtext );
    PrintOption( psz_name, p_item->i_short, NULL,
                 psz_text, psz_longtext, psz_arguments );
    free( psz_name );
    free( psz_text );
    free( psz_longtext );
ivoire's avatar
ivoire committed
407
    free( psz_arguments );
408
409
410
411
412
413
}

void PrintOption( char *psz_option, char i_short, char *psz_exclusive,
                   char *psz_text, char *psz_longtext, char *psz_args )
{
    char *foo;
414
415
416
417
418
419
420
421
422
423
    if( psz_text )
    {
        while( (foo = strchr( psz_text, ':' ))) *foo=';';
        while( (foo = strchr( psz_text, '"' ))) *foo='\'';
    }
    if( psz_longtext )
    {
        while( (foo = strchr( psz_longtext, ':' ))) *foo=';';
        while( (foo = strchr( psz_longtext, '"' ))) *foo='\'';
    }
424
425
426
427
428
    if( !psz_longtext ||
        strchr( psz_longtext, '\n' ) ||
        strchr( psz_longtext, '(' ) ) psz_longtext = psz_text;
    if( i_short )
    {
ivoire's avatar
ivoire committed
429
430
431
432
433
434
        if( !psz_exclusive )
            printf( "  \"(-%c)--%s%s[%s]", i_short,
                    psz_option, psz_args?"=":"", psz_text );
        else
            printf( "  \"(-%c%s)--%s%s[%s]", i_short, psz_exclusive,
                    psz_option, psz_args?"=":"", psz_text );
435
436
437
438
        if( psz_args )
            printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
        else
            printf( "\"\\\n" );
439
440

        printf( "  \"(--%s%s)-%c[%s]", psz_option, psz_exclusive ? psz_exclusive : "",
441
                i_short, psz_text );
442
443
444
445
        if( psz_args )
            printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
        else
            printf( "\"\\\n" );
446

447
448
449
450
    }
    else
    {
        if( psz_exclusive )
451
452
            printf( "  \"(%s)--%s%s[%s]", psz_exclusive, psz_option,
                    psz_args?"=":"", psz_text );
453
454
        else
            printf( "  \"--%s[%s]", psz_option, psz_text );
455

456
457
458
459
460
461
462
        if( psz_args )
            printf( ":%s:%s\"\\\n", psz_longtext, psz_args );
        else
            printf( "\"\\\n" );
    }
}