Compare commits
10 commits
4327869d23
...
82a20c4b87
Author | SHA1 | Date | |
---|---|---|---|
82a20c4b87 | |||
8e9faefc0e | |||
f3b8196ac5 | |||
6b78bb16d8 | |||
ca7a655ebb | |||
007cd0e22a | |||
5fb7f0263a | |||
7d1b87d8df | |||
6afa1f1f3a | |||
97b2c0b6a9 |
8 changed files with 164 additions and 70 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -53,3 +53,4 @@ Mkfile.old
|
||||||
dkms.conf
|
dkms.conf
|
||||||
|
|
||||||
bin/
|
bin/
|
||||||
|
zig-cache/
|
||||||
|
|
|
@ -7,14 +7,10 @@ source is open, data is closed, this is for my own personal use
|
||||||
## depedencies
|
## depedencies
|
||||||
|
|
||||||
- a robot girl to use those utils
|
- a robot girl to use those utils
|
||||||
- posix compliant make
|
- the zig compiler https://ziglang.org
|
||||||
- a c compiler
|
|
||||||
|
|
||||||
```
|
```
|
||||||
make
|
zig build install --prefix ~/.local/
|
||||||
|
|
||||||
# or just remove PREFIX for /usr/local
|
|
||||||
make PREFIX=$HOME/.local install
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## using
|
## using
|
||||||
|
|
31
build.zig
Normal file
31
build.zig
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
const Builder = @import("std").build.Builder;
|
||||||
|
|
||||||
|
pub fn build(b: *Builder) void {
|
||||||
|
const mode = b.standardReleaseOptions();
|
||||||
|
const exe = b.addExecutable("journal", null);
|
||||||
|
exe.setBuildMode(mode);
|
||||||
|
exe.linkSystemLibrary("c");
|
||||||
|
|
||||||
|
const source_files = [_][]const u8{"src/journal/main.c"};
|
||||||
|
|
||||||
|
for (source_files) |source| {
|
||||||
|
exe.addCSourceFile(source, [_][]const u8{"-Wall"});
|
||||||
|
}
|
||||||
|
|
||||||
|
const zig_sources = [_][]const u8{"src/journal/journal.zig"};
|
||||||
|
|
||||||
|
for (zig_sources) |source| {
|
||||||
|
const obj = b.addObject("journal", source);
|
||||||
|
obj.linkSystemLibrary("c");
|
||||||
|
exe.addObject(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
// enable us to run the main journal executable via
|
||||||
|
// the build system
|
||||||
|
const run_cmd = exe.run();
|
||||||
|
const run_step = b.step("run", "Run journal");
|
||||||
|
run_step.dependOn(&run_cmd.step);
|
||||||
|
|
||||||
|
b.default_step.dependOn(&exe.step);
|
||||||
|
b.installArtifact(exe);
|
||||||
|
}
|
|
@ -1,57 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
// functions to deal with the journal file
|
|
||||||
|
|
||||||
|
|
||||||
FILE *journal_open(char *topic)
|
|
||||||
{
|
|
||||||
char *home_path = getenv("HOME");
|
|
||||||
|
|
||||||
// combine w/ home path
|
|
||||||
char *journal_dir = (char*)malloc(1024);
|
|
||||||
snprintf(journal_dir, 1024, "%s/.lunabot", home_path);
|
|
||||||
|
|
||||||
// construct a 744
|
|
||||||
mkdir(journal_dir, S_IRWXU | S_IRGRP | S_IROTH);
|
|
||||||
|
|
||||||
char *journal_path = (char*)malloc(1024);
|
|
||||||
snprintf(journal_path, 1024, "%s/%s", journal_dir, topic);
|
|
||||||
|
|
||||||
FILE* res = fopen(journal_path, "a");
|
|
||||||
|
|
||||||
free(journal_path);
|
|
||||||
free(journal_dir);
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
void journal_write(FILE* journal_fd, char* message)
|
|
||||||
{
|
|
||||||
// TODO: strlen()?
|
|
||||||
fwrite(message, strlen(message), 1, journal_fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
void journal_close(FILE* journal_fd)
|
|
||||||
{
|
|
||||||
fclose(journal_fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
void journal_write_topic(FILE *journal_fd, char *topic, char *message)
|
|
||||||
{
|
|
||||||
char *tstamp = malloc(128 * sizeof(char));
|
|
||||||
char fmt_msg[512];
|
|
||||||
|
|
||||||
time_t rawtime;
|
|
||||||
time(&rawtime);
|
|
||||||
const struct tm *cur_time = gmtime(&rawtime);
|
|
||||||
strftime(tstamp, 128, "%c", cur_time);
|
|
||||||
|
|
||||||
sprintf(fmt_msg, "[%s] [%s]: %s\n", tstamp, topic, message);
|
|
||||||
free(tstamp);
|
|
||||||
|
|
||||||
journal_write(journal_fd, fmt_msg);
|
|
||||||
}
|
|
|
@ -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
|
||||||
|
|
119
src/journal/journal.zig
Normal file
119
src/journal/journal.zig
Normal file
|
@ -0,0 +1,119 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const os = std.os;
|
||||||
|
|
||||||
|
const c = @cImport({
|
||||||
|
@cInclude("stdio.h");
|
||||||
|
@cInclude("stdlib.h");
|
||||||
|
@cInclude("sys/stat.h");
|
||||||
|
@cInclude("string.h");
|
||||||
|
@cInclude("time.h");
|
||||||
|
});
|
||||||
|
|
||||||
|
const warn = std.debug.warn;
|
||||||
|
const allocator = std.heap.c_allocator;
|
||||||
|
|
||||||
|
fn sliceify(ptr: [*]u8) []u8 {
|
||||||
|
return ptr[0..c.strlen(ptr)];
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sliceifyKnown(slice: []u8) []u8 {
|
||||||
|
return slice[0..c.strlen(slice.ptr)];
|
||||||
|
}
|
||||||
|
|
||||||
|
export fn journal_open(topic_cptr: ?[*]u8) os.fd_t {
|
||||||
|
const topic = sliceify(topic_cptr.?);
|
||||||
|
|
||||||
|
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: os.fd_t, message_cptr: ?[*]u8) void {
|
||||||
|
std.os.write(
|
||||||
|
journal,
|
||||||
|
sliceify(message_cptr.?),
|
||||||
|
) catch |err| {
|
||||||
|
std.debug.warn("Error while writing to file: {}\n", err);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export fn journal_close(journal: os.fd_t) void {
|
||||||
|
std.os.close(journal);
|
||||||
|
}
|
||||||
|
|
||||||
|
export fn journal_write_topic(
|
||||||
|
journal: os.fd_t,
|
||||||
|
topic_cptr: ?[*]u8,
|
||||||
|
message_cptr: ?[*]u8,
|
||||||
|
) void {
|
||||||
|
var topic = sliceify(topic_cptr.?);
|
||||||
|
var message = sliceify(message_cptr.?);
|
||||||
|
|
||||||
|
var tstamp_total = allocator.alloc(u8, 128) catch |err| {
|
||||||
|
warn("failed to allocate string for timestamp\n");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
defer allocator.free(tstamp_total);
|
||||||
|
|
||||||
|
var rawtime: c.time_t = undefined;
|
||||||
|
|
||||||
|
_ = c.time(&rawtime);
|
||||||
|
var cur_time: [*c]const c.struct_tm = c.gmtime(&rawtime);
|
||||||
|
_ = c.strftime(tstamp_total.ptr, usize(128), c"%c", cur_time);
|
||||||
|
|
||||||
|
var tstamp = sliceifyKnown(tstamp_total);
|
||||||
|
|
||||||
|
std.debug.warn("tstamp: {}\ntopic: {}\nmsg: {}\n", tstamp, topic, message);
|
||||||
|
|
||||||
|
var fmt_msg = std.fmt.allocPrint(
|
||||||
|
allocator,
|
||||||
|
"[{}] [{}]: {}\n",
|
||||||
|
tstamp,
|
||||||
|
topic,
|
||||||
|
message,
|
||||||
|
) catch |err| {
|
||||||
|
warn("Failed to make formatted message: {}\n", err);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
// catch unreachable is intended.
|
||||||
|
var fmt_msg_cstr = std.cstr.addNullByte(
|
||||||
|
allocator,
|
||||||
|
fmt_msg,
|
||||||
|
) catch unreachable;
|
||||||
|
|
||||||
|
journal_write(journal, fmt_msg_cstr.ptr);
|
||||||
|
}
|
|
@ -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);
|
||||||
|
|
Loading…
Reference in a new issue