1 /* $NetBSD: luasystm.c,v 1.1 2013/12/17 00:02:22 lneto Exp $ */ 2 3 /* 4 * Copyright (c) 2011, 2013 Marc Balmer <mbalmer@NetBSD.org>. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the Author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 /* Lua systm module */ 32 33 #include <sys/param.h> 34 #include <sys/lua.h> 35 #include <sys/callout.h> 36 #ifdef _MODULE 37 #include <sys/module.h> 38 #endif 39 #include <sys/systm.h> 40 41 #include <lua.h> 42 #include <lauxlib.h> 43 44 #ifdef _MODULE 45 MODULE(MODULE_CLASS_MISC, luasystm, "lua"); 46 47 /* Various printing functions */ 48 static int 49 print(lua_State *L) 50 { 51 const char *s; 52 53 s = lua_tostring(L, -1); 54 if (s) 55 printf("%s", s); 56 return 0; 57 } 58 59 static int 60 print_nolog(lua_State *L) 61 { 62 const char *s; 63 64 s = lua_tostring(L, -1); 65 if (s) 66 printf_nolog("%s", s); 67 return 0; 68 } 69 70 static int 71 uprint(lua_State *L) 72 { 73 const char *s; 74 75 s = lua_tostring(L, -1); 76 if (s) 77 uprintf("%s", s); 78 return 0; 79 } 80 81 static int 82 systm_aprint_normal(lua_State *L) 83 { 84 const char *s; 85 86 s = lua_tostring(L, -1); 87 if (s) 88 aprint_normal("%s", s); 89 return 0; 90 } 91 92 static int 93 systm_aprint_naive(lua_State *L) 94 { 95 const char *s; 96 97 s = lua_tostring(L, -1); 98 if (s) 99 aprint_naive("%s", s); 100 return 0; 101 } 102 103 static int 104 systm_aprint_verbose(lua_State *L) 105 { 106 const char *s; 107 108 s = lua_tostring(L, -1); 109 if (s) 110 aprint_verbose("%s", s); 111 return 0; 112 } 113 114 static int 115 systm_aprint_debug(lua_State *L) 116 { 117 const char *s; 118 119 s = lua_tostring(L, -1); 120 if (s) 121 aprint_debug("%s", s); 122 return 0; 123 } 124 125 static int 126 systm_aprint_error(lua_State *L) 127 { 128 const char *s; 129 130 s = lua_tostring(L, -1); 131 if (s) 132 aprint_error("%s", s); 133 return 0; 134 } 135 136 static int 137 systm_aprint_get_error_count(lua_State *L) 138 { 139 lua_pushinteger(L, aprint_get_error_count()); 140 return 1; 141 } 142 143 /* panicing */ 144 145 static int 146 systm_panic(lua_State *L) 147 { 148 const char *s; 149 150 s = lua_tostring(L, -1); 151 if (s) 152 panic("%s", s); 153 return 0; 154 } 155 156 /* callouts */ 157 158 /* mutexes */ 159 160 static int 161 luaopen_systm(void *ls) 162 { 163 lua_State *L = (lua_State *)ls; 164 const luaL_Reg systm_lib[ ] = { 165 { "print", print }, 166 { "print_nolog", print_nolog }, 167 { "uprint", uprint }, 168 { "aprint_normal", systm_aprint_normal }, 169 { "aprint_naive", systm_aprint_naive }, 170 { "aprint_verbose", systm_aprint_verbose }, 171 { "aprint_debug", systm_aprint_debug }, 172 { "aprint_error", systm_aprint_error }, 173 { "aprint_get_error_count", systm_aprint_get_error_count }, 174 175 /* panicing */ 176 { "panic", systm_panic }, 177 178 /* callouts */ 179 180 /* mutexes */ 181 182 {NULL, NULL} 183 }; 184 185 luaL_register(L, "systm", systm_lib); 186 187 /* some string values */ 188 lua_pushstring(L, copyright); 189 lua_setfield(L, -2, "copyright"); 190 lua_pushstring(L, cpu_model); 191 lua_setfield(L, -2, "cpu_model"); 192 lua_pushstring(L, machine); 193 lua_setfield(L, -2, "machine"); 194 lua_pushstring(L, machine_arch); 195 lua_setfield(L, -2, "machine_arch"); 196 lua_pushstring(L, osrelease); 197 lua_setfield(L, -2, "osrelease"); 198 lua_pushstring(L, ostype); 199 lua_setfield(L, -2, "ostype"); 200 lua_pushstring(L, kernel_ident); 201 lua_setfield(L, -2, "kernel_ident"); 202 lua_pushstring(L, version); 203 lua_setfield(L, -2, "version"); 204 205 /* some integer values */ 206 lua_pushinteger(L, ncpu); 207 lua_setfield(L, -2, "ncpu"); 208 209 return 1; 210 } 211 212 static int 213 luasystm_modcmd(modcmd_t cmd, void *opaque) 214 { 215 int error; 216 217 switch (cmd) { 218 case MODULE_CMD_INIT: 219 error = lua_mod_register("systm", luaopen_systm); 220 break; 221 case MODULE_CMD_FINI: 222 error = lua_mod_unregister("systm"); 223 break; 224 default: 225 error = ENOTTY; 226 } 227 return error; 228 } 229 #endif 230