Skip to content

Commit

Permalink
Some more fixes for getting pnalogin to work.
Browse files Browse the repository at this point in the history
It's almost working.
  • Loading branch information
mkrueger committed Jan 20, 2025
1 parent 84838ed commit 732f79b
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 49 deletions.
6 changes: 3 additions & 3 deletions crates/icbsetup/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use icy_board_engine::icy_board::{read_with_encoding_detection, IcyBoard};
use icy_board_tui::{get_text_args, print_error, term};
use import::{console_logger::ConsoleLogger, PCBoardImporter};
use semver::Version;
use walkdir::WalkDir;
use std::{
collections::HashMap,
fs,
path::PathBuf,
process::{self, exit},
sync::{Arc, Mutex},
};
use walkdir::WalkDir;

pub mod app;
mod create;
Expand Down Expand Up @@ -142,14 +142,14 @@ fn main() -> Result<()> {
Some(Commands::PPEConvert(PPEConvert { path })) => {
println!("Converting PPE data files in {}", path.display());
println!("Caution - this command is used for converting CP437 to UTF-8 in a directory.");

println!("Converting directories to lower case...");
for entry in WalkDir::new(path).into_iter().filter_map(|e| e.ok()) {
if !entry.path().is_dir() {
continue;
}
let lower_case = entry.path().to_string_lossy().to_string().to_lowercase();
if lower_case == "." || lower_case == ".." {
if lower_case == "." || lower_case == ".." {
continue;
}
println!("Rename directory {} to {}", entry.path().display(), lower_case);
Expand Down
9 changes: 9 additions & 0 deletions crates/icy_board_engine/src/icy_board/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,15 @@ pub fn read_with_encoding_detection<P: AsRef<Path>>(path: &P) -> Res<String> {
}
}

pub fn read_data_with_encoding_detection(data: &[u8]) -> Res<String> {
let import = if data.starts_with(&UTF8_BOM) {
String::from_utf8_lossy(&data[UTF8_BOM.len()..]).to_string()
} else {
crate::tables::import_cp437_string(&data, false)
};
Ok(import)
}

pub fn convert_to_utf8<P: AsRef<Path>, Q: AsRef<Path>>(from: &P, to: &Q) -> Res<()> {
let import = read_with_encoding_detection(from)?;
write_with_bom(to, &import)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,11 @@ pub async fn instr(vm: &mut VirtualMachine<'_>, args: &[PPEExpr]) -> Res<Variabl
if sub.is_empty() {
return Ok(VariableValue::new_int(0));
}

match str.find(&sub) {
Some(x) => Ok(VariableValue::new_int(1 + x as i32)),
Some(x) => {
let x = str[0..x].chars().count();
Ok(VariableValue::new_int(1 + x as i32))
}
_ => Ok(VariableValue::new_int(0)),
}
}
Expand Down
21 changes: 16 additions & 5 deletions crates/icy_board_engine/src/vm/io.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{
fs::{self, File, OpenOptions},
io::{BufRead, BufReader, Read, Result, Seek, SeekFrom, Write},
io::{BufRead, Cursor, Read, Result, Seek, SeekFrom, Write},
path::Path,
time::SystemTime,
};

use crate::Res;
use crate::{icy_board::read_data_with_encoding_detection, Res};

use crate::vm::VMError;

Expand Down Expand Up @@ -126,7 +126,7 @@ pub trait PCBoardIO: Send {

struct FileChannel {
file: Option<Box<File>>,
reader: Option<BufReader<File>>,
reader: Option<Cursor<String>>,
_content: Vec<u8>,
err: bool,
}
Expand Down Expand Up @@ -231,6 +231,12 @@ impl PCBoardIO for DiskIO {
};

if let Some(f) = &mut chan.file {
if let Ok(md) = f.metadata() {
if md.len() == 0 {
const UTF8_BOM: [u8; 3] = [0xEF, 0xBB, 0xBF];
let _ = f.write(&UTF8_BOM);
}
}
let _ = f.write(text.as_bytes());
chan.err = false;
} else {
Expand All @@ -245,8 +251,13 @@ impl PCBoardIO for DiskIO {
return Err(Box::new(VMError::FileChannelNotOpen(channel)));
};

if let Some(f) = chan.file.take() {
chan.reader = Some(BufReader::new(*f));
if let Some(mut f) = chan.file.take() {
let mut buf = Vec::new();
let _ = f.read_to_end(&mut buf);
let str = read_data_with_encoding_detection(&buf).unwrap();
let c = Cursor::new(str);

chan.reader = Some(c);
}
if let Some(reader) = &mut chan.reader {
let mut line = String::new();
Expand Down
54 changes: 30 additions & 24 deletions crates/icy_board_engine/src/vm/statements/predefined_procedures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ pub async fn closecap(vm: &mut VirtualMachine<'_>, args: &[PPEExpr]) -> Res<()>
}

pub async fn message(vm: &mut VirtualMachine<'_>, args: &[PPEExpr]) -> Res<()> {
let conf = vm.eval_expr(&args[0]).await?.as_int() as u16;
let conf = vm.eval_expr(&args[0]).await?.as_int();
let to = vm.eval_expr(&args[1]).await?.as_string();
let from = vm.eval_expr(&args[2]).await?.as_string();
let subject = vm.eval_expr(&args[3]).await?.as_string();
Expand All @@ -788,31 +788,35 @@ pub async fn message(vm: &mut VirtualMachine<'_>, args: &[PPEExpr]) -> Res<()> {
log::error!("PPE function 'message': Message text file not found {}", file.display());
return Err(Box::new(IcyError::FileNotFound("MESSAGE".to_string(), file.display().to_string())));
}
let mut message = JamMessage::default()
.with_from(BString::from(from))
.with_to(BString::from(to))
.with_subject(BString::from(subject))
.with_date_time(Utc::now())
.with_text(BString::from(fs::read_to_string(file)?));
// TODO: Message Security
if pack_out_date > 0 {
message = message.with_packout_date(IcbDate::from_pcboard(pack_out_date).to_utc_date_time());
}

if let Ok(area_opt) = vm.icy_board_state.show_message_areas(conf, "").await {
match area_opt {
Some(area) => {
let mut message = JamMessage::default()
.with_from(BString::from(from))
.with_to(BString::from(to))
.with_subject(BString::from(subject))
.with_date_time(Utc::now())
.with_text(BString::from(fs::read_to_string(file)?));
// TODO: Message Security
if pack_out_date > 0 {
message = message.with_packout_date(IcbDate::from_pcboard(pack_out_date).to_utc_date_time());
if conf >= 0 {
if let Ok(area_opt) = vm.icy_board_state.show_message_areas(conf as u16, "").await {
match area_opt {
Some(area) => {
vm.icy_board_state
.send_message(conf as i32, area as i32, message, IceText::SavingMessage)
.await?;
}
None => {
vm.icy_board_state
.display_text(IceText::MessageAborted, display_flags::LFBEFORE | display_flags::NEWLINE)
.await?;
log::error!("Message area not found: {}", conf);
}
vm.icy_board_state
.send_message(conf as i32, area as i32, message, IceText::SavingMessage)
.await?;
}
None => {
vm.icy_board_state
.display_text(IceText::MessageAborted, display_flags::LFBEFORE | display_flags::NEWLINE)
.await?;
log::error!("Message area not found: {}", conf);
}
}
} else {
vm.icy_board_state.send_message(-1, 0, message, IceText::SavingMessage).await?;
}
Ok(())
}
Expand Down Expand Up @@ -1366,8 +1370,10 @@ pub async fn brag(vm: &mut VirtualMachine<'_>, args: &[PPEExpr]) -> Res<()> {
panic!("TODO")
}
pub async fn frealtuser(vm: &mut VirtualMachine<'_>, args: &[PPEExpr]) -> Res<()> {
log::error!(" frealtuser not implemented statement!");
panic!("TODO")
if let Some(user) = &vm.icy_board_state.session.current_user.clone() {
vm.set_user_variables(user);
}
Ok(())
}
pub async fn setlmr(vm: &mut VirtualMachine<'_>, args: &[PPEExpr]) -> Res<()> {
log::error!(" setlmr not implemented statement!");
Expand Down
1 change: 1 addition & 0 deletions crates/icy_board_engine/tests/test_data/cursor_pos.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2x2
2 changes: 2 additions & 0 deletions crates/icy_board_engine/tests/test_data/cursor_pos.pps
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
AnsiPos 2,2
PrintLn GetX(), "x", GetY()
6 changes: 6 additions & 0 deletions crates/icy_board_engine/tests/test_data/string_functions.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1
3
0
7
F█oB█r
34
17 changes: 17 additions & 0 deletions crates/icy_board_engine/tests/test_data/string_functions.pps
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
STRING S
INTEGER I, L

S = "*"
PRINTLN INSTR(S, "*")
S = "██*"
PRINTLN INSTR(S, "*")
PRINTLN INSTR(S, "A")

S = "F█o*B█r"
L = LEN(S)
PRINTLN L

I = INSTR(S, "*")
PRINT LEFT(S, I - 1)
PRINTLN MID(S, I + 1, L-I)
PRINTLN MID("12345", 3, 2)
31 changes: 16 additions & 15 deletions crates/icy_board_engine/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{env, path::PathBuf, sync::Arc, thread};
use icy_board_engine::{
compiler::PPECompiler,
executable::{Executable, LAST_PPLC},
icy_board::{bbs::BBS, state::IcyBoardState},
icy_board::{bbs::BBS, read_data_with_encoding_detection, state::IcyBoardState},
parser::{Encoding, UserTypeRegistry},
};
use icy_net::{channel::ChannelConnection, Connection, ConnectionType};
Expand All @@ -25,14 +25,14 @@ fn test_compiler() {
let executable = fs::read_to_string(&cur_entry).unwrap();
let mut out_path = cur_entry.clone();
out_path.set_extension("out");
let expected_output = fs::read_to_string(&out_path).unwrap();
let expected_output = unsafe { String::from_utf8_unchecked(fs::read(&out_path).unwrap()) };

let file_name = cur_entry.file_name().unwrap().to_str().unwrap();
run_test(file_name, &executable, &expected_output);
}
}

fn run_test(file_name: &str, input: &str, output: &str) {
fn run_test(file_name: &str, input: &str, expected_output: &str) {
println!("Test {}...", file_name);
let reg = UserTypeRegistry::default();
let (ast, errors) = icy_board_engine::parser::parse_ast(PathBuf::from(&file_name), input, &reg, Encoding::Utf8, LAST_PPLC);
Expand All @@ -56,7 +56,7 @@ fn run_test(file_name: &str, input: &str, output: &str) {
let (mut ui_connection, connection) = ChannelConnection::create_pair();

let mut state = IcyBoardState::new(bbs, board, node_states, node, Box::new(connection)).await;
let result = Arc::new(tokio::sync::Mutex::new(String::new()));
let result = Arc::new(tokio::sync::Mutex::new(Vec::new()));

let res = result.clone();
let _ = std::thread::Builder::new().name("Terminal update".to_string()).spawn(move || {
Expand All @@ -67,27 +67,28 @@ fn run_test(file_name: &str, input: &str, output: &str) {
break;
};
if size == 0 {
continue;
break;
}
let s = std::str::from_utf8(&buffer[0..size]).unwrap();
res.lock().await.push_str(s);
res.lock().await.extend(&buffer[0..size]);
}
});
});
state.run_executable(&file_name, None, executable.clone()).await.unwrap();
thread::sleep(std::time::Duration::from_millis(10));
thread::sleep(std::time::Duration::from_millis(50));
let x = result.as_ref().lock().await.clone();
x
});

let result = read_data_with_encoding_detection(&result).unwrap();
let result = result.replace("\r\n", "\n");
/*
if result != output {
if result != expected_output {
println!("Input: {}", input);
println!("------");
executable.print_variable_table();
executable.print_disassembler();
}*/
assert_eq!(result, output);
println!("------ Result:");
println!("{}", result);
println!("------ Expected:");
println!("{}", expected_output);
}
assert_eq!(result, expected_output);
}
Err(err) => {
panic!("Error creating executable: {}", err);
Expand Down

0 comments on commit 732f79b

Please sign in to comment.