Large set of changes to migrate sb_out and sb_err to wcstring

This commit is contained in:
ridiculousfish 2012-02-22 10:51:06 -08:00
parent 917b4ff8bc
commit 62bd43f17f
16 changed files with 537 additions and 542 deletions

File diff suppressed because it is too large Load Diff

View File

@ -92,12 +92,16 @@ enum
/** /**
Stringbuffer used to represent standard output Stringbuffer used to represent standard output
*/ */
extern string_buffer_t *sb_out; //extern string_buffer_t *sb_out;
/** /**
Stringbuffer used to represent standard error Stringbuffer used to represent standard error
*/ */
extern string_buffer_t *sb_err; //extern string_buffer_t *sb_err;
/** Get the string used to represent stdout and stderr */
const wcstring &get_stdout_buffer();
const wcstring &get_stderr_buffer();
/** /**
Kludge. Tells builtins if output is to screen Kludge. Tells builtins if output is to screen

View File

@ -179,8 +179,7 @@ static void write_part( const wchar_t *begin,
} }
} }
sb_append( sb_out, stdout_buffer.append((const wchar_t *)out.buff);
(wchar_t *)out.buff );
free( buff ); free( buff );
tok_destroy( &tok ); tok_destroy( &tok );
@ -200,8 +199,8 @@ static void write_part( const wchar_t *begin,
// debug( 0, L"woot2 %ls -> %ls", buff, esc ); // debug( 0, L"woot2 %ls -> %ls", buff, esc );
sb_append( sb_out, esc ); stdout_buffer.append(esc);
sb_append( sb_out, L"\n" ); stdout_buffer.append(L"\n");
free( esc ); free( esc );
free( buff ); free( buff );
@ -255,11 +254,9 @@ static int builtin_commandline( parser_t &parser, wchar_t **argv )
return 1; return 1;
} }
sb_append( sb_err, stderr_buffer.append(argv[0]);
argv[0], stderr_buffer.append(L": Can not set commandline in non-interactive mode\n");
L": Can not set commandline in non-interactive mode\n", builtin_print_help( parser, argv[0], stderr_buffer );
NULL );
builtin_print_help( parser, argv[0], sb_err );
return 1; return 1;
} }
@ -351,11 +348,11 @@ static int builtin_commandline( parser_t &parser, wchar_t **argv )
case 0: case 0:
if(long_options[opt_index].flag != 0) if(long_options[opt_index].flag != 0)
break; break;
sb_printf( sb_err, append_format( stderr_buffer,
BUILTIN_ERR_UNKNOWN, BUILTIN_ERR_UNKNOWN,
argv[0], argv[0],
long_options[opt_index].name ); long_options[opt_index].name );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
@ -418,7 +415,7 @@ static int builtin_commandline( parser_t &parser, wchar_t **argv )
break; break;
case 'h': case 'h':
builtin_print_help( parser, argv[0], sb_out ); builtin_print_help( parser, argv[0], stdout_buffer );
return 0; return 0;
case L'?': case L'?':
@ -436,22 +433,22 @@ static int builtin_commandline( parser_t &parser, wchar_t **argv )
*/ */
if( buffer_part || cut_at_cursor || append_mode || tokenize || cursor_mode || line_mode || search_mode ) if( buffer_part || cut_at_cursor || append_mode || tokenize || cursor_mode || line_mode || search_mode )
{ {
sb_printf(sb_err, append_format(stderr_buffer,
BUILTIN_ERR_COMBO, BUILTIN_ERR_COMBO,
argv[0] ); argv[0] );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
if( argc == woptind ) if( argc == woptind )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_MISSING, BUILTIN_ERR_MISSING,
argv[0] ); argv[0] );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
for( i=woptind; i<argc; i++ ) for( i=woptind; i<argc; i++ )
@ -468,11 +465,11 @@ static int builtin_commandline( parser_t &parser, wchar_t **argv )
} }
else else
{ {
sb_printf( sb_err, append_format(stderr_buffer,
_(L"%ls: Unknown input function '%ls'\n"), _(L"%ls: Unknown input function '%ls'\n"),
argv[0], argv[0],
argv[i] ); argv[i] );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
} }
@ -486,45 +483,45 @@ static int builtin_commandline( parser_t &parser, wchar_t **argv )
if( (search_mode || line_mode || cursor_mode) && (argc-woptind > 1) ) if( (search_mode || line_mode || cursor_mode) && (argc-woptind > 1) )
{ {
sb_append( sb_err, append_format(stderr_buffer,
argv[0], argv[0],
L": Too many arguments\n", L": Too many arguments\n",
NULL ); NULL );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
if( (buffer_part || tokenize || cut_at_cursor) && (cursor_mode || line_mode || search_mode) ) if( (buffer_part || tokenize || cut_at_cursor) && (cursor_mode || line_mode || search_mode) )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_COMBO, BUILTIN_ERR_COMBO,
argv[0] ); argv[0] );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
if( (tokenize || cut_at_cursor) && (argc-woptind) ) if( (tokenize || cut_at_cursor) && (argc-woptind) )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_COMBO2, BUILTIN_ERR_COMBO2,
argv[0], argv[0],
L"--cut-at-cursor and --tokenize can not be used when setting the commandline" ); L"--cut-at-cursor and --tokenize can not be used when setting the commandline" );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
if( append_mode && !(argc-woptind) ) if( append_mode && !(argc-woptind) )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_COMBO2, BUILTIN_ERR_COMBO2,
argv[0], argv[0],
L"insertion mode switches can not be used when not in insertion mode" ); L"insertion mode switches can not be used when not in insertion mode" );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
@ -552,11 +549,11 @@ static int builtin_commandline( parser_t &parser, wchar_t **argv )
new_pos = wcstol( argv[woptind], &endptr, 10 ); new_pos = wcstol( argv[woptind], &endptr, 10 );
if( *endptr || errno ) if( *endptr || errno )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_NOT_NUMBER, BUILTIN_ERR_NOT_NUMBER,
argv[0], argv[0],
argv[woptind] ); argv[woptind] );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
} }
current_buffer = reader_get_buffer(); current_buffer = reader_get_buffer();
@ -566,7 +563,7 @@ static int builtin_commandline( parser_t &parser, wchar_t **argv )
} }
else else
{ {
sb_printf( sb_out, L"%d\n", reader_get_cursor_pos() ); append_format(stdout_buffer, L"%d\n", reader_get_cursor_pos() );
return 0; return 0;
} }
@ -576,7 +573,7 @@ static int builtin_commandline( parser_t &parser, wchar_t **argv )
{ {
int pos = reader_get_cursor_pos(); int pos = reader_get_cursor_pos();
const wchar_t *buff = reader_get_buffer(); const wchar_t *buff = reader_get_buffer();
sb_printf( sb_out, L"%d\n", parse_util_lineno( buff, pos ) ); append_format(stdout_buffer, L"%d\n", parse_util_lineno( buff, pos ) );
return 0; return 0;
} }

View File

