Index: /branches/prototype-v0/zoo-project/zoo-kernel/Makefile.in
===================================================================
--- /branches/prototype-v0/zoo-project/zoo-kernel/Makefile.in	(revision 872)
+++ /branches/prototype-v0/zoo-project/zoo-kernel/Makefile.in	(revision 873)
@@ -78,4 +78,7 @@
 	g++ ${XML2CFLAGS} ${PYTHONCFLAGS} ${CFLAGS} -c service_internal_python.c
 
+service_internal_r.o: service_internal_r.c service.h
+	g++ ${XML2CFLAGS} ${RCFLAGS} ${CFLAGS} -c service_internal_r.c
+
 service_internal_otb.o: service_internal_otb.c service_internal_otb.h service.h
 	g++ ${XML2CFLAGS} ${OTBCFLAGS} ${CFLAGS} -c service_internal_otb.c
@@ -114,13 +117,13 @@
 	g++ -c ${XML2CFLAGS} ${PYTHONCFLAGS} ${CFLAGS} service_loader.c
 
-zoo_service_loader.o: zoo_service_loader.c service.h version.h ${MS_FILE} ${PYTHON_FILE} ${PHP_FILE} ${JAVA_FILE} ${JS_FILE} ${PERL_FILE} ${RUBY_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE} ${MONO_FILE} ${HPC_FILES} ${METADB_FILE}
-	g++ -g -O2 ${JSON_CFLAGS} ${GDAL_CFLAGS} ${XML2CFLAGS} ${CFLAGS} ${MONO_CFLAGS} ${SAGA_CFLAGS} ${OTBCFLAGS} ${PYTHONCFLAGS} ${JAVACFLAGS} ${JSCFLAGS} ${PERLCFLAGS} ${PHPCFLAGS} ${SAGA_ENABLED} ${OTB_ENABLED} ${HPC_ENABLED} ${PYTHON_ENABLED} ${JS_ENABLED} ${PHP_ENABLED} ${PERL_ENABLED} ${JAVA_ENABLED} ${MONO_ENABLED} ${METADB_ENABLED} -c zoo_service_loader.c  -fno-common -DPIC -o zoo_service_loader.o
+zoo_service_loader.o: zoo_service_loader.c service.h version.h ${MS_FILE} ${R_FILE} ${PYTHON_FILE} ${PHP_FILE} ${JAVA_FILE} ${JS_FILE} ${PERL_FILE} ${RUBY_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE} ${MONO_FILE} ${HPC_FILES} ${METADB_FILE}
+	g++ -g -O2 ${JSON_CFLAGS} ${GDAL_CFLAGS} ${XML2CFLAGS} ${CFLAGS} ${MONO_CFLAGS} ${SAGA_CFLAGS} ${OTBCFLAGS} ${RCFLAGS} ${PYTHONCFLAGS} ${JAVACFLAGS} ${JSCFLAGS} ${PERLCFLAGS} ${PHPCFLAGS} ${SAGA_ENABLED} ${OTB_ENABLED} ${HPC_ENABLED} ${PYTHON_ENABLED} ${R_ENABLED} ${JS_ENABLED} ${PHP_ENABLED} ${PERL_ENABLED} ${JAVA_ENABLED} ${MONO_ENABLED} ${METADB_ENABLED} -c zoo_service_loader.c  -fno-common -DPIC -o zoo_service_loader.o
 
 libzoo_service.${EXT}: version.h service_internal.o service.o sqlapi.o
 	gcc -shared  ${GDAL_CFLAGS} ${DEFAULT_OPTS} -fpic -o libzoo_service.${EXT} ${CFLAGS}  service_internal.o service.o sqlapi.o ${FCGI_LDFLAGS} ${GDAL_LIBS} ${ZOO_LDFLAGS}
 
-zoo_loader.cgi: version.h libzoo_service.${EXT} zoo_loader.c zoo_service_loader.o  ulinet.o service.h lex.sr.o service_conf.tab.o service_conf.y ulinet.o main_conf_read.tab.o lex.cr.o request_parser.o response_print.o server_internal.o caching.o ${MS_FILE} ${PYTHON_FILE} ${PHP_FILE} ${JAVA_FILE} ${JS_FILE} ${PERL_FILE} ${RUBY_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE} ${HPC_FILES} ${METADB_FILE} ${JSON_FILE} service_callback.o
+zoo_loader.cgi: version.h libzoo_service.${EXT} zoo_loader.c zoo_service_loader.o  ulinet.o service.h lex.sr.o service_conf.tab.o service_conf.y ulinet.o main_conf_read.tab.o lex.cr.o request_parser.o response_print.o server_internal.o caching.o ${MS_FILE} ${PYTHON_FILE} ${PHP_FILE} ${JAVA_FILE} ${JS_FILE} ${PERL_FILE} ${RUBY_FILE} ${YAML_FILE} ${OTB_FILE} ${R_FILE} ${SAGA_FILE} ${HPC_FILES} ${METADB_FILE} ${JSON_FILE} service_callback.o
 	g++ -g -O2 ${JSON_CFLAGS} ${JSCFLAGS} ${PHPCFLAGS}  ${PERLCFLAGS} ${RUBYCFLAGS}  ${JAVACFLAGS} ${XML2CFLAGS} ${PYTHONCFLAGS} ${CFLAGS} -c zoo_loader.c  -fno-common -DPIC -o zoo_loader.o
