Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 40 additions & 30 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ pub fn build(b: *std.Build) !void {
"-O2",
});

// The standard library is compiled by a custom tool (mquickjs_build.c) to
// C structures that may reside in ROM. Hence the standard library
// instantiation is very fast and requires almost no RAM. An example of
// standard library for mqjs is provided in mqjs_stdlib.c. The result of
// The standard library is compiled by a custom tool (mquickjs_build.c) to
// C structures that may reside in ROM. Hence the standard library
// instantiation is very fast and requires almost no RAM. An example of
// standard library for mqjs is provided in mqjs_stdlib.c. The result of
// its compilation is mqjs_stdlib.h
const mqjs_stdlib_tool = b.addExecutable(.{
.name = "mqjs_stdlib",
Expand All @@ -62,17 +62,17 @@ pub fn build(b: *std.Build) !void {
.link_libc = true,
}),
});
mqjs_stdlib_tool.addCSourceFiles(.{
mqjs_stdlib_tool.root_module.addCSourceFiles(.{
.files = &.{ "mqjs_stdlib.c", "mquickjs_build.c" },
.flags = cHostFlags.items,
});

// Generate Header Files
const gen_atoms = b.addRunArtifact(mqjs_stdlib_tool);
gen_atoms.addArg("-a");
const mquickjs_atom_h = gen_atoms.captureStdOut();
const mquickjs_atom_h = gen_atoms.captureStdOut(.{});
const gen_stdlib = b.addRunArtifact(mqjs_stdlib_tool);
const mqjs_stdlib_h = gen_stdlib.captureStdOut();
const mqjs_stdlib_h = gen_stdlib.captureStdOut(.{});
const wf = b.addWriteFiles();
_ = wf.addCopyFile(mquickjs_atom_h, "mquickjs_atom.h");
_ = wf.addCopyFile(mqjs_stdlib_h, "mqjs_stdlib.h");
Expand All @@ -89,14 +89,28 @@ pub fn build(b: *std.Build) !void {
});

// Compile the Zig version of readline into an object file
const translate_c = b.addTranslateC(.{
.root_source_file = b.path("translate_c.h"),
.target = target,
.optimize = .Debug,
});
// translate_c.linkSystemLibrary(name: []const u8, options: LinkSystemLibraryOptions)
const readline_obj = b.addObject(.{
.name = "readline",
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.root_source_file = b.path("readline.zig"),
.link_libc = true,
}),
.root_module = b.createModule(
.{
.target = target,
.optimize = optimize,
.root_source_file = b.path("readline.zig"),
.link_libc = true,
.imports = &[_]std.Build.Module.Import{
.{
.name = "translate_c",
.module = translate_c.createModule(),
},
},
},
),
});

