X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=driver%2Fxscreensaver-text;h=24149408bb05b4faa9e7ae385d2fc2e210f2f6a5;hb=50be9bb40dc60130c99ffa568e6677779904ff70;hp=52be88b73d3643a9e2dccd35ca5946b106036752;hpb=447db08c956099b3b183886729108bf5b364c4b8;p=xscreensaver diff --git a/driver/xscreensaver-text b/driver/xscreensaver-text index 52be88b7..24149408 100755 --- a/driver/xscreensaver-text +++ b/driver/xscreensaver-text @@ -1,5 +1,5 @@ #!/usr/bin/perl -w -# Copyright © 2005 Jamie Zawinski +# Copyright © 2005-2010 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 @@ -13,18 +13,24 @@ # .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; + use Socket; use POSIX qw(strftime); use Text::Wrap qw(wrap); use bytes; my $progname = $0; $progname =~ s@.*/@@g; -my $version = q{ $Revision: 1.3 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/; +my $version = q{ $Revision: 1.22 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/; my $verbose = 0; my $http_proxy = undef; @@ -37,6 +43,7 @@ my $text_program = ''; my $text_url = ''; my $wrap_columns = undef; +my $nyarlathotep_p = 0; # Maps HTML character entities to the corresponding Latin1 characters. @@ -67,7 +74,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 @@ -108,19 +128,23 @@ my %unicode_latin1_table = ( # Convert any HTML entities to Latin1 characters. # -sub de_entify { +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; @@ -130,7 +154,7 @@ sub de_entify { # Convert any Unicode characters to Latin1 if possible. # Unconvertable bytes are left alone. # -sub de_unicoddle { +sub de_unicoddle($) { my ($text) = @_; foreach my $key (keys (%unicode_latin1_table)) { my $val = $unicode_latin1_table{$key}; @@ -142,8 +166,7 @@ sub de_unicoddle { # Reads the prefs we use from ~/.xscreensaver # -sub get_prefs { - +sub get_x11_prefs() { my $got_any_p = 0; local *IN; @@ -152,20 +175,20 @@ sub get_prefs { my $body = ''; while () { $body .= $_; } close IN; - $got_any_p = get_prefs_1 ($body); + $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) { @@ -181,7 +204,7 @@ sub get_prefs { } -sub get_prefs_1 { +sub get_x11_prefs_1($) { my ($body) = @_; my $got_any_p = 0; @@ -208,9 +231,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'; } + + $v = get_cocoa_pref_1 ($id, "textLiteral"); + $text_literal = $v if defined ($v); + + $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 { +sub safe_system(@) { my (@cmd) = @_; print STDERR "$progname: executing " . join(' ', @cmd) . "\n" @@ -226,7 +303,7 @@ sub safe_system { } -sub which { +sub which($) { my ($cmd) = @_; if ($cmd =~ m@^\./|^/@) { @@ -243,7 +320,7 @@ sub which { } -sub output { +sub output() { # Do some basic sanity checking (null text, null file names, etc.) # @@ -257,11 +334,14 @@ 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') { + $text_file =~ s@^~/@$ENV{HOME}/@s; # allow literal "~/" + local *IN; if (open (IN, "<$text_file")) { print STDERR "$progname: reading $text_file\n" if ($verbose); @@ -273,7 +353,10 @@ sub output { reformat_text ($body); } else { # stream it - while () { print $_; } + while () { + y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p); + print $_; + } } close IN; } else { @@ -282,7 +365,8 @@ sub output { } elsif ($text_mode eq 'program') { - $text_program = which ($text_program); + my ($prog, $args) = ($text_program =~ m/^([^\s]+)(.*)$/); + $text_program = which ($prog) . $args; print STDERR "$progname: running $text_program\n" if ($verbose); if ($wrap_columns && $wrap_columns > 0) { @@ -301,13 +385,37 @@ 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"); + + my $unamep = 1; + + if (-f "/etc/redhat-release") { # "Fedora Core release 4 (Stentz)" + system ("cat", "/etc/redhat-release"); + } + + if (-f "/etc/release") { # "Solaris 10 3/05 s10_74L2a X86" + safe_system ("head", "-1", "/etc/release"); + } + + 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 Speed:\s*(.*)$/mi); + my ($t) = ($sp =~ m/^\s*Machine Name:\s*(.*)$/mi); + print "$v\n" if ($v); + print "$s $t\n" if ($s && $t); + $unamep = !defined ($v); + } + + if ($unamep) { + safe_system ("uname", "-sr"); # "Linux 2.6.15-1.1831_FC4" + } + print "\n"; safe_system ("date", "+%c"); print "\n"; my $ut = `uptime`; - $ut =~ s/^[ \d:]*//; + $ut =~ s/^[ \d:]*(am|pm)?//i; $ut =~ s/,\s*(load)/\n$1/; print "$ut\n"; } @@ -317,9 +425,10 @@ sub output { # Loads the given URL, returns: $http, $head, $body. # -sub get_url_1 { +sub get_url_1($;$) { my ($url, $referer) = @_; + $url =~ s@^feed:@http:@si; if (! ($url =~ m@^http://@i)) { error ("not an HTTP URL: $url"); } @@ -418,7 +527,7 @@ sub get_url_1 { # Loads the given URL, processes redirects, returns (content-type, body). # -sub get_url { +sub get_url($;$) { my ($url, $referer) = @_; print STDERR "$progname: loading $url\n" if ($verbose > 2); @@ -479,7 +588,7 @@ sub get_url { # We don't necessarily take the Content-Type header at face value. # Returns 'html', 'rss', or 'text'; # -sub guess_content_type { +sub guess_content_type($$) { my ($ct, $body) = @_; $body =~ s/^(.{512}).*/$1/s; # only look in first half K of file @@ -502,12 +611,22 @@ sub guess_content_type { return 'text'; } -sub reformat_html { +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; } @@ -540,15 +659,22 @@ sub reformat_html { 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 $_; } -sub reformat_rss { +sub reformat_rss($) { my ($body) = @_; $body =~ s/(<(ITEM|ENTRY)\b)/\001\001$1/gsi; my @items = split (/\001\001/, $body); + + print STDERR "$progname: converting RSS ($#items items)...\n" + if ($verbose > 2); + shift @items; # Let's skip forward in the stream by a random amount, so that if @@ -556,32 +682,50 @@ sub reformat_rss { # multi-headed machine), they get different text. (Put the items # that we take off the front back on the back.) # - if ($#items > 10) { + if ($#items > 7) { my $n = int (rand ($#items - 5)); + print STDERR "$progname: rotating by $n items...\n" if ($verbose > 2); while ($n-- > 0) { push @items, (shift @items); } } - my $i = 0; + my $i = -1; foreach (@items) { + $i++; - my ($title, $body1, $body2); + my ($title, $body1, $body2, $body3); - $title = $2 if (m@<(TITLE [^<>\s]*)[^<>]*>\s*(.*?)\s*@xsi); - $body1 = $3 if (m@<((DESCRIPTION) [^<>\s]*)[^<>]*>\s*(.*?)\s*@xsi); - $body2 = $3 if (m@<((CONTENT) [^<>\s]*)[^<>]*>\s*(.*?)\s*@xsi); + $title = $3 if (m@<((TITLE) [^<>\s]*)[^<>]*>\s*(.*?)\s*@xsi); + $body1 = $3 if (m@<((DESCRIPTION) [^<>\s]*)[^<>]*>\s*(.*?)\s*@xsi); + $body2 = $3 if (m@<((CONTENT) [^<>\s]*)[^<>]*>\s*(.*?)\s*@xsi); + $body3 = $3 if (m@<((SUMMARY) [^<>\s]*)[^<>]*>\s*(.*?)\s*@xsi); # If there are both and or , # use whichever one contains more text. # - if ($body1 && $body2 && length($body2) >= length($body1)) { + if ($body3 && length($body3) >= length($body2 || '')) { + $body2 = $body3; + } + if ($body2 && length($body2) >= length($body1 || '')) { $body1 = $body2; } - next unless defined ($body1); - $title = rss_field_to_html ($title); - $body1 = rss_field_to_html ($body1); + if (! $body1) { + if ($title) { + print STDERR "$progname: no body in item $i (\"$title\")\n" + if ($verbose > 2); + } else { + print STDERR "$progname: no body or title in item $i\n" + if ($verbose > 2); + next; + } + } + + $title = rss_field_to_html ($title || ''); + $body1 = rss_field_to_html ($body1 || ''); + + $title = '' if ($body1 eq $title); # Identical in Twitter's atom feed. reformat_html ("$title

