Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: avoid $status and $options in POSIX mode



On Sep 17,  8:25pm, Bart Schaefer wrote:
}
} Starting the shell as zsh and then running "emulate" is not the same as
} compatibility mode, and is unlikely ever to be; "emulate" is meant to
} switch back and forth with minimal loss of state, not to entirely wipe
} the slate and become another shell.

However, it occurs to me that's no reason not to provide a way to wipe
the slate ...

I've probably forgotten something here, but this should give the basic
idea.  This creates a module named "autoconf/posixshell" (it could be
called anything, including "posix/shell" ... just tweak posixshell.mdd)
which sets up a parameter scope (as if entering a shell function) and
predeclares variables to mask all those that, according to the manual
(and params.c) are not supposed to be used when not in native mode.
Then it runs "emulate -R sh".

When the module is unloaded, it restores everything, very much like
leaving a function scope.  I'm not entirely sure there aren't side
effects of making the global parameter scope deeper, but I've tried
a few simple tests with it and it seems to work as expected.

There are probably other things that could be done here -- for example,
hiding or overriding various builtins (though if you ever want to back
out again you'd better not hide "zmodload") -- and of course the print
statements could go away.  Oh, a fairly large omission at this point
is that it doesn't disable autoloads from other modules, so this does
not yet solve the problem of $options manifesting itself.  However,
the upshot of this is, with a module like this installed in the proper
location, autoconf could replace "emulate sh" (or whatever it's
doing now) with "zmodload autoconf/posixshell" and thereby assure
itself of whatever environment it needed.

Aside:  The module docs in INSTALL and zsh-development-guide could
really use some fixing up.  In particular they should mention that if
configure has been run with --disable-dynamic then the modules whose
mdd files say "dynamic" are never so much as compiled.  That had me
confused for a bit about why my newly-added module was not built.


--- /dev/null	2010-08-30 09:03:23.520307808 -0700
+++ posixshell.c	2010-09-17 23:44:20.000000000 -0700
@@ -0,0 +1,126 @@
+/*
+ * posixshell.c - a POSIX helper module for zsh
+ *
+ * This file is part of zsh, the Z shell.
+ *
+ * Copyright (c) 2010 Bart Schaefer
+ * All rights reserved.
+ *
+ * Permission is hereby granted, without written agreement and without
+ * license or royalty fees, to use, copy, modify, and distribute this
+ * software and to distribute modified versions of this software for any
+ * purpose, provided that the above copyright notice and the following
+ * two paragraphs appear in all copies of this software.
+ *
+ * In no event shall Bart Schaefer or the Zsh Development Group be liable
+ * to any party for direct, indirect, special, incidental, or consequential
+ * damages arising out of the use of this software and its documentation,
+ * even if Bart Schaefer and the Zsh Development Group have been advised of
+ * the possibility of such damage.
+ *
+ * Bart Schaefer and the Zsh Development Group specifically disclaim any
+ * warranties, including, but not limited to, the implied warranties of
+ * merchantability and fitness for a particular purpose.  The software
+ * provided hereunder is on an "as is" basis, and Bart Schaefer and the
+ * Zsh Development Group have no obligation to provide maintenance,
+ * support, updates, enhancements, or modifications.
+ *
+ */
+
+#include "posixshell.mdh"
+#include "posixshell.pro"
+
+static char saveopts[OPT_SIZE];
+static int saveemulation;
+
+/**/
+int
+setup_(UNUSED(Module m))
+{
+    /* Copied from doshfunc() */
+    memcpy(saveopts, opts, sizeof(opts));
+    saveemulation = emulation;
+
+    printf("Entering POSIX emulation.\n");
+    fflush(stdout);
+
+    return 0;
+}
+
+static struct features module_features = {
+    0
+};
+
+/**/
+int
+features_(Module m, char ***features)
+{
+    *features = featuresarray(m, &module_features);
+    return 0;
+}
+
+/**/
+int
+enables_(Module m, int **enables)
+{
+    return 0;
+}
+
+/**/
+int
+boot_(Module m)
+{
+    startparamscope();
+
+    createparam("ARGC", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("HISTCHARS", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("histchars", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("status", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("prompt", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("PROMPT", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("PROMPT2", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("PROMPT3", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("PROMPT4", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("MANPATH", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("argv", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("fignore", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("cdpath", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("fpath", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("mailpath", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("manpath", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("psvar", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("watch", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("zsh_eval_context", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("module_path", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("path", PM_HIDE|PM_LOCAL|PM_UNSET);
+    createparam("pipestatus", PM_HIDE|PM_LOCAL|PM_UNSET);
+
+    execstring("emulate -R sh", 1, 0, "POSIX");
+    errflag = lastval = 0;
+
+    return 0;
+}
+
+/**/
+int
+cleanup_(Module m)
+{
+    endparamscope();
+    return 0;
+}
+
+/**/
+int
+finish_(UNUSED(Module m))
+{
+    printf("Leaving POSIX emulation.\n");
+    fflush(stdout);
+
+    /* Copied from doshfunc() */
+    saveopts[PRIVILEGED] = opts[PRIVILEGED];
+    saveopts[RESTRICTED] = opts[RESTRICTED];
+    memcpy(opts, saveopts, sizeof(opts));
+    emulation = saveemulation;
+
+    return 0;
+}
--- /dev/null	2010-08-30 09:03:23.520307808 -0700
+++ posixshell.mdd	2010-09-17 23:28:47.000000000 -0700
@@ -0,0 +1,5 @@
+name=autoconf/posixshell
+link=dynamic
+load=no
+
+objects="posixshell.o"



Messages sorted by: Reverse Date, Date, Thread, Author