// example
Expand All @@ -108,12 +122,12 @@ pub fn build(b: *std.Build) !void {
.link_libc = true,
}),
});
example_stdlib_tool.addCSourceFiles(.{
example_stdlib_tool.root_module.addCSourceFiles(.{
.files = &.{ "example_stdlib.c", "mquickjs_build.c" },
.flags = &.{ "-O2", "-D_GNU_SOURCE" },
});
const gen_example_stdlib = b.addRunArtifact(example_stdlib_tool);
const example_stdlib_h = gen_example_stdlib.captureStdOut();
const example_stdlib_h = gen_example_stdlib.captureStdOut(.{});
const example_exe = b.addExecutable(.{
.name = "example",
.root_module = b.createModule(.{
Expand All @@ -123,12 +137,10 @@ pub fn build(b: *std.Build) !void {
}),
});
_ = wf.addCopyFile(example_stdlib_h, "example_stdlib.h");
example_exe.addConfigHeader(
b.addConfigHeader(.{.style = .blank }, .{})
);
example_exe.addIncludePath(wf.getDirectory());
example_exe.addIncludePath(b.path("."));
example_exe.addCSourceFiles(.{
example_exe.root_module.addConfigHeader(b.addConfigHeader(.{ .style = .blank }, .{}));
example_exe.root_module.addIncludePath(wf.getDirectory());
example_exe.root_module.addIncludePath(b.path("."));
example_exe.root_module.addCSourceFiles(.{
.files = &.{
"example.c",
"mquickjs.c",
Expand All @@ -137,7 +149,7 @@ pub fn build(b: *std.Build) !void {
},
.flags = cFlags.items,
});
example_exe.addObject(cutils_obj);
example_exe.root_module.addObject(cutils_obj);
const build_example_step = b.step("example", "Build example");
const install_example = b.addInstallArtifact(example_exe, .{});
build_example_step.dependOn(&install_example.step);
Expand All @@ -151,7 +163,7 @@ pub fn build(b: *std.Build) !void {
.link_libc = true,
}),
});
exe.addCSourceFiles(.{
exe.root_module.addCSourceFiles(.{
.files = &.{
"mqjs.c",
// "readline_tty.c",
Expand All @@ -163,12 +175,10 @@ pub fn build(b: *std.Build) !void {
.flags = cFlags.items,
});
// exe.addObject(cutils_obj);
exe.addObject(readline_obj);
exe.addConfigHeader(
b.addConfigHeader(.{.style = .blank }, .{})
);
exe.addIncludePath(wf.getDirectory());
exe.addIncludePath(b.path("."));
exe.root_module.addObject(readline_obj);
exe.root_module.addConfigHeader(b.addConfigHeader(.{ .style = .blank }, .{}));
exe.root_module.addIncludePath(wf.getDirectory());
exe.root_module.addIncludePath(b.path("."));
b.installArtifact(exe);

// make test
Expand Down Expand Up @@ -211,4 +221,4 @@ pub fn build(b: *std.Build) !void {
octane_step.dependOn(&run_octane.step);

std.debug.print("Build complete. The executable is located in ./zig-out/bin/\n", .{});
}
}
62 changes: 34 additions & 28 deletions readline_tty.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ const std = @import("std");
const builtin = @import("builtin");
const readline = @import("./readline.zig");
const cutils = @import("./cutils.zig");
const c = @cImport({
@cInclude("stdio.h");
@cInclude("stdarg.h");
});
const c = @import("translate_c");
// const c = @cImport({
// @cInclude("stdio.h");
// @cInclude("stdarg.h");
// });

extern "c" fn printf(fmt: [*c]const u8, ...) c_int;
extern "c" fn atexit(func: ?*const fn () callconv(.c) void) c_int;
Expand Down Expand Up @@ -64,7 +65,7 @@ extern "c" fn _get_osfhandle(fd: c_int) isize;
extern "c" fn _setmode(fd: c_int, mode: c_int) c_int;
extern "system" fn SetConsoleMode(hConsoleHandle: isize, dwMode: u32) c_int;
extern "system" fn GetConsoleMode(hConsoleHandle: isize, lpMode: *u32) c_int;
extern "system" fn SetConsoleCtrlHandler(HandlerRoutine: ?*const fn (u32) callconv(.WINAPI) c_int, Add: c_int) c_int;
extern "system" fn SetConsoleCtrlHandler(HandlerRoutine: ?*const fn (u32) callconv(.winapi) c_int, Add: c_int) c_int;

const COORD = extern struct { X: i16, Y: i16 };
const SMALL_RECT = extern struct { Left: i16, Top: i16, Right: i16, Bottom: i16 };
Expand All @@ -81,9 +82,9 @@ extern "system" fn GetConsoleScreenBufferInfo(hConsoleOutput: isize, lpConsoleSc
const __ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x0004;
const __ENABLE_VIRTUAL_TERMINAL_INPUT: u32 = 0x0200;

pub const ctrl_handler = switch(builtin.os.tag) {
pub const ctrl_handler = switch (builtin.os.tag) {
.windows => (struct {
fn ctrl_handler(dwCtrlType: u32) callconv(.WINAPI) BOOL {
fn ctrl_handler(dwCtrlType: u32) callconv(.winapi) BOOL {
if (dwCtrlType == CTRL_C_EVENT) {
ctrl_c_pressed += 1;
if (ctrl_c_pressed >= 4) {
Expand All @@ -101,13 +102,13 @@ pub const ctrl_handler = switch(builtin.os.tag) {
};

// if processed input is enabled, Ctrl-C is handled by ctrl_handler()
pub const set_processed_input = switch(builtin.os.tag) {
pub const set_processed_input = switch (builtin.os.tag) {
.windows => (struct {
fn set_processed_input(enable: BOOL) void {
var mode: u32 = 0;
const handle = _get_osfhandle(0);
if (GetConsoleMode(handle, &mode) == 0) return;

if (enable != 0) {
mode |= ENABLE_PROCESSED_INPUT;
} else {
Expand All @@ -126,7 +127,7 @@ pub const set_processed_input = switch(builtin.os.tag) {
var oldtty: std.posix.termios = undefined;
var old_fd0_flags: i32 = 0;

pub const term_exit = switch(builtin.os.tag) {
pub const term_exit = switch (builtin.os.tag) {
.windows => @panic("posix only function"),
else => (struct {
fn term_exit() callconv(.c) void {
Expand All @@ -136,7 +137,7 @@ pub const term_exit = switch(builtin.os.tag) {
}).term_exit,
};

pub const sigint_handler = switch(builtin.os.tag) {
pub const sigint_handler = switch (builtin.os.tag) {
.windows => @panic("posix only function"),
else => (struct {
fn sigint_handler(signo: c_int) callconv(.c) void {
Expand All @@ -146,15 +147,15 @@ pub const sigint_handler = switch(builtin.os.tag) {
// just to be able to stop the process if it is hanged
const SIGINT = 2;
const SIG_DFL = 0;
_ = signal(SIGINT, @as(?*const fn(c_int) callconv(.c) void, @ptrFromInt(SIG_DFL)));
_ = signal(SIGINT, @as(?*const fn (c_int) callconv(.c) void, @ptrFromInt(SIG_DFL)));
}
}
}).sigint_handler,
};

// ---------- END posix implementation END ----------

pub export const readline_tty_init = switch(builtin.os.tag) {
pub export const readline_tty_init = switch (builtin.os.tag) {
.windows => (struct {
// windows implementation
fn readline_tty_init() callconv(.c) c_int {
Expand Down Expand Up @@ -193,9 +194,9 @@ pub export const readline_tty_init = switch(builtin.os.tag) {
tty.iflag.IGNCR = false; // Ignore CR.
tty.iflag.ICRNL = false; // Map CR to NL on input.
tty.iflag.IXON = false; // Enable start/stop output control.

tty.oflag.OPOST = true;

tty.lflag.ECHO = false;
tty.lflag.ECHONL = false;
tty.lflag.ICANON = false;
Expand All @@ -222,11 +223,10 @@ pub export const readline_tty_init = switch(builtin.os.tag) {
// fcntl(0, F_SETFL, O_NONBLOCK);
var n_cols: c_int = 80;
var ws: P.winsize = undefined;

if (
P.system.ioctl(0, P.T.IOCGWINSZ, @intFromPtr(&ws)) == 0 and
ws.col >= 4 and ws.row >= 4
) {

if (P.system.ioctl(0, P.T.IOCGWINSZ, @intFromPtr(&ws)) == 0 and
ws.col >= 4 and ws.row >= 4)
{
n_cols = ws.col;
}
return n_cols;
Expand All @@ -235,14 +235,17 @@ pub export const readline_tty_init = switch(builtin.os.tag) {
};

pub fn term_printf(fmt: [*c]const u8, ...) callconv(.c) void {
var ap = @cVaStart();
const c_ap: [*c]c.struct___va_list_tag_1 = @ptrCast(&ap);
_ = c.vprintf(fmt, c_ap);
@cVaEnd(&ap);
// var ap = @cVaStart();
// const c_ap: [*c]c.struct___va_list_tag_1 = @ptrCast(&ap);
// _ = c.vprintf(fmt, c_ap);
// @cVaEnd(&ap);

const str = std.mem.span(fmt);
std.debug.print("{s}", .{str});
}

pub fn term_flush() void {
_= c.fflush(c.stdout);
_ = c.fflush(null);
}

export fn readline_tty(s: *ReadlineState, prompt: [*c]const u8, multi_line: BOOL) [*c]const u8 {
Expand All @@ -260,13 +263,16 @@ export fn readline_tty(s: *ReadlineState, prompt: [*c]const u8, multi_line: BOOL
var exit_loop = false;

while (ret_str == null and !exit_loop) {
const len_result = std.posix.read(0, &buf) catch 0;
const len_result = switch (builtin.os.tag) {
.windows => std.c.read(std.Io.File.stdin().handle, &buf, buf.len),
else => std.posix.read(0, &buf) catch 0,
};
if (len_result == 0) break;

var i: usize = 0;
while (i < len_result) : (i += 1) {
const ch: c_int = buf[i];

if (builtin.os.tag == .windows and ch == 3) {
// ctrl-C
ctrl_c_pressed += 1;
Expand Down Expand Up @@ -309,4 +315,4 @@ export fn readline_is_interrupted() BOOL {
const ret: BOOL = if (ctrl_c_pressed != 0) TRUE else FALSE;
ctrl_c_pressed = 0;
return ret;
}
}
2 changes: 2 additions & 0 deletions translate_c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include <stdio.h>
#include <stdarg.h>