[net.sources] making pathalias easier

chuqui@nsc.UUCP (Zonker T. Chuqui) (10/15/84)

With the posting of the pathalias sources, I've hacked together some stuff
that makes using pathalias a little bit easier. There are three tools in
this package: 'uupath' - which lets a user find paths in the database
(originally from Bill Sebok, modified for the new pathalias format);
'usemap' - a program designed to run from your 'sys' file and automatically
unpack the usenet files posted to net.news.map; and 'mkalpath' - a shell
scripts and two awk scripts designed to make building pathalias databases a
bit easier. 

Please note that people running the mods to 'vnews' and 'readnews' that
there is a slight format change between the old version of pathalias that
they were designed for and the version recently posted. In the new version
the key is stored as '<sitename>!' instead of '<sitename>'. This actually
makes sense because it simplifies walking through paths looking for things
to optimize. Because I was in a hurry, I simply hacked mine to
strcat(sitename,"!"); before the fetch-- ugly, but quick and it's working
again. I'll look into really fixing that code later.

chuq
--- path.sh ---

#! /bin/sh
# The rest of this file is a shell script which will extract:
# README mkalpath usemap.c usenet.awk uucp.awk uupath.c uupath.l
echo x - README
cat >README <<'!!ChuquiCo!!Software!!'

This package contains three separate sets of programs. The first is the
source and man-page for 'uupath' which will print out paths from a 
database created by the pathalias program recently posted. This was
originally written by Bill Sebok and converted to understand the current
pathalias format. 

The second program is usemap.c. This can be used to automatically unpack
the usenet maps posted on a regular basis to net.news.map. Compile the
program (making sure the proper place for the maps is defined in) and
place it in your news directory (usually /usr/lib/news). Then, if you add
a line to your sys file like:

	map:net.news.map:B:/usr/lib/news/usemap

these maps will automatically be unpacked and updated for you when they 
arrive. 

The third program is a shell script and two awk scripts. These can be used
to regenerate the pathalias data base. They are currently set up to work
specifically on the file structure I put together (described below as an
example) but they should be easy to modify. mkalpath is the script, the
two .awk scripts are called by it. 

What I've done is create two files in /usr/spool/news-- Maps and Paths. Maps
is where the unpacked usenet maps live. Paths is where the pathalias data,
including the postings from the uucp project, live. Since I have 4.2, I've
set up the home for my pathalias files to be /usr/lib/uucp/alpath (and
associated dbmfiles) which are really symbolic links to the files in ./
Paths.

The Paths directory has a few sub-directories. 'localsites' is where I keep
the pathalias information for the sites I talk to directly. I also keep a
file in there called Forces that I use to coerce pathalias to use paths I
know to be preferable. 'netsites' is the data published by the uucp group
unpacked from the archives. Because of the number of files, I broke it up
from the distributed flat file into a series of sub directories based on
the first letter of the site name (only for [a-z]-- everything else goes
into misc). So, nsc would be in netsites/n/nsc. The information for my
site is kept in the file nsc since I don't want it overwritten by the uucp
stuff (which I also plan on automating the unpacking of when it starts
coming out regularly). The directory structure looks something like this:

  72 -rw-rw-r--  1 chuqui      69489 Oct 13 21:10 alpath
   4 -rw-rw-r--  1 chuqui       4096 Oct 13 21:10 alpath.dir
 132 -rw-rw-r--  1 chuqui     131072 Oct 13 21:10 alpath.pag
   1 drwxr-xr-x  2 chuqui        512 Oct 14 14:17 localsites/
   1 drwxrwxr-x 29 chuqui        512 Oct 13 15:00 netsites/
   1 -rw-r--r--  1 chuqui        630 Oct 14 14:27 nsc
   1 -rw-rw-r--  1 chuqui        108 Oct 13 21:11 results
   1 -rw-rw-r--  1 chuqui        845 Oct 13 21:10 unreach
   1 -rw-r--r--  1 chuqui        524 Oct 13 20:04 usenet.awk
  60 -rw-rw-r--  1 chuqui      57143 Oct 13 21:05 usenetsites
   1 -rw-r--r--  1 chuqui        643 Oct 13 20:04 uucp.awk

with netsites having subdirectories ./misc and ./[a-z].

