cgi_js.c File Reference

#include "cgi.h"

Include dependency graph for cgi_js.c:

Go to the source code of this file.

Functions

int RunScriptFunction (ScriptEnvironment *Env, FILE *script, int end, char *name, CGINameValue *Variables, char **outstr)
int SkipWhitespace (FILE *source, int end)
int SpecialWord (FILE *source, char *buf, int size, int end)
int ReadNextToken (FILE *source, char *buf, int size, int end, int *isSyntax)
char * JSReport (ScriptEnvironment *Env, char *cmd, int argc, char **argv, CGINameValue *Params)
char * JSGetEnv (ScriptEnvironment *Env, char *cmd, int argc, char **argv, CGINameValue *Params)
char * JSToNumber (ScriptEnvironment *Env, char *cmd, int argc, char **argv, CGINameValue *Params)
char * JSHTMLEscape (ScriptEnvironment *Env, char *cmd, int argc, char **argv, CGINameValue *Params)
char * JSTimeStamp (ScriptEnvironment *Env, char *cmd, int argc, char **argv, CGINameValue *Params)
char * JSHasRecord (ScriptEnvironment *Env, char *cmd, int argc, char **argv, CGINameValue *Params)
char * JSRandom (ScriptEnvironment *Env, char *cmd, int argc, char **argv, CGINameValue *Params)
char * JSEval (ScriptEnvironment *Env, char *cmd, int argc, char **argv, CGINameValue *Params)
char * JStoFixed (ScriptEnvironment *Env, char *cmd, int argc, char **argv, CGINameValue *Params)
char * JSstrlen (ScriptEnvironment *Env, char *cmd, int argc, char **argv, CGINameValue *Params)
char * JSsubstr (ScriptEnvironment *Env, char *cmd, int argc, char **argv, CGINameValue *Params)
char * JSIndexOf (ScriptEnvironment *Env, char *cmd, int argc, char **argv, CGINameValue *Params)
char * JSPrint (ScriptEnvironment *Env, char *cmd, int argc, char **argv, CGINameValue *Params)
int RunScriptObject (ScriptEnvironment *Env, FILE *script, int end, char *name, CGINameValue *Variables, char **outstr)
int EvaluateArgument (ScriptEnvironment *Env, FILE *script, int *e, CGINameValue *Variables, char *endchars, char **outstr, int asNumber)
int DelimitedSection (FILE *script, char *out, size_t length, int end, char *open, char *close, int errbase)
int RunScript (ScriptEnvironment *Env, FILE *script, CGINameValue *Variables)

Variables

static unsigned int random_seed


Function Documentation

int DelimitedSection FILE *  script,
char *  out,
size_t  length,
int  end,
char *  open,
char *  close,
int  errbase
 

Definition at line 689 of file cgi_js.c.

References ReadNextToken().

Referenced by RunScript().

00690 {
00691    char word[8];
00692    char c;
00693    int count = 1;
00694    size_t i=0;
00695 
00696    word[0] = '\n';
00697    while (word[0] == '\n')
00698      end = ReadNextToken(script,word,sizeof(word),end,0);
00699 
00700    if (!strchr(open,word[0])) return errbase;
00701 
00702    while (count && fread(&c,1,1,script))
00703    {
00704     if (strchr(open,c)) count++;
00705     else if (strchr(close,c)) count--;
00706     if (count && out && i< length)
00707      out[i++]=c;
00708    }
00709 
00710    if (out && i<length) out[i]=0;
00711    if (!strchr(close,c)) return errbase+1; /* unterminated end of line */
00712    return 0;
00713 }

int EvaluateArgument ScriptEnvironment Env,
FILE *  script,
int *  e,
CGINameValue Variables,
char *  endchars,
char **  outstr,
int  asNumber
 

Arithmetic or string concatenation? Decide based on the first object. If the first symbol is a variable name that is null, then do string concatenation. Otherwise, if the first symbol evaluates to a number, do arithmetic. Construct a text buffer before evaluating algebra. Functions and objects are evaluated as necessary.

Definition at line 520 of file cgi_js.c.

References BufferWrite(), CGIFREE, CopyBuffer(), DeleteBuffer(), EvaluateExpression(), GetFieldValue, IsNumber(), NewBuffer(), ReadNextToken(), RunScriptFunction(), and RunScriptObject().

Referenced by RunScript(), and RunScriptFunction().

