@@ -92,8 +92,8 @@ struct test {
92
92
};
93
93
94
94
static_fn char * nxtarg (struct test * , int );
95
- static_fn int eval_expr (struct test * , int );
96
- static_fn int eval_e3 (struct test * );
95
+ static_fn int eval_expr (Shell_t * shp , struct test * , int );
96
+ static_fn int eval_e3 (Shell_t * shp , struct test * );
97
97
98
98
static_fn int test_strmatch (Shell_t * shp , const char * str , const char * pat ) {
99
99
int match [2 * (MATCH_MAX + 1 )], n ;
@@ -223,7 +223,7 @@ int b_test(int argc, char *argv[], Shbltin_t *context) {
223
223
default : { break ; }
224
224
}
225
225
tdata .ac = argc ;
226
- result = !eval_expr (& tdata , 0 );
226
+ result = !eval_expr (shp , & tdata , 0 );
227
227
228
228
done :
229
229
sh_popcontext (shp , & buff );
@@ -236,11 +236,11 @@ int b_test(int argc, char *argv[], Shbltin_t *context) {
236
236
// Flag is 1 when in parenthesis.
237
237
// Flag is 2 when evaluating -a.
238
238
//
239
- static_fn int eval_expr (struct test * tp , int flag ) {
239
+ static_fn int eval_expr (Shell_t * shp , struct test * tp , int flag ) {
240
240
int r ;
241
241
char * p ;
242
242
243
- r = eval_e3 (tp );
243
+ r = eval_e3 (shp , tp );
244
244
while (tp -> ap < tp -> ac ) {
245
245
p = nxtarg (tp , 0 );
246
246
// Check for -o and -a.
@@ -254,10 +254,10 @@ static_fn int eval_expr(struct test *tp, int flag) {
254
254
tp -> ap -- ;
255
255
break ;
256
256
}
257
- r |= eval_expr (tp , 3 );
257
+ r |= eval_expr (shp , tp , 3 );
258
258
continue ;
259
259
} else if (* p == 'a' ) {
260
- r &= eval_expr (tp , 2 );
260
+ r &= eval_expr (shp , tp , 2 );
261
261
continue ;
262
262
}
263
263
}
@@ -280,15 +280,15 @@ static_fn char *nxtarg(struct test *tp, int mt) {
280
280
return tp -> av [tp -> ap ++ ];
281
281
}
282
282
283
- static_fn int eval_e3 (struct test * tp ) {
283
+ static_fn int eval_e3 (Shell_t * shp , struct test * tp ) {
284
284
char * arg , * cp ;
285
285
int op ;
286
286
char * binop ;
287
287
288
288
arg = nxtarg (tp , 0 );
289
- if (c_eq (arg , '!' )) return !eval_e3 (tp );
289
+ if (c_eq (arg , '!' )) return !eval_e3 (shp , tp );
290
290
if (c_eq (arg , '(' )) {
291
- op = eval_expr (tp , 1 );
291
+ op = eval_expr (shp , tp , 1 );
292
292
cp = nxtarg (tp , 0 );
293
293
if (!cp || !c_eq (cp , ')' )) {
294
294
errormsg (SH_DICT , ERROR_exit (2 ), e_missing , "')'" );
@@ -301,11 +301,14 @@ static_fn int eval_e3(struct test *tp) {
301
301
if (c2_eq (arg , '-' , 't' )) {
302
302
if (cp ) {
303
303
op = strtol (cp , & binop , 10 );
304
- return * binop ? 0 : tty_check (op );
304
+ if (* binop ) return 0 ;
305
+ if (shp -> subshell && op == STDOUT_FILENO ) return 0 ;
306
+ return tty_check (op );
305
307
}
306
308
// Test -t with no arguments.
307
309
tp -> ap -- ;
308
- return tty_check (1 );
310
+ if (shp -> subshell ) return 0 ;
311
+ return tty_check (STDOUT_FILENO );
309
312
}
310
313
if (* arg == '-' && arg [2 ] == 0 ) {
311
314
op = arg [1 ];
@@ -441,7 +444,9 @@ int test_unop(Shell_t *shp, int op, const char *arg) {
441
444
case 't' : {
442
445
char * last ;
443
446
op = strtol (arg , & last , 10 );
444
- return * last ? 0 : tty_check (op );
447
+ if (* last ) return 0 ;
448
+ if (shp -> subshell && op == STDOUT_FILENO ) return 0 ;
449
+ return tty_check (op );
445
450
}
446
451
case 'v' :
447
452
case 'R' : {
0 commit comments