mkalpath uses the scripts usenet.awk and uucp.awk to create the file
usenetsites from the information in Maps. It then compiles nsc,
localsites/*, netsites/*/* and usenetsites into a rather large file and
feeds that to pathalias. pathalias creates the database in alpath and
alpath.{dir,pag}. unreach is the file where pathalias error messages are
sent, and unreach is a file which will tell you how many sites can be
found a certain # of hops away.

mkalpath and the two awk scripts were originally posted to the net long
enough ago that I no longer have a pointer to the author. I've hacked away
at them to do what I want them to do since then (the program is his/hers,
the bugs are mine). All of this can be improved significantly-- it works,
but it can work better. I've found this stuff to make working with
pathalias MUCH easier which is why I'm posting it now. Any improvements or
suggestions you might have are more than welcome, and as it evolves I'll
post changes.

chuq (nsc!chuqui)!!ChuquiCo!!Software!!
echo x - mkalpath
cat >mkalpath <<'!!ChuquiCo!!Software!!'
#! /bin/sh 
# run this shell script in this directory to update your mail database
SITE=nsc
DIR=/usr/spool/news/Paths
MAP=/usr/spool/news/Maps
T1=/tmp/mp$$
T=/tmp/pa$$
trap "rm -f $T $T1; exit" 0 1 2 3 15
cd ${DIR}
# generate the data from the usenet maps
( cd ${MAP} ; cat *.* > $T1 )
echo -n "# Usenet map extracted on " > usenetsites
date >> usenetsites
awk -f usenet.awk <$T1 >> usenetsites
echo -n "# Uucp map extracted from usenet map on " >> usenetsites
date >> usenetsites
awk -f uucp.awk <$T1 >> usenetsites
# compile the data from local sources and the uucp maps
cat ${SITE} localsites/* > $T
cat netsites/Misc/* netsites/[a-l]/* >> $T
cat netsites/[m-z]/* >> $T
# save the old stuff (just in case...)
cp alpath OLDalpath
cp alpath.dir OLDalpath.dir
cp alpath.pag OLDalpath.pag
cat >alpath </dev/null
cat >alpath.dir </dev/null
cat >alpath.pag  </dev/null
# generate the database
pathalias -p -b -c -l ${SITE} -P alpath $T >alpath 2>unreach
( echo "	uucp"; echo "#sites	hops" ;\
	sed 's/[^!]//g' < alpath | sort | uniq -c ) >results
!!ChuquiCo!!Software!!
echo x - usemap.c
cat >usemap.c <<'!!ChuquiCo!!Software!!'
# ifndef NOSCCS
static char *sccsid = "@(#)usemap.c	1.2	10/13/84";
# endif

/*
 * uumap.c - unpack USENET maps into a useful form
 * chuq von rospach (nsc!chuqui)
 *
 * This program will go to some defined location, read stdin to eat the
 * news header and send the rest off to a shell for unpacking. It is
 * used to automatically update the usenet maps published in net.news.map.
 * 
 * to use, install uumap in /usr/lib/news and install a sys line entry
 * that says 'map:net.news.map:B:/usr/lib/news/uumap'
 */

#define MAPDIR "/usr/spool/news/Maps"
#define ERRLOG "/usr/lib/news/errlog"

#include <stdio.h>
main()
{
    char    buf[BUFSIZ];
    FILE * pfp, *popen ();

	/*
	 * set up an environment that allows the shell to operate
	 * in the background. Error messages end up in the netnews
	 * errlog file, and stdout gets eaten (this has to be done because
	 * the shar archive uses 'echo')
	 */
	fclose(stderr);
    if (fopen (ERRLOG, "a") == NULL)
		exit(-99);
	fclose(stdout);
    if (fopen ("/dev/null", "w") == NULL)
		exit(-999);

	fprintf(stderr,"usemap: unpacking\n");
	fflush(stderr);

    chdir (MAPDIR);
/* eat header */
    while (gets (buf, BUFSIZ) != NULL) {
		if (!strlen (buf))
		    break;
    }

/* feed rest to shell */
    pfp = popen ("/bin/sh", "w");
    while (fgets (buf, BUFSIZ, stdin) != NULL) {
		fputs (buf, pfp);
	}
    pclose (pfp);
	fflush(stderr); /* make sure it all gets out */
}
!!ChuquiCo!!Software!!
echo x - usenet.awk
cat >usenet.awk <<'!!ChuquiCo!!Software!!'
BEGIN	{
		site = "";
		contline = 0;
	}
/^$/	{ site = ""; }
/^Name:/ {
		if (NF > 1) {
			site = $2;
			if (site ~ /,$/)
				site = substr(site, 1, length(site)-1);
		}
		next;
	}