00527 {
00528      char word[4096];
00529      int mode = asNumber; /*0=unknown, 1=number, 2=text */
00530      int ret = 0;
00531      char* value;
00532      int parity = 0; /* 1=syntax expected */
00533      int end = *e;
00534      TextBuffer* m = NewBuffer(1024);
00535 
00536      while(!ret && end != -1 && !strchr(endchars,end))
00537      {
00538        int isSyntax = 0;
00539        value = 0;
00540        end = ReadNextToken(script,word,sizeof(word),end,&isSyntax);
00541 
00542        if (word[0] == 0)
00543          continue;
00544 
00545        if (strchr(endchars,word[0]))
00546        { end = word[0]; break; }
00547 
00548        if (isSyntax) /*adjacent symbols are ok, but not adjacent expressions */
00549        {
00550          if (mode != 2)
00551            BufferWrite(m,word);
00552          parity = 0;
00553          continue;
00554        }
00555 
00556        if (mode && parity)
00557         ret = 47;
00558 
00559        if (!strcmp(word,"("))
00560        {
00561          ret = EvaluateArgument(Env,script,&end,Variables,")",&value,1);
00562        }
00563        else if (word[0] == '\'')
00564        {
00565          BufferWrite(m,word+1);
00566          if (mode == 0) mode = 2; /* a string */
00567        }
00568        else if (end == '(') /* function call */
00569        {
00570          ret = RunScriptFunction(Env,script,' ',word,Variables,&value);
00571          end = ' ';
00572        }
00573        else if (end == '[') /* object variable */
00574        {
00575          ret = RunScriptObject(Env,script,' ',word,Variables,&value);
00576          end = ' ';
00577        }
00578        else if (IsNumber(word,0))
00579        {
00580          BufferWrite(m,word);
00581          if (mode == 0) mode = 1;
00582        }
00583        else
00584        {
00585          char* x = GetFieldValue(Variables,word);
00586          BufferWrite(m,x);
00587          if (mode == 0)
00588           mode = (x[0] && IsNumber(x,0)) ? 1 : 2;
00589        }
00590 
00591        parity = 1;
00592        if (ret) break;
00593 
00594        if (value)
00595        {
00596         if (mode == 0)
00597           mode = (value[0] && IsNumber(value,0)) ? 1 : 2;
00598 
00599         BufferWrite(m,value);
00600         CGIFREE(value);
00601        }
00602        else if (mode == 0) mode = 2;
00603        /* function returned null, shorthand for empty string */
00604      } /* while */
00605 
00606    if (ret == 0 && mode==0) /* empty line, no symbols */
00607      ret = 49;
00608 
00609    if (!ret)
00610    {
00611      value = CopyBuffer(m);
00612 
00613      if (mode == 1)
00614      {
00615        *outstr = EvaluateExpression(Variables,0,value);
00616        CGIFREE(value);
00617      }
00618      else
00619        *outstr = value;
00620    }
00621 
00622    DeleteBuffer(m);
00623    *e = end;
00624    return ret;
00625 }

char* JSEval ScriptEnvironment Env,
char *  cmd,
int  argc,
char **  argv,
CGINameValue Params
 

Definition at line 338 of file cgi_js.c.

References EvaluateExpression().

Referenced by AdminRunScript(), CGImain(), ODBCRunScript(), RunReportF(), ValidateBack(), ValidatePage(), and WFmain().

00339 {
00340  if (!argc) return 0;
00341  return EvaluateExpression(Params,0,argv[0]);
00342 }

char* JSGetEnv ScriptEnvironment Env,
char *  cmd,
int  argc,
char **  argv,
CGINameValue Params
 

Definition at line 215 of file cgi_js.c.

References GetEnvironment(), ScriptEnvironment::htmlout, and strdup().

Referenced by CGImain(), ODBCRunScript(), RunReportF(), ValidateBack(), and ValidatePage().

00216 {
00217 #ifndef VPWSCGI
00218   char* c;
00219  if (argc == 0) return 0;
00220  c = GetEnvironment(Env->htmlout,argv[0]);
00221  if (c) return strdup(c);
00222 #endif
00223  return 0;
00224 }

char* JSHasRecord ScriptEnvironment Env,
char *  cmd,
int  argc,
char **  argv,
CGINameValue Params
 

Definition at line 290 of file cgi_js.c.

References ScriptEnvironment::argv0, DatabaseClose(), DatabaseFindNextRecord(), DatabaseOpen(), ExpandLocalPath(), MAXPATH, and strdup().

Referenced by CGImain(), RunReportF(), and WFmain().

