mirror of
https://github.com/fish-shell/fish-shell.git
synced 2025-03-27 14:45:13 +08:00
Remove string_buffer_t (!)
This commit is contained in:
parent
a9313fc0c3
commit
8a46931e34
@ -168,7 +168,7 @@ static int builtin_count_args( wchar_t **argv )
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
This function works like wperror, but it prints its result into
|
This function works like wperror, but it prints its result into
|
||||||
the sb_err string_buffer_t instead of to stderr. Used by the builtin
|
the sb_err string instead of to stderr. Used by the builtin
|
||||||
commands.
|
commands.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
13
builtin.h
13
builtin.h
@ -89,15 +89,6 @@ enum
|
|||||||
#define BUILTIN_END_BLOCK_UNKNOWN _( L"%ls: Unknown block type '%ls'\n" )
|
#define BUILTIN_END_BLOCK_UNKNOWN _( L"%ls: Unknown block type '%ls'\n" )
|
||||||
|
|
||||||
#define BUILTIN_ERR_NOT_NUMBER _( L"%ls: Argument '%ls' is not a number\n" )
|
#define BUILTIN_ERR_NOT_NUMBER _( L"%ls: Argument '%ls' is not a number\n" )
|
||||||
/**
|
|
||||||
Stringbuffer used to represent standard output
|
|
||||||
*/
|
|
||||||
//extern string_buffer_t *sb_out;
|
|
||||||
|
|
||||||
/**
|
|
||||||
Stringbuffer used to represent standard error
|
|
||||||
*/
|
|
||||||
//extern string_buffer_t *sb_err;
|
|
||||||
|
|
||||||
/** Get the string used to represent stdout and stderr */
|
/** Get the string used to represent stdout and stderr */
|
||||||
const wcstring &get_stdout_buffer();
|
const wcstring &get_stdout_buffer();
|
||||||
@ -150,12 +141,12 @@ wcstring_list_t builtin_get_names(void);
|
|||||||
void builtin_get_names(std::vector<completion_t> &list);
|
void builtin_get_names(std::vector<completion_t> &list);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Pushes a new set of input/output to the stack. The new stdin is supplied, a new set of output string_buffer_ts is created.
|
Pushes a new set of input/output to the stack. The new stdin is supplied, a new set of output strings is created.
|
||||||
*/
|
*/
|
||||||
void builtin_push_io( parser_t &parser, int stdin_fd );
|
void builtin_push_io( parser_t &parser, int stdin_fd );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Pops a set of input/output from the stack. The output string_buffer_ts are destroued, but the input file is not closed.
|
Pops a set of input/output from the stack. The output strings are destroued, but the input file is not closed.
|
||||||
*/
|
*/
|
||||||
void builtin_pop_io(parser_t &parser);
|
void builtin_pop_io(parser_t &parser);
|
||||||
|
|
||||||
|
@ -227,9 +227,9 @@ void complete_remove( const wchar_t *cmd,
|
|||||||
void complete( const wcstring &cmd, std::vector<completion_t> &comp, complete_type_t type, wcstring_list_t *to_load = NULL );
|
void complete( const wcstring &cmd, std::vector<completion_t> &comp, complete_type_t type, wcstring_list_t *to_load = NULL );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Print a list of all current completions into the string_buffer_t.
|
Print a list of all current completions into the string.
|
||||||
|
|
||||||
\param out The string_buffer_t to write completions to
|
\param out The string to write completions to
|
||||||
*/
|
*/
|
||||||
void complete_print( wcstring &out );
|
void complete_print( wcstring &out );
|
||||||
|
|
||||||
|
@ -156,7 +156,7 @@ static const wchar_t *hightlight_var[] =
|
|||||||
;
|
;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
This string_buffer_t contains the text that should be sent back to the calling program
|
This string contains the text that should be sent back to the calling program
|
||||||
*/
|
*/
|
||||||
static wcstring out_buff;
|
static wcstring out_buff;
|
||||||
/**
|
/**
|
||||||
|
@ -715,7 +715,7 @@ void parser_t::destroy()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Print error message to string_buffer_t if an error has occured while parsing
|
Print error message to string if an error has occured while parsing
|
||||||
|
|
||||||
\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.
|
||||||
|
181
util.cpp
181
util.cpp
@ -163,190 +163,9 @@ int wcsfilecmp( const wchar_t *a, const wchar_t *b )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void sb_init( string_buffer_t * b)
|
|
||||||
{
|
|
||||||
wchar_t c=0;
|
|
||||||
|
|
||||||
CHECK( b, );
|
|
||||||
|
|
||||||
if( !b )
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset( b, 0, sizeof(string_buffer_t) );
|
|
||||||
b_append( b, &c, sizeof( wchar_t));
|
|
||||||
b->used -= sizeof(wchar_t);
|
|
||||||
}
|
|
||||||
|
|
||||||
void sb_append_substring( string_buffer_t *b, const wchar_t *s, size_t l )
|
|
||||||
{
|
|
||||||
wchar_t tmp=0;
|
|
||||||
|
|
||||||
CHECK( b, );
|
|
||||||
CHECK( s, );
|
|
||||||
|
|
||||||
b_append( b, s, sizeof(wchar_t)*l );
|
|
||||||
b_append( b, &tmp, sizeof(wchar_t) );
|
|
||||||
b->used -= sizeof(wchar_t);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void sb_append_char( string_buffer_t *b, wchar_t c )
|
|
||||||
{
|
|
||||||
wchar_t tmp=0;
|
|
||||||
|
|
||||||
CHECK( b, );
|
|
||||||
|
|
||||||
b_append( b, &c, sizeof(wchar_t) );
|
|
||||||
b_append( b, &tmp, sizeof(wchar_t) );
|
|
||||||
b->used -= sizeof(wchar_t);
|
|
||||||
}
|
|
||||||
|
|
||||||
void sb_append_internal( string_buffer_t *b, ... )
|
|
||||||
{
|
|
||||||
va_list va;
|
|
||||||
wchar_t *arg;
|
|
||||||
|
|
||||||
CHECK( b, );
|
|
||||||
|
|
||||||
va_start( va, b );
|
|
||||||
while( (arg=va_arg(va, wchar_t *) )!= 0 )
|
|
||||||
{
|
|
||||||
b_append( b, arg, sizeof(wchar_t)*(wcslen(arg)+1) );
|
|
||||||
b->used -= sizeof(wchar_t);
|
|
||||||
}
|
|
||||||
va_end( va );
|
|
||||||
}
|
|
||||||
|
|
||||||
int sb_printf( string_buffer_t *buffer, const wchar_t *format, ... )
|
|
||||||
{
|
|
||||||
va_list va;
|
|
||||||
int res;
|
|
||||||
|
|
||||||
CHECK( buffer, -1 );
|
|
||||||
CHECK( format, -1 );
|
|
||||||
|
|
||||||
va_start( va, format );
|
|
||||||
res = sb_vprintf( buffer, format, va );
|
|
||||||
va_end( va );
|
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sb_vprintf( string_buffer_t *buffer, const wchar_t *format, va_list va_orig )
|
|
||||||
{
|
|
||||||
int res;
|
|
||||||
|
|
||||||
CHECK( buffer, -1 );
|
|
||||||
CHECK( format, -1 );
|
|
||||||
|
|
||||||
if( !buffer->length )
|
|
||||||
{
|
|
||||||
buffer->length = MIN_SIZE;
|
|
||||||
buffer->buff = (char *)malloc( MIN_SIZE );
|
|
||||||
if( !buffer->buff )
|
|
||||||
{
|
|
||||||
oom_handler( buffer );
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
while( 1 )
|
|
||||||
{
|
|
||||||
va_list va;
|
|
||||||
va_copy( va, va_orig );
|
|
||||||
|
|
||||||
res = vswprintf( (wchar_t *)((char *)buffer->buff+buffer->used),
|
|
||||||
(buffer->length-buffer->used)/sizeof(wchar_t),
|
|
||||||
format,
|
|
||||||
va );
|
|
||||||
|
|
||||||
|
|
||||||
va_end( va );
|
|
||||||
if( res >= 0 )
|
|
||||||
{
|
|
||||||
buffer->used+= res*sizeof(wchar_t);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
As far as I know, there is no way to check if a
|
|
||||||
vswprintf-call failed because of a badly formated string
|
|
||||||
option or because the supplied destination string was to
|
|
||||||
small. In GLIBC, errno seems to be set to EINVAL either way.
|
|
||||||
|
|
||||||
Because of this, sb_printf will on failiure try to
|
|
||||||
increase the buffer size until the free space is
|
|
||||||
larger than SB_MAX_SIZE, at which point it will
|
|
||||||
conclude that the error was probably due to a badly
|
|
||||||
formated string option, and return an error. Make
|
|
||||||
sure to null terminate string before that, though.
|
|
||||||
*/
|
|
||||||
|
|
||||||
if( buffer->length - buffer->used > SB_MAX_SIZE )
|
|
||||||
{
|
|
||||||
wchar_t tmp=0;
|
|
||||||
b_append( buffer, &tmp, sizeof(wchar_t) );
|
|
||||||
buffer->used -= sizeof(wchar_t);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer->buff = (char *)realloc( buffer->buff, 2*buffer->length );
|
|
||||||
|
|
||||||
if( !buffer->buff )
|
|
||||||
{
|
|
||||||
oom_handler( buffer );
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer->length *= 2;
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void sb_destroy( string_buffer_t * b )
|
|
||||||
{
|
|
||||||
CHECK( b, );
|
|
||||||
|
|
||||||
free( b->buff );
|
|
||||||
}
|
|
||||||
|
|
||||||
void sb_clear( string_buffer_t * b )
|
|
||||||
{
|
|
||||||
sb_truncate( b, 0 );
|
|
||||||
assert( !wcslen( (wchar_t *)b->buff) );
|
|
||||||
}
|
|
||||||
|
|
||||||
void sb_truncate( string_buffer_t *b, int chars_left )
|
|
||||||
{
|
|
||||||
wchar_t *arr;
|
|
||||||
|
|
||||||
CHECK( b, );
|
|
||||||
|
|
||||||
b->used = (chars_left)*sizeof( wchar_t);
|
|
||||||
arr = (wchar_t *)b->buff;
|
|
||||||
arr[chars_left] = 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t sb_length( string_buffer_t *b )
|
|
||||||
{
|
|
||||||
CHECK( b, -1 );
|
|
||||||
return (b->used-1)/sizeof( wchar_t);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void b_init( buffer_t *b)
|
void b_init( buffer_t *b)
|
||||||
{
|
{
|
||||||
CHECK( b, );
|
CHECK( b, );
|
||||||
|
76
util.h
76
util.h
@ -26,13 +26,6 @@ typedef struct buffer
|
|||||||
}
|
}
|
||||||
buffer_t;
|
buffer_t;
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
String buffer struct. An autoallocating buffer used for
|
|
||||||
concatenating strings. This is really just a buffer_t.
|
|
||||||
*/
|
|
||||||
typedef buffer_t string_buffer_t;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Set the out-of-memory handler callback function. If a memory
|
Set the out-of-memory handler callback function. If a memory
|
||||||
allocation fails, this function will be called.
|
allocation fails, this function will be called.
|
||||||
@ -95,75 +88,6 @@ int mini( int a, int b );
|
|||||||
int wcsfilecmp( const wchar_t *a, const wchar_t *b );
|
int wcsfilecmp( const wchar_t *a, const wchar_t *b );
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
String buffer functions
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
Initialize the specified string_buffer
|
|
||||||
*/
|
|
||||||
void sb_init( string_buffer_t * );
|
|
||||||
|
|
||||||
/**
|
|
||||||
Append a part of a string to the buffer.
|
|
||||||
*/
|
|
||||||
void sb_append_substring( string_buffer_t *, const wchar_t *, size_t );
|
|
||||||
|
|
||||||
/**
|
|
||||||
Append a character to the buffer.
|
|
||||||
*/
|
|
||||||
void sb_append_char( string_buffer_t *, wchar_t );
|
|
||||||
|
|
||||||
/**
|
|
||||||
Append all specified items to buffer.
|
|
||||||
*/
|
|
||||||
#define sb_append( sb,... ) sb_append_internal( sb, __VA_ARGS__, NULL )
|
|
||||||
|
|
||||||
/**
|
|
||||||
Append a null terminated list of strings to the buffer.
|
|
||||||
Example:
|
|
||||||
|
|
||||||
sb_append2( my_buff, L"foo", L"bar", NULL );
|
|
||||||
|
|
||||||
Do not forget to cast the last 0 to (void *), or you might encounter errors on 64-bit platforms!
|
|
||||||
*/
|
|
||||||
__sentinel void sb_append_internal( string_buffer_t *, ... );
|
|
||||||
|
|
||||||
/**
|
|
||||||
Append formated string data to the buffer. This function internally
|
|
||||||
relies on \c vswprintf, so any filter options supported by that
|
|
||||||
function is also supported by this function.
|
|
||||||
*/
|
|
||||||
int sb_printf( string_buffer_t *buffer, const wchar_t *format, ... );
|
|
||||||
|
|
||||||
/**
|
|
||||||
Vararg version of sb_printf.
|
|
||||||
*/
|
|
||||||
int sb_vprintf( string_buffer_t *buffer, const wchar_t *format, va_list va_orig );
|
|
||||||
|
|
||||||
/**
|
|
||||||
Destroy the buffer and free its memory
|
|
||||||
*/
|
|
||||||
void sb_destroy( string_buffer_t * );
|
|
||||||
|
|
||||||
/**
|
|
||||||
Completely truncate the buffer. This will not deallocate the memory
|
|
||||||
used, it will only set the contents of the string to L"\\0".
|
|
||||||
*/
|
|
||||||
void sb_clear( string_buffer_t * );
|
|
||||||
|
|
||||||
/**
|
|
||||||
Truncate the string to the specified number of characters. This
|
|
||||||
will not deallocate the memory used.
|
|
||||||
*/
|
|
||||||
void sb_truncate( string_buffer_t *, int chars_left );
|
|
||||||
|
|
||||||
/**
|
|
||||||
Return the number of characters in the string
|
|
||||||
*/
|
|
||||||
ssize_t sb_length( string_buffer_t * );
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Buffer functions
|
Buffer functions
|
||||||
*/
|
*/
|
||||||
|
@ -51,11 +51,6 @@ typedef std::string cstring;
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
|
||||||
For wgettext: Number of string_buffer_t in the ring of buffers
|
|
||||||
*/
|
|
||||||
#define BUFF_COUNT 4
|
|
||||||
|
|
||||||
/* Lock to protect wgettext */
|
/* Lock to protect wgettext */
|
||||||
static pthread_mutex_t wgettext_lock;
|
static pthread_mutex_t wgettext_lock;
|
||||||
|
|
||||||
|
13
wutil.h
13
wutil.h
@ -17,19 +17,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
Wide version of the dirent data structure
|
|
||||||
*/
|
|
||||||
struct wdirent
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
The name of the current directory
|
|
||||||
*/
|
|
||||||
wchar_t *d_name;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Call this function on startup to create internal wutil
|
Call this function on startup to create internal wutil
|
||||||
resources. This function doesn't do anything.
|
resources. This function doesn't do anything.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user