Skip to content

Commit

Permalink
Add glkunix_set_base_file so that Hugo can compile
Browse files Browse the repository at this point in the history
  • Loading branch information
curiousdannii committed Mar 5, 2024
1 parent 6d89f6c commit 393a215
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 16 deletions.
32 changes: 19 additions & 13 deletions remglk/src/glkapi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod windows;
use std::cmp::min;
use std::mem;
use std::ops::DerefMut;
use std::path;
use std::str;
use std::time::SystemTime;

Expand Down Expand Up @@ -66,13 +67,15 @@ where S: Default + GlkSystem {
timer: TimerData,
pub windows: GlkObjectStore<Window>,
windows_changed: bool,
pub working_directory: path::PathBuf,
}

impl<S> GlkApi<S>
where S: Default + GlkSystem {
pub fn new(system: S) -> Self {
GlkApi {
system,
working_directory: S::working_directory(),
..Default::default()
}
}
Expand Down Expand Up @@ -189,16 +192,18 @@ where S: Default + GlkSystem {
self.exited = true;
let update = self.update();
self.system.send_glkote_update(update);
// TODO: remove once we're no longer using glkote-term
self.system.send_glkote_update(Update::Exit);
}

pub fn glk_fileref_create_by_name(&mut self, usage: u32, filename: String, rock: u32) -> GlkFileRef {
let filetype = file_type(usage & fileusage_TypeMask);
self.create_fileref(clean_filename(filename, filetype), rock, usage, None)
self.create_fileref(FileRefResponse::Name(clean_filename(filename, filetype)), rock, usage)
}

// For glkunix_stream_open_pathname
pub fn glk_fileref_create_by_name_uncleaned(&mut self, usage: u32, filename: String, rock: u32) -> GlkFileRef {
self.create_fileref(filename, rock, usage, None)
self.create_fileref(FileRefResponse::Name(filename), rock, usage)
}

pub fn glk_fileref_create_by_prompt(&mut self, usage: u32, fmode: FileMode, rock: u32) -> GlkResult<Option<GlkFileRef>> {
Expand All @@ -216,11 +221,7 @@ where S: Default + GlkSystem {
if let Some(event) = event {
let res = self.handle_event(event)?;
if let Some(fref) = res.fref {
let (filename, fref) = match fref {
FileRefResponse::String(filename) => (filename, None),
FileRefResponse::Fref(fref) => (fref.filename.clone(), Some(fref)),
};
return Ok(Some(self.create_fileref(filename, rock, usage, fref)));
return Ok(Some(self.create_fileref(fref, rock, usage)));
}
}
else {
Expand All @@ -231,13 +232,13 @@ where S: Default + GlkSystem {

pub fn glk_fileref_create_from_fileref(&mut self, usage: u32, fileref: &GlkFileRef, rock: u32) -> GlkFileRef {
let fileref = lock!(fileref);
self.create_fileref(fileref.system_fileref.filename.clone(), rock, usage, None)
self.create_fileref(FileRefResponse::Fref(fileref.system_fileref.clone()), rock, usage)
}

pub fn glk_fileref_create_temp(&mut self, usage: u32, rock: u32) -> GlkFileRef {
let filetype = file_type(usage & fileusage_TypeMask);
let system_fileref = self.system.fileref_temporary(filetype);
self.create_fileref(system_fileref.filename.clone(), rock, usage, Some(system_fileref))
self.create_fileref(FileRefResponse::Fref(system_fileref), rock, usage)
}

pub fn glk_fileref_delete_file(&mut self, fileref: &GlkFileRef) {
Expand Down Expand Up @@ -1227,10 +1228,15 @@ where S: Default + GlkSystem {

// Internal functions

fn create_fileref(&mut self, filename: String, rock: u32, usage: u32, system_fileref: Option<SystemFileRef>) -> GlkFileRef {
let system_fileref = system_fileref.unwrap_or_else(|| {
self.system.fileref_construct(filename, file_type(usage & fileusage_TypeMask), None)
});
fn create_fileref(&mut self, fileref: FileRefResponse, rock: u32, usage: u32) -> GlkFileRef {
let mut system_fileref = match fileref {
FileRefResponse::Name(filename) => self.system.fileref_construct(filename, file_type(usage & fileusage_TypeMask), None),
FileRefResponse::Fref(fref) => fref,
};

// Set all paths relative to the working directory.
let path = self.working_directory.join(system_fileref.filename);
system_fileref.filename = path.to_str().unwrap().to_owned();

let fref = FileRef::new(system_fileref, usage);
let fref_glkobj = GlkObject::new(fref);
Expand Down
4 changes: 3 additions & 1 deletion remglk/src/glkapi/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub struct SpecialEvent {
#[derive(Deserialize)]
#[serde(untagged)]
pub enum FileRefResponse {
String(String),
Name(String),
Fref(SystemFileRef),
}

Expand Down Expand Up @@ -392,6 +392,8 @@ impl From<Metrics> for GlkResult<'static, NormalisedMetrics> {
#[serde(tag = "type")]
pub enum Update {
Error(ErrorUpdate),
// TODO: remove once we're no longer using glkote-term
Exit,
Pass(PassUpdate),
Retry(RetryUpdate),
#[serde(rename = "update")]
Expand Down
4 changes: 4 additions & 0 deletions remglk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ https://github.com/curiousdannii/remglk-rs
pub mod blorb;
pub mod glkapi;

use std::path::PathBuf;

use glkapi::constants::*;
use glkapi::protocol::{Event, SystemFileRef, Update};

Expand All @@ -30,4 +32,6 @@ pub trait GlkSystem {
fn send_glkote_update(&mut self, update: Update);
/** Get an event from GlkOte */
fn get_glkote_event(&mut self) -> Option<Event>;

fn working_directory() -> PathBuf;
}
4 changes: 4 additions & 0 deletions remglk_capi/src/glk/gi_dispa.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
distributed under the MIT license; see the "LICENSE" file.
*/

#include <stdint.h>

/* These constants define the classes of opaque objects. It's a bit ugly
to put them in this header file, since more classes may be added in
the future. But if you find yourself stuck with an obsolete version
Expand Down Expand Up @@ -52,6 +54,8 @@ typedef struct gidispatch_intconst_struct {
typedef union glk_objrock_union {
glui32 num;
void *ptr;
// Add a u64 dummy variant to work around https://github.com/rust-lang/rust/issues/121408
int64_t dummy_variant;
} gidispatch_rock_t;

/* The following functions are part of the Glk library itself, not the dispatch
Expand Down
9 changes: 9 additions & 0 deletions remglk_capi/src/glkstart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ https://github.com/curiousdannii/remglk-rs

use std::env;
use std::ffi::{c_char, c_int, CStr, CString};
use std::path;
use std::slice;
use std::str;

Expand Down Expand Up @@ -199,6 +200,14 @@ unsafe fn glkunix_arguments() -> Vec<GlkUnixArgument> {
.collect()
}

#[no_mangle]
pub extern "C" fn glkunix_set_base_file(filename_ptr: *const c_char) {
let filename = unsafe {CStr::from_ptr(filename_ptr)}.to_str().unwrap();
let mut path = path::PathBuf::from(filename);
path.pop();
glkapi().lock().unwrap().working_directory = path;
}

#[no_mangle]
pub extern "C" fn glkunix_stream_open_pathname(filename_ptr: *const i8, textmode: u32, rock: u32) -> StreamPtr {
// Remglk banned this from being used except during glkunix_startup_code, but I don't think that's really necessary
Expand Down
1 change: 1 addition & 0 deletions remglk_capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ extern "C" fn main(argc: c_int, argv: *const *const c_char) -> c_int {
args: processed_args.iter().map(|arg| arg.as_ptr()).collect::<Vec<*const c_char>>().as_ptr(),
})} == 0 {
glk_exit();
return 0;
}

// Wait for the initial event with the metrics
Expand Down
6 changes: 6 additions & 0 deletions remglk_capi/src/systems/emglken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ https://github.com/curiousdannii/remglk-rs

use std::collections::HashMap;
use std::mem::MaybeUninit;
use std::path::PathBuf;
use std::slice;

use serde::de::DeserializeOwned;
Expand Down Expand Up @@ -118,6 +119,11 @@ impl GlkSystem for EmglkenSystem {
let json = serde_json::to_string(&update).unwrap();
unsafe {emglken_send_glkote_update(json.as_ptr(), json.len())};
}

fn working_directory() -> PathBuf {
// TODO: do something better here when we can do it reliably in both Node and browser
PathBuf::new()
}
}

fn buffer_to_boxed_slice(buffer: MaybeUninit<EmglkenBuffer>) -> Box<[u8]> {
Expand Down
8 changes: 6 additions & 2 deletions remglk_capi/src/systems/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ https://github.com/curiousdannii/remglk-rs
*/

use std::collections::HashMap;
use std::env::temp_dir;
use std::env;
use std::fs;
use std::io::{self, BufRead};
use std::path::Path;
Expand Down Expand Up @@ -66,7 +66,7 @@ impl GlkSystem for StandardSystem {
fn fileref_temporary(&mut self, filetype: FileType) -> SystemFileRef {
let filename = format!("remglktempfile-{}", self.tempfile_counter);
self.tempfile_counter += 1;
let path = temp_dir().join(filename);
let path = env::temp_dir().join(filename);
SystemFileRef {
filename: path.to_str().unwrap().to_string(),
usage: Some(filetype),
Expand Down Expand Up @@ -104,4 +104,8 @@ impl GlkSystem for StandardSystem {
let output = serde_json::to_string(&update).unwrap();
println!("{}", output);
}

fn working_directory() -> std::path::PathBuf {
env::current_dir().unwrap()
}
}

0 comments on commit 393a215

Please sign in to comment.