⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 deadbeat.pl

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 PL
字号:
#!/usr/bin/perl -w

#*____________________________________________________________________________*\
#*
#
# 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 STDOUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
#
#*____________________________________________________________________________*|
#*
#* $Source: /cvsroot/pi3web/Pi3Web_200/Source/DeadBeat/deadbeat.pl,v $
#* $Date: 2003/05/13 18:41:56 $
#*
# Description:
#	Simple server-side javascript compiler.
#
# Parameters
#	deadbeat [-c|v|V|d|?] [-o outfile.web]
#		[script1.html ...scriptn.html]
#		[funct1.js ...functn.js]
#
#*____________________________________________________________________________*/

#/*___________________________________________________________________________*\
# Global declarations:
#\*___________________________________________________________________________*/
$verbose = 0;
$Verbose = 0;
$display_js = 0;
$outfile = '';
$syntax_only = 0;
$file_found = 0;


#/*___________________________________________________________________________*\
# *
# Function:
# Synopsis:
# Description:
#	Globals common to file, line and global state tracking.
#\*___________________________________________________________________________*/
$current_filename = '';
$current_linenumber = 0;
# 
# Global state
#	0 = in html section
#	1 = in js section
#
$global_state = 0;
#
# Data
#
$html_data = '';
$js_data = '';

#
# Records
#
%Web = {} ;		# The webfile
%CFile = {} ;	# Current file

#
# Execute program
#
&do_main( @ARGV );

#/*___________________________________________________________________________*\
# *
# Function:
# Synopsis:
# Description:
#\*___________________________________________________________________________*/
sub usage
	{
	local( $stream ) = shift;
	print $stream "deadbeat.pl: [-c|v|V|d|?] [-o outfile.web]\n\t";
	print $stream "[script1.html ...scriptn.html]\n\t";
	print $stream "[funct1.js ...functn.js]\n";
	print $stream "Arguments:\n";
	print $stream "\t-o: Specify output file.\n";
	print $stream "\t-c: Syntax check only.\n";
	print $stream "\t-v: Verbose output.\n";
	print $stream "\t-V: Really verbose output.\n";
	print $stream "\t-d: Display Javascript.\n";
	print $stream "\t-?: Show this usage.\n";
	print $stream "\t-o: Specify output file.\n";
	}

