/*
 *
 *	Copyright (C) 1989, 1993 by the Massachusetts Institute of Technology
 *    	Developed by the MIT Student Information Processing Board (SIPB).
 *    	For copying information, see the file mit-copyright.h in this release.
 *
 */

/*
 * edsc.h --- include file for the emacs discuss back-end program
 */

#include <discuss/discuss.h>
#include <com_err.h>

extern int errno;

extern char *user_id;
extern tfile stdout_tf;
extern int cache_activated, cache_working;
extern int use_vectors;

/* selection flags */
#define flag_AREF		8

/* filtering flags */
#define filter_INCLUDE_DELETED 1
#define filter_ONLY_DELETED    2
#define filter_ONLY_INITIAL    4
#define filter_ONLY_TERMINAL   16
#define filter_FLAG_SET        32
#define filter_FLAG_RESET      64

/*
 * Here is the cache information.....
 */

/*
 * The caching is only enabled if EDSC_CACHE is defined....
 */
#define EDSC_CACHE

#ifdef EDSC_CACHE
#define CACHE_DIR	"/tmp"
#define CACHE_FN_SIZE	80
#define CACHE_STRATEGY_SIZE	256
#define CACHE_DEFAULT_SIZE	50

#define CACHE_FLG_INFO	1
#define CACHE_FLG_TEXT	2

struct cache_meeting {
	int	ref_count;
	name_blk	nb;
	mtg_info	m_info;
	int		meeting_code; /* Error code when fetching */
				      /* meeting info */
	struct cache_meeting	*next;
	struct cache_meeting	*prev;
};

struct cache_info {
	trn_nums trn_num;
	struct cache_meeting	*meeting;
	int	flags;
	char	filename[CACHE_FN_SIZE];
	trn_info3 t_info;
	int	info_code;	/* Error code when fetching info */
	int	text_code;	/* Error code when fetch the text */
	struct cache_info      	*next;	/* Used to keep LRU list */
					/* Also used for empty list */
	struct cache_info	*prev;
};

/*
 * The cache work instructions are divided into two fields:
 *
 * The upper 4 bits define the opcode, the lower 4 bites define the
 * direction, if any.
 *
 * OPCODES:
 * 	STOP --- Stop doing any cache work.
 * 	GET_CUR --- Get the current transaction
 * 	FETCH_CUR --- Fetch the article one away in the indicated direction
 * 			from the current article.
 * 	SET_PTR --- Set the pointer to the current article
 * 	MFETCH_PTR --- Move the pointer one in the indicated direction and
 * 			fetch that article
 */

typedef char	cache_dir;

#define CACHE_OP_MASK		0xF0
#define CACHE_OP_STOP		(0 << 4)
#define CACHE_OP_GET_CUR	(1 << 4)
#define CACHE_OP_FETCH_CUR	(2 << 4)
#define CACHE_OP_SET_PTR	(3 << 4)
#define CACHE_OP_MFETCH_PTR	(4 << 4)

#define CACHE_DIR_MASK		0x0F
#define CACHE_DIR_CURRENT	0
#define CACHE_DIR_NEXT		1
#define CACHE_DIR_PREV		2
#define CACHE_DIR_NREF		3
#define CACHE_DIR_PREF		4
#define CACHE_DIR_FREF		5
#define CACHE_DIR_LREF		6

extern int			cache_pc;
extern struct cache_info	*cache_current;
extern cache_dir		cache_current_direction;
	
#endif /* EDSC_CACHE */

#ifdef EDSC_CACHE
#define PROTOCOL_VERSION "25"
#else
#define PROTOCOL_VERSION "24"
#endif

extern const char edsc_version_string[];

#define edsc_protocol_version	PROTOCOL_VERSION

/* do_cache.c */
void do_cache_work (void);
int move_trn (struct cache_info *, cache_dir);
int do_scd (char *);
int do_it (char *);
int cache_it (name_blk *, trn_nums);
int do_itn (char *);
int cache_itn (name_blk *, trn_nums);
int do_im (char *);
int cache_transaction (name_blk *, trn_nums, struct cache_info **);
void cache_get_transaction_text (struct cache_info *);
void cache_init (int);
void cache_flush (void);
int cache_shutdown (void);
void free_cache_entry (struct cache_info *);
struct cache_info *cache_empty (void);
struct cache_info *cache_search (name_blk *, trn_nums);
struct cache_meeting *get_cache_meeting (name_blk *);
struct cache_meeting *search_cache_meetings (name_blk *);
void free_cache_meeting (struct cache_meeting *);
int poll_input (int);

/* do_misc.c */
int do_gpv (char *);
int do_set (char *);
int do_pacl (char *);
char *fix_principal (char *);
int do_dacl (char *);
int do_sacl (char *);
int do_ls (char *);

/* do_mtg.c */
int do_gmi (char *);
int handle_moved_meeting (name_blk *, mtg_info *, int *, char *, int);
int do_gml (char *);
int do_ss (char *);
int do_gmn (char *);
int do_am (char *);
int do_dm (char *);
int hostpath_to_nb (const char *, char *, name_blk *, int *);

/* do_trn.c */
int do_gti (char *);
int do_gt (char *);
int do_gtf (char *);
int do_gtfc (char *);
int do_grt (char *);
int do_at (char *);
int do_nut (char *);
int do_grtn (char *);
int do_sfl (char *);
int do_rt (char *);
int do_dt (char *);

/* edsc.c */
void crash_handler (int);
int main (int, char **);
int log_warn (int, char *);
int bit_bucket (int, char *);
int do_quit (char *);

#include "parse.h"
#include "stime.h"
