]>
git.proxmox.com Git - mirror_frr.git/blob - lib/lua.c
2 * This file defines the lua interface into
5 * Copyright (C) 2016 Cumulus Networks, Inc.
8 * This file is part of FreeRangeRouting (FRR).
10 * FRR is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free Software
12 * Foundation; either version 2, or (at your option) any later version.
14 * FRR is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License along
20 * with FRR; see the file COPYING. If not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 static int lua_zlog_debug(lua_State
*L
)
35 const char *string
= lua_tostring(L
, 1);
38 zlog_debug("%s", string
);
43 const char *get_string(lua_State
*L
, const char *key
)
47 lua_pushstring(L
, key
);
50 str
= (const char *)lua_tostring(L
, -1);
56 int get_integer(lua_State
*L
, const char *key
)
60 lua_pushstring(L
, key
);
63 result
= lua_tointeger(L
, -1);
69 static void *lua_alloc(void *ud
, void *ptr
, size_t osize
,
72 (void)ud
; (void)osize
; /* not used */
77 return realloc(ptr
, nsize
);
80 lua_State
*lua_initialize(const char *file
)
83 lua_State
*L
= lua_newstate(lua_alloc
, NULL
);
85 zlog_debug("Newstate: %p", L
);
87 zlog_debug("Opened lib");
88 status
= luaL_loadfile(L
, file
);
90 zlog_debug("Failure to open %s %d", file
, status
);
95 lua_pcall(L
, 0, LUA_MULTRET
, 0);
96 zlog_debug("Setting global function");
97 lua_pushcfunction(L
, lua_zlog_debug
);
98 lua_setglobal(L
, "zlog_debug");
103 void lua_setup_prefix_table(lua_State
*L
, const struct prefix
*prefix
)
108 lua_pushstring(L
, prefix2str(prefix
, buffer
, 100));
109 lua_setfield(L
, -2, "route");
110 lua_pushinteger(L
, prefix
->family
);
111 lua_setfield(L
, -2, "family");
112 lua_setglobal(L
, "prefix");
115 enum lua_rm_status
lua_run_rm_rule(lua_State
*L
, const char *rule
)
119 lua_getglobal(L
, rule
);
120 status
= lua_pcall(L
, 0, 1, 0);
122 zlog_debug("Executing Failure with function: %s: %d",
124 return LUA_RM_FAILURE
;
127 status
= lua_tonumber(L
, -1);