luasystm.c revision 1.1
11.1Slneto/* $NetBSD: luasystm.c,v 1.1 2013/12/17 00:02:22 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.1Slneto#ifdef _MODULE 371.1Slneto#include <sys/module.h> 381.1Slneto#endif 391.1Slneto#include <sys/systm.h> 401.1Slneto 411.1Slneto#include <lua.h> 421.1Slneto#include <lauxlib.h> 431.1Slneto 441.1Slneto#ifdef _MODULE 451.1SlnetoMODULE(MODULE_CLASS_MISC, luasystm, "lua"); 461.1Slneto 471.1Slneto/* Various printing functions */ 481.1Slnetostatic int 491.1Slnetoprint(lua_State *L) 501.1Slneto{ 511.1Slneto const char *s; 521.1Slneto 531.1Slneto s = lua_tostring(L, -1); 541.1Slneto if (s) 551.1Slneto printf("%s", s); 561.1Slneto return 0; 571.1Slneto} 581.1Slneto 591.1Slnetostatic int 601.1Slnetoprint_nolog(lua_State *L) 611.1Slneto{ 621.1Slneto const char *s; 631.1Slneto 641.1Slneto s = lua_tostring(L, -1); 651.1Slneto if (s) 661.1Slneto printf_nolog("%s", s); 671.1Slneto return 0; 681.1Slneto} 691.1Slneto 701.1Slnetostatic int 711.1Slnetouprint(lua_State *L) 721.1Slneto{ 731.1Slneto const char *s; 741.1Slneto 751.1Slneto s = lua_tostring(L, -1); 761.1Slneto if (s) 771.1Slneto uprintf("%s", s); 781.1Slneto return 0; 791.1Slneto} 801.1Slneto 811.1Slnetostatic int 821.1Slnetosystm_aprint_normal(lua_State *L) 831.1Slneto{ 841.1Slneto const char *s; 851.1Slneto 861.1Slneto s = lua_tostring(L, -1); 871.1Slneto if (s) 881.1Slneto aprint_normal("%s", s); 891.1Slneto return 0; 901.1Slneto} 911.1Slneto 921.1Slnetostatic int 931.1Slnetosystm_aprint_naive(lua_State *L) 941.1Slneto{ 951.1Slneto const char *s; 961.1Slneto 971.1Slneto s = lua_tostring(L, -1); 981.1Slneto if (s) 991.1Slneto aprint_naive("%s", s); 1001.1Slneto return 0; 1011.1Slneto} 1021.1Slneto 1031.1Slnetostatic int 1041.1Slnetosystm_aprint_verbose(lua_State *L) 1051.1Slneto{ 1061.1Slneto const char *s; 1071.1Slneto 1081.1Slneto s = lua_tostring(L, -1); 1091.1Slneto if (s) 1101.1Slneto aprint_verbose("%s", s); 1111.1Slneto return 0; 1121.1Slneto} 1131.1Slneto 1141.1Slnetostatic int 1151.1Slnetosystm_aprint_debug(lua_State *L) 1161.1Slneto{ 1171.1Slneto const char *s; 1181.1Slneto 1191.1Slneto s = lua_tostring(L, -1); 1201.1Slneto if (s) 1211.1Slneto aprint_debug("%s", s); 1221.1Slneto return 0; 1231.1Slneto} 1241.1Slneto 1251.1Slnetostatic int 1261.1Slnetosystm_aprint_error(lua_State *L) 1271.1Slneto{ 1281.1Slneto const char *s; 1291.1Slneto 1301.1Slneto s = lua_tostring(L, -1); 1311.1Slneto if (s) 1321.1Slneto aprint_error("%s", s); 1331.1Slneto return 0; 1341.1Slneto} 1351.1Slneto 1361.1Slnetostatic int 1371.1Slnetosystm_aprint_get_error_count(lua_State *L) 1381.1Slneto{ 1391.1Slneto lua_pushinteger(L, aprint_get_error_count()); 1401.1Slneto return 1; 1411.1Slneto} 1421.1Slneto 1431.1Slneto/* panicing */ 1441.1Slneto 1451.1Slnetostatic int 1461.1Slnetosystm_panic(lua_State *L) 1471.1Slneto{ 1481.1Slneto const char *s; 1491.1Slneto 1501.1Slneto s = lua_tostring(L, -1); 1511.1Slneto if (s) 1521.1Slneto panic("%s", s); 1531.1Slneto return 0; 1541.1Slneto} 1551.1Slneto 1561.1Slneto/* callouts */ 1571.1Slneto 1581.1Slneto/* mutexes */ 1591.1Slneto 1601.1Slnetostatic int 1611.1Slnetoluaopen_systm(void *ls) 1621.1Slneto{ 1631.1Slneto lua_State *L = (lua_State *)ls; 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.1Slneto luaL_register(L, "systm", systm_lib); 1861.1Slneto 1871.1Slneto /* some string values */ 1881.1Slneto lua_pushstring(L, copyright); 1891.1Slneto lua_setfield(L, -2, "copyright"); 1901.1Slneto lua_pushstring(L, cpu_model); 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.1Slneto error = lua_mod_register("systm", luaopen_systm); 2201.1Slneto break; 2211.1Slneto case MODULE_CMD_FINI: 2221.1Slneto error = lua_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