00291 {
00292  FILE* database;
00293  CGINameValue* header;
00294  char dbfile[MAXPATH];
00295 
00296  int r = 0;
00297 
00298  if (argc < 2) return 0;
00299 
00300  if (strlen(argv[0]) > MAXPATH) return 0;
00301 
00302  dbfile[0] =0;
00303 
00304  if (Env->argv0)
00305   if (strlen(argv[0]) + strlen(Env->argv0) < MAXPATH )
00306     ExpandLocalPath(Env->argv0,dbfile,argv[0],0);
00307 
00308  if (DatabaseOpen(*dbfile?dbfile:argv[0],&database,&header,0,0))
00309  {
00310   r = DatabaseFindNextRecord(database,header,Params,argv[1]);
00311   DatabaseClose(database,header);
00312  }
00313  return strdup(r?"1":"0");
00314 }

char* JSHTMLEscape ScriptEnvironment Env,
char *  cmd,
int  argc,
char **  argv,
CGINameValue Params
 

in: jack & jill have < 2 broken heads out: jack & jill have < 2 broken heads

Definition at line 236 of file cgi_js.c.

References CGIMALLOC, NULL, and UTF8ToUCS2C().

Referenced by CGImain(), RunReportF(), and WFmain().

00237 {
00238    int c=0;
00239    size_t i,j,k=1;
00240    char *in;
00241     char *out;
00242    j=0;
00243 
00244    if (argc != 1) return NULL;
00245 
00246    in = argv[0];
00247    for (i=0; in[i]; i++)
00248    {
00249     j++; //2 chars, 11 bits. max 4095 -> 4 + 3 = 7
00250          //3 chars, 16 bits. max 65535 -> 5 + 3 = 8
00251          //4 chars, 23 bits. max 16777215 -> 8 + 3 = 11
00252     if (in[i] & 0x80) j+= 3;
00253     else if (strchr("<>&\"",in[i])) j += 5;
00254    }
00255    out = CGIMALLOC(strlen(in) + j);
00256 
00257    j = 0;
00258    for (i=0; in[i]; )
00259    {
00260     k = UTF8ToUCS2C(in+i,&c);
00261     if (k > 1)
00262     {
00263      j += sprintf(out+j,"&#%d;",c);
00264      i += k;
00265     }
00266     else
00267     {
00268      switch(in[i])
00269      {
00270       case '<': memcpy(out+j,"&lt;",4); j+=4; break;
00271       case '>': memcpy(out+j,"&gt;",4); j+=4; break;
00272       case '&': memcpy(out+j,"&amp;",5); j+=5; break;
00273       case '\"': memcpy(out+j,"&quot;",6); j+=6; break;
00274       default:
00275        out[j++] = in[i];
00276      }
00277      i++;
00278    }
00279   }
00280  return out;
00281 }

char* JSIndexOf ScriptEnvironment Env,
char *  cmd,
int  argc,
char **  argv,
CGINameValue Params
 

indexOf(string,substring,startpos,reverse)

Definition at line 399 of file cgi_js.c.

References strdup(), and stristr().

Referenced by AdminRunScript(), CGImain(), ODBCRunScript(), RunReportF(), ValidateBack(), ValidatePage(), and WFmain().

00400 {
00401  char* str;
00402  char* sub;
00403  int start=0;
00404  int dir=0;
00405  int length;
00406  char*c=0;
00407  char temp[32];
00408  if (argc < 2) return strdup("-1");
00409  str = argv[0];
00410  sub = argv[1];
00411  if (argc > 2) start = atoi(argv[2]);
00412  if (argc > 3) dir = atoi(argv[3]);
00413  length = strlen(str);
00414  if (start < 0) start = length - start;
00415  if (start < 0) start = 0;
00416  if (sub && start < length)
00417  {
00418   if (dir)
00419    {
00420     int i = length - strlen(sub);
00421     while (i > start && c == 0)
00422     {
00423      c = (char*)stristr(str + i,sub);
00424      i--;
00425     }
00426    }
00427   if (!c)
00428    c = (char*)stristr(str + start,sub);
00429  }
00430 
00431  if (!c) return strdup("-1");
00432  start = c - str;
00433  sprintf(temp,"%d",start);
00434  return strdup(temp);
00435 }

char* JSPrint ScriptEnvironment Env,
char *  cmd,
int  argc,
char **  argv,
CGINameValue Params
 

Definition at line 438 of file cgi_js.c.

References ScriptEnvironment::htmlout, HTMLWrite(), NULL, and stristr().

Referenced by AdminRunScript(), CGImain(), ODBCRunScript(), RunReportF(), ValidateBack(), ValidatePage(), and WFmain().

00439 {
00440  int i;
00441  for (i=0; i<argc; i++)
00442  {
00443   HTMLWrite(Env->htmlout,argv[i]);
00444  }
00445  if (stristr(cmd,"ln")) HTMLWrite(Env->htmlout,"\r\n");
00446  return NULL;
00447 }

char* JSRandom ScriptEnvironment Env,
char *  cmd,
int  argc,
char **  argv,
CGINameValue Params
 

