summaryrefslogtreecommitdiff
path: root/src/lang/func.cc
blob: 98cc04c8c9080137c9641bf2b64eb12bd1f3b8b4 (plain)
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
/*************************************************************************
 *
 * 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 <lang/env.h>
#include <unistd.h>

const mstring cron_tag="cron.";
const int SSECS=4;

TOK_IMPL(def) {
  expr_t* x=env.arg(2);
  func_t* tok=Func(*env.toks,env[0],x->expr,tok_t::PUBLIC,env[1]);
  if(x->status&OP_PARSE) tok->x.status=OP_PARSE; else tok->x.expand(env);
}

TOK_IMPL(redef) {
  expr_t* x=env.arg(2);
  func_t* tok=Func(*env.toks,env[0],x->expr,tok_t::PUBLIC,env[1],1);
  if(x->status&OP_PARSE) tok->x.status=OP_PARSE; else tok->x.expand(env);
}

TOK_IMPL(ldef) {
  if(env.toks->get(env[0])) THROW("htc: local function would override global token");
  expr_t* x=env.arg(2);
  func_t* tok=Func(env.loc,env[0],x->expr,tok_t::PUBLIC,env[1]);
  if(x->status&OP_PARSE) tok->x.status=OP_PARSE; else tok->x.expand(env);
}

TOK_IMPL(cron) {
  mstring name=cron_tag+env[0];
  expr_t* x=env.arg(1);
  cron_t* tok=Cron(*env.toks,name,x->expr,tok_t::PUBLIC,atoi(env[0]));
  if(x->status&OP_PARSE) tok->x.status=OP_PARSE; else tok->x.expand(env);
}

///////////////////////////////////////////////////

static void Run(tokmap& m,mstream& out,Env& env,const sref& s) throw(merror_t) {
  tok_t* p=m.get(s);
  if(!p) THROW("htc: cannot run "<<s<<"; does not exist");
  try {
    if(p->is_reference()) env.parse(out,((reference_t*)p)->data);
    else if(!p->is_runnable()) env.parse(out,p->body);
  }
  catch(const merror_t& e) {
    out<<e.desc;
  }
}

TOK_IMPL(run) {
  Env e(env,env.mod,env.pwd);
  Run(*env.toks,out,e,env[0]);
}
TOK_IMPL(run_private) {
  Env e(env,XPRIVATE,env.pwd);
  Run(*env.toks,out,e,env[0]);
}
TOK_IMPL(run_public) {
  Env e(env,XPUBLIC,env.pwd);
  Run(*env.toks,out,e,env[0]);
}
TOK_IMPL(run_local) {
  try {
    Env e(env,XPUBLIC,env.pwd);
    e.parse(out,env.gloc(env[0]));
  }
  catch(const merror_t& e) {
    out<<e.desc;
  }
}
TOK_IMPL(run_cron) {
  mrvec<cron_t*> cron;
  int cont=1; for(tokmap* tok=env.toks;tok&&cont;tok=tok->up) {
    for(Env::iterator p=tok->begin();p!=tok->end();++p) {
      if((*p)->name.left(cron_tag.size())==cron_tag&&(*p)->is_cron()) {
        cron.push_back((cron_t*)(*p));
        cont=0;
      }
    }
  }
  while(env.req->is_running()&&cron.size()) {
    int cont=1;
    for(int i=0;i<cron.size()&&cont;i++) {
      if(time(0)>cron[i]->last+cron[i]->interval) {
        mstream ms;
        env.eval(ms,&cron[i]->x,0);
        cron[i]->last=time(0);
        cont=0;
      }
    }
    if(cont) sleep(SSECS);
  }
}

//////////////////////////////////////////////////////

void envAddFunc(tokmap& T)
{
  TOK_ADD(def,"xxr",tok_t::PUBLIC,
          "%Define new global function\n"
          "%Syntax: \\def{varname}{argument string [xr?*]}{body}\n"
          "%\n"
          "%The argument string is a sequence of characters describing\n"
          "%the argument type, which is one of x (pre-evaluate), r (post-evaluate),\n"
          "%? (optional post-evaluate) or * (optional at end, post-evaluate).\n"
          "%The can be only one optional (?) argument.\n"
          "%The body contains the function body. The arguments are retrieved\n"
          "%as \\1, \\2 etc, except the optional argument which is retrieved as \\0,\n"
          "%regardless of where it stands in the sequence.\n"
          "%\n"
          "%Generates an error if already defined.\n"
          );
  TOK_ADD(redef,"xxr",tok_t::PRIVATE,
          "%Redefine global function\n"
          "%Syntax: \\def{varname}{argument string [xr?*]}{body}\n"
          "%\n"
          "%The argument string is a sequence of characters describing\n"
          "%the argument type, which is one of x (pre-evaluate), r (post-evaluate),\n"
          "%? (optional post-evaluate) or * (optional at end, post-evaluate).\n"
          "%The can be only one optional (?) argument.\n"
          "%The body contains the function body. The arguments are retrieved\n"
          "%as \\1, \\2 etc, except the optional argument which is retrieved as \\0,\n"
          "%regardless of where it stands in the sequence.\n"
          "%\n"
          "%Takes precedence over any existing function.\n"
          );
  TOK_ADD(ldef,"xxr",tok_t::PUBLIC,
          "%Define a local function\n"
          "%Syntax: \\ldef{varname}{argument string [xr?*]}{body}\n"
          "%\n"
          "%The argument string is a sequence of characters describing\n"
          "%the argument type, which is one of x (pre-evaluate), r (post-evaluate),\n"
          "%? (optional post-evaluate) or * (optional at end, post-evaluate).\n"
          "%The can be only one optional (?) argument.\n"
          "%The body contains the function body. The arguments are retrieved\n"
          "%as \\1, \\2 etc, except the optional argument which is retrieved as \\0,\n"
          "%regardless of where it stands in the sequence.\n"
          "%\n"
          "%Local functions work just like global functions, but are only\n"
          "%accessible from within the current stack frame.\n"
          );
  TOK_ADD(cron,"xr",tok_t::SYSONLY,
          "%Define a cron function\n"
          "%Syntax: \\cron{interval}{body}\n"
          "%\n"
          "%A cron function is executed every interval seconds,\n"
          "%when running in cron mode.\n"
          "%\n"
          "%See \\run.cron.\n"
          );

  //////////////////////////////////////////////////////

  TOK_ADD(run,"x",tok_t::SYSONLY,
          "%Evaluate the body of variable in current running mode\n"
          "%Syntax: \\run{varname}\n"
          "%\n"
          "%The running modes are:\n"
          "%\n"
          "%XINIT    Init mode is only used to collect meta information\n"
          "%XSYSTEM  The system mode - everything is accessible\n"
          "%XPRIVATE Private mode - suitable for in-door custom programs\n"
          "%XPUBLIC  Public mode - quite restrictive, may be run by anyone\n"
          "%         via the web. This is the default mode for all derivative\n"
          "%         functions, as well as for globals and constants.\n"
          "%\n"
          "%IT IS A MAJOR SECURITY HAZARD TO EVALUATE UNKNOWN\n"
          "%VARIABLES IN XSYSTEM MODE!!\n"
          "%\n"
          "%If at all, user-supplied information should only be run\n"
          "%in public mode, for instance to allow the expansion of latex\n"
          "%commands. NEVER use this mode outside the core.\n"
          );
  TOK_ADD(run_private,"x",tok_t::SYSONLY,
          "%Evaluate the body of variable in private mode\n"
          "%Syntax: \\run.private{varname}\n"
          "%\n"
          "%The running modes are:\n"
          "%\n"
          "%XINIT    Init mode is only used to collect meta information\n"
          "%XSYSONLY The system mode - everything is accessible\n"
          "%XPRIVATE Private mode - suitable for in-door custom htc code\n"
          "%XPUBLIC  Public mode - quite restrictive, may be run by anyone\n"
          "%         via the web. This is the default mode for all derivative\n"
          "%         functions, as well as for globals and constants.\n"
          "%\n"
          "%The execution of sysonly functions is silent, just as it wasn't there.\n"
          );
  TOK_ADD(run_public,"x",tok_t::PRIVATE,
          "%Evaluate the body of variable in public mode\n"
          "%Syntax: \\run.public{varname}\n"
          "%\n"
          "%The running modes are:\n"
          "%\n"
          "%XINIT    Init mode is only used to collect meta information\n"
          "%XSYSONLY The system mode - everything is accessible\n"
          "%XPRIVATE Private mode - suitable for in-door custom programs\n"
          "%XPUBLIC  Public mode - quite restrictive, may be run by anyone\n"
          "%         via the web. This is the default mode for all derivative\n"
          "%         functions, as well as for globals and constants.\n"
          "%\n"
          "%The execution of sysonly or private functions is silent,\n"
          "%just as they weren't there.\n"
          );
  TOK_ADD(run_local,"x",tok_t::PRIVATE,
          "%Evaluate the body of the local variable in public mode\n"
          "%Syntax: \\run.local{varname}\n"
          "%\n"
          "%The running modes are:\n"
          "%\n"
          "%XINIT    Init mode is only used to collect meta information\n"
          "%XSYSTEM  The system mode - everything is accessible\n"
          "%XPRIVATE Private mode - suitable for in-door custom programs\n"
          "%XPUBLIC  Public mode - quite restrictive, may be run by anyone\n"
          "%         via the web. This is the default mode for all derivative\n"
          "%         functions, as well as for globals and constants.\n"
          "%\n"
          "%The execution of sysonly or private functions is silent,\n"
          "%just as they weren't there.\n"
          );
  TOK_ADD(run_cron,"",tok_t::SYSONLY,
          "%Go into an infinite loop and run the crontab command.\n"
          "%Syntax: \\run.cron{}\n"
          "%\n"
          "%Note that only cron functions from the highest global level\n"
          "%where a cron function is defined is run. Hence you may redefine\n"
          "%earlier crontabs, but cannot mix between levels.\n"
          "%\n"
          "%You can only have one crontab of a certain interval.\n"
          "%\n"
          "%Produces no output.\n"
          );
}