Article View: news.software.nn
Article #4828Re: Script to sort .newsrc similar as presentation sequence?
From: Amos Shapir
Date: Wed, 22 Sep 1993 14:39
Date: Wed, 22 Sep 1993 14:39
393 lines
11465 bytes
11465 bytes
samik@uta.fi (Sami K|ykk{) 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?
I have been using the following program for a while -- so long in
fact, that I forget where I got it from...
o / o / o / o /
--Cut-here-------X---------------X---------------X---------------X----
o \ o \ o \ o \
/* re-order the .newsrc according to my likes */
#include <stdio.h>
#include <strings.h>
/*
* misc utility routines
*/
/* like malloc, except emalloc() will abort instead of returning NULL */
static char *emalloc(len)
int len; /* number of bytes to allocate */
{
char *malloc();
char *new = malloc(len);
if (!new) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
return new;
}
/* return a copy of string str */
static char *stralloc(str)
char *str; /* string to copy */
{
return strcat(emalloc(strlen(str) + 1), str);
}
/* like strncmp, except the lengths of both strings are supplied */
static int mystrncmp(str1, str1len, str2, str2len)
char *str1; /* first string */
int str1len; /* str1 length */
char *str2; /* second string */
int str2len; /* str2 length */
{
if (str1len < str2len) {
int tmp = strncmp(str1, str2, str1len);
if (tmp == 0) /* longest prefix is equal */
return -1;
return tmp;
} else if (str1len > str2len) {
int tmp = strncmp(str1, str2, str2len);
if (tmp == 0) /* longest prefix is equal */
return 1;
return tmp;
} /* else str1len == str2len */
return strncmp(str1, str2, str1len);
}
/*
* low level group list routines/data
*/
/* return the category index for category at grp and catlen characters long */
/* return -1 if category is not found */
static int catindex(grp, catlen)
char *grp; /* group name string */
int catlen; /* category name length */
{
char **cat; /* loop index */
static char *categories[] = { /* the category names */
/* edit to taste and locale */
/* most local to all but most global */
"general",
/* among the most global, */
/* interesting/important to boring/worthless */
"control",
"local",
"il",
"comp",
"gnu",
"misc",
"news",
"alt",
"sci",
"soc",
"talk",
"rec",
"clari",
"usa-today",
"trial",
"bionet",
"bit",
"cs",
"dcomp",
"fa",
"mail",
"u3b",
"unix-pc",
"test",
"junk",
};
/* number of entries in categories[] */
#define NCATEGORIES (sizeof categories / sizeof categories[0])
for (cat = categories; cat < &categories[NCATEGORIES]; ++cat) {
if (catlen == strlen(*cat) && strncmp(grp, *cat, catlen) == 0)
return cat - categories;
}
return -1;
}
/* a group list element */
struct group { /* a doubly-linked list for easy insertion */
struct group *next; /* next struct group in list */
struct group *prev; /* previous struct group in list */
char *group; /* the original input line */
int catindex; /* corresponding index into categories */
int categorylen; /* length of category prefix */
int grouplen; /* length of group prefix */
};
struct group *groups = NULL; /* pointer to the head of the group list */
struct group *end_groups = NULL; /* pointer to the tail of the group list */
/* loop to iterate through the groups, indexed by cur */
#define FOREACH_GROUP(cur) for (cur = groups; cur; cur = cur->next)
#ifdef DEBUG /* for debugging */
/* print the data in a struct group */
static void groupprint(grp)
struct group *grp; /* pointer to the group to print */
{
printf("%x next %x prev %x cat %d catlen %d grplen %d %s", grp,
grp->next, grp->prev, grp->catindex, grp->categorylen,
grp->grouplen, grp->group);
}
/* print all groups */
static void groupallprint()
{
struct group *grp; /* loop index */
FOREACH_GROUP(grp) groupprint(grp);
}
#endif /* DEBUG */
/* return a struct group, initialized from line, abort if out of memory */
static struct group *groupalloc(line)
char *line; /* the .newsrc line to "parse" */
{
register struct group *new; /* pointer to the "parsed" .newsrc line */
/* allocate/initialize a new group entry */
new = (struct group *)emalloc(sizeof(struct group));
new->group = stralloc(line);
/* find group length (first [:!\n]) */
new->grouplen = strcspn(line, ":!\n");
/* find category length (first [.:!\n]) */
new->categorylen = strcspn(line, ".:!\n");
/* error checking */
if ((new->catindex = catindex(line, new->categorylen)) < 0) {
/* If this message comes out, edit the categories[] array */
fprintf(stderr, "Unknown category: %.*s\n",
new->categorylen, line);
}
new->next = NULL;
new->prev = NULL;
return new;
}
/* insert grp just before cur (at tail, if cur == NULL) */
static void groupinsert(grp, cur)
register struct group *grp; /* group to insert */
register struct group *cur; /* group to be just after grp, ow/ NULL */
{
grp->next = cur;
if (cur) { /* insert just before cur */
grp->prev = cur->prev;
if (cur->prev) /* grp is after cur->prev */
cur->prev->next = grp;
else /* grp is now the first group */
groups = grp;
cur->prev = grp;
} else { /* insert at tail */
grp->prev = end_groups;
if (end_groups)
end_groups->next = grp;
else /* group list was empty */
groups = grp; /* grp is now also the first group */
end_groups = grp;
}
}
/* return logical: "Is grp a *.tail group (or the group 'tail')?" */
static int grptailcmp(grp, tail)
struct group *grp; /* the group to test */
char *tail; /* the tail to test for (e.g. "general", "misc") */
{
char *tmp = rindex(grp->group, '.'); /* pointer to tail of grp */
int taillen = strlen(tail); /* length of tail */
if (tmp) /* Is grp a *.tail group? */
return grp->group + grp->grouplen == ++tmp + taillen
&& strncmp(tmp, tail, taillen) == 0;
else /* Is grp the group "tail"? */
return grp->grouplen == taillen
&& strncmp(grp->group, tail, taillen) == 0;
}
/* like strcmp(), except for checking ordering of group heads (X.*) */
/* Does not know about categories, or *.general, *.misc, ... */
static int grpheadcmp(grp1, grp2)
struct group *grp1; /* first group to compare */
struct group *grp2; /* second group to compare */
{
char *head1tail; /* pointer to "tail" of grp1 */
char *head2tail; /* pointer to "tail" of grp2 */
#ifdef KNOW_CATEGORIES
if (grp1->catindex > grp2->catindex) {
return 1;
}
if (grp1->catindex < grp2->catindex) {
return -1;
}
/* hereafter: categories are equal */
#else /* KNOW_CATEGORIES */
/* assume categories are equal or don't matter */
#endif /* KNOW_CATEGORIES */
head1tail = rindex(grp1->group, '.');
head2tail = rindex(grp2->group, '.');
if (head1tail == head2tail) /* grp1 is grp2 */
return 0;
if (!head1tail) /* only grp1 has no head */
return -1;
if (!head2tail) /* only grp2 has no head */
return 1;
/* finally, the implied comparison */
return mystrncmp(grp1->group, head1tail - grp1->group,
grp2->group, head2tail - grp2->group);
}
/*
* high level group routines
*/
/* like strcmp(), except it compares "struct group"'s instead of strings */
static int grpcmp(grp1, grp2)
struct group *grp1; /* first group to compare */
struct group *grp2; /* second group to compare */
{
/* optimization */
/* . the categories are in a specified order */
if (grp2->catindex > grp1->catindex)
return -1; /* grp1 is before grp2 */
if (grp2->catindex < grp1->catindex)
return 1; /* grp1 is after grp2 */
/* hereafter: grp2->catindex == grp1->catindex */
#ifdef PREFIX_GOES_AFTER /* e.g. comp.sources after comp.sources.bugs */
/* . X is after all other X.* */
if (grp1->grouplen < grp2->grouplen) {
/* if grp1 is a prefix of grp2 ... */
if (grp2->group[grp1->grouplen] == '.' &&
strncmp(grp1->group, grp2->group, grp1->grouplen) == 0)
return 1;
/* . X is after all other X.* */
} else if (grp1->grouplen > grp2->grouplen) {
/* if grp2 is a prefix of grp1 ... */
if (grp1->group[grp2->grouplen] == '.' &&
strncmp(grp1->group, grp2->group, grp2->grouplen) == 0)
return -1;
}
#endif /* PREFIX_GOES_AFTER */
/* . X.general is before all other X* */
if (grptailcmp(grp1, "general")) {
if (grpheadcmp(grp1, grp2) == 0)
return -1; /* grp1 <= grp2 */
/* . X.misc is after all other X* */
} else if (grptailcmp(grp1, "misc")) {
if (grpheadcmp(grp1, grp2) == 0)
return 1; /* grp1 >= grp2 */
}
/* . X.general is before all other X* */
if (grptailcmp(grp2, "general")) {
if (grpheadcmp(grp1, grp2) == 0)
return 1; /* grp1 >= grp2 */
/* . X.misc is after all other X* */
} else if (grptailcmp(grp2, "misc")) {
if (grpheadcmp(grp1, grp2) == 0)
return -1; /* grp1 <= grp2 */
}
/* . other groups within one category are sorted as if by "sort" */
return mystrncmp(grp1->group, grp1->grouplen,
grp2->group, grp2->grouplen);
}
/* add line from .newsrc to group list at the proper place */
static void addgroup(line)
char *line; /* .newsrc line to insert */
{
struct group *new = groupalloc(line); /* "parsed" version of line */
struct group *cur; /* search loop index */
/* optimization, the list is usually "almost" already sorted */
if (!end_groups || grpcmp(new, end_groups) > 0) {
groupinsert(new, (struct group *)NULL);
return;
}
/* find insertion point */
FOREACH_GROUP(cur)
if (grpcmp(new, cur) < 0) break;
/* insert new just before cur (at tail, if cur == NULL) */
groupinsert(new, cur);
}
/* Read a .newsrc from stdin. Write a sorted .newsrc to stdout. */
main()
{
struct group *cur; /* loop index */
static char line[512]; /* hopefully more than long enough */
/* read in .newsrc, (bubble) sorting on the fly */
while (fgets(line, sizeof line, stdin))
addgroup(line);
/* write .newsrc */
FOREACH_GROUP(cur) printf("%s", cur->group);
exit(0);
}
/*LINTLIBRARY*/
/*
* Return the number of characters in the maximum leading segment
* of string which consists solely of characters NOT from charset.
*/
int
strcspn(string, charset)
char *string;
register char *charset;
{
register char *p, *q;
for(q=string; *q != '\0'; ++q) {
for(p=charset; *p != '\0' && *p != *q; ++p)
;
if(*p != '\0')
break;
}
return(q-string);
}
o / o / o / o /
--Cut-here-------X---------------X---------------X---------------X----
o \ o \ o \ o \
May the Source be with you, always...
--
Amos Shapir Net: amos@cs.huji.ac.il
Paper: The Hebrew Univ. of Jerusalem, Dept. of Comp. Science.
Givat-Ram, Jerusalem 91904, Israel
Technical Details
Message-ID:
<27po25INNcql@pita.cs.huji.ac.il>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!dearn!barilvm!kineret.huji.ac.il!bagel.cs.huji.ac.il!not-for-mailReferences:
<samik.748476449@uta.fi>Show Full Headers
Complete Article Headers
Message-ID: <27po25INNcql@pita.cs.huji.ac.il> Subject: Re: Script to sort .newsrc similar as presentation sequence? Date: Wed, 22 Sep 1993 14:39:01 +0000 From: amos@cs.huji.ac.il (Amos Shapir) 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!dearn!barilvm!kineret.huji.ac.il!bagel.cs.huji.ac.il!not-for-mail Newsgroups: news.software.nn Reply-To: amos@cs.huji.ac.il Organization: The Hebrew U. of Jerusalem, Computer Science Dep. NNTP-Posting-Host: pita.cs.huji.ac.il Lines: 394