Definition at line 319 of file cgi_js.c.

References CGIMALLOC, and random_seed.

Referenced by AdminRunScript(), CGImain(), ODBCRunScript(), RunReportF(), ValidateBack(), ValidatePage(), and WFmain().

00320 {
00321  unsigned int x;
00322  char* result = (char*)CGIMALLOC(32);
00323 #ifdef __WINCE__
00324  random_seed += GetTickCount();
00325 #else
00326  random_seed += time(0);
00327 #endif
00328  if (argc == 0) x = random_seed;
00329  else
00330   { x = atoi(argv[0]);
00331     if (!x ) x = random_seed;
00332     else x = random_seed % x;
00333   }
00334  sprintf(result,"%d",x);
00335  return result;
00336 }

char* JSReport ScriptEnvironment Env,
char *  cmd,
int  argc,
char **  argv,
CGINameValue Params
 

Definition at line 173 of file cgi_js.c.

References ScriptEnvironment::argv0, CGIFCLOSE, CGIFOPEN, ScriptEnvironment::config, CopyBuffer(), DeleteBuffer(), ExpandLocalPath(), MAXPATH, NewBuffer(), NULL, and RunReport().

Referenced by AdminRunScript(), CGImain(), ODBCRunScript(), RunReportF(), ValidateBack(), ValidatePage(), and WFmain().

00174 {
00175  EZSSTREAM result;
00176  int i;
00177  char*c = 0;
00178 
00179  if (!Env->config || !argc) return 0;
00180 
00181  memset(&result,0,sizeof(result));
00182  result.t = NewBuffer(1024);
00183 
00184 
00185  for (i=0; i< argc; i ++)
00186  {
00187   if (*argv[i])
00188     {
00189       char filename[MAXPATH];
00190       FILE* script = 0;
00191       if (Env->argv0)
00192        if (strlen(argv[i]) + strlen(Env->argv0) < MAXPATH )
00193         {
00194           ExpandLocalPath(Env->argv0,filename,argv[i],0);
00195           script = CGIFOPEN(filename,"rb");
00196         }
00197       if (!script) script = CGIFOPEN(argv[i],"rb");
00198 
00199       if (script)
00200       {
00201        RunReport(Env->argv0,&result,script,Params,NULL,NULL,0,NULL);
00202        CGIFCLOSE(script);
00203      }
00204     }
00205  }
00206  if (argc)
00207  {
00208   c = CopyBuffer(result.t);
00209  }
00210  DeleteBuffer(result.t);
00211  return c;
00212 }

char* JSstrlen ScriptEnvironment Env,
char *  cmd,
int  argc,
char **  argv,
CGINameValue Params
 

length(string)

Definition at line 363 of file cgi_js.c.

References strdup().

Referenced by AdminRunScript(), CGImain(), ODBCRunScript(), RunReportF(), ValidateBack(), ValidatePage(), and WFmain().

00364 {
00365  char temp[32];
00366  sprintf(temp,"%d",argc ? strlen(argv[0]) : 0);
00367  return strdup(temp);
00368 }

char* JSsubstr ScriptEnvironment Env,
char *  cmd,
int  argc,
char **  argv,
CGINameValue Params
 

substr(string,start,length)

Definition at line 370 of file cgi_js.c.

References NULL, strdup(), and strndup().

Referenced by AdminRunScript(), CGImain(), ODBCRunScript(), RunReportF(), ValidateBack(), ValidatePage(), and WFmain().

00371 {
00372  char* str;
00373  int start=0;
00374  int delta=65535;
00375  int length;
00376  int end;
00377 
00378  if (!argc) return NULL;
00379  str = argv[0];
00380  if (argc > 1) start = atoi(argv[1]);
00381  if (argc > 2) delta= atoi(argv[2]);
00382 
00383  length = strlen(str);
00384  if (start < 0) start = length + start;
00385  if (start < 0) start = 0;
00386  end = start + delta;
00387 
00388  if (end == start || start >= length) return strdup("");
00389 
00390  if (end >= length) end = length;
00391 
00392  length = abs(start - end);
00393  if (end < start) start = end;
00394 
00395  return strndup(str + start, length);
00396 }

char* JSTimeStamp ScriptEnvironment Env,
char *  cmd,
int  argc,
char **  argv,
CGINameValue Params
 

Definition at line 283 of file cgi_js.c.

References GetTime(), and strdup().

00284 {
00285     char datetime[16];
00286     GetTime(datetime,datetime+8,0);
00287     return strdup(datetime);
00288 }

char* JStoFixed ScriptEnvironment Env,
char *  cmd,
int  argc,
char **  argv,
CGINameValue Params
 

