From 883b36abe8cbe661ce417a69621d4740e02e50c0 Mon Sep 17 00:00:00 2001 From: "Patrick H. E. Foubet" Date: Sun, 31 Aug 2014 16:42:08 +0200 Subject: [PATCH] Version 0.51 --- NEWS | 10 ++ configure | 20 ++-- configure.ac | 2 +- src/conf.h | 2 +- src/debug.c | 6 +- src/err.c | 3 +- src/gplot.c | 6 +- src/histo.c | 11 +- src/lib.c | 23 +++++ src/lib.h | 3 + src/nife.c | 106 +++++++++++++++++--- src/nife.h | 4 + src/stackC.c | 40 ++++++++ src/stackC.h | 5 + src/stackF.c | 257 ++++++++++++++++++++++++++++++++++++++++++++--- src/stackF.h | 2 + src/stackL.c | 21 ++++ src/stackL.h | 4 + src/stackN.c | 109 +++++++++++++++++++- src/stackN.h | 40 ++++---- src/stackV.c | 278 ++++++++++++++++++++++++++++++++++++++++----------- src/stackV.h | 5 + 22 files changed, 828 insertions(+), 129 deletions(-) diff --git a/NEWS b/NEWS index 3be3622..704b20e 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,15 @@ History of Nife releases : +2014-08-31 - Nife 0.51 is released. + * Finalising the version 1.0 of the functions dump and restore with the + definition of the NBLF, Nife Binary Linkable Format. + * Modify some labels to clarify the perception of internal mechanisms. + * Some modifications to avoid unnecessary warnings at compilation. + * Temporary suspension of some functions related to the dynamic aspect + of the language because, used improperly, it could be dangerous. + These functions will be include again, but with more explanations and + examples. + 2014-06-27 - Nife 0.49 is released. * Add DEC, HEX, OCT and BIN functions to define the print mode for the integers. The default is decimal. diff --git a/configure b/configure index f3b08c3..564e196 100755 --- a/configure +++ b/configure @@ -1,6 +1,6 @@ #! /bin/sh # Guess values for system-dependent variables and create Makefiles. -# Generated by GNU Autoconf 2.69 for nife 0.49. +# Generated by GNU Autoconf 2.69 for nife 0.51. # # Report bugs to . # @@ -580,8 +580,8 @@ MAKEFLAGS= # Identity of this package. PACKAGE_NAME='nife' PACKAGE_TARNAME='nife' -PACKAGE_VERSION='0.49' -PACKAGE_STRING='nife 0.49' +PACKAGE_VERSION='0.51' +PACKAGE_STRING='nife 0.51' PACKAGE_BUGREPORT='nife@seriane.fr' PACKAGE_URL='' @@ -1262,7 +1262,7 @@ if test "$ac_init_help" = "long"; then # Omit some internal or obsolete options to make the list less imposing. # This message is too long to be a string in the A/UX 3.1 sh. cat <<_ACEOF -\`configure' configures nife 0.49 to adapt to many kinds of systems. +\`configure' configures nife 0.51 to adapt to many kinds of systems. Usage: $0 [OPTION]... [VAR=VALUE]... @@ -1328,7 +1328,7 @@ fi if test -n "$ac_init_help"; then case $ac_init_help in - short | recursive ) echo "Configuration of nife 0.49:";; + short | recursive ) echo "Configuration of nife 0.51:";; esac cat <<\_ACEOF @@ -1415,7 +1415,7 @@ fi test -n "$ac_init_help" && exit $ac_status if $ac_init_version; then cat <<\_ACEOF -nife configure 0.49 +nife configure 0.51 generated by GNU Autoconf 2.69 Copyright (C) 2012 Free Software Foundation, Inc. @@ -1838,7 +1838,7 @@ cat >config.log <<_ACEOF This file contains any messages produced by compilers while running configure, to aid debugging if configure makes a mistake. -It was created by nife $as_me 0.49, which was +It was created by nife $as_me 0.51, which was generated by GNU Autoconf 2.69. Invocation command line was $ $0 $@ @@ -2653,7 +2653,7 @@ fi # Define the identity of the package. PACKAGE='nife' - VERSION='0.49' + VERSION='0.51' cat >>confdefs.h <<_ACEOF @@ -5480,7 +5480,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 # report actual input values of CONFIG_FILES etc. instead of their # values after options handling. ac_log=" -This file was extended by nife $as_me 0.49, which was +This file was extended by nife $as_me 0.51, which was generated by GNU Autoconf 2.69. Invocation command line was CONFIG_FILES = $CONFIG_FILES @@ -5546,7 +5546,7 @@ _ACEOF cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`" ac_cs_version="\\ -nife config.status 0.49 +nife config.status 0.51 configured by $0, generated by GNU Autoconf 2.69, with options \\"\$ac_cs_config\\" diff --git a/configure.ac b/configure.ac index c6e73f6..91c2b46 100644 --- a/configure.ac +++ b/configure.ac @@ -2,7 +2,7 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ([2.67]) -AC_INIT([nife], [0.49], [nife@seriane.fr]) +AC_INIT([nife], [0.51], [nife@seriane.fr]) AM_INIT_AUTOMAKE AC_CONFIG_SRCDIR([src/tasks.c]) diff --git a/src/conf.h b/src/conf.h index 231f480..a80143a 100644 --- a/src/conf.h +++ b/src/conf.h @@ -20,7 +20,7 @@ along with this program. If not, see #ifdef HAVE_CONFIG_H #include "../config.h" #else -#define VERSION "0.47" +#define VERSION "0.51" #endif #ifdef HAVE_COMEDILIB_H diff --git a/src/debug.c b/src/debug.c index 3073de6..0d35902 100644 --- a/src/debug.c +++ b/src/debug.c @@ -32,9 +32,9 @@ int Debug=1; /* OK by default */ void D_Reset(void) { -int fd; +int fd, nc; char NF[24]; - chdir(".nife"); + nc=chdir(".nife"); if (Debug) { sprintf(NF,".nife_%d.log",getpid()); if ((fd=open(NF,O_CREAT|O_RDWR|O_TRUNC,0644)) < 0) perror(NF); @@ -43,7 +43,7 @@ char NF[24]; close(fd); } } else dup2(1,2); - chdir(".."); + nc=chdir(".."); } void D_Update(void) diff --git a/src/err.c b/src/err.c index c546efc..cea7d6d 100644 --- a/src/err.c +++ b/src/err.c @@ -134,6 +134,7 @@ void stopErr(char *M, char *F) static void traiteErr(int n, char * L) { +int v; ERROR=n; if (D_Cod==0) { if (ECHOOFF) printf("\n"); @@ -155,7 +156,7 @@ static void traiteErr(int n, char * L) if (iTERM) { printf("In loading stdin : line %d !\n", getFDlig()); close(FD_IN); /* pipe ou autre */ - dup(iTERM); /* stdin on term */ + v = dup(iTERM); /* stdin on term */ iTERM = 0; } } diff --git a/src/gplot.c b/src/gplot.c index c24994e..fb1461d 100644 --- a/src/gplot.c +++ b/src/gplot.c @@ -52,13 +52,13 @@ static void *stackGP = VIDE; /* file manipulations */ static void GPF_init(char *f) { -int fd, r; +int fd, r, nc; r = chdir(".nife"); if ((fd = creat(f,0600)) != -1) { - write(fd,"# Nife auto-generated GNUplot file !\n",37); + nc=write(fd,"# Nife auto-generated GNUplot file !\n",37); close(fd); } - if (r==0) chdir(".."); + if (r==0) r=chdir(".."); } static void GPF_del(char *f) diff --git a/src/histo.c b/src/histo.c index ce31357..63a53fb 100644 --- a/src/histo.c +++ b/src/histo.c @@ -157,6 +157,7 @@ int iTERM=0; void termInit(void) { +int nc; for (iTERM=0; iTERM<3; iTERM++) { if (tcgetattr(iTERM, &t0) != -1) break; if (iTERM<2) continue; @@ -174,7 +175,7 @@ void termInit(void) t1.c_cflag |= CS8; if (tcsetattr(iTERM, TCSAFLUSH, &t1) == -1) perror("tcsetattr"); /* raw */ /* clear screen */ - system("clear"); + nc=system("clear"); } void termReset(void) @@ -204,7 +205,7 @@ int lireLigne(int fd, char *b, char *s, int nc) nc = nb de caracteres possible (longueur du buffer */ { char *d, *f, c, c2, c3, *h, *w, *Wl, *rac; -int n, i, l, ls=0, ins=0, ignTild=0, nbT=0; +int n, i, l, ls=0, ins=0, ignTild=0, nbT=0, Nc; unsigned int j; /* printf("lireLigne ... \n"); */ d = b; @@ -330,8 +331,8 @@ unsigned int j; /* gestion des caracteres speciaux */ case '\033': /* ESCAPE */ ignTild=1; - read(fd,&c2,1); - read(fd,&c3,1); + Nc=read(fd,&c2,1); + Nc=read(fd,&c3,1); if (c2 == '[') { switch(c3) { case '2' : /* Insert */ @@ -441,7 +442,7 @@ finBoucle: } if ((n<1) && iTERM) { close(FD_IN); /* pipe ou autre */ - dup(iTERM); /* stdin on term */ + Nc=dup(iTERM); /* stdin on term */ iTERM = 0; if (ECHOOFF) printf("\n"); return 0; diff --git a/src/lib.c b/src/lib.c index 158a4e3..bb4cad3 100644 --- a/src/lib.c +++ b/src/lib.c @@ -83,6 +83,12 @@ char *LMax; pLibs += strlen(l); *pLibs++ = '\0'; } + +void rmAllFonU(void) +{ + while (Fonctions[NBFonc-1].fam == F_USR) NBFonc--; +} + static void addFonc(char *l, void (*f)(void)) { addFoncT(l,f,0,F_CORE); @@ -351,9 +357,11 @@ void initLib(void) addFonP("install",IF_INSTALL); addFonP("install_f",IF_INSTALLF); addFonP("install_v",IF_INSTALLV); +/* pas pour le moment !! addFonP("df_init",IF_DF_INIT); addFonP("df_start",IF_DF_START); addFonP("df_stop",IF_DF_STOP); +****************/ addFonc("?ls",IF_show_stackL); addFonc("?s",IF_show_stack); /* addFonc("?libX",show_lib_addr); for debugging */ @@ -544,6 +552,10 @@ int i; } return VIDE; } +void * libByInd(long i) +{ + return((void*)Fonctions[i].fct); +} int execLibNrpc(char *C) { @@ -666,3 +678,14 @@ int i; return NULL; } +long iLibByAddr(void *A) +{ +PFC f; +int i; + f = (PFC)A; + for (i=0;i",C); + if ((write (fd, (void*)b, LMARK)) != LMARK) + stopErr("dump","marque"); +} + +void restore_marque(int fd, char C) +{ +char b[LMARK+1]; + /* printf("Restore %c ! \n", C); */ + if ((read(fd, (void*)b, LMARK)) == LMARK) + if (b[1] == C) return; + stopErr("restore","marque"); +} + +void rest_links_pr(int i, char *O, char *C) +{ + if (i) { + printf("Linking %d %s", i, O); + if (i > 1) printf("s"); + printf(" to %s stack.\n",C); + } +} + +void dump_rest_pr(int T, int N, char * L) /* T=0 dump, T=1 restore */ +{ + if (T==0) printf("Dump "); + printf("%d elt",N); + if (N>1) printf("s"); + printf(" for %s stack",L); + if (T) printf(" restored"); + printf(".\n"); +} static void restoreFic(char *L) { int fd; struct DumpEnt E; +char * F; + dropTrSuite(); - if ((fd = open(L,O_RDONLY)) == -1) { - perror(L); + F = DumpRest_ext(L); + if ((fd = open(F,O_RDONLY)) == -1) { + perror(F); messErr(43); } else { - if (read(fd,(void*)&E, sizeof(E)) != sizeof(E)) { + if (read(fd,(void*)&E, LENT) != LENT) { printf("File too small !\n"); messErr(59); } else { - if (strncmp(E.L,"Nblf", 4) == 0) { + if (strncmp(E.L,"Nblf010", 7) == 0) { + if (E.Scs == (long)getScs()) { if (E.V == atof(VERSION)) { + restore_marque(fd, 'N'); + restore_stackN(fd); + restore_marque(fd, 'C'); + restore_stackC(fd); + restore_marque(fd, 'L'); + restore_stackL(fd); + restore_marque(fd, 'V'); restore_stackV(fd); + restore_links_stackN(); + restore_marque(fd, 'F'); restore_stackF(fd); + restore_marque(fd, 'X'); + restore_links_stackV(); } else printf("This file is just available for Nife v %g !\n",E.V); + } else printf("This file have another SCS !\n"); } else printf("Not a NBLF File !\n"); close(fd); } - } + free((void*)F); } static void dumpFic(char *L) { int fd; struct DumpEnt E; +char * F; + dropTrSuite(); - if ((fd = open(L,O_CREAT|O_WRONLY,0600)) == -1) { - perror(L); + F = DumpRest_ext(L); + if ((fd = open(F,O_CREAT|O_WRONLY,0600)) == -1) { + perror(F); messErr(58); } else { - strncpy(E.L,"Nblf", 4); + strncpy(E.L,"Nblf010", 7); E.V=atof(VERSION); E.Scs=(long)getScs(); - write(fd,(void*)&E, sizeof(E)); - dump_stackV(fd); - dump_stackF(fd); - close(fd); + if ((write(fd,(void*)&E, LENT)) == LENT) { + dump_marque(fd, 'N'); + dump_stackN(fd); + dump_marque(fd, 'C'); + dump_stackC(fd); + dump_marque(fd, 'L'); + dump_stackL(fd); + dump_marque(fd, 'V'); + dump_stackV(fd); + dump_marque(fd, 'F'); + dump_stackF(fd); + dump_marque(fd, 'X'); + close(fd); + } else messErr(58); } + free((void*)F); } static void lectFic(char *L) diff --git a/src/nife.h b/src/nife.h index 38cd632..e55b974 100644 --- a/src/nife.h +++ b/src/nife.h @@ -42,5 +42,9 @@ extern void putTrSuite(PFC); extern void dropTrSuite(void); extern void compileFile(char *); extern PFC getTrSuite(void); +extern void dump_marque(int fd, char C); +extern void restore_marque(int fd, char C); +extern void dump_rest_pr(int T, int N, char * L); +extern void rest_links_pr(int i, char *O, char *C); #endif diff --git a/src/stackC.c b/src/stackC.c index a085ab3..2c82fb0 100644 --- a/src/stackC.c +++ b/src/stackC.c @@ -241,3 +241,43 @@ void IF_debString(void) _MODIF_stringEnCours_(1); } +void dump_eltC(int fd, char *A) +{ +uint32_t l; + l=strlen(A); + write(fd, (void*)&l, sizeof(l)); + write(fd, A, l+1); +} + +void dump_stackC(int fd) +{ +uint32_t n, i, l; + n = i_StackC; + write(fd, (void*)&n, sizeof(n)); + for (i=0; il, (long)F); */ if (F->c == VIDE) return; @@ -227,7 +228,7 @@ char Ctyp; printf(" %-25s%c %d octets\n",N->l,Ctyp,*((int*)N->c)); Next = N->n; } - printf("\n"); + printf("\n"); } static char cod[MAXCODE]; @@ -921,6 +922,36 @@ struct Fct * N; return VIDE; } +void * codFctByInd(long i) +{ +void * Next; +struct Fct * N; +long j=0; + Next = stackF; + while (Next != VIDE) { + N = (struct Fct*) Next; + j++; + if (i==j) return (N->c); + Next = N->n; + } + return VIDE; +} + +void * fctByInd(long i) +{ +void * Next; +struct Fct * N; +long j=0; + Next = stackF; + while (Next != VIDE) { + N = (struct Fct*) Next; + j++; + if (i==j) return Next; + Next = N->n; + } + return VIDE; +} + void * fctByCode(void * C) { void * Next; @@ -1003,6 +1034,36 @@ struct Fct * N; return NULL; } +long iFctByCode(void * A) +{ +void * Next; +struct Fct * N; +long i=0; + Next = stackF; + while (Next != VIDE) { + i++; + N = (struct Fct*) Next; + if (N->c==A) return i; + Next = N->n; + } + return 0L; +} + +long iFctByAddr(void * A) +{ +void * Next; +struct Fct * N; +long i=0; + Next = stackF; + while (Next != VIDE) { + i++; + N = (struct Fct*) Next; + if (Next==A) return i; + Next = N->n; + } + return 0L; +} + char * codByAddr(void * A) { void * Next; @@ -1330,13 +1391,74 @@ void IF_debBackC1(void) putTrSuite(suiteBackC1); } +void dump_code(int fd, char * C) +{ +void * A; +uint32_t i; + write(fd, C, 1); + bcopy((void*)(C+1),(void*)&A,sizeof(A)); + switch((Code)*C) { + case T_ONER : + case T_RET : + case T_END : + case T_JEND : + case T_NOP : + case T_DO_I : + case T_DO_J : + case T_DO_K : + case T_LOOP : + case T_PLOO : + case T_EXEK : + break; /* nothing */ + case T_NUM : + dump_eltN(fd, A, 0); + break; + case T_CHA : + case T_BKC : + case T_BKC1 : + dump_eltC(fd, (char*)A); + break; + case T_LIB : + i = iLibByAddr(A); + write(fd, (void*)&i, sizeof(i)); + break; + case T_FCT : + i = iFctByCode(A); + write(fd, (void*)&i, sizeof(i)); + break; + case T_VAR : + i = iVarByAddr(A); + write(fd, (void*)&i, sizeof(i)); + break; + case T_IF : + case T_IFN : + case T_DO : + case T_IFD : + case T_JMP : + case T_GOTO : + bcopy((void*)(C+1),(void*)&i,sizeof(i)); + write(fd, (void*)&i, sizeof(i)); + break; +/* en principe pas possible !!! + case T_FCTP : + i = iFctByAddr(A); + write(fd, (void*)&i, sizeof(i)); + break; +*********************/ + default : + printf("dump_code : Code %d inconnu !\n",(int)(*C)); + messErr(58); + } +} + void dump_stackF(int fd) { -long n=0, i, j, v, vi; +uint32_t n=0, vv; +long v, vi, i, j, pas, t; int *av; void *Next, *A; struct Fct * N; -char * C, *F, *D; +char * C, *F; Next = stackF; while (Next != VIDE) { N = (struct Fct*) Next; @@ -1344,6 +1466,7 @@ char * C, *F, *D; Next = N->n; } write(fd, (void*)&n, sizeof(n)); + t = sizeof(A) + 1; for (i=n; i>0; i--) { Next = stackF; j=0; @@ -1353,35 +1476,139 @@ char * C, *F, *D; if (i==j) break; Next = N->n; } + updDynFct(Next,0); write(fd, (void*)&(N->typ), sizeof(N->typ)); - write(fd, (void*)(N->l), strlen(N->l)+1); + dump_eltC(fd, N->l); A = N->c; av = (int*)A; vi = (long)*av++; /* i */ - write(fd, (void*)&v, sizeof(v)); + vv = vi / t; + write(fd, (void*)&vv, sizeof(vv)); v = (long)*av++; /* ea */ - write(fd, (void*)&v, sizeof(v)); + vv = v / t; + write(fd, (void*)&vv, sizeof(vv)); v = (long)*av++; /* Ea */ - write(fd, (void*)&v, sizeof(v)); + vv = v / t; + write(fd, (void*)&vv, sizeof(vv)); C = (char*)A+(3*sizeof(int)); - D = C; F = C+vi; + pas = sizeof(A) + 1; + while (C < F) { + dump_code(fd, C); + C += pas; + } } + dump_rest_pr(0,n,"user functions"); } +void restore_links_stackF(void) +{ +void **ANext, *A; + return; +} + +static int NbARIV, NbARIL, NbARIF; +void restore_code(int fd, char * b, long delta) +{ +void * A; +uint32_t i; +int nc; + if ((read(fd, (void*)b, 1)) != 1) return; + switch((Code)*b) { + case T_NUM : + A = restore_eltN(fd); + bcopy((void*)(&A),(void*)(b+1),sizeof(A)); + break; + case T_CHA : + case T_BKC : + case T_BKC1 : + A = (void*)restore_eltC(fd); + bcopy((void*)(&A),(void*)(b+1),sizeof(A)); + break; + case T_LIB : + nc = read(fd, (void*)&i, sizeof(i)); + A = libByInd(i); + bcopy((void*)(&A),(void*)(b+1),sizeof(A)); + NbARIL++; + break; + case T_FCT : + nc = read(fd, (void*)&i, sizeof(i)); + A = codFctByInd(i-delta); + /* printf("i=%d delta=%d A=%lx\n",i,delta,(long)A); */ + bcopy((void*)(&A),(void*)(b+1),sizeof(A)); + NbARIF++; + break; + case T_VAR : + nc = read(fd, (void*)&i, sizeof(i)); + A = varAddrByInd(i); + bcopy((void*)(&A),(void*)(b+1),sizeof(A)); + NbARIV++; + break; + case T_IF : + case T_IFN : + case T_DO : + case T_IFD : + case T_JMP : + case T_GOTO : + nc = read(fd, (void*)&i, sizeof(i)); + bcopy((void*)(&i),(void*)(b+1),sizeof(i)); + break; + default : + break; + } + /* printf("\tCODE=%d i=%d\n", (int)*b, (int)i); */ +} void restore_stackF(int fd) { -long n=0, i; +uint32_t n=0, vv; +long i, vi, v, pas, t; short typ; -char Lib[LDFLT+2], *b; +char *Lib, *C, *F; +void * M; +struct Fct *Elt; +int *bi, nc; if (read(fd, (void*)&n, sizeof(n)) != sizeof(n)) return; - /* printf("Il y a %ld fonctions !\n",n); */ + /* suppress all fcts */ + rmAllFonU(); + while (stackF != VIDE) { + Elt = (struct Fct *)stackF; + stackF = Elt->n; + eraseFct(Elt); + } + /* printf("Nb fct : %d\n", n); */ + NbARIV=NbARIF=NbARIL=0; + t = sizeof(M) + 1; for (i=0; i type=%d vi=%d v1=%d", Lib, typ, (int)vi, (int)v); */ + nc=read(fd, (void*)&vv, sizeof(vv)); + v = vv * t; + *bi = (int)v; + /* printf(" v2=%d\n", (int)v); */ + C = (char*)M+(3*sizeof(int)); + F = C+vi; + pas = sizeof(M) + 1; + putCodeFct(M); + while (C < F) { + restore_code(fd, C, n-(i+1)); + C += pas; + } } + dump_rest_pr(1,n,"user functions"); + rest_links_pr(NbARIV, "variable", "user functions"); + rest_links_pr(NbARIL, "library function", "user functions"); + rest_links_pr(NbARIF, "user function", "user functions"); } diff --git a/src/stackF.h b/src/stackF.h index 9e943bb..509d5ef 100644 --- a/src/stackF.h +++ b/src/stackF.h @@ -82,9 +82,11 @@ extern void IF_DO_Next(void); extern void IF_DO_Show(void); extern void execCode(void *C); extern void * fctByName(char *L); +extern void * fctByInd(long i); extern void * fctByCode(void *C); extern void execFctV(void * A); extern char * fctByAddr(void *A); +extern long iFctByAddr(void *A); extern char * codByAddr(void *A); extern void IF_nDO(void); diff --git a/src/stackL.c b/src/stackL.c index 777c812..218d274 100644 --- a/src/stackL.c +++ b/src/stackL.c @@ -178,3 +178,24 @@ char s; } else printf("\n"); } + +void dump_stackL(int fd) +{ +uint32_t n; + n = i_StackL; + write(fd, (void*)&n, sizeof(n)); + if (n) write(fd, (void*)stackL, n); + dump_rest_pr(0,n,"logical"); +} + +void restore_stackL(int fd) +{ +uint32_t n=0; + if (read(fd, (void*)&n, sizeof(n)) != sizeof(n)) return; + IF_stackL_clear(); + if (n) { + read(fd, (void*)stackL, n); + _MODIF_i_StackL_(n); + } + dump_rest_pr(1,n,"logical"); +} diff --git a/src/stackL.h b/src/stackL.h index 8540e62..addf4b0 100644 --- a/src/stackL.h +++ b/src/stackL.h @@ -37,4 +37,8 @@ extern void IF_true(void); extern void IF_false(void); extern void IF_show_stackL(void); +extern void dump_stackL(int fd); +extern void restore_stackL(int fd); + + #endif diff --git a/src/stackN.c b/src/stackN.c index 4be6e54..2a3e93a 100644 --- a/src/stackN.c +++ b/src/stackN.c @@ -494,7 +494,6 @@ int s; NElt->n = StackN; NElt->t = Elt->t & ~MSK_V; /* au cas ou Var */ _MODIF_STACKN_(M); - ; } void IF_dup(void) @@ -1958,3 +1957,111 @@ void IF_TABProd(void) { TAB_Fct('*'); } void IF_TABSum(void) { TAB_Fct('+'); } void IF_TABMinMax(void) { TAB_Fct('E'); } +#define LENT 20 +void dump_eltN(int fd, void *A, uint32_t MSK) +{ +struct Num * N; +long l; +long long v; +uint32_t t; + N = (struct Num*)A; + t = N->t; + if ((MSK) && (t & MSK_V)) { + v = (long long)iVarByAddrA(A); + t = 1 | MSK_V; + write(fd, (void*)&t, sizeof(t)); + write(fd, (void*)&v, sizeof(v)); + } else { + /* t &= ~MSK_V; */ + write(fd, (void*)&t, sizeof(t)); + l = t & MSK_T; + write(fd, (void*)&(N->l), (size_t)(l*sizeof(double))); + } +} + +static int NbARIV; +void restore_links_stackN(void) +{ +void **ANext, *A; +struct Num * N, *T; + if (NbARIV == 0) return; + ANext = &StackN; + while (*ANext != VIDE) { + N = (struct Num*) *ANext; + if (N->t & MSK_V) { + A = varAddrAByInd(N->l); + T = (struct Num*) A; + T->n = N->n; + *ANext = A; + free((void*)N); + } + ANext = &(N->n); + } + rest_links_pr(NbARIV, "variable", "numerical"); +} + +void dump_stackN(int fd) +{ +void * Next; +struct Num * N; +uint32_t n=0; +int i,j; + Next = StackN; + while (Next != VIDE) { + N = (struct Num*) Next; + n++; + Next = N->n; + } + write(fd, (void*)&n, sizeof(n)); + for (i=n; i>0; i--) { + Next = StackN; + j=0; + while (Next != VIDE) { + N = (struct Num*) Next; + j++; + if (i==j) break; + Next = N->n; + } + dump_eltN(fd, Next, MSK_V); + } + dump_rest_pr(0,n,"numerical"); +} + +static void * restore_eltN_l(int fd, int K) +{ +uint32_t t, u; +int s; +void * M; +struct Num * N; + read(fd, (void*)&t, sizeof(t)); + u = t & MSK_T; + if ((t & MSK_V) && K) NbARIV++; + /* printf("%u : %d\n", u, NbARIV); /* debug */ + s = sizeof(struct Num)+((u-1)*(sizeof(double))); + if ((M = malloc(s)) == NULL) stopErr("restore_eltN","malloc"); + N = (struct Num*)M; + N->t = t; + N->n = StackN; + read(fd, (void*)&(N->l), u*sizeof(double)); + return M; +} + +void * restore_eltN(int fd) +{ + return restore_eltN_l(fd,0); +} + +void restore_stackN(int fd) +{ +uint32_t n=0, i; +void * M; + NbARIV = 0; + if (read(fd, (void*)&n, sizeof(n)) != sizeof(n)) return; + IF_stack_clear(); + for (i=0; i #define VT_N 3 /* NUMBER */ #define VT_L 4 /* LIB FUNCTION */ #define VT_F 5 /* USER FUNCTION */ +#define VT_XF 15 /* USER FUNCTION NOT IDENTIFIED during restore */ #define VT_V 9 /* VARIABLE (for "install_v ... in" only) */ @@ -73,20 +74,26 @@ struct Var *vS, *vD; vD->a = duplicateNum(vS->a, 0); } -void initVar(char *Lib) +void initVarSL(char *L) { -void * M, *L; +void * M; struct Var * N; - if ((M = malloc(sizeof(struct Var))) == NULL) stopErr("initVar","malloc"); - if ((L = malloc(strlen(Lib)+1)) == NULL) stopErr("initVar","malloc"); - strcpy((char*)L,Lib); + if ((M = malloc(sizeof(struct Var))) == NULL) stopErr("initVarSL","malloc"); N = (struct Var*)M; - N->l = (char*)L; + N->l = L; N->n = stackV; N->t = VT_I; stackV = M; } +void initVar(char *Lib) +{ +void *L; + if ((L = malloc(strlen(Lib)+1)) == NULL) stopErr("initVar","malloc"); + strcpy((char*)L,Lib); + initVarSL((char*)L); +} + static void setCodeVar(struct Var * Elt, short t, void* A) { switch (Elt->t) { /* TODO VT_F */ @@ -147,49 +154,13 @@ struct Var * N; case VT_F : printf("User Fct. %s", fctByAddr(N->a)); break; + default : + printf("Undefined (%d) !??", N->t); } printf("\n"); Next = N->n; } - printf("\n"); -} - -void dump_stackV(int fd) -{ -void * Next, *A; -char * noms, *pn; -struct Var * N; -long n, l, i; - n=l=0; - Next = stackV; - while (Next != VIDE) { - N = (struct Var*) Next; - n++; - l += (strlen(N->l)+1); - Next = N->n; - } - l++; - write(fd, (void*)&n, sizeof(n)); - if (n>0) { - if ((A = malloc(l)) == NULL) stopErr("dump_stackV","malloc"); - noms = (char*)A; - pn = noms; - Next = stackV; - while (Next != VIDE) { - N = (struct Var*) Next; - strcpy(pn,N->l); - pn += (strlen(N->l)+1); - Next = N->n; - } - /* on remonte la liste */ - while (pn > noms) { - pn -= 2; - while (*pn != '\0') pn--; - pn++; - write (fd,pn,strlen(pn)+1); - } - free(A); - } + printf("\n"); } static void newVar(char * S) @@ -201,19 +172,6 @@ char Lib[LDFLT+1]; dropTrSuite(); } -void restore_stackV(int fd) -{ -long n=0, i; -char Lib[LDFLT+2], *b; - if (read(fd, (void*)&n, sizeof(n)) != sizeof(n)) return; - *Lib=' '; - for (i=0; in; + } + return 0L; +} + +long iVarByAddrA(void * A) +{ +void * Next; +struct Var * N; +long i=0; + Next = stackV; + while (Next != VIDE) { + i++; + N = (struct Var*) Next; + if (N->a==A) return(i); + Next = N->n; + } + return 0L; +} + +void * varAddrByInd(long i) +{ +void * Next; +struct Var * N; +long j=0; + Next = stackV; + while (Next != VIDE) { + j++; + if (i==j) return(Next); + N = (struct Var*) Next; + Next = N->n; + } + return NULL; +} + +void * varAddrAByInd(long i) +{ +void * Next; +struct Var * N; +long j=0; + Next = stackV; + while (Next != VIDE) { + j++; + N = (struct Var*) Next; + if (i==j) return(N->a); + Next = N->n; + } + return NULL; +} + int isVarChar(void * A) { struct Var * N; @@ -440,3 +458,147 @@ struct Var * N; return 0; } +void dump_eltV(int fd, void *A) +{ +struct Var * N; +uint32_t a; +int nc; + N = (struct Var*)A; + nc = write(fd, (void*)&(N->t), sizeof(N->t)); + dump_eltC(fd, N->l); + switch(N->t) { + case VT_I : + break; + case VT_B : + nc = write(fd, (void*)&(N->b), sizeof(N->b)); + break; + case VT_C : + dump_eltC(fd, (char*)N->a); + break; + case VT_N : + dump_eltN(fd, N->a, 0); + break; + case VT_L : + a = iLibByAddr(N->a); + nc = write(fd, (void*)&a, sizeof(a)); + break; + case VT_F : + a = iFctByAddr(N->a); + nc = write(fd, (void*)&a, sizeof(a)); + break; + default : + printf("Var type %d !?\n", N->t); + break; + } +} + +void dump_stackV(int fd) +{ +void * Next, *A; +struct Var * N; +uint32_t n=0; +long l, i, j; +int nc; + Next = stackV; + while (Next != VIDE) { + N = (struct Var*) Next; + n++; + Next = N->n; + } + nc = write(fd, (void*)&n, sizeof(n)); + for (i=n; i>0; i--) { + Next = stackV; + j=0; + while (Next != VIDE) { + N = (struct Var*) Next; + j++; + if (i==j) break; + Next = N->n; + } + dump_eltV(fd, Next); + } + dump_rest_pr(0,n,"variables"); +} + +void restore_links_stackV(void) +{ +void * Next; +struct Var * N; +long i, j=0; + Next = stackV; + while (Next != VIDE) { + N = (struct Var*) Next; + if (N->t == VT_XF) { + i = (long)(N->a); + N->a = fctByInd(i); + N->t = VT_F; + j++; + } + Next = N->n; + } + rest_links_pr(j, "user function", "variables"); +} + +static int NbARIL; +void restore_eltV(int fd) +{ +struct Var * N; +short t; +char *L; +void *A; +bool b; +uint32_t i; +long Vi; +int nc; + if (read(fd, (void*)&t, sizeof(t)) != sizeof(t)) return; + L = restore_eltC(fd); + initVarSL(L); + /* printf("Var %s %d\n", L, t); */ + N = (struct Var *)stackV; + switch(t) { + case VT_I : + A = VIDE; + break; + case VT_B : + A = VIDE; + nc = read(fd, (void*)&b, sizeof(b)); + break; + case VT_C : + A = (void*) restore_eltC(fd); + break; + case VT_N : + A = restore_eltN(fd); + break; + case VT_L : + nc = read(fd, (void*)&i, sizeof(i)); + A = libByInd(i); + NbARIL++; + break; + case VT_F : + nc = read(fd, (void*)&i, sizeof(i)); + /* A = fctByInd(i); not possible ! */ + Vi = (long)i; + A = (void*)Vi; + t = VT_XF; + break; + default : + printf("Var type %d !?\n", N->t); + break; + } + setCodeVar(N, t, A); + if (t == VT_B) N->b = b; +} + +void restore_stackV(int fd) +{ +uint32_t n=0, i; + if (read(fd, (void*)&n, sizeof(n)) != sizeof(n)) return; + NbARIL=0; + while (stackV != VIDE) rmVar(&stackV, (struct Var *)stackV); + for (i=0; i