20
20
#[license = "MIT/ASL2"];
21
21
#[crate_type = "lib"];
22
22
23
+ extern mod rustpkg(vers = "0.7-pre");
24
+ extern mod rustdoc(vers = "0.7-pre");
25
+ extern mod rusti(vers = "0.7-pre");
26
+ extern mod rustc(vers = "0.7-pre");
27
+
23
28
use core::run;
24
29
25
30
enum ValidUsage {
@@ -36,13 +41,13 @@ impl ValidUsage {
36
41
}
37
42
38
43
enum Action<'self> {
39
- Exec (&'self str),
40
- Call (&'self fn(args: &[~str]) -> ValidUsage)
44
+ Call (&'self fn(args: &[~ str]) -> ValidUsage ),
45
+ CallMain (&'static str, &' self fn()),
41
46
}
42
47
43
48
enum UsageSource<'self> {
44
- UsgExec (&'self str),
45
- UsgStr (&'self str)
49
+ UsgStr (&'self str),
50
+ UsgCall (&'self fn()),
46
51
}
47
52
48
53
struct Command<'self> {
@@ -55,9 +60,9 @@ struct Command<'self> {
55
60
static commands: &'static [Command<'static>] = &[
56
61
Command{
57
62
cmd: "build",
58
- action: Exec ("rustc"),
63
+ action: CallMain ("rustc", rustc::main ),
59
64
usage_line: "compile rust source files",
60
- usage_full: UsgExec("rustc --help")
65
+ usage_full: UsgCall(rustc_help),
61
66
},
62
67
Command{
63
68
cmd: "run",
@@ -81,21 +86,21 @@ static commands: &'static [Command<'static>] = &[
81
86
},
82
87
Command{
83
88
cmd: "doc",
84
- action: Exec ("rustdoc"),
89
+ action: CallMain ("rustdoc", rustdoc::main ),
85
90
usage_line: "generate documentation from doc comments",
86
- usage_full: UsgExec(" rustdoc --help")
91
+ usage_full: UsgCall( rustdoc::config::usage),
87
92
},
88
93
Command{
89
94
cmd: "pkg",
90
- action: Exec ("rustpkg"),
95
+ action: CallMain ("rustpkg", rustpkg::main ),
91
96
usage_line: "download, build, install rust packages",
92
- usage_full: UsgExec(" rustpkg --help")
97
+ usage_full: UsgCall( rustpkg::usage::general),
93
98
},
94
99
Command{
95
100
cmd: "sketch",
96
- action: Exec ("rusti"),
101
+ action: CallMain ("rusti", rusti::main ),
97
102
usage_line: "run a rust interpreter",
98
- usage_full: UsgStr("\nUsage:\trusti")
103
+ usage_full: UsgStr("\nUsage:\trusti"),
99
104
},
100
105
Command{
101
106
cmd: "help",
@@ -109,6 +114,10 @@ static commands: &'static [Command<'static>] = &[
109
114
}
110
115
];
111
116
117
+ fn rustc_help() {
118
+ rustc::usage(copy os::args()[0])
119
+ }
120
+
112
121
fn find_cmd(command_string: &str) -> Option<Command> {
113
122
do commands.find |command| {
114
123
command.cmd == command_string
@@ -120,20 +129,14 @@ fn cmd_help(args: &[~str]) -> ValidUsage {
120
129
match find_cmd(command_string) {
121
130
Some(command) => {
122
131
match command.action {
123
- Exec(s ) => io::println(fmt!(
132
+ CallMain(prog, _ ) => io::println(fmt!(
124
133
"The %s command is an alias for the %s program.",
125
- command.cmd, s )),
134
+ command.cmd, prog )),
126
135
_ => ()
127
136
}
128
137
match command.usage_full {
129
- UsgStr(msg) => io::println(fmt!("%s\n", msg)),
130
- UsgExec(commandline) => {
131
- let mut words = ~[];
132
- for str::each_word(commandline) |word| { words.push(word.to_owned()) }
133
- let words = words;
134
- let (prog, args) = (words.head(), words.tail());
135
- run::run_program(*prog, args);
136
- }
138
+ UsgStr(msg) => io::println(fmt!("%s\n", msg)),
139
+ UsgCall(f) => f(),
137
140
}
138
141
Valid
139
142
},
@@ -151,50 +154,40 @@ fn cmd_test(args: &[~str]) -> ValidUsage {
151
154
match args {
152
155
[filename] => {
153
156
let test_exec = Path(filename).filestem().unwrap() + "test~";
154
- if run::run_program("rustc", [
155
- ~"--test",
156
- filename.to_owned(),
157
- ~"-o",
158
- test_exec.to_owned()
159
- ]) == 0 {
160
- run::run_program(~"./" + test_exec, []);
161
- }
157
+ invoke("rustc", &[~"--test", filename.to_owned(),
158
+ ~"-o", test_exec.to_owned()], rustc::main);
159
+ run::run_program(~"./" + test_exec, []);
162
160
Valid
163
161
}
164
- _ => Invalid
162
+ _ => Invalid
165
163
}
166
164
}
167
165
168
166
fn cmd_run(args: &[~str]) -> ValidUsage {
169
167
match args {
170
168
[filename, ..prog_args] => {
171
169
let exec = Path(filename).filestem().unwrap() + "~";
172
- if run::run_program("rustc", [
173
- filename.to_owned(),
174
- ~"-o",
175
- exec.to_owned()
176
- ]) == 0 {
177
- run::run_program(~"./"+exec, prog_args);
178
- }
170
+ invoke("rustc", &[filename.to_owned(), ~"-o", exec.to_owned()],
171
+ rustc::main);
172
+ run::run_program(~"./"+exec, prog_args);
179
173
Valid
180
174
}
181
- _ => Invalid
175
+ _ => Invalid
182
176
}
183
177
}
184
178
179
+ fn invoke(prog: &str, args: &[~str], f: &fn()) {
180
+ let mut osargs = ~[prog.to_owned()];
181
+ osargs.push_all_move(args.to_owned());
182
+ os::set_args(osargs);
183
+ f();
184
+ }
185
+
185
186
fn do_command(command: &Command, args: &[~str]) -> ValidUsage {
186
187
match command.action {
187
188
Call(f) => f(args),
188
- Exec(commandline) => {
189
- let mut words = ~[];
190
- for str::each_word(commandline) |word| { words.push(word.to_owned()) }
191
- let words = words;
192
- let (prog, prog_args) = (words.head(), words.tail());
193
- let exitstatus = run::run_program(
194
- *prog,
195
- vec::append(vec::to_owned(prog_args), args)
196
- );
197
- os::set_exit_status(exitstatus);
189
+ CallMain(prog, f) => {
190
+ invoke(prog, args, f);
198
191
Valid
199
192
}
200
193
}
@@ -232,11 +225,9 @@ pub fn main() {
232
225
let args = os_args.tail();
233
226
234
227
if !args.is_empty() {
235
- for commands.each |command| {
236
- if command.cmd == *args.head() {
237
- let result = do_command(command, args.tail());
238
- if result.is_valid() { return; }
239
- }
228
+ for find_cmd(*args.head()).each |command| {
229
+ let result = do_command(command, args.tail());
230
+ if result.is_valid() { return; }
240
231
}
241
232
}
242
233
0 commit comments