ADDED: working_directory/2, addressing a remaining aspect of #511

This commit is contained in:
Markus Triska
2020-07-14 22:37:05 +02:00
parent 5134e64cae
commit 94e9e17c79
3 changed files with 38 additions and 1 deletions

View File

@@ -177,6 +177,7 @@ pub enum SystemClauseType {
DirectoryExists,
MakeDirectory,
DeleteFile,
WorkingDirectory,
DeleteAttribute,
DeleteHeadAttribute,
DynamicModuleResolution(usize),
@@ -343,6 +344,7 @@ impl SystemClauseType {
&SystemClauseType::DirectoryExists => clause_name!("$directory_exists"),
&SystemClauseType::MakeDirectory => clause_name!("$make_directory"),
&SystemClauseType::DeleteFile => clause_name!("$delete_file"),
&SystemClauseType::WorkingDirectory => clause_name!("$working_directory"),
&SystemClauseType::REPL(REPLCodePtr::CompileBatch) => clause_name!("$compile_batch"),
&SystemClauseType::REPL(REPLCodePtr::UseModule) => clause_name!("$use_module"),
&SystemClauseType::REPL(REPLCodePtr::UseQualifiedModule) => {
@@ -678,6 +680,7 @@ impl SystemClauseType {
("$directory_exists", 1) => Some(SystemClauseType::DirectoryExists),
("$make_directory", 1) => Some(SystemClauseType::MakeDirectory),
("$delete_file", 1) => Some(SystemClauseType::DeleteFile),
("$working_directory", 2) => Some(SystemClauseType::WorkingDirectory),
("$use_module", 1) => Some(SystemClauseType::REPL(REPLCodePtr::UseModule)),
("$use_module_from_file", 1) =>
Some(SystemClauseType::REPL(REPLCodePtr::UseModuleFromFile)),

View File

@@ -51,7 +51,8 @@
file_exists/1,
directory_exists/1,
delete_file/1,
make_directory/1]).
make_directory/1,
working_directory/2]).
:- use_module(library(error)).
:- use_module(library(lists)).
@@ -85,3 +86,8 @@ make_directory(Directory) :-
delete_file(File) :-
list_of_chars(File),
'$delete_file'(File).
working_directory(Dir0, Dir) :-
can_be(list, Dir0),
can_be(list, Dir),
'$working_directory'(Dir0, Dir).

View File

@@ -890,6 +890,34 @@ impl MachineState {
}
}
}
&SystemClauseType::WorkingDirectory => {
if let Ok(dir) = env::current_dir() {
let current =
match dir.to_str() {
Some(d) => { d }
_ => { let stub = MachineError::functor_stub(clause_name!("working_directory"), 2);
let err = MachineError::representation_error(RepFlag::Character);
let err = self.error_form(err, stub);
return Err(err);
}
};
let chars = self.heap.put_complete_string(current);
self.unify(self[temp_v!(1)], chars);
let next = self.heap_pstr_iter(self[temp_v!(2)]).to_string();
match env::set_current_dir(std::path::Path::new(&next)) {
Ok(_) => { }
_ => { self.fail = true;
return Ok(());
}
}
} else {
self.fail = true;
return Ok(());
}
}
&SystemClauseType::AtEndOfExpansion => {
if self.cp == LocalCodePtr::TopLevel(0, 0) {
self.at_end_of_expansion = true;