Subversion Repositories wimsdev

Rev

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