Received: from MIT.MIT.EDU by po6.MIT.EDU (5.61/4.7) id AA18122; Fri, 9 Oct 92 15:44:45 EDT
Received: from NOC2.DCCS.UPENN.EDU by MIT.EDU with SMTP
	id AA12300; Fri, 9 Oct 92 15:44:42 EDT
Received: from NISC2.UPENN.EDU by noc2.dccs.upenn.edu
	id AA25487; Fri, 9 Oct 92 15:44:38 -0400
Return-Path: <murphy@nisc2.upenn.edu>
Received: by nisc2.upenn.edu
	id AA17010; Fri, 9 Oct 92 15:44:37 -0400
Date: Fri, 9 Oct 92 15:44:37 -0400
From: murphy@nisc2.upenn.edu (L. Murphy)
Posted-Date: Fri, 9 Oct 92 15:44:37 -0400
Message-Id: <9210091944.AA17010@nisc2.upenn.edu>
To: thorne@MIT.EDU, wade@MIT.EDU
Subject: source code to testweb program


This is a program I wrote which tests the integrity of the pips.web
data structure.  It needs to be linked with web.o, util.o, and glob.o
from your source tree.  I hope you find it useful.


#include <errno.h>
#include <stdio.h>
#include "pdb.h"
#include "web.h"

extern long *make_link_block(char links[], short *linkcnt);
extern long _allocated;
static NODE *firstnode = 0, *lastnode = 0;
static char msg[100];

dofree (void *ptr, int sz)
{
  _allocated -= sz;
  free (ptr);
}

static prmsg (FILE *fp, NODE *nd, char *msg, int resolved)
{
  char nodebuf[NLINE_MAXLEN];
  char linkbuf[100];

  sprintf (nodebuf, "%d:%s:%s:%s:%s",
	   n_id(nd),
	   n_topic(nd),
	   n_title(nd),
	   n_source(nd),
	   n_file(nd));
  fprintf (fp, "%s\n%s\n\n", nodebuf, msg);
  if (fp != stderr)
    fflush(fp);
}

static NODE *find_webnode_addr (long nodeid)
{
  NODE *n;
  
  for (n = firstnode; n; n = n_next(n))
    if (n_id(n) == nodeid)
      return n;
  return (NODE *) NULL;
}



static int parsenodeline (char *line, NODE *nd, char **plinks,char **clinks )
{
  char           *topic, *title, *source, *locker, *file, *cp;
  int             datalen;
  char           *found;
#define finddelim(ptr1,ptr2)  {if ((ptr2 = index (ptr1, DLM))) *ptr2 = 0; else return 0;}

  if (!isdigit(*line))
    return 0;

  cp = line;

  finddelim (cp, found);
  nd->nd_id = atol(cp);
  cp = found+1;

  finddelim (cp, found);
  nd->nd_flags = (short) atoi (cp);
  cp = found+1;

  finddelim (cp, found);
  nd->nd_date = (short) atoi(cp);
  cp = found+1;
  
  finddelim (cp, found);
  topic = cp;
  cp = found+1;

  finddelim (cp, found);
  title = cp;
  cp = found+1;

  finddelim (cp, found);
  source = cp;
  cp = found+1;

  finddelim (cp, found);
  locker = cp;
  cp = found+1;

  finddelim (cp, found);
  file = cp;
  cp = found+1;

  nd->nd_off[0] = (u_char) (title - topic);
  nd->nd_off[1] = (u_char) (source - topic);
  nd->nd_off[2] = (u_char) (locker - topic);
  nd->nd_off[3] = (u_char) (file - topic);

  finddelim (cp, found);
  *plinks = cp;
  *clinks = found+1;

  datalen = *plinks - topic + 1;
  if (**plinks == NULL) 
    datalen++; 

  nd->nd_data = (char *) domalloc(datalen);
  memcpy(nd->nd_data, topic, datalen);

  return 1;
}