@ -397,11 +397,11 @@ static int builtin_complete( parser_t &parser, wchar_t **argv )
case 0: case 0:
if(long_options[opt_index].flag != 0) if(long_options[opt_index].flag != 0)
break; break;
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_UNKNOWN, BUILTIN_ERR_UNKNOWN,
argv[0], argv[0],
long_options[opt_index].name ); long_options[opt_index].name );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
res = 1; res = 1;
@ -434,7 +434,7 @@ static int builtin_complete( parser_t &parser, wchar_t **argv )
} }
else else
{ {
sb_printf( sb_err, L"%ls: Invalid token '%ls'\n", argv[0], woptarg ); append_format(stderr_buffer, L"%ls: Invalid token '%ls'\n", argv[0], woptarg );
res = 1; res = 1;
} }
break; break;
@ -481,7 +481,7 @@ static int builtin_complete( parser_t &parser, wchar_t **argv )
break; break;
case 'h': case 'h':
builtin_print_help( parser, argv[0], sb_out ); builtin_print_help( parser, argv[0], stdout_buffer );
return 0; return 0;
case '?': case '?':
@ -499,12 +499,12 @@ static int builtin_complete( parser_t &parser, wchar_t **argv )
{ {
if( parser.test( condition, 0, 0, 0 ) ) if( parser.test( condition, 0, 0, 0 ) )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
L"%ls: Condition '%ls' contained a syntax error\n", L"%ls: Condition '%ls' contained a syntax error\n",
argv[0], argv[0],
condition ); condition );
parser.test( condition, 0, sb_err, argv[0] ); parser.test( condition, 0, &stderr_buffer, argv[0] );
res = 1; res = 1;
} }
@ -517,12 +517,12 @@ static int builtin_complete( parser_t &parser, wchar_t **argv )
{ {
if( parser.test_args( comp, 0, 0 ) ) if( parser.test_args( comp, 0, 0 ) )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
L"%ls: Completion '%ls' contained a syntax error\n", L"%ls: Completion '%ls' contained a syntax error\n",
argv[0], argv[0],
comp ); comp );
parser.test_args( comp, sb_err, argv[0] ); parser.test_args( comp, &stderr_buffer, argv[0] );
res = 1; res = 1;
} }
@ -567,11 +567,11 @@ static int builtin_complete( parser_t &parser, wchar_t **argv )
if( !(next.description).empty() ) if( !(next.description).empty() )
{ {
sb_printf( sb_out, L"%ls%ls\t%ls\n", prepend, next.completion.c_str(), next.description.c_str() ); append_format(stdout_buffer, L"%ls%ls\t%ls\n", prepend, next.completion.c_str(), next.description.c_str() );
} }
else else
{ {
sb_printf( sb_out, L"%ls%ls\n", prepend, next.completion.c_str() ); append_format(stdout_buffer, L"%ls%ls\n", prepend, next.completion.c_str() );
} }
} }
@ -583,10 +583,10 @@ static int builtin_complete( parser_t &parser, wchar_t **argv )
} }
else if( woptind != argc ) else if( woptind != argc )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
_( L"%ls: Too many arguments\n" ), _( L"%ls: Too many arguments\n" ),
argv[0] ); argv[0] );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
res = 1; res = 1;
} }
@ -594,7 +594,7 @@ static int builtin_complete( parser_t &parser, wchar_t **argv )
{ {
/* No arguments specified, meaning we print the definitions of /* No arguments specified, meaning we print the definitions of
* all specified completions to stdout.*/ * all specified completions to stdout.*/
complete_print( sb_out ); complete_print( stdout_buffer );
} }
else else
{ {

View File

@ -84,24 +84,22 @@ static void builtin_jobs_print( job_t *j, int mode, int header )
/* /*
Print table header before first job Print table header before first job
*/ */
sb_append( sb_out, _( L"Job\tGroup\t" )); stdout_buffer.append( _( L"Job\tGroup\t" ));
#ifdef HAVE__PROC_SELF_STAT #ifdef HAVE__PROC_SELF_STAT
sb_append( sb_out, _( L"CPU\t" ) ); stdout_buffer.append( _( L"CPU\t" ) );
#endif #endif
sb_append( sb_out, _( L"State\tCommand\n" ) ); stdout_buffer.append( _( L"State\tCommand\n" ) );
} }
sb_printf( sb_out, L"%d\t%d\t", j->job_id, j->pgid ); append_format(stdout_buffer, L"%d\t%d\t", j->job_id, j->pgid );
#ifdef HAVE__PROC_SELF_STAT #ifdef HAVE__PROC_SELF_STAT
sb_printf( sb_out, L"%d%%\t", cpu_use(j) ); append_format(stdout_buffer, L"%d%%\t", cpu_use(j) );
#endif #endif
sb_append( sb_out, stdout_buffer.append(job_is_stopped(j)?_(L"stopped"):_(L"running"));
job_is_stopped(j)?_(L"stopped"):_(L"running"), stdout_buffer.append(L"\t");
L"\t", stdout_buffer.append(j->command_cstr());
j->command_cstr(), stdout_buffer.append(L"\t");
L"\n",
NULL );
break; break;
} }
@ -112,9 +110,9 @@ static void builtin_jobs_print( job_t *j, int mode, int header )
/* /*
Print table header before first job Print table header before first job
*/ */
sb_append( sb_out, _( L"Group\n" )); stdout_buffer.append( _( L"Group\n" ));
} }
sb_printf( sb_out, L"%d\n", j->pgid ); append_format(stdout_buffer, L"%d\n", j->pgid );
break; break;
} }
@ -125,12 +123,12 @@ static void builtin_jobs_print( job_t *j, int mode, int header )
/* /*
Print table header before first job Print table header before first job
*/ */
sb_append( sb_out, _( L"Procces\n" )); stdout_buffer.append( _( L"Procces\n" ));
} }
for( p=j->first_process; p; p=p->next ) for( p=j->first_process; p; p=p->next )
{ {
sb_printf( sb_out, L"%d\n", p->pid ); append_format(stdout_buffer, L"%d\n", p->pid );
} }
break; break;
} }
@ -142,12 +140,12 @@ static void builtin_jobs_print( job_t *j, int mode, int header )
/* /*
Print table header before first job Print table header before first job
*/ */
sb_append( sb_out, _( L"Command\n" )); stdout_buffer.append( _( L"Command\n" ));
} }
for( p=j->first_process; p; p=p->next ) for( p=j->first_process; p; p=p->next )
{ {
sb_printf( sb_out, L"%ls\n", p->argv0() ); append_format(stdout_buffer, L"%ls\n", p->argv0() );
} }
break; break;
} }
@ -217,12 +215,12 @@ static int builtin_jobs( parser_t &parser, wchar_t **argv )
case 0: case 0:
if(long_options[opt_index].flag != 0) if(long_options[opt_index].flag != 0)
break; break;
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_UNKNOWN, BUILTIN_ERR_UNKNOWN,
argv[0], argv[0],
long_options[opt_index].name ); long_options[opt_index].name );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
@ -247,7 +245,7 @@ static int builtin_jobs( parser_t &parser, wchar_t **argv )
} }
case 'h': case 'h':
builtin_print_help( parser, argv[0], sb_out ); builtin_print_help( parser, argv[0], stdout_buffer );
return 0; return 0;
case '?': case '?':
@ -300,7 +298,7 @@ static int builtin_jobs( parser_t &parser, wchar_t **argv )
pid=wcstol( argv[i], &end, 10 ); pid=wcstol( argv[i], &end, 10 );
if( errno || *end ) if( errno || *end )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
_( L"%ls: '%ls' is not a job\n" ), _( L"%ls: '%ls' is not a job\n" ),
argv[0], argv[0],
argv[i] ); argv[i] );
@ -315,7 +313,7 @@ static int builtin_jobs( parser_t &parser, wchar_t **argv )
} }
else else
{ {
sb_printf( sb_err, append_format(stderr_buffer,
_( L"%ls: No suitable job: %d\n" ), _( L"%ls: No suitable job: %d\n" ),
argv[0], argv[0],
pid ); pid );
@ -343,7 +341,7 @@ static int builtin_jobs( parser_t &parser, wchar_t **argv )
if( !found ) if( !found )
{ {
sb_printf( sb_out, append_format(stdout_buffer,
_( L"%ls: There are no jobs\n" ), _( L"%ls: There are no jobs\n" ),
argv[0] ); argv[0] );
} }

View File

@ -26,6 +26,8 @@ Functions used for implementing the set builtin.
#include "proc.h" #include "proc.h"
#include "parser.h" #include "parser.h"
/* We know about these buffers */
extern wcstring stdout_buffer, stderr_buffer;
/** /**
Error message for invalid path operations Error message for invalid path operations
@ -91,12 +93,7 @@ static int my_env_set( const wchar_t *key, wcstring_list_t &val, int scope )
{ {
const wchar_t *colon; const wchar_t *colon;
sb_printf( sb_err, append_format(stderr_buffer, _(BUILTIN_SET_PATH_ERROR), L"set", dir, key);
_(BUILTIN_SET_PATH_ERROR),
L"set",
dir,
key );
colon = wcschr( dir, L':' ); colon = wcschr( dir, L':' );
if( colon && *(colon+1) ) if( colon && *(colon+1) )
@ -113,12 +110,7 @@ static int my_env_set( const wchar_t *key, wcstring_list_t &val, int scope )
if( show_hint ) if( show_hint )
{ {
sb_printf( sb_err, append_format(stderr_buffer, _(BUILTIN_SET_PATH_HINT), L"set", key, key, wcschr( dir, L':' )+1);
_(BUILTIN_SET_PATH_HINT),
L"set",
key,
key,
wcschr( dir, L':' )+1);
} }
if( error ) if( error )
@ -156,14 +148,14 @@ static int my_env_set( const wchar_t *key, wcstring_list_t &val, int scope )
{ {
case ENV_PERM: case ENV_PERM:
{ {
sb_printf( sb_err, _(L"%ls: Tried to change the read-only variable '%ls'\n"), L"set", key ); append_format(stderr_buffer, _(L"%ls: Tried to change the read-only variable '%ls'\n"), L"set", key);
retcode=1; retcode=1;
break; break;
} }
case ENV_INVALID: case ENV_INVALID:
{ {
sb_printf( sb_err, _(L"%ls: Unknown error"), L"set" ); append_format(stderr_buffer, _(L"%ls: Unknown error"), L"set" );
retcode=1; retcode=1;
break; break;
} }
@ -208,7 +200,7 @@ static int parse_index( std::vector<long> &indexes,
if (*src != L'[') if (*src != L'[')
{ {
sb_printf( sb_err, _(BUILTIN_SET_ARG_COUNT), L"set" ); append_format(stderr_buffer, _(BUILTIN_SET_ARG_COUNT), L"set" );
return 0; return 0;
} }
@ -216,7 +208,7 @@ static int parse_index( std::vector<long> &indexes,
if( (wcsncmp( src_orig, name, len )!=0) || (wcslen(name) != (len)) ) if( (wcsncmp( src_orig, name, len )!=0) || (wcslen(name) != (len)) )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
_(L"%ls: Multiple variable names specified in single call (%ls and %.*ls)\n"), _(L"%ls: Multiple variable names specified in single call (%ls and %.*ls)\n"),
L"set", L"set",
name, name,
@ -244,7 +236,7 @@ static int parse_index( std::vector<long> &indexes,
if( end==src || errno ) if( end==src || errno )
{ {
sb_printf(sb_err, _(L"%ls: Invalid index starting at '%ls'\n"), L"set", src); append_format(stderr_buffer, _(L"%ls: Invalid index starting at '%ls'\n"), L"set", src);
return 0; return 0;
} }
@ -326,7 +318,7 @@ static void print_variables(int include_values, int esc, int scope)
const wcstring key = names.at(i); const wcstring key = names.at(i);
const wcstring e_key = escape_string(key, 0); const wcstring e_key = escape_string(key, 0);
sb_append(sb_out, e_key.c_str()); stdout_buffer.append(e_key);
if( include_values ) if( include_values )
{ {
@ -343,17 +335,18 @@ static void print_variables(int include_values, int esc, int scope)
wcstring e_value = esc ? expand_escape_variable(value) : value; wcstring e_value = esc ? expand_escape_variable(value) : value;
sb_append(sb_out, L" ", e_value.c_str(), NULL); stdout_buffer.append(L" ");
stdout_buffer.append(e_value);
if( shorten ) if( shorten )
{ {
sb_append(sb_out, L"\u2026"); stdout_buffer.append(L"\u2026");
} }
} }
} }
sb_append(sb_out, L"\n"); stdout_buffer.append(L"\n");
} }
} }
@ -487,7 +480,7 @@ static int builtin_set( parser_t &parser, wchar_t **argv )
break; break;
case 'h': case 'h':
builtin_print_help( parser, argv[0], sb_out ); builtin_print_help( parser, argv[0], stdout_buffer );
return 0; return 0;
case '?': case '?':
@ -510,11 +503,11 @@ static int builtin_set( parser_t &parser, wchar_t **argv )
if( query && (erase || list || global || local || universal || exportv || unexport ) ) if( query && (erase || list || global || local || universal || exportv || unexport ) )
{ {
sb_printf(sb_err, append_format(stderr_buffer,
BUILTIN_ERR_COMBO, BUILTIN_ERR_COMBO,
argv[0] ); argv[0] );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
@ -522,11 +515,11 @@ static int builtin_set( parser_t &parser, wchar_t **argv )
/* We can't both list and erase varaibles */ /* We can't both list and erase varaibles */
if( erase && list ) if( erase && list )
{ {
sb_printf(sb_err, append_format(stderr_buffer,
BUILTIN_ERR_COMBO, BUILTIN_ERR_COMBO,
argv[0] ); argv[0] );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
@ -535,10 +528,10 @@ static int builtin_set( parser_t &parser, wchar_t **argv )
*/ */
if( local + global + universal > 1 ) if( local + global + universal > 1 )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_GLOCAL, BUILTIN_ERR_GLOCAL,
argv[0] ); argv[0] );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
@ -547,10 +540,10 @@ static int builtin_set( parser_t &parser, wchar_t **argv )
*/ */
if( exportv && unexport ) if( exportv && unexport )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_EXPUNEXP, BUILTIN_ERR_EXPUNEXP,
argv[0] ); argv[0] );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
@ -596,7 +589,7 @@ static int builtin_set( parser_t &parser, wchar_t **argv )
if( !parse_index( indexes, arg, dest, result.size() ) ) if( !parse_index( indexes, arg, dest, result.size() ) )
{ {
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
retcode = 1; retcode = 1;
break; break;
} }
@ -638,11 +631,11 @@ static int builtin_set( parser_t &parser, wchar_t **argv )
if( erase ) if( erase )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
_(L"%ls: Erase needs a variable name\n%ls\n"), _(L"%ls: Erase needs a variable name\n%ls\n"),
argv[0] ); argv[0] );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
retcode = 1; retcode = 1;
} }
else else
@ -667,15 +660,15 @@ static int builtin_set( parser_t &parser, wchar_t **argv )
if( !wcslen( dest ) ) if( !wcslen( dest ) )
{ {
free( dest ); free( dest );
sb_printf( sb_err, BUILTIN_ERR_VARNAME_ZERO, argv[0] ); append_format(stderr_buffer, BUILTIN_ERR_VARNAME_ZERO, argv[0] );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
if( (bad_char = wcsvarname( dest ) ) ) if( (bad_char = wcsvarname( dest ) ) )
{ {
sb_printf( sb_err, BUILTIN_ERR_VARCHAR, argv[0], *bad_char ); append_format(stderr_buffer, BUILTIN_ERR_VARCHAR, argv[0], *bad_char );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
free( dest ); free( dest );
return 1; return 1;
} }
@ -683,8 +676,8 @@ static int builtin_set( parser_t &parser, wchar_t **argv )
if( slice && erase && (scope != ENV_USER) ) if( slice && erase && (scope != ENV_USER) )
{ {
free( dest ); free( dest );
sb_printf( sb_err, _(L"%ls: Can not specify scope when erasing array slice\n"), argv[0] ); append_format(stderr_buffer, _(L"%ls: Can not specify scope when erasing array slice\n"), argv[0] );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
@ -712,7 +705,7 @@ static int builtin_set( parser_t &parser, wchar_t **argv )
{ {
if( !parse_index( indexes, argv[woptind], dest, result.size() ) ) if( !parse_index( indexes, argv[woptind], dest, result.size() ) )
{ {
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
retcode = 1; retcode = 1;
break; break;
} }
@ -724,8 +717,8 @@ static int builtin_set( parser_t &parser, wchar_t **argv )
{ {
if( val_count < idx_count ) if( val_count < idx_count )
{ {
sb_printf( sb_err, _(BUILTIN_SET_ARG_COUNT), argv[0] ); append_format(stderr_buffer, _(BUILTIN_SET_ARG_COUNT), argv[0] );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
retcode=1; retcode=1;
break; break;
} }
@ -762,9 +755,9 @@ static int builtin_set( parser_t &parser, wchar_t **argv )
indexes, indexes,
value ) ) value ) )
{ {
sb_printf( sb_err, L"%ls: ", argv[0] ); append_format(stderr_buffer, L"%ls: ", argv[0] );
sb_printf( sb_err, ARRAY_BOUNDS_ERR ); append_format(stderr_buffer, ARRAY_BOUNDS_ERR );
sb_append( sb_err, L"\n" ); stderr_buffer.push_back(L'\n');
} }
my_env_set(dest, result, scope); my_env_set(dest, result, scope);
@ -792,10 +785,10 @@ static int builtin_set( parser_t &parser, wchar_t **argv )
{ {
if( woptind != argc ) if( woptind != argc )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
_(L"%ls: Values cannot be specfied with erase\n"), _(L"%ls: Values cannot be specfied with erase\n"),
argv[0] ); argv[0] );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
retcode=1; retcode=1;
} }
else else

View File

@ -144,9 +144,9 @@ static void print( int resource, int hard )
rlim_t l = get( resource, hard ); rlim_t l = get( resource, hard );
if( l == RLIM_INFINITY ) if( l == RLIM_INFINITY )
sb_append( sb_out, L"unlimited\n" ); stdout_buffer.append( L"unlimited\n" );
else else
sb_printf( sb_out, L"%d\n", l / get_multiplier( resource ) ); append_format(stdout_buffer, L"%d\n", l / get_multiplier( resource ) );
} }
@ -172,7 +172,7 @@ static void print_all( int hard )
const wchar_t *unit = ((resource_arr[i].resource==RLIMIT_CPU)?L"(seconds, ":(get_multiplier(resource_arr[i].resource)==1?L"(":L"(kB, ")); const wchar_t *unit = ((resource_arr[i].resource==RLIMIT_CPU)?L"(seconds, ":(get_multiplier(resource_arr[i].resource)==1?L"(":L"(kB, "));
sb_printf( sb_out, append_format(stdout_buffer,
L"%-*ls %10ls-%lc) ", L"%-*ls %10ls-%lc) ",
w, w,
resource_arr[i].desc, resource_arr[i].desc,
@ -181,11 +181,11 @@ static void print_all( int hard )
if( l == RLIM_INFINITY ) if( l == RLIM_INFINITY )
{ {
sb_append( sb_out, L"unlimited\n" ); stdout_buffer.append( L"unlimited\n" );
} }
else else
{ {
sb_printf( sb_out, L"%d\n", l/get_multiplier(resource_arr[i].resource) ); append_format(stdout_buffer, L"%d\n", l/get_multiplier(resource_arr[i].resource) );
} }
} }
@ -240,7 +240,7 @@ static int set( int resource, int hard, int soft, rlim_t value )
if( setrlimit( resource, &ls ) ) if( setrlimit( resource, &ls ) )
{ {
if( errno == EPERM ) if( errno == EPERM )
sb_printf( sb_err, L"ulimit: Permission denied when changing resource of type '%ls'\n", get_desc( resource ) ); append_format(stderr_buffer, L"ulimit: Permission denied when changing resource of type '%ls'\n", get_desc( resource ) );
else else
builtin_wperror( L"ulimit" ); builtin_wperror( L"ulimit" );
return 1; return 1;
@ -347,11 +347,11 @@ static int builtin_ulimit( parser_t &parser, wchar_t ** argv )
case 0: case 0:
if(long_options[opt_index].flag != 0) if(long_options[opt_index].flag != 0)
break; break;
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_UNKNOWN, BUILTIN_ERR_UNKNOWN,
argv[0], argv[0],
long_options[opt_index].name ); long_options[opt_index].name );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
@ -415,7 +415,7 @@ static int builtin_ulimit( parser_t &parser, wchar_t ** argv )
#endif #endif
case L'h': case L'h':
builtin_print_help( parser, argv[0], sb_out ); builtin_print_help( parser, argv[0], stdout_buffer );
return 0; return 0;
case L'?': case L'?':
@ -432,11 +432,9 @@ static int builtin_ulimit( parser_t &parser, wchar_t ** argv )
} }
else else
{ {
sb_append( sb_err, stderr_buffer.append(argv[0]);
argv[0], stderr_buffer.append(L": Too many arguments\n");
L": Too many arguments\n", builtin_print_help( parser, argv[0], stderr_buffer );
NULL );
builtin_print_help( parser, argv[0], sb_err );
return 1; return 1;
} }
@ -488,11 +486,11 @@ static int builtin_ulimit( parser_t &parser, wchar_t ** argv )
new_limit = wcstol( argv[woptind], &end, 10 ); new_limit = wcstol( argv[woptind], &end, 10 );
if( errno || *end ) if( errno || *end )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
L"%ls: Invalid limit '%ls'\n", L"%ls: Invalid limit '%ls'\n",
argv[0], argv[0],
argv[woptind] ); argv[woptind] );
builtin_print_help( parser, argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
new_limit *= get_multiplier( what ); new_limit *= get_multiplier( what );
@ -503,11 +501,9 @@ static int builtin_ulimit( parser_t &parser, wchar_t ** argv )
default: default:
{ {
sb_append( sb_err, stderr_buffer.append(argv[0]);
argv[0], stderr_buffer.append(L": Too many arguments\n");
L": Too many arguments\n", builtin_print_help( parser, argv[0], stderr_buffer );
NULL );
builtin_print_help( parser, argv[0], sb_err );
return 1; return 1;
} }

View File

@ -470,6 +470,13 @@ wcstring vformat_string(const wchar_t *format, va_list va_orig)
return result; return result;
} }
void append_format(wcstring &str, const wchar_t *format, ...)
{
va_list va;
va_start( va, format );
str.append(vformat_string(format, va));
va_end( va );
}
wchar_t *wcsvarname( const wchar_t *str ) wchar_t *wcsvarname( const wchar_t *str )
{ {
@ -648,14 +655,11 @@ ssize_t write_loop(int fd, const char *buff, size_t count)
return out_cum; return out_cum;
} }
void debug( int level, const wchar_t *msg, ... ) void debug( int level, const wchar_t *msg, ... )
{ {
va_list va; va_list va;
string_buffer_t sb; wcstring sb;
string_buffer_t sb2;
int errno_old = errno; int errno_old = errno;
@ -664,37 +668,117 @@ void debug( int level, const wchar_t *msg, ... )
CHECK( msg, ); CHECK( msg, );
sb_init( &sb ); sb = format_string(L"%ls: ", program_name);
sb_init( &sb2 ); sb.append(vformat_string(msg, va));
sb_printf( &sb, L"%ls: ", program_name ); wcstring sb2;
write_screen( sb, sb2 );
va_start( va, msg ); fwprintf( stderr, L"%ls", sb2.c_str() );
sb_vprintf( &sb, msg, va );
va_end( va );
write_screen( (wchar_t *)sb.buff, &sb2 );
fwprintf( stderr, L"%ls", sb2.buff );
sb_destroy( &sb );
sb_destroy( &sb2 );
errno = errno_old; errno = errno_old;
} }
void write_screen( const wchar_t *msg, string_buffer_t *buff ) void write_screen( const wcstring &msg, wcstring &buff )
{
const wchar_t *start, *pos;
int line_width = 0;
int tok_width = 0;
int screen_width = common_get_width();
if( screen_width )
{
start = pos = msg.c_str();
while( 1 )
{
int overflow = 0;
tok_width=0;
/*
Tokenize on whitespace, and also calculate the width of the token
*/
while( *pos && ( !wcschr( L" \n\r\t", *pos ) ) )
{
/*
Check is token is wider than one line.
If so we mark it as an overflow and break the token.
*/
if((tok_width + wcwidth(*pos)) > (screen_width-1))
{
overflow = 1;
break;
}
tok_width += wcwidth( *pos );
pos++;
}
/*
If token is zero character long, we don't do anything
*/
if( pos == start )
{
start = pos = pos+1;
}
else if( overflow )
{
/*
In case of overflow, we print a newline, except if we already are at position 0
*/
wchar_t *token = wcsndup( start, pos-start );
if( line_width != 0 )
buff.push_back(L'\n');
buff.append(format_string(L"%ls-\n", token));
free( token );
line_width=0;
}
else
{
/*
Print the token
*/
wchar_t *token = wcsndup( start, pos-start );
if( (line_width + (line_width!=0?1:0) + tok_width) > screen_width )
{
buff.push_back(L'\n');
line_width=0;
}
buff.append(format_string(L"%ls%ls", line_width?L" ":L"", token ));
free( token );
line_width += (line_width!=0?1:0) + tok_width;
}
/*
Break on end of string
*/
if( !*pos )
{
break;
}
start=pos;
}
}
else
{
buff.append(msg);
}
buff.push_back(L'\n');
}
void write_screen( const wcstring &msg, string_buffer_t *buff )
{ {
const wchar_t *start, *pos; const wchar_t *start, *pos;
int line_width = 0; int line_width = 0;
int tok_width = 0; int tok_width = 0;
int screen_width = common_get_width(); int screen_width = common_get_width();
CHECK( msg, );
CHECK( buff, ); CHECK( buff, );
if( screen_width ) if( screen_width )
{ {
start = pos = msg; start = pos = msg.c_str();
while( 1 ) while( 1 )
{ {
int overflow = 0; int overflow = 0;
@ -769,7 +853,7 @@ void write_screen( const wchar_t *msg, string_buffer_t *buff )
} }
else else
{ {
sb_printf( buff, L"%ls", msg ); sb_printf( buff, L"%ls", msg.c_str() );
} }
sb_append_char( buff, L'\n' ); sb_append_char( buff, L'\n' );
} }
@ -1833,7 +1917,6 @@ void bugreport()
_( L"This is a bug. Break on bugreport to debug." _( L"This is a bug. Break on bugreport to debug."
L"If you can reproduce it, please send a bug report to %s." ), L"If you can reproduce it, please send a bug report to %s." ),
PACKAGE_BUGREPORT ); PACKAGE_BUGREPORT );
while (1) sleep(10000);
} }

