aboutsummaryrefslogtreecommitdiff
path: root/examples/parselog.rs
blob: 804c3994929c175d54d85f35829e92848eedcff9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! Parse input from stdin and log actions on stdout
extern crate vtparse;

use std::io::{self, Read};

use vtparse::{StateMachine, Parser};

/// A type implementing Parser that just logs actions
struct Log;

impl Parser for Log {
    fn print(&mut self, _machine: &StateMachine, c: char) {
        println!("[print] {:?}", c);
    }
    fn execute(&mut self, _machine: &StateMachine, byte: u8) {
        println!("[execute] byte={:02x}", byte);
    }
    fn hook(&mut self, _machine: &StateMachine, byte: u8) {
        println!("[hook] byte={:02x}", byte);
    }
    fn put(&mut self, _machine: &StateMachine, byte: u8) {
        println!("[put] byte={:02x}", byte);
    }
    fn osc_start(&mut self, _machine: &StateMachine, byte: u8) {
        println!("[osc_start] byte={:02x}", byte);
    }
    fn osc_put(&mut self, _machine: &StateMachine, byte: u8) {
        println!("[osc_put] byte={:02x}", byte);
    }
    fn osc_end(&mut self, _machine: &StateMachine, byte: u8) {
        println!("[osc_end] byte={:02x}", byte);
    }
    fn unhook(&mut self, _machine: &StateMachine, byte: u8) {
        println!("[unhook] byte={:02x}", byte);
    }
    fn csi_dispatch(&mut self, machine: &StateMachine, c: char) {
        println!("[csi_dispatch] params={:?}, intermediates={:?}, action={:?}",
                 machine.params(), machine.intermediates(), c);
    }
    fn esc_dispatch(&mut self, machine: &StateMachine, byte: u8) {
        println!("[csi_dispatch] params={:?}, intermediates={:?}, action={:?}",
                 machine.params(), machine.intermediates(), byte as char);
    }
}

fn main() {
    let input = io::stdin();
    let mut handle = input.lock();

    let mut statemachine = StateMachine::new();
    let mut parser = Log;

    let mut buf: [u8; 2048] = unsafe { std::mem::uninitialized() };

    loop {
        match handle.read(&mut buf) {
            Ok(0) => break,
            Ok(n) => {
                for byte in &buf[..n] {
                    statemachine.advance(&mut parser, *byte);
                }
            },
            Err(err) => {
                println!("err: {}", err);
                break;
            }
        }
    }
}