@@ -29,7 +29,7 @@ extern mod rustc(vers = "0.7-pre");
29
29
extern mod syntax(vers = "0.7-pre");
30
30
31
31
use core::*;
32
- use core::io::WriterUtil;
32
+ use core::io::{ReaderUtil, WriterUtil} ;
33
33
use rustc::driver::{driver, session};
34
34
use syntax::{ast, diagnostic};
35
35
use syntax::ast_util::*;
@@ -241,23 +241,29 @@ fn compile_crate(src_filename: ~str, binary: ~str) -> Option<bool> {
241
241
242
242
/// Tries to get a line from rl after outputting a prompt. Returns
243
243
/// None if no input was read (e.g. EOF was reached).
244
- fn get_line(prompt: ~str) -> Option<~str> {
245
- let result = unsafe { rl::read(prompt) };
244
+ fn get_line(use_rl: bool, prompt: ~str) -> Option<~str> {
245
+ if use_rl {
246
+ let result = unsafe { rl::read(prompt) };
246
247
247
- if result.is_none() {
248
- return None;
248
+ match result {
249
+ None => None,
250
+ Some(line) => {
251
+ unsafe { rl::add_history(line) };
252
+ Some(line)
253
+ }
254
+ }
255
+ } else {
256
+ if io::stdin().eof() {
257
+ None
258
+ } else {
259
+ Some(io::stdin().read_line())
260
+ }
249
261
}
250
-
251
- let line = result.get();
252
-
253
- unsafe { rl::add_history(line) };
254
-
255
- return Some(line);
256
262
}
257
263
258
264
/// Run a command, e.g. :clear, :exit, etc.
259
265
fn run_cmd(repl: &mut Repl, _in: @io::Reader, _out: @io::Writer,
260
- cmd: ~str, args: ~[~str]) -> CmdAction {
266
+ cmd: ~str, args: ~[~str], use_rl: bool ) -> CmdAction {
261
267
let mut action = action_none;
262
268
match cmd {
263
269
~"exit" => repl.running = false,
@@ -313,7 +319,7 @@ fn run_cmd(repl: &mut Repl, _in: @io::Reader, _out: @io::Writer,
313
319
let mut multiline_cmd = ~"";
314
320
let mut end_multiline = false;
315
321
while (!end_multiline) {
316
- match get_line(~"rusti| ") {
322
+ match get_line(use_rl, ~"rusti| ") {
317
323
None => fail!(~"unterminated multiline command :{ .. :}"),
318
324
Some(line) => {
319
325
if str::trim(line) == ~":}" {
@@ -333,7 +339,8 @@ fn run_cmd(repl: &mut Repl, _in: @io::Reader, _out: @io::Writer,
333
339
334
340
/// Executes a line of input, which may either be rust code or a
335
341
/// :command. Returns a new Repl if it has changed.
336
- fn run_line(repl: &mut Repl, in: @io::Reader, out: @io::Writer, line: ~str)
342
+ fn run_line(repl: &mut Repl, in: @io::Reader, out: @io::Writer, line: ~str,
343
+ use_rl: bool)
337
344
-> Option<Repl> {
338
345
if line.starts_with(~":") {
339
346
let full = line.substr(1, line.len() - 1);
@@ -349,11 +356,11 @@ fn run_line(repl: &mut Repl, in: @io::Reader, out: @io::Writer, line: ~str)
349
356
vec::slice(split, 1, len).to_vec()
350
357
} else { ~[] };
351
358
352
- match run_cmd(repl, in, out, cmd, args) {
359
+ match run_cmd(repl, in, out, cmd, args, use_rl ) {
353
360
action_none => { }
354
361
action_run_line(multiline_cmd) => {
355
362
if !multiline_cmd.is_empty() {
356
- return run_line(repl, in, out, multiline_cmd);
363
+ return run_line(repl, in, out, multiline_cmd, use_rl );
357
364
}
358
365
}
359
366
}
@@ -386,30 +393,37 @@ pub fn main() {
386
393
stmts: ~""
387
394
};
388
395
389
- io::println("WARNING: The Rust REPL is experimental and may be");
390
- io::println("unstable. If you encounter problems, please use the");
391
- io::println("compiler instead.");
392
-
393
- unsafe {
394
- do rl::complete |line, suggest| {
395
- if line.starts_with(":") {
396
- suggest(~":clear");
397
- suggest(~":exit");
398
- suggest(~":help");
399
- suggest(~":load");
396
+ let istty = unsafe { libc::isatty(libc::STDIN_FILENO as i32) } != 0;
397
+
398
+ // only print this stuff if the user is actually typing into rusti
399
+ if istty {
400
+ io::println("WARNING: The Rust REPL is experimental and may be");
401
+ io::println("unstable. If you encounter problems, please use the");
402
+ io::println("compiler instead.");
403
+
404
+ unsafe {
405
+ do rl::complete |line, suggest| {
406
+ if line.starts_with(":") {
407
+ suggest(~":clear");
408
+ suggest(~":exit");
409
+ suggest(~":help");
410
+ suggest(~":load");
411
+ }
400
412
}
401
413
}
402
414
}
403
415
404
416
while repl.running {
405
- match get_line(repl.prompt) {
417
+ match get_line(istty, repl.prompt) {
406
418
None => break,
407
419
Some(line) => {
408
420
if line.is_empty() {
409
- io::println(~"()");
421
+ if istty {
422
+ io::println(~"()");
423
+ }
410
424
loop;
411
425
}
412
- match run_line(&mut repl, in, out, line) {
426
+ match run_line(&mut repl, in, out, line, istty ) {
413
427
Some(new_repl) => repl = new_repl,
414
428
None => { }
415
429
}
0 commit comments