📄 extensions.texi
字号:
@var{attribute-name}. The index @var{n} is counted from zero, so@smallexample %[@var{attribute-name}](0)@end smallexample@noindentis equivalent to@smallexample %[@var{attribute-name}]@end smallexample@item IdentifiersIdentifiers represent functions and variables. These are described inthe next sub-subsection.@item Regexp group referencesA sequence of characters in the form@smallexample@samp{\@var{number}}@end smallexample@noindentrefers to the contents of parenthesized group number @var{number}obtained as a result of the last executed @samp{=~} command.The regexp group reference has always string data type. For example:@smallexample@groupstringbasename(string @var{arg})@{ if (arg =~ ".*/\(.*\)\..*") return \1; else return arg;@}@end group@end smallexampleThis function strips from @var{arg} all leading components up to thelast slash character, and all trailing components after the last dotcharacter. It returns @var{arg} unaltered if it does not containslashes and dots. It is roughly analogous to the system @code{basename}utility.@end table@comment **L4***************************************************************@node Identifiers@subsubsection Rewrite Identifiers@cindex Identifiers, Rewrite@cindex Rewrite identifiersA valid identifier is a string of characters meeting the followingrequirements:@enumerate 1@item It starts with either a lower- or an uppercase letter of the Latinalphabet or either of the following symbols: @samp{_}, @samp{$}.@item It consists of alphanumeric characters, underscores(@samp{_}), anddollar signs (@samp{$}).@end enumerate@comment **L4***************************************************************@node Declarations@subsubsection Rewrite Declarations@cindex Declarations, Rewrite@subheading Function declarationsA Rewrite function is declared as follows:@smallexample@var{type} @var{function-name} (@var{parameter-list})@end smallexample@noindentwhere @var{type} specifies the return type of the function,@var{function-name} declares the symbolic name of the function, and@var{parameter-list} declares the formal parameters to the function.It is a comma-separated list of declarations in the form@smallexample@var{type} @var{parm-name}@end smallexample@noindent@var{type} being the parameter type, and @var{parm-name} being itssymbolic name. Both @var{function-name} and @var{parm-name} shouldbe valid identifiers.@subheading Variable declarationsThere are no global variables in Rewrite. All variables are local.The local variables are declared right after the opening curly brace(@samp{@{}) and before any executable statements. The declarationsyntax is@smallexample@var{type} @var{ident_list} ;@end smallexample@noindentHere @var{ident_list} is either a valid Rewrite identifier or a comma-separated list of such identifiers.Note that, unlike inC, no assignments are allowed in variable declarations.@comment **L4***************************************************************@node Statements@subsubsection Rewrite Statements@cindex Statements, RewriteThe Rewrite statements are: expressions, assignments, conditionalstatements, and return statements. A statement is terminated by a semicolon.@subheading ExpressionsAn @dfn{expression} is one of the following:@itemize @bullet@item A variable identifier@item A type coercion expression@item An arithmetic expression@item A boolean expression@item An assignment@item A function call@item A delete statement@end itemize@subheading Type coercionThe type coercion is like a type cast in C. Its syntax is@smallexample@samp{(} @var{type} @samp{)} @var{ident}@end smallexample@noindentThe result of type coercion is as follows:@multitable @columnfractions .20 .20 .60@item @var{type} @tab Variable type @tab Resulting conversion@item integer@tab integer@tab No conversion. This results in the same integer value.@item integer@tab string@tab If the string value of the variable is a valid @sc{ascii} representationof the integer number (either decimal, octal, or hex), it is converted tothe integer; otherwise the result of the conversion is undefined.@item string@tab integer@tab The @sc{ascii} representation (in decimal) of the integer number. @item string@tab string@tab No conversion. This results in the same string value.@end multitable@subheading AssignmentAn assignment is@smallexample@var{ident} = @var{expression} ;@end smallexample@noindentThe variable @var{ident} is assigned the value of @var{expression}.@subheading Function callsThese take the form@smallexample@var{ident} ( @var{arg-list} )@end smallexample@noindentwhere @var{ident} is the identifier representing the function, and@var{arg-list} is a comma-separated list of expressions supplyingactual arguments to the function. The number of the expressionsmust correspond exactly to the number of formal parameters in thefunction definition. The function that @var{ident} references can be either a compiled function or a built-infunction.@subheading @samp{delete} statementThe @samp{delete} statement is used to delete an attribute or attributesfrom the incoming request. Its syntax is:@smallexampledelete @var{attribute-name};delete @var{attribute-name}(@var{n});@end smallexampleThe first variant deletes @emph{all} the attributes of the given type.The second variant deletes only the @var{n}th occurrence of the matchingattribute.@comment **L4***************************************************************@node Regular Expressions@subsubsection Regular Expressions@cindex Regular Expressions, RewriteRewrite uses POSIX regular expressions(@xref{Top,,Regular Expression Library,regex,Regular ExpressionLibrary}, for the detailed description of these).You control the exact type of regular expressions by the use of thepragmatic comment @code{regex}. Its syntax is as follows:@smallexample#pragma regex @var{option-list}@end smallexample@noindent@var{Option-list} is a whitespace-separated list of options. Each optionis one of the following words prefixed with @samp{+} or @samp{-}:@table @code@item extendedUse POSIX extended regular expression syntax when interpreting regularexpressions.@item icaseDo not differentiate case. Subsequent regular expression comparisonswill be case insensitive. @item newlineMatch-any-character operators don't match a newline.A non-matching list (@samp{[^...]}) not containing a newline doesnot match a newline.Match-beginning-of-line operator (@samp{^}) matches the empty stringimmediately after a newline.Match-end-of-line operator (@samp{$}) matches the empty stringimmediately before a newline.@end tablePrefixing an option with @samp{+} means to enable the correspondingbehavior. Prefixing it with @samp{-} means to disable it. Thus,the following statement:@smallexample#pragma regex +extended +icase@end smallexample@noindentwill enable extended POSIX regular expressions using case-insensitivecomparison. Using the following comment:@smallexample#pragma regex -extended @end smallexample@noindentwill switch to the basic POSIX regular expressions.The settings of a @code{regex} pragmatic comment remain in force up tothe end of current source file, or to the next @code{regex} comment,whichever occurs first.For compatibility with previous versions, GNU Radius usesthe following defaults:@smallexample#pragma regex -extended -icase -newline@end smallexample@noindenti.e. all regular expressions are treated as basic POSIX, comparisonis case-sensitive.@comment **L4***************************************************************@node Built-in Functions@subsubsection Rewrite Built-in Functions@cindex Built-in functions, RewriteThe following built-in functions are provided:@deftypefn Function integer length (string @var{s})Returns the length of the string @var{s}.@smallexamplelength("string") @result{} 6@end smallexample@end deftypefn@deftypefn Function integer index (string @var{s}, integer @var{c})Returns the index of the first occurrence of the character @var{c} inthe string @var{s}. Returns @minus{}1 if no such occurrence is found.@smallexampleindex("/raddb/users", 47) @result{} 0index("/raddb/users", 45) @result{} @minus{}1@end smallexample@end deftypefn@deftypefn Function integer rindex (string @var{s}, integer @var{i})Returns the index of the last occurrence of the character @var{c} inthe string @var{s}. Returns @minus{}1 if no such occurrence is found.@smallexamplerindex("/raddb/users", 47) @result{} 6@end smallexample@end deftypefn@deftypefn Function string substr (string @var{s}, integer @var{start}, integer @var{length})Returns the substring of @var{s} of length at most @var{length} starting atposition @var{start}.@smallexamplesubstr("foo-bar-baz", 3, 5) @result{} "-bar-"@end smallexample@end deftypefnAll character positions in strings are counted from 0.@deftypefn Function string field (string @var{buffer}, integer @var{n})This function regards the @var{buffer} argument as consisting offields separated with any amount of whitespace. It extracts andreturns the @var{n}th field. @var{n} is counted from 1.@smallexamplefield("GNU's not UNIX", 1) @result{} "GNU's"field("GNU's not UNIX", 2) @result{} "not"field("GNU's not UNIX", 3) @result{} "UNIX"field("GNU's not UNIX", 4) @result{} ""@end smallexample@end deftypefn@deftypefn Function integer logit (string @var{msg})Outputs its argument to the Radius log channel @code{info}. Returns 0.For debugging purposes.@end deftypefn@deftypefn Function integer inet_aton (string @var{str})Converts the Internet host address @var{str} from thestandard numbers-and-dots notation into the equivalent integer inhost byte order.@smallexampleinet_aton("127.0.0.1") @result{} 2130706433@end smallexample@end deftypefn@deftypefn Function string inet_ntoa (integer @var{ip})Converts the Internet host address @var{ip} givenin host byte order to a string in standard numbers-and-dots notation.@smallexampleinet_ntoa(2130706433) @result{} "127.0.0.1"@end smallexample@end deftypefn@deftypefn Function integer htonl (integer @var{n})Converts the integer @var{n}, regarded as long, from host to network byteorder.@end deftypefn@deftypefn Function integer ntohl (integer @var{n})Converts the integer @var{n}, regarded as long, from network to host byteorder.@end deftypefn@deftypefn Function integer htons (integer @var{n})Converts the integer @var{n}, regarded as short, from host to network byteorder.@end deftypefn@deftypefn Function integer ntohs (integer @var{n})Converts the integer @var{n}, regarded as short, from network to host byteorder.@end deftypefn@deftypefn Function string gsub (string @var{regex}, string @var{repl}, string @var{str})For each substring matching the regular expression @var{regex} in the string@var{str}, substitute the string @var{repl}, and return the resultingstring.@smallexamplegsub("s","S","strings") @result{} "StringS"gsub("[0-9][0-9]*","N","28 or 29 days") @result{} "N or N days"gsub("[()'\"]","/","\"a\" (quoted) 'string'") @result{} "/a/ /quoted/ /string/"@end smallexample@end deftypefn@deftypefn Function string qprn (string @var{str})Replace all non-printable characters in string S by theircorresponding hex value preceeded by a percent sign. Return theresulting string. Printable are alphabetical characters, decimaldigits and dash (@samp{-}). Other characters are considered non-printable.For example:@smallexampleqprn("a string/value") @result{} "a%20string%2Fvalue"@end smallexample@noindent@end deftypefn@deftypefn Function string quote_string (string @var{str})Replace all non-printable characters in string @var{str} by theirthree-digit octal code prefixed with a backslash, or by their Cescape notation, as appropriate. @dfn{Non-printable} charactersdepend on the locale settings. For example, suppose that the currentlocale is set to ISO-8859-1 (a so called ``Latin-1'' character set)and @point{} represents a tab character. Then:@smallexamplequote_string("Fran@,{c}ois contains non@point{}printable chars") @result{} "Fran\347ois contains non\tprintable chars"@end smallexample @end deftypefn@deftypefn Function string unquote_string (string @var{str})Replace C escape notations in string @var{str} with correspondingcharacters using current locale. For example, for ISO-8859-1 locale:@smallexampleunquote_string("Fran\347ois") @result{} "Fran@,{c}ois"@end smallexample@end deftypefn@deftypefn Function string toupper (string @var{str})Returns the copy of the string @var{str} with all alphabetical charactersconverted to upper case. For example:@smallexampletoupper("a-string") @result{} "A-STRING"@end smallexample@end deftypefn@deftypefn Function string tolower (string @var{str})Returns the copy of the string @var{str} with all alphabetical charactersconverted to lower case. For example:@smallexampletolower("A-STRING") @result{} "a-string"@end smallexample
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -