[ruamoko] Make a common sprintf wrapper function

This takes care of converting from progs varargs to what PR_Sprintf
expects. I got tired of modifying the wrappers when I found a third one.
This commit is contained in:
Bill Currie 2022-02-01 09:24:02 +09:00
parent 5d41e90cc7
commit b8c2b7f856
4 changed files with 37 additions and 39 deletions

View file

@ -33,6 +33,9 @@
#include "QF/quakeio.h"
struct progs_s;
struct dstring_s;
void RUA_Cbuf_Init (struct progs_s *pr, int secure);
void RUA_Cmd_Init (struct progs_s *pr, int secure);
void RUA_Cvar_Init (struct progs_s *pr, int secure);
@ -49,6 +52,8 @@ void RUA_String_Init (struct progs_s *pr, int secure);
void RUA_QFile_Init (struct progs_s *pr, int secure);
void RUA_QFS_Init (struct progs_s *pr, int secure);
void RUA_Sprintf (struct progs_s *pr, struct dstring_s *dstr);
int QFile_AllocHandle (struct progs_s *pr, QFile *file);
QFile *QFile_GetFile (struct progs_s *pr, int handle);
struct plitem_s *Plist_GetItem (struct progs_s *pr, int handle);

View file

@ -60,16 +60,36 @@ bi_strlen (progs_t *pr)
R_INT (pr) = strlen(s);
}
static void
bi_sprintf (progs_t *pr)
void
RUA_Sprintf (progs_t *pr, dstring_t *dstr)
{
const char *fmt = P_GSTRING (pr, 0);
int count = pr->pr_argc - 1;
pr_type_t **args = pr->pr_params + 1;
if (pr->progs->version == PROG_VERSION) {
__auto_type va_list = &P_PACKED (pr, pr_va_list_t, 1);
count = va_list->count;
if (count) {
args = alloca (count * sizeof (pr_type_t *));
for (int i = 0; i < count; i++) {
args[i] = &pr->pr_globals[va_list->list + i * 4];
}
} else {
args = 0;
}
}
PR_Sprintf (pr, dstr, "bi_sprintf", fmt, count, args);
}
static void
bi_sprintf (progs_t *pr)
{
dstring_t *dstr;
dstr = dstring_newstr ();
PR_Sprintf (pr, dstr, "bi_sprintf", fmt, count, args);
RUA_Sprintf (pr, dstr);
RETURN_STRING (pr, dstr->str);
dstring_delete (dstr);
}

View file

@ -53,6 +53,7 @@
#include "QF/zone.h"
#include "compat.h"
#include "rua_internal.h"
#include "ruamoko/qwaq/qwaq.h"
#include "ruamoko/qwaq/debugger/debug.h"
@ -147,25 +148,9 @@ init_qf (void)
static void
bi_printf (progs_t *pr)
{
const char *fmt = P_GSTRING (pr, 0);
int count = pr->pr_argc - 1;
pr_type_t **args = pr->pr_params + 1;
dstring_t *dstr = dstring_new ();
if (pr->progs->version == PROG_VERSION) {
__auto_type va_list = &P_PACKED (pr, pr_va_list_t, 1);
count = va_list->count;
if (count) {
args = alloca (count * sizeof (pr_type_t *));
for (int i = 0; i < count; i++) {
args[i] = &pr->pr_globals[va_list->list + i * 4];
}
} else {
args = 0;
}
}
PR_Sprintf (pr, dstr, "bi_printf", fmt, count, args);
RUA_Sprintf (pr, dstr);
if (dstr->str) {
Sys_Printf ("%s", dstr->str);
}

View file

@ -40,37 +40,25 @@
#include "QF/dstring.h"
#include "QF/progs.h"
#include "rua_internal.h"
#include "test-bi.h"
static void
bi_printf (progs_t *pr)
{
const char *fmt = P_GSTRING (pr, 0);
int count = pr->pr_argc - 1;
pr_type_t **args = pr->pr_params + 1;
static dstring_t *dstr;
if (!dstr)
if (!dstr) {
dstr = dstring_new ();
else
} else {
dstring_clear (dstr);
if (pr->progs->version == PROG_VERSION) {
__auto_type va_list = &P_PACKED (pr, pr_va_list_t, 1);
count = va_list->count;
if (count) {
args = alloca (count * sizeof (pr_type_t *));
for (int i = 0; i < count; i++) {
args[i] = &pr->pr_globals[va_list->list + i * 4];
}
} else {
args = 0;
}
}
PR_Sprintf (pr, dstr, "bi_printf", fmt, count, args);
if (dstr->str)
RUA_Sprintf (pr, dstr);
if (dstr->str) {
fputs (dstr->str, stdout);
}
}
static void