Created
September 4, 2017 01:58
Star
You must be signed in to star a gist
An example Lua module written in C.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A Lua module written in C. | |
// | |
// This module enables terminal-based ASCII art | |
// of a loquacious bovine nature. | |
// | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include "lua.h" | |
#include "lauxlib.h" | |
// This is a Lua-callable function. | |
// It prints out a cow uttering the user's string. | |
static int say(lua_State *L) { | |
char *str; | |
asprintf(&str, "cowsay %s", lua_tostring(L, -1)); | |
system(str); | |
free(str); | |
return 0; | |
} | |
// This is the module's initialization function. | |
// It's called implicitly by Lua when the module loads. | |
int luaopen_cow(lua_State *L) { | |
static const struct luaL_Reg cow_lib[] = { | |
{"say", say}, | |
{NULL, NULL} | |
}; | |
luaL_newlib(L, cow_lib); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment