File: luaext_platform.cc

package info (click to toggle)
monotone 0.31-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 20,680 kB
  • ctags: 14,801
  • sloc: cpp: 87,711; ansic: 64,862; sh: 5,691; lisp: 954; perl: 783; makefile: 509; python: 265; sql: 98; sed: 16
file content (111 lines) | stat: -rw-r--r-- 2,203 bytes parent folder | download
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

#include "lua.hh"

#include <signal.h>
#include <cstdlib>

#include "platform.hh"

using std::malloc;
using std::free;

LUAEXT(get_ostype, )
{
  std::string str;
  get_system_flavour(str);
  lua_pushstring(L, str.c_str());
  return 1;
}

LUAEXT(existsonpath, )
{
  const char *exe = luaL_checkstring(L, -1);
  lua_pushnumber(L, existsonpath(exe));
  return 1;
}

LUAEXT(is_executable, )
{
  const char *path = luaL_checkstring(L, -1);
  lua_pushboolean(L, is_executable(path));
  return 1;
}

LUAEXT(make_executable, )
{
  const char *path = luaL_checkstring(L, -1);
  lua_pushnumber(L, make_executable(path));
  return 1;
}

LUAEXT(spawn, )
{
  int n = lua_gettop(L);
  const char *path = luaL_checkstring(L, 1);
  char **argv = (char**)malloc((n+1)*sizeof(char*));
  int i;
  pid_t ret;
  if (argv==NULL)
    return 0;
  argv[0] = (char*)path;
  for (i=1; i<n; i++) argv[i] = (char*)luaL_checkstring(L, i+1);
  argv[i] = NULL;
  ret = process_spawn(argv);
  free(argv);
  lua_pushnumber(L, ret);
  return 1;
}

LUAEXT(spawn_redirected, )
{
  int n = lua_gettop(L);
  char const * infile = luaL_checkstring(L, 1);
  char const * outfile = luaL_checkstring(L, 2);
  char const * errfile = luaL_checkstring(L, 3);
  const char *path = luaL_checkstring(L, 4);
  n -= 3;
  char **argv = (char**)malloc((n+1)*sizeof(char*));
  int i;
  pid_t ret;
  if (argv==NULL)
    return 0;
  argv[0] = (char*)path;
  for (i=1; i<n; i++) argv[i] = (char*)luaL_checkstring(L,  i+4);
  argv[i] = NULL;
  ret = process_spawn_redirected(infile, outfile, errfile, argv);
  free(argv);
  lua_pushnumber(L, ret);
  return 1;
}

LUAEXT(wait, )
{
  pid_t pid = static_cast<pid_t>(luaL_checknumber(L, -1));
  int res;
  int ret;
  ret = process_wait(pid, &res);
  lua_pushnumber(L, res);
  lua_pushnumber(L, ret);
  return 2;
}

LUAEXT(kill, )
{
  int n = lua_gettop(L);
  pid_t pid = static_cast<pid_t>(luaL_checknumber(L, -2));
  int sig;
  if (n>1)
    sig = static_cast<int>(luaL_checknumber(L, -1));
  else
    sig = SIGTERM;
  lua_pushnumber(L, process_kill(pid, sig));
  return 1;
}

LUAEXT(sleep, )
{
  int seconds = static_cast<int>(luaL_checknumber(L, -1));
  lua_pushnumber(L, process_sleep(seconds));
  return 1;
}