Definition at line 344 of file cgi_js.c.

References NULL, and strdup().

Referenced by AdminRunScript(), CGImain(), ODBCRunScript(), RunReportF(), ValidateBack(), ValidatePage(), and WFmain().

00345 {
00346    double d;
00347    int x;
00348     char format[32];
00349    char result[128];
00350 
00351    if (argc == 0) return 0;
00352    d = strtod(argv[0],NULL);
00353    if (argc == 2) x = atoi(argv[1]);
00354    else x = 0;
00355 
00356    sprintf(format,"%%.%df",x);
00357    sprintf(result,format,d);
00358 
00359     return strdup(result);
00360 }

char* JSToNumber ScriptEnvironment Env,
char *  cmd,
int  argc,
char **  argv,
CGINameValue Params
 

Definition at line 226 of file cgi_js.c.

References IsNumber(), and strdup().

Referenced by AdminRunScript(), CGImain(), ODBCRunScript(), RunReportF(), ValidateBack(), ValidatePage(), and WFmain().

00227 {
00228  if (argc == 0) return 0;
00229  if (!IsNumber(argv[0],0)) return 0;
00230  return strdup(argv[0]);
00231 }

int ReadNextToken FILE *  source,
char *  buf,
int  size,
int  end,
int *  isSyntax
 

read the next token from the file stream. Punctuation such as { } ; count as words. Strings may be ' or " delimited

Definition at line 116 of file cgi_js.c.

References ReadUntilChar(), SkipWhitespace(), and SpecialWord().

Referenced by DelimitedSection(), EvaluateArgument(), RunScript(), and RunScriptObject().

00117 {
00118  /* <= >= += -= *= /= == are awkward */
00119  buf[0]=0;
00120  if (end == -1) return -1; /* already hit the EOF */
00121  buf[0] = (char)SkipWhitespace(source,end);
00122 
00123  while (1)
00124   {
00125   if (strchr("\r\n#*-/^,;{}()[]<>+=",buf[0])) /* 1 or 2 character word */
00126    {
00127     if (isSyntax) *isSyntax = 1;
00128     if (buf[0] == '\r') buf[0] = '\n';
00129     if (buf[0] && strchr("<>+=/",buf[0])) /* check for 2-character operators */
00130      return SpecialWord(source,buf,size,buf[0]);
00131 
00132     /* syntax char -- don't need to check the next word */
00133     buf[1]=0;
00134     return ' '; /* pretend that whitespace stopped the scan */
00135    }
00136 
00137   if (buf[0] == '%') /* %> ends an inline script, and % is a modulus */
00138    {
00139      if (!fread(buf+1,1,1,source)) return -1;
00140      if (buf[1] == '>') {buf[0]=0; return -1;}
00141      else {end=buf[1]; buf[1]=0; return end;}
00142    }
00143 
00144   if (buf[0] == '\"' || buf[0] == '\'') /* read a string */
00145    {
00146     int i = 1;
00147     end = buf[0];
00148     buf[0] = '\''; /* mark as a string by setting the first character */
00149     while (1)
00150      {
00151       if (!fread(buf+i,1,1,source)) return -1;
00152       if (buf[i] == '\\')
00153        {
00154         if (!fread(buf+i,1,1,source)) return -1;
00155         if (buf[i] == 'n') buf[i] = '\n'; /* special replacement codes */
00156         else if (buf[i] == 'r') buf[i] = '\r';
00157         else if (buf[i] == 't') buf[i] = '\t';
00158        }
00159       else if (buf[i] == end) break;
00160       if (i < size-1) i++;
00161      }
00162     buf[i] = 0; /* terminate the string */
00163     return ' '; /* Done! Treat as whitespace termination */
00164    }
00165  else  /* ordinary word -- read until whitespace or grammar*/
00166   {    /* if whitespace stopped the scan, keep reading until the next char */
00167    end = ReadUntilChar(source,buf+1,size-1," \t\r\n#*-/,;{}()[]<>+=",0);
00168    return SkipWhitespace(source,end);
00169   }
00170  } /* while */
00171 }

int RunScript ScriptEnvironment Env,
FILE *  script,
CGINameValue Variables
 

return values are

  • 0: OK nonzero: error. see cgi.j for error codes

Definition at line 719 of file cgi_js.c.

References CGIFREE, CGIMALLOC, ScriptEnvironment::curpos, DelimitedSection(), ScriptEnvironment::endchar, EvaluateArgument(), EvaluateLogic(), ExtendNVP(), GetFieldValue, HasTokenI(), IsNumber(), ScriptEnvironment::lastLine, ReadNextToken(), ReadUntilChar(), ReadUntilWord(), RenameField(), RunScript(), RunScriptFunction(), SetFieldValue(), strdup(), strdup3(), and stricmp().

