Exemplo n.º 1
0
/* diriter ... -- entry */
static int ex_dir(lua_State *L)
{
  const char *pathname;
  DIR **pd;
  struct dirent *d;
  switch (lua_type(L, 1)) {
  default: return luaL_typerror(L, 1, "pathname");
  case LUA_TSTRING:
    pathname = lua_tostring(L, 1);
    lua_pushcfunction(L, ex_dir);       /* pathname ... iter */
    pd = lua_newuserdata(L, sizeof *pd);/* pathname ... iter state */
    *pd = opendir(pathname);
    if (!*pd) return push_error(L);
    luaL_getmetatable(L, DIR_HANDLE);   /* pathname ... iter state M */
    lua_setmetatable(L, -2);            /* pathname ... iter state */
    lua_pushvalue(L, 1);                /* pathname ... iter state pathname */
    diriter_setpathname(L, -2);         /* pathname ... iter state */
    return 2;
  case LUA_TUSERDATA:
    pd = luaL_checkudata(L, 1, DIR_HANDLE);
    do d = readdir(*pd);
    while (d && isdotfile(d->d_name));
    if (!d) { diriter_close(L); return push_error(L); }
    new_dirent(L);                      /* diriter ... entry */
    diriter_getpathname(L, 1);          /* diriter ... entry dir */
    lua_pushstring(L, d->d_name);       /* diriter ... entry dir name */
    lua_pushvalue(L, -1);               /* diriter ... entry dir name name */
    lua_setfield(L, -4, "name");        /* diriter ... entry dir name */
    lua_concat(L, 2);                   /* diriter ... entry fullpath */
    lua_replace(L, 1);                  /* fullpath ... entry */
    lua_replace(L, 2);                  /* fullpath entry ... */
    return ex_dirent(L);
  }
  /*NOTREACHED*/
}
Exemplo n.º 2
0
BOOL WINAPI SQLGetInstalledDrivers(LPSTR lpszBuf, WORD cbBufMax,
               WORD *pcbBufOut)
{
    BOOL ret;
    int size_wbuf = cbBufMax;
    LPWSTR wbuf;
    WORD size_used;

    TRACE("%p %d %p\n", lpszBuf, cbBufMax, pcbBufOut);

    wbuf = HeapAlloc(GetProcessHeap(), 0, size_wbuf*sizeof(WCHAR));
    if (wbuf)
    {
        ret = SQLGetInstalledDriversW(wbuf, size_wbuf, &size_used);
        if (ret)
        {
            if (!(ret = SQLInstall_narrow(2, lpszBuf, wbuf, size_used, cbBufMax, pcbBufOut)))
            {
                push_error(ODBC_ERROR_GENERAL_ERR, odbc_error_general_err);
            }
        }
        HeapFree(GetProcessHeap(), 0, wbuf);
        /* ignore failure; we have achieved the aim */
    }
    else
    {
        push_error(ODBC_ERROR_OUT_OF_MEM, odbc_error_out_of_mem);
        ret = FALSE;
    }
    return ret;
}
Exemplo n.º 3
0
/* pathname/file [entry] -- entry */
static int ex_dirent(lua_State *L)
{
  struct stat st;
  switch (lua_type(L, 1)) {
  default: return luaL_typerror(L, 1, "file or pathname");
  case LUA_TSTRING: {
    const char *name = lua_tostring(L, 1);
    if (-1 == stat(name, &st))
      return push_error(L);
    } break;
  case LUA_TUSERDATA: {
    FILE *f = check_file(L, 1, NULL);
    if (-1 == fstat(fileno(f), &st))
      return push_error(L);
    } break;
  }
  if (lua_type(L, 2) != LUA_TTABLE) {
    lua_settop(L, 1);
    new_dirent(L);
  }
  else {
    lua_settop(L, 2);
  }
  if (S_ISDIR(st.st_mode))
    lua_pushliteral(L, "directory");
  else
    lua_pushliteral(L, "file");
  lua_setfield(L, 2, "type");
  lua_pushnumber(L, st.st_size);
  lua_setfield(L, 2, "size");
  return 1;
}
Exemplo n.º 4
0
static char call_lib(
  fdja_value *node, fdja_value *exe,
  fdja_value *cargs, const char *libname)
{
  // Note: for now, we don't care how many times a lib is read and bound
  // in the current execution...

  char r = 'r'; // 'error' for now
  char *path = NULL;
  char *cnid = NULL;

  // 1. find lib

  if (execution_id == NULL)
  {
    push_error(node, "cannot call lib, execution_id is NULL", NULL);
    goto _over;
  }

  path = locate_lib(libname);

  if (path == NULL)
  {
    push_error(node, "couldn't find lib '%s'", libname, NULL);
    goto _over;
  }

  log_i(node, exe, "lib at %s", path);

  fdja_value *t = fdja_parse_radial_f(path);
  //fdja_putdc(t);

  if (t == NULL)
  {
    push_error(node, "couldn't parse lib at %s", path, NULL);
    goto _over;
  }

  // 2. place lib in nodes{}

  size_t lc = libcounter_next();
  fdja_value *n = fdja_o("tree", t, NULL);

  fdja_pset(execution, "nodes.%zx", lc, n);

  // 4. call lib

  cnid = flu_sprintf("%zu-%zu", lc, counter_next());
  queue_child_execute(cnid, node, exe, fdja_clone(t), NULL);

  r = 'k';

_over:

  fdja_free(cargs);
  free(path);
  free(cnid);

  return r;
}
Exemplo n.º 5
0
/* This is implemented sensibly rather than according to exact conformance to Microsoft's buggy implementations
 * e.g. The Microsoft one occasionally actually adds a third nul character (possibly beyond the buffer).
 * e.g. If the key has no drivers then version 3.525.1117.0 does not modify the buffer at all, not even a nul character.
 */
