コード例 #1
0
ファイル: luasupport.c プロジェクト: caryhaynie/jamplus
static LIST *luahelper_addtolist(ls_lua_State *L, LIST *list, int index)
{
    if (ls_lua_isboolean(L, index))
        return list_append(list, ls_lua_toboolean(L, index) ? "true" : "false", 0);

    if (ls_lua_isnumber(L, index)) {
        LIST* newList;
        ls_lua_pushvalue(L, index);
        newList = list_append(list, ls_lua_tostring(L, -1), 0);
        ls_lua_pop(L, 1);
        return newList;
    }

    if (ls_lua_isstring(L, index)) {
        const char* value = ls_lua_tostring(L, index);
        return list_append(list, value, 0);
    }

    else if (ls_lua_istable(L, index))
    {
        ls_lua_pushnil(L);
        while (ls_lua_next(L, index) != 0)
        {
            list = luahelper_addtolist(L, list, ls_lua_gettop(L));
            ls_lua_pop(L, 1);
        }
    }

    return list;
}
コード例 #2
0
ファイル: luasupport.c プロジェクト: lucasmeijer/jamplus
int LS_jam_setvar(ls_lua_State *L)
{
    int numParams = ls_lua_gettop(L);
    if (numParams < 2  ||  numParams > 3)
	return 0;

    if (!ls_lua_isstring(L, 1))
	return 0;

    if (numParams == 2)
    {
	var_set(ls_lua_tostring(L, 1), luahelper_addtolist(L, L0, 2), VAR_SET);
    }
    else
    {
	TARGET *t;

	if (!ls_lua_isstring(L, 2))
	    return 0;

	t = bindtarget(ls_lua_tostring(L, 1));
	pushsettings(t->settings);
	var_set(ls_lua_tostring(L, 2), luahelper_addtolist(L, L0, 3), VAR_SET);
	popsettings(t->settings);
    }

    return 0;
}
コード例 #3
0
ファイル: luasupport.c プロジェクト: caryhaynie/jamplus
const char* luahelper_linefilter(const char* line, size_t lineSize) {
    int ret;
    int top;
    char* out;

    if (linefilter_stack_position == -1) {
        fprintf(stderr, "jam: Line filter access not enabled.\n");
        exit(1);
    }

    ls_lua_init();

    top = ls_lua_gettop(L);
    ls_lua_pushvalue(L, linefilter_stack_position);
    ls_lua_pushlstring(L, line, lineSize);                    /* function line */
    ret = ls_lua_pcall(L, 1, 1, 0);
    if (ret != 0)
    {
        if (ls_lua_isstring(L, -1))
            fprintf(stderr, "jam: Error running line filter.\n%s\n", ls_lua_tostring(L, -1));
        ls_lua_settop(L, top);
        return NULL;
    }

    out = malloc(ls_lua_rawlen(L, -1) + 1);
    strcpy(out, ls_lua_tostring(L, -1));
    ls_lua_settop(L, top);

    return out;
}
コード例 #4
0
ファイル: luasupport.c プロジェクト: lucasmeijer/jamplus
int luahelper_taskadd(const char* taskscript)
{
    int ret;
    int ref;
    size_t taskscriptlen = strlen(taskscript);
    char* newTaskScript;

    ls_lua_init();

    ls_lua_getfield(L, LUA_GLOBALSINDEX, "lanes");			/* lanes */
    ls_lua_getfield(L, -1, "gen");							/* lanes gen */
    ls_lua_pushstring(L, "*");								/* lanes gen * */

    newTaskScript = malloc( taskscriptlen + 1 );
    strncpy(newTaskScript, taskscript, taskscriptlen);
    newTaskScript[taskscriptlen] = 0;
    ret = ls_luaL_loadstring(L, newTaskScript);			/* lanes gen * script */
    if (ret != 0)
    {
	if (ls_lua_isstring(L, -1))
	    printf("jam: Error compiling Lua lane\n%s\n", ls_lua_tostring(L, -1));
	ls_lua_pop(L, 2);
	printf("%s\n", newTaskScript);
	free(newTaskScript);
	return -1;
    }

    free(newTaskScript);
    ret = ls_lua_pcall(L, 2, 1, 0);						/* lanes lane_h */
    if (ret != 0)
    {
	if (ls_lua_isstring(L, -1))
	    printf("jam: Error creating Lua lane\n%s\n", ls_lua_tostring(L, -1));
	ls_lua_pop(L, 2);
	return -1;
    }

    if (!ls_lua_isfunction(L, -1))							/* lanes lane_h */
    {
	ls_lua_pop(L, 2);
	return -1;
    }

    ret = ls_lua_pcall(L, 0, 1, 0);						/* lanes ret */
    if (ret != 0)
    {
	if (ls_lua_isstring(L, -1))
	    printf("jam: Error calling Lua lane\n%s\n", ls_lua_tostring(L, -1));
	ls_lua_pop(L, 2);
	return -1;
    }

    ref = ls_luaL_ref(L, LUA_REGISTRYINDEX);
    ls_lua_pop(L, 1);
    return ref;
}
コード例 #5
0
ファイル: luasupport.c プロジェクト: caryhaynie/jamplus
static LIST *ls_lua_callhelper(int top, int ret)
{
    LIST *retList;
    int numParams;
    int i;

    if (ret != 0)
    {
        if (ls_lua_isstring(L, -1))
        {
            printf("jam: Error compiling Lua code\n%s\n", ls_lua_tostring(L, -1));
            exit(EXITBAD);
        }
        ls_lua_pop(L, 1);
        return L0;
    }

    ret = ls_lua_pcall(L, 0, -1, 0);
    if (ret != 0)
    {
        if (ls_lua_isstring(L, -1))
        {
            int ret;
            printf("jam: Error running Lua code\n%s\n", ls_lua_tostring(L, -1));
            ls_lua_getglobal(L, "debug");
            ls_lua_getfield(L, -1, "traceback");
            ret = ls_lua_pcall(L, 0, 1, 0);
            if (ret == 0) {
                if (ls_lua_isstring(L, -1)) {
                    puts(ls_lua_tostring(L, -1));
                }
            }
            exit(EXITBAD);
        }
        ls_lua_pop(L, 1);
        return L0;
    }

    retList = L0;

    numParams = ls_lua_gettop(L) - top;
    for (i = 1; i <= numParams; ++i)
    {
        retList = luahelper_addtolist(L, retList, top + i);
    }

    return retList;
}
コード例 #6
0
ファイル: luasupport.c プロジェクト: caryhaynie/jamplus
int LS_jam_expand(ls_lua_State *L)
{
    LIST *list = L0;
    LISTITEM* item;
    int index;

    int numParams = ls_lua_gettop(L);
    if (numParams < 1  ||  numParams > 1)
        return 0;

    if (!ls_lua_isstring(L, 1))
        return 0;

    {
        LOL lol;
        const char* src = ls_lua_tostring(L, 1);
        lol_init(&lol);
        list = var_expand(L0, src, src + strlen(src), &lol, 0);
    }

    ls_lua_newtable(L);
    index = 1;
    for (item = list_first(list); item; item = list_next(item), ++index)
    {
        ls_lua_pushnumber(L, index);
        ls_lua_pushstring(L, list_value(item));
        ls_lua_settable(L, -3);
    }

    return 1;
}
コード例 #7
0
ファイル: luasupport.c プロジェクト: lucasmeijer/jamplus
int LS_jam_evaluaterule(ls_lua_State *L)
{
    LOL lol;
    int i;
    LIST *list;
    int index;

    int numParams = ls_lua_gettop(L);
    if (numParams < 1)
	return 0;

    if (!ls_lua_isstring(L, 1))
	return 0;

    lol_init(&lol);

    for (i = 0; i < numParams - 1; ++i)
    {
	lol_add(&lol, luahelper_addtolist(L, L0, 2 + i));
    }
    list = evaluate_rule(ls_lua_tostring(L, 1), &lol, L0);
    lol_free(&lol);

    ls_lua_newtable(L);
    index = 1;
    for (; list; list = list_next(list), ++index)
    {
	ls_lua_pushnumber(L, index);
	ls_lua_pushstring(L, list->string);
	ls_lua_settable(L, -3);
    }

    return 1;
}
コード例 #8
0
ファイル: luasupport.c プロジェクト: lucasmeijer/jamplus
int luahelper_md5callback(const char *filename, MD5SUM sum, const char* callback)
{
    int ret;

    ls_lua_init();

    ls_lua_getfield(L, LUA_GLOBALSINDEX, callback);
    if (!ls_lua_isfunction(L, -1))
    {
	ls_lua_pop(L, 1);
	printf("jam: Error calling Lua md5 callback '%s'.\n", callback);
	memset(sum, 0, sizeof(MD5SUM));
	return 0;
    }

    ls_lua_pushstring(L, filename);
    ret = ls_lua_pcall(L, 1, 1, 0);
    if (ret != 0)
    {
	if (ls_lua_isstring(L, -1))
	    printf("jam: Error running Lua md5 callback\n%s\n", ls_lua_tostring(L, -1));
	ls_lua_pop(L, 1);
	memset(sum, 0, sizeof(MD5SUM));
	return 0;
    }

    if (ls_lua_isnil(L, -1))
    {
	memset(sum, 0, sizeof(MD5SUM));
	ls_lua_pop(L, 1);
	return 0;
    }

    if (!ls_lua_isstring(L, -1)  ||  ls_lua_objlen(L, -1) != sizeof(MD5SUM))
    {
	printf("jam: Error running Lua md5 callback '%s'.\n", callback);
	memset(sum, 0, sizeof(MD5SUM));
	ls_lua_pop(L, 1);
	return 0;
    }

    memcpy(sum, ls_lua_tostring(L, -1), sizeof(MD5SUM));
    ls_lua_pop(L, 1);
    return 1;
}
コード例 #9
0
ファイル: luasupport.c プロジェクト: caryhaynie/jamplus
int LS_jam_getvar(ls_lua_State *L)
{
    LIST *list;
    LISTITEM* item;
    int index;

    int numParams = ls_lua_gettop(L);
    if (numParams < 1  ||  numParams > 2)
        return 0;

    if (!ls_lua_isstring(L, 1))
        return 0;

    if (numParams == 1)
    {
        list = var_get(ls_lua_tostring(L, 1));
    }
    else
    {
        TARGET *t;

        if (!ls_lua_isstring(L, 2))
            return 0;

        t = bindtarget(ls_lua_tostring(L, 1));
        pushsettings(t->settings);
        list = var_get(ls_lua_tostring(L, 2));
        popsettings(t->settings);
    }

    ls_lua_newtable(L);
    index = 1;
    for (item = list_first(list); item; item = list_next(item), ++index)
    {
        ls_lua_pushnumber(L, index);
        ls_lua_pushstring(L, list_value(item));
        ls_lua_settable(L, -3);
    }

    return 1;
}
コード例 #10
0
ファイル: luasupport.c プロジェクト: caryhaynie/jamplus
int LS_jam_print(ls_lua_State *L)
{
    int numParams = ls_lua_gettop(L);
    if (numParams < 1  ||  numParams > 1)
        return 0;

    if (!ls_lua_isstring(L, 1))
        return 0;

    puts(ls_lua_tostring(L, 1));

    return 0;
}
コード例 #11
0
ファイル: luasupport.c プロジェクト: lucasmeijer/jamplus
static LIST *luahelper_addtolist(ls_lua_State *L, LIST *list, int index)
{
    if (ls_lua_isboolean(L, index))
	return list_new(list, ls_lua_toboolean(L, index) ? "true" : "false", 0);

    if (ls_lua_isnumber(L, index))
	return list_new(list, ls_lua_tostring(L, index), 0);

    if (ls_lua_isstring(L, index))
	return list_new(list, ls_lua_tostring(L, index), 0);

    else if (ls_lua_istable(L, index))
    {
	ls_lua_pushnil(L);
	while (ls_lua_next(L, index) != 0)
	{
	    list = luahelper_addtolist(L, list, index + 2);
	    ls_lua_pop(L, 1);
	}
    }

    return list;
}
コード例 #12
0
ファイル: luasupport.c プロジェクト: caryhaynie/jamplus
static int pmain (ls_lua_State *L)
{
    int top;
    int ret;

    ls_luaL_openlibs(L);

    ls_lua_pushcclosure(L, LS_jam_getvar, 0);
    ls_lua_setglobal(L, "jam_getvar");
    ls_lua_pushcclosure(L, LS_jam_setvar, 0);
    ls_lua_setglobal(L, "jam_setvar");
    ls_lua_pushcclosure(L, LS_jam_action, 0);
    ls_lua_setglobal(L, "jam_action");
    ls_lua_pushcclosure(L, LS_jam_evaluaterule, 0);
    ls_lua_setglobal(L, "jam_evaluaterule");
    ls_lua_pushcclosure(L, LS_jam_expand, 0);
    ls_lua_setglobal(L, "jam_expand");
    ls_lua_pushcclosure(L, LS_jam_parse, 0);
    ls_lua_setglobal(L, "jam_parse");
    ls_lua_pushcclosure(L, LS_jam_print, 0);
    ls_lua_setglobal(L, "jam_print");

    top = ls_lua_gettop(L);
    ret = ls_luaL_loadstring(L, "lanes = require 'lanes'");
    ls_lua_callhelper(top, ret);

    ls_lua_getglobal(L, "lanes");                       /* lanes */
    ls_lua_getfield(L, -1, "configure");                /* lanes configure */
    ls_lua_newtable(L);                                 /* lanes configure table */
    ls_lua_pushcclosure(L, lanes_on_state_create, 0);   /* lanes configure table lanes_on_state_create */
    ls_lua_setfield(L, -2, "on_state_create");          /* lanes configure table */
    ret = ls_lua_pcall(L, 1, 0, 0);                     /* lanes */
    if (ret != 0) {
        const char* err = ls_lua_tostring(L, -1);  (void)err;
	}
    ls_lua_pop(L, 2);

    ls_lua_newtable(L);
    ls_lua_setglobal(L, "LineFilters");

    return 0;
}
コード例 #13
0
ファイル: luasupport.c プロジェクト: caryhaynie/jamplus
int LS_jam_evaluaterule(ls_lua_State *L)
{
    LOL lol;
    int i;
    LIST *list;
    LISTITEM* item;
    int index;
    const char* rule;

    int numParams = ls_lua_gettop(L);
    if (numParams < 1)
        return 0;

    if (!ls_lua_isstring(L, 1))
        return 0;

    lol_init(&lol);

    rule = ls_lua_tostring(L, 1);
    for (i = 0; i < numParams - 1; ++i)
    {
        lol_add(&lol, luahelper_addtolist(L, L0, 2 + i));
    }
    list = evaluate_rule(rule, &lol, L0);
    lol_free(&lol);

    ls_lua_newtable(L);
    index = 1;
    for (item = list_first(list); item; item = list_next(item), ++index)
    {
        ls_lua_pushnumber(L, index);
        ls_lua_pushstring(L, list_value(item));
        ls_lua_settable(L, -3);
    }

    return 1;
}
コード例 #14
0
ファイル: luasupport.c プロジェクト: lucasmeijer/jamplus
void luahelper_taskcancel(int taskid)
{
    int ret;

    ls_lua_init();

    ls_lua_rawgeti(L, LUA_REGISTRYINDEX, taskid);
    ls_lua_pushvalue(L, -1);
    ls_lua_getfield(L, -1, "cancel");
    ls_lua_pushvalue(L, -2);
    ls_lua_pushnumber(L, 0);
    ls_lua_pushboolean(L, 1);

    ret = ls_lua_pcall(L, 3, -1, 0);
    if (ret != 0)
    {
	if (ls_lua_isstring(L, -1))
	    printf("jam: Error running Lua task.cancel\n%s\n", ls_lua_tostring(L, -1));
	ls_lua_pop(L, 2);
	return;
    }

    ls_lua_pop(L, 1);
}
コード例 #15
0
ファイル: luasupport.c プロジェクト: caryhaynie/jamplus
int luahelper_taskadd(const char* taskscript, LOL* args)
{
    int ret;
    int ref;
    size_t taskscriptlen = strlen(taskscript);
    char* newTaskScript;
	int i;

    ls_lua_init();

    ls_lua_getglobal(L, "lanes");                             /* lanes */
    ls_lua_getfield(L, -1, "gen");                            /* lanes gen */
    ls_lua_pushstring(L, "*");                                /* lanes gen * */

    newTaskScript = malloc( taskscriptlen + 1 );
    strncpy(newTaskScript, taskscript, taskscriptlen);
    newTaskScript[taskscriptlen] = 0;
    ret = ls_luaL_loadstring(L, newTaskScript);            /* lanes gen * script */
    if (ret != 0)
    {
        if (ls_lua_isstring(L, -1))
            printf("jam: Error compiling Lua lane\n%s\n", ls_lua_tostring(L, -1));
        ls_lua_pop(L, 2);
        printf("%s\n", newTaskScript);
        free(newTaskScript);
        return -1;
    }

    free(newTaskScript);
    ret = ls_lua_pcall(L, 2, 1, 0);                        /* lanes lane_h */
    if (ret != 0)
    {
        if (ls_lua_isstring(L, -1))
            printf("jam: Error creating Lua lane\n%s\n", ls_lua_tostring(L, -1));
        ls_lua_pop(L, 2);
        return -1;
    }

    if (!ls_lua_isfunction(L, -1))                            /* lanes lane_h */
    {
        ls_lua_pop(L, 2);
        return -1;
    }

    for (i = 0; i < args->count; ++i)
    {
        LIST* list = lol_get(args, i);
        LISTITEM *l2;
        int index = 0;
        ls_lua_newtable(L);
		for (l2 = list_first(list); l2; l2 = list_next(l2))
		{
			ls_lua_pushstring(L, list_value(l2));
			ls_lua_rawseti(L, -2, ++index);
		}
    }

    ret = ls_lua_pcall(L, args->count, 1, 0);                        /* lanes ret */
    if (ret != 0)
    {
        if (ls_lua_isstring(L, -1))
            printf("jam: Error calling Lua lane\n%s\n", ls_lua_tostring(L, -1));
        ls_lua_pop(L, 2);
        return -1;
    }

    ref = ls_luaL_ref(L, LUA_REGISTRYINDEX);
    ls_lua_pop(L, 1);
    return ref;
}
コード例 #16
0
ファイル: luasupport.c プロジェクト: lucasmeijer/jamplus
int luahelper_taskisrunning(int taskid, int* returnValue)
{
	const char* status;
	ls_lua_init();

	ls_lua_rawgeti(L, LUA_REGISTRYINDEX, taskid);		/* lane_h */
	if (!ls_lua_istable(L, -1))
	{
		*returnValue = 1;
		ls_lua_pop(L, 1);
		return 0;
	}
	ls_lua_getfield(L, -1, "status");					/* lane_h status */

	status = ls_lua_tostring(L, -1);
	if (strcmp(status, "done") == 0)
	{
		int ret;

		ls_lua_pop(L, 1);								/* lane_h */
		ls_lua_getfield(L, -1, "join");					/* lane_h join(function) */
		ls_lua_pushvalue(L, -2);						/* lane_h join(function) lane_h */
		ret = ls_lua_pcall(L, 1, 1, 0);					/* lane_h ret */
		if (ret != 0)
		{
			if (ls_lua_isstring(L, -1))
				printf("jam: Error in Lua lane\n%s\n", ls_lua_tostring(L, -1));
			*returnValue = 1;
		}
		if (ls_lua_isnumber(L, -1))
			*returnValue = (int)ls_lua_tonumber(L, -1);
		ls_lua_pop(L, 2);
		ls_luaL_unref(L, LUA_REGISTRYINDEX, taskid);
		return 0;
	}
	else if (strcmp(status, "error") == 0  ||  strcmp(status, "cancelled") == 0)
	{
		int ret;

		*returnValue = 1;

		ls_lua_pop(L, 1);								/* lane_h */
		ls_lua_getfield(L, -1, "join");					/* lane_h join(function) */
		ls_lua_pushvalue(L, -2);						/* lane_h join(function) lane_h */
		ret = ls_lua_pcall(L, 1, 3, 0);					/* lane_h nil err stack_tbl */
		if (ret != 0)
		{
			if (ls_lua_isstring(L, -1))
				printf("jam: Error in Lua lane\n%s\n", ls_lua_tostring(L, -1));
			ls_lua_pop(L, 2);
			return 0;
		}

		if (ls_lua_isstring(L, -2))
		{
			printf("jam: Error in Lua lane\n%s\n", ls_lua_tostring(L, -2));
		}

		ls_lua_pop(L, 4);								/* */

		ls_luaL_unref(L, LUA_REGISTRYINDEX, taskid);
		return 0;
	}

	ls_lua_pop(L, 2);
	return 1;
}
コード例 #17
0
ファイル: luasupport.c プロジェクト: caryhaynie/jamplus
// jam_action(name, function, {options})
int LS_jam_action(ls_lua_State *L)
{
    RULE* rule;
    const char* name;
    int paramIndex = 2;

    int numParams = ls_lua_gettop(L);
    if (numParams < 2) {
        //ls_luaL_error
        return 0;
    }

    if (!ls_lua_isstring(L, 1))
        return 0;
    name = ls_lua_tostring(L, 1);
    rule = bindrule(name);

    if (rule->actions) {
        freestr(rule->actions);
        rule->actions = NULL;
    }

    if (rule->bindlist) {
        list_free(rule->bindlist);
        rule->bindlist = L0;
    }

    if (ls_lua_isstring(L, 2))
        paramIndex = 2;
    else if (ls_lua_isstring(L, 3))
        paramIndex = 3;
    else {
        return 0;
    }

    rule->actions = copystr(ls_lua_tostring(L, paramIndex));
    rule->flags = 0;

    paramIndex = paramIndex == 2 ? 3 : 2;

    if (ls_lua_istable(L, paramIndex)) {
        ls_lua_getfield(L, paramIndex, "bind");
        if (ls_lua_istable(L, -1)) {
            ls_lua_pushnil(L);
            while (ls_lua_next(L, -2) != 0) {
                if (!ls_lua_tostring(L, -1)) {
                    printf("!!\n");
                    exit(1);
                }
                rule->bindlist = list_append(rule->bindlist, ls_lua_tostring(L, -1), 0);
                ls_lua_pop(L, 1);
            }
            ls_lua_pop(L, 1);
        }
        ls_lua_pop(L, 1);

        ls_lua_getfield(L, paramIndex, "updated");
        rule->flags |= ls_lua_toboolean(L, -1) ? RULE_UPDATED : 0;
        ls_lua_pop(L, 1);

        ls_lua_getfield(L, paramIndex, "together");
        rule->flags |= ls_lua_toboolean(L, -1) ? RULE_TOGETHER : 0;
        ls_lua_pop(L, 1);

        ls_lua_getfield(L, paramIndex, "ignore");
        rule->flags |= ls_lua_toboolean(L, -1) ? RULE_IGNORE : 0;
        ls_lua_pop(L, 1);

        ls_lua_getfield(L, paramIndex, "quietly");
        rule->flags |= ls_lua_toboolean(L, -1) ? RULE_QUIETLY : 0;
        ls_lua_pop(L, 1);

        ls_lua_getfield(L, paramIndex, "piecemeal");
        rule->flags |= ls_lua_toboolean(L, -1) ? RULE_PIECEMEAL : 0;
        ls_lua_pop(L, 1);

        ls_lua_getfield(L, paramIndex, "existing");
        rule->flags |= ls_lua_toboolean(L, -1) ? RULE_EXISTING : 0;
        ls_lua_pop(L, 1);

        ls_lua_getfield(L, paramIndex, "response");
        rule->flags |= ls_lua_toboolean(L, -1) ? RULE_RESPONSE : 0;
        ls_lua_pop(L, 1);

        ls_lua_getfield(L, paramIndex, "lua");
        rule->flags |= ls_lua_toboolean(L, -1) ? RULE_LUA : 0;
        ls_lua_pop(L, 1);

        ls_lua_getfield(L, paramIndex, "screenoutput");
        rule->flags |= ls_lua_toboolean(L, -1) ? RULE_SCREENOUTPUT : 0;
        ls_lua_pop(L, 1);

        ls_lua_getfield(L, paramIndex, "removeemptydirs");
        rule->flags |= ls_lua_toboolean(L, -1) ? RULE_REMOVEEMPTYDIRS : 0;
        ls_lua_pop(L, 1);

        ls_lua_getfield(L, paramIndex, "maxtargets");
        if (ls_lua_isnumber(L, -1)) {
            rule->flags |= RULE_MAXTARGETS;
            rule->maxtargets = (int)ls_lua_tonumber(L, -1);
        }
        ls_lua_pop(L, 1);

        ls_lua_getfield(L, paramIndex, "maxline");
        if (ls_lua_isnumber(L, -1)) {
            rule->flags |= RULE_MAXLINE;
            rule->maxline = (int)ls_lua_tonumber(L, -1);
        }
        ls_lua_pop(L, 1);
    }

    return 0;
}
コード例 #18
0
ファイル: luasupport.c プロジェクト: caryhaynie/jamplus
int LS_jam_parse(ls_lua_State *L)
{
    int numParams = ls_lua_gettop(L);
    if (numParams < 1  ||  numParams > 1)
        return 0;

    if (!ls_lua_isstring(L, 1))
        return 0;

    {
        const char* src = ls_lua_tostring(L, 1);
        const char* ptr = src;
        const char* startPtr;
        char** lines;
        int numberOfLines = 1;
        int status;

        while (*ptr) {
            if (*ptr == '\n') {
                ++numberOfLines;
            }
            ++ptr;
        }
        lines = malloc(sizeof(char*) * (numberOfLines + 1));
        numberOfLines = 0;
        startPtr = ptr = src;
        while (1) {
            if (*ptr == '\n'  ||  *ptr == 0) {
                char* line;
                if (*ptr == '\n')
                    ++ptr;
                line = malloc(ptr - startPtr + 1);
                memcpy(line, startPtr, ptr - startPtr);
                line[ptr - startPtr] = 0;
                startPtr = ptr;
                lines[numberOfLines++] = line;
                if (*ptr == 0)
                    break;
            } else {
                ++ptr;
            }
        }
        lines[numberOfLines] = 0;
        parse_lines(lines[0], lines);

        status = yyanyerrors();

        while (numberOfLines > 0) {
            free(lines[--numberOfLines]);
        }
        free(lines);

        if (status) {
            struct ls_lua_Debug ar;
            if (ls_lua_getstack(L, 1, &ar)) {
                ls_lua_getinfo(L, "nSl", &ar);
            }

            printf("jam: Error parsing Jam code near %s[%d].\n", ar.short_src, ar.currentline);
            exit(EXITBAD);
        }
    }

    return 0;
}