Referenced by AdminRunScript(), CGImain(), ODBCRunScript(), RunReportF(), RunScript(), ValidateBack(), ValidatePage(), and WFmain().

00720 {
00721  char* name;
00722  char word[4096];
00723  /* int end = 0; */
00724  int inif = 0;
00725  int CurrentTruth = 0;
00726  int ret = 0;
00727  int end = ' '; /* start with a clean slate */
00728  /* HTMLPrintf(htmlout,"<P>Q3=%s\n",GetFieldValue(Variables,"Q3"));  */
00729 
00730  name = 0;
00731  while (ret==0 && Env->endchar != -1)
00732  {
00733   Env->lastLine = ftell(script);
00734   end = ReadNextToken(script,word,sizeof(word),end,0);
00735   if (end <= 0) break;
00736 
00737   if (word[0] == '#') /* Perl comments */
00738    {
00739     end = ReadUntilChar(script,0,0,"\n",'\r');
00740     continue;
00741    }
00742   if (word[0] == 0) continue; /* oops, between words. ignore it. */
00743   if (word[0] == ';' || word[0] == '\n' || word[0] == ',') continue;
00744            /* comma or space can be delimiters, we don't care */
00745   if (word[0] == '\'') ret = 43; /* misplaced string constant */
00746   if (word[0] == '}') break;
00747   if (word[0] == '{')
00748    {
00749     ret = RunScript(Env,script,Variables);
00750     if (ret) break;
00751     end = Env->endchar;
00752     continue;
00753    }
00754 
00755   /* C++ comments */
00756   if (!strcmp(word,"//") && (end != '\n' && end != '\r'))
00757     {
00758      end = ReadUntilChar(script,0,0,"\r\n",0);
00759      continue;
00760     }
00761 
00762   if (word[0] == '/') /* C comments */
00763    if (word[1] == '*' && !strstr(word+2,"*/"))
00764     {
00765      ReadUntilWord(script,0,0,"*/");
00766      end = ' ';
00767      continue;
00768     }
00769 
00770   if (!stricmp(word,"var"))
00771     continue;
00772 
00773   if (HasTokenI("</script>,&exit,quit,exit,&end,&return,%>",word,0))
00774     {end = -1; break;}
00775 
00776   // delete variable_name
00777   if (!stricmp(word,"delete"))
00778   {
00779         end = ReadNextToken(script,word,sizeof(word),end,0);
00780         if (*word) RenameField(Variables,word,0);
00781       if (end < 0) break;
00782         continue;
00783     }
00784 
00785   if (!stricmp(word,"if")) /*evaluate a condition, and run the child code  */
00786     {/* end resets after DelimitedSection() */
00787 startif:
00788      ret = DelimitedSection(script,word,sizeof(word),end,"(",")",41);
00789      if (ret) break;
00790 
00791      CurrentTruth = EvaluateLogic(word,Variables,0);
00792      inif = 1;
00793 
00794      if (CurrentTruth)
00795      {
00796       word[0] = '\n';
00797       while (word[0] == '\n')
00798         end = ReadNextToken(script,word,sizeof(word),0,0);
00799       if (word[0] != '{') {ret = 70; break;}
00800       ret = RunScript(Env,script,Variables);
00801       if (ret) break;
00802       end = Env->endchar;
00803      }
00804      else
00805      {
00806       ret = DelimitedSection(script,0,0,0,"{","}",70);
00807       if (ret) break;
00808       end = ' ';
00809      }
00810      continue;
00811     }
00812 
00813   if (!stricmp(word,"else")) /* finish an if condition */
00814     {
00815      if (!inif) {ret = 44; break; }
00816 
00817      if (end != '{')
00818      {
00819       end = ReadNextToken(script,word,sizeof(word),end,0);
00820       if (!stricmp(word,"if")) /* else if {foo()} , next token = if*/
00821        goto elsif;
00822       else if (end < 0)
00823        break;
00824       else /* else {foo()}, next token = { */
00825        end=word[0];
00826      }
00827 
00828      inif = 0;
00829      if (!CurrentTruth)
00830      {
00831       if (end != '{')
00832       {
00833        ret = 70;
00834        break;
00835       }
00836       ret = RunScript(Env,script,Variables);
00837       if (ret) break;
00838       end = Env->endchar;
00839      }
00840      else
00841      {
00842 skip:
00843       ret = DelimitedSection(script,0,0,end,"{","}",70);
00844       if (ret) break;
00845       end = ' ';
00846      }
00847      continue;
00848     }
00849 
00850   if (!stricmp(word,"elif") || !stricmp(word,"elsif")) /*evaluate a condition, and run the child code*/
00851    {
00852 elsif:
00853     if (!inif)  {ret = 44; break; }
00854     if (CurrentTruth)
00855      goto skip;    /* previous was false, so OK to evaluate this section */
00856     else
00857      goto startif; /* evaluate this section */
00858    }
00859 
00860   /* out of an if section */
00861   inif = 0;
00862 
00863   /* variable assignment or function call*/
00864   CGIFREE(name);
00865   name = strdup(word);
00866 
00867   end = ReadNextToken(script,word,sizeof(word),end,0);
00868 
00869   /* email = email + "@" "caltech.edu" */
00870   /* total = Q1 + Q2 + Q3 + (Q4 * 4) */
00871   if (!strcmp(word,"=") || !stricmp(word,"+=")) /* variable assignment  */
00872    {
00873     char* value = 0;
00874     ret = EvaluateArgument(Env,script,&end,Variables,"\r\n;}",&value,0);
00875     if (ret) break;
00876 
00877     if (word[0] == '+')
00878     {
00879      char * c = GetFieldValue(Variables,name);
00880      if (IsNumber(c,0) && IsNumber(value,0))
00881      {
00882       char* x=0;
00883       double a = strtod(c,&x);
00884       if (*x) goto concat;
00885       a += strtod(value,&x);
00886       if (*x) goto concat;
00887       CGIFREE(value);
00888       value = CGIMALLOC(64);
00889       sprintf(value,"%f",a);
00890      }
00891      else
00892      {
00893      concat:
00894       c = strdup3(c,value,"");
00895       CGIFREE(value);
00896       value = c;
00897      }
00898     }
00899 
00900     if (!SetFieldValue(Variables,name,value))
00901     {
00902      ExtendNVP(Variables,128);
00903      SetFieldValue(Variables,name,value);
00904      /* add to the list of 'shadow variables' that won't get saved */
00905     }
00906     CGIFREE(value);
00907    }
00908   else if (!strcmp(word,"++")) /* numeric increment  */
00909    {
00910     char * c = GetFieldValue(Variables,name);
00911     char value[128];
00912     sprintf(value,"%f",strtod(c,0) + 1.0);
00913     SetFieldValue(Variables,name,value);
00914    }
00915   else if (!strcmp(word,"(")) /* command, no lvalue */
00916    {
00917     ret = RunScriptFunction(Env,script,end,name,Variables,0);
00918     end = ' '; /* eat the paren */
00919    }
00920   else
00921    {
00922     ret = 46; /* unknown command */
00923     break;
00924    }
00925  } /* while */
00926 
00927  CGIFREE(name);
00928  Env->endchar = end;
00929  Env->curpos = ftell(script);
00930  if (ret == -1) ret = 0; /* -1 is OK */
00931  return ret;
00932 } /* RunScript */