BOOL WINAPI SQLGetInstalledDriversW(LPWSTR lpszBuf, WORD cbBufMax,
               WORD *pcbBufOut)
{
    HKEY hDrivers; /* Registry handle to the Drivers key */
    LONG reg_ret; /* Return code from registry functions */
    BOOL success = FALSE; /* The value we will return */

    clear_errors();

    TRACE("%p %d %p\n", lpszBuf, cbBufMax, pcbBufOut);

    if (!lpszBuf || cbBufMax == 0)
    {
        push_error(ODBC_ERROR_INVALID_BUFF_LEN, odbc_error_invalid_buff_len);
    }
    else if ((reg_ret = RegOpenKeyExW (HKEY_LOCAL_MACHINE /* The drivers does not depend on the config mode */,
            drivers_key, 0, KEY_READ /* Maybe overkill */,
            &hDrivers)) == ERROR_SUCCESS)
    {
        DWORD index = 0;
        cbBufMax--;
        success = TRUE;
        while (cbBufMax > 0)
        {
            DWORD size_name;
            size_name = cbBufMax;
            if ((reg_ret = RegEnumValueW(hDrivers, index, lpszBuf, &size_name, NULL, NULL, NULL, NULL)) == ERROR_SUCCESS)
            {
                index++;
                assert (size_name < cbBufMax && *(lpszBuf + size_name) == 0);
                size_name++;
                cbBufMax-= size_name;
                lpszBuf+=size_name;
            }
            else
            {
                if (reg_ret != ERROR_NO_MORE_ITEMS)
                {
                    success = FALSE;
                    push_error(ODBC_ERROR_GENERAL_ERR, odbc_error_general_err);
                }
                break;
            }
        }
        *lpszBuf = 0;
        if ((reg_ret = RegCloseKey (hDrivers)) != ERROR_SUCCESS)
            TRACE ("Error %d closing ODBC Drivers key\n", reg_ret);
    }
    else
    {
        /* MSDN states that it returns failure with COMPONENT_NOT_FOUND in this case.
         * Version 3.525.1117.0 (Windows 2000) does not; it actually returns success.
         * I doubt if it will actually be an issue.
         */
        push_error(ODBC_ERROR_COMPONENT_NOT_FOUND, odbc_error_component_not_found);
    }
    return success;
}
Exemplo n.º 6
0
Arquivo: nfct.c Projeto: kelvie/libnet
/*-
-- cthandle = nfct.callback_register(cthandle, ctmsgtype)

ctmsgtype is one of "new", "update", "destroy", or "all" (default is "all").

Only one registration can be active at a time, the latest call replaces
any previous ones.

Returns cthandle on success, nil,emsg,errno on failure.
*/
static int callback_register(lua_State* L)
{
    struct nfct_handle* cthandle = check_cthandle(L);
    static const char* msgtype_opts[] = {
        "new", "update", "destroy", "all", "error", NULL
    };
    static enum nf_conntrack_msg_type msgtype_vals[] = {
        NFCT_T_NEW, NFCT_T_UPDATE, NFCT_T_DESTROY, NFCT_T_ALL, NFCT_T_ERROR,
    };
    int msgtype_opt = luaL_checkoption(L, 2, "all", msgtype_opts);
    enum nf_conntrack_msg_type msgtype_val = msgtype_vals[msgtype_opt];
    int ret;

    /* Clear any current handler to avoid memory leaks. */
    nfct_callback_unregister(cthandle);

    ret = nfct_callback_register(cthandle, msgtype_val, cb, L);

    if(ret < 0) {
        push_error(L);
        return 3;
    }

    lua_pushvalue(L, 1);

    return 1;
}
Exemplo n.º 7
0
/// push error if needed.
// @param L the state
// @param bval 0 or 1
// @return 1; a boolean true value, or 2, `nil`, last Windows error
// @function push_bool
int push_bool(lua_State *L, int bval) {
    if (bval) {
        return push_ok(L);
    } else {
        return push_error(L);
    }
}
Exemplo n.º 8
0
static int file_lock(lua_State *L,
                     FILE *f, const char *mode, long offset, long length)
{
  static const ULARGE_INTEGER zero_len;
  static const OVERLAPPED zero_ov;
  HANDLE h = file_handle(f);
  ULARGE_INTEGER len = zero_len;
  OVERLAPPED ov = zero_ov;
  DWORD flags = 0;
  BOOL ret;
  if (length) len.LowPart = length;
  else len.LowPart = len.HighPart = -1;
  ov.Offset = offset;
  switch (*mode) {
    case 'w':
      flags = LOCKFILE_EXCLUSIVE_LOCK;
      /*FALLTHRU*/
    case 'r':
      flags |= LOCKFILE_FAIL_IMMEDIATELY;
      ret = LockFileEx(h, flags, 0, len.LowPart, len.HighPart, &ov);
      break;
    case 'u':
      ret = UnlockFileEx(h, 0, len.LowPart, len.HighPart, &ov);
      break;
    default:
      return luaL_error(L, "invalid mode");
  }
  if (!ret)
    return push_error(L);
  /* return the file */
  lua_settop(L, 1);
  return 1;
}
Exemplo n.º 9
0
static int lconn_get_reply(lua_State * L)
{
  redisContext * pContext = check_connection(L, 1);

  int nret = 0;

  redisReply * pReply = NULL;

  int ok = redisGetReply(pContext, (void **)&pReply);
  if (ok != REDIS_OK || pReply == NULL)
  {
    /* TODO: Shouldn't we clear the context error state somehow after this? */
    return push_error(L, pContext);
  }

  nret = push_reply(L, pReply);

  /*
  * TODO: Not entirely safe: if above code throws error, reply object is leaked.
  */
  freeReplyObject(pReply);
  pReply = NULL;

  return nret;
}
Exemplo n.º 10
0
/* pathname -- true/nil error */
static int ex_remove(lua_State *L)
{
  const char *pathname = luaL_checkstring(L, 1);
  struct stat attr;
  if (stat(pathname, &attr) == -1)
    return push_error(L);
  if (attr.st_mode & S_IFDIR) {
    if (!path_destroy(pathname))
      return push_error(L);
  } else {
    if (remove(pathname) == -1)
      return push_error(L);
  }
  lua_pushboolean(L, 1);
  return 1;
}
Exemplo n.º 11
0
static int lconn_command(lua_State * L)
{
  redisContext * pContext = check_connection(L, 1);

  const char * argv[LUAHIREDIS_MAXARGS];
  size_t argvlen[LUAHIREDIS_MAXARGS];
  int nargs = load_args(L, pContext, 2, argv, argvlen);

  int nret = 0;

  redisReply * pReply = (redisReply *)redisCommandArgv(
      pContext, nargs, argv, argvlen
    );
  if (pReply == NULL)
  {
    /* TODO: Shouldn't we clear the context error state somehow after this? */
    return push_error(L, pContext);
  }

  nret = push_reply(L, pReply);

  /*
  * TODO: Not entirely safe: if above code throws error, reply object is leaked.
  */
  freeReplyObject(pReply);
  pReply = NULL;

  return nret;
}
Exemplo n.º 12
0
/* pathname -- true/nil error */
static int ex_chdir(lua_State *L)
{
  const char *pathname = luaL_checkstring(L, 1);
  if (!SetCurrentDirectory(pathname))
    return push_error(L);
  lua_pushboolean(L, 1);
  return 1;
}
Exemplo n.º 13
0
/* pathname -- true/nil error */
static int ex_remove(lua_State *L)
{
  const char *pathname = luaL_checkstring(L, 1);
  DWORD attr = GetFileAttributes(pathname);
  if (attr == (DWORD)-1)
    return push_error(L);
  if (attr & FILE_ATTRIBUTE_DIRECTORY) {
    if (!path_destroy(pathname))
		return push_error(L);
  } else {
    SetFileAttributes(pathname, FILE_ATTRIBUTE_ARCHIVE);
    if (!DeleteFile(pathname))
      return push_error(L);
  }
  lua_pushboolean(L, 1);
  return 1;
}
Exemplo n.º 14
0
/* pathname -- true/nil error */
static int ex_chdir(lua_State *L)
{
  const char *pathname = luaL_checkstring(L, 1);
  if (-1 == chdir(pathname))
    return push_error(L);
  lua_pushboolean(L, 1);
  return 1;
}
Exemplo n.º 15
0
/* -- pathname/nil error */
static int ex_getcwd(lua_State *L)
{
  char pathname[PATH_MAX + 1];
  if (!getcwd(pathname, sizeof pathname))
    return push_error(L);
  lua_pushstring(L, pathname);
  return 1;
}
Exemplo n.º 16
0
/* -- pathname/nil error */
static int ex_getcwd(lua_State *L)
{
  char pathname[MAX_PATH + 1];
  size_t len = GetCurrentDirectory(sizeof pathname, pathname);
  if (len == 0) return push_error(L);
  lua_pushlstring(L, pathname, len);
  return 1;
}
Exemplo n.º 17
0
/* pathname -- true/nil error */
int ex_mkdir(lua_State *L)
{
  const char *pathname = luaL_checkstring(L, 1);
  if (!path_create(pathname))
    return push_error(L);
  lua_pushboolean(L, 1);
  return 1;
}
Exemplo n.º 18
0
/// get the text on the clipboard.
// @return the text
// @function get_clipboard
def get_clipboard() {
  HGLOBAL glob;
  LPCWSTR p;
  if (! OpenClipboard(NULL)) {
    return push_error(L);
  }
  glob = GetClipboardData(CF_UNICODETEXT);
  if (glob == NULL) {
    CloseClipboard();
    return push_error(L);
  }
  p = GlobalLock(glob);
  push_wstring(L,p);
  GlobalUnlock(glob);
  CloseClipboard();
  return 1;
}
Exemplo n.º 19
0
/* name value -- true/nil error
 * name nil -- true/nil error */
