Added a primitive interactive mode to carne and modified the file::read

GIB builtin to use Qopen instead of COM_LoadHunkFile.  This makes it work
properly in carne.
This commit is contained in:
Brian Koropoff 2002-11-15 23:27:07 +00:00
parent cb81f17697
commit 78785a90f8
2 changed files with 82 additions and 22 deletions

View file

@ -542,8 +542,9 @@ GIB_File_Transform_Path_Secure (dstring_t *path)
void
GIB_File_Read_f (void)
{
QFile *file;
char *path, *contents;
int mark;
int len;
if (GIB_Argc () != 2) {
Cbuf_Error ("syntax",
@ -562,15 +563,22 @@ GIB_File_Read_f (void)
return;
}
path = GIB_Argv (1);
mark = Hunk_LowMark ();
contents = (char *) COM_LoadHunkFile (path);
file = Qopen (path, "r");
if (file) {
len = Qfilesize (file);
contents = (char *) malloc (len + 1);
SYS_CHECKMEM (contents);
contents[len] = 0;
Qread (file, contents, len);
Qclose (file);
}
if (!contents) {
Cbuf_Error ("file",
"file::read: could not open %s for reading: %s", path, strerror (errno));
return;
}
GIB_Return (contents);
Hunk_FreeToLowMark (mark);
free (contents);
}
void

View file

@ -14,27 +14,36 @@
#include "QF/gib_init.h"
#include "QF/gib_thread.h"
#include "QF/gib_function.h"
#include "QF/gib_builtin.h"
#include "QF/gib_buffer.h"
#include "QF/dstring.h"
#include "QF/va.h"
extern gib_thread_t *gib_threads;
int main (int argc, char **argv)
static qboolean carne_done = false;
static int carne_exitcode = 0;
void
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));
}
int
Carne_Execute_Script (const char *path, cbuf_args_t *args)
{
QFile *file;
cbuf_t *mbuf = Cbuf_New (&gib_interp);
char *f;
int len, i;
cbuf_args_t *args = Cbuf_ArgsNew ();
cbuf_t *mbuf = Cbuf_New (&gib_interp);
// Initialize required QF subsystems
Cvar_Init_Hash ();
Cmd_Init_Hash ();
Cmd_Init ();
GIB_Init (false); // No sandbox
// Load the script
file = Qopen (argv[1], "r");
file = Qopen (path, "r");
if (file) {
len = Qfilesize (file);
f = (char *) malloc (len + 1);
@ -46,7 +55,7 @@ int main (int argc, char **argv)
}
Qclose (file);
} else {
printf ("Could not open %s for reading: %s\n", argv[1], strerror(errno));
printf ("Could not open %s for reading: %s\n", path, strerror(errno));
return 1;
}
@ -57,18 +66,61 @@ int main (int argc, char **argv)
}
GIB_Parse_Strip_Comments (mbuf);
// Prepare arguments
for (i = 1; i < argc; i++)
Cbuf_ArgsAdd (args, argv[i]);
GIB_Function_Prepare_Args (mbuf, args);
Cbuf_ArgsDelete (args);
// Main loop
while (1) {
GIB_Thread_Execute ();
Cbuf_Execute_Stack (mbuf);
// Check if there is anything left to do
if (!gib_threads && !mbuf->down && !mbuf->buf->str[0])
return 0;
if (carne_done || (!gib_threads && !mbuf->down && !mbuf->buf->str[0]))
break;
}
Cbuf_DeleteStack (mbuf);
return carne_exitcode;
}
int
Carne_Execute_Stdin ()
{
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;
}
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
GIB_Builtin_Add ("exit", Carne_GIB_Exit_f, GIB_BUILTIN_NORMAL);
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 ();
}