c-shell 0.3.1

An interactive shell for C: type an expression, a statement or a function and see what your real compiler does with it
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
//! c-shell — an interactive prompt for C.
//!
//! Type an expression to see its value, a statement to run it, a function to
//! define it. Everything is compiled by a real C compiler, because the point
//! is to find out what *your* compiler does, not what an interpreter would.

mod codegen;
mod editor;
mod errmap;
mod eval;
mod i18n;
mod lex;
mod magic;
mod proc;
mod session;
mod std_index;
mod toolchain;
mod ui;
mod web;

use anyhow::{Context, Result};
use clap::{
    Arg, ArgAction, CommandFactory, FromArgMatches, Parser,
    error::{ContextKind, ErrorKind},
};
use reedline::{
    EditCommand, Emacs, IdeMenu, KeyCode, KeyModifiers, MenuBuilder, Reedline, ReedlineEvent,
    ReedlineMenu, Signal, default_emacs_keybindings,
};
use std::io::{BufRead, IsTerminal};
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use crate::codegen::Slot;
use crate::eval::{Eval, Evaluator};
use crate::i18n::Language;
use crate::session::Session;
use crate::ui::Ui;

#[derive(Parser)]
#[command(
    name = "c-shell",
    version,
    about = "An interactive prompt for C",
    disable_help_flag = true,
    disable_version_flag = true
)]
struct Args {
    /// C compiler to use (default: $CC, then the first one found on PATH)
    #[arg(long, value_name = "PATH")]
    cc: Option<String>,

    /// Language standard, e.g. c11, c17, c23 (default: whatever the
    /// compiler itself defaults to, e.g. gnu23 for current gcc)
    #[arg(long, value_name = "STD")]
    std: Option<String>,

    /// Evaluate CODE and exit (repeatable). Values print bare, diagnostics
    /// go to stderr, and a failing input sets a nonzero exit code
    #[arg(
        short = 'e',
        long = "eval",
        value_name = "CODE",
        conflicts_with = "script"
    )]
    eval: Vec<String>,

    /// Run inputs from FILE and exit; nonzero exit code on any failure
    #[arg(long, value_name = "FILE")]
    script: Option<std::path::PathBuf>,

    /// Suppress the banner and hints
    #[arg(short = 'q', long)]
    quiet: bool,

    /// Seconds each compilation or program run may take
    #[arg(long, value_name = "SECS", default_value_t = 10)]
    timeout: u64,

    /// Disable colored output
    #[arg(long)]
    no_color: bool,

    /// UI language; overrides automatic locale detection
    #[arg(long, value_enum, value_name = "LANG")]
    lang: Option<Language>,

    /// Start a browser terminal available only on this machine
    #[arg(long, conflicts_with_all = ["eval", "script"])]
    web: bool,

    /// Do not open the browser automatically
    #[arg(long, requires = "web")]
    no_open: bool,
}

/// How results are rendered.
#[derive(Clone, Copy, PartialEq)]
pub(crate) enum Style {
    /// `Out[n]:` labels, notes, everything on stdout — a transcript.
    Repl,
    /// Bare values on stdout, diagnostics on stderr, no notes — for `-e`,
    /// where the output is probably feeding a pipe.
    Bare,
}

impl Style {
    pub(crate) fn diagnostic(self, message: &str) {
        if self == Self::Bare {
            eprintln!("{message}");
        } else {
            println!("{message}");
        }
    }
}

fn command_for(language: Language) -> clap::Command {
    let tr = |key| i18n::text_for(language, key);
    let command = Args::command()
        .arg(
            Arg::new("help")
                .short('h')
                .long("help")
                .action(ArgAction::Help)
                .help(tr("arg-help")),
        )
        .arg(
            Arg::new("version")
                .short('V')
                .long("version")
                .action(ArgAction::Version)
                .help(tr("arg-version")),
        )
        .about(tr("app-about"))
        .mut_arg("cc", |arg| arg.help(tr("arg-cc")))
        .mut_arg("std", |arg| arg.help(tr("arg-std")))
        .mut_arg("eval", |arg| arg.help(tr("arg-eval")))
        .mut_arg("script", |arg| arg.help(tr("arg-script")))
        .mut_arg("quiet", |arg| arg.help(tr("arg-quiet")))
        .mut_arg("timeout", |arg| {
            arg.help(tr("arg-timeout")).hide_default_value(true)
        })
        .mut_arg("no_color", |arg| arg.help(tr("arg-no-color")))
        .mut_arg("lang", |arg| {
            arg.help(tr("arg-lang")).hide_possible_values(true)
        })
        .mut_arg("web", |arg| arg.help(tr("arg-web")))
        .mut_arg("no_open", |arg| arg.help(tr("arg-no-open")));
    if language.is_chinese() {
        let usage = tr("cli-usage");
        let options = tr("cli-options");
        command.help_template(format!(
            "{{before-help}}{{about-with-newline}}\n{usage}{{usage}}\n\n{options}\n{{options}}{{after-help}}"
        ))
    } else {
        command
    }
}

