X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=driver%2Fxscreensaver-text;h=e4c588c50cf41cd929f032ea11b34343c8cdae83;hb=ff35d056d723c9a5ffe728dbba5f1c25e141be04;hp=e6a00226863743cca1e138b8051957ebafa38de7;hpb=07faf451b99879183ed7e909e43a0e065be1ee7f;p=xscreensaver diff --git a/driver/xscreensaver-text b/driver/xscreensaver-text index e6a00226..e4c588c5 100755 --- a/driver/xscreensaver-text +++ b/driver/xscreensaver-text @@ -1,5 +1,5 @@ #!/usr/bin/perl -w -# Copyright © 2005, 2006 Jamie Zawinski +# Copyright © 2005-2013 Jamie Zawinski # # Permission to use, copy, modify, distribute, and sell this software and its # documentation for any purpose is hereby granted without fee, provided that @@ -21,15 +21,20 @@ # 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.14 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/; +my $version = q{ $Revision: 1.29 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/; my $verbose = 0; my $http_proxy = undef; @@ -39,7 +44,8 @@ my $text_mode = 'date'; my $text_literal = ''; my $text_file = ''; my $text_program = ''; -my $text_url = ''; +my $text_url = 'http://twitter.com/statuses/public_timeline.atom'; +# Default URL needs to be set and match what's in OSX/XScreenSaverView.m my $wrap_columns = undef; my $nyarlathotep_p = 0; @@ -73,7 +79,20 @@ my %entity_table = ( "ocirc" => 'ô', "otilde" => 'õ', "ouml" => 'ö', "divide" => '÷', "oslash" => 'ø', "ugrave" => 'ù', "uacute" => 'ú', "ucirc" => 'û', "uuml" => 'ü', "yacute" => 'ý', "thorn" => 'þ', "yuml" => 'ÿ', - "apos" => '\'' + "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" => "Ø", "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 @@ -118,15 +137,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 Á } else { - $c = $entity_table{$3}; + if ($c =~ m@^x([\dA-F]+)$@si) { # for A + $c = chr(hex($1)); + } elsif ($c =~ m@^\d+$@si) { # for A + $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; @@ -174,15 +197,16 @@ sub get_x11_prefs() { } 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; } @@ -229,9 +253,12 @@ sub get_cocoa_prefs($) { 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); @@ -366,12 +393,14 @@ sub output() { } else { # $text_mode eq 'date' - safe_system ("uname", "-n"); + my $n = `uname -n`; + $n =~ s/\.local\n/\n/s; + print $n; my $unamep = 1; if (-f "/etc/redhat-release") { # "Fedora Core release 4 (Stentz)" - system ("cat", "/etc/redhat-release"); + safe_system ("cat", "/etc/redhat-release"); } if (-f "/etc/release") { # "Solaris 10 3/05 s10_74L2a X86" @@ -382,8 +411,8 @@ sub output() { 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 Speed:\s*(.*)$/mi); - my ($t) = ($sp =~ m/^\s*Machine Name:\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); @@ -405,166 +434,6 @@ sub output() { } -# Loads the given URL, returns: $http, $head, $body. -# -sub get_url_1($;$) { - my ($url, $referer) = @_; - - if (! ($url =~ m@^(http|feed)://@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 $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"; - } - } - print S $hdrs; - my $http = || ""; - - $_ = $http; - s/[\r\n]+$//s; - print STDERR " <== $_\n" if ($verbose > 3); - - while () { - $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/\r\n/\n/gs; - print STDERR " <== $_" if ($verbose > 4); - $body .= $_; - $lines++; - } - - print STDERR " <== [ body ]: $lines lines, " . length($body) . " bytes\n" - if ($verbose == 4); - - close S; - - if (!$http) { - error ("null response: $url"); - } - - 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); -} - - # Make an educated guess as to what's in this document. # We don't necessarily take the Content-Type header at face value. # Returns 'html', 'rss', or 'text'; @@ -592,12 +461,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@(]*>\s*)(.*?)(/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; } @@ -615,6 +495,19 @@ sub reformat_html($$) { s@<[^<>]*>?@@gs; # lose all other HTML tags $_ = de_entify ($_); # convert HTML entities + # For Wikipedia: delete anything inside {{ }} and unwrap [[tags]] + # + if ($rss_p eq 'wiki') { + s@/\*.*?\*/@@si; # /* ... */ + 1 while (s/{{[^{}]*}}//gs); # {{ ... }} + s/\[\[([^:\[\]\|]+)\|([^\[\]]+)\]\]/$2/gs; # [[link|anchor]] + s/\[\[([^:\[\]\|]+)\]\]/$1/gs; # [[anchor]] + s/\[http:[^\[\]\s]+\s+([^\[\]]+)\]/$1/gs; # [url anchor] +# s@\s*.*?@*@gs; # url -> "*" + s/<[^<>]*>//gs; # -- omit. + } + + # 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; @@ -626,10 +519,13 @@ sub reformat_html($$) { if (!defined($wrap_columns) || $wrap_columns > 0) { $Text::Wrap::columns = ($wrap_columns || 72); + $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 } + s/^\n+//gs; + y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p); print STDOUT $_; } @@ -638,6 +534,8 @@ sub reformat_html($$) { sub reformat_rss($) { my ($body) = @_; + my $wiki_p = ($body =~ m@[^<>]*Wiki@si); + $body =~ s/(<(ITEM|ENTRY)\b)/\001\001$1/gsi; my @items = split (/\001\001/, $body); @@ -694,7 +592,9 @@ sub reformat_rss($) { $title = rss_field_to_html ($title || ''); $body1 = rss_field_to_html ($body1 || ''); - reformat_html ("$title

$body1", 1); + $title = '' if ($body1 eq $title); # Identical in Twitter's atom feed. + + reformat_html ("$title

$body1", $wiki_p ? 'wiki' : 'rss'); print "\n"; } } @@ -724,6 +624,7 @@ 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; } @@ -733,18 +634,58 @@ sub reformat_text($) { } +# 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') { @@ -809,16 +750,16 @@ 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/^--?cocoa$/) { $cocoa_id = shift @ARGV; } @@ -842,6 +783,42 @@ sub main() { } 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.livejournal.com/817438.html, 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();