home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-convex / checknews < prev    next >
Encoding:
Text File  |  1991-05-16  |  1.8 KB  |  75 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # checknews -- pipe to words for formatting.
  4. # by Tom Christiansen <tchrist@convex.com>
  5.  
  6. &getwin;
  7.  
  8. #$ARGV[0] eq '-c' && ($chopit++, shift);
  9. $chopit++;
  10.  
  11. $WIDTH = ($cols > 80) ? 20: 13;
  12.  
  13. die "can't fork: $!" unless defined ($pid = open(WORDS, "|-"));
  14. if ($pid) {
  15.     select(WORDS);
  16.     open(RN, "rn -t -s750 -c @ARGV|");
  17.     @newsgroups = <RN>;
  18.     $chopit = 0  if @newsgroups < 5;
  19.     for (@newsgroups) {
  20.     next unless ($_, $count) = /^(\S+): (\d+)/;
  21.     0 while $chopit && $WIDTH < length && s/\b(\w)[^.]+\./$1./;
  22.     $_ = sprintf("%3d %s\n", $count, $_);
  23.     s/ +$//;
  24.     print;
  25.     }
  26.     close WORDS;
  27.     wait;
  28.     exit $?;
  29.  
  30.  
  31. ##################################################################
  32. # from here on in is my basic "words" script...  included here
  33. # so that checknews is self-contained.  some versions of pr
  34. # are smart enough to do this.  sadly, ours is not one of them.
  35.  
  36. sub eol { ($elt+1) % $cols == 0; }    # is this the last elt on line?
  37.  
  38. $maxlen = 1;    # widest string yet seen
  39.  
  40.  
  41.  
  42. while (<STDIN>) {     # read stdin into $_
  43.     s/\s+$//;
  44.     $maxlen = $mylen if (($mylen = length($_)) > $maxlen);
  45.     push(list, $_);
  46.  
  47. $maxlen += 1;  # spaces
  48.  
  49. $cols = int($cols / $maxlen);
  50. $rows = int(($#list+$cols) / $cols);
  51. $mask = sprintf("%%-%ds ", $maxlen);
  52.  
  53. for ($elt = 0; $elt < $rows * $cols; $elt++) { 
  54.     $target =  ($elt%$cols) * $rows + int(($elt/$cols));
  55.     $piece = sprintf($mask, $target < ($#list+1) ? $list[$target] : "");
  56.     $piece =~ s/\s+$// if do eol();  # don't blank pad to eol of line
  57.     print $piece;
  58.     print "\n" if do eol();
  59. }
  60.  
  61. print "\n" if do eol();
  62.  
  63. exit 0;
  64.  
  65. sub getwin {
  66.     $TIOCGWINSZ = 0x40087468;  # should be require sys/ioctl.pl
  67.     if (ioctl(STDOUT, $TIOCGWINSZ, $winsize)) {
  68.     ($rows, $cols, $xpixel, $ypixel) = unpack('S4', $winsize);
  69.     } else {
  70.     $cols = 80;
  71.     }
  72. }
  73.