diff --git a/src/env/env.c b/src/env/env.c
index c1f1de8..d994dd9 100644
--- a/src/env/env.c
+++ b/src/env/env.c
@@ -242,4 +242,65 @@ int glp_free_env(void)
       return 0;
 }
 
+/***********************************************************************
+*  NAME
+*
+*  push_env - save a copy of the environment block
+*
+*  SYNOPSIS
+*
+*  #include "env.h"
+*  void push_env(ENV *ctx);
+*
+*  DESCRIPTION
+*
+*  The routine push_env saves a copy of the environment block to the
+*  memory pointed by ctx to be used for error recovery */
+
+void push_env(ENV *ctx)
+{     ENV *env = get_env_ptr();
+      *ctx = *env;
+}
+
+/***********************************************************************
+*  NAME
+*
+*  pop_env - restore state using an earlier environment block copy
+*
+*  SYNOPSIS
+*
+*  #include "env.h"
+*  void pop_env(ENV *ctx);
+*
+*  DESCRIPTION
+*
+*  The routine pop_env uses an earlier copy of the environment block
+*  to restore the previous error hook and terminal output state. In
+*  case of error, any memory allocated after the previous environment
+*  block was saved is freed and the error state is cleared */
+
+void pop_env(ENV *ctx)
+{     ENV *env = get_env_ptr();
+      if (env->err_st)
+      {  MBD *ptr;
+         xprintf("Recovering outer context\n");
+         env->err_st = 0;
+         env->err_file = NULL;
+         env->err_line = 0;
+         env->err_buf[0] = '\0';
+         while(env->mem_ptr != ctx->mem_ptr)
+         {  ptr = env->mem_ptr;
+            env->mem_ptr = ptr->next;
+            env->mem_total -= ptr->size;
+            free(ptr);
+            env->mem_count--;
+         }
+         if (env->mem_ptr)
+            env->mem_ptr->prev = NULL;
+      }
+      env->err_hook = ctx->err_hook;
+      env->err_info = ctx->err_info;
+      env->term_out = ctx->term_out;
+}
+
 /* eof */
diff --git a/src/env/env.h b/src/env/env.h
index 7c0504a..5df3ade 100644
--- a/src/env/env.h
+++ b/src/env/env.h
@@ -126,6 +126,14 @@ struct MBD
 ENV *get_env_ptr(void);
 /* retrieve pointer to environment block */
 
+#define push_env _glp_push_env
+void push_env(ENV *ctx);
+/* save a copy of the environment block */
+
+#define pop_env _glp_pop_env
+void pop_env(ENV *ctx);
+/* restore state using an earlier environment block copy */
+
 #define tls_set_ptr _glp_tls_set_ptr
 void tls_set_ptr(void *ptr);
 /* store global pointer in TLS */
