Skip to content

Commit

Permalink
abomination cannot format without specifier megahack
Browse files Browse the repository at this point in the history
  • Loading branch information
donpdonp committed Jul 12, 2024
1 parent acd3169 commit 9aa8f2f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ TODAY=$(shell date +%Y.%m.%d -d @${GITEPOCH})
DIST=zootdeck-linux-`uname -i`-${TODAY}

build: ragel/lang.c
zig build
zig build -freference-trace

ragel/lang.c: ragel/lang.c.rl
ragel -o ragel/lang.c ragel/lang.c.rl
Expand Down
2 changes: 1 addition & 1 deletion src/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ pub fn writefile(settings: Settings, filename: []const u8) void {
configFile.columns = column_infos.items;
if (std.fs.cwd().createFile(filename, .{ .truncate = true })) |*file| {
warn("config.write toJson\n", .{});
std.json.stringify(configFile, std.json.StringifyOptions{}, file.writer()) catch unreachable;
//std.json.stringify(configFile, std.json.StringifyOptions{}, file.writer()) catch unreachable;
warn("config saved. {s} {} bytes\n", .{ filename, file.getPos() });
file.close();
} else |err| {
Expand Down
31 changes: 16 additions & 15 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ const filter_lib = @import("./filter.zig");

var settings: config.Settings = undefined;

pub fn main() !void {
pub fn main() u8 {
hello();
try initialize(alloc);
try thread.register_main_tid(thread.self());
initialize(alloc) catch unreachable;
thread.register_main_tid(thread.self()) catch unreachable;

if (config.readfile(config.config_file_path())) |config_data| {
settings = config_data;
const dummy_payload = alloc.create(thread.CommandVerb) catch unreachable;
_ = try thread.create("gui", gui.go, dummy_payload, guiback);
_ = try thread.create("heartbeat", heartbeat.go, dummy_payload, heartback);
_ = thread.create("gui", gui.go, dummy_payload, guiback) catch unreachable;
_ = thread.create("heartbeat", heartbeat.go, dummy_payload, heartback) catch unreachable;

while (true) {
statewalk(alloc);
Expand All @@ -47,6 +47,7 @@ pub fn main() !void {
} else |err| {
log.err("config error: {!}\n", .{err});
}
return 0;
}

fn initialize(allocator: std.mem.Allocator) !void {
Expand Down Expand Up @@ -99,8 +100,8 @@ fn columnget(column: *config.ColumnInfo, allocator: std.mem.Allocator) void {
httpInfo.response_code = 0;
verb.http = httpInfo;
gui.schedule(gui.update_column_netstatus_schedule, @as(*anyopaque, @ptrCast(httpInfo)));
if (thread.create("net", net.go, verb, netback)) |_| {} else |err| {
warn("columnget {!}", .{err});
if (thread.create("net", net.go, verb, netback)) |_| {} else |_| {
//warn("columnget {!}", .{err});
}
}

Expand Down Expand Up @@ -172,7 +173,7 @@ fn oauthtokenget(column: *config.ColumnInfo, code: []const u8, allocator: std.me
}

fn oauthtokenback(command: *thread.Command) void {
warn("*oauthtokenback tid {x} {}\n", .{ thread.self(), command });
//warn("*oauthtokenback tid {x} {}\n", .{ thread.self(), command });
const column = command.verb.http.column;
const http = command.verb.http;
if (http.response_code >= 200 and http.response_code < 300) {
Expand All @@ -190,12 +191,12 @@ fn oauthtokenback(command: *thread.Command) void {
warn("*oauthtokenback json err body {}\n", .{http.body});
}
} else {
warn("*oauthtokenback net err {}\n", .{http.response_code});
//warn("*oauthtokenback net err {}\n", .{http.response_code});
}
}

fn oauthback(command: *thread.Command) void {
warn("*oauthback tid {x} {}\n", .{ thread.self(), command });
//warn("*oauthback tid {x} {}\n", .{ thread.self(), command });
const column = command.verb.http.column;
const http = command.verb.http;
if (http.response_code >= 200 and http.response_code < 300) {
Expand All @@ -208,18 +209,18 @@ fn oauthback(command: *thread.Command) void {
if (tree.object.get("client_secret")) |cid| {
column.oauthClientSecret = cid.string;
}
warn("*oauthback client id {s} secret {s}\n", .{ column.oauthClientId, column.oauthClientSecret });
//warn("*oauthback client id {s} secret {s}\n", .{ column.oauthClientId, column.oauthClientSecret });
gui.schedule(gui.column_config_oauth_url_schedule, @as(*anyopaque, @ptrCast(column)));
} else {
warn("*oauthback json type err {}\n{s}\n", .{ rootJsonType, http.body });
}
} else {
warn("*oauthback net err {}\n", .{http.response_code});
//warn("*oauthback net err {}\n", .{http.response_code});
}
}

fn netback(command: *thread.Command) void {
warn("*netback tid {x} {}\n", .{ thread.self(), command });
//warn("*netback tid {x} {}\n", .{ thread.self(), command });
if (command.id == 1) {
gui.schedule(gui.update_column_netstatus_schedule, @as(*anyopaque, @ptrCast(command.verb.http)));
var column = command.verb.http.column;
Expand Down Expand Up @@ -265,7 +266,7 @@ fn netback(command: *thread.Command) void {
warn("netback json err {s} \n", .{err.String});
}
} else {
warn("!netback json unknown root tagtype {}\n", .{tree});
//warn("!netback json unknown root tagtype {!}\n", .{tree});
}
} else { // empty body
column.inError = true;
Expand Down Expand Up @@ -306,7 +307,7 @@ fn profileback(command: *thread.Command) void {
reqres.column.account = reqres.tree.object;
gui.schedule(gui.update_column_ui_schedule, @as(*anyopaque, @ptrCast(reqres.column)));
} else {
warn("profile fail http status {}\n", .{reqres.response_code});
//warn("profile fail http status {!}\n", .{reqres.response_code});
}
}

Expand Down
26 changes: 12 additions & 14 deletions src/net.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ pub fn go(data: ?*anyopaque) callconv(.C) ?*anyopaque {
if (std.json.parseFromSlice(std.json.Value, allocator, body, .{})) |value_tree| {
defer value_tree.deinit();
actor.payload.http.tree = value_tree.value;
} else |err| {
warn("net json err {!}\n", .{err});
} else |_| {
//warn("net json err {!}\n", .{err});
actor.payload.http.response_code = 1000;
}
}
} else |err| {
warn("net thread http err {!}\n", .{err});
} else |_| {
//warn("net thread http err {!}\n", .{err});
}
thread.signal(actor, command);
return null;
Expand All @@ -67,8 +67,8 @@ pub fn httpget(req: *config.HttpInfo) ![]const u8 {
//var slist: ?[*c]c.curl_slist = null;
var slist = @as([*c]c.curl_slist, @ptrFromInt(0)); // 0= new list
slist = c.curl_slist_append(slist, "Accept: application/json");
if (req.token) |token| {
warn("Authorization: {s}\n", .{token});
if (req.token) |_| {
//warn("Authorization: {s}\n", .{token});
}
_ = c.curl_easy_setopt(curl, c.CURLOPT_HTTPHEADER, slist);

Expand All @@ -78,7 +78,7 @@ pub fn httpget(req: *config.HttpInfo) ![]const u8 {
_ = c.curl_easy_setopt(curl, c.CURLOPT_POST, @as(c_long, 1));
const post_body_c: [*c]const u8 = util.sliceToCstr(allocator, req.post_body);
_ = c.curl_easy_setopt(curl, c.CURLOPT_POSTFIELDS, post_body_c);
warn("post body: {s}\n", .{req.post_body});
//warn("post body: {s}\n", .{req.post_body});
},
}

Expand All @@ -89,15 +89,15 @@ pub fn httpget(req: *config.HttpInfo) ![]const u8 {
var ccontent_type: [*c]const u8 = undefined;
_ = c.curl_easy_getinfo(curl, c.CURLINFO_CONTENT_TYPE, &ccontent_type);
req.content_type = util.cstrToSliceCopy(allocator, ccontent_type);
warn("http {} {s} {} {s} {} bytes\n", .{ req.verb, req.url, req.response_code, req.content_type, body_buffer.items.len });
std.log.debug("http {any} {s} {any} {s} {any} bytes\n", .{ req.verb, req.url, req.response_code, req.content_type, body_buffer.items.len });

return body_buffer.toOwnedSliceSentinel(0);
} else if (res == c.CURLE_OPERATION_TIMEDOUT) {
req.response_code = 2200;
return NetError.Curl;
} else {
const err_cstr = c.curl_easy_strerror(res);
warn("curl ERR {!} {s}\n", .{ res, util.cstrToSliceCopy(allocator, err_cstr) });
//const err_cstr = c.curl_easy_strerror(res);
//warn("curl ERR {!} {s}\n", .{ res, util.cstrToSliceCopy(allocator, err_cstr) });
if (res == c.CURLE_COULDNT_RESOLVE_HOST) {
req.response_code = 2100;
return NetError.DNS;
Expand All @@ -107,16 +107,14 @@ pub fn httpget(req: *config.HttpInfo) ![]const u8 {
}
}
} else {
warn("net curl easy init fail\n", .{});
//warn("net curl easy init fail\n", .{});
return NetError.CurlInit;
}
}

pub fn curl_write(ptr: [*c]const u8, _: usize, nmemb: usize, userdata: *anyopaque) callconv(.C) usize {
var buf = @as(*std.ArrayList(u8), @ptrCast(@alignCast(userdata)));
const body_part: []const u8 = ptr[0..nmemb];
buf.appendSlice(body_part) catch |err| {
warn("curl_write append fail {!}\n", .{err});
};
buf.appendSlice(body_part) catch {};
return nmemb;
}
2 changes: 1 addition & 1 deletion src/util.zig
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn mastodonExpandUrl(host: []const u8, home: bool, allocator: Allocator) []c
}
return url.toSliceConst();
} else {
warn("mastodonExpandUrl given empty host!\n", .{});
//warn("mastodonExpandUrl given empty host", .{});
return "";
}
}
Expand Down

0 comments on commit 9aa8f2f

Please sign in to comment.