summaryrefslogtreecommitdiff
path: root/bin/perload
blob: 7ef71373c46ea79f9724fcccdab3f6907c6e64f5 (plain)
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#! /usr/bin/perl
'/bin/true' && eval 'exec perl -S $0 "$@"'
	if $running_under_some_shell;
'di';
'ig00';

#
# This perl script is its own manual page [generated by wrapman]
#

# $Id: perload,v 3.0.1.1 1994/10/29 15:45:36 ram Exp ram $
#
#  Copyright (c) 1991-1997, 2004-2006, Raphael Manfredi
#  
#  You may redistribute only under the terms of the Artistic Licence,
#  as specified in the README file that comes with the distribution.
#  You may reuse parts of this distribution only within the terms of
#  that same Artistic Licence; a copy of which may be found at the root
#  of the source tree for dist 4.0.
#
# $Log: perload,v $
# Revision 3.0.1.1  1994/10/29  15:45:36  ram
# patch36: added minimal support for perl5 dataloading
#
# Revision 3.0  1993/08/18  12:04:06  ram
# Baseline for dist 3.0 netwide release.
#

# Replace each function definition in a loading section by two stubs and
# reject the definition into the DATA part of the script if in a dataload
# section or into a FILE if in an autoload section.

$in_load = 0;					# In a loading section
$autoload = '';					# Name of autoloaded file
$has_invocation_stub = 0;		# True if we detect a #! stub
$current_package = 'main';		# Current package
$init_emitted = 0;				# True when dataloading stamp was emitted
$in_function = 0;

require 'getopt.pl';
&Getopt;

while (<>) {
	if ($. == 1 && /^(:|#).*perl/) {	# Invocation stub
		$has_invocation_stub = 1;
		print;
		next;
	}
	if ($. <= 3 && $has_invocation_stub) {
		print;
		next;
	}
	if (/^\s*$/) {
		&flush_comment;
		print unless $in_function;
		print if $in_function && !$in_load;
		if ($in_function && $in_load) {
			push(@Data, "\n") unless $autoload;
			$Auto{$autoload} .= "\n" if $autoload;
		}
		next;
	}
	if (/^\s*;?#/) {
		if (/#\s*perload on/i) {		# Enter a loading section
			print unless /:$/;
			$in_load = 1;
			next;
		}
		if (/#\s*perload off/i) {		# End a loading section
			print unless /:$/;
			$in_load = 0;
			next;
		}
		if (/#\s*autoload (\S+)/i) {	# Enter autoloading section
			print unless /:$/;
			push(@autoload, $autoload);	# Directives may be nested
			$autoload = $1;
			$in_load += 2;
			next;
		}
		if (/#\s*offload/i) {			# End autoloading section
			print unless /:$/;
			$autoload = pop(@autoload);	# Revert to previously active file
			$in_load -= 2;
			next;
		}
		&emit_init unless $init_emitted;
		push(@Comment, $_) unless $in_function;
		print if $in_function && !$in_load;
		next unless $in_function;
		push(@Data, $_) unless $autoload;
		$Auto{$autoload} .= $_ if $autoload;
		next;
	}
	&emit_init unless $init_emitted;
	/^package (\S+)\s*;/ && ($current_package = $1);
	unless ($in_load) {
		&flush_comment;
		print;
		next;
	}
	# We are in a loading section
	if (/^sub\s+([\w']+)\s*\{(.*)/) {
		die "line $.: function $1 defined within another function.\n"
			if $in_function;
		# Silently ignore one-line functions
		if (/\}/) {
			&flush_comment;
			print;
			next;
		}
		$comment = $2;
		$in_function = 1;
		$function = $1;
		($fn_package, $fn_basename) = $function =~ /^(\w+)'(\w+)/;
		unless ($fn_package) {
			$fn_package = $current_package;
			$fn_basename = $function;
		}
		# Keep leading function comment
		foreach (@Comment) {
			push(@Data, $_) unless $autoload;
			$Auto{$autoload} .= $_ if $autoload;
		}
		@Comment = ();
		# Change package context for correct compilation: the name is visible
		# within the original function package while the body of the function
		# is compiled within the current package.
		$declaration = "sub $fn_package" . "'load_$fn_basename {$comment\n";
		$package_context = "\tpackage $current_package;\n";
		if ($autoload) {
			$Auto{$autoload} .= $declaration . $package_context;
		} else {
			push(@Data, $declaration, $package_context);
		}
		# Emit stubs
		print "sub $fn_package", "'$fn_basename";
		print " { &auto_$fn_package", "'$fn_basename; }\n";
		print "sub auto_$fn_package", "'$fn_basename { ";
		print '&main\'dataload' unless $autoload;
		print '&main\'autoload(' . "'$autoload'" . ', @_)' if $autoload;
		print "; }\n";
		next;
	}
	unless ($in_function) {
		&flush_comment;
		print;
		next;
	}
	# We are in a loading section and inside a function body
	push(@Data, $_) unless $autoload;
	$Auto{$autoload} .= $_ if $autoload;
	$in_function = 0 if /^\}/;
	if (/^\}/) {
		push(@Data, "\n") unless $autoload;
		$Auto{$autoload} .= "\n" if $autoload;
	}
}

