From http://www.jwz.org/xscreensaver/xscreensaver-5.30.tar.gz
[xscreensaver] / driver / xscreensaver-text
index 4960295634d7f437e9b945783e77f15ab449e097..6162b78deb9ac93b76fca20750427eb9526f0780 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/perl -w
-# Copyright © 2005 Jamie Zawinski <jwz@jwz.org>
+# Copyright © 2005-2014 Jamie Zawinski <jwz@jwz.org>
 #
 # Permission to use, copy, modify, distribute, and sell this software and its
 # documentation for any purpose is hereby granted without fee, provided that
 # .xscreensaver file.  It may load a file, a URL, run a program, or just
 # print the date.
 #
+# In a native MacOS build of xscreensaver, this script is included in
+# the Contents/Resources/ directory of each screen saver .bundle that
+# uses it; and in that case, it looks up its resources using
+# /usr/bin/defaults instead.
+#
 # Created: 19-Mar-2005.
 
 require 5;
-use diagnostics;
+#use diagnostics;      # Fails on some MacOS 10.5 systems
 use strict;
+
+# Some Linux systems don't install LWP by default!
+# Only error out if we're actually loading a URL instead of local data.
+BEGIN { eval 'use LWP::UserAgent;' }
+
 use Socket;
 use POSIX qw(strftime);
 use Text::Wrap qw(wrap);
 use bytes;
 
 my $progname = $0; $progname =~ s@.*/@@g;
