Subversion Repositories wimsdev

Rev

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