Subversion Repositories wimsdev

Rev

Rev 10 | Rev 8147 | 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
 
7676 bpr 18
/* Check type of a file */
10 reyssat 19
 
20
/*************** Customization: change values hereafter ****************/
21
 
7676 bpr 22
/* limit of data buffers */
10 reyssat 23
#define buflim 1024*1024*16
24
 
25
/***************** Nothing should need change hereafter *****************/
26
 
27
#include "../wims.h"
28
 
29
char filename[1024]="";
30
char *filebuf;
31
int filelen=0;
32
 
33
void *xmalloc(size_t n)
34
{
35
    void *p;
36
    p=malloc(n);
37
    if(p==NULL) exit(1);
38
    return p;
39
}
40
 
7676 bpr 41
/* get the file */
10 reyssat 42
void prepare_file(void)
43
{
44
    FILE *f;
45
    long int flen;
46
 
47
    filelen=0;
48
    f=fopen(filename,"r"); if(f==NULL) return;
49
    fseek(f,0,SEEK_END);flen=ftell(f); fseek(f,0,SEEK_SET);
50
    if(flen>buflim) return;
51
    filebuf=xmalloc(2*flen+1024);flen=fread(filebuf,1,flen,f);
52
    fclose(f);
53
    if(flen>0 && flen<buflim) filebuf[flen]=0; else flen=0;
54
    filelen=flen;
55
}
56
 
57
int main(int argc, char *argv[])
58
{
59
    char *ftype="text";
60
    char *p;
61
    char *mod, *unt;
62
 
63
    unt=getenv("untrust");
64
    if(unt!=NULL && strcasecmp(unt,"yes")==0) return 1;
65
    mod=getenv("w_module");
66
    if(mod==NULL) p=argv[1]; else p=getenv("wims_exec_parm");
67
    if(p==NULL || *p==0) return 1;
68
    snprintf(filename,sizeof(filename)-128,"%s",p);
69
    prepare_file();
70
    for(p=filebuf;p<filebuf+filelen;p++) {
7676 bpr 71
      if((*p>=0 && *p<=6) || (127&*p)<=1)
72
          {ftype="binary"; goto fin;}
10 reyssat 73
    }
74
    for(p=strchr(filebuf,'<'); p!=NULL; p=strchr(p,'<')) {
7676 bpr 75
      p++;
76
      if((strncasecmp(p,"body",4)==0 && !isalnum(*(p+4))) ||
77
         (strncasecmp(p,"html",4)==0 && !isalnum(*(p+4))) ||
78
         (strncasecmp(p,"img",3)==0 && isspace(*(p+3))) ||
79
         strncasecmp(p,"p>",2)==0 ||
80
         strncasecmp(p,"/a>",3)==0) {
81
          ftype="html"; goto fin;
82
      }
10 reyssat 83
    }
84
    if(strstr(filebuf,"\\begin{")!=NULL ||
85
       strstr(filebuf,"\\end{")!=NULL ||
86
       (strchr(filebuf,'$')!=NULL && strchr(filebuf,'\\')!=NULL &&
7676 bpr 87
      strchr(filebuf,'{')!=NULL)) {
88
      ftype="latex"; goto fin;
10 reyssat 89
    }
90
    fin: printf("%s",ftype);
91
    return 0;
92
}
93