static int ex_setenv(lua_State *L)
{
  const char *nam = luaL_checkstring(L, 1);
  const char *val = lua_tostring(L, 2);
  int err = val ? setenv(nam, val, 1) : unsetenv(nam);
  if (err == -1) return push_error(L);
  lua_pushboolean(L, 1);
  return 1;
}
Exemplo n.º 20
0
static int lhiredis_connect(lua_State * L)
{
  luahiredis_Connection * pResult = NULL;
  redisContext * pContext = NULL;

  const char * host_or_socket = luaL_checkstring(L, 1);

  /* TODO: Support Timeout and UnixTimeout flavors */
  if (lua_isnoneornil(L, 2))
  {
    pContext = redisConnectUnix(host_or_socket);
  }
  else
  {
    pContext = redisConnect(host_or_socket, luaL_checkint(L, 2));
  }

  if (!pContext)
  {
    luaL_checkstack(L, 2, "not enough stack to push error");
    lua_pushnil(L);
    lua_pushliteral(L, "failed to create hiredis context");
    return 2;
  }

  if (pContext->err)
  {
    int result = push_error(L, pContext);

    redisFree(pContext);
    pContext = NULL;

    return result;
  }

  luaL_checkstack(L, 1, "not enough stack to create connection");
  pResult = (luahiredis_Connection *)lua_newuserdata(
      L, sizeof(luahiredis_Connection)
    );
  pResult->pContext = pContext;

  if (luaL_newmetatable(L, LUAHIREDIS_CONN_MT))
  {
    /* Module table to be set as upvalue */
    luaL_checkstack(L, 1, "not enough stack to register connection MT");

    lua_pushvalue(L, lua_upvalueindex(1));
    setfuncs(L, M, 1);

    lua_pushvalue(L, -1);
    lua_setfield(L, -2, "__index");
  }

  lua_setmetatable(L, -2);

  return 1;
}
Exemplo n.º 21
0
 /// get a thread's priority
 // @function get_priority
 def get_priority() {
   int res = GetThreadPriority(this->thread);
   if (res != THREAD_PRIORITY_ERROR_RETURN) {
     lua_pushinteger(L,res);
     return 1;
   } else {
     return push_error(L);
   }
 }
