Subversion Repositories wimsdev

Rev

Rev 14941 | Rev 15276 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 reyssat 1
/*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
2
 *
3
 *  This program is free software; you can redistribute it and/or modify
4
 *  it under the terms of the GNU General Public License as published by
5
 *  the Free Software Foundation; either version 2 of the License, or
6
 *  (at your option) any later version.
7
 *
8
 *  This program is distributed in the hope that it will be useful,
9
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 *  GNU General Public License for more details.
12
 *
13
 *  You should have received a copy of the GNU General Public License
14
 *  along with this program; if not, write to the Free Software
15
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
 */
17
 
8185 bpr 18
/* this is part of wims server source.
19
 * This file does execution commands.
20
 */
21
#include "wims.h"
10 reyssat 22
 
23
static void _skip_contents(int isif);
24
 
8213 bpr 25
/* common routine for the two if's. */
10 reyssat 26
static void _exec_if_while(char *p, int numerical, int isif)
27
{
12245 bpr 28
  if(compare(p,numerical,0)==0) _skip_contents(isif); /* skip if false */
29
  else if(!isif) m_file.for_idx++;
30
  return;
10 reyssat 31
}
32
 
7379 bpr 33
    /* 'if' non-numerical (unless comparisons are < or >, etc.) */
10 reyssat 34
void exec_if(char *p)
35
{
12245 bpr 36
   _exec_if_while(p,0,1);
10 reyssat 37
}
38
 
8213 bpr 39
/* 'if' numerical. */
10 reyssat 40
void exec_ifval(char *p)
41
{
12245 bpr 42
  _exec_if_while(p,1,1);
10 reyssat 43
}
44
 
45
void _exec_while(char *p, int numerical)
46
{
12245 bpr 47
  FOR_STACK *stk;
48
  if(m_file.for_idx>=MAX_FOR_LEVEL) module_error("too_many_fors");
49
  stk=&(m_file.for_stack[m_file.for_idx]);
50
  stk->lineno=m_file.l;
51
  memmove(stk->varname,"?",2);
52
  _exec_if_while(p,numerical,0);
10 reyssat 53
}
54
 
8213 bpr 55
/* 'while' non-numerical (unless comparisons are < or >, etc.) */
10 reyssat 56
void exec_while(char *p)
57
{
12245 bpr 58
  _exec_while(p,0);
10 reyssat 59
}
60
 
8213 bpr 61
/* 'while' numerical. */
10 reyssat 62
void exec_whileval(char *p)
63
{
12245 bpr 64
  _exec_while(p,1);
10 reyssat 65
}
66
 
67
void exec_endwhile(char *p)
68
{
12245 bpr 69
  FOR_STACK *stk;
70
  if(m_file.for_idx<=0) module_error("next_without_for");
71
  stk=&(m_file.for_stack[m_file.for_idx-1]);
72
  if(stk->varname[0]!='?') module_error("next_without_for");
73
  m_file.for_idx--; *p=0;
74
  m_file.linepointer=stk->lineno;
75
  executed_gotos++;
76
  if(executed_gotos>=GOTO_LIMIT) module_error("too_many_gotos");
10 reyssat 77
}
78
 
8213 bpr 79
/* Should provide a method to stop infinite loop. */
10 reyssat 80
void exec_goto(char *p)
81
{
12245 bpr 82
  char lbuf[MAX_NAMELEN+17];
83
  char *label_start, *line_start;
84
  int old_line;
85
  int i;
86
  executed_gotos++;
87
  if(executed_gotos>=GOTO_LIMIT) module_error("too_many_gotos");
88
  old_line=m_file.l;
89
  label_start=find_word_start(p);
90
  *find_word_end(label_start)=0;
91
  m_file.l=m_file.linepointer=0;
92
  for(i=0;i<m_file.linecnt;i++) {
93
    if(((m_file.lines[i].isstart)&4)==0) continue;
94
    line_start=find_word_start((m_file.lines[i]).address);
95
    if(line_start>(m_file.lines[i+1]).address && i<m_file.linecnt)
96
      continue;
97
    m_file.linepointer=i;
98
    wgetline(lbuf,MAX_NAMELEN+8,&m_file);
99
    line_start=find_word_start(lbuf);
100
    if(*line_start!=label_prefix_char) continue;
101
    line_start++;*find_word_end(line_start)=0;
102
    if(line_start[0]!='*' && strcmp(line_start,label_start)!=0) continue;
103
    *p=0; i++; return;
104
  }
105
  m_file.l=old_line;
106
  setvar(error_data_string,label_start); module_error("label_not_found");
107
/*    m_file.linepointer=m_file.linecnt; *p=0; return;
108
*/
109
}
10 reyssat 110
 
8213 bpr 111
/* 'else' with or without 'if'.
112
 * Philosophy: we implement a loosely checked grammar.
113
 * We cannot check 'if' grammar if we want to use 'goto'
114
 * together with 'if'.
115
 */
10 reyssat 116
void exec_else(char *p)
117
{
12245 bpr 118
  _skip_contents(1); return;
10 reyssat 119
}
120
 
8213 bpr 121
/* 'endif': nothing needs to be done here. */
10 reyssat 122
void exec_endif(char *p)
123
{
12245 bpr 124
  return;
10 reyssat 125
}
126
 
8213 bpr 127
/* find out the end of a for loop */
10 reyssat 128
void goto_for_end(void)
129
{
12245 bpr 130
  int inner;
131
  char buf[16];
132
  inner=0;
133
  while(m_file.linepointer<m_file.linecnt) {
134
    if((m_file.lines[m_file.linepointer].isstart&2)==0) {
135
      m_file.linepointer++; continue;
136
    }
137
    if(wgetline(buf,8,&m_file)==EOF) break;
138
    *find_word_end(buf)=0;
139
    if(strcmp(buf,"!for")==0) {
140
      inner++; continue;
141
    }
142
    if(strcmp(buf,"!next")==0) {
143
      if(inner>0) {
144
        inner--; continue;
7379 bpr 145
      }
12245 bpr 146
      else break;
10 reyssat 147
    }
12245 bpr 148
  }
10 reyssat 149
}
150
 
8213 bpr 151
/* for */
10 reyssat 152
void exec_for(char *p)
153
{
12245 bpr 154
  char *p1, *p2, *p3;
155
  double v1, v2, step;
156
  char buf[MAX_LINELEN+1];
157
  FOR_STACK *stk;
10 reyssat 158
 
12245 bpr 159
  if(m_file.for_idx>=MAX_FOR_LEVEL) module_error("too_many_fors");
160
  stk=&(m_file.for_stack[m_file.for_idx]);
161
  stk->lineno=m_file.l;
162
  p1=find_word_start(p);
163
  for(p2=p1; !isspace(*p2) && *p2!=0 && *p2!='=' ; p2++);
164
  if(*p2==0) syntax: module_error("for_syntax");
165
  if(p2-p1>MAX_NAMELEN) module_error("name_too_long");
166
  memmove(stk->varname,p1,p2-p1);
167
  stk->varname[p2-p1]=0;
168
  p1=find_word_start(p2); if(*p1==0) goto syntax;
169
  if(*p1=='=') {
170
    p1++;
171
    assign:
172
    stk->from=0;
173
    p2=wordchr(p1,"to");
174
    if(p2==NULL) p2=strstr(p1,"..");
175
    if(p2==NULL) goto syntax;
176
    *p2=0; p2+=2;
177
    p3=wordchr(p2,"step");
178
    if(p3!=NULL) {
179
      *p3=0; p3+=strlen("step");
180
      step=evalue(p3);
181
      if(step==0) module_error("zero_step");
182
    }
183
    else step=1;
184
      v1=evalue(p1); v2=evalue(p2);
185
      float2str(v1,buf); setvar(stk->varname, buf);
186
      if((step>0 && v1>v2) || (step<0 && v1<v2) ) {  /* condition not met */
10 reyssat 187
  loop_out:
7379 bpr 188
        goto_for_end();
189
      }
12245 bpr 190
    else {
191
      stk->varval=v1; stk->varend=v2; stk->step=step;
192
      stk->lineno=m_file.linepointer;
193
      m_file.for_idx++;
10 reyssat 194
    }
12245 bpr 195
    *p=0; return;
196
  }
197
  if(memcmp(p1,"from",strlen("from"))==0 && isspace(*(p1+strlen("from")))) {
198
    p1+=strlen("from"); goto assign;
199
  }
200
  if(memcmp(p1,"in",strlen("in"))==0 && isspace(*(p1+strlen("in")))) {
201
    stk->from=1; p1+=strlen("in");
202
    p1=find_word_start(p1); if(*p1==0) goto loop_out;
203
    p2=xmalloc(MAX_LINELEN+1);
204
    mystrncpy(p2,p1,MAX_LINELEN);
205
    substit(p2);
206
    strip_trailing_spaces(p2);
207
    if(*p2==0) goto loop_out;
208
    p1=strparchr(p2,','); stk->bufpt=p2;
209
    if(p1!=NULL) {
210
      *p1=0; stk->list_pt=find_word_start(p1+1);
10 reyssat 211
    }
12245 bpr 212
    else stk->list_pt=NULL;
213
    strip_trailing_spaces(p2); setvar(stk->varname,p2);
214
    stk->lineno=m_file.linepointer;
215
    m_file.for_idx++; *p=0; return;
216
  }
217
  goto syntax;
10 reyssat 218
}
219
 
8213 bpr 220
/* break a for loop */
10 reyssat 221
void exec_break(char *p)
222
{
12245 bpr 223
  FOR_STACK *stk;
224
  if(m_file.for_idx>0) {
225
    stk=&(m_file.for_stack[m_file.for_idx-1]);
226
    if(stk->varname[0]=='?') _skip_contents(0);
227
    else goto_for_end();
228
    m_file.for_idx--;
229
  }
230
  *p=0; return;
10 reyssat 231
}
232
 
8213 bpr 233
/* next */
10 reyssat 234
void exec_next(char *p)
235
{
12245 bpr 236
  double v1, v2, step;
237
  char *p1, *p2;
238
  char buf[MAX_LINELEN+1];
239
  FOR_STACK *stk;
240
  if(m_file.for_idx<=0) module_error("next_without_for");
241
  stk=&(m_file.for_stack[m_file.for_idx-1]);
242
  if(stk->varname[0]=='?') module_error("next_without_for");
243
  if(stk->from) { /* list style */
244
    if(stk->list_pt==NULL) {
245
      free(stk->bufpt);
246
      m_file.for_idx--;
247
      *p=0; return;
10 reyssat 248
    }
12245 bpr 249
    p1=strchr(stk->list_pt,',');
250
    if(p1!=NULL) {
251
      *p1=0; p2=find_word_start(p1+1);
10 reyssat 252
    }
12245 bpr 253
    else p2=NULL;
254
    strip_trailing_spaces(stk->list_pt);
255
    setvar(stk->varname,stk->list_pt);
256
    stk->list_pt=p2; goto loop_back;
257
  }
258
  v1=stk->varval; v2=stk->varend; step=stk->step;
259
  v1+=step; stk->varval=v1;
260
  float2str(v1,buf);
261
  setvar(stk->varname, buf);
262
  if((step>0 && v1<=v2) || (step<0 && v1>=v2)) { /* loop */
263
    loop_back:
264
    m_file.linepointer=stk->lineno;
265
    executed_gotos++;
266
    if(executed_gotos>=GOTO_LIMIT) module_error("too_many_gotos");
267
  }
268
  else m_file.for_idx--;
269
  *p=0; return;
10 reyssat 270
}
271
 
8213 bpr 272
/* Execution of a file in the module directory,
273
 * only for trusted modules.
274
 */
10 reyssat 275
void exec_mexec(char *p)
276
{
12245 bpr 277
  direct_exec=1;
278
  calc_mexec(p);
279
  direct_exec=0;
10 reyssat 280
}
281
 
8213 bpr 282
/* call shell. */
10 reyssat 283
void _exec_ex(char *p, char *arg0, char *arg1, int n)
284
{
12245 bpr 285
  char *abuf[8];
286
  char errorfname[MAX_FNAME+1];
10 reyssat 287
 
12245 bpr 288
  if(robot_access) {
289
    *p=0; return;
290
  }
291
  if(!noout || !trusted_module() || is_class_module) {
292
    _calc_exec(p,arg0,arg1,n); if(outputing) output0(p);
293
    return;
294
  }
295
  mkfname(errorfname,"%s/exec.err",tmp_dir);
296
  abuf[0]=arg0; abuf[1]=arg1; abuf[n]=p; abuf[n+1]=NULL;
297
  wrapexec=1; exportall();
298
  execredirected(abuf[0],NULL,NULL,errorfname,abuf);
10 reyssat 299
}
300
 
8213 bpr 301
/* call shell. */
10 reyssat 302
void exec_sh(char *p)
303
{
12245 bpr 304
  _exec_ex(p,"sh","-c",2);
10 reyssat 305
}
306
 
8213 bpr 307
/* call perl. */
10 reyssat 308
void exec_perl(char *p)
309
{
12245 bpr 310
  _exec_ex(p,"perl","-e",2);
10 reyssat 311
}
312
 
8213 bpr 313
/* file should not be cached */
10 reyssat 314
void exec_nocache(char *p)
315
{    m_file.nocache|=1; *p=0; }
316
 
8214 bpr 317
/*
318
 * Read another file.
319
 * Read a file in order to perform tests.
320
 * Relevant global variables:
321
 * - m_file, a WORKING_FILE structure (should be restored to its original
322
 *   value at the end of the procedure)
323
 * - untrust, an integer (should be restored to its original
324
 *   value at the end of the procedure)
325
 * - outputing, an integer; its value is juste read.
326
 * - readnest, an integer, which is incremented during the function
327
 *   and decremented when it finishes.
328
 * Relevant global variables used in subprograms:
329
 * - module_prefix, a const char * which will be preprended before
330
 *   the filename, (see lines.c, label = openit).
331
 * Relevant wims_environment parameters:
332
 * - wims_read_parm  (should be restored to its original
333
 *   value at the end of the procedure)
334
 * - wims_trustfile: this parameter is only read.
335
 * Important procedures called by this one:
336
 * - phtml_put(char*, int)
337
 * - var_proc(char*, int)
338
 * - module_error(const char*)
339
 * @param p pointer to the filename, possibly followed by parameters
340
 */
341
 
