Subversion Repositories wimsdev

Rev

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

Rev Author Line No. Line
7673 bpr 1
/*    Copyright (C) 2012 WIMSDEV
5522 bpr 2
 *    This file is part of the WIMS package.
3
 *
4
 *  This program is free software; you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  (at your option) any later version.
8
 *
9
 *  This program is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this program; if not, write to the Free Software
16
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
 */
8185 bpr 18
#include "wims.h"
5522 bpr 19
 
20
int mathml(char *p, int option ){
5578 schaersvoo 21
    if (strlen(p)==0) return 1 ;
5522 bpr 22
    if( mathalign_base <= 1){
23
      internal_error(" why is wims trying mathml()?\n");
5595 schaersvoo 24
      return 0; // go to insmath with gifs
5522 bpr 25
    }
26
    if(strlen(p) > MAX_LINELEN ){ // too big ? probably too big for gifs as well ; but error signalling is better in gif-methods
27
      mathalign_base = 1;// 0 or 1 position of tex_gifs
5595 schaersvoo 28
      return 0; // go to insmath with gifs
5522 bpr 29
    }
30
    //singlespace(p); // needed for check unbalanced \left\right in wims_mathml.cc --> insmath.c
31
    singlespace(p);
32
    int my_pipe[2];
33
    pid_t pid;
34
    if(pipe(my_pipe)){// pipe could not be opened...
35
      internal_error("mathml(): pipe() failure.\n");
36
      mathalign_base = 1;
5595 schaersvoo 37
      return 0; // go to insmath with gifs
5522 bpr 38
    }
39
    else
40
    {
41
      pid = fork();
42
      if (pid == (pid_t) 0){
43
          // this buffer should probably be set to 0.5 * MAX_LINELEN : mathml is very big !
44
          // if output mathml string larger ;ERROR will be signaled by wims_matml.
45
          char mml_buffer[MAX_LINELEN+1];
46
          sprintf(mml_buffer, "%d", MAX_LINELEN);// int --> char
47
          // setting --tex-size argument to wims_mathml.cc
7673 bpr 48
          char mathml_tex_size[64];//this should be a string like "250%"
5556 schaersvoo 49
          int texsize_list[]={20,30,50,70,90,100,120,130,150,180,220,300};
5522 bpr 50
          // analogue to  mathfonts.c but now we talk %  and we could make any amount of changes
7673 bpr 51
          // not limited by font-size !!
52
          // when math_with_gifs is removed from wims, we can rethink / improve this
5522 bpr 53
          int idx = 6;
54
          int use_js_zoom = 0; // default js-zoom in mathml if disabled
55
          // enable via adm/light
7673 bpr 56
          // next code is stolen from wims.c
5522 bpr 57
          if(getvar("useropts") != NULL || getvar("wims_useropts") != NULL){
58
            char *u;
59
            u = getvar("useropts"); // set via adm/light or via cookie?
60
            if(u == NULL || *u == 0){ u=getvar("wims_useropts");} // wims_user? look into def file
61
            if(u != NULL && *u != 0){
7673 bpr 62
                if(myisdigit(u[0])){ //2 digit code : 12,22,32,...,92  [tex_size mathalignbase]
5522 bpr 63
                  idx = u[0] - '0';
64
                  if(idx < 0 || idx > 11){ idx = 6; } // the default value 120%
65
                }
66
                if(myisdigit(u[2]) && u[2]!=0){ // suppose we add 0 or 1 to useropts for using js-zoom on mathml?
67
                // 3 digitcode:  621 == fontsize idx=6 & use mathml & use zoom
68
                  use_js_zoom = u[2] - '0';
69
                  if(use_js_zoom > 1 || use_js_zoom < 0){
70
                      use_js_zoom = 0; //disable js-zooming of mathml
71
                  }
72
                }
73
            }
74
          }
75
          else
76
          { // we take the default setting from adm/management config
77
            idx = atoi(getvar("wims_texbasesize")) ;
78
            if(idx < 0 || idx > 11){ idx = 6; } // the default value 120%
79
          }
5578 schaersvoo 80
          // check is a module wants to disable zooming (eg drag&drop or others )
81
          // !set disable_zoom=yes
82
          // if not set: disable_zoom="no"; see config.c
83
          if( strcmp( getvar("disable_zoom") , "yes" ) == 0 ){
7673 bpr 84
             use_js_zoom = 0;
5578 schaersvoo 85
          }
5522 bpr 86
          // now write the "char" 200% into variable "mathml_tex_size"
87
          snprintf(mathml_tex_size,sizeof(mathml_tex_size),"%d%%",texsize_list[idx]);
88
          // int --> char : added % sign (needed for mathml) [%% = escaped %]
89
          if(mathml_tex_size == NULL || strlen(mathml_tex_size) == 0 ){ // this should not happen
7673 bpr 90
            sprintf(mathml_tex_size,"%s","120%");
91
            // if it goes wrong we set 120%
5522 bpr 92
            // default in itex2MML was 110% : but we thought it was too small... ?
93
          }
94
          char zoom[2];
95
          snprintf(zoom, 2 ,"%d",use_js_zoom);// int --> char : "0" or "1"
96
          char *argv[]={"wims_mathml","--use-zoom",zoom,"--tex-size",mathml_tex_size,"--max-mml-size",mml_buffer,"--tex-string",p,NULL};
8155 bpr 97
/* This is the child process. Close other end first. */
5522 bpr 98
          close(my_pipe[0]);
99
          dup2(my_pipe[1], 1);  // send stdout to the pipe
100
          dup2(my_pipe[1], 2);  // send stderr to the pipe
101
          close(my_pipe[1]);
102
          execv("../bin/wims_mathml",argv);
103
          internal_error("could not execute wims_mathml\n");
104
          mathalign_base = 1;
5595 schaersvoo 105
          return 0; // go to insmath with gifs
5522 bpr 106
      }
107
      else
108
      {
109
          if (pid < (pid_t) 0){
110
            close(my_pipe[0]);  // close the read end of the pipe in the parent
111
            close(my_pipe[1]);  // close the write end of the pipe in the parent
112
            internal_error("mathml(): fork() failure.\n");
113
            mathalign_base = 1;
5595 schaersvoo 114
            return 0; // go to insmath with gifs
5522 bpr 115
          }
116
          else
117
          {
7673 bpr 118
             int status;
5522 bpr 119
            FILE *stream;
120
            close(my_pipe[1]);  // close the write end of the pipe in the parent
121
            stream = fdopen (my_pipe[0], "r");
122
            char buffer[MAX_LINELEN+1];
123
            // make buffer filled with 'zero/null'
124
            memset(buffer,'\0',MAX_LINELEN);//bzero(buffer,maxsize);
8155 bpr 125
/* read output from program line by line */
5522 bpr 126
            if (option == 1) {
127
                *p=0;
128
                while ( fgets(buffer, MAX_LINELEN, stream) != NULL ){
7673 bpr 129
                     if(strcmp(buffer,"ERROR") != 0){
130
                      mystrncpy(p, buffer, MAX_LINELEN-1);
131
                     }
8155 bpr 132
                    else /* ERROR close stream; close pipe; wait for clean exit */
5537 schaersvoo 133
                    {
8155 bpr 134
                  fclose (stream); /* do not know if this is really needed... but it won't hurt ? */
7673 bpr 135
                  close(my_pipe[0]);
136
                  waitpid(pid, &status, 0);
8155 bpr 137
                     mathalign_base=1; /* go to insmath with gifs */
7673 bpr 138
                     return 0;
5537 schaersvoo 139
                    }
5522 bpr 140
                }
141
            }
8155 bpr 142
            else /* this will probably not used ? remove it ? */
5522 bpr 143
            {
7673 bpr 144
             while ( fgets(buffer, MAX_LINELEN, stream) != NULL ){
145
                 if(strcmp(buffer,"ERROR") != 0){
146
                  output("%s", buffer);
147
                 }
5537 schaersvoo 148
                    else
149
                    {
7673 bpr 150
                     mathalign_base=1;
151
                     return 0;
5522 bpr 152
                    }
5537 schaersvoo 153
                }
5522 bpr 154
            }
155
            fclose (stream);
156
            close(my_pipe[0]);
157
            waitpid(pid, &status, 0);
158
          }
159
       }
160
    }
161
    return 1;
162
}