zsh.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*****************************************************************************
  2. * zsh.cpp: create zsh completion rule for vlc
  3. *****************************************************************************
  4. * Copyright © 2005-2011 the VideoLAN team
  5. *
  6. * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
  7. Rafaël Carré <funman@videolanorg>
  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. #ifdef HAVE_CONFIG_H
  24. # include "config.h"
  25. #endif
  26. #include <stdio.h>
  27. #include <map>
  28. #include <string>
  29. #include <sstream>
  30. #include <vlc/vlc.h>
  31. #include <vlc_common.h>
  32. #include <vlc_modules.h>
  33. #include <vlc_plugin.h>
  34. #include "../src/modules/modules.h" /* evil hack */
  35. typedef std::pair<std::string, std::string> mpair;
  36. typedef std::multimap<std::string, std::string> mumap;
  37. mumap capabilities;
  38. typedef std::pair<int, std::string> mcpair;
  39. typedef std::multimap<int, std::string> mcmap;
  40. mcmap categories;
  41. static void ReplaceChars(char *str)
  42. {
  43. if (str) {
  44. char *parser;
  45. while ((parser = strchr(str, ':'))) *parser=';' ;
  46. while ((parser = strchr(str, '"'))) *parser='\'' ;
  47. while ((parser = strchr(str, '`'))) *parser='\'' ;
  48. }
  49. }
  50. static void PrintOption(const module_config_t *item, const std::string &opt,
  51. const std::string &excl, const std::string &args)
  52. {
  53. char *longtext = item->psz_longtext;
  54. char *text = item->psz_text;
  55. char i_short = item->i_short;
  56. ReplaceChars(longtext);
  57. ReplaceChars(text);
  58. if (!longtext || strchr(longtext, '\n') || strchr(longtext, '('))
  59. longtext = text;
  60. printf(" \"");
  61. const char *args_c = args.empty() ? "" : "=";
  62. if (i_short) {
  63. printf("(-%c", i_short);
  64. if (!excl.empty())
  65. printf("%s", excl.c_str());
  66. printf(")--%s%s[%s]", opt.c_str(), args_c, text);
  67. if (!args.empty())
  68. printf(":%s:%s", longtext, args.c_str());
  69. printf("\"\\\n \"(--%s%s)-%c", opt.c_str(), excl.c_str(), i_short);
  70. } else {
  71. if (!excl.empty())
  72. printf("(%s)", excl.c_str());
  73. printf("--%s", opt.c_str());
  74. if (!excl.empty())
  75. printf("%s", args_c);
  76. }
  77. printf("[%s]", text);
  78. if (!args.empty())
  79. printf( ":%s:%s", longtext, args.c_str());
  80. puts( "\"\\");
  81. }
  82. static void ParseOption(const module_config_t *item)
  83. {
  84. std::string excl, args;
  85. std::string list;
  86. std::pair<mcmap::iterator, mcmap::iterator> range;
  87. std::pair<mumap::iterator, mumap::iterator> range_mod;
  88. if (item->b_removed)
  89. return;
  90. switch(item->i_type)
  91. {
  92. case CONFIG_ITEM_MODULE:
  93. range_mod = capabilities.equal_range(item->psz_type);
  94. args = "(" + (*range_mod.first).second;
  95. while (range_mod.first++ != range_mod.second)
  96. args += " " + range_mod.first->second;
  97. args += ")";
  98. break;
  99. case CONFIG_ITEM_MODULE_CAT:
  100. range = categories.equal_range(item->min.i);
  101. args = "(" + (*range.first).second;
  102. while (range.first++ != range.second)
  103. args += " " + range.first->second;
  104. args += ")";
  105. break;
  106. case CONFIG_ITEM_MODULE_LIST_CAT:
  107. range = categories.equal_range(item->min.i);
  108. args = std::string("_values -s , ") + item->psz_name;
  109. while (range.first != range.second)
  110. args += " '*" + range.first++->second + "'";
  111. break;
  112. case CONFIG_ITEM_LOADFILE:
  113. case CONFIG_ITEM_SAVEFILE:
  114. args = "_files";
  115. break;
  116. case CONFIG_ITEM_DIRECTORY:
  117. args = "_files -/";
  118. break;
  119. case CONFIG_ITEM_STRING:
  120. case CONFIG_ITEM_INTEGER:
  121. if (item->list_count == 0)
  122. break;
  123. for (int i = 0; i < item->list_count; i++) {
  124. std::string val;
  125. if (item->list_text) {
  126. const char *text = item->list_text[i];
  127. if (item->i_type == CONFIG_ITEM_INTEGER) {
  128. std::stringstream s;
  129. s << item->list.i[i];
  130. val = s.str() + "\\:\\\"" + text;
  131. } else {
  132. if (!item->list.psz[i] || !text)
  133. continue;
  134. val = item->list.psz[i] + std::string("\\:\\\"") + text;
  135. }
  136. } else
  137. val = std::string("\\\"") + item->list.psz[i];
  138. list = val + "\\\" " + list;
  139. }
  140. if (item->list_text)
  141. args = std::string("((") + list + "))";
  142. else
  143. args = std::string("(") + list + ")";
  144. break;
  145. case CONFIG_ITEM_BOOL:
  146. excl = std::string("--no") + item->psz_name + " --no-" + item->psz_name;
  147. PrintOption(item, item->psz_name, excl, args);
  148. excl = std::string("--no") + item->psz_name + " --" + item->psz_name;
  149. PrintOption(item, std::string("no-") + item->psz_name, excl, args);
  150. excl = std::string("--no-")+ item->psz_name + " --" + item->psz_name;
  151. PrintOption(item, std::string("no") + item->psz_name, excl, args);
  152. return;
  153. case CONFIG_ITEM_KEY:
  154. case CONFIG_SECTION:
  155. case CONFIG_ITEM_FLOAT:
  156. default:
  157. break;
  158. }
  159. PrintOption(item, item->psz_name, "", args);
  160. }
  161. static void PrintModule(const module_t *mod)
  162. {
  163. const char *name = mod->pp_shortcuts[0];
  164. if (!strcmp(name, "main"))
  165. return;
  166. if (mod->psz_capability)
  167. capabilities.insert(mpair(mod->psz_capability, name));
  168. module_config_t *max = &mod->p_config[mod->i_config_items];
  169. for (module_config_t *cfg = mod->p_config; cfg && cfg < max; cfg++)
  170. if (cfg->i_type == CONFIG_SUBCATEGORY)
  171. categories.insert(mcpair(cfg->value.i, name));
  172. if (!mod->parent)
  173. printf("%s ", name);
  174. }
  175. static void ParseModule(const module_t *mod)
  176. {
  177. if (mod->parent)
  178. return;
  179. module_config_t *max = mod->p_config + mod->confsize;
  180. for (module_config_t *cfg = mod->p_config; cfg && cfg < max; cfg++)
  181. if (CONFIG_ITEM(cfg->i_type))
  182. ParseOption(cfg);
  183. }
  184. int main(int argc, const char **argv)
  185. {
  186. libvlc_instance_t *libvlc = libvlc_new(argc, argv);
  187. if (!libvlc)
  188. return 1;
  189. size_t modules = 0;
  190. module_t **mod_list;
  191. mod_list = module_list_get(&modules);
  192. if (!mod_list || modules == 0)
  193. return 2;
  194. module_t **max = &mod_list[modules];
  195. puts("#compdef vlc cvlc rvlc svlc mvlc qvlc nvlc\n"
  196. "#This file is autogenerated by zsh.cpp"
  197. "typeset -A opt_args"
  198. "local context state line ret=1"
  199. "local modules\n");
  200. printf("vlc_modules=\"");
  201. for (module_t **mod = mod_list; mod < max; mod++)
  202. PrintModule(*mod);
  203. puts("\"\n");
  204. puts("_arguments -S -s \\");
  205. for (module_t **mod = mod_list; mod < max; mod++)
  206. ParseModule(*mod);
  207. puts(" \"(--module)-p[print help on module]:print help on module:($vlc_modules)\"\\");
  208. puts(" \"(-p)--module[print help on module]:print help on module:($vlc_modules)\"\\");
  209. puts(" \"(--help)-h[print help]\"\\");
  210. puts(" \"(-h)--help[print help]\"\\");
  211. puts(" \"(--longhelp)-H[print detailed help]\"\\");
  212. puts(" \"(-H)--longhelp[print detailed help]\"\\");
  213. puts(" \"(--list)-l[print a list of available modules]\"\\");
  214. puts(" \"(-l)--list[print a list of available modules]\"\\");
  215. puts(" \"--reset-config[reset the current config to the default values]\"\\");
  216. puts(" \"--config[use alternate config file]\"\\");
  217. puts(" \"--reset-plugins-cache[resets the current plugins cache]\"\\");
  218. puts(" \"--version[print version information]\"\\");
  219. puts(" \"*:Playlist item:->mrl\" && ret=0\n");
  220. puts("case $state in");
  221. puts(" mrl)");
  222. puts(" _alternative 'files:file:_files' 'urls:URL:_urls' && ret=0");
  223. puts(" ;;");
  224. puts("esac\n");
  225. puts("return ret");
  226. module_list_free(mod_list);
  227. libvlc_release(libvlc);
  228. return 0;
  229. }