-	g++  ${JSON_CFLAGS} ${JSCFLAGS} ${SAGA_CFLAGS} ${OTBCFLAGS} ${GDAL_CFLAGS} ${XML2CFLAGS} ${PHPCFLAGS} ${PERLCFLAGS} ${JAVACFLAGS} ${PYTHONCFLAGS} ${CFLAGS} zoo_loader.o zoo_service_loader.o ${MS_FILE} ${PYTHON_FILE}  ${PERL_FILE} ${PHP_FILE}  ${JS_FILE} ${JAVA_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE} ${MONO_FILE} ${HPC_FILES} ${METADB_FILE} ${JSON_FILE} service_callback.o response_print.o server_internal.o caching.o request_parser.o ulinet.o lex.cr.o lex.sr.o service_conf.tab.o main_conf_read.tab.o -o zoo_loader.cgi -L. ${LDFLAGS}
+	g++  ${JSON_CFLAGS} ${JSCFLAGS} ${SAGA_CFLAGS} ${OTBCFLAGS} ${GDAL_CFLAGS} ${XML2CFLAGS} ${PHPCFLAGS} ${PERLCFLAGS} ${JAVACFLAGS} ${PYTHONCFLAGS} ${CFLAGS} zoo_loader.o zoo_service_loader.o ${MS_FILE} ${PYTHON_FILE}  ${PERL_FILE} ${PHP_FILE}  ${JS_FILE} ${JAVA_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE} ${MONO_FILE} ${HPC_FILES} ${METADB_FILE} ${JSON_FILE} ${R_FILE} service_callback.o response_print.o server_internal.o caching.o request_parser.o ulinet.o lex.cr.o lex.sr.o service_conf.tab.o main_conf_read.tab.o -o zoo_loader.cgi -L. ${LDFLAGS}
 
 zcfg2yaml: zcfg2yaml.c service.h lex.sr.o service_conf.tab.o service_conf.y main_conf_read.tab.o lex.cr.o response_print.o server_internal.o service_internal.o ${MS_FILE} ${YAML_FILE}
Index: /branches/prototype-v0/zoo-project/zoo-kernel/ZOOMakefile.opts.in
===================================================================
--- /branches/prototype-v0/zoo-project/zoo-kernel/ZOOMakefile.opts.in	(revision 872)
+++ /branches/prototype-v0/zoo-project/zoo-kernel/ZOOMakefile.opts.in	(revision 873)
@@ -58,4 +58,9 @@
 PYTHON_FILE=@PYTHON_FILE@
 
+RCFLAGS=@R_CPPFLAGS@
+RLDFLAGS=@R_LDFLAGS@
+R_ENABLED=@R_ENABLED@
+R_FILE=@R_FILE@
+
 RUBYCFLAGS=@RUBY_CPPFLAGS@
 RUBYLDFLAGS=@RUBY_LDFLAGS@
@@ -115,5 +120,5 @@
 
 CFLAGS=@SSH2_CPPFLAGS@ @GETTEXT_CFLAGS@ @PROJ_CPPFLAGS@ @RELY_ON_DB@ @DEB_DEF@ -fpic @OPENSSL_CFLAGS@ @UUID_CFLAGS@ ${FCGI_CFLAGS} ${YAML_CFLAGS} ${MACOS_CFLAGS} ${MS_CFLAGS} -I../../thirds/cgic206 -I. -DLINUX_FREE_ISSUE #-DDEBUG #-DDEBUG_SERVICE_CONF
-LDFLAGS=-lzoo_service @SSH2_LDFLAGS@ @PROJ_LDFLAGS@ @DEFAULT_LIBS@ -L../../thirds/cgic206 -lcgic ${GDAL_LIBS} ${XML2LDFLAGS} ${PYTHONLDFLAGS} ${PERLLDFLAGS}  ${PHPLDFLAGS} ${JAVALDFLAGS} ${JSLDFLAGS}  ${FCGI_LDFLAGS} @OPENSSL_LDFLAGS@ @UUID_LDFLAGS@ ${MS_LDFLAGS} ${MACOS_LD_FLAGS} ${MACOS_LD_NET_FLAGS} ${YAML_LDFLAGS} ${OTBLDFLAGS} ${SAGA_LDFLAGS} ${MONO_LDFLAGS} @GETTEXT_LDFLAGS@ @JSON_LDFLAGS@ @XSLT_LDFLAGS@
+LDFLAGS=-lzoo_service @SSH2_LDFLAGS@ @PROJ_LDFLAGS@ @DEFAULT_LIBS@ -L../../thirds/cgic206 -lcgic ${GDAL_LIBS} ${XML2LDFLAGS} ${PYTHONLDFLAGS} ${PERLLDFLAGS}  ${PHPLDFLAGS} ${JAVALDFLAGS} ${JSLDFLAGS}  ${FCGI_LDFLAGS} @OPENSSL_LDFLAGS@ @UUID_LDFLAGS@ ${MS_LDFLAGS} ${MACOS_LD_FLAGS} ${MACOS_LD_NET_FLAGS} ${YAML_LDFLAGS} ${OTBLDFLAGS} ${SAGA_LDFLAGS} ${MONO_LDFLAGS} ${RLDFLAGS} @GETTEXT_LDFLAGS@ @JSON_LDFLAGS@ @XSLT_LDFLAGS@
 
 DATAROOTDIR=@datarootdir@/zoo-project
Index: /branches/prototype-v0/zoo-project/zoo-kernel/configure.ac
===================================================================
--- /branches/prototype-v0/zoo-project/zoo-kernel/configure.ac	(revision 872)
+++ /branches/prototype-v0/zoo-project/zoo-kernel/configure.ac	(revision 873)
@@ -523,4 +523,40 @@
 AC_SUBST([MS_FILE])
 AC_SUBST([MS_VERSION])