$body1", 1); print "\n"; @@ -589,7 +733,7 @@ sub reformat_rss { } -sub rss_field_to_html { +sub rss_field_to_html($) { my ($body) = @_; # Assume that if is present, everything inside that. @@ -605,7 +749,7 @@ sub rss_field_to_html { } -sub reformat_text { +sub reformat_text($) { my ($body) = @_; # only re-wrap if --cols was specified. Otherwise, dump it as is. @@ -617,21 +761,47 @@ sub reformat_text { $body =~ s/[ \t]+$//gm; } + $body =~ y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p); print STDOUT $body; } -sub get_url_text { - my ($url) = @_; +# 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() { # historical suckage: the environment variable name is lower case. $http_proxy = $ENV{http_proxy} || $ENV{HTTP_PROXY}; - if ($http_proxy && $http_proxy =~ m@^http://([^/]*)/?$@ ) { - # historical suckage: allow "http://host:port" as well as "host:port". - $http_proxy = $1; + if (defined ($http_proxy)) { + if ($http_proxy && $http_proxy =~ m@^http://([^/]*)/?$@ ) { + # historical suckage: allow "http://host:port" as well as "host:port". + $http_proxy = $1; + } + + } else { + 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); + # Note: this ignores the "ExceptionsList". + if ($server) { + $http_proxy = $server; + $http_proxy .= ":$port" if $port; + } } + print STDERR "$progname: proxy server: $http_proxy\n" + if ($verbose > 2 && $http_proxy); +} + + +sub get_url_text($) { + my ($url) = @_; + + set_proxy(); + my ($ct, $body) = get_url ($url); $ct = guess_content_type ($ct, $body); @@ -639,7 +809,6 @@ sub get_url_text { print STDERR "$progname: converting HTML...\n" if ($verbose > 2); reformat_html ($body, 0); } elsif ($ct eq 'rss') { - print STDERR "$progname: converting RSS...\n" if ($verbose > 2); reformat_rss ($body); } else { print STDERR "$progname: plain text...\n" if ($verbose > 2); @@ -649,13 +818,13 @@ sub get_url_text { -sub error { +sub error($) { my ($err) = @_; print STDERR "$progname: $err\n"; exit 1; } -sub usage { +sub usage() { print STDERR "usage: $progname [ --options ... ]\n" . ("\n" . " Prints out some text for use by various screensavers,\n" . @@ -686,9 +855,10 @@ sub usage { exit 1; } -sub main { +sub main() { my $load_p = 1; + my $cocoa_id = undef; while ($#ARGV >= 0) { $_ = shift @ARGV; @@ -697,25 +867,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/^--?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 three 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; +main(); exit 0;