Article View: news.software.nn
Article #4837Re: Script to sort .newsrc similar as presentation sequence?
From: Alan Barrett
Date: Thu, 23 Sep 1993 14:53
Date: Thu, 23 Sep 1993 14:53
288 lines
10507 bytes
10507 bytes
In article <samik.748476449@uta.fi>,
samik@kaktus.uta.fi writes:
> Is there some way to sort .newsrc so that groups are in same order as
> in the presentation sequence defined in .nn/init file?
This is slow, and parses only a subset of the stuff that
can live in a .nn/init file, but it works for me.
--apb
Alan Barrett, Dept. of Electronic Eng., Univ. of Natal, Durban, South Africa
RFC822: barrett@ee.und.ac.za
#! /bin/sh
# This is a shell archive. Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file". To overwrite existing
# files, type "sh file -c". You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g.. If this archive is complete, you
# will see the following message at the end:
# "End of shell archive."
# Contents: sort-newsrc
# Wrapped by barrett@lucy.ee.und.ac.za on Thu Sep 23 16:51:31 1993
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'sort-newsrc' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'sort-newsrc'\"
else
echo shar: Extracting \"'sort-newsrc'\" \(7895 characters\)
sed "s/^X//" >'sort-newsrc' <<'END_OF_FILE'
X#!/usr/bin/perl
X
X# Sort a .newsrc file, according to sequence information like that
X# used by nn.
X
X# A. P. Barrett <barrett@ee.und.ac.za>, December 1990.
X
X# Inputs:
X# 1. The old .newsrc file. Presented on stdin.
X# 2. A .nn/init file, or reasonable facsimilie thereof. Named
X# as a command line argument, or defaulted to $HOME/.nn/init
X# if that exists.
X#
X# Output:
X# A new .newsrc file. Sent to stdout.
X#
X# Action:
X# The .newsrc file is sorted according to the sequence information
X# in the .nn/init file. This will not necessarily be identical to the
X# order used by nn, because we do not read the global nn/init file, and
X# we make no attempt to deal with non-vanilla features of nninit files
X# or newsrc files.
X#
X# Format of nninit file:
X# The format is actually more complicated; this script supports only the
X# following subset.
X# 1. '#' comments work as expected.
X# 2. Everything up to and including a line containing just the word
X# 'sequence' is ignored.
X# 3. Everything from the 'sequence' line to the end of the file defines
X# the presentation sequence.
X# 4. A group or set of groups in included by listing its name. 'comp.unix'
X# matches just one group; 'comp.unix.' matches all subgroups of comp.unix,
X# but does not match the parent group; 'comp.unix*' matches both the
X# parent and subgroups. '.test' matcehs all groups whose name ends with
X# .test.
X# 5. A group is excluded by placing a '!' before its name.
X# 6. A group is pushed to the start or end of the sequence by preceding it
X# with a '<' or a '>'.
X# 7. A group that has been excluded may be reconsidered between a pair of
X# '%' signs. Thus, '! comp.misc comp. % comp.misc %' will force
X# comp.misc to appear after all other comp groups.
X# 8. Nn allows several groups may be combined by separating them with commas
X# instead of spaces. This script treats commas in the same way as it
X# treats spaces.
X# 9. Nn allows a group to have a save file and an entry sequence associated
X# with it. This script ignores such options.
X
X########################################################################
X
X@OLDNEWSRC = (); # group names from old newscr file,
X # in the order that they were read.
X # (unused at present; when the RC flag in the nninit
X # file is implemented, this array will probably be
X # useful.)
X
X%INFO = (); # information about each group. This is the line
X # from the newsrc file (apart from the group name
X # itself).
X
X@SELECTED = (); # group names for new newsrc file,
X # in the order selected by the nninit file.
X
X@TAILEND = (); # groups forced to end of sequence by '>' in nninit.
X
X%UNTOUCHED = (); # groups not yet either selected or deselected
X # by the nninit file.
X
X%DESELECTED = (); # groups deselected by '!' in nninit.
X
X$debug = 0; # debugging?
X
X$NNINIT = $ENV{"HOME"}."/.nn/init"; # default nninit file
X
X#
X# Process command line args.
X#
X# This is probably one of the stupidest command line parsers you
X# have seen for quite a while.
X#
X
Xwhile ($_ = shift (@ARGV)) {
X if (/-v/) {
X $debug++;
X }
X else {
X $NNINIT = $_;
X }
X}
X
X#
X# Read the .newsrc file from stdin.
X#
X
Xwhile (<>) {
X # split into group name and other info
X ($group,$info) = /(.*)([!:].*)/;
X warn "read from newsrc: $group\n" if $debug >= 4;
X # remember the info (subscribed or unsubscribed; what has been read)
X $INFO{$group} = $info;
X # remember the order that the groups appeared in the old newsrc
X push (@OLDNEWSRC, $group);
X # the group is still waiting to be selected
X $UNTOUCHED{$group} = 1;
X}
X
X#
X# Open, slurp and close nninit file
X#
X
Xopen (NNINIT, $NNINIT) || die "cannot open nn init file: $NNINIT: $!\n";
Xundef $/; # get ready to slurp
X$_ = <NNINIT>; # slurp the file
Xclose (NNINIT); # hope there is no error here
X
X#
X# Now process the nninit file.
X#
X
X undef $lastword; # previous word
X $between_percents = 0; # this is true inside a '% ... %' construct.
X
X s/#[^\n]*//g; # ignore comments
X s/^(.*\n)*\s*sequence\s*\n//; # ignore stuff before sequence line
X s/\(([^\)]|\\.)*\)//g; # ignore entry action inside parentheses
X # (allow for \ escapes)
X s/,/ /g; # treat commas like spaces
X s#\b[+/~]\S*##g; # ignore save file names (words
X # starting with +, / or ~)
X s/!:\S+/!/; # treat '!:stuff' like '!'
X s/([!%<>])/ $1 /; # put spaces around special chars
X # to facilitate breaking into words
X s/^\s+//; s/\s+$//; # kill leading and trailing blanks
X
X foreach $word (split (/\s+/,$_)) { # process each word
X warn "word from nninit: $word\n" if $debug > 3;
X
X # '!', '<' and '>' must be followed by group name
X if (($lastword =~ /[!<>]/) && ($word =~ /[%!<>]/)) {
X die "$NNINIT line $.: !, < or > must be followed by group name\n";
X }
X
X # '%' toggles state
X elsif ($word eq '%') {
X $between_percents = ! $between_percents;
X }
X
X # '!', '<' and '>' just get remembered
X elsif ($word =~ /[!<>]/) {
X # do nothing
X }
X
X # otherwise it must be a group name to be selected or deselected
X else {
X @MATCHED = ();
X # check the untouched groups
X &find_matches (*UNTOUCHED, *MATCHED, $word);
X # if between percents, also check deselected groups
X &find_matches (*DESELECTED, *MATCHED, $word) if $between_percents;
X
X if ($#MATCHED < 0) {
X warn "nothing matched: $word\n" if $debug;
X }
X # process the groups just matched
X elsif ($lastword eq '!') {
X foreach $group (@MATCHED) {
X $DESELECTED{$group} = 1;
X }
X warn "deselect: ".join(" ",sort(@MATCHED))."\n" if $debug;
X }
X elsif ($lastword eq '<') {
X unshift (@SELECTED, sort(@MATCHED));
X warn "force to start: ".join(" ",sort(@MATCHED))."\n" if $debug;
X }
X elsif ($lastword eq '>') {
X push (@TAILEND, sort(@MATCHED));
X warn "force to end: ".join(" ",sort(@MATCHED))."\n" if $debug;
X }
X else {
X push (@SELECTED, sort(@MATCHED));
X warn "select: ".join(" ",sort(@MATCHED))."\n" if $debug;
X }
X }
X
X # remember the word just read
X $lastword = $word;
X
X } # process next word
X
X#
X# Now we can output the results.
X# As well as the selected groups (from the @SELECTED and
X# @TAILEND arrays), we also output deselected groups
X# and untouched groups
X#
Xwarn "SELECTED: ".join(" ",@SELECTED)."\n" if $debug >= 2;
Xwarn "TAILEND: ".join(" ",@TAILEND)."\n"
X if ($debug >= 2 && $#TAILEND >= 0);
Xwarn "UNTOUCHED: ".join(" ",keys(%UNTOUCHED))."\n"
X if ($debug >= 1 && $#UNTOUCHED >= 0);
Xwarn "DESELECTED: ".join(" ",keys(%DESELECTED))."\n"
X if ($debug >= 1 && $#DESELECTED >= 0);
X
Xforeach $group (@SELECTED, @TAILEND, keys(%UNTOUCHED), keys(%DESELECTED)) {
X print $group, $INFO{$group}, "\n";
X}
X
X#############
X
Xsub find_matches
X#
X# find all groups matching a pattern. delete them from the input
X# associative array and push them onto the output array.
X# Returns number of matches.
X#
X{
X local (*IN, *OUT, $pattern) = @_;
X local ($count) = 0;
X
X # if the pattern is not a wildcard, quickly do what is required.
X if (($pattern !~ /^\./) && ($pattern !~ /[.*]$/)) {
X if (delete $IN{$pattern}) {
X push (@OUT, $pattern);
X $count++;
X warn "matched: $pattern\n" if $debug >= 3;
X }
X }
X
X # if the pattern is a wildcard, check each group for a match
X else {
X $pattern =~ s/(\W)/\\$1/g; # quote all special chars in the pattern
X $pattern = "^$pattern$"; # anchor the pattern
X $pattern =~ s/\\\*/.*/g; # '*' becomes '.*'
X $pattern =~ s/\\\.\$$/\\./; # '.' at end un-anchors pattern
X $pattern =~ s/^\^\\\./\\./; # '.' at start un-anchors pattern
X $pattern =~ s/^\^\.\*//; # '^.*' at start is useless
X $pattern =~ s/\.\*\$$//; # '.*$' at end is useless
X warn "matching pattern: /$pattern/\n" if $debug >= 3;
X
X foreach $group (keys (%IN)) {
X if ($group =~ /$pattern/) {
X delete $IN{$group};
X push (@OUT, $group);
X $count++;
X warn "matched: $group\n" if $debug >= 3;
X }
X }
X }
X
X # return result
X $count;
X}
END_OF_FILE
if test 7895 -ne `wc -c <'sort-newsrc'`; then
echo shar: \"'sort-newsrc'\" unpacked with wrong size!
fi
chmod +x 'sort-newsrc'
# end of 'sort-newsrc'
fi
echo shar: End of shell archive.
exit 0
Technical Details
Message-ID:
<27sd95$1qg@lucy.ee.und.ac.za>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!archive!mbox2nntp-news.software.nn.mbox.zip!gmd.de!xlink.net!howland.reston.ans.net!math.ohio-state.edu!cs.utexas.edu!uunet!psgrain!ee.und.ac.za!tplinfmReferences:
<samik.748476449@uta.fi>Show Full Headers
Complete Article Headers
Message-ID: <27sd95$1qg@lucy.ee.und.ac.za> Subject: Re: Script to sort .newsrc similar as presentation sequence? Date: Thu, 23 Sep 1993 14:53:25 +0000 From: barrett@lucy.ee.und.ac.za (Alan Barrett) References: <samik.748476449@uta.fi> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!archive!mbox2nntp-news.software.nn.mbox.zip!gmd.de!xlink.net!howland.reston.ans.net!math.ohio-state.edu!cs.utexas.edu!uunet!psgrain!ee.und.ac.za!tplinfm Newsgroups: news.software.nn Organization: Elec. Eng., Univ. Natal, Durban, S. Africa Lines: 288 NNTP-Posting-Host: lucy.ee.und.ac.za