+
+# ===========================================================================
+# Detect if R is installed
+# ===========================================================================
+
+AC_ARG_WITH([r], 
+	[AS_HELP_STRING([--with-r=PATH], [To enable python support or Specifies an alternative directory for R installation,  disabled by default])], 
+	[R_PATH="$withval"; R_ENABLED="-DUSE_R"], [R_ENABLED=""])
+
+if test -z "$R_ENABLED"
+then
+	R_FILE=""
+else
+	R_FILE="service_internal_r.o"
+	# Extract the linker and include flags 
+	R_LDFLAGS="-L$R_PATH/lib/ -lR"
+	R_CPPFLAGS="-I$R_PATH/include/"
+
+	# Check headers file
+	CPPFLAGS_SAVE="$CPPFLAGS"
+	CPPFLAGS="$R_CPPFLAGS"
+	AC_CHECK_HEADERS([RInternals.h],
+		 [], [AC_MSG_ERROR([could not find headers include related to R])])
+
+	# Ensure we can link against libphp
+	#LIBS_SAVE="$LIBS"
+	#LIBS="$R_LDFLAGS"
+	#AC_CHECK_LIB([$LIBS], [R_tryEval], [], [AC_MSG_ERROR([could not find libR])], [])
+	#LIBS="$LIBS_SAVE"
+fi
+
+AC_SUBST([R_CPPFLAGS])
+AC_SUBST([R_LDFLAGS])
+AC_SUBST([R_ENABLED])
+AC_SUBST([R_FILE])
+
 
 # ===========================================================================
Index: /branches/prototype-v0/zoo-project/zoo-kernel/service.c
===================================================================
--- /branches/prototype-v0/zoo-project/zoo-kernel/service.c	(revision 872)
+++ /branches/prototype-v0/zoo-project/zoo-kernel/service.c	(revision 873)
@@ -190,9 +190,25 @@
  * Count number of map in a map
  *
