2 * example.c - an example module for zsh
4 * This file is part of zsh, the Z shell.
6 * Copyright (c) 1996-1997 Zoltán Hidvégi
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.
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.
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.
30 #include "example.mdh"
31 #include "example.pro"
35 static zlong intparam
;
36 static char *strparam
;
37 static char **arrparam
;
42 bin_example(char *nam
, char **args
, Options ops
, UNUSED(int func
))
45 char **oargs
= args
, **p
= arrparam
;
49 for (c
= 32; ++c
< 128;)
52 printf("\nArguments:");
53 for (; *args
; i
++, args
++) {
57 printf("\nName: %s\n", nam
);
58 #ifdef ZSH_64_BIT_TYPE
59 printf("\nInteger Parameter: %s\n", output64(intparam
));
61 printf("\nInteger Parameter: %ld\n", intparam
);
63 printf("String Parameter: %s\n", strparam
? strparam
: "");
64 printf("Array Parameter:");
66 while (*p
) printf(" %s", *p
++);
71 strparam
= ztrdup(*oargs
? *oargs
: "");
73 arrparam
= zarrdup(oargs
);
79 cond_p_len(char **a
, UNUSED(int id
))
81 char *s1
= cond_str(a
, 0, 0);
84 zlong v
= cond_val(a
, 1);
86 return strlen(s1
) == v
;
94 cond_i_ex(char **a
, UNUSED(int id
))
96 char *s1
= cond_str(a
, 0, 0), *s2
= cond_str(a
, 1, 0);
98 return !strcmp("example", dyncat(s1
, s2
));
103 math_sum(UNUSED(char *name
), int argc
, mnumber
*argv
, UNUSED(int id
))
110 if (argv
->type
== MN_INTEGER
) {
112 ret
.u
.d
+= (double) argv
->u
.l
;
114 ret
.u
.l
+= argv
->u
.l
;
117 ret
.u
.d
+= argv
->u
.d
;
119 ret
.u
.d
= ((double) ret
.u
.l
) + ((double) argv
->u
.d
);
125 ret
.type
= (f
? MN_FLOAT
: MN_INTEGER
);
132 math_length(UNUSED(char *name
), char *arg
, UNUSED(int id
))
136 ret
.type
= MN_INTEGER
;
137 ret
.u
.l
= strlen(arg
);
144 ex_wrapper(Eprog prog
, FuncWrap w
, char *name
)
146 if (strncmp(name
, "example", 7))
149 int ogd
= opts
[GLOBDOTS
];
152 runshfunc(prog
, w
, name
);
153 opts
[GLOBDOTS
] = ogd
;
160 * boot_ is executed when the module is loaded.
163 static struct builtin bintab
[] = {
164 BUILTIN("example", 0, bin_example
, 0, -1, 0, "flags", NULL
),
167 static struct conddef cotab
[] = {
168 CONDDEF("ex", CONDF_INFIX
, cond_i_ex
, 0, 0, 0),
169 CONDDEF("len", 0, cond_p_len
, 1, 2, 0),
172 static struct paramdef patab
[] = {
173 ARRPARAMDEF("exarr", &arrparam
),
174 INTPARAMDEF("exint", &intparam
),
175 STRPARAMDEF("exstr", &strparam
),
178 static struct mathfunc mftab
[] = {
179 STRMATHFUNC("length", math_length
, 0),
180 NUMMATHFUNC("sum", math_sum
, 1, -1, 0),
183 static struct funcwrap wrapper
[] = {
187 static struct features module_features
= {
188 bintab
, sizeof(bintab
)/sizeof(*bintab
),
189 cotab
, sizeof(cotab
)/sizeof(*cotab
),
190 mftab
, sizeof(mftab
)/sizeof(*mftab
),
191 patab
, sizeof(patab
)/sizeof(*patab
),
197 setup_(UNUSED(Module m
))
199 printf("The example module has now been set up.\n");
206 features_(Module m
, char ***features
)
208 *features
= featuresarray(m
, &module_features
);
214 enables_(Module m
, int **enables
)
216 return handlefeatures(m
, &module_features
, enables
);
224 strparam
= ztrdup("example");
225 arrparam
= (char **) zalloc(3 * sizeof(char *));
226 arrparam
[0] = ztrdup("example");
227 arrparam
[1] = ztrdup("array");
229 return addwrapper(m
, wrapper
);
236 deletewrapper(m
, wrapper
);
237 return setfeatureenables(m
, &module_features
, NULL
);
242 finish_(UNUSED(Module m
))
244 printf("Thank you for using the example module. Have a nice day.\n");