Commit c019812e authored by Sam Hocevar's avatar Sam Hocevar

* ./src/libvlc.c: the --help output is now word-wrapped (Closes: #142).

  * ./src/libvlc.c: removed unnecessary malloc() calls.
  * ./src/libvlc.c: used {a,b,c} for lists; [a|b|c] is for optional args.
parent 44f34d7d
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* libvlc.c: main libvlc source * libvlc.c: main libvlc source
***************************************************************************** *****************************************************************************
* Copyright (C) 1998-2002 VideoLAN * Copyright (C) 1998-2002 VideoLAN
* $Id: libvlc.c,v 1.63 2003/02/06 23:59:40 sam Exp $ * $Id: libvlc.c,v 1.64 2003/02/14 18:22:23 sam Exp $
* *
* Authors: Vincent Seguin <seguin@via.ecp.fr> * Authors: Vincent Seguin <seguin@via.ecp.fr>
* Samuel Hocevar <sam@zoy.org> * Samuel Hocevar <sam@zoy.org>
...@@ -97,6 +97,7 @@ static void Version ( void ); ...@@ -97,6 +97,7 @@ static void Version ( void );
#ifdef WIN32 #ifdef WIN32
static void ShowConsole ( void ); static void ShowConsole ( void );
#endif #endif
static int ConsoleWidth ( void );
/***************************************************************************** /*****************************************************************************
* VLC_Version: return the libvlc version. * VLC_Version: return the libvlc version.
...@@ -417,7 +418,7 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] ) ...@@ -417,7 +418,7 @@ int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
*/ */
msg_Flush( p_vlc ); msg_Flush( p_vlc );
/* p_vlc inititalization. FIXME ? */ /* p_vlc initialization. FIXME ? */
p_vlc->i_desync = config_GetInt( p_vlc, "desync" ) * (mtime_t)1000; p_vlc->i_desync = config_GetInt( p_vlc, "desync" ) * (mtime_t)1000;
#if defined( __i386__ ) #if defined( __i386__ )
...@@ -1055,14 +1056,15 @@ static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] ) ...@@ -1055,14 +1056,15 @@ static int GetFilenames( vlc_t *p_vlc, int i_argc, char *ppsz_argv[] )
*****************************************************************************/ *****************************************************************************/
static void Usage( vlc_t *p_this, char const *psz_module_name ) static void Usage( vlc_t *p_this, char const *psz_module_name )
{ {
#define FORMAT_STRING " --%s%s%s%s%s%s%s %s%s\n" #define FORMAT_STRING " %s --%s%s%s%s%s%s%s "
/* option name -------------' | | | | | | /* short option ------' | | | | | | |
* <bra --------------------------' | | | | | * option name ------------' | | | | | |
* option type or "" ---------------' | | | | * <bra -------------------------' | | | | |
* ket> ------------------------------' | | | * option type or "" --------------' | | | |
* padding spaces ----------------------' | | * ket> -----------------------------' | | |
* comment --------------------------------' | * padding spaces ---------------------' | |
* comment suffix ---------------------------' * comment -------------------------------' |
* comment suffix --------------------------'
* *
* The purpose of having bra and ket is that we might i18n them as well. * The purpose of having bra and ket is that we might i18n them as well.
*/ */
...@@ -1073,7 +1075,10 @@ static void Usage( vlc_t *p_this, char const *psz_module_name ) ...@@ -1073,7 +1075,10 @@ static void Usage( vlc_t *p_this, char const *psz_module_name )
module_config_t *p_item; module_config_t *p_item;
char psz_spaces[PADDING_SPACES+LINE_START+1]; char psz_spaces[PADDING_SPACES+LINE_START+1];
char psz_format[sizeof(FORMAT_STRING)]; char psz_format[sizeof(FORMAT_STRING)];
char psz_buffer[1000];
char psz_short[4];
int i_index; int i_index;
int i_width = ConsoleWidth() - (PADDING_SPACES+LINE_START+1);
memset( psz_spaces, ' ', PADDING_SPACES+LINE_START ); memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
psz_spaces[PADDING_SPACES+LINE_START] = '\0'; psz_spaces[PADDING_SPACES+LINE_START] = '\0';
...@@ -1113,6 +1118,7 @@ static void Usage( vlc_t *p_this, char const *psz_module_name ) ...@@ -1113,6 +1118,7 @@ static void Usage( vlc_t *p_this, char const *psz_module_name )
p_item->i_type != CONFIG_HINT_END; p_item->i_type != CONFIG_HINT_END;
p_item++ ) p_item++ )
{ {
char *psz_text;
char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL; char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
char *psz_suf = "", *psz_prefix = NULL; char *psz_suf = "", *psz_prefix = NULL;
int i; int i;
...@@ -1134,16 +1140,15 @@ static void Usage( vlc_t *p_this, char const *psz_module_name ) ...@@ -1134,16 +1140,15 @@ static void Usage( vlc_t *p_this, char const *psz_module_name )
} }
else else
{ {
psz_bra = " ["; psz_bra = " {";
psz_type = malloc( 1000 ); psz_type = psz_buffer;
memset( psz_type, 0, 1000 ); psz_type[0] = '\0';
for( i=0; p_item->ppsz_list[i]; i++ ) for( i = 0; p_item->ppsz_list[i]; i++ )
{ {
if( i ) strcat( psz_type, "," );
strcat( psz_type, p_item->ppsz_list[i] ); strcat( psz_type, p_item->ppsz_list[i] );
strcat( psz_type, "|" );
} }
psz_type[ strlen( psz_type ) - 1 ] = '\0'; psz_ket = "}";
psz_ket = "]";
break; break;
} }
case CONFIG_ITEM_INTEGER: case CONFIG_ITEM_INTEGER:
...@@ -1162,82 +1167,108 @@ static void Usage( vlc_t *p_this, char const *psz_module_name ) ...@@ -1162,82 +1167,108 @@ static void Usage( vlc_t *p_this, char const *psz_module_name )
break; break;
} }
if( !psz_type )
{
continue;
}
/* Add short option if any */ /* Add short option if any */
if( p_item->i_short ) if( p_item->i_short )
{ {
psz_format[2] = '-'; sprintf( psz_short, "-%c,", p_item->i_short );
psz_format[3] = p_item->i_short;
psz_format[4] = ',';
} }
else else
{ {
psz_format[2] = ' '; strcpy( psz_short, " " );
psz_format[3] = ' ';
psz_format[4] = ' ';
} }
if( psz_type )
{
i = PADDING_SPACES - strlen( p_item->psz_name ) i = PADDING_SPACES - strlen( p_item->psz_name )
- strlen( psz_bra ) - strlen( psz_type ) - strlen( psz_bra ) - strlen( psz_type )
- strlen( psz_ket ) - 1; - strlen( psz_ket ) - 1;
if( p_item->i_type == CONFIG_ITEM_BOOL
&& !b_help_module ) if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
{ {
/* If option is of type --foo-bar, we print its counterpart /* If option is of type --foo-bar, we print its counterpart
* as --no-foo-bar, but if it is of type --foobar (without * as --no-foo-bar, but if it is of type --foobar (without
* dashes in the name) we print it as --nofoobar. Both * dashes in the name) we print it as --nofoobar. Both
* values are of course valid, only the display changes. */ * values are of course valid, only the display changes. */
vlc_bool_t b_dash = VLC_FALSE; psz_prefix = strchr( p_item->psz_name, '-' ) ? ", --no-"
psz_prefix = p_item->psz_name; : ", --no";
while( *psz_prefix ) i -= strlen( p_item->psz_name ) + strlen( psz_prefix );
{
if( *psz_prefix++ == '-' )
{
b_dash = VLC_TRUE;
break;
}
} }
if( b_dash ) if( i < 0 )
{ {
psz_prefix = ", --no-"; psz_spaces[0] = '\n';
i -= strlen( p_item->psz_name ) + strlen( ", --no-" ); i = 0;
} }
else else
{ {
psz_prefix = ", --no"; psz_spaces[i] = '\0';
i -= strlen( p_item->psz_name ) + strlen( ", --no" );
}
} }
if( i < 0 ) if( p_item->i_type == CONFIG_ITEM_BOOL && !b_help_module )
{ {
i = 0; fprintf( stdout, psz_format, psz_short, p_item->psz_name,
psz_spaces[i] = '\n'; psz_prefix, p_item->psz_name, psz_bra, psz_type,
psz_ket, psz_spaces );
} }
else else
{ {
psz_spaces[i] = '\0'; fprintf( stdout, psz_format, psz_short, p_item->psz_name,
"", "", psz_bra, psz_type, psz_ket, psz_spaces );
} }
if( p_item->i_type == CONFIG_ITEM_BOOL && psz_spaces[i] = ' ';
!b_help_module )
/* We wrap the rest of the output */
sprintf( psz_buffer, "%s%s", p_item->psz_text, psz_suf );
psz_text = psz_buffer;
while( *psz_text )
{
char *psz_parser, *psz_word;
int i_end = strlen( psz_text );
/* If the remaining text fits in a line, print it. */
if( i_end <= i_width )
{ {
fprintf( stdout, psz_format, p_item->psz_name, psz_prefix, fprintf( stdout, "%s\n", psz_text );
p_item->psz_name, psz_bra, psz_type, psz_ket, break;
psz_spaces, p_item->psz_text, psz_suf );
} }
else
/* Otherwise, eat as many words as possible */
psz_parser = psz_text;
do
{ {
fprintf( stdout, psz_format, p_item->psz_name, "", "", psz_word = psz_parser;
psz_bra, psz_type, psz_ket, psz_spaces, psz_parser = strchr( psz_word, ' ' );
p_item->psz_text, psz_suf ); /* If no space was found, we reached the end of the text
* block; otherwise, we skip the space we just found. */
psz_parser = psz_parser ? psz_parser + 1
: psz_text + i_end;
} while( psz_parser - psz_text <= i_width );
/* We cut a word in one of these cases:
* - it's the only word in the line and it's too long.
* - we used less than 80% of the width and the word we are
* going to wrap is longer than 40% of the width, and even
* if the word would have fit in the next line. */
if( psz_word == psz_text
|| ( psz_word - psz_text < 80 * i_width / 100
&& psz_parser - psz_word > 40 * i_width / 100 ) )
{
char c = psz_text[i_width];
psz_text[i_width] = '\0';
fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
psz_text += i_width;
psz_text[0] = c;
} }
psz_spaces[i] = ' '; else
if ( p_item->ppsz_list )
{ {
free( psz_type ); psz_word[-1] = '\0';
fprintf( stdout, "%s\n%s", psz_text, psz_spaces );
psz_text = psz_word;
} }
} }
} }
...@@ -1348,3 +1379,40 @@ static void ShowConsole( void ) ...@@ -1348,3 +1379,40 @@ static void ShowConsole( void )
return; return;
} }
#endif #endif
/*****************************************************************************
* ConsoleWidth: Return the console width in characters
*****************************************************************************
* We use the stty shell command to get the console width; if this fails or
* if the width is less than 80, we default to 80.
*****************************************************************************/
static int ConsoleWidth( void )
{
char buf[20], *psz_parser;
FILE *file;
int i_ret, i_width = 80;
file = popen( "stty size 2>/dev/null", "r" );
if( file )
{
i_ret = fread( buf, 1, 20, file );
if( i_ret > 0 )
{
buf[19] = '\0';
psz_parser = strchr( buf, ' ' );
if( psz_parser )
{
i_ret = atoi( psz_parser + 1 );
if( i_ret >= 80 )
{
i_width = i_ret;
}
}
}
pclose( file );
}
return i_width;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment