naev 0.11.5
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_compile( const char *cond )
54{
55 int ret, ref;
56 char buf[STRMAX_SHORT];
57
58 /* Load the string directly. */
59 if (strstr( cond, "return" ) != NULL) {
60 lua_pushstring(naevL, cond);
61 }
62 else {
63 /* Append "return" first. */
64 lua_pushstring(naevL, "return ");
65 lua_pushstring(naevL, cond);
66 lua_concat(naevL, 2);
67 }
68 ret = luaL_loadbuffer( naevL, lua_tostring(naevL,-1), lua_strlen(naevL,-1), "Lua Conditional" );
69 switch (ret) {
70 case LUA_ERRSYNTAX:
71 snprintf( buf, sizeof(buf), _("Lua conditional syntax error: %s"), lua_tostring(naevL, -1));
72 goto cond_err;
73 case LUA_ERRMEM:
74 snprintf( buf, sizeof(buf), _("Lua Conditional ran out of memory: %s"), lua_tostring(naevL, -1));
75 goto cond_err;
76 default:
77 break;
78 }
79 ref = luaL_ref( naevL, LUA_REGISTRYINDEX ); /* pops */
80 lua_pop( naevL, 1 );
81 return ref;
82
83cond_err:
85 WARN( "%s", buf );
86
87 /* Clear the stack. */
88 lua_settop(naevL, 0);
89 return LUA_NOREF;
90}
91
98int cond_check( const char *cond )
99{
100 int ret;
101 char buf[STRMAX_SHORT];
102
103 /* Load the string directly. */
104 if (strstr( cond, "return" ) != NULL) {
105 lua_pushstring(naevL, cond);
106 }
107 else {
108 /* Append "return" first. */
109 lua_pushstring(naevL, "return ");
110 lua_pushstring(naevL, cond);
111 lua_concat(naevL, 2);
112 }
113 ret = nlua_dobufenv(cond_env, lua_tostring(naevL,-1),
114 lua_strlen(naevL,-1), "Lua Conditional");
115 switch (ret) {
116 case LUA_ERRSYNTAX:
117 snprintf( buf, sizeof(buf), _("Lua conditional syntax error: %s"), lua_tostring(naevL, -1));
118 goto cond_err;
119 case LUA_ERRRUN:
120 snprintf( buf, sizeof(buf), _("Lua Conditional had a runtime error: %s"), lua_tostring(naevL, -1));
121 goto cond_err;
122 case LUA_ERRMEM:
123 snprintf( buf, sizeof(buf), _("Lua Conditional ran out of memory: %s"), lua_tostring(naevL, -1));
124 goto cond_err;
125 case LUA_ERRERR:
126 snprintf( buf, sizeof(buf), _("Lua Conditional had an error while handling error function: %s"), lua_tostring(naevL, -1));
127 goto cond_err;
128 default:
129 break;
130 }
131
132 /* Check the result. */
133 if (lua_isboolean(naevL, -1)) {
134 ret = !!lua_toboolean(naevL, -1);
135 lua_pop(naevL, 1);
136
137 /* Clear the stack. */
138 lua_settop(naevL, 0);
139
140 return ret;
141 }
142 snprintf( buf, sizeof(buf), _("Lua Conditional didn't return a boolean"));
143
144cond_err:
146 WARN( "%s", buf );
147
148 /* Clear the stack. */
149 lua_settop(naevL, 0);
150 return -1;
151}
152
153int cond_checkChunk( int chunk, const char *cond )
154{
155 char buf[STRMAX_SHORT];
156 int ret;
157
158 if (chunk==LUA_NOREF) {
159 WARN(_("Trying to run Lua Conditional chunk that is not referenced!"));
160 return 0;
161 }
162
163 ret = nlua_dochunkenv( cond_env, chunk, "Lua Conditional" );
164 switch (ret) {
165 case LUA_ERRRUN:
166 snprintf( buf, sizeof(buf), _("Lua Conditional had a runtime error: %s"), lua_tostring(naevL, -1));
167 goto cond_err;
168 case LUA_ERRMEM:
169 snprintf( buf, sizeof(buf), _("Lua Conditional ran out of memory: %s"), lua_tostring(naevL, -1));
170 goto cond_err;
171 case LUA_ERRERR:
172 snprintf( buf, sizeof(buf), _("Lua Conditional had an error while handling error function: %s"), lua_tostring(naevL, -1));
173 goto cond_err;
174 default:
175 break;
176 }
177
178 /* Check the result. */
179 if (lua_isboolean(naevL, -1)) {
180 ret = !!lua_toboolean(naevL, -1);
181 lua_pop(naevL, 1);
182
183 /* Clear the stack. */
184 lua_settop(naevL, 0);
185
186 return ret;
187 }
188 snprintf( buf, sizeof(buf), _("Lua Conditional didn't return a boolean"));
189
190cond_err:
192 WARN( "%s", buf );
193
194 /* Clear the stack. */
195 lua_settop(naevL, 0);
196 return -1;
197}
int cond_check(const char *cond)
Checks to see if a condition is true.
Definition cond.c:98
void cond_exit(void)
Destroys the conditional subsystem.
Definition cond.c:41
int cond_init(void)
Initializes the conditional subsystem.
Definition cond.c:24
int cond_compile(const char *cond)
Compiles a conditional statement that can then be used as a reference.
Definition cond.c:53
Header file with generic functions and naev-specifics.
int nlua_loadStandard(nlua_env env)
Loads the standard Naev Lua API.
Definition nlua.c:798
void print_with_line_numbers(const char *str)
Prints to stderr with line numbers.
Definition nstring.c:175