Cleaned up lots of typecasts, simplified some string handling

This commit is contained in:
ridiculousfish 2012-08-04 15:11:43 -07:00
parent 5880cd88c8
commit c67702a498
19 changed files with 54 additions and 116 deletions

View file

@ -2218,8 +2218,7 @@ static int builtin_read( parser_t &parser, wchar_t **argv )
while( !finished )
{
char b;
int read_res = read_blocked( builtin_stdin, &b, 1 );
if( read_res <= 0 )
if( read_blocked( builtin_stdin, &b, 1 ) <= 0 )
{
eof=1;
break;

View file

@ -61,8 +61,7 @@ static const wchar_t *current_buffer=0;
What the commandline builtin considers to be the current cursor
position.
*/
static const size_t kInvalidCursorPosition = (size_t)(-1);
static size_t current_cursor_pos = kInvalidCursorPosition;
static size_t current_cursor_pos = (size_t)(-1);
/**
Returns the current commandline buffer.

View file

@ -292,10 +292,10 @@ static int builtin_jobs( parser_t &parser, wchar_t **argv )
for( i=woptind; i<argc; i++ )
{
long pid;
int pid;
wchar_t *end;
errno=0;
pid=wcstol( argv[i], &end, 10 );
pid=fish_wcstoi( argv[i], &end, 10 );
if( errno || *end )
{
append_format(stderr_buffer,
@ -305,7 +305,7 @@ static int builtin_jobs( parser_t &parser, wchar_t **argv )
return 1;
}
j = job_get_from_pid( (int)pid );
j = job_get_from_pid( pid );
if( j && !job_is_completed( j ) )
{

View file

@ -618,7 +618,7 @@ __sentinel bool contains_internal( const wcstring &needle, ... )
return res;
}
int read_blocked(int fd, void *buf, size_t count)
long read_blocked(int fd, void *buf, size_t count)
{
ssize_t res;
sigset_t chldset, oldset;
@ -628,7 +628,7 @@ int read_blocked(int fd, void *buf, size_t count)
VOMIT_ON_FAILURE(pthread_sigmask(SIG_BLOCK, &chldset, &oldset));
res = read( fd, buf, count );
VOMIT_ON_FAILURE(pthread_sigmask(SIG_SETMASK, &oldset, NULL));
return (int)res;
return res;
}
ssize_t write_loop(int fd, const char *buff, size_t count)

View file

@ -583,7 +583,7 @@ __sentinel bool contains_internal( const wcstring &needle, ... );
if you _know_ there is data available for reading, or the program
will hang until there is data.
*/
int read_blocked(int fd, void *buf, size_t count);
long read_blocked(int fd, void *buf, size_t count);
/**
Loop a write request while failiure is non-critical. Return -1 and set errno

View file

@ -1083,15 +1083,15 @@ env_var_t env_get_string( const wcstring &key )
}
else if( key == L"COLUMNS" )
{
return to_string((long)common_get_width());
return to_string(common_get_width());
}
else if( key == L"LINES" )
{
return to_string((long)common_get_width());
return to_string(common_get_width());
}
else if( key == L"status" )
{
return to_string((long)proc_get_last_status());
return to_string(proc_get_last_status());
}
else if( key == L"umask" )
{

View file

@ -528,7 +528,7 @@ static void event_fire_delayed()
/*
Send all signals in our private list
*/
for( i=0; i<(size_t)lst->count; i++ )
for( int i=0; i < lst->count; i++ )
{
e.param1.signal = lst->signal[i];
e.arguments->at(0) = sig2wcs( e.param1.signal );

View file

@ -95,6 +95,8 @@ static void exec_write_and_exit( int fd, const char *buff, size_t count, int sta
void exec_close( int fd )
{
ASSERT_IS_MAIN_THREAD();
/* This may be called in a child of fork(), so don't allocate memory */
if( fd < 0 )
{
@ -113,7 +115,7 @@ void exec_close( int fd )
}
/* Maybe remove this from our set of open fds */
if (fd < (int)open_fds.size()) {
if (fd < open_fds.size()) {
open_fds[fd] = false;
}
}
@ -134,7 +136,7 @@ int exec_pipe( int fd[2])
debug( 4, L"Created pipe using fds %d and %d", fd[0], fd[1]);
int max_fd = std::max(fd[0], fd[1]);
if ((int)open_fds.size() <= max_fd) {
if (open_fds.size() <= max_fd) {
open_fds.resize(max_fd + 1, false);
}
open_fds.at(fd[0]) = true;

View file

@ -494,7 +494,7 @@ static void completion_print( int cols,
int is_last = (j==(cols-1));
if( (int)lst.size() <= j*rows + i )
if( lst.size() <= j*rows + i )
continue;
el = lst.at(j*rows + i );
@ -872,11 +872,9 @@ static void mangle_descriptions( wcstring_list_t &lst )
*/
static void join_completions( wcstring_list_t lst )
{
long i;
std::map<wcstring, long> desc_table;
for( i=0; i<(long)lst.size(); i++ )
for( size_t i=0; i<lst.size(); i++ )
{
const wchar_t *item = lst.at(i).c_str();
const wchar_t *desc = wcschr( item, COMPLETE_SEP );

View file

@ -197,41 +197,6 @@ static void handle_term( int signal )
}
/**
Writes a pid_t in decimal representation to str.
str must contain sufficient space.
The conservatively approximate maximum number of characters a pid_t will
represent is given by: (int)(0.31 * sizeof(pid_t) + CHAR_BIT + 1)
Returns the length of the string
*/
static int sprint_pid_t( pid_t pid, char *str )
{
int len, i = 0;
int dig;
/* Store digits in reverse order into string */
while( pid != 0 )
{
dig = pid % 10;
str[i] = '0' + dig;
pid = ( pid - dig ) / 10;
i++;
}
len = i;
/* Reverse digits */
i /= 2;
while( i )
{
i--;
dig = str[i];
str[i] = str[len - 1 - i];
str[len - 1 - i] = dig;
}
return len;
}
/**
Writes a pseudo-random number (between one and maxlen) of pseudo-random
digits into str.
@ -247,7 +212,7 @@ static int sprint_pid_t( pid_t pid, char *str )
Excludes chars other than digits since ANSI C only guarantees that digits
are consecutive.
*/
static int sprint_rand_digits( char *str, int maxlen )
static void sprint_rand_digits( char *str, int maxlen )
{
int i, max;
struct timeval tv;
@ -265,7 +230,7 @@ static int sprint_rand_digits( char *str, int maxlen )
{
str[i] = '0' + 10 * (rand() / (RAND_MAX + 1.0));
}
return i;
str[i] = 0;
}
@ -278,45 +243,22 @@ static int sprint_rand_digits( char *str, int maxlen )
fallback.
The memory returned should be freed using free().
*/
static char *gen_unique_nfs_filename( const char *filename )
static std::string gen_unique_nfs_filename( const char *filename )
{
size_t pidlen, hnlen, orglen = strlen( filename );
char hostname[HOST_NAME_MAX + 1];
char *newname;
char hostname[HOST_NAME_MAX + 1];
char pid_str[256];
snprintf(pid_str, sizeof pid_str, "%ld", (long)getpid());
if ( gethostname( hostname, HOST_NAME_MAX + 1 ) == 0 )
{
hnlen = strlen( hostname );
if ( gethostname( hostname, sizeof hostname ) != 0 )
{
sprint_rand_digits( hostname, HOST_NAME_MAX );
}
else
{
hnlen = sprint_rand_digits( hostname, HOST_NAME_MAX );
hostname[hnlen] = '\0';
}
newname = (char *)malloc( orglen + 1 /* period */ + hnlen + 1 /* period */ +
/* max possible pid size: 0.31 ~= log(10)2 */
(int)(0.31 * sizeof(pid_t) * CHAR_BIT + 1)
+ 1 /* '\0' */ );
if ( newname == NULL )
{
debug( 1, L"gen_unique_nfs_filename: %s", strerror( errno ) );
return newname;
}
memcpy( newname, filename, orglen );
newname[orglen] = '.';
memcpy( newname + orglen + 1, hostname, hnlen );
newname[orglen + 1 + hnlen] = '.';
pidlen = sprint_pid_t( getpid(), newname + orglen + 1 + hnlen + 1 );
newname[orglen + 1 + hnlen + 1 + pidlen] = '\0';
/* debug( 1, L"gen_unique_nfs_filename returning with: newname = \"%s\"; "
L"HOST_NAME_MAX = %d; hnlen = %d; orglen = %d; "
L"sizeof(pid_t) = %d; maxpiddigits = %d; malloc'd size: %d",
newname, (int)HOST_NAME_MAX, hnlen, orglen,
(int)sizeof(pid_t),
(int)(0.31 * sizeof(pid_t) * CHAR_BIT + 1),
(int)(orglen + 1 + hnlen + 1 +
(int)(0.31 * sizeof(pid_t) * CHAR_BIT + 1) + 1) ); */
std::string newname(filename);
newname.push_back('.');
newname.append(hostname);
newname.push_back('.');
newname.append(pid_str);
return newname;
}
@ -348,11 +290,8 @@ static int acquire_lock_file( const char *lockfile, const int timeout, int force
/*
(Re)create a unique file and check that it has one only link.
*/
char *linkfile = gen_unique_nfs_filename( lockfile );
if( linkfile == NULL )
{
goto done;
}
const std::string linkfile_str = gen_unique_nfs_filename( lockfile );
const char * const linkfile = linkfile_str.c_str();
(void)unlink( linkfile );
/* OK to not use CLO_EXEC here because fishd is single threaded */
if( ( fd = open( linkfile, O_CREAT|O_RDONLY, 0600 ) ) == -1 )
@ -439,7 +378,6 @@ static int acquire_lock_file( const char *lockfile, const int timeout, int force
done:
/* The linkfile is not needed once the lockfile has been created */
(void)unlink( linkfile );
free( linkfile );
return ret;
}

View file

@ -1313,7 +1313,7 @@ void highlight_shell( const wcstring &buff, std::vector<int> &color, size_t pos,
// highlight the end of the subcommand
assert(end >= subbuff);
if ((size_t)(end - subbuff) < length) {
if ((end - subbuff) < length) {
color.at(end-subbuff)=HIGHLIGHT_OPERATOR;
}
@ -1418,7 +1418,7 @@ static void highlight_universal_internal( const wcstring &buffstr, std::vector<i
if( level == 0 )
{
level++;
lst.push_back((long)(str-buff));
lst.push_back(str-buff);
prev_q = *str;
}
else
@ -1442,7 +1442,7 @@ static void highlight_universal_internal( const wcstring &buffstr, std::vector<i
else
{
level++;
lst.push_back((long)(str-buff));
lst.push_back(str-buff);
prev_q = *str;
}
}

2
io.cpp
View file

@ -64,7 +64,7 @@ void io_buffer_read( io_data_t *d )
while(1)
{
char b[4096];
int l;
long l;
l=read_blocked( d->param1.pipe_fd[0], b, 4096 );
if( l==0 )
{

View file

@ -1033,13 +1033,13 @@ static void write_file( const char *file, int print_path )
\param files the list of files for which to perform the action
\param fileno an internal value. Should always be set to zero.
*/
static void launch( char *filter, const string_list_t &files, int fileno )
static void launch( char *filter, const string_list_t &files, size_t fileno )
{
char *filter_org=filter;
int count=0;
int launch_again=0;
if( (int)files.size() <= fileno )
if( files.size() <= fileno )
return;

View file

@ -161,7 +161,7 @@ static bool write_color(char *todo, unsigned char idx, bool is_fg) {
} else {
/* We are attempting to bypass the term here. Generate the ANSI escape sequence ourself. */
char stridx[128];
format_long_safe(stridx, (long)idx);
format_long_safe(stridx, idx);
char buff[128] = "\x1b[";
strcat(buff, is_fg ? "38;5;" : "48;5;");
strcat(buff, stridx);

View file

@ -132,7 +132,7 @@ size_t parse_util_get_offset( const wcstring &str, int line, long line_offset )
if( off2 == (size_t)(-1) )
{
off2 = (int)(wcslen( buff )+1);
off2 = wcslen( buff )+1;
}
if( line_offset2 < 0 )

View file

@ -377,8 +377,8 @@ static void mark_process_status( const job_t *j,
char mess[MESS_SIZE];
snprintf( mess,
MESS_SIZE,
"Process %d exited abnormally\n",
(int) p->pid );
"Process %ld exited abnormally\n",
(long) p->pid );
/*
If write fails, do nothing. We're in a signal handlers error
handler. If things aren't working properly, it's safer to
@ -866,7 +866,7 @@ static void read_try( job_t *j )
while(1)
{
char b[BUFFER_SIZE];
int l;
long l;
l=read_blocked( buff->param1.pipe_fd[0],
b, BUFFER_SIZE );

View file

@ -444,7 +444,9 @@ static void reader_repaint()
parser_t::principal_parser().test( data->command_line.c_str(), &data->indents[0], 0, 0 );
wcstring full_line = (data->autosuggestion.empty() ? data->command_line : data->autosuggestion);
size_t len = std::max((size_t)1, full_line.size());
size_t len = full_line.size();
if (len < 1)
len = 1;
std::vector<color_t> colors = data->colors;
colors.resize(len, HIGHLIGHT_AUTOSUGGESTION);

View file

@ -60,7 +60,7 @@ void validate_pointer( const void *ptr, const wchar_t *err, int null_ok )
Test if the pointer data crosses a segment boundary.
*/
if( (0x00000003l & (long)ptr) != 0 )
if( (0x00000003l & (intptr_t)ptr) != 0 )
{
debug( 0, _(L"The pointer '%ls' is invalid"), err );
sanity_lose();

View file

@ -384,10 +384,10 @@ int wcs2sig( const wchar_t *str )
}
}
errno=0;
long res = wcstol( str, &end, 10 );
if( !errno && res>=0 && res <= INT_MAX && !*end )
return (int)res;
int res = fish_wcstoi( str, &end, 10 );
if( !errno && res>=0 && !*end )
return res;
return -1;
}