Example #1
0
static int luaB_towstring (lua_State *L) {
	char buff[64];
	luaL_checkany(L, 1);
	if (luaL_callmeta(L, 1, "__towstring"))  /* is there a metafield? */
		return 1;  /* use its value */
	switch (lua_type(L, 1)) {
	case LUA_TNUMBER:
		lua_pushwstring(L, lua_towstring(L, 1));
		break;
	case LUA_TSTRING: {
		luaL_Buffer b;
		size_t l;
		size_t i;
		const char *s = lua_tostring(L, 1);
		l = lua_strlen(L, 1);
		if (l == 0)
		{
			lua_WChar str[] = { '\0' };
			lua_pushwstring(L, (const lua_WChar*)str);
		}
		else
		{
			luaL_wbuffinit(L, &b);
			for (i=0; i<l; i++)
				luaL_addwchar(&b, (lua_WChar)(unsigned char)s[i]);
			luaL_pushresult(&b);
		}
		return 1;
    }
	case LUA_TWSTRING:
		lua_pushvalue(L, 1);
		return 1;
	case LUA_TBOOLEAN:
		strcpy(buff, (lua_toboolean(L, 1) ? "true" : "false"));
		break;
	case LUA_TNIL:
		strcpy(buff, "nil");
		break;
	default:
		sprintf(buff, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1));
		break;
	}

	{
		luaL_Buffer b;
		size_t l;
		size_t i;
		const lua_WChar *s = lua_towstring(L, 1);
		l = lua_strlen(L, 1);
		luaL_wbuffinit(L, &b);
		for (i=0; i<l; i++)
			luaL_addwchar(&b, (lua_WChar)(s[i]));
		luaL_pushresult(&b);
	}

	return 1;
}
Example #2
0
static void add_s (MatchState *ms, luaL_Buffer *b,
                   const lua_WChar *s, const lua_WChar *e) {
  lua_State *L = ms->L;
  if (lua_iswstring(L, 3)) {
    const lua_WChar *news = lua_towstring(L, 3);
    size_t l = lua_strlen(L, 3);
    size_t i;
    for (i=0; i<l; i++) {
      if (news[i] != ESC)
        luaL_putwchar(b, news[i]);
      else {
        i++;  /* skip ESC */
        if (!iswdigit(news[i]))
          luaL_putwchar(b, news[i]);
        else {
          int level = check_capture(ms, news[i]);
          push_onecapture(ms, level);
          luaL_addvalue(b);  /* add capture to accumulated result */
        }
      }
    }
  }
  else {  /* is a function */
    int n;
    lua_pushvalue(L, 3);
    n = push_captures(ms, s, e);
    lua_call(L, n, 1);
    if (lua_iswstring(L, -1))
      luaL_addvalue(b);  /* add return to accumulated result */
    else
      lua_pop(L, 1);  /* function result is not a string: pop it */
  }
}
Example #3
0
static int luaB_tostring (lua_State *L) {
  char buff[128];
  luaL_checkany(L, 1);
  if (luaL_callmeta(L, 1, "__tostring"))  /* is there a metafield? */
    return 1;  /* use its value */
  switch (lua_type(L, 1)) {
    case LUA_TNUMBER:
      lua_pushstring(L, lua_tostring(L, 1));
      return 1;
    case LUA_TSTRING:
      lua_pushvalue(L, 1);
      return 1;
    case LUA_TWSTRING: {
	  luaL_Buffer b;
	  size_t l;
	  size_t i;
	  const lua_WChar *s = lua_towstring(L, 1);
	  l = lua_strlen(L, 1);
	  if (l == 0)
	  {
	      lua_pushstring(L, "");
	  }
	  else
	  {
		  luaL_buffinit(L, &b);
		  for (i=0; i<l; i++)
			luaL_putchar(&b, (unsigned char)(s[i]));
		  luaL_pushresult(&b);
	  }
      return 1;
    }
    case LUA_TBOOLEAN:
      lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
      return 1;
    case LUA_TTABLE:
      sprintf(buff, "table: %p", lua_topointer(L, 1));
      break;
    case LUA_TFUNCTION:
      sprintf(buff, "function: %p", lua_topointer(L, 1));
      break;
    case LUA_TUSERDATA:
    case LUA_TLIGHTUSERDATA:
      sprintf(buff, "userdata: %p", lua_touserdata(L, 1));
      break;
    case LUA_TTHREAD:
      sprintf(buff, "thread: %p", (void *)lua_tothread(L, 1));
      break;
    case LUA_TNIL:
      lua_pushliteral(L, "nil");
      return 1;
  }
  lua_pushstring(L, buff);
  return 1;
}
Example #4
0
static int gfind_aux (lua_State *L) {
  MatchState ms;
  const lua_WChar *s = lua_towstring(L, lua_upvalueindex(1));
  size_t ls = lua_strlen(L, lua_upvalueindex(1));
  const lua_WChar *p = lua_towstring(L, lua_upvalueindex(2));
  const lua_WChar *src;
  ms.L = L;
  ms.src_init = s;
  ms.src_end = s+ls;
  for (src = s + (size_t)lua_tonumber(L, lua_upvalueindex(3));
       src <= ms.src_end;
       src++) {
    const lua_WChar *e;
    ms.level = 0;
    if ((e = match(&ms, src, p)) != NULL) {
      int newstart = e-s;
      if (e == src) newstart++;  /* empty match? go at least one position */
      lua_pushnumber(L, newstart);
      lua_replace(L, lua_upvalueindex(3));
      return push_captures(&ms, src, e);
    }
  }
  return 0;  /* not found */
}
Example #5
0
static void infIAT(lua_State* L, hook_t* h) {
	h->type = hook_t::type::IAT;
	const char* dllname = luaL_checkstring(L, 3);
	const char* apiname = nullptr;
	if (lua_isinteger(L, 4)) {
		apiname = (const char*)luaL_checkinteger(L, 4);
	}
	else {
		apiname = luaL_checkstring(L, 4);
	}
	if (lua_isinteger(L, 2)) {
		h->real = base::hook::iat((HMODULE)luaL_checkinteger(L, 2), dllname, apiname, h->fake);
	}
	else {
		std::wstring modulename = lua_towstring(L, 2);
		h->real = base::hook::iat(modulename.c_str(), dllname, apiname, h->fake);
	}
}
Example #6
0
/*
** If your system does not support `stdout', you can just remove this function.
** If you need, you can define your own `print' function, following this
** model but changing `fputs' to put the strings at a proper place
** (a console window or a log file, for instance).
*/
static int luaB_print (lua_State *L) {
  int n = lua_gettop(L);  /* number of arguments */
  int i;
  lua_getglobal(L, "towstring");
  lua_getglobal(L, "tostring");
  for (i=1; i<=n; i++) {
    const char *s = NULL;
	const lua_WChar *ws = NULL;
	if (lua_type(L, i) != LUA_TWSTRING) {
      lua_pushvalue(L, -1);  /* function to be called */
      lua_pushvalue(L, i);   /* value to print */
      lua_call(L, 1, 1);
      s = lua_tostring(L, -1);  /* get result */
      if (s == NULL)
        return luaL_error(L, LUA_QL("tostring") " must return a string to "
                             LUA_QL("print"));
	} else {
		lua_pushvalue(L, -2);  /* function to be called */
		lua_pushvalue(L, i);   /* value to print */
		lua_call(L, 1, 1);
		ws = lua_towstring(L, -1);  /* get result */
		if (ws == NULL)
			return luaL_error(L, "`tostring' must return a string to `print'");
	}
    if (i>1) fputs("\t", stdout);
	if (s)
      fputs(s, stdout);
	else {
      wchar_t out[512];
      wchar_t* outEnd = out + sizeof(out) - 2;
      while (*ws) {
        wchar_t* outPos = out;
        while (*ws && outPos != outEnd) {
          *outPos++ = *ws++;
        }
        *outPos++ = 0;
        fputws(out, stdout);
      }
    }
    lua_pop(L, 1);  /* pop result */
  }
  fputs("\n", stdout);
  return 0;
}
Example #7
0
/*
** If your system does not support `stdout', you can just remove this function.
** If you need, you can define your own `print' function, following this
** model but changing `fputs' to put the strings at a proper place
** (a console window or a log file, for instance).
*/
static int luaB_print (lua_State *L) {
  int n = lua_gettop(L);  /* number of arguments */
  int i;
  lua_getglobal(L, "towstring");
  lua_getglobal(L, "tostring");
  for (i=1; i<=n; i++) {
    const char *s = NULL;
    const lua_WChar *ws = NULL;
	if (lua_type(L, i) != LUA_TWSTRING)
	{
		lua_pushvalue(L, -1);  /* function to be called */
		lua_pushvalue(L, i);   /* value to print */
		lua_call(L, 1, 1);
		s = lua_tostring(L, -1);  /* get result */
		if (s == NULL)
          return luaL_error(L, "`tostring' must return a string to `print'");
	}
	else
	{
		lua_pushvalue(L, -2);  /* function to be called */
		lua_pushvalue(L, i);   /* value to print */
		lua_call(L, 1, 1);
	  ws = lua_towstring(L, -1);  /* get result */
	  if (ws == NULL)
          return luaL_error(L, "`tostring' must return a string to `print'");
	}
    if (i>1) fputs("\t", stdout);
	if (s)
      fputs(s, stdout);
	else
	  fputws(ws, stdout);
    lua_pop(L, 1);  /* pop result */
  }
  fputs("\n", stdout);
  return 0;
}
Example #8
0
static int wgmatch_aux (lua_State *L) {
    WMatchState ms;
    size_t ls;
    const lua_WChar *s = lua_tolwstring(L, lua_upvalueindex(1), &ls);
    const lua_WChar *p = lua_towstring(L, lua_upvalueindex(2));
    const lua_WChar *src;
    ms.L = L;
    ms.src_init = s;
    ms.src_end = s+ls;
    for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));
            src <= ms.src_end;
            src++) {
        const lua_WChar *e;
        ms.level = 0;
        if ((e = wmatch(&ms, src, p)) != NULL) {
            lua_Integer newstart = e-s;
            if (e == src) newstart++;  /* empty wmatch? go at least one position */
            lua_pushinteger(L, newstart);
            lua_replace(L, lua_upvalueindex(3));
            return wpush_captures(&ms, src, e);
        }
    }
    return 0;  /* not found */
}
Example #9
0
static int g_read (lua_State *L, FILE *f, int first) {
  int nargs = lua_gettop(L) - 1;
  int success;
  int n;
  if (nargs == 0) {  /* no arguments? */
    success = read_line(L, f);
    n = first+1;  /* to return 1 result */
  }
  else {  /* ensure stack space for all results and for auxlib's buffer */
    luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
    success = 1;
    for (n = first; nargs-- && success; n++) {
      if (lua_type(L, n) == LUA_TNUMBER) {
        size_t l = (size_t)lua_tonumber(L, n);
        success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
      }
      else if (lua_type(L, n) == LUA_TSTRING) {
        const char *p = lua_tostring(L, n);
        luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
        switch (p[1]) {
          case 'n':  /* number */
            success = read_number(L, f);
            break;
          case 'l':  /* line */
            success = read_line(L, f);
            break;
          case 'a':  /* file */
            read_chars(L, f, ~((size_t)0));  /* read MAX_SIZE_T chars */
            success = 1; /* always success */
            break;
          case 'w':  /* word */
            return luaL_error(L, "obsolete option `*w' to 'read'");
          default:
            return luaL_argerror(L, n, "invalid format");
        }
      } else if (lua_type(L, n) == LUA_TWSTRING) {
        const lua_WChar *p = lua_towstring(L, n);
        if (!p || p[0] != '*')
          return luaL_error(L, "invalid `read' option");
        switch (p[1]) {
          case 'n':  /* number */
            success = uread_number(L, f);
            break;
          case 'l':  /* line */
            success = uread_line(L, f);
            break;
          case 'a':  /* file */
            uread_chars(L, f, ~((size_t)0));  /* read MAX_SIZE_T chars */
            success = 1; /* always success */
            break;
          case 'w':  /* word */
            return luaL_error(L, "obsolete option `*w' to 'read'");
            break;
          default:
            return luaL_argerror(L, n, "invalid format");
        }
      }
    }
  }
  if (!success) {
    lua_pop(L, 1);  /* remove last result */
    lua_pushnil(L);  /* push nil instead */
  }
  return n - first;
}
Example #10
0
static int LS_LOG( LuaState* state )
{
	lua_State* L = *state;
	int n = lua_gettop(L);  /* number of arguments */
	int i;
	lua_getglobal(L, "towstring");
	lua_getglobal(L, "tostring");
	for (i=1; i<=n; i++) {
		const char *s = NULL;
		const lua_WChar *ws = NULL;
		if (lua_type(L, i) != LUA_TWSTRING)
		{
			lua_pushvalue(L, -1);  /* function to be called */
			lua_pushvalue(L, i);   /* value to print */
			lua_call(L, 1, 1);
			s = lua_tostring(L, -1);  /* get result */
			if (s == NULL)
				return luaL_error(L, "`tostring' must return a string to `print'");
		}
		else
		{
			lua_pushvalue(L, -2);  /* function to be called */
			lua_pushvalue(L, i);   /* value to print */
			lua_call(L, 1, 1);
			ws = lua_towstring(L, -1);  /* get result */
			if (ws == NULL)
				return luaL_error(L, "`tostring' must return a string to `print'");
		}
		if (i>1)
		{
#ifdef WIN32
//			OutputDebugStringA("\t");
#else
			fputs("\t", stdout);
#endif
		}
		if (s)
		{
#ifdef WIN32
			OutputDebugStringA(s);
			throw std::exception(s);
#else
			fputs(s, stdout);
#endif
		}
		else
		{
#ifdef WIN32
			OutputDebugStringW((LPCWSTR)ws);
#else
//			fputws(ws, stdout);
#endif
		}
		lua_pop(L, 1);  /* pop result */
	}

#ifdef WIN32
	OutputDebugStringA("\n");
#else
	fputs("\n", stdout);
#endif

	return 0;
}
Example #11
0
static int luaB_towstring (lua_State *L) {
  char buff[64];
  switch (lua_type(L, 1)) {
    case LUA_TNUMBER:
      lua_pushwstring(L, lua_towstring(L, 1));
      return 1;
    case LUA_TSTRING: {
	 luaL_Buffer b;
	 size_t l;
	 size_t i;
	 const char *s = lua_tostring(L, 1);
	 l = lua_strlen(L, 1);
	 if (l == 0)
	 {
	     lua_pushwstring(L, L"");
	 }
	 else
	 {
		 luaL_wbuffinit(L, &b);
		 for (i=0; i<l; i++)
			luaL_putwchar(&b, (lua_WChar)s[i]);
		 luaL_pushresult(&b);
	 }
      return 1;
    }
    case LUA_TWSTRING:
      lua_pushvalue(L, 1);
      return 1;
    case LUA_TTABLE:
      sprintf(buff, "%.40s: %p", lua_type(L, 1), lua_topointer(L, 1));
      break;
    case LUA_TFUNCTION:
      sprintf(buff, "function: %p", lua_topointer(L, 1));
      break;
    case LUA_TUSERDATA: {
      const char *t = lua_typename(L, lua_type(L, 1));
      if (strcmp(t, "userdata") == 0)
        sprintf(buff, "userdata: %p", lua_touserdata(L, 1));
      else
        sprintf(buff, "%.40s: %p", t, lua_touserdata(L, 1));
      break;
    }
    case LUA_TNIL:
      lua_pushliteral(L, "nil");
      return 1;
    case LUA_TTHREAD:
      sprintf(buff, "thread: %p", (void *)lua_tothread(L, 1));
      break;
    default:
      luaL_argerror(L, 1, "value expected");
  }

  {
	 luaL_Buffer b;
	 size_t l;
	 size_t i;
	 const lua_WChar *s = lua_towstring(L, 1);
	 l = lua_strlen(L, 1);
	 luaL_wbuffinit(L, &b);
	 for (i=0; i<l; i++)
		luaL_putwchar(&b, (lua_WChar)(s[i]));
	 luaL_pushresult(&b);
  }

/*  lua_pushstring(L, buff);*/
  return 1;
}
Example #12
0
LUALIB_API const lua_WChar *luaL_checklwstring (lua_State *L, int narg, size_t *len) {
  const lua_WChar *s = lua_towstring(L, narg);
  if (!s) tag_error(L, narg, LUA_TWSTRING);
  if (len) *len = lua_strlen(L, narg);
  return s;
}
Example #13
0
static int LS_LOG( lua_State* L )
{
	int n = lua_gettop(L);  /* number of arguments */
	int i;
	lua_getglobal(L, "towstring");
	lua_getglobal(L, "tostring");
	for (i=1; i<=n; i++) {
		const char *s = NULL;
		const lua_WChar *ws = NULL;
		if (lua_type(L, i) == LUA_TSTRING)
		{
			s = lua_tostring(L, -1);
		}
		else if (lua_type(L, i) != LUA_TWSTRING)
		{
			lua_pushvalue(L, -1);  /* function to be called */
			lua_pushvalue(L, i);   /* value to print */
			lua_call(L, 1, 1);
			s = lua_tostring(L, -1);  /* get result */
			if (s == NULL)
				return luaL_error(L, "`tostring' must return a string to `print'");
		}
		else
		{
			lua_pushvalue(L, -2);  /* function to be called */
			lua_pushvalue(L, i);   /* value to print */
			lua_call(L, 1, 1);
			ws = lua_towstring(L, -1);  /* get result */
			if (ws == NULL)
				return luaL_error(L, "`tostring' must return a string to `print'");
		}
		if (i>1)
		{
#ifdef WIN32
			OutputDebugStringA("\t");
#else
			fputs("\t", stdout);
#endif
		}
		if (s)
		{
#ifdef WIN32
			OutputDebugStringA(s);
#else
			fputs(s, stdout);
#endif
		}
		else if (ws)
		{
            wchar_t out[512];
            wchar_t* outEnd = out + sizeof(out) - 2;
            while (*ws) {
                wchar_t* outPos = out;
                while (*ws && outPos != outEnd) {
                *outPos++ = *ws++;
                }
                *outPos++ = 0;
#ifdef WIN32
			    OutputDebugStringW(out);
#else
    			fputws(out, stdout);
#endif
            }
		}
		lua_pop(L, 1);  /* pop result */
	}

#ifdef WIN32
	OutputDebugStringA("\n");
#else
	fputs("\n", stdout);
#endif

	return 0;
}