Skip to content

Commit 3f8621e

Browse files
CLeanup the io_stack in builtin.cpp, other changes to migrate away from al_list
1 parent 5f686eb commit 3f8621e

File tree

2 files changed

+23
-145
lines changed

2 files changed

+23
-145
lines changed

builtin.cpp

+17-11
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <wctype.h>
3535
#include <sys/time.h>
3636
#include <time.h>
37+
#include <stack>
3738

3839
#include "fallback.h"
3940
#include "util.h"
@@ -125,7 +126,12 @@ string_buffer_t *sb_out=0, *sb_err=0;
125126
/**
126127
Stack containing builtin I/O for recursive builtin calls.
127128
*/
128-
static array_list_t io_stack;
129+
struct io_stack_elem_t {
130+
int in;
131+
string_buffer_t *out;
132+
string_buffer_t *err;
133+
};
134+
static std::stack<io_stack_elem_t> io_stack;
129135

130136
/**
131137
The file from which builtin functions should attempt to read, use
@@ -3623,8 +3629,6 @@ void builtin_init()
36233629
{
36243630

36253631
wopterr = 0;
3626-
al_init( &io_stack );
3627-
36283632
for( size_t i=0; i < BUILTIN_COUNT; i++ )
36293633
{
36303634
intern_static( builtin_datas[i].name );
@@ -3633,7 +3637,6 @@ void builtin_init()
36333637

36343638
void builtin_destroy()
36353639
{
3636-
al_destroy( &io_stack );
36373640
}
36383641

36393642
int builtin_exists( const wcstring &cmd )
@@ -3714,11 +3717,11 @@ const wchar_t *builtin_get_desc( const wchar_t *name )
37143717

37153718
void builtin_push_io( parser_t &parser, int in )
37163719
{
3720+
ASSERT_IS_MAIN_THREAD();
37173721
if( builtin_stdin != -1 )
37183722
{
3719-
al_push_long( &io_stack, (long)builtin_stdin );
3720-
al_push( &io_stack, sb_out );
3721-
al_push( &io_stack, sb_err );
3723+
struct io_stack_elem_t elem = {builtin_stdin, sb_out, sb_err};
3724+
io_stack.push(elem);
37223725
}
37233726
builtin_stdin = in;
37243727
sb_out = (string_buffer_t *)malloc(sizeof(string_buffer_t));
@@ -3729,17 +3732,20 @@ void builtin_push_io( parser_t &parser, int in )
37293732

37303733
void builtin_pop_io(parser_t &parser)
37313734
{
3735+
ASSERT_IS_MAIN_THREAD();
37323736
builtin_stdin = 0;
37333737
sb_destroy( sb_out );
37343738
sb_destroy( sb_err );
37353739
free( sb_out);
37363740
free(sb_err);
37373741

3738-
if( al_get_count( &io_stack ) >0 )
3742+
if( ! io_stack.empty() )
37393743
{
3740-
sb_err = (string_buffer_t *)al_pop( &io_stack );
3741-
sb_out = (string_buffer_t *)al_pop( &io_stack );
3742-
builtin_stdin = (int)al_pop_long( &io_stack );
3744+
struct io_stack_elem_t &elem = io_stack.top();
3745+
sb_err = elem.err;
3746+
sb_out = elem.out;
3747+
builtin_stdin = elem.in;
3748+
io_stack.pop();
37433749
}
37443750
else
37453751
{

builtin_set.cpp

+6-134
Original file line numberDiff line numberDiff line change
@@ -56,125 +56,7 @@ static int is_path_variable( const wchar_t *env )
5656
Call env_set. If this is a path variable, e.g. PATH, validate the
5757
elements. On error, print a description of the problem to stderr.
5858
*/
59-
static int my_env_set( const wchar_t *key, array_list_t *val, int scope )
60-
{
61-
string_buffer_t sb;
62-
int i;
63-
int retcode = 0;
64-
wchar_t *val_str=0;
65-
66-
if( is_path_variable( key ) )
67-
{
68-
int error = 0;
69-
70-
for( i=0; i<al_get_count( val ); i++ )
71-
{
72-
int show_perror = 0;
73-
int show_hint = 0;
74-
75-
struct stat buff;
76-
const wchar_t *dir = (wchar_t *)al_get( val, i );
77-
78-
if( wstat( dir, &buff ) )
79-
{
80-
error = 1;
81-
show_perror = 1;
82-
}
83-
84-
if( !( S_ISDIR(buff.st_mode) ) )
85-
{
86-
error = 1;
87-
88-
}
89-
90-
if( error )
91-
{
92-
const wchar_t *colon;
93-
94-
sb_printf( sb_err,
95-
_(BUILTIN_SET_PATH_ERROR),
96-
L"set",
97-
dir,
98-
key );
99-
100-
colon = wcschr( dir, L':' );
101-
102-
if( colon && *(colon+1) )
103-
{
104-
show_hint = 1;
105-
}
106-
107-
}
108-
109-
if( show_perror )
110-
{
111-
builtin_wperror( L"set" );
112-
}
113-
114-
if( show_hint )
115-
{
116-
sb_printf( sb_err,
117-
_(BUILTIN_SET_PATH_HINT),
118-
L"set",
119-
key,
120-
key,
121-
wcschr( dir, L':' )+1);
122-
}
123-
124-
if( error )
125-
{
126-
break;
127-
}
128-
129-
}
130-
131-
if( error )
132-
{
133-
return 1;
134-
}
135-
136-
}
137-
138-
sb_init( &sb );
139-
140-
if( al_get_count( val ) )
141-
{
142-
for( i=0; i<al_get_count( val ); i++ )
143-
{
144-
wchar_t *next =(wchar_t *)al_get( val, i );
145-
sb_append( &sb, next?next:L"" );
146-
if( i<al_get_count( val )-1 )
147-
{
148-
sb_append( &sb, ARRAY_SEP_STR );
149-
}
150-
}
151-
val_str = (wchar_t *)sb.buff;
152-
153-
}
154-
155-
switch( env_set( key, val_str, scope | ENV_USER ) )
156-
{
157-
case ENV_PERM:
158-
{
159-
sb_printf( sb_err, _(L"%ls: Tried to change the read-only variable '%ls'\n"), L"set", key );
160-
retcode=1;
161-
break;
162-
}
163-
164-
case ENV_INVALID:
165-
{
166-
sb_printf( sb_err, _(L"%ls: Unknown error"), L"set" );
167-
retcode=1;
168-
break;
169-
}
170-
}
171-
172-
sb_destroy( &sb );
173-
174-
return retcode;
175-
}
176-
177-
static int my_env_set2( const wchar_t *key, wcstring_list_t &val, int scope )
59+
static int my_env_set( const wchar_t *key, wcstring_list_t &val, int scope )
17860
{
17961
string_buffer_t sb;
18062
size_t i;
@@ -864,7 +746,7 @@ static int builtin_set( parser_t &parser, wchar_t **argv )
864746
if( erase )
865747
{
866748
erase_values(result, indexes);
867-
my_env_set2( dest, result, scope);
749+
my_env_set( dest, result, scope);
868750
}
869751
else
870752
{
@@ -885,9 +767,7 @@ static int builtin_set( parser_t &parser, wchar_t **argv )
885767
sb_append( sb_err, L"\n" );
886768
}
887769

888-
my_env_set2(dest,
889-
result,
890-
scope);
770+
my_env_set(dest, result, scope);
891771

892772
// al_destroy( &value );
893773

@@ -925,18 +805,10 @@ static int builtin_set( parser_t &parser, wchar_t **argv )
925805
}
926806
else
927807
{
928-
array_list_t val;
929-
al_init( &val );
930-
808+
wcstring_list_t val;
931809
for( i=woptind; i<argc; i++ )
932-
{
933-
al_push( &val, argv[i] );
934-
}
935-
936-
retcode = my_env_set( dest, &val, scope );
937-
938-
al_destroy( &val );
939-
810+
val.push_back(argv[i]);
811+
retcode = my_env_set( dest, val, scope );
940812
}
941813
}
942814

0 commit comments

Comments
 (0)