zsh.cpp 9.15 KB
Newer Older
1 2 3
/*****************************************************************************
 * zsh.cpp: create zsh completion rule for vlc
 *****************************************************************************
Rafaël Carré's avatar
Rafaël Carré committed
4
 * Copyright © 2005-2011 the VideoLAN team
5
 *
6
 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
Rafaël Carré's avatar
Rafaël Carré committed
7
            Rafaël Carré <funman@videolanorg>
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
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23
 *****************************************************************************/

Rafaël Carré's avatar
Rafaël Carré committed
24 25 26 27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

28
#include <algorithm>
29
#include <iostream>
30
#include <map>
31
#include <set>
32
#include <string>
Rafaël Carré's avatar
Rafaël Carré committed
33
#include <sstream>
Rafaël Carré's avatar
Rafaël Carré committed
34 35 36 37

#include <vlc/vlc.h>
#include <vlc_common.h>
#include <vlc_modules.h>
Rafaël Carré's avatar
Rafaël Carré committed
38
#include <vlc_plugin.h>
39

40
typedef std::pair<std::string, std::string> mpair;
Rafaël Carré's avatar
Rafaël Carré committed
41
typedef std::multimap<std::string, std::string> mumap;
Rafaël Carré's avatar
Rafaël Carré committed
42
mumap capabilities;
Rafaël Carré's avatar
Rafaël Carré committed
43

44
typedef std::pair<int, std::string> mcpair;
Rafaël Carré's avatar
Rafaël Carré committed
45
typedef std::multimap<int, std::string> mcmap;
Rafaël Carré's avatar
Rafaël Carré committed
46
mcmap categories;
Rafaël Carré's avatar
Rafaël Carré committed
47

48 49 50
std::set<std::string> mnames;

static void ReplaceChars(std::string& str)
51
{
52 53 54
    std::replace(str.begin(), str.end(), ':', ';');
    std::replace(str.begin(), str.end(), '"', '\'');
    std::replace(str.begin(), str.end(), '`', '\'');
55 56
}

Rafaël Carré's avatar
Rafaël Carré committed
57 58
static void PrintOption(const module_config_t *item, const std::string &opt,
                        const std::string &excl, const std::string &args)
59
{
60 61
    std::string longtext = item->psz_longtext ? item->psz_longtext : "";
    std::string text = item->psz_text ? item->psz_text : "";
Rafaël Carré's avatar
Rafaël Carré committed
62 63 64
    char i_short = item->i_short;
    ReplaceChars(longtext);
    ReplaceChars(text);
65

66
    if (!longtext.length() || longtext.find('\n') != std::string::npos || longtext.find('(') != std::string::npos)
Rafaël Carré's avatar
Rafaël Carré committed
67
        longtext = text;
68

69
    std::cout << "  \"";
70

Rafaël Carré's avatar
Rafaël Carré committed
71 72
    const char *args_c = args.empty() ? "" : "=";
    if (i_short) {
73
        std::cout << "(-" << i_short;
74

Rafaël Carré's avatar
Rafaël Carré committed
75
        if (!excl.empty())
76
            std::cout << excl;
77

78
        std::cout << ")--" << opt << args_c << "[" << text << "]";
Rafaël Carré's avatar
Rafaël Carré committed
79 80

        if (!args.empty())
81
            std::cout << ":" << longtext << ":" << args;
82

83
        std::cout << "\"\\\n  \"(--" << opt << excl << ")-" << i_short;
Rafaël Carré's avatar
Rafaël Carré committed
84 85
    } else {
        if (!excl.empty())
86 87
            std::cout << "(" << excl << ")";
        std::cout << "--" << opt;
Rafaël Carré's avatar
Rafaël Carré committed
88
        if (!excl.empty())
89
            std::cout << args_c;
90
    }
Rafaël Carré's avatar
Rafaël Carré committed
91

92
    std::cout << "[" << text << "]";
Rafaël Carré's avatar
Rafaël Carré committed
93
    if (!args.empty())
94 95
        std::cout << ":" << longtext << ":" << args;
    std::cout << "\"\\\n";
96 97
}