fn cli_error_context(error: &clap::Error, kind: ContextKind) -> Option<String> {
    error.get(kind).map(ToString::to_string)
}

fn exit_with_chinese_cli_error(error: clap::Error, command: &mut clap::Command) -> ! {
    let argument = cli_error_context(&error, ContextKind::InvalidArg)
        .unwrap_or_else(|| i18n::text("cli-argument"));
    let value = cli_error_context(&error, ContextKind::InvalidValue).unwrap_or_default();
    let prior = cli_error_context(&error, ContextKind::PriorArg).unwrap_or_default();
    let message = match error.kind() {
        ErrorKind::InvalidValue | ErrorKind::ValueValidation => i18n::text_with(
            "cli-invalid-value",
            &[("argument", argument), ("value", value)],
        ),
        ErrorKind::UnknownArgument | ErrorKind::InvalidSubcommand => {
            i18n::text_with("cli-unknown-argument", &[("argument", argument)])
        }
        ErrorKind::NoEquals => i18n::text_with("cli-equals-required", &[("argument", argument)]),
        ErrorKind::TooManyValues | ErrorKind::WrongNumberOfValues => {
            i18n::text_with("cli-wrong-value-count", &[("argument", argument)])
        }
        ErrorKind::TooFewValues | ErrorKind::MissingRequiredArgument => {
            i18n::text_with("cli-missing-value", &[("argument", argument)])
        }
        ErrorKind::ArgumentConflict => i18n::text_with(
            "cli-argument-conflict",
            &[("argument", argument), ("prior", prior)],
        ),
        ErrorKind::InvalidUtf8 => i18n::text("cli-invalid-utf8"),
        _ => i18n::text("cli-invalid-arguments"),
    };
    eprintln!("{}{message}", i18n::text("cli-error"));

    if let Some(values) = cli_error_context(&error, ContextKind::ValidValue)
        && !values.is_empty()
    {
        eprintln!(
            "{}",
            i18n::text_with("cli-valid-values", &[("values", values)])
        );
    }
    if let Some(suggestion) = cli_error_context(&error, ContextKind::SuggestedArg)
        && !suggestion.is_empty()
    {
        eprintln!(
            "{}",
            i18n::text_with("cli-suggestion", &[("suggestion", suggestion)])
        );
    }
    let usage = command.render_usage().to_string();
    let usage = usage.strip_prefix("Usage: ").unwrap_or(&usage);
    eprintln!("\n{}{usage}", i18n::text("cli-usage"));
    eprintln!("\n{}", i18n::text("cli-more-info"));
    std::process::exit(error.exit_code());
}

fn parse_args(language: Language) -> Args {
    let mut command = command_for(language);
    let matches = match command.clone().try_get_matches() {
        Ok(matches) => matches,
        Err(error) if language.is_chinese() && error.use_stderr() => {
            exit_with_chinese_cli_error(error, &mut command)
        }
        Err(error) => error.exit(),
    };
    Args::from_arg_matches(&matches).expect("clap arguments match Args")
}