@auto = keys %Auto;
if (@auto > 0) {
	print &q(<<'EOC');
:# Load the calling function from file and call it. This function is called
:# only once per file to be loaded.
:sub main'autoload {
:	local($__file__) = shift(@_);
:	local($__packname__) = (caller(1))[3];
:	$__packname__ =~ s/::/'/;
:	local($__rpackname__) = $__packname__;
:	local($__saved__) = $@;
:	$__rpackname__ =~ s/^auto_//;
:	&perload'load_from_file($__file__);
:	$__rpackname__ =~ s/'/'load_/;
:	$@ = $__saved__;		# Restore value $@ had on entrance
:	&$__rpackname__(@_);	# Call newly loaded function
:}
:
:# Load file and compile it, substituing the second stub function with the
:# loaded ones. Location of the file uses the @AUTO array.
:sub perload'load_from_file {
:	package perload;
:	local($file) = @_;				# File to be loaded
:	local($body) = ' ' x 1024;		# Pre-extent
:	local($load) = ' ' x 256;		# Loading operations
:	# Avoid side effects by protecting special variables which will be
:	# changed by the autoloading operation.
:	local($., $_, $@);
:	$body = '';
:	$load = '';
:	&init_auto unless defined(@'AUTO);	# Make sure we have a suitable @AUTO
:	&locate_file unless -f "$file";		# Locate file if relative path
:	open(FILE, $file) ||
:		die "Can't load $'__rpackname__ from $file: $!\n";
:	while (<FILE>) {
:		$load .= '*auto_' . $1 . '\'' . $2 . '= *' . $1 . '\'' . "load_$2;\n"
:			if (/^sub\s+(\w+)'load_(\w+)\s*\{/);
:		$body .= $_;
:	}
:	close FILE;
EOC
	if ($opt_t) {
		print &q(<<'EOC');
:	# Untaint body when running setuid
:	$body =~ /^([^\0]*)/;
:	# No need to untaint $load, as it was built using trusted variables
:	eval $1 . $load;
EOC
	} else {
		print &q(<<'EOC');
:	eval $body . $load;
EOC
	}
	print &q(<<'EOC');
:	chop($@) && die "$@, while parsing code of $file.\n";
:}
:
:# Initialize the @AUTO array. Attempt defining it by using the AUTOLIB
:# environment variable if set, otherwise look in auto/ first, then in the
:# current directory.
:sub perload'init_auto {
:	if (defined $ENV{'AUTOLIB'} && $ENV{'AUTOLIB'}) {
:		@AUTO = split(':', $ENV{'AUTOLIB'});
:	} else {
:		@AUTO = ('auto', '.');
:	}
:}
:
:# Locate to-be-loaded file held in $file by looking through the @AUTO array.
:# This variable, defined in 'load_from_file', is modified as a side effect.
:sub perload'locate_file {
:	package perload;
:	local($fullpath);
:	foreach $dir (@'AUTO) {
:		$fullpath = $dir . '/' . $file;
:		last if -f "$fullpath";
:		$fullpath = '';
:	}
:	$file = $fullpath if $fullpath;		# Update var from 'load_from_file'
:}
:
EOC
}

