Finally, all global variables have been eliminated. The local symbol
[mpsl.git] / mpsl_f.c
blobe3b8efd6578f477ebd052b46fb3082c66063a6f6
1 /*
3 MPSL - Minimum Profit Scripting Language
4 Copyright (C) 2003/2006 Angel Ortega <angel@triptico.com>
6 mpsl_f.c - Minimum Profit Scripting Language Function Library
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include "config.h"
28 #include <stdio.h>
29 #include <wchar.h>
30 #include "mpdm.h"
31 #include "mpsl.h"
33 /*******************
34 Data
35 ********************/
37 /*******************
38 Code
39 ********************/
41 #define A(n) mpdm_aget(a, n)
42 #define A0 A(0)
43 #define A1 A(1)
44 #define A2 A(2)
45 #define IA(n) mpdm_ival(A(n))
46 #define IA1 IA(1)
47 #define IA2 IA(2)
48 #define IA3 IA(3)
50 static mpdm_t F_size(mpdm_t a) { return(MPDM_I(mpdm_size(A0))); }
51 static mpdm_t F_clone(mpdm_t a) { return(mpdm_clone(A0)); }
52 static mpdm_t F_dump(mpdm_t a) { mpdm_dump(A0); return(NULL); }
53 static mpdm_t F_cmp(mpdm_t a) { return(MPDM_I(mpdm_cmp(A0, A1))); }
55 static mpdm_t F_is_array(mpdm_t a) { return(mpsl_boolean(MPDM_IS_ARRAY(A0))); }
56 static mpdm_t F_is_hash(mpdm_t a) { return(mpsl_boolean(MPDM_IS_HASH(A0))); }
57 static mpdm_t F_is_exec(mpdm_t a) { return(mpsl_boolean(MPDM_IS_EXEC(A0))); }
59 static mpdm_t F_splice(mpdm_t a) { return(mpdm_splice(A0,A1,IA2,IA3)); }
60 static mpdm_t F_expand(mpdm_t a) { return(mpdm_expand(A0,IA1,IA2)); }
61 static mpdm_t F_collapse(mpdm_t a) { return(mpdm_collapse(A0,IA1,IA2)); }
62 static mpdm_t F_ins(mpdm_t a) { return(mpdm_ins(A0,A1,IA2)); }
63 static mpdm_t F_adel(mpdm_t a) { return(mpdm_adel(A0,IA1)); }
64 static mpdm_t F_shift(mpdm_t a) { return(mpdm_shift(A0)); }
65 static mpdm_t F_push(mpdm_t a) { return(mpdm_push(A0,A1)); }
66 static mpdm_t F_pop(mpdm_t a) { return(mpdm_pop(A0)); }
67 static mpdm_t F_queue(mpdm_t a) { return(mpdm_queue(A0,A1,IA2)); }
68 static mpdm_t F_seek(mpdm_t a) { return(MPDM_I(mpdm_seek(A0,A1,IA2))); }
69 static mpdm_t F_sort(mpdm_t a) { return(mpdm_sort_cb(A0,1,A1)); }
70 static mpdm_t F_split(mpdm_t a) { return(mpdm_split(A0,A1)); }
71 static mpdm_t F_join(mpdm_t a) { return(mpdm_join(A0,A1)); }
73 static mpdm_t F_hsize(mpdm_t a) { return(MPDM_I(mpdm_hsize(A0))); }
74 static mpdm_t F_exists(mpdm_t a) { return(mpsl_boolean(mpdm_exists(A0, A1))); }
75 static mpdm_t F_hdel(mpdm_t a) { return(mpdm_hdel(A0, A1)); }
76 static mpdm_t F_keys(mpdm_t a) { return(mpdm_keys(A0)); }
78 static mpdm_t F_open(mpdm_t a) { return(mpdm_open(A0, A1)); }
79 static mpdm_t F_close(mpdm_t a) { return(mpdm_close(A0)); }
80 static mpdm_t F_read(mpdm_t a) { return(mpdm_read(A0)); }
81 static mpdm_t F_write(mpdm_t a) { return(mpsl_boolean(mpdm_write(A0,A1))); }
82 static mpdm_t F_unlink(mpdm_t a) { return(mpsl_boolean(mpdm_unlink(A0))); }
83 static mpdm_t F_glob(mpdm_t a) { return(mpdm_glob(A0)); }
84 static mpdm_t F_encoding(mpdm_t a) { return(MPDM_I(mpdm_encoding(A0))); }
86 static mpdm_t F_regex(mpdm_t a) { return(mpdm_regex(A0,A1,IA2)); }
87 static mpdm_t F_sregex(mpdm_t a) { return(mpdm_sregex(A0,A1,A2,IA3)); }
89 static mpdm_t F_gettext(mpdm_t a) { return(mpdm_gettext(A0)); }
90 static mpdm_t F_gettext_domain(mpdm_t a) { return(MPDM_I(mpdm_gettext_domain(A0, A1))); }
92 static mpdm_t F_load(mpdm_t a) { return(mpdm_exec(mpsl_compile_file(A0), NULL)); }
93 static mpdm_t F_error(mpdm_t a) { return(mpsl_error(A0)); }
95 static mpdm_t F_eval(mpdm_t a)
97 mpdm_t c;
99 a = mpdm_clone(a);
100 c = mpdm_adel(a, 0);
102 return(mpsl_eval(c, a));
105 static mpdm_t F_print(mpdm_t a)
107 int n;
109 for(n = 0;n < mpdm_size(a);n++)
110 mpdm_write_wcs(stdout, mpdm_string(A(n)));
111 return(NULL);
115 static struct
117 wchar_t * name;
118 mpdm_t (* func)(mpdm_t);
119 } mpsl_funcs[]=
121 { L"size", F_size },
122 { L"clone", F_clone },
123 { L"dump", F_dump },
124 { L"cmp", F_cmp },
125 { L"is_array", F_is_array },
126 { L"is_hash", F_is_hash },
127 { L"is_exec", F_is_exec },
128 { L"splice", F_splice },
129 { L"expand", F_expand },
130 { L"collapse", F_collapse },
131 { L"ins", F_ins },
132 { L"adel", F_adel },
133 { L"shift", F_shift },
134 { L"push", F_push },
135 { L"pop", F_pop },
136 { L"queue", F_queue },
137 { L"seek", F_seek },
138 { L"sort", F_sort },
139 { L"split", F_split },
140 { L"join", F_join },
141 { L"hsize", F_hsize },
142 { L"exists", F_exists },
143 { L"hdel", F_hdel },
144 { L"keys", F_keys },
145 { L"open", F_open },
146 { L"close", F_close },
147 { L"read", F_read },
148 { L"write", F_write },
149 { L"unlink", F_unlink },
150 { L"glob", F_glob },
151 { L"encoding", F_encoding },
152 { L"regex", F_regex },
153 { L"sregex", F_sregex },
154 { L"load", F_load },
155 { L"error", F_error },
156 { L"eval", F_eval },
157 { L"print", F_print },
158 { L"gettext", F_gettext },
159 { L"gettext_domain", F_gettext_domain },
160 { NULL, NULL }
165 * mpsl_argv - Fills the ARGV global array.
166 * @argc: number of arguments
167 * @argv: array of string values
169 * Fills the ARGV global MPSL array with an array of arguments. These
170 * are usually the ones sent to main().
172 void mpsl_argv(int argc, char * argv[])
174 int n;
175 mpdm_t ARGV;
177 /* create the ARGV array */
178 ARGV = MPDM_A(0);
180 for(n = 0;n < argc;n++)
181 mpdm_push(ARGV, MPDM_MBS(argv[n]));
183 mpdm_hset_s(mpdm_root(), L"ARGV", ARGV);
187 /* in mpsl_c.c */
188 mpdm_t mpsl_build_opcodes(void);
192 * mpsl_startup - Initializes MPSL.
194 * Initializes the Minimum Profit Scripting Language. Returns 0 if
195 * everything went OK.
197 int mpsl_startup(void)
199 int n;
200 mpdm_t r;
201 mpdm_t m;
202 mpdm_t c;
204 /* startup MPDM */
205 mpdm_startup();
207 r = mpdm_root();
209 /* creates all the symbols in the CORE library */
210 c = MPDM_H(0);
211 for(n = 0;mpsl_funcs[n].name != NULL;n++)
213 mpdm_t f = MPDM_S(mpsl_funcs[n].name);
214 mpdm_t x = MPDM_X(mpsl_funcs[n].func);
216 mpdm_hset(r, f, x);
217 mpdm_hset(c, f, x);
220 /* creates INC, unless already defined */
221 if(mpdm_hget_s(r, L"INC") == NULL)
222 mpdm_hset_s(r, L"INC", MPDM_A(0));
224 /* the TRUE value */
225 mpdm_hset_s(r, L"TRUE", MPDM_I(1));
227 /* standard file descriptors */
228 mpdm_hset_s(r, L"STDIN", MPDM_F(stdin));
229 mpdm_hset_s(r, L"STDOUT", MPDM_F(stdout));
230 mpdm_hset_s(r, L"STDERR", MPDM_F(stderr));
232 /* fill now the MPSL hash */
233 m = MPDM_H(0);
234 mpdm_hset_s(r, L"MPSL", m);
236 /* store things there */
237 mpdm_hset_s(m, L"VERSION", MPDM_MBS(VERSION));
238 mpdm_hset_s(m, L"OPCODE", mpsl_build_opcodes());
239 mpdm_hset_s(m, L"LOCAL", MPDM_A(0));
240 mpdm_hset_s(m, L"CORE", c);
242 return(0);
247 * mpsl_shutdown - Shuts down MPSL.
249 * Shuts down MPSL. No MPSL functions should be used from now on.
251 void mpsl_shutdown(void)
253 mpdm_shutdown();