List for cgit developers and users
 help / color / mirror / Atom feed
From: john at keeping.me.uk (John Keeping)
Subject: [PATCH 06/12] filter: add preliminary lua support
Date: Mon, 13 Jan 2014 08:55:31 +0000	[thread overview]
Message-ID: <20140113085531.GY7608@serenity.lan> (raw)
In-Reply-To: <1389586279-23724-7-git-send-email-Jason@zx2c4.com>

On Mon, Jan 13, 2014 at 05:11:13AM +0100, Jason A. Donenfeld wrote:
[snip]
> +static int html_lua_filter(lua_State *lua_state)
> +{
> +	size_t len;
> +	const char *str;
> +
> +	str = lua_tostring(lua_state, 1);
> +	if (!str)
> +		return 0;
> +	len = strlen(str);
> +	libc_write(STDOUT_FILENO, str, len);
> +	return 0;
> +}
> +
> +static void cleanup_lua_filter(struct cgit_filter *base)
> +{
> +	struct lua_filter *filter = (struct lua_filter *) base;
> +
> +	if (!filter->lua_state)
> +		return;
> +
> +	lua_close(filter->lua_state);
> +	filter->lua_state = NULL;
> +	if (filter->script_file) {
> +		free(filter->script_file);
> +		filter->script_file = NULL;
> +	}
> +}
> +
> +static int init_lua_filter(struct lua_filter *filter)
> +{
> +	if (filter->lua_state)
> +		return 0;
> +
> +	if (!(filter->lua_state = luaL_newstate()))
> +		return 1;
> +
> +	luaL_openlibs(filter->lua_state);
> +
> +	lua_pushcfunction(filter->lua_state, html_lua_filter);
> +	lua_setglobal(filter->lua_state, "html");

It would be good to provide some of the other html_* functions here,
particularly html_txt so that filters don't need to re-invent the wheel
for escaping output.  That's probably slightly harder than the plain
HTML, but I suspect we just need to wrap the call to the underlying
function in an unhook_write/hook_write pair.

> +
> +	if (luaL_dofile(filter->lua_state, filter->script_file)) {
> +		lua_close(filter->lua_state);
> +		filter->lua_state = NULL;
> +		return 1;
> +	}
> +	return 0;
> +}
> +
> +static int open_lua_filter(struct cgit_filter *base, va_list ap)
> +{
> +	struct lua_filter *filter = (struct lua_filter *) base;
> +	int i;
> +
> +	if (init_lua_filter(filter))
> +		return 1;
> +
> +	hook_write(base, write_lua_filter);
> +
> +	lua_getglobal(filter->lua_state, "filter_open");
> +	for (i = 0; i < filter->base.argument_count; ++i)
> +		lua_pushstring(filter->lua_state, va_arg(ap, char *));
> +	if (lua_pcall(filter->lua_state, filter->base.argument_count, 0, 0))
> +		return 1;
> +	return 0;
> +}
> +
> +static int close_lua_filter(struct cgit_filter *base)
> +{
> +	struct lua_filter *filter = (struct lua_filter *) base;
> +	int ret = 0;
> +
> +	lua_getglobal(filter->lua_state, "filter_close");
> +	if (lua_pcall(filter->lua_state, 0, 0, 0))
> +		ret = 1;
> +	unhook_write();
> +	return ret;
> +}
> +
> +static void fprintf_lua_filter(struct cgit_filter *base, FILE *f, const char *prefix)
> +{
> +	struct lua_filter *filter = (struct lua_filter *) base;
> +	fprintf(f, "%slua:%s\n", prefix, filter->script_file);
> +}
> +
> +
> +static struct cgit_filter *new_lua_filter(const char *cmd, int argument_count)
> +{
> +	struct lua_filter *filter;
> +
> +	filter = xmalloc(sizeof(*filter));
> +	memset(filter, 0, sizeof(*filter));
> +	filter->base.open = open_lua_filter;
> +	filter->base.close = close_lua_filter;
> +	filter->base.fprintf = fprintf_lua_filter;
> +	filter->base.cleanup = cleanup_lua_filter;
> +	filter->base.argument_count = argument_count;
> +	filter->script_file = xstrdup(cmd);
> +
> +	return &filter->base;
>  }
>  
> +#endif


  parent reply	other threads:[~2014-01-13  8:55 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-13  4:11 [PATCH 00/12] filter framework and lua integration: complete Jason
2014-01-13  4:11 ` [PATCH 01/12] filter: add fprintf_filter function Jason
2014-01-13  4:11 ` [PATCH 02/12] filter: add interface layer Jason
2014-01-13  4:11 ` [PATCH 03/12] filter: introduce "filter type" prefix Jason
2014-01-13  4:11 ` [PATCH 04/12] filter: allow for cleanup hook for filter types Jason
2014-01-13  4:11 ` [PATCH 05/12] filter: basic write hooking infrastructure Jason
2014-01-13  8:19   ` cgit
2014-01-13  4:11 ` [PATCH 06/12] filter: add preliminary lua support Jason
2014-01-13  8:31   ` cgit
2014-01-13  8:53     ` john
2014-01-13  8:39   ` cgit
2014-01-13  8:55   ` john [this message]
2014-01-13  9:41   ` bluewind
2014-01-13  4:11 ` [PATCH 07/12] filter: document lua filter type Jason
2014-01-13  4:11 ` [PATCH 08/12] filter: lua error reporting Jason
2014-01-13  4:11 ` [PATCH 09/12] filter: return on null filter from open and close Jason
2014-01-13  4:11 ` [PATCH 10/12] filter: add support for email filter Jason
2014-01-13  4:11 ` [PATCH 11/12] filter: add simple gravatar " Jason
2014-01-13  4:11 ` [PATCH 12/12] filter: add gravatar lua script Jason

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140113085531.GY7608@serenity.lan \
    --to=cgit@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).