zsh-workers
 help / color / mirror / code / Atom feed
728559fc9d522195bcb4828c55133884be0759f9 blob 5577 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
 
/*
 * param_persistent.c - bindings for persistent parameter scopes
 *
 * This file is part of zsh, the Z shell.
 *
 * Copyright (c) 2015 Barton E. 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 Barton E. 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 Barton E. Schaefer and the Zsh
 * Development Group have been advised of the possibility of such damage.
 *
 * Barton E. 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
 * Barton E. Schaefer and the Zsh Development Group have no
 * obligation to provide maintenance, support, updates, enhancements, or
 * modifications.
 *
 */

#include "param_persistent.mdh"
#include "param_persistent.pro"

/*
 * The trick here is:
 *
 * bin_persistent() calls makepersistent() on its "args" array.  It
 * is not a keyword, so the "assigns" list should be empty.
 *
 * makepersistent() walks the array of arguments and, for each name,
 * pastes up a ".persistent_FUNC.name" string where FUNC is the name
 * of the function that invoked "persistent".  It tests whether there
 * is already a parameter having that name.  If not, the name in the
 * assign struct is changed to this name.  A new assignment of the
 * original name to this new name is placed in a new list.  The new
 * list of assignments thus created is returned.
 *
 * Assignments for which the persistent name already exists are dropped,
 * so the parameter retains its flags and value from the previous call.
 *
 * bin_persistent() then calls bin_typeset() on the new list with opts
 * arranged so a to create named references.  If this succeeds, it calls
 * bin_typeset() again on the rewritten array to handle creation of any
 * actual parameters to which the named references (the original names
 * to be made persistent) need to point.
 *
 * The end result is that after the first call to "persistent" there is
 * a global variable in a hidden namespace to which each persistent name
 * is a reference.  Thereafter, everything happens via the named reference.
 */

static int makepersistent_error = 0;

/**/
static Asgment
cpyasg(char ***argvp)
{
    char *s = **argvp, *v = NULL, *new;
    Asgment a;

    /* Like builtin.c:getasg() without assigns */
    if (!s || !*s)
	return NULL;
    if (*s == '=') {
	makepersistent_error = 1;
	return NULL;
    }

    a = (Asgment)zhalloc(sizeof(struct asgment));
    a->name = s;
    a->flags = 0;
    new = zhtricat(".persistent_",argzero,".");    

    for (; *s && *s != '='; s++);
    if (*s) {
	*s = '\0';
	v = s + 1;
    }
    a->value.scalar = new = dyncat(new, **argvp);

    if (is_persistent(new))
	for (char **argv = *argvp; *argv; argv++)
	    argv[0] = argv[1];
    else {
	if (v)
	    **argvp = zhtricat(new, "=", v);
	else
	    **argvp = dupstring(new);
	(*argvp)++;
    }
    return a;
}

/**/
static LinkList
makepersistent(char **argv)
{
    LinkList persist = newlinklist();
    char ***argvp = &argv;
    Asgment asg;

    while (**argvp) {
	if ((asg = cpyasg(argvp)))
	    uaddlinknode(persist, &asg->node);
    }

    return persist;
}

/**/
static int
is_persistent(const char *name)
{
    return (!strncmp(name, ".persistent_", 12) &&
	    !!paramtab->getnode(paramtab, name));
}

/**/
static int
bin_persistent(char *nam, char **args, LinkList assigns, Options ops, int func)
{
    int from_typeset = 0;
    int save_llevel = locallevel;
    makepersistent_error = 0;

    if (*itype_end(argzero, IIDENT, 0)) {
	zwarnnam("persistent", "invalid scope: %s", argzero);
	return 1;
    }

    LinkList namerefs = makepersistent(args);
    if (makepersistent_error)
	return 1;
    if (*args) {
	queue_signals();
	locallevel = 0;	/* Does this work? */
	from_typeset = bin_typeset(nam, args, assigns, ops, func);
	locallevel = save_llevel;
	unqueue_signals();
    }
    if (!from_typeset) {
	*args = 0;
	memset((void *)ops, 0, sizeof(struct options));
	ops->ind['n'] = ops->ind['H'] = 1;
	return bin_typeset(nam, args, namerefs, ops, func);
    }

    return makepersistent_error | from_typeset;
}

/*
 * Standard module configuration/linkage
 */

static struct builtin bintab[] = {
    /* Copied from BUILTIN("local"), removed "m" and "p" */
    BUILTIN("persistent", BINF_MAGICEQUALS | BINF_ASSIGN, (HandlerFunc)bin_persistent, 0, -1, 0, "AE:%F:%HL:%PR:%TUZ:%ahi:%lnrtux", NULL)
};

static struct features module_features = {
    bintab, sizeof(bintab)/sizeof(*bintab),
    NULL, 0,
    NULL, 0,
    NULL, 0,
    0
};

/**/
int
setup_(UNUSED(Module m))
{
    return 0;
}

/**/
int
features_(Module m, char ***features)
{
    *features = featuresarray(m, &module_features);
    return 0;
}

/**/
int
enables_(Module m, int **enables)
{
    return handlefeatures(m, &module_features, enables);
}

/**/
int
boot_(Module m)
{
    return 0;
}

/**/
int
cleanup_(Module m)
{
    return setfeatureenables(m, &module_features, NULL);
}

/**/
int
finish_(UNUSED(Module m))
{
    return 0;
}
debug log:

solving 728559fc9 ...
found 728559fc9 in https://inbox.vuxu.org/zsh-workers/CAH+w=7b6e7ucWDt8hpJNbNQP5M2sYm8-5kERaF=hNDwiuUkt-g@mail.gmail.com/

applying [1/1] https://inbox.vuxu.org/zsh-workers/CAH+w=7b6e7ucWDt8hpJNbNQP5M2sYm8-5kERaF=hNDwiuUkt-g@mail.gmail.com/
diff --git a/Src/Modules/param_persistent.c b/Src/Modules/param_persistent.c
new file mode 100644
index 000000000..728559fc9

1:88: trailing whitespace.
    new = zhtricat(".persistent_",argzero,".");    
Checking patch Src/Modules/param_persistent.c...
Applied patch Src/Modules/param_persistent.c cleanly.
warning: 1 line adds whitespace errors.

index at:
100644 728559fc9d522195bcb4828c55133884be0759f9	Src/Modules/param_persistent.c

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