10 reyssat 342
void exec_read(char *p)
343
{
12245 bpr 344
  WORKING_FILE save;
345
  char parmsave[MAX_LINELEN+1];
346
  char *pp,*ps;
347
  int t,cache;
7379 bpr 348
 
12245 bpr 349
  strip_trailing_spaces(p);p=find_word_start(p);
350
  if(*p==0) return;
351
  pp=find_word_end(p); if(*pp) *pp++=0;
352
  pp=find_word_start(pp);
353
  ps=getvar("wims_read_parm"); if(ps==NULL) ps="";
354
  mystrncpy(parmsave,ps,sizeof(parmsave));
355
  setvar("wims_read_parm",pp);
356
  memmove(&save,&m_file,sizeof(WORKING_FILE));
357
  cache=1; if(p[0]=='.' && p[1]=='/') {p+=2; cache=0;}
358
  t=untrust;
359
  if(strncmp(p,"wimshome/",9)==0) {
360
    if((untrust&255)!=0 &&
7379 bpr 361
        (m_file.name[0]=='/' || strncmp(m_file.name,"wimshome/",9)==0))
12245 bpr 362
      module_error("Illegal_file_access");
13691 bpr 363
    untrust|=0x200;
364
  }
12245 bpr 365
  if((untrust&0x202)!=0) {
366
    pp=getvar("wims_trustfile");
367
    if(pp!=NULL && *pp!=0 && wordchr(pp,p)!=NULL)
368
      untrust&=0xfdfd;
369
  }
370
  readnest++; if(readnest>MAX_READNEST) module_error("too_many_nested_read");
371
  if(outputing) phtml_put(p,cache); else var_proc(p,cache);
372
  readnest--; untrust=t;
373
  memmove(&m_file,&save,sizeof(WORKING_FILE));
374
  if (trace_file){
375
    int i;
376
    putc('\n',trace_file);
377
    for(i=1; i<=2*trace_indent-2; i++) putc(' ',trace_file);
378
    fprintf(trace_file,"%s:",m_file.filepath); fflush(trace_file);
379
  }
380
  setvar("wims_read_parm",parmsave);
10 reyssat 381
}
382
 
8213 bpr 383
/* read a variable processing file */
10 reyssat 384
void exec_readproc(char *p)
385
{
12245 bpr 386
  int o=outputing; outputing=0; exec_read(p); outputing=o;
10 reyssat 387
}
388
 
389
void exec_defread(char *p)
390
{
12245 bpr 391
  int t,o;
392
  secure_exec();
393
  t=untrust; untrust|=0x400;
394
  o=outputing; outputing=0;
395
  exec_read(p); untrust=t; outputing=o;
10 reyssat 396
}
397
 
8213 bpr 398
/* Change to another file (no return) */
10 reyssat 399
void exec_changeto(char *p)
400
{
12245 bpr 401
  m_file.linepointer=m_file.linecnt;
402
  exec_read(p);
10 reyssat 403
}
404
 
405
int header_executed=0, tail_executed=0;
406
 
8213 bpr 407
/* internal routine: get other language versions */
10 reyssat 408
void other_langs(void)
409
{
12245 bpr 410
  int i,j,k;
411
  char *p, lbuf[4], pbuf[MAX_FNAME+1], *phtml;
412
  char namebuf[MAX_FNAME+1], listbuf[4*MAX_LANGUAGES];
413
  static int otherlangs_got=0;
10 reyssat 414
 
12245 bpr 415
  if(otherlangs_got) return;
416
  mystrncpy(namebuf,module_prefix,sizeof(namebuf));
417
  listbuf[0]=0;j=strlen(namebuf)-3; p=listbuf;
418
  if(j>=2 && strcmp("light",namebuf+j-2)==0) {
419
    setvar("wims_light_module","yes");
420
    phtml=getvar("phtml");
421
    if(phtml==NULL || *phtml==0) return;
422
    mystrncpy(pbuf,phtml,sizeof(pbuf));
423
    j=strlen(pbuf)-3;
424
    if(pbuf[j]!='.') return;
425
    j++; ovlstrcpy(lbuf,pbuf+j); pbuf[j]=0;
426
    j=strlen(namebuf);
427
    snprintf(namebuf+j,MAX_FNAME-j-10,"/pages/%s",pbuf);
428
    j=strlen(namebuf);
429
    for(i=k=0;i<available_lang_no;i++) {
430
      if(strcmp(lbuf,available_lang[i])==0) continue;
431
      mystrncpy(namebuf+j,available_lang[i],MAX_FNAME-j);
432
      if(ftest(namebuf)<0) continue;
433
      if(k>0) *p++=',';
434
      mystrncpy(p,available_lang[i],sizeof(listbuf)-4-(p-listbuf));
435
      p+=strlen(p);
436
      k++;
10 reyssat 437
    }
12245 bpr 438
  goto end;
439
  }
440
  if(j>0 && namebuf[j]=='.') {
441
    setvar("wims_light_module","");
442
    j++; ovlstrcpy(lbuf,namebuf+j); namebuf[j]=0;
443
    for(i=k=0;i<available_lang_no;i++) {
444
      if(strcmp(lbuf,available_lang[i])==0) continue;
445
      snprintf(namebuf+j,MAX_FNAME-j,"%s/main.phtml",
446
           available_lang[i]);
447
      if(ftest(namebuf)<0) continue;
448
      if(k>0) *p++=',';
449
      mystrncpy(p,available_lang[i],sizeof(listbuf)-4-(p-listbuf));
450
      p+=strlen(p);
451
      k++;
10 reyssat 452
    }
12245 bpr 453
  }
454
  end: setvar("wims_otherlangs",listbuf); otherlangs_got=1;
10 reyssat 455
}
456
 
8213 bpr 457
/* Standardised reference to wims home */
10 reyssat 458
void exec_homeref(char *p)
459
{
12245 bpr 460
  char *ref, *user;
7379 bpr 461
 
12245 bpr 462
  if(ismhelp || tail_executed) return;
463
  setvar("wims_homeref_parm",p); *p=0;
464
  user=getvar("wims_user");
465
  if(user==NULL) user="";
466
  if(*user==0 || robot_access) ref=home_referer;
467
  else {
468
    if(strcmp(user,"supervisor")==0) ref=home_referer_supervisor;
469
    else ref=home_referer_user;
470
  }
471
  if(user[0]==0 && !robot_access) other_langs();
472
  phtml_put_base(ref,0); tail_executed=1;
10 reyssat 473
}
474
 
8213 bpr 475
/* Standardised header menu */
10 reyssat 476
void exec_headmenu(char *p)
477
{
12245 bpr 478
  char *ref, *user;
7379 bpr 479
 
12245 bpr 480
  if(header_executed) return;
481
  setvar("wims_headmenu_parm",p); *p=0;
482
  user=getvar("wims_user");
483
  if(user==NULL) user="";
484
  if(*user==0 || robot_access) ref=header_menu;
485
  else {
486
    if(strcmp(user,"supervisor")==0) ref=header_menu_supervisor;
487
    else ref=header_menu_user;
488
  }
489
  if(user[0]==0 && !robot_access) other_langs();
490
  phtml_put_base(ref,0); header_executed=1;
10 reyssat 491
}
492
 
8213 bpr 493
/* uniformized title */
10 reyssat 494
void exec_title(char *p)
495
{
12245 bpr 496
  char *s;
497
  *p=0;
498
  if(!outputing) return;
499
  s=getvar("wims_title_title");
500
  if(s==NULL || *s==0) {
501
    s=getvar("module_title");
502
    if(s==NULL || *s==0) return;
503
    force_setvar("wims_title_title",s);
504
  }
505
  phtml_put_base(title_page,0);
10 reyssat 506
}
507
 
8213 bpr 508
/* standardized html tail */
10 reyssat 509
void exec_tail(char *p)
510
{
12245 bpr 511
  if(!outputing || tail_executed) {
512
    *p=0; return;
513
  }
514
  if(!ismhelp) exec_homeref(p);
515
  *p=0;
516
  _output_("</body></html>");
517
  tail_executed=1;
10 reyssat 518
}
519
 
5925 bpr 520
void exec_formend(char *p)
521
{
12245 bpr 522
  _output_("\n</div></form>");
5925 bpr 523
}
524
 
10 reyssat 525
void determine_font(char *l);
526
 