Exemplo n.º 22
0
/* name value -- true/nil error
 * name nil -- true/nil error */
static int ex_setenv(lua_State *L)
{
  const char *nam = luaL_checkstring(L, 1);
  const char *val = lua_tostring(L, 2);
  if (!SetEnvironmentVariable(nam, val))
    return push_error(L);
  lua_pushboolean(L, 1);
  return 1;
}
Exemplo n.º 23
0
int main() {
    auto expected = error_function();
    printf("You should see the same message printed twice.\n");
    print(expected.error);
    printf("%s\n", tocstr(expected.error));
    print_all(expected.push_error(2, "stacking another error"));

    return 0;
}
Exemplo n.º 24
0
/* name -- value/nil */
static int ex_getenv(lua_State *L)
{
  const char *nam = luaL_checkstring(L, 1);
  char *val = getenv(nam);
  if (!val)
    return push_error(L);
  lua_pushstring(L, val);
  return 1;
}
Exemplo n.º 25
0
/* pathname/file [entry] -- entry */
static int ex_dirent(lua_State *L)
{
  int isdir;
  lua_Number size;
  DWORD attr;
  switch (lua_type(L, 1)) {
  default: return luaL_typerror(L, 1, "file or pathname");
  case LUA_TSTRING: {
    const char *name = lua_tostring(L, 1);
    attr = GetFileAttributes(name);
    if (attr == (DWORD)-1)
      return push_error(L);
    isdir = attr & FILE_ATTRIBUTE_DIRECTORY;
    if (isdir)
      size = 0;
    else
      size = get_file_size(name);
    } break;
  case LUA_TUSERDATA: {
    FILE *f = check_file(L, 1, NULL);
    BY_HANDLE_FILE_INFORMATION info;
    if (!GetFileInformationByHandle(file_handle(f), &info))
      return push_error(L);
    attr = info.dwFileAttributes;
    isdir = attr & FILE_ATTRIBUTE_DIRECTORY;
    size = qword_to_number(info.nFileSizeHigh, info.nFileSizeLow);
    } break;
  }
  if (lua_type(L, 2) != LUA_TTABLE) {
    lua_settop(L, 1);
    new_dirent(L);
  }
  else {
    lua_settop(L, 2);
  }
  if (isdir)
    lua_pushliteral(L, "directory");
  else
    lua_pushliteral(L, "file");
  lua_setfield(L, 2, "type");
  lua_pushnumber(L, size);
  lua_setfield(L, 2, "size");
  return 1;
}
Exemplo n.º 26
0
Arquivo: nfct.c Projeto: kelvie/libnet
/*-
-- cthandle = nfct.open(subsys, [subscription...])

subsys is "track" or "expect"

subscription is the groups for which notifications are requested, zero or more of
"none", "new", "update", "destroy", or "all" (default is "none").

Returns a conntrack handle on success, or nil,emsg,errno on failure.

There is no garbage collection, nfct.fini() must be called on the handle to
release it's resources.
*/
static int hopen(lua_State *L)
{
    static const char* subsys_opts[] = {
        "track", "expect", NULL
    };
    static u_int8_t subsys_vals[] = {
        NFNL_SUBSYS_CTNETLINK, NFNL_SUBSYS_CTNETLINK_EXP,
    };
    int subsys_opt = luaL_checkoption(L, 1, NULL, subsys_opts);
    u_int8_t subsys_val = subsys_vals[subsys_opt];
    static const char*  subscription_opts[] = {
        "none", "new", "update", "destroy", "all", NULL
    };
    unsigned subscription_vals[2][5] = {
        {   /* [0] == "track" */
            0,
            NF_NETLINK_CONNTRACK_NEW,
            NF_NETLINK_CONNTRACK_UPDATE,
            NF_NETLINK_CONNTRACK_DESTROY,
            NFCT_ALL_CT_GROUPS
        },
        {   /* [1] == "expect" */
            0,
            NF_NETLINK_CONNTRACK_EXP_NEW,
            NF_NETLINK_CONNTRACK_EXP_UPDATE,
            NF_NETLINK_CONNTRACK_EXP_DESTROY,
            NF_NETLINK_CONNTRACK_EXP_NEW
            |NF_NETLINK_CONNTRACK_EXP_UPDATE
            |NF_NETLINK_CONNTRACK_EXP_DESTROY
        }
    };
    unsigned subscription_val = 0;
    int narg = 0;
    struct nfct_handle* ct = NULL;

    /* the check option should have ensured that the opt index is 0 or 1,
     * so we can safely use it to index into the watfor vals
     */
    assert(subsys_opt == 0 || subsys_opt == 1);

    for(narg = 2; narg <= lua_gettop(L); narg++) {
        int subscription_opt = luaL_checkoption(L, narg, NULL, subscription_opts);
        subscription_val |= subscription_vals[subsys_opt][subscription_opt];
    }

    ct = nfct_open(subsys_val, subscription_val);

    if(!ct) {
        push_error(L);
        return 3;
    }

    lua_pushlightuserdata(L, ct);

    return 1;
}
Exemplo n.º 27
0
 /// get the window class name.
 // Useful to find all instances of a running program, when you
 // know the class of the top level window.
 // @function get_class_name
 def get_class_name() {
   static char buff[1024];
   int n = GetClassName(this->hwnd,buff,sizeof(buff));
   if (n > 0) {
     lua_pushstring(L,buff);
     return 1;
   } else {
     return push_error(L);
   }
 }