if (@Data > 0) {
	print &q(<<'EOC');
:# Load the calling function from DATA segment and call it. This function is
:# called only once per routine to be loaded.
:sub main'dataload {
:	local($__packname__) = (caller(1))[3];
:	$__packname__ =~ s/::/'/;
:	local($__rpackname__) = $__packname__;
:	local($__at__) = $@;
:	$__rpackname__ =~ s/^auto_//;
:	&perload'load_from_data($__rpackname__);
:	local($__fun__) = "$__rpackname__";
:	$__fun__ =~ s/'/'load_/;
:	eval "*$__packname__ = *$__fun__;";	# Change symbol table entry
:	die $@ if $@;		# Should not happen
:	$@ = $__at__;		# Restore value $@ had on entrance
:	&$__fun__;			# Call newly loaded function
:}
:
:# Load function name given as argument, fatal error if not existent
:sub perload'load_from_data {
:	package perload;
:	local($pos) = $Datapos{$_[0]};			# Offset within DATA
:	# Avoid side effects by protecting special variables which will be changed
:	# by the dataloading operation.
:	local($., $_, $@);
:	$pos = &fetch_function_code unless $pos;
:	die "Function $_[0] not found in data section.\n" unless $pos;
:	die "Cannot seek to $pos into data section.\n"
:		unless seek(main'DATA, $pos, 0);
:	local($/) = "\n}";
:	local($body) = scalar(<main'DATA>);
:	die "End of file found while loading $_[0].\n" unless $body =~ /^\}$/m;
EOC
	if ($opt_t) {
		print &q(<<'EOC');
:	# Untaint body when running setuid
:	$body =~ /^([^\0]*)/;
:	# Now we may safely eval it without getting an insecure dependency
:	eval $1;		# Load function into perl space
EOC
	} else {
		print &q(<<'EOC');
:	eval $body;		# Load function into perl space
EOC
	}
	print &q(<<'EOC');
:	chop($@) && die "$@, while parsing code of $_[0].\n";
:}
:
EOC
	print &q(<<'EOC') unless $opt_o;
:# Parse text after the END token and record defined loadable functions (i.e.
:# those whose name starts with load_) into the %Datapos array. Such function
:# definitions must be left adjusted. Stop as soon as the function we want
:# has been found.
:sub perload'fetch_function_code {
:	package perload;
:	local($pos) = tell main'DATA;
:	local($in_function) = 0;
:	local($func_name);
:	local($., $_);
:	while (<main'DATA>) {
:		if (/^sub\s+(\w+)'load_(\w+)\s*\{/) {
:			die "DATA line $.: function $1'$2 defined within $func_name.\n"
:				if $in_function;
:			$func_name = $1 . '\'' . $2;
:			$Datapos{$func_name} = $pos;
:			$in_function = 1;
:			next;
:		}
:		$in_function = 0 if /^\}/;
:		next if $in_function;
:		return $pos if $func_name eq $_[0];
:		$pos = tell main'DATA;
:	}
:	0;		# Function not found
:}
:
EOC
	print &q(<<'EOC') if $opt_o;
:# This function is called only once, and fills in the %Datapos array with
:# the offset of each of the dataloaded routines held in the data section.
:sub perload'fetch_function_code {
:	package perload;
:	local($start) = 0;
:	local($., $_);
:	while (<main'DATA>) {			# First move to start of offset table
:		next if /^#/;
:		last if /^$/ && ++$start > 2;	# Skip two blank line after end token
:	}
:	$start = tell(main'DATA);		# Offsets in table are relative to here
:	local($key, $value);
:	while (<main'DATA>) {			# Load the offset table
:		last if /^$/;				# Ends with a single blank line
:		($key, $value) = split(' ');
:		$Datapos{$key} = $value + $start;
:	}
:	$Datapos{$_[0]};		# All that pain to get this offset...
:}
:
EOC
	print &q(<<'EOC');
:#
:# The perl compiler stops here.
:#
:
:__END__
:
:#
:# Beyond this point lie functions we may never compile.
:#
:
EOC
	# Option -o directs us to optimize the function location by emitting an
	# offset table, which lists all the position within DATA for each possible
	# dataloaded routine.
	if ($opt_o) {
		print &q(<<'EOC');
:#
:# DO NOT CHANGE A IOTA BEYOND THIS COMMENT!
:# The following table lists offsets of functions within the data section.
:# Should modifications be needed, change original code and rerun perload
:# with the -o option to regenerate a proper offset table.
:#
:
EOC
		$trailing_message = &q(<<'EOC');
:
:#
:# End of offset table and beginning of dataloading section.
:#
:
EOC
		$pos = 0;			# Offset relative to this point (start of table)
		foreach (@Data) {
			$Datapos{"$1\'$2"} = $pos - $now
				if /^sub\s+(\w+)'load_(\w+)\s*\{/;	# } for vi
			$pos += length;
		}
		@poskeys = keys %Datapos;	# Array of routine names (fully qualified)

		# Write out a formatted table, each entry stored on $entry bytes and
		# formatted with the $format string.
		($entry, $format) = &get_format(*poskeys);

		# The total size occupied by the table is the size of one item times
		# the number of items plus the final trailing message at the end of
		# the table.
		$table_size = $entry * @poskeys + length($trailing_message);

		# Output formatted table
		foreach (sort @poskeys) {
			printf($format, $_, $table_size + $Datapos{$_});
		}
		print $trailing_message;
	}

	# Output code for each dataloaded function
	foreach (@Data) {
		print;
	}
	print &q(<<'EOC');
