Skip to content

Commit

Permalink
[#50296] Improve C macro definitions, represent TcsetattrAction as …
Browse files Browse the repository at this point in the history
…`i32` primitive, modify `build.rs` script
  • Loading branch information
k-juszczyk committed Nov 24, 2023
1 parent 5a74589 commit 5175705
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 30 deletions.
15 changes: 3 additions & 12 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ const CLIB_DIR: &str = "c_lib";
const CLIB_THIRD_PARTY_DIR: &str = "c_lib/third_party";

fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed={CLIB_THIRD_PARTY_DIR}/termios/termios.c");
println!("cargo:rerun-if-changed={CLIB_THIRD_PARTY_DIR}/termios/termios.h");
println!("cargo:rerun-if-changed={CLIB_THIRD_PARTY_DIR}/termios/*");
println!("cargo:rerun-if-changed={CLIB_DIR}/wasi_ext_lib.c");
println!("cargo:rerun-if-changed={CLIB_DIR}/wasi_ext_lib.h");
println!("cargo:rerun-if-changed={CLIB_DIR}/Makefile");
let mut make = Command::new("make");
#[cfg(feature = "hterm")]
make.arg("CFLAGS=-DHTERM");
Expand All @@ -32,12 +25,10 @@ fn main() {
println!("cargo:rustc-link-search={CLIB_DIR}/bin/");

println!("cargo:rustc-link-lib=static=wasi_ext_lib");
println!("cargo:rerun-if-changed={CLIB_THIRD_PARTY_DIR}/termios/*");
println!("cargo:rerun-if-changed={CLIB_THIRD_PARTY_DIR}/termios/termios.h");
println!("cargo:rerun-if-changed={CLIB_THIRD_PARTY_DIR}/termios/termios.c");
println!("cargo:rerun-if-changed={CLIB_DIR}/wasi_ext_lib.h");
println!("cargo:rerun-if-changed={CLIB_DIR}");
println!("cargo:rerun-if-changed={CLIB_THIRD_PARTY_DIR}/termios");
println!("cargo:rerun-if-changed={CLIB_THIRD_PARTY_DIR}/termios/bits");
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed={CLIB_DIR}/wasi_ext_lib.c");
println!("cargo:rerun-if-changed=src/lib.rs");

let mut bgen;
Expand Down
15 changes: 7 additions & 8 deletions c_lib/wasi_ext_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
#define _MAX_FD_NUM 1024

// Extended fs_fdflags
#define WASI_EXT_FDFLAG_CTRL_BIT (__wasi_fdflags_t)0x0020
#define WASI_EXT_FDFLAG_MASK (__wasi_fdflags_t)0xffc0
#define WASI_EXT_FDFLAG_CLOEXEC (__wasi_fdflags_t)0x0040
#define WASI_EXT_FDFLAG_CTRL_BIT ((__wasi_fdflags_t)0x0020)
#define WASI_EXT_FDFLAG_MASK ((__wasi_fdflags_t)0xffc0)
#define WASI_EXT_FDFLAG_CLOEXEC ((__wasi_fdflags_t)0x0040)

// Fnctl commands
enum FcntlCommand { F_MVFD, F_GETFD, F_SETFD };
Expand All @@ -57,7 +57,6 @@ enum RedirectType {
DUPLICATE,
CLOSE
};

struct Redirect {
union Data {
struct Path {
Expand All @@ -79,11 +78,11 @@ struct Env {

#ifdef HTERM
typedef uint32_t WasiEvents;
#define WASI_EVENTS_NUM (size_t)2
#define WASI_EVENTS_MASK_SIZE (size_t)4 // number of bytes
#define WASI_EVENTS_NUM ((size_t)2)
#define WASI_EVENTS_MASK_SIZE ((size_t)4) // number of bytes
// Hterm events
#define WASI_EVENT_WINCH (WasiEvents)(1 << 0)
#define WASI_EVENT_SIGINT (WasiEvents)(1 << 1)
#define WASI_EVENT_WINCH ((WasiEvents)(1 << 0))
#define WASI_EVENT_SIGINT ((WasiEvents)(1 << 1))
#endif

int wasi_ext_chdir(const char *);
Expand Down
19 changes: 9 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ pub enum Redirect {
Close(Fd),
}

#[repr(i32)]
pub enum TcsetattrAction {
TCSANOW,
TCSADRAIN,
TCSAFLUSH,
TCSANOW = termios::TCSANOW as i32,
TCSADRAIN = termios::TCSADRAIN as i32,
TCSAFLUSH = termios::TCSAFLUSH as i32,
}

impl From<&Redirect> for wasi_ext_lib_generated::Redirect {
Expand Down Expand Up @@ -432,14 +433,12 @@ pub fn tcsetattr(
act: TcsetattrAction,
termios_p: &termios::termios,
) -> Result<(), ExitCode> {
let act_num = match act {
TcsetattrAction::TCSANOW => termios::TCSANOW,
TcsetattrAction::TCSADRAIN => termios::TCSADRAIN,
TcsetattrAction::TCSAFLUSH => termios::TCSAFLUSH,
} as c_int;

let result = unsafe {
termios::wasi_ext_tcsetattr(fd as c_int, act_num, termios_p as *const termios::termios)
termios::wasi_ext_tcsetattr(
fd as c_int,
act as c_int,
termios_p as *const termios::termios,
)
};

if result == 0 {
Expand Down

0 comments on commit 5175705

Please sign in to comment.