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 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_charset.h>
31 #include <vlc_modules.h>
32 #include <vlc_plugin.h>
33 #include "modules/modules.h"
34 #include "config/configuration.h"
37 #if defined( WIN32 ) && !defined( UNDER_CE )
38 static void ShowConsole (void);
39 static void PauseConsole (void);
41 # define ShowConsole() (void)0
42 # define PauseConsole() (void)0
46 static void Help (vlc_object_t *, const char *);
47 static void Usage (vlc_object_t *, const char *);
48 static void Version (void);
49 static void ListModules (vlc_object_t *, bool);
50 static int ConsoleWidth (void);
53 * Checks for help command line options such as --help or --version.
54 * If one is found, print the corresponding text.
55 * \return true if a command line options caused some help message to be
56 * printed, false otherwise.
58 bool config_PrintHelp (vlc_object_t *obj)
62 /* Check for short help option */
63 if (var_InheritBool (obj, "help"))
69 /* Check for version option */
70 if (var_InheritBool (obj, "version"))
76 /* Check for help on modules */
77 str = var_InheritString (obj, "module");
85 /* Check for full help option */
86 if (var_InheritBool (obj, "full-help"))
88 var_Create (obj, "advanced", VLC_VAR_BOOL);
89 var_SetBool (obj, "advanced", true);
90 var_Create (obj, "help-verbose", VLC_VAR_BOOL);
91 var_SetBool (obj, "help-verbose", true);
92 Help (obj, "full-help");
96 /* Check for long help option */
97 if (var_InheritBool (obj, "longhelp"))
99 Help (obj, "longhelp");
103 /* Check for module list option */
104 if (var_InheritBool (obj, "list"))
106 ListModules (obj, false );
110 if (var_InheritBool (obj, "list-verbose"))
112 ListModules (obj, true);
119 /*****************************************************************************
120 * Help: print program help
121 *****************************************************************************
122 * Print a short inline help. Message interface is initialized at this stage.
123 *****************************************************************************/
124 static inline void print_help_on_full_help( void )
126 utf8_fprintf( stdout, "\n" );
127 utf8_fprintf( stdout, "%s\n", _("To get exhaustive help, use '-H'.") );
130 static const char vlc_usage[] = N_(
131 "Usage: %s [options] [stream] ...\n"
132 "You can specify multiple streams on the commandline.\n"
133 "They will be enqueued in the playlist.\n"
134 "The first item specified will be played first.\n"
137 " --option A global option that is set for the duration of the program.\n"
138 " -option A single letter version of a global --option.\n"
139 " :option An option that only applies to the stream directly before it\n"
140 " and that overrides previous settings.\n"
142 "Stream MRL syntax:\n"
143 " [[access][/demux]://]URL[#[title][:chapter][-[title][:chapter]]]\n"
144 " [:option=value ...]\n"
146 " Many of the global --options can also be used as MRL specific :options.\n"
147 " Multiple :option=value pairs can be specified.\n"
150 " file:///path/file Plain media file\n"
151 " http://host[:port]/file HTTP URL\n"
152 " ftp://host[:port]/file FTP URL\n"
153 " mms://host[:port]/file MMS URL\n"
154 " screen:// Screen capture\n"
155 " dvd://[device] DVD device\n"
156 " vcd://[device] VCD device\n"
157 " cdda://[device] Audio CD device\n"
158 " udp://[[<source address>]@[<bind address>][:<bind port>]]\n"
159 " UDP stream sent by a streaming server\n"
160 " vlc://pause:<seconds> Pause the playlist for a certain time\n"
161 " vlc://quit Special item to quit VLC\n"
164 static void Help (vlc_object_t *p_this, char const *psz_help_name)
168 if( psz_help_name && !strcmp( psz_help_name, "help" ) )
170 utf8_fprintf( stdout, vlc_usage, "vlc" );
171 Usage( p_this, "=help" );
172 Usage( p_this, "=main" );
173 print_help_on_full_help();
175 else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
177 utf8_fprintf( stdout, vlc_usage, "vlc" );
178 Usage( p_this, NULL );
179 print_help_on_full_help();
181 else if( psz_help_name && !strcmp( psz_help_name, "full-help" ) )
183 utf8_fprintf( stdout, vlc_usage, "vlc" );
184 Usage( p_this, NULL );
186 else if( psz_help_name )
188 Usage( p_this, psz_help_name );
194 /*****************************************************************************
195 * Usage: print module usage
196 *****************************************************************************
197 * Print a short inline help. Message interface is initialized at this stage.
198 *****************************************************************************/
199 # define COL(x) "\033[" #x ";1m"
201 # define GREEN COL(32)
202 # define YELLOW COL(33)
203 # define BLUE COL(34)
204 # define MAGENTA COL(35)
205 # define CYAN COL(36)
206 # define WHITE COL(0)
207 # define GRAY "\033[0m"
209 print_help_section( const module_t *m, const module_config_t *p_item,
210 bool b_color, bool b_description )
212 if( !p_item ) return;
215 utf8_fprintf( stdout, RED" %s:\n"GRAY,
216 module_gettext( m, p_item->psz_text ) );
217 if( b_description && p_item->psz_longtext )
218 utf8_fprintf( stdout, MAGENTA" %s\n"GRAY,
219 module_gettext( m, p_item->psz_longtext ) );
223 utf8_fprintf( stdout, " %s:\n",
224 module_gettext( m, p_item->psz_text ) );
225 if( b_description && p_item->psz_longtext )
226 utf8_fprintf( stdout, " %s\n",
227 module_gettext(m, p_item->psz_longtext ) );
231 static void Usage (vlc_object_t *p_this, char const *psz_search)
233 #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
234 /* short option ------' | | | | | | |
235 * option name ------------' | | | | | |
236 * <bra ---------------------' | | | | |
237 * option type or "" ----------' | | | |
238 * ket> -------------------------' | | |
239 * padding spaces -----------------' | |
240 * comment --------------------------' |
241 * comment suffix ---------------------'
243 * The purpose of having bra and ket is that we might i18n them as well.
246 #define COLOR_FORMAT_STRING (WHITE" %s --%s"YELLOW"%s%s%s%s%s%s "GRAY)
247 #define COLOR_FORMAT_STRING_BOOL (WHITE" %s --%s%s%s%s%s%s%s "GRAY)
250 #define PADDING_SPACES 25
252 # define OPTION_VALUE_SEP "="
254 # define OPTION_VALUE_SEP " "
256 char psz_spaces_text[PADDING_SPACES+LINE_START+1];
257 char psz_spaces_longtext[LINE_START+3];
258 char psz_format[sizeof(COLOR_FORMAT_STRING)];
259 char psz_format_bool[sizeof(COLOR_FORMAT_STRING_BOOL)];
260 char psz_buffer[10000];
262 int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
263 int i_width_description = i_width + PADDING_SPACES - 1;
264 bool b_advanced = var_InheritBool( p_this, "advanced" );
265 bool b_description = var_InheritBool( p_this, "help-verbose" );
266 bool b_description_hack;
267 bool b_color = var_InheritBool( p_this, "color" );
268 bool b_has_advanced = false;
269 bool b_found = false;
270 int i_only_advanced = 0; /* Number of modules ignored because they
271 * only have advanced options */
272 bool b_strict = psz_search && *psz_search == '=';
273 if( b_strict ) psz_search++;
275 memset( psz_spaces_text, ' ', PADDING_SPACES+LINE_START );
276 psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
277 memset( psz_spaces_longtext, ' ', LINE_START+2 );
278 psz_spaces_longtext[LINE_START+2] = '\0';
282 b_color = false; // don't put color control codes in a .txt file
286 strcpy( psz_format, COLOR_FORMAT_STRING );
287 strcpy( psz_format_bool, COLOR_FORMAT_STRING_BOOL );
291 strcpy( psz_format, FORMAT_STRING );
292 strcpy( psz_format_bool, FORMAT_STRING );
295 /* List all modules */
296 module_t **list = module_list_get (NULL);
300 /* Ugly hack to make sure that the help options always come first
303 Usage( p_this, "help" );
305 /* Enumerate the config for each module */
306 for (size_t i = 0; list[i]; i++)
309 module_t *p_parser = list[i];
310 module_config_t *p_item = NULL;
311 module_config_t *p_section = NULL;
312 module_config_t *p_end = p_parser->p_config + p_parser->confsize;
313 const char *objname = module_get_object (p_parser);
316 ( b_strict ? strcmp( objname, psz_search )
317 : !strstr( objname, psz_search ) ) )
319 char *const *pp_shortcuts = p_parser->pp_shortcuts;
321 for( i = 0; i < p_parser->i_shortcuts; i++ )
323 if( b_strict ? !strcmp( psz_search, pp_shortcuts[i] )
324 : !!strstr( pp_shortcuts[i], psz_search ) )
327 if( i == p_parser->i_shortcuts )
331 /* Ignore modules without config options */
332 if( !p_parser->i_config_items )
337 b_help_module = !strcmp( "help", objname );
338 /* Ugly hack to make sure that the help options always come first
340 if( !psz_search && b_help_module )
343 /* Ignore modules with only advanced config options if requested */
346 for( p_item = p_parser->p_config;
350 if( CONFIG_ITEM(p_item->i_type) &&
351 !p_item->b_advanced && !p_item->b_removed ) break;
354 if( p_item == p_end )
363 /* Print name of module */
364 if( strcmp( "main", objname ) )
367 utf8_fprintf( stdout, "\n " GREEN "%s" GRAY " (%s)\n",
368 module_gettext( p_parser, p_parser->psz_longname ),
371 utf8_fprintf( stdout, "\n %s\n",
372 module_gettext(p_parser, p_parser->psz_longname ) );
374 if( p_parser->psz_help )
377 utf8_fprintf( stdout, CYAN" %s\n"GRAY,
378 module_gettext( p_parser, p_parser->psz_help ) );
380 utf8_fprintf( stdout, " %s\n",
381 module_gettext( p_parser, p_parser->psz_help ) );
384 /* Print module options */
385 for( p_item = p_parser->p_config;
389 char *psz_text, *psz_spaces = psz_spaces_text;
390 const char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
391 const char *psz_suf = "", *psz_prefix = NULL;
395 /* Skip removed options */
396 if( p_item->b_removed )
400 /* Skip advanced options if requested */
401 if( p_item->b_advanced && !b_advanced )
403 b_has_advanced = true;
407 switch( CONFIG_CLASS(p_item->i_type) )
409 case 0: // hint class
410 switch( p_item->i_type )
412 case CONFIG_HINT_CATEGORY:
413 case CONFIG_HINT_USAGE:
414 if( !strcmp( "main", objname ) )
417 utf8_fprintf( stdout, GREEN "\n %s\n" GRAY,
418 module_gettext( p_parser, p_item->psz_text ) );
420 utf8_fprintf( stdout, "\n %s\n",
421 module_gettext( p_parser, p_item->psz_text ) );
423 if( b_description && p_item->psz_longtext )
426 utf8_fprintf( stdout, CYAN " %s\n" GRAY,
427 module_gettext( p_parser, p_item->psz_longtext ) );
429 utf8_fprintf( stdout, " %s\n",
430 module_gettext( p_parser, p_item->psz_longtext ) );
434 case CONFIG_HINT_SUBCATEGORY:
435 if( strcmp( "main", objname ) )
443 case CONFIG_ITEM_STRING:
444 print_help_section( p_parser, p_section, b_color,
447 psz_bra = OPTION_VALUE_SEP "<";
448 psz_type = _("string");
451 if( p_item->ppsz_list )
453 psz_bra = OPTION_VALUE_SEP "{";
454 psz_type = psz_buffer;
455 psz_buffer[0] = '\0';
456 for( i = 0; p_item->ppsz_list[i]; i++ )
458 if( i ) strcat( psz_buffer, "," );
459 strcat( psz_buffer, p_item->ppsz_list[i] );
464 case CONFIG_ITEM_INTEGER:
465 print_help_section( p_parser, p_section, b_color,
468 psz_bra = OPTION_VALUE_SEP "<";
469 psz_type = _("integer");
472 if( p_item->min.i || p_item->max.i )
474 sprintf( psz_buffer, "%s [%"PRId64" .. %"PRId64"]",
475 psz_type, p_item->min.i, p_item->max.i );
476 psz_type = psz_buffer;
481 psz_bra = OPTION_VALUE_SEP "{";
482 psz_type = psz_buffer;
483 psz_buffer[0] = '\0';
484 for( i = 0; p_item->ppsz_list_text[i]; i++ )
486 if( i ) strcat( psz_buffer, ", " );
487 sprintf( psz_buffer + strlen(psz_buffer), "%i (%s)",
489 module_gettext( p_parser, p_item->ppsz_list_text[i] ) );
494 case CONFIG_ITEM_FLOAT:
495 print_help_section( p_parser, p_section, b_color,
498 psz_bra = OPTION_VALUE_SEP "<";
499 psz_type = _("float");
501 if( p_item->min.f || p_item->max.f )
503 sprintf( psz_buffer, "%s [%f .. %f]", psz_type,
504 p_item->min.f, p_item->max.f );
505 psz_type = psz_buffer;
508 case CONFIG_ITEM_BOOL:
509 print_help_section( p_parser, p_section, b_color,
512 psz_bra = ""; psz_type = ""; psz_ket = "";
515 psz_suf = p_item->value.i ? _(" (default enabled)") :
516 _(" (default disabled)");
526 /* Add short option if any */
527 if( p_item->i_short )
529 sprintf( psz_short, "-%c,", p_item->i_short );
533 strcpy( psz_short, " " );
536 i = PADDING_SPACES - strlen( p_item->psz_name )
537 - strlen( psz_bra ) - strlen( psz_type )
538 - strlen( psz_ket ) - 1;
540 if( CONFIG_CLASS(p_item->i_type) == CONFIG_ITEM_BOOL
543 psz_prefix = ", --no-";
544 i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
549 psz_spaces[0] = '\n';
554 psz_spaces[i] = '\0';
557 if( CONFIG_CLASS(p_item->i_type) == CONFIG_ITEM_BOOL
560 utf8_fprintf( stdout, psz_format_bool, psz_short,
561 p_item->psz_name, psz_prefix, p_item->psz_name,
562 psz_bra, psz_type, psz_ket, psz_spaces );
566 utf8_fprintf( stdout, psz_format, psz_short, p_item->psz_name,
567 "", "", psz_bra, psz_type, psz_ket, psz_spaces );
572 /* We wrap the rest of the output */
573 sprintf( psz_buffer, "%s%s", module_gettext( p_parser, p_item->psz_text ),
575 b_description_hack = b_description;
578 psz_text = psz_buffer;
579 i_cur_width = b_description && !b_description_hack
580 ? i_width_description
582 if( !*psz_text ) strcpy(psz_text, " ");
585 char *psz_parser, *psz_word;
586 size_t i_end = strlen( psz_text );
588 /* If the remaining text fits in a line, print it. */
589 if( i_end <= i_cur_width )
593 if( !b_description || b_description_hack )
594 utf8_fprintf( stdout, BLUE"%s\n"GRAY, psz_text );
596 utf8_fprintf( stdout, "%s\n", psz_text );
600 utf8_fprintf( stdout, "%s\n", psz_text );
605 /* Otherwise, eat as many words as possible */
606 psz_parser = psz_text;
609 psz_word = psz_parser;
610 psz_parser = strchr( psz_word, ' ' );
611 /* If no space was found, we reached the end of the text
612 * block; otherwise, we skip the space we just found. */
613 psz_parser = psz_parser ? psz_parser + 1
616 } while( (size_t)(psz_parser - psz_text) <= i_cur_width );
618 /* We cut a word in one of these cases:
619 * - it's the only word in the line and it's too long.
620 * - we used less than 80% of the width and the word we are
621 * going to wrap is longer than 40% of the width, and even
622 * if the word would have fit in the next line. */
623 if( psz_word == psz_text
624 || ( (size_t)(psz_word - psz_text) < 80 * i_cur_width / 100
625 && (size_t)(psz_parser - psz_word) > 40 * i_cur_width / 100 ) )
627 char c = psz_text[i_cur_width];
628 psz_text[i_cur_width] = '\0';
631 if( !b_description || b_description_hack )
632 utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
633 psz_text, psz_spaces );
635 utf8_fprintf( stdout, "%s\n%s",
636 psz_text, psz_spaces );
640 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
642 psz_text += i_cur_width;
650 if( !b_description || b_description_hack )
651 utf8_fprintf( stdout, BLUE"%s\n%s"GRAY,
652 psz_text, psz_spaces );
654 utf8_fprintf( stdout, "%s\n%s",
655 psz_text, psz_spaces );
659 utf8_fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
665 if( b_description_hack && p_item->psz_longtext )
667 sprintf( psz_buffer, "%s%s",
668 module_gettext( p_parser, p_item->psz_longtext ),
670 b_description_hack = false;
671 psz_spaces = psz_spaces_longtext;
672 utf8_fprintf( stdout, "%s", psz_spaces );
681 utf8_fprintf( stdout, "\n" WHITE "%s" GRAY " %s\n", _( "Note:" ),
682 _( "add --advanced to your command line to see advanced options."));
684 utf8_fprintf( stdout, "\n%s %s\n", _( "Note:" ),
685 _( "add --advanced to your command line to see advanced options."));
688 if( i_only_advanced > 0 )
692 utf8_fprintf( stdout, "\n" WHITE "%s" GRAY " ", _( "Note:" ) );
693 utf8_fprintf( stdout, _( "%d module(s) were not displayed because they only have advanced options.\n" ), i_only_advanced );
697 utf8_fprintf( stdout, "\n%s ", _( "Note:" ) );
698 utf8_fprintf( stdout, _( "%d module(s) were not displayed because they only have advanced options.\n" ), i_only_advanced );
704 utf8_fprintf( stdout, "\n" WHITE "%s" GRAY "\n",
705 _( "No matching module found. Use --list or " \
706 "--list-verbose to list available modules." ) );
708 utf8_fprintf( stdout, "\n%s\n",
709 _( "No matching module found. Use --list or " \
710 "--list-verbose to list available modules." ) );
713 /* Release the module list */
714 module_list_free (list);
717 /*****************************************************************************
718 * ListModules: list the available modules with their description
719 *****************************************************************************
720 * Print a list of all available modules (builtins and plugins) and a short
721 * description for each one.
722 *****************************************************************************/
723 static void ListModules (vlc_object_t *p_this, bool b_verbose)
727 bool b_color = var_InheritBool( p_this, "color" );
731 b_color = false; // don't put color control codes in a .txt file
737 /* List all modules */
738 module_t **list = module_list_get (NULL);
740 /* Enumerate each module */
741 for (size_t j = 0; (p_parser = list[j]) != NULL; j++)
743 const char *objname = module_get_object (p_parser);
745 utf8_fprintf( stdout, GREEN" %-22s "WHITE"%s\n"GRAY, objname,
746 module_gettext( p_parser, p_parser->psz_longname ) );
748 utf8_fprintf( stdout, " %-22s %s\n", objname,
749 module_gettext( p_parser, p_parser->psz_longname ) );
753 char *const *pp_shortcuts = p_parser->pp_shortcuts;
754 for( unsigned i = 0; i < p_parser->i_shortcuts; i++ )
756 if( strcmp( pp_shortcuts[i], objname ) )
759 utf8_fprintf( stdout, CYAN" s %s\n"GRAY,
762 utf8_fprintf( stdout, " s %s\n",
766 if( p_parser->psz_capability )
769 utf8_fprintf( stdout, MAGENTA" c %s (%d)\n"GRAY,
770 p_parser->psz_capability,
773 utf8_fprintf( stdout, " c %s (%d)\n",
774 p_parser->psz_capability,
779 module_list_free (list);
783 /*****************************************************************************
784 * Version: print complete program version
785 *****************************************************************************
786 * Print complete program version and build number.
787 *****************************************************************************/
788 static void Version( void )
791 utf8_fprintf( stdout, _("VLC version %s (%s)\n"), VERSION_MESSAGE,
793 utf8_fprintf( stdout, _("Compiled by %s on %s (%s)\n"),
794 VLC_CompileBy(), VLC_CompileHost(), __DATE__" "__TIME__ );
795 utf8_fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
796 utf8_fprintf( stdout, "%s", LICENSE_MSG );
800 #if defined (WIN32) && !defined (UNDER_CE)
801 /*****************************************************************************
802 * ShowConsole: On Win32, create an output console for debug messages
803 *****************************************************************************
804 * This function is useful only on Win32.
805 *****************************************************************************/
806 static void ShowConsole( void )
810 if( getenv( "PWD" ) ) return; /* Cygwin shell or Wine */
813 /* Use the ANSI code page (e.g. Windows-1252) as expected by the LibVLC
814 * Unicode/locale subsystem. By default, we have the obsolecent OEM code
815 * page (e.g. CP437 or CP850). */
816 SetConsoleOutputCP (GetACP ());
817 SetConsoleTitle ("VLC media player version "PACKAGE_VERSION);
819 freopen( "CONOUT$", "w", stderr );
820 freopen( "CONIN$", "r", stdin );
822 f_help = fopen( "vlc-help.txt", "wt" );
826 freopen( "vlc-help.txt", "wt", stdout );
827 utf8_fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
829 else freopen( "CONOUT$", "w", stdout );
832 /*****************************************************************************
833 * PauseConsole: On Win32, wait for a key press before closing the console
834 *****************************************************************************
835 * This function is useful only on Win32.
836 *****************************************************************************/
837 static void PauseConsole( void )
839 if( getenv( "PWD" ) ) return; /* Cygwin shell or Wine */
841 utf8_fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
847 /*****************************************************************************
848 * ConsoleWidth: Return the console width in characters
849 *****************************************************************************
850 * We use the stty shell command to get the console width; if this fails or
851 * if the width is less than 80, we default to 80.
852 *****************************************************************************/
853 static int ConsoleWidth( void )
855 unsigned i_width = 80;
858 FILE *file = popen( "stty size 2>/dev/null", "r" );
861 if (fscanf (file, "%*u %u", &i_width) <= 0)
865 #elif !defined (UNDER_CE)
866 CONSOLE_SCREEN_BUFFER_INFO buf;
868 if (GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &buf))
869 i_width = buf.dwSize.X;