Ejemplo n.º 1
0
static int engine_CreateFakeClient (lua_State *L) {
  engine->CreateFakeClient(luaL_checkstring(L, 1));
  return 0;
}
Ejemplo n.º 2
0
static int Paug_matches(lua_State *L)
{
    augeas *a = Paug_checkarg(L, 1);
    const char *path = luaL_checkstring(L, 2);
    return pushresult(L, aug_match(a, path, NULL), a, path);
}
Ejemplo n.º 3
0
static int os_rename(lua_State* L)
{
	const char* fromname = luaL_checkstring(L, 1);
	const char* toname = luaL_checkstring(L, 2);
	return os_pushresult(L, rename(fromname, toname) == 0, fromname);
}
Ejemplo n.º 4
0
Archivo: lpty.c Proyecto: jgaskins/brat
/* lpty_startproc
 *
 * start a process with our pty as its controlling terminal.
 *
 * Arguments:
 *	L	Lua State
 *
 * Lua Stack:
 *	1	lpty userdata
 *
 * Lua Returns:
 *	+1	false if the re was already an active subprocess of this pty, true if
 * 		not and we started one.
 * 
 * Note:
 * 	We can not determine wether the command to be spawned in the child process
 * 	was successful.
 */
static int lpty_startproc(lua_State *L)
{
	lPty *pty = lpty_checkLPty(L, 1);
	const char *cmd = luaL_checkstring(L, 2);
	
	/* if pty already has an active child process we just return false and do
	 * nothing */
	if (_lpty_hasrunningchild(pty))
		lua_pushboolean(L, 0);
	else {
		pid_t child;
		int ttyfd = pty->s_fd;

		signal(SIGCHLD, _lpty_sigchld_handler);

		/* now start child process */
		child = fork();
		if (child == 0) {
			/* child process */
			/* fill execvp args array from function arguments */
			int nargs = lua_gettop(L) - 1;
			const char **args = calloc(nargs + 1, sizeof(char*));
			int i;
			args[0] = cmd;
			for (i = 1; i < nargs; ++i)
				args[i] = lua_tostring(L, 2 + i);
			args[nargs] = NULL;
			
			/* prepare child processes standard file handles */
			dup2(ttyfd, 0);
			dup2(ttyfd, 1);
			dup2(ttyfd, 2);

			/* need to create new session id for slave in order for the tty to
			 * become a controlling tty */
			if (setsid() < (pid_t)0) {
				fprintf(stderr, "lpty failed to create new session id.");
				exit(EXIT_FAILURE);
				/* we need to terminate here! */
			}

			/* reset SIGCHLD handler then start our process */
			signal(SIGCHLD, SIG_DFL);
			execvp(cmd, (char* const*)args);

			/* if we ever get here, an error has occurred.
			 * Note: this error will only be visible as output to the pty from the parent side!
			 */
			free(args);
			fprintf(stderr, "error: lpty failed to start child process: %s", strerror(errno));
			exit(EXIT_FAILURE);
			/* we need to terminate here! */
		} else if (child > 0) {
			/* parent process: clean up, store child pid, return success */
			pty->child = child;
			lua_pushboolean(L, 1);
		} else {
			return lpty_error(L, pty->flags.throwerrors, "lpty failed to create child process: %s", strerror(errno));
		}
	} 
	return 1;
}
Ejemplo n.º 5
0
static int l_siplua_moduleFunc(lua_State *L)
{
  struct sipapi_object *o;
  const char *func;
  int n, nargs;
  cmd_export_t *exp_func_struct;
  action_elem_t elems[MAX_ACTION_ELEMS];
  const char *s, *msg;
  char *str;
  int i;
  struct action *act;
  int retval;

  o = luaL_checkudata(L, 1, "siplua.api");
  func = luaL_checkstring(L, 2);
  n = lua_gettop(L);
  nargs = n - 2;
  if (n - 1 > MAX_ACTION_ELEMS)
    return luaL_error(L, "function '%s' called with too many arguments [%d > %d]",
	       func, nargs, MAX_ACTION_ELEMS - 1);
  exp_func_struct = find_cmd_export_t((char *)func, nargs, 0);
  if (!exp_func_struct)
    {
      return luaL_error(L, "function '%s' called, but not available.");
    }
  elems[0].type = CMD_ST;
  elems[0].u.data = exp_func_struct;
  memset(&elems[1], '\0', nargs * sizeof(action_elem_t));
  for (i = 0; i < nargs; ++i)
    {
      s = lua_tostring(L, 3 + i);
      if (!s)
	{
	  siplua_moduleFunc_free(func, exp_func_struct, elems, nargs);
	  msg = lua_pushfstring(L, "%s expected, got %s",
				lua_typename(L, LUA_TSTRING), luaL_typename(L, 3 + i));
	  return luaL_argerror(L, 3 + i, msg);
	}
      str = pkg_malloc(strlen(s) + 1);
      if (!str)
	{
	  siplua_moduleFunc_free(func, exp_func_struct, elems, nargs);
	  return luaL_error(L, "Not enough memory");
	}
      strcpy(str, s);
      /* We should maybe try STR_ST and elems[].u.str.{s,len} */
      elems[i + 1].type = STRING_ST;
      elems[i + 1].u.data = str; /* elems[].u.string */
    }
  act = mk_action(MODULE_T, n - 2 + 1, elems, 0);
  if (!act)
    {
      siplua_moduleFunc_free(func, exp_func_struct, elems, nargs);
      return luaL_error(L, "action structure could not be created. Error.");
    }
/*   siplua_log(L_DBG, "fixup/%p free_fixup/%p", */
/* 	     exp_func_struct->fixup, */
/* 	     exp_func_struct->free_fixup); */
  if (exp_func_struct->fixup)
    {
      if (!siplua_unsafemodfnc && !exp_func_struct->free_fixup)
	{
	  siplua_moduleFunc_free(func, exp_func_struct, act->elem, nargs);
	  return luaL_error(L, "Module function '%s' is unsafe. Call is refused.\n", func);
	}
      if (nargs == 0)
	{
	  retval = exp_func_struct->fixup(0, 0);
	  if (retval < 0)
	    {
	      siplua_moduleFunc_free(func, exp_func_struct, act->elem, nargs);
	      return luaL_error(L, "Error in fixup (0)\n");
	    }
	}
      for (i = 0; i < nargs; ++i)
	{
	  retval = exp_func_struct->fixup(&act->elem[i + 1].u.data, i + 1);
	  if (retval < 0)
	    {
	      siplua_moduleFunc_free(func, exp_func_struct, act->elem, nargs);
	      return luaL_error(L, "Error in fixup (%d)\n", i + 1);
	    }
	  act->elem[i + 1].type = MODFIXUP_ST;
	}
    }
  retval = do_action(act, o->msg);
  siplua_moduleFunc_free(func, exp_func_struct, act->elem, nargs);
  pkg_free(act);
  lua_pushinteger(L, retval);
  return 1;
}
Ejemplo n.º 6
0
static int
lsettext(lua_State *L) {
	struct sprite *s = self(L);
	if (s->type != TYPE_LABEL) {
		return luaL_error(L, "Only label can set rich text");
	}
	if (lua_isnoneornil(L, 2)) {
		s->data.rich_text = NULL;
		lua_pushnil(L);
		lua_setuservalue(L, 1);
		return 0;
	}
  if (lua_isstring(L, 2)) {
    s->data.rich_text = (struct rich_text*)lua_newuserdata(L, sizeof(struct rich_text));
    s->data.rich_text->text = lua_tostring(L, 2);
    s->data.rich_text->count = 0;
		s->data.rich_text->fields = NULL;

		lua_createtable(L, 2, 0);
		lua_pushvalue(L, 2);
		lua_rawseti(L, -2, 1);
		lua_pushvalue(L, 3);
		lua_rawseti(L, -2, 2);
		lua_setuservalue(L, 1);
    return 0;
  }

  s->data.rich_text = NULL;
  if (!lua_istable(L, 2) || lua_rawlen(L, 2) != 2) {
    return luaL_error(L, "rich text must has a table with two items");
  }

  lua_rawgeti(L, 2, 1);
  const char *txt = luaL_checkstring(L, -1);
  lua_pop(L, 1);

  lua_rawgeti(L, 2, 2);
	int cnt = lua_rawlen(L, -1);
  lua_pop(L, 1);

	struct rich_text *rich = (struct rich_text*)lua_newuserdata(L, sizeof(struct rich_text));

	rich->text = txt;
  rich->count = cnt;
	int size = cnt * sizeof(struct label_field);
	rich->fields = (struct label_field*)lua_newuserdata(L, size);

	struct label_field *fields = rich->fields;
	int i;
  lua_rawgeti(L, 2, 2);
	for (i=0; i<cnt; i++) {
		lua_rawgeti(L, -1, i+1);
		if (!lua_istable(L,-1)) {
			return luaL_error(L, "rich text unit must be table");
		}

		lua_rawgeti(L, -1, 1);  //start
		((struct label_field*)(fields+i))->start = luaL_checkinteger(L, -1);
		lua_pop(L, 1);

    lua_rawgeti(L, -1, 2);  //end
		((struct label_field*)(fields+i))->end = luaL_checkinteger(L, -1);
    lua_pop(L, 1);

		lua_rawgeti(L, -1, 3);  //color
		((struct label_field*)(fields+i))->color = luaL_checkunsigned(L, -1);
		lua_pop(L, 1);

		//extend here

		lua_pop(L, 1);
	}
  lua_pop(L, 1);

	lua_createtable(L,3,0);
	lua_pushvalue(L, 3);
	lua_rawseti(L, -2, 1);
	lua_pushvalue(L, 4);
	lua_rawseti(L, -2, 2);
	lua_rawgeti(L, 2, 1);
	lua_rawseti(L, -2, 3);
	lua_setuservalue(L, 1);

	s->data.rich_text = rich;
	return 0;
}
Ejemplo n.º 7
0
std::string l_data_type<std::string>::get(lua_State *l, int n) { return luaL_checkstring(l, n); }
Ejemplo n.º 8
0
int icu_case(lua_State *L) {
  size_t input_l;
  const char* input = luaL_checklstring(L, 1, &input_l);

  const char* locale = luaL_checkstring(L, 2);
  const char* recase = luaL_checkstring(L, 3);

  /* Convert input to ICU-friendly UChars */
  UChar *input_as_uchar;
  int32_t l;
  utf8_to_uchar(input, input_l, input_as_uchar, l);

  /* Now do the conversion */
  UChar *output;
  int32_t l2 = 0;

  UErrorCode err = U_ZERO_ERROR;

  if (strcmp(recase, "title") == 0) {
    l2 = u_strToTitle(NULL, 0, input_as_uchar, l, NULL, locale, &err);
    err = U_ZERO_ERROR;
    output = malloc(l2 * sizeof(UChar));
    u_strToTitle(output, l2, input_as_uchar, l, NULL, locale, &err);
  } else {
    conversion_function_t conversion;
    if (strcmp(recase, "upper") == 0) {
      conversion = u_strToUpper;
    } else if (strcmp(recase, "lower") == 0) {
      conversion = u_strToLower;
    } else {
      free(input_as_uchar);
      return luaL_error(L, "Unknown case conversion type %s", recase);
    }
    l2 = conversion(NULL, 0, input_as_uchar, l, locale, &err);
    err = U_ZERO_ERROR;
    output = malloc(l2 * sizeof(UChar));
    conversion(output, l2, input_as_uchar, l, locale, &err);
  }
  if (!U_SUCCESS(err)) {
    free(input_as_uchar);
    free(output);
    return luaL_error(L, "Error in case conversion %s", u_errorName(err));
  }

  int32_t l3 = 0;
  char possibleOutbuf[4096];
  u_strToUTF8(possibleOutbuf, 4096, &l3, output, l2, &err);
  if (U_SUCCESS(err)) {
    lua_pushstring(L, possibleOutbuf);
    free(input_as_uchar);
    free(output);
    return 1;
  }
  char *utf8output;
  if (err == U_BUFFER_OVERFLOW_ERROR) {
    utf8output = malloc(l3);
    u_strToUTF8(utf8output, l3, NULL, output, l2, &err);
    if (!U_SUCCESS(err)) goto fail;
    utf8output[l3] = '\0';
    lua_pushstring(L, utf8output);
    free(input_as_uchar);
    free(output);
    free(utf8output);
    return 1;
  }
  fail:
    return luaL_error(L, "Error in UTF8 conversion %s", u_errorName(err));
}
Ejemplo n.º 9
0
int icu_breakpoints(lua_State *L) {
  const char* input = luaL_checkstring(L, 1);
  int input_l = strlen(input);
  const char* locale = luaL_checkstring(L, 2);
  UChar *buffer;
  int32_t l, breakcount = 0;
  UErrorCode err = U_ZERO_ERROR;
  u_strFromUTF8(NULL, 0, &l, input, input_l, &err);
  /* Above call returns an error every time. */
  err = U_ZERO_ERROR;
  buffer = malloc(l * sizeof(UChar));
  u_strFromUTF8(buffer, l, &l, input, input_l, &err);

  UBreakIterator* wordbreaks, *linebreaks;
  int32_t i, previous;
  wordbreaks = ubrk_open(UBRK_WORD, locale, buffer, l, &err);
  if(U_FAILURE(err)) {
    luaL_error(L, "Word break parser failure: %s", u_errorName(err));
  }

  linebreaks = ubrk_open(UBRK_LINE, locale, buffer, l, &err);
  if(U_FAILURE(err)) {
    luaL_error(L, "Line break parser failure: %s", u_errorName(err));
  }

  previous = 0;
  i = 0;
  while (i <= l) {
    int32_t out_l;
    int32_t type;
    if (!ubrk_isBoundary(linebreaks, i) && !ubrk_isBoundary(wordbreaks,i)) {
      i++; continue;
    }
    lua_checkstack(L, 3);
    /* At some kind of boundary */
    lua_newtable(L);
    lua_pushstring(L, "type");
    lua_pushstring(L, ubrk_isBoundary(linebreaks,i) ? "line" : "word");
    lua_settable(L, -3);

    int32_t utf8_index = 0;
    err = U_ZERO_ERROR;
    u_strToUTF8(NULL, 0, &utf8_index, buffer, i, &err);
    assert(U_SUCCESS(err) || err == U_BUFFER_OVERFLOW_ERROR);

    lua_pushstring(L, "index");
    lua_pushinteger(L, utf8_index);
    lua_settable(L, -3);

    if (ubrk_isBoundary(linebreaks, i)) {
      lua_pushstring(L, "subtype");
      type = ubrk_getRuleStatus(linebreaks);
      if (type >= UBRK_LINE_SOFT && type < UBRK_LINE_SOFT_LIMIT) {
        lua_pushstring(L, "soft");
      } else {
        lua_pushstring(L, "hard");
      }
      lua_settable(L, -3);
    }
    lua_pushstring(L, "token");
    lua_pushlstring(L, input+previous, utf8_index-previous);

    lua_settable(L, -3);

    previous = utf8_index;
    breakcount++;
    i++;
  }
  ubrk_close(wordbreaks);
  ubrk_close(linebreaks);
  return breakcount;
}
Ejemplo n.º 10
0
static int engine_InsertServerCommand (lua_State *L) {
  engine->InsertServerCommand(luaL_checkstring(L, 1));
  return 0;
}
Ejemplo n.º 11
0
int icu_bidi_runs(lua_State *L) {
  size_t input_l;
  const char* input = luaL_checklstring(L, 1, &input_l);
  const char* direction = luaL_checkstring(L, 2);

  UChar *input_as_uchar;
  int32_t l;
  utf8_to_uchar(input, input_l, input_as_uchar, l);

  UBiDiLevel paraLevel = 0;
  if (strncasecmp(direction, "RTL", 3) == 0) {
    paraLevel = 1;
  }
  /* Now let's bidi! */
  UBiDi* bidi = ubidi_open();
  UErrorCode err = U_ZERO_ERROR;
  ubidi_setPara(bidi, input_as_uchar, l, paraLevel, NULL, &err);
  if (!U_SUCCESS(err)) {
    free(input_as_uchar);
    ubidi_close(bidi);
    return luaL_error(L, "Error in bidi %s", u_errorName(err));
  }

  int count = ubidi_countRuns(bidi,&err);
  int start, length;

  lua_checkstack(L,count);
  for (int i=0; i < count; i++) {
    UBiDiDirection dir = ubidi_getVisualRun(bidi, i, &start, &length);
    lua_newtable(L);
    // Convert back to UTF8...
    int32_t l3 = 0;
    char* possibleOutbuf = malloc(4*length);
    if(!possibleOutbuf) {
      return luaL_error(L, "Couldn't malloc");
    }
    u_strToUTF8(possibleOutbuf, 4 * length, &l3, input_as_uchar+start, length, &err);
    if (!U_SUCCESS(err)) {
      free(possibleOutbuf);
      return luaL_error(L, "Bidi run too big? %s", u_errorName(err));
    }
    lua_pushstring(L, "run");
    lua_pushstring(L, possibleOutbuf);
    free(possibleOutbuf);
    lua_settable(L, -3);

    lua_pushstring(L, "start");
    int32_t new_start = start;
    // Length/start is given in terms of UTF16 codepoints.
    // But we want a count of Unicode characters. This means
    // surrogate pairs need to be counted as 1.
    for (int j=0; j< start; j++) {
      if (U_IS_TRAIL(*(input_as_uchar+j))) new_start--;
    }
    lua_pushinteger(L, new_start);
    lua_settable(L, -3);

    lua_pushstring(L, "length");
    for (int j=start; j< start+length; j++) {
      if (U_IS_TRAIL(*(input_as_uchar+j))) length--;
    }
    lua_pushinteger(L, length);
    lua_settable(L, -3);

    lua_pushstring(L, "dir");
    lua_pushstring(L, dir == UBIDI_RTL ? "RTL" : "LTR");
    lua_settable(L, -3);

    lua_pushstring(L, "level");
    lua_pushinteger(L, ubidi_getLevelAt(bidi, start));
    lua_settable(L, -3);
  }

  free(input_as_uchar);
  ubidi_close(bidi);
  return count;
}
Ejemplo n.º 12
0
static int engine_GetClientConVarValue (lua_State *L) {
  lua_pushstring(L, engine->GetClientConVarValue(luaL_checkinteger(L, 1), luaL_checkstring(L, 2)));
  return 1;
}
Ejemplo n.º 13
0
static int engine_ForceSimpleMaterial (lua_State *L) {
  engine->ForceSimpleMaterial(luaL_checkstring(L, 1));
  return 0;
}
Ejemplo n.º 14
0
static int engine_ForceExactFile (lua_State *L) {
  engine->ForceExactFile(luaL_checkstring(L, 1));
  return 0;
}
Ejemplo n.º 15
0
/**
 * Gets the attacks of a unit or unit type (__index metamethod).
 * - Arg 1: table containing the userdata containing the unit or unit type.
 * - Arg 2: index (int) or id (string) identifying a particular attack.
 * - Ret 1: the unit's attacks.
 */