int RunScriptFunction ScriptEnvironment Env,
FILE *  script,
int  end,
char *  name,
CGINameValue Variables,
char **  outstr
 

Definition at line 627 of file cgi_js.c.

References CGIMALLOC, EvaluateArgument(), ScriptEnvironment::F2, ScriptEnvironment::Functions, ScriptFunction::name, NULL, ScriptFunction::parameters, ScriptFunction::run, and stricmp().

Referenced by EvaluateArgument(), and RunScript().

00633 {
00634 /*   char word[4096];*/
00635     ScriptFunction * fn = NULL;
00636     char ** argv = {NULL};
00637     int argc = 0;
00638    int ret = 0;
00639 
00640     int i;
00641 
00642     if (!Env->Functions && !Env->F2) return 48;
00643 
00644     if (name[0] == '&') name ++; /* looks like perl */
00645 
00646     if (Env->Functions)
00647    for (i=0; fn == NULL && Env->Functions[i].run != NULL; i++)
00648       if (!stricmp(Env->Functions[i].name,name))
00649              fn = Env->Functions + i;
00650 
00651     if (Env->F2)
00652    for (i=0; fn == NULL && Env->F2[i].run != NULL; i++)
00653        if (!stricmp(Env->F2[i].name,name))
00654              fn = Env->F2 + i;
00655 
00656     if (fn == NULL) return 48;
00657 
00658    if (end != ';' && end != '\n')
00659    {
00660     argv = (char**) CGIMALLOC (sizeof(char*) * (1+fn->parameters));
00661     memset(argv,0,sizeof(char*) * (1+fn->parameters));
00662 
00663     while(!ret && end != ')')
00664      {
00665         char* value;
00666         ret = EvaluateArgument(Env, script, &end, Variables, ",)}",&value,0);
00667         if (!ret && value != NULL && argc < fn->parameters)
00668           argv[argc++] = value;
00669         if (end == ',') end = ' ';
00670     }
00671    }
00672 
00673     if (!ret)
00674     {
00675      char* result  = fn->run(Env,name,argc,argv,Variables);
00676      if (outstr != NULL) *outstr = result;
00677      else if (result != NULL) CGIFREE(result);
00678     }
00679 
00680     for (i=0; i< argc; i++)
00681     {
00682          CGIFREE(argv[i]);
00683      }
00684      if (argv) CGIFREE(argv);
00685     return ret;
00686 }

