Skip to content

Commit f3e2d2f

Browse files
Lots of warnings fixes and const correctness
1 parent a359f45 commit f3e2d2f

File tree

9 files changed

+29
-191
lines changed

9 files changed

+29
-191
lines changed

builtin_set.cpp

Lines changed: 12 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ static int my_env_set( const wchar_t *key, array_list_t *val, int scope )
177177
static int my_env_set2( const wchar_t *key, wcstring_list_t &val, int scope )
178178
{
179179
string_buffer_t sb;
180-
int i;
180+
size_t i;
181181
int retcode = 0;
182182
wchar_t *val_str=0;
183183

@@ -304,84 +304,7 @@ static int my_env_set2( const wchar_t *key, wcstring_list_t &val, int scope )
304304
305305
\return the total number of indexes parsed, or -1 on error
306306
*/
307-
static int parse_index( array_list_t *indexes,
308-
const wchar_t *src,
309-
const wchar_t *name,
310-
int var_count )
311-
{
312-
size_t len;
313-
314-
int count = 0;
315-
const wchar_t *src_orig = src;
316-
317-
if (src == 0)
318-
{
319-
return 0;
320-
}
321-
322-
while (*src != L'\0' && (iswalnum(*src) || *src == L'_'))
323-
{
324-
src++;
325-
}
326-
327-
if (*src != L'[')
328-
{
329-
sb_printf( sb_err, _(BUILTIN_SET_ARG_COUNT), L"set" );
330-
return 0;
331-
}
332-
333-
len = src-src_orig;
334-
335-
if( (wcsncmp( src_orig, name, len )!=0) || (wcslen(name) != (len)) )
336-
{
337-
sb_printf( sb_err,
338-
_(L"%ls: Multiple variable names specified in single call (%ls and %.*ls)\n"),
339-
L"set",
340-
name,
341-
len,
342-
src_orig);
343-
return 0;
344-
}
345-
346-
src++;
347-
348-
while (iswspace(*src))
349-
{
350-
src++;
351-
}
352-
353-
while (*src != L']')
354-
{
355-
wchar_t *end;
356-
357-
long l_ind;
358-
359-
errno = 0;
360-
361-
l_ind = wcstol(src, &end, 10);
362-
363-
if( end==src || errno )
364-
{
365-
sb_printf(sb_err, _(L"%ls: Invalid index starting at '%ls'\n"), L"set", src);
366-
return 0;
367-
}
368-
369-
if( l_ind < 0 )
370-
{
371-
l_ind = var_count+l_ind+1;
372-
}
373-
374-
al_push_long(indexes, l_ind);
375-
src = end;
376-
count++;
377-
while (iswspace(*src)) src++;
378-
}
379-
380-
return count;
381-
}
382-
383-
384-
static int parse_index2( std::vector<long> &indexes,
307+
static int parse_index( std::vector<long> &indexes,
385308
const wchar_t *src,
386309
const wchar_t *name,
387310
int var_count )
@@ -457,46 +380,11 @@ static int parse_index2( std::vector<long> &indexes,
457380
return count;
458381
}
459382

460-
/**
461-
Update a list \c list by writing copies (using wcsdup) of the
462-
values specified by \c values to the indexes specified by \c
463-
indexes. The previous entries at the specidied position will be
464-
free'd.
465-
466-
\return 0 if the operation was successfull, non-zero otherwise
467-
*/
468-
static int update_values( array_list_t *list,
469-
array_list_t *indexes,
470-
array_list_t *values )
471-
{
472-
int i;
473-
474-
/* Replace values where needed */
475-
for( i = 0; i < al_get_count(indexes); i++ )
476-
{
477-
/*
478-
The '- 1' below is because the indices in fish are
479-
one-based, but the array_list_t uses zero-based indices
480-
*/
481-
long ind = al_get_long(indexes, i) - 1;
482-
const wchar_t *newv = (const wchar_t*) al_get(values, i);
483-
if( ind < 0 )
484-
{
485-
return 1;
486-
}
487-
488-
free((void *) al_get(list, ind));
489-
al_set(list, ind, newv != 0 ? wcsdup(newv) : wcsdup(L""));
490-
}
491-
492-
return 0;
493-
}
494-
495-
static int update_values2( wcstring_list_t &list,
383+
static int update_values( wcstring_list_t &list,
496384
std::vector<long> &indexes,
497385
wcstring_list_t &values )
498386
{
499-
int i;
387+
size_t i;
500388

501389
/* Replace values where needed */
502390
for( i = 0; i < indexes.size(); i++ )
@@ -518,61 +406,13 @@ static int update_values2( wcstring_list_t &list,
518406

519407
return 0;
520408
}
521-
/**
522-
Return 1 if an array list of longs contains the specified
523-
value, 0 otherwise
524-
*/
525-
static int al_contains_long( array_list_t *list,
526-
long val)
527-
{
528-
int i;
529-
530-
for (i = 0; i < al_get_count(list); i++)
531-
{
532-
long current = al_get_long(list, i);
533-
if( current != 0 && current == val )
534-
{
535-
return 1;
536-
}
537-
}
538-
539-
return 0;
540-
}
541-
542-
543-
/**
544-
Erase from a list values at specified indexes
545-
*/
546-
static void erase_values(array_list_t *list, array_list_t *indexes)
547-
{
548-
long i;
549-
array_list_t result;
550-
551-
al_init(&result);
552-
553-
for (i = 0; i < al_get_count(list); i++)
554-
{
555-
if (!al_contains_long(indexes, i + 1))
556-
{
557-
al_push(&result, al_get(list, i));
558-
}
559-
else
560-
{
561-
free( (void *)al_get(list, i));
562-
}
563-
}
564-
565-
al_truncate(list,0);
566-
al_push_all( list, &result );
567-
al_destroy(&result);
568-
}
569409

570410
/**
571411
Erase from a list of wcstring values at specified indexes
572412
*/
573-
static void erase_values2 (wcstring_list_t &list, std::vector<long> &indexes)
413+
static void erase_values(wcstring_list_t &list, std::vector<long> &indexes)
574414
{
575-
long i;
415+
size_t i;
576416
wcstring_list_t result;
577417

578418
// al_init(&result);
@@ -878,15 +718,15 @@ static int builtin_set( wchar_t **argv )
878718
{
879719
std::vector<long> indexes;
880720
wcstring_list_t result;
881-
int j;
721+
size_t j;
882722

883723
// al_init( &result );
884724
// al_init( &indexes );
885725
env_var_t dest_str = env_get_string(dest);
886726
if (! dest_str.missing())
887727
tokenize_variable_array2( dest_str, result );
888728

889-
if( !parse_index2( indexes, arg, dest, result.size() ) )
729+
if( !parse_index( indexes, arg, dest, result.size() ) )
890730
{
891731
builtin_print_help( argv[0], sb_err );
892732
retcode = 1;
@@ -895,7 +735,7 @@ static int builtin_set( wchar_t **argv )
895735
for( j=0; j < indexes.size() ; j++ )
896736
{
897737
long idx = indexes[j];
898-
if( idx < 1 || idx > result.size() )
738+
if( idx < 1 || (size_t)idx > result.size() )
899739
{
900740
retcode++;
901741
}
@@ -1006,7 +846,7 @@ static int builtin_set( wchar_t **argv )
1006846

1007847
for( ; woptind<argc; woptind++ )
1008848
{
1009-
if( !parse_index2( indexes, argv[woptind], dest, result.size() ) )
849+
if( !parse_index( indexes, argv[woptind], dest, result.size() ) )
1010850
{
1011851
builtin_print_help( argv[0], sb_err );
1012852
retcode = 1;
@@ -1041,7 +881,7 @@ static int builtin_set( wchar_t **argv )
1041881

1042882
if( erase )
1043883
{
1044-
erase_values2(result, indexes);
884+
erase_values(result, indexes);
1045885
my_env_set2( dest, result, scope);
1046886
}
1047887
else
@@ -1054,7 +894,7 @@ static int builtin_set( wchar_t **argv )
1054894
value.push_back( argv[woptind++] );
1055895
}
1056896

1057-
if( update_values2( result,
897+
if( update_values( result,
1058898
indexes,
1059899
value ) )
1060900
{

common.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -620,16 +620,16 @@ int read_blocked(int fd, void *buf, size_t count)
620620
return res;
621621
}
622622

623-
ssize_t write_loop(int fd, char *buff, size_t count)
623+
ssize_t write_loop(int fd, const char *buff, size_t count)
624624
{
625-
ssize_t out=0;
626-
ssize_t out_cum=0;
625+
size_t out=0;
626+
size_t out_cum=0;
627627
while( 1 )
628628
{
629629
out = write( fd,
630630
&buff[out_cum],
631631
count - out_cum );
632-
if (out == -1)
632+
if (out < 0)
633633
{
634634
if( errno != EAGAIN &&
635635
errno != EINTR )
@@ -638,7 +638,7 @@ ssize_t write_loop(int fd, char *buff, size_t count)
638638
}
639639
} else
640640
{
641-
out_cum += out;
641+
out_cum += (size_t)out;
642642
}
643643
if( out_cum >= count )
644644
{

common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ int read_blocked(int fd, void *buf, size_t count);
432432
Loop a write request while failiure is non-critical. Return -1 and set errno
433433
in case of critical error.
434434
*/
435-
ssize_t write_loop(int fd, char *buff, size_t count);
435+
ssize_t write_loop(int fd, const char *buff, size_t count);
436436

437437

438438
/**

expand.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,6 @@ static int find_process( const wchar_t *proc,
380380
array_list_t *out )
381381
{
382382
DIR *dir;
383-
struct wdirent *next;
384383
wchar_t *pdir_name;
385384
wchar_t *pfile_name;
386385
wchar_t *cmd=0;
@@ -1775,7 +1774,6 @@ int expand_string2( const wcstring &input, std::vector<wcstring> &output, int fl
17751774
size_t i;
17761775
int cmdsubst_ok = 1;
17771776
int res = EXPAND_OK;
1778-
int start_count = output.size();
17791777

17801778
if( (!(flags & ACCEPT_INCOMPLETE)) && expand_is_clean( input.c_str() ) )
17811779
{
@@ -2179,7 +2177,7 @@ int expand_string( void *context,
21792177
if( ((flags & ACCEPT_INCOMPLETE) && (!(flags & EXPAND_SKIP_WILDCARDS))) ||
21802178
wildcard_has( next, 1 ) )
21812179
{
2182-
wchar_t *start, *rest;
2180+
const wchar_t *start, *rest;
21832181
array_list_t *list = out;
21842182

21852183
if( next[0] == '/' )

kill.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@ void kill_replace( wchar_t *old, wchar_t *newv )
202202
kill_add( newv );
203203
}
204204

205-
wchar_t *kill_yank_rotate()
205+
const wchar_t *kill_yank_rotate()
206206
{
207207
if( kill_current == 0 )
208208
return L"";
209209
kill_current = kill_current->prev;
210-
return (wchar_t *)kill_current->data;
210+
return (const wchar_t *)kill_current->data;
211211
}
212212

213213
/**
@@ -256,7 +256,7 @@ static void kill_check_x_buffer()
256256
}
257257

258258

259-
wchar_t *kill_yank()
259+
const wchar_t *kill_yank()
260260
{
261261
kill_check_x_buffer();
262262
if( kill_current == 0 )

kill.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ void kill_add( wchar_t *str );
2222
/**
2323
Rotate the killring
2424
*/
25-
wchar_t *kill_yank_rotate();
25+
const wchar_t *kill_yank_rotate();
2626
/**
2727
Paste from the killring
2828
*/
29-
wchar_t *kill_yank();
29+
const wchar_t *kill_yank();
3030
/**
3131
Sanity check
3232
*/

print_help.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
#define HELP_ERR "Could not show help message\n"
1515

1616
/* defined in common.h */
17-
ssize_t write_loop(int fd, char *buff, size_t count);
17+
ssize_t write_loop(int fd, const char *buff, size_t count);
1818

1919

20-
void print_help( char *c, int fd )
20+
void print_help( const char *c, int fd )
2121
{
2222
char cmd[ CMD_LEN];
2323
int printed = snprintf( cmd, CMD_LEN, "fish -c '__fish_print_help %s >&%d'", c, fd );

print_help.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
Print help message for the specified command
1111
*/
1212

13-
void print_help( char *cmd, int fd );
13+
void print_help( const char *cmd, int fd );
1414

1515
#endif

0 commit comments

Comments
 (0)