luasystm.c revision 1.5
11.5Slneto/*	$NetBSD: luasystm.c,v 1.5 2014/07/19 18:38:35 lneto Exp $ */
21.1Slneto
31.1Slneto/*
41.1Slneto * Copyright (c) 2011, 2013 Marc Balmer <mbalmer@NetBSD.org>.
51.1Slneto * All rights reserved.
61.1Slneto *
71.1Slneto * Redistribution and use in source and binary forms, with or without
81.1Slneto * modification, are permitted provided that the following conditions
91.1Slneto * are met:
101.1Slneto * 1. Redistributions of source code must retain the above copyright
111.1Slneto *    notice, this list of conditions and the following disclaimer.
121.1Slneto * 2. Redistributions in binary form must reproduce the above copyright
131.1Slneto *    notice, this list of conditions and the following disclaimer in the
141.1Slneto *    documentation and/or other materials provided with the distribution.
151.1Slneto * 3. The name of the Author may not be used to endorse or promote products
161.1Slneto *    derived from this software without specific prior written permission.
171.1Slneto *
181.1Slneto * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
191.1Slneto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201.1Slneto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
211.1Slneto * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
221.1Slneto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231.1Slneto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
241.1Slneto * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251.1Slneto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261.1Slneto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271.1Slneto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281.1Slneto * SUCH DAMAGE.
291.1Slneto */
301.1Slneto
311.1Slneto/* Lua systm module */
321.1Slneto
331.1Slneto#include <sys/param.h>
341.1Slneto#include <sys/lua.h>
351.1Slneto#include <sys/callout.h>
361.2Schristos#include <sys/cpu.h>
371.1Slneto#ifdef _MODULE
381.1Slneto#include <sys/module.h>
391.1Slneto#endif
401.1Slneto#include <sys/systm.h>
411.1Slneto
421.1Slneto#include <lua.h>
431.1Slneto#include <lauxlib.h>
441.1Slneto
451.1Slneto#ifdef _MODULE
461.1SlnetoMODULE(MODULE_CLASS_MISC, luasystm, "lua");
471.1Slneto
481.1Slneto/* Various printing functions */
491.1Slnetostatic int
501.1Slnetoprint(lua_State *L)
511.1Slneto{
521.1Slneto	const char *s;
531.1Slneto
541.1Slneto	s = lua_tostring(L, -1);
551.1Slneto	if (s)
561.1Slneto		printf("%s", s);
571.1Slneto	return 0;
581.1Slneto}
591.1Slneto
601.1Slnetostatic int
611.1Slnetoprint_nolog(lua_State *L)
621.1Slneto{
631.1Slneto	const char *s;
641.1Slneto
651.1Slneto	s = lua_tostring(L, -1);
661.1Slneto	if (s)
671.1Slneto		printf_nolog("%s", s);
681.1Slneto	return 0;
691.1Slneto}
701.1Slneto
711.1Slnetostatic int
721.1Slnetouprint(lua_State *L)
731.1Slneto{
741.1Slneto	const char *s;
751.1Slneto
761.1Slneto	s = lua_tostring(L, -1);
771.1Slneto	if (s)
781.1Slneto		uprintf("%s", s);
791.1Slneto	return 0;
801.1Slneto}
811.1Slneto
821.1Slnetostatic int
831.1Slnetosystm_aprint_normal(lua_State *L)
841.1Slneto{
851.1Slneto	const char *s;
861.1Slneto
871.1Slneto	s = lua_tostring(L, -1);
881.1Slneto	if (s)
891.1Slneto		aprint_normal("%s", s);
901.1Slneto	return 0;
911.1Slneto}
921.1Slneto
931.1Slnetostatic int
941.1Slnetosystm_aprint_naive(lua_State *L)
951.1Slneto{
961.1Slneto	const char *s;
971.1Slneto
981.1Slneto	s = lua_tostring(L, -1);
991.1Slneto	if (s)
1001.1Slneto		aprint_naive("%s", s);
1011.1Slneto	return 0;
1021.1Slneto}
1031.1Slneto
1041.1Slnetostatic int
1051.1Slnetosystm_aprint_verbose(lua_State *L)
1061.1Slneto{
1071.1Slneto	const char *s;
1081.1Slneto
1091.1Slneto	s = lua_tostring(L, -1);
1101.1Slneto	if (s)
1111.1Slneto		aprint_verbose("%s", s);
1121.1Slneto	return 0;
1131.1Slneto}
1141.1Slneto
1151.1Slnetostatic int
1161.1Slnetosystm_aprint_debug(lua_State *L)
1171.1Slneto{
1181.1Slneto	const char *s;
1191.1Slneto
1201.1Slneto	s = lua_tostring(L, -1);
1211.1Slneto	if (s)
1221.1Slneto		aprint_debug("%s", s);
1231.1Slneto	return 0;
1241.1Slneto}
1251.1Slneto
1261.1Slnetostatic int
1271.1Slnetosystm_aprint_error(lua_State *L)
1281.1Slneto{
1291.1Slneto	const char *s;
1301.1Slneto
1311.1Slneto	s = lua_tostring(L, -1);
1321.1Slneto	if (s)
1331.1Slneto		aprint_error("%s", s);
1341.1Slneto	return 0;
1351.1Slneto}
1361.1Slneto
1371.1Slnetostatic int
1381.1Slnetosystm_aprint_get_error_count(lua_State *L)
1391.1Slneto{
1401.1Slneto	lua_pushinteger(L, aprint_get_error_count());
1411.1Slneto	return 1;
1421.1Slneto}
1431.1Slneto
1441.1Slneto/* panicing */
1451.1Slneto
1461.1Slnetostatic int
1471.1Slnetosystm_panic(lua_State *L)
1481.1Slneto{
1491.1Slneto	const char *s;
1501.1Slneto
1511.1Slneto	s = lua_tostring(L, -1);
1521.1Slneto	if (s)
1531.1Slneto		panic("%s", s);
1541.1Slneto	return 0;
1551.1Slneto}
1561.1Slneto
1571.1Slneto/* callouts */
1581.1Slneto
1591.1Slneto/* mutexes */
1601.1Slneto
1611.1Slnetostatic int
1621.3Slnetoluaopen_systm(lua_State *L)
1631.1Slneto{
1641.1Slneto	const luaL_Reg systm_lib[ ] = {
1651.1Slneto		{ "print",			print },
1661.1Slneto		{ "print_nolog",		print_nolog },
1671.1Slneto		{ "uprint",			uprint },
1681.1Slneto		{ "aprint_normal",		systm_aprint_normal },
1691.1Slneto		{ "aprint_naive",		systm_aprint_naive },
1701.1Slneto		{ "aprint_verbose",		systm_aprint_verbose },
1711.1Slneto		{ "aprint_debug",		systm_aprint_debug },
1721.1Slneto		{ "aprint_error",		systm_aprint_error },
1731.1Slneto		{ "aprint_get_error_count",	systm_aprint_get_error_count },
1741.1Slneto
1751.1Slneto		/* panicing */
1761.1Slneto		{ "panic",			systm_panic },
1771.1Slneto
1781.1Slneto		/* callouts */
1791.1Slneto
1801.1Slneto		/* mutexes */
1811.1Slneto
1821.1Slneto		{NULL, NULL}
1831.1Slneto	};
1841.1Slneto
1851.5Slneto	luaL_newlib(L, systm_lib);
1861.1Slneto
1871.1Slneto	/* some string values */
1881.1Slneto	lua_pushstring(L, copyright);
1891.1Slneto	lua_setfield(L, -2, "copyright");
1901.2Schristos	lua_pushstring(L, cpu_getmodel());
1911.1Slneto	lua_setfield(L, -2, "cpu_model");
1921.1Slneto	lua_pushstring(L, machine);
1931.1Slneto	lua_setfield(L, -2, "machine");
1941.1Slneto	lua_pushstring(L, machine_arch);
1951.1Slneto	lua_setfield(L, -2, "machine_arch");
1961.1Slneto	lua_pushstring(L, osrelease);
1971.1Slneto	lua_setfield(L, -2, "osrelease");
1981.1Slneto	lua_pushstring(L, ostype);
1991.1Slneto	lua_setfield(L, -2, "ostype");
2001.1Slneto	lua_pushstring(L, kernel_ident);
2011.1Slneto	lua_setfield(L, -2, "kernel_ident");
2021.1Slneto	lua_pushstring(L, version);
2031.1Slneto	lua_setfield(L, -2, "version");
2041.1Slneto
2051.1Slneto	/* some integer values */
2061.1Slneto	lua_pushinteger(L, ncpu);
2071.1Slneto	lua_setfield(L, -2, "ncpu");
2081.1Slneto
2091.1Slneto	return 1;
2101.1Slneto}
2111.1Slneto
2121.1Slnetostatic int
2131.1Slnetoluasystm_modcmd(modcmd_t cmd, void *opaque)
2141.1Slneto{
2151.1Slneto	int error;
2161.1Slneto
2171.1Slneto	switch (cmd) {
2181.1Slneto	case MODULE_CMD_INIT:
2191.4Slneto		error = klua_mod_register("systm", luaopen_systm);
2201.1Slneto		break;
2211.1Slneto	case MODULE_CMD_FINI:
2221.4Slneto		error = klua_mod_unregister("systm");
2231.1Slneto		break;
2241.1Slneto	default:
2251.1Slneto		error = ENOTTY;
2261.1Slneto	}
2271.1Slneto	return error;
2281.1Slneto}
2291.1Slneto#endif
230