/* * Changes the size of the buffer to contain at least newsize bytes */ void change_buffer_size(const size_t newsize) { if ( BUFFER_PTR == NULL ) { if ((BUFFER_PTR = malloc( BLOCK_SIZE < newsize ? newsize : BLOCK_SIZE)) == NULL) { YAP_Error(0,0,"Prolog2Term: Out of memory.\n"); #ifdef MPI MPI_Finalize(); #endif YAP_Exit( 1 ); } } else if ((BUFFER_SIZE>=BLOCK_SIZE && BUFFER_SIZE>=newsize) ) { return; } else if ((BUFFER_PTR = realloc( BUFFER_PTR, newsize)) == NULL) { YAP_Error(0,0,"Prolog2Term: Out of memory.\n"); #ifdef MPI MPI_Finalize(); #endif YAP_Exit( 1 ); } BUFFER_SIZE=newsize; }
/* execute a command as a detached process */ static YAP_Bool do_shell(void) { #if defined(__MINGW32__) || _MSC_VER YAP_Error(0, 0L, "system not available in this configuration"); return (FALSE); #elif HAVE_SYSTEM char *buf = YAP_AllocSpaceFromYap(BUF_SIZE); int sys; if (buf == NULL) { YAP_Error(0, 0L, "No Temporary Space for Shell"); return (FALSE); } #if HAVE_STRNCPY strncpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1)), BUF_SIZE); strncpy(buf, " ", BUF_SIZE); strncpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG2)), BUF_SIZE); strncpy(buf, " ", BUF_SIZE); strncpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3)), BUF_SIZE); #else strcpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1))); strcpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG2))); strcpy(buf, " "); strcpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3))); #endif sys = system(buf); YAP_FreeSpaceFromYap(buf); if (sys < 0) { return YAP_Unify(YAP_ARG5, YAP_MkIntTerm(errno)); } return YAP_Unify(YAP_ARG4, YAP_MkIntTerm(sys)); #else char *cptr[4]; int t; int sys; cptr[0] = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1)); cptr[1] = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG2)); cptr[2] = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3)); cptr[3] = NULL; t = fork(); if (t < 0) { return YAP_Unify(YAP_ARG5, YAP_MkIntTerm(errno)); } else if (t == 0) { t = execvp(cptr[0], cptr); return t; } else { t = wait(&sys); if (t < 0) { return YAP_Unify(YAP_ARG5, YAP_MkIntTerm(errno)); } } return YAP_Unify(YAP_ARG4, YAP_MkIntTerm(sys)); #endif }
/* return YAP's environment */ static YAP_Bool p_environ(void) { #if HAVE_ENVIRON && 0 #if HAVE__NSGETENVIRON char **ptr = _NSGetEnviron(); #elif defined(__MINGW32__) || _MSC_VER extern char **_environ; char **ptr = _environ; #else extern char **environ; char **ptr = environ; #endif YAP_Term t1 = YAP_ARG1; long int i; i = YAP_IntOfTerm(t1); if (ptr[i] == NULL) return (FALSE); else { YAP_Term t = YAP_BufferToString(ptr[i]); return (YAP_Unify(t, YAP_ARG2)); } #else YAP_Error(0, 0L, "environ not available in this configuration"); return (FALSE); #endif }
/* * Read a prolog term from a stream * (the prolog term must have been writen by the write_term_to_stream) */ YAP_Term read_term_from_stream(const int fd) { size_t size; RESET_BUFFER(); if (!read(fd,(void*)&size,sizeof(size_t))) { // read the size of the term YAP_Error(0,0,"Prolog2Term: IO error in read.\n"); } #ifdef DEBUG write_msg(__FUNCTION__,__FILE__,__LINE__,"read_term_from_stream>>>>size:%d\n",size); #endif if ( size> BUFFER_SIZE) expand_buffer(size-BUFFER_SIZE); if (!read(fd,BUFFER_PTR,size)) { YAP_Error(0,0,"Prolog2Term: IO error in read.\n"); }; // read term from stream return YAP_ImportTerm( BUFFER_PTR); }
/* * Writes a term to a stream. */ size_t write_term_to_stream(const int fd,const YAP_Term term) { RESET_BUFFER(); printf("BUFFER_PTR=%p\n", BUFFER_PTR); p2c_putt(term); if (write(fd,(void*)BUFFER_PTR,BUFFER_LEN) < 0) { // write term YAP_Error(0,0,"Prolog2Term: IO error in write.\n"); return -1; } return BUFFER_LEN; }
/* * Adds 'space' to the size of the currently allocated buffer */ static void expand_buffer(const size_t space ) { BUFFER_PTR = realloc( BUFFER_PTR, BUFFER_SIZE + space ); if( BUFFER_PTR == NULL ) { YAP_Error(0,0,"Prolog2Term: Out of memory.\n"); #ifdef MPI MPI_Finalize(); #endif YAP_Exit( 1 ); } BUFFER_SIZE+=space; }
static int init_standard_system(int argc, char *argv[], YAP_init_args *iap) { YAP_file_type_t BootMode = YAP_parse_yap_arguments(argc, argv, iap); /* init memory */ iap->initial_file_type = BootMode = YAP_Init(iap); if (iap->ErrorNo) { /* boot failed */ YAP_Error(iap->ErrorNo, 0L, iap->ErrorCause); } return BootMode; }
/** @pred system(+ _S_) Passes command _S_ to the Bourne shell (on UNIX environments) or the current command interpreter in WIN32 environments. Note that it executes them command as a detached process. It requires `system` to be implemented by the system library. */ static YAP_Bool do_system(void) { char *command = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1)); #if HAVE_SYSTEM int sys = system(command); if (sys < 0) { return YAP_Unify(YAP_ARG3, YAP_MkIntTerm(errno)); } return YAP_Unify(YAP_ARG2, YAP_MkIntTerm(sys)); #else YAP_Error(0, 0L, "system not available in this configuration, trying %s", command); return FALSE; #endif }
/* * Changes the size of the buffer to contain at least newsize bytes */ void change_buffer_size(const size_t newsize) { if ( BUFFER_SIZE>=BLOCK_SIZE && BUFFER_SIZE>newsize) return; if(BUFFER_PTR!=NULL) free(BUFFER_PTR); BUFFER_PTR = (char*)malloc(newsize); if( BUFFER_PTR == NULL ) { YAP_Error(0,0,"Prolog2Term: Out of memory.\n"); #ifdef MPI MPI_Finalize(); #endif YAP_Exit( 1 ); } BUFFER_SIZE=newsize; }
/* * Adds 'space' to the size of the currently allocated buffer */ static void expand_buffer(const size_t space ) { char *oldblock; // BUFFER_PTR = realloc( BUFFER_PTR, BUFFER_SIZE + space ); oldblock= BUFFER_PTR; BUFFER_PTR = (char*)malloc( BUFFER_SIZE + space ); if( BUFFER_PTR == NULL ) { YAP_Error(0,0,"Prolog2Term: Out of memory.\n"); #ifdef MPI MPI_Finalize(); #endif YAP_Exit( 1 ); } memcpy(BUFFER_PTR,oldblock,BUFFER_SIZE); if(oldblock!=NULL) free(oldblock); BUFFER_SIZE+=space; }
static int init_standard_system(int argc, char *argv[], YAP_init_args *iap) { int BootMode; BootMode = YAP_parse_yap_arguments(argc,argv,iap); /* init memory */ if (BootMode == YAP_BOOT_FROM_PROLOG || BootMode == YAP_FULL_BOOT_FROM_PROLOG) { int NewBootMode = YAP_Init(iap); if (NewBootMode != YAP_BOOT_FROM_PROLOG && BootMode != YAP_FULL_BOOT_FROM_PROLOG) BootMode = NewBootMode; } else { BootMode = YAP_Init(iap); } if (iap->ErrorNo) { /* boot failed */ YAP_Error(iap->ErrorNo,0L,iap->ErrorCause); } return BootMode; }