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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! ## Runtime
//!
//! `runtime` contains the runtime functions for pyc core

/*
*
*   Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
*
* 	This file is part of "Pyc"
*
*   Pyc is free software: you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation, either version 3 of the License, or
*   (at your option) any later version.
*
*   Pyc is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with Pyc.  If not, see <http://www.gnu.org/licenses/>.
*
*/

//Deps
extern crate ansi_term;
extern crate nix;

mod props;

use ansi_term::Colour;
use std::path::{Path, PathBuf};
use std::thread::sleep;
use std::time::{Duration};

//Config
use crate::config;
//Props
use props::RuntimeProps;
//Shell
use crate::shell::proc::ShellState;
use crate::shell::{Shell};
use crate::shell::unixsignal::UnixSignal;
//Translator
use crate::translator::ioprocessor::IOProcessor;
//Utils
use crate::utils::console;
use crate::utils::file;

//@! Runners

/// ### run_interactive
///
/// Run pyc in interactive mode

pub fn run_interactive(processor: IOProcessor, config: config::Config, shell: Option<String>, history_file: Option<PathBuf>) -> u8 {
    //Instantiate Runtime Props
    let mut props: RuntimeProps = RuntimeProps::new(true, config, processor);
    //Determine the shell to use
    let (shell, args): (String, Vec<String>) = resolve_shell(&props.config, shell);
    //Intantiate and start a new shell
    let mut shell: Shell = match Shell::start(shell, args, &props.config.prompt_config) {
        Ok(sh) => sh,
        Err(err) => {
            print_err(
                String::from(format!("Could not start shell: {}", err)),
                props.config.output_config.translate_output,
                &props.processor,
            );
            return 255;
        }
    };
    //If history file is set, load history
    if let Some(history_file) = history_file.clone() {
        match file::read_lines(history_file.clone()) {
            Ok(lines) => shell.history.load(lines),
            Err(err) => print_err(
                String::from(format!("Could not load history from '{}': {}", history_file.display(), err)),
                props.config.output_config.translate_output,
                &props.processor,
            )
        }
    };
    //@! Main loop
    while props.get_last_state() != ShellState::Terminated {
        //@! Print prompt if state is Idle and state has changed
        let current_state: ShellState = shell.get_state();
        if current_state != props.get_last_state() {
            props.update_state(current_state);
        }
        if props.get_state_changed() && current_state == ShellState::Idle {
            //Force shellenv to refresh info
            shell.refresh_env();
            //Print prompt
            console::print(format!("{} ", shell.get_promptline(&props.processor)));
            props.report_state_changed_notified(); //Force state changed to false
        } else if props.get_state_changed() {
            props.report_state_changed_notified(); //Check has been done, nothing to do
        }
        //@! Read user input
        if let Some(ev) = console::read() {
            props.handle_input_event(ev, &mut shell);
        };
        //Update state after write
        let new_state = shell.get_state(); //Force last state to be changed
        if new_state != props.get_last_state() {
            props.update_state(new_state);
        }
        //@! Read Shell stdout
        read_from_shell(&mut shell, &props.config, &props.processor);
        //Check if shell has terminated
        sleep(Duration::from_nanos(100)); //Sleep for 100ns
    } //@! End of loop
    //Write history back to file
    if let Some(history_file) = history_file {
        let lines: Vec<String> = shell.history.dump();
        if let Err(err) = file::write_lines(history_file.clone(), lines) {
            print_err(
                String::from(format!("Could not write history to '{}': {}", history_file.display(), err)),
                props.config.output_config.translate_output,
                &props.processor,
            );
        }
    };
    //Return shell exitcode
    match shell.stop() {
        Ok(rc) => rc,
        Err(err) => {
            print_err(format!("Could not stop shell: {}", err), props.config.output_config.translate_output, &props.processor);
            255
        }
    }
}

