summaryrefslogtreecommitdiff
path: root/crates/nu-command/src/system/run_external.rs
blob: d4645e87e83afefbc6e7e4a14deb2aac489fb63d (plain)
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
use nu_cmd_base::hook::eval_hook;
use nu_engine::{command_prelude::*, env_to_strings, get_eval_expression};
use nu_protocol::{
    ast::{Expr, Expression},
    did_you_mean,
    process::ChildProcess,
    ByteStream, NuGlob, OutDest,
};
use nu_system::ForegroundChild;
use nu_utils::IgnoreCaseExt;
use std::{
    borrow::Cow,
    io::Write,
    path::{Path, PathBuf},
    process::Stdio,
    sync::{atomic::AtomicBool, Arc},
    thread,
};

#[derive(Clone)]
pub struct External;

impl Command for External {
    fn name(&self) -> &str {
        "run-external"
    }

    fn usage(&self) -> &str {
        "Runs external command."
    }

    fn signature(&self) -> nu_protocol::Signature {
        Signature::build(self.name())
            .input_output_types(vec![(Type::Any, Type::Any)])
            .required("command", SyntaxShape::String, "External command to run.")
            .rest("args", SyntaxShape::Any, "Arguments for external command.")
            .category(Category::System)
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let cwd = engine_state.cwd(Some(stack))?;

        // Evaluate the command name in the same way the arguments are evaluated. Since this isn't
        // a spread, it should return a one-element vec.
        let name_expr = call
            .positional_nth(0)
            .ok_or_else(|| ShellError::MissingParameter {
                param_name: "command".into(),
                span: call.head,
            })?;
        let name = eval_argument(engine_state, stack, name_expr, false)?
            .pop()
            .expect("eval_argument returned zero-element vec")
            .into_spanned(name_expr.span);

        // Find the absolute path to the executable. On Windows, set the
        // executable to "cmd.exe" if it's is a CMD internal command. If the
        // command is not found, display a helpful error message.
        let executable = if cfg!(windows) && is_cmd_internal_command(&name.item) {
            PathBuf::from("cmd.exe")
        } else {
            // Expand tilde on the name if it's a bare string (#13000)
            let expanded_name = if is_bare_string(name_expr) {
                expand_tilde(&name.item)
            } else {
                name.item.clone()
            };

            // Determine the PATH to be used and then use `which` to find it - though this has no
            // effect if it's an absolute path already
            let paths = nu_engine::env::path_str(engine_state, stack, call.head)?;
            let Some(executable) = which(&expanded_name, &paths, &cwd) else {
                return Err(command_not_found(
                    &name.item,
                    call.head,
                    engine_state,
                    stack,
                ));
            };
            executable
        };

        // Create the command.
        let mut command = std::process::Command::new(executable);

        // Configure PWD.
        command.current_dir(cwd);

        // Configure environment variables.
        let envs = env_to_strings(engine_state, stack)?;
        command.env_clear();
        command.envs(envs);

        // Configure args.
        let args = eval_arguments_from_call(engine_state, stack, call)?;
        #[cfg(windows)]
        if is_cmd_internal_command(&name.item) {
            use std::os::windows::process::CommandExt;

            // The /D flag disables execution of AutoRun commands from registry.
            // The /C flag followed by a command name instructs CMD to execute
            // that command and quit.
            command.args(["/D", "/C", &name.item]);
            for arg in &args {
                command.raw_arg(escape_cmd_argument(arg)?.as_ref());
            }
        } else {
            command.args(args.into_iter().map(|s| s.item));
        }
        #[cfg(not(windows))]
        command.args(args.into_iter().map(|s| s.item));

        // Configure stdout and stderr. If both are set to `OutDest::Pipe`,
        // we'll setup a pipe that merge two streams into one.
        let stdout = stack.stdout();
        let stderr = stack.stderr();
        let merged_stream = if matches!(stdout, OutDest::Pipe) && matches!(stderr, OutDest::Pipe) {
            let (reader, writer) = os_pipe::pipe()?;
            command.stdout(writer.try_clone()?);
            command.stderr(writer);
            Some(reader)
        } else {
            command.stdout(Stdio::try_from(stdout)?);
            command.stderr(Stdio::try_from(stderr)?);
            None
        };

        // Configure stdin. We'll try connecting input to the child process
        // directly. If that's not possible, we'll setup a pipe and spawn a
        // thread to copy data into the child process.
        let data_to_copy_into_stdin = match input {
            PipelineData::ByteStream(stream, metadata) => match stream.into_stdio() {
                Ok(stdin) => {
                    command.stdin(stdin);
                    None
                }
                Err(stream) => {
                    command.stdin(Stdio::piped());
                    Some(PipelineData::ByteStream(stream, metadata))
                }
            },
            PipelineData::Empty => {
                command.stdin(Stdio::inherit());
                None
            }
            value => {
                command.stdin(Stdio::piped());
                Some(value)
            }
        };

        // Log the command we're about to run in case it's useful for debugging purposes.
        log::trace!("run-external spawning: {command:?}");

        // Spawn the child process. On Unix, also put the child process to
        // foreground if we're in an interactive session.
        #[cfg(windows)]
        let mut child = ForegroundChild::spawn(command)?;
        #[cfg(unix)]
        let mut child = ForegroundChild::spawn(
            command,
            engine_state.is_interactive,
            &engine_state.pipeline_externals_state,
        )?;

        // If we need to copy data into the child process, do it now.
        if let Some(data) = data_to_copy_into_stdin {
            let stdin = child.as_mut().stdin.take().expect("stdin is piped");
            let engine_state = engine_state.clone();
            let stack = stack.clone();
            thread::Builder::new()
                .name("external stdin worker".into())
                .spawn(move || {
                    let _ = write_pipeline_data(engine_state, stack, data, stdin);
                })
                .err_span(call.head)?;
        }

        // Wrap the output into a `PipelineData::ByteStream`.
        let child = ChildProcess::new(
            child,
            merged_stream,
            matches!(stderr, OutDest::Pipe),
            call.head,
        )?;
        Ok(PipelineData::ByteStream(
            ByteStream::child(child, call.head),
            None,
        ))
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Run an external command",
                example: r#"run-external "echo" "-n" "hello""#,
                result: None,
            },
            Example {
                description: "Redirect stdout from an external command into the pipeline",
                example: r#"run-external "echo" "-n" "hello" | split chars"#,
                result: None,
            },
            Example {
                description: "Redirect stderr from an external command into the pipeline",
                example: r#"run-external "nu" "-c" "print -e hello" e>| split chars"#,
                result: None,
            },
        ]
    }
}

