2021-03-27 11:09:37 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2002-11-14 02:10:55 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "QF/cbuf.h"
|
|
|
|
#include "QF/cmd.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/zone.h"
|
|
|
|
#include "QF/quakefs.h"
|
|
|
|
#include "QF/quakeio.h"
|
2003-04-13 22:07:58 +00:00
|
|
|
#include "QF/gib.h"
|
2002-11-14 02:10:55 +00:00
|
|
|
#include "QF/dstring.h"
|
|
|
|
#include "QF/va.h"
|
|
|
|
|
2003-04-13 22:07:58 +00:00
|
|
|
#include "gib_thread.h"
|
|
|
|
#include "gib_parse.h"
|
|
|
|
|
2023-06-13 09:06:11 +00:00
|
|
|
static bool carne_done = false;
|
2002-11-15 23:27:07 +00:00
|
|
|
static int carne_exitcode = 0;
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-11-15 23:27:07 +00:00
|
|
|
Carne_GIB_Exit_f (void)
|
|
|
|
{
|
|
|
|
carne_done = true;
|
|
|
|
// Put it in wait mode so that Cbuf_Execute_Stack clears out
|
|
|
|
// we can then safely nuke the stack later
|
|
|
|
cbuf_active->state = CBUF_STATE_WAIT;
|
|
|
|
if (GIB_Argc() == 2)
|
|
|
|
carne_exitcode = atoi (GIB_Argv(1));
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static int
|
2002-11-15 23:27:07 +00:00
|
|
|
Carne_Execute_Script (const char *path, cbuf_args_t *args)
|
2002-11-14 02:10:55 +00:00
|
|
|
{
|
|
|
|
QFile *file;
|
2003-04-13 22:07:58 +00:00
|
|
|
cbuf_t *mbuf = Cbuf_New (GIB_Interpreter ());
|
2002-11-14 02:10:55 +00:00
|
|
|
char *f;
|
|
|
|
int len, i;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2002-11-15 23:27:07 +00:00
|
|
|
file = Qopen (path, "r");
|
2002-11-14 02:10:55 +00:00
|
|
|
if (file) {
|
|
|
|
len = Qfilesize (file);
|
|
|
|
f = (char *) malloc (len + 1);
|
|
|
|
if (f) {
|
|
|
|
f[len] = 0;
|
|
|
|
Qread (file, f, len);
|
2003-01-28 21:16:21 +00:00
|
|
|
// If there is a hash-bang, strip it out
|
|
|
|
i = 0;
|
|
|
|
if (f[0] == '#')
|
|
|
|
for (; f[i] != '\n' && f[i+1]; i++);
|
|
|
|
Cbuf_AddText (mbuf, f+i);
|
2003-02-14 08:06:01 +00:00
|
|
|
GIB_DATA(mbuf)->script = malloc (sizeof (gib_script_t));
|
|
|
|
GIB_DATA(mbuf)->script->file = strdup (path);
|
|
|
|
GIB_DATA(mbuf)->script->text = strdup (f);
|
|
|
|
GIB_DATA(mbuf)->script->refs = 1;
|
2002-11-14 02:10:55 +00:00
|
|
|
free (f);
|
|
|
|
}
|
|
|
|
Qclose (file);
|
|
|
|
} else {
|
2002-11-15 23:27:07 +00:00
|
|
|
printf ("Could not open %s for reading: %s\n", path, strerror(errno));
|
2003-02-26 07:44:34 +00:00
|
|
|
carne_exitcode = 1;
|
|
|
|
goto ERROR;
|
2002-11-14 02:10:55 +00:00
|
|
|
}
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-02-26 07:44:34 +00:00
|
|
|
if (gib_parse_error) {
|
|
|
|
carne_exitcode = 2;
|
|
|
|
goto ERROR;
|
2012-05-21 23:23:22 +00:00
|
|
|
}
|
|
|
|
|
2003-10-19 00:51:47 +00:00
|
|
|
//GIB_Function_Prepare_Args_D (mbuf, args->argv, args->argc);
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2002-11-14 02:10:55 +00:00
|
|
|
// Main loop
|
|
|
|
while (1) {
|
|
|
|
GIB_Thread_Execute ();
|
|
|
|
Cbuf_Execute_Stack (mbuf);
|
|
|
|
// Check if there is anything left to do
|
2003-01-28 21:16:21 +00:00
|
|
|
if (carne_done || !GIB_DATA(mbuf)->program)
|
2002-11-15 23:27:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-02-26 07:44:34 +00:00
|
|
|
ERROR:
|
2002-11-15 23:27:07 +00:00
|
|
|
Cbuf_DeleteStack (mbuf);
|
|
|
|
return carne_exitcode;
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static int
|
|
|
|
Carne_Execute_Stdin (void)
|
2002-11-15 23:27:07 +00:00
|
|
|
{
|
|
|
|
char linebuf[1024];
|
2003-04-13 22:07:58 +00:00
|
|
|
cbuf_t *cbuf = Cbuf_New (GIB_Interpreter ());
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2002-11-15 23:27:07 +00:00
|
|
|
memset (linebuf, 0, sizeof(linebuf));
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2002-11-15 23:27:07 +00:00
|
|
|
while (fgets(linebuf, sizeof(linebuf)-1, stdin)) {
|
|
|
|
GIB_Thread_Execute ();
|
|
|
|
Cbuf_AddText (cbuf, linebuf);
|
2003-02-14 08:06:01 +00:00
|
|
|
if (!gib_parse_error)
|
|
|
|
Cbuf_Execute_Stack (cbuf);
|
2002-11-15 23:27:07 +00:00
|
|
|
if (carne_done)
|
|
|
|
break;
|
2002-11-14 02:10:55 +00:00
|
|
|
}
|
2002-11-15 23:27:07 +00:00
|
|
|
Cbuf_DeleteStack (cbuf);
|
|
|
|
return carne_exitcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2012-05-21 23:23:22 +00:00
|
|
|
cbuf_args_t *args;
|
2002-11-15 23:27:07 +00:00
|
|
|
int result, i;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2002-11-15 23:27:07 +00:00
|
|
|
// Initialize required QF subsystems
|
2011-09-11 05:56:47 +00:00
|
|
|
Sys_Init ();
|
2002-11-15 23:27:07 +00:00
|
|
|
GIB_Init (false); // No sandbox
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Builtin_Add ("exit", Carne_GIB_Exit_f);
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2002-11-15 23:27:07 +00:00
|
|
|
if (argc > 1) {
|
|
|
|
// Prepare arguments
|
2003-02-26 07:44:34 +00:00
|
|
|
args = Cbuf_ArgsNew ();
|
2002-11-15 23:27:07 +00:00
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
Cbuf_ArgsAdd (args, argv[i]);
|
|
|
|
// Run the script
|
|
|
|
result = Carne_Execute_Script (argv[1], args);
|
|
|
|
Cbuf_ArgsDelete (args);
|
|
|
|
return result;
|
|
|
|
} else
|
|
|
|
return Carne_Execute_Stdin ();
|
2002-11-14 02:10:55 +00:00
|
|
|
}
|