1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
use crate::{current_dir_str, get_config, get_full_help};
use nu_path::expand_path_with;
use nu_protocol::{
ast::{
Argument, Assignment, Block, Call, Expr, Expression, ExternalArgument, PathMember,
PipelineElement, Redirection,
},
engine::{Closure, EngineState, Stack},
eval_base::Eval,
Config, DeclId, IntoPipelineData, IntoSpanned, PipelineData, RawStream, ShellError, Span,
Spanned, Type, Value, VarId, ENV_VARIABLE_ID,
};
use std::thread::{self, JoinHandle};
use std::{borrow::Cow, collections::HashMap};
pub fn eval_call(
engine_state: &EngineState,
caller_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
if nu_utils::ctrl_c::was_pressed(&engine_state.ctrlc) {
return Ok(Value::nothing(call.head).into_pipeline_data());
}
let decl = engine_state.get_decl(call.decl_id);
if !decl.is_known_external() && call.named_iter().any(|(flag, _, _)| flag.item == "help") {
let mut signature = engine_state.get_signature(decl);
signature.usage = decl.usage().to_string();
signature.extra_usage = decl.extra_usage().to_string();
let full_help = get_full_help(
&signature,
&decl.examples(),
engine_state,
caller_stack,
decl.is_parser_keyword(),
);
Ok(Value::string(full_help, call.head).into_pipeline_data())
} else if let Some(block_id) = decl.get_block_id() {
let block = engine_state.get_block(block_id);
let mut callee_stack = caller_stack.gather_captures(engine_state, &block.captures);
// Rust does not check recursion limits outside of const evaluation.
// But nu programs run in the same process as the shell.
// To prevent a stack overflow in user code from crashing the shell,
// we limit the recursion depth of function calls.
// Picked 50 arbitrarily, should work on all architectures.
const MAXIMUM_CALL_STACK_DEPTH: u64 = 50;
callee_stack.recursion_count += 1;
if callee_stack.recursion_count > MAXIMUM_CALL_STACK_DEPTH {
callee_stack.recursion_count = 0;
return Err(ShellError::RecursionLimitReached {
recursion_limit: MAXIMUM_CALL_STACK_DEPTH,
span: block.span,
});
}
for (param_idx, (param, required)) in decl
.signature()
.required_positional
.iter()
.map(|p| (p, true))
.chain(
decl.signature()
.optional_positional
.iter()
.map(|p| (p, false)),
)
.enumerate()
{
let var_id = param
.var_id
.expect("internal error: all custom parameters must have var_ids");
if let Some(arg) = call.positional_nth(param_idx) {
let result = eval_expression(engine_state, caller_stack, arg)?;
let param_type = param.shape.to_type();
if required && !result.get_type().is_subtype(¶m_type) {
// need to check if result is an empty list, and param_type is table or list
// nushell needs to pass type checking for the case.
let empty_list_matches = result
.as_list()
.map(|l| {
l.is_empty() && matches!(param_type, Type::List(_) | Type::Table(_))
})
.unwrap_or(false);
if !empty_list_matches {
return Err(ShellError::CantConvert {
to_type: param.shape.to_type().to_string(),
from_type: result.get_type().to_string(),
span: result.span(),
help: None,
});
}
}
callee_stack.add_var(var_id, result);
} else if let Some(value) = ¶m.default_value {
callee_stack.add_var(var_id, value.to_owned());
} else {
callee_stack.add_var(var_id, Value::nothing(call.head));
}
}
if let Some(rest_positional) = decl.signature().rest_positional {
let mut rest_items = vec![];
for result in call.rest_iter_flattened(
decl.signature().required_positional.len()
+ decl.signature().optional_positional.len(),
|expr| eval_expression(engine_state, caller_stack, expr),
)? {
rest_items.push(result);
}
let span = if let Some(rest_item) = rest_items.first() {
rest_item.span()
} else {
call.head
};
callee_stack.add_var(
rest_positional
.var_id
.expect("Internal error: rest positional parameter lacks var_id"),
Value::list(rest_items, span),
)
}
for named in decl.signature().named {
if let Some(var_id) = named.var_id {
let mut found = false;
for call_named in call.named_iter() {
if let (Some(spanned), Some(short)) = (&call_named.1, named.short) {
if spanned.item == short.to_string() {
if let Some(arg) = &call_named.2 {
let result = eval_expression(engine_state, caller_stack, arg)?;
callee_stack.add_var(var_id, result);
} else if let Some(value) = &named.default_value {
callee_stack.add_var(var_id, value.to_owned());
} else {
callee_stack.add_var(var_id, Value::bool(true, call.head))
}
found = true;
}
} else if call_named.0.item == named.long {
if let Some(arg) = &call_named.2 {
let result = eval_expression(engine_state, caller_stack, arg)?;
callee_stack.add_var(var_id, result);
} else if let Some(value) = &named.default_value {
callee_stack.add_var(var_id, value.to_owned());
} else {
callee_stack.add_var(var_id, Value::bool(true, call.head))
}
found = true;
}
}
if !found {
if named.arg.is_none() {
callee_stack.add_var(var_id, Value::bool(false, call.head))
} else if let Some(value) = named.default_value {
callee_stack.add_var(var_id, value);
} else {
callee_stack.add_var(var_id, Value::nothing(call.head))
}
}
}
}
let result = eval_block_with_early_return(
engine_state,
&mut callee_stack,
block,
input,
call.redirect_stdout,
call.redirect_stderr,
);
if block.redirect_env {
redirect_env(engine_state, caller_stack, &callee_stack);
}
result
} else {
// We pass caller_stack here with the knowledge that internal commands
// are going to be specifically looking for global state in the stack
// rather than any local state.
decl.run(engine_state, caller_stack, call, input)
}
}
/// Redirect the environment from callee to the caller.
pub fn redirect_env(engine_state: &EngineState, caller_stack: &mut Stack, callee_stack: &Stack) {
// Grab all environment variables from the callee
let caller_env_vars = caller_stack.get_env_var_names(engine_state);
// remove env vars that are present in the caller but not in the callee
// (the callee hid them)
for var in caller_env_vars.iter() {
if !callee_stack.has_env_var(engine_state, var) {
caller_stack.remove_env_var(engine_state, var);
}
}
// add new env vars from callee to caller
for (var, value) in callee_stack.get_stack_env_vars() {
caller_stack.add_env_var(var, value);
}
}
enum RedirectTarget {
Piped(bool, bool),
CombinedPipe,
}
#[allow(clippy::too_many_arguments)]
fn eval_external(
engine_state: &EngineState,
stack: &mut Stack,
head: &Expression,
args: &[ExternalArgument],
input: PipelineData,
redirect_target: RedirectTarget,
is_subexpression: bool,
) -> Result<PipelineData, ShellError> {
let decl_id = engine_state
.find_decl("run-external".as_bytes(), &[])
.ok_or(ShellError::ExternalNotSupported { span: head.span })?;
let command = engine_state.get_decl(decl_id);
let mut call = Call::new(head.span);
call.add_positional(head.clone());
for arg in args {
match arg {
ExternalArgument::Regular(expr) => call.add_positional(expr.clone()),
ExternalArgument::Spread(expr) => call.add_spread(expr.clone()),
}
}
match redirect_target {
RedirectTarget::Piped(redirect_stdout, redirect_stderr) => {
if redirect_stdout {
call.add_named((
Spanned {
item: "redirect-stdout".into(),
span: head.span,
},
None,
None,
))
}
if redirect_stderr {
call.add_named((
Spanned {
item: "redirect-stderr".into(),
span: head.span,
},
None,
None,
))
}
}
RedirectTarget::CombinedPipe => call.add_named((
Spanned {
item: "redirect-combine".into(),
span: head.span,
},
None,
None,
)),
}
if is_subexpression {
call.add_named((
Spanned {
item: "trim-end-newline".into(),
span: head.span,
},
None,
None,
))
}
command.run(engine_state, stack, &call, input)
}
pub fn eval_expression(
engine_state: &EngineState,
stack: &mut Stack,
expr: &Expression,
) -> Result<Value, ShellError> {
<EvalRuntime as Eval>::eval(engine_state, stack, expr)
}
/// Checks the expression to see if it's a internal or external call. If so, passes the input
/// into the call and gets out the result
/// Otherwise, invokes the expression
///
/// It returns PipelineData with a boolean flag, indicating if the external failed to run.
/// The boolean flag **may only be true** for external calls, for internal calls, it always to be false.
pub fn eval_expression_with_input(
engine_state: &EngineState,
stack: &mut Stack,
expr: &Expression,
mut input: PipelineData,
redirect_stdout: bool,
redirect_stderr: bool,
) -> Result<(PipelineData, bool), ShellError> {
match expr {
Expression {
expr: Expr::Call(call),
..
} => {
if !redirect_stdout || redirect_stderr {
// we're doing something different than the defaults
let mut call = call.clone();
call.redirect_stdout = redirect_stdout;
call.redirect_stderr = redirect_stderr;
input = eval_call(engine_state, stack, &call, input)?;
} else {
input = eval_call(engine_state, stack, call, input)?;
}
}
Expression {
expr: Expr::ExternalCall(head, args, is_subexpression),
..
} => {
input = eval_external(
engine_state,
stack,
head,
args,
input,
RedirectTarget::Piped(redirect_stdout, redirect_stderr),
*is_subexpression,
)?;
}
Expression {
expr: Expr::Subexpression(block_id),
..
} => {
let block = engine_state.get_block(*block_id);
// FIXME: protect this collect with ctrl-c
input = eval_subexpression(engine_state, stack, block, input)?;
}
elem @ Expression {
expr: Expr::FullCellPath(full_cell_path),
..
} => match &full_cell_path.head {
Expression {
expr: Expr::Subexpression(block_id),
span,
..
} => {
let block = engine_state.get_block(*block_id);
// FIXME: protect this collect with ctrl-c
input = eval_subexpression(engine_state, stack, block, input)?;
let value = input.into_value(*span);
input = value
.follow_cell_path(&full_cell_path.tail, false)?
.into_pipeline_data()
}
_ => {
input = eval_expression(engine_state, stack, elem)?.into_pipeline_data();
}
},
elem => {
input = eval_expression(engine_state, stack, elem)?.into_pipeline_data();
}
};
// Given input is PipelineData::ExternalStream
// `might_consume_external_result` will consume `stderr` stream if `stdout` is empty.
// it's not intended if user want to redirect stderr message.
//
// e.g:
// 1. cargo check e>| less
// 2. cargo check e> result.txt
//
// In these two cases, stdout will be empty, but nushell shouldn't consume the `stderr`
// stream it needs be passed to next command.
if !redirect_stderr {
Ok(might_consume_external_result(input))
} else {
Ok((input, false))
}
}
// Try to catch and detect if external command runs to failed.
fn might_consume_external_result(input: PipelineData) -> (PipelineData, bool) {
input.is_external_failed()
}
#[allow(clippy::too_many_arguments)]
fn eval_element_with_input(
engine_state: &EngineState,
stack: &mut Stack,
element: &PipelineElement,
mut input: PipelineData,
redirect_stdout: bool,
redirect_stderr: bool,
redirect_combine: bool,
stderr_writer_jobs: &mut Vec<DataSaveJob>,
) -> Result<(PipelineData, bool), ShellError> {
match element {
PipelineElement::Expression(pipe_span, expr)
| PipelineElement::OutErrPipedExpression(pipe_span, expr) => {
if matches!(element, PipelineElement::OutErrPipedExpression(..))
&& !matches!(input, PipelineData::ExternalStream { .. })
{
return Err(ShellError::GenericError {
error: "`o+e>|` only works with external streams".into(),
msg: "`o+e>|` only works on external streams".into(),
span: *pipe_span,
help: None,
inner: vec![],
});
}
match expr {
Expression {
expr: Expr::ExternalCall(head, args, is_subexpression),
..
} if redirect_combine => {
let result = eval_external(
engine_state,
stack,
head,
args,
input,
RedirectTarget::CombinedPipe,
*is_subexpression,
)?;
Ok(might_consume_external_result(result))
}
_ => eval_expression_with_input(
engine_state,
stack,
expr,
input,
redirect_stdout,
redirect_stderr,
),
}
}
PipelineElement::ErrPipedExpression(pipe_span, expr) => {
let input = match input {
PipelineData::ExternalStream {
stdout,
stderr,
exit_code,
span,
metadata,
trim_end_newline,
} => PipelineData::ExternalStream {
stdout: stderr, // swap stderr and stdout to get stderr piped feature.
stderr: stdout,
exit_code,
span,
metadata,
trim_end_newline,
},
_ => {
return Err(ShellError::GenericError {
error: "`e>|` only works with external streams".into(),
msg: "`e>|` only works on external streams".into(),
span: *pipe_span,
help: None,
inner: vec![],
})
}
};
eval_expression_with_input(
engine_state,
stack,
expr,
input,
redirect_stdout,
redirect_stderr,
)
}
PipelineElement::Redirection(span, redirection, expr, is_append_mode) => {
match &expr.expr {
Expr::String(_)
| Expr::FullCellPath(_)
| Expr::StringInterpolation(_)
| Expr::Filepath(_, _) => {
let exit_code = match &mut input {
PipelineData::ExternalStream { exit_code, .. } => exit_code.take(),
_ => None,
};
let (input, result_out_stream, result_is_out) =
adjust_stream_for_input_and_output(input, redirection);
if let Some(save_command) = engine_state.find_decl(b"save", &[]) {
let save_call = gen_save_call(
save_command,
(*span, expr.clone(), *is_append_mode),
None,
);
match result_out_stream {
None => {
eval_call(engine_state, stack, &save_call, input).map(|_| {
// save is internal command, normally it exists with non-ExternalStream
// but here in redirection context, we make it returns ExternalStream
// So nu handles exit_code correctly
//
// Also, we don't want to run remaining commands if this command exits with non-zero
// exit code, so we need to consume and check exit_code too
might_consume_external_result(PipelineData::ExternalStream {
stdout: None,
stderr: None,
exit_code,
span: *span,
metadata: None,
trim_end_newline: false,
})
})
}
Some(result_out_stream) => {
// delegate to a different thread
// so nushell won't hang if external command generates both too much
// stderr and stdout message
let stderr_stack = stack.clone();
let engine_state_clone = engine_state.clone();
stderr_writer_jobs.push(DataSaveJob::spawn(
engine_state_clone,
stderr_stack,
save_call,
input,
)?);
let (result_out_stream, result_err_stream) = if result_is_out {
(result_out_stream, None)
} else {
// we need `stdout` to be an empty RawStream
// so nushell knows this result is not the last part of a command.
(
Some(RawStream::new(
Box::new(std::iter::empty()),
None,
*span,
Some(0),
)),
result_out_stream,
)
};
Ok(might_consume_external_result(
PipelineData::ExternalStream {
stdout: result_out_stream,
stderr: result_err_stream,
exit_code,
span: *span,
metadata: None,
trim_end_newline: false,
},
))
}
}
} else {
Err(ShellError::CommandNotFound { span: *span })
}
}
_ => Err(ShellError::CommandNotFound { span: *span }),
}
}
PipelineElement::SeparateRedirection {
out: (out_span, out_expr, out_append_mode),
err: (err_span, err_expr, err_append_mode),
} => match (&out_expr.expr, &err_expr.expr) {
(
Expr::String(_)
| Expr::FullCellPath(_)
| Expr::StringInterpolation(_)
| Expr::Filepath(_, _),
Expr::String(_)
| Expr::FullCellPath(_)
| Expr::StringInterpolation(_)
| Expr::Filepath(_, _),
) => {
if let Some(save_command) = engine_state.find_decl(b"save", &[]) {
let exit_code = match &mut input {
PipelineData::ExternalStream { exit_code, .. } => exit_code.take(),
_ => None,
};
let save_call = gen_save_call(
save_command,
(*out_span, out_expr.clone(), *out_append_mode),
Some((*err_span, err_expr.clone(), *err_append_mode)),
);
eval_call(engine_state, stack, &save_call, input).map(|_| {
// save is internal command, normally it exists with non-ExternalStream
// but here in redirection context, we make it returns ExternalStream
// So nu handles exit_code correctly
might_consume_external_result(PipelineData::ExternalStream {
stdout: None,
stderr: None,
exit_code,
span: *out_span,
metadata: None,
trim_end_newline: false,
})
})
} else {
Err(ShellError::CommandNotFound { span: *out_span })
}
}
(_out_other, err_other) => {
if let Expr::String(_) = err_other {
Err(ShellError::CommandNotFound { span: *out_span })
} else {
Err(ShellError::CommandNotFound { span: *err_span })
}
}
},
PipelineElement::SameTargetRedirection {
cmd: (cmd_span, cmd_exp),
redirection: (redirect_span, redirect_exp, is_append_mode),
} => {
// general idea: eval cmd and call save command to redirect stdout to result.
input = match &cmd_exp.expr {
Expr::ExternalCall(head, args, is_subexpression) => {
// if cmd's expression is ExternalStream, then invoke run-external with
// special --redirect-combine flag.
eval_external(
engine_state,
stack,
head,
args,
input,
RedirectTarget::CombinedPipe,
*is_subexpression,
)?
}
_ => {
// we need to redirect output, so the result can be saved and pass to `save` command.
eval_element_with_input(
engine_state,
stack,
&PipelineElement::Expression(*cmd_span, cmd_exp.clone()),
input,
true,
redirect_stderr,
redirect_combine,
stderr_writer_jobs,
)
.map(|x| x.0)?
}
};
eval_element_with_input(
engine_state,
stack,
&PipelineElement::Redirection(
*redirect_span,
Redirection::Stdout,
redirect_exp.clone(),
*is_append_mode,
),
input,
redirect_stdout,
redirect_stderr,
redirect_combine,
stderr_writer_jobs,
)
}
PipelineElement::And(_, expr) => eval_expression_with_input(
engine_state,
stack,
expr,
input,
redirect_stdout,
redirect_stderr,
),
PipelineElement::Or(_, expr) => eval_expression_with_input(
engine_state,
stack,
expr,
input,
redirect_stdout,
redirect_stderr,
),
}
}
// In redirection context, if nushell gets an ExternalStream
// it might want to take a stream from `input`(if `input` is `PipelineData::ExternalStream`)
// so this stream can be handled by next command.
//
//
// 1. get a stderr redirection, we need to take `stdout` out of `input`.
// e.g: nu --testbin echo_env FOO e> /dev/null | str length
// 2. get a stdout redirection, we need to take `stderr` out of `input`.
// e.g: nu --testbin echo_env FOO o> /dev/null e>| str length
//
// Returns 3 values:
// 1. adjusted pipeline data
// 2. a result stream which is taken from `input`, it can be handled in next command
// 3. a boolean value indicates if result stream should be a stdout stream.
fn adjust_stream_for_input_and_output(
input: PipelineData,
redirection: &Redirection,
) -> (PipelineData, Option<Option<RawStream>>, bool) {
match (redirection, input) {
(
Redirection::Stderr,
PipelineData::ExternalStream {
stdout,
stderr,
exit_code,
span,
metadata,
trim_end_newline,
},
) => (
PipelineData::ExternalStream {
stdout: stderr,
stderr: None,
exit_code,
span,
metadata,
trim_end_newline,
},
Some(stdout),
true,
),
(
Redirection::Stdout,
PipelineData::ExternalStream {
stdout,
stderr,
exit_code,
span,
metadata,
trim_end_newline,
},
) => (
PipelineData::ExternalStream {
stdout,
stderr: None,
exit_code,
span,
metadata,
trim_end_newline,
},
Some(stderr),
false,
),
(_, input) => (input, None, true),
}
}
fn is_redirect_stderr_required(elements: &[PipelineElement], idx: usize) -> bool {
let elements_length = elements.len();
if idx < elements_length - 1 {
let next_element = &elements[idx + 1];
match next_element {
PipelineElement::Redirection(_, Redirection::Stderr, _, _)
| PipelineElement::Redirection(_, Redirection::StdoutAndStderr, _, _)
| PipelineElement::SeparateRedirection { .. }
| PipelineElement::ErrPipedExpression(..)
| PipelineElement::OutErrPipedExpression(..) => return true,
PipelineElement::Redirection(_, Redirection::Stdout, _, _) => {
// a stderr redirection, but we still need to check for the next 2nd
// element, to handle for the following case:
// cat a.txt out> /dev/null e>| lines
//
// we only need to check the next 2nd element because we already make sure
// that we don't have duplicate err> like this:
// cat a.txt out> /dev/null err> /tmp/a
if idx < elements_length - 2 {
let next_2nd_element = &elements[idx + 2];
if matches!(next_2nd_element, PipelineElement::ErrPipedExpression(..)) {
return true;
}
}
}
_ => {}
}
}
false
}
fn is_redirect_stdout_required(elements: &[PipelineElement], idx: usize) -> bool {
let elements_length = elements.len();
if idx < elements_length - 1 {
let next_element = &elements[idx + 1];
match next_element {
// is next element a stdout relative redirection?
PipelineElement::Redirection(_, Redirection::Stdout, _, _)
| PipelineElement::Redirection(_, Redirection::StdoutAndStderr, _, _)
| PipelineElement::SeparateRedirection { .. }
| PipelineElement::Expression(..)
| PipelineElement::OutErrPipedExpression(..) => return true,
PipelineElement::Redirection(_, Redirection::Stderr, _, _) => {
// a stderr redirection, but we still need to check for the next 2nd
// element, to handle for the following case:
// cat a.txt err> /dev/null | lines
//
// we only need to check the next 2nd element because we already make sure
// that we don't have duplicate err> like this:
// cat a.txt err> /dev/null err> /tmp/a
if idx < elements_length - 2 {
let next_2nd_element = &elements[idx + 2];
if matches!(next_2nd_element, PipelineElement::Expression(..)) {
return true;
}
}
}
_ => {}
}
}
false
}
fn is_redirect_combine_required(elements: &[PipelineElement], idx: usize) -> bool {
let elements_length = elements.len();
idx < elements_length - 1
&& matches!(
&elements[idx + 1],
PipelineElement::OutErrPipedExpression(..)
)
}
pub fn eval_block_with_early_return(
engine_state: &EngineState,
stack: &mut Stack,
block: &Block,
input: PipelineData,
redirect_stdout: bool,
redirect_stderr: bool,
) -> Result<PipelineData, ShellError> {
match eval_block(
engine_state,
stack,
block,
input,
redirect_stdout,
redirect_stderr,
) {
Err(ShellError::Return { span: _, value }) => Ok(PipelineData::Value(*value, None)),
x => x,
}
}
pub fn eval_block(
engine_state: &EngineState,
stack: &mut Stack,
block: &Block,
mut input: PipelineData,
redirect_stdout: bool,
redirect_stderr: bool,
) -> Result<PipelineData, ShellError> {
let num_pipelines = block.len();
for (pipeline_idx, pipeline) in block.pipelines.iter().enumerate() {
let mut stderr_writer_jobs = vec![];
let elements = &pipeline.elements;
let elements_length = pipeline.elements.len();
for (idx, element) in elements.iter().enumerate() {
let mut redirect_stdout = redirect_stdout;
let mut redirect_stderr = redirect_stderr;
if !redirect_stderr && is_redirect_stderr_required(elements, idx) {
redirect_stderr = true;
}
if !redirect_stdout {
if is_redirect_stdout_required(elements, idx) {
redirect_stdout = true;
}
} else if idx < elements_length - 1
&& matches!(elements[idx + 1], PipelineElement::ErrPipedExpression(..))
{
redirect_stdout = false;
}
let redirect_combine = is_redirect_combine_required(elements, idx);
// if eval internal command failed, it can just make early return with `Err(ShellError)`.
let eval_result = eval_element_with_input(
engine_state,
stack,
element,
input,
redirect_stdout,
redirect_stderr,
redirect_combine,
&mut stderr_writer_jobs,
);
match (eval_result, redirect_stderr) {
(Err(error), true) => {
input = PipelineData::Value(
Value::error(
error,
Span::unknown(), // FIXME: where does this span come from?
),
None,
)
}
(output, _) => {
let output = output?;
input = output.0;
// external command may runs to failed
// make early return so remaining commands will not be executed.
// don't return `Err(ShellError)`, so nushell wouldn't show extra error message.
if output.1 {
return Ok(input);
}
}
}
}
// `eval_element_with_input` may creates some threads
// to write stderr message to a file, here we need to wait and make sure that it's
// finished.
for h in stderr_writer_jobs {
let _ = h.join();
}
if pipeline_idx < (num_pipelines) - 1 {
match input {
PipelineData::Value(Value::Nothing { .. }, ..) => {}
PipelineData::ExternalStream {
ref mut exit_code, ..
} => {
let exit_code = exit_code.take();
input.drain()?;
if let Some(exit_code) = exit_code {
let mut v: Vec<_> = exit_code.collect();
if let Some(v) = v.pop() {
let break_loop = !matches!(v.as_i64(), Ok(0));
stack.add_env_var("LAST_EXIT_CODE".into(), v);
if break_loop {
input = PipelineData::empty();
break;
}
}
}
}
_ => input.drain()?,
}
input = PipelineData::empty()
}
}
Ok(input)
}
pub fn eval_subexpression(
engine_state: &EngineState,
stack: &mut Stack,
block: &Block,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
eval_block(engine_state, stack, block, input, true, false)
}
pub fn eval_variable(
engine_state: &EngineState,
stack: &Stack,
var_id: VarId,
span: Span,
) -> Result<Value, ShellError> {
match var_id {
// $nu
nu_protocol::NU_VARIABLE_ID => {
if let Some(val) = engine_state.get_constant(var_id) {
Ok(val.clone())
} else {
Err(ShellError::VariableNotFoundAtRuntime { span })
}
}
// $env
ENV_VARIABLE_ID => {
let env_vars = stack.get_env_vars(engine_state);
let env_columns = env_vars.keys();
let env_values = env_vars.values();
let mut pairs = env_columns
.map(|x| x.to_string())
.zip(env_values.cloned())
.collect::<Vec<(String, Value)>>();
pairs.sort_by(|a, b| a.0.cmp(&b.0));
Ok(Value::record(pairs.into_iter().collect(), span))
}
var_id => stack.get_var(var_id, span),
}
}
fn gen_save_call(
save_decl_id: DeclId,
out_info: (Span, Expression, bool),
err_info: Option<(Span, Expression, bool)>,
) -> Call {
let (out_span, out_expr, out_append_mode) = out_info;
let mut call = Call {
decl_id: save_decl_id,
head: out_span,
arguments: vec![],
redirect_stdout: false,
redirect_stderr: false,
parser_info: HashMap::new(),
};
let mut args = vec![
Argument::Positional(out_expr),
Argument::Named((
Spanned {
item: "raw".into(),
span: out_span,
},
None,
None,
)),
Argument::Named((
Spanned {
item: "force".into(),
span: out_span,
},
None,
None,
)),
];
if out_append_mode {
call.set_parser_info(
"out-append".to_string(),
Expression {
expr: Expr::Bool(true),
span: out_span,
ty: Type::Bool,
custom_completion: None,
},
);
}
if let Some((err_span, err_expr, err_append_mode)) = err_info {
args.push(Argument::Named((
Spanned {
item: "stderr".into(),
span: err_span,
},
None,
Some(err_expr),
)));
if err_append_mode {
call.set_parser_info(
"err-append".to_string(),
Expression {
expr: Expr::Bool(true),
span: err_span,
ty: Type::Bool,
custom_completion: None,
},
);
}
}
call.arguments.append(&mut args);
call
}
/// A job which saves `PipelineData` to a file in a child thread.
struct DataSaveJob {
inner: JoinHandle<()>,
}
impl DataSaveJob {
pub fn spawn(
engine_state: EngineState,
mut stack: Stack,
save_call: Call,
input: PipelineData,
) -> Result<Self, ShellError> {
let span = save_call.head;
Ok(Self {
inner: thread::Builder::new()
.name("stderr saver".to_string())
.spawn(move || {
let result = eval_call(&engine_state, &mut stack, &save_call, input);
if let Err(err) = result {
eprintln!("WARNING: error occurred when redirect to stderr: {:?}", err);
}
})
.map_err(|e| e.into_spanned(span))?,
})
}
pub fn join(self) -> thread::Result<()> {
self.inner.join()
}
}
struct EvalRuntime;
impl Eval for EvalRuntime {
type State<'a> = &'a EngineState;
type MutState = Stack;
fn get_config<'a>(engine_state: Self::State<'a>, stack: &mut Stack) -> Cow<'a, Config> {
Cow::Owned(get_config(engine_state, stack))
}
fn eval_filepath(
engine_state: &EngineState,
stack: &mut Stack,
path: String,
quoted: bool,
span: Span,
) -> Result<Value, ShellError> {
if quoted {
Ok(Value::string(path, span))
} else {
let cwd = current_dir_str(engine_state, stack)?;
let path = expand_path_with(path, cwd);
Ok(Value::string(path.to_string_lossy(), span))
}
}
fn eval_directory(
engine_state: Self::State<'_>,
stack: &mut Self::MutState,
path: String,
quoted: bool,
span: Span,
) -> Result<Value, ShellError> {
if path == "-" {
Ok(Value::string("-", span))
} else if quoted {
Ok(Value::string(path, span))
} else {
let cwd = current_dir_str(engine_state, stack)?;
let path = expand_path_with(path, cwd);
Ok(Value::string(path.to_string_lossy(), span))
}
}
fn eval_var(
engine_state: &EngineState,
stack: &mut Stack,
var_id: VarId,
span: Span,
) -> Result<Value, ShellError> {
eval_variable(engine_state, stack, var_id, span)
}
fn eval_call(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_: Span,
) -> Result<Value, ShellError> {
// FIXME: protect this collect with ctrl-c
Ok(eval_call(engine_state, stack, call, PipelineData::empty())?.into_value(call.head))
}
fn eval_external_call(
engine_state: &EngineState,
stack: &mut Stack,
head: &Expression,
args: &[ExternalArgument],
is_subexpression: bool,
_: Span,
) -> Result<Value, ShellError> {
let span = head.span;
// FIXME: protect this collect with ctrl-c
Ok(eval_external(
engine_state,
stack,
head,
args,
PipelineData::empty(),
RedirectTarget::Piped(false, false),
is_subexpression,
)?
.into_value(span))
}
fn eval_subexpression(
engine_state: &EngineState,
stack: &mut Stack,
block_id: usize,
span: Span,
) -> Result<Value, ShellError> {
let block = engine_state.get_block(block_id);
// FIXME: protect this collect with ctrl-c
Ok(eval_subexpression(engine_state, stack, block, PipelineData::empty())?.into_value(span))
}
fn regex_match(
engine_state: &EngineState,
op_span: Span,
lhs: &Value,
rhs: &Value,
invert: bool,
expr_span: Span,
) -> Result<Value, ShellError> {
lhs.regex_match(engine_state, op_span, rhs, invert, expr_span)
}
fn eval_assignment(
engine_state: &EngineState,
stack: &mut Stack,
lhs: &Expression,
rhs: &Expression,
assignment: Assignment,
op_span: Span,
_expr_span: Span,
) -> Result<Value, ShellError> {
let rhs = eval_expression(engine_state, stack, rhs)?;
let rhs = match assignment {
Assignment::Assign => rhs,
Assignment::PlusAssign => {
let lhs = eval_expression(engine_state, stack, lhs)?;
lhs.add(op_span, &rhs, op_span)?
}
Assignment::MinusAssign => {
let lhs = eval_expression(engine_state, stack, lhs)?;
lhs.sub(op_span, &rhs, op_span)?
}
Assignment::MultiplyAssign => {
let lhs = eval_expression(engine_state, stack, lhs)?;
lhs.mul(op_span, &rhs, op_span)?
}
Assignment::DivideAssign => {
let lhs = eval_expression(engine_state, stack, lhs)?;
lhs.div(op_span, &rhs, op_span)?
}
Assignment::AppendAssign => {
let lhs = eval_expression(engine_state, stack, lhs)?;
lhs.append(op_span, &rhs, op_span)?
}
};
match &lhs.expr {
Expr::Var(var_id) | Expr::VarDecl(var_id) => {
let var_info = engine_state.get_var(*var_id);
if var_info.mutable {
stack.add_var(*var_id, rhs);
Ok(Value::nothing(lhs.span))
} else {
Err(ShellError::AssignmentRequiresMutableVar { lhs_span: lhs.span })
}
}
Expr::FullCellPath(cell_path) => {
match &cell_path.head.expr {
Expr::Var(var_id) | Expr::VarDecl(var_id) => {
// The $env variable is considered "mutable" in Nushell.
// As such, give it special treatment here.
let is_env = var_id == &ENV_VARIABLE_ID;
if is_env || engine_state.get_var(*var_id).mutable {
let mut lhs = eval_expression(engine_state, stack, &cell_path.head)?;
lhs.upsert_data_at_cell_path(&cell_path.tail, rhs)?;
if is_env {
if cell_path.tail.is_empty() {
return Err(ShellError::CannotReplaceEnv {
span: cell_path.head.span,
});
}
// The special $env treatment: for something like $env.config.history.max_size = 2000,
// get $env.config (or whichever one it is) AFTER the above mutation, and set it
// as the "config" environment variable.
let vardata =
lhs.follow_cell_path(&[cell_path.tail[0].clone()], false)?;
match &cell_path.tail[0] {
PathMember::String { val, span, .. } => {
if val == "FILE_PWD"
|| val == "CURRENT_FILE"
|| val == "PWD"
{
return Err(ShellError::AutomaticEnvVarSetManually {
envvar_name: val.to_string(),
span: *span,
});
} else {
stack.add_env_var(val.to_string(), vardata);
}
}
// In case someone really wants an integer env-var
PathMember::Int { val, .. } => {
stack.add_env_var(val.to_string(), vardata);
}
}
} else {
stack.add_var(*var_id, lhs);
}
Ok(Value::nothing(cell_path.head.span))
} else {
Err(ShellError::AssignmentRequiresMutableVar { lhs_span: lhs.span })
}
}
_ => Err(ShellError::AssignmentRequiresVar { lhs_span: lhs.span }),
}
}
_ => Err(ShellError::AssignmentRequiresVar { lhs_span: lhs.span }),
}
}
fn eval_row_condition_or_closure(
engine_state: &EngineState,
stack: &mut Stack,
block_id: usize,
span: Span,
) -> Result<Value, ShellError> {
let captures = engine_state
.get_block(block_id)
.captures
.iter()
.map(|&id| {
stack
.get_var(id, span)
.or_else(|_| {
engine_state
.get_var(id)
.const_val
.clone()
.ok_or(ShellError::VariableNotFoundAtRuntime { span })
})
.map(|var| (id, var))
})
.collect::<Result<_, _>>()?;
Ok(Value::closure(Closure { block_id, captures }, span))
}
fn eval_overlay(engine_state: &EngineState, span: Span) -> Result<Value, ShellError> {
let name = String::from_utf8_lossy(engine_state.get_span_contents(span)).to_string();
Ok(Value::string(name, span))
}
fn unreachable(expr: &Expression) -> Result<Value, ShellError> {
Ok(Value::nothing(expr.span))
}
}