fn main() -> Result<std::process::ExitCode> {
    let detected_language = i18n::detect();
    let language = i18n::requested_from_args(std::env::args_os()).unwrap_or(detected_language);
    i18n::set(language);
    let args = parse_args(language);
    let language = args.lang.unwrap_or(detected_language);
    i18n::set(language);

    if args.web {
        let mut child_args = Vec::<std::ffi::OsString>::new();
        if let Some(cc) = &args.cc {
            child_args.extend(["--cc".into(), cc.into()]);
        }
        if let Some(standard) = &args.std {
            child_args.extend(["--std".into(), standard.into()]);
        }
        child_args.extend(["--timeout".into(), args.timeout.to_string().into()]);
        child_args.extend(["--lang".into(), language.code().into()]);
        if args.quiet {
            child_args.push("--quiet".into());
        }
        if args.no_color {
            child_args.push("--no-color".into());
        }
        web::serve(web::Config {
            child_args,
            language,
            open_browser: !args.no_open,
            quiet: args.quiet,
        })?;
        return Ok(std::process::ExitCode::SUCCESS);
    }

    // Auto-detect: no colors when redirected, explicitly opted out, or on a
    // terminal that declared itself unable (TERM=dumb).
    let stdout_is_terminal = std::io::stdout().is_terminal();
    let terminal_is_usable = std::env::var("TERM").map_or(true, |t| t != "dumb");
    let color = !args.no_color
        && std::env::var_os("NO_COLOR").is_none()
        && stdout_is_terminal
        && terminal_is_usable;
    // Hyperlinks are independent of color: --no-color/NO_COLOR disable
    // styling, not navigation. Unknown OSC sequences are safely ignored, and
    // the URL remains the visible label.
    let ui = Ui {
        color,
        hyperlinks: stdout_is_terminal && terminal_is_usable,
        language,
    };

    let tc = toolchain::Toolchain::detect(args.cc.as_deref(), args.std.as_deref())?;
    let mut ev = Evaluator::new(tc, Duration::from_secs(args.timeout))?;
    // At a real terminal, forward only the newest input's output as it arrives
    // so prompts before scanf are visible. Batch transcripts remain buffered.
    let stdin_is_terminal = std::io::stdin().is_terminal();
    ev.set_stream_output(stdin_is_terminal);
    // Piped REPL mode consumes stdin as C source. A TTY, `-e`, or a script
    // leaves stdin available as the evaluated program's input.
    ev.set_program_stdin(stdin_is_terminal || !args.eval.is_empty() || args.script.is_some());
    let mut session = Session::default();

    // ---- non-interactive modes -------------------------------------------

    if !args.eval.is_empty() {
        let mut ok = true;
        for code in &args.eval {
            let (input_ok, quit, _) = submit(code, &mut session, &mut ev, &ui, Style::Bare, false)?;
            ok &= input_ok;
            if quit {
                break;
            }
        }
        return Ok(std::process::ExitCode::from(u8::from(!ok)));
    }

    if let Some(path) = &args.script {
        let file = std::fs::File::open(path).with_context(|| {
            i18n::text_with_for(
                language,
                "script-open-error",
                &[("path", path.display().to_string())],
            )
        })?;
        let ok = run_batch(std::io::BufReader::new(file), &mut session, &mut ev, &ui)?;
        return Ok(std::process::ExitCode::from(u8::from(!ok)));
    }

    if !std::io::stdin().is_terminal() {
        // Piped input: a transcript without prompts or banner. Tolerant exit
        // code, like the interactive REPL it substitutes for.
        let stdin = std::io::stdin();
        run_batch(stdin.lock(), &mut session, &mut ev, &ui)?;
        return Ok(std::process::ExitCode::SUCCESS);
    }

    // ---- interactive REPL ------------------------------------------------

    if !args.quiet && std::io::stdout().is_terminal() {
        print!("{}", ui.banner());
        println!(
            "{} {}  ·  {}",
            ui.bold("c-shell"),
            env!("CARGO_PKG_VERSION"),
            ev.tc.describe()
        );
        println!("{}", ui.dim(&ui.text("startup-hint")));
        if std::env::var_os("C_SHELL_WEB").is_none() {
            println!("{}", ui.dim(&ui.text("web-launch-hint")));
        }
    }

    // Completion vocabulary, refreshed after every input and shared with the
    // completer.
    let idents = Arc::new(Mutex::new(Vec::new()));

    // Shared editor state: the highlighter stores the current buffer and cursor
    // on every repaint, and the edit mode reads them to calculate indentation.
    let edit_state = Arc::new(Mutex::new(editor::EditState::default()));
    let completion_active = Arc::new(AtomicBool::new(false));

    // Tab opens the IPython-style dropdown, then steps through it. reedline
    // auto-saves submitted lines to an in-memory history (capacity 1000, no
    // file), and the validator drives multi-line continuation.
    let mut keybindings = default_emacs_keybindings();
    keybindings.add_binding(
        KeyModifiers::NONE,
        KeyCode::Tab,
        ReedlineEvent::UntilFound(vec![
            ReedlineEvent::Menu("completion_menu".to_string()),
            ReedlineEvent::MenuNext,
        ]),
    );
    let edit_mode = editor::CEditMode::new(
        Emacs::new(keybindings),
        Arc::clone(&edit_state),
        Arc::clone(&completion_active),
    );
    let menu = editor::TrackingMenu::new(
        IdeMenu::default().with_name("completion_menu"),
        completion_active,
    );
    let mut rl = Reedline::create()
        .with_completer(Box::new(editor::CCompleter::new(Arc::clone(&idents))))
        .with_highlighter(Box::new(editor::CHighlighter::new(
            color,
            Arc::clone(&edit_state),
        )))
        .with_validator(Box::new(editor::CValidator))
        .with_menu(ReedlineMenu::EngineCompleter(Box::new(menu)))
        .with_edit_mode(Box::new(edit_mode))
        .with_history(Box::new(reedline::FileBackedHistory::new(1000)?))
        .with_ansi_colors(color);

    let mut edit_buffer: Option<String> = None;
    loop {
        let n = session.counter + 1;
        let prompt = editor::CPrompt { n };
        // `%edit` preloads the buffer: reedline clears it on submit, so the
        // insert lands in an empty buffer that read_line then edits in place.
        if let Some(initial) = edit_buffer.take() {
            rl.run_edit_commands(&[EditCommand::InsertString(initial)]);
        }
        let line = match rl.read_line(&prompt)? {
            Signal::Success(l) => l,
            Signal::CtrlC => continue,
            Signal::CtrlD => break,
            _ => continue,
        };
        let raw = line.trim();
        if raw.is_empty() {
            continue;
        }
        let (_, quit, next_edit) = submit(raw, &mut session, &mut ev, &ui, Style::Repl, true)?;
        // `%edit` itself consumes no input number. Its selected text becomes
        // the editable buffer at the same prompt on the next read_line call.
        edit_buffer = next_edit;
        *idents.lock().expect("ident vocabulary") = session.identifiers();
        if quit {
            break;
        }
    }

    println!("{}", ui.dim(&ui.text("bye")));
    Ok(std::process::ExitCode::SUCCESS)
}