/// Removes surrounding quotes from a string. Doesn't remove quotes from raw
/// strings. Returns the original string if it doesn't have matching quotes.
fn remove_quotes(s: &str) -> &str {
    let quoted_by_double_quotes = s.len() >= 2 && s.starts_with('"') && s.ends_with('"');
    let quoted_by_single_quotes = s.len() >= 2 && s.starts_with('\'') && s.ends_with('\'');
    let quoted_by_backticks = s.len() >= 2 && s.starts_with('`') && s.ends_with('`');
    if quoted_by_double_quotes || quoted_by_single_quotes || quoted_by_backticks {
        &s[1..s.len() - 1]
    } else {
        s
    }
}

/// Evaluate all arguments from a call, performing expansions when necessary.
pub fn eval_arguments_from_call(
    engine_state: &EngineState,
    stack: &mut Stack,
    call: &Call,
) -> Result<Vec<Spanned<String>>, ShellError> {
    let ctrlc = &engine_state.ctrlc;
    let cwd = engine_state.cwd(Some(stack))?;
    let mut args: Vec<Spanned<String>> = vec![];
    for (expr, spread) in call.rest_iter(1) {
        if is_bare_string(expr) {
            // If `expr` is a bare string, perform tilde-expansion,
            // glob-expansion, and inner-quotes-removal, in that order.
            for arg in eval_argument(engine_state, stack, expr, spread)? {
                let tilde_expanded = expand_tilde(&arg);
                for glob_expanded in expand_glob(&tilde_expanded, &cwd, expr.span, ctrlc)? {
                    let inner_quotes_removed = remove_inner_quotes(&glob_expanded);
                    args.push(inner_quotes_removed.into_owned().into_spanned(expr.span));
                }
            }
        } else {
            for arg in eval_argument(engine_state, stack, expr, spread)? {
                args.push(arg.into_spanned(expr.span));
            }
        }
    }
    Ok(args)
}