-my $version = q{ $Revision: 1.7 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
+my ($version) = ('$Revision: 1.33 $' =~ m/\s(\d[.\d]+)\s/s);
 
 my $verbose = 0;
 my $http_proxy = undef;
@@ -34,77 +44,110 @@ my $text_mode     = 'date';
 my $text_literal  = '';
 my $text_file     = '';
 my $text_program  = '';
-my $text_url      = '';
+my $text_url      = 'http://en.wikipedia.org/w/index.php?title=Special:NewPages&feed=rss';
+# Default URL needs to be set and match what's in OSX/XScreenSaverView.m
 
-my $wrap_columns  = undef;
+my $wrap_columns   = undef;
+my $truncate_lines = undef;
+my $nyarlathotep_p = 0;
 
 
 # Maps HTML character entities to the corresponding Latin1 characters.
 #
 my %entity_table = (
-   "quot"   => '"', "amp"    => '&', "lt"     => '<', "gt"     => '>',
-   "nbsp"   => ' ', "iexcl"  => '¡', "cent"   => '¢', "pound"  => '£',
-   "curren" => '¤', "yen"    => '¥', "brvbar" => '¦', "sect"   => '§',
-   "uml"    => '¨', "copy"   => '©', "ordf"   => 'ª', "laquo"  => '«',
-   "not"    => '¬', "shy"    => '­', "reg"    => '®', "macr"   => '¯',
-   "deg"    => '°', "plusmn" => '±', "sup2"   => '²', "sup3"   => '³',
-   "acute"  => '´', "micro"  => 'µ', "para"   => '¶', "middot" => '·',
-   "cedil"  => '¸', "sup1"   => '¹', "ordm"   => 'º', "raquo"  => '»',
-   "frac14" => '¼', "frac12" => '½', "frac34" => '¾', "iquest" => '¿',
-   "Agrave" => 'À', "Aacute" => 'Á', "Acirc"  => 'Â', "Atilde" => 'Ã',
-   "Auml"   => 'Ä', "Aring"  => 'Å', "AElig"  => 'Æ', "Ccedil" => 'Ç',
-   "Egrave" => 'È', "Eacute" => 'É', "Ecirc"  => 'Ê', "Euml"   => 'Ë',
-   "Igrave" => 'Ì', "Iacute" => 'Í', "Icirc"  => 'Î', "Iuml"   => 'Ï',
-   "ETH"    => 'Ð', "Ntilde" => 'Ñ', "Ograve" => 'Ò', "Oacute" => 'Ó',
-   "Ocirc"  => 'Ô', "Otilde" => 'Õ', "Ouml"   => 'Ö', "times"  => '×',
-   "Oslash" => 'Ø', "Ugrave" => 'Ù', "Uacute" => 'Ú', "Ucirc"  => 'Û',
-   "Uuml"   => 'Ü', "Yacute" => 'Ý', "THORN"  => 'Þ', "szlig"  => 'ß',
-   "agrave" => 'à', "aacute" => 'á', "acirc"  => 'â', "atilde" => 'ã',
-   "auml"   => 'ä', "aring"  => 'å', "aelig"  => 'æ', "ccedil" => 'ç',
-   "egrave" => 'è', "eacute" => 'é', "ecirc"  => 'ê', "euml"   => 'ë',
-   "igrave" => 'ì', "iacute" => 'í', "icirc"  => 'î', "iuml"   => 'ï',
-   "eth"    => 'ð', "ntilde" => 'ñ', "ograve" => 'ò', "oacute" => 'ó',
-   "ocirc"  => 'ô', "otilde" => 'õ', "ouml"   => 'ö', "divide" => '÷',
-   "oslash" => 'ø', "ugrave" => 'ù', "uacute" => 'ú', "ucirc"  => 'û',
-   "uuml"   => 'ü', "yacute" => 'ý', "thorn"  => 'þ', "yuml"   => 'ÿ',
-   "apos"   => '\''
+   "quot"   => '"',    "amp"    => '&',    "lt"     => '<',
+   "gt"     => '>',    "nbsp"   => " ",    "iexcl"  => "\xA1",
+   "cent"   => "\xA2", "pound"  => "\xA3", "curren" => "\xA4",
+   "yen"    => "\xA5", "brvbar" => "\xA6", "sect"   => "\xA7",
+   "uml"    => "\xA8", "copy"   => "\xA9", "ordf"   => "\xAA",
+   "laquo"  => "\xAB", "not"    => "\xAC", "shy"    => "\xAD",
+   "reg"    => "\xAE", "macr"   => "\xAF", "deg"    => "\xB0",
+   "plusmn" => "\xB1", "sup2"   => "\xB2", "sup3"   => "\xB3",
+   "acute"  => "\xB4", "micro"  => "\xB5", "para"   => "\xB6",
+   "middot" => "\xB7", "cedil"  => "\xB8", "sup1"   => "\xB9",
+   "ordm"   => "\xBA", "raquo"  => "\xBB", "frac14" => "\xBC",
+   "frac12" => "\xBD", "frac34" => "\xBE", "iquest" => "\xBF",
+   "Agrave" => "\xC0", "Aacute" => "\xC1", "Acirc"  => "\xC2",
+   "Atilde" => "\xC3", "Auml"   => "\xC4", "Aring"  => "\xC5",
+   "AElig"  => "\xC6", "Ccedil" => "\xC7", "Egrave" => "\xC8",
+   "Eacute" => "\xC9", "Ecirc"  => "\xCA", "Euml"   => "\xCB",
+   "Igrave" => "\xCC", "Iacute" => "\xCD", "Icirc"  => "\xCE",
+   "Iuml"   => "\xCF", "ETH"    => "\xD0", "Ntilde" => "\xD1",
+   "Ograve" => "\xD2", "Oacute" => "\xD3", "Ocirc"  => "\xD4",
+   "Otilde" => "\xD5", "Ouml"   => "\xD6", "times"  => "\xD7",
+   "Oslash" => "\xD8", "Ugrave" => "\xD9", "Uacute" => "\xDA",
+   "Ucirc"  => "\xDB", "Uuml"   => "\xDC", "Yacute" => "\xDD",
+   "THORN"  => "\xDE", "szlig"  => "\xDF", "agrave" => "\xE0",
+   "aacute" => "\xE1", "acirc"  => "\xE2", "atilde" => "\xE3",
+   "auml"   => "\xE4", "aring"  => "\xE5", "aelig"  => "\xE6",
+   "ccedil" => "\xE7", "egrave" => "\xE8", "eacute" => "\xE9",
+   "ecirc"  => "\xEA", "euml"   => "\xEB", "igrave" => "\xEC",
+   "iacute" => "\xED", "icirc"  => "\xEE", "iuml"   => "\xEF",
+   "eth"    => "\xF0", "ntilde" => "\xF1", "ograve" => "\xF2",
+   "oacute" => "\xF3", "ocirc"  => "\xF4", "otilde" => "\xF5",
+   "ouml"   => "\xF6", "divide" => "\xF7", "oslash" => "\xF8",
+   "ugrave" => "\xF9", "uacute" => "\xFA", "ucirc"  => "\xFB",
+   "uuml"   => "\xFC", "yacute" => "\xFD", "thorn"  => "\xFE",
+   "yuml"   => "\xFF", "apos"   => "\'",
+
+   # HTML 4 entities that do not have 1:1 Latin1 mappings.
+   "bull"  => "*",    "hellip"=> "...",  "prime" => "'",  "Prime" => "\"",
+   "frasl" => "/",    "trade" => "[tm]", "larr"  => "<-", "rarr"  => "->",
+   "harr"  => "<->",  "lArr"  => "<=",   "rArr"  => "=>", "hArr"  => "<=>",
+   "empty" => "\xD8", "minus" => "-",    "lowast"=> "*",  "sim"   => "~",
+   "cong"  => "=~",   "asymp" => "~",    "ne"    => "!=", "equiv" => "==",
+   "le"    => "<=",   "ge"    => ">=",   "lang"  => "<",  "rang"  => ">",
+   "loz"   => "<>",   "OElig" => "OE",   "oelig" => "oe", "Yuml"  => "Y",
+   "circ"  => "^",    "tilde" => "~",    "ensp"  => " ",  "emsp"  => " ",
+   "thinsp"=> " ",    "ndash" => "-",    "mdash" => "-",  "lsquo" => "`",
+   "rsquo" => "'",    "sbquo" => "'",    "ldquo" => "\"", "rdquo" => "\"",
+   "bdquo" => "\"",   "lsaquo"=> "<",    "rsaquo"=> ">",
 );
 
 # Maps certain UTF8 characters (2 or 3 bytes) to the corresponding
 # Latin1 characters.
 #
 my %unicode_latin1_table = (
-   "\xC2\xA1" => '¡', "\xC2\xA2" => '¢', "\xC2\xA3" => '£', "\xC2\xA4" => '¤',
-   "\xC2\xA5" => '¥', "\xC2\xA6" => '¦', "\xC2\xA7" => '§', "\xC2\xA8" => '¨',
-   "\xC2\xA9" => '©', "\xC2\xAA" => 'ª', "\xC2\xAB" => '«', "\xC2\xAC" => '¬',
-   "\xC2\xAD" => '­', "\xC2\xAE" => '®', "\xC2\xAF" => '¯', "\xC2\xB0" => '°',
-   "\xC2\xB1" => '±', "\xC2\xB2" => '²', "\xC2\xB3" => '³', "\xC2\xB4" => '´',
-   "\xC2\xB5" => 'µ', "\xC2\xB6" => '¶', "\xC2\xB7" => '·', "\xC2\xB8" => '¸',
-   "\xC2\xB9" => '¹', "\xC2\xBA" => 'º', "\xC2\xBB" => '»', "\xC2\xBC" => '¼',
-   "\xC2\xBD" => '½', "\xC2\xBE" => '¾', "\xC2\xBF" => '¿', "\xC3\x80" => 'À',
-   "\xC3\x81" => 'Á', "\xC3\x82" => 'Â', "\xC3\x83" => 'Ã', "\xC3\x84" => 'Ä',
-   "\xC3\x85" => 'Å', "\xC3\x86" => 'Æ', "\xC3\x87" => 'Ç', "\xC3\x88" => 'È',
-   "\xC3\x89" => 'É', "\xC3\x8A" => 'Ê', "\xC3\x8B" => 'Ë', "\xC3\x8C" => 'Ì',
-   "\xC3\x8D" => 'Í', "\xC3\x8E" => 'Î', "\xC3\x8F" => 'Ï', "\xC3\x90" => 'Ð',
-   "\xC3\x91" => 'Ñ', "\xC3\x92" => 'Ò', "\xC3\x93" => 'Ó', "\xC3\x94" => 'Ô',
-   "\xC3\x95" => 'Õ', "\xC3\x96" => 'Ö', "\xC3\x97" => '×', "\xC3\x98" => 'Ø',
-   "\xC3\x99" => 'Ù', "\xC3\x9A" => 'Ú', "\xC3\x9B" => 'Û', "\xC3\x9C" => 'Ü',
-   "\xC3\x9D" => 'Ý', "\xC3\x9E" => 'Þ', "\xC3\x9F" => 'ß', "\xC3\xA0" => 'à',
-   "\xC3\xA1" => 'á', "\xC3\xA2" => 'â', "\xC3\xA3" => 'ã', "\xC3\xA4" => 'ä',
-   "\xC3\xA5" => 'å', "\xC3\xA6" => 'æ', "\xC3\xA7" => 'ç', "\xC3\xA8" => 'è',
-   "\xC3\xA9" => 'é', "\xC3\xAA" => 'ê', "\xC3\xAB" => 'ë', "\xC3\xAC" => 'ì',
-   "\xC3\xAD" => 'í', "\xC3\xAE" => 'î', "\xC3\xAF" => 'ï', "\xC3\xB0" => 'ð',
-   "\xC3\xB1" => 'ñ', "\xC3\xB2" => 'ò', "\xC3\xB3" => 'ó', "\xC3\xB4" => 'ô',
-   "\xC3\xB5" => 'õ', "\xC3\xB6" => 'ö', "\xC3\xB7" => '÷', "\xC3\xB8" => 'ø',
-   "\xC3\xB9" => 'ù', "\xC3\xBA" => 'ú', "\xC3\xBB" => 'û', "\xC3\xBC" => 'ü',
-   "\xC3\xBD" => 'ý', "\xC3\xBE" => 'þ', "\xC3\xBF" => 'ÿ',
-
-   "\xE2\x80\x93" => '--',  "\xE2\x80\x94" => '--',
-   "\xE2\x80\x98" => '`',   "\xE2\x80\x99" => '\'',
-   "\xE2\x80\x9C" => "``",  "\xE2\x80\x9D" => "''",
-   "\xE2\x80\xA6" => '...',
-);
-
+   "\xC2\xA1" => "\xA1", "\xC2\xA2" => "\xA2", "\xC2\xA3" => "\xA3",
+   "\xC2\xA4" => "\xA4", "\xC2\xA5" => "\xA5", "\xC2\xA6" => "\xA6",
+   "\xC2\xA7" => "\xA7", "\xC2\xA8" => "\xA8", "\xC2\xA9" => "\xA9",
+   "\xC2\xAA" => "\xAA", "\xC2\xAB" => "\xAB", "\xC2\xAC" => "\xAC",
+   "\xC2\xAD" => "\xAD", "\xC2\xAE" => "\xAE", "\xC2\xAF" => "\xAF",
+   "\xC2\xB0" => "\xB0", "\xC2\xB1" => "\xB1", "\xC2\xB2" => "\xB2",
+   "\xC2\xB3" => "\xB3", "\xC2\xB4" => "\xB4", "\xC2\xB5" => "\xB5",
+   "\xC2\xB6" => "\xB6", "\xC2\xB7" => "\xB7", "\xC2\xB8" => "\xB8",
+   "\xC2\xB9" => "\xB9", "\xC2\xBA" => "\xBA", "\xC2\xBB" => "\xBB",
+   "\xC2\xBC" => "\xBC", "\xC2\xBD" => "\xBD", "\xC2\xBE" => "\xBE",
+   "\xC2\xBF" => "\xBF", "\xC3\x80" => "\xC0", "\xC3\x81" => "\xC1",
+   "\xC3\x82" => "\xC2", "\xC3\x83" => "\xC3", "\xC3\x84" => "\xC4",
+   "\xC3\x85" => "\xC5", "\xC3\x86" => "\xC6", "\xC3\x87" => "\xC7",
+   "\xC3\x88" => "\xC8", "\xC3\x89" => "\xC9", "\xC3\x8A" => "\xCA",
+   "\xC3\x8B" => "\xCB", "\xC3\x8C" => "\xCC", "\xC3\x8D" => "\xCD",
+   "\xC3\x8E" => "\xCE", "\xC3\x8F" => "\xCF", "\xC3\x90" => "\xD0",
+   "\xC3\x91" => "\xD1", "\xC3\x92" => "\xD2", "\xC3\x93" => "\xD3",
+   "\xC3\x94" => "\xD4", "\xC3\x95" => "\xD5", "\xC3\x96" => "\xD6",
+   "\xC3\x97" => "\xD7", "\xC3\x98" => "\xD8", "\xC3\x99" => "\xD9",
+   "\xC3\x9A" => "\xDA", "\xC3\x9B" => "\xDB", "\xC3\x9C" => "\xDC",
+   "\xC3\x9D" => "\xDD", "\xC3\x9E" => "\xDE", "\xC3\x9F" => "\xDF",
+   "\xC3\xA0" => "\xE0", "\xC3\xA1" => "\xE1", "\xC3\xA2" => "\xE2",
+   "\xC3\xA3" => "\xE3", "\xC3\xA4" => "\xE4", "\xC3\xA5" => "\xE5",
+   "\xC3\xA6" => "\xE6", "\xC3\xA7" => "\xE7", "\xC3\xA8" => "\xE8",
+   "\xC3\xA9" => "\xE9", "\xC3\xAA" => "\xEA", "\xC3\xAB" => "\xEB",
+   "\xC3\xAC" => "\xEC", "\xC3\xAD" => "\xED", "\xC3\xAE" => "\xEE",
+   "\xC3\xAF" => "\xEF", "\xC3\xB0" => "\xF0", "\xC3\xB1" => "\xF1",
+   "\xC3\xB2" => "\xF2", "\xC3\xB3" => "\xF3", "\xC3\xB4" => "\xF4",
+   "\xC3\xB5" => "\xF5", "\xC3\xB6" => "\xF6", "\xC3\xB7" => "\xF7",
+   "\xC3\xB8" => "\xF8", "\xC3\xB9" => "\xF9", "\xC3\xBA" => "\xFA",
+   "\xC3\xBB" => "\xFB", "\xC3\xBC" => "\xFC", "\xC3\xBD" => "\xFD",
+   "\xC3\xBE" => "\xFE", "\xC3\xBF" => "\xFF",
+
+   "\xE2\x80\x93" => '--',       "\xE2\x80\x94" => '--',
+   "\xE2\x80\x98" => '`',        "\xE2\x80\x99" => '\'',
+   "\xE2\x80\x9C" => "``",       "\xE2\x80\x9D" => "''",
+   "\xE2\x80\xB2" => "'",        "\xE2\x80\xA6" => '...',
+   "\xE2\x86\x90" => ' &larr; ', "\xE2\x84\xA2" => '&trade;',
+   "\xE2\x80\xA2" => '&bull;',   "\xC2\xA0" => ' ',  # &nbsp;
+ );
 
 # Convert any HTML entities to Latin1 characters.
 #
