Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibGAP #493

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/c_filt1.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef AVOID_PRECOMPILED
/* C file produced by GAC */
#include "src/compiled.h"
#include "compiled.h"

/* global variables used in handlers */
static GVar G_IS__FUNCTION;
Expand Down
2 changes: 1 addition & 1 deletion src/c_meths1.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef AVOID_PRECOMPILED
/* C file produced by GAC */
#include "src/compiled.h"
#include "compiled.h"

/* global variables used in handlers */
static GVar G_METHOD__0ARGS;
Expand Down
2 changes: 1 addition & 1 deletion src/c_oper1.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef AVOID_PRECOMPILED
/* C file produced by GAC */
#include "src/compiled.h"
#include "compiled.h"

/* global variables used in handlers */
static GVar G_REREADING;
Expand Down
2 changes: 1 addition & 1 deletion src/c_random.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef AVOID_PRECOMPILED
/* C file produced by GAC */
#include "src/compiled.h"
#include "compiled.h"

/* global variables used in handlers */
static GVar G_QUO__INT;
Expand Down
2 changes: 1 addition & 1 deletion src/c_type1.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef AVOID_PRECOMPILED
/* C file produced by GAC */
#include "src/compiled.h"
#include "compiled.h"

/* global variables used in handlers */
static GVar G_NAME__FUNC;
Expand Down
13 changes: 5 additions & 8 deletions src/gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@ typedef struct {
#endif

static StructImportedGVars ImportedGVars[MAX_IMPORTED_GVARS];
static Int NrImportedGVars;
Int NrImportedGVars;

static StructImportedGVars ImportedFuncs[MAX_IMPORTED_GVARS];
static Int NrImportedFuncs;
Int NrImportedFuncs;

char *original_argv0;
static char **sysargv;
static char **sysenviron;
char **sysenviron;

Obj ShellContext = 0;
Obj BaseShellContext = 0;
Expand Down Expand Up @@ -730,11 +730,7 @@ int DoFixGac(char *myself)
}
#endif

#ifdef COMPILECYGWINDLL
#define main realmain
#endif