/// Evaluates an expression, coercing the values to strings.
///
/// Note: The parser currently has a special hack that retains surrounding
/// quotes for string literals in `Expression`, so that we can decide whether
/// the expression is considered a bare string. The hack doesn't affect string
/// literals within lists or records. This function will remove the quotes
/// before evaluating the expression.
fn eval_argument(
    engine_state: &EngineState,
    stack: &mut Stack,
    expr: &Expression,
    spread: bool,
) -> Result<Vec<String>, ShellError> {
    // Remove quotes from string literals.
    let mut expr = expr.clone();
    if let Expr::String(s) = &expr.expr {
        expr.expr = Expr::String(remove_quotes(s).into());
    }

    let eval = get_eval_expression(engine_state);
    match eval(engine_state, stack, &expr)? {
        Value::List { vals, .. } => {
            if spread {
                vals.into_iter()
                    .map(|val| val.coerce_into_string())
                    .collect()
            } else {
                Err(ShellError::CannotPassListToExternal {
                    arg: String::from_utf8_lossy(engine_state.get_span_contents(expr.span)).into(),
                    span: expr.span,
                })
            }
        }
        value => {
            if spread {
                Err(ShellError::CannotSpreadAsList { span: expr.span })
            } else {
                Ok(vec![value.coerce_into_string()?])
            }
        }
    }
}

/// Returns whether an expression is considered a bare string.
///
/// Bare strings are defined as string literals that are either unquoted or
/// quoted by backticks. Raw strings or string interpolations don't count.
fn is_bare_string(expr: &Expression) -> bool {
    let Expr::String(s) = &expr.expr else {
        return false;
    };
    let quoted_by_double_quotes = s.len() >= 2 && s.starts_with('"') && s.ends_with('"');
    let quoted_by_single_quotes = s.len() >= 2 && s.starts_with('\'') && s.ends_with('\'');
    !quoted_by_double_quotes && !quoted_by_single_quotes
}

/// Performs tilde expansion on `arg`. Returns the original string if `arg`
/// doesn't start with tilde.
fn expand_tilde(arg: &str) -> String {
    nu_path::expand_tilde(arg).to_string_lossy().to_string()
}

