File: virt_handler.pm

package info (click to toggle)
virtuoso-opensource 6.1.4%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 245,116 kB
  • sloc: ansic: 639,631; sql: 439,225; xml: 287,085; java: 61,048; sh: 38,723; cpp: 36,889; cs: 25,240; php: 12,562; yacc: 9,036; lex: 7,149; makefile: 6,093; jsp: 4,447; awk: 1,643; perl: 1,017; ruby: 1,003; python: 329
file content (403 lines) | stat: -rw-r--r-- 9,922 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#  
#  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
#  project.
#  
#  Copyright (C) 1998-2006 OpenLink Software
#  
#  This project is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; only version 2 of the License, dated June 1991.
#  
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#  General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#  
#  
package VIRT::Embed::Persistent;

 my $stdout_ref;
 my $stdin_ref;
 my $stderr_ref;
#use strict;
 our %Cache;
 use Symbol qw(delete_package);

 # static
 sub valid_package_name {
     my($string) = @_;
     $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
     # second pass only for words starting with a digit
     $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;

     # Dress it up as a real package name
     $string =~ s|/|::|g;
     return "Embed" . $string;
 }

 sub set_env_vars {
   my $v_options = shift;
   $$stderr_ref->{'html_mode'} = 0;

   if ($$v_options{'__VIRT_CGI'} == '1')
     {
       foreach my $key (keys %$v_options)
	 {
	   if ($key !~ /__VIRT.*/)
	     {
	       my $val = $$v_options{$key};
#	       print STDERR "set_env_vars [$key]=[$val]\n";
	       $ENV{$key} = $val;
	     }
	 }
     }
   else
     {
        $$stdout_ref->{'html_mode'} = 0;
     }
 }

 # static
 sub eval_file {
     my($filename, $delete, $v_options, $v_params, $v_lines) = @_;
     #print STDERR "filename=[$filename] delete=[$delete] v_options=".$v_options." v_params=".$v_params." v_lines=".$v_lines."\n";
     my $package = valid_package_name($filename);
     my $mtime = -M $filename;
     $stdout_ref->text_reset;
     $stderr_ref->text_reset;
     $stdin_ref->setouttext ($v_params);
#    foreach my $xx2 ( keys %$v_options ) 
#       {
#	 print STDERR "key1=[$xx2]\n";
#	 print STDERR "data1=[",$v_options->{$xx2},"]\n";
#       }
     set_env_vars ($v_options);
     if(defined $Cache{$package}{mtime}
        &&
        $Cache{$package}{mtime} <= $mtime)
     {
        # we have compiled this subroutine already,
        # it has not been updated on disk, nothing left to do
        print STDERR "already compiled $package->handler\n";
     }
     else {
        local *FH;
        open FH, $filename or die "open '$filename' $!";
        local($/) = undef;
        my $sub = <FH>;
        close FH;

        #wrap the code into a subroutine inside our unique package
        my $eval = qq{package $package; sub handler { $sub; }};
        {
            # hide our variables within this block
            my($filename,$mtime,$package,$sub);
            eval $eval;
        }
        die $@ if $@;

        #cache it unless we're cleaning out each time
        $Cache{$package}{mtime} = $mtime unless $delete;
     }

     eval {$package->handler;};
     die $@ if ($@ && $@ != '');

     delete_package($package) if $delete;

     return ($stdout_ref->getintext, $stdout_ref->getinhdr, $stderr_ref->getintext);
 }

 # static
 sub eval_string {
     my($filename, $content, $delete, $mtime, $v_options, $v_params, $v_lines) = @_;
     #print STDERR "filename=[$filename] content=[$content] delete=[$delete] mtime=[$mtime] v_options=%v_options v_params=%v_params v_lines=",@v_lines,"\n";
     my $package = valid_package_name($filename);
     #my $mtime = -M $filename;
     $stdout_ref->text_reset;
     $stderr_ref->text_reset;
     $stdin_ref->setouttext ($v_params);
     set_env_vars ($v_options);
     if(0)
     {
        # we have compiled this subroutine already,
        # it has not been updated on disk, nothing left to do
        #print STDERR "already compiled $package->handler\n";
     }
     else {
        local($/) = undef;
        my $sub = $content;
        close FH;

        #wrap the code into a subroutine inside our unique package
        my $eval = qq{package $package; sub handler { $sub; }};
        {
            # hide our variables within this block
            my($filename,$mtime,$package,$sub);
            eval $eval;
        }
        die $@ if $@;

        #cache it unless we're cleaning out each time
        #$Cache{$package}{mtime} = $mtime unless $delete;
     }

     eval {$package->handler;};
     die $@ if ($@ && $@ != '');

     delete_package($package) if $delete;

     return ($stdout_ref->getintext, $stdout_ref->getinhdr, $stderr_ref->getintext);
 }

 # instance:  tie members
 sub TIEHANDLE { 
   #print STDERR "<shout>\n"; 
   my $i = {}; 
   text_reset (\$i);
   bless \$i, shift
 }  

 sub WRITE {
   my($self, $buf,$len,$offset) = @_;
   my $txt = substr (''.$buf, $len, $offset);
   #print STDERR "\n\n\n\n\n\nWRITE:\n";
   $$self->{'inbuffer'} .= $txt;
 }

 sub PRINT { 
   my $self = shift;
   my $txt = join ('', @_);
   #print STDERR "\n\n\n\n\n\nPRINT\n";
   $$self->{'inbuffer'} .= $txt;
 }  
 
 sub PRINTF {
   my $self = shift;
   my $fmt = shift;
   my $txt = sprintf($fmt, @_);
   #print STDERR "\n\n\n\n\n\nPRINTF\n";
   $$self->{'inbuffer'} .= $txt;
 }  
 