- * @param m the maps to count
+ * @param m the map to count
  * @return number of map in a map
  */
 int count(map* m){
   map* tmp=m;
+  int c=0;
+  while(tmp!=NULL){
+    c++;
+    tmp=tmp->next;
+  }
+  return c;
+}
+
+/**
+ * Count number of maps in a maps
+ *
+ * @param m the maps to count
+ * @return number of maps in a maps
+ */
+int maps_length(maps* m){
+  maps* tmp=m;
   int c=0;
   while(tmp!=NULL){
Index: /branches/prototype-v0/zoo-project/zoo-kernel/service_internal_r.c
===================================================================
--- /branches/prototype-v0/zoo-project/zoo-kernel/service_internal_r.c	(revision 873)
+++ /branches/prototype-v0/zoo-project/zoo-kernel/service_internal_r.c	(revision 873)
@@ -0,0 +1,403 @@
+/*
+ * Author : Gérald FENOY
+ *
+ * Copyright (c) 2018 GeoLabs SARL
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "service_internal_r.h"
+
+void jump_to_toplevel(void){
+  fprintf(stderr,"Error occured\n");
+  fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
+  fflush(stderr);
+  //resetStack(1);
+  fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
+  fflush(stderr);
+}
+
+void init_zoo(SEXP conf,SEXP outputs){
+  const char* names[]={
+    "SERVICE_SUCCEEDEED",
+    "SERVICE_FAILED",
+    "conf",
+    "outputs",
+    ""
+  };
+  SEXP res = PROTECT(mkNamed(VECSXP, names));
+  SET_VECTOR_ELT(res, 0, Rf_ScalarInteger(3));
+  SET_VECTOR_ELT(res, 1, Rf_ScalarInteger(4));
+  SET_VECTOR_ELT(res, 2, conf);
+  SET_VECTOR_ELT(res, 3, outputs);
+  defineVar(install("zoo"),res, R_GlobalEnv);
+
+  static const R_CallMethodDef callMethods[]  = {
+    {"ZOOTranslate", (DL_FUNC) &RTranslate, 1},
+    {"ZOOUpdateStatus", (DL_FUNC) &RUpdateStatus, 2},
+    {NULL, NULL, 0}
+  };
+  
+  static R_NativePrimitiveArgType RTranslate_t[] = {
+    STRSXP
+  };
+  static R_NativePrimitiveArgType RUpdateStatus_t[] = {
+    VECSXP,STRSXP
+  };
+  
+  static const R_CMethodDef cMethods[] = {
+   {"ZOOTranslate", (DL_FUNC) &RTranslate, 1, RTranslate_t},
+   {"ZOOUpdateStatus", (DL_FUNC) &RUpdateStatus, 2, RUpdateStatus_t},
+   {NULL, NULL, 0, NULL}
+  };
+  
+  DllInfo *info = R_getEmbeddingDllInfo();
+  R_registerRoutines(info, cMethods, callMethods, NULL, NULL);
+}
+
+/**
+ * Load a R script then run the function corresponding to the service
+ * by passing the conf, inputs and outputs parameters by reference. 
+ *
+ * @param main_conf the conf maps containing the main.cfg settings
+ * @param request the map containing the HTTP request
+ * @param s the service structure
+ * @param real_inputs the maps containing the inputs
+ * @param real_outputs the maps containing the outputs
+ */
+int zoo_r_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){
+  SEXP pName;
+  int result=0;
+  maps* m=*main_conf;
+  maps* inputs=*real_inputs;
+  maps* outputs=*real_outputs;
+  map* tmp0=getMapFromMaps(*main_conf,"lenv","cwd");
+  char *ntmp=tmp0->value;
+  map* tmp=NULL;
+  int hasToClean=0;
+  char *r_path, *rpath;
+  map* cwdMap=getMapFromMaps(*main_conf,"main","servicePath");
+  int r_argc = 3;
+  char *r_argv[] = { "R", "--no-save", "--silent" };
+  Rf_initEmbeddedR(r_argc, r_argv);
+  if(cwdMap!=NULL)
+    r_path=cwdMap->value;
+  else{
+    if(tmp0!=NULL)
+      r_path=tmp0->value;
+    else
+      r_path=(char*)".";
+  }
+
+  tmp=getMap(s->content,"serviceProvider");
+  map* mp=getMap(request,"metapath");
+  if(tmp!=NULL){
+    if(mp!=NULL && strlen(mp->value)>0){
+      char *mps=zStrdup(mp->value);
+      int i,len=strlen(mps);
+      int j=0;
+      for(i=0;i<len;i++){
+	if(mps[i]=='/'){
+	  mps[i]='.';
+	}
+      }
+      char *mn=(char*)malloc((strlen(mps)+strlen(tmp->value)+2)*sizeof(char));
+      sprintf(mn,"%s.%s",mps,tmp->value);
+      pName = mkString(mn);
+      free(mn);
+      free(mps);
+    }
+    else{
+      dumpMap(tmp);
+      char *tmpStr=(char*)malloc((strlen(r_path)+strlen(tmp->value)+2)*sizeof(char));
+      sprintf(tmpStr,"%s/%s",r_path,tmp->value);
+      pName = mkString(tmpStr);
+      free(tmpStr);
+    }
+  }
+  else{
+    errorException (m, "Unable to parse serviceProvider please check your zcfg file.", "NoApplicableCode", NULL);
+    return -1;
+  }
+  SEXP e;
+  int errorOccurred;
+  PROTECT(e = lang2(install("source"), pName));
+  R_tryEval(e, R_GlobalEnv, &errorOccurred);
+  UNPROTECT(1);
+  if (errorOccurred){
+    setMapInMaps(*main_conf,"lenv","message",_("Unable to load your R file"));
+    return SERVICE_FAILED;
+  }else{
+    result=SERVICE_FAILED;
+    {
+      SEXP pValue;
+      SEXP  arg1=RList_FromMaps(m);
+      SEXP  arg2=RList_FromMaps(*real_inputs);
+      SEXP  arg3=RList_FromMaps(*real_outputs);
+      SEXP r_call;
+      init_zoo(arg1,arg3);
+      PROTECT(r_call = lang4(install(s->name), arg1,arg2,arg3));
+      int errorOccurred;
+      
+      SEXP ret = R_tryEval(r_call, R_GlobalEnv, &errorOccurred);
+      if (!errorOccurred) {
+	int *val = INTEGER(ret);
+        for (int i = 0; i < LENGTH(ret); i++)
+	  if(i==0){
+	      result=val[i];
+	      SEXP zooEnv = findVar(install("zoo"), R_GlobalEnv);
+	      if(zooEnv!=NULL){
+		SEXP names = Rf_getAttrib(zooEnv, R_NamesSymbol);
+		int nbKeys=nrows(names);
+		int i;
+		for(i=0;i<nbKeys;i++){
+		  if(i==2){
+		    freeMaps(main_conf);
+		    free(*main_conf);
+		    SEXP confList=VECTOR_ELT(zooEnv,i);
+		    *main_conf=mapsFromRList(confList);
+		  }
+		  if(i==3){
+		    freeMaps(real_outputs);
+		    free(*real_outputs);
+		    SEXP outList=VECTOR_ELT(zooEnv,i);
+		    *real_outputs=mapsFromRList(outList);
+		  }
+		}
+	      }
+	      return result;
+	    }
+      }else{
+	const char* tmpStr=R_curErrorBuf();
+	setMapInMaps(*main_conf,"lenv","message",tmpStr);
+	char* finalStr=(char*)malloc((strlen(tmpStr)+strlen(_("Unable to run your R service: "))+2)*sizeof(char));
+	sprintf(finalStr,"%s %s",_("Unable to run your R service: "),tmpStr);
+	fprintf(stderr,"%s %d %s \n",__FILE__,__LINE__,tmpStr);
+	fflush(stderr);
+	errorException(*main_conf,finalStr,"NoApplicableCode",NULL);
+	free(finalStr);
+	result=-1;
+      }
+    }
+  }
+  Rf_endEmbeddedR(0);
+  return result;
+}
+
+char** listMapsKeys(maps* m){
+  char** res=NULL;
+  maps* tmp=m;
+  int i=0;
+  while(tmp!=NULL){
+    if(i==0)
+      res=(char**)malloc(2*sizeof(char*));
+    else
+      res=(char**)realloc(res,(i+2)*sizeof(char*));
+    res[i]=zStrdup(tmp->name);
+    res[i+1]="";
+    i++;
+    tmp=tmp->next;
+  }
+  return res;
+}
+
+char** listMapKeys(map* m){
+  char** res=NULL;
+  map* tmp=m;
+  int i=0;
+  while(tmp!=NULL){
+    if(i==0)
+      res=(char**)malloc(2*sizeof(char*));
+    else
+      res=(char**)realloc(res,(i+2)*sizeof(char*));
+    res[i]=zStrdup(tmp->name);
+    res[i+1]="";
+    i++;
+    tmp=tmp->next;
+  }
+  return res;
+}
+
+/**
+ * Convert a maps to a R List
+ *
+ * @param t the maps to convert
+ * @return a new SEXP containing the converted maps
+ * @see RList_FromMap
+ * @warning make sure to free resources returned by this function
+ */
+SEXP RList_FromMaps(maps* t){
+  maps* tmp=t;
+  char** keys=listMapsKeys(t);
+  SEXP res = PROTECT(mkNamed(VECSXP,(const char**) keys));
+  free(keys);
+  int cnt=0;
+  while(tmp!=NULL){
+    SEXP input = RList_FromMap(tmp->content);
+    SET_VECTOR_ELT(res,cnt,input);
+    cnt++;
+    tmp=tmp->next;
+  } 
+  UNPROTECT(1);
+  return res;
+}
+
+/**
+ * Convert a map to a R List
+ *
+ * @param t the map to convert
+ * @return a new SEXP containing the converted maps
+ * @warning make sure to free resources returned by this function
+ */
+SEXP RList_FromMap(map* t){
+  map* tmp=t;
+  int hasSize=0;
+  char** keys=listMapKeys(t);
+  SEXP res = PROTECT(mkNamed(VECSXP, (const char**)keys));
+  free(keys);
+  int cnt=0;
+  while(tmp!=NULL){
+    SEXP value=mkString(tmp->value);
+    SET_VECTOR_ELT(res,cnt,value);
+    cnt++;
+    tmp=tmp->next;
+  }
+  UNPROTECT(1);
+  return res;
+}
+
+/**
+ * Convert a R List to a maps 
+ *
+ * @param t the PyDictObject to convert
+ * @return a new maps containing the converted PyDictObject
+ * @warning make sure to free resources returned by this function
+ */
+maps* mapsFromRList(SEXP t){
+  maps* res=NULL;
+  maps* cursor=NULL;
+  SEXP names = Rf_getAttrib(t, R_NamesSymbol);
+  int nbKeys=nrows(names);
+  int i;
+  for(i=0;i<nbKeys;i++){
+    SEXP key=STRING_ELT(names,i);
+    SEXP value=VECTOR_ELT(t,i);
+    cursor=createMaps(R_CHAR(key));
+    cursor->content=mapFromRList(value);
+    cursor->next=NULL;
+    if(res==NULL)
+      res=dupMaps(&cursor);
+    else
+      addMapsToMaps(&res,cursor);
+    freeMap(&cursor->content);
+    free(cursor->content);
+    free(cursor);
+  }
+  return res;
+}
+
+/**
+ * Convert a R List to a map 
+ *
+ * @param t the PyDictObject to convert
+ * @return a new map containing the converted PyDictObject
+ * @warning make sure to free resources returned by this function
+ */
+map* mapFromRList(SEXP t){
+  map* res=NULL;
+  SEXP names = Rf_getAttrib(t, R_NamesSymbol);
+  int nbKeys=nrows(names);
+  int i;
+  for(i=0;i<nbKeys;i++){
+    SEXP key=STRING_ELT(names,i);
+    SEXP value=VECTOR_ELT(t,i);
+    if(strncmp(R_CHAR(key),"child",5)!=0){
+      {
+	const char* lkey=R_CHAR(key);
+	const char* lvalue=CHAR(STRING_ELT(value,0));
+	if(res!=NULL){
+	  addToMap(res,lkey,lvalue);
+	}
+	else{
+	  res=createMap(lkey,lvalue);
+	}
+      }
+    }
+  }
+  return res;
+}
+
+/**
+ * Use the ZOO-Services messages translation function from the R
+ * environment
+ *
+ * @param str the R string passed from the R environment
+ * @return a new R string containing the translated value
+ * @see _ss
+ */
+SEXP
+RTranslate(SEXP str)
+{
+  if (!isString(str) || !TYPEOF(STRING_ELT( str, 0 )) == CHARSXP){
+#ifdef DEBUG
+    fprintf(stderr,"Incorrect arguments to update status function");
+#endif
+    return R_NilValue;
+  }
+  const char* tmpStr=CHAR(STRING_ELT(str,0));
+  return mkString(_ss(tmpStr));
+}
+
+/**
+ * Update the ongoing status of a running service from the R environment
+ *
+ * @param confdict the R arguments passed from the R environment
+ * @param status the R arguments passed from the R environment
+ * @return Nil to the Python environment
+ * @see _updateStatus
+ */
+SEXP
+RUpdateStatus(SEXP confdict,SEXP status)
+{
+  maps* conf;
+  int istatus;
+  char* cstatus=NULL;
+  if (!isInteger(status)){
+#ifdef DEBUG
+    fprintf(stderr,"Incorrect arguments to update status function");
+#endif
+    return R_NilValue;
+  }
+  istatus=asInteger(status);
+  if (istatus < 0 || istatus > 100){
+    return R_NilValue;
+  }
+  // create a local copy and update the lenv map
+  conf = mapsFromRList(confdict);
+  if(status!=NULL){
+    maps* tmpMaps=getMaps(conf,"lenv");
+    addIntToMap(tmpMaps->content,"status",istatus);
+  }
+  else
+    setMapInMaps(conf,"lenv","status","15");
+  _updateStatus(conf);
+  freeMaps(&conf);
+  free(conf);
+  return R_NilValue;
+}
Index: /branches/prototype-v0/zoo-project/zoo-kernel/service_internal_r.h
===================================================================
--- /branches/prototype-v0/zoo-project/zoo-kernel/service_internal_r.h	(revision 873)
+++ /branches/prototype-v0/zoo-project/zoo-kernel/service_internal_r.h	(revision 873)
@@ -0,0 +1,47 @@
+/*
+ * Author : Gérald FENOY
+ *
+ * Copyright (c) 2009-2010 GeoLabs SARL
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef ZOO_SERVICE_INTERNAL_R_H
+#define ZOO_SERVICE_INTERNAL_R_H 1
+
+#pragma once 
+
+#include <Rinternals.h>
+#include <Rembedded.h>
+#include "service_internal.h"
+#include "response_print.h"
+#include "cgic.h"
+
+SEXP RList_FromMaps(maps* t);
+SEXP RList_FromMap(map* t);
+
+maps* mapsFromRList(SEXP t);
+map* mapFromRList(SEXP t);
+
+int zoo_r_support(maps**,map*,service*,maps**,maps**);
+
+SEXP RTranslate(SEXP);
+SEXP RUpdateStatus(SEXP,SEXP);
+
+#endif
Index: /branches/prototype-v0/zoo-project/zoo-kernel/service_json.c
===================================================================
--- /branches/prototype-v0/zoo-project/zoo-kernel/service_json.c	(revision 872)
+++ /branches/prototype-v0/zoo-project/zoo-kernel/service_json.c	(revision 873)
@@ -89,4 +89,54 @@
   }
 
+  json_object* elementsToJson(elements* myElements){
+    json_object *res=json_object_new_object();
+    elements* cur=myElements;
+    while(cur!=NULL){
+      json_object *cres=json_object_new_object();
+      json_object_object_add(cres,"content",mapToJson(cur->content));
+      json_object_object_add(cres,"metadata",mapToJson(cur->metadata));
+      json_object_object_add(cres,"additional_parameters",mapToJson(cur->additional_parameters));
+      if(cur->format!=NULL){
+	json_object_object_add(cres,"format",json_object_new_string(cur->format));
+      }
+      if(cur->child==NULL){
+	if(cur->defaults!=NULL)
+	  json_object_object_add(cres,"defaults",mapToJson(cur->defaults->content));
+	else
+	  json_object_object_add(cres,"defaults",mapToJson(NULL));
+	iotype* scur=cur->supported;
+	json_object *resi=json_object_new_array();
+	while(scur!=NULL){
+	  json_object_array_add(resi,mapToJson(scur->content));
+	  scur=scur->next;
+	}
+	json_object_object_add(cres,"supported",resi);
+	fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
+	fflush(stderr);
+      }
+      
+      dumpElements(cur->child);
+      json_object_object_add(cres,"child",elementsToJson(cur->child));
+      fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
+      fflush(stderr);
+
+      json_object_object_add(res,cur->name,cres);
+      cur=cur->next;
+    }
+    return res;
+  }
+  
+  json_object* serviceToJson(service* myService){
+    json_object *res=json_object_new_object();
+    json_object_object_add(res,"name",json_object_new_string(myService->name));
+    json_object_object_add(res,"content",mapToJson(myService->content));
+    json_object_object_add(res,"metadata",mapToJson(myService->metadata));
+    json_object_object_add(res,"additional_parameters",mapToJson(myService->additional_parameters));
+    json_object_object_add(res,"inputs",elementsToJson(myService->inputs));
+    json_object_object_add(res,"outputs",elementsToJson(myService->outputs));
+    return res;
+  }
+  
+  
 #ifdef __cplusplus
 }
Index: /branches/prototype-v0/zoo-project/zoo-kernel/service_json.h
===================================================================
--- /branches/prototype-v0/zoo-project/zoo-kernel/service_json.h	(revision 872)
+++ /branches/prototype-v0/zoo-project/zoo-kernel/service_json.h	(revision 873)
@@ -38,4 +38,5 @@
   json_object* mapToJson(map*);
   json_object* mapsToJson(maps*);
+  json_object* serviceToJson(service*);
 #ifdef __cplusplus
 }
Index: /branches/prototype-v0/zoo-project/zoo-kernel/zoo_service_loader.c
===================================================================
--- /branches/prototype-v0/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 872)
+++ /branches/prototype-v0/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 873)
@@ -35,4 +35,8 @@
 #ifdef USE_OTB
 #include "service_internal_otb.h"
+#endif
+
+#ifdef USE_R
+#include "service_internal_r.h"
 #endif
 
@@ -706,4 +710,15 @@
 #endif
 
+#ifdef USE_R
+  if (strncasecmp (r_inputs->value, "R", 6) == 0)
+    {	  
+      *eres =
+        zoo_r_support (&m, request_inputs, s1,
+                            &request_input_real_format,
+                            &request_output_real_format);
+    }
+  else
+#endif
+
 #ifdef USE_JAVA
   if (strncasecmp (r_inputs->value, "JAVA", 4) == 0)
@@ -1711,4 +1726,8 @@
 				  addNestedOutputs(&s1);
 #endif
+				  json_object* jobj=serviceToJson(s1);
+				  const char* jsonStr=json_object_to_json_string_ext(jobj,JSON_C_TO_STRING_PLAIN);
+				  fprintf(stderr,"*** %s %d %s \n",__FILE__,__LINE__,jsonStr);
+
 				  printDescribeProcessForProcess (zooRegistry,m, doc, n, s1);
 				  freeService (&s1);
@@ -2548,4 +2567,6 @@
   rewind(stdout);
 
+  fprintf(stderr,"%s %d %d\n",__FILE__,__LINE__,eres);
+
   if (eres != -1)
     outputResponse (s1, request_input_real_format,
Index: /branches/prototype-v0/zoo-project/zoo-services/cgal/voronoi.c
===================================================================
--- /branches/prototype-v0/zoo-project/zoo-services/cgal/voronoi.c	(revision 872)
+++ /branches/prototype-v0/zoo-project/zoo-services/cgal/voronoi.c	(revision 873)
@@ -197,4 +197,14 @@
     int ns = 0;
     int nr = 0;
+    int nf = 0;
+    Face_iterator fit =T.faces_begin();
+    FILE* f=fopen("/tmp/toto.log","w+");
+    for ( ; fit !=T.faces_end(); ++fit) {
+      fprintf(stderr," *** %s %d %d %d\n",__FILE__,__LINE__,nf,fit.info());
+      fflush(stderr);
+      
+      nf++;
+    }
+    fclose(f);
     Edge_iterator eit =T.edges_begin();
     for ( ; eit !=T.edges_end(); ++eit) {
@@ -203,4 +213,5 @@
 	const Pointz p1=tmp->source();
 	const Pointz p2=tmp->target();
+	fprintf(stderr,"P1 %d %d | P2 %d %d\n",p1.x(),p1.y(),p2.x(),p2.y());
 #ifdef DEBUG
 	fprintf(stderr,"P1 %d %d | P2 %d %d\n",p1.x(),p1.y(),p2.x(),p2.y());
Index: /branches/prototype-v0/zoo-project/zoo-services/hello-r/cgi-env/RVoronoi.zcfg
===================================================================
--- /branches/prototype-v0/zoo-project/zoo-services/hello-r/cgi-env/RVoronoi.zcfg	(revision 873)
+++ /branches/prototype-v0/zoo-project/zoo-services/hello-r/cgi-env/RVoronoi.zcfg	(revision 873)
@@ -0,0 +1,56 @@
+[RVoronoi]
+ Title = Voronoi Diagram. 
+ Abstract = Computes the edges of Voronoi diagram for a set of data points.
+ Profile = urn:ogc:wps:1.0.0:voronoi
+ processVersion = 2
+ storeSupported = true
+ statusSupported = true
+ serviceProvider = hello.R
+ serviceType = R
+ <DataInputs>
+  [InputPoints]
+   Title = Data points
+   Abstract = The set of data points.
+   minOccurs = 1
+   maxOccurs = 1
+   <ComplexData>
+    <Default>
+     mimeType = text/xml
+     encoding = UTF-8
+     schema = http://schemas.opengis.net/gml/3.1.0/base/feature.xsd
+    </Default>
+    <Supported>
+     mimeType = text/xml
+     encoding = base64
+     schema = http://schemas.opengis.net/gml/3.1.0/base/feature.xsd
+    </Supported>
+   </ComplexData>
+ </DataInputs>
+ <DataOutputs>
+  [Result]
+   Title = Voronoi Diagram.
+   Abstract = JSON String / GML Entity of the Voronoi Diagram.
+   <ComplexOutput>
+    <Default>
+     mimeType = application/json
+     encoding = UTF-8
+    </Default>
+    <Supported>
+     mimeType = image/png
+     useMapserver = true
+     msStyle = STYLE COLOR 125 0 105 OUTLINECOLOR 0 0 0 WIDTH 0.3 OPACITY 65 END
+    </Supported>
+    <Supported>
+     mimeType = text/xml
+     encoding = base64
+     schema = http://schemas.opengis.net/gml/3.1.0/base/feature.xsd
+     useMapserver = true
+    </Supported>
+    <Supported>
+     mimeType = text/xml
+     encoding = UTF-8
+     schema = http://schemas.opengis.net/gml/3.1.0/base/feature.xsd
+     useMapserver = true
+    </Supported>
+   </ComplexOutput>
+ </DataOutputs>  
Index: /branches/prototype-v0/zoo-project/zoo-services/hello-r/cgi-env/failR.zcfg
===================================================================
--- /branches/prototype-v0/zoo-project/zoo-services/hello-r/cgi-env/failR.zcfg	(revision 873)
+++ /branches/prototype-v0/zoo-project/zoo-services/hello-r/cgi-env/failR.zcfg	(revision 873)
@@ -0,0 +1,28 @@
+[failR]
+ Title = HelloWorld Service in R
+ Abstract = Output and Hello Wolrd string
+ processVersion = 2
+ storeSupported = true
+ statusSupported = true
+ serviceProvider = hello.R
+ serviceType = R
+ <DataInputs>
+  [S]
+   Title = Name
+   Abstract = The name to display in the hello message
+   minOccurs = 0
+   maxOccurs = 1
+   <LiteralData>
+    dataType = string
+    <Default />
+   </LiteralData>
+ </DataInputs>
+ <DataOutputs>
+  [Result]
+   Title = The resulting string
+   Abstract = The string created by service.
+   <LiteralOutput>
+     dataType = string
+    <Default />
+   </LiteralOutput>
+ </DataOutputs>  
Index: /branches/prototype-v0/zoo-project/zoo-services/hello-r/cgi-env/hello.R
===================================================================
--- /branches/prototype-v0/zoo-project/zoo-services/hello-r/cgi-env/hello.R	(revision 873)
+++ /branches/prototype-v0/zoo-project/zoo-services/hello-r/cgi-env/hello.R	(revision 873)
@@ -0,0 +1,61 @@
+source("minimal.r")
+
+hellor <- function(a,b,c) {
+    # Create a useless graph
+    cars <- c(1,2,3,4,5)
+    filePath <- paste(a[["main"]][["tmpPath"]],paste("cars",zoo[["conf"]][["lenv"]][["usid"]],".png",sep=""),sep="/")
+    png(filePath)
+    plot(cars)
+    quartzFonts(avenir = c("Avenir Book", "Avenir Black", "Avenir Book Oblique", "Avenir Black Oblique"))
+    par(family = 'avenir')
+    title("Hello from R World!")
+    dev.off()
+    # Set the result
+    zoo[["outputs"]][["Result"]][["value"]] <<- paste("Hello",b[["S"]][["value"]],"from the R World! (plot ref:",filePath,")",sep=" ")
+    # Update the main conf settings
+    zoo[["conf"]][["lenv"]][["message"]] <<- "Running from R world!"
+    # Return SERVICE_SUCCEEDEED
+    return(zoo[["SERVICE_SUCCEEDEED"]])
+}
+
+failR <- function(conf,inputs,outputs){
+    zoo[["conf"]][["lenv"]][["message"]] <<- ZOOTranslate("Failed running from R world!")
+    return(zoo[["SERVICE_FAILED"]])
+}
+
+voronoipolygons <- function(x,poly) {
+  require(deldir)
+  if (.hasSlot(x, 'coords')) {
+    crds <- x@coords  
+  } else crds <- x
+  bb = bbox(poly)
+  rw = as.numeric(t(bb))
+  z <- deldir(crds[,1], crds[,2],rw=rw)
+  w <- tile.list(z)
+  polys <- vector(mode='list', length=length(w)) 
+  require(sp)
+  finalCoordinates <- c()
+  for (i in seq(along=polys)) {
+    pcrds <- cbind(w[[i]]$x, w[[i]]$y)
+    pcrds <- rbind(pcrds, pcrds[1,])
+    finalCoordinates[[i]] <- i
+    polys[[i]] <- Polygons(list(Polygon(pcrds)), ID=as.character(i))
+  }
+  SP <- SpatialPolygons(polys)
+  voronoi <- SpatialPolygonsDataFrame(SP, data=data.frame(identifier=finalCoordinates))
+  return(voronoi)
+}
+
+RVoronoi <- function(conf,inputs,outputs){
+    require(rgdal)
+    require(rgeos)
+    message(names(inputs[["InputPoints"]]))
+    message(inputs[["InputPoints"]])
+    myGeoData <- readOGR(inputs[["InputPoints"]][["cache_file"]],layer=ogrListLayers(inputs[["InputPoints"]][["cache_file"]])[1], verbose = FALSE);
+    voronoiData <- voronoipolygons(myGeoData,myGeoData)
+    filePath <- paste(conf[["main"]][["tmpPath"]],paste("RBuffer_",zoo[["conf"]][["lenv"]][["usid"]],".shp",sep=""),sep="/")
+    writeOGR(voronoiData, dsn=filePath, layer="Voronoi", driver="ESRI Shapefile", verbose = FALSE)
+    zoo[["outputs"]][["Result"]][["storage"]] <<- filePath
+    return(zoo[["SERVICE_SUCCEEDEED"]])
+}
+
Index: /branches/prototype-v0/zoo-project/zoo-services/hello-r/cgi-env/hellor.zcfg
===================================================================
--- /branches/prototype-v0/zoo-project/zoo-services/hello-r/cgi-env/hellor.zcfg	(revision 873)
+++ /branches/prototype-v0/zoo-project/zoo-services/hello-r/cgi-env/hellor.zcfg	(revision 873)
@@ -0,0 +1,28 @@
+[hellor]
+ Title = HelloWorld Service in R
+ Abstract = Output and Hello Wolrd string
+ processVersion = 2
+ storeSupported = true
+ statusSupported = true
+ serviceProvider = hello.R
+ serviceType = R
+ <DataInputs>
+  [S]
+   Title = Name
+   Abstract = The name to display in the hello message
+   minOccurs = 1
+   maxOccurs = 1
+   <LiteralData>
+    dataType = string
+    <Default />
+   </LiteralData>
+ </DataInputs>
+ <DataOutputs>
+  [Result]
+   Title = The resulting string
+   Abstract = The string created by service.
+   <LiteralOutput>
+     dataType = string
+    <Default />
+   </LiteralOutput>
+ </DataOutputs>  
Index: /branches/prototype-v0/zoo-project/zoo-services/utils/status/Makefile
===================================================================
--- /branches/prototype-v0/zoo-project/zoo-services/utils/status/Makefile	(revision 872)
+++ /branches/prototype-v0/zoo-project/zoo-services/utils/status/Makefile	(revision 873)
@@ -1,8 +1,8 @@
 ZRPATH=../../..
 include ${ZRPATH}/zoo-kernel/ZOOMakefile.opts
-CFLAGS=-I${INST_INCLUDE} ${ZOO_CFLAGS} ${XML2CFLAGS} ${GDAL_CFLAGS} ${PYTHONCFLAGS} -DLINUX_FREE_ISSUE #-DDEBUG
+CFLAGS1=${CFLAGS} -I${INST_INCLUDE} ${ZOO_CFLAGS} ${XML2CFLAGS} ${GDAL_CFLAGS} ${PYTHONCFLAGS} -DLINUX_FREE_ISSUE #-DDEBUG
 
 cgi-env/wps_status.zo: service.c
-	g++ -rdynamic ${CFLAGS} -shared -fpic -o cgi-env/wps_status.zo ./service.c ${GDAL_LIBS} ${XML2LDFLAGS} ${MACOS_LD_FLAGS} ${ZOO_LDFLAGS} ${MACOS_LD_NET_FLAGS} ${XSLT_LDFLAGS} ${FCGI_LDFLAGS} -L${ZRPATH}/zoo-kernel/ -lzoo_service
+	g++ ${CFLAGS1} -shared -fpic -o cgi-env/wps_status.zo ./service.c ${GDAL_LIBS} ${XML2LDFLAGS} ${MACOS_LD_FLAGS} ${ZOO_LDFLAGS} ${MACOS_LD_NET_FLAGS} ${XSLT_LDFLAGS} ${FCGI_LDFLAGS} -L${ZRPATH}/zoo-kernel/ -lzoo_service
 
 install:
