Blame ext/posix/getopt.c

Packit 437b5e
/*
Packit 437b5e
 * POSIX library for Lua 5.1, 5.2 & 5.3.
Packit 437b5e
 * (c) Gary V. Vaughan <gary@vaughan.pe>, 2013-2015
Packit 437b5e
 * (c) Reuben Thomas <rrt@sc3d.org> 2010-2013
Packit 437b5e
 * (c) Natanael Copa <natanael.copa@gmail.com> 2008-2010
Packit 437b5e
 * Clean up and bug fixes by Leo Razoumov <slonik.az@gmail.com> 2006-10-11
Packit 437b5e
 * Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> 07 Apr 2006 23:17:49
Packit 437b5e
 * Based on original by Claudio Terra for Lua 3.x.
Packit 437b5e
 * With contributions by Roberto Ierusalimschy.
Packit 437b5e
 * With documentation from Steve Donovan 2012
Packit 437b5e
 */
Packit 437b5e
/***
Packit 437b5e
 Command Line Argument Processing.
Packit 437b5e
Packit 437b5e
@module posix.getopt
Packit 437b5e
*/
Packit 437b5e
Packit 437b5e
#include <config.h>
Packit 437b5e
Packit 437b5e
#include <getopt.h>
Packit 437b5e
Packit 437b5e
#include "_helpers.c"
Packit 437b5e
Packit 437b5e
Packit 437b5e
/* getopt_long */
Packit 437b5e
Packit 437b5e
/* N.B. We don't need the symbolic constants no_argument,
Packit 437b5e
   required_argument and optional_argument, since their values are
Packit 437b5e
   defined as 0, 1 and 2 respectively. */