View File

@ -351,6 +351,7 @@ void append_path_component(wcstring &path, const wcstring &component);
wcstring format_string(const wchar_t *format, ...); wcstring format_string(const wchar_t *format, ...);
wcstring vformat_string(const wchar_t *format, va_list va_orig); wcstring vformat_string(const wchar_t *format, va_list va_orig);
void append_format(wcstring &str, const wchar_t *format, ...);
/** /**
Returns a newly allocated wide character string array equivalent of Returns a newly allocated wide character string array equivalent of
@ -545,7 +546,8 @@ void common_handle_winch( int signal );
Write paragraph of output to the specified stringbuffer, and redo Write paragraph of output to the specified stringbuffer, and redo
the linebreaks to fit the current screen. the linebreaks to fit the current screen.
*/ */
void write_screen( const wchar_t *msg, string_buffer_t *buff ); void write_screen( const wcstring &msg, string_buffer_t *buff );
void write_screen( const wcstring &msg, wcstring &buff );
/** /**
Tokenize the specified string into the specified wcstring_list_t. Tokenize the specified string into the specified wcstring_list_t.

View File

@ -1853,7 +1853,7 @@ void complete( const wchar_t *cmd,
argument to the specified stringbuffer, but only if arguemnt is argument to the specified stringbuffer, but only if arguemnt is
non-null and longer than 0 characters. non-null and longer than 0 characters.
*/ */
static void append_switch( string_buffer_t *out, static void append_switch( wcstring &out,
const wcstring &opt, const wcstring &opt,
const wcstring &argument ) const wcstring &argument )
{ {
@ -1861,13 +1861,11 @@ static void append_switch( string_buffer_t *out,
return; return;
wcstring esc = escape_string( argument, 1 ); wcstring esc = escape_string( argument, 1 );
sb_printf( out, L" --%ls %ls", opt.c_str(), esc.c_str() ); append_format( out, L" --%ls %ls", opt.c_str(), esc.c_str() );
} }
void complete_print( string_buffer_t *out ) void complete_print( wcstring &out )
{ {
CHECK( out, );
for (completion_entry_list_t::const_iterator iter = completion_entries.begin(); iter != completion_entries.end(); iter++) for (completion_entry_list_t::const_iterator iter = completion_entries.begin(); iter != completion_entries.end(); iter++)
{ {
const completion_entry_t *e = *iter; const completion_entry_t *e = *iter;
@ -1883,7 +1881,7 @@ void complete_print( string_buffer_t *out )
} }
; ;
sb_printf( out, append_format( out,
L"complete%ls", L"complete%ls",
modestr[o->result_mode] ); modestr[o->result_mode] );
@ -1894,7 +1892,7 @@ void complete_print( string_buffer_t *out )
if( o->short_opt != 0 ) if( o->short_opt != 0 )
{ {
sb_printf( out, append_format( out,
L" --short-option '%lc'", L" --short-option '%lc'",
o->short_opt ); o->short_opt );
} }
@ -1916,7 +1914,7 @@ void complete_print( string_buffer_t *out )
L"condition", L"condition",
o->condition ); o->condition );
sb_printf( out, L"\n" ); out.append( L"\n" );
} }
} }
} }