sub FILENO {
   my $self = shift;
   return (undef);
}

 sub READ {
   my $self = shift;
   my $bufref = \$_[0];
   my(undef,$len,$offset) = @_;
   unless (defined $offset) { $offset = 0; }
#   print STDERR "READ called, \$buf=$bufref, \$len=$len, \$offset=$offset\n";

   my $out_len = $$self->{'outlen'};
   my $out_ofs = $$self->{'outofs'};
   if ($out_ofs >= $out_len)
     {
       return 0;
     }
   
   my $to_copy = $len;
   if ($len > $out_len - $out_ofs)
     {
       $to_copy = $out_len - $out_ofs;
     }

   my $tail = substr ($$bufref, $offset, $to_copy);
   my $head = substr ($$bufref, 0, $offset);
   my $chunk = substr ($$self->{'outtext'}, $out_ofs, $to_copy);

#   print STDERR "READ will return $to_copy chars\n";
   $$bufref = $tail.$chunk.$tail;
   $$self->{'outofs'} += $to_copy;
   $to_copy;
 }
 
 sub READLINE { 
   my $self = shift; 
   #print STDERR "READLINE called $$self times\n"; 
   die "READLINE unimplemented";
 }  

 sub GETC { 
   my $self = shift; 
   if ($$self->{'outofs'} < $$self->{'outlen'})
     {
       my $ret = substr ($$self->{'outtext'}, $$self->{'outofs'}, $$self->{'outofs'} + 1);
       $$self->{'outofs'} += 1;
       return $ret;
     }

   return; 
 }  

 sub BINMODE {
   my $self = shift; 
 }
 
 sub CLOSE { 
   #print STDERR "CLOSE called.\n" 
 }  
 
 sub DESTROY { 
 }  
  
 sub tie_all {
   $stdout_ref = tie(*STDOUT,'VIRT::Embed::Persistent');
   $stderr_ref = tie(*STDERR,'VIRT::Embed::Persistent');
   $stdin_ref = tie(*STDIN, 'VIRT::Embed::Persistent');
 }

 sub text_reset {
   my $self = shift;
   $$self->{'intext'} = '';
   $$self->{'inbuffer'} = '';
   $$self->{'inhdr'} = '';
   $$self->{'outtext'} = '';
   $$self->{'outofs'} = 0;
   $$self->{'outlen'} = 0;
   $$self->{'html_mode'} = 1;
 }
 
 sub parseintext {
   my $sel = shift;
   my $txt = $$sel->{'inbuffer'};

   if ($$sel->{'html_mode'} == 0)
     {
       $$sel->{'intext'} = $txt;
       $$sel->{'inhdr'} = '';
       return $$sel->{'intext'};
     }

   my $empty_line_idx = index ($txt, "\n\r\n");
   my $len = 3;
   if ($empty_line_idx == -1)
     {
       $empty_line_idx = index $txt, "\n\n";
       $len = 2;
     }
   if ($empty_line_idx == -1)
     {
        $$sel->{'inhdr'} .= '';
        $$sel->{'intext'} .= $txt;
	return $txt;
     }
   else
      {
        $$sel->{'hdrmode'} = 0;
        my $to_hdr = substr ($txt, 0, $empty_line_idx + $len);
        my $to_txt = substr ($txt, $empty_line_idx + $len);
        #print STDERR "vh: adding [$to_hdr] to hdr, [$to_txt] to txt\n";
        $$sel->{'inhdr'} .= $to_hdr;
        $$sel->{'intext'} .= $to_txt;
	return  $$sel->{'intext'};  
      }
 }

 sub getinhdr {
   my $self = shift;
   if ($$self->{'intext'} == '' && $$self->{'inhdr'} == '')
     {
       $self->parseintext;
     }  
   return $$self->{'inhdr'};
 }

 sub getintext {
   my $self = shift;
   if ($$self->{'intext'} == '' && $$self->{'inhdr'} == '')
     {
       $self->parseintext;
     }  
   return $$self->{'intext'};
 }

 sub setouttext {
   my $self = shift;
   $$self->{'outtext'} = shift;
   $$self->{'outofs'} = 0;
   $$self->{'outlen'} = length ($$self->{'outtext'});
 }
 
  my @virt_env;

  sub TIEHASH {
    my $self = shift;
    my $org_hash = shift;
#    print STDERR "TIEHASH\n";

    my $inst = {
      LIST => {}
    };
    $inst->{LIST} = $org_hash;
  }

  sub FETCH {
    my ($self, $key) = @_;
#    print STDERR "FETCH key=[$key]\n";

    return $self->{LIST}->{$key};
  }
    
  sub STORE {
    my ($self, $key, $value) = @_;
#    print STDERR "STORE key=[$key] val=[$value]\n";

    my $mm2 = $self->{LIST};
    my %mm = %$mm2;
    $self->{LIST}->{$key} = $value;
  }
    
  sub DELETE {
    my ($self, $key) = @_;
#    print STDERR "DELETE\n";

    return delete $self->{LIST}->{$key};
  }

  sub CLEAR {
    my $self = shift;
#    print STDERR "CLEAR\n";
    foreach my $key (keys %{$self->{LIST}}) {
      $self->DELETE ($key);
    }
  }

  sub EXISTS {
    my ($self, $key) = @_;
#    print STDERR "EXISTS key=[$key]\n";
    return exists $self->{LIST}->{$key};  
  }

  sub FIRSTKEY {
    my $self = shift;
#    print STDERR "FIRSTKEY\n";
    my $a = keys %{$self->{LIST}};
    each %{$self->{LIST}};
  }

  sub NEXTKEY {
    my ($self, $lastkey) = @_;
#    print STDERR "NEXTKEY\n";
    each %{$self->{LIST}};
  }

  sub exit {
#    print STDERR "EXIT called\n";
    die "";
  }

  @virt_env = {};
  tie %ENV, 'VIRT::Embed::Persistent', @virt_env;
  tie_all ();
  *CORE::GLOBAL::exit = \&VIRT::Embed::Persistent::exit;
1;

__END__