aboutsummaryrefslogtreecommitdiffstats
path: root/rs/shell/src/main.rs
blob: cbe01e77b786315662b1e5831f4dd8834294afbd (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::{process::ExitCode, os::unix::process::CommandExt};

fn parse_sq(s: &str) -> Option<(String, &str)> {
    #[derive(PartialEq, Eq)]
    enum SqState {
        Quoted,
        Unquoted { may_escape: bool },
        UnquotedEscaped,
    }
    
    let mut result = String::new();
    let mut state = SqState::Unquoted { may_escape: false };
    let mut remaining = "";
    for (i, c) in s.char_indices() {
        match state {
            SqState::Unquoted { may_escape: false} => {
                if c != '\'' {
                    return None;
                }
                state = SqState::Quoted
            }
            SqState::Quoted => {
                if c == '\'' {
                    state = SqState::Unquoted { may_escape: true };
                    continue;
                }
                result.push(c);
            }
            SqState::Unquoted { may_escape: true }=> {
                if is_posix_space(c) {
                    remaining = &s[i..];
                    break;
                }
                if c != '\\' {
                    return None;
                }
                state = SqState::UnquotedEscaped;
            }
            SqState::UnquotedEscaped => {
                if c != '\\' && c != '!' {
                    return None;
                }
                result.push(c);
                state = SqState::Unquoted { may_escape: false };
            }
        }
    }

    if state != (SqState::Unquoted { may_escape: true }) {
        return None
    }
    Some((result, remaining))
}

fn parse_cmd(mut cmd: &str) -> Option<Vec<String>> {
    let mut args = Vec::<String>::new();

    cmd = cmd.trim_matches(is_posix_space);
    while cmd != "" {
        if cmd.starts_with('\'') {
            let (arg, remaining) = parse_sq(cmd)?;
            args.push(arg);
            cmd = remaining.trim_start_matches(is_posix_space);
        } else if let Some((arg, remaining)) = cmd.split_once(is_posix_space) {
            args.push(arg.to_owned());
            cmd = remaining.trim_start_matches(is_posix_space);
        } else {
            args.push(cmd.to_owned());
            cmd = "";
        }
    }

    Some(args)
}

fn is_posix_space(c: char) -> bool {
    // Form feed: 0x0c
    // Vertical tab: 0x0b
    c == ' ' || c == '\x0c' || c == '\n' || c == '\r' || c == '\t' || c == '\x0b'
}

fn main() -> ExitCode {
    let mut args = std::env::args().skip(1);
    if args.next() != Some("-c".to_string()) {
        eprintln!("Expected usage: shell -c <argument>");
        return ExitCode::FAILURE;
    }
    let Some(cmd) = args.next() else {
        eprintln!("Missing argument for argument '-c'");
        return ExitCode::FAILURE;
    };
    if args.next() != None {
        eprintln!("Too many arguments passed");
        return ExitCode::FAILURE;
    }

    let Some(mut cmd) = parse_cmd(&cmd) else {
        eprintln!("Bad command");
        return ExitCode::FAILURE;
    };

    let Some(mut program) = cmd.drain(0..1).next() else {
        eprintln!("Bad command");
        return ExitCode::FAILURE;
    };
    if program == "git" {
        let Some(subcommand) = cmd.drain(0..1).next() else {
            eprintln!("Bad command");
            return ExitCode::FAILURE;
        };
        program.push('-');
        program.push_str(&subcommand);
    }

    let mut args = Vec::new();

    let git_cmds = ["receive-pack", "upload-archive", "upload-pack"];
    if git_cmds.contains(&program.as_str()) {
        if cmd.len() != 1 {
            eprintln!("Bad command");
            return ExitCode::FAILURE;
        }
        let repository = cmd[0].trim_start_matches('/');
        args.push(repository);
    } else if program == "git-lfs-authenticate" {
        if cmd.len() != 2 {
            eprintln!("Bad command");
            return ExitCode::FAILURE;
        }
        let repository = cmd[0].trim_start_matches('/');
        args.push(repository);
        args.push(&cmd[1]);
    }

    let e = std::process::Command::new(program).args(args).exec();
    eprintln!("Error: {e}");
    ExitCode::FAILURE
}