Compare commits

..

No commits in common. "5fb7f0263a39b11f27ab3d91144723a27e94b9a7" and "6afa1f1f3a3e6782f1e58557accb71413193d17c" have entirely different histories.

4 changed files with 47 additions and 101 deletions

View file

@ -6,13 +6,13 @@ pub fn build(b: *Builder) void {
exe.setBuildMode(mode);
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| {
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| {
const obj = b.addObject("journal", source);

View file

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

View file

@ -1,6 +1,3 @@
const std = @import("std");
const os = std.os;
const c = @cImport({
@cInclude("stdio.h");
@cInclude("stdlib.h");
@ -9,6 +6,7 @@ const c = @cImport({
@cInclude("time.h");
});
const std = @import("std");
const warn = std.debug.warn;
const allocator = std.heap.c_allocator;
@ -16,101 +14,53 @@ fn alloc_str(size: c_ulong) [*c]u8 {
return @ptrCast([*c]u8, @alignCast(@alignOf(u8), c.malloc(c_ulong(size))));
}
fn free_str(ptr: [*c]u8) void {
c.free(@ptrCast(?*c_void, ptr));
export fn journal_open(topic: [*c]u8) [*c](c.FILE) {
var home_path: [*c]const u8 = c.getenv(c"HOME");
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_open(topic_opt: [*c]u8) os.fd_t {
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_write(journal: [*c]c.FILE, message: [*c]u8) void {
_ = c.fwrite(message, c.strlen(message), 1, journal);
}
export fn journal_write(journal: os.fd_t, message: [*c]u8) void {
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_close(journal: [*c]c.FILE) void {
_ = c.fclose(journal);
}
export fn journal_close(journal: os.fd_t) void {
std.os.close(journal);
}
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);
export fn journal_write_topic(journal: *c.FILE, topic: [*c]u8, message: [*c]u8) void {
var tstamp: [*c]u8 = alloc_str(128);
var fmt_msg: [*c]u8 = alloc_str(128);
var rawtime: c.time_t = undefined;
_ = c.time(&rawtime);
var cur_time: [*c]const c.struct_tm = c.gmtime(&rawtime);
_ = c.strftime(tstamp_cstr, usize(128), c"%c", cur_time);
var tstamp = tstamp_cstr[0..c.strlen(tstamp_cstr)];
_ = c.strftime(tstamp, usize(128), c"%c", cur_time);
_ = c.sprintf(fmt_msg, c"[%s] [%s]: %s\n", tstamp, topic, message);
std.debug.warn("tstamp: {}\ntopic: {}\nmsg: {}\n", tstamp, topic, message);
c.free(@ptrCast(?*c_void, tstamp));
// 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);
journal_write(journal, fmt_msg);
}

View file

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