#!/usr/bin/perl # This is a cgi script in perl. # # It creates HTML files related to the number googolplex written out in # book form. # # (all typed...) # # Wolfgang Hartmut Nitsche # http://www.stanford.edu/~nitsche/ # http://nitsche.mobi # http://www.GoogolplexWrittenOut.com ########################################################### ########################################################### ## ## ## I N I T I A L P A R T O F T H I S S C R I P T ## ## ## ########################################################### ########################################################### $query = $ENV{'QUERY_STRING'}; # The query string $filename = $ENV{'SCRIPT_FILENAME'}; # This is the file-name of this perl script, for # example $filename = 'makehtml.pl' # Show the source code of this perl script, if requested: if($query eq 'source') { $sourcefile = ''; open(FILE, "<$filename"); while() { $sourcefile .= $_; } close(FILE); print "Content-type: text/plain\n\n"; print $sourcefile; exit; } # The number of the last volume of the books: $voltotal = "1" . ("0" x 94); # Use the query string might: $what = ''; $volnow = ''; %queryhash = (); @querypairs = split(/&/, $query); foreach $querysegment (@querypairs) { ($key, $value) = split(/=/, $querysegment); $key =~ tr/+/ /; $key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; if( ($key eq 'goto') or ($key eq 'summary') or ($key eq 'list') ) { $what = $key; $volnow = $value; } } # $what should be 'goto' or 'summary' or 'list' or '' # in case of 'goto', we force $volnow to become a valid number: if($what eq 'goto') { $volnow =~ s/[^0-9]//g; # remove everything except numbers $volnow =~ s/^0+//g; # remove all leading zeros if( $volnow eq '' ) { $volnow = "1"; } elsif( (length($volnow)>length($voltotal)) or ((length($volnow)==length($voltotal)) and ($volnow gt $voltotal)) ) { $volnow = $voltotal; } } # get rid of stupid characters which might cause problems: $volnow =~ s/[^\!-\~]/\?/g; $volnow =~ s/[\<\>\&]/\?/g; # Check if $volnow is a valid volume number: $errormessage = ''; if( $volnow eq '' ) { $errormessage = 'requested volume number is an empty string'; } elsif( $volnow =~ /[^0-9]/ ) { $errormessage = 'requested volume number contains non-numeric characters'; } elsif( substr($volnow, 0, 1) eq '0' ) { $errormessage = 'requested volume number starts with leading zero'; } elsif( length($volnow) > length($voltotal) ) { $errormessage = 'requested volume number is longer and therefore larger than '.$voltotal; } elsif( (length($volnow) == length($voltotal)) and ($volnow gt $voltotal) ) { $errormessage = 'requested volume number is of equal length but larger than '.$voltotal; } # Check if $what has a valid value: if( $what eq '' ) { $errormessage = 'Apparently the request does not contain a valid query string, and no volume number can be extracted.'; } # in case of 'list', check if $volnow which is the last requested volume in the list ends with 000: if( ($what eq 'list') and (substr('xxxx'.$volnow, -3, 3) ne '000') ) { $errormessage = 'The request last volume in the list does not end with 000.'; } if($errormessage ne '') { print "Content-type: text/html\n"; print "Status: 400 Bad Request\n"; print "\n"; print ''."\n"; print "\n"; print "\n"; print "error (400 Bad Request)\n"; print ''."\n"; print ''."\n"; print "\n"; print "\n"; print "

error (400 Bad Request)

\n"; print "

The volume which you requested does not exist.

\n"; print "

\n"; print "

Valid volume numbers nust be a number from 1 to $voltotal\n"; print "and must be written without leading zeros\n"; print "and without any non-digit characters.

\n"; print "

\n"; print "Your requested volume number was
\n"; print "$volnow
\n"; print "which caused the following problem:
\n"; print "$errormessage\n"; print "

\n"; print '

Please go back to http://www.GoogolplexWrittenOut.com

'."\n"; print "\n"; print "\n"; exit; } elsif($what eq 'goto') { $location = 'http://www.GoogolplexWrittenOut.com/volume'. $volnow . '.htm'; print "Status: 302 Found\n"; print "Location: $location\n"; print "Content-type: text/html\n"; print "\n"; print ''."\n"; print ''."\n"; print "\n"; print "\n"; print "302 Found\n"; print ''."\n"; print "\n"; print "\n"; print "

302 Found

\n"; print "

If you are not redirected automatically, please click the link below:
\n"; print qq{$location\n}; print "

