Skip to content

Commit

Permalink
Remove shitty awk calls
Browse files Browse the repository at this point in the history
  • Loading branch information
qpfiffer committed Oct 3, 2023
1 parent f2eb031 commit 0391aca
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
2 changes: 2 additions & 0 deletions c_include/shithouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ struct sh_response {
struct sh_app *sh_app(const char *entry_point);
struct sh_response *sh_process_request(struct sh_app *app, const struct sh_request *req);
void sh_free_response(struct sh_response *r);

static int dir_iter(lua_State *L);
57 changes: 57 additions & 0 deletions c_src/shithouse.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// vim: noet ts=4 sw=4
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
Expand All @@ -9,12 +10,68 @@

#include "shithouse.h"

static int l_dir(lua_State *L) {
/* Straight out of the docs */
/* https://www.lua.org/pil/29.1.html */
const char *path = luaL_checkstring(L, 1);

/* create a userdatum to store a DIR address */
DIR **d = (DIR **)lua_newuserdata(L, sizeof(DIR *));

/* set its metatable */
luaL_getmetatable(L, "LuaBook.dir");
lua_setmetatable(L, -2);

/* try to open the given directory */
*d = opendir(path);
if (*d == NULL) /* error opening the directory? */
luaL_error(L, "cannot open %s: %s", path, strerror(errno));

/* creates and returns the iterator function
(its sole upvalue, the directory userdatum,
is already on the stack top */
lua_pushcclosure(L, dir_iter, 1);
return 1;
}

static int dir_iter(lua_State *L) {
DIR *d = *(DIR **)lua_touserdata(L, lua_upvalueindex(1));
struct dirent *entry;
while ((entry = readdir(d)) != NULL) {
if (entry->d_name[0] != '.')
break;
}

if (entry) {
lua_pushstring(L, entry->d_name);
return 1;
}
else return 0; /* no more values to return */
}

static int dir_gc(lua_State *L) {
DIR *d = *(DIR **)lua_touserdata(L, 1);
if (d) closedir(d);
return 0;
}

struct sh_app *sh_app(const char *entry_point) {
struct sh_app *new_app = calloc(sizeof(struct sh_app), 1);

lua_State *L = lua_open();
luaL_openlibs(L);

luaL_newmetatable(L, "LuaBook.dir");

/* set its __gc field */
lua_pushstring(L, "__gc");
lua_pushcfunction(L, dir_gc);
lua_settable(L, -3);

/* register the `dir' function */
lua_pushcfunction(L, l_dir);
lua_setglobal(L, "dir");

if (luaL_loadfile(L, entry_point)) {
luaL_error(L, "Cannot load %s: %s", entry_point, lua_tostring(L, -1));
return NULL;
Expand Down
30 changes: 18 additions & 12 deletions src/Filters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ end
function Filters.all_bumps_for_tag(tag, ctext)
local to_return = {}
local fixed, what = string.gsub(tag, " ", "")
local all_bumps = io.popen("ls -clt " .. config.TAGS .. "/" .. fixed .. " | awk '{print $9}' | grep -v '^$'")
all_bumps:flush()
for line in all_bumps:lines() do
local all_bumps = {}
for fname in dir(config.BUMPS) do
table.insert(all_bumps, fname)
end
for dont_care, line in pairs(all_bumps) do
to_return[#to_return + 1] = "<li><a href=\"//"
to_return[#to_return + 1] = line
to_return[#to_return + 1] = "."
Expand All @@ -54,7 +56,6 @@ function Filters.all_bumps_for_tag(tag, ctext)
to_return[#to_return + 1] = "</a></li>"
end

all_bumps:close()
return table.concat(to_return)
end

Expand Down Expand Up @@ -92,9 +93,12 @@ end
function Filters.all_bumps(text, ctext)
local to_return = {}
-- Held together with bash, sweet jams and summer dreams.
local all_bumps = io.popen("ls -lt " .. config.BUMPS .. " | awk '{print $9}' | grep -v '^$'")
all_bumps:flush()
for line in all_bumps:lines() do
local all_bumps = {}
for fname in dir(config.BUMPS) do
table.insert(all_bumps, fname)
end

for dont_care, line in pairs(all_bumps) do
local meta_data = Utils.check_for_bump(line)
local is_nsfw = false

Expand Down Expand Up @@ -124,7 +128,6 @@ function Filters.all_bumps(text, ctext)
to_return[#to_return + 1] = "\"> TAGS &raquo;</a></li>"
end

all_bumps:close()
return table.concat(to_return)
end

Expand All @@ -133,14 +136,18 @@ function Filters.all_bumps_json(text, ctext)
local to_return = {}
local first = true
-- Held together with bash, sweet jams and summer dreams.
local all_bumps = io.popen("ls -clt " .. config.BUMPS .. " | awk '{print $9}' | grep -v '^$'")
all_bumps:flush()

local all_bumps = {}
for fname in dir(config.BUMPS) do
table.insert(all_bumps, fname)
end

if not all_bumps then
-- Not sure why this is ever nil, but here we are.
return ""
end

for line in all_bumps:lines() do
for dont_care, line in pairs(all_bumps) do
local meta_data = Utils.check_for_bump(line)
local obj = {}

Expand Down Expand Up @@ -201,7 +208,6 @@ function Filters.all_bumps_json(text, ctext)
to_return[#to_return + 1] = "\"}"
end

all_bumps:close()
return table.concat(to_return)
end

Expand Down

0 comments on commit 0391aca

Please sign in to comment.