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

  1. From: lee@sq.sq.com (Liam R. E. Quin)
  2. Newsgroups: alt.sources
  3. Subject: newsorder -- re-order your .newsrc by preference
  4. Message-ID: <1991Jun10.040158.2938@sq.sq.com>
  5. Date: 10 Jun 91 04:01:58 GMT
  6.  
  7. I've been meaning to post this for ages...  And then last week someone posted
  8. something similar, so here goes!
  9.  
  10. This awk script (you need the "new" awk or nawk/gawk/mawk) reads a template
  11. file (~/.newsorder), and sorts your .newsrc based on what it sees there.
  12. Here's some of mine, with annottions.  There's a smaller smaple in the
  13. distribution itself.
  14.  
  15.  
  16.     # This file is read by newsorder as a template for sorting
  17.     # my .newsrc file.
  18.     sq.*        # I want to see local newsgroups first...
  19.     comp.text.sgml    # then SGML cos that's our business :-) :-)
  20.     comp.text*, !comp.text.tex
  21.             # all the rest of the text groups except the tex one
  22.     *.frp
  23.     *.magick        # now some relaxation...
  24.     *.pagan
  25.     *.rpg
  26.     comp.font*        # more work:
  27.     comp.text.tex
  28.     comp.archives
  29.     *windows*
  30.     *sources* !*.d !*.wanted    # some sources groups
  31.     #new newsgroups:
  32.     /^.*:$/        # this pattern matches new newsgroups, so if
  33.             # rm adds them to my .newsrc, I can run newsorder
  34.             # and they all end up here where I see them.
  35.  
  36.     # All other newsgroups go here, in the order in which they appear
  37.     # in my .newsrc...
  38.  
  39.  
  40. : To unbundle, sh this file  ---- cut here ---- cut here ---- snip! ---- ow!
  41. echo x - Makefile 1>&2
  42. sed 's/^X//' >Makefile <<'@@@End of Makefile'
  43. X# Makefile for newsorder
  44. X# Liam Quin, 1991
  45. X# $Id: Makefile,v 1.6 91/02/07 17:58:40 lee Exp $
  46. X
  47. XBINDIR=/home/lee/scripts
  48. XMAKE=make
  49. XCLEAN=clean
  50. X
  51. XMANDIR=/usr/man/man1
  52. XMANSECTION=1L
  53. Xnewsorder: newsorder.sh
  54. X    cp newsorder.sh newsorder
  55. X    chmod 755 newsorder
  56. X
  57. Xinstall: newsorder
  58. X    -rm -f $(BINDIR)/newsorder
  59. X    cp newsorder $(BINDIR)/newsorder
  60. X    chmod 555 $(BINDIR)/newsorder
  61. X    # -rm -f $(MANDIR)/newsorder.${MANSECTION}
  62. X    # sed -e 's/ 1L / $(MANSECTION) /' newsorder.man \
  63. X    #       > $(MANDIR)/newsorder.$(MANSECTION)
  64. X    $(MAKE) $(CLEAN)
  65. X
  66. Xclean:
  67. X    -rm -f newsorder .newsorder shar core *.o *.out
  68. X    @: delete files that are stored in RCS format and unchanged:
  69. X    -for i in * ; do \
  70. X        test -f RCS/$$i,v && rcsdiff $$i && rm $$i ; \
  71. X    done
  72. X    @: remind me that there are still files here...
  73. X    -/bin/ls -l
  74. X
  75. Xshar: Makefile newsorder.sh .newsorder
  76. X    -rm -f shar
  77. X    bundle $? > shar
  78. X
  79. XMakefile:: RCS/Makefile,v
  80. X    co -u Makefile
  81. X
  82. Xnewsorder.sh:: RCS/newsorder.sh,v
  83. X    co -u newsorder.sh
  84. X
  85. X.newsorder:: RCS/.newsorder,v
  86. X    co -u .newsorder
  87. @@@End of Makefile
  88. echo x - newsorder.man 1>&2
  89. sed 's/^X//' >newsorder.man <<'@@@End of newsorder.man'
  90. X.\" @(#)$Id: newsorder.man,v 1.5 91/02/07 17:49:03 lee Exp $
  91. X.TH NEWSORDER 1L
  92. X.SH NAME
  93. Xnewsorder \- sort netnews .newsrc files by specified template
  94. X.SH SYNOPSIS
  95. X.B newsorder
  96. X[
  97. X.I inut-file
  98. X] 
  99. X.SH DESCRIPTION
  100. X.I newsorder
  101. Xreads the file
  102. X.I "$HOME/.newsorder"
  103. Xand uses it as a basis to sort your
  104. X.I \&.newsrc
  105. Xfile.
  106. XIf a filename option is given, this is sorted instead of your
  107. X.I .newsrc
  108. Xfile.
  109. XThe filename \- is taken to be standard input.
  110. X.PP
  111. XEach line in the template file names a group or hierarchy of news-groups.
  112. XFor example:
  113. X.sp 0.5
  114. X.RS
  115. X# newsorder file for Liam
  116. X.br
  117. Xsq.*  # SoftQuad local groups first
  118. X.br
  119. X*frp  # anything that ends in frp
  120. X.br
  121. Xcomp.text* !comp.text.scribe # all the comp.text groups except one
  122. X.br
  123. Xcomp.lang*, !comp.lang.postscript, !*.intercal
  124. X.sp 0.5
  125. X.RE
  126. X.LP
  127. XNote that there is a difference between .* and * \- in particular,
  128. X`comp.text.*' would not match the `comp.text' newsgroup.
  129. X.PP
  130. XAny lines in your
  131. X.I \&.newsrc
  132. Xfile that don't match any of the patterns will end up at the end of the new
  133. Xfile, in the order in which they were found.  Hence,
  134. X.I \&newsorder
  135. Xhas the effect of selecting certain groups and moving them to the front,
  136. Xso that the news-readers will present them first.
  137. X.PP
  138. X.I Newsorder
  139. Xwill prompt you asking whether or not to overwrite your
  140. X.I \&.newsrc
  141. Xfile.  The resulting file is left in
  142. X.IR ~/.newsrc-sorted ,
  143. Xand optionally copied onto
  144. X.I ~/.newsrc
  145. Xitself.
  146. XIn any case a copy of your original unsorted
  147. X.I .newsrc
  148. Xis made in
  149. X.IR ~/.newsrc-unsorted .
  150. X.SH AUTHOR
  151. XLiam Quin, after an idea by Bob Gibson.
  152. X.SH BUGS
  153. XCould be more enthusiastic about looking for your
  154. X.I \&.newsrc
  155. Xfile.
  156. X.br
  157. XThe name
  158. X.I newsorder
  159. Xis probably a little long for System V users.
  160. X.
  161. X.\" $Log:    newsorder.man,v $
  162. X.\" Revision 1.5  91/02/07  17:49:03  lee
  163. X.\" Fixed documentation of argument.
  164. X.\" 
  165. X.\" Revision 1.4  91/02/07  17:26:35  lee
  166. X.\" Put all filenames into italic, and changed the section from V to L.
  167. X.\" 
  168. X.\" Revision 1.3  91/02/07  17:12:44  lee
  169. X.\" Documented filename - as stdin and the backup of the .newsrc file.
  170. X.\" 
  171. X.\" Revision 1.2  91/01/31  20:47:15  lee
  172. X.\" fixed a typo in the BUGS section!
  173. X.\" 
  174. X.\" Revision 1.1  91/01/31  20:45:47  lee
  175. X.\" Initial revision
  176. X.\" 
  177. X.\" 
  178. @@@End of newsorder.man
  179. echo x - newsorder.sh 1>&2
  180. sed 's/^X//' >newsorder.sh <<'@@@End of newsorder.sh'
  181. X#! /bin/sh
  182. X# $Id: newsorder.sh,v 1.7 91/04/23 15:43:27 lee Exp $
  183. X# Liam Quin, 1991
  184. X
  185. X# newssort -- sort .newsrc ino order based on template
  186. X
  187. XTemplateFile="$HOME/.newsorder"
  188. X
  189. X# determine echo variant for prompts...
  190. XN=-n; C=''; if test `echo 'hello\c'` = 'hello'; then N=''; C='\c'; fi
  191. Xexport N C
  192. X
  193. XCMDNAME=${CMDNAME-"`basename $0`"}
  194. Xexport CMDNAME
  195. X
  196. Xif test x"$1" = x""
  197. Xthen
  198. X    InputFile="$HOME/.newsrc"
  199. Xelif test -f "$1"
  200. Xthen
  201. X    InputFile="$1"
  202. Xelif test x"$1" = x"-"
  203. Xthen
  204. X    InputFile="-"
  205. Xelse
  206. X    echo "Usage: `basename $0` [input-file (default is $HOME/.newsrc)]" 1>&2
  207. X    exit 1
  208. Xfi
  209. X
  210. Xcat "$InputFile" > $HOME/.newsrc-unsorted
  211. X
  212. Xcat "$InputFile" |
  213. Xnawk '
  214. X
  215. XBEGIN {
  216. X    # FORMAT is how the lines are printed.   The first number is the weighting
  217. X    # for sorting -- lower numbers go higher up in .newsrc -- the 2nd is the
  218. X    # input line number, so that the sort is stable.
  219. X    FORMAT = "%d %d %s\n"
  220. X
  221. X    # read the template file
  222. X    while (getline < "'"$TemplateFile"'") {
  223. X    gsub(/[     ]*#.*$/, "") # delete comments
  224. X    if ($0 ~ /^[     ]*$/) {
  225. X        # ignore blank lines
  226. X        continue
  227. X    }
  228. X    if ($0 ~ /^\/.*\//) {
  229. X        Line = $0
  230. X        sub(/^\//, "")
  231. X        sub(/\/[^\/]*$/, "")
  232. X        Patterns[++PatCount] = $0
  233. X        MatchField[PatCount] = 0 # match on the entire line
  234. X        $0 = Line
  235. X        sub(/^\/.*\//, "") # to allow (literal) inclusions and exclusions
  236. X    } else {
  237. X        gsub(/,/, " ") # turn , into space
  238. X        gsub(/[.]/, "\\.")
  239. X        gsub(/[*]/, ".*")
  240. X        Patterns[++PatCount] = "^" $1 "[:!]*$"
  241. X        MatchField[PatCount] = 1 # match on the newsgroup name only
  242. X    }
  243. X    # Uncommment the next 2 lines to see the patterns...
  244. X    printf "PatCount %d Pattern /%s/\n",
  245. X        PatCount, Patterns[PatCount] > "/dev/tty"
  246. X
  247. X    # deal with exclusions:
  248. X
  249. X    for (i = 2; i <= NF; i++) {
  250. X        if ($i ~ /^!/) {
  251. X        sub(/[!]/, "", $i)
  252. X        j = ++HasExclusions[PatCount]
  253. X        Exclusions[PatCount,j] = "^" $i "[:!]*$"
  254. X        } else {
  255. X        printf "'"${CMDNAME}"': '"${TemplateFile}"': %d: %s",PatCount,
  256. X            "Exclusion patterns mst start with a !\n" | "cat 1>&2"
  257. X        exit 1
  258. X        }
  259. X    }
  260. X    }
  261. X}
  262. X
  263. X# Now go through .newsrc and assign a priority to each line
  264. X
  265. X/^[     ]*$/ { next }
  266. X/^[oO]ptions:/ { printf FORMAT, 0, NR, $0; next }
  267. X
  268. X{
  269. X    # Pattern 0 is used for the Options: line which must come first,
  270. X    # so we loop from 1 to PatCount...
  271. X    for (i = 1; i <= PatCount; i++) {
  272. X    if ($(MatchField[i]) ~ Patterns[i]) {
  273. X        Wanted = 1
  274. X        if (i in HasExclusions) {
  275. X        # see if it matches any of the exclusion patterns:
  276. X        for (j = 1; j <= HasExclusions[i]; j++) {
  277. X            if ($1 ~ Exclusions[i,j]) {
  278. X            # Aha!  Not wanted here...
  279. X            Wanted = 0
  280. X            break
  281. X            }
  282. X        }
  283. X        }
  284. X        if (Wanted) {
  285. X        printf FORMAT, i, NR, $0
  286. X        if (!(i in Matched)) {
  287. X            PatUsedCount++ # Used a pattern for the first time
  288. X        }
  289. X        Matched[i]++
  290. X        next
  291. X        }
  292. X    }
  293. X    }
  294. X    printf FORMAT, i, NR, $0
  295. X    next
  296. X}
  297. X
  298. XEND {
  299. X    # display information about which patterns worked:
  300. X    printf "'"${CMDNAME}"': Used %d out of %d patterns from template \"%s\"\n",
  301. X            PatUsedCount, PatCount, "'"${TemplateFile}"'" | "cat 1>&2"
  302. X    for (i = 1; i <= PatCount; i++) {
  303. X    if (!(i in Matched)) {
  304. X        printf "'"$0"': Unused: %d: /%s/\n",
  305. X            i, Patterns[i] | "cat 1>&2"
  306. X    }
  307. X    }
  308. X}
  309. X' |sort +0n +1n |sed -e 's/^[0-9][0-9]* [0-9][0-9]* //' > $HOME/.newsrc-sorted
  310. X
  311. Xwait # in case the cat hasn't finished from within awk
  312. X
  313. Xecho $N "Overwrite $HOME/.newsrc with sorted version? $C" 1>&2
  314. Xread ans
  315. X
  316. Xcase "$ans" in
  317. X[yY]*)    cp $HOME/.newsrc-sorted $HOME/.newsrc || exit 1 ;;
  318. X*)    echo "Sorted output left in $HOME/.newsrc-sorted" 1>&2 ;;
  319. Xesac
  320. X
  321. Xexit $?
  322. X
  323. X#
  324. X# $Log:    newsorder.sh,v $
  325. X# Revision 1.7  91/04/23  15:43:27  lee
  326. X# Now recognises a pattern like /.../ in the 1st field as an expression to
  327. X# match over the entire input line, so you can use /:$/ to match new
  328. X# newsgroups.
  329. X# 
  330. X# Revision 1.6  91/02/07  17:09:55  lee
  331. X# Improved the arguments to sort after Bob Gibson (rgj@sq.com) found a bug
  332. X# 
  333. X# Revision 1.5  91/01/31  20:44:35  lee
  334. X# Improved error-checking and replaced "cp -i" with an explicit test.
  335. X# 
  336. X# Revision 1.4  91/01/30  19:28:01  lee
  337. X# Improved error message slightly.
  338. X# 
  339. X# Revision 1.3  91/01/30  19:25:47  lee
  340. X# Added exclusion patterns, so you can do
  341. X# comp.text*, !comp.text.tex
  342. X# if you like.
  343. X# 
  344. X# Comments (introduced with a #) and blank lines are now ignored.
  345. X# 
  346. X# Revision 1.2  91/01/30  16:49:38  lee
  347. X# Added summary statistics.
  348. X# 
  349. X# Revision 1.1  91/01/29  23:10:45  lee
  350. X# Initial revision
  351. X# 
  352. X#
  353. @@@End of newsorder.sh
  354. echo End of archive
  355. exit 0
  356. -- 
  357. Liam Quin, lee@sq.com, SoftQuad, Toronto, +1 416 963 8337
  358. the barefoot programmer
  359.