\n"; print "\n"; print "\n"; exit; } elsif($what eq 'summary') { $volnext = ''; if($volnow ne $voltotal) { @digits = split(//, '0'.$volnow); $digits[-1]++; for($idigits=$#digits; $idigits>=0; $idigits--) { if(10==$digits[$idigits]) { $digits[$idigits] = 0; $digits[$idigits-1]++; } } $volnext = join('', @digits); $volnext =~ s/^0+//g; # remove all leading zeros } $volprevious = ''; if($volnow ne '1') { @digits = split(//, '0'.$volnow); $digits[-1]--; for($idigits=$#digits; $idigits>=0; $idigits--) { if(-1==$digits[$idigits]) { $digits[$idigits] = 9; $digits[$idigits-1]--; } } $volprevious = join('', @digits); $volprevious =~ s/^0+//g; # remove all leading zeros } print "Content-type: text/html\n"; print "\n"; print ''."\n"; print "\n"; print "\n"; print "Googolplex Written Out, volume $volnow\n"; print ''."\n"; print '' . "\n"; print ''."\n"; print ''."\n"; print ''."\n"; print "\n"; print qq{\n}; print "

Googolplex Written Out, volume $volnow

\n"; print "

Free download

\n"; print "

Use the following link to download this free book in PDF form:
\n"; print qq{volume$volnow.pdf [PDF file]\n}; print "

\n"; print "

Print edition

\n"; print "

This book is also available as a printed book.\n"; print "The printed books are sold by the company Peecho.\n"; print "The author of Googolplex Written Out has nothing to do with Peecho, does not get any money from them, and is not responsible for any problems which might arise with your order.\n"; print qq{Instead of buying it from Peecho, you can also download exactly the same book for free as a PDF file.\n}; print "
\n"; print qq{Buy printed book from Peecho

\n}; print "
\n"; print qq{

\n}; if($volnow ne '1') { print qq{}; } print qq{<<first volume|}; if($volnow ne '1') { print qq{}; } print qq{\n}; if($volprevious ne '') { print qq{}; } print qq{<previous volume|}; if($volprevious ne '') { print qq{}; } print qq{\n}; print qq{[current volume]\n}; if($volnext ne '') { print qq{}; } print qq{|next volume>}; if($volnext ne '') { print qq{}; } print qq{\n}; if($volnow ne $voltotal) { print qq{}; } print qq{|last volume>>}; if($volnow ne $voltotal) { print qq{}; } print qq{\n}; print qq{

\n}; print qq{
\n}; print qq{Go to volume\n}; print qq{\n}; print qq{\n}; print qq{
\n}; print '

Start page: http://www.GoogolplexWrittenOut.com

'."\n"; print "\n"; print "\n"; exit; } elsif($what eq 'list') { $listnumbernow = substr($volnow, 0, length($volnow)-3); $listnumbernext = ''; if($listnumbernow.'000' ne $voltotal) { @digits = split(//, '0'.$listnumbernow); $digits[-1]++; for($idigits=$#digits; $idigits>=0; $idigits--) { if(10==$digits[$idigits]) { $digits[$idigits] = 0; $digits[$idigits-1]++; } } $listnumbernext = join('', @digits); $listnumbernext =~ s/^0+//g; # remove all leading zeros } $listnumberprevious = ''; if($listnumbernow ne '1') { @digits = split(//, '0'.$listnumbernow); $digits[-1]--; for($idigits=$#digits; $idigits>=0; $idigits--) { if(-1==$digits[$idigits]) { $digits[$idigits] = 9; $digits[$idigits-1]--; } } $listnumberprevious = join('', @digits); $listnumberprevious =~ s/^0+//g; # remove all leading zeros } print "Content-type: text/html\n"; print "\n"; print ''."\n"; print "\n"; print "\n"; print "Googolplex Written Out, list $listnumbernow of volumes\n"; print ''."\n"; print '' . "\n"; print ''."\n"; print ''."\n"; print ''."\n"; print "\n"; print qq{\n}; print "

Googolplex Written Out, list $listnumbernow of volumes

\n"; print "
\n"; if($listnumberprevious ne '') { print '

< show previous 1000 volumes ]

' . "\n"; } print "\n"; if($listnumbernext ne '') { print '

[ show next 1000 volumes >

' . "\n"; } print "
\n"; print '

Start page: http://www.GoogolplexWrittenOut.com

'."\n"; print "\n"; print "\n"; exit; } ################################# ################################# ## ## ## E N D O F P R O G R A M ## ## ## ################################# #################################