/// Performs glob expansion on `arg`. If the expansion found no matches or the pattern
/// is not a valid glob, then this returns the original string as the expansion result.
///
/// Note: This matches the default behavior of Bash, but is known to be
/// error-prone. We might want to change this behavior in the future.
fn expand_glob(
    arg: &str,
    cwd: &Path,
    span: Span,
    interrupt: &Option<Arc<AtomicBool>>,
) -> Result<Vec<String>, ShellError> {
    const GLOB_CHARS: &[char] = &['*', '?', '['];

    // Don't expand something that doesn't include the GLOB_CHARS
    if !arg.contains(GLOB_CHARS) {
        return Ok(vec![arg.into()]);
    }

    // We must use `nu_engine::glob_from` here, in order to ensure we get paths from the correct
    // dir
    let glob = NuGlob::Expand(arg.to_owned()).into_spanned(span);
    let Ok((_prefix, paths)) = nu_engine::glob_from(&glob, cwd, span, None) else {
        // If an error occurred, return the original input
        return Ok(vec![arg.into()]);
    };

    // If the first component of the original `arg` string path was '.', that should be preserved
    let relative_to_dot = Path::new(arg).starts_with(".");

    let paths = paths
        // Skip over glob failures. These are usually just inaccessible paths.
        .flat_map(|path_result| match path_result {
            Ok(path) => Some(path),
            Err(err) => {
                // But internally log them just in case we need to debug this.
                log::warn!("Error in run_external::expand_glob(): {}", err);
                None
            }
        })
        // Make the paths relative to the cwd
        .map(|path| {
            path.strip_prefix(cwd)
                .map(|path| path.to_owned())
                .unwrap_or(path)
        })
        // Add './' to relative paths if the original pattern had it
        .map(|path| {
            if relative_to_dot && path.is_relative() {
                Path::new(".").join(path)
            } else {
                path
            }
        })
        // Convert the paths returned to UTF-8 strings.
        //
        // FIXME: this fails to return the correct results for non-UTF-8 paths, but we don't support
        // those in Nushell yet.
        .map(|path| path.to_string_lossy().into_owned())
        // Abandon if ctrl-c is pressed
        .map(|path| {
            if !nu_utils::ctrl_c::was_pressed(interrupt) {
                Ok(path)
            } else {
                Err(ShellError::InterruptedByUser { span: Some(span) })
            }
        })
        .collect::<Result<Vec<String>, ShellError>>()?;

    if !paths.is_empty() {
        Ok(paths)
    } else {
        // If we failed to match, return the original input
        Ok(vec![arg.into()])
    }
}

/// Transforms `--option="value"` into `--option=value`. `value` can be quoted
/// with double quotes, single quotes, or backticks. Only removes the outermost
/// pair of quotes after the equal sign.
fn remove_inner_quotes(arg: &str) -> Cow<'_, str> {
    // Check that `arg` is a long option.
    if !arg.starts_with("--") {
        return Cow::Borrowed(arg);
    }
    // Split `arg` on the first `=`.
    let Some((option, value)) = arg.split_once('=') else {
        return Cow::Borrowed(arg);
    };
    // Check that `option` doesn't contain quotes.
    if option.contains('"') || option.contains('\'') || option.contains('`') {
        return Cow::Borrowed(arg);
    }
    // Remove the outermost pair of quotes from `value`.
    let value = remove_quotes(value);
    Cow::Owned(format!("{option}={value}"))
}

/// Write `PipelineData` into `writer`. If `PipelineData` is not binary, it is
/// first rendered using the `table` command.
///
/// Note: Avoid using this function when piping data from an external command to
/// another external command, because it copies data unnecessarily. Instead,
/// extract the pipe from the `PipelineData::ByteStream` of the first command
/// and hand it to the second command directly.
fn write_pipeline_data(
    mut engine_state: EngineState,
    mut stack: Stack,
    data: PipelineData,
    mut writer: impl Write,
) -> Result<(), ShellError> {
    if let PipelineData::ByteStream(stream, ..) = data {
        stream.write_to(writer)?;
    } else if let PipelineData::Value(Value::Binary { val, .. }, ..) = data {
        writer.write_all(&val)?;
    } else {
        stack.start_capture();

        // Turn off color as we pass data through
        Arc::make_mut(&mut engine_state.config).use_ansi_coloring = false;

        // Invoke the `table` command.
        let output =
            crate::Table.run(&engine_state, &mut stack, &Call::new(Span::unknown()), data)?;

        // Write the output.
        for value in output {
            let bytes = value.coerce_into_binary()?;
            writer.write_all(&bytes)?;
        }
    }
    Ok(())
}