Rafaël Carré's avatar
Rafaël Carré committed
98
static void ParseOption(const module_config_t *item)
99
{
Rafaël Carré's avatar
Rafaël Carré committed
100 101 102 103
    std::string excl, args;
    std::string list;
    std::pair<mcmap::iterator, mcmap::iterator> range;
    std::pair<mumap::iterator, mumap::iterator> range_mod;
104

Rafaël Carré's avatar
Rafaël Carré committed
105
    if (item->b_removed)
106
        return;
107

Rafaël Carré's avatar
Rafaël Carré committed
108
    switch(item->i_type)
109 110
    {
    case CONFIG_ITEM_MODULE:
111
        range_mod = capabilities.equal_range(item->psz_type ? item->psz_type : "");
Rafaël Carré's avatar
Rafaël Carré committed
112 113 114 115
        args = "(" + (*range_mod.first).second;
        while (range_mod.first++ != range_mod.second)
            args += " " + range_mod.first->second;
        args += ")";
116
    break;
Rafaël Carré's avatar
Rafaël Carré committed
117

118
    case CONFIG_ITEM_MODULE_CAT:
Rafaël Carré's avatar
Rafaël Carré committed
119 120 121 122 123
        range = categories.equal_range(item->min.i);
        args = "(" + (*range.first).second;
        while (range.first++ != range.second)
            args += " " + range.first->second;
        args += ")";
124
    break;
Rafaël Carré's avatar
Rafaël Carré committed
125

126
    case CONFIG_ITEM_MODULE_LIST_CAT:
Rafaël Carré's avatar
Rafaël Carré committed
127
        range = categories.equal_range(item->min.i);
Rafaël Carré's avatar
Rafaël Carré committed
128 129 130
        args = std::string("_values -s , ") + item->psz_name;
        while (range.first != range.second)
            args += " '*" + range.first++->second + "'";
131
    break;
132

133 134
    case CONFIG_ITEM_LOADFILE:
    case CONFIG_ITEM_SAVEFILE:
Rafaël Carré's avatar
Rafaël Carré committed
135
        args = "_files";
136 137
        break;
    case CONFIG_ITEM_DIRECTORY:
Rafaël Carré's avatar
Rafaël Carré committed
138
        args = "_files -/";
139 140
        break;

Rafaël Carré's avatar
Rafaël Carré committed
141
    case CONFIG_ITEM_STRING:
142
    case CONFIG_ITEM_INTEGER:
143
        if (item->list_count == 0)
Rafaël Carré's avatar
Rafaël Carré committed
144 145
            break;

146
        for (int i = 0; i < item->list_count; i++) {
Rafaël Carré's avatar
Rafaël Carré committed
147
            std::string val;
148 149
            if (item->list_text) {
                const char *text = item->list_text[i];
Rafaël Carré's avatar
Rafaël Carré committed
150 151
                if (item->i_type == CONFIG_ITEM_INTEGER) {
                    std::stringstream s;
152
                    s << item->list.i[i];
Rafaël Carré's avatar
Rafaël Carré committed
153 154
                    val = s.str() + "\\:\\\"" + text;
                } else {
155
                    if (!item->list.psz[i] || !text)
Rafaël Carré's avatar
Rafaël Carré committed
156
                        continue;
157
                    val = item->list.psz[i] + std::string("\\:\\\"") + text;
Rafaël Carré's avatar
Rafaël Carré committed
158 159
                }
            } else
160
                val = std::string("\\\"") + item->list.psz[i];
Rafaël Carré's avatar
Rafaël Carré committed
161 162

            list = val + "\\\" " + list;
163 164
        }

165
        if (item->list_text)
Rafaël Carré's avatar
Rafaël Carré committed
166 167 168
            args = std::string("((") + list + "))";
        else
            args = std::string("(") + list + ")";
169 170 171 172

        break;

    case CONFIG_ITEM_BOOL:
Rafaël Carré's avatar
Rafaël Carré committed
173 174 175 176 177 178 179 180
        excl = std::string("--no") + item->psz_name + " --no-" + item->psz_name;
        PrintOption(item, item->psz_name,                       excl, args);

        excl = std::string("--no") + item->psz_name + " --"    + item->psz_name;
        PrintOption(item, std::string("no-") + item->psz_name,  excl, args);

        excl = std::string("--no-")+ item->psz_name + " --"  + item->psz_name;
        PrintOption(item, std::string("no") + item->psz_name,   excl, args);
181 182
        return;

Rafaël Carré's avatar
Rafaël Carré committed
183
    case CONFIG_ITEM_KEY:
184
    case CONFIG_SECTION:
Rafaël Carré's avatar
Rafaël Carré committed
185
    case CONFIG_ITEM_FLOAT:
186 187 188
    default:
        break;
    }
Rafaël Carré's avatar
Rafaël Carré committed
189 190

    PrintOption(item, item->psz_name, "", args);
191 192
}

