26763: fix problem on failed cd -s to relative path
[zsh.git] / Src / Modules / example.c
blobfa86f260c12e67c792fa8d865f57f61c92f900cb
1 /*
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
7 * All rights reserved.
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"
33 /* parameters */
35 static zlong intparam;
36 static char *strparam;
37 static char **arrparam;
40 /**/
41 static int
42 bin_example(char *nam, char **args, Options ops, UNUSED(int func))
44 unsigned char c;
45 char **oargs = args, **p = arrparam;
46 long i = 0;
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);
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");
69 intparam = i;
70 zsfree(strparam);
71 strparam = ztrdup(*oargs ? *oargs : "");
72 freearray(arrparam);
73 arrparam = zarrdup(oargs);
74 return 0;
77 /**/
78 static int
79 cond_p_len(char **a, UNUSED(int id))
81 char *s1 = cond_str(a, 0, 0);
83 if (a[1]) {
84 zlong v = cond_val(a, 1);
86 return strlen(s1) == v;
87 } else {
88 return !s1[0];
92 /**/
93 static int
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));
101 /**/
102 static mnumber
103 math_sum(UNUSED(char *name), int argc, mnumber *argv, UNUSED(int id))
105 mnumber ret;
106 int f = 0;
108 ret.u.l = 0;
109 while (argc--) {
110 if (argv->type == MN_INTEGER) {
111 if (f)
112 ret.u.d += (double) argv->u.l;
113 else
114 ret.u.l += argv->u.l;
115 } else {
116 if (f)
117 ret.u.d += argv->u.d;
118 else {
119 ret.u.d = ((double) ret.u.l) + ((double) argv->u.d);
120 f = 1;
123 argv++;
125 ret.type = (f ? MN_FLOAT : MN_INTEGER);
127 return ret;
130 /**/
131 static mnumber
132 math_length(UNUSED(char *name), char *arg, UNUSED(int id))
134 mnumber ret;
136 ret.type = MN_INTEGER;
137 ret.u.l = strlen(arg);
139 return ret;
142 /**/
143 static int
144 ex_wrapper(Eprog prog, FuncWrap w, char *name)
146 if (strncmp(name, "example", 7))
147 return 1;
148 else {
149 int ogd = opts[GLOBDOTS];
151 opts[GLOBDOTS] = 1;
152 runshfunc(prog, w, name);
153 opts[GLOBDOTS] = ogd;
155 return 0;
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[] = {
184 WRAPDEF(ex_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),
195 /**/
197 setup_(UNUSED(Module m))
199 printf("The example module has now been set up.\n");
200 fflush(stdout);
201 return 0;
204 /**/
206 features_(Module m, char ***features)
208 *features = featuresarray(m, &module_features);
209 return 0;
212 /**/
214 enables_(Module m, int **enables)
216 return handlefeatures(m, &module_features, enables);
219 /**/
221 boot_(Module m)
223 intparam = 42;
224 strparam = ztrdup("example");
225 arrparam = (char **) zalloc(3 * sizeof(char *));
226 arrparam[0] = ztrdup("example");
227 arrparam[1] = ztrdup("array");
228 arrparam[2] = NULL;
229 return addwrapper(m, wrapper);
232 /**/
234 cleanup_(Module m)
236 deletewrapper(m, wrapper);
237 return setfeatureenables(m, &module_features, NULL);
240 /**/
242 finish_(UNUSED(Module m))
244 printf("Thank you for using the example module. Have a nice day.\n");
245 fflush(stdout);
246 return 0;