📄 _pi3expr.cpp
字号:
/*____________________________________________________________________________*\
Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.
These sources, libraries and applications are
FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
as long as the following conditions are adhered to.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*____________________________________________________________________________*|
*
* $Source: /cvsroot/pi3web/Pi3Web_200/Source/Pi3API/_Pi3Expr.cpp,v $
* $Date: 2004/07/04 19:40:44 $
*
Description:
Description:
------------
Expressions are snippets of a mini-lanaguage for generating and
manipulating text. In general it a pretty simple concept, - expressions
can be simple expressions like static strings, or can be more complex
like list of different types of expressions (ExpressionComponent) or
expression whose results are a function of thier subexpressions
(FunctionComponents).
The only other complicating factor is that expressions try to
optimize themselves by merging and preevaluating static expressions
at the construction phase.
There are three kinds of classes used internally here:-
- Context, this contains the context for an expression evaluation
including a PIHTTP object (which may be empty if this is an
optimization (compilation) of a static expression). This class
also contains parameter information set by the expression
evaluator.
- Components, all derived classes of ComponentBase. They form
of the core of expressions and are polymorphic expressions of
various types.
- Pi3Expression, this is a wrapper around a single
ExpressionComponent and is the interface to the outside world.
NOTES:
------
Make this more efficient by
- evaluate all shortcuts and update short documentation
- maybe change the way that Context object are generated for the
compile phase, they should probably be passed in as arguments. This
also supports run-time compilation which would resolve everything
to a string.
- A little genernal cleanup is in order here. In particular,
ditch FunctionArgument classes in favour of using
Components directly. And get rid of the automatic creation of empty
PIHTTP and Context classes during optimization, instead make one
single one and pass it around.
- bug, it is an error that the following
(Process, thread) ($P, $k)
evaluates to
Process/thread -1/508
\*____________________________________________________________________________*/
//$SourceTop:$
#include <stdio.h>
#include <ctype.h>
#include "PIStrStr.h"
#include "_Pi3Expr.h"
#include "AutoDel.h"
#include "HTTPCore.h"
#include "HTTPUtil.h"
#define INDENT_PATTERN "\t"
/*
#define D { cerr << __FILE__ << ", " << __LINE__ << endl; };
*/
#define D
/*
** NOTE: This is because of some compiler problems
*/
void *_Pi3Expression::Compiler_Bug_Alloc( void *p1, void *p2,
void *p3 )
{
return PI_NEW( _Pi3Expression( (const char *)p1, (FnPi3Write *)p2,
*( (PIString *)p3 ) ) );
}
/*____________________________________________________________________________*\
*
Class:
Description:
Documentation.
\*____________________________________________________________________________*/
#if 0
/*
** HTML documentation
*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
Pi3Expression
Description:
A Pi3Expression is a general purpose expression syntax for efficient
dynamic creation of text based on varied criteria. The expression takes
the form of a sequence of text characters with some control characters
which modify the meaning of the text which follows them. A Pi3Expression
does not have to be enclosed in quotes but often will be quoted in
configuration files to deliniate the expression from other configuration
components.
For illustration all examples used here will enclose the example expression
in double-quotations ("").
Components:
<H5>Overview</H5>
<TABLE BORDER=1>
<TH>Component
<TH>Syntax
<TH>Short Description
<TH>Example Expression
<TH>Example Result
<TR>
<TD>String
<TD>Any character except $,%,&
<TD>Plain text
<TD>"Hello, World!"
<TD>Hello, World!
<TR>
<TD>Escape Sequences
<TD>\c, where c is any character
<TD>Escaped charcaters, like printf format
<TD>"Line1\nLine2"
<TD>Line1<BR>Line2
<TR>
<TD>Literal
<TD>'any characters'
<TD>Literal text, characters not interpreted.
<TD>"The percent character is '%'"
<TD>The percent character is %
<TR>
<TD>Parameter
<TD>%c, where c is a letter
<TD>A parameter, value depends on Pi3Expression context
<TD>"Parameter value of parameter ''v'' is ''%v''"
<TD>Parameter value of parameter 'v' is 'john'
<TR>
<TD>Shortcut
<TD>$s, where s is a letter
<TD>A shortcut, shortcut value mappings are given below
<TD>"The current process id is $P"
<TD>The current process id is 24456
<TR>
<TD>Functions
<TD>&FunctionName(Param1,Param2,..,ParamN)
<TD>A text processing function, described in 'Pi3Expression_Functions'
<TD>"Process id truncated to two characters is &trunc($P,2)"
<TD>Process id truncated to two characters is 24
</TABLE>
<H4>Detailed Description of Components</H4>
<H5>
String
</H5>
Regular text that does not conform to other expression components is
reproduced exactly if the expression result as in the expression string.
The characters '(', ',', and ')' are considered normal characters outside
a function context.
<H5>
Escape Sequences
</H5>
The escape or backslash character will be expanded to a control character in accordance with the following table:-
<CENTER>
<TABLE BORDER=1>
<TH>Escape Sequence
<TH>Meaning
<TR>
<TD>\a
<TD>Alert
<TR>
<TD>\b
<TD>Backslash
<TR>
<TD>\f
<TD>Form-feed
<TR>
<TD>\n
<TD>New-line
<TR>
<TD>\r
<TD>Carriage return
<TR>
<TD>\t
<TD>Horizontal tab
<TR>
<TD>\v
<TD>Vertical tab
</TABLE>
</CENTER>
Additionally any other character including characters with special meanings
in Pi3Expressions ($,%,&,\,´) can be specified by placing ´\´ before them.
<H5>
Literal
</H5>
Character sequences enclosed in single quotes will be reproduced in
the expression result as in the expression specification, but with the
single quotes ommitted.
<H5>
Parameter
</H5>
The '%' character followed by a letter specifies a parameter. The
value of a parameter depends on the evaluation context and should be
documented along with the facility that uses the expression. Parameter
characters are case sensitive.
<H5>
Shortcut
</H5>
The '$' character followed by a letter specifies a shortcut. Shortcut
characters are case sensitive. The follow table gives shortcut mappings.
<CENTER>
<TABLE BORDER=1>
<TH>Shortcut<TH>Evaluates to<TR>
<TD>$a<TD>E-mail address of administrator (for current virtual host)<TR>
<TD>$A<TD>IP Address of remote client<TR>
<TD>$b<TD>Decimal representation of number of bytes sent to client this request<TR>
<TD>$c<TD>Content-Type for the response<TR>
<TD>$C<TD>Content-Type for the request (incoming)<TR>
<TD>$d<TD>Debug flag, returns non-zero length string if debugging is on<TR>
<TD>$D<TD>Handler time delta. Time spent in last handler object<TR>
<TD>$e<TD>SSL cipher name<TR>
<TD>$E<TD>All error messages logged while handling this (sub)request<TR>
<TD>$f<TD>Physical path to resource<TR>
<TD>$g<TD>Indicate whether or not SSL is being used (HTTPS), (on/off)<TR>
<TD>$G<TD>SSL secret key size<TR>
<TD>$h<TD>Hostname of client<TR>
<TD>$H<TD>Request HTTP protocol stamp<TR>
<TD>$i<TD>Remoteident of client<TR>
<TD>$I<TD>Path info section of client request URL<TR>
<TD>$k<TD>Thread id of executing thread of execution<TR>
<TD>$K<TD>SSL public key size<TR>
<TD>$l<TD>Local IP address of connection<TR>
<TD>$m<TD>Request method from client<TR>
<TD>$M<TD>End of line sequence for the current operating system<TR>
<TD>$n<TD>Name of current handler object<TR>
<TD>$N<TD>Name of current handler phase<TR>
<TD>$o<TD>Objectname of virtual host<TR>
<TD>$p<TD>Port associated with this virtual host<TR>
<TD>$P<TD>Process id<TR>
<TD>$q<TD>Client query string<TR>
<TD>$r<TD>Full uninterpreted request line from client<TR>
<TD>$R<TD>Name of result code from last handler<TR>
<TD>$s<TD>Current decimal HTTP response status code<TR>
<TD>$S<TD>Server version stamp<TR>
<TD>$t<TD>Time in common logfile format<TR>
<TD>$T<TD>Elapsed since request context (PIHTTP) was created<TR>
<TD>$u<TD>Remote username (from authentication)<TR>
<TD>$U<TD>URL path of original request<TR>
<TD>$v<TD>Hostname of virtual host<TR>
<TD>$x<TD>Authentication type from is access was authenticated<TR>
<TD>$X<TD>Depth of current request in subrequest hierarchy<TR>
<TD>$y<TD>Content-Length: outgoing to client<TR>
<TD>$Y<TD>Content-Length: incoming from client<TR>
<TD>$z<TD>Script name<TR>
<TD>$Z<TD>Path translated (url-unencoded path info)<TR>
</TABLE>
</CENTER>
<H5>
Functions
</H5>
The built-in text manipulation functions are described in the section
'Pi3Expression_Functions'.
Examples:
<CENTER>
<TABLE BORDER=1>
<TD><B>Expression</B><TD>Process/Thread information [$P|$k]<TR>
<TD><B>Evaluates to</B><TD>Process/Thread information [24965|23]<TR>
</TABLE>
<TABLE BORDER=1>
<TD><B>Expression</B><TD>[$t]$h|$r<TR>
<TD><B>Evaluates to</B><TD>|15/May/1997:21:30:16 +0000|caught.evil.dom|GET /../../etc/passwd HTTP/1.0<TR>
</TABLE>
</CENTER>
/*___+++HTMLDOC_END+++___*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
Pi3Expression_Functions
Description:
Pi3Expressions can use built in functions for text manipulation and
conditional processing. These functions can be nested to perform
complex evaluations. The built-in text processing functions are
described here.
Where text is interpreted as a numeric value, leading whitespace is
skipped, then the text before the first non-digit is interpreted as
a decimal number.
Where text is interpreted as a boolean value, any non-zero length is
considered 'true' and any text of length zero is considered 'false'.
Functions:
<H5>Overview</H5>
Note the following conventions used to describing these functions:-
<LIST>
<ITEM>text - any text, e.g. "foo", "234'&'ad"
<ITEM>number - text interpreted as a number, e.g. "5a" (interpreted as 5).
<ITEM>[field] - an optional argument
<ITEM>value1|value2 - either "value1" or "value2" as text, e.g left|right
</LIST>
<TABLE BORDER=1>
<TH>Function
<TH>Number of Arguments
<TH>Syntax
<TH>Short Description
<TH>Example Expression
<TH>Example Result
<TR>
<TD>abbrev
<TD>3
<TD>&abbrev(text,number,ellipse)
<TD>truncate text and append other text if truncated
<TD>"&abbrev(Abcdef,7,...)"<BR>"&abbrev(Abcdefgh,7,...)"
<TD>"Abcdef"<BR>"Abcd..."
<TR>
<TD>align
<TD>2..4
<TD>&align(text,number,[left|right],[padding])
<TD>Align text within a field
<TD>"&align(1234,7)"<BR>"&align(1234,7,right,-)"
<TD>"1234 "<BR>"---1234"
<TR>
<TD>arg,_
<TD>0..2
<TD>&_([index],number)
<TD>Function argument
<TD>"&_(0,0)"; &arg(1)
<TD>context dependent
<TR>
<TD>cmp
<TD>2
<TD>&cmp(text1,text2)
<TD>String comparision
<TD>"&cmp(hello,Hello)"
<TD>""
<TR>
<TD>cmpi
<TD>2
<TD>&cmpi(text1,text2)
<TD>String comparision case insensitive
<TD>"&cmpi(hello,Hello)"
<TD>"true"
<TR>
<TR>
<TD>dblookup
<TD>3..7
<TD>&dblookup(dbname,type,variable[,flags][,fnvalue][,first][,last])
<TD>Lookup a value in an internal database
<TD>"&dblookup(request,rfc822,Content-Type)"
<TD>"application/x-www-form-urlencoded"
<TR>
<TD>dbreplace
<TD>3..7
<TD>&dbreplace(dbname,type,variable,value[,flags])
<TD>Replace a value in an internal database
<TD>"&dbreplace(request,rfc822,Content-Type,text/html)"
<TD>"text/html"
<TR>
<TD>if
<TD>2..3
<TD>&if(boolean,true_text,false_text)
<TD>Conditionally include text
<TD>"&if(text,yes,no)"<BR>"&if(,yes,no)"
<TD>"yes"<BR>"no"
<TR>
<TD>not
<TD>1
<TD>&not(boolean)
<TD>Negate boolean argument
<TD>"&not(text)"<BR>"&not()"
<TD>""<BR>"true"
<TR>
<TD>regexp
<TD>2
<TD>&regexp(regexp,text)
<TD>Regular expression comparision
<TD>"&regexp(j*n,john)"<BR>"&regexp(b*l,john)"
<TD>"true"<BR>""
<TR>
<TD>trunc
<TD>2
<TD>&trunc(text,number)
<TD>Truncate text to length of number
<TD>"&trunc(0123,2)"
<TD>"01"
</TABLE>
<H4>Detailed Description of Functions</H4>
<H5>
abbrev(field,fieldlen,append)
</H5>
This function is used to truncate text to a field of fieldlen characters
appending text 'append' at the end of the field if it was truncated, this
is useful for truncating test to fit in a field while given a clear
indication if the field was concatenated.
<H5>
align(text,length[,left|right][,padding])
</H5>
Write text 'text' into a field of length characters, aligning to the
left or right. The character sequence padding is repeated to
fill out the field.
The default alignment is left. The default padding is spaces.
If 'text' is longer than the value of 'length', it will not be truncated.
A field of exactly 'length' character can be ensured by using an expression like:-
<CODE>
&align(&trunc(text is too big,10),10)
</CODE>
<H5>
arg([index],[context])
</H5>
This is a special function with meaning only within the context of another
function which is iterating through a number of elements. The argument
'index' is the index of the argument from the enclosing function. The
argument context is a number indicating the which enclosing functions
context to use. Context 0 refers to the callees context, 1 to the callees
callee etc.
<H5>
cmp(text1,text2), cmpi(text1,text2)
</H5>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -