author | wenzelm |
Wed, 09 Mar 2016 20:44:02 +0100 | |
changeset 62577 | 7e2aa1d67dd8 |
parent 62562 | 905a5db3932d |
child 62586 | a522a5692832 |
permissions | -rw-r--r-- |
62559 | 1 |
/* Title: Pure/Tools/ml_console.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Run Isabelle process with raw ML console and line editor. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
62562
905a5db3932d
back to external line editor, due to problems of JLine with multithreading of in vs. out;
wenzelm
parents:
62561
diff
changeset
|
10 |
import java.io.{IOException, BufferedReader, InputStreamReader} |
62559 | 11 |
|
12 |
||
13 |
object ML_Console |
|
14 |
{ |
|
15 |
/* command line entry point */ |
|
16 |
||
17 |
def main(args: Array[String]) |
|
18 |
{ |
|
19 |
Command_Line.tool { |
|
20 |
var dirs: List[Path] = Nil |
|
21 |
var logic = Isabelle_System.getenv("ISABELLE_LOGIC") |
|
22 |
var modes: List[String] = Nil |
|
23 |
var no_build = false |
|
24 |
var options = Options.init() |
|
25 |
var system_mode = false |
|
26 |
||
27 |
val getopts = Getopts(""" |
|
28 |
Usage: isabelle console [OPTIONS] |
|
29 |
||
30 |
Options are: |
|
31 |
-d DIR include session directory |
|
32 |
-l NAME logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """) |
|
33 |
-m MODE add print mode for output |
|
34 |
-n no build of session image on startup |
|
35 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) |
|
36 |
-r logic session is RAW_ML_SYSTEM |
|
37 |
-s system build mode for session image |
|
38 |
||
62562
905a5db3932d
back to external line editor, due to problems of JLine with multithreading of in vs. out;
wenzelm
parents:
62561
diff
changeset
|
39 |
Run Isabelle process with raw ML console and line editor |
905a5db3932d
back to external line editor, due to problems of JLine with multithreading of in vs. out;
wenzelm
parents:
62561
diff
changeset
|
40 |
(ISABELLE_LINE_EDITOR=""" + quote(Isabelle_System.getenv("ISABELLE_LINE_EDITOR")) + """. |
62559 | 41 |
""", |
42 |
"d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))), |
|
43 |
"l:" -> (arg => logic = arg), |
|
44 |
"m:" -> (arg => modes = arg :: modes), |
|
45 |
"n" -> (arg => no_build = true), |
|
46 |
"o:" -> (arg => options = options + arg), |
|
47 |
"r" -> (_ => logic = "RAW_ML_SYSTEM"), |
|
48 |
"s" -> (_ => system_mode = true)) |
|
49 |
||
50 |
val more_args = getopts(args) |
|
51 |
if (!more_args.isEmpty) getopts.usage() |
|
52 |
||
53 |
||
54 |
// build |
|
55 |
if (!no_build && logic != "RAW_ML_SYSTEM" && |
|
56 |
Build.build(options = options, build_heap = true, no_build = true, |
|
57 |
dirs = dirs, system_mode = system_mode, sessions = List(logic)) != 0) |
|
58 |
{ |
|
59 |
val progress = new Console_Progress |
|
60 |
progress.echo("Build started for Isabelle/" + logic + " ...") |
|
61 |
progress.interrupt_handler { |
|
62 |
val rc = |
|
63 |
Build.build(options = options, progress = progress, build_heap = true, |
|
64 |
dirs = dirs, system_mode = system_mode, sessions = List(logic)) |
|
65 |
if (rc != 0) sys.exit(rc) |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
// process loop |
|
70 |
val process = |
|
62577
7e2aa1d67dd8
clarified interactive mode, which is relevant for ML prompts;
wenzelm
parents:
62562
diff
changeset
|
71 |
ML_Process(options, heap = logic, args = List("-i"), |
62559 | 72 |
modes = if (logic == "RAW_ML_SYSTEM") Nil else modes ::: List("ASCII")) |
73 |
val process_output = Future.thread[Unit]("process_output") { |
|
62561
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
74 |
try { |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
75 |
var result = new StringBuilder(100) |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
76 |
var finished = false |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
77 |
while (!finished) { |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
78 |
var c = -1 |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
79 |
var done = false |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
80 |
while (!done && (result.length == 0 || process.stdout.ready)) { |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
81 |
c = process.stdout.read |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
82 |
if (c >= 0) result.append(c.asInstanceOf[Char]) |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
83 |
else done = true |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
84 |
} |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
85 |
if (result.length > 0) { |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
86 |
System.out.print(result.toString) |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
87 |
System.out.flush() |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
88 |
result.length = 0 |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
89 |
} |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
90 |
else { |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
91 |
process.stdout.close() |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
92 |
finished = true |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
93 |
} |
62559 | 94 |
} |
95 |
} |
|
62561
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
96 |
catch { case e: IOException => case Exn.Interrupt() => } |
62559 | 97 |
} |
98 |
val process_input = Future.thread[Unit]("process_input") { |
|
62562
905a5db3932d
back to external line editor, due to problems of JLine with multithreading of in vs. out;
wenzelm
parents:
62561
diff
changeset
|
99 |
val reader = new BufferedReader(new InputStreamReader(System.in)) |
62559 | 100 |
POSIX_Interrupt.handler { process.interrupt } { |
62561
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
101 |
try { |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
102 |
var finished = false |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
103 |
while (!finished) { |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
104 |
reader.readLine() match { |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
105 |
case null => |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
106 |
process.stdin.close() |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
107 |
finished = true |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
108 |
case line => |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
109 |
process.stdin.write(line) |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
110 |
process.stdin.write("\n") |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
111 |
process.stdin.flush() |
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
112 |
} |
62559 | 113 |
} |
114 |
} |
|
62561
4bf00f54e4bc
ignore execeptions that usually occur due to shutdown;
wenzelm
parents:
62559
diff
changeset
|
115 |
catch { case e: IOException => case Exn.Interrupt() => } |
62559 | 116 |
} |
117 |
} |
|
118 |
val process_result = Future.thread[Int]("process_result") { |
|
119 |
val rc = process.join |
|
120 |
process_input.cancel |
|
121 |
rc |
|
122 |
} |
|
123 |
||
124 |
process_output.join |
|
125 |
process_input.join |
|
62562
905a5db3932d
back to external line editor, due to problems of JLine with multithreading of in vs. out;
wenzelm
parents:
62561
diff
changeset
|
126 |
process_result.join |
62559 | 127 |
} |
128 |
} |
|
129 |
} |