tools/llvm: Do not build with symbols
[minix3.git] / lib / lua / syslog / syslog.c
blobfd4d49224c111ec004bffecce797e7f859f10d38
1 /* $NetBSD: syslog.c,v 1.1 2013/11/12 14:32:03 mbalmer Exp $ */
3 /*
4 * Copyright (c) 2013 Marc Balmer <marc@msys.ch>
5 * All rights reserved.
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 /* Lua binding for syslog */
30 #include <sys/types.h>
32 #include <errno.h>
33 #include <lua.h>
34 #include <lauxlib.h>
35 #include <stdlib.h>
36 #include <syslog.h>
37 #include <unistd.h>
39 int luaopen_syslog(lua_State *);
41 static int
42 syslog_openlog(lua_State *L)
44 const char *ident;
45 int option;
46 int facility;
48 ident = luaL_checkstring(L, 1);
49 option = luaL_checkinteger(L, 2);
50 facility = luaL_checkinteger(L, 3);
51 openlog(ident, option, facility);
52 return 0;
55 static int
56 syslog_syslog(lua_State *L)
58 syslog(luaL_checkint(L, 1), "%s", luaL_checkstring(L, 2));
59 return 0;
62 static int
63 syslog_closelog(lua_State *L)
65 closelog();
66 return 0;
69 static int
70 syslog_setlogmask(lua_State *L)
72 lua_pushinteger(L, setlogmask(luaL_checkint(L, 1)));
73 return 1;
76 static void
77 syslog_set_info(lua_State *L)
79 lua_pushliteral(L, "_COPYRIGHT");
80 lua_pushliteral(L, "Copyright (C) 2013 by "
81 "Marc Balmer <marc@msys.ch>");
82 lua_settable(L, -3);
83 lua_pushliteral(L, "_DESCRIPTION");
84 lua_pushliteral(L, "syslog binding for Lua");
85 lua_settable(L, -3);
86 lua_pushliteral(L, "_VERSION");
87 lua_pushliteral(L, "syslog 1.0.0");
88 lua_settable(L, -3);
91 struct constant {
92 const char *name;
93 int value;
96 #define CONSTANT(NAME) { #NAME, NAME }
98 static struct constant syslog_constant[] = {
99 /* syslog options */
100 CONSTANT(LOG_CONS),
101 CONSTANT(LOG_NDELAY),
102 CONSTANT(LOG_NOWAIT),
103 CONSTANT(LOG_ODELAY),
104 CONSTANT(LOG_PERROR),
105 CONSTANT(LOG_PID),
107 /* syslog facilities */
108 CONSTANT(LOG_AUTH),
109 CONSTANT(LOG_AUTHPRIV),
110 CONSTANT(LOG_CRON),
111 CONSTANT(LOG_DAEMON),
112 CONSTANT(LOG_FTP),
113 CONSTANT(LOG_KERN),
114 CONSTANT(LOG_LOCAL0),
115 CONSTANT(LOG_LOCAL1),
116 CONSTANT(LOG_LOCAL2),
117 CONSTANT(LOG_LOCAL3),
118 CONSTANT(LOG_LOCAL4),
119 CONSTANT(LOG_LOCAL5),
120 CONSTANT(LOG_LOCAL6),
121 CONSTANT(LOG_LOCAL7),
122 CONSTANT(LOG_LPR),
123 CONSTANT(LOG_MAIL),
124 CONSTANT(LOG_NEWS),
125 CONSTANT(LOG_SYSLOG),
126 CONSTANT(LOG_USER),
127 CONSTANT(LOG_UUCP),
129 /* syslog levels */
130 CONSTANT(LOG_EMERG),
131 CONSTANT(LOG_ALERT),
132 CONSTANT(LOG_CRIT),
133 CONSTANT(LOG_ERR),
134 CONSTANT(LOG_WARNING),
135 CONSTANT(LOG_NOTICE),
136 CONSTANT(LOG_INFO),
137 CONSTANT(LOG_DEBUG),
139 { NULL, 0 }
143 luaopen_syslog(lua_State *L)
145 int n;
146 struct luaL_Reg luasyslog[] = {
147 { "openlog", syslog_openlog },
148 { "syslog", syslog_syslog },
149 { "closelog", syslog_closelog },
150 { "setlogmask", syslog_setlogmask },
151 { NULL, NULL }
154 #if LUA_VERSION_NUM >= 502
155 luaL_newlib(L, luasyslog);
156 #else
157 luaL_register(L, "syslog", luasyslog);
158 #endif
159 syslog_set_info(L);
160 for (n = 0; syslog_constant[n].name != NULL; n++) {
161 lua_pushinteger(L, syslog_constant[n].value);
162 lua_setfield(L, -2, syslog_constant[n].name);
164 return 1;