"Fossies" - the Fresh Open Source Software Archive

Member "xscreensaver-6.01/hacks/xscreensaver-text" (29 Apr 2021, 31416 Bytes) of package /linux/misc/xscreensaver-6.01.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "xscreensaver-text": 6.00_vs_6.01.

    1 #!/usr/bin/perl -w
    2 # Copyright © 2005-2021 Jamie Zawinski <jwz@jwz.org>
    3 #
    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 
   10 # implied warranty.
   11 #
   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
   14 # print the date.
   15 #
   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.
   20 #
   21 # Created: 19-Mar-2005.
   22 
   23 require 5;
   24 #use diagnostics;   # Fails on some MacOS 10.5 systems
   25 use strict;
   26 
   27 use Socket;
   28 use POSIX qw(strftime);
   29 
   30 # Some Linux systems don't install LWP by default!
   31 # Only error out if we're actually loading a URL instead of local data.
   32 BEGIN { eval 'use LWP::UserAgent;' }
   33 
   34 # Not sure how prevalent this is. Hope it's part of the default install.
   35 BEGIN { eval 'use HTML::Entities;' }
   36 
   37 # I think this is part of the default install, but just in case.
   38 BEGIN { eval 'use Text::Wrap qw(wrap);' }
   39 
   40 
   41 my $progname = $0; $progname =~ s@.*/@@g;
   42 my ($version) = ('$Revision: 1.62 $' =~ m/\s(\d[.\d]+)\s/s);
   43 
   44 my $verbose = 0;
   45 my $http_proxy = undef;
   46 
   47 my $config_file = $ENV{HOME} . "/.xscreensaver";
   48 my $text_mode     = 'date';
   49 my $text_literal  = '';
   50 my $text_file     = '';
   51 my $text_program  = '';
   52 my $text_url      = 'https://en.wikipedia.org/w/index.php?title=Special:NewPages&feed=rss';
   53 # Default URL needs to be set and match what's in OSX/XScreenSaverView.m
   54 
   55 my $wrap_columns   = undef;
   56 my $truncate_lines = undef;
   57 my $latin1_p = 0;
   58 my $nyarlathotep_p = 0;
   59 
   60 
   61 # Convert any HTML entities to Latin1 characters.
   62 #
   63 sub de_entify($) {
   64   my ($text) = @_;
   65 
   66   return '' unless defined($text);
   67   return $text unless ($text =~ m/&/s);
   68 
   69   # Convert any HTML entities to Unicode characters,
   70   # if the HTML::Entities module is installed.
   71   eval {
   72     my $t2 = $text;
   73     $text = undef;
   74     $text = HTML::Entities::decode_entities ($t2);
   75   };
   76   return $text if defined($text);
   77 
   78   # If it's not installed, just complain instead of trying to halfass it.
   79   print STDOUT ("\n\tPerl is broken. Do this to repair it:\n" .
   80                 "\n\tsudo cpan HTML::Entities\n\n");
   81   exit (1);
   82 }
   83 
   84 
   85 # Convert any Unicode characters to Latin1 if possible.
   86 # Unconvertable bytes are left alone.
   87 #
   88 sub utf8_to_latin1($) {
   89   my ($text) = @_;
   90 
   91   utf8::encode ($text);  # Unpack Unicode back to multi-byte UTF-8.
   92 
   93   # Maybe it would be better to handle this in the Unicode domain
   94   # by doing things like s/\x{2018}/\"/g, but without decoding the
   95   # string back to UTF-8 first, I'm at a loss as to how to have
   96   # "&aacute;" print as "\340" instead of as "\303\240".
   97 
   98   $text =~ s/ \xC2 ( [\xA0-\xFF] ) / $1 /gsex;
   99   $text =~ s/ \xC3 ( [\x80-\xFF] ) / chr (ord($1) | 0x40) /gsex;
  100 
  101   # Handles a few 3-byte sequences too.
  102   $text =~ s/\xE2\x80\x93/--/gs;
  103   $text =~ s/\xE2\x80\x94/--/gs;
  104   $text =~ s/\xE2\x80\x98/`/gs;
  105   $text =~ s/\xE2\x80\x99/'/gs;
  106   $text =~ s/\xE2\x80\x9C/``/gs;
  107   $text =~ s/\xE2\x80\x9D/'/gs;
  108   $text =~ s/\xE2\x80\xA2/&bull;/gs;
  109   $text =~ s/\xE2\x80\xA6/.../gs;
  110   $text =~ s/\xE2\x80\xB2/'/gs;
  111   $text =~ s/\xE2\x84\xA2/&trade;/gs;
  112   $text =~ s/\xE2\x86\x90/ &larr; /gs;
  113 
  114   return $text;
  115 }
  116 
  117 
  118 # Reads the prefs we use from ~/.xscreensaver
  119 #
  120 sub get_x11_prefs() {
  121   my $got_any_p = 0;
  122 
  123   if (open (my $in, '<', $config_file)) {
  124     print STDERR "$progname: reading $config_file\n" if ($verbose > 1);
  125     local $/ = undef;  # read entire file
  126     my $body = <$in>;
  127     close $in;
  128     $got_any_p = get_x11_prefs_1 ($body);
  129 
  130   } elsif ($verbose > 1) {
  131     print STDERR "$progname: $config_file: $!\n";
  132   }
  133 
  134   if (! $got_any_p && defined ($ENV{DISPLAY})) {
  135     # We weren't able to read settings from the .xscreensaver file.
  136     # Fall back to any settings in the X resource database
  137     # (/usr/X11R6/lib/X11/app-defaults/XScreenSaver)
  138     #
  139     print STDERR "$progname: reading X resources\n" if ($verbose > 1);
  140     my $body = `appres XScreenSaver xscreensaver -1`;
  141     $got_any_p = get_x11_prefs_1 ($body);
  142   }
  143 
  144   if ($verbose > 1) {
  145     print STDERR "$progname: mode:    $text_mode\n";
  146     print STDERR "$progname: literal: $text_literal\n";
  147     print STDERR "$progname: file:    $text_file\n";
  148     print STDERR "$progname: program: $text_program\n";
  149     print STDERR "$progname: url:     $text_url\n";
  150   }
  151 
  152   $text_mode =~ tr/A-Z/a-z/;
  153   $text_literal =~ s@\\n@\n@gs;
  154   $text_literal =~ s@\\\n@\n@gs;
  155 }
  156 
  157 
  158 sub get_x11_prefs_1($) {
  159   my ($body) = @_;
  160 
  161   my $got_any_p = 0;
  162   $body =~ s@\\\n@@gs;
  163   $body =~ s@^[ \t]*#[^\n]*$@@gm;
  164 
  165   if ($body =~ m/^[.*]*textMode:[ \t]*([^\s]+)\s*$/im) {
  166     $text_mode = $1;
  167     $got_any_p = 1;
  168   }
  169   if ($body =~ m/^[.*]*textLiteral:[ \t]*(.*?)[ \t]*$/im) {
  170     $text_literal = $1;
  171   }
  172   if ($body =~ m/^[.*]*textFile:[ \t]*(.*?)[ \t]*$/im) {
  173     $text_file = $1;
  174   }
  175   if ($body =~ m/^[.*]*textProgram:[ \t]*(.*?)[ \t]*$/im) {
  176     $text_program = $1;
  177   }
  178   if ($body =~ m/^[.*]*textURL:[ \t]*(.*?)[ \t]*$/im) {
  179     $text_url = $1;
  180   }
  181 
  182   return $got_any_p;
  183 }
  184 
  185 
  186 sub get_cocoa_prefs($) {
  187   my ($id) = @_;
  188   my $v;
  189  
  190   print STDERR "$progname: reading Cocoa prefs: \"$id\"\n" if ($verbose > 1);
  191 
  192   $v = get_cocoa_pref_1 ($id, "textMode");
  193   $text_mode = $v if defined ($v);
  194 
  195   # The "textMode" pref is set to a number instead of a string because I
  196   # couldn't figure out the black magic to make Cocoa bindings work right.
  197   #
  198   # Update: as of 5.33, Cocoa writes strings instead of numbers, but 
  199   # pre-existing saved preferences might still have numbers in them.
  200   #
  201   if    ($text_mode eq '0') { $text_mode = 'date';    }
  202   elsif ($text_mode eq '1') { $text_mode = 'literal'; }
  203   elsif ($text_mode eq '2') { $text_mode = 'file';    }
  204   elsif ($text_mode eq '3') { $text_mode = 'url';     }
  205   elsif ($text_mode eq '4') { $text_mode = 'program'; }
  206 
  207   $v = get_cocoa_pref_1 ($id, "textLiteral");
  208   $text_literal = $v if defined ($v);
  209   $text_literal =~ s@\\n@\n@gs;
  210   $text_literal =~ s@\\\n@\n@gs;
  211 
  212   $v = get_cocoa_pref_1 ($id, "textFile");
  213   $text_file = $v if defined ($v);
  214 
  215   $v = get_cocoa_pref_1 ($id, "textProgram");
  216   $text_program = $v if defined ($v);
  217 
  218   $v = get_cocoa_pref_1 ($id, "textURL");
  219   $text_url = $v if defined ($v);
  220 }
  221 
  222 
  223 sub get_cocoa_pref_1($$) {
  224   my ($id, $key) = @_;
  225   # make sure there's nothing stupid/malicious in either string.
  226   $id  =~ s/[^-a-z\d. ]/_/gsi;
  227   $key =~ s/[^-a-z\d. ]/_/gsi;
  228   my $cmd = "defaults -currentHost read \"$id\" \"$key\"";
  229 
  230   print STDERR "$progname: executing $cmd\n"
  231     if ($verbose > 3);
  232 
  233   my $val = `$cmd 2>/dev/null`;
  234   $val =~ s/^\s+//s;
  235   $val =~ s/\s+$//s;
  236 
  237   print STDERR "$progname: Cocoa: $id $key = \"$val\"\n"
  238     if ($verbose > 2);
  239 
  240   $val = undef if ($val =~ m/^$/s);
  241 
  242   return $val;
  243 }
  244 
  245 
  246 # like system() but checks errors.
  247 #
  248 sub safe_system(@) {
  249   my (@cmd) = @_;
  250 
  251   print STDERR "$progname: executing " . join(' ', @cmd) . "\n"
  252     if ($verbose > 3);
  253 
  254   system @cmd;
  255   my $exit_value  = $? >> 8;
  256   my $signal_num  = $? & 127;
  257   my $dumped_core = $? & 128;
  258   error ("$cmd[0]: core dumped!") if ($dumped_core);
  259   error ("$cmd[0]: signal $signal_num!") if ($signal_num);
  260   error ("$cmd[0]: exited with $exit_value!") if ($exit_value);
  261 }
  262 
  263 
  264 sub which($) {
  265   my ($cmd) = @_;
  266 
  267   if ($cmd =~ m@^\./|^/@) {
  268     error ("cannot execute $cmd") unless (-x $cmd);
  269     return $cmd;
  270   }
  271  
  272  foreach my $dir (split (/:/, $ENV{PATH})) {
  273     my $cmd2 = "$dir/$cmd";
  274     print STDERR "$progname:   checking $cmd2\n" if ($verbose > 3);
  275     return $cmd2 if (-x "$cmd2");
  276   }
  277   error ("$cmd not found on \$PATH");
  278 }
  279 
  280 
  281 sub output() {
  282 
  283   binmode (STDOUT, ($latin1_p ? ':raw' : ':utf8'));
  284   binmode (STDERR, ':utf8');
  285 
  286   # Do some basic sanity checking (null text, null file names, etc.)
  287   #
  288   if (($text_mode eq 'literal' && $text_literal =~ m/^\s*$/i) ||
  289       ($text_mode eq 'file'    && $text_file    =~ m/^\s*$/i) ||
  290       ($text_mode eq 'program' && $text_program =~ m/^\s*$/i) ||
  291       ($text_mode eq 'url'     && $text_url     =~ m/^\s*$/i)) {
  292     print STDERR "$progname: falling back to 'date'\n" if ($verbose);
  293     $text_mode = 'date';
  294   }
  295 
  296   if ($text_mode eq 'literal') {
  297     $text_literal = strftime ($text_literal, localtime);
  298     $text_literal = utf8_to_latin1($text_literal) if ($latin1_p);
  299     $text_literal =~ y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
  300     print STDOUT $text_literal;
  301     print STDOUT "\n" unless ($text_literal =~ m/\n$/s);
  302 
  303   } elsif ($text_mode eq 'file') {
  304 
  305     $text_file =~ s@^~/@$ENV{HOME}/@s;     # allow literal "~/"
  306 
  307     if (open (my $in, '<:raw', $text_file)) {
  308       print STDERR "$progname: reading $text_file\n" if ($verbose);
  309       binmode (STDOUT, ':raw');
  310 
  311       if (($wrap_columns && $wrap_columns > 0) || $truncate_lines) {
  312         # read it, then reformat it.
  313         local $/ = undef;  # read entire file
  314         my $body = <$in>;
  315         $body = reformat_text ($body);
  316         print STDOUT $body;
  317       } else {
  318         # stream it by lines
  319         while (<$in>) { 
  320           $_ = utf8_to_latin1($_) if ($latin1_p);
  321           y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
  322           print STDOUT $_;
  323         }
  324       }
  325       close $in;
  326     } else {
  327       error ("$text_file: $!");
  328     }
  329 
  330   } elsif ($text_mode eq 'program') {
  331 
  332     my ($prog, $args) = ($text_program =~ m/^([^\s]+)(.*)$/);
  333     $text_program = which ($prog) . $args;
  334     print STDERR "$progname: running $text_program\n" if ($verbose);
  335 
  336     if (($wrap_columns && $wrap_columns > 0) || $truncate_lines) {
  337       # read it, then reformat it.
  338       my $lines = 0;
  339       my $body = "";
  340       my $cmd = "( $text_program ) 2>&1";
  341       # $cmd .= " | sed -l"; # line buffer instead of 4k pipe buffer
  342       open (my $pipe, '-|:unix', $cmd);
  343       while (my $line = <$pipe>) {
  344         $body .= $line;
  345         $lines++;
  346         last if ($truncate_lines && $lines > $truncate_lines);
  347       }
  348       close $pipe;
  349 
  350       # I don't understand why we must do this here, but must not do this
  351       # in the 'file' branch above, which reads the file with :raw...
  352       utf8::decode ($body);  # Pack multi-byte UTF-8 back into wide chars.
  353 
  354       $body = reformat_text ($body);
  355       print STDOUT $body;
  356     } else {
  357       # stream it
  358       safe_system ("$text_program");
  359     }
  360 
  361   } elsif ($text_mode eq 'url') {
  362 
  363     get_url_text ($text_url);
  364 
  365   } else { # $text_mode eq 'date'
  366 
  367     my $body = '';
  368 
  369     my $n = `uname -n`;
  370     $n =~ s/\.local\n/\n/s;
  371     $body .= $n;
  372 
  373     my $unamep = 1;
  374 
  375     # The following code makes an effort to figure out the version of the
  376     # OS / distribution, and a one-line summary of the hardware.  We can
  377     # easily get the kernel version from uname, but the kernel version has
  378     # no relevance to anyone's life.  The userspace version is what matters.
  379     #
  380     # In a truly shocking turn of events, nearly every distro uses a different
  381     # file to identify itself.  Are you shocked?  I for one am shocked.
  382 
  383     $ENV{LC_NUMERIC} = 'C';   # Decimals with periods, not commas, plz.
  384 
  385     if (open (my $in, "<:utf8", "/etc/os-release")) {
  386       while (<$in>) {             # PRETTY_NAME="CentOS Linux 7 (Core)"
  387         if (m/^PRETTY_NAME="(.*)"/si) {   # PRETTY_NAME="Raspbian 10 (buster)"
  388           # Annoying that "10.6" is only in /etc/debian_version and
  389           # /etc/os-release only contains "10".
  390           $body .= "$1\n";
  391           $unamep = 0;
  392           last;
  393         }
  394       }
  395       close $in;
  396     } elsif (open ($in, "<:utf8", "/etc/lsb-release")) {
  397       while (<$in>) {            # DISTRIB_DESCRIPTION="Ubuntu 14.04.2 LTS"
  398         if (m/^DISTRIB_DESCRIPTION="(.*)"/si) {
  399           $body .= "$1\n";
  400           $unamep = 0;
  401           last;
  402         }
  403       }
  404       close $in;
  405     } elsif (-f "/etc/system-release") {    # "CentOS Linux release 7.7 (Core)"
  406       $body .= `cat /etc/system-release`;
  407     } elsif (-f "/etc/redhat-release") {    # "Fedora Core release 4 (Stentz)"
  408       $body .= `cat /etc/redhat-release`;
  409     } elsif (-f "/etc/SuSE-release") {      # "SUSE Linux Enterprise Server 11"
  410       $body .= `head -1 /etc/SuSE-release`;
  411     } elsif (-f "/etc/release") {           # "Solaris 10 3/05 s10_74L2a X86"
  412       $body .= `head -1 /etc/release`;
  413     } elsif (-f "/usr/sbin/system_profiler") {   # "Mac OS X 10.4.5 (8H14)"
  414       my $sp =
  415         `/usr/sbin/system_profiler SPSoftwareDataType SPHardwareDataType 2>&-`;
  416       my ($v) = ($sp =~ m/^\s*System Version:\s*(.*)$/mi);
  417       my ($s) = ($sp =~ m/^\s*(?:CPU|Processor) Speed:\s*(.*)$/mi);
  418       my ($t) = ($sp =~ m/^\s*(?:Machine|Model) Name:\s*(.*)$/mi);
  419       my ($m) = ($sp =~ m/^\s*Memory:\s*(.*)$/mi);
  420       $t .= ", $m" if $t;
  421       $body .= "$v\n" if ($v);
  422       $body .= "$s $t\n" if ($s && $t);
  423       $unamep = !defined ($v);
  424     }
  425 
  426     $body =~ s@ GNU/Linux @ @;          # Line is too long for "gltext"
  427     $body .= `uname -sr` if ($unamep);      # "Linux 2.6.15-1.1831_FC4"
  428 
  429     # If /bin/lscpu exists, we can get some details about the hardware
  430     # we're running on.  This info also exists in various /proc/ files,
  431     # but if /proc/ exists, lscpu probably exists as well.
  432     #
  433     my $cpu = `lscpu 2>&-`;
  434     if ($cpu) {
  435       my ($model) = ($cpu =~ m/^Model name:\s*(.*)$/mi);
  436      #my ($arch)  = ($cpu =~ m/^Architecture:\s*(.*)$/mi);
  437       my ($speed) = ($cpu =~ m/^CPU max MHz:\s*(.*)$/mi);
  438          ($speed) = ($cpu =~ m/^CPU MHz:\s*(.*)$/mi) unless $speed;
  439       if ($speed && $speed >= 1000) {
  440         $speed = sprintf("%.1fGHz", $speed/1000);
  441       } else {
  442         $speed = sprintf("%.1fMHz", $speed);
  443       }
  444 
  445       # Abbreviate stupidly verbose marketing nonsense like:
  446       # "Intel(R) Xeon(R) CPU E3-1275 v5 @ 3.60GHz" and
  447       # "11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz"
  448       $model =~ s/\s*\@.*//gs;            # GHz
  449       $model =~ s/\s*\((R|TM)\)//gs;          # So useful!
  450       $model =~ s/\s*\b(CPU|Processor)\b//gsi;    # It is? Really?
  451       $model =~ s/\b\d+[a-z]+ gen //gsi;      # Nth Gen
  452 
  453       # Maybe we can tell how much RAM is installed.
  454       my $ram = `cat /proc/meminfo 2>&-`;
  455       if ($ram && $ram =~ m/^Memtotal:\s+(\d+)/mi) {
  456         $ram = $1;
  457         if    ($ram >= 1024*1024) { $ram = sprintf("%.0fGB", $ram/1024/1024); }
  458         elsif ($ram >= 1024) { $ram = sprintf("%.0fMB", $ram/1024); }
  459         else { $ram = sprintf("%.0fKB", $ram); }
  460       }
  461 
  462       $body .= "$speed"  if $speed;
  463       $body .= " $model" if $model;
  464      #$body .= " $arch"  if $arch;
  465       $body .= " $ram"   if $ram;
  466       $body .= "\n";
  467     }
  468 
  469     $body =~ s/[ \t]+/ /gm;
  470     $body =~ s/^ | $//gm;
  471 
  472     $body .= "\n";
  473     $body .= strftime ('%c', localtime);
  474     $body .= "\n";
  475 
  476     my $ut = `uptime`;
  477     $ut =~ s/^[ \d:]*(am|pm)? *//i;
  478     $ut =~ s/,\s*(load)/\n$1/;
  479     $body .= "$ut\n";
  480 
  481     if ($truncate_lines) {
  482       $body =~ s/^(([^\n]*\n){$truncate_lines}).*$/$1/s;
  483     }
  484 
  485     print STDOUT $body;
  486   }
  487 }
  488 
  489 
  490 # Make an educated guess as to what's in this document.
  491 # We don't necessarily take the Content-Type header at face value.
  492 # Returns 'html', 'rss', or 'text';
  493 #
  494 sub guess_content_type($$) {
  495   my ($ct, $body) = @_;
  496 
  497   $body =~ s/^(.{512}).*/$1/s;  # only look in first half K of file
  498 
  499   if ($ct =~ m@^text/.*html@i)          { return 'html'; }
  500   if ($ct =~ m@\b(atom|rss|xml)\b@i)    { return 'rss';  }
  501 
  502   if ($body =~ m@^\s*<\?xml@is)         { return 'rss';  }
  503   if ($body =~ m@^\s*<!DOCTYPE RSS@is)  { return 'rss';  }
  504   if ($body =~ m@^\s*<!DOCTYPE HTML@is) { return 'html'; }
  505 
  506   if ($body =~ m@<(BASE|HTML|HEAD|BODY|SCRIPT|STYLE|TABLE|A\s+HREF)\b@i) {
  507     return 'html';
  508   }
  509 
  510   if ($body =~ m@<(RSS|CHANNEL|GENERATOR|DESCRIPTION|CONTENT|FEED|ENTRY)\b@i) {
  511     return 'rss';
  512   }
  513 
  514   return 'text';
  515 }
  516 
  517 
  518 sub reformat_html($$) {
  519   my ($body, $rss_p) = @_;
  520   $_ = $body;
  521 
  522   # In HTML, try to preserve newlines inside of PRE.
  523   #
  524   if (! $rss_p) {
  525     s@(<PRE\b[^<>]*>\s*)(.*?)(</PRE)@{
  526       my ($a, $b, $c) = ($1, $2, $3);
  527       $b =~ s/[\r\n]/<BR>/gs;
  528       $a . $b . $c;
  529      }@gsexi;
  530   }
  531 
  532   if (! $rss_p) {
  533     # In HTML, unfold lines.
  534     # In RSS, assume \n means literal line break.
  535     s@[\r\n]@ @gsi;
  536   }
  537 
  538   # This right here is the part where I doom us all to inhuman
  539   # toil for the One whose Name cannot be expressed in the
  540   # Basic Multilingual Plane. http://jwz.org/b/yhAT He comes.
  541 
  542   s@<!--.*?-->@@gsi;                 # lose comments
  543   s@<(STYLE|SCRIPT)\b[^<>]*>.*?</\1\s*>@@gsi;    # lose css and js
  544 
  545   s@</?(BR|TR|TD|LI|DIV)\b[^<>]*>@\n@gsi; # line break at BR, TD, DIV, etc
  546   s@</?(P|UL|OL|BLOCKQUOTE)\b[^<>]*>@\n\n@gsi; # two line breaks
  547 
  548   s@<lj\s+user=\"?([^<>\"]+)\"?[^<>]*>?@$1@gsi;  # handle <LJ USER=>
  549   s@</?[BI]>@*@gsi;                      # bold, italic => asterisks
  550 
  551 
  552   s@<[^<>]*>?@@gs;                # lose all other HTML tags
  553   $_ = de_entify ($_);            # convert HTML entities
  554 
  555   # For Wikipedia: delete anything inside {{ }} and unwrap [[tags]],
  556   # among other things.
  557   #
  558   if ($rss_p eq 'wiki') {
  559 
  560     s@<!--.*?-->@@gsi;               # lose HTML comments again
  561 
  562     # Creation line is often truncated: screws up parsing with unbalanced {{.
  563     s@(: +[^a-zA-Z ]* *Created page) with [^\n]+@$1@s;
  564 
  565     s@/\*.*?\*/@@si;                   # /* ... */
  566 
  567     # Try to omit all tables, since they're impossible to read as text.
  568     #
  569     1 while (s/\{\{[^{}]*}}/ /gs);         # {{ ... }}
  570     1 while (s/\{\|.*?\|\}/\n\n/gs);           # {| ... |}
  571     1 while (s/\|-.*?\|/ /gs);             # |- ... |  (table cell)
  572 
  573     # Convert anchors to something more readable.
  574     #
  575     s/\[\[([^\[\]\|]+)\|([^\[\]]+)\]\]/$2/gs;      # [[link|anchor]]
  576     s/\[\[([^:\[\]\|]+)\]\]/$1/gs;         # [[anchor]]
  577     s/\[https?:[^\[\]\s]+\s+([^\[\]]+)\]/$1/gs;    # [url anchor]
  578 
  579     # Convert all references to asterisks.
  580     s@\s*<ref>\s*.*?</ref>@*@gs;           # <ref> ... <ref> ->  "*"
  581     s@\n[ \t]*\d+\s*\^\s*http[^\s]+[ \t]*\n@\n@gs; # 1 ^ URL (a Reflist)
  582 
  583     s@\[\[File:([^\|\]]+).*?\]\]@\n$1\n@gs;   # [[File: X | ... ]]
  584     s@\[\[Category:.*?\]\]@@gs;           # omit categories
  585 
  586     s/<[^<>]*>//gs; # Omit all remaining tags
  587     s/\'{3,}//gs;   # Omit ''' and ''''
  588     s/\'\'/\"/gs;   # ''  ->  "
  589     s/\`\`/\"/gs;   # ``  ->  "
  590     s/\"\"+/\"/gs;  # ""  ->  "
  591 
  592     s/^[ \t]*[*#]+[ \t]*$//gm;  # Omit lines with just * or # on them
  593 
  594     # Omit trailing headlines with no text after them (e.g. == Notes ==)
  595     1 while (s/\n==+[ \t]*[^\n=]+[ \t]*==+\s*$/\n/s);
  596 
  597     $_ = de_entify ($_);            # convert HTML entities, again
  598   }
  599 
  600 
  601   # elide any remaining non-Latin1 binary data.
  602   if ($latin1_p) {
  603     utf8::encode ($_);  # Unpack Unicode back to multi-byte UTF-8.
  604     s/([^\000-\176]+(\s*[^\000-\176]+)[^a-z\d]*)/\xAB...\xBB /g;
  605   }
  606 
  607   $_ .= "\n";
  608 
  609   s/[ \t]+$//gm;                  # lose whitespace at end of line
  610   s@\n\n\n+@\n\n@gs;              # compress blank lines
  611 
  612   if (!defined($wrap_columns) || $wrap_columns > 0) {
  613     # Text::Wrap sometimes dies with "This shouldn't happen" if columns
  614     # is small, but not in any predictable way.
  615     # Also there's a chance it might not be installed.
  616     eval {
  617       $Text::Wrap::columns = ($wrap_columns || 72);
  618       $Text::Wrap::break = '[\s/|]';  # wrap on slashes for URLs
  619 
  620       $_ = wrap ("", "  ", $_);     # wrap the lines as a paragraph
  621       s/[ \t]+$//gm;                # lose whitespace at end of line again
  622     };
  623   }
  624 
  625   s/^\n+//gs;
  626 
  627   if ($truncate_lines) {
  628     s/^(([^\n]*\n){$truncate_lines}).*$/$1/s;
  629   }
  630 
  631   $_ = utf8_to_latin1($_) if ($latin1_p);
  632   y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
  633 
  634   return $_;
  635 }
  636 
  637 
  638 sub reformat_rss($) {
  639   my ($body) = @_;
  640 
  641   my $wiki_p = ($body =~ m@<generator>[^<>]*Wiki@si);
  642 
  643   $body =~ s/(<(ITEM|ENTRY)\b)/\001\001$1/gsi;
  644   my @items = split (/\001\001/, $body);
  645 
  646   print STDERR "$progname: converting RSS ($#items items)...\n"
  647     if ($verbose > 2);
  648 
  649   shift @items;
  650 
  651   # Let's skip forward in the stream by a random amount, so that if
  652   # two copies of ljlatest are running at the same time (e.g., on a
  653   # multi-headed machine), they get different text.  (Put the items
  654   # that we take off the front back on the back.)
  655   #
  656   if ($#items > 7) {
  657     my $n = int (rand ($#items - 5));
  658     print STDERR "$progname: rotating by $n items...\n" if ($verbose > 2);
  659     while ($n-- > 0) {
  660       push @items, (shift @items);
  661     }
  662   }
  663 
  664   my $out = '';
  665 
  666   my $i = -1;
  667   foreach (@items) {
  668     $i++;
  669 
  670     my ($title, $author, $body1, $body2, $body3);
  671     
  672     $title = $3 if (m@<((TITLE)       [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
  673     $author= $3 if (m@<((DC:CREATOR)  [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
  674     $body1 = $3 if (m@<((DESCRIPTION) [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
  675     $body2 = $3 if (m@<((CONTENT)     [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
  676     $body3 = $3 if (m@<((SUMMARY)     [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
  677 
  678     # If there are both <description> and <content> or <content:encoded>,
  679     # use whichever one contains more text.
  680     #
  681     if ($body3 && length($body3) >= length($body2 || '')) {
  682       $body2 = $body3;
  683     }
  684     if ($body2 && length($body2) >= length($body1 || '')) {
  685       $body1 = $body2;
  686     }
  687 
  688     if (! $body1) {
  689       if ($title) {
  690         print STDERR "$progname: no body in item $i (\"$title\")\n"
  691           if ($verbose > 2);
  692       } else {
  693         print STDERR "$progname: no body or title in item $i\n"
  694           if ($verbose > 2);
  695         next;
  696       }
  697     }
  698 
  699     $title = rss_field_to_html ($title || '');
  700     $author= rss_field_to_html ($author || '');
  701     $body1 = rss_field_to_html ($body1 || '');
  702 
  703     $title = '' if ($body1 eq $title);  # Identical in Twitter's atom feed.
  704 
  705     # Omit author if it's in the title or body
  706     $author = '' if ($author &&
  707                      ($title =~ m/\Q$author\E/si ||
  708                       $body1 =~ m/\Q$author\E/si));
  709 
  710     $title = $author if ($author && !$title);
  711     $title = "$author: $title" if ($author && $title);
  712 
  713     $out .= reformat_html ("$title<P>$body1", $wiki_p ? 'wiki' : 'rss');
  714     $out .= "\n";
  715   }
  716 
  717   if ($truncate_lines) {
  718     $out =~ s/^(([^\n]*\n){$truncate_lines}).*$/$1/s;
  719   }
  720 
  721   return $out;
  722 }
  723 
  724 
  725 sub rss_field_to_html($) {
  726   my ($body) = @_;
  727 
  728   # If <![CDATA[...]]> is present, everything inside that is HTML,
  729   # and not double-encoded.
  730   #
  731   if ($body =~ m/^\s*<!\[CDATA\[(.*?)\]\s*\]/is) {
  732     $body = $1;
  733   } else {
  734     $body = de_entify ($body);      # convert entities to get HTML from XML
  735   }
  736 
  737   return $body;
  738 }
  739 
  740 
  741 sub reformat_text($) {
  742   my ($body) = @_;
  743 
  744   # only re-wrap if --cols was specified.  Otherwise, dump it as is.
  745   #
  746   if ($wrap_columns && $wrap_columns > 0) {
  747     print STDERR "$progname: wrapping at $wrap_columns...\n" if ($verbose > 2);
  748 
  749     # Text::Wrap sometimes dies with "This shouldn't happen" if columns
  750     # is small, but not in any predictable way.
  751     # Also there's a chance it might not be installed.
  752     eval {
  753       $Text::Wrap::columns = $wrap_columns;
  754       $Text::Wrap::break = '[\s/]';  # wrap on slashes for URLs
  755 
  756       $body = wrap ("", "", $body);
  757       $body =~ s/[ \t]+$//gm;
  758     };
  759   }
  760 
  761   if ($truncate_lines) {
  762     $body =~ s/^(([^\n]*\n){$truncate_lines}).*$/$1/s;
  763   }
  764 
  765   $body = utf8_to_latin1($body) if ($latin1_p);
  766   $body =~ y/A-Za-z/N-ZA-Mn-za-m/ if ($nyarlathotep_p);
  767   return $body;
  768 }
  769 
  770 
  771 # Figure out what the proxy server should be, either from environment
  772 # variables or by parsing the output of the (MacOS) program "scutil",
  773 # which tells us what the system-wide proxy settings are.
  774 #
  775 sub set_proxy($) {
  776   my ($ua) = @_;
  777 
  778   my $proxy_data = `scutil --proxy 2>/dev/null`;
  779   foreach my $proto ('http', 'https') {
  780     my ($server) = ($proxy_data =~ m/\b${proto}Proxy\s*:\s*([^\s]+)/si);
  781     my ($port)   = ($proxy_data =~ m/\b${proto}Port\s*:\s*([^\s]+)/si);
  782     my ($enable) = ($proxy_data =~ m/\b${proto}Enable\s*:\s*([^\s]+)/si);
  783 
  784     if ($server && $enable) {
  785       # Note: this ignores the "ExceptionsList".
  786       my $proto2 = 'http';
  787       $ENV{"${proto}_proxy"} = ("${proto2}://" . $server .
  788                                 ($port ? ":$port" : "") . "/");
  789       print STDERR "$progname: MacOS $proto proxy: " .
  790                    $ENV{"${proto}_proxy"} . "\n"
  791         if ($verbose > 2);
  792     }
  793   }
  794 
  795   $ua->env_proxy();
  796 }
  797 
  798 
  799 sub get_url_text($) {
  800   my ($url) = @_;
  801 
  802   my $ua = eval 'LWP::UserAgent->new';
  803 
  804   if (! $ua) {
  805     print STDOUT ("\n\tPerl is broken. Do this to repair it:\n" .
  806                   "\n\tsudo cpan LWP::UserAgent" .
  807                   " LWP::Protocol::https Mozilla::CA\n\n");
  808     return;
  809   }
  810 
  811   # Half the time, random Linux systems don't have Mozilla::CA installed,
  812   # which results in "Can't verify SSL peers without knowning which
  813   # Certificate Authorities to trust".
  814   #
  815   # I'm going to take a controversial stand here and say that, for the
  816   # purposes of plain-text being displayed in a screen saver via RSS,
  817   # the chances of a certificate-based man-in-the-middle attack having
  818   # a malicious effect on anyone anywhere at any time is so close to
  819   # zero that it can be discounted.  So, just don't bother validating
  820   # SSL connections.
  821   #
  822   $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
  823   eval {
  824     $ua->ssl_opts (verify_hostname => 0, SSL_verify_mode => 0);
  825   };
  826 
  827 
  828   set_proxy ($ua);
  829   $ua->agent ("$progname/$version");
  830   my $res = $ua->get ($url);
  831   my $body;
  832   my $ct;
  833 
  834   if ($res && $res->is_success) {
  835     $body = $res->decoded_content || '';
  836     $ct   = $res->header ('Content-Type') || 'text/plain';
  837 
  838   } else {
  839     my $err = ($res ? $res->status_line : '') || '';
  840     $err = 'unknown error' unless $err;
  841     $err = "$url: $err";
  842     # error ($err);
  843     $body = "Error loading URL $err\n\n";
  844     $ct = 'text/plain';
  845   }
  846 
  847   # This is not necessary, since HTTP::Message::decoded_content() has
  848   # already done 'decode (<charset-header>, $body)'.
  849   # utf8::decode ($body);  # Pack multi-byte UTF-8 back into wide chars.
  850 
  851   $ct = guess_content_type ($ct, $body);
  852   if ($ct eq 'html') {
  853     print STDERR "$progname: converting HTML...\n" if ($verbose > 2);
  854     $body = reformat_html ($body, 0);
  855   } elsif ($ct eq 'rss')  {
  856     $body = reformat_rss ($body);
  857   } else {
  858     print STDERR "$progname: plain text...\n" if ($verbose > 2);
  859     $body = reformat_text ($body);
  860   }
  861   print STDOUT $body;
  862 }
  863 
  864 
  865 
  866 sub error($) {
  867   my ($err) = @_;
  868   print STDERR "$progname: $err\n";
  869   exit 1;
  870 }
  871 
  872 sub usage() {
  873   print STDERR "usage: $progname [ --options ... ]\n" .
  874    ("\n" .
  875     "       Prints out some text for use by various screensavers,\n" .
  876     "       according to the options in the ~/.xscreensaver file.\n" .
  877     "       This may dump the contents of a file, run a program,\n" .
  878     "       or load a URL.\n".
  879     "\n" .
  880     "   Options:\n" .
  881     "\n" .
  882     "       --date           Print the host name and current time.\n" .
  883     "\n" .
  884     "       --text STRING    Print out the given text.  It may contain %\n" .
  885     "                        escape sequences as per strftime(2).\n" .
  886     "\n" .
  887     "       --file PATH      Print the contents of the given file.\n" .
  888     "                        If --cols is specified, re-wrap the lines;\n" .
  889     "                        otherwise, print them as-is.\n" .
  890     "\n" .
  891     "       --program CMD    Run the given program and print its output.\n" .
  892     "                        If --cols is specified, re-wrap the output.\n" .
  893     "\n" .
  894     "       --url HTTP-URL   Download and print the contents of the HTTP\n" .
  895     "                        document.  If it contains HTML, RSS, or Atom,\n" .
  896     "                        it will be converted to plain-text.\n" .
  897     "\n" .
  898     "       --cols N         Wrap lines at this column.  Default 72.\n" .
  899     "\n" .
  900     "       --lines N        No more than N lines of output.\n" .
  901     "\n" .
  902     "       --latin1         Emit Latin1 instead of UTF-8.\n" .
  903     "\n");
  904   exit 1;
  905 }
  906 
  907 sub main() {
  908 
  909   my $load_p = 1;
  910   my $cocoa_id = undef;
  911 
  912   my @oargv = @ARGV;
  913   while ($#ARGV >= 0) {
  914     $_ = shift @ARGV;
  915     if ($_ eq "--verbose") { $verbose++; }
  916     elsif (m/^-v+$/) { $verbose += length($_)-1; }
  917     elsif (m/^--?date$/)    { $text_mode = 'date';
  918                               $load_p = 0; }
  919     elsif (m/^--?text$/)    { $text_mode = 'literal';
  920                               $text_literal = shift @ARGV || '';
  921                               $text_literal =~ s@\\n@\n@gs;
  922                               $text_literal =~ s@\\\n@\n@gs;
  923                               $load_p = 0; }
  924     elsif (m/^--?file$/)    { $text_mode = 'file';
  925                               $text_file = shift @ARGV || '';
  926                               $load_p = 0; }
  927     elsif (m/^--?program$/) { $text_mode = 'program';
  928                               $text_program = shift @ARGV || '';
  929                               $load_p = 0; }
  930     elsif (m/^--?url$/)     { $text_mode = 'url';
  931                               $text_url = shift @ARGV || '';
  932                               $load_p = 0; }
  933     elsif (m/^--?col(umn)?s?$/) { $wrap_columns = 0 + shift @ARGV; }
  934     elsif (m/^--?lines?$/)  { $truncate_lines = 0 + shift @ARGV; }
  935     elsif (m/^--?cocoa$/)   { $cocoa_id = shift @ARGV; }
  936     elsif (m/^--?latin1$/)  { $latin1_p++; }
  937     elsif (m/^--?nyarlathotep$/) { $nyarlathotep_p++; }
  938     elsif (m/^-./) { usage; }
  939     else { usage; }
  940   }
  941 
  942   print STDERR "$progname: args: @oargv\n" if ($verbose > 1);
  943 
  944   if (!defined ($cocoa_id)) {
  945     # see OSX/XScreenSaverView.m
  946     $cocoa_id = $ENV{XSCREENSAVER_CLASSPATH};
  947   }
  948 
  949   print STDERR "$progname: Cocoa ID: $cocoa_id\n"
  950     if ($verbose > 1 && $cocoa_id);
  951 
  952   if ($load_p) {
  953     if (defined ($cocoa_id)) {
  954       get_cocoa_prefs($cocoa_id);
  955     } else {
  956       get_x11_prefs();
  957     }
  958   }
  959 
  960   output();
  961 
  962 
  963   if (defined ($cocoa_id)) {
  964     #
  965     # On MacOS, sleep for 10 seconds between when the last output is
  966     # printed, and when this process exits.  This is because MacOS
  967     # 10.5.0 and later broke ptys in a new and exciting way: basically,
  968     # once the process at the end of the pty exits, you have exactly
  969     # 1 second to read all the queued data off the pipe before it is
  970     # summarily flushed.
  971     #
  972     # Many of the screen savers were written to depend on being able
  973     # to read a small number of bytes, and continue reading until they
  974     # reached EOF.  This is no longer possible.
  975     #
  976     # Note that the current MacOS behavior has all four of these
  977     # awesome properties: 1) Inconvenient; 2) Has no sane workaround;
  978     # 3) Different behavior than MacOS 10.1 through 10.4; and 4)
  979     # Different behavior than every other Unix in the world.
  980     #
  981     # See http://jwz.org/b/DHke, and for those of you inside Apple,
  982     # "Problem ID 5606018".
  983     #
  984     # One workaround would be to rewrite the savers to have an
  985     # internal buffer, and always read as much data as possible as
  986     # soon as a pipe has input available.  However, that's a lot more
  987     # work, so instead, let's just not exit right away, and hope that
  988     # 10 seconds is enough.
  989     #
  990     # This will solve the problem for invocations of xscreensaver-text
  991     # that produce little output (e.g., date-mode); and won't solve it
  992     # in cases where a large amount of text is generated in a short
  993     # amount of time (e.g., url-mode.)
  994     #
  995     my $secs = 10;
  996     $secs = 1 if (($truncate_lines || 0) < 10);  # for 'gltext'
  997     sleep ($secs);
  998   }
  999 }
 1000 
 1001 main();
 1002 exit 0;