2 # Copyright © 2005-2012 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
27 # Some Linux systems don't install LWP by default!
28 # Only error out if we're actually loading a URL instead of local data.
29 BEGIN { eval 'use LWP::UserAgent;' }
32 use POSIX qw(strftime);
33 use Text::Wrap qw(wrap);
36 my $progname = $0; $progname =~ s@.*/@@g;
37 my $version = q{ $Revision: 1.26 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
40 my $http_proxy = undef;
42 my $config_file = $ENV{HOME} . "/.xscreensaver";
43 my $text_mode = 'date';
44 my $text_literal = '';
46 my $text_program = '';
47 my $text_url = 'http://twitter.com/statuses/public_timeline.atom';
48 # Default URL needs to be set and match what's in OSX/XScreenSaverView.m
50 my $wrap_columns = undef;
51 my $nyarlathotep_p = 0;
54 # Maps HTML character entities to the corresponding Latin1 characters.
57 "quot" => '"', "amp" => '&', "lt" => '<', "gt" => '>',
58 "nbsp" => ' ', "iexcl" => '¡', "cent" => '¢', "pound" => '£',
59 "curren" => '¤', "yen" => '¥', "brvbar" => '¦', "sect" => '§',
60 "uml" => '¨', "copy" => '©', "ordf" => 'ª', "laquo" => '«',
61 "not" => '¬', "shy" => '', "reg" => '®', "macr" => '¯',
62 "deg" => '°', "plusmn" => '±', "sup2" => '²', "sup3" => '³',
63 "acute" => '´', "micro" => 'µ', "para" => '¶', "middot" => '·',
64 "cedil" => '¸', "sup1" => '¹', "ordm" => 'º', "raquo" => '»',
65 "frac14" => '¼', "frac12" => '½', "frac34" => '¾', "iquest" => '¿',
66 "Agrave" => 'À', "Aacute" => 'Á', "Acirc" => 'Â', "Atilde" => 'Ã',
67 "Auml" => 'Ä', "Aring" => 'Å', "AElig" => 'Æ', "Ccedil" => 'Ç',
68 "Egrave" => 'È', "Eacute" => 'É', "Ecirc" => 'Ê', "Euml" => 'Ë',
69 "Igrave" => 'Ì', "Iacute" => 'Í', "Icirc" => 'Î', "Iuml" => 'Ï',
70 "ETH" => 'Ð', "Ntilde" => 'Ñ', "Ograve" => 'Ò', "Oacute" => 'Ó',
71 "Ocirc" => 'Ô', "Otilde" => 'Õ', "Ouml" => 'Ö', "times" => '×',
72 "Oslash" => 'Ø', "Ugrave" => 'Ù', "Uacute" => 'Ú', "Ucirc" => 'Û',
73 "Uuml" => 'Ü', "Yacute" => 'Ý', "THORN" => 'Þ', "szlig" => 'ß',
74 "agrave" => 'à', "aacute" => 'á', "acirc" => 'â', "atilde" => 'ã',
75 "auml" => 'ä', "aring" => 'å', "aelig" => 'æ', "ccedil" => 'ç',
76 "egrave" => 'è', "eacute" => 'é', "ecirc" => 'ê', "euml" => 'ë',
77 "igrave" => 'ì', "iacute" => 'í', "icirc" => 'î', "iuml" => 'ï',
78 "eth" => 'ð', "ntilde" => 'ñ', "ograve" => 'ò', "oacute" => 'ó',
79 "ocirc" => 'ô', "otilde" => 'õ', "ouml" => 'ö', "divide" => '÷',
80 "oslash" => 'ø', "ugrave" => 'ù', "uacute" => 'ú', "ucirc" => 'û',
81 "uuml" => 'ü', "yacute" => 'ý', "thorn" => 'þ', "yuml" => 'ÿ',
84 # HTML 4 entities that do not have 1:1 Latin1 mappings.
85 "bull" => "*", "hellip"=> "...", "prime" => "'", "Prime" => "\"",
86 "frasl" => "/", "trade" => "[tm]", "larr" => "<-", "rarr" => "->",
87 "harr" => "<->", "lArr" => "<=", "rArr" => "=>", "hArr" => "<=>",
88 "empty" => "Ø", "minus" => "-", "lowast"=> "*", "sim" => "~",
89 "cong" => "=~", "asymp" => "~", "ne" => "!=", "equiv" => "==",
90 "le" => "<=", "ge" => ">=", "lang" => "<", "rang" => ">",
91 "loz" => "<>", "OElig" => "OE", "oelig" => "oe", "Yuml" => "Y",
92 "circ" => "^", "tilde" => "~", "ensp" => " ", "emsp" => " ",
93 "thinsp"=> " ", "ndash" => "-", "mdash" => "-", "lsquo" => "`",
94 "rsquo" => "'", "sbquo" => "'", "ldquo" => "\"", "rdquo" => "\"",
95 "bdquo" => "\"", "lsaquo"=> "<", "rsaquo"=> ">",
98 # Maps certain UTF8 characters (2 or 3 bytes) to the corresponding
101 my %unicode_latin1_table = (
102 "\xC2\xA1" => '¡', "\xC2\xA2" => '¢', "\xC2\xA3" => '£', "\xC2\xA4" => '¤',
103 "\xC2\xA5" => '¥', "\xC2\xA6" => '¦', "\xC2\xA7" => '§', "\xC2\xA8" => '¨',
104 "\xC2\xA9" => '©', "\xC2\xAA" => 'ª', "\xC2\xAB" => '«', "\xC2\xAC" => '¬',
105 "\xC2\xAD" => '', "\xC2\xAE" => '®', "\xC2\xAF" => '¯', "\xC2\xB0" => '°',
106 "\xC2\xB1" => '±', "\xC2\xB2" => '²', "\xC2\xB3" => '³', "\xC2\xB4" => '´',
107 "\xC2\xB5" => 'µ', "\xC2\xB6" => '¶', "\xC2\xB7" => '·', "\xC2\xB8" => '¸',
108 "\xC2\xB9" => '¹', "\xC2\xBA" => 'º', "\xC2\xBB" => '»', "\xC2\xBC" => '¼',
109 "\xC2\xBD" => '½', "\xC2\xBE" => '¾', "\xC2\xBF" => '¿', "\xC3\x80" => 'À',
110 "\xC3\x81" => 'Á', "\xC3\x82" => 'Â', "\xC3\x83" => 'Ã', "\xC3\x84" => 'Ä',
111 "\xC3\x85" => 'Å', "\xC3\x86" => 'Æ', "\xC3\x87" => 'Ç', "\xC3\x88" => 'È',
112 "\xC3\x89" => 'É', "\xC3\x8A" => 'Ê', "\xC3\x8B" => 'Ë', "\xC3\x8C" => 'Ì',
113 "\xC3\x8D" => 'Í', "\xC3\x8E" => 'Î', "\xC3\x8F" => 'Ï', "\xC3\x90" => 'Ð',
114 "\xC3\x91" => 'Ñ', "\xC3\x92" => 'Ò', "\xC3\x93" => 'Ó', "\xC3\x94" => 'Ô',
115 "\xC3\x95" => 'Õ', "\xC3\x96" => 'Ö', "\xC3\x97" => '×', "\xC3\x98" => 'Ø',
116 "\xC3\x99" => 'Ù', "\xC3\x9A" => 'Ú', "\xC3\x9B" => 'Û', "\xC3\x9C" => 'Ü',
117 "\xC3\x9D" => 'Ý', "\xC3\x9E" => 'Þ', "\xC3\x9F" => 'ß', "\xC3\xA0" => 'à',
118 "\xC3\xA1" => 'á', "\xC3\xA2" => 'â', "\xC3\xA3" => 'ã', "\xC3\xA4" => 'ä',
119 "\xC3\xA5" => 'å', "\xC3\xA6" => 'æ', "\xC3\xA7" => 'ç', "\xC3\xA8" => 'è',
120 "\xC3\xA9" => 'é', "\xC3\xAA" => 'ê', "\xC3\xAB" => 'ë', "\xC3\xAC" => 'ì',
121 "\xC3\xAD" => 'í', "\xC3\xAE" => 'î', "\xC3\xAF" => 'ï', "\xC3\xB0" => 'ð',
122 "\xC3\xB1" => 'ñ', "\xC3\xB2" => 'ò', "\xC3\xB3" => 'ó', "\xC3\xB4" => 'ô',
123 "\xC3\xB5" => 'õ', "\xC3\xB6" => 'ö', "\xC3\xB7" => '÷', "\xC3\xB8" => 'ø',
124 "\xC3\xB9" => 'ù', "\xC3\xBA" => 'ú', "\xC3\xBB" => 'û', "\xC3\xBC" => 'ü',
125 "\xC3\xBD" => 'ý', "\xC3\xBE" => 'þ', "\xC3\xBF" => 'ÿ',
127 "\xE2\x80\x93" => '--', "\xE2\x80\x94" => '--',
128 "\xE2\x80\x98" => '`', "\xE2\x80\x99" => '\'',
129 "\xE2\x80\x9C" => "``", "\xE2\x80\x9D" => "''",
130 "\xE2\x80\xA6" => '...',
134 # Convert any HTML entities to Latin1 characters.
138 $text =~ s/(&(\#)?([[:alpha:]\d]+);?)/
142 $c = $entity_table{$c}; # for Á
144 if ($c =~ m@^x([\dA-F]+)$@si) { # for A
146 } elsif ($c =~ m@^\d+$@si) { # for A
152 ($c || "[$3]"); # for &unknown; => "[unknown]"
159 # Convert any Unicode characters to Latin1 if possible.
160 # Unconvertable bytes are left alone.
162 sub de_unicoddle($) {
164 foreach my $key (keys (%unicode_latin1_table)) {
165 my $val = $unicode_latin1_table{$key};
166 $text =~ s/$key/$val/gs;
172 # Reads the prefs we use from ~/.xscreensaver
174 sub get_x11_prefs() {
178 if (open (IN, "<$config_file")) {
179 print STDERR "$progname: reading $config_file\n" if ($verbose > 1);
181 while (<IN>) { $body .= $_; }
183 $got_any_p = get_x11_prefs_1 ($body);
185 } elsif ($verbose > 1) {
186 print STDERR "$progname: $config_file: $!\n";
189 if (! $got_any_p && defined ($ENV{DISPLAY})) {
190 # We weren't able to read settings from the .xscreensaver file.
191 # Fall back to any settings in the X resource database
192 # (/usr/X11R6/lib/X11/app-defaults/XScreenSaver)
194 print STDERR "$progname: reading X resources\n" if ($verbose > 1);
195 my $body = `appres XScreenSaver xscreensaver -1`;
196 $got_any_p = get_x11_prefs_1 ($body);
200 printf STDERR "$progname: mode: $text_mode\n";
201 printf STDERR "$progname: literal: $text_literal\n";
202 printf STDERR "$progname: file: $text_file\n";
203 printf STDERR "$progname: program: $text_program\n";
204 printf STDERR "$progname: url: $text_url\n";
207 $text_mode =~ tr/A-Z/a-z/;
208 $text_literal =~ s@\\n@\n@gs;
209 $text_literal =~ s@\\\n@\n@gs;
213 sub get_x11_prefs_1($) {
219 if ($body =~ m/^[.*]*textMode:[ \t]*([^\s]+)\s*$/im) {
223 if ($body =~ m/^[.*]*textLiteral:[ \t]*(.*?)[ \t]*$/im) {
226 if ($body =~ m/^[.*]*textFile:[ \t]*(.*?)[ \t]*$/im) {
229 if ($body =~ m/^[.*]*textProgram:[ \t]*(.*?)[ \t]*$/im) {
232 if ($body =~ m/^[.*]*textURL:[ \t]*(.*?)[ \t]*$/im) {
240 sub get_cocoa_prefs($) {
244 print STDERR "$progname: reading Cocoa prefs: \"$id\"\n" if ($verbose > 1);
246 $v = get_cocoa_pref_1 ($id, "textMode");
247 $text_mode = $v if defined ($v);
249 # The "textMode" pref is set to a number instead of a string because I
250 # can't figure out the black magic to make Cocoa bindings work right.
252 if ($text_mode eq '0') { $text_mode = 'date'; }
253 elsif ($text_mode eq '1') { $text_mode = 'literal'; }
254 elsif ($text_mode eq '2') { $text_mode = 'file'; }
255 elsif ($text_mode eq '3') { $text_mode = 'url'; }
256 elsif ($text_mode eq '4') { $text_mode = 'program'; }
258 $v = get_cocoa_pref_1 ($id, "textLiteral");
259 $text_literal = $v if defined ($v);
260 $text_literal =~ s@\\n@\n@gs;
261 $text_literal =~ s@\\\n@\n@gs;
263 $v = get_cocoa_pref_1 ($id, "textFile");
264 $text_file = $v if defined ($v);
266 $v = get_cocoa_pref_1 ($id, "textProgram");
267 $text_program = $v if defined ($v);
269 $v = get_cocoa_pref_1 ($id, "textURL");
270 $text_url = $v if defined ($v);
274 sub get_cocoa_pref_1($$) {
276 # make sure there's nothing stupid/malicious in either string.
277 $id =~ s/[^-a-z\d. ]/_/gsi;
278 $key =~ s/[^-a-z\d. ]/_/gsi;
279 my $cmd = "defaults -currentHost read \"$id\" \"$key\"";
281 print STDERR "$progname: executing $cmd\n"
284 my $val = `$cmd 2>/dev/null`;
288 print STDERR "$progname: Cocoa: $id $key = \"$val\"\n"
291 $val = undef if ($val =~ m/^$/s);
297 # like system() but checks errors.
302 print STDERR "$progname: executing " . join(' ', @cmd) . "\n"
306 my $exit_value = $? >> 8;
307 my $signal_num = $? & 127;
308 my $dumped_core = $? & 128;
309 error ("$cmd[0]: core dumped!") if ($dumped_core);
310 error ("$cmd[0]: signal $signal_num!") if ($signal_num);
311 error ("$cmd[0]: exited with $exit_value!") if ($exit_value);
318 if ($cmd =~ m@^\./|^/@) {
319 error ("cannot execute $cmd") unless (-x $cmd);
323 foreach my $dir (split (/:/, $ENV{PATH})) {
324 my $cmd2 = "$dir/$cmd";
325 print STDERR "$progname: checking $cmd2\n" if ($verbose > 3);
326 return $cmd2 if (-x "$cmd2");
328 error ("$cmd not found on \$PATH");
334 # Do some basic sanity checking (null text, null file names, etc.)
336 if (($text_mode eq 'literal' && $text_literal =~ m/^\s*$/i) ||
337 ($text_mode eq 'file' && $text_file =~ m/^\s*$/i) ||
338 ($text_mode eq 'program' && $text_program =~ m/^\s*$/i) ||
339 ($text_mode eq 'url' && $text_url =~ m/^\s*$/i)) {
340 print STDERR "$progname: falling back to 'date'\n" if ($verbose);
344 if ($text_mode eq 'literal') {
345 $text_literal = strftime ($text_literal, localtime);
346 $text_literal =~ y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
347 print STDOUT $text_literal;
348 print STDOUT "\n" unless ($text_literal =~ m/\n$/s);
350 } elsif ($text_mode eq 'file') {
352 $text_file =~ s@^~/@$ENV{HOME}/@s; # allow literal "~/"
355 if (open (IN, "<$text_file")) {
356 print STDERR "$progname: reading $text_file\n" if ($verbose);
358 if ($wrap_columns && $wrap_columns > 0) {
359 # read it, then reformat it.
361 while (<IN>) { $body .= $_; }
362 reformat_text ($body);
366 y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
372 error ("$text_file: $!");
375 } elsif ($text_mode eq 'program') {
377 my ($prog, $args) = ($text_program =~ m/^([^\s]+)(.*)$/);
378 $text_program = which ($prog) . $args;
379 print STDERR "$progname: running $text_program\n" if ($verbose);
381 if ($wrap_columns && $wrap_columns > 0) {
382 # read it, then reformat it.
383 my $body = `( $text_program ) 2>&1`;
384 reformat_text ($body);
387 safe_system ("$text_program");
390 } elsif ($text_mode eq 'url') {
392 get_url_text ($text_url);
394 } else { # $text_mode eq 'date'
397 $n =~ s/\.local\n/\n/s;
402 if (-f "/etc/redhat-release") { # "Fedora Core release 4 (Stentz)"
403 safe_system ("cat", "/etc/redhat-release");
406 if (-f "/etc/release") { # "Solaris 10 3/05 s10_74L2a X86"
407 safe_system ("head", "-1", "/etc/release");
410 if (-f "/usr/sbin/system_profiler") { # "Mac OS X 10.4.5 (8H14)"
412 `/usr/sbin/system_profiler SPSoftwareDataType SPHardwareDataType`;
413 my ($v) = ($sp =~ m/^\s*System Version:\s*(.*)$/mi);
414 my ($s) = ($sp =~ m/^\s*(?:CPU|Processor) Speed:\s*(.*)$/mi);
415 my ($t) = ($sp =~ m/^\s*(?:Machine|Model) Name:\s*(.*)$/mi);
416 print "$v\n" if ($v);
417 print "$s $t\n" if ($s && $t);
418 $unamep = !defined ($v);
422 safe_system ("uname", "-sr"); # "Linux 2.6.15-1.1831_FC4"
426 safe_system ("date", "+%c");
429 $ut =~ s/^[ \d:]*(am|pm)?//i;
430 $ut =~ s/,\s*(load)/\n$1/;
437 # Make an educated guess as to what's in this document.
438 # We don't necessarily take the Content-Type header at face value.
439 # Returns 'html', 'rss', or 'text';
441 sub guess_content_type($$) {
442 my ($ct, $body) = @_;
444 $body =~ s/^(.{512}).*/$1/s; # only look in first half K of file
446 if ($ct =~ m@^text/.*html@i) { return 'html'; }
447 if ($ct =~ m@\b(atom|rss|xml)\b@i) { return 'rss'; }
449 if ($body =~ m@^\s*<\?xml@is) { return 'rss'; }
450 if ($body =~ m@^\s*<!DOCTYPE RSS@is) { return 'rss'; }
451 if ($body =~ m@^\s*<!DOCTYPE HTML@is) { return 'html'; }
453 if ($body =~ m@<(BASE|HTML|HEAD|BODY|SCRIPT|STYLE|TABLE|A\s+HREF)\b@i) {
457 if ($body =~ m@<(RSS|CHANNEL|GENERATOR|DESCRIPTION|CONTENT|FEED|ENTRY)\b@i) {
465 sub reformat_html($$) {
466 my ($body, $rss_p) = @_;
469 # In HTML, try to preserve newlines inside of PRE.
472 s@(<PRE\b[^<>]*>\s*)(.*?)(</PRE)@{
473 my ($a, $b, $c) = ($1, $2, $3);
474 $b =~ s/[\r\n]/<BR>/gs;
480 # In HTML, unfold lines.
481 # In RSS, assume \n means literal line break.
485 s@<!--.*?-->@@gsi; # lose comments
486 s@<(STYLE|SCRIPT)\b[^<>]*>.*?</\1\s*>@@gsi; # lose css and js
488 s@</?(BR|TR|TD|LI|DIV)\b[^<>]*>@\n@gsi; # line break at BR, TD, DIV, etc
489 s@</?(P|UL|OL|BLOCKQUOTE)\b[^<>]*>@\n\n@gsi; # two line breaks
491 s@<lj\s+user=\"?([^<>\"]+)\"?[^<>]*>?@$1@gsi; # handle <LJ USER=>
492 s@</?[BI]>@*@gsi; # bold, italic => asterisks
495 s@<[^<>]*>?@@gs; # lose all other HTML tags
496 $_ = de_entify ($_); # convert HTML entities
498 # elide any remaining non-Latin1 binary data...
499 s/([\177-\377]+(\s*[\177-\377]+)[^a-z\d]*)/«...» /g;
500 #s/([\177-\377]+(\s*[\177-\377]+)[^a-z\d]*)/«$1» /g;
504 s/[ \t]+$//gm; # lose whitespace at end of line
505 s@\n\n\n+@\n\n@gs; # compress blank lines
507 if (!defined($wrap_columns) || $wrap_columns > 0) {
508 $Text::Wrap::columns = ($wrap_columns || 72);
509 $_ = wrap ("", " ", $_); # wrap the lines as a paragraph
510 s/[ \t]+$//gm; # lose whitespace at end of line again
515 y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
520 sub reformat_rss($) {
523 $body =~ s/(<(ITEM|ENTRY)\b)/\001\001$1/gsi;
524 my @items = split (/\001\001/, $body);
526 print STDERR "$progname: converting RSS ($#items items)...\n"
531 # Let's skip forward in the stream by a random amount, so that if
532 # two copies of ljlatest are running at the same time (e.g., on a
533 # multi-headed machine), they get different text. (Put the items
534 # that we take off the front back on the back.)
537 my $n = int (rand ($#items - 5));
538 print STDERR "$progname: rotating by $n items...\n" if ($verbose > 2);
540 push @items, (shift @items);
548 my ($title, $body1, $body2, $body3);
550 $title = $3 if (m@<((TITLE) [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
551 $body1 = $3 if (m@<((DESCRIPTION) [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
552 $body2 = $3 if (m@<((CONTENT) [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
553 $body3 = $3 if (m@<((SUMMARY) [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
555 # If there are both <description> and <content> or <content:encoded>,
556 # use whichever one contains more text.
558 if ($body3 && length($body3) >= length($body2 || '')) {
561 if ($body2 && length($body2) >= length($body1 || '')) {
567 print STDERR "$progname: no body in item $i (\"$title\")\n"
570 print STDERR "$progname: no body or title in item $i\n"
576 $title = rss_field_to_html ($title || '');
577 $body1 = rss_field_to_html ($body1 || '');
579 $title = '' if ($body1 eq $title); # Identical in Twitter's atom feed.
581 reformat_html ("$title<P>$body1", 1);
587 sub rss_field_to_html($) {
590 # Assume that if <![CDATA[...]]> is present, everything inside that.
592 if ($body =~ m/^\s*<!\[CDATA\[(.*?)\]\s*\]/is) {
595 $body = de_entify ($body); # convert entities to get HTML from XML
598 $body = de_unicoddle ($body); # convert UTF8 to Latin1
603 sub reformat_text($) {
606 # only re-wrap if --cols was specified. Otherwise, dump it as is.
608 if ($wrap_columns && $wrap_columns > 0) {
609 print STDERR "$progname: wrapping at $wrap_columns...\n" if ($verbose > 2);
610 $Text::Wrap::columns = $wrap_columns;
611 $body = wrap ("", "", $body);
612 $body =~ s/[ \t]+$//gm;
615 $body =~ y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
620 # Figure out what the proxy server should be, either from environment
621 # variables or by parsing the output of the (MacOS) program "scutil",
622 # which tells us what the system-wide proxy settings are.
627 if (!defined($ENV{http_proxy}) && !defined($ENV{HTTP_PROXY})) {
628 my $proxy_data = `scutil --proxy 2>/dev/null`;
629 my ($server) = ($proxy_data =~ m/\bHTTPProxy\s*:\s*([^\s]+)/s);
630 my ($port) = ($proxy_data =~ m/\bHTTPPort\s*:\s*([^\s]+)/s);
632 # Note: this ignores the "ExceptionsList".
633 $ENV{http_proxy} = "http://" . $server . ($port ? ":$port" : "") . "/";
634 print STDERR "$progname: MacOS proxy: $ENV{http_proxy}\n"
643 sub get_url_text($) {
646 my $ua = eval 'LWP::UserAgent->new';
649 print STDOUT ("\n\tPerl is broken. Do this to repair it:\n" .
650 "\n\tsudo cpan LWP::UserAgent\n\n");
655 $ua->agent ("$progname/$version");
656 my $res = $ua->get ($url);
660 if ($res && $res->is_success) {
661 $body = $res->decoded_content || '';
662 $ct = $res->header ('Content-Type') || 'text/plain';
665 my $err = ($res ? $res->status_line : '') || '';
666 $err = 'unknown error' unless $err;
669 $body = "Error loading URL $err\n\n";
673 $ct = guess_content_type ($ct, $body);
675 print STDERR "$progname: converting HTML...\n" if ($verbose > 2);
676 reformat_html ($body, 0);
677 } elsif ($ct eq 'rss') {
678 reformat_rss ($body);
680 print STDERR "$progname: plain text...\n" if ($verbose > 2);
681 reformat_text ($body);
689 print STDERR "$progname: $err\n";
694 print STDERR "usage: $progname [ --options ... ]\n" .
696 " Prints out some text for use by various screensavers,\n" .
697 " according to the options in the ~/.xscreensaver file.\n" .
698 " This may dump the contents of a file, run a program,\n" .
703 " --date Print the host name and current time.\n" .
705 " --text STRING Print out the given text. It may contain %\n" .
706 " escape sequences as per strftime(2).\n" .
708 " --file PATH Print the contents of the given file.\n" .
709 " If --cols is specified, re-wrap the lines;\n" .
710 " otherwise, print them as-is.\n" .
712 " --program CMD Run the given program and print its output.\n" .
713 " If --cols is specified, re-wrap the output.\n" .
715 " --url HTTP-URL Download and print the contents of the HTTP\n" .
716 " document. If it contains HTML, RSS, or Atom,\n" .
717 " it will be converted to plain-text.\n" .
719 " --cols N Wrap lines at this column. Default 72.\n" .
727 my $cocoa_id = undef;
729 while ($#ARGV >= 0) {
731 if ($_ eq "--verbose") { $verbose++; }
732 elsif (m/^-v+$/) { $verbose += length($_)-1; }
733 elsif (m/^--?date$/) { $text_mode = 'date';
735 elsif (m/^--?text$/) { $text_mode = 'literal';
736 $text_literal = shift @ARGV || '';
738 elsif (m/^--?file$/) { $text_mode = 'file';
739 $text_file = shift @ARGV || '';
741 elsif (m/^--?program$/) { $text_mode = 'program';
742 $text_program = shift @ARGV || '';
744 elsif (m/^--?url$/) { $text_mode = 'url';
745 $text_url = shift @ARGV || '';
747 elsif (m/^--?col(umn)?s?$/) { $wrap_columns = 0 + shift @ARGV; }
748 elsif (m/^--?cocoa$/) { $cocoa_id = shift @ARGV; }
749 elsif (m/^--?nyarlathotep$/) { $nyarlathotep_p++; }
750 elsif (m/^-./) { usage; }
756 if (!defined ($cocoa_id)) {
757 # see OSX/XScreenSaverView.m
758 $cocoa_id = $ENV{XSCREENSAVER_CLASSPATH};
761 if (defined ($cocoa_id)) {
762 get_cocoa_prefs($cocoa_id);
771 if (defined ($cocoa_id)) {
773 # On MacOS, sleep for 10 seconds between when the last output is
774 # printed, and when this process exits. This is because MacOS
775 # 10.5.0 and later broke ptys in a new and exciting way: basically,
776 # once the process at the end of the pty exits, you have exactly
777 # 1 second to read all the queued data off the pipe before it is
780 # Many of the screen savers were written to depend on being able
781 # to read a small number of bytes, and continue reading until they
782 # reached EOF. This is no longer possible.
784 # Note that the current MacOS behavior has all four of these
785 # awesome properties: 1) Inconvenient; 2) Has no sane workaround;
786 # 3) Different behavior than MacOS 10.1 through 10.4; and 4)
787 # Different behavior than every other Unix in the world.
789 # See http://jwz.livejournal.com/817438.html, and for those of
790 # you inside Apple, "Problem ID 5606018".
792 # One workaround would be to rewrite the savers to have an
793 # internal buffer, and always read as much data as possible as
794 # soon as a pipe has input available. However, that's a lot more
795 # work, so instead, let's just not exit right away, and hope that
796 # 10 seconds is enough.
798 # This will solve the problem for invocations of xscreensaver-text
799 # that produce little output (e.g., date-mode); and won't solve it
800 # in cases where a large amount of text is generated in a short
801 # amount of time (e.g., url-mode.)