@@ -112,15 +155,19 @@ sub de_entify($) {
   my ($text) = @_;
   $text =~ s/(&(\#)?([[:alpha:]\d]+);?)/
     {
-     my $c;
-     if ($2) {
-       $c = chr($3);  # the &#number is always decimal, right?
+     my $c = $3;
+     if (! defined($2)) {
+       $c = $entity_table{$c};         # for &Aacute;
      } else {
-       $c = $entity_table{$3};
+       if ($c =~ m@^x([\dA-F]+)$@si) { # for &#x41;
+         $c = chr(hex($1));
+       } elsif ($c =~ m@^\d+$@si) {    # for &#65;
+         $c = chr($c);
+       } else {
+         $c = undef;
+       }
      }
-#    print STDERR "$progname: warning: unknown HTML character entity \"$1\"\n"
-#     unless $c;
-     ($c ? $c : "[$3]");
+     ($c || "[$3]");                   # for &unknown; => "[unknown]"
     }
    /gexi;
   return $text;
@@ -132,7 +179,8 @@ sub de_entify($) {
 #
 sub de_unicoddle($) {
   my ($text) = @_;
-  foreach my $key (keys (%unicode_latin1_table)) {
+  foreach my $key (sort { length($b) <=> length($a) }
+                   keys (%unicode_latin1_table)) {
     my $val = $unicode_latin1_table{$key};
     $text =~ s/$key/$val/gs;
   }
@@ -142,46 +190,45 @@ sub de_unicoddle($) {
 
 # Reads the prefs we use from ~/.xscreensaver
 #
-sub get_prefs() {
-
+sub get_x11_prefs() {
   my $got_any_p = 0;
-  local *IN;
 
-  if (open (IN, "<$config_file")) {
+  if (open (my $in, '<', $config_file)) {
     print STDERR "$progname: reading $config_file\n" if ($verbose > 1);
-    my $body = '';
-    while (<IN>) { $body .= $_; }
-    close IN;
-    $got_any_p = get_prefs_1 ($body);
+    local $/ = undef;  # read entire file
+    my $body = <$in>;
+    close $in;
+    $got_any_p = get_x11_prefs_1 ($body);
 
   } elsif ($verbose > 1) {
     print STDERR "$progname: $config_file: $!\n";
   }
 
-  if (! $got_any_p) {
+  if (! $got_any_p && defined ($ENV{DISPLAY})) {
     # We weren't able to read settings from the .xscreensaver file.
     # Fall back to any settings in the X resource database
     # (/usr/X11R6/lib/X11/app-defaults/XScreenSaver)
     #
     print STDERR "$progname: reading X resources\n" if ($verbose > 1);
     my $body = `appres XScreenSaver xscreensaver -1`;
-    $got_any_p = get_prefs_1 ($body);
+    $got_any_p = get_x11_prefs_1 ($body);
   }
 
   if ($verbose > 1) {
-    printf STDERR "$progname: mode:    $text_mode\n";
-    printf STDERR "$progname: literal: $text_literal\n";
-    printf STDERR "$progname: file:    $text_file\n";
-    printf STDERR "$progname: program: $text_program\n";
-    printf STDERR "$progname: url:     $text_url\n";
+    print STDERR "$progname: mode:    $text_mode\n";
+    print STDERR "$progname: literal: $text_literal\n";
+    print STDERR "$progname: file:    $text_file\n";
+    print STDERR "$progname: program: $text_program\n";
+    print STDERR "$progname: url:     $text_url\n";
   }
 
   $text_mode =~ tr/A-Z/a-z/;
   $text_literal =~ s@\\n@\n@gs;
+  $text_literal =~ s@\\\n@\n@gs;
 }
 
 
-sub get_prefs_1($) {
+sub get_x11_prefs_1($) {
   my ($body) = @_;
 
   my $got_any_p = 0;
@@ -208,6 +255,63 @@ sub get_prefs_1($) {
 }
 
 
+sub get_cocoa_prefs($) {
+  my ($id) = @_;
+  my $v;
+  print STDERR "$progname: reading Cocoa prefs: \"$id\"\n" if ($verbose > 1);
+
+  $v = get_cocoa_pref_1 ($id, "textMode");
+  $text_mode = $v if defined ($v);
+
+  # The "textMode" pref is set to a number instead of a string because I
+  # can't figure out the black magic to make Cocoa bindings work right.
+  #
+  if    ($text_mode eq '0') { $text_mode = 'date';    }
+  elsif ($text_mode eq '1') { $text_mode = 'literal'; }
+  elsif ($text_mode eq '2') { $text_mode = 'file';    }
+  elsif ($text_mode eq '3') { $text_mode = 'url';     }
+  elsif ($text_mode eq '4') { $text_mode = 'program'; }
+
+  $v = get_cocoa_pref_1 ($id, "textLiteral");
+  $text_literal = $v if defined ($v);
+  $text_literal =~ s@\\n@\n@gs;
+  $text_literal =~ s@\\\n@\n@gs;
+
+  $v = get_cocoa_pref_1 ($id, "textFile");
+  $text_file = $v if defined ($v);
+
+  $v = get_cocoa_pref_1 ($id, "textProgram");
+  $text_program = $v if defined ($v);
+
+  $v = get_cocoa_pref_1 ($id, "textURL");
+  $text_url = $v if defined ($v);
+}
+
+
+sub get_cocoa_pref_1($$) {
+  my ($id, $key) = @_;
+  # make sure there's nothing stupid/malicious in either string.
+  $id  =~ s/[^-a-z\d. ]/_/gsi;
+  $key =~ s/[^-a-z\d. ]/_/gsi;
+  my $cmd = "defaults -currentHost read \"$id\" \"$key\"";
+
+  print STDERR "$progname: executing $cmd\n"
+    if ($verbose > 3);
+
+  my $val = `$cmd 2>/dev/null`;
+  $val =~ s/^\s+//s;
+  $val =~ s/\s+$//s;
+
+  print STDERR "$progname: Cocoa: $id $key = \"$val\"\n"
+    if ($verbose > 2);
+
+  $val = undef if ($val =~ m/^$/s);
+
+  return $val;
+}
+
+
 # like system() but checks errors.
 #
 sub safe_system(@) {
@@ -257,25 +361,31 @@ sub output() {
 
   if ($text_mode eq 'literal') {
     $text_literal = strftime ($text_literal, localtime);
+    $text_literal =~ y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
     print STDOUT $text_literal;
     print STDOUT "\n" unless ($text_literal =~ m/\n$/s);
 
   } elsif ($text_mode eq 'file') {
 
-    local *IN;
-    if (open (IN, "<$text_file")) {
+    $text_file =~ s@^~/@$ENV{HOME}/@s;     # allow literal "~/"
+
+    if (open (my $in, '<', $text_file)) {
       print STDERR "$progname: reading $text_file\n" if ($verbose);
 
-      if ($wrap_columns && $wrap_columns > 0) {
+      if (($wrap_columns && $wrap_columns > 0) || $truncate_lines) {
         # read it, then reformat it.
-        my $body = '';
-        while (<IN>) { $body .= $_; }
-        reformat_text ($body);
+        local $/ = undef;  # read entire file
+        my $body = <$in>;
+        $body = reformat_text ($body);
+        print STDOUT $body;
       } else {
-        # stream it
-        while (<IN>) { print $_; }
+        # stream it by lines
+        while (<$in>) { 
+          y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
+          print STDOUT $_;
+        }
       }
-      close IN;
+      close $in;
     } else {
       error ("$text_file: $!");
     }
@@ -286,10 +396,21 @@ sub output() {
     $text_program = which ($prog) . $args;
     print STDERR "$progname: running $text_program\n" if ($verbose);
 
-    if ($wrap_columns && $wrap_columns > 0) {
+    if (($wrap_columns && $wrap_columns > 0) || $truncate_lines) {
       # read it, then reformat it.
-      my $body = `( $text_program ) 2>&1`;
-      reformat_text ($body);
+      my $lines = 0;
+      my $body = "";
+      my $cmd = "( $text_program ) 2>&1";
+      # $cmd .= " | sed -l"; # line buffer instead of 4k pipe buffer
+      open (my $pipe, '-|:unix', $cmd);
+      while (my $line = <$pipe>) {
+        $body .= $line;
+        $lines++;
+        last if ($truncate_lines && $lines > $truncate_lines);
+      }
+      close $pipe;
+      $body = reformat_text ($body);
+      print STDOUT $body;
     } else {
       # stream it
       safe_system ("$text_program");
@@ -301,178 +422,44 @@ sub output() {
 
   } else { # $text_mode eq 'date'
 
-    safe_system ("uname", "-n");
-    if (-f "/etc/redhat-release") { system ("cat", "/etc/redhat-release"); }
-    safe_system ("uname", "-sr");
-    print "\n";
-    safe_system ("date", "+%c");
-    print "\n";
-    my $ut = `uptime`;
-    $ut =~ s/^[ \d:]*(am|pm)?//i;
-    $ut =~ s/,\s*(load)/\n$1/;
-    print "$ut\n";
-  }
-
-}
-
-
-# Loads the given URL, returns: $http, $head, $body.
-#
-sub get_url_1($;$) {
-  my ($url, $referer) = @_;
-  
-  if (! ($url =~ m@^http://@i)) {
-    error ("not an HTTP URL: $url");
-  }
-
-  my ($url_proto, $dummy, $serverstring, $path) = split(/\//, $url, 4);
-  $path = "" unless $path;
-
-  my ($them,$port) = split(/:/, $serverstring);
-  $port = 80 unless $port;
-
-  my $them2 = $them;
-  my $port2 = $port;
-  if ($http_proxy) {
-    $serverstring = $http_proxy if $http_proxy;
-    $serverstring =~ s@^[a-z]+://@@;
-    ($them2,$port2) = split(/:/, $serverstring);
-    $port2 = 80 unless $port2;
-  }
-
-  my ($remote, $iaddr, $paddr, $proto, $line);
-  $remote = $them2;
-  if ($port2 =~ /\D/) { $port2 = getservbyname($port2, 'tcp') }
-  if (!$port2) {
-    error ("unrecognised port in $url");
-  }
-
-  $iaddr = inet_aton($remote);
-  error ("host not found: $remote") unless ($iaddr);
-
-  $paddr   = sockaddr_in($port2, $iaddr);
+    my $n = `uname -n`;
+    $n =~ s/\.local\n/\n/s;
+    print $n;
 
+    my $unamep = 1;
 
-  my $head = "";
-  my $body = "";
-
-  $proto   = getprotobyname('tcp');
-  if (!socket(S, PF_INET, SOCK_STREAM, $proto)) {
-    error ("socket: $!");
-  }
-  if (!connect(S, $paddr)) {
-    error ("connect($serverstring): $!");
-  }
-
-  select(S); $| = 1; select(STDOUT);
-
-  my $user_agent = "$progname/$version";
-
-  my $hdrs = ("GET " . ($http_proxy ? $url : "/$path") . " HTTP/1.0\r\n" .
-              "Host: $them\r\n" .
-              "User-Agent: $user_agent\r\n");
-  if ($referer) {
-    $hdrs .= "Referer: $referer\r\n";
-  }
-  $hdrs .= "\r\n";
-
-  if ($verbose > 3) {
-    foreach (split('\r?\n', $hdrs)) {
-      print STDERR "  ==> $_\n";
+    if (-f "/etc/redhat-release") {        # "Fedora Core release 4 (Stentz)"
+      safe_system ("cat", "/etc/redhat-release");
     }
-  }
-  print S $hdrs;
-  my $http = <S> || "";
-
-  $_  = $http;
-  s/[\r\n]+$//s;
-  print STDERR "  <== $_\n" if ($verbose > 3);
 
-  while (<S>) {
-    $head .= $_;
-    s/[\r\n]+$//s;
-    last if m@^$@;
-    print STDERR "  <== $_\n" if ($verbose > 3);
-  }
-
-  print STDERR "  <== \n" if ($verbose > 4);
-  my $lines = 0;
-  while (<S>) {
-    s/\r\n/\n/gs;
-    print STDERR "  <== $_" if ($verbose > 4);
-    $body .= $_;
-    $lines++;
-  }
+    if (-f "/etc/release") {               # "Solaris 10 3/05 s10_74L2a X86"
+      safe_system ("head", "-1", "/etc/release");
+    }
 
-  print STDERR "  <== [ body ]: $lines lines, " . length($body) . " bytes\n"
-    if ($verbose == 4);
+    if (-f "/usr/sbin/system_profiler") {   # "Mac OS X 10.4.5 (8H14)"
+      my $sp =                             # "iMac G5"
+        `/usr/sbin/system_profiler SPSoftwareDataType SPHardwareDataType`;
+      my ($v) = ($sp =~ m/^\s*System Version:\s*(.*)$/mi);
+      my ($s) = ($sp =~ m/^\s*(?:CPU|Processor) Speed:\s*(.*)$/mi);
+      my ($t) = ($sp =~ m/^\s*(?:Machine|Model) Name:\s*(.*)$/mi);
+      print "$v\n" if ($v);
+      print "$s $t\n" if ($s && $t);
+      $unamep = !defined ($v);
+    }
 
-  close S;
+    if ($unamep) {
+      safe_system ("uname", "-sr");        # "Linux 2.6.15-1.1831_FC4"
+    }
 
-  if (!$http) {
-    error ("null response: $url");
+    print "\n";
+    safe_system ("date", "+%c");
+    print "\n";
+    my $ut = `uptime`;
+    $ut =~ s/^[ \d:]*(am|pm)?//i;
+    $ut =~ s/,\s*(load)/\n$1/;
+    print "$ut\n";
   }
 
-  return ( $http, $head, $body );
-}
-
-
-# Loads the given URL, processes redirects, returns (content-type, body).
-#
-sub get_url($;$) {
-  my ($url, $referer) = @_;
-
-  print STDERR "$progname: loading $url\n" if ($verbose > 2);
-
-  my $orig_url = $url;
-  my $loop_count = 0;
-  my $max_loop_count = 10;
-
-  do {
-    my ( $http, $head, $body ) = get_url_1 ($url, $referer);
-
-    $http =~ s/[\r\n]+$//s;
-
-    if ( $http =~ m@^HTTP/[0-9.]+ 30[123]@ ) {
-      $_ = $head;
-
-      my ( $location ) = m@^location:[ \t]*(.*)$@im;
-      if ( $location ) {
-        $location =~ s/[\r\n]$//;
-
-        print STDERR "$progname: redirect from $url to $location\n"
-          if ($verbose > 3);
-
-        $referer = $url;
-        $url = $location;
-
-        if ($url =~ m@^/@) {
-          $referer =~ m@^(http://[^/]+)@i;
-          $url = $1 . $url;
-        } elsif (! ($url =~ m@^[a-z]+:@i)) {
-          $_ = $referer;
-          s@[^/]+$@@g if m@^http://[^/]+/@i;
-          $_ .= "/" if m@^http://[^/]+$@i;
-          $url = $_ . $url;
-        }
-
-      } else {
-        error ("no Location with \"$http\"");
-      }
-
-      if ($loop_count++ > $max_loop_count) {
-        error ("too many redirects ($max_loop_count) from $orig_url");
-      }
-
-    } elsif ( $http =~ m@^HTTP/[0-9.]+ ([4-9][0-9][0-9].*)$@ ) {
-      error ("failed: $1 ($url)");
-
-    } else {
-      my $ct = 'text/plain';
-      $ct = $1 if ($head =~ m/^content-type:\s*([^\s]+)/mi);
-      return ($ct, $body);
-    }
-  } while (1);
 }
 
 
@@ -503,12 +490,23 @@ sub guess_content_type($$) {
   return 'text';
 }
 
+
 sub reformat_html($$) {
   my ($body, $rss_p) = @_;
   $_ = $body;
 
+  # In HTML, try to preserve newlines inside of PRE.
+  #
+  if (! $rss_p) {
+    s@(<PRE\b[^<>]*>\s*)(.*?)(</PRE)@{
+      my ($a, $b, $c) = ($1, $2, $3);
+      $b =~ s/[\r\n]/<BR>/gs;
+      $a . $b . $c;
+     }@gsexi;
+  }
+
   if (! $rss_p) {
-    # In HTML, unfold lines (this breaks PRE.  Sue me.)
+    # In HTML, unfold lines.
     # In RSS, assume \n means literal line break.
     s@[\r\n]@ @gsi;
   }
@@ -526,9 +524,51 @@ sub reformat_html($$) {
   s@<[^<>]*>?@@gs;                # lose all other HTML tags
   $_ = de_entify ($_);            # convert HTML entities
 
+  # For Wikipedia: delete anything inside {{ }} and unwrap [[tags]],
+  # among other things.
+  #
+  if ($rss_p eq 'wiki') {
+
+    # Creation line is often truncated: screws up parsing with unbalanced {{.
+    s@(: +<- +Created page) with [^\n]+@$1@s;
+
+    s@/\*.*?\*/@@si;                              # /* ... */
+
+    # Try to omit all tables, since they're impossible to read as text.
+    #
+    1 while (s/{{[^{}]*}}/ /gs);                  # {{ ... }}
+    1 while (s/{\|.*?\|}/\n\n/gs);                # {| ... |}
+    1 while (s/\|-.*?\|/ /gs);                    # |- ... |  (table cell)
+
+    # Convert anchors to something more readable.
+    #
+    s/\[\[([^\[\]\|]+)\|([^\[\]]+)\]\]/$2/gs;     # [[link|anchor]]
+    s/\[\[([^:\[\]\|]+)\]\]/$1/gs;                # [[anchor]]
+    s/\[https?:[^\[\]\s]+\s+([^\[\]]+)\]/$1/gs;           # [url anchor]
+
+    # Convert all references to asterisks.
+    s@\s*<ref>\s*.*?</ref>@*@gs;                  # <ref> ... <ref> ->  "*"
+    s@\n[ \t]*\d+\s*\^\s*http[^\s]+[ \t]*\n@\n@gs; # 1 ^ URL (a Reflist)
+
+    s@\[\[File:([^\|\]]+).*?\]\]@\n$1\n@gs;      # [[File: X | ... ]]
+    s@\[\[Category:.*?\]\]@@gs;                          # omit categories
+
+    s/<[^<>]*>//gs;    # Omit all remaining tags
+    s/\'{3,}//gs;      # Omit ''' and ''''
+    s/\'\'/\"/gs;      # ''  ->  "
+    s/\`\`/\"/gs;      # ``  ->  "
+    s/\"\"+/\"/gs;     # ""  ->  "
+
+    s/^[ \t]*[*#]+[ \t]*$//gm; # Omit lines with just * or # on them
+
+    # Omit trailing headlines with no text after them (e.g. == Notes ==)
+    1 while (s/\n==+[ \t]*[^\n=]+[ \t]*==+\s*$/\n/s);
+  }
+
+
   # elide any remaining non-Latin1 binary data...
-  s/([\177-\377]+(\s*[\177-\377]+)[^a-z\d]*)/«...» /g;
-  #s/([\177-\377]+(\s*[\177-\377]+)[^a-z\d]*)/«$1» /g;
+  s/([^\000-\176]+(\s*[^\000-\176]+)[^a-z\d]*)/\xAB...\xBB /g;
+# s/([^\000-\176]+(\s*[^\000-\176]+)[^a-z\d]*)/\xAB$1\xBB /g;
 
   $_ .= "\n";
 
@@ -537,17 +577,28 @@ sub reformat_html($$) {
 
   if (!defined($wrap_columns) || $wrap_columns > 0) {
     $Text::Wrap::columns = ($wrap_columns || 72);
-    $_ = wrap ("", "  ", $_);     # wrap the lines as a paragraph
-    s/[ \t]+$//gm;                # lose whitespace at end of line again
+    $Text::Wrap::break = '[\s/|]';  # wrap on slashes for URLs
+    $_ = wrap ("", "  ", $_);       # wrap the lines as a paragraph
+    s/[ \t]+$//gm;                  # lose whitespace at end of line again
   }
 
-  print STDOUT $_;
+  s/^\n+//gs;
+
+  if ($truncate_lines) {
+    s/^(([^\n]*\n){$truncate_lines}).*$/$1/s;
+  }
+
+  y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
+
+  return $_;
 }
 
 
 sub reformat_rss($) {
   my ($body) = @_;
 
+  my $wiki_p = ($body =~ m@<generator>[^<>]*Wiki@si);
+
   $body =~ s/(<(ITEM|ENTRY)\b)/\001\001$1/gsi;
   my @items = split (/\001\001/, $body);
 
@@ -569,6 +620,8 @@ sub reformat_rss($) {
     }
   }
 
+  my $out = '';
+
   my $i = -1;
   foreach (@items) {
     $i++;
@@ -604,16 +657,25 @@ sub reformat_rss($) {
     $title = rss_field_to_html ($title || '');
     $body1 = rss_field_to_html ($body1 || '');
 
-    reformat_html ("$title<P>$body1", 1);
-    print "\n";
+    $title = '' if ($body1 eq $title);  # Identical in Twitter's atom feed.
+
+    $out .= reformat_html ("$title<P>$body1", $wiki_p ? 'wiki' : 'rss');
+    $out .= "\n";
+  }
+
+  if ($truncate_lines) {
+    $out =~ s/^(([^\n]*\n){$truncate_lines}).*$/$1/s;
   }
+
+  return $out;
 }
 
 
 sub rss_field_to_html($) {
   my ($body) = @_;
 
-  # Assume that if <![CDATA[...]]> is present, everything inside that.
+  # If <![CDATA[...]]> is present, everything inside that is HTML,
+  # and not double-encoded.
   #
   if ($body =~ m/^\s*<!\[CDATA\[(.*?)\]\s*\]/is) {
     $body = $1;
@@ -634,37 +696,84 @@ sub reformat_text($) {
   if ($wrap_columns && $wrap_columns > 0) {
     print STDERR "$progname: wrapping at $wrap_columns...\n" if ($verbose > 2);
     $Text::Wrap::columns = $wrap_columns;
+    $Text::Wrap::break = '[\s/]';  # wrap on slashes for URLs
     $body = wrap ("", "", $body);
     $body =~ s/[ \t]+$//gm;
   }
 
-  print STDOUT $body;
+  if ($truncate_lines) {
+    $body =~ s/^(([^\n]*\n){$truncate_lines}).*$/$1/s;
+  }
+
+  $body =~ y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
+  return $body;
+}
+
+
+# Figure out what the proxy server should be, either from environment
+# variables or by parsing the output of the (MacOS) program "scutil",
+# which tells us what the system-wide proxy settings are.
+#
+sub set_proxy($) {
+  my ($ua) = @_;
+
+  if (!defined($ENV{http_proxy}) && !defined($ENV{HTTP_PROXY})) {
+    my $proxy_data = `scutil --proxy 2>/dev/null`;
+    my ($server) = ($proxy_data =~ m/\bHTTPProxy\s*:\s*([^\s]+)/s);
+    my ($port)   = ($proxy_data =~ m/\bHTTPPort\s*:\s*([^\s]+)/s);
+    if ($server) {
+      # Note: this ignores the "ExceptionsList".
+      $ENV{http_proxy} = "http://" . $server . ($port ? ":$port" : "") . "/";
+      print STDERR "$progname: MacOS proxy: $ENV{http_proxy}\n"
+        if ($verbose > 2)
+      }
+  }
+
+  $ua->env_proxy();
 }
 
 
 sub get_url_text($) {
   my ($url) = @_;
 
-  # historical suckage: the environment variable name is lower case.
-  $http_proxy = $ENV{http_proxy} || $ENV{HTTP_PROXY};
+  my $ua = eval 'LWP::UserAgent->new';
 
-  if ($http_proxy && $http_proxy =~ m@^http://([^/]*)/?$@ ) {
-    # historical suckage: allow "http://host:port" as well as "host:port".
-    $http_proxy = $1;
+  if (! $ua) {
+    print STDOUT ("\n\tPerl is broken. Do this to repair it:\n" .
+                  "\n\tsudo cpan LWP::UserAgent\n\n");
+    return;
   }
 
-  my ($ct, $body) = get_url ($url);
+  set_proxy ($ua);
+  $ua->agent ("$progname/$version");
+  my $res = $ua->get ($url);
+  my $body;
+  my $ct;
+
+  if ($res && $res->is_success) {
+    $body = $res->decoded_content || '';
+    $ct   = $res->header ('Content-Type') || 'text/plain';
+
+  } else {
+    my $err = ($res ? $res->status_line : '') || '';
+    $err = 'unknown error' unless $err;
+    $err = "$url: $err";
+    # error ($err);
+    $body = "Error loading URL $err\n\n";
+    $ct = 'text/plain';
+  }
 
   $ct = guess_content_type ($ct, $body);
   if ($ct eq 'html') {
     print STDERR "$progname: converting HTML...\n" if ($verbose > 2);
-    reformat_html ($body, 0);
+    $body = reformat_html ($body, 0);
   } elsif ($ct eq 'rss')  {
-    reformat_rss ($body);
+    $body = reformat_rss ($body);
   } else {
     print STDERR "$progname: plain text...\n" if ($verbose > 2);
-    reformat_text ($body);
+    $body = reformat_text ($body);
   }
+  print STDOUT $body;
 }
 
 
@@ -702,6 +811,8 @@ sub usage() {
     "                        it will be converted to plain-text.\n" .
     "\n" .
     "       --cols N         Wrap lines at this column.  Default 72.\n" .
+    "\n" .
+    "       --lines N        No more than N lines of output.\n" .
     "\n");
   exit 1;
 }
@@ -709,6 +820,7 @@ sub usage() {
 sub main() {
 
   my $load_p = 1;
+  my $cocoa_id = undef;
 
   while ($#ARGV >= 0) {
     $_ = shift @ARGV;
@@ -717,24 +829,76 @@ sub main() {
     elsif (m/^--?date$/)    { $text_mode = 'date';
                               $load_p = 0; }
     elsif (m/^--?text$/)    { $text_mode = 'literal';
-                              $text_literal = shift @ARGV;
+                              $text_literal = shift @ARGV || '';
                               $load_p = 0; }
     elsif (m/^--?file$/)    { $text_mode = 'file';
-                              $text_file = shift @ARGV;
+                              $text_file = shift @ARGV || '';
                               $load_p = 0; }
     elsif (m/^--?program$/) { $text_mode = 'program';
-                              $text_program = shift @ARGV;
+                              $text_program = shift @ARGV || '';
                               $load_p = 0; }
     elsif (m/^--?url$/)     { $text_mode = 'url';
-                              $text_url = shift @ARGV;
+                              $text_url = shift @ARGV || '';
                               $load_p = 0; }
     elsif (m/^--?col(umn)?s?$/) { $wrap_columns = 0 + shift @ARGV; }
+    elsif (m/^--?lines?$/)  { $truncate_lines = 0 + shift @ARGV; }
+    elsif (m/^--?cocoa$/)   { $cocoa_id = shift @ARGV; }
+    elsif (m/^--?nyarlathotep$/) { $nyarlathotep_p++; }
     elsif (m/^-./) { usage; }
     else { usage; }
   }
 
-  get_prefs() if ($load_p);
+  if ($load_p) {
+
+    if (!defined ($cocoa_id)) {
+      # see OSX/XScreenSaverView.m
+      $cocoa_id = $ENV{XSCREENSAVER_CLASSPATH};
+    }
+
+    if (defined ($cocoa_id)) {
+      get_cocoa_prefs($cocoa_id);
+    } else {
+      get_x11_prefs();
+    }
+  }
+
   output();
+
+
+  if (defined ($cocoa_id)) {
+    #
+    # On MacOS, sleep for 10 seconds between when the last output is
+    # printed, and when this process exits.  This is because MacOS
+    # 10.5.0 and later broke ptys in a new and exciting way: basically,
+    # once the process at the end of the pty exits, you have exactly
+    # 1 second to read all the queued data off the pipe before it is
+    # summarily flushed.
+    #
+    # Many of the screen savers were written to depend on being able
+    # to read a small number of bytes, and continue reading until they
+    # reached EOF.  This is no longer possible.
+    #
+    # Note that the current MacOS behavior has all four of these
+    # awesome properties: 1) Inconvenient; 2) Has no sane workaround;
+    # 3) Different behavior than MacOS 10.1 through 10.4; and 4)
+    # Different behavior than every other Unix in the world.
+    #
+    # See http://jwz.org/b/DHke, and for those of you inside Apple,
+    # "Problem ID 5606018".
+    #
+    # One workaround would be to rewrite the savers to have an
+    # internal buffer, and always read as much data as possible as
+    # soon as a pipe has input available.  However, that's a lot more
+    # work, so instead, let's just not exit right away, and hope that
+    # 10 seconds is enough.
+    #
+    # This will solve the problem for invocations of xscreensaver-text
+    # that produce little output (e.g., date-mode); and won't solve it
+    # in cases where a large amount of text is generated in a short
+    # amount of time (e.g., url-mode.)
+    #
+    sleep (10);
+  }
 }
 
 main();