int main (
int gap_main_loop (
int argc,
char * argv [],
char * environ [] )
Expand Down Expand Up @@ -1344,6 +1340,7 @@ Obj FuncCALL_WITH_CATCH( Obj self, Obj func, Obj args )

Obj FuncJUMP_TO_CATCH( Obj self, Obj payload)
{
libgap_call_error_handler();
TLS(ThrownObject) = payload;
syLongjmp(TLS(ReadJmpError), 1);
return 0;
Expand Down
4 changes: 4 additions & 0 deletions src/gasman.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@

#include "objects.h" /* objects */
#include "scanner.h" /* scanner */
#include "libgap_internal.h" /* gasman callback */

#include "code.h" /* coder */
#include "thread.h" /* threads */
Expand Down Expand Up @@ -2210,6 +2211,9 @@ UInt CollectBags (
/* prepare the list of marked bags for the future */
MarkedBags = 0;

/* call the libgap callback so library users can mark their own bags */
libgap_call_gasman_callback();

/* mark from the static area */
for ( i = 0; i < GlobalBags.nr; i++ )
MARK_BAG( *GlobalBags.addr[i] );
Expand Down
5 changes: 4 additions & 1 deletion src/iostream.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
#include "thread.h" /* threads */
#include "tls.h" /* thread-local storage */

#include "libgap_internal.h" /* GAP shared library */

#include <stdio.h> /* standard input/output functions */
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -886,8 +888,9 @@ static Int InitKernel(
InitHdlrFuncsFromTable( GVarFuncs );

/* Set up the trap to detect future dying children */
#ifdef LIBGAP_SIGNALS
signal( SIGCHLD, ChildStatusChanged );

#endif
return 0;
}

Expand Down
220 changes: 220 additions & 0 deletions src/libgap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/* LibGAP - a shared library version of the GAP kernel
* Copyright (C) 2013 Volker Braun <vbraun.name@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/


#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "system.h"
#include "scanner.h"
#include "gap.h"
#include "libgap.h"
#include "libgap_internal.h"

/* Pointers to input/output buffers. libGAP users must not access these buffers directly!
*/

#define BUFFER_STEP 16*1024

static char* stdin_buffer = NULL;

static char* stdout_buffer = NULL;
static size_t stdout_bufsize = 0;
static size_t stdout_pos = 0;


/* stderr is captured in a static buffer to make it easier to pass it around in the error handler */

#define STDERR_BUFSIZE 4096

static char stderr_buffer[STDERR_BUFSIZE];
static size_t stderr_pos = 0;

int libgap_in_enter_exit_block = 0; /* false */

/*************************************************************************/
/*** Initialize / Finalize ***********************************************/
/*************************************************************************/

/* from gap.c */
extern char **sysenviron;
extern Int NrImportedGVars;
extern Int NrImportedFuncs;



/*************************************************************************/
/*** Global Initialization ***********************************************/
/*************************************************************************/

void libgap_initialize(int argc, char** argv)
{
sysenviron = environ;
NrImportedGVars = 0;
NrImportedFuncs = 0;
UserHasQUIT = 0;
UserHasQuit = 0;
libgap_mark_stack_bottom();
InitializeGap( &argc, argv );
}


void libgap_finalize()
{
FinishBags();
}


/*************************************************************************/
/*** Garbage collector callback ******************************************/
/*************************************************************************/

static libgap_gasman_callback_ptr gasman_callback = NULL;

void libgap_set_gasman_callback(libgap_gasman_callback_ptr callback)
{
gasman_callback = callback;
}

void libgap_call_gasman_callback()
{
if (gasman_callback != NULL)
(*gasman_callback)();
}


/*************************************************************************/
/*** Input/Output interaction ********************************************/
/*************************************************************************/

void libgap_start_interaction(char* inputline)
{
assert(stdin_buffer == NULL);
stdin_buffer = inputline;

stdout_bufsize = BUFFER_STEP;
stdout_buffer = (char*)malloc(stdout_bufsize);
stdout_pos = 0;

stderr_pos = 0;
}

char* libgap_get_output()
{
libgap_append_stdout('\0');
return stdout_buffer;
}

char* libgap_get_error()
{
libgap_append_stderr('\0');
return strdup(stderr_buffer);
}

void libgap_finish_interaction()
{
while (Symbol != S_EOF)
GetSymbol();
stdin_buffer = NULL;

stdout_bufsize = 0;
stdout_pos = 0;
free(stdout_buffer);
stdout_buffer = NULL;

stderr_pos = 0;
ClearError();
}




/*************************************************************************/
/*** Let GAP access the buffers ******************************************/
/*************************************************************************/

static libgap_error_func_ptr error_func = NULL;

void libgap_set_error_handler(libgap_error_func_ptr callback)
{
error_func = callback;
}


void libgap_call_error_handler()
{
if (error_func == NULL) {
printf("An error occurred, but libGAP has no handler set.\n");
printf("Error message: %s\n", libgap_get_error());
return;
}
libgap_append_stderr('\0');
stderr_pos = 0;
ClearError();
(*error_func) (stderr_buffer);
}



/*************************************************************************/
/*** Let GAP access the buffers ******************************************/
/*************************************************************************/

char* libgap_get_input(char* line, int length)
{
// TODO: copy in length chunks
if (stdin_buffer == NULL) {
return NULL;
}
assert(strlen(stdin_buffer) < length);
strcpy(line, stdin_buffer);
stdin_buffer = NULL;
return line;
}

void libgap_append_stdout(char ch)
{
if (stdout_buffer == NULL)
return;
if (stdout_pos == stdout_bufsize) {
char* old_stdout_buffer = stdout_buffer;
size_t old_stdout_bufsize = stdout_bufsize;
stdout_bufsize += BUFFER_STEP;
stdout_buffer = (char*)malloc(stdout_bufsize);
memcpy(stdout_buffer, old_stdout_buffer, old_stdout_bufsize);
free(old_stdout_buffer);
}
stdout_buffer[stdout_pos++] = ch;
}


void libgap_append_stderr(char ch)
{
stderr_buffer[stderr_pos++] = ch;
if (stderr_pos == STDERR_BUFSIZE)
stderr_pos--;
}


void libgap_set_error(char* msg)
{
stderr_pos = 0;
int i;
for (i=0; i<strlen(msg); i++)
libgap_append_stderr(msg[i]);
}
Loading