luasystm.c revision 1.2
1/*	$NetBSD: luasystm.c,v 1.2 2014/03/24 20:21:02 christos 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#include <sys/cpu.h>
37#ifdef _MODULE
38#include <sys/module.h>
39#endif
40#include <sys/systm.h>
41
42#include <lua.h>
43#include <lauxlib.h>
44
45#ifdef _MODULE
46MODULE(MODULE_CLASS_MISC, luasystm, "lua");
47
48/* Various printing functions */
49static int
50print(lua_State *L)
51{
52	const char *s;
53
54	s = lua_tostring(L, -1);
55	if (s)
56		printf("%s", s);
57	return 0;
58}
59
60static int
61print_nolog(lua_State *L)
62{
63	const char *s;
64
65	s = lua_tostring(L, -1);
66	if (s)
67		printf_nolog("%s", s);
68	return 0;
69}
70
71static int
72uprint(lua_State *L)
73{
74	const char *s;
75
76	s = lua_tostring(L, -1);
77	if (s)
78		uprintf("%s", s);
79	return 0;
80}
81
82static int
83systm_aprint_normal(lua_State *L)
84{
85	const char *s;
86
87	s = lua_tostring(L, -1);
88	if (s)
89		aprint_normal("%s", s);
90	return 0;
91}
92
93static int
94systm_aprint_naive(lua_State *L)
95{
96	const char *s;
97
98	s = lua_tostring(L, -1);
99	if (s)
100		aprint_naive("%s", s);
101	return 0;
102}
103
104static int
105systm_aprint_verbose(lua_State *L)
106{
107	const char *s;
108
109	s = lua_tostring(L, -1);
110	if (s)
111		aprint_verbose("%s", s);
112	return 0;
113}
114
115static int
116systm_aprint_debug(lua_State *L)
117{
118	const char *s;
119
120	s = lua_tostring(L, -1);
121	if (s)
122		aprint_debug("%s", s);
123	return 0;
124}
125
126static int
127systm_aprint_error(lua_State *L)
128{
129	const char *s;
130
131	s = lua_tostring(L, -1);
132	if (s)
133		aprint_error("%s", s);
134	return 0;
135}
136
137static int
138systm_aprint_get_error_count(lua_State *L)
139{
140	lua_pushinteger(L, aprint_get_error_count());
141	return 1;
142}
143
144/* panicing */
145
146static int
147systm_panic(lua_State *L)
148{
149	const char *s;
150
151	s = lua_tostring(L, -1);
152	if (s)
153		panic("%s", s);
154	return 0;
155}
156
157/* callouts */
158
159/* mutexes */
160
161static int
162luaopen_systm(void *ls)
163{
164	lua_State *L = (lua_State *)ls;
165	const luaL_Reg systm_lib[ ] = {
166		{ "print",			print },
167		{ "print_nolog",		print_nolog },
168		{ "uprint",			uprint },
169		{ "aprint_normal",		systm_aprint_normal },
170		{ "aprint_naive",		systm_aprint_naive },
171		{ "aprint_verbose",		systm_aprint_verbose },
172		{ "aprint_debug",		systm_aprint_debug },
173		{ "aprint_error",		systm_aprint_error },
174		{ "aprint_get_error_count",	systm_aprint_get_error_count },
175
176		/* panicing */
177		{ "panic",			systm_panic },
178
179		/* callouts */
180
181		/* mutexes */
182
183		{NULL, NULL}
184	};
185
186	luaL_register(L, "systm", systm_lib);
187
188	/* some string values */
189	lua_pushstring(L, copyright);
190	lua_setfield(L, -2, "copyright");
191	lua_pushstring(L, cpu_getmodel());
192	lua_setfield(L, -2, "cpu_model");
193	lua_pushstring(L, machine);
194	lua_setfield(L, -2, "machine");
195	lua_pushstring(L, machine_arch);
196	lua_setfield(L, -2, "machine_arch");
197	lua_pushstring(L, osrelease);
198	lua_setfield(L, -2, "osrelease");
199	lua_pushstring(L, ostype);
200	lua_setfield(L, -2, "ostype");
201	lua_pushstring(L, kernel_ident);
202	lua_setfield(L, -2, "kernel_ident");
203	lua_pushstring(L, version);
204	lua_setfield(L, -2, "version");
205
206	/* some integer values */
207	lua_pushinteger(L, ncpu);
208	lua_setfield(L, -2, "ncpu");
209
210	return 1;
211}
212
213static int
214luasystm_modcmd(modcmd_t cmd, void *opaque)
215{
216	int error;
217
218	switch (cmd) {
219	case MODULE_CMD_INIT:
220		error = lua_mod_register("systm", luaopen_systm);
221		break;
222	case MODULE_CMD_FINI:
223		error = lua_mod_unregister("systm");
224		break;
225	default:
226		error = ENOTTY;
227	}
228	return error;
229}
230#endif
231