/// Feed complete inputs from a reader, accumulating lines until the input is
/// syntactically finished — a function definition in a script spans many
/// lines. Returns whether every input succeeded.
fn run_batch<R: BufRead>(
    reader: R,
    session: &mut Session,
    ev: &mut Evaluator,
    ui: &Ui,
) -> Result<bool> {
    let mut all_ok = true;
    let mut pending = String::new();
    for line in reader.lines() {
        let line = line?;

        // A balanced `if` needs one physical line of lookahead: `else` and
        // intervening comment-only lines still belong to it. Any other line
        // confirms the if and starts a fresh REPL input.
        if !pending.is_empty() && editor::can_accept_else(&pending) {
            let comment = !line.trim().is_empty() && lex::is_blank(&line);
            if !editor::starts_with_else(&line) && !comment {
                let (ok, quit, _) = submit_batch_pending(&mut pending, session, ev, ui)?;
                all_ok &= ok;
                if quit {
                    return Ok(all_ok);
                }
            }
        }

        if pending.is_empty() && line.trim().is_empty() {
            continue;
        }
        if !pending.is_empty() {
            pending.push('\n');
        }
        pending.push_str(&line);
        if editor::is_structurally_incomplete(&pending) || editor::can_accept_else(&pending) {
            continue;
        }
        let (ok, quit, _) = submit_batch_pending(&mut pending, session, ev, ui)?;
        all_ok &= ok;
        if quit {
            return Ok(all_ok);
        }
    }
    // An unterminated or lookahead-delayed tail still gets evaluated: a
    // truncated construct earns the compiler's honest diagnostic.
    if !pending.trim().is_empty() {
        let (ok, _, _) = submit_batch_pending(&mut pending, session, ev, ui)?;
        all_ok &= ok;
    }
    Ok(all_ok)
}

fn submit_batch_pending(
    pending: &mut String,
    session: &mut Session,
    ev: &mut Evaluator,
    ui: &Ui,
) -> Result<(bool, bool, Option<String>)> {
    let input = std::mem::take(pending);
    submit(input.trim(), session, ev, ui, Style::Repl, false)
}

/// Run one complete input: magic command or C code. Returns (succeeded,
/// quit-requested, next interactive edit buffer).
fn submit(
    raw: &str,
    session: &mut Session,
    ev: &mut Evaluator,
    ui: &Ui,
    style: Style,
    interactive_edit: bool,
) -> Result<(bool, bool, Option<String>)> {
    let raw = raw.trim();
    // Comment-only input is legal to type but has nothing to evaluate.
    if raw.is_empty() || lex::is_blank(raw) {
        return Ok((true, false, None));
    }
    if raw.starts_with('%') {
        return match magic::handle(raw, session, ev, ui, style)? {
            magic::Action::Quit => Ok((true, true, None)),
            magic::Action::Continue(ok) => Ok((ok, false, None)),
            magic::Action::Prefill(input) if interactive_edit => Ok((true, false, Some(input))),
            magic::Action::Prefill(_) => {
                let message = ui.err(&ui.text("edit-interactive-only"));
                if style == Style::Bare {
                    eprintln!("{message}");
                } else {
                    println!("{message}");
                }
                Ok((false, false, None))
            }
        };
    }

    let bare = style == Style::Bare;
    let n = session.counter + 1;
    session.counter = n;
    session.remember_input(n, raw);

    let external_warning = warn_external_replay(raw, session, ui, style);

    match ev.eval(session, raw)? {
        Eval::CompileError(diag) => {
            let msg = ui.err(diag.trim_end());
            if bare {
                eprintln!("{msg}");
            } else {
                println!("{msg}");
            }
            Ok((false, false, None))
        }
        Eval::Done(o) => Ok((
            finish_input(raw, o, session, ui, style, external_warning),
            false,
            None,
        )),
    }
}

