home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3476 < prev    next >
Internet Message Format  |  1991-06-10  |  5KB

  1. From: dwallach@soda.berkeley.edu (Dan Wallach)
  2. Newsgroups: comp.lang.perl,alt.sources
  3. Subject: Yet another newsrc fixer, new and improved.
  4. Message-ID: <1991Jun11.062654.9679@agate.berkeley.edu>
  5. Date: 11 Jun 91 06:26:54 GMT
  6.  
  7. Well, I've gotten some interesting feedback about my newsrc fixer.
  8. It appears that you need a fairly recent version of perl to make it
  9. work, as older versions barf on the $ENV{...} stuff.  Well, I don't
  10. have docs for anything before version 4, and I'm fairly happy that
  11. it works under 3, pl.44.  I'll eventually figure out how to make this
  12. thing totally portable, but until then...
  13.  
  14. Here's version 1.1 of fixnewsrc (try typing "newsrc" really fast...)
  15. which is far more robust in its error detection, and also takes full
  16. Perl regular expressions in your .news.favorite file if you like.
  17.  
  18. It's not exactly the most stylish perl out there, but I'm happy.
  19.  
  20. Don't worry, be happy,
  21.  
  22. Dan Wallach
  23. dwallach@soda.berkeley.edu
  24.  
  25.  
  26. -------- cut here ---------
  27. #!/usr/bin/perl
  28.  
  29. # FixNewsrc V1.1 by Dan Wallach
  30. # dwallach@soda.berkeley.edu
  31.  
  32. # run "fixnewsrc -help" for documentation
  33.  
  34. # New features in 1.1:
  35. #
  36. # handles arbitrary Perl patterns via eval (idea from Liam Quin's awk script)
  37. # handles stranger and more obscure error cases (happy, Tom? :-)
  38.  
  39. # return true if it actually printed anything
  40. sub counter {
  41.     $counter++;
  42.     if(($counter % 100) == 0) {
  43.     &clear_blurt;
  44.     print STDERR "$counter..." if ($counter % 100) == 0;
  45.     return 1;
  46.     }
  47.     return 0;
  48. }
  49.  
  50. sub tally_counter {
  51.     print STDERR "$counter";
  52.     print STDERR (defined $verbosity)?" total\n\n":"\n";
  53. }
  54.  
  55. sub clear_counter {
  56.     $counter = 0;
  57. }
  58.  
  59. sub blurt {
  60.     return unless $verbosity;
  61.     print STDERR "\n" unless $prev_blurt;
  62.     print STDERR @_;
  63.     $prev_blurt = 1;
  64. }
  65.  
  66. sub clear_blurt {
  67.     $prev_blurt = 0;
  68. }
  69.  
  70. sub insert {
  71.     local($group) = split(/[:!]/, @_[0]);
  72.     if(!defined $newsrc{$group}) {
  73.         &blurt("Warning: $group not in .newsrc!\n")
  74.         if !defined($inserted{$group});
  75.         next;
  76.     }
  77.  
  78.     &blurt(">> $_\n");
  79.     &counter;
  80.     push (@output, $newsrc{$group});
  81.     $inserted{$group} = 1;
  82.     delete $newsrc{$group};
  83. }
  84.  
  85. sub print_favorites {
  86.     print STDERR "Parsing favorites: ";
  87.     favorite: foreach(<FAVORITE>) {
  88.     chop;
  89.     s/\s*\#.*$//;
  90.     next if /^$/;
  91.  
  92.     if(/\(/) {
  93.         &blurt("Matching: $_\n");
  94.         $pattern = $_;
  95.         foreach (@newsrc) {
  96.         eval <<END_OF_EVAL;
  97.             if ($pattern) {
  98.             &insert(\$_);
  99.             }
  100. END_OF_EVAL
  101.         }
  102.         &blurt("Match complete\n");
  103.         next favorite;
  104.     }
  105.     &insert($_);
  106.     }
  107.     &tally_counter;
  108. }
  109.  
  110. if(@ARGV == 1 && $ARGV[0] eq "-v") {
  111.     # verbose mode on
  112.     $verbosity = 1;
  113.     shift;
  114. }
  115.  
  116. if(@ARGV) {
  117.     print STDERR <<NO_MORE_HELP;
  118. fixnewsrc, V1.1 by Dan Wallach <dwallach@soda.berkeley.edu>
  119. Usage: $0       [-v] [any other argument]
  120.     -v == more verbose
  121.     anything else == this help message
  122.  
  123. This program sorts your .newsrc, putting groups you read on top.  In addition,
  124. if you have a file in your home directory called .news.favorite, then the
  125. list of newsgroups in this file appear at the top of your .newsrc, so you
  126. can still read groups in your favorite order.
  127.  
  128. Put any Perl expression you want to describe your group in parenthesis, and
  129. that's good, too.  If it's not in parenthesis, it's considered to be exact.
  130. Remember: you're matching on :'s and !'s, too.
  131.  
  132. # Example:
  133. rec.humor.funny                        # comments, and blank lines are cool
  134. alt.fan.warlord
  135. ucb.computing.announce
  136. comp.lang.perl
  137. (/comp\\.text\\..*/ && (!/comp\\.text\\.tex/))  # comp.text everything but tex
  138. # Here's a more complicated one which matches "nas" and "nas.msgs"
  139. # but not "nasa.nudge" or "arc.nasamail.arc"
  140. (/^nas\\..*/ || /^nas[:!]/)
  141. NO_MORE_HELP
  142.     exit 0;
  143. }
  144.  
  145. die "No .newsrc file!  Crapped out at" unless -e "$ENV{HOME}/.newsrc";
  146. open(NEWSRC, "$ENV{HOME}/.newsrc") || die "Can't open .newsrc: $!, crapped out at";
  147.  
  148. # we want to keep this associative array around for printing favorites
  149. # so if we've already printed something, we just delete it from the
  150. # associative array, and go on.
  151.  
  152. print STDERR "Reading groups: ";
  153. &clear_counter;
  154. foreach(<NEWSRC>) {
  155.     chop;
  156.     next if /^$/;
  157.     &counter;
  158.     $fullentry = $_;
  159.     s/[:!].*$//;
  160.     &blurt("Warning: $_ appears more than once!\n") if defined($newsrc{$_});
  161.     $newsrc{$_} = $fullentry;
  162. }
  163. &tally_counter;
  164.  
  165. print STDERR "Sorting..." if $verbosity;
  166. @newsrc = sort values %newsrc;
  167. print STDERR "Done\n" if $verbosity;
  168. # output time... clear the counter and let's deal with the favorites file
  169. &clear_counter;
  170.  
  171. if (open(FAVORITE, "$ENV{HOME}/.news.favorite")) {
  172.     &print_favorites;
  173. } else {
  174.     print STDERR "No .news.favorite file found.  Just sorting .newsrc\n";
  175. }
  176.  
  177. # yeah, we have to do it twice... It's good enough...
  178. undef @newsrc;
  179. print STDERR "Sorting again..." if $verbosity;
  180. @newsrc = sort values %newsrc;
  181. print STDERR "Done\n" if $verbosity;
  182. print STDERR "Generating output: ";
  183.  
  184. #
  185. # I could just grep through the array for :'s then !'s, but that requies
  186. # making two passes.  This works in one pass.
  187. #
  188. foreach(@newsrc) {
  189.     if(/:/) {
  190.     &counter;
  191.     push (@output, $_);
  192.     } elsif (/!/) {
  193.     &counter;
  194.     push (@output2, $_);
  195.     }
  196. }
  197. &tally_counter;
  198.  
  199. close(NEWSRC);
  200. rename("$ENV{HOME}/.newsrc", "$ENV{HOME}/.newsrc.bak") ||
  201.     die "Can't rename .newsrc: $!, crapped out at";
  202.  
  203. open(NEWSRC, "> $ENV{HOME}/.newsrc") || die "Can't open .newsrc for writing: $!, crapped out at";
  204.  
  205. $\ = $, = "\n";
  206. print NEWSRC @output, @output2;
  207.