1/*
2 * zleparameter.c - parameter interface to zle internals
3 *
4 * This file is part of zsh, the Z shell.
5 *
6 * Copyright (c) 1999 Sven Wischnowsky
7 * All rights reserved.
8 *
9 * Permission is hereby granted, without written agreement and without
10 * license or royalty fees, to use, copy, modify, and distribute this
11 * software and to distribute modified versions of this software for any
12 * purpose, provided that the above copyright notice and the following
13 * two paragraphs appear in all copies of this software.
14 *
15 * In no event shall Sven Wischnowsky or the Zsh Development Group be liable
16 * to any party for direct, indirect, special, incidental, or consequential
17 * damages arising out of the use of this software and its documentation,
18 * even if Sven Wischnowsky and the Zsh Development Group have been advised of
19 * the possibility of such damage.
20 *
21 * Sven Wischnowsky and the Zsh Development Group specifically disclaim any
22 * warranties, including, but not limited to, the implied warranties of
23 * merchantability and fitness for a particular purpose.  The software
24 * provided hereunder is on an "as is" basis, and Sven Wischnowsky and the
25 * Zsh Development Group have no obligation to provide maintenance,
26 * support, updates, enhancements, or modifications.
27 *
28 */
29
30#include "zleparameter.mdh"
31#include "zleparameter.pro"
32
33/* Functions for the zlewidgets special parameter. */
34
35/**/
36static char *
37widgetstr(Widget w)
38{
39    if (!w)
40	return dupstring("undefined");
41    if (w->flags & WIDGET_INT)
42	return dupstring("builtin");
43    if (w->flags & WIDGET_NCOMP) {
44	char *t = (char *) zhalloc(13 + strlen(w->u.comp.wid) +
45				   strlen(w->u.comp.func));
46
47	strcpy(t, "completion:");
48	strcat(t, w->u.comp.wid);
49	strcat(t, ":");
50	strcat(t, w->u.comp.func);
51
52	return t;
53    }
54    return dyncat("user:", w->u.fnnam);
55}
56
57/**/
58static HashNode
59getpmwidgets(UNUSED(HashTable ht), const char *name)
60{
61    Param pm = NULL;
62    Thingy th;
63
64    pm = (Param) hcalloc(sizeof(struct param));
65    pm->node.nam = dupstring(name);
66    pm->node.flags = PM_SCALAR | PM_READONLY;
67    pm->gsu.s = &nullsetscalar_gsu;
68
69    if ((th = (Thingy) thingytab->getnode(thingytab, name)) &&
70	!(th->flags & DISABLED))
71	pm->u.str = widgetstr(th->widget);
72    else {
73	pm->u.str = dupstring("");
74	pm->node.flags |= PM_UNSET;
75    }
76    return &pm->node;
77}
78
79/**/
80static void
81scanpmwidgets(UNUSED(HashTable ht), ScanFunc func, int flags)
82{
83    struct param pm;
84    int i;
85    HashNode hn;
86
87    memset((void *)&pm, 0, sizeof(struct param));
88    pm.node.flags = PM_SCALAR | PM_READONLY;
89    pm.gsu.s = &nullsetscalar_gsu;
90
91    for (i = 0; i < thingytab->hsize; i++)
92	for (hn = thingytab->nodes[i]; hn; hn = hn->next) {
93	    pm.node.nam = hn->nam;
94	    if (func != scancountparams &&
95		((flags & (SCANPM_WANTVALS|SCANPM_MATCHVAL)) ||
96		 !(flags & SCANPM_WANTKEYS)))
97		pm.u.str = widgetstr(((Thingy) hn)->widget);
98	    func(&pm.node, flags);
99	}
100}
101
102/* Functions for the zlekeymaps special parameter. */
103
104static char **
105keymapsgetfn(UNUSED(Param pm))
106{
107    int i;
108    HashNode hn;
109    char **ret, **p;
110
111    p = ret = (char **) zhalloc((keymapnamtab->ct + 1) * sizeof(char *));
112
113    for (i = 0; i < keymapnamtab->hsize; i++)
114	for (hn = keymapnamtab->nodes[i]; hn; hn = hn->next)
115	    *p++ = dupstring(hn->nam);
116    *p = NULL;
117
118    return ret;
119}
120
121/*
122 * This is a duplicate of stdhash_gsu.  On some systems
123 * (such as Cygwin) we can't put a pointer to an imported variable
124 * in a compile-time initialiser, so we use this instead.
125 */
126static const struct gsu_hash zlestdhash_gsu =
127{ hashgetfn, hashsetfn, stdunsetfn };
128static const struct gsu_array keymaps_gsu =
129{ keymapsgetfn, arrsetfn, stdunsetfn };
130
131static struct paramdef partab[] = {
132    SPECIALPMDEF("keymaps", PM_ARRAY|PM_READONLY, &keymaps_gsu, NULL, NULL),
133    SPECIALPMDEF("widgets", PM_READONLY,
134		 &zlestdhash_gsu, getpmwidgets, scanpmwidgets)
135};
136
137static struct features module_features = {
138    NULL, 0,
139    NULL, 0,
140    NULL, 0,
141    partab, sizeof(partab)/sizeof(*partab),
142    0
143};
144
145/**/
146int
147setup_(UNUSED(Module m))
148{
149    return 0;
150}
151
152/**/
153int
154features_(Module m, char ***features)
155{
156    *features = featuresarray(m, &module_features);
157    return 0;
158}
159
160/**/
161int
162enables_(Module m, int **enables)
163{
164    return handlefeatures(m, &module_features, enables);
165}
166
167/**/
168int
169boot_(UNUSED(Module m))
170{
171    return 0;
172}
173
174/**/
175int
176cleanup_(Module m)
177{
178    return setfeatureenables(m, &module_features, NULL);
179}
180
181/**/
182int
183finish_(UNUSED(Module m))
184{
185    return 0;
186}
187