mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2024-11-22 12:31:10 +00:00
[ruamoko] Fortify and extend the script api
Meaning some leaks have been plugged, and some useful functions added: loading a file (avoids polluting progs memory), setting the single character lexeme string, and getting the line number.
This commit is contained in:
parent
ac42bca98b
commit
a22c2224e0
3 changed files with 88 additions and 9 deletions
|
@ -44,50 +44,59 @@
|
||||||
|
|
||||||
#include "rua_internal.h"
|
#include "rua_internal.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct rua_script_s {
|
||||||
|
struct rua_script_s *next;
|
||||||
|
struct rua_script_s **prev;
|
||||||
script_t script;
|
script_t script;
|
||||||
|
char *text; // only for Script_FromFile
|
||||||
pr_string_t dstr;
|
pr_string_t dstr;
|
||||||
progs_t *pr;
|
progs_t *pr;
|
||||||
} rua_script_t;
|
} rua_script_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
PR_RESMAP(rua_script_t) scripts;
|
PR_RESMAP(rua_script_t) script_map;
|
||||||
|
rua_script_t *scripts;
|
||||||
} script_resources_t;
|
} script_resources_t;
|
||||||
|
|
||||||
static rua_script_t *
|
static rua_script_t *
|
||||||
script_new (script_resources_t *res)
|
script_new (script_resources_t *res)
|
||||||
{
|
{
|
||||||
return PR_RESNEW (res->scripts);
|
return PR_RESNEW (res->script_map);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
script_free (script_resources_t *res, rua_script_t *script)
|
script_free (script_resources_t *res, rua_script_t *script)
|
||||||
{
|
{
|
||||||
PR_RESFREE (res->scripts, script);
|
PR_RESFREE (res->script_map, script);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
script_reset (script_resources_t *res)
|
script_reset (script_resources_t *res)
|
||||||
{
|
{
|
||||||
PR_RESRESET (res->scripts);
|
PR_RESRESET (res->script_map);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline rua_script_t *
|
static inline rua_script_t *
|
||||||
script_get (script_resources_t *res, int index)
|
script_get (script_resources_t *res, int index)
|
||||||
{
|
{
|
||||||
return PR_RESGET(res->scripts, index);
|
return PR_RESGET(res->script_map, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int __attribute__((pure))
|
static inline int __attribute__((pure))
|
||||||
script_index (script_resources_t *res, rua_script_t *script)
|
script_index (script_resources_t *res, rua_script_t *script)
|
||||||
{
|
{
|
||||||
return PR_RESINDEX(res->scripts, script);
|
return PR_RESINDEX(res->script_map, script);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
bi_script_clear (progs_t *pr, void *_res)
|
bi_script_clear (progs_t *pr, void *_res)
|
||||||
{
|
{
|
||||||
script_resources_t *res = (script_resources_t *) _res;
|
script_resources_t *res = (script_resources_t *) _res;
|
||||||
|
for (rua_script_t *s = res->scripts; s; s = s->next) {
|
||||||
|
free ((char *) s->script.single);
|
||||||
|
free (s->text);
|
||||||
|
}
|
||||||
|
res->scripts = 0;
|
||||||
script_reset (res);
|
script_reset (res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,6 +118,12 @@ bi_Script_New (progs_t *pr, void *_res)
|
||||||
script->dstr = PR_NewMutableString (pr);
|
script->dstr = PR_NewMutableString (pr);
|
||||||
script->script.token = PR_GetMutableString (pr, script->dstr);
|
script->script.token = PR_GetMutableString (pr, script->dstr);
|
||||||
script->pr = pr;
|
script->pr = pr;
|
||||||
|
script->next = res->scripts;
|
||||||
|
script->prev = &res->scripts;
|
||||||
|
if (res->scripts) {
|
||||||
|
res->scripts->prev = &script->next;
|
||||||
|
}
|
||||||
|
res->scripts = script;
|
||||||
R_INT (pr) = script_index (res, script);
|
R_INT (pr) = script_index (res, script);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,6 +136,12 @@ bi_Script_Delete (progs_t *pr, void *_res)
|
||||||
if (!script)
|
if (!script)
|
||||||
PR_RunError (pr, "invalid script handle");
|
PR_RunError (pr, "invalid script handle");
|
||||||
PR_FreeString (pr, script->dstr);
|
PR_FreeString (pr, script->dstr);
|
||||||
|
free ((char *) script->script.single);
|
||||||
|
free (script->text);
|
||||||
|
if (script->next) {
|
||||||
|
script->next->prev = script->prev;
|
||||||
|
}
|
||||||
|
*script->prev = script->next;
|
||||||
script_free (res, script);
|
script_free (res, script);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,6 +157,29 @@ bi_Script_Start (progs_t *pr, void *_res)
|
||||||
R_STRING (pr) = script->dstr;
|
R_STRING (pr) = script->dstr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
bi_Script_FromFile (progs_t *pr, void *_res)
|
||||||
|
{
|
||||||
|
script_resources_t *res = _res;
|
||||||
|
rua_script_t *script = script_get (res, P_INT (pr, 0));
|
||||||
|
if (!script)
|
||||||
|
PR_RunError (pr, "invalid script handle");
|
||||||
|
QFile *file = QFile_GetFile (pr, P_INT (pr, 2));
|
||||||
|
long offset;
|
||||||
|
long size;
|
||||||
|
long len;
|
||||||
|
|
||||||
|
offset = Qtell (file);
|
||||||
|
size = Qfilesize (file);
|
||||||
|
len = size - offset;
|
||||||
|
script->text = malloc (len + 1);
|
||||||
|
Qread (file, script->text, len);
|
||||||
|
script->text[len] = 0;
|
||||||
|
|
||||||
|
Script_Start (&script->script, P_GSTRING (pr, 1), script->text);
|
||||||
|
R_STRING (pr) = script->dstr;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
bi_Script_TokenAvailable (progs_t *pr, void *_res)
|
bi_Script_TokenAvailable (progs_t *pr, void *_res)
|
||||||
{
|
{
|
||||||
|
@ -177,7 +221,7 @@ bi_Script_Error (progs_t *pr, void *_res)
|
||||||
|
|
||||||
if (!script)
|
if (!script)
|
||||||
PR_RunError (pr, "invalid script handle");
|
PR_RunError (pr, "invalid script handle");
|
||||||
R_STRING (pr) = PR_SetString (pr, script->script.error);
|
RETURN_STRING (pr, script->script.error);
|
||||||
script->script.error = 0;
|
script->script.error = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,17 +237,45 @@ bi_Script_NoQuoteLines (progs_t *pr, void *_res)
|
||||||
script->script.no_quote_lines = P_INT (pr, 1);
|
script->script.no_quote_lines = P_INT (pr, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
bi_Script_SetSingle (progs_t *pr, void *_res)
|
||||||
|
{
|
||||||
|
script_resources_t *res = _res;
|
||||||
|
rua_script_t *script = script_get (res, P_INT (pr, 0));
|
||||||
|
if (!script)
|
||||||
|
PR_RunError (pr, "invalid script handle");
|
||||||
|
free ((char *) script->script.single);
|
||||||
|
script->script.single = 0;
|
||||||
|
const char *single = P_GSTRING (pr, 1);
|
||||||
|
if (single) {
|
||||||
|
script->script.single = strdup (single);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
bi_Script_GetLine (progs_t *pr, void *_res)
|
||||||
|
{
|
||||||
|
script_resources_t *res = _res;
|
||||||
|
rua_script_t *script = script_get (res, P_INT (pr, 0));
|
||||||
|
if (!script)
|
||||||
|
PR_RunError (pr, "invalid script handle");
|
||||||
|
R_INT (pr) = script->script.line;
|
||||||
|
}
|
||||||
|
|
||||||
#define bi(x,np,params...) {#x, bi_##x, -1, np, {params}}
|
#define bi(x,np,params...) {#x, bi_##x, -1, np, {params}}
|
||||||
#define p(type) PR_PARAM(type)
|
#define p(type) PR_PARAM(type)
|
||||||
static builtin_t builtins[] = {
|
static builtin_t builtins[] = {
|
||||||
bi(Script_New, 0),
|
bi(Script_New, 0),
|
||||||
bi(Script_Delete, 1, p(ptr)),
|
bi(Script_Delete, 1, p(ptr)),
|
||||||
bi(Script_Start, 3, p(ptr), p(string), p(string)),
|
bi(Script_Start, 3, p(ptr), p(string), p(string)),
|
||||||
|
bi(Script_FromFile, 3, p(ptr), p(string), p(ptr)),
|
||||||
bi(Script_TokenAvailable, 2, p(ptr), p(int)),
|
bi(Script_TokenAvailable, 2, p(ptr), p(int)),
|
||||||
bi(Script_GetToken, 2, p(ptr), p(int)),
|
bi(Script_GetToken, 2, p(ptr), p(int)),
|
||||||
bi(Script_UngetToken, 1, p(ptr)),
|
bi(Script_UngetToken, 1, p(ptr)),
|
||||||
bi(Script_Error, 1, p(ptr)),
|
bi(Script_Error, 1, p(ptr)),
|
||||||
bi(Script_NoQuoteLines, 1, p(ptr)),
|
bi(Script_NoQuoteLines, 1, p(ptr)),
|
||||||
|
bi(Script_SetSingle, 2, p(ptr), p(string)),
|
||||||
|
bi(Script_GetLine, 1, p(ptr)),
|
||||||
{0}
|
{0}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,19 @@
|
||||||
#ifndef __ruamoko_script_h
|
#ifndef __ruamoko_script_h
|
||||||
#define __ruamoko_script_h
|
#define __ruamoko_script_h
|
||||||
|
|
||||||
typedef struct rua_script *script_t;
|
typedef @handle rua_script script_t;
|
||||||
|
|
||||||
@extern script_t Script_New (void);
|
@extern script_t Script_New (void);
|
||||||
@extern void Script_Delete (script_t script);
|
@extern void Script_Delete (script_t script);
|
||||||
// returns the token string
|
// returns the token string
|
||||||
@extern string Script_Start (script_t script, string file, string data);
|
@extern string Script_Start (script_t script, string file, string data);
|
||||||
|
@extern string Script_FromFile (script_t script, string filename, @handle _qfile_t file);
|
||||||
@extern int Script_TokenAvailable (script_t script, int crossline);
|
@extern int Script_TokenAvailable (script_t script, int crossline);
|
||||||
@extern int Script_GetToken (script_t script, int crossline);
|
@extern int Script_GetToken (script_t script, int crossline);
|
||||||
@extern void Script_UngetToken (script_t script);
|
@extern void Script_UngetToken (script_t script);
|
||||||
@extern string Script_Error (script_t script);
|
@extern string Script_Error (script_t script);
|
||||||
@extern int Script_NoQuoteLines (script_t script);
|
@extern int Script_NoQuoteLines (script_t script);
|
||||||
|
@extern void Script_SetSingle (script_t script, string single);
|
||||||
|
@extern int Script_GetLine (script_t script);
|
||||||
|
|
||||||
#endif//__ruamoko_script_h
|
#endif//__ruamoko_script_h
|
||||||
|
|
|
@ -1,11 +1,15 @@
|
||||||
|
#include <qfile.h>
|
||||||
#include <script.h>
|
#include <script.h>
|
||||||
|
|
||||||
script_t Script_New (void) = #0;
|
script_t Script_New (void) = #0;
|
||||||
void Script_Delete (script_t script) = #0;
|
void Script_Delete (script_t script) = #0;
|
||||||
// returns the token string
|
// returns the token string
|
||||||
string Script_Start (script_t script, string file, string data) = #0;
|
string Script_Start (script_t script, string file, string data) = #0;
|
||||||
|
string Script_FromFile (script_t script, string filename, QFile file) = #0;
|
||||||
int Script_TokenAvailable (script_t script, int crossline) = #0;
|
int Script_TokenAvailable (script_t script, int crossline) = #0;
|
||||||
int Script_GetToken (script_t script, int crossline) = #0;
|
int Script_GetToken (script_t script, int crossline) = #0;
|
||||||
void Script_UngetToken (script_t script) = #0;
|
void Script_UngetToken (script_t script) = #0;
|
||||||
string Script_Error (script_t script) = #0;
|
string Script_Error (script_t script) = #0;
|
||||||
int Script_NoQuoteLines (script_t script) = #0;
|
int Script_NoQuoteLines (script_t script) = #0;
|
||||||
|
void Script_SetSingle (script_t script, string single) = #0;
|
||||||
|
int Script_GetLine (script_t script) = #0;
|
||||||
|
|
Loading…
Reference in a new issue