11071 schaersvoo 527
/* see changelog wims_mathml for history */
13744 obado 528
void _headmathjax (char *p)
7379 bpr 529
{
12737 obado 530
  _output_("\n<script>/*<![CDATA[*/\
9618 schaersvoo 531
function wims_mathml_zoom(id){\
532
var math = document.getElementById(id);\
533
if(math.getAttribute(\"mathsize\") == \"100%\"){\
10964 obado 534
  math.setAttribute(\"mathsize\",\"200%\");}else{\
535
  math.setAttribute(\"mathsize\",\"100%\");};\
9618 schaersvoo 536
};\
14608 obado 537
window.addEventListener(\"load\", function() {\
13744 obado 538
  var ua = navigator.userAgent.toLowerCase();\
539
  var gecko = ua.indexOf(\"gecko\") > -1 && ua.indexOf(\"khtml\") === -1 && ua.indexOf(\"trident\") === -1;\
540
  if(! gecko ){\
541
    var script = document.createElement(\"script\");\
542
    script.src  = \"scripts/js/mathjax/MathJax.js?config=TeX-MML-AM_HTMLorMML\";\
543
    document.head.appendChild(script);\
544
  };\
545
});/*]]>*/</script>\n");
12737 obado 546
 
547
  // Safari/MathJax bugfix : refresh display when document ready.
548
  _output_("\n<script>\
549
reload_mathML=function(){var t=document.getElementsByClassName(\"wims_mathml\");Array.prototype.filter.call(t,function(t){var e=t.style.display;t.style.display=\"none\",t.style.opacity=\"0\",setTimeout(function(){t.style.display=e},1e3),setTimeout(function(){t.style.opacity=\"1\"},1100)})},document.onreadystatechange=function(){\"complete\"===document.readyState&&(/^((?!chrome|android).)*safari/i.test(navigator.userAgent)&&reload_mathML())};\
550
</script>");
7370 bpr 551
}
552
 
8213 bpr 553
/* standardized header */
1050 bpr 554
void _header(char *p, int option)
10 reyssat 555
{
12245 bpr 556
  char *s1, *s2, hbuf[MAX_LINELEN+1], *ws="", *ws2="", *bo, *ol;
557
  char wsbuf[MAX_LINELEN+1],wsbuf2[MAX_LINELEN+1];
558
  setvar("wims_header_parm",p); *p=0;
559
  if(!outputing || header_executed) return;
560
  s1=getvar("wims_window");
561
  if(mode==mode_popup) {
562
    if(s1!=NULL && *s1!=0) {
563
      char *p1, *p2;
564
      int t1,t2/*,t3,t4*/;
565
      p1=find_word_start(s1);
566
      for(p2=p1; myisdigit(*p2); p2++);
567
      *p2=0; t1=atoi(p1); p1=p2+1;
568
      while(!myisdigit(*p1) && *p1) p1++;
569
      for(p2=p1; myisdigit(*p2); p2++);
570
      *p2=0; t2=atoi(p1); p1=p2+1;
7379 bpr 571
/*        while(!myisdigit(*p1) && *p1) p1++;
572
        for(p2=p1; myisdigit(*p2); p2++);
573
        *p2=0; t3=atoi(p1); p1=p2+1;
574
        while(!myisdigit(*p1) && *p1) p1++;
575
        for(p2=p1; myisdigit(*p2); p2++);
576
        *p2=0; t4=atoi(p1); p1=p2+1;
577
        while(!myisdigit(*p1) && *p1) p1++;
578
        for(p2=p1; myisdigit(*p2); p2++);
579
        if(t3<5) t3=5; if(t4<20) t4=20;
12245 bpr 580
*/     snprintf(wsbuf,sizeof(wsbuf),
7379 bpr 581
          "window.focus();window.resizeTo(%d,%d);",t1,t2); ws=wsbuf;
582
/*      snprintf(wsbuf,sizeof(wsbuf),
583
             "window.focus();window.resizeto(%d,%d);window.moveto(%d,%d);",
584
             t1,t2,t3,t4); ws=wsbuf;
12245 bpr 585
*/   }
586
  }
587
  else {
588
    if(s1!=NULL && strcmp(s1,"new")==0)
7379 bpr 589
      ws="window.focus();window.resizeTo(800,640);window.moveTo(15,35);";
12245 bpr 590
  }
591
  if(strstr(session_prefix,"_exam")!=NULL) {
7379 bpr 592
/*    char buf[64]; */
12245 bpr 593
    if(*ws==0) ws="window.focus();";
594
    else ws= "window.focus();window.moveTo(5,70);";
7379 bpr 595
/*    snprintf(buf,sizeof(buf),"name.phtml.%s",lang);
596
      phtml_put_base(buf);
597
      phtml_put_base("jsclock.phtml"); */
12245 bpr 598
  }
599
  s1=getvar("wims_html_header"); if(s1==NULL) s1="";
600
  determine_font(getvar("module_language"));
601
  s2=getvar("module_title"); if(s2!=NULL && *s2!=0) {
602
    mystrncpy(hbuf,s2,sizeof(hbuf)); calc_detag(hbuf);
603
    setvar("module_title2",hbuf);
604
  }
605
  mystrncpy(hbuf,s1,sizeof(hbuf)); substit(hbuf);
606
  s2=getvar("wims_htmlbody"); if(s2==NULL) s2="";
607
  bo=getvar("wims_html_bodyoption"); if(bo==NULL) bo="";
608
  ws2=getvar("wims_html_onload"); if(ws2==NULL) ws2="";
609
  snprintf(wsbuf2,sizeof(wsbuf2),"%s%s",ws,ws2);
610
  setvar("wims_html_onload",wsbuf2);
611
  if(wsbuf2[0]) ol=" onload="; else ol="";
13744 obado 612
 
613
  output("<!DOCTYPE html>\
614
    \n<html lang=\"%s\"><head>\n\
615
    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, minimum-scale=1.0\"/>\n\
616
    %s\n", lang, hbuf);
617
 
618
  _headmathjax(p);
619
 
12245 bpr 620
  if(ol[0]) {
13744 obado 621
    output("</head>\n<body %s %s\"%s\" %s>", s2, ol, wsbuf2, bo);}
622
  else {
623
    output("</head>\n<body %s %s%s %s>", s2, ol, wsbuf2, bo);
12245 bpr 624
  }
625
  exec_headmenu(p);
626
  if(option) exec_title(p);
627
  if(cmd_type==cmd_help) {
628
    char *s=getvar("special_parm");
629
    if(s==NULL) s="";
630
    m_file.linepointer=m_file.linecnt;
631
    if(strcmp(s,"about")==0) ovlstrcpy(hbuf,"about.phtml");
632
    else ovlstrcpy(hbuf,"help.phtml");
633
    exec_read(hbuf); exec_tail(p); /* param of exec_...() must be readable */
634
    return;
635
  }
636
  header_executed=1;
10 reyssat 637
}
638
 
1050 bpr 639
void exec_header(char *p) { _header(p,1);}
640
void exec_header1(char *p) { _header(p,0);}
641
 
10 reyssat 642
char href_target[128];
643
char jsbuf[512];
5729 bpr 644
#define jsstr " onclick=\"%s=window.open('','%s','status=no,toolbar=no,location=no,menubar=no,scrollbars=yes,resizable=yes')\""
10 reyssat 645
int ref_mhelp=0;
646
int follow_list[]={
12245 bpr 647
  ro_session, ro_lang, ro_useropts, ro_module
10 reyssat 648
};
649
#define follow_no (sizeof(follow_list)/sizeof(follow_list[0]))
650
 
651
void _httpfollow(char b1[], char *wn, int new)
652
{
12245 bpr 653
  int i;
654
  char *p1, *s, *ss, sb[MAX_LINELEN+1], qbuf[MAX_LINELEN+1];
10 reyssat 655
 
12245 bpr 656
  sb[0]=0;
657
  for(i=0;i<follow_no;i++) {
658
    if(robot_access && follow_list[i]==ro_session) continue;
659
    if(!new && follow_list[i]!=ro_session
660
        && follow_list[i]!=ro_module && follow_list[i]!=ro_lang)
661
      continue;
662
    if(follow_list[i]==ro_module) {
663
      char *pp;
664
      if(new) continue;
665
      pp=strstr(b1,"cmd=");
666
      if(pp==NULL) continue;
667
      pp+=strlen("cmd=");
668
      if(memcmp(pp,"intro",strlen("intro"))==0 ||
669
         memcmp(pp,"new",strlen("new"))==0) continue;
670
    }
671
    s=getvar(ro_name[follow_list[i]]);
672
    ss=strstr(b1,ro_name[follow_list[i]]);
673
    if(s!=NULL && *s!=0 &&
674
      (ss==NULL || (ss>b1 && *(ss-1)!='&')
675
       || *(ss+strlen(ro_name[follow_list[i]]))!='=')) {
676
      if(follow_list[i]==ro_session && memcmp(href_target,"wims_",5)==0) {
677
        char st[MAX_LINELEN+1];
678
        char *s1;
679
        s1=getvar("wims_session");
680
        if(s1==NULL) internal_error("exec_href() error.\n");
681
        if(ref_mhelp) {
682
          if(strstr(s1,"_mhelp")!=0)
683
            snprintf(st,sizeof(st),"%s",s1);
684
          else
685
            snprintf(st,sizeof(st),"%s_mhelp",s1);
686
        }
7379 bpr 687
        else snprintf(st,sizeof(st),"%.10s%s",s1,href_target+4);
688
          s=st;
12245 bpr 689
      }
690
      snprintf(sb+strlen(sb),MAX_LINELEN-strlen(sb),"%s=%s&",
7379 bpr 691
             ro_name[follow_list[i]],s);
12245 bpr 692
      if(ismhelp && follow_list[i]==ro_session &&
7379 bpr 693
           strstr(sb,"_mhelp")==NULL)
12245 bpr 694
        snprintf(sb+strlen(sb)-1,MAX_LINELEN-strlen(sb)+1,"_mhelp&");
7379 bpr 695
      }
12245 bpr 696
  }
697
  snprintf(qbuf,MAX_LINELEN,"%s%s%s",wn,sb,b1);
698
  /* cleaning up query string */
699
  for(p1=qbuf;*p1;p1++) {
700
    if(*p1=='"') string_modify(qbuf,p1,p1+1,"%22");
701
    if(*p1=='&' && isalpha(*(p1+1))) {
702
      p1++; string_modify(qbuf,p1,p1,"+");
10 reyssat 703
    }
12245 bpr 704
  }
705
  mystrncpy(b1,qbuf,MAX_LINELEN);
10 reyssat 706
}
707
 
8213 bpr 708
/* Restart with a new module, using http code 302. */
10 reyssat 709
void exec_restart(char *p)
710
{
13887 obado 711
  /* buf will contain all the url parameters*/
712
  /* buf2 will be the final URL */
12245 bpr 713
  char buf[MAX_LINELEN+1], *rfn, buf2[MAX_LINELEN+1];
13887 obado 714
  /* long int t; */
7379 bpr 715
 
12245 bpr 716
  if(robot_access || outputing || !trusted_module() || is_class_module) return;
13887 obado 717
  /* accessfile(buf,"r","%s/restart.time",s2_prefix);
7379 bpr 718
    t=atoi(buf); if(t==nowtime) return;    */ /* possible looping */
12245 bpr 719
  mystrncpy(buf,find_word_start(p),sizeof(buf));
720
  *find_word_end(buf)=0;
721
  _httpfollow(buf,"",0);
13887 obado 722
  nph_header(302);
12245 bpr 723
  rfn=strchr(ref_name,':');
724
  if(rfn==NULL) {
725
    usual: snprintf(buf2,sizeof(buf2),"%s?%s",ref_name,buf);
726
  }
727
  else {
728
    char *p;
729
    p=getvar("wims_protocol");
730
    if(p!=NULL && strcmp(p,"https")==0) {
731
      snprintf(buf2,sizeof(buf2),"https%s?%s",rfn,buf);
10 reyssat 732
    }
12245 bpr 733
    else goto usual;
734
  }
735
  printf("Location: %s\r\n\r\n\
11726 bpr 736
<!DOCTYPE html>\
10 reyssat 737
<html><body><a href=\"%s\">%s</a></body></html>",buf2,buf2,buf2);
12245 bpr 738
  close_working_file(&m_file,0); write_logs();
739
  snprintf(buf,sizeof(buf),"%ld",(long)nowtime);
740
/*    accessfile(buf,"w","%s/restart.time",s2_prefix);*/
741
  delete_pid(); exit(0);
10 reyssat 742
}
743
 
12245 bpr 744
/*  extract target tag from parm string. */
10 reyssat 745
void href_find_target(char *p)
746
{
12245 bpr 747
  char *pp, *pe,buf1[MAX_LINELEN+1];
748
  href_target[0]=0; jsbuf[0]=0; ref_mhelp=0;
749
  for(pp=find_word_start(p);*pp!=0;pp=find_word_start(pe)) {
750
    pe=find_word_end(pp);
751
    if(strncasecmp(pp,"target=wims_",strlen("target=wims_"))!=0) continue;
752
    memmove(buf1,pp,pe-pp); buf1[pe-pp]=0; substit(buf1);
753
    if(strncasecmp(buf1,"target=wims_mhelp",strlen("target=wims_mhelp"))==0) {
754
      if(*pe!=0) *pe++=0;
755
      ovlstrcpy(href_target,"wims_help");
756
      ref_mhelp=1;
757
    }
758
    else {
759
      if(*pe!=0) *pe++=0;
760
        mystrncpy(href_target,buf1+strlen("target="),sizeof(href_target));
761
    }
762
    snprintf(jsbuf,sizeof(jsbuf),jsstr,href_target,href_target);
763
    ovlstrcpy(pp,pe);return;
764
  }
765
  pp=getvar("module_help");
766
  if(href_target[0]==0 && pp!=NULL && strcmp(pp,"popup")==0 &&
767
     (pe=strstr(p,"cmd=help"))!=NULL) {
768
    if(pe==p || *(pe-1)=='&') {
769
      ovlstrcpy(href_target,"wims_help"); ref_mhelp=1;
7379 bpr 770
      snprintf(jsbuf,sizeof(jsbuf),jsstr,href_target,href_target);
10 reyssat 771
    }
12245 bpr 772
  }
10 reyssat 773
}
774
 
775
void _href_getdef(char src[], char vname[], char buf[], int buflen)
776
{
12245 bpr 777
  char *p1, *p2, *p3;
778
  buf[0]=0;
779
  for(p1=strstr(src,vname); p1; p1=strstr(p2,vname)) {
780
    p2=p1+strlen(vname); if(*p2!='=') continue;
781
    if(p1>src && *(p1-1)!='&') continue;
782
    p2++; p3=strchr(p2,'&'); if(p3==NULL) p3=p2+strlen(p2);
783
    if(p3-p2>=buflen) return; /* too long */
784
    memmove(buf,p2, p3-p2); buf[p3-p2]=0; return;
785
  }
10 reyssat 786
}
787
 
8213 bpr 788
/* Create href to wims requests. subst() is not done. */
10 reyssat 789
void exec_href(char *p)
790
{
12245 bpr 791
  char *s, st[128], sti[128], stc[128], stt[128], *p1, *p2, *p3, *wn="";
792
  char *U="<span class=\"disabled_link\">%s</span>";
793
  char b1[MAX_LINELEN+1], b2[MAX_LINELEN+1];
794
  int new=0;
795
  if(!outputing) return;
796
  href_find_target(p);
797
  p1=find_word_start(p);
798
  p2=find_word_end(p1); if(*p2) *(p2++)=0;
799
  mystrncpy(b1,p1,sizeof(b1));
800
  mystrncpy(b2,find_word_start(p2),sizeof(b2));
801
  substit(b1); substit(b2);
802
  /* standard reference */
803
  if(*b2==0 && strchr(b1,'=')==NULL) {
804
    char b[MAX_LINELEN+1], *ll;
805
    p1=find_word_start(b1); *find_word_end(p1)=0;
806
    if(*p1==0 || strlen(p1)>64) return;
807
    ll=getvar("module_language");
808
    if(ll==NULL || *ll==0 || *(ll+1)==0 || *(ll+2)!=0) ll=lang;
809
    accessfile(b,"r","html/href.%s",ll);
810
    memmove(p1+1,p1,64); *p1='\n'; strcat(p1,"  ");
811
    p2=strstr(b,p1); if(p2==NULL) return;
812
    p1=find_word_start(p2+strlen(p1)); p2=find_word_end(p1);
813
    if(*p2) *(p2++)=0;
814
    p3=strchr(p2,'\n'); if(p3!=NULL) *p3=0;
10 reyssat 815
    mystrncpy(b1,p1,sizeof(b1));
816
    mystrncpy(b2,find_word_start(p2),sizeof(b2));
817
    substit(b1); substit(b2);
12245 bpr 818
  }
7379 bpr 819
    /* for robots: only references without defining cmd. */
12245 bpr 820
  if(robot_access && strstr(b1,"cmd=")!=NULL &&
821
      strstr(b1,"module=adm/doc")==NULL) {
822
    _output_(b2); return;
823
  }
824
  if(robot_access && strstr(aliased_cgi,"yes")!=NULL) {
825
    char mbuf[256], lbuf[16];
826
    _href_getdef(b1,"module",mbuf,sizeof(mbuf));
827
    if(mbuf[0]==0) mystrncpy(mbuf,home_module,sizeof(mbuf));
828
    _href_getdef(b1,"lang",lbuf,sizeof(lbuf));
829
    if(strlen(lbuf)!=2) {mystrncpy(lbuf,lang,4);lbuf[2]=0;}
830
    if(strncmp(mbuf,"adm/doc",strlen("adm/doc"))==0) {
831
      char dbuf[256], bbuf[256];
832
      _href_getdef(b1,"doc",dbuf,sizeof(dbuf));
833
      _href_getdef(b1,"block",bbuf,sizeof(bbuf));
834
      if(!myisdigit(dbuf[0])) dbuf[0]=0;
835
      if(dbuf[0]!=0 && bbuf[0]==0) snprintf(bbuf,sizeof(bbuf),"main");
836
      if(dbuf[0]==0)
837
        output("<a href=\"%s%s_doc~.html\">%s</a>", ref_base,lbuf,b2);
838
      else
839
        output("<a href=\"%s%s_doc~%s~%s.html\">%s</a>",
840
           ref_base,lbuf,dbuf,bbuf,b2);
10 reyssat 841
    }
12245 bpr 842
    else {
843
      for(s=strchr(mbuf,'/'); s!=NULL; s=strchr(s+1,'/')) *s='~';
844
      output("<a href=\"%s%s_%s.html\">%s</a>", ref_base,lbuf,mbuf,b2);
845
    }
7379 bpr 846
    return;
12245 bpr 847
  }
848
  s=getvar("wims_ref_id");
849
  if(s!=NULL && *s!=0 && !isspace(*s)) {
850
    snprintf(sti,sizeof(sti)," id=\"%s\"",s);
851
  }
852
  else sti[0]=0;
7379 bpr 853
 
12245 bpr 854
  s=getvar("wims_ref_class");
855
  if(s!=NULL && *s!=0 && !isspace(*s)) {
856
    snprintf(stc,sizeof(stc)," class=\"%s\"",s);
857
  }
858
  else stc[0]=0;
7379 bpr 859
 
12245 bpr 860
  s=getvar("wims_ref_title");
861
  if(s!=NULL && *s!=0 && !isspace(*s)) {
862
    snprintf(stt,sizeof(stt)," title=\"%s\"",s);
863
  }
864
  else stt[0]=0;
7379 bpr 865
 
12245 bpr 866
  s=getvar("wims_ref_target");
867
  if(href_target[0]!=0) s=href_target;
868
  if(s!=NULL && *s!=0 && !isspace(*s)) {
869
    snprintf(st,sizeof(st)," target=\"%s\"",s);
870
    if(strcmp(s,"_parent")!=0) {
871
      new=1; wn="wims_window=new&";
10 reyssat 872
    }
12245 bpr 873
  }
874
  else st[0]=0;
875
  _httpfollow(b1,wn,new);
876
  tohttpquery(b1);
877
  if(strstr(session_prefix,"_check")!=NULL) {
878
    if(*b2) output(U,b2);
879
  else _output_("<a id=\"0\"></a>");
880
  return;
881
  }
882
  if(jsbuf[0]==0 && st[0]==0 && strstr(session_prefix,"_exam")!=NULL) {
883
    p1=strstr(b1,"cmd=");
884
    if(p1!=NULL) {
885
      p1+=strlen("cmd=");
886
      if(strncmp(p1,"new",3)==0 || strncmp(p1,"renew",5)==0 ||
887
          strncmp(p1,"intro",5)==0) {
888
        if(*b2) output(U,b2);
889
        else _output_("<a id=\"#\"></a>");
890
        return;
7379 bpr 891
      }
10 reyssat 892
    }
12245 bpr 893
  }
894
  if(*b2)
895
    output("<a href=\"%s?%s\"%s%s %s %s %s>%s</a>",
896
       ref_name, b1, st, jsbuf,sti,stc,stt,b2);
897
  else
898
    output("<a href=\"%s?%s\"%s%s %s %s %s>",ref_name, b1, st, jsbuf,sti,stc,stt);
899
  setvar("wims_ref_id","");
900
  setvar("wims_ref_class","");
901
  setvar("wims_ref_title","");
10 reyssat 902
}
903
 
8213 bpr 904
/* Create form refering to the page. */
10 reyssat 905
void exec_form(char *p)
906
{
15130 obado 907
  char *s, *p1, *p2, *a, *m, *opt, st[128], *wn="", *form_id;
908
  char abuf[128], idbuf[128];
12245 bpr 909
  int i, new=0;
910
  if(!outputing) return;
911
  href_find_target(p);
912
  s=getvar("wims_ref_target");
913
  if(href_target[0]!=0) s=href_target;
914
  if(s!=NULL && *s!=0 && !isspace(*s)) {
915
    snprintf(st,sizeof(st)," target=\"%s\"",s);
916
    if(strcmp(s,"_parent")!=0) {
917
      new=1; wn="<input type=\"hidden\" name=\"wims_window\" value=\"yes\" />\n";
10 reyssat 918
    }
12245 bpr 919
  }
920
  else st[0]=0;
921
  a=getvar("wims_ref_anchor"); if(a==NULL) a="";
922
  opt=find_word_start(find_word_end(find_word_start(p)));
923
  m=getvar("wims_form_method");
924
  if(m!=NULL) {
925
    m=find_word_start(m);
926
    if(strncasecmp(m,"post",4)==0) m="post";
927
    else if(strncasecmp(m,"get",3)==0) m="get";
928
      else if(strncasecmp(m,"file",4)==0) {
929
       m="post\" enctype=\"multipart/form-data";
930
      snprintf(abuf,sizeof(abuf),"?form-data%ld%s",random(),a); a=abuf;
931
      force_setvar("wims_form_method","");
10 reyssat 932
    }
933
    else m=default_form_method;
12245 bpr 934
  }
935
  else m=default_form_method;
15130 obado 936
 
12245 bpr 937
  if(strstr(session_prefix,"_check")!=NULL) {
938
    output("<form action=\"NON_EXISTING_PAGE\" onsubmit=\"window.close();\" %s>\n",
7379 bpr 939
           opt);
12245 bpr 940
    return;
941
  }
15130 obado 942
 
943
  form_id=getvar("wims_form_id");
944
  if(form_id==NULL || *form_id==0 || isspace(*form_id)){
945
    form_id="";
946
  } else {
947
    snprintf(idbuf,sizeof(idbuf),"id=\"%s\"",find_word_start(form_id));form_id=idbuf;
948
  }
949
  force_setvar("wims_form_id","");
950
 
951
  output("<form action=\"%s%s\"%s method=\"%s\" %s %s>\n<div class='wims_form'>%s",ref_name,a,st,m,opt,form_id,wn);
12245 bpr 952
  if(a!=abuf && a[0]) force_setvar("wims_ref_anchor","");
953
  for(i=0;i<follow_no;i++) {
954
    if(robot_access && follow_list[i]==ro_session) continue;
955
    if(!new && follow_list[i]!=ro_session
956
     && follow_list[i]!=ro_module && follow_list[i]!=ro_lang)
957
      continue;
958
    if(follow_list[i]==ro_module) continue;
959
    s=getvar(ro_name[follow_list[i]]);
960
    if(s!=NULL && *s!=0) {
961
      if(follow_list[i]==ro_session && memcmp(href_target,"wims_",5)==0) {
962
        char st[MAX_LINELEN+1];
963
        char *s1;
964
        s1=getvar("wims_session");
965
        if(s1==NULL) internal_error("exec_form() error.\n");
966
        snprintf(st,sizeof(st),"%.10s%s",s1,href_target+4);
967
        s=st;
968
      }
969
      output("<input type=\"hidden\" name=\"%s\" value=\"%s\" />\n",
7379 bpr 970
           ro_name[follow_list[i]],s);
10 reyssat 971
    }
12245 bpr 972
  }
973
  p1=find_word_start(p);p2=find_word_end(p1);
974
  if(p2>p1) {
975
    char buf[64];
976
    int i;
977
    i=p2-p1; if(i>60) i=60;
978
    memmove(buf,p1,i);buf[i]=0;
979
    for(i=0;i<CMD_NO && strcmp(buf,commands[i]);i++);
980
    if(i<CMD_NO) {
981
      output("<input type=\"hidden\" name=\"cmd\" value=\"%s\" />\n",buf);
982
      if(i!=cmd_intro && i!=cmd_new)
983
        output("<input type=\"hidden\" name=\"module\" value=\"%s\" />\n",
984
            getvar(ro_name[ro_module]));
985
    }
986
  }
10 reyssat 987
}
988
 
8213 bpr 989
/* Creat link to trap robot access, an internal command
990
 * which should not be documented
991
 */
10 reyssat 992
void exec_robottrap(char *p)
993
{
12245 bpr 994
  char buf[MAX_LINELEN+1];
995
  if(robot_access) return;
996
  ovlstrcpy(buf,"session=$wims_session.1&module=adm/trap");
997
  _output_("<!-- "); exec_href(buf);_output_("Robot trapper, do not click!</a> -->");
14734 obado 998
  _output_("<div class='wimstrap hide'>");exec_href(buf); _output_("<span>Robot trapper, do not click!</span></a></div>");
10 reyssat 999
}
1000
 
8213 bpr 1001
/* set definitions in a file. Trusted modules only. */
10 reyssat 1002
void exec_setdef(char *p)
1003
{
12245 bpr 1004
   char *p1, *pp;
1005
   char nbuf[MAX_LINELEN+1], fbuf[MAX_LINELEN+1], tbuf[MAX_LINELEN+1];
1006
   if(robot_access || !trusted_module() || is_class_module) return;
1007
   p1=wordchr(p,"in"); if(p1==NULL) module_error("syntax_error");
1008
   *p1=0; p1=find_word_start(p1+strlen("in"));
1009
   ovlstrcpy(nbuf,p);
1010
   mystrncpy(tbuf,p1,sizeof(tbuf));
1011
   substit(nbuf); substit(tbuf);
1012
   if(find_module_file(tbuf,fbuf,1)) return;
1013
   pp=find_word_start(nbuf); p1=find_word_start(fbuf); *find_word_end(p1)=0;
1014
   strip_trailing_spaces(pp);
1015
   setdef(p1,pp);
10 reyssat 1016
}
1017
 
8213 bpr 1018
/* Set a variable. */
10 reyssat 1019
void exec_set(char *name)
1020
{
12245 bpr 1021
  char *p, *defn, *parm;
1022
  char tbuf2[MAX_LINELEN+1], namebuf[MAX_LINELEN+1];
1023
  int i;
10 reyssat 1024
 
12245 bpr 1025
  p=strchr(name,'=');
1026
  if(p==NULL) return; /* warning or error! */
1027
  *p=0; defn=find_word_start(p+1);
1028
  *find_word_end(name)=0;
1029
  mystrncpy(namebuf,find_word_start(name),sizeof(namebuf));
1030
  /* we allow substit in names, to implement array */
1031
  substit(namebuf); *find_word_end(namebuf)=0;
1032
  if(*defn!=calc_prefix_char) {
1033
  /* substitute by default */
1034
    mystrncpy(tbuf2,defn,sizeof(tbuf2));
1035
    substit(tbuf2); setvar(namebuf,tbuf2); return;
1036
  }
1037
  /* called from !readdef */
1038
  if((untrust&4)!=0) module_error("not_trusted");
1039
  /* definition is a command  */
1040
  parm=find_word_end(defn+1);
1041
  if( *parm != 0 ) { *parm=0; parm=find_word_start(parm+1); }
1042
  i=m_file.lines[m_file.l].varcode;
1043
  if(i<0) {
1044
    i=search_list(calc_routine,CALC_FN_NO,sizeof(calc_routine[0]),defn+1);
1045
    m_file.lines[m_file.l].varcode=i;
1046
  }
1047
  if(i<0) {
1048
      /* replace by warning? */
1049
    setvar(error_data_string,defn+1); module_error("bad_cmd");
1050
    return;
1051
  }
1052
  mystrncpy(tbuf2,parm,sizeof(tbuf2)); execnt++;
1053
  if(calc_routine[i].tag==0) substit(tbuf2);
1054
  tbuf2[sizeof(tbuf2)-1]=0; calc_routine[i].routine(tbuf2);
1055
      /* remove trailing new line */
1056
  tbuf2[sizeof(tbuf2)-1]=0;
1057
  if(tbuf2[strlen(tbuf2)-1]=='\n') tbuf2[strlen(tbuf2)-1]=0;
10 reyssat 1058
    setvar(namebuf,tbuf2);
1059
}
1060
 
8213 bpr 1061
/* set but do not overwrite. */
10 reyssat 1062
void exec_default(char *p)
1063
{
12245 bpr 1064
  char *start, *end, c, *pp;
1065
  char namebuf[MAX_LINELEN+1];
1066
  start=find_word_start(p);
1067
  for(end=start;*end!=0 && !isspace(*end) && *end!='='; end++);
1068
  c=*end; *end=0;
1069
  if(end-start<=MAX_LINELEN-1) {
1070
    memmove(namebuf,start,end-start+1); substit(namebuf);
1071
    pp=getvar(namebuf);
1072
    if(pp!=NULL && *pp!=0) return;
1073
  }
1074
  *end=c; exec_set(p);
10 reyssat 1075
}
1076
 
8213 bpr 1077
/* Does nothing; just a comment. */
10 reyssat 1078
void exec_comment(char *p)
1079
{
12245 bpr 1080
  return;
10 reyssat 1081
}
1082
 
8213 bpr 1083
/* Exit the file under interpretation */
10 reyssat 1084
void exec_exit(char *p)
1085
{
12245 bpr 1086
  m_file.linepointer=m_file.linecnt;
1087
  return;
10 reyssat 1088
}
1089
 
13438 obado 1090
/* output a file. Aliases:
7379 bpr 1091
     * getfile, outfile, fileout */
10 reyssat 1092
void exec_getfile(char *p)
1093
{
14941 bpr 1094
  char *s, sti[128], stc[128], stt[128], sgf[128], *p1, url[MAX_LINELEN+1];
12245 bpr 1095
  char *prompt;
7379 bpr 1096
 
12245 bpr 1097
  p=find_word_start(p); prompt=find_word_end(p);
1098
  if(*prompt!=0) *prompt++=0;
1099
  prompt=find_word_start(prompt);
1100
  if(*p==0 || !outputing) return;
1101
  if(!trusted_module() || is_class_module) return;
1102
  s=getvar(ro_name[ro_session]);
1103
  if(s==NULL || *s==0 || strstr(s,"robot")!=NULL) return;
1104
  mystrncpy(url,ref_name,sizeof(url));
1105
  for(p1=url+strlen(url);p1>url && *(p1-1)!='/'; p1--);
1106
  if(good_httpd) snprintf(p1,sizeof(url)+p1-url,
1107
              "getfile/%s?&+session=%s&+modif=%ld",
1108
              p,s,(long)nowtime);
1109
  else snprintf(url,sizeof(url),
1110
      "%s?cmd=getfile&+session=%s&+special_parm=%s&+modif=%ld",
8895 bpr 1111
          ref_name,s,p,(long)nowtime);
12245 bpr 1112
  snprintf(jsbuf,sizeof(jsbuf),jsstr,"wims_file","wims_file");
13438 obado 1113
 
1114
  s=getvar("wims_ref_id");
1115
  if(s!=NULL && *s!=0 && !isspace(*s)) {
1116
    snprintf(sti,sizeof(sti)," id=\"%s\"",s);
1117
  }
1118
  else sti[0]=0;
1119
 
1120
  s=getvar("wims_ref_class");
1121
  if(s!=NULL && *s!=0 && !isspace(*s)) {
1122
    snprintf(stc,sizeof(stc)," class=\"%s\"",s);
1123
  }
1124
  else stc[0]=0;
1125
 
1126
  s=getvar("wims_ref_title");
1127
  if(s!=NULL && *s!=0 && !isspace(*s)) {
1128
    snprintf(stt,sizeof(stt)," title=\"%s\"",s);
1129
  }
1130
  else stt[0]=0;
1131
 
14941 bpr 1132
  s=getvar("wims_getfile_fname");
1133
  if(s!=NULL && *s!=0 && !isspace(*s)) {
1134
    snprintf(sgf,sizeof(sgf)," download=\"%s\"",s);
1135
  }
1136
  else sgf[0]=0;
1137
 
1138
  if(*prompt) output("<a href=\"%s\" %s %s %s %s>%s</a>\n", url,sti,stc,stt,sgf,prompt);
13438 obado 1139
  else output("<a href=\"%s\" %s %s %s></a>",url,sti,stc,stt);
1140
 
1141
  setvar("wims_ref_id","");
1142
  setvar("wims_ref_class","");
1143
  setvar("wims_ref_title","");
14941 bpr 1144
  setvar("wims_getfile_fname","");
10 reyssat 1145
}
1146
 
8213 bpr 1147
/* internal */
10 reyssat 1148
void count_insert(void)
1149
{
12245 bpr 1150
  insert_no++;
1151
  if(insert_no>=INS_LIMIT) module_error("too_many_ins");
10 reyssat 1152
}
1153
 
1154
int animated_ins=0;
1155
int grouped_ins=0;
1156
 
8185 bpr 1157
/* generic insertion */
10 reyssat 1158
void _exec_ins(char *p, char *script_name,char *format)
1159
{
12245 bpr 1160
  char *s, *b, *at, *tag, *tag2, *al, *fmt, *mh;
1161
  char *p1, *pt;
1162
  char buf[1024],buf2[1024],url[MAX_LINELEN+1],altbuf[1024];
1163
  char outbuf[1024];
1164
  int border, middle, vspace;
1165
  long int tel;
10 reyssat 1166
 
12245 bpr 1167
  if(robot_access) return;
1168
  count_insert(); outbuf[0]=0;
1169
  setenv("ins_source",p,1); /* value kept from user tamper */
1170
  if(animated_ins) fmt=getvar("anim_format"); else fmt=format;
1171
  if(fmt==NULL) fmt="gif";
1172
  if(ismhelp) mh="mh"; else mh="";
1173
  snprintf(buf,sizeof(buf),"%s/insert%s-%d.%s",s2_prefix,mh,insert_no,fmt);
1174
  if(grouped_ins) {unlink(buf); goto grouped;}
1175
  exportall();
1176
  call_ssh("%s/%s %d %s >%s/ins.out 2>%s/ins.err",
1177
      bin_dir,script_name,insert_no,tmp_dir,tmp_dir,tmp_dir);
1178
  unlink(buf); wrapexec=1;
1179
  if(trusted_module()) setenv("trusted_module","yes",1);
1180
  else if(untrust) setenv("trusted_module","no",1);
1181
  call_ssh("mv %s/insert%s-%d.%s %s >/dev/null 2>/dev/null",
1182
       tmp_dir,mh,insert_no,fmt,s2_prefix);
1183
  tel=filelength("%s", buf);
1184
  if(tel<=5) {
1185
    char bbuf[MAX_LINELEN+1];
1186
    accessfile(bbuf,"r","%s/ins.err",tmp_dir);
1187
    snprintf(url,sizeof(url),"gifs/badins.gif");
1188
    for(p1=bbuf;p1<bbuf+512 && *p1;p1++)
7379 bpr 1189
      if(*p1=='<' || *p1=='>') *p1='?';
13691 bpr 1190
    *p1=0;
1191
    if(bbuf[0]==0) snprintf(bbuf,sizeof(bbuf),"Fail");
1192
    snprintf(outbuf+strlen(outbuf),sizeof(outbuf)-strlen(outbuf),
1193
       " <img src=\"%s\" alt=\"Error\" /> <small><pre>%s</pre></small> <br /> ",
1194
       url,bbuf);
1195
    setvar("ins_warn","fail");
1196
    setvar("ins_cnt","0");
1197
    goto reset;
12245 bpr 1198
  }
1199
  grouped:
1200
  s=getvar(ro_name[ro_session]);
1201
  b=getvar("ins_border"); at=getvar("ins_attr");
1202
  tag=getvar("ins_tag");  al=getvar("ins_align");
1203
  if(at==NULL) at="";
1204
  if(tag==NULL) tag="";
1205
  if(al==NULL) al="";
1206
  al=find_word_start(al);
1207
  if(*al!=0) snprintf(buf2,sizeof(buf2),"vertical-align:%s",al); else buf2[0]=0;
1208
  if(strcasecmp(al,"middle")==0) middle=1; else middle=0;
1209
  tag2=""; vspace=0;
1210
  if(*tag!=0) {
1211
    mystrncpy(buf,tag,sizeof(buf)); tag=find_word_start(buf);
1212
    tag2=find_word_end(tag);
1213
    if(*tag2!=0) *tag2++=0;
1214
    tag2=find_word_start(tag2);
1215
  }
1216
  if(b==NULL || *b==0) border=0;
1217
  else border=atoi(b);
1218
  if(border<0) border=0;
1219
  if(border>100) border=100;
1220
  if(middle) {
1221
    snprintf(outbuf+strlen(outbuf),
1222
       sizeof(outbuf)-strlen(outbuf),"%s",mathalign_sup1);
1223
    vspace=2;
1224
  }
1225
  mystrncpy(url,ref_name,sizeof(url));
1226
  for(p1=url+strlen(url);p1>url && *(p1-1)!='/'; p1--);
1227
  snprintf(p1,sizeof(url)+p1-url,
7379 bpr 1228
         "wims.%s?cmd=getins&+session=%s&+special_parm=insert%s-%d.%s&+modif=%ld",
8895 bpr 1229
         fmt,s,mh,insert_no,fmt,(long)nowtime);
12245 bpr 1230
  if(strchr(ins_alt,'"')!=NULL || strlen(ins_alt)>256) ins_alt[0]=0;
1231
  pt=getvar("wims_ins_alt"); if(pt==NULL) pt="";
12526 bpr 1232
  if(strcmp(pt,"empty")==0) altbuf[0]=0;
1233
  else
1234
    if(ins_alt[0] && strcmp(pt,"none")!=0)
1235
      snprintf(altbuf,sizeof(altbuf)," alt=\"\"");
1236
    else
1237
      snprintf(altbuf,sizeof(altbuf)," alt=\"%s\"",ins_alt);
12245 bpr 1238
  if(strcasecmp(tag,"form")!=0) {
1239
    snprintf(outbuf+strlen(outbuf),sizeof(outbuf)-strlen(outbuf),
1240
       "<img src=\"%s\" style=\"border:solid;border-width:%dpx;margin-bottom:%dpx;%s\" %s %s />",
1241
       url, border, vspace, buf2, at, altbuf);
1242
  }
1243
  else {
1244
    char *n, *nend;
6168 bpr 1245
/* fix: add quotes at name=" " */
12245 bpr 1246
    if(*tag2!=0) {n="name=\"" ; nend="\"";} else {n=""; nend="";}
1247
    snprintf(outbuf+strlen(outbuf),sizeof(outbuf)-strlen(outbuf),
1248
        "<input type=\"image\" %s%s%s src=\"%s\" style=\"border:solid;border-width:%dpx;margin-bottom:%dpx;%s\" %s %s />",
1249
        n,tag2,nend,url,border,vspace,buf2,at,altbuf);
1250
  }
1251
  if(middle) snprintf(outbuf+strlen(outbuf),
1252
           sizeof(outbuf)-strlen(outbuf),"%s",mathalign_sup2);
1253
  setvar("ins_warn",""); ins_alt[0]=0;
1254
  setvar("ins_cnt",int2str(insert_no));
1255
  reset:
1256
  if(outputing) _output_(outbuf);
1257
  setvar("ins_out",outbuf);
1258
  setvar("ins_attr",""); setvar("ins_tag","");
1259
  setvar("ins_url",url);
1260
  snprintf(buf2,sizeof(buf2),"insert%s-%d.%s",mh,insert_no,fmt);
1261
  setvar("ins_filename",buf2);
1262
  animated_ins=0;
10 reyssat 1263
}
1264
 
8155 bpr 1265
/* instex: dynamically insert tex outputs */
10 reyssat 1266
void exec_instex(char *p)
1267
{
12245 bpr 1268
  char *ts, *tc, *f, *mh, buf[MAX_FNAME+1];
7379 bpr 1269
 
12245 bpr 1270
  if(robot_access) {
1271
    *p=0; return;
1272
  }
1273
  f=instex_check_static(p); substit(p);
1274
  if(f==NULL) {
8155 bpr 1275
/* Use static instex if there is no real substitution
1276
 * and the source file is not in sessions directory.
1277
 */
12245 bpr 1278
    calc_instexst(p); if(outputing) _output_(p);
1279
    return;
1280
  }
1281
  if(ismhelp) mh="mh"; else mh="";
1282
  fix_tex_size(); f="gif";
1283
  setenv("texgif_style",instex_style,1);
1284
  tc=getvar("instex_texheader"); if (tc) { setenv("texgif_texheader",tc,1);}
1285
  setenv("texgif_tmpdir",tmp_dir,1);
1286
  setenv("texgif_src",p,1);
1287
  if(ins_alt[0]==0) mystrncpy(ins_alt,p,sizeof(ins_alt));
1288
  mkfname(buf,"%s/insert%s-%d.gif",tmp_dir,mh,insert_no+1);
1289
  setenv("texgif_outfile",buf,1);
1290
  ts=getvar("wims_texsize"); tc=getvar("instex_color");
1291
  if(lastout_file!=-1 && (tc==NULL || *tc==0) &&
1292
      (ts==NULL || *ts==0 || strcmp(ts,"0")==0) &&
1293
     strstr(p,"\\begin{")==NULL) {
1294
    int ls, ln;
1295
    char *pagebreak;
1296
    ls=strlen(instex_src); ln=strlen(instex_fname);
1297
    if(ls+strlen(p)>=MAX_LINELEN-256 ||
1298
     ln+strlen(buf)>=MAX_LINELEN-16) {
1299
        instex_flush(); ls=ln=0;
10 reyssat 1300
    }
12245 bpr 1301
    if(instex_cnt>0) pagebreak="\\pagebreak\n"; else pagebreak="";
1302
    snprintf(instex_src+ls,MAX_LINELEN-ls,"%s %s %s %s\n",
7379 bpr 1303
         pagebreak,instex_style,p,instex_style);
12245 bpr 1304
    snprintf(instex_fname+ln,MAX_LINELEN-ln,"%s\n",buf);
7379 bpr 1305
      grouped_ins=1;
12245 bpr 1306
  }
1307
  mkfname(buf,"%s/texgif.dvi",tmp_dir); unlink(buf);
1308
  wrapexec=0; _exec_ins(p,instex_processor,f);
1309
  if(grouped_ins) instex_cnt++;
1310
  grouped_ins=0;
10 reyssat 1311
}
1312
 
8152 bpr 1313
/* patches the gnuplot integer division (mis)feature. */
10 reyssat 1314
void gnuplot_patch(char *p,int oneline)
1315
{
12245 bpr 1316
  char *pp;
1317
  for(pp=strchr(p,'/');pp!=NULL;pp=strchr(pp+1,'/')) {
1318
    char *p1;
1319
    if(pp<=p || !myisdigit(*(pp-1)) || !myisdigit(*(pp+1))) continue;
1320
    for(p1=pp-2;p1>=p && myisdigit(*p1);p1--);
1321
    if(p1>=p && *p1=='.') continue;
1322
    for(p1=pp+2;*p1 && myisdigit(*p1);p1++);
1323
    if(*p1=='.') continue;
1324
    string_modify(p,p1,p1,".0");
1325
  }
1326
  for(pp=strchr(p,'^');pp!=NULL;pp=strchr(pp+1,'^'))
1327
    string_modify(p,pp,pp+1,"**");
8155 bpr 1328
/* disallow new lines and ';' */
12245 bpr 1329
  if(oneline)
1330
    for(pp=p;*pp!=0;pp++) if(*pp==';' || *pp=='\n') *pp=' ';
10 reyssat 1331
}
1332
 
8152 bpr 1333
/* This is to disable pipe in the gnuplot plotting function.
1334
 * We do not allow ' followed by < .
1335
 */
10 reyssat 1336
void prepare_insplot_parm(char *p)
1337
{
12245 bpr 1338
  int i,j,multanim; char *pp, *s;
1339
  double d;
1340
  char setbuf[MAX_LINELEN+10],buf[MAX_LINELEN+1];
7379 bpr 1341
 
12245 bpr 1342
  j=strlen(p);
8155 bpr 1343
/* pipe in plot command */
12245 bpr 1344
  for(i=0;i<j;i++) {
1345
    if(*(p+i)!='\'' && *(p+i)!='"') continue;
1346
    pp=find_word_start(p+i+1); if(*pp=='<') module_error("illegal_plot_cmd");
1347
  }
1348
  gnuplot_patch(p,1);
8155 bpr 1349
/* multiplot */
12245 bpr 1350
  multanim=0;
1351
  pp=getvar("insplot_split");
1352
  if(pp!=NULL) i=linenum(pp); else i=0;
8155 bpr 1353
/* arbitrary limit: 16 multiplots */
12245 bpr 1354
  if(i>16) i=16;
1355
  if(i>1) {
1356
    char tbuf[MAX_LINELEN*(i+1)+100], bbuf[MAX_LINELEN+1];
1357
    tbuf[0]=0;
1358
    if(*p!=0) snprintf(tbuf,sizeof(tbuf),"%s\n",p);
1359
    snprintf(buf,sizeof(buf),"%d",i); setenv("multiplot",buf,1);
1360
    for(j=1;j<=i;j++) {
1361
      snprintf(buf,sizeof(buf),"insplot_parm_%d",j);
1362
      pp=getvar(buf);
1363
      if(pp==NULL || *pp==0) {
1364
        if(j==1 && *p!=0) continue;
1365
        pp="";
1366
      }
1367
      else {
1368
        mystrncpy(bbuf,pp,sizeof(bbuf));
1369
        gnuplot_patch(bbuf,1);
1370
      }
1371
      strcat(tbuf,bbuf);strcat(tbuf,"\n");
1372
    }
7379 bpr 1373
    setenv("insplot_source",tbuf,1);
1374
    if(varchr(tbuf,"s")!=NULL) multanim=1;
12245 bpr 1375
  }
8155 bpr 1376
/* no illegal chaining */
12245 bpr 1377
  pp=getvar("insplot_font"); if(pp!=NULL) {
1378
    for(s=pp;s<pp+MAX_LINELEN && *s;s++)
1379
    if(*s==';' || *s=='\n' || *s==' ') *s=0;
1380
    if(s>=pp+MAX_LINELEN) *s=0;
1381
    setvar("insplot_font",pp);
1382
  }
1383
  pp=getvar("insplot_set");
1384
  if(pp!=NULL) {
1385
    char tbuf[MAX_LINELEN+1];
1386
    mystrncpy(tbuf,pp,sizeof(tbuf));
1387
    i=strlen(tbuf)-1;
1388
    while(i>0 && isspace(tbuf[i])) i--;
1389
    if(tbuf[i]==';') tbuf[i]=0;
1390
    gnuplot_patch(tbuf,0);pp=tbuf;
1391
    ovlstrcpy(setbuf,"set "); j=strlen("set ");
1392
    for(i=0; *(pp+i)!=0 && j<MAX_LINELEN; i++) {
1393
      if(*(pp+i)=='\n') {setbuf[j++]=' '; continue;}
1394
      if(*(pp+i)!=';') {setbuf[j++]=*(pp+i); continue;}
1395
      ovlstrcpy(setbuf+j,"\nset "); j+=strlen("\nset ");
10 reyssat 1396
    }
12245 bpr 1397
    setbuf[j]=0;
1398
    setenv("insplot_set",setbuf,1);
1399
  }
1400
  else setenv("insplot_set","",1);
8155 bpr 1401
/* frames of animation */
12245 bpr 1402
  pp=getvar("ins_anim_frames");
1403
  if(pp!=NULL) i=evalue(pp); else i=1;
1404
  if(i>=ANIM_LIMIT) i=ANIM_LIMIT-1;
1405
  if(i<1) i=1;
1406
  if(strstr(setbuf,"step")==NULL && strstr(p,"step")==NULL
1407
     && varchr(setbuf,"s")==NULL && varchr(p,"s")==NULL && !multanim) i=1;
1408
  setenv("ins_anim_frames",int2str(i),1);
1409
  setvar("ins_anim_frames","");
1410
  if(i>1) {setvar("ins_animation","yes");animated_ins=1;}
1411
  else setvar("ins_animation","no");
8155 bpr 1412
/* delay of animation */
12245 bpr 1413
  pp=getvar("ins_anim_delay");
1414
  if(pp!=NULL) d=evalue(pp); else d=0;
1415
  if(d>=10) d=10;
1416
  if(d<0) d=0;
1417
  setenv("ins_anim_delay",int2str(d*100),1);
10 reyssat 1418
}
1419
 
8152 bpr 1420
/* Insert dynamic 2d plot */
10 reyssat 1421
void exec_insplot(char *p)
1422
{
12245 bpr 1423
  char *fmt;
1424
  if(robot_access) {
1425
    *p=0; return;
1426
  }
1427
  fmt=getvar("ins_format"); if(fmt==NULL || *fmt==0) fmt=DEFAULT_INS_FORMAT;
1428
  prepare_insplot_parm(p); setenv("insplot_method","2D",1);
1429
  _exec_ins(p,insplot_processor,fmt);
1430
  wrapexec=1;
10 reyssat 1431
/*    call_ssh("mv %s/insplot_cmd %s 2>/dev/null",tmp_dir,s2_prefix); */
12245 bpr 1432
   unsetenv("multiplot"); setvar("insplot_split","");
10 reyssat 1433
}
1434
 
8213 bpr 1435
/* Insert dynamic 3d plot */
10 reyssat 1436
void exec_insplot3d(char *p)
1437
{
12245 bpr 1438
  char *fmt;
1439
  if(robot_access) {
1440
    *p=0; return;
1441
  }
1442
  fmt=getvar("ins_format"); if(fmt==NULL || *fmt==0) fmt=DEFAULT_INS_FORMAT;
1443
  prepare_insplot_parm(p); setenv("insplot_method","3D",1);
1444
  _exec_ins(p,insplot_processor,fmt);
1445
  wrapexec=1;
10 reyssat 1446
/*     call_ssh("mv %s/insplot_cmd %s 2>/dev/null",tmp_dir,s2_prefix); */
12245 bpr 1447
  unsetenv("multiplot");setvar("insplot_split","");
10 reyssat 1448
}
1449
 
8155 bpr 1450
/* Insert dynamic gif draw. The parm preparation is specific to fly. */
10 reyssat 1451
void exec_insdraw(char *p)
1452
{
12245 bpr 1453
  char *pp, *fmt;
1454
  int i;
1455
  double d;
7379 bpr 1456
 
12245 bpr 1457
  if(robot_access) {
1458
    *p=0; return;
1459
  }
7370 bpr 1460
/*    calc_tolower(p);*/
12245 bpr 1461
  fmt=getvar("ins_format"); if(fmt==NULL || *fmt==0) fmt=DEFAULT_INS_FORMAT;
1462
  while((pp=wordchr(p,"output"))!=NULL) memmove(pp,"zqkwfx",6);
8155 bpr 1463
/* frames of animation */
12245 bpr 1464
  pp=getvar("ins_anim_frames");
1465
  if(pp!=NULL) i=evalue(pp); else i=1;
1466
  if(i>=ANIM_LIMIT) i=ANIM_LIMIT-1;
1467
  if(i<1) i=1;
1468
  if(i>1 && varchr(p,"s")==NULL && varchr(p,"animstep")==NULL
1469
     && varchr(p,"step")==NULL) i=1;
1470
  setenv("ins_anim_frames",int2str(i),1);
1471
  setvar("ins_anim_frames","");
1472
  if(i>1) {setvar("ins_animation","yes");animated_ins=1;}
1473
  else setvar("ins_animation","no");
8155 bpr 1474
/* delay of animation */
12245 bpr 1475
  pp=getvar("ins_anim_delay");
1476
  if(pp!=NULL) d=evalue(pp); else d=0;
1477
  if(d>=10) d=10;
1478
  if(d<0) d=0;
1479
  setenv("ins_anim_delay",int2str(d*100),1);
1480
  pp=getvar("insdraw_filebase");
1481
  if(pp!=NULL && strstr(pp,parent_dir_string)!=NULL)
1482
    setvar("insdraw_filebase","");
1483
  _exec_ins(p,insdraw_processor,fmt);
10 reyssat 1484
}
1485
 
1486
void exec_increase(char *p)
1487
{
12245 bpr 1488
  char *p1, *p2;
1489
  p1=find_word_start(p); p2=find_word_end(p1);
1490
  if(p2<=p1) {
1491
    *p=0; return;
1492
  }
1493
  *p2=0;p2=getvar(p1);
1494
  if(p2==NULL) p2="";
1495
  setvar(p1,int2str(atoi(p2)+1)); *p=0;
10 reyssat 1496
}
1497
 
8957 bpr 1498
void exec_setseed(char *p)
12245 bpr 1499
{
1500
  char buf[64];
1501
  snprintf(buf,sizeof(buf),"%s",p);
1502
  force_setvar("wims_seed",buf);
1503
  double d=evalue(p);
1504
  srandom(d); *p=0;
8957 bpr 1505
}
1506
 
7370 bpr 1507
/* bound a variable */
10 reyssat 1508
void exec_bound(char *p)
1509
{
12245 bpr 1510
  char *p1, *p2, *p3;
1511
  int doub,i,bcnt,defaulted;
1512
  double d1,d2,dd,val;
1513
  char nbuf[MAX_LINELEN+1],lbuf[MAX_LINELEN+1],dbuf[MAX_LINELEN+1];
1514
  char vbuf[MAX_LINELEN+1];
1515
  char *blist[2048];
7379 bpr 1516
 
12245 bpr 1517
  p1=find_word_start(p); p2=find_word_end(p1);
1518
  if(*p2==0) {
1519
    syntax: module_error("syntax_error");
1520
  }
1521
  *p2=0; ovlstrcpy(nbuf,p1);substit(nbuf); p1=find_word_start(p2+1);
1522
  p2=getvar(nbuf);if(p2==NULL) p2="";
1523
  mystrncpy(vbuf,find_word_start(p2),sizeof(vbuf));
1524
  strip_trailing_spaces(vbuf);
1525
  p2=find_word_end(p1); if(*p2==0) goto syntax;
1526
  *p2=0;p2++;
1527
  p3=wordchr(p2,"default");
1528
  if(p3!=NULL) {
1529
    *p3=0; defaulted=1;
1530
    p3=find_word_start(p3+strlen("default"));
1531
    ovlstrcpy(dbuf,p3); substit(dbuf);
1532
  }
1533
  else defaulted=0;
1534
  if(strcmp(p1,"between")==0) {
1535
    p1=find_word_start(p2);
1536
    i=strlen("integer");
1537
    if(strncmp(p1,"integer",i)==0 &&
1538
        (isspace(*(p1+i)) || (*(p1+i)=='s' && isspace(*(p1+i+1))))) {
1539
      doub=0; p1=find_word_start(find_word_end(p1));
1540
      val=rint(evalue(vbuf));
1541
      if(vbuf[0]) float2str(val,vbuf);
10 reyssat 1542
    }
12245 bpr 1543
    else {
1544
      doub=1;val=evalue(vbuf);
10 reyssat 1545
    }
12245 bpr 1546
    p2=wordchr(p1,"and"); p3=p2+strlen("and");
1547
    if(p2==NULL) {
1548
      p2=strchr(p1,','); p3=p2+1;
1549
    }
1550
    if(p2==NULL) goto syntax;
1551
   *p2=0;p2=find_word_start(p3);
1552
    if(*p1==0 || *p2==0) goto syntax;
1553
    d1=evalue(p1);d2=evalue(p2);
1554
    if(!isfinite(d1) || !isfinite(d2) ||
1555
      fabs(d1)>(double)(1E10) || fabs(d2)>(double)(1E10)) goto syntax;
1556
    if(d1>d2) {
1557
      dd=d1;d1=d2;d2=dd;
1558
    }
1559
    if(vbuf[0] && val<=d2 && val>=d1) {
1560
      if(!doub) setvar(nbuf,vbuf);
1561
      *p=0; return;
1562
    }
1563
    if(defaulted) ovlstrcpy(p,dbuf);
1564
    else {
7379 bpr 1565
      if(!doub) {
12245 bpr 1566
        d1=ceil(d1);d2=floor(d2);
7379 bpr 1567
      }
1568
      if(vbuf[0]==0 || val<d1) val=d1;
1569
      else val=d2;
1570
      float2str(val,p);
10 reyssat 1571
    }
12245 bpr 1572
    setvar(nbuf,p); *p=0; return;
1573
  }
1574
  else {
7379 bpr 1575
    if(strcmp(p1,"within")==0 || strcmp(p1,"among")==0) {
12245 bpr 1576
      ovlstrcpy(lbuf,p2);substit(lbuf);
1577
      bcnt=cutitems(lbuf,blist,2048);
1578
      if(bcnt<=0) {
1579
        *p=0; return;
1580
      }
1581
      for(i=0;i<bcnt;i++) {
1582
        if(strcmp(blist[i],vbuf)==0) {
7379 bpr 1583
          *p=0; return;
1584
        }
12245 bpr 1585
      }
1586
      if(defaulted) ovlstrcpy(p,dbuf); else ovlstrcpy(p,blist[0]);
1587
      setvar(nbuf,p); *p=0;
1588
      return;
10 reyssat 1589
    }
12245 bpr 1590
  else goto syntax;
1591
  }
10 reyssat 1592
}
1593
 
8155 bpr 1594
/* detrust the module. */
10 reyssat 1595
void exec_detrust(char *p)
12245 bpr 1596
{
1597
  untrust|=1; *p=0;
1598
}
10 reyssat 1599
 
1600
void exec_warn(char *p)
1601
{
12245 bpr 1602
  char *p1,*p2;
1603
  char buf[MAX_FNAME+1];
1604
  WORKING_FILE save;
10 reyssat 1605
 
12245 bpr 1606
  if(!outputing) goto end;
1607
  p1=find_word_start(p);p2=find_word_end(p1);
1608
  if(p2<=p1) goto end;
1609
  *p2=0;
1610
  snprintf(buf,sizeof(buf),"wims_warn_%s",p1);
1611
  p2=getvar(buf);
1612
  if(p2==NULL || *p2==0) goto end;
1613
  p2=getvar("module_language");if(p2==NULL) p2="en";
1614
  mkfname(buf,"msg/warn_%s.phtml.%s",p1,p2);
1615
  memmove(&save,&m_file,sizeof(WORKING_FILE));
1616
  if(open_working_file(&m_file,buf)==0) phtml_put(NULL,0);
1617
  memmove(&m_file,&save,sizeof(WORKING_FILE));
1618
  end:
1619
  *p=0; return;
10 reyssat 1620
}
1621
 
8155 bpr 1622
/* write an error message. */
10 reyssat 1623
void exec_msg(char *p)
1624
{
12245 bpr 1625
  char *p1,*p2, buf[64], *l;
1626
  secure_exec();
1627
  p1=find_word_start(p); p2=find_word_end(p1);
1628
  if(*p2) {
1629
    *p2=0; p2=find_word_start(p2+1);
1630
  }
1631
  force_setvar("wims_error",p1); force_setvar("wims_error_parm",p2);
1632
  l=getvar("module_language");
1633
  if(l!=NULL && strlen(l)==2) {
1634
    snprintf(buf,sizeof(buf),"msg.phtml.%s",l);
1635
    phtml_put_base(buf,0);
1636
  }
1637
  *p=0;
10 reyssat 1638
}
1639
 
8185 bpr 1640
struct distr_cmd distr_cmd[]={
12245 bpr 1641
  {"char",      NULL},
1642
  {"charof",    NULL},
1643
  {"chars",     NULL},
1644
  {"charsof",   NULL},
1645
  {"item",      cutitems},
1646
  {"itemof",    cutitems},
1647
  {"items",     cutitems},
1648
  {"itemsof",   cutitems},
1649
  {"line",      cutlines},
1650
  {"lineof",    cutlines},
1651
  {"lines",     cutlines},
1652
  {"linesof",   cutlines},
1653
  {"list",      cutitems},
1654
  {"word",      cutwords},
1655
  {"wordof",    cutwords},
1656
  {"words",     cutwords},
1657
  {"wordsof",   cutwords}
10 reyssat 1658
};
1659
 
8185 bpr 1660
int distr_cmd_no=(sizeof(distr_cmd)/sizeof(distr_cmd[0]));
10 reyssat 1661
 
8155 bpr 1662
/* distribute a number of lines, items, etc. into a list of vars. */
10 reyssat 1663
void exec_distribute(char *p)
1664
{
12245 bpr 1665
  int i,k,n;
1666
  char *p1, *p2;
1667
  char bf1[MAX_LINELEN+1],bf2[MAX_LINELEN+1];
1668
  char *names[4096],*vals[4096];
1669
  p1=find_word_start(p); p2=find_word_end(p1);
1670
  if(p2<=p1 || *p2==0) module_error("syntax_error");
1671
  *p2++=0;
1672
  i=search_list(distr_cmd,distr_cmd_no,sizeof(distr_cmd[0]),p1);
1673
  if(i<0) module_error("syntax_error");
1674
  p2=find_word_start(p2); p1=wordchr(p2,"into");
1675
  if(p1==NULL) module_error("syntax_error");
1676
  *p1=0;mystrncpy(bf1,p2,sizeof(bf1));
1677
  p1=find_word_start(p1+strlen("into"));
1678
  mystrncpy(bf2,p1,sizeof(bf2));
1679
  substit(bf1);substit(bf2);
1680
  strip_trailing_spaces(bf1);
1681
  items2words(bf2); n=cutwords(bf2,names,4096);
1682
  if(distr_cmd[i].routine!=NULL) {
1683
    k=distr_cmd[i].routine(bf1,vals,n);
1684
    for(i=0;i<k;i++) setvar(names[i],vals[i]);
1685
    for(;i<n;i++) setvar(names[i],"");
1686
  }
1687
  else {
1688
    char buf[2];
1689
    buf[1]=0;
1690
    for(p1=bf1,i=0;i<n;i++) {
1691
      buf[0]=*p1; if(*p1) p1++;
1692
      setvar(names[i],buf);
10 reyssat 1693
    }
12245 bpr 1694
  }
10 reyssat 1695
}
1696
 
8155 bpr 1697
/* reset variables */
10 reyssat 1698
void exec_reset(char *p)
1699
{
12245 bpr 1700
  char *p1, *p2;
10 reyssat 1701
 
12245 bpr 1702
  items2words(p);
1703
  for(p1=find_word_start(p); *p1; p1=find_word_start(p2)) {
1704
    p2=find_word_end(p1); if(*p2) *p2++=0;
1705
    setvar(p1,"");
1706
  }
10 reyssat 1707
}
1708
 
8155 bpr 1709
/* exchange the values of two variables */
10 reyssat 1710
void exec_exchange(char *p)
1711
{
12245 bpr 1712
  char buf[MAX_LINELEN+1],b1[MAX_LINELEN+1],b2[MAX_LINELEN+1];
1713
  char *p1,*p2,*pb;
1714
  p1=wordchr(p,"and");
1715
  if(p1!=NULL) {
1716
    *p1=0; p2=find_word_start(p1+strlen("and"));
1717
  }
1718
  else {
1719
    p1=strchr(p,',');
1720
    if(p1==NULL) module_error("syntax_error");
1721
    *p1=0; p2=find_word_start(p1+1);
1722
  }
1723
  p1=find_word_start(p);
1724
  mystrncpy(b1,p1,sizeof(b1)); substit(b1); *find_word_end(b1)=0;
1725
  mystrncpy(b2,p2,sizeof(b2)); substit(b2); *find_word_end(b2)=0;
1726
  if(*b1==0 || *b2==0) module_error("syntax_error");
1727
  pb=getvar(b1);if(pb==NULL) pb="";
1728
  mystrncpy(buf,pb,sizeof(buf));
1729
  pb=getvar(b2);if(pb==NULL) pb="";
1730
  setvar(b1,pb); setvar(b2,buf);
10 reyssat 1731
}
1732
 
8155 bpr 1733
/* Send a mail */
10 reyssat 1734
void exec_mailto(char *p)
1735
{
12245 bpr 1736
  char *p1,*p2,*pp;
10 reyssat 1737
 
12245 bpr 1738
  if(!trusted_module() || is_class_module) return;
1739
  p1=strchr(p,'\n'); if(p1==NULL) return;
1740
  *p1++=0; p=find_word_start(p);
1741
  if(*p==0) return;
1742
  p2=strchr(p1,'\n'); if(p2==NULL) return;
1743
  *p2++=0;
1744
  for(pp=p1;*pp;pp++) if(*pp=='"' || *pp=='\n') *pp=' ';
1745
  accessfile(p2,"w","%s/mail.body",tmp_dir);
1746
  wrapexec=1;
14281 obado 1747
  call_sh("mail %s -s \" %s \nContent-Type: text/html\" %s <%s/mail.body; chmod og-rwx %s/mail.body",
12245 bpr 1748
      mail_opt, p1,p,tmp_dir,tmp_dir);
1749
  mail_log(p);
1750
  *p=0;
10 reyssat 1751
}
1752
 
8155 bpr 1753
/* Generates a user error. Internal and undocumented. */
10 reyssat 1754
void exec_usererror(char *p)
1755
{
12245 bpr 1756
  if(trusted_module()) user_error(p);
10 reyssat 1757
}
1758
 
8155 bpr 1759
/* stop output. */
10 reyssat 1760
void exec_directout(char *p)
1761
{
12245 bpr 1762
  if(outputing || !trusted_module()) return;
1763
  printf("%s",p);
1764
  noout=1;
10 reyssat 1765
}
1766
 
1767
enum {
12245 bpr 1768
  EXEC_IF, EXEC_JUMP, EXEC_ELSE, EXEC_ENDIF, EXEC_EXEC,
1769
  EXEC_WHILE, EXEC_ENDWHILE,
1770
  EXEC_FOR, EXEC_VAR, EXEC_DEBUG, EXEC_DAEMON,
1771
  EXEC_SET, EXEC_DEFAULT, EXEC_COMMENT, EXEC_READ, EXEC_HREF,
1772
  EXEC_INS, EXEC_STRING, EXEC_PEDIA, EXEC_DIR,
1773
  EXEC_TRUST, EXEC_WARN, EXEC_ERROR, EXEC_SQL, EXEC_SCORE,
1774
  EXEC_MAIL, EXEC_OTHER
10 reyssat 1775
} EXEC_TYPES;
1776
#define EXEC_SUBST 0x1000
1777
#define EXEC_USECALC 0x2000
1778
#define EXEC_PROCTOO 0x4000
1779
MYFUNCTION exec_routine[]={
12245 bpr 1780
  {"!",        EXEC_COMMENT,        exec_comment},
1781
  {"TeXmath",  EXEC_STRING|EXEC_SUBST|EXEC_USECALC,texmath},
1782
  {"add",      EXEC_STRING|EXEC_USECALC,calc_sum},
1783
  {"advance",  EXEC_VAR|EXEC_SUBST,   exec_increase},
1784
  {"append",   EXEC_STRING|EXEC_USECALC,calc_append},
1785
  {"appendfile",EXEC_DIR|EXEC_SUBST,    fileappend},
1786
  {"bound",     EXEC_STRING,       exec_bound},
1787
  {"break",     EXEC_FOR,        exec_break},
1788
  {"call",      EXEC_EXEC|EXEC_SUBST|EXEC_PROCTOO|EXEC_USECALC,    calc_exec},
1789
  {"changeto",  EXEC_READ|EXEC_SUBST,    exec_changeto},
1790
  {"char",      EXEC_STRING|EXEC_USECALC,calc_charof},
1791
  {"chars",     EXEC_STRING|EXEC_USECALC,calc_charof},
1792
  {"checkhost", EXEC_STRING|EXEC_USECALC|EXEC_SUBST,calc_checkhost},
1793
  {"column",    EXEC_STRING|EXEC_USECALC,calc_columnof},
1794
  {"columns",   EXEC_STRING|EXEC_USECALC,calc_columnof},
1795
  {"comment",   EXEC_COMMENT,        exec_comment},
1796
  {"daemon",    EXEC_DAEMON|EXEC_USECALC|EXEC_SUBST,calc_daemon},
1797
  {"date",      EXEC_STRING|EXEC_USECALC|EXEC_SUBST,calc_date},
1798
  {"deaccent",  EXEC_STRING|EXEC_USECALC|EXEC_SUBST,deaccent},
1799
  {"debug",     EXEC_DEBUG,        calc_debug},
1800
  {"declosing", EXEC_STRING|EXEC_USECALC|EXEC_SUBST,calc_declosing},
1801
  {"def",       EXEC_SET,        exec_set},
1802
  {"default",   EXEC_SET,        exec_default},
1803
  {"define",    EXEC_SET,        exec_set},
1804
  {"definitionof", EXEC_SCORE|EXEC_USECALC,calc_defof},
1805
  {"defof",     EXEC_SCORE|EXEC_USECALC,calc_defof},
1806
  {"defread",   EXEC_READ|EXEC_SUBST,    exec_defread},
1807
  {"detag",     EXEC_STRING|EXEC_USECALC|EXEC_SUBST,calc_detag},
1808
  {"detrust",   EXEC_TRUST,        exec_detrust},
7379 bpr 1809
/*      {"dictionary",EXEC_STRING|EXEC_USECALC,calc_dictionary},    */
12245 bpr 1810
  {"dir",       EXEC_DIR|EXEC_SUBST|EXEC_USECALC,calc_listfile},
1811
  {"distribute",EXEC_STRING,        exec_distribute},
1812
  {"distrust",  EXEC_TRUST,        exec_detrust},
1813
  {"else",      EXEC_ELSE,        exec_else},
1814
  {"embraced",  EXEC_STRING|EXEC_USECALC,calc_embraced},
1815
  {"encyclo",   EXEC_PEDIA|EXEC_SUBST|EXEC_USECALC,pedia},
1816
  {"encyclopedia",EXEC_PEDIA|EXEC_SUBST|EXEC_USECALC,pedia},
1817
  {"endif",     EXEC_ENDIF,        exec_endif},
1818
  {"endwhile",  EXEC_ENDWHILE,        exec_endwhile},
1819
  {"evalsubst", EXEC_STRING|EXEC_USECALC,calc_evalsubst},
1820
  {"evalsubstit",EXEC_STRING|EXEC_USECALC,calc_evalsubst},
1821
  {"evalsubstitute",EXEC_STRING|EXEC_USECALC,calc_evalsubst},
1822
  {"evaluesubst",EXEC_STRING|EXEC_USECALC,calc_evalsubst},
1823
  {"evaluesubstit",EXEC_STRING|EXEC_USECALC,calc_evalsubst},
1824
  {"evaluesubstitute",EXEC_STRING|EXEC_USECALC,calc_evalsubst},
1825
  {"examscore", EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_examscore},
1826
  {"exchange",  EXEC_STRING,        exec_exchange},
1827
  {"exec",      EXEC_EXEC|EXEC_SUBST|EXEC_PROCTOO|EXEC_USECALC,    calc_exec},
1828
  {"execute",   EXEC_EXEC|EXEC_SUBST|EXEC_PROCTOO|EXEC_USECALC,    calc_exec},
1829
  {"exit",      EXEC_JUMP,        exec_exit},
1830
  {"fileappend",EXEC_DIR|EXEC_SUBST,    fileappend},
1831
  {"fileexists",EXEC_STRING|EXEC_USECALC, calc_fileexists},
1832
  {"filelist",  EXEC_DIR|EXEC_SUBST|EXEC_USECALC,calc_listfile},
1833
  {"fileout",   EXEC_HREF|EXEC_SUBST,    exec_getfile},
1834
  {"filewrite", EXEC_DIR|EXEC_SUBST,    filewrite},
1835
  {"filexists",EXEC_STRING|EXEC_USECALC, calc_fileexists},
1836
  {"for",       EXEC_FOR,        exec_for},
1837
  {"form",      EXEC_HREF|EXEC_SUBST,    exec_form},
1838
  {"formbar",   EXEC_HREF,        exec_formbar},
1839
  {"formcheckbox",EXEC_HREF,        exec_formcheckbox},
1840
  {"formend",   EXEC_STRING,    exec_formend},
1841
  {"formradio", EXEC_HREF,        exec_formradio},
1842
  {"formradiobar",EXEC_HREF,        exec_formbar},
1843
  {"formselect",EXEC_HREF,        exec_formselect},
1844
  {"getdef",    EXEC_SCORE|EXEC_USECALC,calc_defof},
1845
  {"getfile",   EXEC_HREF|EXEC_SUBST,    exec_getfile},
1846
  {"getscore",  EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscore},
1847
  {"getscorebest",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscorebest},
1848
  {"getscorelast",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscorelast},
1849
  {"getscorelevel",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscorelevel},
1850
  {"getscoremean",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscoremean},
1851
  {"getscorepercent",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscorepercent},
1852
  {"getscorequality",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscoremean},
1853
  {"getscoreremain",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscoreremain},
1854
  {"getscorerequire",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscorerequire},
1855
  {"getscoretry",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscoretry},
1856
  {"getscoreweight",EXEC_SCORE|EXEC_SUBST|EXEC_USECALC,calc_getscoreweight},
1857
  {"goto",      EXEC_JUMP|EXEC_SUBST,    exec_goto},
1858
  {"header",    EXEC_HREF,        exec_header},
1859
  {"header1",   EXEC_HREF,        exec_header1},
1860
  {"headmenu",  EXEC_HREF,        exec_headmenu},
1861
  {"hex",       EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_hex},
1862
  {"homeref",   EXEC_HREF,        exec_homeref},
1863
  {"href",      EXEC_HREF,        exec_href},
13757 bpr 1864
  {"html2iso",  EXEC_STRING|EXEC_USECALC|EXEC_SUBST,calc_html2iso},
12245 bpr 1865
  {"htmlbar",   EXEC_HREF,        exec_formbar},
1866
  {"htmlcheckbox",EXEC_HREF,        exec_formcheckbox},
1867
  {"htmlheader",EXEC_HREF,        exec_header},
1868
  {"htmlmath",  EXEC_STRING|EXEC_SUBST|EXEC_USECALC,htmlmath},
1869
  {"htmlradio", EXEC_HREF,        exec_formradio},
1870
  {"htmlradiobar",EXEC_HREF,        exec_formbar},
1871
  {"htmlselect",EXEC_HREF,        exec_formselect},
1872
  {"htmltail",  EXEC_HREF,        exec_tail},
1873
  {"htmltitle", EXEC_HREF,        exec_title},
1874
  {"if",        EXEC_IF,        exec_if},
1875
  {"ifval",     EXEC_IF,        exec_ifval},
1876
  {"ifvalue",   EXEC_IF,        exec_ifval},
1877
  {"imgrename", EXEC_STRING|EXEC_USECALC|EXEC_SUBST,calc_imgrename},
1878
  {"include",   EXEC_READ|EXEC_SUBST,   exec_read},
1879
  {"increase",  EXEC_VAR|EXEC_SUBST,    exec_increase},
1880
  {"input",     EXEC_READ|EXEC_SUBST,    exec_read},
1881
  {"insdraw",   EXEC_INS|EXEC_SUBST,    exec_insdraw},
1882
  {"insmath",   EXEC_INS,        insmath},
1883
  {"inspaint",  EXEC_INS|EXEC_SUBST,    exec_insdraw},
1884
  {"insplot",   EXEC_INS|EXEC_SUBST,    exec_insplot},
1885
  {"insplot3d", EXEC_INS|EXEC_SUBST,    exec_insplot3d},
1886
  {"instex",    EXEC_INS,        exec_instex},
1887
  {"instexst",  EXEC_INS|EXEC_USECALC,    calc_instexst},
1888
  {"instexstatic",EXEC_INS|EXEC_USECALC,    calc_instexst},
1889
  {"item",        EXEC_STRING|EXEC_USECALC,calc_itemof},
1890
  {"items",     EXEC_STRING|EXEC_USECALC,calc_itemof},
1891
  {"items2lines", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,items2lines},
1892
  {"items2words",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,items2words},
1893
  {"itemstolines",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,items2lines},
1894
  {"itemstowords",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,items2words},
1895
  {"let",        EXEC_SET,        exec_set},
1896
  {"leveldata",  EXEC_STRING|EXEC_USECALC,calc_leveldata},
1897
  {"levelpoints",EXEC_STRING|EXEC_USECALC,calc_leveldata},
1898
  {"line",       EXEC_STRING|EXEC_USECALC,calc_lineof},
1899
  {"lines",      EXEC_STRING|EXEC_USECALC,calc_lineof},
1900
  {"lines2items",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,lines2items},
1901
  {"lines2list", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,lines2items},
1902
  {"lines2words",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,lines2words},
1903
  {"linestoitems",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,lines2items},
1904
  {"linestolist",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,lines2items},
1905
  {"linestowords",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,lines2words},
1906
  {"list2lines", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,items2lines},
1907
  {"list2words", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,items2words},
1908
  {"listfile",   EXEC_DIR|EXEC_SUBST|EXEC_USECALC,calc_listfile},
1909
  {"listfiles",  EXEC_DIR|EXEC_SUBST|EXEC_USECALC,calc_listfile},
1910
  {"listintersect",EXEC_STRING|EXEC_USECALC,calc_listintersect},
1911
  {"listintersection", EXEC_STRING|EXEC_USECALC,calc_listintersect},
1912
  {"listtolines",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,items2lines},
1913
  {"listtowords",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,items2words},
1914
  {"listunion",  EXEC_STRING|EXEC_USECALC,calc_listunion},
1915
  {"listuniq",   EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_listuniq},
1916
  {"listunique", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_listuniq},
1917
  {"listvar",    EXEC_STRING|EXEC_SUBST|EXEC_USECALC,mathvarlist},
1918
  {"lookup",     EXEC_STRING|EXEC_USECALC,    calc_lookup},
1919
  {"lower",      EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_tolower},
1920
  {"lowercase",  EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_tolower},
1921
  {"ls",         EXEC_DIR|EXEC_SUBST|EXEC_USECALC,calc_listfile},
1922
  {"mailto",     EXEC_MAIL|EXEC_SUBST,    exec_mailto},
1923
  {"mailurl",    EXEC_MAIL|EXEC_SUBST|EXEC_USECALC,calc_mailurl},
1924
  {"makelist",   EXEC_STRING|EXEC_USECALC,calc_makelist},
1925
  {"math2html",  EXEC_STRING|EXEC_SUBST|EXEC_USECALC,htmlmath},
1926
  {"math2mathml",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,mathmlmath},
1927
  {"math2tex",   EXEC_STRING|EXEC_SUBST|EXEC_USECALC,texmath},
1928
  {"mathmlmath", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,mathmlmath},
1929
  {"mathsubst",  EXEC_STRING|EXEC_USECALC,calc_mathsubst},
1930
  {"mathsubstit",EXEC_STRING|EXEC_USECALC,calc_mathsubst},
1931
  {"mathsubstitute",EXEC_STRING|EXEC_USECALC,calc_mathsubst},
1932
  {"mexec",     EXEC_EXEC|EXEC_SUBST,    exec_mexec},
1933
  {"module",    EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_module},
1934
  {"msg",       EXEC_EXEC|EXEC_SUBST,exec_msg},
1935
  {"multiply",  EXEC_STRING|EXEC_USECALC,calc_product},
1936
  {"next",      EXEC_FOR,        exec_next},
1937
  {"nocache",   EXEC_READ,        exec_nocache},
1938
  {"non_empty", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_nonempty},
1939
  {"nonempty",  EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_nonempty},
1940
  {"nospace",   EXEC_STRING|EXEC_SUBST|EXEC_USECALC,nospace},
1941
  {"outfile",   EXEC_HREF|EXEC_SUBST,    exec_getfile},
1942
  {"pedia",     EXEC_PEDIA|EXEC_SUBST|EXEC_USECALC,pedia},
1943
  {"perl",      EXEC_EXEC|EXEC_PROCTOO|EXEC_SUBST,exec_perl},
1944
  {"position",  EXEC_STRING|EXEC_USECALC,calc_pos},
1945
  {"positionof",EXEC_STRING|EXEC_USECALC,calc_pos},
1946
  {"positions", EXEC_STRING|EXEC_USECALC,calc_pos},
1947
  {"prod",      EXEC_STRING|EXEC_USECALC,calc_product},
1948
  {"product",   EXEC_STRING|EXEC_USECALC,calc_product},
1949
  {"rawmath",   EXEC_STRING|EXEC_SUBST|EXEC_USECALC,rawmath},
1950
  {"rawmatrix", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,rawmatrix},
1951
  {"reaccent",  EXEC_STRING|EXEC_USECALC|EXEC_SUBST,reaccent},
1952
  {"read",      EXEC_READ|EXEC_SUBST,    exec_read},
1953
  {"readdef",   EXEC_READ|EXEC_SUBST,    exec_defread},
1954
  {"readproc",  EXEC_READ|EXEC_SUBST,    exec_readproc},
1955
  {"record",    EXEC_STRING|EXEC_USECALC,calc_recordof},
1956
  {"records",   EXEC_STRING|EXEC_USECALC,calc_recordof},
1957
  {"recursion", EXEC_STRING|EXEC_USECALC,calc_recursion},
1958
  {"reinput",   EXEC_STRING|EXEC_USECALC|EXEC_SUBST,calc_reinput},
1959
  {"rem",       EXEC_COMMENT,        exec_comment},
1960
  {"remark",    EXEC_COMMENT,        exec_comment},
1961
  {"replace",   EXEC_STRING|EXEC_USECALC,calc_replace},
1962
  {"reset",     EXEC_SET|EXEC_SUBST,    exec_reset},
1963
  {"restart",   EXEC_JUMP|EXEC_SUBST,    exec_restart},
1964
  {"return",    EXEC_JUMP,        exec_exit},
1965
  {"robotrap",  EXEC_HREF|EXEC_SUBST,    exec_robottrap},
1966
  {"robottrap", EXEC_HREF|EXEC_SUBST,    exec_robottrap},
1967
  {"rootof",    EXEC_STRING|EXEC_USECALC,calc_solve},
1968
  {"row",       EXEC_STRING|EXEC_USECALC,calc_rowof},
1969
  {"rows",      EXEC_STRING|EXEC_USECALC,calc_rowof},
1970
  {"rows2lines",EXEC_STRING|EXEC_USECALC|EXEC_SUBST,calc_rows2lines},
1971
  {"run",       EXEC_EXEC|EXEC_SUBST|EXEC_PROCTOO|EXEC_USECALC,    calc_exec},
1972
  {"select",    EXEC_STRING|EXEC_USECALC,calc_select},
1973
  {"set",       EXEC_SET,        exec_set},
1974
  {"setdef",    EXEC_OTHER,        exec_setdef},
1975
  {"setseed",   EXEC_STRING|EXEC_SUBST,        exec_setseed},
1976
  {"sh",        EXEC_EXEC|EXEC_PROCTOO|EXEC_SUBST,exec_sh},
1977
  {"shortout",  EXEC_JUMP|EXEC_SUBST,    exec_directout},
1978
  {"singlespace",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,singlespace},
1979
  {"solve",     EXEC_STRING|EXEC_USECALC,calc_solve},
1980
  {"sort",      EXEC_STRING|EXEC_USECALC, calc_sort},
7379 bpr 1981
/*      {"sql",     EXEC_SQL|EXEC_SUBST|EXEC_USECALC, calc_sql}, */
12245 bpr 1982
  {"staticinstex",EXEC_INS|EXEC_USECALC,    calc_instexst},
1983
  {"stinstex",  EXEC_INS|EXEC_USECALC,    calc_instexst},
1984
  {"sum",       EXEC_STRING|EXEC_USECALC,calc_sum},
1985
  {"system",    EXEC_EXEC|EXEC_PROCTOO|EXEC_SUBST,exec_sh},
1986
  {"tail",      EXEC_HREF,        exec_tail},
1987
  {"test",      EXEC_DEBUG,        exec_test},
1988
  {"texmath",   EXEC_STRING|EXEC_SUBST|EXEC_USECALC,texmath},
1989
  {"text",      EXEC_STRING|EXEC_USECALC,text},
1990
  {"title",     EXEC_HREF,        exec_title},
1991
  {"tohex",     EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_hex},
1992
  {"tolower",   EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_tolower},
1993
  {"toupper",   EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_toupper},
1994
  {"translate", EXEC_STRING|EXEC_USECALC,calc_translate},
1995
  {"trim",      EXEC_STRING|EXEC_USECALC,calc_trim},
1996
  {"upper",     EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_toupper},
1997
  {"uppercase", EXEC_STRING|EXEC_SUBST|EXEC_USECALC,calc_toupper},
1998
  {"usererror", EXEC_WARN|EXEC_SUBST,    exec_usererror},
1999
  {"values",    EXEC_STRING|EXEC_USECALC,calc_values},
2000
  {"varlist",   EXEC_STRING|EXEC_SUBST|EXEC_USECALC,mathvarlist},
2001
  {"warn",      EXEC_WARN|EXEC_SUBST,    exec_warn},
2002
  {"warning",   EXEC_WARN|EXEC_SUBST,    exec_warn},
2003
  {"while",     EXEC_WHILE,        exec_while},
2004
  {"whileval",  EXEC_WHILE,        exec_whileval},
2005
  {"whilevalue",EXEC_WHILE,        exec_whileval},
2006
  {"wimsheader",EXEC_HREF,        exec_header},
2007
  {"wimsref",   EXEC_HREF,        exec_homeref},
2008
  {"wimstail",  EXEC_HREF,        exec_tail},
2009
  {"wimstitle", EXEC_HREF,        exec_title},
2010
  {"word",      EXEC_STRING|EXEC_USECALC,calc_wordof},
2011
  {"words",     EXEC_STRING|EXEC_USECALC,calc_wordof},
2012
  {"words2items",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,words2items},
2013
  {"words2lines",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,words2lines},
2014
  {"words2list",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,words2items},
2015
  {"wordstoitems",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,words2items},
2016
  {"wordstolines",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,words2lines},
2017
  {"wordstolist",EXEC_STRING|EXEC_SUBST|EXEC_USECALC,words2items},
2018
  {"writefile", EXEC_DIR|EXEC_SUBST,    filewrite},
10 reyssat 2019
};
8185 bpr 2020
int EXEC_FN_NO=(sizeof(exec_routine)/sizeof(exec_routine[0]));
10 reyssat 2021
 
8155 bpr 2022
/* internal: to skip the content of a false if/while. */
10 reyssat 2023
static void _skip_contents(int isif)
2024
{
12245 bpr 2025
  char buf[MAX_NAMELEN+8], *p1;
2026
  int i,j,loop;
2027
  loop=0;
2028
  while(m_file.linepointer<m_file.linecnt) {
2029
    j=m_file.linepointer;
2030
    if((m_file.lines[j].isstart&2)==0) {
2031
      m_file.linepointer++; continue;
2032
    }
2033
    i=m_file.lines[j].execcode;
2034
    if(i<0) {
2035
      if(wgetline(buf,MAX_NAMELEN+4,&m_file)==EOF) return;
2036
      p1=buf+1; if(*p1!='i' && *p1!='e' && *p1!='w') continue;
2037
      *find_word_end(p1)=0;
2038
      i=search_list(exec_routine,EXEC_FN_NO,sizeof(exec_routine[0]),p1);
2039
      if(i>=0) m_file.lines[j].execcode=i;
2040
    }
2041
    else m_file.linepointer++;
2042
    if(i<0) continue;
2043
    switch(exec_routine[i].tag & 0xffff) {
2044
      case EXEC_WHILE:
2045
        if(!isif) loop++;
2046
        break;
2047
      case EXEC_IF:
2048
        if(isif) loop++;
2049
        break;
2050
      case EXEC_ELSE: {
2051
        if(!isif) break;
2052
        if(loop<=0) return; else break;
7379 bpr 2053
      }
12245 bpr 2054
      case EXEC_ENDIF: {
2055
        if(!isif) break;
2056
        if(loop>0) {
2057
          loop--; break;
2058
        }
2059
        else return;
7379 bpr 2060
      }
12245 bpr 2061
      case EXEC_ENDWHILE: {
2062
        if(isif) break;
2063
        if(loop>0) {
2064
          loop--; break;
7379 bpr 2065
        }
12245 bpr 2066
        else return;
7379 bpr 2067
      }
12245 bpr 2068
      default: break;
10 reyssat 2069
    }
12245 bpr 2070
  }
10 reyssat 2071
}
2072
 
8155 bpr 2073
/* Execute a command defined by !. Returns 0 if OK. */
10 reyssat 2074
void exec_main(char *p)
2075
{
12245 bpr 2076
  int i,j;
2077
  char *pp;
2078
  char tbuf2[MAX_LINELEN+1];
10 reyssat 2079
 
12245 bpr 2080
  pp=find_word_end(p);
2081
  if(*pp!=0) {
2082
    *(pp++)=0; pp=find_word_start(pp);
2083
  }
2084
  i=m_file.lines[m_file.l].execcode;
2085
  if(i<0) {
2086
    i=search_list(exec_routine,EXEC_FN_NO,sizeof(exec_routine[0]),p);
2087
    m_file.lines[m_file.l].execcode=i;
2088
  }
2089
  if(i<0) {
2090
    setvar(error_data_string,p); module_error("bad_cmd");
2091
  }
8155 bpr 2092
/* called from !readdef, no right other than set; bail out */
12245 bpr 2093
  execnt++;
2094
  if((untrust&4)!=0 && (j=(exec_routine[i].tag&0xffff))!=EXEC_SET) {
2095
    tbuf2[0]=0; exec_exit(tbuf2);
2096
  }
2097
  ovlstrcpy(tbuf2,pp); j=exec_routine[i].tag;
2098
  if(j&EXEC_SUBST) substit(tbuf2);
2099
  if(j&EXEC_USECALC) {
2100
    if(!outputing && (j&EXEC_PROCTOO)==0) return;
2101
    exec_routine[i].routine(tbuf2); if(outputing) output0(tbuf2);
2102
  }
2103
  else exec_routine[i].routine(tbuf2);
2104
  return;
10 reyssat 2105
}