Skip to content

Commit 85c2b0c

Browse files
committed
Add args support and move tests to specific command line
1 parent fbf9752 commit 85c2b0c

4 files changed

Lines changed: 82 additions & 7 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,11 @@ dpkey module steals key input processing from DOS and converts scan code to asci
9090
about scan code: see [PS/2 Keyboard - OSDev Wiki](https://wiki.osdev.org/PS/2_Keyboard).
9191

9292
![sample2](https://github.com/o8vm/rust_dos/blob/images/dpkey.gif)
93+
94+
## Tests
95+
96+
To run the tests suite, you can type on your DOS emulator:
97+
98+
```shell
99+
rust_dos.com test
100+
```

src/dos.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub mod error_code;
99
pub mod panic;
1010
pub mod math;
1111
pub mod cooperative_multitasking;
12+
pub mod env;
13+
1214
use core::arch::asm;
1315

1416
pub use alloc::string::String as String;

src/dos/env.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use alloc::vec::Vec;
2+
use core::slice;
3+
4+
fn command_tail<'a>() -> &'a [u8] {
5+
unsafe {
6+
let ptr = 0x80 as *const u8;
7+
let len = *ptr as usize;
8+
let bytes = slice::from_raw_parts(ptr.add(1), len);
9+
10+
let trimmed = bytes.iter()
11+
.position(|&b| b == 0x0D)
12+
.unwrap_or(len);
13+
14+
&bytes[..trimmed]
15+
}
16+
}
17+
18+
pub fn args() -> Vec<&'static str> {
19+
let mut args = Vec::new();
20+
let line = command_tail();
21+
22+
let s = unsafe { core::str::from_utf8_unchecked(line) };
23+
let bytes = s.as_bytes();
24+
let len = bytes.len();
25+
let mut i = 0;
26+
27+
while i < len {
28+
while i < len && bytes[i].is_ascii_whitespace() {
29+
i += 1;
30+
}
31+
if i >= len { break; }
32+
33+
let start = i;
34+
let mut in_quotes = false;
35+
36+
while i < len {
37+
let c = bytes[i];
38+
if c == b'"' {
39+
in_quotes = !in_quotes;
40+
i += 1;
41+
continue;
42+
}
43+
if !in_quotes && c.is_ascii_whitespace() {
44+
break;
45+
}
46+
i += 1;
47+
}
48+
49+
let end = i;
50+
let token = s[start..end].trim_matches('"');
51+
args.push(token);
52+
}
53+
54+
args
55+
}

src/main.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
1+
#![feature(abi_x86_interrupt)]
12
#![no_std]
23
#![no_main]
34

45
extern crate alloc;
56

67
mod dos_tests;
78

8-
use rust_dos::*;
99
use crate::dos_tests::allocator_test::allocator_test;
10+
use crate::dos_tests::cooperative_multitasking_test::cooperative_multitasking_test;
1011
use crate::dos_tests::datetime::datetime_test;
1112
use crate::dos_tests::file::file_read_test;
12-
use crate::dos_tests::cooperative_multitasking_test::cooperative_multitasking_test;
13+
use rust_dos::*;
1314

1415
entry!(main);
1516

1617
fn main() {
17-
allocator_test();
18-
file_read_test();
19-
datetime_test();
20-
cooperative_multitasking_test();
21-
println!("Hello, World!");
18+
let args = dos::env::args();
19+
20+
if args.len() >= 1 && args[0] == "test" {
21+
allocator_test();
22+
file_read_test();
23+
datetime_test();
24+
cooperative_multitasking_test();
25+
} else {
26+
println!("Hello, World!");
27+
}
2228
}
29+
30+
31+
32+

0 commit comments

Comments
 (0)