/// Render and commit a C input identically for ordinary evaluation and %time.
pub(crate) fn finish_input(
    raw: &str,
    o: crate::eval::Outcome,
    session: &mut Session,
    ui: &Ui,
    style: Style,
    external_warning: bool,
) -> bool {
    let n = session.counter;
    let bare = style == Style::Bare;

    // Live output has already been forwarded by Evaluator. If the C
    // program left a partial line, visibly mark the newline c-shell
    // inserts to protect the following warning, value or prompt.
    if o.streamed_output_needs_newline {
        println!("{}", ui.inserted_newline_marker());
    }
    let note = |s: &str| {
        if !bare {
            println!("{}", ui.dim(s));
        }
    };
    if o.rewritten.is_some() {
        note(&ui.text("note-missing-semicolon"));
    }
    if o.unprintable {
        let msg = ui.text("unprintable-value");
        if bare {
            eprintln!("{msg}");
        } else {
            note(&msg);
        }
    }
    if !o.warnings.trim().is_empty() {
        let msg = ui.warn(o.warnings.trim_end());
        if bare {
            eprintln!("{msg}");
        } else {
            println!("{msg}");
        }
    }
    print!("{}", o.output);
    // Programs rarely end their output with a newline, and without
    // this the next prompt would be glued onto the last line.
    if !o.output.is_empty() && !o.output.ends_with('\n') {
        println!();
    }
    if !o.errors.is_empty() {
        if bare {
            eprint!("{}", o.errors);
        } else {
            print!("{}", ui.err(&o.errors));
        }
    }
    if let Some(v) = &o.value {
        if bare {
            println!("{v}");
        } else {
            println!("{}{}", ui.out_label(n), v);
        }
    }
    let committed = o.rewritten.clone().unwrap_or_else(|| raw.to_string());
    match &o.abnormal {
        // A crashing input is not kept: every later evaluation
        // replays the whole session, so committing it would make
        // the prompt permanently unusable.
        Some(msg) => {
            if bare {
                eprintln!("{msg}");
            } else {
                println!("{}", ui.err(msg));
            }
            note(&ui.text("note-input-not-kept"));
        }
        None => {
            if let Some(index) = o.file_replacement {
                session.replace_file(index, &committed);
                session.attach_stdin_events(o.stdin_events.clone());
            } else if o.scoped_rebind {
                session.commit_scoped(&committed);
                session.attach_stdin_events(o.stdin_events.clone());
            } else {
                session.commit_with_stdin(&committed, o.slot, o.stdin_events.clone());
            }
            if external_warning {
                session.mark_external_replay_warning_shown();
            }
        }
    }
    if o.abnormal.is_none() {
        if o.file_replacement.is_some() {
            note(&ui.text("note-replaced-file"));
        } else if o.slot == Slot::FileScope {
            note(&ui.text("note-added-file"));
        }
        if o.scoped_rebind {
            note(&ui.text("note-shadowed"));
        }
        if !o.stdin_events.is_empty() {
            let message = ui.text_with(
                "note-stdin-captured",
                &[("count", o.stdin_events.len().to_string())],
            );
            note(&message);
        }
    }
    o.abnormal.is_none()
}

/// Emit the once-per-session warning before an input can affect external state.
pub(crate) fn warn_external_replay(raw: &str, session: &Session, ui: &Ui, style: Style) -> bool {
    let bare = style == Style::Bare;
    let external_calls = lex::external_replay_calls(raw)
        .into_iter()
        .filter(|name| name != "scanf")
        .collect::<Vec<_>>();
    let external_warning = !external_calls.is_empty() && !session.external_replay_warning_shown();
    if external_warning {
        let calls = external_calls
            .iter()
            .map(|name| format!("{name}()"))
            .collect::<Vec<_>>()
            .join(", ");
        let warning = ui.text_with("external-side-effect-warning", &[("calls", calls)]);
        let msg = ui.warn(&warning);
        if bare {
            eprintln!("{msg}");
        } else {
            println!("{msg}");
        }
    }
    external_warning
}