static int impl_unit_attacks_get(lua_State *L)
{
	if(!lua_istable(L, 1)) {
		return luaW_type_error(L, 1, "unit attacks");
	}
	lua_rawgeti(L, 1, 0);
	lua_unit* lu = luaW_tounit_ref(L, -1);
	const unit_type* ut = luaW_tounittype(L, -1);
	if(lu && lu->get()) {
		unit* u = lu->get();
		attack_ptr atk = lua_isnumber(L, 2) ? find_attack(u, luaL_checkinteger(L, 2) - 1) : find_attack(u, luaL_checkstring(L, 2));
		luaW_pushweapon(L, atk);
	} else if(ut) {
		const_attack_ptr atk = lua_isnumber(L, 2) ? find_attack(ut, luaL_checkinteger(L, 2) - 1) : find_attack(ut, luaL_checkstring(L, 2));
		luaW_pushweapon(L, atk);
	} else {
		return luaL_argerror(L, 1, "unit not found");
	}
	return 1;
}
Ejemplo n.º 16
0
int libmcache::lua_mcache_get(lua_State* L)
{
    int top=lua_gettop(L);

    LMCACHE* mcache=(LMCACHE*)luaL_checkudata(L,1,LUA_MCACHE);
    
    if(!mcache->fp)
        return luaL_error(L,err_closed);
    
    const char* key=luaL_checkstring(L,2);

    fprintf(mcache->fp,"get %s\r\n",key);

    fflush(mcache->fp);

    char result[512];

    static const char end_tag[]="END";
    static const char value_tag[]="VALUE";
    
    for(;;)
    {    
	if(!fgets(result,sizeof(result),mcache->fp))
	{
	    lua_mcache_close(mcache);
	    return luaL_error(L,err_io);
	}
    
	char* p=strchr(result,'\r');
	if(p)
	    *p=0;

	if(!strncmp(result,end_tag,sizeof(end_tag)-1))
	    break;
	
	if(strncmp(result,value_tag,sizeof(value_tag)-1))
	{
	    lua_mcache_close(mcache);
	    return luaL_error(L,err_invalid_server_response,result);
	}
	

	char* len=lua_mcache_find_substring_by_id(result+(sizeof(value_tag)-1),2);
	
	if(!len)
	{
	    lua_mcache_close(mcache);
	    return luaL_error(L,err_invalid_server_response,result);
	}
	
	p=strchr(len,' ');
	if(p)
	    *p=0;
	
	size_t length=atol(len);

	luaL_Buffer buf;
	luaL_buffinit(L,&buf);
	
	size_t l=0;
	
	while(l<length)
	{
	    size_t m=length-l;

	    size_t n=fread(result,1,m<sizeof(result)?m:sizeof(result),mcache->fp);
	    
	    if(!n)
		break;

	    luaL_addlstring(&buf,result,n);

	    l+=n;
	}
	
	luaL_pushresult(&buf);
	
	if(l!=length || !fgets(result,sizeof(result),mcache->fp))
	{
	    lua_mcache_close(mcache);
	    return luaL_error(L,err_io);
	}
    }

    return lua_gettop(L)-top;
}
Ejemplo n.º 17
0
static int impl_unit_attacks_set(lua_State* L)
{
	if(!lua_istable(L, 1)) {
		return luaW_type_error(L, 1, "unit attacks");
	}
	lua_rawgeti(L, 1, 0);
	const unit_type* ut = luaW_tounittype(L, -1);
	if(ut) {
		return luaL_argerror(L, 1, "unit type attack table is immutable");
	}

	unit& u = luaW_checkunit(L, -1);
	attack_ptr atk = lua_isnumber(L, 2) ? find_attack(&u, luaL_checkinteger(L, 2) - 1) : find_attack(&u, luaL_checkstring(L, 2));
	if(lua_isnumber(L, 2) && lua_tonumber(L, 2) - 1 > u.attacks().size()) {
		return luaL_argerror(L, 2, "attack can only be added at the end of the list");
	}

	if(lua_isnil(L, 3)) {
		// Delete the attack
		u.remove_attack(atk);
		return 0;
	}

	auto iter = get_attack_iter(u, atk), end = u.attacks().end();
	if(const_attack_ptr atk2 = luaW_toweapon(L, 3)) {
		if(iter == end) {
			atk = u.add_attack(end, *atk2);
		} else {
			iter.base()->reset(new attack_type(*atk2));
			atk = *iter.base();
		}
	} else {
		config cfg = luaW_checkconfig(L, 3);
		if(iter == end) {
			atk = u.add_attack(end, cfg);
		} else {
			iter.base()->reset(new attack_type(cfg));
			atk = *iter.base();
		}
	}
	if(!lua_isnumber(L, 2)) {
		atk->set_id(lua_tostring(L, 2));
	}
	return 0;
}
Ejemplo n.º 18
0
int CLuaInstFileHelpers::FileHelpersTouch(lua_State *L)
{
	CLuaFileHelpers *D = FileHelpersCheckData(L, 1);
	if (!D) return 0;

	int numargs = lua_gettop(L) - 1;
	int min_numargs = 1;
	if (numargs < min_numargs) {
		printf("luascript touch: not enough arguments (%d, expected %d)\n", numargs, min_numargs);
		lua_pushboolean(L, false);
		return 1;
	}
	if (!lua_isstring(L, 2)) {
		printf("%s: argument 1 is not a string.\n",__func__);
		lua_pushboolean(L, false);
		return 1;
	}
	const char *file = luaL_checkstring(L, 2);

	bool ret = true;
	lua_Debug ar;

	if (!file_exists(file)) {
		FILE *f = fopen(file, "w");
		if (f == NULL) {
			ret = false;
			lua_getstack(L, 1, &ar);
			lua_getinfo(L, "Sl", &ar);
			const char* s = strerror(errno);
			printf(">>> Lua script error [%s:%d] %s\n    (error from neutrino: [%s:%d])\n",
			       ar.short_src, ar.currentline, s, __path_file__, __LINE__);
			lua_pushboolean(L, ret);
			return 1;
		}
		fclose(f);
		if (numargs == min_numargs) {
			lua_pushboolean(L, ret);
			return 1;
		}
	}

	time_t modTime;
	if (numargs == min_numargs)
		/* current time */
		modTime = time(NULL);
	else
		/* new time */
		modTime = (time_t)luaL_checkint(L, 3);

	utimbuf utb;
	utb.actime  = modTime;
	utb.modtime = modTime;
	if (utime(file, &utb) != 0) {
		ret = false;
		lua_getstack(L, 1, &ar);
		lua_getinfo(L, "Sl", &ar);
		const char* s = strerror(errno);
		printf(">>> Lua script error [%s:%d] %s\n    (error from neutrino: [%s:%d])\n",
		       ar.short_src, ar.currentline, s, __path_file__, __LINE__);
	}

	lua_pushboolean(L, ret);
	return 1;
}
Ejemplo n.º 19
0
const char* l_data_type<const char*>::get(lua_State *l, int n) { return luaL_checkstring(l, n); }
Ejemplo n.º 20
0
int CLuaInstFileHelpers::FileHelpersLn(lua_State *L)
{
	CLuaFileHelpers *D = FileHelpersCheckData(L, 1);
	if (!D) return 0;

	int numargs = lua_gettop(L) - 1;
	int min_numargs = 2;
	if (numargs < min_numargs) {
		printf("luascript ln: not enough arguments (%d, expected %d)\n", numargs, min_numargs);
		lua_pushboolean(L, false);
		return 1;
	}
	if (!lua_isstring(L, 2) || !lua_isstring(L, 3)) {
		printf("%s: argument 1 or 2 is not a string.\n",__func__);
		lua_pushboolean(L, false);
		return 1;
	}
	const char *src = luaL_checkstring(L, 2);
	const char *link = luaL_checkstring(L, 3);

	const char *flags = "";
	if (numargs > min_numargs){
		if (!lua_isstring(L, 4)) {
		printf("%s: argument 3 is not a string.\n",__func__);
			lua_pushboolean(L, false);
			return 1;
		}
		flags = luaL_checkstring(L, 4);
	}
	bool symlnk = (strchr(flags, 's') != NULL);
	bool force  = (strchr(flags, 'f') != NULL);
	lua_Debug ar;

	if (!symlnk) {
		lua_getstack(L, 1, &ar);
		lua_getinfo(L, "Sl", &ar);
		const char* s = "Currently only supports symlinks.";
		printf(">>> Lua script error [%s:%d] %s\n    (error from neutrino: [%s:%d])\n",
		       ar.short_src, ar.currentline, s, __path_file__, __LINE__);
		lua_pushboolean(L, false);
		return 1;
	}

	bool ret = true;
	if (symlink(src, link) != 0) {
		if (force && (errno == EEXIST)) {
			if (unlink(link) == 0) {
				if (symlink(src, link) == 0) {
					lua_pushboolean(L, ret);
					return 1;
				}
			}
		}
		ret = false;
		lua_getstack(L, 1, &ar);
		lua_getinfo(L, "Sl", &ar);
		const char* s = strerror(errno);
		printf(">>> Lua script error [%s:%d] %s\n    (error from neutrino: [%s:%d])\n",
		       ar.short_src, ar.currentline, s, __path_file__, __LINE__);

	}

	lua_pushboolean(L, ret);
	return 1;
}
Ejemplo n.º 21
0
static int luasrc_ConVar (lua_State *L) {
  ConVar *pConVar = new ConVar(luaL_checkstring(L, 1), luaL_checkstring(L, 2), luaL_optint(L, 3, 0), luaL_optstring(L, 4, 0), luaL_optboolean(L, 5, 0), luaL_optnumber(L, 6, 0.0), luaL_optboolean(L, 7, 0), luaL_optnumber(L, 8, 0));
  cvar->RegisterConCommand(pConVar);
  lua_pushconvar(L, pConVar);
  return 1;
}
Ejemplo n.º 22
0
int CLuaInstFileHelpers::FileHelpersExist(lua_State *L)
{
	CLuaFileHelpers *D = FileHelpersCheckData(L, 1);
	if (!D) return 0;

	int numargs = lua_gettop(L) - 1;
	int min_numargs = 2;
	if (numargs < min_numargs) {
		printf("luascript exist: not enough arguments (%d, expected %d)\n", numargs, min_numargs);
		lua_pushnil(L);
		return 1;
	}

	if (!lua_isstring(L, 2) || !lua_isstring(L, 3)) {
		printf("%s: argument 1 or 2 is not a string.\n",__func__);
		lua_pushboolean(L, false);
		return 1;
	}
	bool ret = false;
	bool err = false;
	int errLine = 0;
	std::string errMsg = "";

	const char *file = luaL_checkstring(L, 2);
	const char *flag = luaL_checkstring(L, 3);

	if (file_exists(file)) {
		struct stat FileInfo;
		if (lstat(file, &FileInfo) == -1) {
			err = true;
			errLine = __LINE__;
			errMsg = (std::string)strerror(errno);
		}
		else if (strchr(flag, 'f') != NULL) {
			if (S_ISREG(FileInfo.st_mode))
				ret = true;
		}
		else if (strchr(flag, 'l') != NULL) {
			if (S_ISLNK(FileInfo.st_mode))
				ret = true;
		}
		else if (strchr(flag, 'd') != NULL) {
			if (S_ISDIR(FileInfo.st_mode))
				ret = true;
		}
		else {
			err = true;
			errLine = __LINE__;
			errMsg = (strlen(flag) == 0) ? "no" : "unknown";
			errMsg += " flag given.";
		}
	}

	if (err) {
		lua_Debug ar;
		lua_getstack(L, 1, &ar);
		lua_getinfo(L, "Sl", &ar);
		printf(">>> Lua script error [%s:%d] %s\n    (error from neutrino: [%s:%d])\n",
		       ar.short_src, ar.currentline, errMsg.c_str(), __path_file__, errLine);
		lua_pushnil(L);
		return 1;
	}

	lua_pushboolean(L, ret);
	return 1;
}
Ejemplo n.º 23
0
static int openFrameBuffer(lua_State *L) {
	const char *fb_device = luaL_checkstring(L, 1);
	FBInfo *fb = (FBInfo*) lua_newuserdata(L, sizeof(FBInfo));
	uint8_t *fb_map_address = NULL;
	const char *config_value = NULL;

	luaL_getmetatable(L, "einkfb");

	fb->buf = (BlitBuffer*) lua_newuserdata(L, sizeof(BlitBuffer));

	luaL_getmetatable(L, "blitbuffer");
	lua_setmetatable(L, -2);

	lua_setfield(L, -2, "bb");
	lua_setmetatable(L, -2);

#ifndef EMULATE_READER
	/* open framebuffer */
	fb->fd = open(fb_device, O_RDWR);
	if (fb->fd == -1) {
		return luaL_error(L, "cannot open framebuffer %s",
				fb_device);
	}

	/* initialize data structures */
	memset(&fb->finfo, 0, sizeof(fb->finfo));
	memset(&fb->vinfo, 0, sizeof(fb->vinfo));

	/* Get fixed screen information */
	if (ioctl(fb->fd, FBIOGET_FSCREENINFO, &fb->finfo)) {
		return luaL_error(L, "cannot get screen info");
	}

	if (fb->finfo.type != FB_TYPE_PACKED_PIXELS) {
		return luaL_error(L, "video type %x not supported",
				fb->finfo.type);
	}

	if (strncmp(fb->finfo.id, "mxc_epdc_fb", 11) == 0) {
		/* Kindle PaperWhite and KT with 5.1 or later firmware */
		einkUpdateFunc = &kindle51einkUpdate;
	} else if (strncmp(fb->finfo.id, "eink_fb", 7) == 0) {
		if (fb->vinfo.bits_per_pixel == 8) {
			/* kindle4 */
			einkUpdateFunc = &kindle4einkUpdate;
		} else {
			/* kindle2, 3, DXG */
			einkUpdateFunc = &kindle3einkUpdate;
		}
	} else {
		return luaL_error(L, "eink model %s not supported",
				fb->finfo.id);
	}

	if (ioctl(fb->fd, FBIOGET_VSCREENINFO, &fb->vinfo)) {
		return luaL_error(L, "cannot get variable screen info");
	}

	if (!fb->vinfo.grayscale) {
		return luaL_error(L, "only grayscale is supported but framebuffer says it isn't");
	}

	if (fb->vinfo.xres <= 0 || fb->vinfo.yres <= 0) {
		return luaL_error(L, "invalid resolution %dx%d.\n",
				fb->vinfo.xres, fb->vinfo.yres);
	}

	/* mmap the framebuffer */
	fb_map_address = mmap(0, fb->finfo.smem_len,
			PROT_READ | PROT_WRITE, MAP_SHARED, fb->fd, 0);
	if(fb_map_address == MAP_FAILED) {
		return luaL_error(L, "cannot mmap framebuffer");
	}
	
	if (fb->vinfo.bits_per_pixel == 8) {
		/* for 8bpp K4, PaperWhite, we create a shadow 4bpp blitbuffer.
		 * These models use 16 scale 8bpp framebuffer, so they are
		 * actually fake 8bpp FB. Therefore, we still treat them as 4bpp
		 *
		 * For PaperWhite, the screen width is 758, but FB's line_length
		 * is 768. So when doing the screen update, you still need to
		 * fill 768 pixels per line, but the trailing 10 px for each
		 * line is actually ignored by driver.
		 * */
		fb->buf->pitch = fb->vinfo.xres / 2;

		fb->buf->data = (uint8_t *)calloc(fb->buf->pitch * fb->vinfo.yres, sizeof(uint8_t));
		if (!fb->buf->data) {
			return luaL_error(L, "failed to allocate memory for framebuffer's shadow blitbuffer!");
		}
		fb->buf->allocated = 1;

		/* now setup framebuffer map */
		fb->real_buf = (BlitBuffer *)malloc(sizeof(BlitBuffer));
		if (!fb->buf->data) {
			return luaL_error(L, "failed to allocate memory for framebuffer's blitbuffer!");
		}
		fb->real_buf->pitch = fb->finfo.line_length;
		fb->real_buf->w = fb->vinfo.xres;
		fb->real_buf->h = fb->vinfo.yres;
		fb->real_buf->allocated = 0;
		fb->real_buf->data = fb_map_address;
	} else {
		fb->buf->pitch = fb->finfo.line_length;
		/* for K2, K3 and DXG, we map framebuffer to fb->buf->data directly */
		fb->real_buf = NULL;
		fb->buf->data = fb_map_address;
		fb->buf->allocated = 0;
	}
#else
	/* read display size configuration from environment variables */
	if(NULL != (config_value = getenv("EMULATE_READER_W")))
		emu_disp_w = atoi(config_value);
	if(NULL != (config_value = getenv("EMULATE_READER_H")))
		emu_disp_h = atoi(config_value);
	emu_w = emu_disp_w;
	emu_h = emu_disp_h;

	if(SDL_Init(SDL_INIT_VIDEO) < 0) {
		return luaL_error(L, "cannot initialize SDL.");
	}
	if(!(fb->screen = SDL_SetVideoMode(emu_w, emu_h, 32, SDL_HWSURFACE))) {
		return luaL_error(L, "can't get video surface %dx%d for 32bpp.",
				emu_w, emu_h);
	}
	fb->vinfo.xres = emu_w;
	fb->vinfo.yres = emu_h;
	fb->buf->pitch = (emu_w + 1) / 2;
	fb->buf->data = calloc(fb->buf->pitch * emu_h, sizeof(char));
	if(fb->buf->data == NULL) {
		return luaL_error(L, "cannot get framebuffer emu memory");
	}
#endif
	fb->buf->w = fb->vinfo.xres;
	fb->buf->h = fb->vinfo.yres;
	memset(fb->buf->data, 0, fb->buf->pitch * fb->buf->h);
	return 1;
}
int lua_RenderStateStateBlock_setBlendSrc(lua_State* state)
{
    // Get the number of parameters.
    int paramCount = lua_gettop(state);

    // Attempt to match the parameters to a valid binding.
    switch (paramCount)
    {
        case 2:
        {
            if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
            {
                // Get parameter 1 off the stack.
                RenderState::Blend param1 = (RenderState::Blend)lua_enumFromString_RenderStateBlend(luaL_checkstring(state, 2));

                RenderState::StateBlock* instance = getInstance(state);
                instance->setBlendSrc(param1);
                
                return 0;
            }
            else
            {
                lua_pushstring(state, "lua_RenderStateStateBlock_setBlendSrc - Failed to match the given parameters to a valid function signature.");
                lua_error(state);
            }
            break;
        }
        default:
        {
            lua_pushstring(state, "Invalid number of parameters (expected 2).");
            lua_error(state);
            break;
        }
    }
    return 0;
}
Ejemplo n.º 25
0
    bool FillVObjectFromLua(lua_State* L, VOBJECT& v, int idx)
    {
        switch(v.vt)
        {
            case V_UINT8:
            {
                v.vv.u8 = (uint8_t)luaL_checkint(L, idx);
                break;
            }
            case V_INT8:
            {
                v.vv.i8 = (int8_t)luaL_checkint(L, idx);
                break;
            }
            case V_UINT16:
            {
                v.vv.u16 = (uint16_t)luaL_checkint(L, idx);
                break;
            }
            case V_INT16:
            {
                v.vv.i16 = (int16_t)luaL_checkint(L, idx);
                break;
            }
            case V_UINT32:
            {
                v.vv.u32 = (uint32_t)luaL_checkint(L, idx);
                break;
            }
            case V_INT32:
            {
                v.vv.i32 = (int32_t)luaL_checkint(L, idx);
                break;
            }
            case V_UINT64:
            {
                v.vv.u64 = (uint64_t)luaL_checknumber(L, idx);
                break;
            }
            case V_INT64:
            {
                v.vv.i64 = (int64_t)luaL_checknumber(L, idx);
                break;
            }
            case V_FLOAT32:
            {
                v.vv.f32 = (float32_t)luaL_checknumber(L, idx);
                break;
            }
            case V_FLOAT64:
            {
                v.vv.f64 = (float64_t)luaL_checknumber(L, idx);
                break;
            }
            case V_STR:
            {
                v.vv.s = new string(luaL_checkstring(L, idx));
                break;
            }
            case V_BLOB:
            {
                size_t _l;
                const char* _s = luaL_checklstring(L, idx, &_l);
                charArrayDummy* d = new charArrayDummy;
                d->m_l = (uint16_t)_l;
                char* _s2 = new char[_l];
                memcpy(_s2, _s, _l);
                d->m_s = _s2;
                v.vv.p = d;
                break;
            }
            case V_LUATABLE:
            {
                if(!lua_istable(L, idx))
                {
                    v.vv.i32 = -1;
                    lua_pushfstring(L, "arg %d need a table", idx);
                    lua_error(L);
                }
                else
                {
                    CLuaCallback& cb = GetWorld()->GetLuaTables();
					//调用者处理
                    //if(v.vv.i32 > 0)
                    //{
                    //   cb.Unref(L, v.vv.i32);
                    //}

                    lua_pushvalue(L, idx);
                    v.vv.i32 = cb.Ref(L);
                }
                break;
            }
            case V_LUA_OBJECT:
            {
                CLuaCallback& cb = GetWorld()->GetLuaTables();
				//调用者处理
                //if(v.vv.i32 > 0)
                //{
                //    cb.Unref(L, v.vv.i32);
                //}

                lua_pushvalue(L, idx);
                v.vv.i32 = cb.Ref(L);
                break;
            }
            case V_REDIS_HASH:
            {
                //不支持newindex
                return false;
            }
            default:
            {
                return false;
            }
        }

        return true;
    }
Ejemplo n.º 26
0
int luagi_reference_is_valid_name( lua_State *L )
{
   const char *name = luaL_checkstring( L, 1 );
   lua_pushboolean( L, git_reference_is_valid_name( name ) );
   return 1;
}
Ejemplo n.º 27
0
static int os_remove(lua_State* L)
{
	const char* filename = luaL_checkstring(L, 1);
	return os_pushresult(L, remove(filename) == 0, filename);
}
Ejemplo n.º 28
0
static int Lua_Font_width(lua_State *L) {
    Lua_Font *font = checkfont(L);
    const char * str = luaL_checkstring(L, 2);
    lua_pushnumber(L, Lua_Font_Width(font, str));
    return 1;
}
Ejemplo n.º 29
0
static int os_getenv(lua_State* L)
{
	lua_pushstring(L, getenv(luaL_checkstring(L, 1)));  /* if NULL push nil */
	return 1;
}
Ejemplo n.º 30
0
static int engine_CopyFile (lua_State *L) {
  lua_pushboolean(L, engine->CopyFile(luaL_checkstring(L, 1), luaL_checkstring(L, 2)));
  return 1;
}