static NODE *addnewnode(char *line, int lineno)
{
  NODE *nd;
  char *parentlinks, *childlinks;

  nd = (NODE *) domalloc (sizeof(NODE));
  if (!nd)
    return (NODE *) 0;
  bzero(nd, sizeof(NODE));

  if (!parsenodeline(line, nd, &parentlinks, &childlinks)) {
    fprintf(stdout, "Line %d: unparseable\n", lineno);
    fflush(stdout);
    dofree (nd, sizeof(NODE));
    return (NODE *) 0;
  }
  else {
    if (find_webnode_addr(nd->nd_id) != (NODE *) NULL) {
      sprintf (msg, "line %d: duplicate nodeid (ignored)", lineno);
      prmsg (stdout, nd, msg, 0);
      return (NODE *) 0;
    }
    
    nd->nd_numchild = 0;
    nd->nd_numparent = 0;
    
    nd->nd_parent_ids = make_link_block (parentlinks, &(nd->nd_numparent));
    nd->nd_child_ids =  make_link_block (childlinks, &(nd->nd_numchild));
      
    if (!firstnode)
      firstnode = nd;
    else
      lastnode->nd_next = nd;
    nd->nd_next = NULL;
    lastnode = nd;
    
    return(lastnode);
  }
}


static void resolvelinklist(NODE *n, short linkcnt, long *nid_block)
{
  int i;
  long nid;
  
  for (i=0; i < linkcnt; i++) {
    nid = nid_block[i];
    if ((nid_block[i] = (long) find_webnode_addr (nid)) == NULL) {
      sprintf (msg, "nodeid %d does not exist", nid);
      prmsg (stdout, n, msg, 0);
    }
  }
}


static void fixpointers ()
{
  NODE *nd;

  for (nd=firstnode; nd != (NODE *) NULL; nd=n_next(nd)) {
    resolvelinklist (nd, nd->nd_numparent, nd->nd_parent_ids);
    resolvelinklist (nd, nd->nd_numchild, nd->nd_child_ids);
  }
}

static void checknodeslist(NODE *nd, char *who)
{
  int doparents, listlen, secondlistlen;
  long *list, *secondlist;
  int i, j;
  NODE *temp;

  doparents = strcasecmp(who, "parent") == 0;

  listlen = doparents ? nd->nd_numparent : nd->nd_numchild;
  list    = doparents ? nd->nd_parent_ids : nd->nd_child_ids;

  for (i=0; i < listlen; i++) {
    if (list[i] != NULL) {
      temp = (NODE *) list[i];
      secondlistlen = doparents ? temp->nd_numchild : temp->nd_numparent;
      secondlist    = doparents ? temp->nd_child_ids : temp->nd_parent_ids;

      for (j=0; j < secondlistlen; ) {
	if (secondlist[j] == (long) nd)
	  break;
	else
	  j++;
      }
      if (j >= secondlistlen) {
	sprintf (msg, "%s %d doesn't point back to me", who, temp->nd_id);
	prmsg (stdout, nd, msg, 1);
      }
    }
  }
}


static void printerrs()
{
  NODE *nd;
  int orphans = 0;
  int i;

  for (nd=firstnode; nd != (NODE *) NULL; nd=nd->nd_next) {
    if (nd != firstnode) {
      for (i=0; i < nd->nd_numparent; i++)
	if (nd->nd_parent_ids[i] != NULL) break;
      if (i >= nd->nd_numparent) {
	orphans++;
	fprintf (stderr, "Orphan: %d:%s:%s:%s:%s\n\n", n_id(nd), n_topic(nd),
		 n_title(nd), n_source(nd), n_file(nd));
      }
    }
    checknodeslist(nd, "parent");
    checknodeslist(nd, "child");
  }
  fprintf(stderr, "\nORPHANS: %d\n", orphans);
}

main (argc, argv)
     int argc;
     char *argv[];
{
  char *webfile = "pips.web";
  int numlines, numnodes;
  char line[NLINE_MAXLEN];
  FILE *webfp;

  if (argc > 1)
    webfile = argv[1];
  
  webfp = fopen (webfile, "r");
  if (webfp == NULL) {
    perror (webfile);
    exit (-1);
  }

  while (fgets (line, NLINE_MAXLEN, webfp) != NULL) {
    numlines++;
    if (*line == '#')
      continue;
    line[strlen(line)-1] = 0;  /* remove lastchar since it's probably \n */
    if (addnewnode(line, numlines) == (NODE *) NULL) {
    }
    else {
      numnodes++; }
  }
  fclose (webfp);
  if (numnodes != numlines) {
    fprintf(stdout, "Number of lines not turned into nodes: %d\n\n",
	    numlines-numnodes);
    fflush(stdout);
  }
  fprintf (stderr, "Read %d lines, parsed %d nodes, _allocated = %ld\n\n",
	   numlines, numnodes,_allocated);

  fixpointers();
  printerrs();
}