int RunScriptObject ScriptEnvironment Env,
FILE *  script,
int  end,
char *  name,
CGINameValue Variables,
char **  outstr
 

Definition at line 450 of file cgi_js.c.

References ScriptObject::data, GetFieldValue, ScriptObject::name, NULL, ScriptEnvironment::Objects, ReadNextToken(), ReadUntilChar(), strdup(), and stricmp().

Referenced by EvaluateArgument().

00456 {
00457    char word[4096];
00458    ScriptObject * ob = NULL;
00459     char* value;
00460     int i;
00461 
00462    if (outstr) *outstr = NULL;
00463 
00464     if (!Env->Objects) return 45;
00465 
00466    for (i=0; Env->Objects[i].name != NULL; i++)
00467     {
00468       if (!stricmp(Env->Objects[i].name,name))
00469          {
00470              ob = Env->Objects + i;
00471              break;
00472          }
00473     }
00474 
00475     if (ob == NULL) return 45;
00476    end = ReadNextToken(script,word,sizeof(word),end,0);
00477    if (word[0] == '[')
00478      end = ReadNextToken(script,word,sizeof(word),end,0);
00479 
00480    if (word[0] == 0) return 45;
00481    if (word[0] == '\'' )
00482    {
00483       if (end == -1 || strchr(word,'\n')) return 40;
00484       value = word + 1;
00485    }
00486    else
00487    {
00488       value = GetFieldValue(Variables,word);
00489    }
00490 
00491    if (end != ']') ReadUntilChar(script,0,0,"]",0);
00492 
00493    if (outstr) *outstr = strdup(GetFieldValue(ob->data,value));
00494    return 0;
00495 }

int SkipWhitespace FILE *  source,
int  end
 

and
are meaningful tokens, so don't skip them. End should generally not be zero

Definition at line 43 of file cgi_js.c.

Referenced by ReadNextToken(), and SpecialWord().

00044 {
00045    char temp;
00046    while (end != -1 && (end == ' ' || end == '\t' || end == 0))
00047    {
00048     if (!fread(&temp,1,1,source)) return -1;
00049     end = temp;
00050    }
00051    return end;
00052 }

int SpecialWord FILE *  source,
char *  buf,
int  size,
int  end
 

Definition at line 54 of file cgi_js.c.

References ReadUntilChar(), and SkipWhitespace().

Referenced by ReadNextToken().

00055 {
00056   if (!fread(buf,1,1,source)) return -1;
00057   if (end == '<' && buf[0] == '/') /* xml end tag */
00058   {
00059    buf[0]='<';
00060    buf[1]='/';
00061    end = ReadUntilChar(source,buf+2,size-2,">",0);
00062    if (end == '>') strcat(buf,">");
00063    return ' ';
00064   }
00065   if (end == '/' && (buf[0] == '/' || buf[0] == '*'))
00066   {
00067    buf[1]=buf[0];
00068    buf[0]=(char) end;
00069    buf[2]=0;
00070    return ' ';
00071   }
00072   if (buf[0] == '=')
00073    {
00074     buf[0] = (char) end;
00075     buf[1] = '='; /* == */
00076 
00077     buf[2] = 0;
00078     if (end != '=')
00079      return ' '; /* += */
00080 
00081     if (!fread(buf+2,1,1,source)) return -1;
00082 
00083     if (buf[2] == '=') /* === */
00084     {
00085       buf[3]=0;
00086       return ' ';
00087     }
00088     else
00089     {
00090      end = buf[2];
00091      buf[2] = 0;
00092      return end;
00093     }
00094    }
00095   else if (buf[0] == '+' && end == '+') /* ++ */
00096    {
00097     buf[1] = buf[0];
00098     buf[2] = 0;
00099     return ' ';
00100    }
00101   else
00102    {
00103     buf[1] = buf[0];
00104     buf[0] = (char)end;
00105     end = buf[1];
00106     buf[1] = 0;
00107     return SkipWhitespace(source,end);
00108    }
00109 }


Variable Documentation

unsigned int random_seed [static]
 

Definition at line 317 of file cgi_js.c.

Referenced by JSRandom().



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/