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