Hex Artifact Content
Not logged in

Artifact 43919ec3bf2423aaf9969e8d7edf3d7145e3ba34:

File src/th_main.c part of check-in [fde1d82372] - Cut over all code to use TH1 instead of subscript. Completely remove the subscript interpreter from the source tree. by drh on 2008-02-13 22:31:59.

0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20  /*.** Copyright 
0010: 28 63 29 20 32 30 30 38 20 44 2e 20 52 69 63 68  (c) 2008 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54  ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66  his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f  ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75  u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20  te it and/or.** 
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20  modify it under 
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65  the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62   GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76  lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c  ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65  ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73  s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20  tributed in the 
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c  hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20  l be useful,.** 
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20  but WITHOUT ANY 
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75  WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69  t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a  ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54  * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52  Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55   A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20  RPOSE.  See the 
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50  GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f  ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a  r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c  ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20  d have received 
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e  a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63  U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e  .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72  g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69  ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65  te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20  ation, Inc., 59 
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53  Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73  uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31  ton, MA  02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20  307, USA..**.** 
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69  Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20  nformation:.**  
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a   drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68  *   http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a  waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0360: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
0370: 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 69 6e 74   contains an int
0380: 65 72 66 61 63 65 20 62 65 74 77 65 65 6e 20 74  erface between t
0390: 68 65 20 54 48 20 73 63 72 69 70 74 69 6e 67 20  he TH scripting 
03a0: 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 28 61 6e 20  language.** (an 
03b0: 69 6e 64 65 70 65 6e 64 65 6e 74 20 70 72 6f 6a  independent proj
03c0: 65 63 74 29 20 61 6e 64 20 66 6f 73 73 69 6c 2e  ect) and fossil.
03d0: 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 22 63 6f  .*/.#include "co
03e0: 6e 66 69 67 2e 68 22 0a 23 69 6e 63 6c 75 64 65  nfig.h".#include
03f0: 20 22 74 68 5f 6d 61 69 6e 2e 68 22 0a 0a 2f 2a   "th_main.h"../*
0400: 0a 2a 2a 20 47 6c 6f 62 61 6c 20 76 61 72 69 61  .** Global varia
0410: 62 6c 65 20 63 6f 75 6e 74 69 6e 67 20 74 68 65  ble counting the
0420: 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 73 74   number of outst
0430: 61 6e 64 69 6e 67 20 63 61 6c 6c 73 20 74 6f 20  anding calls to 
0440: 6d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6d 61 64 65  malloc().** made
0450: 20 62 79 20 74 68 65 20 74 68 31 20 69 6d 70 6c   by the th1 impl
0460: 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 54 68 69 73  ementation. This
0470: 20 69 73 20 75 73 65 64 20 74 6f 20 63 61 74 63   is used to catc
0480: 68 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 73 0a 2a  h memory leaks.*
0490: 2a 20 69 6e 20 74 68 65 20 69 6e 74 65 72 70 72  * in the interpr
04a0: 65 74 65 72 2e 20 4f 62 76 69 6f 75 73 6c 79 2c  eter. Obviously,
04b0: 20 69 74 20 61 6c 73 6f 20 6d 65 61 6e 73 20 74   it also means t
04c0: 68 31 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64  h1 is not thread
04d0: 73 61 66 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  safe..*/.static 
04e0: 69 6e 74 20 6e 4f 75 74 73 74 61 6e 64 69 6e 67  int nOutstanding
04f0: 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 0a 2f 2a 0a  Malloc = 0;../*.
0500: 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
0510: 6e 73 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 20 61  ns of malloc() a
0520: 6e 64 20 66 72 65 65 28 29 20 74 6f 20 70 61 73  nd free() to pas
0530: 73 20 74 6f 20 74 68 65 20 69 6e 74 65 72 70 72  s to the interpr
0540: 65 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eter..*/.static 
0550: 76 6f 69 64 20 2a 78 4d 61 6c 6c 6f 63 28 75 6e  void *xMalloc(un
0560: 73 69 67 6e 65 64 20 69 6e 74 20 6e 29 7b 0a 20  signed int n){. 
0570: 20 76 6f 69 64 20 2a 70 20 3d 20 6d 61 6c 6c 6f   void *p = mallo
0580: 63 28 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  c(n);.  if( p ){
0590: 0a 20 20 20 20 6e 4f 75 74 73 74 61 6e 64 69 6e  .    nOutstandin
05a0: 67 4d 61 6c 6c 6f 63 2b 2b 3b 0a 20 20 7d 0a 20  gMalloc++;.  }. 
05b0: 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 73 74 61   return p;.}.sta
05c0: 74 69 63 20 76 6f 69 64 20 78 46 72 65 65 28 76  tic void xFree(v
05d0: 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  oid *p){.  if( p
05e0: 20 29 7b 0a 20 20 20 20 6e 4f 75 74 73 74 61 6e   ){.    nOutstan
05f0: 64 69 6e 67 4d 61 6c 6c 6f 63 2d 2d 3b 0a 20 20  dingMalloc--;.  
0600: 7d 0a 20 20 66 72 65 65 28 70 29 3b 0a 7d 0a 73  }.  free(p);.}.s
0610: 74 61 74 69 63 20 54 68 5f 56 74 61 62 20 76 74  tatic Th_Vtab vt
0620: 61 62 20 3d 20 7b 20 78 4d 61 6c 6c 6f 63 2c 20  ab = { xMalloc, 
0630: 78 46 72 65 65 20 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a  xFree };.../*.**
0640: 20 54 72 75 65 20 69 66 20 6f 75 74 70 75 74 20   True if output 
0650: 69 73 20 65 6e 61 62 6c 65 64 2e 20 20 46 61 6c  is enabled.  Fal
0660: 73 65 20 69 66 20 64 69 73 61 62 6c 65 64 2e 0a  se if disabled..
0670: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 6e  */.static int en
0680: 61 62 6c 65 4f 75 74 70 75 74 20 3d 20 31 3b 0a  ableOutput = 1;.
0690: 0a 2f 2a 0a 2a 2a 20 54 48 20 63 6f 6d 6d 61 6e  ./*.** TH comman
06a0: 64 3a 20 20 20 20 20 65 6e 61 62 6c 65 5f 6f 75  d:     enable_ou
06b0: 74 70 75 74 20 42 4f 4f 4c 45 41 4e 0a 2a 2a 0a  tput BOOLEAN.**.
06c0: 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69 73  ** Enable or dis
06d0: 61 62 6c 65 20 74 68 65 20 70 75 74 73 20 61 6e  able the puts an
06e0: 64 20 68 70 75 74 73 20 63 6f 6d 6d 61 6e 64 73  d hputs commands
06f0: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
0700: 65 6e 61 62 6c 65 4f 75 74 70 75 74 43 6d 64 28  enableOutputCmd(
0710: 0a 20 20 54 68 5f 49 6e 74 65 72 70 20 2a 69 6e  .  Th_Interp *in
0720: 74 65 72 70 2c 20 0a 20 20 76 6f 69 64 20 2a 70  terp, .  void *p
0730: 2c 20 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 0a  , .  int argc, .
0740: 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
0750: 20 63 68 61 72 20 2a 2a 61 72 67 76 2c 20 0a 20   char **argv, . 
0760: 20 69 6e 74 20 2a 61 72 67 6c 0a 29 7b 0a 20 20   int *argl.){.  
0770: 69 66 28 20 61 72 67 63 21 3d 32 20 29 7b 0a 20  if( argc!=2 ){. 
0780: 20 20 20 72 65 74 75 72 6e 20 54 68 5f 57 72 6f     return Th_Wro
0790: 6e 67 4e 75 6d 41 72 67 73 28 69 6e 74 65 72 70  ngNumArgs(interp
07a0: 2c 20 22 65 6e 61 62 6c 65 5f 6f 75 74 70 75 74  , "enable_output
07b0: 20 42 4f 4f 4c 45 41 4e 22 29 3b 0a 20 20 7d 0a   BOOLEAN");.  }.
07c0: 20 20 72 65 74 75 72 6e 20 54 68 5f 54 6f 49 6e    return Th_ToIn
07d0: 74 28 69 6e 74 65 72 70 2c 20 61 72 67 76 5b 31  t(interp, argv[1
07e0: 5d 2c 20 61 72 67 6c 5b 31 5d 2c 20 26 65 6e 61  ], argl[1], &ena
07f0: 62 6c 65 4f 75 74 70 75 74 29 3b 0a 7d 0a 0a 2f  bleOutput);.}../
0800: 2a 0a 2a 2a 20 53 65 6e 64 20 74 65 78 74 20 74  *.** Send text t
0810: 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  o the appropriat
0820: 65 20 6f 75 74 70 75 74 3a 20 20 45 69 74 68 65  e output:  Eithe
0830: 72 20 74 6f 20 74 68 65 20 63 6f 6e 73 6f 6c 65  r to the console
0840: 0a 2a 2a 20 6f 72 20 74 6f 20 74 68 65 20 43 47  .** or to the CG
0850: 49 20 72 65 70 6c 79 20 62 75 66 66 65 72 2e 0a  I reply buffer..
0860: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
0870: 65 6e 64 54 65 78 74 28 63 6f 6e 73 74 20 63 68  endText(const ch
0880: 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e  ar *z, int n, in
0890: 74 20 65 6e 63 6f 64 65 29 7b 0a 20 20 69 66 28  t encode){.  if(
08a0: 20 65 6e 61 62 6c 65 4f 75 74 70 75 74 20 26 26   enableOutput &&
08b0: 20 6e 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 3c   n ){.    if( n<
08c0: 30 20 29 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a  0 ) n = strlen(z
08d0: 29 3b 0a 20 20 20 20 69 66 28 20 65 6e 63 6f 64  );.    if( encod
08e0: 65 20 29 7b 0a 20 20 20 20 20 20 7a 20 3d 20 68  e ){.      z = h
08f0: 74 6d 6c 69 7a 65 28 7a 2c 20 6e 29 3b 0a 20 20  tmlize(z, n);.  
0900: 20 20 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a      n = strlen(z
0910: 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
0920: 20 67 2e 63 67 69 50 61 6e 69 63 20 29 7b 0a 20   g.cgiPanic ){. 
0930: 20 20 20 20 20 63 67 69 5f 61 70 70 65 6e 64 5f       cgi_append_
0940: 63 6f 6e 74 65 6e 74 28 7a 2c 20 6e 29 3b 0a 20  content(z, n);. 
0950: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
0960: 66 77 72 69 74 65 28 7a 2c 20 31 2c 20 6e 2c 20  fwrite(z, 1, n, 
0970: 73 74 64 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20  stdout);.    }. 
0980: 20 20 20 69 66 28 20 65 6e 63 6f 64 65 20 29 20     if( encode ) 
0990: 66 72 65 65 28 28 63 68 61 72 2a 29 7a 29 3b 0a  free((char*)z);.
09a0: 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 48 20    }.}../*.** TH 
09b0: 63 6f 6d 6d 61 6e 64 3a 20 20 20 20 20 70 75 74  command:     put
09c0: 73 20 53 54 52 49 4e 47 0a 2a 2a 20 54 48 20 63  s STRING.** TH c
09d0: 6f 6d 6d 61 6e 64 3a 20 20 20 20 20 68 74 6d 6c  ommand:     html
09e0: 20 53 54 52 49 4e 47 0a 2a 2a 0a 2a 2a 20 4f 75   STRING.**.** Ou
09f0: 74 70 75 74 20 53 54 52 49 4e 47 20 61 73 20 48  tput STRING as H
0a00: 54 4d 4c 20 28 68 74 6d 6c 29 20 6f 72 20 75 6e  TML (html) or un
0a10: 63 68 61 6e 67 65 64 20 28 70 75 74 73 29 2e 20  changed (puts). 
0a20: 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20   .*/.static int 
0a30: 70 75 74 73 43 6d 64 28 0a 20 20 54 68 5f 49 6e  putsCmd(.  Th_In
0a40: 74 65 72 70 20 2a 69 6e 74 65 72 70 2c 20 0a 20  terp *interp, . 
0a50: 20 76 6f 69 64 20 2a 70 43 6f 6e 76 65 72 74 2c   void *pConvert,
0a60: 20 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20   .  int argc, . 
0a70: 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
0a80: 63 68 61 72 20 2a 2a 61 72 67 76 2c 20 0a 20 20  char **argv, .  
0a90: 69 6e 74 20 2a 61 72 67 6c 0a 29 7b 0a 20 20 69  int *argl.){.  i
0aa0: 66 28 20 61 72 67 63 21 3d 32 20 29 7b 0a 20 20  f( argc!=2 ){.  
0ab0: 20 20 72 65 74 75 72 6e 20 54 68 5f 57 72 6f 6e    return Th_Wron
0ac0: 67 4e 75 6d 41 72 67 73 28 69 6e 74 65 72 70 2c  gNumArgs(interp,
0ad0: 20 22 70 75 74 73 20 53 54 52 49 4e 47 22 29 3b   "puts STRING");
0ae0: 0a 20 20 7d 0a 20 20 73 65 6e 64 54 65 78 74 28  .  }.  sendText(
0af0: 28 63 68 61 72 2a 29 61 72 67 76 5b 31 5d 2c 20  (char*)argv[1], 
0b00: 61 72 67 6c 5b 31 5d 2c 20 70 43 6f 6e 76 65 72  argl[1], pConver
0b10: 74 21 3d 30 29 3b 0a 20 20 72 65 74 75 72 6e 20  t!=0);.  return 
0b20: 54 48 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TH_OK;.}../*.** 
0b30: 54 48 20 63 6f 6d 6d 61 6e 64 3a 20 20 20 20 20  TH command:     
0b40: 20 77 69 6b 69 20 53 54 52 49 4e 47 0a 2a 2a 0a   wiki STRING.**.
0b50: 2a 2a 20 52 65 6e 64 65 72 20 74 68 65 20 69 6e  ** Render the in
0b60: 70 75 74 20 73 74 72 69 6e 67 20 61 73 20 77 69  put string as wi
0b70: 6b 69 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ki..*/.static in
0b80: 74 20 77 69 6b 69 43 6d 64 28 0a 20 20 54 68 5f  t wikiCmd(.  Th_
0b90: 49 6e 74 65 72 70 20 2a 69 6e 74 65 72 70 2c 20  Interp *interp, 
0ba0: 0a 20 20 76 6f 69 64 20 2a 70 2c 20 0a 20 20 69  .  void *p, .  i
0bb0: 6e 74 20 61 72 67 63 2c 20 0a 20 20 63 6f 6e 73  nt argc, .  cons
0bc0: 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
0bd0: 2a 2a 61 72 67 76 2c 20 0a 20 20 69 6e 74 20 2a  **argv, .  int *
0be0: 61 72 67 6c 0a 29 7b 0a 20 20 69 66 28 20 61 72  argl.){.  if( ar
0bf0: 67 63 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74  gc!=2 ){.    ret
0c00: 75 72 6e 20 54 68 5f 57 72 6f 6e 67 4e 75 6d 41  urn Th_WrongNumA
0c10: 72 67 73 28 69 6e 74 65 72 70 2c 20 22 77 69 6b  rgs(interp, "wik
0c20: 69 20 53 54 52 49 4e 47 22 29 3b 0a 20 20 7d 0a  i STRING");.  }.
0c30: 20 20 69 66 28 20 65 6e 61 62 6c 65 4f 75 74 70    if( enableOutp
0c40: 75 74 20 29 7b 0a 20 20 20 20 42 6c 6f 62 20 73  ut ){.    Blob s
0c50: 72 63 3b 0a 20 20 20 20 62 6c 6f 62 5f 69 6e 69  rc;.    blob_ini
0c60: 74 28 26 73 72 63 2c 20 28 63 68 61 72 2a 29 61  t(&src, (char*)a
0c70: 72 67 76 5b 31 5d 2c 20 61 72 67 6c 5b 31 5d 29  rgv[1], argl[1])
0c80: 3b 0a 20 20 20 20 77 69 6b 69 5f 63 6f 6e 76 65  ;.    wiki_conve
0c90: 72 74 28 26 73 72 63 2c 20 30 2c 20 57 49 4b 49  rt(&src, 0, WIKI
0ca0: 5f 49 4e 4c 49 4e 45 29 3b 0a 20 20 20 20 62 6c  _INLINE);.    bl
0cb0: 6f 62 5f 72 65 73 65 74 28 26 73 72 63 29 3b 0a  ob_reset(&src);.
0cc0: 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 54 48 5f    }.  return TH_
0cd0: 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 48 20  OK;.}../*.** TH 
0ce0: 63 6f 6d 6d 61 6e 64 3a 20 20 20 20 20 68 61 73  command:     has
0cf0: 63 61 70 20 53 54 52 49 4e 47 0a 2a 2a 0a 2a 2a  cap STRING.**.**
0d00: 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
0d10: 74 68 65 20 75 73 65 72 20 68 61 73 20 61 6c 6c  the user has all
0d20: 20 6f 66 20 74 68 65 20 63 61 70 61 62 69 6c 69   of the capabili
0d30: 74 69 65 73 20 6c 69 73 74 65 64 20 69 6e 20 53  ties listed in S
0d40: 54 52 49 4e 47 2e 0a 2a 2f 0a 73 74 61 74 69 63  TRING..*/.static
0d50: 20 69 6e 74 20 68 61 73 63 61 70 43 6d 64 28 0a   int hascapCmd(.
0d60: 20 20 54 68 5f 49 6e 74 65 72 70 20 2a 69 6e 74    Th_Interp *int
0d70: 65 72 70 2c 20 0a 20 20 76 6f 69 64 20 2a 70 2c  erp, .  void *p,
0d80: 20 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20   .  int argc, . 
0d90: 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
0da0: 63 68 61 72 20 2a 2a 61 72 67 76 2c 20 0a 20 20  char **argv, .  
0db0: 69 6e 74 20 2a 61 72 67 6c 0a 29 7b 0a 20 20 69  int *argl.){.  i
0dc0: 66 28 20 61 72 67 63 21 3d 32 20 29 7b 0a 20 20  f( argc!=2 ){.  
0dd0: 20 20 72 65 74 75 72 6e 20 54 68 5f 57 72 6f 6e    return Th_Wron
0de0: 67 4e 75 6d 41 72 67 73 28 69 6e 74 65 72 70 2c  gNumArgs(interp,
0df0: 20 22 68 61 73 63 61 70 20 53 54 52 49 4e 47 22   "hascap STRING"
0e00: 29 3b 0a 20 20 7d 0a 20 20 54 68 5f 53 65 74 52  );.  }.  Th_SetR
0e10: 65 73 75 6c 74 49 6e 74 28 69 6e 74 65 72 70 2c  esultInt(interp,
0e20: 20 6c 6f 67 69 6e 5f 68 61 73 5f 63 61 70 61 62   login_has_capab
0e30: 69 6c 69 74 79 28 28 63 68 61 72 2a 29 61 72 67  ility((char*)arg
0e40: 76 5b 31 5d 2c 61 72 67 6c 5b 31 5d 29 29 3b 0a  v[1],argl[1]));.
0e50: 20 20 72 65 74 75 72 6e 20 54 48 5f 4f 4b 3b 0a    return TH_OK;.
0e60: 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  }../*.** Make su
0e70: 72 65 20 74 68 65 20 69 6e 74 65 72 70 72 65 74  re the interpret
0e80: 65 72 20 68 61 73 20 62 65 65 6e 20 69 6e 69 74  er has been init
0e90: 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 76 6f 69 64  ialized..*/.void
0ea0: 20 54 68 5f 46 6f 73 73 69 6c 49 6e 69 74 28 76   Th_FossilInit(v
0eb0: 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73  oid){.  static s
0ec0: 74 72 75 63 74 20 5f 43 6f 6d 6d 61 6e 64 20 7b  truct _Command {
0ed0: 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
0ee0: 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20 54 68 5f 43  *zName;.    Th_C
0ef0: 6f 6d 6d 61 6e 64 50 72 6f 63 20 78 50 72 6f 63  ommandProc xProc
0f00: 3b 0a 20 20 20 20 76 6f 69 64 20 2a 70 43 6f 6e  ;.    void *pCon
0f10: 74 65 78 74 3b 0a 20 20 7d 20 61 43 6f 6d 6d 61  text;.  } aComma
0f20: 6e 64 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 22 65  nd[] = {.    {"e
0f30: 6e 61 62 6c 65 5f 6f 75 74 70 75 74 22 2c 20 65  nable_output", e
0f40: 6e 61 62 6c 65 4f 75 74 70 75 74 43 6d 64 2c 20  nableOutputCmd, 
0f50: 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 7b 22 68       0},.    {"h
0f60: 61 73 63 61 70 22 2c 20 20 20 20 20 20 20 20 68  ascap",        h
0f70: 61 73 63 61 70 43 6d 64 2c 20 20 20 20 20 20 20  ascapCmd,       
0f80: 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 7b 22 68       0},.    {"h
0f90: 74 6d 6c 22 2c 20 20 20 20 20 20 20 20 20 20 70  tml",          p
0fa0: 75 74 73 43 6d 64 2c 20 20 20 20 20 20 20 20 20  utsCmd,         
0fb0: 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 7b 22 70       0},.    {"p
0fc0: 75 74 73 22 2c 20 20 20 20 20 20 20 20 20 20 70  uts",          p
0fd0: 75 74 73 43 6d 64 2c 20 20 20 20 20 20 20 28 76  utsCmd,       (v
0fe0: 6f 69 64 2a 29 31 7d 2c 0a 20 20 20 20 7b 22 77  oid*)1},.    {"w
0ff0: 69 6b 69 22 2c 20 20 20 20 20 20 20 20 20 20 77  iki",          w
1000: 69 6b 69 43 6d 64 2c 20 20 20 20 20 20 20 20 20  ikiCmd,         
1010: 20 20 20 20 20 30 7d 2c 0a 20 20 7d 3b 0a 20 20       0},.  };.  
1020: 69 66 28 20 67 2e 69 6e 74 65 72 70 3d 3d 30 20  if( g.interp==0 
1030: 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  ){.    int i;.  
1040: 20 20 67 2e 69 6e 74 65 72 70 20 3d 20 54 68 5f    g.interp = Th_
1050: 43 72 65 61 74 65 49 6e 74 65 72 70 28 26 76 74  CreateInterp(&vt
1060: 61 62 29 3b 0a 20 20 20 20 74 68 5f 72 65 67 69  ab);.    th_regi
1070: 73 74 65 72 5f 6c 61 6e 67 75 61 67 65 28 67 2e  ster_language(g.
1080: 69 6e 74 65 72 70 29 3b 20 20 20 20 20 20 20 2f  interp);       /
1090: 2a 20 42 61 73 69 63 20 73 63 72 69 70 74 69 6e  * Basic scriptin
10a0: 67 20 63 6f 6d 6d 61 6e 64 73 2e 20 2a 2f 0a 20  g commands. */. 
10b0: 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69     for(i=0; i<si
10c0: 7a 65 6f 66 28 61 43 6f 6d 6d 61 6e 64 29 2f 73  zeof(aCommand)/s
10d0: 69 7a 65 6f 66 28 61 43 6f 6d 6d 61 6e 64 5b 30  izeof(aCommand[0
10e0: 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ]); i++){.      
10f0: 54 68 5f 43 72 65 61 74 65 43 6f 6d 6d 61 6e 64  Th_CreateCommand
1100: 28 67 2e 69 6e 74 65 72 70 2c 20 61 43 6f 6d 6d  (g.interp, aComm
1110: 61 6e 64 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 61 43  and[i].zName, aC
1120: 6f 6d 6d 61 6e 64 5b 69 5d 2e 78 50 72 6f 63 2c  ommand[i].xProc,
1130: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1140: 20 20 20 20 20 20 20 20 61 43 6f 6d 6d 61 6e 64          aCommand
1150: 5b 69 5d 2e 70 43 6f 6e 74 65 78 74 2c 20 30 29  [i].pContext, 0)
1160: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
1170: 2a 0a 2a 2a 20 53 74 6f 72 65 20 61 20 73 74 72  *.** Store a str
1180: 69 6e 67 20 76 61 6c 75 65 20 69 6e 20 61 20 76  ing value in a v
1190: 61 72 69 61 62 6c 65 20 69 6e 20 74 68 65 20 69  ariable in the i
11a0: 6e 74 65 72 70 72 65 74 65 72 2e 0a 2a 2f 0a 76  nterpreter..*/.v
11b0: 6f 69 64 20 54 68 5f 53 74 6f 72 65 28 63 6f 6e  oid Th_Store(con
11c0: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
11d0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 61 6c  const char *zVal
11e0: 75 65 29 7b 0a 20 20 54 68 5f 46 6f 73 73 69 6c  ue){.  Th_Fossil
11f0: 49 6e 69 74 28 29 3b 0a 20 20 54 68 5f 53 65 74  Init();.  Th_Set
1200: 56 61 72 28 67 2e 69 6e 74 65 72 70 2c 20 28 75  Var(g.interp, (u
1210: 63 68 61 72 2a 29 7a 4e 61 6d 65 2c 20 2d 31 2c  char*)zName, -1,
1220: 20 28 75 63 68 61 72 2a 29 7a 56 61 6c 75 65 2c   (uchar*)zValue,
1230: 20 73 74 72 6c 65 6e 28 7a 56 61 6c 75 65 29 29   strlen(zValue))
1240: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 72 69  ;.}../*.** Retri
1250: 65 76 65 20 61 20 73 74 72 69 6e 67 20 76 61 6c  eve a string val
1260: 75 65 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65  ue from the inte
1270: 72 70 72 65 74 65 72 2e 20 20 49 66 20 6e 6f 20  rpreter.  If no 
1280: 73 75 63 68 0a 2a 2a 20 76 61 72 69 61 62 6c 65  such.** variable
1290: 20 65 78 69 73 74 73 2c 20 72 65 74 75 72 6e 20   exists, return 
12a0: 4e 55 4c 4c 2e 0a 2a 2f 0a 63 68 61 72 20 2a 54  NULL..*/.char *T
12b0: 68 5f 46 65 74 63 68 28 63 6f 6e 73 74 20 63 68  h_Fetch(const ch
12c0: 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 2a  ar *zName, int *
12d0: 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63  pSize){.  int rc
12e0: 3b 0a 20 20 54 68 5f 46 6f 73 73 69 6c 49 6e 69  ;.  Th_FossilIni
12f0: 74 28 29 3b 0a 20 20 72 63 20 3d 20 54 68 5f 47  t();.  rc = Th_G
1300: 65 74 56 61 72 28 67 2e 69 6e 74 65 72 70 2c 20  etVar(g.interp, 
1310: 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 69 66  zName, -1);.  if
1320: 28 20 72 63 3d 3d 54 48 5f 4f 4b 20 29 7b 0a 20  ( rc==TH_OK ){. 
1330: 20 20 20 72 65 74 75 72 6e 20 54 68 5f 47 65 74     return Th_Get
1340: 52 65 73 75 6c 74 28 67 2e 69 6e 74 65 72 70 2c  Result(g.interp,
1350: 20 70 53 69 7a 65 29 3b 0a 20 20 7d 65 6c 73 65   pSize);.  }else
1360: 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
1370: 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
1380: 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
1390: 73 74 72 69 6e 67 20 62 65 67 69 6e 73 20 77 69  string begins wi
13a0: 74 68 20 74 68 65 20 54 48 31 20 62 65 67 69 6e  th the TH1 begin
13b0: 2d 73 63 72 69 70 74 0a 2a 2a 20 74 61 67 3a 20  -script.** tag: 
13c0: 20 3c 74 68 31 3e 2e 0a 2a 2f 0a 73 74 61 74 69   <th1>..*/.stati
13d0: 63 20 69 6e 74 20 69 73 42 65 67 69 6e 53 63 72  c int isBeginScr
13e0: 69 70 74 54 61 67 28 63 6f 6e 73 74 20 63 68 61  iptTag(const cha
13f0: 72 20 2a 7a 29 7b 0a 20 20 72 65 74 75 72 6e 20  r *z){.  return 
1400: 7a 5b 30 5d 3d 3d 27 3c 27 0a 20 20 20 20 20 20  z[0]=='<'.      
1410: 26 26 20 28 7a 5b 31 5d 3d 3d 27 74 27 20 7c 7c  && (z[1]=='t' ||
1420: 20 7a 5b 31 5d 3d 3d 27 54 27 29 0a 20 20 20 20   z[1]=='T').    
1430: 20 20 26 26 20 28 7a 5b 32 5d 3d 3d 27 68 27 20    && (z[2]=='h' 
1440: 7c 7c 20 7a 5b 32 5d 3d 3d 27 48 27 29 0a 20 20  || z[2]=='H').  
1450: 20 20 20 20 26 26 20 7a 5b 33 5d 3d 3d 27 31 27      && z[3]=='1'
1460: 0a 20 20 20 20 20 20 26 26 20 7a 5b 34 5d 3d 3d  .      && z[4]==
1470: 27 3e 27 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  '>';.}../*.** Re
1480: 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
1490: 20 73 74 72 69 6e 67 20 62 65 67 69 6e 73 20 77   string begins w
14a0: 69 74 68 20 74 68 65 20 54 48 31 20 65 6e 64 2d  ith the TH1 end-
14b0: 73 63 72 69 70 74 0a 2a 2a 20 74 61 67 3a 20 20  script.** tag:  
14c0: 3c 2f 74 68 31 3e 2e 0a 2a 2f 0a 73 74 61 74 69  </th1>..*/.stati
14d0: 63 20 69 6e 74 20 69 73 45 6e 64 53 63 72 69 70  c int isEndScrip
14e0: 74 54 61 67 28 63 6f 6e 73 74 20 63 68 61 72 20  tTag(const char 
14f0: 2a 7a 29 7b 0a 20 20 72 65 74 75 72 6e 20 7a 5b  *z){.  return z[
1500: 30 5d 3d 3d 27 3c 27 0a 20 20 20 20 20 20 26 26  0]=='<'.      &&
1510: 20 7a 5b 31 5d 3d 3d 27 2f 27 0a 20 20 20 20 20   z[1]=='/'.     
1520: 20 26 26 20 28 7a 5b 32 5d 3d 3d 27 74 27 20 7c   && (z[2]=='t' |
1530: 7c 20 7a 5b 32 5d 3d 3d 27 54 27 29 0a 20 20 20  | z[2]=='T').   
1540: 20 20 20 26 26 20 28 7a 5b 33 5d 3d 3d 27 68 27     && (z[3]=='h'
1550: 20 7c 7c 20 7a 5b 33 5d 3d 3d 27 48 27 29 0a 20   || z[3]=='H'). 
1560: 20 20 20 20 20 26 26 20 7a 5b 34 5d 3d 3d 27 31       && z[4]=='1
1570: 27 0a 20 20 20 20 20 20 26 26 20 7a 5b 35 5d 3d  '.      && z[5]=
1580: 3d 27 3e 27 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ='>';.}../*.** I
1590: 66 20 73 74 72 69 6e 67 20 7a 5b 30 2e 2e 2e 5d  f string z[0...]
15a0: 20 63 6f 6e 74 61 69 6e 73 20 61 20 76 61 6c 69   contains a vali
15b0: 64 20 76 61 72 69 61 62 6c 65 20 6e 61 6d 65 2c  d variable name,
15c0: 20 72 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e   return.** the n
15d0: 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
15e0: 65 72 73 20 69 6e 20 74 68 61 74 20 6e 61 6d 65  ers in that name
15f0: 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65  .  Otherwise, re
1600: 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69  turn 0..*/.stati
1610: 63 20 69 6e 74 20 76 61 6c 69 64 56 61 72 4e 61  c int validVarNa
1620: 6d 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  me(const char *z
1630: 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a  ){.  int i = 0;.
1640: 20 20 69 6e 74 20 69 6e 42 72 61 63 6b 65 74 20    int inBracket 
1650: 3d 20 30 3b 0a 20 20 69 66 28 20 7a 5b 30 5d 3d  = 0;.  if( z[0]=
1660: 3d 27 3c 27 20 29 7b 0a 20 20 20 20 69 6e 42 72  ='<' ){.    inBr
1670: 61 63 6b 65 74 20 3d 20 31 3b 0a 20 20 20 20 7a  acket = 1;.    z
1680: 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 5b  ++;.  }.  if( z[
1690: 30 5d 3d 3d 27 3a 27 20 26 26 20 7a 5b 31 5d 3d  0]==':' && z[1]=
16a0: 3d 27 3a 27 20 26 26 20 69 73 61 6c 70 68 61 28  =':' && isalpha(
16b0: 7a 5b 32 5d 29 20 29 7b 0a 20 20 20 20 7a 20 2b  z[2]) ){.    z +
16c0: 3d 20 33 3b 0a 20 20 20 20 69 20 2b 3d 20 33 3b  = 3;.    i += 3;
16d0: 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73 61  .  }else if( isa
16e0: 6c 70 68 61 28 7a 5b 30 5d 29 20 29 7b 0a 20 20  lpha(z[0]) ){.  
16f0: 20 20 7a 20 2b 2b 3b 0a 20 20 20 20 69 20 2b 3d    z ++;.    i +=
1700: 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   1;.  }else{.   
1710: 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
1720: 20 77 68 69 6c 65 28 20 69 73 61 6c 6e 75 6d 28   while( isalnum(
1730: 7a 5b 30 5d 29 20 7c 7c 20 7a 5b 30 5d 3d 3d 27  z[0]) || z[0]=='
1740: 5f 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20  _' ){.    z++;. 
1750: 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66     i++;.  }.  if
1760: 28 20 69 6e 42 72 61 63 6b 65 74 20 29 7b 0a 20  ( inBracket ){. 
1770: 20 20 20 69 66 28 20 7a 5b 30 5d 21 3d 27 3e 27     if( z[0]!='>'
1780: 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
1790: 20 69 20 2b 3d 20 32 3b 0a 20 20 7d 0a 20 20 72   i += 2;.  }.  r
17a0: 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn i;.}../*.*
17b0: 2a 20 54 68 65 20 7a 5b 5d 20 69 6e 70 75 74 20  * The z[] input 
17c0: 63 6f 6e 74 61 69 6e 73 20 74 65 78 74 20 6d 69  contains text mi
17d0: 78 65 64 20 77 69 74 68 20 54 48 31 20 73 63 72  xed with TH1 scr
17e0: 69 70 74 73 2e 0a 2a 2a 20 54 68 65 20 54 48 31  ipts..** The TH1
17f0: 20 73 63 72 69 70 74 73 20 61 72 65 20 63 6f 6e   scripts are con
1800: 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 3c 74  tained within <t
1810: 68 31 3e 2e 2e 2e 3c 2f 74 68 31 3e 2e 20 0a 2a  h1>...</th1>. .*
1820: 2a 20 54 48 31 20 76 61 72 69 61 62 6c 65 73 20  * TH1 variables 
1830: 61 72 65 20 24 61 61 61 20 6f 72 20 24 3c 61 61  are $aaa or $<aa
1840: 61 3e 2e 20 20 54 68 65 20 66 69 72 73 74 20 66  a>.  The first f
1850: 6f 72 6d 20 6f 66 0a 2a 2a 20 76 61 72 69 61 62  orm of.** variab
1860: 6c 65 20 69 73 20 6c 69 74 65 72 61 6c 2e 20 20  le is literal.  
1870: 54 68 65 20 73 65 63 6f 6e 64 20 69 73 20 72 75  The second is ru
1880: 6e 20 74 68 72 6f 75 67 68 20 68 74 6d 6c 69 7a  n through htmliz
1890: 65 0a 2a 2a 20 62 65 66 6f 72 65 20 62 65 69 6e  e.** before bein
18a0: 67 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a 0a 2a  g inserted..**.*
18b0: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 70  * This routine p
18c0: 72 6f 63 65 73 73 65 73 20 74 68 65 20 74 65 6d  rocesses the tem
18d0: 70 6c 61 74 65 20 61 6e 64 20 77 72 69 74 65 73  plate and writes
18e0: 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20   the results.** 
18f0: 6f 6e 20 65 69 74 68 65 72 20 73 74 64 6f 75 74  on either stdout
1900: 20 6f 72 20 69 6e 74 6f 20 43 47 49 2e 0a 2a 2f   or into CGI..*/
1910: 0a 69 6e 74 20 54 68 5f 52 65 6e 64 65 72 28 63  .int Th_Render(c
1920: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
1930: 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 69 6e   int i = 0;.  in
1940: 74 20 6e 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  t n;.  int rc = 
1950: 54 48 5f 4f 4b 3b 0a 20 20 75 63 68 61 72 20 2a  TH_OK;.  uchar *
1960: 7a 52 65 73 75 6c 74 3b 0a 20 20 54 68 5f 46 6f  zResult;.  Th_Fo
1970: 73 73 69 6c 49 6e 69 74 28 29 3b 0a 20 20 77 68  ssilInit();.  wh
1980: 69 6c 65 28 20 7a 5b 69 5d 20 29 7b 0a 20 20 20  ile( z[i] ){.   
1990: 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 24 27 20 26   if( z[i]=='$' &
19a0: 26 20 28 6e 20 3d 20 76 61 6c 69 64 56 61 72 4e  & (n = validVarN
19b0: 61 6d 65 28 26 7a 5b 69 2b 31 5d 29 29 3e 30 20  ame(&z[i+1]))>0 
19c0: 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  ){.      const c
19d0: 68 61 72 20 2a 7a 56 61 72 3b 0a 20 20 20 20 20  har *zVar;.     
19e0: 20 69 6e 74 20 6e 56 61 72 3b 0a 20 20 20 20 20   int nVar;.     
19f0: 20 73 65 6e 64 54 65 78 74 28 7a 2c 20 69 2c 20   sendText(z, i, 
1a00: 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b  0);.      if( z[
1a10: 69 2b 31 5d 3d 3d 27 3c 27 20 29 7b 0a 20 20 20  i+1]=='<' ){.   
1a20: 20 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c 65       /* Variable
1a30: 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 24 3c  s of the form $<
1a40: 61 61 61 3e 20 2a 2f 0a 20 20 20 20 20 20 20 20  aaa> */.        
1a50: 7a 56 61 72 20 3d 20 26 7a 5b 69 2b 32 5d 3b 0a  zVar = &z[i+2];.
1a60: 20 20 20 20 20 20 20 20 6e 56 61 72 20 3d 20 6e          nVar = n
1a70: 2d 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  -2;.      }else{
1a80: 0a 20 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69  .        /* Vari
1a90: 61 62 6c 65 73 20 6f 66 20 74 68 65 20 66 6f 72  ables of the for
1aa0: 6d 20 24 61 61 61 20 2a 2f 0a 20 20 20 20 20 20  m $aaa */.      
1ab0: 20 20 7a 56 61 72 20 3d 20 26 7a 5b 69 2b 31 5d    zVar = &z[i+1]
1ac0: 3b 0a 20 20 20 20 20 20 20 20 6e 56 61 72 20 3d  ;.        nVar =
1ad0: 20 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   n;.      }.    
1ae0: 20 20 72 63 20 3d 20 54 68 5f 47 65 74 56 61 72    rc = Th_GetVar
1af0: 28 67 2e 69 6e 74 65 72 70 2c 20 28 75 63 68 61  (g.interp, (ucha
1b00: 72 2a 29 7a 56 61 72 2c 20 6e 56 61 72 29 3b 0a  r*)zVar, nVar);.
1b10: 20 20 20 20 20 20 7a 20 2b 3d 20 69 2b 31 2b 6e        z += i+1+n
1b20: 3b 0a 20 20 20 20 20 20 69 20 3d 20 30 3b 0a 20  ;.      i = 0;. 
1b30: 20 20 20 20 20 7a 52 65 73 75 6c 74 20 3d 20 28       zResult = (
1b40: 75 63 68 61 72 2a 29 54 68 5f 47 65 74 52 65 73  uchar*)Th_GetRes
1b50: 75 6c 74 28 67 2e 69 6e 74 65 72 70 2c 20 26 6e  ult(g.interp, &n
1b60: 29 3b 0a 20 20 20 20 20 20 73 65 6e 64 54 65 78  );.      sendTex
1b70: 74 28 28 63 68 61 72 2a 29 7a 52 65 73 75 6c 74  t((char*)zResult
1b80: 2c 20 6e 2c 20 6e 3e 6e 56 61 72 29 3b 0a 20 20  , n, n>nVar);.  
1b90: 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 5b 69 5d    }else if( z[i]
1ba0: 3d 3d 27 3c 27 20 26 26 20 69 73 42 65 67 69 6e  =='<' && isBegin
1bb0: 53 63 72 69 70 74 54 61 67 28 26 7a 5b 69 5d 29  ScriptTag(&z[i])
1bc0: 20 29 7b 0a 20 20 20 20 20 20 73 65 6e 64 54 65   ){.      sendTe
1bd0: 78 74 28 7a 2c 20 69 2c 20 30 29 3b 0a 20 20 20  xt(z, i, 0);.   
1be0: 20 20 20 7a 20 2b 3d 20 69 2b 35 3b 0a 20 20 20     z += i+5;.   
1bf0: 20 20 20 66 6f 72 28 69 3d 30 3b 20 7a 5b 69 5d     for(i=0; z[i]
1c00: 20 26 26 20 28 7a 5b 69 5d 21 3d 27 3c 27 20 7c   && (z[i]!='<' |
1c10: 7c 20 21 69 73 45 6e 64 53 63 72 69 70 74 54 61  | !isEndScriptTa
1c20: 67 28 26 7a 5b 69 5d 29 29 3b 20 69 2b 2b 29 7b  g(&z[i])); i++){
1c30: 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 54 68 5f  }.      rc = Th_
1c40: 45 76 61 6c 28 67 2e 69 6e 74 65 72 70 2c 20 30  Eval(g.interp, 0
1c50: 2c 20 28 63 6f 6e 73 74 20 75 63 68 61 72 2a 29  , (const uchar*)
1c60: 7a 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66 28  z, i);.      if(
1c70: 20 72 63 21 3d 54 48 5f 4f 4b 20 29 20 62 72 65   rc!=TH_OK ) bre
1c80: 61 6b 3b 0a 20 20 20 20 20 20 7a 20 2b 3d 20 69  ak;.      z += i
1c90: 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 30 5d  ;.      if( z[0]
1ca0: 20 29 7b 20 7a 20 2b 3d 20 36 3b 20 7d 0a 20 20   ){ z += 6; }.  
1cb0: 20 20 20 20 69 20 3d 20 30 3b 0a 20 20 20 20 7d      i = 0;.    }
1cc0: 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 2b 2b 3b  else{.      i++;
1cd0: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
1ce0: 20 72 63 3d 3d 54 48 5f 45 52 52 4f 52 20 29 7b   rc==TH_ERROR ){
1cf0: 0a 20 20 20 20 73 65 6e 64 54 65 78 74 28 22 3c  .    sendText("<
1d00: 68 72 3e 3c 70 3e 3c 66 6f 6e 74 20 63 6f 6c 6f  hr><p><font colo
1d10: 72 3d 5c 22 72 65 64 5c 22 3e 3c 62 3e 45 52 52  r=\"red\"><b>ERR
1d20: 4f 52 3a 20 22 2c 20 2d 31 2c 20 30 29 3b 0a 20  OR: ", -1, 0);. 
1d30: 20 20 20 7a 52 65 73 75 6c 74 20 3d 20 28 75 63     zResult = (uc
1d40: 68 61 72 2a 29 54 68 5f 47 65 74 52 65 73 75 6c  har*)Th_GetResul
1d50: 74 28 67 2e 69 6e 74 65 72 70 2c 20 26 6e 29 3b  t(g.interp, &n);
1d60: 0a 20 20 20 20 73 65 6e 64 54 65 78 74 28 28 63  .    sendText((c
1d70: 68 61 72 2a 29 7a 52 65 73 75 6c 74 2c 20 6e 2c  har*)zResult, n,
1d80: 20 31 29 3b 0a 20 20 20 20 73 65 6e 64 54 65 78   1);.    sendTex
1d90: 74 28 22 3c 2f 62 3e 3c 2f 66 6f 6e 74 3e 3c 2f  t("</b></font></
1da0: 70 3e 22 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 7d  p>", -1, 0);.  }
1db0: 65 6c 73 65 7b 0a 20 20 20 20 73 65 6e 64 54 65  else{.    sendTe
1dc0: 78 74 28 7a 2c 20 69 2c 20 30 29 3b 0a 20 20 7d  xt(z, i, 0);.  }
1dd0: 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1de0: 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20  ./*.** COMMAND: 
1df0: 74 65 73 74 2d 74 68 2d 72 65 6e 64 65 72 0a 2a  test-th-render.*
1e00: 2f 0a 76 6f 69 64 20 74 65 73 74 5f 74 68 5f 72  /.void test_th_r
1e10: 65 6e 64 65 72 28 76 6f 69 64 29 7b 0a 20 20 42  ender(void){.  B
1e20: 6c 6f 62 20 69 6e 3b 0a 20 20 69 66 28 20 67 2e  lob in;.  if( g.
1e30: 61 72 67 63 3c 33 20 29 7b 0a 20 20 20 20 75 73  argc<3 ){.    us
1e40: 61 67 65 28 22 46 49 4c 45 22 29 3b 0a 20 20 7d  age("FILE");.  }
1e50: 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 69 6e  .  blob_zero(&in
1e60: 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 61 64 5f 66  );.  blob_read_f
1e70: 72 6f 6d 5f 66 69 6c 65 28 26 69 6e 2c 20 67 2e  rom_file(&in, g.
1e80: 61 72 67 76 5b 32 5d 29 3b 0a 20 20 54 68 5f 52  argv[2]);.  Th_R
1e90: 65 6e 64 65 72 28 62 6c 6f 62 5f 73 74 72 28 26  ender(blob_str(&
1ea0: 69 6e 29 29 3b 0a 7d 0a                          in));.}.