/*
 * UPDNODES V1.3-05 - 18-Apr-91 - bmc, tmk
 *
 *			C O P Y R I G H T
 *
 *	Copyright (C) 1990, 1991 by Terence M. Kennedy,
 *	All Rights Reserved.
 *
 *	This software is furnished under a license and may be used and
 *	copied  only  in accordance with the terms of such license and
 *	with the  inclusion  of  the  above  copyright  notice.   This
 *	software  or  any  other copies thereof may not be provided or
 *	otherwise made available to any other person.  No title to and
 *	ownership of the software is hereby transferred.
 *
 *	This  program is written to support the BITNET  network and it
 *	may be used and copied freely in the network and any other non
 *      commercial network as long as this header remains intact.
 *
 *	The information in this software is subject to change  without
 *	notice  and should not be  construed as a commitment by either
 *	Terence M. Kennedy or Saint Peter's College.
 */

/*
 *	M O D I F I C A T I O N    H I S T O R Y
 *
 *	VER/ED		EDIT DATE	REASON
 *	V1.3-00		30-Jun-90	Initial coding.
 *	V1.3-01		11-Jul-90	Fix bug with handling of -l option.
 *	V1.3-02		07-Feb-91	Fix multiple bugs in handling NCS char-
 *					acters in the nodes file (note that they
 *					shouldn't be there anyway 8-).
 *	V1.3-03		11-Mar-91	Additional port: Unix, clean up code for
 *					actual distribution.
 *	V1.3-04		12-Mar-91	Add check for proper update file.
 *	V1.3-05		18-Apr-91	Handle new format nodes file (no LINKS
 *					tag, non-indented continuation lines).
 *					Changed files: IO.C, MERGE.C
 */

#define PROGRAM_NAME "UPDNODES/ASC"
#define VERSION "1.3"
#define BASELEVEL "05"

#define TRUE	1
#define FALSE	0

#define LOG_MUNDANE 2
#define LOG_UPDATE  1
#define LOG_MAJOR   0

#define TAG_STR_LEN	13
#define STD_STR_LEN	65
#define VALUE_STR_LEN	256
#define TAGS_IN_NODE	75

#ifdef	MSDOS
#define SYSERROR 1
#define SYSGOOD  0
#define DEFDELTA    "UPDATE.NOD"
#define DEFBASE     "BITEARN.NOD"
#define DEFOUT	    "BITEARN.NEW"

typedef unsigned int int16u;
#endif	/* MSDOS */

#ifdef	__pdp11c
#define SYSERROR 0
#define SYSGOOD  1
#define DEFDELTA    "UPDATE.NOD"
#define DEFBASE     "BITEARN.NOD"
#define DEFOUT	    "BITEARN.NEW"
#define	SHORT_NAMES			/* Have short external names */

typedef unsigned int int16u;
#endif	/* __pdp11c */

#ifdef	VMS
#define SYSERROR 0
#define SYSGOOD  1
#define DEFDELTA    "UPDATE.NODUPD"
#define DEFBASE     "BITEARN.NODES"
#define DEFOUT	    "BITEARN.NEW"

typedef unsigned short int16u;
#endif	/* VMS */

#ifdef	unix
#define	oldcc				/* No advanced prototypes */
/* #define WEIRD_BIT_ORDER */		/* Doesn't pack bits properly */
#define SYSERROR    1
#define SYSGOOD	    0
#define DEFDELTA    "update.nodupd"
#define DEFBASE     "bitearn.nodes"
#define DEFOUT	    "bitearn.new"

typedef unsigned short int16u;		/* select proper type for 16 bits */
#endif	/* unix */

#ifdef	oldcc
#define	void	 int
#define	const
#define	SHORT_NAMES			/* Have short external names */
#endif	/* oldcc */

#ifdef	SHORT_NAMES
#define	major_tag	    majtag
#define	major_tag_length    majlen
#define	apply_vers_delta    apvers
#define	apply_links_delta   aplink
#define	old_checksum	    ock
#define	old_checksum_verify ockver
#define	new_checksum	    nck
#define new_checksum_verify cvkver
#endif	/* SHORT_NAMES */

struct tag_entry {
    char name[TAG_STR_LEN];
    char value[VALUE_STR_LEN];
};

typedef struct tag_entry TAG_ENTRY;

struct node_entry {
    char action[4];
    char name[STD_STR_LEN];
    int16u checksum;
    char have_checksum;
    int num_tags;
    TAG_ENTRY tags[TAGS_IN_NODE];
};

typedef struct node_entry NODE_ENTRY;

extern int verbose;
extern int linemax;

extern FILE * delta;
extern FILE * base;
extern FILE * out;

extern char deltaline[VALUE_STR_LEN];
extern char baseline[VALUE_STR_LEN];

extern int16u old_checksum;
extern int16u new_checksum;

extern char major_tag[STD_STR_LEN];
extern int major_tag_length;

extern int err_count;

#ifndef	oldcc			/* 2.11 BSD doesn't have proper prototypes */
/* From CHKSUM.C */
void validate_checksum(NODE_ENTRY * maj, char * desc, int16u * tot);
int estrcmp(const char * s1, const char * s2);

/* From CHKSUM.C or CHECKSUM.ASM */
void atoe(unsigned char * s);
int16u chksum(char * in_string, int16u oldsum);

/* From IO.C */
void write_node(NODE_ENTRY * maj);
void read_delta_line(void);
void read_base_line(void);
int get_delta_tag(NODE_ENTRY * major);
int get_base_tag(NODE_ENTRY * major);
char * trim(char * s);

/* From LOG.C */
void log_line(int level, char * fmt, ...);
void error(char * fmt, ...);

/* From MERGE.C */
void merge_net(void);

/* From PARSE.C */
void parse_line(NODE_ENTRY * maj, char * s);
void min_merge(NODE_ENTRY * maj, char * tag, char * value);
TAG_ENTRY * min_find(NODE_ENTRY * maj, char * tag);
void apply_vers_delta(NODE_ENTRY * vers, NODE_ENTRY * upd);
void apply_links_delta(NODE_ENTRY * links, NODE_ENTRY * upd);
void min_incorporate(NODE_ENTRY * orig, NODE_ENTRY * upd);
#else
/* From CHKSUM.C */
void validate_checksum();
int estrcmp();

/* From CHKSUM.C or CHECKSUM.ASM */
void atoe();
int16u chksum();

/* From IO.C */
void write_node();
void read_delta_line();
void read_base_line();
int get_delta_tag();
int get_base_tag();
char * trim();

/* From LOG.C */
void log_line();
void error();

/* From MERGE.C */
void merge_net();

/* From PARSE.C */
void parse_line();
void min_merge();
TAG_ENTRY * min_find();
void apply_vers_delta();
void apply_links_delta();
void min_incorporate();
#endif	/* oldcc */