/// ### run_command
/// 
/// Run command in shell and return
pub fn run_command(mut command: String, processor: IOProcessor, config: config::Config, shell: Option<String>) -> u8 {
    //Instantiate Runtime Props
    let mut props: RuntimeProps = RuntimeProps::new(false, config, processor);
    //Determine the shell to use
    let (shell, args): (String, Vec<String>) = resolve_shell(&props.config, shell);
    //Intantiate and start a new shell
    let mut shell: Shell = match Shell::start(shell, args, &props.config.prompt_config) {
        Ok(sh) => sh,
        Err(err) => {
            print_err(
                String::from(format!("Could not start shell: {}", err)),
                props.config.output_config.translate_output,
                &props.processor,
            );
            return 255;
        }
    };
    //Prepare command
    while command.ends_with('\n') {
        command.pop();
    }
    while command.ends_with(';') {
        command.pop();
    }
    //FIXME: handle fish $status
    command.push_str("; exit $?\n");
    //Write command
    if let Err(err) = shell.write(command) {
        print_err(
            String::from(format!("Could not start shell: {}", err)),
            props.config.output_config.translate_output,
            &props.processor,
        );
        return 255;
    }
    let _ = shell.write(String::from("\n"));
    //@! Main loop
    loop { //Check state after reading/writing, since program could have already terminate
        //@! Read user input
        if let Some(ev) = console::read() {
            props.handle_input_event(ev, &mut shell);
        };
        //@! Read Shell stdout
        read_from_shell(&mut shell, &props.config, &props.processor);
        //Check if shell has terminated
        if shell.get_state() == ShellState::Terminated {
            break;
        }
        sleep(Duration::from_nanos(100)); //Sleep for 100ns
    } //@! End of main loop
    //Return shell exitcode
    match shell.stop() {
        Ok(rc) => rc,
        Err(err) => {
            print_err(format!("Could not stop shell: {}", err), props.config.output_config.translate_output, &props.processor);
            255
        }
    }
}

/// ### run_file
/// 
/// Run shell reading commands from file
pub fn run_file(file: String, processor: IOProcessor, config: config::Config, shell: Option<String>) -> u8 {
    let file_path: &Path = Path::new(file.as_str());
    let lines: Vec<String> = match file::read_lines(file_path) {
        Ok(lines) => lines,
        Err(_) => {
            print_err(format!("{}: No such file or directory", file), config.output_config.translate_output, &processor);
            return 255
        }
    };
    //Join lines in a single command
    let command: String = script_lines_to_string(&lines);
    //Execute command
    run_command(command, processor, config, shell)
}

//@! Shell functions

/// ### read_from_shell
/// 
/// Read from shell stderr and stdout
fn read_from_shell(shell: &mut Shell, config: &config::Config, processor: &IOProcessor) {
    if let Ok((out, err)) = shell.read() {
        if out.is_some() {
            //Convert out to cyrillic
            print_out(out.unwrap(), config.output_config.translate_output, &processor);
        }
        if err.is_some() {
            //Convert err to cyrillic
            print_err(err.unwrap().to_string(), config.output_config.translate_output, &processor);
        }
    }
}

/// ### resolve_shell
/// 
/// Resolve shell to use from configuration and arguments
fn resolve_shell(config: &config::Config, shellopt: Option<String>) -> (String, Vec<String>) {
    match shellopt {
        Some(sh) => (sh, vec![]),
        None => (config.shell_config.exec.clone(), config.shell_config.args.clone()) //Get shell from config
    }
}

/// ### script_lines_to_string
/// 
/// Converts script lines to a single command as string
fn script_lines_to_string(lines: &Vec<String>) -> String {
    let mut command: String = String::new();
    for line in lines.iter() {
        if line.starts_with("#") {
            continue;
        }
        if line.len() == 0 {
            continue;
        }
        command.push_str(line);
        //Don't add multiple semicolons
        if ! line.ends_with(";") {
            command.push(';');
        }
    }
    command
}

/// ### resolve_command
///
/// resolve command according to configured alias

fn resolve_command(argv: &mut Vec<String>, config: &config::Config) {
    //Process arg 0
    match config.get_alias(&argv[0]) {
        Some(resolved) => argv[0] = resolved,
        None => {}
    };
}

/*
/// ### get_shell_from_env
///
/// Try to get the shell path from SHELL environment variable
fn get_shell_from_env() -> Result<String, ()> {
    if let Ok(val) = env::var("SHELL") {
        Ok(val)
    } else {
        Err(())
    }
}
*/

//@! Prompt functions

/// ### print_err
/// 
/// print error message; the message is may converted to cyrillic if translate config is true

fn print_err(err: String, to_cyrillic: bool, processor: &IOProcessor) {
    match to_cyrillic {
        true => eprintln!("{}", Colour::Red.paint(processor.text_to_cyrillic(&err))),
        false => eprintln!("{}", Colour::Red.paint(err)),
    };
}

/// ### print_out
///
/// print normal message; the message is may converted to cyrillic if translate config is true

fn print_out(out: String, to_cyrillic: bool, processor: &IOProcessor) {
    match to_cyrillic {
        true => console::println(format!("{}", processor.text_to_cyrillic(&out))),
        false => console::println(format!("{}", out)),
    };
}