Rafaël Carré's avatar
Rafaël Carré committed
193
static void PrintModule(const module_t *mod)
194
{
195
    if (module_is_main(mod))
Rafaël Carré's avatar
Rafaël Carré committed
196 197
        return;

198 199 200 201 202
    const char *name = module_get_object(mod);
    const char *cap = module_get_capability(mod);

    if (strcmp(cap, "none"))
        capabilities.insert(mpair(cap, name));
Rafaël Carré's avatar
Rafaël Carré committed
203

204 205 206 207 208 209
    unsigned int cfg_size = 0;
    module_config_t *cfg_list = module_config_get(mod, &cfg_size);

    for (unsigned int j = 0; j < cfg_size; ++j)
    {
        const module_config_t *cfg = cfg_list + j;
Rafaël Carré's avatar
Rafaël Carré committed
210 211
        if (cfg->i_type == CONFIG_SUBCATEGORY)
            categories.insert(mcpair(cfg->value.i, name));
212 213 214
    }

    module_config_free(cfg_list);
Rafaël Carré's avatar
Rafaël Carré committed
215

216 217
    if (mnames.find(name) == mnames.end())
    {
218
        std::cout << name << " ";
219 220
        mnames.insert(name);
    }
Rafaël Carré's avatar
Rafaël Carré committed
221
}
222

Rafaël Carré's avatar
Rafaël Carré committed
223
static void ParseModule(const module_t *mod)
Rafaël Carré's avatar
Rafaël Carré committed
224
{
225 226
    unsigned int cfg_size = 0;
    module_config_t *cfg_list = module_config_get(mod, &cfg_size);
Rafaël Carré's avatar
Rafaël Carré committed
227

228 229 230
    for (unsigned int j = 0; j < cfg_size; ++j)
    {
        const module_config_t *cfg = cfg_list + j;
Rafaël Carré's avatar
Rafaël Carré committed
231 232
        if (CONFIG_ITEM(cfg->i_type))
            ParseOption(cfg);
233 234 235
    }

    module_config_free(cfg_list);
236 237
}

Rafaël Carré's avatar
Rafaël Carré committed
238 239
int main(int argc, const char **argv)
{
240
    libvlc_instance_t *libvlc = libvlc_new(argc - 1, argv + 1);
Rafaël Carré's avatar
Rafaël Carré committed
241 242 243
    if (!libvlc)
        return 1;

Rafaël Carré's avatar
Rafaël Carré committed
244 245 246 247 248
    size_t modules = 0;
    module_t **mod_list;

    mod_list = module_list_get(&modules);
    if (!mod_list || modules == 0)
249 250
    {
        libvlc_release(libvlc);
Rafaël Carré's avatar
Rafaël Carré committed
251
        return 2;
252
    }
Rafaël Carré's avatar
Rafaël Carré committed
253 254 255

    module_t **max = &mod_list[modules];

256
    std::cout << "#compdef vlc cvlc rvlc svlc mvlc qvlc nvlc\n"
257 258 259
           "#This file is autogenerated by zsh.cpp\n"
           "typeset -A opt_args\n"
           "local context state line ret=1\n"
260
           "local modules\n\n";
Rafaël Carré's avatar
Rafaël Carré committed
261

262
    std::cout << "vlc_modules=\"";
Rafaël Carré's avatar
Rafaël Carré committed
263 264
    for (module_t **mod = mod_list; mod < max; mod++)
        PrintModule(*mod);
265
    std::cout << "\"\n\n";
Rafaël Carré's avatar
Rafaël Carré committed
266

267
    std::cout << "_arguments -S -s \\\n";
Rafaël Carré's avatar
Rafaël Carré committed
268 269
    for (module_t **mod = mod_list; mod < max; mod++)
        ParseModule(*mod);
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    std::cout << "  \"(--module)-p[print help on module]:print help on module:($vlc_modules)\"\\\n";
    std::cout << "  \"(-p)--module[print help on module]:print help on module:($vlc_modules)\"\\\n";
    std::cout << "  \"(--help)-h[print help]\"\\\n";
    std::cout << "  \"(-h)--help[print help]\"\\\n";
    std::cout << "  \"(--longhelp)-H[print detailed help]\"\\\n";
    std::cout << "  \"(-H)--longhelp[print detailed help]\"\\\n";
    std::cout << "  \"(--list)-l[print a list of available modules]\"\\\n";
    std::cout << "  \"(-l)--list[print a list of available modules]\"\\\n";
    std::cout << "  \"--reset-config[reset the current config to the default values]\"\\\n";
    std::cout << "  \"--config[use alternate config file]\"\\\n";
    std::cout << "  \"--reset-plugins-cache[resets the current plugins cache]\"\\\n";
    std::cout << "  \"--version[print version information]\"\\\n";
    std::cout << "  \"*:Playlist item:->mrl\" && ret=0\n\n";

    std::cout << "case $state in\n";
    std::cout << "  mrl)\n";
    std::cout << "    _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0\n";
    std::cout << "  ;;\n";
    std::cout << "esac\n\n";

    std::cout << "return ret\n";
Rafaël Carré's avatar
Rafaël Carré committed
291

Rafaël Carré's avatar
Rafaël Carré committed
292
    module_list_free(mod_list);
Rafaël Carré's avatar
Rafaël Carré committed
293 294 295
    libvlc_release(libvlc);
    return 0;
}