2 # Copyright © 2005-2015 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;' }
31 # Not sure how prevalent this is. Hope it's part of the default install.
32 BEGIN { eval 'use HTML::Entities;' }
35 use POSIX qw(strftime);
36 use Text::Wrap qw(wrap);
39 my $progname = $0; $progname =~ s@.*/@@g;
40 my ($version) = ('$Revision: 1.39 $' =~ m/\s(\d[.\d]+)\s/s);
43 my $http_proxy = undef;
45 my $config_file = $ENV{HOME} . "/.xscreensaver";
46 my $text_mode = 'date';
47 my $text_literal = '';
49 my $text_program = '';
50 my $text_url = 'https://en.wikipedia.org/w/index.php?title=Special:NewPages&feed=rss';
51 # Default URL needs to be set and match what's in OSX/XScreenSaverView.m
53 my $wrap_columns = undef;
54 my $truncate_lines = undef;
56 my $nyarlathotep_p = 0;
59 # Convert any HTML entities to Latin1 characters.
64 return '' unless defined($text);
65 return $text unless ($text =~ m/&/s);
67 # Convert any HTML entities to Unicode characters,
68 # if the HTML::Entities module is installed.
72 $text = HTML::Entities::decode_entities ($t2);
74 return $text if defined($text);
76 # If it's not installed, just complain instead of trying to halfass it.
77 print STDOUT ("\n\tPerl is broken. Do this to repair it:\n" .
78 "\n\tsudo cpan HTML::Entities\n\n");
83 # Convert any Unicode characters to Latin1 if possible.
84 # Unconvertable bytes are left alone.
86 sub utf8_to_latin1($) {
89 utf8::encode ($text); # Unpack Unicode back to multi-byte UTF-8.
91 # Maybe it would be better to handle this in the Unicode domain
92 # by doing things like s/\x{2018}/\"/g, but without decoding the
93 # string back to UTF-8 first, I'm at a loss as to how to have
94 # "á" print as "\340" instead of as "\303\240".
96 $text =~ s/ \xC2 ( [\xA0-\xFF] ) / $1 /gsex;
97 $text =~ s/ \xC3 ( [\x80-\xFF] ) / chr (ord($1) | 0x40) /gsex;
99 # Handles a few 3-byte sequences too.
100 $text =~ s/\xE2\x80\x93/--/gs;
101 $text =~ s/\xE2\x80\x94/--/gs;
102 $text =~ s/\xE2\x80\x98/`/gs;
103 $text =~ s/\xE2\x80\x99/'/gs;
104 $text =~ s/\xE2\x80\x9C/``/gs;
105 $text =~ s/\xE2\x80\x9D/'/gs;
106 $text =~ s/\xE2\x80\xA2/•/gs;
107 $text =~ s/\xE2\x80\xA6/.../gs;
108 $text =~ s/\xE2\x80\xB2/'/gs;
109 $text =~ s/\xE2\x84\xA2/™/gs;
110 $text =~ s/\xE2\x86\x90/ ← /gs;
116 # Reads the prefs we use from ~/.xscreensaver
118 sub get_x11_prefs() {
121 if (open (my $in, '<', $config_file)) {
122 print STDERR "$progname: reading $config_file\n" if ($verbose > 1);
123 local $/ = undef; # read entire file
126 $got_any_p = get_x11_prefs_1 ($body);
128 } elsif ($verbose > 1) {
129 print STDERR "$progname: $config_file: $!\n";
132 if (! $got_any_p && defined ($ENV{DISPLAY})) {
133 # We weren't able to read settings from the .xscreensaver file.
134 # Fall back to any settings in the X resource database
135 # (/usr/X11R6/lib/X11/app-defaults/XScreenSaver)
137 print STDERR "$progname: reading X resources\n" if ($verbose > 1);
138 my $body = `appres XScreenSaver xscreensaver -1`;
139 $got_any_p = get_x11_prefs_1 ($body);
143 print STDERR "$progname: mode: $text_mode\n";
144 print STDERR "$progname: literal: $text_literal\n";
145 print STDERR "$progname: file: $text_file\n";
146 print STDERR "$progname: program: $text_program\n";
147 print STDERR "$progname: url: $text_url\n";
150 $text_mode =~ tr/A-Z/a-z/;
151 $text_literal =~ s@\\n@\n@gs;
152 $text_literal =~ s@\\\n@\n@gs;
156 sub get_x11_prefs_1($) {
162 if ($body =~ m/^[.*]*textMode:[ \t]*([^\s]+)\s*$/im) {
166 if ($body =~ m/^[.*]*textLiteral:[ \t]*(.*?)[ \t]*$/im) {
169 if ($body =~ m/^[.*]*textFile:[ \t]*(.*?)[ \t]*$/im) {
172 if ($body =~ m/^[.*]*textProgram:[ \t]*(.*?)[ \t]*$/im) {
175 if ($body =~ m/^[.*]*textURL:[ \t]*(.*?)[ \t]*$/im) {
183 sub get_cocoa_prefs($) {
187 print STDERR "$progname: reading Cocoa prefs: \"$id\"\n" if ($verbose > 1);
189 $v = get_cocoa_pref_1 ($id, "textMode");
190 $text_mode = $v if defined ($v);
192 # The "textMode" pref is set to a number instead of a string because I
193 # couldn't figure out the black magic to make Cocoa bindings work right.
195 # Update: as of 5.33, Cocoa writes strings instead of numbers, but
196 # pre-existing saved preferences might still have numbers in them.
198 if ($text_mode eq '0') { $text_mode = 'date'; }
199 elsif ($text_mode eq '1') { $text_mode = 'literal'; }
200 elsif ($text_mode eq '2') { $text_mode = 'file'; }
201 elsif ($text_mode eq '3') { $text_mode = 'url'; }
202 elsif ($text_mode eq '4') { $text_mode = 'program'; }
204 $v = get_cocoa_pref_1 ($id, "textLiteral");
205 $text_literal = $v if defined ($v);
206 $text_literal =~ s@\\n@\n@gs;
207 $text_literal =~ s@\\\n@\n@gs;
209 $v = get_cocoa_pref_1 ($id, "textFile");
210 $text_file = $v if defined ($v);
212 $v = get_cocoa_pref_1 ($id, "textProgram");
213 $text_program = $v if defined ($v);
215 $v = get_cocoa_pref_1 ($id, "textURL");
216 $text_url = $v if defined ($v);
220 sub get_cocoa_pref_1($$) {
222 # make sure there's nothing stupid/malicious in either string.
223 $id =~ s/[^-a-z\d. ]/_/gsi;
224 $key =~ s/[^-a-z\d. ]/_/gsi;
225 my $cmd = "defaults -currentHost read \"$id\" \"$key\"";
227 print STDERR "$progname: executing $cmd\n"
230 my $val = `$cmd 2>/dev/null`;
234 print STDERR "$progname: Cocoa: $id $key = \"$val\"\n"
237 $val = undef if ($val =~ m/^$/s);
243 # like system() but checks errors.
248 print STDERR "$progname: executing " . join(' ', @cmd) . "\n"
252 my $exit_value = $? >> 8;
253 my $signal_num = $? & 127;
254 my $dumped_core = $? & 128;
255 error ("$cmd[0]: core dumped!") if ($dumped_core);
256 error ("$cmd[0]: signal $signal_num!") if ($signal_num);
257 error ("$cmd[0]: exited with $exit_value!") if ($exit_value);
264 if ($cmd =~ m@^\./|^/@) {
265 error ("cannot execute $cmd") unless (-x $cmd);
269 foreach my $dir (split (/:/, $ENV{PATH})) {
270 my $cmd2 = "$dir/$cmd";
271 print STDERR "$progname: checking $cmd2\n" if ($verbose > 3);
272 return $cmd2 if (-x "$cmd2");
274 error ("$cmd not found on \$PATH");
280 # Do some basic sanity checking (null text, null file names, etc.)
282 if (($text_mode eq 'literal' && $text_literal =~ m/^\s*$/i) ||
283 ($text_mode eq 'file' && $text_file =~ m/^\s*$/i) ||
284 ($text_mode eq 'program' && $text_program =~ m/^\s*$/i) ||
285 ($text_mode eq 'url' && $text_url =~ m/^\s*$/i)) {
286 print STDERR "$progname: falling back to 'date'\n" if ($verbose);
290 if ($text_mode eq 'literal') {
291 $text_literal = strftime ($text_literal, localtime);
292 $text_literal = utf8_to_latin1($text_literal) if ($latin1_p);
293 $text_literal =~ y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
294 print STDOUT $text_literal;
295 print STDOUT "\n" unless ($text_literal =~ m/\n$/s);
297 } elsif ($text_mode eq 'file') {
299 $text_file =~ s@^~/@$ENV{HOME}/@s; # allow literal "~/"
301 if (open (my $in, '<', $text_file)) {
302 print STDERR "$progname: reading $text_file\n" if ($verbose);
304 if (($wrap_columns && $wrap_columns > 0) || $truncate_lines) {
305 # read it, then reformat it.
306 local $/ = undef; # read entire file
308 $body = reformat_text ($body);
313 $_ = utf8_to_latin1($_) if ($latin1_p);
314 y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
320 error ("$text_file: $!");
323 } elsif ($text_mode eq 'program') {
325 my ($prog, $args) = ($text_program =~ m/^([^\s]+)(.*)$/);
326 $text_program = which ($prog) . $args;
327 print STDERR "$progname: running $text_program\n" if ($verbose);
329 if (($wrap_columns && $wrap_columns > 0) || $truncate_lines) {
330 # read it, then reformat it.
333 my $cmd = "( $text_program ) 2>&1";
334 # $cmd .= " | sed -l"; # line buffer instead of 4k pipe buffer
335 open (my $pipe, '-|:unix', $cmd);
336 while (my $line = <$pipe>) {
339 last if ($truncate_lines && $lines > $truncate_lines);
342 $body = reformat_text ($body);
346 safe_system ("$text_program");
349 } elsif ($text_mode eq 'url') {
351 get_url_text ($text_url);
353 } else { # $text_mode eq 'date'
356 $n =~ s/\.local\n/\n/s;
361 if (-f "/etc/redhat-release") { # "Fedora Core release 4 (Stentz)"
362 safe_system ("cat", "/etc/redhat-release");
365 if (-f "/etc/release") { # "Solaris 10 3/05 s10_74L2a X86"
366 safe_system ("head", "-1", "/etc/release");
369 if (-f "/usr/sbin/system_profiler") { # "Mac OS X 10.4.5 (8H14)"
371 `/usr/sbin/system_profiler SPSoftwareDataType SPHardwareDataType 2>/dev/null`;
372 # system_profiler on OS X 10.10 generates spurious error messages.
373 my ($v) = ($sp =~ m/^\s*System Version:\s*(.*)$/mi);
374 my ($s) = ($sp =~ m/^\s*(?:CPU|Processor) Speed:\s*(.*)$/mi);
375 my ($t) = ($sp =~ m/^\s*(?:Machine|Model) Name:\s*(.*)$/mi);
376 print "$v\n" if ($v);
377 print "$s $t\n" if ($s && $t);
378 $unamep = !defined ($v);
382 safe_system ("uname", "-sr"); # "Linux 2.6.15-1.1831_FC4"
386 safe_system ("date", "+%c");
389 $ut =~ s/^[ \d:]*(am|pm)?//i;
390 $ut =~ s/,\s*(load)/\n$1/;
397 # Make an educated guess as to what's in this document.
398 # We don't necessarily take the Content-Type header at face value.
399 # Returns 'html', 'rss', or 'text';
401 sub guess_content_type($$) {
402 my ($ct, $body) = @_;
404 $body =~ s/^(.{512}).*/$1/s; # only look in first half K of file
406 if ($ct =~ m@^text/.*html@i) { return 'html'; }
407 if ($ct =~ m@\b(atom|rss|xml)\b@i) { return 'rss'; }
409 if ($body =~ m@^\s*<\?xml@is) { return 'rss'; }
410 if ($body =~ m@^\s*<!DOCTYPE RSS@is) { return 'rss'; }
411 if ($body =~ m@^\s*<!DOCTYPE HTML@is) { return 'html'; }
413 if ($body =~ m@<(BASE|HTML|HEAD|BODY|SCRIPT|STYLE|TABLE|A\s+HREF)\b@i) {
417 if ($body =~ m@<(RSS|CHANNEL|GENERATOR|DESCRIPTION|CONTENT|FEED|ENTRY)\b@i) {
425 sub reformat_html($$) {
426 my ($body, $rss_p) = @_;
429 # In HTML, try to preserve newlines inside of PRE.
432 s@(<PRE\b[^<>]*>\s*)(.*?)(</PRE)@{
433 my ($a, $b, $c) = ($1, $2, $3);
434 $b =~ s/[\r\n]/<BR>/gs;
440 # In HTML, unfold lines.
441 # In RSS, assume \n means literal line break.
445 # This right here is the part where I doom us all to inhuman
446 # toil for the One whose Name cannot be expressed in the
447 # Basic Multilingual Plane. http://jwz.org/b/yhAT He comes.
449 s@<!--.*?-->@@gsi; # lose comments
450 s@<(STYLE|SCRIPT)\b[^<>]*>.*?</\1\s*>@@gsi; # lose css and js
452 s@</?(BR|TR|TD|LI|DIV)\b[^<>]*>@\n@gsi; # line break at BR, TD, DIV, etc
453 s@</?(P|UL|OL|BLOCKQUOTE)\b[^<>]*>@\n\n@gsi; # two line breaks
455 s@<lj\s+user=\"?([^<>\"]+)\"?[^<>]*>?@$1@gsi; # handle <LJ USER=>
456 s@</?[BI]>@*@gsi; # bold, italic => asterisks
459 s@<[^<>]*>?@@gs; # lose all other HTML tags
460 $_ = de_entify ($_); # convert HTML entities
462 # For Wikipedia: delete anything inside {{ }} and unwrap [[tags]],
463 # among other things.
465 if ($rss_p eq 'wiki') {
467 s@<!--.*?-->@@gsi; # lose HTML comments again
469 # Creation line is often truncated: screws up parsing with unbalanced {{.
470 s@(: +[^a-zA-Z ]* *Created page) with [^\n]+@$1@s;
472 s@/\*.*?\*/@@si; # /* ... */
474 # Try to omit all tables, since they're impossible to read as text.
476 1 while (s/\{\{[^{}]*}}/ /gs); # {{ ... }}
477 1 while (s/\{\|.*?\|\}/\n\n/gs); # {| ... |}
478 1 while (s/\|-.*?\|/ /gs); # |- ... | (table cell)
480 # Convert anchors to something more readable.
482 s/\[\[([^\[\]\|]+)\|([^\[\]]+)\]\]/$2/gs; # [[link|anchor]]
483 s/\[\[([^:\[\]\|]+)\]\]/$1/gs; # [[anchor]]
484 s/\[https?:[^\[\]\s]+\s+([^\[\]]+)\]/$1/gs; # [url anchor]
486 # Convert all references to asterisks.
487 s@\s*<ref>\s*.*?</ref>@*@gs; # <ref> ... <ref> -> "*"
488 s@\n[ \t]*\d+\s*\^\s*http[^\s]+[ \t]*\n@\n@gs; # 1 ^ URL (a Reflist)
490 s@\[\[File:([^\|\]]+).*?\]\]@\n$1\n@gs; # [[File: X | ... ]]
491 s@\[\[Category:.*?\]\]@@gs; # omit categories
493 s/<[^<>]*>//gs; # Omit all remaining tags
494 s/\'{3,}//gs; # Omit ''' and ''''
495 s/\'\'/\"/gs; # '' -> "
496 s/\`\`/\"/gs; # `` -> "
497 s/\"\"+/\"/gs; # "" -> "
499 s/^[ \t]*[*#]+[ \t]*$//gm; # Omit lines with just * or # on them
501 # Omit trailing headlines with no text after them (e.g. == Notes ==)
502 1 while (s/\n==+[ \t]*[^\n=]+[ \t]*==+\s*$/\n/s);
504 $_ = de_entify ($_); # convert HTML entities, again
508 # elide any remaining non-Latin1 binary data.
510 utf8::encode ($_); # Unpack Unicode back to multi-byte UTF-8.
511 s/([^\000-\176]+(\s*[^\000-\176]+)[^a-z\d]*)/\xAB...\xBB /g;
516 s/[ \t]+$//gm; # lose whitespace at end of line
517 s@\n\n\n+@\n\n@gs; # compress blank lines
519 if (!defined($wrap_columns) || $wrap_columns > 0) {
520 $Text::Wrap::columns = ($wrap_columns || 72);
521 $Text::Wrap::break = '[\s/|]'; # wrap on slashes for URLs
522 $_ = wrap ("", " ", $_); # wrap the lines as a paragraph
523 s/[ \t]+$//gm; # lose whitespace at end of line again
528 if ($truncate_lines) {
529 s/^(([^\n]*\n){$truncate_lines}).*$/$1/s;
532 $_ = utf8_to_latin1($_) if ($latin1_p);
533 y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
539 sub reformat_rss($) {
542 my $wiki_p = ($body =~ m@<generator>[^<>]*Wiki@si);
544 $body =~ s/(<(ITEM|ENTRY)\b)/\001\001$1/gsi;
545 my @items = split (/\001\001/, $body);
547 print STDERR "$progname: converting RSS ($#items items)...\n"
552 # Let's skip forward in the stream by a random amount, so that if
553 # two copies of ljlatest are running at the same time (e.g., on a
554 # multi-headed machine), they get different text. (Put the items
555 # that we take off the front back on the back.)
558 my $n = int (rand ($#items - 5));
559 print STDERR "$progname: rotating by $n items...\n" if ($verbose > 2);
561 push @items, (shift @items);
571 my ($title, $body1, $body2, $body3);
573 $title = $3 if (m@<((TITLE) [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
574 $body1 = $3 if (m@<((DESCRIPTION) [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
575 $body2 = $3 if (m@<((CONTENT) [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
576 $body3 = $3 if (m@<((SUMMARY) [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
578 # If there are both <description> and <content> or <content:encoded>,
579 # use whichever one contains more text.
581 if ($body3 && length($body3) >= length($body2 || '')) {
584 if ($body2 && length($body2) >= length($body1 || '')) {
590 print STDERR "$progname: no body in item $i (\"$title\")\n"
593 print STDERR "$progname: no body or title in item $i\n"
599 $title = rss_field_to_html ($title || '');
600 $body1 = rss_field_to_html ($body1 || '');
602 $title = '' if ($body1 eq $title); # Identical in Twitter's atom feed.
604 $out .= reformat_html ("$title<P>$body1", $wiki_p ? 'wiki' : 'rss');
608 if ($truncate_lines) {
609 $out =~ s/^(([^\n]*\n){$truncate_lines}).*$/$1/s;
616 sub rss_field_to_html($) {
619 # If <![CDATA[...]]> is present, everything inside that is HTML,
620 # and not double-encoded.
622 if ($body =~ m/^\s*<!\[CDATA\[(.*?)\]\s*\]/is) {
625 $body = de_entify ($body); # convert entities to get HTML from XML
632 sub reformat_text($) {
635 # only re-wrap if --cols was specified. Otherwise, dump it as is.
637 if ($wrap_columns && $wrap_columns > 0) {
638 print STDERR "$progname: wrapping at $wrap_columns...\n" if ($verbose > 2);
639 $Text::Wrap::columns = $wrap_columns;
640 $Text::Wrap::break = '[\s/]'; # wrap on slashes for URLs
641 $body = wrap ("", "", $body);
642 $body =~ s/[ \t]+$//gm;
645 if ($truncate_lines) {
646 $body =~ s/^(([^\n]*\n){$truncate_lines}).*$/$1/s;
649 $body = utf8_to_latin1($body) if ($latin1_p);
650 $body =~ y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
655 # Figure out what the proxy server should be, either from environment
656 # variables or by parsing the output of the (MacOS) program "scutil",
657 # which tells us what the system-wide proxy settings are.
662 if (!defined($ENV{http_proxy}) && !defined($ENV{HTTP_PROXY})) {
663 my $proxy_data = `scutil --proxy 2>/dev/null`;
664 my ($server) = ($proxy_data =~ m/\bHTTPProxy\s*:\s*([^\s]+)/s);
665 my ($port) = ($proxy_data =~ m/\bHTTPPort\s*:\s*([^\s]+)/s);
667 # Note: this ignores the "ExceptionsList".
668 $ENV{http_proxy} = "http://" . $server . ($port ? ":$port" : "") . "/";
669 print STDERR "$progname: MacOS proxy: $ENV{http_proxy}\n"
678 sub get_url_text($) {
681 my $ua = eval 'LWP::UserAgent->new';
684 print STDOUT ("\n\tPerl is broken. Do this to repair it:\n" .
685 "\n\tsudo cpan LWP::UserAgent\n\n");
690 $ua->agent ("$progname/$version");
691 my $res = $ua->get ($url);
695 if ($res && $res->is_success) {
696 $body = $res->decoded_content || '';
697 $ct = $res->header ('Content-Type') || 'text/plain';
700 my $err = ($res ? $res->status_line : '') || '';
701 $err = 'unknown error' unless $err;
704 $body = "Error loading URL $err\n\n";
708 utf8::decode ($body); # Pack multi-byte UTF-8 back into wide chars.
710 $ct = guess_content_type ($ct, $body);
712 print STDERR "$progname: converting HTML...\n" if ($verbose > 2);
713 $body = reformat_html ($body, 0);
714 } elsif ($ct eq 'rss') {
715 $body = reformat_rss ($body);
717 print STDERR "$progname: plain text...\n" if ($verbose > 2);
718 $body = reformat_text ($body);
727 print STDERR "$progname: $err\n";
732 print STDERR "usage: $progname [ --options ... ]\n" .
734 " Prints out some text for use by various screensavers,\n" .
735 " according to the options in the ~/.xscreensaver file.\n" .
736 " This may dump the contents of a file, run a program,\n" .
741 " --date Print the host name and current time.\n" .
743 " --text STRING Print out the given text. It may contain %\n" .
744 " escape sequences as per strftime(2).\n" .
746 " --file PATH Print the contents of the given file.\n" .
747 " If --cols is specified, re-wrap the lines;\n" .
748 " otherwise, print them as-is.\n" .
750 " --program CMD Run the given program and print its output.\n" .
751 " If --cols is specified, re-wrap the output.\n" .
753 " --url HTTP-URL Download and print the contents of the HTTP\n" .
754 " document. If it contains HTML, RSS, or Atom,\n" .
755 " it will be converted to plain-text.\n" .
757 " --cols N Wrap lines at this column. Default 72.\n" .
759 " --lines N No more than N lines of output.\n" .
761 " --latin1 Emit Latin1 instead of UTF-8.\n" .
769 my $cocoa_id = undef;
771 while ($#ARGV >= 0) {
773 if ($_ eq "--verbose") { $verbose++; }
774 elsif (m/^-v+$/) { $verbose += length($_)-1; }
775 elsif (m/^--?date$/) { $text_mode = 'date';
777 elsif (m/^--?text$/) { $text_mode = 'literal';
778 $text_literal = shift @ARGV || '';
780 elsif (m/^--?file$/) { $text_mode = 'file';
781 $text_file = shift @ARGV || '';
783 elsif (m/^--?program$/) { $text_mode = 'program';
784 $text_program = shift @ARGV || '';
786 elsif (m/^--?url$/) { $text_mode = 'url';
787 $text_url = shift @ARGV || '';
789 elsif (m/^--?col(umn)?s?$/) { $wrap_columns = 0 + shift @ARGV; }
790 elsif (m/^--?lines?$/) { $truncate_lines = 0 + shift @ARGV; }
791 elsif (m/^--?cocoa$/) { $cocoa_id = shift @ARGV; }
792 elsif (m/^--?latin1$/) { $latin1_p++; }
793 elsif (m/^--?nyarlathotep$/) { $nyarlathotep_p++; }
794 elsif (m/^-./) { usage; }
800 if (!defined ($cocoa_id)) {
801 # see OSX/XScreenSaverView.m
802 $cocoa_id = $ENV{XSCREENSAVER_CLASSPATH};
805 if (defined ($cocoa_id)) {
806 get_cocoa_prefs($cocoa_id);
815 if (defined ($cocoa_id)) {
817 # On MacOS, sleep for 10 seconds between when the last output is
818 # printed, and when this process exits. This is because MacOS
819 # 10.5.0 and later broke ptys in a new and exciting way: basically,
820 # once the process at the end of the pty exits, you have exactly
821 # 1 second to read all the queued data off the pipe before it is
824 # Many of the screen savers were written to depend on being able
825 # to read a small number of bytes, and continue reading until they
826 # reached EOF. This is no longer possible.
828 # Note that the current MacOS behavior has all four of these
829 # awesome properties: 1) Inconvenient; 2) Has no sane workaround;
830 # 3) Different behavior than MacOS 10.1 through 10.4; and 4)
831 # Different behavior than every other Unix in the world.
833 # See http://jwz.org/b/DHke, and for those of you inside Apple,
834 # "Problem ID 5606018".
836 # One workaround would be to rewrite the savers to have an
837 # internal buffer, and always read as much data as possible as
838 # soon as a pipe has input available. However, that's a lot more
839 # work, so instead, let's just not exit right away, and hope that
840 # 10 seconds is enough.
842 # This will solve the problem for invocations of xscreensaver-text
843 # that produce little output (e.g., date-mode); and won't solve it
844 # in cases where a large amount of text is generated in a short
845 # amount of time (e.g., url-mode.)