:#
:# End of dataloading section.
:#
:
EOC
}

if (@auto > 0) {
	mkdir('auto',0755) unless -d 'auto';
	foreach $file (@auto) {
		unless (open(AUTO, ">auto/$file")) {
			warn "Can't create auto/$file: $!\n";
			next;
		}
		print AUTO &q(<<'EOC');
:# This file was generated by perload
:
EOC
		print AUTO $Auto{$file};
		close AUTO;
	}
}

# Compute optimum format for routine offset table, returning both the size of
# each entry and the formating string for printf.
sub get_format {
	local(*names) = @_;
	local($name_len) = 0;
	local($max_len) = 0;
	foreach (@names) {
		$name_len = length;
		$max_len = $name_len if $name_len > $max_len;
	}
	# The size of each entry (preceded by one tab, followed by 12 chars)
	$name_len = $max_len + 1 + 12;
	($name_len, "\t%${max_len}s %10d\n");
}

sub emit_init {
	print &q(<<'EOC');
:#
:# This perl program uses dynamic loading [generated by perload]
:#
:
:$ENV{LC_ALL} = 'C';
:
EOC
	$init_emitted = 1;
}

sub flush_comment {
	print @Comment if @Comment > 0;
	@Comment = ();
}

sub q {
	local($_) = @_;
	s/^://gm;
	$_;
}

#
# These next few lines are legal in both perl and nroff.
#

.00;		# finish .ig
 
'di			\" finish diversion--previous line must be blank
.nr nl 0-1	\" fake up transition to first page again
.nr % 0		\" start at page 1
'; __END__	\" the perl compiler stops here

