1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <termios.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "imapfilter.h"
static int ifsys_echo(lua_State *lua);
static int ifsys_noecho(lua_State *lua);
static int ifsys_popen(lua_State *lua);
static int ifsys_pclose(lua_State *lua);
static int ifsys_read(lua_State *lua);
static int ifsys_write(lua_State *lua);
static int ifsys_sleep(lua_State *lua);
static int ifsys_daemon(lua_State *lua);
/* Lua imapfilter library of system's functions. */
static const luaL_reg ifsyslib[] = {
{ "echo", ifsys_echo },
{ "noecho", ifsys_noecho },
{ "popen", ifsys_popen },
{ "pclose", ifsys_pclose },
{ "read", ifsys_read },
{ "write", ifsys_write },
{ "sleep", ifsys_sleep },
{ "daemon", ifsys_daemon },
{ NULL, NULL }
};
/*
* Enable character echoing.
*/
static int
ifsys_echo(lua_State *lua)
{
struct termios t;
if (lua_gettop(lua) != 0)
luaL_error(lua, "wrong number of arguments");
if (tcgetattr(fileno(stdin), &t)) {
fprintf(stderr, "getting term attributs; %s\n",
strerror(errno));
return 1;
}
t.c_lflag |= (ECHO);
t.c_lflag &= ~(ECHONL);
if (tcsetattr(fileno(stdin), TCSAFLUSH, &t)) {
fprintf(stderr, "setting term attributes; %s\n",
strerror(errno));
return 1;
}
return 0;
}
/*
* Disable character echoing.
*/
static int
ifsys_noecho(lua_State *lua)
{
struct termios t;
if (lua_gettop(lua) != 0)
luaL_error(lua, "wrong number of arguments");
if (tcgetattr(fileno(stdin), &t)) {
fprintf(stderr, "getting term attributs; %s\n",
strerror(errno));
return 1;
}
t.c_lflag &= ~(ECHO);
t.c_lflag |= (ECHONL);
if (tcsetattr(fileno(stdin), TCSAFLUSH, &t)) {
fprintf(stderr, "setting term attributes; %s\n",
strerror(errno));
return 1;
}
return 0;
}
/*
* Lua implementation of the POSIX popen() function.
*/
static int
ifsys_popen(lua_State *lua)
{
FILE **fp;
if (lua_gettop(lua) != 2)
luaL_error(lua, "wrong number of arguments");
luaL_checktype(lua, 1, LUA_TSTRING);
luaL_checktype(lua, 2, LUA_TSTRING);
fp = (FILE **) lua_newuserdata(lua, sizeof(FILE *));
*fp = NULL;
*fp = popen(lua_tostring(lua, 1), lua_tostring(lua, 2));
lua_remove(lua, 1);
lua_remove(lua, 1);
return (*fp == NULL ? 0 : 1);
}
/*
* Lua implementation of the POSIX pclose() function.
*/
static int
ifsys_pclose(lua_State *lua)
{
lua_Number r;
FILE *fp;
if (lua_gettop(lua) != 1)
luaL_error(lua, "wrong number of arguments");
luaL_checktype(lua, 1, LUA_TUSERDATA);
fp = *(FILE **) (lua_touserdata(lua, 1));
r = (lua_Number) (pclose(fp) >> 8);
lua_pop(lua, 1);
if (r == -1)
return 0;
fp = NULL;
lua_pushnumber(lua, r);
return 1;
}
/*
* Reads a line from a file stream.
*/
static int
ifsys_read(lua_State *lua)
{
FILE *fp;
luaL_Buffer b;
char *c;
size_t n;
if (lua_gettop(lua) != 1)
luaL_error(lua, "wrong number of arguments");
luaL_checktype(lua, 1, LUA_TUSERDATA);
fp = *(FILE **) (lua_touserdata(lua, 1));
lua_pop(lua, 1);
luaL_buffinit(lua, &b);
for (;;) {
c = luaL_prepbuffer(&b);
if (fgets(c, LUAL_BUFFERSIZE, fp) == NULL && feof(fp)) {
luaL_pushresult(&b);
return (lua_strlen(lua, -1) > 0);
}
n = strlen(c);
if (c[n - 1] != '\n')
luaL_addsize(&b, n);
else {
luaL_addsize(&b, n);
luaL_pushresult(&b);
return 1;
}
}
}
/*
* Writes a string to a file stream.
*/
static int
ifsys_write(lua_State *lua)
{
if (lua_gettop(lua) != 2)
luaL_error(lua, "wrong number of arguments");
luaL_checktype(lua, 1, LUA_TUSERDATA);
luaL_checktype(lua, 2, LUA_TSTRING);
fwrite(lua_tostring(lua, 2), sizeof(char), strlen(lua_tostring(lua,
2)), *(FILE **) (lua_touserdata(lua, 1)));
lua_pop(lua, 2);
return 0;
}
/*
* Lua implementation of the POSIX sleep() function.
*/
static int
ifsys_sleep(lua_State *lua)
{
if (lua_gettop(lua) != 1)
luaL_error(lua, "wrong number of arguments");
luaL_checktype(lua, 1, LUA_TNUMBER);
lua_pushnumber(lua,
(lua_Number) (sleep) ((unsigned int)(lua_tonumber(lua, 1))));
lua_remove(lua, 1);
return 1;
}
/*
* Lua implementation of the BSD daemon() function.
*/
static int
ifsys_daemon(lua_State *lua)
{
if (lua_gettop(lua) != 0)
luaL_error(lua, "wrong number of arguments");
switch (fork()) {
case -1:
fprintf(stderr, "forking; %s\n", strerror(errno));
exit(1);
break;
case 0:
break;
default:
exit(0);
break;
}
if (setsid() == -1) {
fprintf(stderr, "creating session; %s\n", strerror(errno));
exit(1);
}
switch (fork()) {
case -1:
fprintf(stderr, "creating session; %s\n", strerror(errno));
exit(1);
break;
case 0:
break;
default:
exit(0);
break;
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
if (open("/dev/null", O_RDWR) != -1) {
dup(STDIN_FILENO);
dup(STDIN_FILENO);
}
return 0;
}
/*
* Open imapfilter library of system's functions.
*/
LUALIB_API int
luaopen_ifsys(lua_State *lua)
{
luaL_openlib(lua, "ifsys", ifsyslib, 0);
return 1;
}
|