Compare commits

...

2 commits

Author SHA1 Message Date
5fb7f0263a remove total reliance on the c stdlib for journal.zig 2019-07-13 11:18:57 -03:00
7d1b87d8df fixes for latest zig 2019-07-13 09:55:01 -03:00
4 changed files with 101 additions and 47 deletions

View file

@ -6,13 +6,13 @@ pub fn build(b: *Builder) void {
exe.setBuildMode(mode); exe.setBuildMode(mode);
exe.linkSystemLibrary("c"); exe.linkSystemLibrary("c");
const source_files = [][]const u8{"src/journal/main.c"}; const source_files = [_][]const u8{"src/journal/main.c"};
for (source_files) |source| { for (source_files) |source| {
exe.addCSourceFile(source, [][]const u8{"-W -O"}); exe.addCSourceFile(source, [_][]const u8{"-W -O"});
} }
const zig_sources = [][]const u8{"src/journal/journal.zig"}; const zig_sources = [_][]const u8{"src/journal/journal.zig"};
for (zig_sources) |source| { for (zig_sources) |source| {
const obj = b.addObject("journal", source); const obj = b.addObject("journal", source);

View file

@ -8,9 +8,9 @@
#define STREQ(a, b) strcmp(a, b) == 0 #define STREQ(a, b) strcmp(a, b) == 0
FILE *journal_open(char *topic); int journal_open(char *topic);
void journal_write(FILE*, char* message); void journal_write(int, char* message);
void journal_write_topic(FILE*, char *topic, char *message); void journal_write_topic(int, char *topic, char *message);
void journal_close(FILE*); void journal_close(int);
#endif #endif

View file

@ -1,3 +1,6 @@
const std = @import("std");
const os = std.os;
const c = @cImport({ const c = @cImport({
@cInclude("stdio.h"); @cInclude("stdio.h");
@cInclude("stdlib.h"); @cInclude("stdlib.h");
@ -6,7 +9,6 @@ const c = @cImport({
@cInclude("time.h"); @cInclude("time.h");
}); });
const std = @import("std");
const warn = std.debug.warn; const warn = std.debug.warn;
const allocator = std.heap.c_allocator; const allocator = std.heap.c_allocator;
@ -14,53 +16,101 @@ fn alloc_str(size: c_ulong) [*c]u8 {
return @ptrCast([*c]u8, @alignCast(@alignOf(u8), c.malloc(c_ulong(size)))); return @ptrCast([*c]u8, @alignCast(@alignOf(u8), c.malloc(c_ulong(size))));
} }
export fn journal_open(topic: [*c]u8) [*c](c.FILE) { fn free_str(ptr: [*c]u8) void {
var home_path: [*c]const u8 = c.getenv(c"HOME"); c.free(@ptrCast(?*c_void, ptr));
var journal_dir: [*c]u8 = alloc_str(512);
var journal_path: [*c]u8 = alloc_str(1024);
_ = c.snprintf(journal_dir, c_ulong(512), c"%s/.lunabot", home_path);
warn("uwu journal_dir = '{}'", journal_dir.*);
//c.mkdir(journal_dir, c.S_IRWXU | c.S_IRGRP | c.S_IROTH);
// taken off `zig translate-c`. this is cursed, i'm sorry.
_ = c.mkdir(journal_dir, c.__mode_t((((256 | 128) | 64) | (256 >> @import("std").math.Log2Int(c_int)(3))) | ((256 >> @import("std").math.Log2Int(c_int)(3)) >> @import("std").math.Log2Int(c_int)(3))));
_ = c.snprintf(journal_path, c_ulong(1024), c"%s/%s", journal_dir, topic);
warn("journal_path = {}", journal_path.*);
var res: [*c](c.FILE) = c.fopen(journal_path, c"a");
c.free(@ptrCast(?*c_void, journal_path));
c.free(@ptrCast(?*c_void, journal_dir));
return res;
} }
export fn journal_write(journal: [*c]c.FILE, message: [*c]u8) void { export fn journal_open(topic_opt: [*c]u8) os.fd_t {
_ = c.fwrite(message, c.strlen(message), 1, journal); const topic_len = c.strlen(topic_opt);
const topic: []u8 = topic_opt[0..topic_len];
const journal_dir = std.fs.path.resolve(allocator, [_][]const u8{
std.os.getenvC(c"HOME").?,
".lunabot",
}) catch |err| {
warn("failed to resolve paths: {}\n", err);
return -1;
};
warn("journal_dir = {}\n", journal_dir);
std.fs.makeDir(journal_dir) catch |err| {
if (err != error.PathAlreadyExists) {
warn("failed to create journal dir {}: {}\n", journal_dir, err);
return -1;
}
};
const journal_path = std.fmt.allocPrint(allocator, "{}/{}", journal_dir, topic) catch |err| {
warn("failed to allocprint journal path: {}\n", err);
return -1;
};
defer allocator.free(journal_path);
warn("journal_path = {}\n", journal_path);
const fd = std.os.open(
journal_path,
std.os.O_WRONLY | std.os.O_CREAT | std.os.O_APPEND,
0o755,
) catch |err| {
warn("failed to open {}: {}\n", journal_path, err);
return -1;
};
return fd;
} }
export fn journal_close(journal: [*c]c.FILE) void { export fn journal_write(journal: os.fd_t, message: [*c]u8) void {
_ = c.fclose(journal); const msglen = c.strlen(message);
std.os.write(journal, message[0..msglen]) catch |err| {
std.debug.warn("Error while writing to file: {}\n", err);
return;
};
} }
export fn journal_write_topic(journal: *c.FILE, topic: [*c]u8, message: [*c]u8) void { export fn journal_close(journal: os.fd_t) void {
var tstamp: [*c]u8 = alloc_str(128); std.os.close(journal);
var fmt_msg: [*c]u8 = alloc_str(128); }
export fn journal_write_topic(
journal: os.fd_t,
topic_opt: [*c]u8,
message_opt: [*c]u8,
) void {
var topic = topic_opt[0..c.strlen(topic_opt)];
var message = message_opt[0..c.strlen(message_opt)];
var tstamp_cstr: [*c]u8 = alloc_str(128);
var rawtime: c.time_t = undefined; var rawtime: c.time_t = undefined;
_ = c.time(&rawtime); _ = c.time(&rawtime);
var cur_time: [*c]const c.struct_tm = c.gmtime(&rawtime); var cur_time: [*c]const c.struct_tm = c.gmtime(&rawtime);
_ = c.strftime(tstamp_cstr, usize(128), c"%c", cur_time);
_ = c.strftime(tstamp, usize(128), c"%c", cur_time); var tstamp = tstamp_cstr[0..c.strlen(tstamp_cstr)];
_ = c.sprintf(fmt_msg, c"[%s] [%s]: %s\n", tstamp, topic, message);
c.free(@ptrCast(?*c_void, tstamp)); std.debug.warn("tstamp: {}\ntopic: {}\nmsg: {}\n", tstamp, topic, message);
journal_write(journal, fmt_msg); // catch unreachable is intended.
var fmt_msg = std.fmt.allocPrint(
allocator,
"[{}] [{}]: {}\n",
tstamp,
topic,
message,
) catch |err| {
warn("Failed to make formatted message: {}\n", err);
return;
};
var fmt_msg_cstr = std.cstr.addNullByte(
allocator,
fmt_msg,
) catch unreachable;
free_str(tstamp_cstr);
journal_write(journal, fmt_msg_cstr.ptr);
} }

View file

@ -63,7 +63,7 @@ int main(int argc, char** argv)
// default handling by journal_write_topic is marked // default handling by journal_write_topic is marked
// as the NULL values in this array. // as the NULL values in this array.
void (*handlers[])(FILE*, char*) = { void (*handlers[])(int, char*) = {
NULL, NULL, NULL NULL, NULL, NULL
}; };
@ -91,9 +91,13 @@ int main(int argc, char** argv)
if(strcmp(topic, cur_topic) == 0) if(strcmp(topic, cur_topic) == 0)
{ {
void (*fun_ptr)(FILE*, char*) = handlers[i]; void (*fun_ptr)(int, char*) = handlers[i];
FILE* journal_file = journal_open(topic); int journal_file = journal_open(topic);
if(journal_file == -1) {
printf("failed to open journal file.\n");
return 1;
}
char *handler_message = extract_handler_msg(argc, argv); char *handler_message = extract_handler_msg(argc, argv);
printf("[%s] said '%s'\n", topic, handler_message); printf("[%s] said '%s'\n", topic, handler_message);