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, "=main" );
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 = "";
355 char psz_buffer[10000]; // XXX
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)
382 bra = OPTION_VALUE_SEP "{";
384 psz_buffer[0] = '\0';
386 for (unsigned i = 0; i < item->list_count; i++)
389 strcat(psz_buffer, ",");
390 strcat(psz_buffer, item->list.psz[i]);
396 case CONFIG_ITEM_INTEGER:
398 if (item->min.i != 0 || item->max.i != 0)
400 sprintf (psz_buffer, "%s [%"PRId64" .. %"PRId64"]",
401 type, item->min.i, item->max.i);
404 if (item->list_count > 0)
406 bra = OPTION_VALUE_SEP "{";
408 psz_buffer[0] = '\0';
410 for (unsigned i = 0; i < item->list_count; i++)
413 strcat(psz_buffer, ", ");
414 sprintf(psz_buffer + strlen(psz_buffer), "%i (%s)",
416 module_gettext(m, item->list_text[i]));
422 case CONFIG_ITEM_FLOAT:
424 if (item->min.f != 0.f || item->max.f != 0.f)
426 sprintf(psz_buffer, "%s [%f .. %f]", type,
427 item->min.f, item->max.f);
432 case CONFIG_ITEM_BOOL:
433 bra = type = ket = "";
435 suffix = item->value.i ? _(" (default enabled)")
436 : _(" (default disabled)");
442 print_section(m, section, color, desc);
444 /* Add short option if any */
446 if (item->i_short != '\0')
447 sprintf(shortopt, "-%c,", item->i_short);
449 strcpy(shortopt, " ");
451 if (CONFIG_CLASS(item->i_type) == CONFIG_ITEM_BOOL)
452 printf(color ? WHITE" %s --%s" "%s%s%s%s%s "GRAY
453 : " %s --%s%s%s%s%s%s ", shortopt, item->psz_name,
454 prefix, item->psz_name, bra, type, ket);
456 printf(color ? WHITE" %s --%s"YELLOW"%s%s%s%s%s "GRAY
457 : " %s --%s%s%s%s%s%s ", shortopt, item->psz_name,
458 "", "", /* XXX */ bra, type, ket);
460 /* Wrap description */
461 int offset = PADDING_SPACES - strlen(item->psz_name)
462 - strlen(bra) - vlc_swidth(type) - strlen(ket) - 1;
463 if (CONFIG_CLASS(item->i_type) == CONFIG_ITEM_BOOL)
464 offset -= strlen(item->psz_name) + vlc_swidth(prefix);
468 offset = PADDING_SPACES + LINE_START;
470 printf("%*s", offset, "");
472 sprintf(psz_buffer, "%s%s", module_gettext(m, item->psz_text), suffix);
473 print_desc(psz_buffer, PADDING_SPACES + LINE_START, color);
475 if (desc && (item->psz_longtext != NULL && item->psz_longtext[0]))
476 { /* Wrap long description */
477 printf("%*s", LINE_START + 2, "");
478 sprintf(psz_buffer, "%s%s", module_gettext(m, item->psz_longtext),
480 print_desc(psz_buffer, LINE_START + 2, false);
484 static bool module_match(const module_t *m, const char *pattern, bool strict)
489 const char *objname = module_get_object(m);
491 if (strict ? (strcmp(objname, pattern) == 0)
492 : (strstr(objname, pattern) != NULL))
495 for (unsigned i = 0; i < m->i_shortcuts; i++)
497 const char *shortcut = m->pp_shortcuts[i];
499 if (strict ? (strcmp(shortcut, pattern) == 0)
500 : (strstr(shortcut, pattern) != NULL))
506 static bool module_show(const module_t *m, bool advanced)
508 for (size_t i = 0; i < m->confsize; i++)
510 const module_config_t *item = m->p_config + i;
512 if (!CONFIG_ITEM(item->i_type))
516 if ((!advanced) && item->b_advanced)
523 static void Usage (vlc_object_t *p_this, char const *psz_search)
525 bool b_has_advanced = false;
527 unsigned i_only_advanced = 0; /* Number of modules ignored because they
528 * only have advanced options */
530 if (psz_search != NULL && psz_search[0] == '=')
538 if (isatty(STDOUT_FILENO))
539 color = var_InheritBool(p_this, "color");
542 const bool desc = var_InheritBool(p_this, "help-verbose");
543 const bool advanced = var_InheritBool(p_this, "advanced");
545 /* List all modules */
547 module_t **list = module_list_get (&count);
549 /* Enumerate the config for each module */
550 for (size_t i = 0; i < count; i++)
552 const module_t *m = list[i];
553 const module_config_t *section = NULL;
554 const char *objname = module_get_object(m);
556 if (m->i_config_items == 0)
557 continue; /* Ignore modules without config options */
558 if (!module_match(m, psz_search, strict))
562 if (!module_show(m, advanced))
563 { /* Ignore modules with only advanced config options if requested */
568 /* Print name of module */
569 printf(color ? "\n " GREEN "%s" GRAY " (%s)\n" : "\n %s\n",
570 module_gettext(m, m->psz_longname), objname);
571 if (m->psz_help != NULL)
572 printf(color ? CYAN" %s\n"GRAY : " %s\n",
573 module_gettext(m, m->psz_help));
575 /* Print module options */
576 for (size_t i = 0; i < m->confsize; i++)
578 const module_config_t *item = m->p_config + i;
581 continue; /* Skip removed options */
582 if (item->b_advanced && !advanced)
583 { /* Skip advanced options unless requested */
584 b_has_advanced = true;
587 print_item(m, item, §ion, color, desc);
592 printf(color ? "\n" WHITE "%s" GRAY " %s\n"
593 : "\n%s %s\n", _( "Note:" ), _( "add --advanced to your "
594 "command line to see advanced options."));
595 if( i_only_advanced > 0 )
597 printf(color ? "\n" WHITE "%s" GRAY " " : "\n%s ", _( "Note:" ) );
598 printf(vlc_ngettext("%u module was not displayed because it only has "
599 "advanced options.\n", "%u modules were not displayed because "
600 "they only have advanced options.\n", i_only_advanced),
604 printf(color ? "\n" WHITE "%s" GRAY "\n" : "\n%s\n",
605 _("No matching module found. Use --list or "
606 "--list-verbose to list available modules."));
608 /* Release the module list */
609 module_list_free (list);
612 /*****************************************************************************
613 * ListModules: list the available modules with their description
614 *****************************************************************************
615 * Print a list of all available modules (builtins and plugins) and a short
616 * description for each one.
617 *****************************************************************************/
618 static void ListModules (vlc_object_t *p_this, bool b_verbose)
624 if (isatty(STDOUT_FILENO))
625 color = var_InheritBool(p_this, "color");
628 /* List all modules */
630 module_t **list = module_list_get (&count);
632 /* Enumerate each module */
633 for (size_t j = 0; j < count; j++)
635 module_t *p_parser = list[j];
636 const char *objname = module_get_object (p_parser);
637 printf(color ? GREEN" %-22s "WHITE"%s\n"GRAY : " %-22s %s\n",
638 objname, module_gettext(p_parser, p_parser->psz_longname));
642 char *const *pp_shortcuts = p_parser->pp_shortcuts;
643 for( unsigned i = 0; i < p_parser->i_shortcuts; i++ )
644 if( strcmp( pp_shortcuts[i], objname ) )
645 printf(color ? CYAN" s %s\n"GRAY : " s %s\n",
647 if (p_parser->psz_capability != NULL)
648 printf(color ? MAGENTA" c %s (%d)\n"GRAY : " c %s (%d)\n",
649 p_parser->psz_capability, p_parser->i_score);
652 module_list_free (list);
656 /*****************************************************************************
657 * Version: print complete program version
658 *****************************************************************************
659 * Print complete program version and build number.
660 *****************************************************************************/
661 static void Version( void )
664 printf(_("VLC version %s (%s)\n"), VERSION_MESSAGE, psz_vlc_changeset);
665 printf(_("Compiled by %s on %s (%s)\n"), VLC_CompileBy(),
666 VLC_CompileHost(), __DATE__" "__TIME__ );
667 printf(_("Compiler: %s\n"), VLC_Compiler());
668 fputs(LICENSE_MSG, stdout);
672 #if defined( _WIN32 ) && !VLC_WINSTORE_APP
673 /*****************************************************************************
674 * ShowConsole: On Win32, create an output console for debug messages
675 *****************************************************************************
676 * This function is useful only on Win32.
677 *****************************************************************************/
678 static void ShowConsole( void )
680 if( getenv( "PWD" ) ) return; /* Cygwin shell or Wine */
682 if( !AllocConsole() ) return;
684 /* Use the ANSI code page (e.g. Windows-1252) as expected by the LibVLC
685 * Unicode/locale subsystem. By default, we have the obsolecent OEM code
686 * page (e.g. CP437 or CP850). */
687 SetConsoleOutputCP (GetACP ());
688 SetConsoleTitle (TEXT("VLC media player version "PACKAGE_VERSION));
690 freopen( "CONOUT$", "w", stderr );
691 freopen( "CONIN$", "r", stdin );
693 if( freopen( "vlc-help.txt", "wt", stdout ) != NULL )
695 fputs( "\xEF\xBB\xBF", stdout );
696 fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
699 freopen( "CONOUT$", "w", stdout );
702 /*****************************************************************************
703 * PauseConsole: On Win32, wait for a key press before closing the console
704 *****************************************************************************
705 * This function is useful only on Win32.
706 *****************************************************************************/
707 static void PauseConsole( void )
709 if( getenv( "PWD" ) ) return; /* Cygwin shell or Wine */
711 utf8_fprintf( stderr, _("\nPress the RETURN key to continue...\n") );