#/*___________________________________________________________________________*\
# *
# Function:
# Synopsis:
# Description:
#	Main function, check command line options and compile each file.
#\*___________________________________________________________________________*/
sub do_main
	{
	local( $state ) = 0;
	foreach( @_ )
		{
		if ( $state == 0 && /-.*/ )	#	Argument
			{
			for( $_ )
				{
				/-v/			&& do { $verbose = 1; last; };
				/-V/			&& do { $verbose = 1; $Verbose = 1; last; };
				/-o/			&& do { $state = 1; last; };
				/-d/			&& do { $display_js = 1; last; };
				/-c/			&& do { $syntax_only = 1; last; };
				/-\?/			&& do { &usage( STDOUT ); exit 0 ; };
				print STDERR "deadbeat.pl: Unknown argument \'$_\'.\n";
				&usage( STDERR );	
				exit -1 ;
				};
			}
		elsif ( $state == 1 )	# read output file
			{
			$outfile = $_;
			$state = 0;
			}
		else	
			{
			if ( $outfile=~// )
				{
				print STDERR "deadbeat.pl: No output file (use -o STDOUTFILE).\n";
				&usage( STDERR );	
				exit -1;
				};
			#
			# Otherwise this is a file
			#
			$file_found = 1 if not $file_found;
			last if not &compile_file( $_ );		# abort on error
			};
		};
	if ( not $file_found )
		{
		print STDERR "deadbeat.pl: No input.\n";
		&usage( STDERR );	
		exit -1;
		};

	#
	# Fixup lengths and write webfile, if this is not a
	# syntax check only
	#
	if ( not $syntax_only )
		{
		&fixup_webfile();
		&write_webfile();
		};
	};

#/*___________________________________________________________________________*\
# *
# Function:
# Synopsis:
# Description:
#	Returns 0 to abort compilation.
#\*___________________________________________________________________________*/
sub compile_file
	{
	local( $file ) = @_;
	open( F, '<'.$file ) or die "Could not open file \'$file\'.\n";
	print STDOUT "Compiling file \'$file\'...\n" if $verbose;
	if ( not &new_file( $file ) )
		{
		close( F );
		return 0;
		};
	while( <F> )	
		{
		if ( not &new_line( $_ ) or not &process_line( $_ ) )
			{
			close( F );
			return 0;
			};
		};
	&flush_file();
	close( F );
	return 1;
	}

#/*___________________________________________________________________________*\
# *
# Function:
# Synopsis:
# Description:
#	Returns 0 to abort compilation.
#\*___________________________________________________________________________*/
sub new_file
	{
	local( $file ) = @_;
	$current_filename = $file;
	$current_linenumber = 0;
	%CFile = {};
	$CFile{"size"}=16;		# for size field
	$CFile{"path"}=$file;
	$CFile{"header_size"}=length($file)+16+16;
	print STDOUT "Opened file \'$file\'.\n" if $verbose;

	#
	# start html section
	#
	&new_html();
	return 1;
	}

#/*___________________________________________________________________________*\
# *
# Function:
# Synopsis:
# Description:
#	Returns 0 to abort compilation.
#\*___________________________________________________________________________*/
sub new_line
	{
	local( $line ) = @_;
	$current_linenumber++;
#	print STDOUT "$current_linenumber." if $verbose;
	return 1;
	}

#/*___________________________________________________________________________*\
# *
# Function:
# Synopsis:
# Description:
#	Flush file record
#\*___________________________________________________________________________*/
sub flush_file
	{
	#
	# flush last section if any
	#
	&flush_section();

	#
	# Flush old file
	#
	return if ( scalar keys %CFile == 0 );
	print STDOUT "Adding file \'$CFile{path}\', size=$CFile{size}.\n"
		if $Verbose;
	push @{ $Web{"files"} }, { %CFile };
	}

#/*___________________________________________________________________________*\
# *
# Function:
# Synopsis:
# Description:
#	Section old section data in File record
#\*___________________________________________________________________________*/
sub flush_section
	{
	local( %Section );

	#
	# Flush old section
	#
	if ( $global_state==0 )
		{
		#
		# HTML
		#
		return if length($html_data)==0;
		$Section{"type"}=0;
		$Section{"data"}=$html_data;
		}
	else
		{
		#
		# JS
		#
		return if length($js_data)==0;
		$Section{"type"}=1;
		$Section{"data"}=$js_data;
		};

	$Section{"size"} = length( $Section{"data"} );
	push @{ $CFile{"sections"} }, { %Section };
	print STDOUT "Adding section type=$Section{type}, length=".length($Section{data}).".\n"
		if $Verbose;
	$CFile{"size"} += $Section{"size"};
	}

#/*___________________________________________________________________________*\
# *
# Function:
# Synopsis:
# Description:
#	Start a new HTML record
#\*___________________________________________________________________________*/
sub new_html
	{
	#
	# Flush old section
	#
	&flush_section();

	$global_state = 0;		#	HTML
	$html_data = '';
	}

#/*___________________________________________________________________________*\
# *
# Function:
# Synopsis:
# Description:
#	Start a new JS record
#\*___________________________________________________________________________*/
sub new_js
	{
	#
	# Flush old section
	#
	&flush_section();
	
	#
	$global_state = 1;		#	JS
	$js_data = '';
	}

#/*___________________________________________________________________________*\
# *
# Function:
# Synopsis:
# Description:
#	Returns 0 to abort compilation.
#\*___________________________________________________________________________*/
sub process_line
	{
	local( $line ) = @_;
	for( $global_state )
		{
		/0/			and do { return 0 if not &html_input( $line ); last; };
		/1/			and do { return 0 if not &js_input( $line ); last };
		};
	return 1;
	}

#/*___________________________________________________________________________*\
# *
# Function:
# Synopsis:
# Description:
#	Returns 0 to abort compilation.
#\*___________________________________________________________________________*/
sub html_input
	{
	local( $line ) = @_;
	local( $_ ) = $line;
	#
	# scan for start of Server section
	#
	if ( /.*<SERVER.*>.*/i ) 
		{
		s/(.*)<SERVER.*>.*/$1/i;
		&add_html( $_ );
		$_ = $line;
		s/.*<SERVER.*>(.*)/$1/i;
		local( $tmp ) = $_;
		#
		# Start a js section
		#
		&new_js();
		&js_input( $tmp );
		}
	else
		{
		&add_html( $_ );
		};
	return 1;
	}

#/*___________________________________________________________________________*\
# *
# Function:
# Synopsis:
# Description:
#	Returns 0 to abort compilation.
#\*___________________________________________________________________________*/
sub js_input
	{
	local( $line ) = @_;
	local( $_ ) = $line;
	#
	# scan for end of Server section
	#
	if ( /.*<\/SERVER.*>.*/i ) 
		{
		s/(.*)<\/SERVER.*>.*/$1/i;
		&add_js( $_ );
		$_ = $line;
		s/.*<\/SERVER.*>(.*)/$1/i;
		local( $tmp ) = $_;
		#
		# Start an html section
		#
		&new_html();
		&html_input( $tmp );
		}
	else
		{
		&add_js( $_ );
		};
	return 1;
	}

#/*___________________________________________________________________________*\
# *
# Function:
# Synopsis:
# Description:
#\*___________________________________________________________________________*/
sub add_html
	{
	local( $html ) = @_;

	print STDOUT "HTML[$current_linenumber]: $html" if $Verbose;
	$html_data = $html_data.$html;
	return 1;
	};

#/*___________________________________________________________________________*\
# *
# Function:
# Synopsis:
# Description:
#\*___________________________________________________________________________*/
sub add_js
	{
	local( $js ) = @_;

	print STDOUT "JS[$current_linenumber]: $js" if $Verbose;
	$js_data = $js_data.$js;
	return 1;
	};

#/*___________________________________________________________________________*\
# *
# Function:
# Synopsis:
# Description:
#	Fixup the data record lengths within a webfile
#\*___________________________________________________________________________*/
sub fixup_webfile
	{	
	local( $size ) = 16+16;
	
	#
	# Loop over all files, fixup file header records
	#
	foreach( @{ $Web{"files"} } )
		{
		%File = %{ $_ };
		$size += $File{"header_size"};
		};

	$Web{"size"}=$size;

	#
	# Go through all files again, fixup length of file records and offsets
	# in file header records
	#
	foreach( @{ $Web{"files"} } )
		{
		%File = %{ $_ };

		#
		# set offset of file
		#
#		${ %{ File{"offset"} = $size;
		${%{$_}}{"offset"} = $size;

		#
		# add size of record
		#
		$size += $File{"size"};
		};

	# remember total length (for check)
	$Web{"total_size"} = $size;
	};

#/*___________________________________________________________________________*\
# *
# Function:
# Synopsis:
# Description:
#	Write out the webfile
#\*___________________________________________________________________________*/
sub write_webfile
	{	
	open( OUT, '>'.$outfile ) || die
		"Could not open output file \'$outfile\'\n" ;
	
	# Write header stamp
	print OUT "DeadBeat_Ver_10" ;
	
	# Write header record size
	printf( OUT "%16d", $Web{"size"} );

	#
	# Write each header file record
	#
	foreach( @{ $Web{"files"} } )
		{
		local( %File ) = %{ $_ };
		printf( OUT "%16d", $File{"header_size"} );
		printf( OUT "%16d", $File{"offset"} );
		printf( OUT "%s", $File{"path"} );
		};

	#
	# Write each file record
	#
	foreach( @{ $Web{"files"} } )
		{
		local( %File ) = %{ $_ };
		printf( OUT "%16d", $File{"size"} );
		#
		# Write sections
		#
		for( @{ $File{"sections"} } )
			{
			local( %Section ) = %{ $_ };
			printf( OUT "%16d", $Section{"size"} );
			printf( OUT "%16d", $Section{"type"} );
			printf( OUT "%s", $Section{"data"} );
			};
		};
	
	close( OUT );
	};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -