1 /*
2 * example.c - an example module for zsh
3 *
4 * This file is part of zsh, the Z shell.
5 *
6 * Copyright (c) 1996-1997 Zoltán Hidvégi
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 Zoltán Hidvégi 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 Zoltán Hidvégi and the Zsh Development Group have been advised of
19 * the possibility of such damage.
20 *
21 * Zoltán Hidvégi 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 Zoltán Hidvégi and the
25 * Zsh Development Group have no obligation to provide maintenance,
26 * support, updates, enhancements, or modifications.
27 *
28 */
29
30 #include "example.mdh"
31 #include "example.pro"
32
33 /* parameters */
34
35 static zlong intparam;
36 static char *strparam;
37 static char **arrparam;
38
39
40 /**/
41 static int
bin_example(char * nam,char ** args,Options ops,UNUSED (int func))42 bin_example(char *nam, char **args, Options ops, UNUSED(int func))
43 {
44 unsigned char c;
45 char **oargs = args, **p = arrparam;
46 long i = 0;
47
48 printf("Options: ");
49 for (c = 32; ++c < 128;)
50 if (OPT_ISSET(ops,c))
51 putchar(c);
52 printf("\nArguments:");
53 for (; *args; i++, args++) {
54 putchar(' ');
55 fputs(*args, stdout);
56 }
57 printf("\nName: %s\n", nam);
58 #ifdef ZSH_64_BIT_TYPE
59 printf("\nInteger Parameter: %s\n", output64(intparam));
60 #else
61 printf("\nInteger Parameter: %ld\n", intparam);
62 #endif
63 printf("String Parameter: %s\n", strparam ? strparam : "");
64 printf("Array Parameter:");
65 if (p)
66 while (*p) printf(" %s", *p++);
67 printf("\n");
68
69 intparam = i;
70 zsfree(strparam);
71 strparam = ztrdup(*oargs ? *oargs : "");
72 if (arrparam)
73 freearray(arrparam);
74 arrparam = zarrdup(oargs);
75 return 0;
76 }
77
78 /**/
79 static int
cond_p_len(char ** a,UNUSED (int id))80 cond_p_len(char **a, UNUSED(int id))
81 {
82 char *s1 = cond_str(a, 0, 0);
83
84 if (a[1]) {
85 zlong v = cond_val(a, 1);
86
87 return strlen(s1) == v;
88 } else {
89 return !s1[0];
90 }
91 }
92
93 /**/
94 static int
cond_i_ex(char ** a,UNUSED (int id))95 cond_i_ex(char **a, UNUSED(int id))
96 {
97 char *s1 = cond_str(a, 0, 0), *s2 = cond_str(a, 1, 0);
98
99 return !strcmp("example", dyncat(s1, s2));
100 }
101
102 /**/
103 static mnumber
math_sum(UNUSED (char * name),int argc,mnumber * argv,UNUSED (int id))104 math_sum(UNUSED(char *name), int argc, mnumber *argv, UNUSED(int id))
105 {
106 mnumber ret;
107 int f = 0;
108
109 ret.u.l = 0;
110 while (argc--) {
111 if (argv->type == MN_INTEGER) {
112 if (f)
113 ret.u.d += (double) argv->u.l;
114 else
115 ret.u.l += argv->u.l;
116 } else {
117 if (f)
118 ret.u.d += argv->u.d;
119 else {
120 ret.u.d = ((double) ret.u.l) + ((double) argv->u.d);
121 f = 1;
122 }
123 }
124 argv++;
125 }
126 ret.type = (f ? MN_FLOAT : MN_INTEGER);
127
128 return ret;
129 }
130
131 /**/
132 static mnumber
math_length(UNUSED (char * name),char * arg,UNUSED (int id))133 math_length(UNUSED(char *name), char *arg, UNUSED(int id))
134 {
135 mnumber ret;
136
137 ret.type = MN_INTEGER;
138 ret.u.l = strlen(arg);
139
140 return ret;
141 }
142
143 /**/
144 static int
ex_wrapper(Eprog prog,FuncWrap w,char * name)145 ex_wrapper(Eprog prog, FuncWrap w, char *name)
146 {
147 if (strncmp(name, "example", 7))
148 return 1;
149 else {
150 int ogd = opts[GLOBDOTS];
151
152 opts[GLOBDOTS] = 1;
153 runshfunc(prog, w, name);
154 opts[GLOBDOTS] = ogd;
155
156 return 0;
157 }
158 }
159
160 /*
161 * boot_ is executed when the module is loaded.
162 */
163
164 static struct builtin bintab[] = {
165 BUILTIN("example", 0, bin_example, 0, -1, 0, "flags", NULL),
166 };
167
168 static struct conddef cotab[] = {
169 CONDDEF("ex", CONDF_INFIX, cond_i_ex, 0, 0, 0),
170 CONDDEF("len", 0, cond_p_len, 1, 2, 0),
171 };
172
173 static struct paramdef patab[] = {
174 ARRPARAMDEF("exarr", &arrparam),
175 INTPARAMDEF("exint", &intparam),
176 STRPARAMDEF("exstr", &strparam),
177 };
178
179 static struct mathfunc mftab[] = {
180 STRMATHFUNC("length", math_length, 0),
181 NUMMATHFUNC("sum", math_sum, 1, -1, 0),
182 };
183
184 static struct funcwrap wrapper[] = {
185 WRAPDEF(ex_wrapper),
186 };
187
188 static struct features module_features = {
189 bintab, sizeof(bintab)/sizeof(*bintab),
190 cotab, sizeof(cotab)/sizeof(*cotab),
191 mftab, sizeof(mftab)/sizeof(*mftab),
192 patab, sizeof(patab)/sizeof(*patab),
193 0
194 };
195
196 /**/
197 int
setup_(UNUSED (Module m))198 setup_(UNUSED(Module m))
199 {
200 printf("The example module has now been set up.\n");
201 fflush(stdout);
202 return 0;
203 }
204
205 /**/
206 int
features_(Module m,char *** features)207 features_(Module m, char ***features)
208 {
209 *features = featuresarray(m, &module_features);
210 return 0;
211 }
212
213 /**/
214 int
enables_(Module m,int ** enables)215 enables_(Module m, int **enables)
216 {
217 return handlefeatures(m, &module_features, enables);
218 }
219
220 /**/
221 int
boot_(Module m)222 boot_(Module m)
223 {
224 intparam = 42;
225 strparam = ztrdup("example");
226 arrparam = (char **) zalloc(3 * sizeof(char *));
227 arrparam[0] = ztrdup("example");
228 arrparam[1] = ztrdup("array");
229 arrparam[2] = NULL;
230 return addwrapper(m, wrapper);
231 }
232
233 /**/
234 int
cleanup_(Module m)235 cleanup_(Module m)
236 {
237 deletewrapper(m, wrapper);
238 return setfeatureenables(m, &module_features, NULL);
239 }
240
241 /**/
242 int
finish_(UNUSED (Module m))243 finish_(UNUSED(Module m))
244 {
245 printf("Thank you for using the example module. Have a nice day.\n");
246 fflush(stdout);
247 return 0;
248 }
249