View File

@ -228,7 +228,7 @@ void complete( const wchar_t* cmd, std::vector<completion_t> &out);
\param out The string_buffer_t to write completions to \param out The string_buffer_t to write completions to
*/ */
void complete_print( string_buffer_t *out ); void complete_print( wcstring &out );
/** /**
Tests if the specified option is defined for the specified command Tests if the specified option is defined for the specified command

View File

@ -880,7 +880,7 @@ static pid_t exec_fork()
/** /**
Perform output from builtins Perform output from builtins
*/ */
static void do_builtin_io( wchar_t *out, wchar_t *err ) static void do_builtin_io( const wchar_t *out, const wchar_t *err )
{ {
if( out ) if( out )
@ -1500,9 +1500,9 @@ void exec( parser_t &parser, job_t *j )
not inside a pipeline, there is no need to fork not inside a pipeline, there is no need to fork
*/ */
skip_fork = skip_fork =
( !sb_out->used ) && get_stdout_buffer().empty() &&
( !sb_err->used ) && get_stderr_buffer().empty() &&
( !p->next ); !p->next;
/* /*
If the output of a builtin is to be sent to an internal If the output of a builtin is to be sent to an internal
@ -1513,15 +1513,14 @@ void exec( parser_t &parser, job_t *j )
io_data_t *io = io_get( j->io, 1 ); io_data_t *io = io_get( j->io, 1 );
int buffer_stdout = io && io->io_mode == IO_BUFFER; int buffer_stdout = io && io->io_mode == IO_BUFFER;
if( ( !sb_err->used ) && if( ( get_stderr_buffer().empty() ) &&
( !p->next ) && ( !p->next ) &&
( sb_out->used ) && ( ! get_stdout_buffer().empty() ) &&
( buffer_stdout ) ) ( buffer_stdout ) )
{ {
char *res = wcs2str( (wchar_t *)sb_out->buff ); std::string res = wcs2string( get_stdout_buffer() );
b_append( io->param2.out_buffer, res, strlen( res ) ); b_append( io->param2.out_buffer, res.c_str(), res.size() );
skip_fork = 1; skip_fork = 1;
free( res );
} }
for( io = j->io; io; io=io->next ) for( io = j->io; io; io=io->next )
@ -1560,8 +1559,8 @@ void exec( parser_t &parser, job_t *j )
*/ */
p->pid = getpid(); p->pid = getpid();
setup_child_process( j, p ); setup_child_process( j, p );
do_builtin_io( sb_out->used ? (wchar_t *)sb_out->buff : 0, sb_err->used ? (wchar_t *)sb_err->buff : 0 ); const wcstring &out = get_stdout_buffer(), &err = get_stderr_buffer();
do_builtin_io( out.empty() ? NULL : out.c_str(), err.empty() ? NULL : err.c_str() );
exit( p->status ); exit( p->status );
} }

