naev 0.10.4
cond.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include "naev.h"
13#include "cond.h"
14
15#include "log.h"
16#include "nlua.h"
17#include "nluadef.h"
18
19static nlua_env cond_env = LUA_NOREF;
24int cond_init (void)
25{
26 if (cond_env != LUA_NOREF)
27 return 0;
28
29 cond_env = nlua_newEnv();
30 if (nlua_loadStandard(cond_env)) {
31 WARN(_("Failed to load standard Lua libraries."));
32 return -1;
33 }
34
35 return 0;
36}
37
41void cond_exit (void)
42{
43 nlua_freeEnv(cond_env);
44 cond_env = LUA_NOREF;
45}
46
53int cond_check( const char *cond )
54{
55 int ret;
56
57 /* Load the string directly. */
58 if (strstr( cond, "return" ) != NULL) {
59 lua_pushstring(naevL, cond);
60 }
61 else {
62 /* Append "return" first. */
63 lua_pushstring(naevL, "return ");
64 lua_pushstring(naevL, cond);
65 lua_concat(naevL, 2);
66 }
67 ret = nlua_dobufenv(cond_env, lua_tostring(naevL,-1),
68 lua_strlen(naevL,-1), "Lua Conditional");
69 switch (ret) {
70 case LUA_ERRSYNTAX:
71 WARN(_("Lua conditional syntax error: %s"), lua_tostring(naevL, -1));
72 goto cond_err;
73 case LUA_ERRRUN:
74 WARN(_("Lua Conditional had a runtime error: %s"), lua_tostring(naevL, -1));
75 goto cond_err;
76 case LUA_ERRMEM:
77 WARN(_("Lua Conditional ran out of memory: %s"), lua_tostring(naevL, -1));
78 goto cond_err;
79 case LUA_ERRERR:
80 WARN(_("Lua Conditional had an error while handling error function: %s"), lua_tostring(naevL, -1));
81 goto cond_err;
82 default:
83 break;
84 }
85
86 /* Check the result. */
87 if (lua_isboolean(naevL, -1)) {
88 ret = !!lua_toboolean(naevL, -1);
89 lua_pop(naevL, 1);
90
91 /* Clear the stack. */
92 lua_settop(naevL, 0);
93
94 return ret;
95 }
96 WARN(_("Lua Conditional didn't return a boolean"));
97
98cond_err:
99 /* Clear the stack. */
100 lua_settop(naevL, 0);
101 return -1;
102}
int cond_check(const char *cond)
Checks to see if a condition is true.
Definition: cond.c:53
void cond_exit(void)
Destroys the conditional subsystem.
Definition: cond.c:41
int cond_init(void)
Initializes the conditional subsystem.
Definition: cond.c:24
Header file with generic functions and naev-specifics.
int nlua_loadStandard(nlua_env env)
Loads the standard Naev Lua API.
Definition: nlua.c:760