'''
''' From here on it's a standard manual page.
'''

.TH PERLOAD 1 "June 20, 1992"
.AT 3
.SH NAME
perload \- builds up autoloaded and dataloaded perl scripts
.SH SYNOPSIS
.B perload
[ \fB\-ot\fR ]
[ \fIfile\fR ]
.SH DESCRIPTION
.I Perload
takes a perl script as argument (or from stdin if no argument is supplied)
and prints out on stdout an equivalent script set-up to perform autoloading
or dataloading. The translation is directed by special comments within the
original script. Using dynamic loading can drastically improve start-up
performances, both in time and in memory, as perl does not need to compile
the whole script nor store its whole compiled form in memory.
.PP
.I Autoloading
delays compilation of some functions until they are needed. The code for these
functions is loaded dynamically at run-time. The atomicity of loading is a
file, which means that putting more than one function into a file will cause
all these functions to be loaded and compiled as soon as one among them is
needed.
.PP
.I Dataloading
is a form of autoloading where no extra file are needed. The script carries
all the functions whose compilation is to be delayed in its data segment
(in the \fIperl\fR sense, i.e. they are accessible via the DATA filehandle).
The scripts parses the data segment and extracts only the code for the needed
subroutine, which means granularity is better than with autloading.
.PP
It is possible for a single script to use both autoloading and dataloading at
the same time. However, it should be noted that a script using only dataloading
is self contained and can be moved or shared accross different platforms without
fear. On the contrary, a script using only autoloading relies on some externally
provided files. Sharing this script among different platforms requires sharing
of these external files. The script itself cannot be redistributed without
also giving the extra files holding the autoloaded functions.
.PP
The major drawback with dataloading is that the DATA filehandle cannot be used
for anything else and may result in code duplication when two scripts could
share the same pieces of code. Autoloading appears as the perfect solution in
this case since two scripts may freely share the same functions without
actually duplicating them on the disk (hence saving some precious disk blocks
:-).
.SH CRITERIA
Functions to be dataloaded or autoloaded must meet the following layout
criteria:
.TP 5
\-
They must not be one-line functions like \fIsub sorter { $a <=> $b }\fR.
Those functions are simply output verbatim, as they are already so
small that it would not be worth to dynamically load them,
.TP
\-
The first line must be of the form \fIsub routine_name {\fR, with an optional
comment allowed after the '{'.
.TP
\-
The function definition must end with a single '}' character left aligned.
.TP
\-
Package directives outside any function must be left aligned.
.PP
All the above restrictions should not be source of a problem if "standard"
writing style is used. There are also some name restrictions: the package
name \fIperload\fR is reserved, as is the \fI@AUTO\fR array when autoloading
is used. Packages must not start with \fIauto_\fR, as this is prepended to
user's package names when building the stubs. Furthermore, the subroutines
names \fImain'autoload\fR and
\fImain'dataload\fR must not be used by the original script. Again, these
should not cause any grief.
.SH DIRECTIVES
The translation performed by
.I Perload
is driven by some special comment directives placed directly within the code.
Ending those directives with a ':' character will actually prevent them from
being output into the produced script. Case is irrelevant for all the directives
and the comment need not be left-aligned, although it must be the first
non-space item on the line.
.PP
The following directives are available:
.TP 10
# Perload ON
Turns on the \fIperload\fR processing. Any function definition which meets
the criteria listed in the previous section will be replaced by two stubs and
its actual definition will be rejected into the data segment (default) or a
file when inside an autoloading section.
.TP
# Perload OFF
Turns off any processing. The script is written as-is on the standard output.
.TP
# Autoload \fIpath\fR
Requests autoloading from file \fIpath\fR, which may be an absolute path or
a relative path. The file will be located at run-time using the @AUTO array
if a non-absolute path is supplied or if the file does not exist as listed.
Autoloading directives may be nested.
.TP
# Offload \fIpath\fR
The argument is not required. The directive ends the previous autoloading
directive (the inmost one). This does not turn off the \fIperload\fR processing
though. The \fIpath\fR name is optional here (in fact, it has only a comment
value).
.SH OPTIONS
Perload accepts only two options. Using \fB\-o\fR is meaningful only when
dataloading is used. It outputs an offset table which lists the relative
offset of the dataloaded functions within the data section. This will spare
perl the run-time parsing needed to locate the function, and results in an good
speed gain. However, it has one major drawback: it prevents people from
actually modifying the source beyond the start of the table. But anything
before can be freely edited, which is particulary useful when tailoring the
script.
.PP
This option should not be used when editing of functions within the data
section is necessary for whatever reason. When \fB\-o\fR is used, any
change in the dataloaded function must be committed by re-running perload
on the original script.
.PP
The other option \fB\-t\fR is to be used when producing a script which is
going to run setuid. The body of the loaded function is untainted before being
fed to eval, which slightly slows down loading (the first time the function is
called), but avoids either an insecure dependency report or weird warnings from
taintperl stating something is wrong (which is the behaviour with 4.0 PL35).
.SH FILES
.TP 10
auto
the subdirectory where all produced autoloaded files are written.
.SH ENVIRONMENT
No environment variables are used by \fIperload\fR. However, the autoloaded
version of the script pays attention to the \fIAUTOLIB\fR variable as a colon
separated set of directories where the to-be-loaded files are to be found
when a non-absolute path was specified. If the \fIAUTOLIB\fR variable is not
set, the default value 'auto:.' is used (i.e. look first in the auto/
subdirectory, then in the current directory.
.SH CAVEAT
Special care is required when using an autoloading script, especially when
executed by the super-user: it would be very easy for someone to leave a
special version of a routine to be loaded, in the hope the super-user (or
another suitable target) executes the autoloaded version of the script with
some \fIad hoc\fR changes...
.PP
The directory holding the to-be-loaded files should therefore be protected
against unauthorized access, and no file should have write permission on them.
The directory itself should not be world-writable either, or someone might
substitute his own version.
It should also be considered wise to manually set the @AUTO variable to a
suitable value within the script itself.
.PP
The \fB\-o\fR option uses \fIperl\fR's special variable \fI$/\fR with a
multi-character value. I suspect this did not work with versions of \fIperl\fR
prior to 4.0, so any script using this optimized form of dataloading will not
be 100% backward compatible.
.SH AUTHOR
Raphael Manfredi <ram@acri.fr>
.SH CREDITS
Valuable input came from Wayne H. Scott <wscott@ecn.purdue.edu>. He is
merely the author of the optimizing offset table (\fB\-o\fR option).
.PP
.I Perload
is based on an article from Tom Christiansen <tchrist@convex.com>,
.I Autoloading in Perl,
explaining the concept of dataloading and giving a basic implementation.
.SH "SEE ALSO"
perl(1).