/// ### shellsignal_to_signal
/// 
/// Converts a signal received on prompt to a UnixSignal
#[allow(dead_code)]
fn shellsignal_to_signal(sig: u8) -> Option<UnixSignal> {
    match sig {
        3 => Some(UnixSignal::Sigint),
        26 => Some(UnixSignal::Sigstop),
        _ => None
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::config::Config;

    use crate::translator::ioprocessor::IOProcessor;
    use crate::translator::new_translator;
    use crate::translator::lang::Language;

    use std::collections::HashMap;
    use std::time::Duration;
    use std::thread::sleep;

    #[test]
    fn test_runtime_read_from_shell() {
        let mut cfg: Config = Config::default();
        cfg.output_config.translate_output = true;
        let iop: IOProcessor = IOProcessor::new(Language::Russian, new_translator(Language::Russian));
        let mut shell: Shell = Shell::start(String::from("sh"), vec![], &cfg.prompt_config).unwrap();
        sleep(Duration::from_millis(500)); //DON'T REMOVE THIS SLEEP
        //Write
        let _ = shell.write(String::from("echo 4\n"));
        sleep(Duration::from_millis(100));
        //Read
        read_from_shell(&mut shell, &cfg, &iop);
        //Don't translate
        cfg.output_config.translate_output = false;
        let _ = shell.write(String::from("echo 5\n"));
        sleep(Duration::from_millis(100));
        read_from_shell(&mut shell, &cfg, &iop);
        //Try stderr
        cfg.output_config.translate_output = true;
        let _ = shell.write(String::from("poropero\n"));
        sleep(Duration::from_millis(100));
        read_from_shell(&mut shell, &cfg, &iop);
        //Try stderr not translated
        cfg.output_config.translate_output = false;
        let _ = shell.write(String::from("poropero\n"));
        sleep(Duration::from_millis(100));
        read_from_shell(&mut shell, &cfg, &iop);
        //Terminate shell
        sleep(Duration::from_millis(500)); //DON'T REMOVE THIS SLEEP
        assert!(shell.stop().is_ok());
        sleep(Duration::from_millis(500)); //DON'T REMOVE THIS SLEEP
    }

    #[test]
    fn test_runtime_resolve_shell() {
        let mut cfg: Config = Config::default();
        cfg.shell_config.args = vec![String::from("-i")];
        //Resolve shell without cli option
        assert_eq!(resolve_shell(&cfg, None), (String::from("bash"), vec![String::from("-i")]));
        //Resolve shell with cli option
        assert_eq!(resolve_shell(&cfg, Some(String::from("fish"))), (String::from("fish"), vec![]));
    }

    #[test]
    fn test_runtime_script_lines_to_command() {
        let lines: Vec<String> = vec![String::from("#!/bin/bash"), String::from(""), String::from("echo 4"), String::from("#this is a comment"), String::from("cat /tmp/output;")];
        assert_eq!(script_lines_to_string(&lines), String::from("echo 4;cat /tmp/output;"));
    }

    #[test]
    fn test_runtime_resolve_command() {
        let mut alias_cfg: HashMap<String, String> = HashMap::new();
        alias_cfg.insert(String::from("ll"), String::from("ls -l"));
        let cfg: Config = Config {
            language: String::from(""),
            shell_config: config::ShellConfig::default(),
            alias: alias_cfg,
            output_config: config::OutputConfig::default(),
            prompt_config: config::PromptConfig::default()
        };
        //Resolve command
        let mut argv: Vec<String> = vec![String::from("ll"), String::from("/tmp/")];
        resolve_command(&mut argv, &cfg);
        assert_eq!(*argv.get(0).unwrap(), String::from("ls -l"));

        //Unresolved command
        let mut argv: Vec<String> = vec![String::from("du"), String::from("-hs")];
        resolve_command(&mut argv, &cfg);
        assert_eq!(*argv.get(0).unwrap(), String::from("du"));
    }

    #[test]
    fn test_runtime_print() {
        let iop: IOProcessor = IOProcessor::new(Language::Russian, new_translator(Language::Russian));
        //Out
        print_out(String::from("Hello"), true, &iop);
        print_out(String::from("Hello"), false, &iop);
        //Err
        print_err(String::from("Hello"), true, &iop);
        print_err(String::from("Hello"), false, &iop);
    }

    #[test]
    fn test_runtime_shellsignal() {
        assert_eq!(shellsignal_to_signal(3).unwrap(), UnixSignal::Sigint);
        assert_eq!(shellsignal_to_signal(26).unwrap(), UnixSignal::Sigstop);
        assert!(shellsignal_to_signal(255).is_none());
    }

}