--- gforth/engine/support.c 2008/10/09 16:30:56 1.33 +++ gforth/engine/support.c 2012/12/31 15:25:19 1.51 @@ -1,6 +1,6 @@ /* Gforth support functions - Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2006,2007 Free Software Foundation, Inc. + Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2006,2007,2008,2009,2010,2011,2012 Free Software Foundation, Inc. This file is part of Gforth. @@ -37,41 +37,22 @@ #include #include #endif +#if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN) /* what else? */ +#include +#endif #ifdef HAS_FILE -char *cstr(Char *from, UCell size, int clear) +char *cstr(Char *from, UCell size) /* return a C-string corresponding to the Forth string ( FROM SIZE ). - the C-string lives until the next call of cstr with CLEAR being true */ + the C-string lives until free */ { - static struct cstr_buffer { - char *buffer; - size_t size; - } *buffers=NULL; - static int nbuffers=0; - static int used=0; - struct cstr_buffer *b; - - if (buffers==NULL) - buffers=malloc(0); - if (clear) - used=0; - if (used>=nbuffers) { - buffers=realloc(buffers,sizeof(struct cstr_buffer)*(used+1)); - buffers[used]=(struct cstr_buffer){malloc(0),0}; - nbuffers=used+1; - } - b=&buffers[used]; - if (size+1 > b->size) { - b->buffer = realloc(b->buffer,size+1); - b->size = size+1; - } - memcpy(b->buffer,from,size); - b->buffer[size]='\0'; - used++; - return b->buffer; + char * string = malloc(size+1); + memcpy(string,from,size); + string[size]='\0'; + return string; } -char *tilde_cstr(Char *from, UCell size, int clear) +char *tilde_cstr(Char *from, UCell size) /* like cstr(), but perform tilde expansion on the string */ { char *s1,*s2; @@ -79,7 +60,7 @@ char *tilde_cstr(Char *from, UCell size, struct passwd *getpwnam (), *user_entry; if (size<1 || from[0]!='~') - return cstr(from, size, clear); + return cstr(from, size); if (size<2 || from[1]=='/') { s1 = (char *)getenv ("HOME"); if(s1 == NULL) @@ -97,7 +78,7 @@ char *tilde_cstr(Char *from, UCell size, for (i=1; ipw_dir; s2 = (char *)from+i; s2_len = size-i; @@ -117,7 +98,7 @@ char *tilde_cstr(Char *from, UCell size, char path[s1_len+s2_len]; memcpy(path,s1,s1_len); memcpy(path+s1_len,s2,s2_len); - return cstr((Char *)path,s1_len+s2_len,clear); + return cstr((Char *)path,s1_len+s2_len); } } @@ -150,6 +131,19 @@ DCell timeval2us(struct timeval *tvp) #endif } +DCell timespec2ns(struct timespec *tvp) +{ +#ifndef BUGGY_LONG_LONG + return (tvp->tv_sec*(DCell)1000000000LL)+tvp->tv_nsec; +#else + DCell d2; + DCell d1=mmul(tvp->tv_sec,1000000000); + d2.lo = d1.lo+tvp->tv_nsec; + d2.hi = d1.hi + (d2.lo0) + gf_regetc(wfileid); for(u2=0; u2[] := []{[.] | . } @@ -415,9 +415,9 @@ Cell to_float(Char *c_addr, UCell u, Flo aftersign: if (s >= send) goto exponent; - switch (c=*s) { + if((c=*s)==dot) { *t++ = '.'; ndots++; s++; goto aftersign; } + switch (c) { case '0' ... '9': *t++ = c; ndigits++; s++; goto aftersign; - case '.': *t++ = c; ndots++; s++; goto aftersign; default: goto exponent; } exponent: @@ -503,8 +503,10 @@ int gforth_system(Char *c_addr, UCell u) char *prefix = getenv("GFORTHSYSTEMPREFIX") ? : DEFAULTSYSTEMPREFIX; size_t prefixlen = strlen(prefix); char buffer[prefixlen+u+1]; + int MAYBE_UNUSED old_tp; + fflush(stdout); #ifndef MSDOS - int old_tp=terminal_prepped; + old_tp=terminal_prepped; deprep_terminal(); #endif memcpy(buffer,prefix,prefixlen); @@ -535,22 +537,26 @@ void gforth_ms(UCell u) UCell gforth_dlopen(Char *c_addr, UCell u) { - char * file=tilde_cstr(c_addr, u, 1); -#ifdef HAVE_LIBLTDL - return (UCell)lt_dlopen(file); + char * file=tilde_cstr(c_addr, u); + UCell lib; +#if defined(HAVE_LIBLTDL) + lib = (UCell)lt_dlopen(file); + free(file); + if(lib) return lib; #elif defined(HAVE_LIBDL) || defined(HAVE_DLOPEN) #ifndef RTLD_GLOBAL #define RTLD_GLOBAL 0 #endif - return (UCell)dlopen(file, RTLD_GLOBAL | RTLD_LAZY); -#else -# ifdef _WIN32 - return (Cell) GetModuleHandle(file); -# else -#warning Define open-lib! - return 0; -# endif + lib = (UCell)dlopen(file, RTLD_GLOBAL); + free(file); + if(lib) return lib; + fprintf(stderr, "%s\n", dlerror()); +#elif defined(_WIN32) + lib = (UCell) GetModuleHandle(file); + free(file); + if(lib) return lib; #endif + return 0; } #endif /* !defined(STANDALONE) */ @@ -584,7 +590,23 @@ typedef UDCell UDWtype; #include "longlong.h" + #if defined(udiv_qrnnd) && !defined(__alpha) && UDIV_NEEDS_NORMALIZATION + +#if defined(count_leading_zeros) +const UQItype __clz_tab[256] = +{ + 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, + 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 +}; +#endif + static Cell MAYBE_UNUSED nlz(UCell x) /* number of leading zeros, adapted from "Hacker's Delight" */ {