zsh-workers
 help / color / mirror / code / Atom feed
* avoid $status and $options in POSIX mode
@ 2010-09-17 17:33 Eric Blake
  2010-09-18  3:25 ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Eric Blake @ 2010-09-17 17:33 UTC (permalink / raw)
  To: zsh-workers

POSIX states "The name space of environment variable names containing 
lowercase letters is reserved for applications. Applications can define 
any environment variables with names from this name space without 
modifying the behavior of the standard utilities."
http://www.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08

Yet, the existence of zsh-magic variables like $status and $options 
infringe upon the right of a user's script to use this namespace for 
their own desires.  For example, see this recent autoconf patch:
http://git.sv.gnu.org/cgit/autoconf.git/commit/?id=23a2c336

It would be really nice if 'emulate sh' could disable zsh magic handling 
of any variables that infringe upon the lower-case namespace reserved 
for applications.

-- 
Eric Blake   eblake@redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: avoid $status and $options in POSIX mode
  2010-09-17 17:33 avoid $status and $options in POSIX mode Eric Blake
@ 2010-09-18  3:25 ` Bart Schaefer
  2010-09-18  7:25   ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2010-09-18  3:25 UTC (permalink / raw)
  To: Eric Blake, zsh-workers

On Sep 17, 11:33am, Eric Blake wrote:
} Subject: avoid $status and $options in POSIX mode
}
} Yet, the existence of zsh-magic variables like $status and $options 
} infringe upon the right of a user's script to use this namespace for 
} their own desires.  For example, see this recent autoconf patch:
} http://git.sv.gnu.org/cgit/autoconf.git/commit/?id=23a2c336

That patch is incorrect (or maybe only partly correct).  The "options"
variable is only defined if the zsh/parameter module has been loaded,
which it won't be when the shell is started as "sh".  The "status"
variable is also disabled when the shell is started as "sh".

If autoconf is running into problems with $options, the maintainers
need to find out why zsh/parameter is being loaded in the first place.
Is the shell run by the name "zsh" and *then* switched to emulation?
In that event, module autoloads will still be in effect, so $options
will load itself.

The zsh manual says:

In `sh' and `ksh' compatibility modes the following parameters are not
special and not initialized by the shell: ARGC, argv, cdpath, fignore,
fpath, HISTCHARS, mailpath, MANPATH, manpath, path, prompt, PROMPT,
PROMPT2, PROMPT3, PROMPT4, psvar, status, watch.

} It would be really nice if 'emulate sh' could disable zsh magic handling 
} of any variables that infringe upon the lower-case namespace reserved 
} for applications.

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.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: avoid $status and $options in POSIX mode
  2010-09-18  3:25 ` Bart Schaefer
@ 2010-09-18  7:25   ` Bart Schaefer
  2010-09-19 21:39     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2010-09-18  7:25 UTC (permalink / raw)
  To: Eric Blake, zsh-workers

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"


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: avoid $status and $options in POSIX mode
  2010-09-18  7:25   ` Bart Schaefer
@ 2010-09-19 21:39     ` Bart Schaefer
  2010-09-20  8:45       ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2010-09-19 21:39 UTC (permalink / raw)
  To: zsh-workers

On Sep 18, 12:25am, Bart Schaefer wrote:
}
} I'm not entirely sure there aren't side effects of [the proposed
} autoconf/posixshell module] making the global parameter scope deeper

The worst effect I've found is that loading the module from inside a
function makes it appear that function scope has never ended.  This
means the module must refuse to load except at top level, or at least
behave differently in a function.

Misc. mostly harmless things ...

- An extra prompt is printed after "exit" when interactive
- The warn_create_global option reports all undeclared parameters
- Pushed history with "fc -a -p" is popped on module unload (feature!)
- Diagnostics print line numbers as if in a function body

} 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.

The most obvious solution to this seems to be to walk the parameter
table looking for PM_AUTOLOAD and mask any parameter that has it.
Anyone have better suggestions?

-- 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: avoid $status and $options in POSIX mode
  2010-09-19 21:39     ` Bart Schaefer
@ 2010-09-20  8:45       ` Peter Stephenson
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2010-09-20  8:45 UTC (permalink / raw)
  To: zsh-workers

On Sun, 19 Sep 2010 14:39:43 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:
> } 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.
> 
> The most obvious solution to this seems to be to walk the parameter
> table looking for PM_AUTOLOAD and mask any parameter that has it.
> Anyone have better suggestions?

I don't see any real problem with this one.  There's plenty of code that
does this already.

-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK



Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-09-20  8:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-17 17:33 avoid $status and $options in POSIX mode Eric Blake
2010-09-18  3:25 ` Bart Schaefer
2010-09-18  7:25   ` Bart Schaefer
2010-09-19 21:39     ` Bart Schaefer
2010-09-20  8:45       ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).