summaryrefslogtreecommitdiff
path: root/src/lang/env.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/lang/env.cc')
-rw-r--r--src/lang/env.cc308
1 files changed, 308 insertions, 0 deletions
diff --git a/src/lang/env.cc b/src/lang/env.cc
new file mode 100644
index 0000000..83f1a7e
--- /dev/null
+++ b/src/lang/env.cc
@@ -0,0 +1,308 @@
1/*************************************************************************
2 *
3 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <lang/env.h>
21#include <mt/random.h>
22
23static unsigned long seed=RandomInit(time(0));
24
25mstring Random(int n)
26{
27 mstring s;
28 for(int i=0;i<n;i++) {
29 int digit=int(10*Uniform());
30 if(digit>9) THROW("env: assertion on lib failed");
31 s+=char(48+digit);
32 }
33 return s;
34}
35
36///////////////////////////////////////////////////
37
38void Env::clear_args()
39{
40 for(int i=0;i<xarg.size();i++) delete xarg[i]; xarg.clear();
41 for(int i=0;i<rarg.size();i++) delete rarg[i]; rarg.clear();
42 args=0;
43}
44
45void Env::parg(mstream& out,int n) throw(merror_t)
46{
47 if(args) {
48 if(n<0) n+=args;
49 if(n>=0&&n<args) {
50 if(n<xsize()) out<<xarg[n]->result;
51 else eval(out,rarg[n-xsize()],0);
52 }
53 }
54 else if(up) up->parg(out,n);
55}
56
57void Env::parg(mstring& tmp,int n) throw(merror_t)
58{
59 swrite sw(tmp);
60 parg(sw,n);
61}
62
63mstring Env::parg(int n) throw(merror_t)
64{
65 mswrite tmp; parg(tmp,n); return tmp.str();
66}
67
68//////////////////////////////////////////////////////
69
70int Env::onarg(int n) const
71{
72 if(args) {
73 if(n<0) n+=args;
74 if(n>=0&&n<args) {
75 if(n<xsize()) return xarg[n]->result.nempty();
76 else return rarg[n-xsize()]->expr.nempty();
77 }
78 }
79 else if(up) return up->onarg(n);
80 return 0;
81}
82
83expr_t* Env::arg(int n) throw(merror_t) {
84 if(args) {
85 if(n<0) n+=args;
86 if(n>=0&&n<args) {
87 if(n<xsize()) return xarg[n];
88 else return rarg[n-xsize()];
89 }
90 }
91 else if(up) return up->arg(n);
92 THROW("env: bad arg number "<<n);
93 return 0;
94}
95
96const sref& Env::operator[](int n) const throw(merror_t) {
97 if(args) {
98 if(n<0) n+=args;
99 if(n>=0&&n<args) {
100 if(n<xsize()) return xarg[n]->result;
101 else THROW("env: post-eval expression not readable here");
102 }
103 }
104 else if(up) return up->operator[](n);
105 THROW("env: bad arg number "<<n);
106 return ms_empty;
107}
108
109//////////////////////////////////////////////////////
110
111const sref Env::glob(const sref& t) const throw(merror_t)
112{
113 const tok_t* p=toks->get(t);
114 if(is_clear(p)&&!p->is_runnable()) return p->body;
115 THROW("env: could not find global variable "<<t);
116 return ms_empty;
117}
118
119int Env::glob_find(const sref& t) const
120{
121 const tok_t* p=toks->get(t);
122 return is_clear(p)&&!p->is_runnable();
123}
124
125int Env::onglob(const sref& t) const
126{
127 const tok_t* p=toks->get(t);
128 return is_clear(p)&&!p->is_runnable()&&p->body.nempty();
129}
130
131void Env::glob_list(rpile& list,const sref& t) const
132{
133 list.clear();
134 if(t.empty()) {
135 for(const_iterator p=toks->begin();p!=toks->end();++p) {
136 if(is_clear(*p)&&!(*p)->is_runnable()) list.push_back((*p)->name);
137 }
138 }
139 else {
140 int cont=1;
141 for(const tokmap* tok=toks;tok&&cont;tok=tok->up) {
142 for(const_iterator p=tok->begin();p!=tok->end();++p) {
143 if((*p)->name.left(t.size())==t&&is_clear(*p)&&!(*p)->is_runnable()) {
144 list.push_back((*p)->name);
145 cont=0;
146 }
147 }
148 }
149 }
150}
151
152void Env::glob_adb(const sref& fx,const sref& t) throw(merror_t)
153{
154 if(!db) THROW("htc: no active database");
155 rpile list; glob_list(list,t);
156 mrvec<int> col(list.size());
157 int index=-1;
158 for(int i=0;i<col.size();i++) {
159 col[i]=db->Index(fx+list[i].right(t.size()));
160 if(col[i]==db->Index()) index=i;
161 }
162 if(index<0) THROW("env: index required when adding a db record");
163 int row=db->Find(glob(list[index]));
164 if(row>=0) {
165 for(int i=0;i<list.size();i++) if(i!=index) db->Set(row,col[i],glob(list[i]));
166 }
167 else {
168 spile nlist(db->Tables());
169 for(int i=0;i<list.size();i++) nlist[col[i]]=glob(list[i]);
170 row=db->Insert(nlist,1);
171 }
172 if(row<0) THROW("htc: could not add record - denied");
173 dbat=row;
174}
175
176//////////////////////////////////////////////////////
177
178const sref Env::aloc(const sref& t,const sref& v) throw(merror_t)
179{
180 if(!is_tokname(t)) THROW("env: bad token name "<<t);
181 return loc.add(new tok_t(t,v,tight()),1,1)->body;
182}
183
184const sref Env::sloc(const sref& t,const sref& v) throw(merror_t)
185{
186 if(db) {
187 int p=db->FindIndex(*t=='@'?t.popf():t);
188 if(p>=0) {
189 if(p==db->Index()) THROW("env: cannot assign to db index");
190 if(dbat<0||dbat>=db->size()) THROW("env: not a valid db record");
191 db->Set(dbat,p,v);
192 return db->at(dbat,p);
193 }
194 }
195 return loc.set(new tok_t(t,v,tight()),1,1)->body;
196}
197
198const sref Env::gloc(const sref& t) const throw(merror_t)
199{
200 if(db) {
201 int p=db->FindIndex(*t=='@'?t.popf():t);
202 if(p>=0) {
203 if(dbat>=0&&dbat<db->size()) return db->at(dbat,p);
204 else THROW("env: not a valid database record");
205 }
206 }
207 const tok_t* p=loc.get(t);
208 if(is_clear(p)&&!p->is_runnable()) return p->body;
209 THROW("env: could not find local variable "<<t);
210 return ms_empty;
211}
212
213int Env::gloc_find(const sref& t) const
214{
215 if(db) {
216 int p=db->FindIndex(*t=='@'?t.popf():t);
217 if(p>=0) return dbat>=0&&dbat<db->size();
218 }
219 const tok_t* p=loc.get(t);
220 return is_clear(p)&&!p->is_runnable();
221}
222
223int Env::onloc(const sref& t) const
224{
225 if(db) {
226 int p=db->FindIndex(*t=='@'?t.popf():t);
227 if(p>=0) return dbat>=0&&dbat<db->size()&&db->at(dbat,p).nempty();
228 }
229 const tok_t* p=loc.get(t);
230 return is_clear(p)&&!p->is_runnable()&&p->body.nempty();
231}
232
233void Env::gloc_list(rpile& list,const sref& t) const
234{
235 list.clear();
236 if(t.empty()) {
237 for(const_iterator p=loc.begin();p!=loc.end();++p) {
238 if(is_clear(*p)&&!(*p)->is_runnable()) list.push_back((*p)->name);
239 }
240 }
241 else if(*t=='@') {
242 if(db) {
243 sref tt=t.popf();
244 for(int i=0;i<db->Tables();i++) {
245 if(tt.empty()||db->Table(i).left(tt.size())==tt) list.push_back(db->Table(i));
246 }
247 }
248 else THROW("env: cannot use '@' - no active database");
249 }
250 else {
251 int cont=1;
252 for(const tokmap* tok=&loc;tok&&cont;tok=tok->up) {
253 for(const_iterator p=tok->begin();p!=tok->end();++p) {
254 if((*p)->name.left(t.size())==t&&is_clear(*p)&&!(*p)->is_runnable()) {
255 list.push_back((*p)->name);
256 cont=0;
257 }
258 }
259 }
260 }
261}
262
263void Env::gloc_adb(const sref& fx,const sref& t) throw(merror_t)
264{
265 if(!db) THROW("htc: no active database");
266 rpile list; gloc_list(list,t);
267 mrvec<int> col(list.size());
268 int index=-1;
269 for(int i=0;i<col.size();i++) {
270 col[i]=db->Index(fx+list[i].right(t.size()));
271 if(col[i]==db->Index()) index=i;
272 }
273 if(index<0) THROW("env: index required when adding a db record");
274 int row=db->Find(gloc(list[index]));
275 if(row>=0) {
276 for(int i=0;i<list.size();i++) if(i!=index) db->Set(row,col[i],gloc(list[i]));
277 }
278 else {
279 spile nlist(db->Tables());
280 for(int i=0;i<list.size();i++) nlist[col[i]]=gloc(list[i]);
281 row=db->Insert(nlist,1);
282 }
283 if(row<0) THROW("htc: could not add record - denied");
284 dbat=row;
285}
286
287//////////////////////////////////////////////////////
288
289const mstring sback="../",sident="./",sdots="..",sslash="//";
290
291mstring Env::URI(const sref& raw)
292{
293 int s; mstring cwd=pwd.left_last('/'),uri=raw;
294 while(uri.left(sback.size())==sback) {
295 uri.erase(0,sback.size());
296 cwd=cwd.left_last('/');
297 }
298 while((s=uri.find(sback))>=0) {
299 mstring tmp=uri.left(s).left_last('/').leftand_last('/');
300 tmp+=uri.right(s+sback.size());
301 uri=tmp;
302 }
303 while((s=uri.find(sdots))>=0) uri.erase(s,sdots.size());
304 while((s=uri.find(sident))>=0) uri.erase(s,sident.size());
305 while((s=uri.find(sslash))>=0) uri.erase(s,sslash.size());
306 if(*uri=='/') return uri;
307 else { cwd+='/'; cwd+=uri; return cwd; }
308}