/// Returns a helpful error message given an invalid command name,
pub fn command_not_found(
    name: &str,
    span: Span,
    engine_state: &EngineState,
    stack: &mut Stack,
) -> ShellError {
    // Run the `command_not_found` hook if there is one.
    if let Some(hook) = &engine_state.config.hooks.command_not_found {
        let mut stack = stack.start_capture();
        // Set a special environment variable to avoid infinite loops when the
        // `command_not_found` hook triggers itself.
        let canary = "ENTERED_COMMAND_NOT_FOUND";
        if stack.has_env_var(engine_state, canary) {
            return ShellError::ExternalCommand {
                label: format!(
                    "Command {name} not found while running the `command_not_found` hook"
                ),
                help: "Make sure the `command_not_found` hook itself does not use unknown commands"
                    .into(),
                span,
            };
        }
        stack.add_env_var(canary.into(), Value::bool(true, Span::unknown()));

        let output = eval_hook(
            &mut engine_state.clone(),
            &mut stack,
            None,
            vec![("cmd_name".into(), Value::string(name, span))],
            hook,
            "command_not_found",
        );

        // Remove the special environment variable that we just set.
        stack.remove_env_var(engine_state, canary);

        match output {
            Ok(PipelineData::Value(Value::String { val, .. }, ..)) => {
                return ShellError::ExternalCommand {
                    label: format!("Command `{name}` not found"),
                    help: val,
                    span,
                };
            }
            Err(err) => {
                return err;
            }
            _ => {
                // The hook did not return a string, so ignore it.
            }
        }
    }

    // If the name is one of the removed commands, recommend a replacement.
    if let Some(replacement) = crate::removed_commands().get(&name.to_lowercase()) {
        return ShellError::RemovedCommand {
            removed: name.to_lowercase(),
            replacement: replacement.clone(),
            span,
        };
    }

    // The command might be from another module. Try to find it.
    if let Some(module) = engine_state.which_module_has_decl(name.as_bytes(), &[]) {
        let module = String::from_utf8_lossy(module);
        // Is the command already imported?
        let full_name = format!("{module} {name}");
        if engine_state.find_decl(full_name.as_bytes(), &[]).is_some() {
            return ShellError::ExternalCommand {
                label: format!("Command `{name}` not found"),
                help: format!("Did you mean `{full_name}`?"),
                span,
            };
        } else {
            return ShellError::ExternalCommand {
                label: format!("Command `{name}` not found"),
                help: format!("A command with that name exists in module `{module}`. Try importing it with `use`"),
                span,
            };
        }
    }

    // Try to match the name with the search terms of existing commands.
    let signatures = engine_state.get_signatures(false);
    if let Some(sig) = signatures.iter().find(|sig| {
        sig.search_terms
            .iter()
            .any(|term| term.to_folded_case() == name.to_folded_case())
    }) {
        return ShellError::ExternalCommand {
            label: format!("Command `{name}` not found"),
            help: format!("Did you mean `{}`?", sig.name),
            span,
        };
    }

    // Try a fuzzy search on the names of all existing commands.
    if let Some(cmd) = did_you_mean(signatures.iter().map(|sig| &sig.name), name) {
        // The user is invoking an external command with the same name as a
        // built-in command. Remind them of this.
        if cmd == name {
            return ShellError::ExternalCommand {
                label: format!("Command `{name}` not found"),
                help: "There is a built-in command with the same name".into(),
                span,
            };
        }
        return ShellError::ExternalCommand {
            label: format!("Command `{name}` not found"),
            help: format!("Did you mean `{cmd}`?"),
            span,
        };
    }

    // We found nothing useful. Give up and return a generic error message.
    ShellError::ExternalCommand {
        label: format!("Command `{name}` not found"),
        help: format!("`{name}` is neither a Nushell built-in or a known external command"),
        span,
    }
}

/// Searches for the absolute path of an executable by name. `.bat` and `.cmd`
/// files are recognized as executables on Windows.
///
/// This is a wrapper around `which::which_in()` except that, on Windows, it
/// also searches the current directory before any PATH entries.
///
/// Note: the `which.rs` crate always uses PATHEXT from the environment. As
/// such, changing PATHEXT within Nushell doesn't work without updating the
/// actual environment of the Nushell process.
pub fn which(name: &str, paths: &str, cwd: &Path) -> Option<PathBuf> {
    #[cfg(windows)]
    let paths = format!("{};{}", cwd.display(), paths);
    which::which_in(name, Some(paths), cwd).ok()
}

