2 # Copyright © 2005-2011 Jamie Zawinski <jwz@jwz.org>
4 # Permission to use, copy, modify, distribute, and sell this software and its
5 # documentation for any purpose is hereby granted without fee, provided that
6 # the above copyright notice appear in all copies and that both that
7 # copyright notice and this permission notice appear in supporting
8 # documentation. No representations are made about the suitability of this
9 # software for any purpose. It is provided "as is" without express or
12 # This program writes some text to stdout, based on preferences in the
13 # .xscreensaver file. It may load a file, a URL, run a program, or just
16 # In a native MacOS build of xscreensaver, this script is included in
17 # the Contents/Resources/ directory of each screen saver .bundle that
18 # uses it; and in that case, it looks up its resources using
19 # /usr/bin/defaults instead.
21 # Created: 19-Mar-2005.
24 #use diagnostics; # Fails on some MacOS 10.5 systems
28 use POSIX qw(strftime);
29 use Text::Wrap qw(wrap);
32 my $progname = $0; $progname =~ s@.*/@@g;
33 my $version = q{ $Revision: 1.24 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
36 my $http_proxy = undef;
38 my $config_file = $ENV{HOME} . "/.xscreensaver";
39 my $text_mode = 'date';
40 my $text_literal = '';
42 my $text_program = '';
45 my $wrap_columns = undef;
46 my $nyarlathotep_p = 0;
49 # Maps HTML character entities to the corresponding Latin1 characters.
52 "quot" => '"', "amp" => '&', "lt" => '<', "gt" => '>',
53 "nbsp" => ' ', "iexcl" => '¡', "cent" => '¢', "pound" => '£',
54 "curren" => '¤', "yen" => '¥', "brvbar" => '¦', "sect" => '§',
55 "uml" => '¨', "copy" => '©', "ordf" => 'ª', "laquo" => '«',
56 "not" => '¬', "shy" => '', "reg" => '®', "macr" => '¯',
57 "deg" => '°', "plusmn" => '±', "sup2" => '²', "sup3" => '³',
58 "acute" => '´', "micro" => 'µ', "para" => '¶', "middot" => '·',
59 "cedil" => '¸', "sup1" => '¹', "ordm" => 'º', "raquo" => '»',
60 "frac14" => '¼', "frac12" => '½', "frac34" => '¾', "iquest" => '¿',
61 "Agrave" => 'À', "Aacute" => 'Á', "Acirc" => 'Â', "Atilde" => 'Ã',
62 "Auml" => 'Ä', "Aring" => 'Å', "AElig" => 'Æ', "Ccedil" => 'Ç',
63 "Egrave" => 'È', "Eacute" => 'É', "Ecirc" => 'Ê', "Euml" => 'Ë',
64 "Igrave" => 'Ì', "Iacute" => 'Í', "Icirc" => 'Î', "Iuml" => 'Ï',
65 "ETH" => 'Ð', "Ntilde" => 'Ñ', "Ograve" => 'Ò', "Oacute" => 'Ó',
66 "Ocirc" => 'Ô', "Otilde" => 'Õ', "Ouml" => 'Ö', "times" => '×',
67 "Oslash" => 'Ø', "Ugrave" => 'Ù', "Uacute" => 'Ú', "Ucirc" => 'Û',
68 "Uuml" => 'Ü', "Yacute" => 'Ý', "THORN" => 'Þ', "szlig" => 'ß',
69 "agrave" => 'à', "aacute" => 'á', "acirc" => 'â', "atilde" => 'ã',
70 "auml" => 'ä', "aring" => 'å', "aelig" => 'æ', "ccedil" => 'ç',
71 "egrave" => 'è', "eacute" => 'é', "ecirc" => 'ê', "euml" => 'ë',
72 "igrave" => 'ì', "iacute" => 'í', "icirc" => 'î', "iuml" => 'ï',
73 "eth" => 'ð', "ntilde" => 'ñ', "ograve" => 'ò', "oacute" => 'ó',
74 "ocirc" => 'ô', "otilde" => 'õ', "ouml" => 'ö', "divide" => '÷',
75 "oslash" => 'ø', "ugrave" => 'ù', "uacute" => 'ú', "ucirc" => 'û',
76 "uuml" => 'ü', "yacute" => 'ý', "thorn" => 'þ', "yuml" => 'ÿ',
79 # HTML 4 entities that do not have 1:1 Latin1 mappings.
80 "bull" => "*", "hellip"=> "...", "prime" => "'", "Prime" => "\"",
81 "frasl" => "/", "trade" => "[tm]", "larr" => "<-", "rarr" => "->",
82 "harr" => "<->", "lArr" => "<=", "rArr" => "=>", "hArr" => "<=>",
83 "empty" => "Ø", "minus" => "-", "lowast"=> "*", "sim" => "~",
84 "cong" => "=~", "asymp" => "~", "ne" => "!=", "equiv" => "==",
85 "le" => "<=", "ge" => ">=", "lang" => "<", "rang" => ">",
86 "loz" => "<>", "OElig" => "OE", "oelig" => "oe", "Yuml" => "Y",
87 "circ" => "^", "tilde" => "~", "ensp" => " ", "emsp" => " ",
88 "thinsp"=> " ", "ndash" => "-", "mdash" => "-", "lsquo" => "`",
89 "rsquo" => "'", "sbquo" => "'", "ldquo" => "\"", "rdquo" => "\"",
90 "bdquo" => "\"", "lsaquo"=> "<", "rsaquo"=> ">",
93 # Maps certain UTF8 characters (2 or 3 bytes) to the corresponding
96 my %unicode_latin1_table = (
97 "\xC2\xA1" => '¡', "\xC2\xA2" => '¢', "\xC2\xA3" => '£', "\xC2\xA4" => '¤',
98 "\xC2\xA5" => '¥', "\xC2\xA6" => '¦', "\xC2\xA7" => '§', "\xC2\xA8" => '¨',
99 "\xC2\xA9" => '©', "\xC2\xAA" => 'ª', "\xC2\xAB" => '«', "\xC2\xAC" => '¬',
100 "\xC2\xAD" => '', "\xC2\xAE" => '®', "\xC2\xAF" => '¯', "\xC2\xB0" => '°',
101 "\xC2\xB1" => '±', "\xC2\xB2" => '²', "\xC2\xB3" => '³', "\xC2\xB4" => '´',
102 "\xC2\xB5" => 'µ', "\xC2\xB6" => '¶', "\xC2\xB7" => '·', "\xC2\xB8" => '¸',
103 "\xC2\xB9" => '¹', "\xC2\xBA" => 'º', "\xC2\xBB" => '»', "\xC2\xBC" => '¼',
104 "\xC2\xBD" => '½', "\xC2\xBE" => '¾', "\xC2\xBF" => '¿', "\xC3\x80" => 'À',
105 "\xC3\x81" => 'Á', "\xC3\x82" => 'Â', "\xC3\x83" => 'Ã', "\xC3\x84" => 'Ä',
106 "\xC3\x85" => 'Å', "\xC3\x86" => 'Æ', "\xC3\x87" => 'Ç', "\xC3\x88" => 'È',
107 "\xC3\x89" => 'É', "\xC3\x8A" => 'Ê', "\xC3\x8B" => 'Ë', "\xC3\x8C" => 'Ì',
108 "\xC3\x8D" => 'Í', "\xC3\x8E" => 'Î', "\xC3\x8F" => 'Ï', "\xC3\x90" => 'Ð',
109 "\xC3\x91" => 'Ñ', "\xC3\x92" => 'Ò', "\xC3\x93" => 'Ó', "\xC3\x94" => 'Ô',
110 "\xC3\x95" => 'Õ', "\xC3\x96" => 'Ö', "\xC3\x97" => '×', "\xC3\x98" => 'Ø',
111 "\xC3\x99" => 'Ù', "\xC3\x9A" => 'Ú', "\xC3\x9B" => 'Û', "\xC3\x9C" => 'Ü',
112 "\xC3\x9D" => 'Ý', "\xC3\x9E" => 'Þ', "\xC3\x9F" => 'ß', "\xC3\xA0" => 'à',
113 "\xC3\xA1" => 'á', "\xC3\xA2" => 'â', "\xC3\xA3" => 'ã', "\xC3\xA4" => 'ä',
114 "\xC3\xA5" => 'å', "\xC3\xA6" => 'æ', "\xC3\xA7" => 'ç', "\xC3\xA8" => 'è',
115 "\xC3\xA9" => 'é', "\xC3\xAA" => 'ê', "\xC3\xAB" => 'ë', "\xC3\xAC" => 'ì',
116 "\xC3\xAD" => 'í', "\xC3\xAE" => 'î', "\xC3\xAF" => 'ï', "\xC3\xB0" => 'ð',
117 "\xC3\xB1" => 'ñ', "\xC3\xB2" => 'ò', "\xC3\xB3" => 'ó', "\xC3\xB4" => 'ô',
118 "\xC3\xB5" => 'õ', "\xC3\xB6" => 'ö', "\xC3\xB7" => '÷', "\xC3\xB8" => 'ø',
119 "\xC3\xB9" => 'ù', "\xC3\xBA" => 'ú', "\xC3\xBB" => 'û', "\xC3\xBC" => 'ü',
120 "\xC3\xBD" => 'ý', "\xC3\xBE" => 'þ', "\xC3\xBF" => 'ÿ',
122 "\xE2\x80\x93" => '--', "\xE2\x80\x94" => '--',
123 "\xE2\x80\x98" => '`', "\xE2\x80\x99" => '\'',
124 "\xE2\x80\x9C" => "``", "\xE2\x80\x9D" => "''",
125 "\xE2\x80\xA6" => '...',
129 # Convert any HTML entities to Latin1 characters.
133 $text =~ s/(&(\#)?([[:alpha:]\d]+);?)/
137 $c = $entity_table{$c}; # for Á
139 if ($c =~ m@^x([\dA-F]+)$@si) { # for A
141 } elsif ($c =~ m@^\d+$@si) { # for A
147 ($c || "[$3]"); # for &unknown; => "[unknown]"
154 # Convert any Unicode characters to Latin1 if possible.
155 # Unconvertable bytes are left alone.
157 sub de_unicoddle($) {
159 foreach my $key (keys (%unicode_latin1_table)) {
160 my $val = $unicode_latin1_table{$key};
161 $text =~ s/$key/$val/gs;
167 # Reads the prefs we use from ~/.xscreensaver
169 sub get_x11_prefs() {
173 if (open (IN, "<$config_file")) {
174 print STDERR "$progname: reading $config_file\n" if ($verbose > 1);
176 while (<IN>) { $body .= $_; }
178 $got_any_p = get_x11_prefs_1 ($body);
180 } elsif ($verbose > 1) {
181 print STDERR "$progname: $config_file: $!\n";
184 if (! $got_any_p && defined ($ENV{DISPLAY})) {
185 # We weren't able to read settings from the .xscreensaver file.
186 # Fall back to any settings in the X resource database
187 # (/usr/X11R6/lib/X11/app-defaults/XScreenSaver)
189 print STDERR "$progname: reading X resources\n" if ($verbose > 1);
190 my $body = `appres XScreenSaver xscreensaver -1`;
191 $got_any_p = get_x11_prefs_1 ($body);
195 printf STDERR "$progname: mode: $text_mode\n";
196 printf STDERR "$progname: literal: $text_literal\n";
197 printf STDERR "$progname: file: $text_file\n";
198 printf STDERR "$progname: program: $text_program\n";
199 printf STDERR "$progname: url: $text_url\n";
202 $text_mode =~ tr/A-Z/a-z/;
203 $text_literal =~ s@\\n@\n@gs;
207 sub get_x11_prefs_1($) {
213 if ($body =~ m/^[.*]*textMode:[ \t]*([^\s]+)\s*$/im) {
217 if ($body =~ m/^[.*]*textLiteral:[ \t]*(.*?)[ \t]*$/im) {
220 if ($body =~ m/^[.*]*textFile:[ \t]*(.*?)[ \t]*$/im) {
223 if ($body =~ m/^[.*]*textProgram:[ \t]*(.*?)[ \t]*$/im) {
226 if ($body =~ m/^[.*]*textURL:[ \t]*(.*?)[ \t]*$/im) {
234 sub get_cocoa_prefs($) {
238 print STDERR "$progname: reading Cocoa prefs: \"$id\"\n" if ($verbose > 1);
240 $v = get_cocoa_pref_1 ($id, "textMode");
241 $text_mode = $v if defined ($v);
243 # The "textMode" pref is set to a number instead of a string because I
244 # can't figure out the black magic to make Cocoa bindings work right.
246 if ($text_mode eq '0') { $text_mode = 'date'; }
247 elsif ($text_mode eq '1') { $text_mode = 'literal'; }
248 elsif ($text_mode eq '2') { $text_mode = 'file'; }
249 elsif ($text_mode eq '3') { $text_mode = 'url'; }
250 elsif ($text_mode eq '4') { $text_mode = 'program'; }
252 $v = get_cocoa_pref_1 ($id, "textLiteral");
253 $text_literal = $v if defined ($v);
255 $v = get_cocoa_pref_1 ($id, "textFile");
256 $text_file = $v if defined ($v);
258 $v = get_cocoa_pref_1 ($id, "textProgram");
259 $text_program = $v if defined ($v);
261 $v = get_cocoa_pref_1 ($id, "textURL");
262 $text_url = $v if defined ($v);
266 sub get_cocoa_pref_1($$) {
268 # make sure there's nothing stupid/malicious in either string.
269 $id =~ s/[^-a-z\d. ]/_/gsi;
270 $key =~ s/[^-a-z\d. ]/_/gsi;
271 my $cmd = "defaults -currentHost read \"$id\" \"$key\"";
273 print STDERR "$progname: executing $cmd\n"
276 my $val = `$cmd 2>/dev/null`;
280 print STDERR "$progname: Cocoa: $id $key = \"$val\"\n"
283 $val = undef if ($val =~ m/^$/s);
289 # like system() but checks errors.
294 print STDERR "$progname: executing " . join(' ', @cmd) . "\n"
298 my $exit_value = $? >> 8;
299 my $signal_num = $? & 127;
300 my $dumped_core = $? & 128;
301 error ("$cmd[0]: core dumped!") if ($dumped_core);
302 error ("$cmd[0]: signal $signal_num!") if ($signal_num);
303 error ("$cmd[0]: exited with $exit_value!") if ($exit_value);
310 if ($cmd =~ m@^\./|^/@) {
311 error ("cannot execute $cmd") unless (-x $cmd);
315 foreach my $dir (split (/:/, $ENV{PATH})) {
316 my $cmd2 = "$dir/$cmd";
317 print STDERR "$progname: checking $cmd2\n" if ($verbose > 3);
318 return $cmd2 if (-x "$cmd2");
320 error ("$cmd not found on \$PATH");
326 # Do some basic sanity checking (null text, null file names, etc.)
328 if (($text_mode eq 'literal' && $text_literal =~ m/^\s*$/i) ||
329 ($text_mode eq 'file' && $text_file =~ m/^\s*$/i) ||
330 ($text_mode eq 'program' && $text_program =~ m/^\s*$/i) ||
331 ($text_mode eq 'url' && $text_url =~ m/^\s*$/i)) {
332 print STDERR "$progname: falling back to 'date'\n" if ($verbose);
336 if ($text_mode eq 'literal') {
337 $text_literal = strftime ($text_literal, localtime);
338 $text_literal =~ y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
339 print STDOUT $text_literal;
340 print STDOUT "\n" unless ($text_literal =~ m/\n$/s);
342 } elsif ($text_mode eq 'file') {
344 $text_file =~ s@^~/@$ENV{HOME}/@s; # allow literal "~/"
347 if (open (IN, "<$text_file")) {
348 print STDERR "$progname: reading $text_file\n" if ($verbose);
350 if ($wrap_columns && $wrap_columns > 0) {
351 # read it, then reformat it.
353 while (<IN>) { $body .= $_; }
354 reformat_text ($body);
358 y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
364 error ("$text_file: $!");
367 } elsif ($text_mode eq 'program') {
369 my ($prog, $args) = ($text_program =~ m/^([^\s]+)(.*)$/);
370 $text_program = which ($prog) . $args;
371 print STDERR "$progname: running $text_program\n" if ($verbose);
373 if ($wrap_columns && $wrap_columns > 0) {
374 # read it, then reformat it.
375 my $body = `( $text_program ) 2>&1`;
376 reformat_text ($body);
379 safe_system ("$text_program");
382 } elsif ($text_mode eq 'url') {
384 get_url_text ($text_url);
386 } else { # $text_mode eq 'date'
389 $n =~ s/\.local\n/\n/s;
394 if (-f "/etc/redhat-release") { # "Fedora Core release 4 (Stentz)"
395 safe_system ("cat", "/etc/redhat-release");
398 if (-f "/etc/release") { # "Solaris 10 3/05 s10_74L2a X86"
399 safe_system ("head", "-1", "/etc/release");
402 if (-f "/usr/sbin/system_profiler") { # "Mac OS X 10.4.5 (8H14)"
404 `/usr/sbin/system_profiler SPSoftwareDataType SPHardwareDataType`;
405 my ($v) = ($sp =~ m/^\s*System Version:\s*(.*)$/mi);
406 my ($s) = ($sp =~ m/^\s*(?:CPU|Processor) Speed:\s*(.*)$/mi);
407 my ($t) = ($sp =~ m/^\s*(?:Machine|Model) Name:\s*(.*)$/mi);
408 print "$v\n" if ($v);
409 print "$s $t\n" if ($s && $t);
410 $unamep = !defined ($v);
414 safe_system ("uname", "-sr"); # "Linux 2.6.15-1.1831_FC4"
418 safe_system ("date", "+%c");
421 $ut =~ s/^[ \d:]*(am|pm)?//i;
422 $ut =~ s/,\s*(load)/\n$1/;
429 # Loads the given URL, returns: $http, $head, $body.
432 my ($url, $referer) = @_;
434 $url =~ s@^feed:@http:@si;
435 if (! ($url =~ m@^http://@i)) {
436 error ("not an HTTP URL: $url");
439 my ($url_proto, $dummy, $serverstring, $path) = split(/\//, $url, 4);
440 $path = "" unless $path;
442 my ($them,$port) = split(/:/, $serverstring);
443 $port = 80 unless $port;
448 $serverstring = $http_proxy if $http_proxy;
449 $serverstring =~ s@^[a-z]+://@@;
450 ($them2,$port2) = split(/:/, $serverstring);
451 $port2 = 80 unless $port2;
454 my ($remote, $iaddr, $paddr, $proto, $line);
456 if ($port2 =~ /\D/) { $port2 = getservbyname($port2, 'tcp') }
458 error ("unrecognised port in $url");
461 $iaddr = inet_aton($remote);
462 error ("host not found: $remote") unless ($iaddr);
464 $paddr = sockaddr_in($port2, $iaddr);
470 $proto = getprotobyname('tcp');
471 if (!socket(S, PF_INET, SOCK_STREAM, $proto)) {
472 error ("socket: $!");
474 if (!connect(S, $paddr)) {
475 error ("connect($serverstring): $!");
478 select(S); $| = 1; select(STDOUT);
480 my $user_agent = "$progname/$version";
482 my $hdrs = ("GET " . ($http_proxy ? $url : "/$path") . " HTTP/1.0\r\n" .
484 "User-Agent: $user_agent\r\n");
486 $hdrs .= "Referer: $referer\r\n";
491 foreach (split('\r?\n', $hdrs)) {
492 print STDERR " ==> $_\n";
496 my $http = <S> || "";
500 print STDERR " <== $_\n" if ($verbose > 3);
506 print STDERR " <== $_\n" if ($verbose > 3);
509 print STDERR " <== \n" if ($verbose > 4);
513 print STDERR " <== $_" if ($verbose > 4);
518 print STDERR " <== [ body ]: $lines lines, " . length($body) . " bytes\n"
524 error ("null response: $url");
527 return ( $http, $head, $body );
531 # Loads the given URL, processes redirects, returns (content-type, body).
534 my ($url, $referer) = @_;
536 print STDERR "$progname: loading $url\n" if ($verbose > 2);
540 my $max_loop_count = 10;
543 my ( $http, $head, $body ) = get_url_1 ($url, $referer);
545 $http =~ s/[\r\n]+$//s;
547 if ( $http =~ m@^HTTP/[0-9.]+ 30[123]@ ) {
550 my ( $location ) = m@^location:[ \t]*(.*)$@im;
552 $location =~ s/[\r\n]$//;
554 print STDERR "$progname: redirect from $url to $location\n"
561 $referer =~ m@^(http://[^/]+)@i;
563 } elsif (! ($url =~ m@^[a-z]+:@i)) {
565 s@[^/]+$@@g if m@^http://[^/]+/@i;
566 $_ .= "/" if m@^http://[^/]+$@i;
571 error ("no Location with \"$http\"");
574 if ($loop_count++ > $max_loop_count) {
575 error ("too many redirects ($max_loop_count) from $orig_url");
578 } elsif ( $http =~ m@^HTTP/[0-9.]+ ([4-9][0-9][0-9].*)$@ ) {
579 error ("failed: $1 ($url)");
582 my $ct = 'text/plain';
583 $ct = $1 if ($head =~ m/^content-type:\s*([^\s]+)/mi);
590 # Make an educated guess as to what's in this document.
591 # We don't necessarily take the Content-Type header at face value.
592 # Returns 'html', 'rss', or 'text';
594 sub guess_content_type($$) {
595 my ($ct, $body) = @_;
597 $body =~ s/^(.{512}).*/$1/s; # only look in first half K of file
599 if ($ct =~ m@^text/.*html@i) { return 'html'; }
600 if ($ct =~ m@\b(atom|rss|xml)\b@i) { return 'rss'; }
602 if ($body =~ m@^\s*<\?xml@is) { return 'rss'; }
603 if ($body =~ m@^\s*<!DOCTYPE RSS@is) { return 'rss'; }
604 if ($body =~ m@^\s*<!DOCTYPE HTML@is) { return 'html'; }
606 if ($body =~ m@<(BASE|HTML|HEAD|BODY|SCRIPT|STYLE|TABLE|A\s+HREF)\b@i) {
610 if ($body =~ m@<(RSS|CHANNEL|GENERATOR|DESCRIPTION|CONTENT|FEED|ENTRY)\b@i) {
617 sub reformat_html($$) {
618 my ($body, $rss_p) = @_;
621 # In HTML, try to preserve newlines inside of PRE.
624 s@(<PRE\b[^<>]*>\s*)(.*?)(</PRE)@{
625 my ($a, $b, $c) = ($1, $2, $3);
626 $b =~ s/[\r\n]/<BR>/gs;
632 # In HTML, unfold lines.
633 # In RSS, assume \n means literal line break.
637 s@<!--.*?-->@@gsi; # lose comments
638 s@<(STYLE|SCRIPT)\b[^<>]*>.*?</\1\s*>@@gsi; # lose css and js
640 s@</?(BR|TR|TD|LI|DIV)\b[^<>]*>@\n@gsi; # line break at BR, TD, DIV, etc
641 s@</?(P|UL|OL|BLOCKQUOTE)\b[^<>]*>@\n\n@gsi; # two line breaks
643 s@<lj\s+user=\"?([^<>\"]+)\"?[^<>]*>?@$1@gsi; # handle <LJ USER=>
644 s@</?[BI]>@*@gsi; # bold, italic => asterisks
647 s@<[^<>]*>?@@gs; # lose all other HTML tags
648 $_ = de_entify ($_); # convert HTML entities
650 # elide any remaining non-Latin1 binary data...
651 s/([\177-\377]+(\s*[\177-\377]+)[^a-z\d]*)/«...» /g;
652 #s/([\177-\377]+(\s*[\177-\377]+)[^a-z\d]*)/«$1» /g;
656 s/[ \t]+$//gm; # lose whitespace at end of line
657 s@\n\n\n+@\n\n@gs; # compress blank lines
659 if (!defined($wrap_columns) || $wrap_columns > 0) {
660 $Text::Wrap::columns = ($wrap_columns || 72);
661 $_ = wrap ("", " ", $_); # wrap the lines as a paragraph
662 s/[ \t]+$//gm; # lose whitespace at end of line again
667 y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
672 sub reformat_rss($) {
675 $body =~ s/(<(ITEM|ENTRY)\b)/\001\001$1/gsi;
676 my @items = split (/\001\001/, $body);
678 print STDERR "$progname: converting RSS ($#items items)...\n"
683 # Let's skip forward in the stream by a random amount, so that if
684 # two copies of ljlatest are running at the same time (e.g., on a
685 # multi-headed machine), they get different text. (Put the items
686 # that we take off the front back on the back.)
689 my $n = int (rand ($#items - 5));
690 print STDERR "$progname: rotating by $n items...\n" if ($verbose > 2);
692 push @items, (shift @items);
700 my ($title, $body1, $body2, $body3);
702 $title = $3 if (m@<((TITLE) [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
703 $body1 = $3 if (m@<((DESCRIPTION) [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
704 $body2 = $3 if (m@<((CONTENT) [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
705 $body3 = $3 if (m@<((SUMMARY) [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
707 # If there are both <description> and <content> or <content:encoded>,
708 # use whichever one contains more text.
710 if ($body3 && length($body3) >= length($body2 || '')) {
713 if ($body2 && length($body2) >= length($body1 || '')) {
719 print STDERR "$progname: no body in item $i (\"$title\")\n"
722 print STDERR "$progname: no body or title in item $i\n"
728 $title = rss_field_to_html ($title || '');
729 $body1 = rss_field_to_html ($body1 || '');
731 $title = '' if ($body1 eq $title); # Identical in Twitter's atom feed.
733 reformat_html ("$title<P>$body1", 1);
739 sub rss_field_to_html($) {
742 # Assume that if <![CDATA[...]]> is present, everything inside that.
744 if ($body =~ m/^\s*<!\[CDATA\[(.*?)\]\s*\]/is) {
747 $body = de_entify ($body); # convert entities to get HTML from XML
750 $body = de_unicoddle ($body); # convert UTF8 to Latin1
755 sub reformat_text($) {
758 # only re-wrap if --cols was specified. Otherwise, dump it as is.
760 if ($wrap_columns && $wrap_columns > 0) {
761 print STDERR "$progname: wrapping at $wrap_columns...\n" if ($verbose > 2);
762 $Text::Wrap::columns = $wrap_columns;
763 $body = wrap ("", "", $body);
764 $body =~ s/[ \t]+$//gm;
767 $body =~ y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
772 # Figure out what the proxy server should be, either from environment
773 # variables or by parsing the output of the (MacOS) program "scutil",
774 # which tells us what the system-wide proxy settings are.
778 # historical suckage: the environment variable name is lower case.
779 $http_proxy = $ENV{http_proxy} || $ENV{HTTP_PROXY};
781 if (defined ($http_proxy)) {
782 if ($http_proxy && $http_proxy =~ m@^http://([^/]*)/?$@ ) {
783 # historical suckage: allow "http://host:port" as well as "host:port".
788 my $proxy_data = `scutil --proxy 2>/dev/null`;
789 my ($server) = ($proxy_data =~ m/\bHTTPProxy\s*:\s*([^\s]+)/s);
790 my ($port) = ($proxy_data =~ m/\bHTTPPort\s*:\s*([^\s]+)/s);
791 # Note: this ignores the "ExceptionsList".
793 $http_proxy = $server;
794 $http_proxy .= ":$port" if $port;
798 print STDERR "$progname: proxy server: $http_proxy\n"
799 if ($verbose > 2 && $http_proxy);
803 sub get_url_text($) {
808 my ($ct, $body) = get_url ($url);
810 $ct = guess_content_type ($ct, $body);
812 print STDERR "$progname: converting HTML...\n" if ($verbose > 2);
813 reformat_html ($body, 0);
814 } elsif ($ct eq 'rss') {
815 reformat_rss ($body);
817 print STDERR "$progname: plain text...\n" if ($verbose > 2);
818 reformat_text ($body);
826 print STDERR "$progname: $err\n";
831 print STDERR "usage: $progname [ --options ... ]\n" .
833 " Prints out some text for use by various screensavers,\n" .
834 " according to the options in the ~/.xscreensaver file.\n" .
835 " This may dump the contents of a file, run a program,\n" .
840 " --date Print the host name and current time.\n" .
842 " --text STRING Print out the given text. It may contain %\n" .
843 " escape sequences as per strftime(2).\n" .
845 " --file PATH Print the contents of the given file.\n" .
846 " If --cols is specified, re-wrap the lines;\n" .
847 " otherwise, print them as-is.\n" .
849 " --program CMD Run the given program and print its output.\n" .
850 " If --cols is specified, re-wrap the output.\n" .
852 " --url HTTP-URL Download and print the contents of the HTTP\n" .
853 " document. If it contains HTML, RSS, or Atom,\n" .
854 " it will be converted to plain-text.\n" .
856 " --cols N Wrap lines at this column. Default 72.\n" .
864 my $cocoa_id = undef;
866 while ($#ARGV >= 0) {
868 if ($_ eq "--verbose") { $verbose++; }
869 elsif (m/^-v+$/) { $verbose += length($_)-1; }
870 elsif (m/^--?date$/) { $text_mode = 'date';
872 elsif (m/^--?text$/) { $text_mode = 'literal';
873 $text_literal = shift @ARGV || '';
875 elsif (m/^--?file$/) { $text_mode = 'file';
876 $text_file = shift @ARGV || '';
878 elsif (m/^--?program$/) { $text_mode = 'program';
879 $text_program = shift @ARGV || '';
881 elsif (m/^--?url$/) { $text_mode = 'url';
882 $text_url = shift @ARGV || '';
884 elsif (m/^--?col(umn)?s?$/) { $wrap_columns = 0 + shift @ARGV; }
885 elsif (m/^--?cocoa$/) { $cocoa_id = shift @ARGV; }
886 elsif (m/^--?nyarlathotep$/) { $nyarlathotep_p++; }
887 elsif (m/^-./) { usage; }
893 if (!defined ($cocoa_id)) {
894 # see OSX/XScreenSaverView.m
895 $cocoa_id = $ENV{XSCREENSAVER_CLASSPATH};
898 if (defined ($cocoa_id)) {
899 get_cocoa_prefs($cocoa_id);
908 if (defined ($cocoa_id)) {
910 # On MacOS, sleep for 10 seconds between when the last output is
911 # printed, and when this process exits. This is because MacOS
912 # 10.5.0 and later broke ptys in a new and exciting way: basically,
913 # once the process at the end of the pty exits, you have exactly
914 # 1 second to read all the queued data off the pipe before it is
917 # Many of the screen savers were written to depend on being able
918 # to read a small number of bytes, and continue reading until they
919 # reached EOF. This is no longer possible.
921 # Note that the current MacOS behavior has all four of these
922 # awesome properties: 1) Inconvenient; 2) Has no sane workaround;
923 # 3) Different behavior than MacOS 10.1 through 10.4; and 4)
924 # Different behavior than every other Unix in the world.
926 # See http://jwz.livejournal.com/817438.html, and for those of
927 # you inside Apple, "Problem ID 5606018".
929 # One workaround would be to rewrite the savers to have an
930 # internal buffer, and always read as much data as possible as
931 # soon as a pipe has input available. However, that's a lot more
932 # work, so instead, let's just not exit right away, and hope that
933 # 10 seconds is enough.
935 # This will solve the problem for invocations of xscreensaver-text
936 # that produce little output (e.g., date-mode); and won't solve it
937 # in cases where a large amount of text is generated in a short
938 # amount of time (e.g., url-mode.)