1 # Various utility functions, including template substitution.
3 # Copyright (C) 1999 Ian Jackson <ijackson@chiark.greenend.org.uk>
5 # This is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation; either version 2,
8 # or (at your option) any later version.
10 # This is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public
16 # License along with this file; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 open RAND,"/dev/urandom" or die $!;
21 sub process_file ($) {
22 local ($filename) = @_;
24 open X, "$filename" or die "$filename: $!";
28 $x[$#x] eq "\@\@\@eof:\@\@\@\n" or die $!;
41 read(RAND,$v,$bytes) == $bytes or die $!;
42 $r= scalar unpack("H$nybbles",$v);
43 print DEBUG "randnybs($nybbles) -> $r\n";
59 # out("<!-- $level $doing $_ -->");
60 last if m/^\@\@\@end\w+\:\@\@\@$/;
62 if (m/^\@\@\@(if|ifnot):([0-9a-z_|]+)\@\@\@$/) {
66 map { $do=1 if getvar($_); } split(/\|/,$v);
67 $do= !$do if $q eq 'ifnot';
68 # out("<!-- $level $doing $do $q $v $_ -->");
70 process($doing && $do);
71 } elsif (m/^\@\@\@foreach\:(area|db)\@\@\@$/) {
74 for (&{"foreach_start_$1"};
76 &{"foreach_incr_$1"}) {
77 &{"foreach_setvars_$1"};
83 } elsif (m/^\@\@\@comment\:(\s.*)?$/) {
87 s/\@\@\@(\w+)\@\@\@/ getvar("$1") /ge;
92 die "$filename:$cl:unknown $_\n" if m/\@\@\@/;
100 defined $$vn or die "$filename:$cl:undefined $vn\n$out";
104 %saniarray= ('<','lt', '>','gt', '&','amp', '"','quot');
108 while ($in =~ m/[<>&"]/) {
109 $out.= $`. '&'. $saniarray{$&}. ';';