View File

@ -108,42 +108,6 @@ static void err( const wchar_t *blah, ... )
wprintf( L"\n" ); wprintf( L"\n" );
} }
/**
Stringbuffer test
*/
static void sb_test()
{
string_buffer_t b;
int res;
sb_init( &b );
if( (res=sb_printf( &b, L"%ls%s", L"Testing ", "string_buffer_t " )) == -1 )
{
err( L"Error %d while testing stringbuffers", res );
}
if( (res=sb_printf( &b, L"%ls", L"functionality" ))==-1)
{
err( L"Error %d while testing stringbuffers", res );
}
say( (wchar_t *)b.buff );
sb_clear( &b );
sb_printf( &b, L"%d %u %o %x %llX", -7, 99999999, 01234567, 0xdeadbeef, 0xdeadbeefdeadbeefll );
if( wcscmp( (wchar_t *)b.buff, NUM_ANS) != 0 )
{
err( L"numerical formating is broken, '%ls' != '%ls'", (wchar_t *)b.buff, NUM_ANS );
}
else
say( L"numerical formating works" );
}
/** /**
Test the escaping/unescaping code by escaping/unescaping random Test the escaping/unescaping code by escaping/unescaping random
strings and verifying that the original string comes back. strings and verifying that the original string comes back.

View File

@ -360,7 +360,6 @@ parser_t::parser_t(enum parser_type_t type) :
error_code(0), error_code(0),
err_pos(0), err_pos(0),
current_tokenizer(NULL), current_tokenizer(NULL),
lineinfo(NULL),
current_tokenizer_pos(0), current_tokenizer_pos(0),
job_start_pos(0), job_start_pos(0),
eval_level(-1), eval_level(-1),
@ -709,12 +708,7 @@ void parser_t::destroy()
} }
} }
if( lineinfo ) lineinfo.clear();
{
sb_destroy( lineinfo );
free(lineinfo );
lineinfo = 0;
}
forbidden_function.clear(); forbidden_function.clear();
@ -726,21 +720,20 @@ void parser_t::destroy()
\param target the buffer to write to \param target the buffer to write to
\param prefix: The string token to prefix the ech line with. Usually the name of the command trying to parse something. \param prefix: The string token to prefix the ech line with. Usually the name of the command trying to parse something.
*/ */
void parser_t::print_errors( string_buffer_t *target, const wchar_t *prefix ) void parser_t::print_errors( wcstring &target, const wchar_t *prefix )
{ {
CHECK( target, );
CHECK( prefix, ); CHECK( prefix, );
if( error_code && ! err_buff.empty() ) if( error_code && ! err_buff.empty() )
{ {
int tmp; int tmp;
sb_printf( target, L"%ls: %ls\n", prefix, err_buff.c_str() ); append_format( target, L"%ls: %ls\n", prefix, err_buff.c_str() );
tmp = current_tokenizer_pos; tmp = current_tokenizer_pos;
current_tokenizer_pos = err_pos; current_tokenizer_pos = err_pos;
sb_printf( target, L"%ls", this->current_line() ); append_format( target, L"%ls", this->current_line() );
current_tokenizer_pos=tmp; current_tokenizer_pos=tmp;
} }
@ -849,14 +842,8 @@ int parser_t::eval_args( const wchar_t *line, std::vector<completion_t> &args )
return 1; return 1;
} }
void parser_t::stack_trace( block_t *b, string_buffer_t *buff) void parser_t::stack_trace( block_t *b, wcstring &buff)
{ {
/*
Validate input
*/
CHECK( buff, );
/* /*
Check if we should end the recursion Check if we should end the recursion
*/ */
@ -869,9 +856,8 @@ void parser_t::stack_trace( block_t *b, string_buffer_t *buff)
This is an event handler This is an event handler
*/ */
wcstring description = event_get_desc( b->state1<const event_t *>() ); wcstring description = event_get_desc( b->state1<const event_t *>() );
sb_printf( buff, _(L"in event handler: %ls\n"), description.c_str()); append_format( buff, _(L"in event handler: %ls\n"), description.c_str());
sb_printf( buff, buff.append( L"\n" );
L"\n" );
/* /*
Stop recursing at event handler. No reason to belive that Stop recursing at event handler. No reason to belive that
@ -897,18 +883,18 @@ void parser_t::stack_trace( block_t *b, string_buffer_t *buff)
case SOURCE: case SOURCE:
{ {
const wcstring &source_dest = b->state1<wcstring>(); const wcstring &source_dest = b->state1<wcstring>();
sb_printf( buff, _(L"in . (source) call of file '%ls',\n"), source_dest.c_str() ); append_format( buff, _(L"in . (source) call of file '%ls',\n"), source_dest.c_str() );
break; break;
} }
case FUNCTION_CALL: case FUNCTION_CALL:
{ {
const wcstring &function_call_name = b->state1<wcstring>(); const wcstring &function_call_name = b->state1<wcstring>();
sb_printf( buff, _(L"in function '%ls',\n"), function_call_name.c_str() ); append_format( buff, _(L"in function '%ls',\n"), function_call_name.c_str() );
break; break;
} }
case SUBST: case SUBST:
{ {
sb_printf( buff, _(L"in command substitution\n") ); append_format( buff, _(L"in command substitution\n") );
break; break;
} }
} }
@ -917,14 +903,14 @@ void parser_t::stack_trace( block_t *b, string_buffer_t *buff)
if( file ) if( file )
{ {
sb_printf( buff, append_format( buff,
_(L"\tcalled on line %d of file '%ls',\n"), _(L"\tcalled on line %d of file '%ls',\n"),
b->src_lineno, b->src_lineno,
file ); file );
} }
else else
{ {
sb_printf( buff, append_format( buff,
_(L"\tcalled on standard input,\n") ); _(L"\tcalled on standard input,\n") );
} }
@ -940,14 +926,13 @@ void parser_t::stack_trace( block_t *b, string_buffer_t *buff)
{ {
sb_append( &tmp, i>1?L" ":L"", process->argv(i), NULL ); sb_append( &tmp, i>1?L" ":L"", process->argv(i), NULL );
} }
sb_printf( buff, _(L"\twith parameter list '%ls'\n"), (wchar_t *)tmp.buff ); append_format( buff, _(L"\twith parameter list '%ls'\n"), (wchar_t *)tmp.buff );
sb_destroy( &tmp ); sb_destroy( &tmp );
} }
} }
sb_printf( buff, append_format( buff, L"\n" );
L"\n" );
} }
/* /*
@ -1091,12 +1076,8 @@ const wchar_t *parser_t::current_line()
if( !line ) if( !line )
return L""; return L"";
if( !lineinfo )
{ lineinfo.clear();
lineinfo = (string_buffer_t *)malloc( sizeof(string_buffer_t) );
sb_init( lineinfo );
}
sb_clear( lineinfo );
/* /*
Calculate line number, line offset, etc. Calculate line number, line offset, etc.
@ -1136,18 +1117,18 @@ const wchar_t *parser_t::current_line()
*/ */
if( !is_interactive || is_function() ) if( !is_interactive || is_function() )
{ {
int prev_width = my_wcswidth( (wchar_t *)lineinfo->buff ); int prev_width = my_wcswidth( lineinfo.c_str() );
if( file ) if( file )
sb_printf( lineinfo, append_format( lineinfo,
_(L"%ls (line %d): "), _(L"%ls (line %d): "),
file, file,
lineno ); lineno );
else else
sb_printf( lineinfo, append_format( lineinfo,
L"%ls: ", L"%ls: ",
_(L"Standard input"), _(L"Standard input"),
lineno ); lineno );
offset = my_wcswidth( (wchar_t *)lineinfo->buff ) - prev_width; offset = my_wcswidth( lineinfo.c_str() ) - prev_width;
} }
else else
{ {
@ -1164,7 +1145,7 @@ const wchar_t *parser_t::current_line()
// Workaround since it seems impossible to print 0 copies of a character using %*lc // Workaround since it seems impossible to print 0 copies of a character using %*lc
if( offset+current_line_width ) if( offset+current_line_width )
{ {
sb_printf( lineinfo, append_format( lineinfo,
L"%ls\n%*lc^\n", L"%ls\n%*lc^\n",
line, line,
offset+current_line_width, offset+current_line_width,
@ -1172,7 +1153,7 @@ const wchar_t *parser_t::current_line()
} }
else else
{ {
sb_printf( lineinfo, append_format( lineinfo,
L"%ls\n^\n", L"%ls\n^\n",
line ); line );
} }
@ -1181,7 +1162,7 @@ const wchar_t *parser_t::current_line()
free( line ); free( line );
parser_t::stack_trace( current_block, lineinfo ); parser_t::stack_trace( current_block, lineinfo );
return (wchar_t *)lineinfo->buff; return lineinfo.c_str();
} }
int parser_t::get_pos() const int parser_t::get_pos() const
@ -2560,7 +2541,7 @@ const wchar_t *parser_get_block_command( int type )
syntax errors in command substitutions, improperly escaped syntax errors in command substitutions, improperly escaped
characters and improper use of the variable expansion operator. characters and improper use of the variable expansion operator.
*/ */
int parser_t::parser_test_argument( const wchar_t *arg, string_buffer_t *out, const wchar_t *prefix, int offset ) int parser_t::parser_test_argument( const wchar_t *arg, wcstring *out, const wchar_t *prefix, int offset )
{ {
wchar_t *unesc; wchar_t *unesc;
wchar_t *pos; wchar_t *pos;
@ -2588,7 +2569,7 @@ int parser_t::parser_test_argument( const wchar_t *arg, string_buffer_t *out, co
error( SYNTAX_ERROR, error( SYNTAX_ERROR,
offset, offset,
L"Mismatched parans" ); L"Mismatched parans" );
this->print_errors( out, prefix); this->print_errors( *out, prefix);
} }
free( arg_cpy ); free( arg_cpy );
return 1; return 1;
@ -2634,7 +2615,7 @@ int parser_t::parser_test_argument( const wchar_t *arg, string_buffer_t *out, co
error( SYNTAX_ERROR, error( SYNTAX_ERROR,
offset, offset,
L"Invalid token '%ls'", arg_cpy ); L"Invalid token '%ls'", arg_cpy );
print_errors( out, prefix); print_errors( *out, prefix);
} }
return 1; return 1;
} }
@ -2660,7 +2641,7 @@ int parser_t::parser_test_argument( const wchar_t *arg, string_buffer_t *out, co
if( out ) if( out )
{ {
expand_variable_error( *this, unesc, pos-unesc, offset ); expand_variable_error( *this, unesc, pos-unesc, offset );
print_errors( out, prefix); print_errors( *out, prefix);
} }
} }
@ -2677,8 +2658,7 @@ int parser_t::parser_test_argument( const wchar_t *arg, string_buffer_t *out, co
} }
int parser_t::test_args(const wchar_t * buff, int parser_t::test_args(const wchar_t * buff, wcstring *out, const wchar_t *prefix )
string_buffer_t *out, const wchar_t *prefix )
{ {
tokenizer tok; tokenizer tok;
tokenizer *previous_tokenizer = current_tokenizer; tokenizer *previous_tokenizer = current_tokenizer;
@ -2717,7 +2697,7 @@ int parser_t::test_args(const wchar_t * buff,
tok_get_pos( &tok ), tok_get_pos( &tok ),
TOK_ERR_MSG, TOK_ERR_MSG,
tok_last(&tok) ); tok_last(&tok) );
print_errors( out, prefix ); print_errors( *out, prefix );
} }
err=1; err=1;
do_loop=0; do_loop=0;
@ -2732,7 +2712,7 @@ int parser_t::test_args(const wchar_t * buff,
tok_get_pos( &tok ), tok_get_pos( &tok ),
UNEXPECTED_TOKEN_ERR_MSG, UNEXPECTED_TOKEN_ERR_MSG,
tok_get_desc( tok_last_type(&tok)) ); tok_get_desc( tok_last_type(&tok)) );
print_errors( out, prefix ); print_errors( *out, prefix );
} }
err=1; err=1;
do_loop=0; do_loop=0;
@ -2753,7 +2733,7 @@ int parser_t::test_args(const wchar_t * buff,
int parser_t::test( const wchar_t * buff, int parser_t::test( const wchar_t * buff,
int *block_level, int *block_level,
string_buffer_t *out, wcstring *out,
const wchar_t *prefix ) const wchar_t *prefix )
{ {
ASSERT_IS_MAIN_THREAD(); ASSERT_IS_MAIN_THREAD();
@ -2851,7 +2831,7 @@ int parser_t::test( const wchar_t * buff,
ILLEGAL_CMD_ERR_MSG, ILLEGAL_CMD_ERR_MSG,
tok_last( &tok ) ); tok_last( &tok ) );
print_errors( out, prefix ); print_errors( *out, prefix );
} }
break; break;
} }
@ -2873,7 +2853,7 @@ int parser_t::test( const wchar_t * buff,
tok_get_pos( &tok ), tok_get_pos( &tok ),
COND_ERR_MSG ); COND_ERR_MSG );
print_errors( out, prefix ); print_errors( *out, prefix );
} }
} }
@ -2909,11 +2889,13 @@ int parser_t::test( const wchar_t * buff,
{ {
if( count >= BLOCK_MAX_COUNT ) if( count >= BLOCK_MAX_COUNT )
{ {
error( SYNTAX_ERROR, if (out) {
tok_get_pos( &tok ), error( SYNTAX_ERROR,
BLOCK_ERR_MSG ); tok_get_pos( &tok ),
BLOCK_ERR_MSG );
print_errors( out, prefix ); print_errors( *out, prefix );
}
} }
else else
{ {
@ -2953,7 +2935,7 @@ int parser_t::test( const wchar_t * buff,
tok_get_pos( &tok ), tok_get_pos( &tok ),
EXEC_ERR_MSG ); EXEC_ERR_MSG );
print_errors( out, prefix ); print_errors( *out, prefix );
} }
} }
@ -2975,7 +2957,7 @@ int parser_t::test( const wchar_t * buff,
tok_get_pos( &tok ), tok_get_pos( &tok ),
EXEC_ERR_MSG ); EXEC_ERR_MSG );
print_errors( out, prefix ); print_errors( *out, prefix );
} }
} }
@ -2997,10 +2979,10 @@ int parser_t::test( const wchar_t * buff,
tok_get_pos( &tok ), tok_get_pos( &tok ),
INVALID_CASE_ERR_MSG ); INVALID_CASE_ERR_MSG );
print_errors( out, prefix); print_errors( *out, prefix);
const wcstring h = builtin_help_get( *this, L"case" ); const wcstring h = builtin_help_get( *this, L"case" );
if( h.size() ) if( h.size() )
sb_printf( out, L"%ls", h.c_str() ); append_format( *out, L"%ls", h.c_str() );
} }
} }
} }
@ -3053,7 +3035,7 @@ int parser_t::test( const wchar_t * buff,
error( SYNTAX_ERROR, error( SYNTAX_ERROR,
tok_get_pos( &tok ), tok_get_pos( &tok ),
INVALID_RETURN_ERR_MSG ); INVALID_RETURN_ERR_MSG );
print_errors( out, prefix ); print_errors( *out, prefix );
} }
} }
} }
@ -3111,7 +3093,7 @@ int parser_t::test( const wchar_t * buff,
error( SYNTAX_ERROR, error( SYNTAX_ERROR,
tok_get_pos( &tok ), tok_get_pos( &tok ),
INVALID_LOOP_ERR_MSG ); INVALID_LOOP_ERR_MSG );
print_errors( out, prefix ); print_errors( *out, prefix );
} }
} }
} }
@ -3132,7 +3114,7 @@ int parser_t::test( const wchar_t * buff,
tok_get_pos( &tok ), tok_get_pos( &tok ),
INVALID_ELSE_ERR_MSG ); INVALID_ELSE_ERR_MSG );
print_errors( out, prefix ); print_errors( *out, prefix );
} }
} }
@ -3149,10 +3131,10 @@ int parser_t::test( const wchar_t * buff,
error( SYNTAX_ERROR, error( SYNTAX_ERROR,
tok_get_pos( &tok ), tok_get_pos( &tok ),
INVALID_END_ERR_MSG ); INVALID_END_ERR_MSG );
print_errors( out, prefix ); print_errors( *out, prefix );
const wcstring h = builtin_help_get( *this, L"end" ); const wcstring h = builtin_help_get( *this, L"end" );
if( h.size() ) if( h.size() )
sb_printf( out, L"%ls", h.c_str() ); append_format( *out, L"%ls", h.c_str() );
} }
} }
@ -3197,7 +3179,7 @@ int parser_t::test( const wchar_t * buff,
L"for", L"for",
tok_last( &tok ) ); tok_last( &tok ) );
print_errors( out, prefix ); print_errors( *out, prefix );
} }
} }
@ -3215,7 +3197,7 @@ int parser_t::test( const wchar_t * buff,
BUILTIN_FOR_ERR_IN, BUILTIN_FOR_ERR_IN,
L"for" ); L"for" );
print_errors( out, prefix ); print_errors( *out, prefix );
} }
} }
} }
@ -3241,7 +3223,7 @@ int parser_t::test( const wchar_t * buff,
error( SYNTAX_ERROR, error( SYNTAX_ERROR,
tok_get_pos( &tok ), tok_get_pos( &tok ),
INVALID_REDIRECTION_ERR_MSG ); INVALID_REDIRECTION_ERR_MSG );
print_errors( out, prefix ); print_errors( *out, prefix );
} }
} }
break; break;
@ -3258,7 +3240,7 @@ int parser_t::test( const wchar_t * buff,
tok_get_pos( &tok ), tok_get_pos( &tok ),
CMD_ERR_MSG, CMD_ERR_MSG,
tok_get_desc( tok_last_type(&tok))); tok_get_desc( tok_last_type(&tok)));
print_errors( out, prefix ); print_errors( *out, prefix );
} }
} }
needs_cmd=0; needs_cmd=0;
@ -3293,7 +3275,7 @@ int parser_t::test( const wchar_t * buff,
tok_get_desc( tok_last_type(&tok))); tok_get_desc( tok_last_type(&tok)));
} }
print_errors( out, prefix ); print_errors( *out, prefix );
} }
} }
else if( forbid_pipeline ) else if( forbid_pipeline )
@ -3305,7 +3287,7 @@ int parser_t::test( const wchar_t * buff,
tok_get_pos( &tok ), tok_get_pos( &tok ),
EXEC_ERR_MSG ); EXEC_ERR_MSG );
print_errors( out, prefix ); print_errors( *out, prefix );
} }
} }
else else
@ -3342,7 +3324,7 @@ int parser_t::test( const wchar_t * buff,
tok_get_desc( tok_last_type(&tok))); tok_get_desc( tok_last_type(&tok)));
} }
print_errors( out, prefix ); print_errors( *out, prefix );
} }
} }
@ -3369,7 +3351,7 @@ int parser_t::test( const wchar_t * buff,
tok_last(&tok) ); tok_last(&tok) );
print_errors( out, prefix ); print_errors( *out, prefix );
} }
} }
@ -3395,7 +3377,7 @@ int parser_t::test( const wchar_t * buff,
L"for", L"for",
arg_count ); arg_count );
print_errors( out, prefix ); print_errors( *out, prefix );
} }
} }
@ -3417,7 +3399,7 @@ int parser_t::test( const wchar_t * buff,
tok_get_pos( &tok ), tok_get_pos( &tok ),
COND_ERR_MSG ); COND_ERR_MSG );
print_errors( out, prefix ); print_errors( *out, prefix );
} }
} }
@ -3430,7 +3412,7 @@ int parser_t::test( const wchar_t * buff,
block_pos[count-1], block_pos[count-1],
BLOCK_END_ERR_MSG ); BLOCK_END_ERR_MSG );
print_errors( out, prefix ); print_errors( *out, prefix );
cmd = parser_get_block_command( block_type[count -1] ); cmd = parser_get_block_command( block_type[count -1] );
if( cmd ) if( cmd )
@ -3438,7 +3420,7 @@ int parser_t::test( const wchar_t * buff,
const wcstring h = builtin_help_get( *this, cmd ); const wcstring h = builtin_help_get( *this, cmd );
if( h.size() ) if( h.size() )
{ {
sb_printf( out, L"%ls", h.c_str() ); append_format( *out, L"%ls", h.c_str() );
} }
} }