Packit 437b5e
static const char *const arg_types[] =
Packit 437b5e
{
Packit 437b5e
	"none", "required", "optional", NULL
Packit 437b5e
};
Packit 437b5e
Packit 437b5e
Packit 437b5e
static int
Packit 437b5e
iter_getopt_long(lua_State *L)
Packit 437b5e
{
Packit 437b5e
	int longindex = 0, ret, argc = lua_tointeger(L, lua_upvalueindex(1));
Packit 437b5e
	char **argv = (char **)lua_touserdata(L, lua_upvalueindex(3));
Packit 437b5e
	struct option *longopts = (struct option *)lua_touserdata(L, lua_upvalueindex(3 + argc + 1));
Packit 437b5e
Packit 437b5e
	if (argv == NULL) /* If we have already completed, return now. */
Packit 437b5e
		return 0;
Packit 437b5e
Packit 437b5e
	/* Fetch upvalues to pass to getopt_long. */
Packit 437b5e
	ret = getopt_long(argc, argv,
Packit 437b5e
			  lua_tostring(L, lua_upvalueindex(2)),
Packit 437b5e
			  longopts,
Packit 437b5e
			  &longindex);
Packit 437b5e
	if (ret == -1)
Packit 437b5e
		return 0;
Packit 437b5e
	else
Packit 437b5e
	{
Packit 437b5e
		char c = ret;
Packit 437b5e
		lua_pushlstring(L, &c, 1);
Packit 437b5e
		lua_pushstring(L, optarg);
Packit 437b5e
		lua_pushinteger(L, optind);
Packit 437b5e
		lua_pushinteger(L, longindex);
Packit 437b5e
		return 4;
Packit 437b5e
	}
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
/***
Packit 437b5e
Parse command-line options.
Packit 437b5e
@function getopt
Packit 437b5e
@param arg command line arguments
Packit 437b5e
@string shortopts short option specifier
Packit 437b5e
@tparam[opt] table longopts long options table
Packit 437b5e
@int[opt=0] opterr index of the option with an error
Packit 437b5e
@int[opt=1] optind index of the next unprocessed option
Packit 437b5e
@return option iterator, returning 4 values
Packit 437b5e
@see getopt(3)
Packit 437b5e
@see getopt_long(3)
Packit 437b5e
@usage
Packit 437b5e
local longopts = {
Packit 437b5e
  {"help", "none", 1},
Packit 437b5e
  {"version", "none", 3},
Packit 437b5e
}
Packit 437b5e
for r, longindex, err, i in P.getopt (arg, "ho:v", longopts, err, i) do
Packit 437b5e
  process (arg, err, i)
Packit 437b5e
end
Packit 437b5e
@see getopt.lua
Packit 437b5e
*/
Packit 437b5e
static int
Packit 437b5e
Pgetopt(lua_State *L)
Packit 437b5e
{
Packit 437b5e
	int argc, i, n = 0;
Packit 437b5e
	const char *shortopts;
Packit 437b5e
	char **argv;
Packit 437b5e
	struct option *longopts;
Packit 437b5e
Packit 437b5e
	checknargs(L, 5);
Packit 437b5e
	checktype(L, 1, LUA_TTABLE, "list");
Packit 437b5e
	shortopts = luaL_checkstring(L, 2);
Packit 437b5e
	if (!lua_isnoneornil(L, 3))
Packit 437b5e
		checktype(L, 3, LUA_TTABLE, "table or nil");
Packit 437b5e
	opterr = optint(L, 4, 0);
Packit 437b5e
	optind = optint(L, 5, 1);
Packit 437b5e
Packit 437b5e
	argc = (int)lua_objlen(L, 1) + 1;
Packit 437b5e
Packit 437b5e
	lua_pushinteger(L, argc);
Packit 437b5e
Packit 437b5e
	lua_pushstring(L, shortopts);
Packit 437b5e
Packit 437b5e
	argv = lua_newuserdata(L, (argc + 1) * sizeof(char *));
Packit 437b5e
	argv[argc] = NULL;
Packit 437b5e
	for (i = 0; i < argc; i++)
Packit 437b5e
	{
Packit 437b5e
		lua_pushinteger(L, i);
Packit 437b5e
		lua_gettable(L, 1);
Packit 437b5e
		argv[i] = (char *)luaL_checkstring(L, -1);
Packit 437b5e
	}
Packit 437b5e
Packit 437b5e
	if (lua_type(L, 3) == LUA_TTABLE)
Packit 437b5e
	{
Packit 437b5e
		n = (int)lua_objlen(L, 3);
Packit 437b5e
	}
Packit 437b5e
	longopts = lua_newuserdata(L, (n + 1) * sizeof(struct option));
Packit 437b5e
	longopts[n].name = NULL;
Packit 437b5e
	longopts[n].has_arg = 0;
Packit 437b5e
	longopts[n].flag = NULL;
Packit 437b5e
	longopts[n].val = 0;
Packit 437b5e
	for (i = 1; i <= n; i++)
Packit 437b5e
	{
Packit 437b5e
		const char *name, *val;
Packit 437b5e
		int has_arg;
Packit 437b5e
Packit 437b5e
		lua_pushinteger(L, i);
Packit 437b5e
		lua_gettable(L, 3);
Packit 437b5e
		luaL_checktype(L, -1, LUA_TTABLE);
Packit 437b5e
Packit 437b5e
		lua_pushinteger(L, 1);
Packit 437b5e
		lua_gettable(L, -2);
Packit 437b5e
		name = luaL_checkstring(L, -1);
Packit 437b5e
Packit 437b5e
		lua_pushinteger(L, 2);
Packit 437b5e
		lua_gettable(L, -3);
Packit 437b5e
		has_arg = luaL_checkoption(L, -1, NULL, arg_types);
Packit 437b5e
		lua_pop(L, 1);
Packit 437b5e
Packit 437b5e
		lua_pushinteger(L, 3);
Packit 437b5e
		lua_gettable(L, -3);
Packit 437b5e
		val = luaL_checkstring(L, -1);
Packit 437b5e
		lua_pop(L, 1);
Packit 437b5e
Packit 437b5e
		longopts[i - 1].name = name;
Packit 437b5e
		longopts[i - 1].has_arg = has_arg;
Packit 437b5e
		longopts[i - 1].flag = NULL;
Packit 437b5e
		longopts[i - 1].val = val[0];
Packit 437b5e
		lua_pop(L, 1);
Packit 437b5e
	}
Packit 437b5e
Packit 437b5e
	/* Push remaining upvalues, and make and push closure. */
Packit 437b5e
	lua_pushcclosure(L, iter_getopt_long, 4 + argc + n);
Packit 437b5e
Packit 437b5e
	return 1;
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
static const luaL_Reg posix_getopt_fns[] =
Packit 437b5e
{
Packit 437b5e
	LPOSIX_FUNC( Pgetopt		),
Packit 437b5e
	{NULL, NULL}
Packit 437b5e
};
Packit 437b5e
Packit 437b5e
Packit 437b5e
LUALIB_API int
Packit 437b5e
luaopen_posix_getopt(lua_State *L)
Packit 437b5e
{
Packit 437b5e
	luaL_register(L, "posix.getopt", posix_getopt_fns);
Packit 437b5e
	lua_pushliteral(L, "posix.getopt for " LUA_VERSION " / " PACKAGE_STRING);
Packit 437b5e
	lua_setfield(L, -2, "version");
Packit 437b5e
Packit 437b5e
	return 1;
Packit 437b5e
}