1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/*************************************************************************
*
* HTCd - Copyright (C) 1998-2006 Henrik Rydberg
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <sh/htc.h>
#include <hdb/buffer.h>
#include <mt/dates.h>
#include <dirent.h>
#include <sys/stat.h>
const mstring sdot=".",sdots="..";
///////////////////////////////////////////////////////////////////
inline void stat(struct stat& fs,Env& env,const sref& path) throw(merror_t) {
if(::stat(DocPath(env,path).c_str(),&fs)<0)
THROW("htc: could not stat "<<path<<" - not found");
}
///////////////////////////////////////////////////////////////////
TOK_IMPL(remfile) {
mstring path=DocPath(env,env[0]);
if(remove(path.c_str())<0) THROW("htc: Could not remove "<<path);
}
TOK_IMPL(renfile) {
mstring oldpath=DocPath(env,env[0]),newpath=DocPath(env,env[1]);
if(rename(oldpath.c_str(),newpath.c_str())<0)
THROW("htc: Could not rename "<<oldpath<<" to "<<newpath);
}
TOK_IMPL(filesafe) {
static const charset fsafe=cset_alphareal|'_';
mstring file=env[0];
int pos=file.rfind('/'); if(pos>=0) file=file.past(pos,1);
pos=file.rfind('\\'); if(pos>=0) file=file.past(pos,1);
while((pos=file.find(sdots))>=0) file.erase(pos,2);
mstring acc;
for(int i=0;i<file.size();i++) if(fsafe.test(file[i])) acc.append(1,file[i]);
out<<acc;
}
///////////////////////////////////////////////////////////////////
TOK_IMPL(seturi) {
Req(env)->uri=env[0];
}
TOK_IMPL(uri) {
out<<Req(env)->uri;
}
TOK_IMPL(pwd) {
out<<Req(env)->uri.left_last('/');
}
TOK_IMPL(npwd) {
mstring s=Req(env)->uri.left_last('/');
for(int i=0;i<MAXLANG;i++) {
if(s.past_first('/').left_first('/')==LANG[i]) {
s=s.past_first('/').right_first('/');
break;
}
}
out<<s;
}
TOK_IMPL(home) {
if(Req(env)->htc()->docroot.nempty())
THROW("htc: home only works in absolute file mode");
out<<htchome();
}
///////////////////////////////////////////////////////////////////
TOK_IMPL(load) {
mstring name=env.parg(-1),uri=env.URI(env[0]),path=DocPath(env,uri);
if(name.nempty()) LoadBuffer(Reference(*env.toks,name,uri,1)->data,path);
else { mstring buffer; LoadBuffer(buffer,path); out<<buffer; }
}
TOK_IMPL(open) {
mstring name=env[0],uri=env.URI(env[1]),path=DocPath(env,uri);
dbref_t* ref=DBRef(*env.toks,env[0],ms_empty,tok_t::PRIVATE,1);
ref->db.Open(path,atoi(env.parg(-1)));
}
TOK_IMPL(save) {
mstring path=DocPath(env,env[0]),var=env[1];
int prot=atoi(env.parg(2)); if(!prot) prot=0600;
tok_t* p=env.toks->get(var);
if(!p) THROW("htc: "<<var<<": no such variable");
if(p->is_reference()) SaveBuffer(((reference_t*)p)->data,path,prot);
else if(p->is_dbref()) ((dbref_t*)p)->db.Saveas(path,prot);
else SaveBuffer(p->body,path,prot);
}
TOK_IMPL(dbmod) {
dbref_t* p=(dbref_t*)env.toks->get(env[0]);
if(!p&&!p->is_dbref()) THROW("htc: "<<env[0]<<": no such database");
out<<p->db.Lastmod();
}
///////////////////////////////////////////////////////////////////
static void Run(mstream& out,Env& env,const mstring& path) throw(merror_t) {
mstring buffer;
LoadBuffer(buffer,path);
env.parse(out,buffer);
}
TOK_IMPL(include) {
mstring cwd=env.URI(env[0]),path=DocPath(env,cwd);
if(!env.glob_find(cwd)) {
env.toks->add(new tok_t(cwd,ms_empty,tok_t::CONSTANT|tok_t::PRIVATE),0,1);
mstream m;
Env e(env,env.mod,cwd);
Run(m,e,path);
}
}
TOK_IMPL(parse) {
mstring cwd=env.URI(env[0]),path=DocPath(env,cwd);
Env e(env,env.mod,cwd);
Run(out,e,path);
}
TOK_IMPL(parse_private) {
mstring cwd=env.URI(env[0]),path=DocPath(env,cwd);
Env e(env,XPRIVATE,cwd);
Run(out,e,path);
}
TOK_IMPL(parse_public) {
mstring cwd=env.URI(env[0]),path=DocPath(env,cwd);
Env e(env,XPUBLIC,cwd);
Run(out,e,path);
}
///////////////////////////////////////////////////////////////////
TOK_IMPL(dirent) {
mstring uri=env.URI(env[0]);
sref uribase=uri.leftand_last('/'),ext=uri.adv(uribase);
mstring dirpath=DocPath(env,uribase);
DIR* dir=opendir(dirpath.c_str());
if(dir) {
dirent* p;
while((p=readdir(dir))!=0) {
sref name(p->d_name);
if(name.empty()||name==sdot||name==sdots) continue;
if(ext.empty()||name.right(-ext.size()-1)==ext) out<<uribase<<name<<"\n";
}
closedir(dir);
}
}
TOK_IMPL(dirmod) {
struct stat fs;
if(::stat(DocPath(env,env[0]).c_str(),&fs)>=0) out<<fs.st_mode;
else out<<(int)0;
}
TOK_IMPL(dirmtime) { struct stat fs; stat(fs,env,env[0]); out<<fs.st_mtime; }
TOK_IMPL(diruid) { struct stat fs; stat(fs,env,env[0]); out<<fs.st_uid; }
TOK_IMPL(dirgid) { struct stat fs; stat(fs,env,env[0]); out<<fs.st_gid; }
TOK_IMPL(filesize) { struct stat fs; stat(fs,env,env[0]); out<<fs.st_size; }
TOK_IMPL(setperm) {
mstring path=DocPath(env,env[0]);
if(chmod(path.c_str(),atoi(env[1]))<0)
THROW("htc: could not set file permission for "<<path);
}
///////////////////////////////////////////////////////////////////
void envAddFile(tokmap& T)
{
TOK_ADD(remfile,"x",tok_t::SYSONLY,
"%Remove file\n"
"%Syntax: \\remfile{uri}\n"
);
TOK_ADD(renfile,"xx",tok_t::SYSONLY,
"%Rename file\n"
"%Syntax: \\renfile{olduri}{newuri}\n"
);
TOK_ADD(filesafe,"x",tok_t::PUBLIC,
"%Transform uri to something safe to save\n"
"%Syntax: \\filesafe{uri}\n"
"%\n"
"%Basically removes eventual path information and\n"
"%odd characters.\n"
);
////////////////////////////////////////////////////////
TOK_ADD(seturi,"x",tok_t::SYSONLY,
"%Set current uri of current request\n"
"%Syntax: \\seturi\n"
"%\n"
"%Always points to a (virtual) uri.\n"
);
TOK_ADD(uri,"",tok_t::PUBLIC,
"%uri of current request\n"
"%Syntax: \\uri\n"
"%\n"
"%Always points to a (virtual) uri.\n"
);
TOK_ADD(pwd,"",tok_t::PUBLIC,
"%uri/directory of current request\n"
"%Syntax: \\pwd\n"
"%\n"
"%Always points to a (virtual) directory, and never contain any\n"
"%trailing slashes.\n"
);
TOK_ADD(npwd,"",tok_t::PUBLIC,
"%uri/directory of current request, without language reference\n"
"%Syntax: \\npwd\n"
"%\n"
"%Always points to a (virtual) directory, and never contain any\n"
"%trailing slashes.\n"
);
TOK_ADD(home,"",tok_t::PRIVATE,
"%Home directory\n"
"%Syntax: \\home\n"
"%\n"
"%Only works in absolute path mode, i.e. when no docroot is set.\n"
);
////////////////////////////////////////////////////////
TOK_ADD(load,"?x",tok_t::PRIVATE,
"%Load file directly to output or to variable\n"
"%Syntax: \\load<varname>{uri}\n"
);
TOK_ADD(open,"xx?",tok_t::SYSONLY,
"%Open database to variable\n"
"%Syntax: \\load{varname}{uri}<update>\n"
"%\n"
"%If update is true, the database is opened for reading\n"
"%and writing. NOTE that all updates are syncronous and\n"
"%instantly - saves or locks are not needed.\n"
);
TOK_ADD(save,"xx?",tok_t::SYSONLY,
"%Save variable to file\n"
"%Syntax: \\save{uri}{varname}<prot>\n"
"%\n"
"%If the variable points to a reference object, the\n"
"%reference is saved. If it points to a database,\n"
"%the database is saved. For globals and functions,\n"
"%the body is saved.\n"
"%\n"
"%The optional prot argument is a (base 10) integer\n"
"%defining the creation mode mask (unix).\n"
);
TOK_ADD(dbmod,"x",tok_t::PUBLIC,
"%Get modification time of open database\n"
"%Syntax: \\dbmod{var}\n"
);
////////////////////////////////////////////////////////
TOK_ADD(include,"x",tok_t::PRIVATE,
"%Include file in current running mode\n"
"%Syntax: \\include{uri}\n"
"%\n"
"%The file is loaded and evaluated, but produces no output\n"
"%Any uri can only be included once, so it is safe to use\n"
"%include as a precondition.\n"
"%\n"
"%NOTE that files parsed this way must be known to\n"
"%the super-user in order not to create a security hole -\n"
"%especially since this command is open in private mode also.\n"
);
TOK_ADD(parse,"x",tok_t::SYSONLY,
"%Parse file in current running mode\n"
"%Syntax: \\parse{uri}\n"
"%\n"
"%The file is loaded and evaluated to output.\n"
"%\n"
"%NOTE that files parsed this way must be known to\n"
"%the super-user in order not to create a security hole.\n"
);
TOK_ADD(parse_private,"x",tok_t::PRIVATE,
"%Parse file in private mode\n"
"%Syntax: \\parse.private{uri}\n"
"%\n"
"%The file is loaded and evaluated to output.\n"
"%\n"
"%System-mode commands are silently ignored.\n"
);
TOK_ADD(parse_public,"x",tok_t::PRIVATE,
"%Parse file in public mode\n"
"%Syntax: \\parse.public{uri}\n"
"%\n"
"%The file is loaded and evaluated to output.\n"
"%\n"
"%System/Private mode commands are silently ignored.\n"
);
////////////////////////////////////////////////////////
TOK_ADD(dirent,"x",tok_t::SYSONLY,
"%Collect directory entries\n"
"%Syntax: \\dirent{uri/match}\n"
"%\n"
"%An argument of anydir/ collects all files in anydir.\n"
"%An argument of anydir/.ext collects all files ending with .ext\n"
"%\n"
"%Note that an argument of anydir collects files in the parent\n"
"%directory of anydir which end whith anydir, which probably is\n"
"%not what you want. Trailing slashes are important.\n"
);
TOK_ADD(dirmod,"x",tok_t::SYSONLY,
"%Return file mode of uri\n"
"%Syntax: \\dirmod{uri}\n"
"%\n"
"%dirmod uses the unix stat function.\n"
"%On failure to read, returns zero.\n"
);
TOK_ADD(dirmtime,"x",tok_t::SYSONLY,
"%Return modification time of uri\n"
"%Syntax: \\dirmtime{uri}\n"
);
TOK_ADD(diruid,"x",tok_t::SYSONLY,
"%Return UNIX uid of uri\n"
"%Syntax: \\diruid{uri}\n"
"%\n"
"%Note that the UNIX uid is different from the HTC one.\n"
);
TOK_ADD(dirgid,"x",tok_t::SYSONLY,
"%Return UNIX gid of uri\n"
"%Syntax: \\dirgid{uri}\n"
"%\n"
"%Note that the UNIX gid is different from the HTC one.\n"
);
TOK_ADD(filesize,"x",tok_t::SYSONLY,
"%Return filesize of uri\n"
"%Syntax: \\filesize{uri}\n"
);
TOK_ADD(setperm,"xx",tok_t::SYSONLY,
"%Set file permissions of uri\n"
"%Syntax: \\setperm{uri}{mode}\n"
"%\n"
"%Mode is an integer bitmask compliant with fcntl.\n"
);
}
|