|
1 // example.c - an example module for zsh |
|
2 // |
|
3 // © Portions Copyright (c) Symbian Software Ltd 2007. All rights reserved. |
|
4 // |
|
5 /* |
|
6 * This file is part of zsh, the Z shell. |
|
7 * |
|
8 * Copyright (c) 1996-1997 Zoltán Hidvégi |
|
9 * All rights reserved. |
|
10 * |
|
11 * Permission is hereby granted, without written agreement and without |
|
12 * license or royalty fees, to use, copy, modify, and distribute this |
|
13 * software and to distribute modified versions of this software for any |
|
14 * purpose, provided that the above copyright notice and the following |
|
15 * two paragraphs appear in all copies of this software. |
|
16 * |
|
17 * In no event shall Zoltán Hidvégi or the Zsh Development Group be liable |
|
18 * to any party for direct, indirect, special, incidental, or consequential |
|
19 * damages arising out of the use of this software and its documentation, |
|
20 * even if Zoltán Hidvégi and the Zsh Development Group have been advised of |
|
21 * the possibility of such damage. |
|
22 * |
|
23 * Zoltán Hidvégi and the Zsh Development Group specifically disclaim any |
|
24 * warranties, including, but not limited to, the implied warranties of |
|
25 * merchantability and fitness for a particular purpose. The software |
|
26 * provided hereunder is on an "as is" basis, and Zoltán Hidvégi and the |
|
27 * Zsh Development Group have no obligation to provide maintenance, |
|
28 * support, updates, enhancements, or modifications. |
|
29 * |
|
30 */ |
|
31 #include "example.mdh" |
|
32 #include "example.pro" |
|
33 |
|
34 #ifdef __SYMBIAN32__ |
|
35 #ifdef __WINSCW__ |
|
36 #pragma warn_unusedarg off |
|
37 #endif//__WINSCW__ |
|
38 #endif//__SYMBIAN32__ |
|
39 |
|
40 /* parameters */ |
|
41 |
|
42 static zlong intparam; |
|
43 static char *strparam; |
|
44 static char **arrparam; |
|
45 |
|
46 |
|
47 /**/ |
|
48 static int |
|
49 bin_example(char *nam, char **args, Options ops, UNUSED(int func)) |
|
50 { |
|
51 unsigned char c; |
|
52 char **oargs = args, **p = arrparam; |
|
53 long i = 0; |
|
54 |
|
55 printf("Options: "); |
|
56 for (c = 32; ++c < 128;) |
|
57 if (OPT_ISSET(ops,c)) |
|
58 putchar(c); |
|
59 printf("\nArguments:"); |
|
60 for (; *args; i++, args++) { |
|
61 putchar(' '); |
|
62 fputs(*args, stdout); |
|
63 } |
|
64 printf("\nName: %s\n", nam); |
|
65 #ifdef ZSH_64_BIT_TYPE |
|
66 printf("\nInteger Parameter: %s\n", output64(intparam)); |
|
67 #else |
|
68 printf("\nInteger Parameter: %ld\n", intparam); |
|
69 #endif |
|
70 printf("String Parameter: %s\n", strparam ? strparam : ""); |
|
71 printf("Array Parameter:"); |
|
72 if (p) |
|
73 while (*p) printf(" %s", *p++); |
|
74 printf("\n"); |
|
75 |
|
76 intparam = i; |
|
77 zsfree(strparam); |
|
78 strparam = ztrdup(*oargs ? *oargs : ""); |
|
79 freearray(arrparam); |
|
80 arrparam = zarrdup(oargs); |
|
81 return 0; |
|
82 } |
|
83 |
|
84 /**/ |
|
85 static int |
|
86 cond_p_len(char **a, UNUSED(int id)) |
|
87 { |
|
88 char *s1 = cond_str(a, 0, 0); |
|
89 |
|
90 if (a[1]) { |
|
91 zlong v = cond_val(a, 1); |
|
92 |
|
93 return strlen(s1) == v; |
|
94 } else { |
|
95 return !s1[0]; |
|
96 } |
|
97 } |
|
98 |
|
99 /**/ |
|
100 static int |
|
101 cond_i_ex(char **a, UNUSED(int id)) |
|
102 { |
|
103 char *s1 = cond_str(a, 0, 0), *s2 = cond_str(a, 1, 0); |
|
104 |
|
105 return !strcmp("example", dyncat(s1, s2)); |
|
106 } |
|
107 |
|
108 /**/ |
|
109 static mnumber |
|
110 math_sum(UNUSED(char *name), int argc, mnumber *argv, UNUSED(int id)) |
|
111 { |
|
112 mnumber ret; |
|
113 int f = 0; |
|
114 |
|
115 ret.u.l = 0; |
|
116 while (argc--) { |
|
117 if (argv->type == MN_INTEGER) { |
|
118 if (f) |
|
119 ret.u.d += (double) argv->u.l; |
|
120 else |
|
121 ret.u.l += argv->u.l; |
|
122 } else { |
|
123 if (f) |
|
124 ret.u.d += argv->u.d; |
|
125 else { |
|
126 ret.u.d = ((double) ret.u.l) + ((double) argv->u.d); |
|
127 f = 1; |
|
128 } |
|
129 } |
|
130 argv++; |
|
131 } |
|
132 ret.type = (f ? MN_FLOAT : MN_INTEGER); |
|
133 |
|
134 return ret; |
|
135 } |
|
136 |
|
137 /**/ |
|
138 static mnumber |
|
139 math_length(UNUSED(char *name), char *arg, UNUSED(int id)) |
|
140 { |
|
141 mnumber ret; |
|
142 |
|
143 ret.type = MN_INTEGER; |
|
144 ret.u.l = strlen(arg); |
|
145 |
|
146 return ret; |
|
147 } |
|
148 |
|
149 /**/ |
|
150 static int |
|
151 ex_wrapper(Eprog prog, FuncWrap w, char *name) |
|
152 { |
|
153 if (strncmp(name, "example", 7)) |
|
154 return 1; |
|
155 else { |
|
156 int ogd = opts[GLOBDOTS]; |
|
157 |
|
158 opts[GLOBDOTS] = 1; |
|
159 runshfunc(prog, w, name); |
|
160 opts[GLOBDOTS] = ogd; |
|
161 |
|
162 return 0; |
|
163 } |
|
164 } |
|
165 |
|
166 /* |
|
167 * boot_ is executed when the module is loaded. |
|
168 */ |
|
169 |
|
170 static struct builtin bintab[] = { |
|
171 BUILTIN("example", 0, bin_example, 0, -1, 0, "flags", NULL), |
|
172 }; |
|
173 |
|
174 static struct conddef cotab[] = { |
|
175 CONDDEF("len", 0, cond_p_len, 1, 2, 0), |
|
176 CONDDEF("ex", CONDF_INFIX, cond_i_ex, 0, 0, 0), |
|
177 }; |
|
178 |
|
179 static struct paramdef patab[] = { |
|
180 INTPARAMDEF("exint", &intparam), |
|
181 STRPARAMDEF("exstr", &strparam), |
|
182 ARRPARAMDEF("exarr", &arrparam), |
|
183 }; |
|
184 |
|
185 static struct mathfunc mftab[] = { |
|
186 NUMMATHFUNC("sum", math_sum, 1, -1, 0), |
|
187 STRMATHFUNC("length", math_length, 0), |
|
188 }; |
|
189 |
|
190 static struct funcwrap wrapper[] = { |
|
191 WRAPDEF(ex_wrapper), |
|
192 }; |
|
193 |
|
194 /**/ |
|
195 int |
|
196 setup_(UNUSED(Module m)) |
|
197 { |
|
198 printf("The example module has now been set up.\n"); |
|
199 fflush(stdout); |
|
200 return 0; |
|
201 } |
|
202 |
|
203 /**/ |
|
204 int |
|
205 boot_(Module m) |
|
206 { |
|
207 intparam = 42; |
|
208 strparam = ztrdup("example"); |
|
209 arrparam = (char **) zalloc(3 * sizeof(char *)); |
|
210 arrparam[0] = ztrdup("example"); |
|
211 arrparam[1] = ztrdup("array"); |
|
212 arrparam[2] = NULL; |
|
213 return !(addbuiltins(m->nam, bintab, sizeof(bintab)/sizeof(*bintab)) | |
|
214 addconddefs(m->nam, cotab, sizeof(cotab)/sizeof(*cotab)) | |
|
215 addparamdefs(m->nam, patab, sizeof(patab)/sizeof(*patab)) | |
|
216 addmathfuncs(m->nam, mftab, sizeof(mftab)/sizeof(*mftab)) | |
|
217 !addwrapper(m, wrapper)); |
|
218 } |
|
219 |
|
220 /**/ |
|
221 int |
|
222 cleanup_(Module m) |
|
223 { |
|
224 deletebuiltins(m->nam, bintab, sizeof(bintab)/sizeof(*bintab)); |
|
225 deleteconddefs(m->nam, cotab, sizeof(cotab)/sizeof(*cotab)); |
|
226 deleteparamdefs(m->nam, patab, sizeof(patab)/sizeof(*patab)); |
|
227 deletemathfuncs(m->nam, mftab, sizeof(mftab)/sizeof(*mftab)); |
|
228 deletewrapper(m, wrapper); |
|
229 return 0; |
|
230 } |
|
231 |
|
232 /**/ |
|
233 int |
|
234 finish_(UNUSED(Module m)) |
|
235 { |
|
236 printf("Thank you for using the example module. Have a nice day.\n"); |
|
237 fflush(stdout); |
|
238 return 0; |
|
239 } |