Subversion Repositories wimsdev

Rev

Rev 5544 | Rev 7673 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  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.  
  18. /* internal variable */
  19. int rawmath_easy=0;
  20.  
  21. #include "hmname.c"
  22. enum {RM_UNKNOWN, RM_FN, RM_VAR, RM_PREFIX};
  23.  
  24. struct {
  25.     char *name;
  26.     int style;
  27.     char *replace;
  28. } mathname[]={
  29.       {"Arc",     RM_PREFIX, "arc"},
  30.       {"Arg",     RM_PREFIX, "arg"},
  31.       {"Ci",      RM_FN,        ""},
  32.       {"E",       RM_VAR,       ""},
  33.       {"Euler",   RM_VAR,       ""},
  34.       {"I",       RM_VAR,       ""},
  35.       {"Int",     RM_FN,        ""},
  36.       {"PI",      RM_VAR,       ""},
  37.       {"Pi",      RM_VAR,       ""},
  38.       {"Prod",    RM_FN,        ""},
  39.       {"Si",      RM_FN,        ""},
  40.       {"Sum",     RM_FN,        ""},
  41.       {"arc",     RM_PREFIX,    ""},
  42.       {"arg",     RM_PREFIX,    ""},
  43.       {"binomial",RM_FN,        ""},
  44.       {"diff",    RM_FN,        ""},
  45.       {"e",       RM_VAR,       ""},
  46.       {"erf",     RM_FN,        ""},
  47.       {"euler",   RM_VAR,       ""},
  48.       {"i",       RM_VAR,       ""},
  49.       {"infinity",RM_VAR,       ""},
  50.       {"int",     RM_FN,        ""},
  51.       {"integrate",RM_FN,       ""},
  52.       {"neq",     RM_VAR,       ""},
  53.       {"pi",      RM_VAR,       ""},
  54.       {"prod",    RM_FN,        ""},
  55.       {"product", RM_FN,        ""},
  56.       {"psi",     RM_FN,        ""},
  57.       {"sum",     RM_FN,        ""},
  58.       {"theta",   RM_FN,        ""},
  59.       {"x",       RM_VAR,       ""},
  60.       {"y",       RM_VAR,       ""},
  61.       {"z",       RM_VAR,       ""},
  62.       {"zeta",    RM_FN,        ""},
  63. };
  64. #define mathname_no (sizeof(mathname)/sizeof(mathname[0]))
  65. char rm_vbuf[MAX_LINELEN+1],rm_fbuf[MAX_LINELEN+1];
  66. char *rm_uservar[MAX_LINELEN+1],*rm_userfn[MAX_LINELEN+1];
  67. int  rm_uservars,rm_userfns;
  68.  
  69. /* add user-defined variables and function names,
  70.  * internal, only called by rawmath(). */
  71. void getuservar(void)
  72. {
  73.     char *p1, *p2, *p;
  74.     rm_uservars=rm_userfns=0;
  75.     p=getvar("wims_rawmath_variables");
  76.     if(p!=NULL && *p!=0) {
  77.        ovlstrcpy(rm_vbuf,p);
  78.        for(p=rm_vbuf;*p;p++) if(*p==',') *p=' ';
  79.        for(p1=find_word_start(rm_vbuf);*p1;p1=find_word_start(p2)) {
  80.           rm_uservar[rm_uservars++]=p1;
  81.           p2=find_word_end(p1);
  82.           if(*p2!=0) *(p2++)=0;
  83.        }
  84.     }
  85.     p=getvar("wims_rawmath_functions");
  86.     if(p!=NULL && *p!=0) {
  87.       ovlstrcpy(rm_fbuf,p);
  88.       for(p=rm_fbuf;*p;p++) if(*p==',') *p=' ';
  89.       for(p1=find_word_start(rm_fbuf);*p1;p1=find_word_start(p2)) {
  90.         rm_userfn[rm_userfns++]=p1;
  91.         p2=find_word_end(p1);
  92.         if(*p2!=0) *(p2++)=0;
  93.       }
  94.     }
  95. }
  96.  
  97. /* Try to split a word into recognizable variables */
  98. char *mathname_split(char *p)
  99. {
  100.     int c,i,j,type;
  101.  
  102.     c=0;
  103.     beg: for(i=get_evalcnt()-1;
  104.     i>=0 && strncmp(p,get_evalname(i),strlen(get_evalname(i)))!=0;
  105.     i--);
  106.     if(i>=0 && get_evaltype(i)>0) {
  107.       type=RM_FN;
  108.       j=strlen(get_evalname(i));
  109.       gotit:
  110.       if(!*(p+j)) return p;
  111.       if(myisdigit(*(p+j)) && type!=RM_FN) return NULL;
  112.       if(!c) {string_modify(p,p+j,p+j," "); p+=j+1;}
  113.       else p+=j;
  114.       c++; goto beg;
  115.     }
  116.     for(i=mathname_no-1;
  117.       i>=0 &&
  118.       (strncmp(p,mathname[i].name,strlen(mathname[i].name))!=0
  119.       || mathname[i].style==RM_PREFIX);
  120.       i--);
  121.     if(i>=0) {
  122.       type=mathname[i].style;
  123.       j=strlen(mathname[i].name);
  124.       goto gotit;
  125.     }
  126.     for(i=0;i<rm_uservars &&
  127.       strncmp(rm_uservar[i],p,strlen(rm_uservar[i]));i++);
  128.     if(i<rm_uservars) {
  129.       type=RM_VAR;
  130.       j=strlen(rm_uservar[i]); goto gotit;
  131.     }
  132.     for(i=0;i<rm_userfns &&
  133.       strncmp(p,rm_userfn[i],strlen(rm_userfn[i]));i++);
  134.     if(i<rm_userfns) {
  135.       type=RM_FN;
  136.       j=strlen(rm_userfn[i]); goto gotit;
  137.     }
  138.     return NULL;    
  139. }
  140.  
  141. int __replace_badchar (char *p, char *old, char *new)
  142. { int cnt = 0;
  143.   char *p1 ;
  144.   while((p1=strstr(p,old))!=NULL) {
  145.     string_modify(p,p1,p1+strlen(old),"%s",new);
  146.     cnt++;
  147.   }
  148.   return cnt ;
  149. }
  150.  
  151. /* translate |x| into abs(x)*/
  152. int __replace_abs ( char *p )
  153. {  
  154.     char *p1, *p2 ;
  155.     while((p1=strchr(p,'|'))!=NULL) {
  156.       p2=find_matching(p1+1,'|');
  157.       if(p2==NULL) { return 1; break;} /* error; drop it. */
  158.       *p2=')'; string_modify(p,p1,p1+1,"abs(");
  159.     }
  160.     return 0;
  161. }
  162.  
  163. /* signs: translate ++, +-, -+, ... into one sign. */
  164. void __replace_plusminus ( char *p )
  165. {
  166.    char *p1, *p2;
  167.    for(p1=p;*p1!=0;p1++) {
  168.      int sign, redundant;
  169.      if(*p1!='+' && *p1!='-') continue;
  170.      if(*p1=='+') sign=1; else sign=-1;
  171.      redundant=0;
  172.      for(p2=find_word_start(p1+1);*p2=='+' || *p2=='-';
  173.         p2=find_word_start(p2+1)) {
  174.          if(*p2=='-') sign*=-1;
  175.          redundant=1;
  176.      }
  177.      if(redundant && *p2!='>' && strncmp(p2,"&gt;",4)!=0) {
  178.         if(sign==1) *p1='+'; else *p1='-';
  179.         ovlstrcpy(p1+1,p2);
  180.      }
  181.    }
  182. }
  183.  
  184. /* dangling decimal points
  185. 4. --> 4.0  4.x -> 4.0x
  186. .5 -> 0.5
  187. another treatment is done in insmath (will replace .. by , )
  188. */
  189. void __treat_decimal(char *p)
  190. { char *p1 ;
  191.   for(p1=strchr(p,'.'); p1!=NULL; p1=strchr(p1+1,'.')) {
  192.         /* multiple .. is conserved */
  193.     if(*(p1+1)=='.') {
  194.         do p1++; while(*p1=='.'); continue;
  195.     }
  196.     if(p1>p && myisdigit(*(p1-1)) && myisdigit(*(p1+1))) continue;
  197.         /* Non-digit dangling '.' is removed */
  198.     if((p1<=p || !myisdigit(*(p1-1))) && !myisdigit(*(p1+1))) {
  199.         ovlstrcpy(p1,p1+1); p1--; continue;
  200.     }
  201.     if(p1==p || !myisdigit(*(p1-1))) { // nondigit.digit
  202.         string_modify(p,p1,p1,"0"); p1++; //add zero before point
  203.     }
  204.     if(!myisdigit(*(p1+1))) string_modify(p,p1+1,p1+1,"0"); //add zero after point
  205.   }
  206. }
  207.  
  208. /* replace new-lines, tabs, " */
  209. void __replace_space(char *p)
  210. {
  211.  char *p1 ;
  212.  for(p1=p;*p1!=0; p1++) {
  213.     if(*p1=='\\' || isspace(*p1)) *p1=' ';// replace \ and all spaces by a simple space -
  214.     if(*p1=='\"') string_modify(p,p1,p1+1,"''"); p1++; // replace " by ''
  215.   }
  216. }
  217.  
  218.     /* Error-tolerante raw math translation routine
  219.      * Translate error-laden raw math into machine-understandable form.
  220.      * do nothing if there is some { or \\
  221.      */
  222. void rawmath(char *p)
  223. {
  224.     char *p1, *p2, *p3, *p4;
  225.     char warnbuf[1024];
  226.     int ambiguous=0,unknown=0,flatpower=0,badprec=0,unmatch=0;// for warning
  227.  
  228.     /* looks like a TeX source : do nothing */
  229.     if( (strchr(p,'\\')!=NULL || strchr(p,'{')!=NULL)) return;
  230.     if(strchr(p,'^')==NULL) flatpower=-1;
  231.     if(strlen(p)>=MAX_LINELEN) {*p=0; return;}
  232.     p1=find_word_start(p);if(*p1==0) return;
  233.     while(*p1=='+') p1++;
  234.     if(p1>p) ovlstrcpy(p,p1);
  235.     (void)__replace_badchar(p,"**", "^");
  236.     if (__replace_badchar(p,"²", "^2 ")) flatpower=1;
  237.     if (__replace_badchar(p,"³", "^3 ")) flatpower=1;
  238.     unmatch=__replace_abs(p);
  239.     __replace_plusminus(p) ;
  240.     __replace_space(p);
  241.     __treat_decimal(p);
  242.     if (rawmath_easy) return;
  243.  
  244.         /* Principal translation: justapositions to multiplications */
  245.     if(strstr(p,"^1/")!=NULL) badprec=1;
  246.     getuservar();
  247.     for(p1=p;*p1;p1++) {
  248.       if(!isalnum(*p1) && *p1!=')' && *p1!=']') continue;
  249.       if(*p1==')' || *p1==']') {
  250.         p2=find_word_start(++p1);
  251.         add_star:
  252.         if(isalnum(*p2) || *p2=='(' || *p2=='[') {
  253.         if(*p2=='(' && *p1==')') ambiguous=1;
  254.         if(p2>p1) *p1='*';
  255.         else string_modify(p,p1,p1,"*");
  256.         }
  257.         p1--;continue;
  258.     }
  259.     p2=find_mathvar_end(p1); p3=find_word_start(p2);
  260.     if(myisdigit(*p1)) {
  261.         p1=p2; p2=p3; goto add_star;
  262.     }
  263.     else {
  264.         char buf[MAX_LINELEN+1];
  265.         int i;
  266.         if(p2-p2>30) goto ambig;
  267.         memcpy(buf,p1,p2-p1);buf[p2-p1]=0;
  268.         i=search_evaltab(buf);
  269.         if(i>=0 && get_evaltype(i)>0) {
  270.           fnname1:
  271.           p1=p2;p2=p3;
  272.         /*fnname:*/
  273.           if(*p2 && *p2!='(' && *p2!='*' && *p2!='/') {
  274.             char hatbuf[MAX_LINELEN+1];
  275.             hatbuf[0]=')'; hatbuf[1]=0;
  276.             if(*p2=='^') {
  277.               p3=p2+1;while(*p3==' ' || *p3=='+' || *p3=='-') p3++;
  278.               if(*p3=='(') {
  279.                 p3=find_matching(p3+1,')');
  280.                 if(p3==NULL) {unmatch=1; p3=p+strlen(p);}
  281.                 else p3++;
  282.               }
  283.               else p3=find_mathvar_end(p3);
  284.               memmove(hatbuf+1,p2,p3-p2);hatbuf[p3-p2+1]=0;
  285.               ovlstrcpy(p2,p3);
  286.               while(*p2==' ') p2++;
  287.               if(*p2=='*' || *p2=='/') {
  288.                 p1--;continue;
  289.               }
  290.               if(*p2=='(') {
  291.                 p3=find_matching(p2+1,')');
  292.                 if(p3==NULL) {unmatch=1; p3=p+strlen(p);}
  293.                 else p3++;
  294.                 string_modify(p,p3,p3,"%s",hatbuf+1);
  295.                 goto dd2;
  296.               }
  297.             }
  298.             p3=p2;if(*p3=='+' || *p3=='-') p3++;
  299.             while(isalnum(*p3) || *p3=='*' || *p3=='/' || *p3=='.')
  300.               p3++;
  301.             for(p4=p2; p4<p3 && !isalnum(*p4); p4++);
  302.             if(p4>=p3) {
  303.               if(hatbuf[1]) string_modify(p,p2,p2,"%s",hatbuf+1);
  304.             }
  305.             else {
  306.               string_modify(p,p3,p3,"%s",hatbuf);
  307.               if(p1==p2) string_modify(p,p2,p2,"(");
  308.               else *p1='(';
  309.             }
  310.             dd2:
  311.             ambiguous=1;
  312.           }
  313.           p1--;continue;
  314.         }
  315.         i=search_list(mathname,mathname_no,sizeof(mathname[0]),buf);
  316.         if(i>=0) {
  317.           if(mathname[i].replace[0]!=0) {
  318.             string_modify(p,p1,p2,mathname[i].replace);
  319.             p2=p1+strlen(mathname[i].replace);
  320.             p3=find_word_start(p2);
  321.           }
  322.           switch(mathname[i].style) {
  323.             case RM_FN:
  324.               goto fnname1;
  325.            
  326.             case RM_VAR:
  327.               p1=p2;p2=p3; goto add_star;
  328.            
  329.             case RM_PREFIX:
  330.               if(*p3!='c' && *p3!='s' && *p3!='t' &&
  331.              *p3!='C' && *p3!='S' && *p3!='T') break;
  332.               ambiguous=1;
  333.               ovlstrcpy(p2,p3); p1--; continue;
  334.           }
  335.         }
  336.         i=search_list(hmname,hmname_no,sizeof(hmname[0]),buf);
  337.         if(i>=0) {
  338.           p1=p2; p2=p3; goto add_star;
  339.         }
  340.         for(i=0;i<rm_uservars && strcmp(buf,rm_uservar[i]);i++);
  341.         if(i<rm_uservars) {
  342.           p1=p2;p2=p3;goto add_star;
  343.         }
  344.         for(i=0;i<rm_userfns && strcmp(buf,rm_userfn[i]);i++);
  345.         if(i<rm_userfns) goto fnname1;
  346.         if(p2-p1>8) goto ambig;
  347.         if(mathname_split(buf)!=NULL) {
  348.           ambiguous=1;
  349.           string_modify(p,p1,p2,"%s",buf);
  350.           p1--; continue;
  351.         }
  352.          /* unknown name */
  353.         ambig: p1=p2;p2=p3;
  354.         if(strlen(buf)>1) {
  355.           for(p3=buf;*p3!=0 && !myisdigit(*p3);p3++);
  356.           if(*p3!=0 && flatpower!=0) flatpower=1;
  357.           else {
  358.             unknown=1;
  359.             force_setvar("wims_warn_rawmath_parm",buf);
  360.           }
  361.         }
  362.         else {
  363.           if(*p2=='(') ambiguous=1;
  364.         }
  365.         if(isalnum(*p2)) {
  366.           if(p2>p1) *p1='*';
  367.           else string_modify(p,p1,p1,"*");
  368.         }
  369.         p1--;continue;
  370.       }
  371.     }
  372.     warnbuf[0]=0;
  373.     if(ambiguous) strcat(warnbuf," ambiguous");
  374.     if(unknown) strcat(warnbuf," unknown");
  375.     if(flatpower>0) strcat(warnbuf," flatpower");
  376.     if(badprec>0) strcat(warnbuf," badprec");
  377.     if(unmatch>0) strcat(warnbuf," unmatched_parentheses");
  378.     if(warnbuf[0]) {
  379.     char buf[MAX_LINELEN+1],*p;
  380.     p=getvar("wims_warn_rawmath");
  381.     if(p!=NULL && *p!=0) {
  382.         snprintf(buf,sizeof(buf),"%s %s",p,warnbuf);
  383.         force_setvar("wims_warn_rawmath",buf);
  384.     }
  385.     else force_setvar("wims_warn_rawmath",warnbuf);
  386.     }
  387. }
  388.  
  389.  /*  replace < and > by html code if htmlmath_gtlt=yes - is only used in deduc - not documented*/
  390. void __replace_htmlmath_gtlt (char *p)
  391. {   char *pp;
  392.     char *p1=getvar("htmlmath_gtlt");
  393.     if(p1!=NULL && strcmp(p1,"yes")==0) {
  394.       for(pp=strchr(p,'<'); pp!=NULL; pp=strchr(pp+1,'<'))
  395.       string_modify(p,pp,pp+1,"&lt;");
  396.       for(pp=strchr(p,'>'); pp!=NULL; pp=strchr(pp+1,'>'))
  397.       string_modify(p,pp,pp+1,"&gt;");
  398.    }
  399. }
  400.  
  401. /* exponents or indices :
  402.  *  all digits or + or - following a ^ or _ are considered as in exponent/subscript
  403.  *  expression with ( ) following a ^ or _ are  considered as in exponent/subscript
  404.  *  the parenthesis are suppressed except in case of exponent and only digits.
  405.  *  if int n != 0, use html code, else use tex code */
  406. void __replace_exponent(char *p, int n)
  407. {
  408.    char *p1;
  409.    char *SUPBEG, *SUPEND;
  410.    if (n) { SUPBEG = "<sup>"; SUPEND = "</sup>";}
  411.    else { SUPBEG = "^{"; SUPEND = "}";}
  412.  
  413.     for(p1=strchr(p,'^');p1!=NULL;p1=strchr(p1+1,'^')) {
  414.         char *p2, *p3, *pp;
  415.         char c;
  416.         p3 = p2 = find_word_start(p1+1);
  417.         if(*p2=='+' || *p2=='-') p2++;
  418.         p2 = find_word_start(p2);
  419.         if(*p2=='(') { /* ^[+-]( */
  420.             p2 = find_matching(p2+1,')');
  421.             /* no matching ')' : p2 = end of line; otherwise just after ')' */
  422.             if (p2==NULL) p2=p+strlen(p); else p2++;
  423.             /* ^( followed by any number of digits/letters, up to p2
  424.              * [FIXME: no sign?] */
  425.             /* do we remove parentheses containing exponent group ? */
  426.             if (*p3=='(') for(pp=p3+1;pp<p2-1;pp++) {
  427.                 if(!isalnum(*pp)) {
  428.                     /* not a digit/letter. Remove (). */
  429.                     p3++;*(p2-1)=0;break;
  430.                 }
  431.                 /* x^(2), parentheses not removed */
  432.             }
  433.             /* p3: start of word after ^ */
  434.             /* matching parentheses from exponent group. : f^(4) 4-derivative */
  435.             /* don't  ignore / remove a leading + sign in exponent group : Cu^+ */
  436.         } else { /* ^[+-] */
  437.             char *ptt=p2;
  438.             p2=find_word_start(find_mathvar_end(p2));
  439.             if(*p2=='(' && isalpha(*ptt)) {
  440.                 /* ^[+-]var( */
  441.                 char *p2t;
  442.                 p2t=find_matching(p2+1,')'); if(p2t!=NULL) p2=p2t+1;
  443.                 /* FIXME: what if no matching ) ? */
  444.             }
  445.             /* ^[+-]var(...): p2 points after closing ')' */
  446.             /* FIXME: I don't think this 'else' branch is sensible. One
  447.              * should NOT accept x^b(c+1) as meaning x^[b(c+1)]. I would
  448.              * remove it altogether. */
  449.         }
  450.         /* p1 points at ^ before exponent group */
  451.         /* p2 points at end of exponent group */
  452.         /* p3 = exponent group (sometimes stripped, without parentheses) */
  453.  
  454.         /* truncate string p at p2 [ c = char deleted by truncation ] */
  455.         c = *p2;if(c!=0) *p2++=0;
  456.         /* replace ^<exponent group>. Add back missing character 'c' */
  457.         string_modify(p,p1,p2, "%s%s%s%c",SUPBEG,p3,SUPEND,c);
  458.     }
  459. }
  460.  
  461. /* if int n != 0, use html code, else use tex code */
  462. void __replace_subscript(char *p, int n)
  463. {
  464.    char *p1, *p2;
  465.    char *SUBBEG, *SUBEND;
  466.    if (n) {SUBBEG = "<sub>"; SUBEND = "</sub>";}
  467.    else {SUBBEG = "_{"; SUBEND = "}";}
  468.    for(p1=strchr(p,'_');p1!=NULL;p1=strchr(p1+1,'_')) {
  469.      char buff[256];
  470.      p2=p1+1;
  471.      if(*p2=='(') p2=find_matching(p2+1,')');
  472.      else p2=find_mathvar_end(p2);
  473.      if(p2==NULL || p2>p1+64) continue;
  474.      if(*(p1+1)=='(') p2++;
  475.      memmove(buff,p1+1,p2-p1-1); buff[p2-p1-1]=0;
  476.      strip_enclosing_par(buff);
  477.      string_modify(p,p1,p2,"%s%s%s",SUBBEG,buff,SUBEND);}
  478. }
  479.  
  480. /* get rid of 1*.. ..*1  exemple : 1 * x, x/1 */
  481. void __replace_getrid1 (char *p)
  482. {   char *p1, *p2, *p3 ;
  483.     for(p1=p;*p1;p1++) {
  484.       if(*p1!='1') continue;
  485.       if(myisdigit(*(p1+1)) || *(p1+1)=='.' ||
  486.        (p1>p && (isalnum(*(p1-1)) || *(p1-1)=='.')) ) continue;
  487.       p2=find_word_start(p1+1);
  488.       if(p1>p+2 && (*(p1-1)=='-' || *(p1-1)=='+')) {
  489.         for(p3=p1-2; p3>p && isspace(*p3); p3--);
  490.         if(p3>p+1 && (*p3=='E' || *p3=='e')) { /* ??? */
  491.         p3--; while(p3>p && isspace(*p3)) p3--;
  492.         if(myisdigit(*p3) || *p3=='.') continue;
  493.         }
  494.       }
  495.       if(p1==p) p3="+";
  496.       else for(p3=p1-1;p3>p && isspace(*p3);p3--);
  497.       if(*p2=='*' && *p3!='/') {/* delete 1 if 1* or /1 */
  498.         ovlstrcpy(p1,p2+1);continue;
  499.       }
  500.       if(isalpha(*p2) && *p3!='/') {
  501.         ovlstrcpy(p1,p2);continue;
  502.       }
  503.       if(*p3=='/' && *p2!='<') ovlstrcpy(p3,p2);
  504.  
  505.     }
  506. }
  507.  
  508. /* get rid of '*' */
  509. void __replace_getridstar (char *p)
  510. { char *p1 ;
  511.  for(p1=strchr(p,'*');p1!=NULL;p1=strchr(p1+1,'*')) {
  512.     char *pq;
  513.     pq=find_word_start(p1+1);
  514.     if(myisdigit(*pq)) {
  515.         string_modify(p,p1,pq,"&times;");
  516.         continue;
  517.     }
  518.     if(p1>p && (isalpha(*(p1-1)) || *(p1-1)==')' || *(p1-1)=='>')
  519.        && (isalnum(*pq) || *pq=='$')) *p1=' ';
  520.     else {
  521.         ovlstrcpy(p1,p1+1);p1--;
  522.     }
  523.   }
  524. }
  525.  
  526. /* <=, >=, ->, =>, <=>
  527.  * if int n != 0, use html code, else use tex code */
  528.  
  529. void __replace_arrow ( char *p, int n)
  530. {   char *p1, *p2, *m_prefix;
  531.     if (n) m_prefix="$m_"; else m_prefix="\\";
  532.    
  533.     for(p1=strstr(p,"&lt;="); p1!=NULL; p1=strstr(p1+1,"&lt;=")) {
  534.       if(*(p1+5)!='&' && *(p1+5)!='=')
  535.         string_modify(p,p1,p1+5, "%sle",m_prefix);
  536.       else {
  537.           for(p2=p1+5; *p2=='='; p2++);
  538.           if(strncmp(p2,"&gt;",4)==0) {
  539.             if(p2>p1+5) { string_modify(p,p1,p2+4,"%sLongleftrightarrow",m_prefix);}
  540.             else { string_modify(p,p1,p2+4,"%sLeftrightarrow",m_prefix);}
  541.           }
  542.           else { string_modify(p,p1,p2,"%sLongleftarrow",m_prefix);}
  543.       }
  544.     }
  545.     for(p1=strstr(p,"&gt;="); p1!=NULL; p1=strstr(p1+1,"&gt;=")) {
  546.       if(*(p1+5)!='=') { string_modify(p,p1,p1+5,"%sge",m_prefix);}
  547.     }
  548.     for(p1=strstr(p,"-&gt;"); p1; p1=strstr(p1+1,"-&gt;")) {
  549.       for(p2=p1; p2>p && *(p2-1)=='-'; p2--);
  550.       if(p2>p && *(p2-1)==';') continue;
  551.       if(p2<p1) { string_modify(p,p2,p1+5,"%slongrightarrow",m_prefix);}
  552.       else {  string_modify(p,p1,p1+5,"%srightarrow",m_prefix);}
  553.     }
  554.     for(p1=strstr(p,"=&gt;"); p1; p1=strstr(p1+1,"=&gt;")) {
  555.       for(p2=p1; p2>p && *(p2-1)=='='; p2--);
  556.       if(p2>p && *(p2-1)==';') continue;
  557.       if(p2<p1) { string_modify(p,p2,p1+5,"%sLongrightarrow",m_prefix);}
  558.       else { string_modify(p,p1,p1+5,"%sRightarrow",m_prefix) ;}
  559.     }
  560. /* Not equal */
  561.     for(p1=strstr(p,"!="); p1; p1=strstr(p1+1,"!=")) {
  562.       if(p1>p && !isspace(*(p1-1))) continue;
  563.        string_modify(p,p1,p1+2,"%sneq",m_prefix);
  564.     }
  565. }
  566.  
  567. /* why <tt> is used sometimes ? replace single characters by italics one
  568.  * is it useful in mathml ?
  569. */
  570. void __replace_italics (char *p, int n)
  571. { char *p1, *p2, *p3, pbuf[16];
  572.   char *ITBEG, *ITEND, *ITtBEG, *ITtEND;
  573.    if (n) {
  574.      ITBEG = "<i>";ITtBEG = "<i><tt>";
  575.      ITEND = "</i>";ITtEND = "</tt></i>";
  576.   } else {;
  577.      ITBEG = "" ; ITtBEG = "";
  578.      ITEND = ""; ITtEND = "";
  579.   }
  580.  
  581.   for(p1=p;*p1;p1++) {
  582.     if(*p1=='<') {
  583.         p1=strchr(p1,'>'); if(p1==NULL) break; //recognize an html tag < >
  584.         else continue;
  585.     }
  586.     if(*p1=='=' && *(p1+1)=='-') {
  587.         string_modify(p,p1+1,p1+1," "); p1+=2; continue;
  588.     }
  589.     if(*p1=='\'') {
  590.         for(p2=p1+1;*p2=='\'';p2++);
  591.         memmove(pbuf,p1,p2-p1); pbuf[p2-p1]=0;
  592.         string_modify(p,p1,p2,"%s%s%s",ITtBEG,pbuf,ITtEND);
  593.         p1=p2+strlen(ITtBEG)+strlen(ITtEND)-1;
  594.         continue;
  595.     }
  596.     if(!isalpha(*p1)) continue;
  597. /* unique letter.*/
  598.     for(p2=p1+1;isalpha(*p2);p2++);
  599.     p3=find_word_start(p2);
  600.     if(p2>p1+5 ||
  601.        (p2>p1+1 && (*p3==';' || *p3=='(' || myisdigit(*p2))))
  602.         {p1=p2-1; continue;}
  603.     if(strncasecmp(p3,"</i>",strlen("</i>"))==0) continue;
  604.     memmove(pbuf,p1,p2-p1); pbuf[p2-p1]=0;
  605.     string_modify(p,p1,p2,"%s%s%s",ITBEG,pbuf,ITEND);
  606.     p1=p2+strlen(ITBEG)+strlen(ITEND)-1;
  607.     }
  608. }
  609.  
  610. /* float (1.2 E-03) : 3E+021 -> 3 × 10^{21} - 3E-21 -> 3 × 10^{-21}
  611.  * or replace variable name (alpha)
  612.  * if int n != 0, use html code, else use tex code */
  613. void __replace_mathvar(char *p,int n)
  614. { char *p1, *p2, *p3;
  615.   char *EXPBEG, *EXPEND, *EXPBEGMINUS, *SUBBEG, *SUBEND, *m_prefix;
  616.  
  617.   if ( n ) {
  618.      EXPBEG = " &times; 10<sup>";
  619.      EXPBEGMINUS = " &times; 10<sup>-";
  620.      EXPEND = "</sup>";
  621.      SUBBEG = "<sub>";
  622.      SUBEND = "</sub>";
  623.      m_prefix="$m_";
  624.   } else {
  625.      EXPBEG = " \\times 10^{" ;
  626.      EXPBEGMINUS = " \\times 10^{-" ;
  627.      EXPEND = "}";
  628.      SUBBEG = "_{";
  629.      SUBEND = "}";
  630.      m_prefix="\\";
  631.   }
  632.   for (p1=find_mathvar_start(p);*p1!=0;p1=find_mathvar_start(p2)) {
  633.     char buf[MAX_LINELEN+1];
  634.     /* if the variable is preceded by \ do nothing - in fact this should not arrive
  635.     */
  636.     if (p1>p && *(p1-1) == '\\' ) break ;
  637.     p2 = find_mathvar_end(p1);
  638.         if (p1 == p2) break;
  639.     memmove(buf,p1,p2-p1);buf[p2-p1]=0;
  640.  
  641.     if(myisdigit(buf[0])) {
  642.             /* number : 3E+021 -> 3 x 10^{21} - 3E-21 -> 3 x 10^{-21} */
  643.         int k = 1, minus = 0;
  644.  
  645. /* see putnumber in texmath.c*/
  646.         if( (p3 = strpbrk(buf, "Ee")) == NULL) continue;
  647.         p1 += p3-buf;
  648.             //count the number of 0, +, -
  649.         if (*(p1+k) == '-') { minus = 1; k++; }
  650.         while(*(p1+k)=='0' || *(p1+k)=='+') k++;
  651.  
  652.         string_modify(p,p1,p1+k, minus? EXPBEGMINUS: EXPBEG);
  653.         p2 += strlen(minus? EXPBEGMINUS: EXPBEG)-k;
  654.         string_modify(p,p2,p2, EXPEND);
  655.         p2 += strlen(EXPEND);
  656.     } else {
  657.             /* alphabetic name, replace greek letters and symbols in hmname*/
  658.             /* not done in texmath.c*/
  659.         int i = search_list(hmname,hmname_no,sizeof(hmname[0]),buf);
  660.         char *n, *r;
  661.         if(i<0) { /* not in list */
  662.                 int k = strlen(buf)-1;
  663.             if(myisdigit(buf[k])) {
  664.                while(k>0 && myisdigit(buf[k-1])) k--;
  665.                string_modify(buf,buf+k,buf+k,SUBBEG);
  666.                string_modify(p,p1,p2,"%s%s",buf,SUBEND);
  667.                p2+=strlen(SUBBEG)+strlen(SUBEND);
  668.             }
  669.             continue;
  670.         }
  671.         n = hmname[i].name;
  672.         r = mathalign_base < 2? hmname[i].replace: hmname[i].replacem;
  673.         if(r[0]==0) { /* replace = "" */
  674.                 string_modify(p,p1,p2,"%s%s",m_prefix, n);
  675.                 p2 = p1+strlen(n)+strlen(m_prefix);
  676.         } else {
  677.                 string_modify(p,p1,p2,"%s",r);
  678.                 p2 = p1+strlen(r);
  679.             }
  680.         }
  681.   }
  682. }
  683.  
  684. /* translate raw math expression coming from calculators into best html way
  685.  * if int n != 0, use html code, else use tex code */
  686. void __htmlmath(char *p,int n)
  687. {
  688.     if(!rawmath_easy) { rawmath_easy=1; rawmath(p); rawmath_easy=0;}
  689.     __replace_htmlmath_gtlt(p); //only used in deductio
  690.     __replace_exponent(p,n);
  691.     __replace_subscript(p,n);
  692.     __replace_getrid1(p);
  693.     __replace_mathvar(p,n);
  694.     __replace_getridstar(p);
  695.     __replace_arrow(p,n);
  696. /* Now make substitutions */
  697.     substit(p);
  698. /* Make single names italic - done automatically by mathml */
  699.    __replace_italics(p,n);
  700.    strip_trailing_spaces(p);
  701. }
  702.  
  703. void htmlmath(char *p)
  704. {
  705.  __htmlmath(p,1) ;
  706. }
  707.  
  708. /* if mathml is closed, it will be just htmlmath*/
  709.  
  710. void mathmlmath(char *p)
  711. {
  712.     if (mathalign_base == 2) { __htmlmath(p,0) ; mathml(p,1);} else { __htmlmath(p,1) ;}
  713. }
  714.