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"
|
|
|
|
#include "QF/gib_parse.h"
|
|
|
|
#include "QF/gib_init.h"
|
|
|
|
#include "QF/gib_thread.h"
|
|
|
|
#include "QF/gib_function.h"
|
2002-11-15 23:27:07 +00:00
|
|
|
#include "QF/gib_builtin.h"
|
|
|
|
#include "QF/gib_buffer.h"
|
2002-11-14 02:10:55 +00:00
|
|
|
#include "QF/dstring.h"
|
|
|
|
#include "QF/va.h"
|
|
|
|
|
|
|
|
extern gib_thread_t *gib_threads;
|
|
|
|
|
2002-11-15 23:27:07 +00:00
|
|
|
static qboolean carne_done = false;
|
|
|
|
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;
|
2002-11-15 23:27:07 +00:00
|
|
|
cbuf_t *mbuf = Cbuf_New (&gib_interp);
|
2002-11-14 02:10:55 +00:00
|
|
|
char *f;
|
|
|
|
int len, i;
|
|
|
|
|
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);
|
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));
|
2002-11-14 02:10:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
//GIB_Parse_Strip_Comments (mbuf);
|
2002-11-14 02:10:55 +00:00
|
|
|
|
2003-01-30 23:26:43 +00:00
|
|
|
GIB_Function_Prepare_Args (mbuf, args->argv, args->argc);
|
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;
|
|
|
|
}
|
|
|
|
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];
|
|
|
|
cbuf_t *cbuf = Cbuf_New (&gib_interp);
|
|
|
|
|
|
|
|
memset (linebuf, 0, sizeof(linebuf));
|
|
|
|
|
|
|
|
while (fgets(linebuf, sizeof(linebuf)-1, stdin)) {
|
|
|
|
GIB_Thread_Execute ();
|
|
|
|
Cbuf_AddText (cbuf, linebuf);
|
|
|
|
Cbuf_Execute_Stack (cbuf);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
cbuf_args_t *args = Cbuf_ArgsNew ();
|
|
|
|
int result, i;
|
|
|
|
|
|
|
|
// Initialize required QF subsystems
|
|
|
|
Cvar_Init_Hash ();
|
|
|
|
Cmd_Init_Hash ();
|
|
|
|
Cmd_Init ();
|
|
|
|
GIB_Init (false); // No sandbox
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Builtin_Add ("exit", Carne_GIB_Exit_f);
|
2002-11-15 23:27:07 +00:00
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
// Prepare arguments
|
|
|
|
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
|
|
|
}
|