Browse Source

Made task2 more complex :|

themultiplexer 8 years ago
parent
commit
47b62ba722
3 changed files with 68 additions and 17 deletions
  1. 10
    1
      hw6/task2/src/command.rs
  2. 6
    1
      hw6/task2/src/main.rs
  3. 52
    15
      hw6/task2/src/shell.rs

+ 10
- 1
hw6/task2/src/command.rs View File

1
+use std::ffi::OsString;
2
+use std::str::FromStr;
3
+
1
 enum Command {
4
 enum Command {
2
     Empty,
5
     Empty,
3
     Exit,
6
     Exit,
6
 
9
 
7
 impl Command {}
10
 impl Command {}
8
 
11
 
12
+struct CommandNotFoundError();
13
+
9
 impl FromStr for Command {
14
 impl FromStr for Command {
10
-    fn from_str(s: &str) -> Result<Self, Self::Err> {}
15
+
16
+    type Err = CommandNotFoundError;
17
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
18
+
19
+    }
11
 }
20
 }

+ 6
- 1
hw6/task2/src/main.rs View File

1
 use std::process;
1
 use std::process;
2
+use shell::Shell;
3
+
2
 mod shell;
4
 mod shell;
5
+mod command;
3
 
6
 
4
 fn main() {
7
 fn main() {
5
-    let mut s = Shell::new(..);
8
+    let reader = shell::R;
9
+    let writer = shell::W;
10
+    let mut s = Shell::new(reader, writer, "schell".to_string());
6
     match s.start() {
11
     match s.start() {
7
         Ok(_) => process::exit(0),
12
         Ok(_) => process::exit(0),
8
         Err(_) => process::exit(1),
13
         Err(_) => process::exit(1),

+ 52
- 15
hw6/task2/src/shell.rs View File

1
 use std::io;
1
 use std::io;
2
 use std::io::prelude::*;
2
 use std::io::prelude::*;
3
-use command;
3
+use std::io::BufRead;
4
+use std::io::Read;
4
 
5
 
5
-struct Shell<R, W> {
6
+
7
+pub struct R;
8
+pub struct W;
9
+
10
+pub struct Shell<R, W> {
6
     pub reader: R,
11
     pub reader: R,
7
     pub writer: W,
12
     pub writer: W,
8
     pub should_exit: bool,
13
     pub should_exit: bool,
13
     /// Creates a new Shell.
18
     /// Creates a new Shell.
14
     pub fn new(input: R, output: W, name: String) -> Self {
19
     pub fn new(input: R, output: W, name: String) -> Self {
15
         Shell {
20
         Shell {
16
-            reader = input,
17
-            writer = output,
18
-            should_exit = false,
19
-            name = name,
21
+            reader : input,
22
+            writer : output,
23
+            should_exit : false,
24
+            name : name,
20
         }
25
         }
21
     }
26
     }
22
     /// Initializes the Shell Loop
27
     /// Initializes the Shell Loop
23
-    pub fn start() -> Result<Self, &str> {
24
-        shell_loop();
28
+    pub fn start<'a>(&self) -> Result<(), &'a str> {
29
+        self.shell_loop();
30
+        Ok(())
25
     }
31
     }
32
+
26
     /// Waits for user inputs.
33
     /// Waits for user inputs.
27
-    fn shell_loop() -> Result {
34
+    fn shell_loop(&self) -> Result<(),()> {
28
         loop {
35
         loop {
29
         }
36
         }
30
     }
37
     }
38
+
39
+    /*
31
     /// Prints the shell prompt.
40
     /// Prints the shell prompt.
32
     fn prompt() -> Result<Option, &str> {
41
     fn prompt() -> Result<Option, &str> {
33
     }
42
     }
34
     /// Runs a command.
43
     /// Runs a command.
35
     fn run(&str) {
44
     fn run(&str) {
36
     }
45
     }
46
+
47
+    */
37
 }
48
 }
38
 
49
 
39
-impl BufRead for Shell<R, W> {
40
-    fn fill_buf(&mut self) -> Result<&[u8]> {
41
-        stdin.fill_buf()
50
+
51
+impl Read for R {
52
+    fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
53
+        let stdin = io::stdin();
54
+        let mut stdin = stdin.lock();
55
+
56
+
57
+        let buffer = stdin.read(buf);
58
+
59
+        match buffer {
60
+            Ok(x) => {
61
+                println!("{} ||| {:?}", x, buffer);
62
+                stdin.consume(x);
63
+                Ok(x)
64
+            },
65
+            Err(_) => Err(io::Error::new(io::ErrorKind::InvalidInput, "jhrthhzruzkikz")),
66
+        }
67
+    }
68
+}
69
+
70
+impl BufRead for R {
71
+
72
+    fn fill_buf(&mut self) -> Result<&[u8], io::Error> {
73
+        Ok(&[11])
42
     }
74
     }
75
+
43
     fn consume(&mut self, amt: usize) {
76
     fn consume(&mut self, amt: usize) {
77
+        let mut buffer = self.fill_buf;
78
+        if buffer.len <= amt {
79
+
80
+        }
44
     }
81
     }
45
 }
82
 }
46
 
83
 
47
-impl Write for Shell<R, W> {
48
-    fn write(&mut self, buf: &[u8]) -> Result<usize> {
84
+impl Write for W {
85
+    fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
49
     }
86
     }
50
-    fn flush(&mut self) -> Result<()> {
87
+    fn flush(&mut self) -> Result<(),  io::Error> {
51
     }
88
     }
52
 }
89
 }

Loading…
Cancel
Save