Additional warning fixes and migration from int to size_t or long where appropriate

This commit is contained in:
ridiculousfish 2012-08-04 13:47:56 -07:00
parent 7a46227141
commit 54ceb4211e
7 changed files with 228 additions and 287 deletions

View file

@ -61,7 +61,8 @@ static const wchar_t *current_buffer=0;
What the commandline builtin considers to be the current cursor What the commandline builtin considers to be the current cursor
position. position.
*/ */
static int current_cursor_pos = -1; static const size_t kInvalidCursorPosition = (size_t)(-1);
static size_t current_cursor_pos = kInvalidCursorPosition;
/** /**
Returns the current commandline buffer. Returns the current commandline buffer.
@ -74,7 +75,7 @@ static const wchar_t *get_buffer()
/** /**
Returns the position of the cursor Returns the position of the cursor
*/ */
static int get_cursor_pos() static size_t get_cursor_pos()
{ {
return current_cursor_pos; return current_cursor_pos;
} }
@ -94,7 +95,7 @@ static void replace_part( const wchar_t *begin,
int append_mode ) int append_mode )
{ {
const wchar_t *buff = get_buffer(); const wchar_t *buff = get_buffer();
long out_pos=get_cursor_pos(); size_t out_pos = get_cursor_pos();
wcstring out; wcstring out;
@ -127,7 +128,7 @@ static void replace_part( const wchar_t *begin,
} }
} }
out.append( end ); out.append( end );
reader_set_buffer( out, (size_t)out_pos ); reader_set_buffer( out, out_pos );
} }
/** /**
@ -222,12 +223,12 @@ static int builtin_commandline( parser_t &parser, wchar_t **argv )
current_buffer = (wchar_t *)builtin_complete_get_temporary_buffer(); current_buffer = (wchar_t *)builtin_complete_get_temporary_buffer();
if( current_buffer ) if( current_buffer )
{ {
current_cursor_pos = (int)wcslen( current_buffer ); current_cursor_pos = wcslen( current_buffer );
} }
else else
{ {
current_buffer = reader_get_buffer(); current_buffer = reader_get_buffer();
current_cursor_pos = (int)reader_get_cursor_pos(); current_cursor_pos = reader_get_cursor_pos();
} }
if( !get_buffer() ) if( !get_buffer() )
@ -387,7 +388,7 @@ static int builtin_commandline( parser_t &parser, wchar_t **argv )
case 'I': case 'I':
current_buffer = woptarg; current_buffer = woptarg;
current_cursor_pos = (int)wcslen( woptarg ); current_cursor_pos = wcslen( woptarg );
break; break;
case 'C': case 'C':
@ -551,7 +552,7 @@ static int builtin_commandline( parser_t &parser, wchar_t **argv )
} }
else else
{ {
append_format(stdout_buffer, L"%d\n", reader_get_cursor_pos() ); append_format(stdout_buffer, L"%lu\n", (unsigned long)reader_get_cursor_pos() );
return 0; return 0;
} }
@ -559,9 +560,9 @@ static int builtin_commandline( parser_t &parser, wchar_t **argv )
if( line_mode ) if( line_mode )
{ {
int pos = reader_get_cursor_pos(); size_t pos = reader_get_cursor_pos();
const wchar_t *buff = reader_get_buffer(); const wchar_t *buff = reader_get_buffer();
append_format(stdout_buffer, L"%d\n", parse_util_lineno( buff, pos ) ); append_format(stdout_buffer, L"%lu\n", (unsigned long)parse_util_lineno( buff, pos ) );
return 0; return 0;
} }

View file

@ -51,53 +51,10 @@
*/ */
#define AUTOLOAD_MIN_AGE 60 #define AUTOLOAD_MIN_AGE 60
int parse_util_lineno( const wchar_t *str, size_t offset )
int parse_util_lineno( const wchar_t *str, int len )
{ {
/** int res = 0;
First cached state for( size_t i=0; str[i] && i<offset; i++ )
*/
static wchar_t *prev_str = 0;
static int i=0;
static int res = 1;
/**
Second cached state
*/
static wchar_t *prev_str2 = 0;
static int i2 = 0;
static int res2 = 1;
CHECK( str, 0 );
if( str != prev_str || i>len )
{
if( prev_str2 == str && i2 <= len )
{
wchar_t *tmp_str = prev_str;
int tmp_i = i;
int tmp_res = res;
prev_str = prev_str2;
i=i2;
res=res2;
prev_str2 = tmp_str;
i2 = tmp_i;
res2 = tmp_res;
}
else
{
prev_str2 = prev_str;
i2 = i;
res2=res;
prev_str = (wchar_t *)str;
i=0;
res=1;
}
}
for( ; str[i] && i<len; i++ )
{ {
if( str[i] == L'\n' ) if( str[i] == L'\n' )
{ {
@ -108,18 +65,11 @@ int parse_util_lineno( const wchar_t *str, int len )
} }
int parse_util_get_line_from_offset( const wcstring &str, int pos ) int parse_util_get_line_from_offset( const wcstring &str, size_t pos )
{ {
// return parse_util_lineno( buff, pos );
const wchar_t *buff = str.c_str(); const wchar_t *buff = str.c_str();
int i;
int count = 0; int count = 0;
if( pos < 0 ) for( size_t i=0; i<pos; i++ )
{
return -1;
}
for( i=0; i<pos; i++ )
{ {
if( !buff[i] ) if( !buff[i] )
{ {
@ -363,7 +313,7 @@ void parse_util_cmdsubst_extent( const wchar_t *buff,
Get the beginning and end of the job or process definition under the cursor Get the beginning and end of the job or process definition under the cursor
*/ */
static void job_or_process_extent( const wchar_t *buff, static void job_or_process_extent( const wchar_t *buff,
int cursor_pos, size_t cursor_pos,
const wchar_t **a, const wchar_t **a,
const wchar_t **b, const wchar_t **b,
int process ) int process )
@ -460,7 +410,7 @@ static void job_or_process_extent( const wchar_t *buff,
} }
void parse_util_process_extent( const wchar_t *buff, void parse_util_process_extent( const wchar_t *buff,
int pos, size_t pos,
const wchar_t **a, const wchar_t **a,
const wchar_t **b ) const wchar_t **b )
{ {
@ -468,7 +418,7 @@ void parse_util_process_extent( const wchar_t *buff,
} }
void parse_util_job_extent( const wchar_t *buff, void parse_util_job_extent( const wchar_t *buff,
int pos, size_t pos,
const wchar_t **a, const wchar_t **a,
const wchar_t **b ) const wchar_t **b )
{ {

View file

@ -53,7 +53,7 @@ void parse_util_cmdsubst_extent( const wchar_t *buff,
\param b the end of the searched string \param b the end of the searched string
*/ */
void parse_util_process_extent( const wchar_t *buff, void parse_util_process_extent( const wchar_t *buff,
int cursor_pos, size_t cursor_pos,
const wchar_t **a, const wchar_t **a,
const wchar_t **b ); const wchar_t **b );
@ -67,7 +67,7 @@ void parse_util_process_extent( const wchar_t *buff,
\param b the end of the searched string \param b the end of the searched string
*/ */
void parse_util_job_extent( const wchar_t *buff, void parse_util_job_extent( const wchar_t *buff,
int cursor_pos, size_t cursor_pos,
const wchar_t **a, const wchar_t **a,
const wchar_t **b ); const wchar_t **b );
@ -94,12 +94,12 @@ void parse_util_token_extent( const wchar_t *buff,
/** /**
Get the linenumber at the specified character offset Get the linenumber at the specified character offset
*/ */
int parse_util_lineno( const wchar_t *str, int len ); int parse_util_lineno( const wchar_t *str, size_t len );
/** /**
Calculate the line number of the specified cursor position Calculate the line number of the specified cursor position
*/ */
int parse_util_get_line_from_offset( const wcstring &str, int pos ); int parse_util_get_line_from_offset( const wcstring &str, size_t pos );
/** /**
Get the offset of the first character on the specified line Get the offset of the first character on the specified line

View file

@ -228,7 +228,7 @@ class reader_data_t
wcstring_list_t search_prev; wcstring_list_t search_prev;
/** The current position in search_prev */ /** The current position in search_prev */
int search_pos; size_t search_pos;
/** Length of the command */ /** Length of the command */
size_t command_length() const { return command_line.size(); } size_t command_length() const { return command_line.size(); }
@ -795,12 +795,10 @@ static int insert_char( wchar_t c )
/** /**
Calculate the length of the common prefix substring of two strings. Calculate the length of the common prefix substring of two strings.
*/ */
static int comp_len( const wchar_t *a, const wchar_t *b ) static size_t comp_len( const wchar_t *a, const wchar_t *b )
{ {
int i; size_t i;
for( i=0; for( i=0; a[i] != L'\0' && b[i] != L'\0' && a[i]==b[i]; i++ )
a[i] != '\0' && b[i] != '\0' && a[i]==b[i];
i++ )
; ;
return i; return i;
} }
@ -808,12 +806,10 @@ static int comp_len( const wchar_t *a, const wchar_t *b )
/** /**
Calculate the case insensitive length of the common prefix substring of two strings. Calculate the case insensitive length of the common prefix substring of two strings.
*/ */
static int comp_ilen( const wchar_t *a, const wchar_t *b ) static size_t comp_ilen( const wchar_t *a, const wchar_t *b )
{ {
int i; size_t i;
for( i=0; for( i=0; a[i] != L'\0' && b[i] != L'\0' && towlower(a[i])==towlower(b[i]); i++ )
a[i] != '\0' && b[i] != '\0' && towlower(a[i])==towlower(b[i]);
i++ )
; ;
return i; return i;
} }
@ -842,7 +838,7 @@ static wcstring completion_apply_to_command_line(const wcstring &val_str, int fl
if( do_replace ) if( do_replace )
{ {
int move_cursor; size_t move_cursor;
const wchar_t *begin, *end; const wchar_t *begin, *end;
wchar_t *escaped; wchar_t *escaped;
@ -974,7 +970,7 @@ static void run_pager( const wcstring &prefix, int is_quoted, const std::vector<
for( size_t i=0; i< comp.size(); i++ ) for( size_t i=0; i< comp.size(); i++ )
{ {
int base_len=-1; long base_len=-1;
const completion_t &el = comp.at( i ); const completion_t &el = comp.at( i );
wchar_t *foo=0; wchar_t *foo=0;
@ -1314,10 +1310,10 @@ static int reader_can_replace( const wcstring &in, int flags )
*/ */
static int handle_completions( const std::vector<completion_t> &comp ) static bool handle_completions( const std::vector<completion_t> &comp )
{ {
wchar_t *base = NULL; wchar_t *base = NULL;
int len = 0; size_t len = 0;
bool done = false; bool done = false;
int count = 0; int count = 0;
int flags=0; int flags=0;
@ -1379,7 +1375,7 @@ static int handle_completions( const std::vector<completion_t> &comp )
if( base ) if( base )
{ {
int new_len = comp_len( base, c.completion.c_str() ); size_t new_len = comp_len( base, c.completion.c_str() );
len = mini(new_len, len); len = mini(new_len, len);
} }
else else
@ -1410,15 +1406,13 @@ static int handle_completions( const std::vector<completion_t> &comp )
if( begin ) if( begin )
{ {
int offset = tok.size(); size_t offset = tok.size();
count = 0; count = 0;
for( size_t i=0; i< comp.size(); i++ ) for( size_t i=0; i< comp.size(); i++ )
{ {
const completion_t &c = comp.at( i ); const completion_t &c = comp.at( i );
int new_len;
if( !(c.flags & COMPLETE_NO_CASE) ) if( !(c.flags & COMPLETE_NO_CASE) )
continue; continue;
@ -1433,7 +1427,7 @@ static int handle_completions( const std::vector<completion_t> &comp )
if( base ) if( base )
{ {
new_len = offset + comp_ilen( base+offset, c.completion.c_str()+offset ); size_t new_len = offset + comp_ilen( base+offset, c.completion.c_str()+offset );
len = new_len < len ? new_len: len; len = new_len < len ? new_len: len;
} }
else else
@ -1499,9 +1493,7 @@ static int handle_completions( const std::vector<completion_t> &comp )
reader_repaint(); reader_repaint();
} }
return len; return len > 0;
} }
@ -1711,7 +1703,7 @@ static void handle_token_history( int forward, int reset )
return; return;
const wchar_t *str=0; const wchar_t *str=0;
int current_pos; long current_pos;
tokenizer tok; tokenizer tok;
if( reset ) if( reset )
@ -1726,7 +1718,7 @@ static void handle_token_history( int forward, int reset )
current_pos = data->token_history_pos; current_pos = data->token_history_pos;
if( forward || data->search_pos + 1 < (long)data->search_prev.size() ) if( forward || data->search_pos + 1 < data->search_prev.size() )
{ {
if( forward ) if( forward )
{ {
@ -1822,8 +1814,8 @@ static void handle_token_history( int forward, int reset )
reader_replace_current_token( str ); reader_replace_current_token( str );
reader_super_highlight_me_plenty( data->buff_pos ); reader_super_highlight_me_plenty( data->buff_pos );
reader_repaint(); reader_repaint();
data->search_pos = data->search_prev.size();
data->search_prev.push_back(str); data->search_prev.push_back(str);
data->search_pos = data->search_prev.size() - 1;
} }
else if( ! reader_interrupted() ) else if( ! reader_interrupted() )
{ {
@ -2011,10 +2003,10 @@ void reader_set_buffer( const wcstring &b, size_t pos )
} }
int reader_get_cursor_pos() size_t reader_get_cursor_pos()
{ {
if( !data ) if( !data )
return -1; return (size_t)(-1);
return data->buff_pos; return data->buff_pos;
} }
@ -2493,20 +2485,19 @@ static int wchar_private( wchar_t c )
Test if the specified character in the specified string is Test if the specified character in the specified string is
backslashed. backslashed.
*/ */
static int is_backslashed( const wchar_t *str, int pos ) static bool is_backslashed( const wchar_t *str, size_t pos )
{ {
int count = 0; size_t count = 0;
int i; size_t idx = pos;
while (idx--)
for( i=pos-1; i>=0; i-- )
{ {
if( str[i] != L'\\' ) if( str[idx] != L'\\' )
break; break;
count++; count++;
} }
return count %2; return (count % 2) == 1;
} }
@ -2514,11 +2505,11 @@ const wchar_t *reader_readline()
{ {
wint_t c; wint_t c;
int i; int last_char=0;
int last_char=0, yank=0; size_t yank_len=0;
const wchar_t *yank_str; const wchar_t *yank_str;
std::vector<completion_t> comp; std::vector<completion_t> comp;
int comp_empty=1; bool comp_empty = true;
int finished=0; int finished=0;
struct termios old_modes; struct termios old_modes;
@ -2595,15 +2586,15 @@ const wchar_t *reader_readline()
if( c != 0 ) if( c != 0 )
break; break;
} }
/* /*
if( (last_char == R_COMPLETE) && (c != R_COMPLETE) && (!comp_empty) ) if( (last_char == R_COMPLETE) && (c != R_COMPLETE) && (!comp_empty) )
{ {
halloc_destroy( comp ); halloc_destroy( comp );
comp = 0; comp = 0;
} }
*/ */
if( last_char != R_YANK && last_char != R_YANK_POP ) if( last_char != R_YANK && last_char != R_YANK_POP )
yank=0; yank_len=0;
const wchar_t *buff = data->command_line.c_str(); const wchar_t *buff = data->command_line.c_str();
switch( c ) switch( c )
{ {
@ -2685,8 +2676,7 @@ const wchar_t *reader_readline()
const wchar_t *begin, *end; const wchar_t *begin, *end;
const wchar_t *token_begin, *token_end; const wchar_t *token_begin, *token_end;
const wchar_t *buff = data->command_line.c_str(); const wchar_t *buff = data->command_line.c_str();
int len; long cursor_steps;
int cursor_steps;
parse_util_cmdsubst_extent( buff, data->buff_pos, &begin, &end ); parse_util_cmdsubst_extent( buff, data->buff_pos, &begin, &end );
@ -2701,7 +2691,7 @@ const wchar_t *reader_readline()
reader_repaint(); reader_repaint();
len = data->buff_pos - (begin-buff); size_t len = data->buff_pos - (begin-buff);
const wcstring buffcpy = wcstring(begin, len); const wcstring buffcpy = wcstring(begin, len);
data->complete_func( buffcpy, comp, COMPLETE_DEFAULT, NULL); data->complete_func( buffcpy, comp, COMPLETE_DEFAULT, NULL);
@ -2799,21 +2789,21 @@ const wchar_t *reader_readline()
{ {
yank_str = kill_yank(); yank_str = kill_yank();
insert_string( yank_str ); insert_string( yank_str );
yank = wcslen( yank_str ); yank_len = wcslen( yank_str );
break; break;
} }
/* rotate killring*/ /* rotate killring*/
case R_YANK_POP: case R_YANK_POP:
{ {
if( yank ) if( yank_len )
{ {
for( i=0; i<yank; i++ ) for( size_t i=0; i<yank_len; i++ )
remove_backward(); remove_backward();
yank_str = kill_yank_rotate(); yank_str = kill_yank_rotate();
insert_string(yank_str); insert_string(yank_str);
yank = wcslen(yank_str); yank_len = wcslen(yank_str);
} }
break; break;
} }
@ -3084,7 +3074,7 @@ const wchar_t *reader_readline()
case R_UP_LINE: case R_UP_LINE:
case R_DOWN_LINE: case R_DOWN_LINE:
{ {
int line_old = parse_util_get_line_from_offset( data->command_line.c_str(), data->buff_pos ); int line_old = parse_util_get_line_from_offset( data->command_line, data->buff_pos );
int line_new; int line_new;
if( c == R_UP_LINE ) if( c == R_UP_LINE )
@ -3096,19 +3086,19 @@ const wchar_t *reader_readline()
if( line_new >= 0 && line_new <= line_count) if( line_new >= 0 && line_new <= line_count)
{ {
int base_pos_new; size_t base_pos_new;
int base_pos_old; size_t base_pos_old;
int indent_old; int indent_old;
int indent_new; int indent_new;
int line_offset_old; size_t line_offset_old;
int total_offset_new; size_t total_offset_new;
base_pos_new = parse_util_get_offset_from_line( data->command_line, line_new ); base_pos_new = parse_util_get_offset_from_line( data->command_line, line_new );
base_pos_old = parse_util_get_offset_from_line( data->command_line, line_old ); base_pos_old = parse_util_get_offset_from_line( data->command_line, line_old );
assert(base_pos_new != (size_t)(-1) && base_pos_old != (size_t)(-1));
indent_old = data->indents.at(base_pos_old); indent_old = data->indents.at(base_pos_old);
indent_new = data->indents.at(base_pos_new); indent_new = data->indents.at(base_pos_new);
@ -3174,10 +3164,10 @@ const wchar_t *reader_readline()
} }
writestr( L"\n" ); writestr( L"\n" );
/* /*
if( comp ) if( comp )
halloc_free( comp ); halloc_free( comp );
*/ */
if( !reader_exit_forced() ) if( !reader_exit_forced() )
{ {
if( tcsetattr(0,TCSANOW,&old_modes)) /* return to previous mode */ if( tcsetattr(0,TCSANOW,&old_modes)) /* return to previous mode */

View file

@ -107,9 +107,9 @@ void reader_set_buffer( const wcstring &b, size_t p );
/** /**
Get the current cursor position in the command line. If interactive Get the current cursor position in the command line. If interactive
mode is uninitialized, return -1. mode is uninitialized, return (size_t)(-1).
*/ */
int reader_get_cursor_pos(); size_t reader_get_cursor_pos();
/** /**
Return the value of the interrupted flag, which is set by the sigint Return the value of the interrupted flag, which is set by the sigint

View file

@ -774,7 +774,7 @@ void s_write( screen_t *s,
size_t explicit_len, size_t explicit_len,
const int *c, const int *c,
const int *indent, const int *indent,
int cursor ) size_t cursor_pos )
{ {
int cursor_arr[2]; int cursor_arr[2];
@ -889,17 +889,17 @@ void s_write( screen_t *s,
prompt_width=0; prompt_width=0;
} }
int i; size_t i;
for( i=0; commandline[i]; i++ ) for( i=0; commandline[i]; i++ )
{ {
int col = c[i]; int col = c[i];
if( i == cursor ) if( i == cursor_pos )
{ {
col = 0; col = 0;
} }
if( i == cursor ) if( i == cursor_pos )
{ {
cursor_arr[0] = s->desired.cursor[0]; cursor_arr[0] = s->desired.cursor[0];
cursor_arr[1] = s->desired.cursor[1]; cursor_arr[1] = s->desired.cursor[1];
@ -907,7 +907,7 @@ void s_write( screen_t *s,
s_desired_append_char( s, commandline[i], col, indent[i], prompt_width ); s_desired_append_char( s, commandline[i], col, indent[i], prompt_width );
if( i== cursor && s->desired.cursor[1] != cursor_arr[1] && commandline[i] != L'\n' ) if( i== cursor_pos && s->desired.cursor[1] != cursor_arr[1] && commandline[i] != L'\n' )
{ {
/* /*
Ugh. We are placed exactly at the wrapping point of a Ugh. We are placed exactly at the wrapping point of a
@ -919,7 +919,7 @@ void s_write( screen_t *s,
cursor_arr[1] = s->desired.cursor[1]; cursor_arr[1] = s->desired.cursor[1];
} }
} }
if( i == cursor ) if( i == cursor_pos )
{ {
memcpy(cursor_arr, s->desired.cursor, sizeof(int)*2); memcpy(cursor_arr, s->desired.cursor, sizeof(int)*2);
} }

View file

@ -150,7 +150,7 @@ void s_write( screen_t *s,
size_t explicit_len, size_t explicit_len,
const int *colors, const int *colors,
const int *indent, const int *indent,
int cursor_pos ); size_t cursor_pos );
/** /**
This function resets the screen buffers internal knowledge about This function resets the screen buffers internal knowledge about