cgi_html.c

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /* cgi_html.c © Copyright 1998-2005 by Raosoft Inc. All Rights Reserved.      */
00003 /*                                                                            */
00004 /* You may use and modify this file for your own use, but may not distribute  */
00005 /* it or derivative works without the prior written consent of Raosoft, Inc.  */
00006 /*                                                                            */
00007 /* If you choose to share your modifications with Raosoft, Inc. the company   */
00008 /* will attempt to incorporate them into future versions of this file.        */
00009 /*                                                                            */
00010 /* This software is provided "as is," and Raosoft makes no warranty, express  */
00011 /* or implied, of fitness for a particular application. Every measure has been*/
00012 /* taken to anticipate risks in//HEREnt to computer networks, but we cannot   */
00013 /* guarantee safety or reliability of this program in every situation.        */
00014 /*                                                                            */
00015 /******************************************************************************/
00016 
00017 /* read an HTML file, set form entries according to a lookup-table from an */
00018 /* ASCII file. Like it? Send me a note: shanti@raosoft.com */
00019 
00020 #include "cgi.h"
00021 
00022 #define EATCOLON(x) while (*x && (strchr(":",*x) != NULL)) x++
00023 #define EATWHITE(x) while (*x && (strchr(" \t\r\n",*x) != NULL)) x++
00024 #define EATTEXT(x) while (*x && (strchr(" \t\r\n",*x) == NULL)) x++
00025 #define STRIPWHITE(x) {size_t n = strlen(x); while (n && strchr(" \t\r\n",x[n-1])) x[--n] = 0;}
00026 #ifdef __BORLANDC__
00027 #pragma warn -par
00028 #endif
00029 
00030 size_t FindEnd(char* str)
00031 {/* search for quote + a character from c2 */
00032   int quote = str[0];
00033   size_t i = 1;
00034 
00035   while (str[i] && str[i] != quote)
00036   {
00037    if (str[i] == '\\') if (str[++i]) {i++; continue; }
00038    i++;
00039   }
00040   return i;
00041 }
00042 
00043 void unescape_c(char*c)
00044 {
00045  char* d = c;
00046  while (*d)
00047  {
00048   if (*d == '\\')
00049    {
00050     if (d[1] == 0) {*c = *d; break;}
00051     if (d[1] == '\r') *c = '\r';
00052     else if (d[1] == '\n') *c = '\n';
00053     else *c = d[1];
00054     c++;
00055     d += 2;
00056    }
00057   else *c++ = *d++;
00058  }
00059  *c = 0;
00060 }
00061 
00062 extern char calctemp[128];
00063 
00090 int FormatReplacement(char* CurrentVariable, STREAM htmlout, CGINameValue* v,CGINameValue* v2)
00091 {
00092  char * value=NULL;
00093  char * ifyes=NULL;
00094  char * ifno=NULL;
00095  size_t lyes =0;
00096  int yes;
00097  char * start;
00098 
00099  if (!htmlout) return 0;
00100  if (v == NULL)
00101   {
00102    HTMLWrite(htmlout,CurrentVariable);
00103    return -1;
00104   }
00105 
00106  yes = EvaluateComparison(CurrentVariable,v,v2,&start);
00107 
00108  if (yes == -2)
00109     {
00110      return -2;
00111     }
00112     if (yes == -1) /* simple replacement */
00113      {
00114        if (!strnicmp(CurrentVariable,"toFixed(",8))
00115        {
00116          char*c;
00117        char a[8];
00118        double dbl;
00119        size_t i, length;
00120          c = CurrentVariable + 8;
00121        i = length = strlen(c);
00122         while (i > 0 && c[i] != ',') i--;
00123         if (i == 0)
00124        {
00125         if (c[length-1] == ')') length--;
00126          value = EvaluateExpressionL(v,v2,c,length);
00127        }
00128         else
00129         {
00130         value = CGIMALLOC(64);
00131           sprintf(a,"%%.%df",atoi(c+i+1));
00132         c = EvaluateExpressionL(v,v2,c,i);
00133         dbl = strtod(c,0);
00134         CGIFREE(c);
00135           sprintf(value,a,dbl);
00136         }
00137        }
00138        else
00139       {
00140         value = EvaluateExpression(v,v2,CurrentVariable);
00141         if (*value && strchr(value,'.'))
00142         {
00143          size_t i = strlen(value)-1;
00144          while (value[i] == '0') value[i--]=0;
00145          if (value[i] == '.') value[i] =0;
00146         }
00147        }
00148       HTMLEscape(htmlout,value);
00149       CGIFREE(value);
00150       return -1;
00151      }
00152  else
00153    {
00154     /* {field=value?true:false} */
00155      char* start1 = start;
00156      start = strchr(start,'?');
00157 
00158      if (start)
00159      {
00160        start++;
00161        EATWHITE(start);
00162        ifyes = start;
00163        if (*ifyes == '\'' || *ifyes == '\"')
00164        {
00165         size_t len = FindEnd(ifyes);
00166         ifyes[len]=0;
00167         ifyes[0] = '\'';
00168         /* ifyes ++; skip the first quote */
00169         start = ifyes + len + 1;
00170        }
00171        else start = strchr(start,':');
00172      }
00173      else start = strchr(start1,':');
00174 
00175      if (start)
00176      {
00177        lyes = start - ifyes;
00178        EATWHITE(start);
00179        EATCOLON(start);
00180 
00181        EATWHITE(start);
00182        ifno = start; //ifno gets set to start
00183        if (*ifno == '\'' || *ifno == '\"')
00184        {
00185         ifno[FindEnd(ifno)] = 0;
00186         ifno[0] = '\'';
00187         /* ifno++; */
00188        }
00189      }
00190 
00191      if (htmlout)
00192      {
00193       if (yes)
00194         {
00195          if (ifyes)
00196          {
00197            if (*ifyes == '\'') /* *ifyes == '?' */
00198            {
00199              unescape_c(++ifyes); /* null termination added earlier */
00200              HTMLWrite(htmlout,ifyes);
00201            }
00202            else
00203            {
00204              value = EvaluateExpressionL(v,v2,ifyes,lyes?lyes:0);
00205              HTMLEscape(htmlout,value);
00206            }
00207          }
00208         }
00209       else
00210         {
00211          if (ifno)
00212          {
00213            if (*ifno == '\'') /* *ifno == ':' */
00214            {
00215              unescape_c(++ifno);
00216              HTMLWrite(htmlout,ifno);
00217            }
00218             else
00219            {
00220              value = EvaluateExpression(v,v2,ifno + 1);
00221              HTMLEscape(htmlout,value);
00222            }
00223          }
00224         }
00225      }
00226      CGIFREE(value);
00227      return yes ? 1 : 0;
00228     }
00229 }
00230 
00231 void WriteHTMLAttributes(STREAM htmlout, CGINameValue * v)
00232 {
00233  int i;
00234  if (!htmlout) return;
00235  for (i=0; v[i].name; i++)
00236  {
00237   if (v[i].name[0])
00238   {
00239    HTMLWrite(htmlout," ");
00240    HTMLWrite(htmlout,v[i].name);
00241    if (v[i].value)
00242    {
00243     HTMLWrite(htmlout,"=\"");
00244     HTMLEscape(htmlout,v[i].value);
00245     HTMLWrite(htmlout,"\"");
00246    }
00247   }
00248  }
00249 }
00250 
00251 /* Usage:
00252   OpenDatabase
00253   int DatabaseFindLastRecord(DATABASE * database,CGINameValue* header,
00254                                                CGINameValue* userid,
00255                                                int casesensitive);
00256   OpenFile
00257   HTMLPresetForm(htmlout,file,header);
00258   CloseFile
00259   CloseDatabase
00260 */
00261 
00262 
00263 /* Commands spoken //HERE:
00264   include filename
00265   source filename
00266   report database_name query_string sort_field sort_direction maxrows
00267 */
00268 
00269 void EZSError(STREAM htmlout,FILE* source,char* command,char* error)
00270 {
00271  if (!htmlout) return;
00272  HTMLWrite(htmlout,"<HR>Error in (");
00273  HTMLWrite(htmlout,command);
00274  HTMLPrintf(htmlout,")[%d]: ",ftell(source));
00275  HTMLWrite(htmlout,error);
00276  HTMLWrite(htmlout,"<HR>");
00277 }
00278 
00279 int ReadNextWord(FILE* source,char* name,size_t length)
00280 {
00281  name[0]=0;
00282  while (fread(name,1,1,source))
00283  {
00284   if (!strchr(" \t\r\n",name[0])) break;
00285   name[0]=0;
00286  }
00287  if (name[0] == '\'' || name[0] == '\"')
00288   {
00289    size_t i=0;
00290    int end = name[0];
00291    char* buf = name;
00292    while (1)
00293      {
00294       if (!fread(buf+i,1,1,source)) return -1;
00295       if (buf[i] == '\\')
00296        {
00297         if (!fread(buf+i,1,1,source)) break;
00298         if (buf[i] == 'n') buf[i] = '\n'; /* special replacement codes */
00299         else if (buf[i] == 'r') buf[i] = '\r';
00300         else if (buf[i] == 't') buf[i] = '\t';
00301        }
00302       else if (buf[i] == end) break;
00303       if (i < length-1) i++;
00304      }
00305     buf[i] = 0;
00306   }
00307  else return ReadUntilChar(source,name+1,length-1," \t\r\n",0);
00308  return name[0]; //quotation mark
00309 }
00310 
00311 int ReadParameter(FILE* source,char* name,size_t length)
00312 {
00313  name[length-1] = 0;
00314  name[0] = 0;
00315 
00316  ReadNextWord(source,name,length-1);
00317 
00318  if (!name[0])
00319   return 0;
00320 
00321  if (!strcmp(name,"%>"))
00322    {
00323      name[0] = 0;
00324      return 0;
00325    }
00326  return 1;
00327 }
00328 
00329 int EZSInclude(char* argv0,STREAM htmlout,FILE* source,CGINameValue* cgidata,CGINameValue* dbdata)
00330 {
00331  char p[MAXPATH];
00332  FILE* f;
00333 
00334  if (!ReadParameter(source,p,MAXPATH))
00335   {
00336    EZSError(htmlout,source,"include","parameter missing");
00337    return 0;
00338   }
00339 
00340  ReadUntilWordS(source,NULL,"%>");
00341  f = FileOpen(argv0,p,"");
00342  if (!f)
00343   {
00344    EZSError(htmlout,source,p,"cannot open file");
00345    return 0;
00346   }
00347 
00348  RunReport(argv0,htmlout, f, cgidata, dbdata,0,0,0);
00349  CGIFCLOSE(f);
00350  return 1;
00351 }
00352 
00353 int EZSSource(char* argv0,STREAM htmlout,FILE* source)
00354 {
00355  char filename[MAXPATH],p[MAXPATH];
00356  if (!ReadParameter(source,p,MAXPATH))
00357   {
00358    EZSError(htmlout,source,"source","parameter missing");
00359    return 0;
00360   }
00361 
00362  ReadUntilWordS(source,NULL,"%>");
00363  ExpandLocalPath(argv0,filename,p,"");
00364  if (!HTMLWriteFile(htmlout, filename))
00365   {
00366    EZSError(htmlout,source,filename,"cannot open file");
00367    return 0;
00368   }
00369  return 1;
00370 }
00371 
00382 int EZSRandom(char* argv0,STREAM htmlout,FILE*source, CGINameValue* cgidata,
00383                     CGINameValue* dbdata,CGINameValue* unused, int preset)
00384 {
00385  char group[32];
00386  char done[32];
00387  char* finish;
00388  int loc[128];
00389  int n=0;
00390  int i;
00391  int end;
00392  int r=452;
00393 
00394  if (!ReadParameter(source,group+1,sizeof(group)-2)) return 0;
00395  if (!ReadParameter(source,done+1,sizeof(done)-2)) return 0;
00396 
00397  ReadUntilWordS(source,NULL,"%>");
00398 
00399  group[0] = done[0] = '<';
00400  strcat(group,">");
00401  strcat(done,">");
00402  if (strlen(done) != strlen(group)) return 0;
00403 
00404  for (i=0; i<128; i++)
00405  {
00406   loc[i] = ftell(source);
00407   if (ReadUntilWordS2(source,NULL,group,done) != 1) break;
00408   n++;
00409  }
00410 
00411  end = ftell(source);
00412 
00413  if (n > 1)
00414  {
00415  char* seed = GetFieldValue(cgidata,"_SESSION");
00416  while (*seed) r += *seed++;
00417  seed = GetFieldValue(cgidata,"HOST");
00418  while (*seed) r += *seed++;
00419 
00420  for (i=0; i<n; i++)
00421  {
00422   int x = loc[i];
00423   int y;
00424 #ifdef __WINCE__
00425   r = r + x + GetTickCount();
00426 #else
00427   r = r + x + clock();
00428 #endif
00429   if (r<0) r = -r;
00430   y = r % n;
00431   loc[i] = loc[y];
00432   loc[y] = x;
00433  }
00434  }
00435 
00436  finish = group+1;
00437  swapchars(finish,'>',0);
00438 
00439  for (i=0; i<n; i++)
00440  {
00441   fseek(source,loc[i],SEEK_SET);
00442   RunReport(argv0,htmlout, source,cgidata,dbdata,unused,preset,finish);
00443  }
00444  fseek(source,end,SEEK_SET);
00445  return 0;
00446 }
00447 
00452 int EZSCounts(char* argv0,STREAM htmlout,FILE*source, CGINameValue* data)
00453 {
00454  char statfile[MAXPATH],filename[MAXPATH];
00455  CGINameValue* count;
00456  CGINameValue* sum;
00457  CGINameValue* total;
00458  int i;
00459 
00460  if (!ReadParameter(source,statfile,sizeof(statfile))) return 0;
00461  ReadUntilWordS(source,NULL,"%>");
00462 
00463  ExpandLocalPath(argv0,filename,statfile,"");
00464  count = ReadINIFileSection(filename, "COUNT",0);
00465  sum = ReadINIFileSection(filename, "SUM",0);
00466 
00467  if (sum)
00468  {
00469  for (i=0;sum[i].name;i++)
00470  {
00471   char * x = strdup3(sum[i].name,".SUM","");
00472   CGIFREE(sum[i].name);
00473   sum[i].name = x;
00474  }
00475  }
00476 
00477  if (count != NULL && sum != NULL)
00478  {
00479   total = CopyListJoin(count,sum,0);
00480   DeleteNVP(count);
00481   DeleteNVP(sum);
00482  }
00483  else
00484  {
00485   total = count;
00486   if (total == NULL) total = sum;
00487  }
00488 
00489  RunReport(argv0,htmlout,source,data,total,0,0,"/report"); /* set the next form in the file */
00490 
00491  DeleteNVP(total);
00492 
00493  return 0;
00494 }
00495 
00496 /*  report database_name query_string sort_field sort_direction maxrows */
00497 /*  report database_name query_string */
00498 int EZSReport(char* argv0,STREAM htmlout, FILE* source, CGINameValue* data)
00499 {
00500  int reportstart;
00501  char datafile[MAXPATH],p[MAXPATH],querystr[1024];
00502  CGINameValue * header;
00503  DATABASE * database;
00504  int i;
00505  int rows=0;
00506  char * query = querystr;
00507 
00508  if (!ReadParameter(source,p,sizeof(datafile))) return 0;
00509  ReadUntilWord(source,querystr,sizeof(querystr),"%>");
00510  EATWHITE(query);
00511  STRIPWHITE(query);
00512  if (*query == '\'' || *query == '\"')
00513   {
00514    query[FindEnd(query)]=0;
00515    query ++;
00516   }
00517  else if (*query == '$')
00518   {
00519    query = GetFieldValue(data,query+1);
00520   }
00521 
00522  ExpandLocalPath(argv0,datafile,p,"");
00523 
00524  if (!DatabaseOpen(datafile,&database,&header,1,0))
00525  {
00526    HTMLWrite(htmlout,"[Error: Unable to open the database ");
00527    HTMLWrite(htmlout,datafile);
00528    HTMLWrite(htmlout,"]");
00529    LogError("\nCGI_DATA: Unable to open the database: ");
00530    LogError(datafile);
00531    return 3;
00532  }
00533 
00534  /* doesn't check security flags. if you wrote the report file, you've */
00535  /* presumably thought about that already */
00536 
00537  RenameField(header,"","#");
00538  reportstart = ftell(source);
00539 
00540  /* case-insensitive search */
00541  RunReport(argv0,0,source,0,0,0,0,"/report");
00542  i=0;
00543  while((rows ? i < rows : 1) && DatabaseFindNextRecord(database,header,data,query))
00544   {
00545    char t[32];
00546    i++;
00547    sprintf(t,"%d",i);
00548 
00549    SetFieldValue(header,"#",t);
00550    fseek(source,reportstart,SEEK_SET);
00551    RunReport(argv0,htmlout,source,data,header,0,0,"/report"); /* set the next form in the file */
00552   }
00553 
00554  DatabaseClose(database,header);
00555  return 0; /* the end of evaluation of this block */
00556 }
00557 
00558 void ExecEZSCommand(char* argv0,STREAM htmlout, FILE* source, CGINameValue* cgidata,
00559                     CGINameValue* dbdata,
00560                     CGINameValue* unused,
00561                     int preset)
00562 {/* Our versions of Active Data Pages */
00563  char cmd[1024]; /* no single symbol can be longer than that */
00564 
00565  if (!htmlout) return;
00566  while (ReadNextWord(source,cmd,sizeof(cmd)) > 0)
00567  {
00568   if (!stricmp(cmd,"%>")) return;
00569   else if (!stricmp(cmd,"include")) {if (!EZSInclude(argv0,htmlout,source,cgidata,dbdata)) return;}
00570   else if (!stricmp(cmd,"source")) {if (!EZSSource(argv0,htmlout,source)) return;}
00571   else if (!stricmp(cmd,"report")) {if (!EZSReport(argv0,htmlout,source,cgidata)) return;}
00572   else if (!stricmp(cmd,"random")) {if (!EZSRandom(argv0,htmlout,source,cgidata,dbdata,unused,preset)) return;}
00573   else if (!stricmp(cmd,"stats")) {if (!EZSCounts(argv0,htmlout,source, cgidata)) return;}
00574   else {
00575      EZSError(htmlout,source,cmd,"Unknown command");
00576      }
00577  }
00578 }
00579 
00580 void FinishTag(FILE* source,char* tag, size_t size, int end, char* finish,char* stripend)
00581 {
00582   size_t l = strlen(tag);
00583   size_t f = strlen(finish);
00584 
00585  // //HERE(finish);
00586 
00587   if (!l) return;
00588 
00589   if (end && end != EOF)
00590     {
00591      tag[l] = (char) end;
00592      l++;
00593      tag[l]=0;
00594     }
00595 
00596   if (l > f && !strcmp(tag+l-f,finish))
00597    {
00598     l -= f;
00599     tag[l]=0;
00600    }
00601   else if (l < size)
00602    {
00603     ReadUntilWord(source,tag+l,size-l,finish);
00604     l = strlen(tag);
00605    }
00606   else
00607     ReadUntilWord(source,0,0,finish);
00608 
00609 
00610   while (l > 0 && strchr(stripend,tag[l-1]))
00611    {
00612     l--;
00613     tag[l] = 0;
00614    }
00615 }
00616 
00617 /* for checkbox questions with overlapping fieldnames */
00618 /* exhaustively checks all the possible values */
00619 /* returns 1: found, 0: not found, 2: no field */
00620 int IsValueSelected(CGINameValue *data,char * fieldname, char * value)
00621 {int x;
00622  int ret=0;
00623 for(x=0; data[x].name; x++)
00624    {
00625     if (!data[x].name[0]) continue;
00626     if (!stricmp(fieldname,data[x].name))
00627      {
00628       ret = 2;
00629       if (!data[x].value) continue;
00630       if (data[x].value == 0 && value[0] == 0) return 1;
00631       if (HasToken(data[x].value,value,NULL)) return 1;
00632      }
00633    }
00634 return ret;
00635 }
00636 int PrintScriptError(STREAM htmlout,ScriptEnvironment* Env, FILE* script, int errcode)
00637 {
00638  char * err;
00639  int len = Env->curpos - Env->lastLine;
00640  switch (errcode)
00641  {
00642   case 40: err="Need a closing quote";break;
00643   case 41: err="( expected after if or elsif";break;
00644   case 42: err="Need a closing paren";break;
00645   case 43: err="Misplaced string constant";break;
00646   case 44: err="An else or elsif must come after an if";break;
00647   case 45: err="Object variable not found";break;
00648   case 46: err="Unable to process this command";break;
00649   case 47: err="Check for an unterminated string, unescaped quote, or missing + sign"; break;
00650   case 48: err="Function not found";                  break;
00651   case 49: err="Expecting an expression"; break;
00652   case 70: err="{ expected after an if "; break;
00653   case 71: err="Need a closing brace";break;
00654   default: err="Unknown scripting error";
00655  }
00656  HTMLPrintf(htmlout,"Script error %d: %s:",errcode,err);
00657  if (len > 1024) len = 1024;
00658  if (len > 0 && script != NULL)
00659  {
00660   char c[1024];
00661   fseek(script,Env->lastLine,0);
00662   fread(c,len,1,script);
00663   HTMLWriteL(htmlout,c,len);
00664  }
00665  return 0;
00666 }
00667 
00668 #if 0
00669 #define PROFILEBEGIN(x) int _timers[x]; {int _t; for (_t=0; _t<x; _t++) _timers[_t] = 0;}
00670 #define START(i) {_timers[i] -= GetTickCount();}
00671 #define STOP(i) {_timers[i] += GetTickCount();}
00672 #define PROFILEEND {int _t; char msg[48]; for (_t=0; _t<32 && _timers[_t]; _t++)\
00673 if (_timers[_t]) {sprintf(msg,"(%d=%d) ",_t,_timers[_t]); LogError(msg);}}
00674 #else
00675 #define PROFILEBEGIN(x)
00676 #define START(i)
00677 #define STOP(i)
00678 #define PROFILEEND
00679 #endif
00680 
00681 int RunReport(char* argv0, 
00682               STREAM htmlout, 
00683               FILE* source,
00684               CGINameValue * cgidata,
00685               CGINameValue * dbdata, 
00686               CGINameValue * unused,
00687               int preset,
00688               char* stop)
00689 {
00690  return RunReportF( argv0, htmlout,source,cgidata,dbdata,unused,preset,stop,0,0);
00691 }
00692 
00693 int RunReportF(char* argv0, 
00694               STREAM htmlout, 
00695               FILE* source,
00696               CGINameValue * cgidata,
00697               CGINameValue * dbdata, 
00698               CGINameValue * unused,
00699               int preset,
00700               char* stop,
00701               char* lastData, 
00702               ScriptFunction * extra 
00703               )
00704 {
00705  char c[2],tag[MAXBUF],currentname[128];
00706  int inscript=0;
00707  int e;
00708 
00709  currentname[0]=0;
00710  PROFILEBEGIN(32)
00711  c[1]=0;
00712 
00713  while (1)
00714   {
00715 
00716      START(0);
00717      if (fread(c,1,1,source) == 0) {STOP(0) break;}
00718      STOP(0)
00719 
00720    if (dbdata && inscript == 0)
00721    {/* search for {} replacements */
00722     if (c[0] == '{')
00723     {
00724      fread(tag,1,1,source);
00725      if (strchr("} \t\r\n",tag[0]))
00726      {/* skip it */
00727        HTMLWrite(htmlout,"{");
00728        HTMLWriteL(htmlout,tag,1);
00729        continue;
00730      }
00731      else
00732      {
00733      START(2)
00734        ReadUntilChar(source,tag+1,sizeof(tag)-3,"}",0);
00735       tag[sizeof(tag)-2] = tag[sizeof(tag)-1] = 0;
00736       STOP(2)
00737      if (!*tag)
00738       continue;
00739 
00740       if (stop)
00741       if (!stricmp(stop,tag))
00742        return 0;
00743 
00744        START(3)
00745       FormatReplacement(tag, htmlout, dbdata,0);
00746        STOP(3)
00747       continue;
00748      }
00749     }
00750    }
00751 
00752    if (c[0] != '<') /* it's a potential tag */
00753    {
00754        START(1)
00755     HTMLWrite(htmlout,c);
00756        STOP(1)
00757     continue;
00758    }
00759 
00760    START(4)
00761    e = ReadUntilChar(source,tag,sizeof(tag)-2,"> \t\r\n",0);
00762    tag[sizeof(tag)-2] = tag[sizeof(tag)-1] = 0;
00763    STOP(4)
00764    if (e == EOF) break;
00765 
00766    if (stop)
00767     if (!stricmp(stop,tag))
00768      {
00769       if (e != '>')
00770        ReadUntilChar(source,lastData,lastData?MAXPATH:0,">",0);
00771       return 0;
00772      }
00773 
00774   if (strnicmp(tag,"!--$",4) == 0)
00775    {
00776       START(5)
00777     FinishTag(source,tag, sizeof(tag), e, "-->"," \t\r\n");
00778     FormatReplacement(tag+4,htmlout,cgidata,dbdata);
00779     STOP(5)
00780     continue;
00781    }
00782 
00783   if (tag[0]=='%' && tag[1] == '=')
00784    {  /* <%=variable_name %>       */
00785   // //HERE("");
00786       START(6)
00787     FinishTag(source,tag, sizeof(tag), e, "%>"," \t\r\n");
00788     FormatReplacement(tag+2,htmlout,cgidata,dbdata);
00789     STOP(6)
00790     continue;
00791    }
00792 
00793   if (!strnicmp(tag,"%end",4)) /* %end or %endif */
00794    {
00795 //     //HERE("");
00796       START(7)
00797     if (e != '>') ReadUntilChar(source,0,0,">",0); /* finish the tag */
00798     STOP(7)
00799     continue;
00800    }
00801 
00802   if (!stricmp(tag,"%break"))
00803    break;
00804 
00805   startif:
00806 
00807   if (!strnicmp(tag,"%if",3) ||!strnicmp(tag,"%elsif",6)||!strnicmp(tag,"%elif",5))
00808    {/* note that nested %if doesn't work */
00809     START(8)
00810     FinishTag(source,tag,sizeof(tag), e, "%>"," \t\r\n");
00811     e = 0;
00812     if (!EvaluateLogic(tag+3,cgidata,dbdata))
00813      { /* skip ahead to the next condition */
00814       if (ReadUntilWordS2(source,0,"<%el","<%en") == 1) /* not case sensitive */
00815       {/* '%elsif' '%elif ' '%else%' '%else ' '%else>' (condition) */
00816        strcpy(tag,"%el");
00817        e = ReadUntilChar(source,tag+3,sizeof(tag)-5,"> \t\r\n",0);
00818 
00819        if (!stricmp(tag,"%else") && e &&strchr(" \t\r\n",e))
00820        {/* '%else %>' '%else >' '%else if (...) %>' */
00821         e = ReadUntilChar(source,tag,sizeof(tag)-2,"> \t\r\n",0);
00822 
00823         if (!stricmp(tag,"if") && e != '>')
00824            strcpy(tag,"%elif");
00825         else
00826            strcpy(tag,"%else");
00827        }
00828        if (!strnicmp(tag,"%elsif",6)||!strnicmp(tag,"%elif",5))
00829          goto startif;
00830       }
00831       if (e != '>') /* EOF or %endif or %else */
00832        e = ReadUntilChar(source,0,0,">",0);/* finish the tag */
00833       /* Resume from the %else and continue until %endif */
00834      }
00835     STOP(8)
00836     continue;
00837    }
00838   if (!strnicmp(tag,"%else",5)) /* %if must have been true, skip to the end */
00839    {
00840       START(9)
00841     ReadUntilWordS(source,NULL,"<%end");
00842     ReadUntilChar(source,0,0,">",0); /* finish the tag */
00843     STOP(9)
00844     continue;
00845    }
00846 
00847   if (!strnicmp(tag,"%ezs",4))
00848    {
00849       START(10)
00850     ExecEZSCommand(argv0,htmlout,source,cgidata,dbdata,unused,preset);
00851     STOP(10)
00852     continue;
00853    }
00854 
00855   if (!stricmp(tag,"%include"))
00856    {
00857       START(11)
00858     EZSInclude(argv0,htmlout,source,cgidata,dbdata);
00859       STOP(11)
00860     continue;
00861    }
00862 
00863   if (!stricmp(tag,"%source"))
00864    {START(12)
00865     EZSSource(argv0,htmlout,source);
00866   STOP(12)
00867     continue;
00868    }
00869 
00870   if (!stricmp(tag,"%report"))
00871    {
00872       START(13)
00873     EZSReport(argv0,htmlout,source,cgidata);
00874       STOP(13)
00875     continue;
00876    }
00877 
00878   if (!strnicmp(tag,"%random",7))
00879    {
00880       START(14)
00881     EZSRandom(argv0,htmlout,source,cgidata,dbdata,unused,preset);
00882       STOP(14)
00883     continue;
00884    }
00885 
00886   if (!stricmp(tag,"%stats"))
00887    {
00888       START(15)
00889     EZSCounts(argv0,htmlout,source, cgidata);
00890       STOP(15)
00891     continue;
00892    }
00893   if (tag[0] == '%' && tag[1] == 0 && htmlout)
00894    {
00895     int ret;
00896     ScriptFunction Functions[] =
00897      {{"print",10,(ScriptFunctionCall)*JSPrint},
00898       {"write",10,(ScriptFunctionCall)*JSPrint},
00899       {"eval",1,(ScriptFunctionCall)*JSEval},
00900       {"timestamp",0,(ScriptFunctionCall)*JSTimeStamp},
00901       {"toFixed",2,(ScriptFunctionCall)*JStoFixed},
00902       {"escape",1,(ScriptFunctionCall)*JSHTMLEscape},
00903       {"subStr",3,(ScriptFunctionCall)*JSsubstr},
00904       {"indexOf",4,(ScriptFunctionCall)*JSIndexOf},
00905       {"length",1,(ScriptFunctionCall)*JSstrlen},
00906       {"hasRecord",1,(ScriptFunctionCall)*JSHasRecord},
00907       {"runReport",8,(ScriptFunctionCall)*JSReport},
00908       {"number",1,(ScriptFunctionCall)*JSToNumber},
00909       {"environment",1,(ScriptFunctionCall)*JSGetEnv},
00910       {"toNumber",1,(ScriptFunctionCall)*JSToNumber},
00911       {"random",1,(ScriptFunctionCall)*JSRandom},
00912       {0,0,0}};
00913     ScriptObject Objects[] =
00914      {{"data",0},
00915       {0,0}};
00916     ScriptEnvironment Env;
00917 
00918       START(16)
00919    memset(&Env,0,sizeof(Env));
00920     Env.Functions = Functions;
00921     Env.Objects   = Objects;
00922     Env.htmlout   = htmlout;
00923     Env.argv0     = argv0;
00924     Env.F2 = extra;
00925 
00926     Objects[0].data=dbdata;
00927 
00928     ret = RunScript(&Env,source,cgidata);
00929     if (ret > 0)
00930     {
00931      HTMLWriteL(htmlout,"***",3);
00932      PrintScriptError(htmlout,&Env, source, ret);
00933      HTMLWriteL(htmlout,"***",3);
00934     }
00935     STOP(16)
00936     continue;
00937    }
00938 
00939   if (stricmp(tag,"SCRIPT") == 0) inscript = 1;
00940   else if (stricmp(tag,"/SCRIPT")==0) inscript = 0;
00941 
00942   if (preset && inscript == 0 && !stricmp(tag,"/SELECT"))
00943   {
00944    currentname[0] = 0;
00945    goto disregard;
00946   }
00947 
00948   if (preset && inscript == 0)
00949    if (!stricmp(tag,"INPUT")
00950          || !stricmp(tag,"SELECT")
00951          || !stricmp(tag,"OPTION")
00952          || !stricmp(tag,"TEXTAREA")
00953          || !stricmp(tag,"/TEXTAREA"))
00954   {
00955        CGINameValue * attributes = 0;
00956        char *type, *name, *value, *fv;
00957        START(17)
00958        if (e != '>') attributes = ReadHTMLAttributes(source,32);
00959 
00960        type = GetFieldValue(attributes,"TYPE");
00961        name  = GetFieldValue(attributes,"NAME");
00962        value = GetFieldValue(attributes,"VALUE");
00963        fv = *name ? GetFieldValue(cgidata,name) : NULLSTR;
00964        STOP(17)
00965 
00966        if (stricmp(tag,"INPUT")==0) /* password, radio, checkbox, submit, reset, or text */
00967         {
00968            START(18)
00969          if (unused && *name) while (RenameField(unused,name,0));
00970 
00971          HTMLWrite(htmlout,"<INPUT");
00972          if (*name)
00973          {
00974           if (stricmp(type,"RADIO")==0 || stricmp(type,"CHECKBOX")==0)
00975           {
00976            if (*fv)
00977             {
00978              if (*value && HasToken(fv,value,0))
00979                 HTMLWrite(htmlout," CHECKED");
00980              else
00981                 RenameField(attributes,"CHECKED",0);
00982             }
00983           }
00984           else /* submit, password, or reset */
00985           {
00986            HTMLWrite(htmlout," VALUE=\"");
00987            if (*fv)
00988             HTMLEscape(htmlout,fv);
00989            else
00990             HTMLWrite(htmlout,value);
00991 
00992            HTMLWrite(htmlout,"\"");
00993            RenameField(attributes,"VALUE",NULL);
00994           }
00995          }
00996          WriteHTMLAttributes(htmlout,attributes);
00997          HTMLWrite(htmlout,">");
00998          STOP(18)
00999         }
01000        else if (stricmp(tag,"SELECT")==0)
01001         {
01002         START(19)
01003          if (unused && *name) RenameField(unused,name,0);
01004 
01005          strncpy(currentname,name,sizeof(currentname));
01006          currentname[sizeof(currentname)-1]=0;
01007          HTMLWrite(htmlout,"<SELECT");
01008          WriteHTMLAttributes(htmlout,attributes);
01009          HTMLWrite(htmlout,">");
01010          STOP(19)
01011         }
01012        else if (stricmp(tag,"OPTION")==0)
01013         {
01014          char* query = GetFieldValue(attributes,"query");
01015            START(20)
01016          if (*query && (EvaluateLogic(query,cgidata,0) != 1))
01017          {
01018           ReadUntilWord(source,0,0,"</OPTION>");
01019          }
01020          else if (*currentname)
01021          {
01022           HTMLWrite(htmlout,"<OPTION");
01023           switch (IsValueSelected(cgidata,currentname,value))
01024           {
01025            case 1: HTMLWrite(htmlout," SELECTED "); break;
01026            case 0: RenameField(attributes,"SELECTED",0);
01027           }
01028           WriteHTMLAttributes(htmlout,attributes);
01029           HTMLWrite(htmlout,">");
01030          }
01031          STOP(20)
01032         }
01033        else if (*name && stricmp(tag,"TEXTAREA")==0)
01034         {
01035         START(21)
01036          if (unused && *name) RenameField(unused,name,0);
01037 
01038          HTMLWrite(htmlout,"<TEXTAREA");
01039          WriteHTMLAttributes(htmlout,attributes);
01040          HTMLWrite(htmlout,">");
01041 
01042          if (*fv)
01043            HTMLEscape(htmlout,fv);
01044          else
01045            HTMLEscape(htmlout,value);
01046 
01047          HTMLWrite(htmlout,"</TEXTAREA>");
01048          ReadUntilWordS(source,0,"</TEXTAREA>");
01049          STOP(21)
01050         }
01051         /* /TEXTAREA does nothing */
01052 
01053        if (attributes) DeleteNVP(attributes);
01054        continue; /* we processed the tag */
01055     }
01056 
01057   /* disregard the tag */
01058   START(22)
01059 disregard:
01060   HTMLWrite(htmlout,"<");
01061   HTMLWrite(htmlout,tag);
01062   STOP(22)
01063   if (e == EOF) break;
01064   START(23)
01065   c[0]=(char)e;
01066   HTMLWrite(htmlout,c);
01067   STOP(23)
01068  }
01069  PROFILEEND
01070  return EOF;
01071 }
01072 
01073 
01074 


Raosoft, Inc.
Raosoft EZReport, EZSurvey, InterForm, RapidReport, Raosoft, and SurveyWin are registered trademarks of Raosoft, Inc. Page contents © 1996-2007 by Raosoft, Inc. You may use and modify this file for your own use, but may not distribute it or derivative works without the prior written consent of Raosoft, Inc. This software is provided "as is," and Raosoft makes no warranty, express or implied, of fitness for a particular application. Every measure has been taken to anticipate risks inherent to computer networks, but we cannot guarantee safety or reliability of this program in every situation.
Tel: 206-525-4025 (US) Email: raosoft@raosoft.com
http://www.raosoft.com/