zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: vlkr1 <vlkr1@proton.me>
Cc: "zsh-workers@zsh.org" <zsh-workers@zsh.org>
Subject: Re: How does zsh internally store and process variables?
Date: Sat, 27 Jan 2024 15:04:59 -0800	[thread overview]
Message-ID: <CAH+w=7avCMYHweuvvrEMMVpOV-1WDsSZnpXjG8fF1Br_pqN3cQ@mail.gmail.com> (raw)
In-Reply-To: <NiMjlpZnB4EgNmxh7Uv-4KJ9SgAs8OUvZv-AvH_sM4zr87dCd20KX4GnHqMvKPMFhMkb9pcRGpmgPnEPvrEywnF7OXWfMxfsR0QLmlHraOo=@proton.me>

On Sat, Jan 27, 2024 at 2:00 PM vlkr1 <vlkr1@proton.me> wrote:
>
> One of the things that we are having trouble with are internal variables.

Have you looked at Etc/zsh-development-guide in the zsh distribution?

There's a section on "Parameters" and how modules should make use of
them.  It's mostly focused on how modules can add their own parameters
rather than on how to access existing parameters, but the basics of
the structures and accessors are all explained (albeit with pointers
to zsh.h for structure definitions).

As a side-effect of evolutionary development, zsh uses a single global
pointer "paramtab" for most of the get/set functions, so it's not
threading-safe code and signal handling has to be carefully managed.
A second global pointer "realparamtab" points to the actual table for
all the variables, so you'll see tests for "paramtab == realparamtab"
in various places.  Internally, associative array key/value pairs are
implemented as parameter tables, so paramtab may point to the
internals of an associative array in some cases, where the associative
array itself is contained in realparamtab (we don't presently allow
them to nest).

In the current development git head, the files
Src/Modules/param_private.c and Src/Modules/ksh93.c may provide some
useful coding hints, although neither is exactly what you're after.

> how its variables are exported (setenv?)

There's a preprocessor definitions for HAVE_PUTENV and
USE_SET_UNSET_ENV.  Primarily these apply in the zgetenv(), zputenv(),
and delenv() functions defined in Src/params.c which are what you
should call, but there are lower-level functions if you really need
them or you can use #ifdef with those macros to choose the system
library routines.

> If the keys and values are copied or expected to be leaked/alloccd with malloc by the caller

Keys are managed by the functions in Src/params.c, so you can pass
constant strings e.g. as the first argument of setsparam() or the
other parameter functions.  String values (second parameter of
setsparam()) should be allocated by the caller but are freed by
Src/params.c when the value changes or is unset.  Values represented
by numeric types (int, float) are not memory-managed.

String values also need to be "metafied".  If you are copying them
from another parameter, they are likely already metafied.  If coming
from user input or the like, use for example ztrdup_metafy() to
allocate a metafied copy for setsparam().


      reply	other threads:[~2024-01-27 23:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-27 22:00 vlkr1
2024-01-27 23:04 ` Bart Schaefer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7avCMYHweuvvrEMMVpOV-1WDsSZnpXjG8fF1Br_pqN3cQ@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=vlkr1@proton.me \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).