/^News:/ {
		if (NF > 1 && site != "") {
			printf "%s\t%s", site, $2;
			for (i = 3; i<=NF; i++)
				printf ", %s", $(i);
			printf "\n";
			contline = 1;
			next;
		}
	}
/^	/ {
		if (conline != 0 && site != "") {
			printf "\t%s", $1;
			for (i = 2; i<=NF; i++)
				printf ", %s", $(i);
			printf "\n";
			next;
		}
	}
/^*/	{ contline = 0; }
!!ChuquiCo!!Software!!
echo x - uucp.awk
cat >uucp.awk <<'!!ChuquiCo!!Software!!'
BEGIN	{
		site = "";
		contline = 0;
	}
/^$/	{ site = ""; }
/^Name:/ {
		if (NF > 1) {
			site = $2;
			if (site ~ /,$/) {
				site = substr(site, 1, length(site)-1);
				printf "%s\t= %s", site, $3;
				for (i = 4; i<=NF; i++)
					printf ", %s", $(i);
				printf "\n";
			}
		}
		next;
	}
/^Mail:/ {
		if (NF > 1 && site != "") {
			printf "%s\t%s", site, $2;
			for (i = 3; i<=NF; i++)
				printf ", %s", $(i);
			contline = 1;
			next;
		}
	}
/^	/ {	if(site != "" && contline != 0) {
			printf ",\n\t%s", $1;
			for (i = 2; i<=NF; i++)
				printf ", %s", $(i);
			next;
		}
	}
/^./	{
		if(contline != 0)
			printf "\n";
		contline = 0;
	}
!!ChuquiCo!!Software!!
echo x - uupath.c
cat >uupath.c <<'!!ChuquiCo!!Software!!'
/* * * * uupath - look up path to computer in database * * * W.Sebok 11/4/83 */
/* Compile with    cc -O -s -o uupath uupath.c -ldbm  */
#include <stdio.h>
#ifdef NULL
#undef NULL
#endif
#include <dbm.h>

#define ARCHIVE	"/usr/lib/uucp/alpath"
#define USAGE	"Usage: uupath site [ dbase ]\n"

main(argc,argv)
	int argc; char *argv[];
{
	char *fil;
	int ret;
	datum key, result;
	char buf[BUFSIZ];

	if (argc<2 || argc>3) {
	 	fprintf(stderr,USAGE);
		exit(1);
	}

	fil =  (argc == 3) ? argv[2] : ARCHIVE;
	strcpy(buf,argv[1]);
	strcat(buf,"!");
	key.dptr = buf;
	key.dsize = strlen(key.dptr) + 1 ;
	ret = dbminit(fil);
	if (ret != 0) {
		exit(1);
	}
	result = fetch(key);
	if (result.dptr != NULL) {
		if (strcmp("!%s",&result.dptr[result.dsize-4])==0) {
			result.dptr[result.dsize-4] = '\0';
		}
		printf("%s\n",result.dptr);
	} else {
		fprintf(stderr,"%s not found\n",argv[1]);
		exit(1);
	}
	exit(0);
}
!!ChuquiCo!!Software!!
echo x - uupath.l
cat >uupath.l <<'!!ChuquiCo!!Software!!'
.TH UUPATH 1l local
.SH NAME
uupath \- look up the network path to a computer in the system database.
.SH SYNOPSIS
.B uupath
site [ dbase ]
.SH DESCRIPTION
.PP
.I Uupath
takes the site-name of a computer, looks it up in the system wide data base, and
prints out the network path to that site.  This is the same data base as is
used by 
.I mail
to find network paths to other computers.
.PP
One can specify an alternate data base with the optional 2nd parameter "dbase".
This data base must be a pair of files created by the
.I dbm(3)
routines and have names of the form dbase.dir and dbase.pag.
.SH FILES
/usr/lib/uucp/alpath.dir,  /usr/lib/uucp/alpath.pag
.SH SEE ALSO
pathalias(1l), dbm(3)
.SH AUTHOR
William L. Sebok (Nov. 4, 1983)
!!ChuquiCo!!Software!!
-- 
From the Department of Bistromatics:                   Chuq Von Rospach
{cbosgd,decwrl,fortune,hplabs,ihnp4,seismo}!nsc!chuqui  nsc!chuqui@decwrl.ARPA

How about 'reason for living?'