Exemplo n.º 28
0
/// copy text onto the clipboard.
// @param text the text
// @function set_clipboard
def set_clipboard(Str text) {
  HGLOBAL glob;
  LPWSTR p;
  int bufsize = 3*strlen(text);
  if (! OpenClipboard(NULL)) {
    return push_error(L);
  }
  EmptyClipboard();
  glob = GlobalAlloc(GMEM_MOVEABLE, bufsize);
  p = (LPWSTR)GlobalLock(glob);
  wstring_buff(text,p,bufsize);
  GlobalUnlock(glob);
  if (SetClipboardData(CF_UNICODETEXT,glob) == NULL) {
    CloseClipboard();
    return push_error(L);
  }
  CloseClipboard();
  return 0;
}
Exemplo n.º 29
0
/* -- in out/nil error */
static int ex_pipe(lua_State *L)
{
  int fd[2];
  if (-1 == pipe(fd))
    return push_error(L);
  closeonexec(fd[0]);
  closeonexec(fd[1]);
  new_file(L, fd[0], "r");
  new_file(L, fd[1], "w");
  return 2;
}
Exemplo n.º 30
0
/* pathname -- true/nil error */
static int ex_chmod(lua_State *L)
{
  const char *pathname = luaL_checkstring(L, 1);
  int inmode = luaL_checkint(L, 2);
  int mode = (((inmode / 100) % 10) * 64) + (((inmode / 10) % 10) * 8) +
	(inmode % 10);
  if (-1 == chmod(pathname, mode))
    return push_error(L);
  lua_pushboolean(L, 1);
  return 1;
}