Back to index

wims  3.65+svn20090927
Defines | Enumerations | Functions | Variables
oef2wims.c File Reference
#include "../Lib/libwims.h"
#include "lines.c"
#include "compare.c"
#include "outexec.c"
#include "process.c"

Go to the source code of this file.

Defines

#define MAX_TITLEN   40
#define MAX_PROMPTLEN   40
#define MAX_PARM   10
#define MAX_PARAM   1024
#define MAX_DEFINE   1024
#define MAX_FLEN   199999
#define elsechar   '\001'
#define endifchar   '\002'
#define nextchar   '\003'
#define whilechar   '\004'
#define VERSION   "3.22"
#define executed_str   "__EXECUTED_1692754_EXERCISE__"
#define dir_no   (sizeof(directives)/sizeof(directives[0]))

Enumerations

enum  {
  t_tit, t_head, t_def, t_wims,
  t_form, t_main, t_plot, t_hint,
  t_ans, t_choi, t_cond, t_cond2,
  t_sol, t_feedback, t_help, t_step
}

Functions

void debug (void)
long int getinp (char fname[])
void process (void)
void _out (int typ)
void output (void)
int main (int argc, char *argv[])

Variables

FILE * outf
int choicecnt = 0
int answercnt = 0
int feedbackcnt = 0
int varcnt = 0
int conditioncnt = 0
int condans = 0
int has_plot = 0
char * primitive_dir = "oef"
int has_help = 0
int posttag = 0
int prevarcnt = -1
int post = 0
int deftag
int prevars
int start_varcnt = 0
int step_defined = 0
int embedcnt = 0
char * badpar = NULL
char * mdef
char * format = "html"
int ao [256]
int aocnt = 0
struct {
char * name
int serial
short int type
short int save
param [MAX_PARAM+32]
char * inpbuf
char * inpend
char outfname [1024]
struct {
char * name
int type
int parmcnt
void(* processor )(char *p[MAX_PARM])
directives []
struct {
char * parm [MAX_PARM]
short int type
short int tag
define [MAX_DEFINE]
int define_no
int title_no = -1
int statement_no = -1

Define Documentation

#define dir_no   (sizeof(directives)/sizeof(directives[0]))

Definition at line 115 of file oef2wims.c.

#define elsechar   '\001'

Definition at line 26 of file oef2wims.c.

#define endifchar   '\002'

Definition at line 27 of file oef2wims.c.

#define executed_str   "__EXECUTED_1692754_EXERCISE__"

Definition at line 31 of file oef2wims.c.

#define MAX_DEFINE   1024

Definition at line 24 of file oef2wims.c.

#define MAX_FLEN   199999

Definition at line 25 of file oef2wims.c.

#define MAX_PARAM   1024

Definition at line 23 of file oef2wims.c.

#define MAX_PARM   10

Definition at line 22 of file oef2wims.c.

Definition at line 21 of file oef2wims.c.

#define MAX_TITLEN   40

Definition at line 20 of file oef2wims.c.

#define nextchar   '\003'

Definition at line 28 of file oef2wims.c.

#define VERSION   "3.22"

Definition at line 30 of file oef2wims.c.

#define whilechar   '\004'

Definition at line 29 of file oef2wims.c.


Enumeration Type Documentation

anonymous enum
Enumerator:
t_tit 
t_head 
t_def 
t_wims 
t_form 
t_main 
t_plot 
t_hint 
t_ans 
t_choi 
t_cond 
t_cond2 
t_sol 
t_feedback 
t_help 
t_step 

Definition at line 64 of file oef2wims.c.


Function Documentation

void _out ( int  typ)

Definition at line 298 of file oef2wims.c.

{
    int i, t;
    for(i=0;i<define_no;i++) {
       t=define[i].type;
       if(directives[t].type!=typ) continue;
       if(typ==t_def && define[i].tag!=deftag) continue;
       directives[t].processor(define[i].parm);
    }
}

Here is the caller graph for this function:

void debug ( void  )

Definition at line 124 of file oef2wims.c.

{
    int i,j,n;
    for(i=0;i<define_no;i++) {
       printf("%s:   ",directives[define[i].type].name);
       n=directives[define[i].type].parmcnt;
       for(j=0;j<n;j++) {
           printf("%s",define[i].parm[j]);
           if(j<n-1) printf(", ");
       }
       printf("\n");
    }
}
long int getinp ( char  fname[])

Definition at line 139 of file oef2wims.c.

{
    FILE *input;
    long int siz;
    int i;
    
    input=fopen(fname,"r");
    if(input==NULL) return 0;
    fseek(input,0L,SEEK_END); siz=ftell(input);
    if(siz<=0 || siz>=MAX_FLEN) {
       fclose(input);
       if(siz==0) return 0; else return -1;
    }
    fseek(input,0L,SEEK_SET);
    inpbuf=xmalloc(siz+10);
    siz=fread(inpbuf,1,siz,input); fclose(input);
    if(siz<=0) return -1;
    inpend=inpbuf+siz; *inpend=0;
    for(i=0;i<siz;i++) 
      if(inpbuf[i]==13 || (inpbuf[i]>=0 && inpbuf[i]<=6)) inpbuf[i]=' ';
    return siz;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char *  argv[] 
)

Definition at line 395 of file oef2wims.c.

{
    int t;
    error1=error; error2=error; error3=error;
    substitute=substit;
    if(argc<=1) return 0; /* no input file */
    if(argc==2 && strcmp(argv[1],"table")==0) {
       if(verify_order(directives, dir_no, sizeof(directives[0]))) return -1;
       if(verify_order(specialfn, specialfn_no, sizeof(specialfn[0]))) return -1;
       puts("Table orders OK."); return 0;
    }
    if(argc>2) snprintf(outfname,sizeof(outfname),"%s",argv[2]);
    else {
       char *fe;
       snprintf(outfname,sizeof(outfname)-10,"%s",argv[1]);
       fe=outfname+strlen(outfname)-strlen(".oef");
       if(strcasecmp(fe,".oef")==0) *fe=0;
       strcat(fe,".def");
    }
    mdef=getenv("oef2wims_mdef"); if(mdef==NULL) mdef="";
    printf("%s..",argv[1]);
    t=getinp(argv[1]);
    if(t<0) error("Source file bad or too long.");
    if(t==0) error("Empty source file.");
    if(checkparentheses(inpbuf,1)!=0) error("Unmatched parentheses");
    outf=fopen(outfname,"w"); if(outf==NULL) error("Unable to open output file.");
    fclose(outf); remove(outfname);
    vbuf_statement[0]=vbuf_hint[0]=vbuf_help[0]=vbuf_solution[0]=0;
    param[1].name="imagedir";param[1].type=pt_text;
    param[2].name="confparm1";param[2].type=pt_text;
    param[3].name="confparm2";param[3].type=pt_text;
    param[4].name="confparm3";param[4].type=pt_text;
    param[5].name="confparm4";param[5].type=pt_text;
    start_varcnt=6;
    process();
    output();
    printf(" -> %s\n",outfname);
    return 0;
}

Here is the call graph for this function:

void output ( void  )

Definition at line 310 of file oef2wims.c.

{
    int i,k;
       /* no statement, nothing to do */
    if(statement_no<0) error("No statement defined.");
    outf=fopen(outfname,"w"); if(outf==NULL) error("Unable to open output file.");
    if(title_no>=0 && *(define[title_no].parm[0])!=0) {
       char *p=define[title_no].parm[0];
       if(strlen(p)>MAX_TITLEN) *(p+MAX_TITLEN)=0;
       fprintf(outf,"!set title=%s\n",p);
    }
    else {
       fprintf(outf,"!set title=No title\n");
    }
    fprintf(outf,"!if $wims_read_parm!=$empty\n\
 !goto $wims_read_parm\n\
!endif\n");
    fprintf(outf,"oef2wims_version=%s\n",VERSION);
    _out(t_head);
    if(aocnt>0) {
       int i;
       fprintf(outf,"\nansorder=");
       for(i=0;i<aocnt;i++) {
           if(ao[i]==t_ans) fprintf(outf,"r");
           else fprintf(outf,"c");
           if(i<aocnt-1) fprintf(outf,",");
       }      
    }
    if(prevarcnt<varcnt) post=varcnt-prevarcnt; else post=0;
    fprintf(outf,"\n\
varcnt=%d\n\
prevarcnt=%d\n\
postvarcnt=%d\n\
replycnt=%d\n\
choicecnt=%d\n\
conditioncnt=%d\n\
feedbackcnt=%d\n\
format=%s\n\n\
val1=$imagedir\n\
val2=$confparm1\n\
val3=$confparm2\n\
val4=$confparm3\n\
val5=$confparm4\n\n\
!if $testcondition=yes\n\
 !goto test\n\
!endif\n\
!if $status=waiting\n\
 !exit\n\
!endif\n\n",varcnt+1,prevarcnt,post,answercnt,choicecnt,
           conditioncnt,feedbackcnt,format);
    varcnt=start_varcnt; deftag=0;
    _out(t_def);
    _out(t_step);
/*    _out(t_wims);  */
    _out(t_plot);
    answercnt=1; _out(t_ans);
    choicecnt=1; _out(t_choi);
    deftag=1; prevars=varcnt;
    _out(t_main);
    _out(t_hint);
    _out(t_help);
    _out(t_sol);
    fprintf(outf,"\n!goto stat\n");
       /* second run to output execution codes */
    p_statement(NULL);
    p_hint(NULL);
    p_help(NULL);
    p_solution(NULL);
    if(post) {
       fprintf(outf,"\n!exit\n\n:postdef\n"); _out(t_def);
    }
    fprintf(outf,"\n!exit\n\n:feedback\n");
    _out(t_feedback);
    fprintf(outf,"\n!exit\n\n:test\n");
    _out(t_cond2); conditioncnt=1; _out(t_cond);
    fprintf(outf,"\n!exit\n\n:stat\nvsavelist=");
    for(k=0,i=1;i<prevars;i++) {
       if(param[i].save==0) continue;
       if(k>0) fprintf(outf,","); k++;
       fprintf(outf,"%d",i);
    }
    fprintf(outf,"\nembedcnt=%d\n",embedcnt);
    fclose(outf);
}

Here is the call graph for this function:

void process ( void  )

Definition at line 163 of file oef2wims.c.

{
    char *p, *pe, *pp, c;
    int i,j;
    
    for(p=inpbuf,define_no=0;define_no<MAX_DEFINE && p<inpend;p++) {
       if(*p!='\\' && *p!='{' && (*p>6 || *p<0)) continue;
       if(*p>0 && *p<=6) {
           pe=""; switch(*p) {
              case elsechar: pe="else"; break;
              case endifchar: pe="endif"; break;
              case nextchar: pe="next"; break;
              case whilechar: pe="endwhile"; break;
           }
           define[define_no].tag=posttag;
           define[define_no++].type=
             search_list(directives,dir_no,sizeof(directives[0]),pe);
           continue;
       }
       if(*p=='{') {
           if((p=find_matching(p+1,'}'))==NULL) error("Unmatched parentheses???");
           continue;
       }
       p++;
       for(pe=p;isalnum(*pe) || *pe=='_'; pe++);
       pp=find_word_start(pe);
       if(*pp!='{') {
           if(pp>p) p=pp-1;
           continue;
       }
       c=*pe; *pe=0;
       i=search_list(directives,dir_no,sizeof(directives[0]),p);
       if(i<0) {
           if(wordchr(mdef,p)!=NULL) {
              pe=find_matching(pp+1,'}'); if(pe==NULL) error("Unmatched parentheses?");
              *pe=0;define[define_no].type=
                search_list(directives,dir_no,sizeof(directives[0]),"mdef");
              replace_newline(pp+1);
              define[define_no].tag=posttag;
              define[define_no].parm[0]=p;
              define[define_no].parm[1]=pp+1;
              define_no++; p=pe;
              continue;
           }
           if((p=find_matching(pp+1,'}'))==NULL) error("Unmatched parentheses?");
           continue;
       }
       define[define_no].type=i;
       define[define_no].tag=posttag;
       for(j=0;j<MAX_PARM && j<directives[i].parmcnt;j++,pp=find_word_start(pe+1)) {
           if(j>0 && *pp!='{') break;
           if((pe=find_matching(pp+1,'}'))==NULL) error("Unmatched parentheses?");
           *pe=0; replace_newline(pp+1);
           define[define_no].parm[j]=pp+1;
       }
       for(;j<MAX_PARM;j++) define[define_no].parm[j]="";
       switch(directives[i].type) {
           case t_tit: {
              title_no=define_no; break;
           }
           case t_ans: {
              if(aocnt<256) ao[aocnt++]=t_ans;
              answercnt++; goto checkeq;
           }
           case t_step: {
              if(step_defined) error("Multiple definition of steps.");
              step_defined=1;      break;
           }
           case t_def: {
              char *pt;
              if(directives[i].parmcnt<2) {
                  char *pt2, *pt3;
                  pt=find_word_start(pe+1); pt3="";
                  if(*pt!='{') pt2=pt;
                  else {
                     *pt=' '; pt2=find_matching(pt,'}');
                     if(pt2==NULL) error("Unmatched parentheses?");
                     pt3=find_word_start(pt2+1);
                  }
                  if(strcmp(directives[i].name,"if")==0) {
                     if(*pt3=='{') {
                         *pt2=elsechar; *pt3=' ';
                         pt2=find_matching(pt3,'}');
                         if(pt2==NULL) error("Unmatched parentheses?");
                     }
                     *pt2=endifchar;
                  }
                  else {
                     if(strcmp(directives[i].name,"while")==0) *pt2=whilechar;
                     else {
                         varcnt++; *pt2=nextchar;
                     }
                  }
                  break;
              }
              varcnt++;
              checkeq:
              if(*(define[define_no].parm[1])==0 && 
                 (pt=strchr(define[define_no].parm[0],'='))!=NULL) {
                  if(*(pt-1)==':') *(pt-1)=0;
                  *pt=0; define[define_no].parm[1]=pt+1;
              }
              break;
           }
           case t_choi: {
              if(aocnt<256) ao[aocnt++]=t_choi;
              choicecnt++; break;
           }
           case t_cond: {
              conditioncnt++; break;
           }
           case t_main: {
              if(posttag) error("Multiple definition of statement.");
              posttag=1; if(prevarcnt<0) prevarcnt=varcnt;
              statement_no=define_no; break;
           }
           case t_plot: {
              has_plot=1; break;
           }
           case t_form: {
              char *s=define[define_no].parm[0];
              s=find_word_start(s);*find_word_end(s)=0;
              if(strcasecmp(s,"html")==0) format="html";
              if(strcasecmp(s,"tex")==0) format="tex";
              break;
           }
           case t_feedback: {
              feedbackcnt++; break;
           }
       }
       define_no++; p=pe;
    }
}

Here is the call graph for this function:


Variable Documentation

int answercnt = 0

Definition at line 36 of file oef2wims.c.

int ao[256]

Definition at line 47 of file oef2wims.c.

int aocnt = 0

Definition at line 48 of file oef2wims.c.

char* badpar = NULL

Definition at line 43 of file oef2wims.c.

int choicecnt = 0

Definition at line 36 of file oef2wims.c.

int condans = 0

Definition at line 36 of file oef2wims.c.

int conditioncnt = 0

Definition at line 36 of file oef2wims.c.

struct { ... } define[MAX_DEFINE]
int define_no

Definition at line 121 of file oef2wims.c.

int deftag

Definition at line 39 of file oef2wims.c.

struct { ... } directives[]
int embedcnt = 0

Definition at line 42 of file oef2wims.c.

int feedbackcnt = 0

Definition at line 36 of file oef2wims.c.

char* format = "html"

Definition at line 45 of file oef2wims.c.

int has_help = 0

Definition at line 38 of file oef2wims.c.

int has_plot = 0

Definition at line 36 of file oef2wims.c.

char* inpbuf

Definition at line 56 of file oef2wims.c.

char * inpend

Definition at line 56 of file oef2wims.c.

char* mdef

Definition at line 44 of file oef2wims.c.

FILE* outf

Definition at line 35 of file oef2wims.c.

char outfname[1024]

Definition at line 57 of file oef2wims.c.

struct { ... } param[MAX_PARAM+32]
int post = 0

Definition at line 39 of file oef2wims.c.

int posttag = 0

Definition at line 39 of file oef2wims.c.

int prevarcnt = -1

Definition at line 39 of file oef2wims.c.

int prevars

Definition at line 39 of file oef2wims.c.

char* primitive_dir = "oef"

Definition at line 37 of file oef2wims.c.

int start_varcnt = 0

Definition at line 40 of file oef2wims.c.

int statement_no = -1

Definition at line 121 of file oef2wims.c.

int step_defined = 0

Definition at line 41 of file oef2wims.c.

int title_no = -1

Definition at line 121 of file oef2wims.c.

int varcnt = 0

Definition at line 36 of file oef2wims.c.