View File

@ -284,7 +284,7 @@ class parser_t {
tokenizer *current_tokenizer; tokenizer *current_tokenizer;
/** String for representing the current line */ /** String for representing the current line */
string_buffer_t *lineinfo; wcstring lineinfo;
/** This is the position of the beginning of the currently parsed command */ /** This is the position of the beginning of the currently parsed command */
int current_tokenizer_pos; int current_tokenizer_pos;
@ -318,8 +318,8 @@ class parser_t {
int parse_job( process_t *p, job_t *j, tokenizer *tok ); int parse_job( process_t *p, job_t *j, tokenizer *tok );
void skipped_exec( job_t * j ); void skipped_exec( job_t * j );
void eval_job( tokenizer *tok ); void eval_job( tokenizer *tok );
int parser_test_argument( const wchar_t *arg, string_buffer_t *out, const wchar_t *prefix, int offset ); int parser_test_argument( const wchar_t *arg, wcstring *out, const wchar_t *prefix, int offset );
void print_errors( string_buffer_t *target, const wchar_t *prefix ); void print_errors( wcstring &target, const wchar_t *prefix );
void print_errors_stderr(); void print_errors_stderr();
public: public:
@ -434,7 +434,7 @@ class parser_t {
\param out if non-null, any errors in the command will be filled out into this buffer \param out if non-null, any errors in the command will be filled out into this buffer
\param prefix the prefix string to prepend to each error message written to the \c out buffer \param prefix the prefix string to prepend to each error message written to the \c out buffer
*/ */
int test( const wchar_t * buff, int *block_level, string_buffer_t *out, const wchar_t *prefix ); int test( const wchar_t * buff, int *block_level, wcstring *out, const wchar_t *prefix );
/** /**
Test if the specified string can be parsed as an argument list, Test if the specified string can be parsed as an argument list,
@ -442,7 +442,7 @@ class parser_t {
string contains errors, and the second bit is set if the string string contains errors, and the second bit is set if the string
contains an unclosed block. contains an unclosed block.
*/ */
int test_args( const wchar_t * buff, string_buffer_t *out, const wchar_t *prefix ); int test_args( const wchar_t * buff, wcstring *out, const wchar_t *prefix );
/** /**
Tell the parser that the specified function may not be run if not Tell the parser that the specified function may not be run if not
@ -481,9 +481,9 @@ class parser_t {
const wchar_t *current_filename() const; const wchar_t *current_filename() const;
/** /**
Write a stack trace starting at the specified block to the specified string_buffer_t Write a stack trace starting at the specified block to the specified wcstring
*/ */
void stack_trace( block_t *b, string_buffer_t *buff); void stack_trace( block_t *b, wcstring &buff);
int get_block_type( const wchar_t *cmd ) const; int get_block_type( const wchar_t *cmd ) const;
const wchar_t *get_block_command( int type ) const; const wchar_t *get_block_command( int type ) const;

View File

@ -2219,8 +2219,7 @@ int reader_shell_test( const wchar_t *b )
if( res & PARSER_TEST_ERROR ) if( res & PARSER_TEST_ERROR )
{ {
string_buffer_t sb; wcstring sb;
sb_init( &sb );
int tmp[1]; int tmp[1];
int tmp2[1]; int tmp2[1];
@ -2228,8 +2227,7 @@ int reader_shell_test( const wchar_t *b )
s_write( &data->screen, L"", L"", tmp, tmp2, 0 ); s_write( &data->screen, L"", L"", tmp, tmp2, 0 );
parser_t::principal_parser().test( b, 0, &sb, L"fish" ); parser_t::principal_parser().test( b, 0, &sb, L"fish" );
fwprintf( stderr, L"%ls", sb.buff ); fwprintf( stderr, L"%ls", sb.c_str() );
sb_destroy( &sb );
} }
return res; return res;
} }
@ -3395,20 +3393,16 @@ static int read_ni( int fd, io_data_t *io )
if( str ) if( str )
{ {
string_buffer_t sb; wcstring sb;
sb_init( &sb );
if( ! parser.test( str, 0, &sb, L"fish" ) ) if( ! parser.test( str, 0, &sb, L"fish" ) )
{ {
parser.eval( str, io, TOP ); parser.eval( str, io, TOP );
} }
else else
{ {
fwprintf( stderr, L"%ls", sb.buff ); fwprintf( stderr, L"%ls", sb.c_str() );
res = 1; res = 1;
} }
sb_destroy( &sb );
free( str ); free( str );
} }
else else