1 /*****************************************************************************
2 * help.c: command line help
3 *****************************************************************************
4 * Copyright (C) 1998-2011 VLC authors and VideoLAN
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_modules.h>
33 #include <vlc_plugin.h>
34 #include "modules/modules.h"
35 #include "config/configuration.h"
39 # include <vlc_charset.h>
40 # define wcwidth(cp) (cp, 1) /* LOL */
44 # include <sys/ioctl.h>
47 #if defined( _WIN32 ) && !VLC_WINSTORE_APP
48 static void ShowConsole (void);
49 static void PauseConsole (void);
51 # define ShowConsole() (void)0
52 # define PauseConsole() (void)0
55 static void Help (vlc_object_t *, const char *);
56 static void Usage (vlc_object_t *, const char *);
57 static void Version (void);
58 static void ListModules (vlc_object_t *, bool);
61 * Returns the console width or a best guess.
63 static unsigned ConsoleWidth(void)
68 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0)
74 if (ioctl(STDOUT_FILENO, WIOCGETD, &uw) == 0)
75 return uw.uw_height / uw.uw_vs;
77 #if defined (_WIN32) && !VLC_WINSTORE_APP
78 CONSOLE_SCREEN_BUFFER_INFO buf;
80 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &buf))
87 * Checks for help command line options such as --help or --version.
88 * If one is found, print the corresponding text.
89 * \return true if a command line options caused some help message to be
90 * printed, false otherwise.
92 bool config_PrintHelp (vlc_object_t *obj)
96 /* Check for short help option */
97 if (var_InheritBool (obj, "help"))
103 /* Check for version option */
104 if (var_InheritBool (obj, "version"))
110 /* Check for help on modules */
111 str = var_InheritString (obj, "module");
119 /* Check for full help option */
120 if (var_InheritBool (obj, "full-help"))
122 var_Create (obj, "advanced", VLC_VAR_BOOL);
123 var_SetBool (obj, "advanced", true);
124 var_Create (obj, "help-verbose", VLC_VAR_BOOL);
125 var_SetBool (obj, "help-verbose", true);
126 Help (obj, "full-help");
130 /* Check for long help option */
131 if (var_InheritBool (obj, "longhelp"))
133 Help (obj, "longhelp");
137 /* Check for module list option */
138 if (var_InheritBool (obj, "list"))
140 ListModules (obj, false );
144 if (var_InheritBool (obj, "list-verbose"))
146 ListModules (obj, true);
153 /*****************************************************************************
154 * Help: print program help
155 *****************************************************************************
156 * Print a short inline help. Message interface is initialized at this stage.
157 *****************************************************************************/
158 static inline void print_help_on_full_help( void )
161 puts(_("To get exhaustive help, use '-H'."));
164 static const char vlc_usage[] = N_(
165 "Usage: %s [options] [stream] ...\n"
166 "You can specify multiple streams on the commandline.\n"
167 "They will be enqueued in the playlist.\n"
168 "The first item specified will be played first.\n"
171 " --option A global option that is set for the duration of the program.\n"
172 " -option A single letter version of a global --option.\n"
173 " :option An option that only applies to the stream directly before it\n"
174 " and that overrides previous settings.\n"
176 "Stream MRL syntax:\n"
177 " [[access][/demux]://]URL[#[title][:chapter][-[title][:chapter]]]\n"
178 " [:option=value ...]\n"
180 " Many of the global --options can also be used as MRL specific :options.\n"
181 " Multiple :option=value pairs can be specified.\n"
184 " file:///path/file Plain media file\n"
185 " http://host[:port]/file HTTP URL\n"
186 " ftp://host[:port]/file FTP URL\n"
187 " mms://host[:port]/file MMS URL\n"
188 " screen:// Screen capture\n"
189 " dvd://[device] DVD device\n"
190 " vcd://[device] VCD device\n"
191 " cdda://[device] Audio CD device\n"
192 " udp://[[<source address>]@[<bind address>][:<bind port>]]\n"
193 " UDP stream sent by a streaming server\n"
194 " vlc://pause:<seconds> Pause the playlist for a certain time\n"
195 " vlc://quit Special item to quit VLC\n"
198 static void Help (vlc_object_t *p_this, char const *psz_help_name)
202 if( psz_help_name && !strcmp( psz_help_name, "help" ) )
204 printf(_(vlc_usage), "vlc");
205 Usage( p_this, "=core" );
206 print_help_on_full_help();
208 else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
210 printf(_(vlc_usage), "vlc");
211 Usage( p_this, NULL );
212 print_help_on_full_help();
214 else if( psz_help_name && !strcmp( psz_help_name, "full-help" ) )
216 printf(_(vlc_usage), "vlc");
217 Usage( p_this, NULL );
219 else if( psz_help_name )
221 Usage( p_this, psz_help_name );
227 /*****************************************************************************
228 * Usage: print module usage
229 *****************************************************************************
230 * Print a short inline help. Message interface is initialized at this stage.
231 *****************************************************************************/
232 # define COL(x) "\033[" #x ";1m"
234 # define GREEN COL(32)
235 # define YELLOW COL(33)
236 # define BLUE COL(34)
237 # define MAGENTA COL(35)
238 # define CYAN COL(36)
239 # define WHITE COL(0)
240 # define GRAY "\033[0m"
241 # define LINE_START 8
242 # define PADDING_SPACES 25
244 static void print_section(const module_t *m, const module_config_t **sect,
245 bool color, bool desc)
247 const module_config_t *item = *sect;
253 printf(color ? RED" %s:\n"GRAY : " %s:\n",
254 module_gettext(m, item->psz_text));
255 if (desc && item->psz_longtext != NULL)
256 printf(color ? MAGENTA" %s\n"GRAY : " %s\n",
257 module_gettext(m, item->psz_longtext));
260 static void print_desc(const char *str, unsigned margin, bool color)
262 unsigned width = ConsoleWidth() - margin;
267 const char *word = str;
268 int wordlen = 0, wordwidth = 0;
275 size_t charlen = vlc_towc(str, &cp);
276 if (unlikely(charlen == (size_t)-1))
279 int charwidth = wcwidth(cp);
289 putchar(' '); /* insert space */
292 fwrite(word, 1, wordlen, stdout); /* write complete word */
301 wordwidth += charwidth;
308 { /* overflow (word wider than line) */
309 fwrite(word, 1, wordlen - charlen, stdout);
310 word = str - charlen;
312 wordwidth = charwidth;
314 printf("\n%*s", margin, ""); /* new line */
322 printf(color ? "%s\n"GRAY : "%s\n", word);
325 static int vlc_swidth(const char *str)
327 for (int total = 0;;)
330 size_t charlen = vlc_towc(str, &cp);
334 if (charlen == (size_t)-1)
345 static void print_item(const module_t *m, const module_config_t *item,
346 const module_config_t **section, bool color, bool desc)
349 # define OPTION_VALUE_SEP " "
351 # define OPTION_VALUE_SEP "="
353 const char *bra = OPTION_VALUE_SEP "<", *type, *ket = ">";
354 const char *prefix = NULL, *suffix = NULL;
355 char *typebuf = NULL;
357 switch (CONFIG_CLASS(item->i_type))
359 case 0: // hint class
360 switch (item->i_type)
362 case CONFIG_HINT_CATEGORY:
363 case CONFIG_HINT_USAGE:
364 printf(color ? GREEN "\n %s\n" GRAY : "\n %s\n",
365 module_gettext(m, item->psz_text));
367 if (desc && item->psz_longtext != NULL)
368 printf(color ? CYAN " %s\n" GRAY : " %s\n",
369 module_gettext(m, item->psz_longtext));
378 case CONFIG_ITEM_STRING:
380 if (item->list_count > 0)
384 for (unsigned i = 0; i < item->list_count; i++)
385 len += strlen(item->list.psz[i]) + 1;
387 typebuf = malloc(len);
391 bra = OPTION_VALUE_SEP "{";
396 for (unsigned i = 0; i < item->list_count; i++)
399 strcat(typebuf, ",");
400 strcat(typebuf, item->list.psz[i]);
405 case CONFIG_ITEM_INTEGER:
408 if (item->list_count > 0)
412 for (unsigned i = 0; i < item->list_count; i++)
413 len += strlen(item->list_text[i]) + 4 * sizeof (int) + 5;
415 typebuf = malloc(len);
419 bra = OPTION_VALUE_SEP "{";
424 for (unsigned i = 0; i < item->list_count; i++)
427 strcat(typebuf, ", ");
428 sprintf(typebuf + strlen(typebuf), "%i (%s)",
430 module_gettext(m, item->list_text[i]));
433 else if (item->min.i != 0 || item->max.i != 0)
435 if (asprintf(&typebuf, "%s [%"PRId64" .. %"PRId64"]",
436 type, item->min.i, item->max.i) >= 0)
443 case CONFIG_ITEM_FLOAT:
445 if (item->min.f != 0.f || item->max.f != 0.f)
447 if (asprintf(&typebuf, "%s [%f .. %f]", type,
448 item->min.f, item->max.f) >= 0)
455 case CONFIG_ITEM_BOOL:
456 bra = type = ket = "";
458 suffix = item->value.i ? _("(default enabled)")
459 : _("(default disabled)");
465 print_section(m, section, color, desc);
467 /* Add short option if any */
469 if (item->i_short != '\0')
470 sprintf(shortopt, "-%c,", item->i_short);
472 strcpy(shortopt, " ");
474 if (CONFIG_CLASS(item->i_type) == CONFIG_ITEM_BOOL)
475 printf(color ? WHITE" %s --%s" "%s%s%s%s%s "GRAY
476 : " %s --%s%s%s%s%s%s ", shortopt, item->psz_name,
477 prefix, item->psz_name, bra, type, ket);
479 printf(color ? WHITE" %s --%s"YELLOW"%s%s%s%s%s "GRAY
480 : " %s --%s%s%s%s%s%s ", shortopt, item->psz_name,
481 "", "", /* XXX */ bra, type, ket);
483 /* Wrap description */
484 int offset = PADDING_SPACES - strlen(item->psz_name)
485 - strlen(bra) - vlc_swidth(type) - strlen(ket) - 1;
486 if (CONFIG_CLASS(item->i_type) == CONFIG_ITEM_BOOL)
487 offset -= strlen(item->psz_name) + vlc_swidth(prefix);
491 offset = PADDING_SPACES + LINE_START;
494 printf("%*s", offset, "");
495 print_desc(module_gettext(m, item->psz_longtext),
496 PADDING_SPACES + LINE_START, color);
500 printf("%*s", offset, "");
501 print_desc(suffix, PADDING_SPACES + LINE_START, color);
504 if (desc && (item->psz_longtext != NULL && item->psz_longtext[0]))
505 { /* Wrap long description */
506 printf("%*s", LINE_START + 2, "");
507 print_desc(module_gettext(m, item->psz_longtext),
508 LINE_START + 2, false);
514 static bool module_match(const module_t *m, const char *pattern, bool strict)
519 const char *objname = module_get_object(m);
521 if (strict ? (strcmp(objname, pattern) == 0)
522 : (strstr(objname, pattern) != NULL))
525 for (unsigned i = 0; i < m->i_shortcuts; i++)
527 const char *shortcut = m->pp_shortcuts[i];
529 if (strict ? (strcmp(shortcut, pattern) == 0)
530 : (strstr(shortcut, pattern) != NULL))
536 static bool module_show(const module_t *m, bool advanced)
538 for (size_t i = 0; i < m->confsize; i++)
540 const module_config_t *item = m->p_config + i;
542 if (!CONFIG_ITEM(item->i_type))
546 if ((!advanced) && item->b_advanced)
553 static void Usage (vlc_object_t *p_this, char const *psz_search)
555 bool b_has_advanced = false;
557 unsigned i_only_advanced = 0; /* Number of modules ignored because they
558 * only have advanced options */
560 if (psz_search != NULL && psz_search[0] == '=')
568 if (isatty(STDOUT_FILENO))
569 color = var_InheritBool(p_this, "color");
572 const bool desc = var_InheritBool(p_this, "help-verbose");
573 const bool advanced = var_InheritBool(p_this, "advanced");
575 /* List all modules */
577 module_t **list = module_list_get (&count);
579 /* Enumerate the config for each module */
580 for (size_t i = 0; i < count; i++)
582 const module_t *m = list[i];
583 const module_config_t *section = NULL;
584 const char *objname = module_get_object(m);
586 if (m->i_config_items == 0)
587 continue; /* Ignore modules without config options */
588 if (!module_match(m, psz_search, strict))
592 if (!module_show(m, advanced))
593 { /* Ignore modules with only advanced config options if requested */
598 /* Print name of module */
599 printf(color ? "\n " GREEN "%s" GRAY " (%s)\n" : "\n %s (%s)\n",
600 module_gettext(m, m->psz_longname), objname);
601 if (m->psz_help != NULL)
602 printf(color ? CYAN" %s\n"GRAY : " %s\n",
603 module_gettext(m, m->psz_help));
605 /* Print module options */
606 for (size_t j = 0; j < m->confsize; j++)
608 const module_config_t *item = m->p_config + j;
611 continue; /* Skip removed options */
612 if (item->b_advanced && !advanced)
613 { /* Skip advanced options unless requested */
614 b_has_advanced = true;
617 print_item(m, item, §ion, color, desc);
622 printf(color ? "\n" WHITE "%s" GRAY " %s\n"
623 : "\n%s %s\n", _( "Note:" ), _( "add --advanced to your "
624 "command line to see advanced options."));
625 if( i_only_advanced > 0 )
627 printf(color ? "\n" WHITE "%s" GRAY " " : "\n%s ", _( "Note:" ) );
628 printf(vlc_ngettext("%u module was not displayed because it only has "
629 "advanced options.\n", "%u modules were not displayed because "
630 "they only have advanced options.\n", i_only_advanced),
634 printf(color ? "\n" WHITE "%s" GRAY "\n" : "\n%s\n",
635 _("No matching module found. Use --list or "
636 "--list-verbose to list available modules."));
638 /* Release the module list */
639 module_list_free (list);
642 /*****************************************************************************
643 * ListModules: list the available modules with their description
644 *****************************************************************************
645 * Print a list of all available modules (builtins and plugins) and a short
646 * description for each one.
647 *****************************************************************************/
648 static void ListModules (vlc_object_t *p_this, bool b_verbose)
654 if (isatty(STDOUT_FILENO))
655 color = var_InheritBool(p_this, "color");
660 /* List all modules */
662 module_t **list = module_list_get (&count);
664 /* Enumerate each module */
665 for (size_t j = 0; j < count; j++)
667 module_t *p_parser = list[j];
668 const char *objname = module_get_object (p_parser);
669 printf(color ? GREEN" %-22s "WHITE"%s\n"GRAY : " %-22s %s\n",
670 objname, module_gettext(p_parser, p_parser->psz_longname));
674 char *const *pp_shortcuts = p_parser->pp_shortcuts;
675 for( unsigned i = 0; i < p_parser->i_shortcuts; i++ )
676 if( strcmp( pp_shortcuts[i], objname ) )
677 printf(color ? CYAN" s %s\n"GRAY : " s %s\n",
679 if (p_parser->psz_capability != NULL)
680 printf(color ? MAGENTA" c %s (%d)\n"GRAY : " c %s (%d)\n",
681 p_parser->psz_capability, p_parser->i_score);
684 module_list_free (list);
688 /*****************************************************************************
689 * Version: print complete program version
690 *****************************************************************************
691 * Print complete program version and build number.
692 *****************************************************************************/
693 static void Version( void )
696 printf(_("VLC version %s (%s)\n"), VERSION_MESSAGE, psz_vlc_changeset);
697 printf(_("Compiled by %s on %s (%s)\n"), VLC_CompileBy(),
698 VLC_CompileHost(), __DATE__" "__TIME__ );
699 printf(_("Compiler: %s\n"), VLC_Compiler());
700 fputs(LICENSE_MSG, stdout);
704 #if defined( _WIN32 ) && !VLC_WINSTORE_APP
705 /*****************************************************************************
706 * ShowConsole: On Win32, create an output console for debug messages
707 *****************************************************************************
708 * This function is useful only on Win32.
709 *****************************************************************************/
710 static void ShowConsole( void )
712 if( getenv( "PWD" ) ) return; /* Cygwin shell or Wine */
714 if( !AllocConsole() ) return;
716 /* Use the ANSI code page (e.g. Windows-1252) as expected by the LibVLC
717 * Unicode/locale subsystem. By default, we have the obsolecent OEM code
718 * page (e.g. CP437 or CP850). */
719 SetConsoleOutputCP (GetACP ());
720 SetConsoleTitle (TEXT("VLC media player version ") TEXT(PACKAGE_VERSION));
722 freopen( "CONOUT$", "w", stderr );
723 freopen( "CONIN$", "r", stdin );
725 if( freopen( "vlc-help.txt", "wt", stdout ) != NULL )
727 fputs( "\xEF\xBB\xBF", stdout );
728 fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
731 freopen( "CONOUT$", "w", stdout );
734 /*****************************************************************************
735 * PauseConsole: On Win32, wait for a key press before closing the console
736 *****************************************************************************
737 * This function is useful only on Win32.
738 *****************************************************************************/
739 static void PauseConsole( void )
741 if( getenv( "PWD" ) ) return; /* Cygwin shell or Wine */
743 utf8_fprintf( stderr, _("\nPress the RETURN key to continue...\n") );