예제 #1
0
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// the table being written is assumed to be at the top of the stack
void fs_read_entries(lua_State* L, const char* origpath, int* tableindex) {
    char* scratch = NULL;
    size_t scratchsize = 4096/*LUAL_BUFFERSIZE*/;
    luaL_Buffer buffer;
    MinFSDirectoryEntry_t* entries;
    
    scratch = realloc(scratch, scratchsize);
    while(!(entries = minfs_read_directory_entries(origpath, scratch, scratchsize))) {
        scratch = realloc(scratch, scratchsize*2);
        scratchsize *= 2;
    }

    for (; entries && entries->entryNameLen != 0; entries = entries->next) {
        luaL_buffinit(L, &buffer);
        luaL_addstring(&buffer, origpath);
        luaL_addstring(&buffer, "/");
        luaL_addstring(&buffer, entries->entryName);
        luaL_pushresult(&buffer);
        if (minfs_is_directory(lua_tostring(L, -1))) {
            lua_pushvalue(L, -2); // the table is assumed to be at the top of the stack
            fs_read_entries(L, lua_tostring(L, -2), tableindex);
            lua_pop(L, 1);
        }
        lua_rawseti(L, -2, (*tableindex)++);
    }

    free(scratch);
}
예제 #2
0
파일: parser.c 프로젝트: GUI/lyaml
static void
parser_generate_error_message (lyaml_parser *parser)
{
   yaml_parser_t *P = &parser->parser;
   char buf[256];
   luaL_Buffer b;

   luaL_buffinit (parser->L, &b);
   luaL_addstring (&b, P->problem ? P->problem : "A problem");
   snprintf (buf, sizeof (buf), " at document: %d", parser->document_count);
   luaL_addstring (&b, buf);

   if (P->problem_mark.line || P->problem_mark.column)
   {
      snprintf (buf, sizeof (buf), ", line: %lu, column: %lu",
         (unsigned long) P->problem_mark.line + 1,
         (unsigned long) P->problem_mark.column + 1);
      luaL_addstring (&b, buf);
   }
   luaL_addstring (&b, "\n");

   if (P->context)
   {
      snprintf (buf, sizeof (buf), "%s at line: %lu, column: %lu\n",
         P->context,
         (unsigned long) P->context_mark.line + 1,
         (unsigned long) P->context_mark.column + 1);
      luaL_addstring (&b, buf);
   }

   luaL_pushresult (&b);
}
예제 #3
0
static void init_delete_hstore_text(lua_State *L, const char* hstore_schema){

    luaL_Buffer b;
    const char *funcname;


    if (OidIsValid(delete_hstore_text_oid))return;

    luaL_buffinit(L, &b);

    if (hstore_schema == NULL){
        luaL_addstring(&b, "delete(hstore,text)");
    }else{
        luaL_addstring(&b, hstore_schema);
        luaL_addstring(&b, ".delete(");
        luaL_addstring(&b, hstore_schema);
        luaL_addstring(&b, ".hstore,text)");
    }
    luaL_pushresult(&b);

    funcname = lua_tostring(L, -1);

    delete_hstore_text_oid = DatumGetObjectId(DirectFunctionCall1(regprocedurein, CStringGetDatum(funcname)));
    lua_pop(L,1);
    if (!OidIsValid(delete_hstore_text_oid)){
        luaL_error(L,"failed to register delete(hstore,text)");
    }
}
예제 #4
0
void push_sysversion(lua_State *L)
{
    int err;
    struct utsname name;

    err = uname(&name);
    if (err<0)
    {
        lua_pushstring(L, "Unknown");
        return;
    }

    luaL_Buffer b;
    luaL_buffinit(L, &b);
    luaL_addstring(&b, name.sysname);
    luaL_addchar(&b, ' ');
    luaL_addstring(&b, name.nodename);
    luaL_addchar(&b, ' ');
    luaL_addstring(&b, name.release);
    luaL_addchar(&b, ' ');
    luaL_addstring(&b, name.version);
    luaL_addchar(&b, ' ');
    luaL_addstring(&b, name.machine);
    luaL_pushresult(&b);
}
예제 #5
0
파일: format_keys.c 프로젝트: lukem/lkonf
lkonf_error
lki_format_keys(
	lkonf_context *	iLc,
	lkonf_keys	iKeys,
	size_t		iMaxKeys)
{
	if (! iLc) {
		return LK_INVALID_ARGUMENT;
	}

	luaL_Buffer lb;
	luaL_buffinit(iLc->state, &lb);

	size_t ki;
	for (ki = 0; iKeys[ki]; ++ki) {
		if (iMaxKeys && ki >= iMaxKeys) {
			break;
		}
		if (ki) {
			luaL_addstring(&lb, ".");
		}
		luaL_addstring(&lb, "\"");
		luaL_addstring(&lb, iKeys[ki]);
		luaL_addstring(&lb, "\"");
	}

	luaL_pushresult(&lb);

	return LK_OK;
}
예제 #6
0
static void
mark_function(lua_State *L, lua_State *dL, const void * parent, const char *desc) {
    int i;
    const void * t = readobject(L, dL, parent, desc);
    if (t == NULL)
        return;

    mark_function_env(L,dL,t);
    for (i=1;; i++) {
        const char *name = lua_getupvalue(L,-1,i);
        if (name == NULL)
            break;
        mark_object(L, dL, t, name[0] ? name : "[upvalue]");
    }
    if (lua_iscfunction(L,-1)) {
        if (i==1) {
            // light c function
            lua_pushnil(dL);
            lua_rawsetp(dL, FUNCTION, t);
        }
        lua_pop(L,1);
    } else {
        lua_Debug ar;
        luaL_Buffer b;
        char tmp[16];

        lua_getinfo(L, ">S", &ar);
        luaL_buffinit(dL, &b);
        luaL_addstring(&b, ar.short_src);
        sprintf(tmp,":%d",ar.linedefined);
        luaL_addstring(&b, tmp);
        luaL_pushresult(&b);
        lua_rawsetp(dL, SOURCE, t);
    }
}
예제 #7
0
파일: test_api.c 프로젝트: fundies/Aegisub
/* Note this one does not dump values to protect from embedded zeroes. */
static int dump_lua_stack(lua_State * L, int base)
{
    int top = lua_gettop(L);

    if (top == 0)
    {
        lua_pushliteral(L, "-- stack is empty --");
    }
    else
    {
        int pos = 0;
        luaL_Buffer b;

        luaL_buffinit(L, &b);

        for (pos = top; pos > 0; --pos)
        {
            luaL_addstring(&b, (pos != base) ? "[" : "{");
            lua_pushinteger(L, pos);
            luaL_addvalue(&b);
            luaL_addstring(&b, (pos != base) ? "] - " : "} -");
            luaL_addstring(&b, luaL_typename(L, pos));
            luaL_addstring(&b, "\n");
        }

        luaL_pushresult(&b);
    }

    if (lua_gettop(L) != top + 1)
    {
        return luaL_error(L, "dumpstack not balanced %d %d", top, lua_gettop(L));
    }

    return 1;
}
예제 #8
0
void readDirCallback(const char* origpath, const char* file, void* opaque) {
    lua_State* L = ((readDirData_t*)opaque)->L;
    int* tableindex = ((readDirData_t*)opaque)->tableindex;
    luaL_Buffer buffer;
    luaL_buffinit(L, &buffer);
    luaL_addstring(&buffer, origpath);
    luaL_addstring(&buffer, "/");
    luaL_addstring(&buffer, file);
    luaL_pushresult(&buffer);
    lua_rawseti(L, -2, (*tableindex)++);
}
예제 #9
0
파일: lauxlib.c 프로젝트: lua/lua
LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s,
                                     const char *p, const char *r) {
  const char *wild;
  size_t l = strlen(p);
  while ((wild = strstr(s, p)) != NULL) {
    luaL_addlstring(b, s, wild - s);  /* push prefix */
    luaL_addstring(b, r);  /* push replacement in place of pattern */
    s = wild + l;  /* continue after 'p' */
  }
  luaL_addstring(b, s);  /* push last suffix */
}
예제 #10
0
void
makeIconRegRegKey(lua_State* L, 
                  const char* name, 
                  const char* id)
{
  luaL_Buffer B;
  luaL_buffinit(L, &B);
  luaL_addstring(&B, NEVER_NULL((char*)name));
  luaL_addchar(&B, '.');
  luaL_addstring(&B, NEVER_NULL((char*)id));
  luaL_pushresult(&B);
}
예제 #11
0
static void
mark_thread(lua_State *L, lua_State *dL, const void * parent, const char *desc) {
	const void * t = readobject(L, dL, parent, desc);
	if (t == NULL)
		return;
	int level = 0;
	lua_State *cL = lua_tothread(L,-1);
	if (cL == L) {
		level = 1;
	} else {
		// mark stack
		int top = lua_gettop(cL);
		luaL_checkstack(cL, 1, NULL);
		int i;
		char tmp[16];
		for (i=0;i<top;i++) {
			lua_pushvalue(cL, i+1);
			sprintf(tmp, "[%d]", i+1);
			mark_object(cL, dL, cL, tmp);
		}
	}
	lua_Debug ar;
	luaL_Buffer b;
	luaL_buffinit(dL, &b);
	while (lua_getstack(cL, level, &ar)) {
		char tmp[128];
		lua_getinfo(cL, "Sl", &ar);
		luaL_addstring(&b, ar.short_src);
		if (ar.currentline >=0) {
			char tmp[16];
			sprintf(tmp,":%d ",ar.currentline);
			luaL_addstring(&b, tmp);
		}

		int i,j;
		for (j=1;j>-1;j-=2) {
			for (i=j;;i+=j) {
				const char * name = lua_getlocal(cL, &ar, i);
				if (name == NULL)
					break;
				snprintf(tmp, sizeof(tmp), "%s : %s:%d",name,ar.short_src,ar.currentline);
				mark_object(cL, dL, t, tmp);
			}
		}

		++level;
	}
	luaL_addstring(&b, "thread: ");
	luaL_pushresult(&b);
	lua_rawsetp(dL, SOURCE, t);
	lua_pop(L,1);
}
예제 #12
0
파일: mime.c 프로젝트: libyyu/build
__CFunEnd
/*=========================================================================*\
* Global Lua functions
\*=========================================================================*/
/*-------------------------------------------------------------------------*\
* Incrementaly breaks a string into lines. The string can have CRLF breaks.
* A, n = wrp(l, B, length)
* A is a copy of B, broken into lines of at most 'length' bytes.
* 'l' is how many bytes are left for the first line of B.
* 'n' is the number of bytes left in the last line of A.
\*-------------------------------------------------------------------------*/
static int mime_global_wrp(lua_State *L)
{
    size_t size = 0;
    int left = (int) luaL_checknumber(L, 1);
    const UC *input = (const UC *) luaL_optlstring(L, 2, NULL, &size);
    const UC *last = input + size;
    int length = (int) luaL_optnumber(L, 3, 76);
    luaL_Buffer buffer;
    /* end of input black-hole */
    if (!input) {
        /* if last line has not been terminated, add a line break */
        if (left < length) lua_pushstring(L, CRLF);
        /* otherwise, we are done */
        else lua_pushnil(L);
        lua_pushnumber(L, length);
        return 2;
    }
    luaL_buffinit(L, &buffer);
    while (input < last) {
        switch (*input) {
            case '\r':
                break;
            case '\n':
                luaL_addstring(&buffer, CRLF);
                left = length;
                break;
            default:
                if (left <= 0) {
                    left = length;
                    luaL_addstring(&buffer, CRLF);
                }
                luaL_addchar(&buffer, *input);
                left--;
                break;
        }
        input++;
    }
    luaL_pushresult(&buffer);
    lua_pushnumber(L, left);
    return 2;
}
예제 #13
0
LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
															   const char *r) {
  const char *wild;
  size_t l = strlen(p);
  luaL_Buffer b;
  luaL_buffinit(L, &b);
  while ((wild = strstr(s, p)) != NULL) {
	luaL_addlstring(&b, s, wild - s);  /* push prefix */
	luaL_addstring(&b, r);  /* push replacement in place of pattern */
	s = wild + l;  /* continue after `p' */
  }
  luaL_addstring(&b, s);  /* push last suffix */
  luaL_pushresult(&b);
  return lua_tostring(L, -1);
}
예제 #14
0
파일: lup.c 프로젝트: tfiz/lupyter
int stringifier_LuaCB(lua_State *L) {
   luaL_Buffer B;
   luaL_buffinit(L, &B);

   int top = lua_gettop(L);
   for (int i = 1; i <= top; i++) {
      if (lua_isnil(L, i)) {
         luaL_addstring(&B, "nil");
      }
      else {
         // gross
         // because lua_tostring blows
         lua_getglobal(L, "tostring");
         lua_pushvalue(L, i);
         if (lua_pcall(L, 1, 1, 0) != 0) {
            printf("fail\n");
            exit(1);
         }
         const char *str = lua_tostring(L, -1);
         size_t str_len = strlen(str);
         char t[str_len + 1];
         memcpy(t, str, str_len + 1);
         lua_pop(L, 1);
         luaL_addstring(&B, t);
      }
      if (i < top) {
         luaL_addchar(&B, '\t');
      }
   }
   luaL_pushresult(&B);
   if (lua_isnil(L, -1)) return 0;
   lua_getfield(L, LUA_REGISTRYINDEX, stringified);
   if (!lua_isnil(L, -1)) {
      const char *previous = lua_tostring(L, -1);
      const char *current = lua_tostring(L, -2);
      if (strcmp(current, "") == 0) {
          return 0;
      }
      lua_pushfstring(L, "%s\n%s", previous, current);
      lua_replace(L, -3);
      lua_pop(L, 1);
   }
   else {
      lua_pop(L, 1);
   }
   lua_setfield(L, LUA_REGISTRYINDEX, stringified);
   return 0;
}
예제 #15
0
파일: lua_wrap.c 프로젝트: Keseril/libquvi
static int l_quvi_resolve(lua_State *l)
{
  char *redirect_url;
  _quvi_media_t m;
  QUVIcode rc;

  m = (_quvi_media_t) getfield_reg_userdata(l, USERDATA_QUVI_MEDIA_T);
  assert(m != NULL);

  if (!lua_isstring(l,1))
    luaL_error(l, "`quvi.resolve' expects `url' argument");

  /* net_wrap.c */
  rc = resolve_wrapper(m->quvi, lua_tostring(l,1), &redirect_url);

  if (rc == QUVI_OK)
    {
      luaL_Buffer b;
      luaL_buffinit(l,&b);
      luaL_addstring(&b, redirect_url ? redirect_url : "");
      luaL_pushresult(&b);
    }

  _free(redirect_url);

  if (rc != QUVI_OK)
    luaL_error(l, "%s", m->quvi->errmsg);

  return (1);
}
예제 #16
0
파일: lua_wrap.c 프로젝트: Keseril/libquvi
static int l_quvi_fetch(lua_State *l)
{
  _quvi_media_t m;
  _quvi_net_t n;
  QUVIcode rc;

  m = (_quvi_media_t) getfield_reg_userdata(l, USERDATA_QUVI_MEDIA_T);
  assert(m != NULL);

  rc = fetch_wrapper(m->quvi, l, &n); /* net_wrap.c */
  if (rc == QUVI_OK)
    {
      luaL_Buffer b;

      if (!m->charset)
        run_lua_charset_func(m, n->fetch.content);

      luaL_buffinit(l, &b);
      luaL_addstring(&b, n->fetch.content);
      luaL_pushresult(&b);
    }

  free_net_handle(&n);

  if (rc != QUVI_OK)
    luaL_error(l, "%s", m->quvi->errmsg);

  return (1);
}
예제 #17
0
int Xml_encode(lua_State *L) {
	if(lua_gettop(L)!=1)
		return 0;
	luaL_checkstring(L,-1);
	size_t i;
	for(i=0; i<sv_code_size; i+=2) {
		luaL_gsub(L, lua_tostring(L,-1), sv_code[i], sv_code[i+1]);
		lua_remove(L,-2);
	}
	char buf[8];
	const char* s=lua_tostring(L,1);
	size_t start, pos;
	luaL_Buffer b;
	luaL_buffinit(L, &b);
	for(start=pos=0; s[pos]!=0; ++pos) if(s[pos]<0) {
		if(pos>start) luaL_addlstring(&b,s+start, pos-start);
		luaL_addstring(&b,char2code((unsigned char)(s[pos]),buf));
		start=pos+1;
	}
	if(pos>start)
		luaL_addlstring(&b,s+start, pos-start);
	luaL_pushresult(&b);
	lua_remove(L,-2);
	return 1;
}
예제 #18
0
파일: lconvlib.c 프로젝트: Udo/np
static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
  size_t l;
  const char *s = luaL_checklstring(L, arg, &l);
  luaL_addchar(b, '"');
  while (l--) {
		switch(*s) {
			case '\n': { luaL_addchar(b, '\\'); luaL_addchar(b, 'n'); break; }
			case '\r': { luaL_addchar(b, '\\'); luaL_addchar(b, 'r'); break; }
			case '"': { luaL_addchar(b, '\\'); luaL_addchar(b, '"'); break; }
			case '\\': { luaL_addchar(b, '\\'); luaL_addchar(b, '\\'); break; }
			case '/': { luaL_addchar(b, '\\'); luaL_addchar(b, '/'); break; }
			case '\b': { luaL_addchar(b, '\\'); luaL_addchar(b, 'b'); break; }
			case '\f': { luaL_addchar(b, '\\'); luaL_addchar(b, 'f'); break; }
			case '\t': { luaL_addchar(b, '\\'); luaL_addchar(b, 't'); break; }
			default: {
				if (*s == '\0' || iscntrl(uchar(*s))) {
		      char buff[10];
        	sprintf(buff, "\\u%04x", (int)uchar(*s));
		      luaL_addstring(b, buff);
		    }
		    else
		      luaL_addchar(b, *s);
			}
		}
    s++;
  }
  luaL_addchar(b, '"');
}
예제 #19
0
파일: gem.c 프로젝트: 1suming/bigworld_ahzs
static int pushchar(int c, int last, const char *marker, 
    luaL_Buffer *buffer) {
  if (candidate(c)) {
    if (candidate(last)) {
      if (c == last) 
        luaL_addstring(buffer, marker);
      return 0;
    } else {
      luaL_addstring(buffer, marker);
      return c;
    }
  } else {
    luaL_putchar(buffer, c);
    return 0;
  }
}
예제 #20
0
파일: lsyncd.c 프로젝트: aking666/lsyncd
/*
| Converts a relative directory path to an absolute.
|
| Params on Lua stack:
|     1: a relative path to directory
|
| Returns on Lua stack:
|     The absolute path of directory
*/
static int
l_realdir( lua_State *L )
{
	luaL_Buffer b;
	const char *rdir = luaL_checkstring(L, 1);
	char *adir = get_realpath(rdir);

	if( !adir )
	{
		printlogf(
			L, "Error",
			"failure getting absolute path of [%s]",
			rdir
		);

		return 0;
	}

	{
		// makes sure its a directory
	    struct stat st;
	    if( stat( adir, &st ) )
		{
			printlogf(
				L, "Error",
				"cannot get absolute path of dir '%s': %s",
				rdir,
				strerror( errno )
			);

			free( adir );

			return 0;
		}

	    if( !S_ISDIR( st.st_mode ) )
		{
			printlogf(
				L, "Error",
				"cannot get absolute path of dir '%s': is not a directory",
				rdir
			);

			free( adir );

			return 0;
	    }
	}

	// returns absolute path with a concated '/'
	luaL_buffinit( L, &b );
	luaL_addstring( &b, adir );
	luaL_addchar( &b, '/' );
	luaL_pushresult( &b );

	free( adir );

	return 1;
}
예제 #21
0
파일: lconvlib.c 프로젝트: Udo/np
static void addquotedHTML (lua_State *L, luaL_Buffer *b, int arg) {
  size_t l;
  const char *s = luaL_checklstring(L, arg, &l);
  while (l--) {
		switch(*s) {
			case '"': { luaL_addstring(b, "&quot;"); break; }
			case '\'': { luaL_addstring(b, "&apos;"); break; }
			case '&': { luaL_addstring(b, "&amp;"); break; }
			case '<': { luaL_addstring(b, "&lt;"); break; }
			case '>': { luaL_addstring(b, "&gt;"); break; }
			default: {
	      luaL_addchar(b, *s);
			}
		}
    s++;
  }
}
예제 #22
0
static void
gen_table_desc(lua_State *dL, luaL_Buffer *b, const void * parent, const char *desc) {
    char tmp[32];
    size_t l = sprintf(tmp,"%p : ",parent);
    luaL_addlstring(b, tmp, l);
    luaL_addstring(b, desc);
    luaL_addchar(b, '\n');
}
예제 #23
0
파일: lua.c 프로젝트: Cynede/hexchat
static void inject_string(script_info *info, char const *line)
{
	lua_State *L = info->state;
	int base, top;
	char *ret_line;
	gboolean force_ret = FALSE;

	if(line[0] == '=')
	{
		line++;
		force_ret = TRUE;
	}
	ret_line = g_strconcat("return ", line, NULL);

	lua_rawgeti(L, LUA_REGISTRYINDEX, info->traceback);
	base = lua_gettop(L);
	if(luaL_loadbuffer(L, ret_line, strlen(ret_line), "@interpreter"))
	{
		if(!force_ret)
			lua_pop(L, 1);
		if(force_ret || luaL_loadbuffer(L, line, strlen(line), "@interpreter"))
		{
			hexchat_printf(ph, "Lua syntax error: %s", luaL_optstring(L, -1, ""));
			lua_pop(L, 2);
			g_free(ret_line);
			return;
		}
	}
	g_free(ret_line);
	info->status |= STATUS_ACTIVE;
	if(lua_pcall(L, 0, LUA_MULTRET, base))
	{
		char const *error = lua_tostring(L, -1);
		lua_pop(L, 2);
		hexchat_printf(ph, "Lua error: %s", error ? error : "(non-string error)");
		return;
	}
	top = lua_gettop(L);
	if(top > base)
	{
		int i;
		luaL_Buffer b;
		luaL_buffinit(L, &b);
		for(i = base + 1; i <= top; i++)
		{
			if(i != base + 1)
				luaL_addstring(&b, " ");
			tostring(L, i);
			luaL_addvalue(&b);
		}
		luaL_pushresult(&b);
		hexchat_print(ph, lua_tostring(L, -1));
		lua_pop(L, top - base + 1);
	}
	lua_pop(L, 1);
	check_deferred(info);
}
예제 #24
0
파일: lbind.c 프로젝트: ifzz/nui
LB_API const char *lbind_dumpstack(lua_State *L, const char *msg) {
  int i, top = lua_gettop(L);
  luaL_Buffer b;
  luaL_buffinit(L, &b);
  luaL_addstring(&b, "dump stack: ");
  luaL_addstring(&b, msg != NULL ? msg : "");
  luaL_addstring(&b, "\n---------------------------\n");
  for (i = 1; i <= top; ++i) {
    lua_pushfstring(L, "%d: ", i);
    luaL_addvalue(&b);
    lbind_tolstring(L, i, NULL);
    luaL_addvalue(&b);
    luaL_addstring(&b, "\n");
  }
  luaL_addstring(&b, "---------------------------\n");
  luaL_pushresult(&b);
  return lua_tostring(L, -1);
}
예제 #25
0
static int l_window_tostring(lua_State *L)
{
	luaL_Buffer	b;

	luaL_buffinit(L, &b);
	luaL_addstring(&b, "l_sdl_window");
	luaL_pushresult(&b);

	return 1;
}
예제 #26
0
static int luahtml_encode(lua_State *L)
{
	struct luahtml_readdata rd;
	char sbuf[16];
	luaL_Buffer b;
	int c;

	if (lua_isuserdata(L, 1))
	{
		rd.utf8.file = luahtml_tofile(L);
		rd.utf8.readchar = luahtml_readfile;
	}
	else
	{
		rd.utf8.src = (unsigned char *) lua_tolstring(L, 1, &rd.utf8.srclen);
		rd.utf8.readchar = luahtml_readstring;
	}

	luaL_buffinit(L, &b);

	rd.utf8.accu = 0;
	rd.utf8.numa = 0;
	rd.utf8.bufc = -1;

	while ((c = luahtml_readutf8(&rd.utf8)) >= 0)
	{
		switch(c)
		{
			case 34:
				luaL_addlstring(&b, "&quot;", 6);
				break;
			case 38:
				luaL_addlstring(&b, "&amp;", 5);
				break;
			case 60:
				luaL_addlstring(&b, "&lt;", 4);
				break;
			case 62:
				luaL_addlstring(&b, "&gt;", 4);
				break;
			default:
				if (/*c == 91 || c == 93 ||*/ c > 126)
				{
					sprintf(sbuf, "&#%03d;", c);
					luaL_addstring(&b, sbuf);
				}
				else
					luaL_addchar(&b, c);
		}
	}

	luaL_pushresult(&b);
	return 1;
}
예제 #27
0
/*-------------------------------------------------------------------------*\
* Incrementaly breaks a string into lines. The string can have CRLF breaks.
* A, n = wrp(l, B, length)
* A is a copy of B, broken into lines of at most 'length' bytes. 
* 'l' is how many bytes are left for the first line of B. 
* 'n' is the number of bytes left in the last line of A. 
\*-------------------------------------------------------------------------*/
static int mime_global_wrp(lua_State *L)
{
    size_t size = 0;
    int left = (int) luaL_checknumber(L, 1);
    const UC *input = (UC *) luaL_optlstring(L, 2, NULL, &size);
    const UC *last = input + size;
    int length = (int) luaL_optnumber(L, 3, 76);
    luaL_Buffer buffer;
    /* end of input black-hole */
    if (!input) {
        /* if last line has not been terminated, add a line break */
        if (left < length) lua_pushstring(L, CRLF);
        /* otherwise, we are done */
        else lua_pushnil(L);
        lua_pushnumber(L, length);
        return 2;
    } 
    luaL_buffinit(L, &buffer);
    while (input < last) {
        switch (*input) {
            case '\r':
                break;
            case '\n':
                luaL_addstring(&buffer, CRLF);
                left = length;
                break;
            default:
                if (left <= 0) {
                    left = length;
                    luaL_addstring(&buffer, CRLF);
                }
                luaL_addchar(&buffer, *input);
                left--;
                break;
        }
        input++;
    }
    luaL_pushresult(&buffer);
    lua_pushnumber(L, left);
    return 2;
}
예제 #28
0
static int
_try_load(lua_State *L, const char * path, int pathlen, const char * name) {
	int namelen = strlen(name);
	char tmp[pathlen + namelen];
	int i;
	for (i=0;i<pathlen;i++) {
		if (path[i] == '?')
			break;
		tmp[i] = path[i];
	}
	if (path[i] == '?') {
		memcpy(tmp+i,name,namelen);
		memcpy(tmp+i+namelen,path+i+1,pathlen - i -1);
	} else {
		fprintf(stderr,"snlua : Invalid lua service path\n");
		exit(1);
	}
	tmp[namelen+pathlen-1] = '\0';
	//	luacode_loadfile is the same with luaL_loadfile except cache.
#ifdef NOCODECACHE
	int r = luaL_loadfile(L,tmp);
#else
	int r = luacode_loadfile(L,tmp);
#endif
	if (r == LUA_OK) {
		int i;
		for (i=namelen+pathlen-2;i>=0;i--) {
			if (tmp[i] == '/') {
				lua_pushlstring(L,tmp,i+1);
				lua_setglobal(L,"SERVICE_PATH");
				break;
			}
		}
		if (i<0) {
			return 0;
		}
		lua_getglobal(L,"package");
		lua_getfield(L,-1,"path");
		luaL_Buffer b;
		luaL_buffinit(L, &b);
		luaL_addlstring(&b, tmp, i+1);
		luaL_addstring(&b, "?.lua;");
		luaL_addvalue(&b);
		luaL_pushresult(&b);
		lua_setfield(L,-2,"path");
		lua_pop(L,1);
		return 0;
	} else if (r == LUA_ERRFILE) {
		lua_pop(L,1);
		return -1;
	}
	return 1;
}
예제 #29
0
static int ime_join_string(lua_State* L){
  luaL_Buffer buf;
  size_t vec_len; size_t i;
  const char * sep;
  const char * str;

  luaL_checktype(L, 1, LUA_TTABLE);

  sep = luaL_checklstring(L, 2, NULL);
  vec_len = lua_objlen(L, 1);

  if ( 0 == vec_len ){
    lua_pop(L, 2);
    lua_pushliteral(L, "");
    return 1;
  }

  luaL_buffinit(L, &buf);

  for ( i = 1; i < vec_len; ++i){
    lua_pushinteger(L, i);
    lua_gettable(L, 1);
    str = luaL_checklstring(L, 3, NULL);
    luaL_addstring(&buf, str);
    lua_pop(L, 1);
    luaL_addstring(&buf, sep);
  }

  /* add tail of string list */
  lua_pushinteger(L, i);
  lua_gettable(L, 1);
  str = luaL_checklstring(L, 3, NULL);
  luaL_addstring(&buf, str);
  lua_pop(L, 1);
  /* remove the args. */
  lua_pop(L, 2);
  luaL_pushresult(&buf);

  return 1;
}
예제 #30
0
static int hj_validate(lua_State *lua)
{
  heka_json *hj = static_cast<heka_json *>
      (luaL_checkudata(lua, 1, mozsvc_heka_json));
  heka_schema *hs = static_cast<heka_schema *>
      (luaL_checkudata(lua, 2, mozsvc_heka_schema));

  rj::SchemaValidator validator(*hs->doc);
  rj::Value *v = hj->doc ? hj->doc : hj->val;
  if (!v->Accept(validator)) {
    lua_pushboolean(lua, false);
    luaL_Buffer b;
    luaL_buffinit(lua, &b);
    rj::StringBuffer sb;
    validator.GetInvalidSchemaPointer().StringifyUriFragment(sb);
    luaL_addstring(&b, "SchemaURI: ");
    luaL_addstring(&b, sb.GetString());
    luaL_addstring(&b, " Keyword: ");
    luaL_addstring(&b, validator.GetInvalidSchemaKeyword());
    sb.Clear();
    validator.GetInvalidDocumentPointer().StringifyUriFragment(sb);
    luaL_addstring(&b, " DocumentURI: ");
    luaL_addstring(&b, sb.GetString());
    luaL_pushresult(&b);
  }
  return 2; // ok, err
}