1/*	$NetBSD: luasystm.c,v 1.6 2022/08/11 23:53:04 gutteridge 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/* panicking */
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(lua_State *L)
163{
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		/* panicking */
176		{ "panic",			systm_panic },
177
178		/* callouts */
179
180		/* mutexes */
181
182		{NULL, NULL}
183	};
184
185	luaL_newlib(L, systm_lib);
186
187	/* some string values */
188	lua_pushstring(L, copyright);
189	lua_setfield(L, -2, "copyright");
190	lua_pushstring(L, cpu_getmodel());
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
212static int
213luasystm_modcmd(modcmd_t cmd, void *opaque)
214{
215	int error;
216
217	switch (cmd) {
218	case MODULE_CMD_INIT:
219		error = klua_mod_register("systm", luaopen_systm);
220		break;
221	case MODULE_CMD_FINI:
222		error = klua_mod_unregister("systm");
223		break;
224	default:
225		error = ENOTTY;
226	}
227	return error;
228}
229#endif
230