/// Returns true if `name` is a (somewhat useful) CMD internal command. The full
/// list can be found at https://ss64.com/nt/syntax-internal.html
fn is_cmd_internal_command(name: &str) -> bool {
    const COMMANDS: &[&str] = &[
        "ASSOC", "CLS", "ECHO", "FTYPE", "MKLINK", "PAUSE", "START", "VER", "VOL",
    ];
    COMMANDS.iter().any(|cmd| cmd.eq_ignore_ascii_case(name))
}

/// Returns true if a string contains CMD special characters.
#[cfg(windows)]
fn has_cmd_special_character(s: &str) -> bool {
    const SPECIAL_CHARS: &[char] = &['<', '>', '&', '|', '^'];
    SPECIAL_CHARS.iter().any(|c| s.contains(*c))
}

/// Escape an argument for CMD internal commands. The result can be safely passed to `raw_arg()`.
#[cfg(windows)]
fn escape_cmd_argument(arg: &Spanned<String>) -> Result<Cow<'_, str>, ShellError> {
    let Spanned { item: arg, span } = arg;
    if arg.contains(['\r', '\n', '%']) {
        // \r and \n trunacte the rest of the arguments and % can expand environment variables
        Err(ShellError::ExternalCommand {
            label:
                "Arguments to CMD internal commands cannot contain new lines or percent signs '%'"
                    .into(),
            help: "some characters currently cannot be securely escaped".into(),
            span: *span,
        })
    } else if arg.contains('"') {
        // If `arg` is already quoted by double quotes, confirm there's no
        // embedded double quotes, then leave it as is.
        if arg.chars().filter(|c| *c == '"').count() == 2
            && arg.starts_with('"')
            && arg.ends_with('"')
        {
            Ok(Cow::Borrowed(arg))
        } else {
            Err(ShellError::ExternalCommand {
                label: "Arguments to CMD internal commands cannot contain embedded double quotes"
                    .into(),
                help: "this case currently cannot be securely handled".into(),
                span: *span,
            })
        }
    } else if arg.contains(' ') || has_cmd_special_character(arg) {
        // If `arg` contains space or special characters, quote the entire argument by double quotes.
        Ok(Cow::Owned(format!("\"{arg}\"")))
    } else {
        // FIXME?: what if `arg.is_empty()`?
        Ok(Cow::Borrowed(arg))
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use nu_protocol::ast::ListItem;
    use nu_test_support::{fs::Stub, playground::Playground};

    #[test]
    fn test_remove_quotes() {
        assert_eq!(remove_quotes(r#""#), r#""#);
        assert_eq!(remove_quotes(r#"'"#), r#"'"#);
        assert_eq!(remove_quotes(r#"''"#), r#""#);
        assert_eq!(remove_quotes(r#""foo""#), r#"foo"#);
        assert_eq!(remove_quotes(r#"`foo '"' bar`"#), r#"foo '"' bar"#);
        assert_eq!(remove_quotes(r#"'foo' bar"#), r#"'foo' bar"#);
        assert_eq!(remove_quotes(r#"r#'foo'#"#), r#"r#'foo'#"#);
    }

    #[test]
    fn test_eval_argument() {
        fn expression(expr: Expr) -> Expression {
            Expression {
                expr,
                span: Span::unknown(),
                ty: Type::Any,
                custom_completion: None,
            }
        }

        fn eval(expr: Expr, spread: bool) -> Result<Vec<String>, ShellError> {
            let engine_state = EngineState::new();
            let mut stack = Stack::new();
            eval_argument(&engine_state, &mut stack, &expression(expr), spread)
        }

        let actual = eval(Expr::String("".into()), false).unwrap();
        let expected = &[""];
        assert_eq!(actual, expected);

        let actual = eval(Expr::String("'foo'".into()), false).unwrap();
        let expected = &["foo"];
        assert_eq!(actual, expected);

        let actual = eval(Expr::RawString("'foo'".into()), false).unwrap();
        let expected = &["'foo'"];
        assert_eq!(actual, expected);

        let actual = eval(Expr::List(vec![]), true).unwrap();
        let expected: &[&str] = &[];
        assert_eq!(actual, expected);

        let actual = eval(
            Expr::List(vec![
                ListItem::Item(expression(Expr::String("'foo'".into()))),
                ListItem::Item(expression(Expr::String("bar".into()))),
            ]),
            true,
        )
        .unwrap();
        let expected = &["'foo'", "bar"];
        assert_eq!(actual, expected);

        eval(Expr::String("".into()), true).unwrap_err();
        eval(Expr::List(vec![]), false).unwrap_err();
    }

    #[test]
    fn test_expand_glob() {
        Playground::setup("test_expand_glob", |dirs, play| {
            play.with_files(&[Stub::EmptyFile("a.txt"), Stub::EmptyFile("b.txt")]);

            let cwd = dirs.test();

            let actual = expand_glob("*.txt", cwd, Span::unknown(), &None).unwrap();
            let expected = &["a.txt", "b.txt"];
            assert_eq!(actual, expected);

            let actual = expand_glob("./*.txt", cwd, Span::unknown(), &None).unwrap();
            let expected = vec![
                Path::new(".").join("a.txt").to_string_lossy().into_owned(),
                Path::new(".").join("b.txt").to_string_lossy().into_owned(),
            ];
            assert_eq!(actual, expected);

            let actual = expand_glob("'*.txt'", cwd, Span::unknown(), &None).unwrap();
            let expected = &["'*.txt'"];
            assert_eq!(actual, expected);

            let actual = expand_glob(".", cwd, Span::unknown(), &None).unwrap();
            let expected = &["."];
            assert_eq!(actual, expected);

            let actual = expand_glob("./a.txt", cwd, Span::unknown(), &None).unwrap();
            let expected = &["./a.txt"];
            assert_eq!(actual, expected);

            let actual = expand_glob("[*.txt", cwd, Span::unknown(), &None).unwrap();
            let expected = &["[*.txt"];
            assert_eq!(actual, expected);
        })
    }

    #[test]
    fn test_remove_inner_quotes() {
        let actual = remove_inner_quotes(r#"--option=value"#);
        let expected = r#"--option=value"#;
        assert_eq!(actual, expected);

        let actual = remove_inner_quotes(r#"--option="value""#);
        let expected = r#"--option=value"#;
        assert_eq!(actual, expected);

        let actual = remove_inner_quotes(r#"--option='value'"#);
        let expected = r#"--option=value"#;
        assert_eq!(actual, expected);

        let actual = remove_inner_quotes(r#"--option "value""#);
        let expected = r#"--option "value""#;
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_write_pipeline_data() {
        let engine_state = EngineState::new();
        let stack = Stack::new();

        let mut buf = vec![];
        let input = PipelineData::Empty;
        write_pipeline_data(engine_state.clone(), stack.clone(), input, &mut buf).unwrap();
        assert_eq!(buf, b"");

        let mut buf = vec![];
        let input = PipelineData::Value(Value::string("foo", Span::unknown()), None);
        write_pipeline_data(engine_state.clone(), stack.clone(), input, &mut buf).unwrap();
        assert_eq!(buf, b"foo");

        let mut buf = vec![];
        let input = PipelineData::Value(Value::binary(b"foo", Span::unknown()), None);
        write_pipeline_data(engine_state.clone(), stack.clone(), input, &mut buf).unwrap();
        assert_eq!(buf, b"foo");

        let mut buf = vec![];
        let input = PipelineData::ByteStream(
            ByteStream::read(
                b"foo".as_slice(),
                Span::unknown(),
                None,
                ByteStreamType::Unknown,
            ),
            None,
        );
        write_pipeline_data(engine_state.clone(), stack.clone(), input, &mut buf).unwrap();
        assert_eq!(buf, b"foo");
    }
}