/*--------------------------------------------------------------------------*/
/*																			*/
/*																			*/
/*		------------		 Bit-Bucket Software, Co.						*/
/*		\ 10001101 /		 Writers and Distributors of					*/
/*		 \ 011110 / 		 Freely Available<tm> Software. 				*/
/*		  \ 1011 /															*/
/*		   ------															*/
/*																			*/
/*	(C) Copyright 1987-90, Bit Bucket Software Co., a Delaware Corporation. */
/*																			*/
/*																			*/
/*			This module was originally written by Vince Perriello			*/
/*																			*/
/*																			*/
/*					 BinkleyTerm Variable Initialization					*/
/*																			*/
/*																			*/
/*	  For complete	details  of the licensing restrictions, please refer	*/
/*	  to the License  agreement,  which  is published in its entirety in	*/
/*	  the MAKEFILE and BT.C, and also contained in the file LICENSE.240.	*/
/*																			*/
/*	  USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE	*/
/*	  BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF	*/
/*	  THIS	AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,	OR IF YOU DO	*/
/*	  NOT HAVE THESE FILES,  YOU  SHOULD  IMMEDIATELY CONTACT BIT BUCKET	*/
/*	  SOFTWARE CO.	AT ONE OF THE  ADDRESSES  LISTED BELOW.  IN NO EVENT	*/
/*	  SHOULD YOU  PROCEED TO USE THIS FILE	WITHOUT HAVING	ACCEPTED THE	*/
/*	  TERMS  OF  THE  BINKLEYTERM  LICENSING  AGREEMENT,  OR  SUCH OTHER	*/
/*	  AGREEMENT AS YOU ARE ABLE TO REACH WITH BIT BUCKET SOFTWARE, CO.		*/
/*																			*/
/*																			*/
/* You can contact Bit Bucket Software Co. at any one of the following		*/
/* addresses:																*/
/*																			*/
/* Bit Bucket Software Co.		  FidoNet  1:104/501, 1:132/491, 1:141/491	*/
/* P.O. Box 460398				  AlterNet 7:491/0							*/
/* Aurora, CO 80046 			  BBS-Net  86:2030/1						*/
/*								  Internet f491.n132.z1.fidonet.org 		*/
/*																			*/
/* Please feel free to contact us at any time to share your comments about	*/
/* our software and/or licensing policies.									*/
/*																			*/
/*--------------------------------------------------------------------------*/

#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>

#ifdef __TURBOC__
#include <mem.h>
#else
#include <memory.h>
#endif

#include "com.h"
#include "xfer.h"
#include "zmodem.h"
#include "keybd.h"
#include "sbuf.h"
#include "sched.h"
#include "externs.h"
#include "prototyp.h"

static void compile_externs (void);

/**
 ** b_initvars -- called before parse_config. Sets defaults that we want
 ** to have set FIRST.
 **/


void b_initvars ()
{
   int k;

	putenv ("TZ=GMT0");
   tzset ();

   DEFAULT.rq_OKFile = DEFAULT.rq_FILES = DEFAULT.rq_About = DEFAULT.rq_Template = DEFAULT.sc_Inbound = NULL;
   DEFAULT.rq_Limit = 30;						 /* This seems a good default */
   DEFAULT.byte_Limit = 500000L;

   KNOWN.rq_OKFile = KNOWN.rq_FILES = KNOWN.rq_About = KNOWN.rq_Template = KNOWN.sc_Inbound = NULL;
   KNOWN.rq_Limit = -1;
   KNOWN.byte_Limit = -1L;

   PROT.rq_OKFile = PROT.rq_FILES = PROT.rq_About = PROT.rq_Template = PROT.sc_Inbound = NULL;
   PROT.rq_Limit = -1;
   PROT.byte_Limit = -1L;

   for (k = 0; k < 10; k++) 					 /* Zero the phone scan list  */
	  {
	  scan_list[k] = NULL;
	  }

   for (k = 0; k < MAX_EXTERN; k++) 			 /* And the external protocols*/
	  {
	  protocols[k] = NULL;
	  }

   for (k = 1; k < ALIAS_CNT; k++)				 /* And the alias list		  */
	  {
	  alias[k].Zone = alias[k].Net = alias[k].Node = alias[k].Point = 0;
	  alias[k].Domain = NULL;
	  }

   alias[0].Zone = 1;							 /* Make sure we have a zone  */
   alias[0].Net = alias[0].Node = (unsigned) -1;	  /* Default Fidonet address   */
   alias[0].Point = 0;
   alias[0].Domain = NULL;

   b_init ();

   baud = 2;
   cur_baud = btypes[baud].rate_value;
   command_line_un = 0;
}

/**
 ** b_defaultvars -- called after all parse_config passes complete.
 ** sets anything not handled by parse_config to default if we know it.
 **/


void b_defaultvars ()
{
   int i;
   char *p;

   if (!fullscreen)
	  do_screen_blank = 0;

   if (modem_init == NULL)
	  modem_init = ctl_string ("|AT|");
   if (modem_busy == NULL)
	  modem_busy = ctl_string ("|AT|");

   if (net_info == NULL)
	  net_info = ctl_string (".\\");


   /* Set up "boss" and "point" addresses correctly if we can */

   if (boss_addr.Zone == 0)
	  boss_addr.Zone = alias[0].Zone;

   if (!boss_addr.Net)
	  {
	  boss_addr.Net  = alias[0].Net;
	  boss_addr.Node = alias[0].Node;
	  boss_addr.Domain = alias[0].Domain;
	  }

   my_addr = alias[0];
   if (alias[0].Point)
	  {
	  alias[0].Net	 = (unsigned) pvtnet;
	  alias[0].Node  = alias[0].Point;
	  alias[0].Point = 0;
	  my_addr.Net = boss_addr.Net;
	  my_addr.Node = boss_addr.Node;
	  my_addr.Domain = boss_addr.Domain;
	  }

   /* If we have the minimum information to do netmail, set the flag */

   if ((alias[0].Zone	   != 0)
   &&  (alias[0].Net	   != 0)
   &&  (system_name 	   != NULL)
   &&  (sysop			   != NULL)
   &&  (hold_area		   != NULL)
   &&  (DEFAULT.sc_Inbound != NULL))
	  net_params = 1;

   /* Make the "higher class" requests at least as well off as the
	  "lowest class"... */

   if (KNOWN.byte_Limit == -1L)
	  KNOWN.byte_Limit = DEFAULT.byte_Limit;
   if (KNOWN.rq_Limit == -1)
	  KNOWN.rq_Limit = DEFAULT.rq_Limit;
   if (KNOWN.rq_FILES == NULL)
	  KNOWN.rq_FILES = DEFAULT.rq_FILES;
   if (KNOWN.rq_OKFile == NULL)
	  KNOWN.rq_OKFile = DEFAULT.rq_OKFile;
   if (KNOWN.rq_About == NULL)
	  KNOWN.rq_About = DEFAULT.rq_About;
   if (KNOWN.rq_Template == NULL)
	  KNOWN.rq_Template = DEFAULT.rq_Template;
   if (KNOWN.sc_Inbound == NULL)
	  KNOWN.sc_Inbound = DEFAULT.sc_Inbound;

   if (PROT.byte_Limit == -1L)
	  PROT.byte_Limit = KNOWN.byte_Limit;
   if (PROT.rq_Limit == -1)
	  PROT.rq_Limit = KNOWN.rq_Limit;
   if (PROT.rq_FILES == NULL)
	  PROT.rq_FILES = KNOWN.rq_FILES;
   if (PROT.rq_OKFile == NULL)
	  PROT.rq_OKFile = KNOWN.rq_OKFile;
   if (PROT.rq_About == NULL)
	  PROT.rq_About = KNOWN.rq_About;
   if (PROT.rq_Template == NULL)
	  PROT.rq_Template = KNOWN.rq_Template;
   if (PROT.sc_Inbound == NULL)
	  PROT.sc_Inbound = KNOWN.sc_Inbound;

   if (!num_events)
	  e_ptrs[0] = calloc (sizeof (EVENT), 1);

   if (extern_index)
	  compile_externs ();						 /* generate extern_protocols */

   if (!colors.calling && colors.hold)
	  colors.calling = ((colors.hold & 0x70) >> 4) | ((colors.hold & 0x7) << 4) | (colors.hold & 0x8);

   if ((!colors.popup) && colors.call)
	  colors.popup = colors.call;

   first_block = 0;

   /* Make our domain first in the list */
   if (my_addr.Domain != NULL)
	  {
	  for (i = 0; domain_name[i] != NULL; i++)
		 {
		 if (domain_name[i] == my_addr.Domain)
			break;
		 }

	  if ((i > 0) && (domain_name[i] == my_addr.Domain))
		 {
		 p = domain_name[0];
		 domain_name[0] = domain_name[i];
		 domain_name[i] = p;
		 p = domain_nodelist[0];
		 domain_nodelist[0] = domain_nodelist[i];
		 domain_nodelist[i] = p;
		 p = domain_abbrev[0];
		 domain_abbrev[0] = domain_abbrev[i];
		 domain_abbrev[i] = p;
		 }
	  }

   set_prior(4);									/* Always High */

#ifdef Snoop
/* if (getenv("SNOOPPIPE"))
	  snoop_open(getenv("SNOOPPIPE")); */
#endif /* Snoop */

   if (Cominit (port_ptr) != 0x1954)
	  {
	  (void) printf (msgtxt[M_DRIVER_DEAD_1]);
	  (void) printf (msgtxt[M_DRIVER_DEAD_2]);
	  (void) printf (msgtxt[M_DRIVER_DEAD_3]);
	  set_prior(2); 								/* Regular */
	  exit (1);
	  }

   set_prior(2);									/* Regular */

   i = un_attended;
   un_attended = 0;

   set_prior(4);									/* Always High */

   (void) set_baud (max_baud.rate_value, 0);
   un_attended = i;

   MDM_ENABLE (lock_baud && (btypes[baud].rate_value >= lock_baud) ? max_baud.rate_mask : btypes[baud].rate_mask);
   DTR_ON ();
   XON_ENABLE ();

   set_prior(2);									/* Regular */

   Txbuf = Secbuf = (char *) malloc (WAZOOMAX + 16);
   if (!Txbuf)
	  {
	  status_line (msgtxt[M_MEM_ERROR]);
	  exit (2);
	  }

   /*
	* Pointing it to the middle of the buffer allows us to pop up
	* file transfer windows if we choose to do so.
	*/
   popbuf = Secbuf + 1500;
}

static void compile_externs ()
{
   register char *c;
   register i;
   char junk[100];
   int j, k, l;
   char *p;
   char x;

   i = l = 0;									 /* start at beginning	*/
   junk [0] = '\0';

   for (k = 0; protocols[k] != NULL; k++)		 /* Total no. of protos */
	  {
	  c = protocols[k]; 						 /* Point at filename	*/
	  if (!dexists (c)) 						 /* Is it there?		*/
		 {
		 (void) printf ("%s %s\n", msgtxt[M_NO_PROTOCOL], c);
		 continue;
		 }
	  p = NULL;
	  while (*c)								 /* Until end of string */
		 {
		 if ((*c == '\\') || (*c == ':'))        /* Look for last path  */
			p = c;								 /* Delimiter			*/
		 c++;
		 }
	  if (strlen (p) < 3)						 /* If no name, 		*/
		 continue;								 /* No protocol...		*/

	  p++;										 /* Point to the		*/
	  x = toupper (*p); 						 /* First character 	*/
	  if (strchr (junk, x) != NULL)
		 {
		 (void) printf ("%s %s\n", msgtxt[M_DUP_PROTOCOL], c);
		 continue;
		 }

	  protos[l].first_char = x; 				 /* Makes lookup fast	*/
	  protos[l++].entry = k;					 /* Now we know where	*/

	  junk[i++] = x;							 /* Store first char	*/
	  junk[i++] = ')';                           /* Then a ')'          */
	  c = ++p;									 /* Point to 2nd char	*/
	  for (j = 0; j < 9; j++)					 /* Up to 9 chars more	*/
		 {
		 if (*c != '.')                          /* If no comma yet,    */
			{
			junk[i++] = tolower (*c);			 /* store the char and	*/
			++c;								 /* bump the pointer	*/
			}
		 else junk[i++] = ' ';                   /* otherwise pad it    */
		 }
	  junk[i++] = ' ';                           /* And one more space  */
	  junk[i] = '\0';                            /* Need for testing    */
	  }

   if (!i)										 /* If we got none, 	*/
	  return;									 /* Return now. 		*/

   i += 2;										 /* Total for malloc	*/
   if ((extern_protocols = malloc ((unsigned) i)) == NULL)	/* Allocate string	   */
	  return;									 /* Return on failure	*/
   (void) strcpy (extern_protocols, junk);				/* Copy the string	   */
   return;										 /* Back to caller		*/
}



/**
 ** b_exitproc -- called by mainline to do exit processing.
 **/

void b_exitproc ()
{
   if (command_line_un)
	  {
	  set_prior(4); 								   /* Always High */
	  mdm_init (modem_busy);
	  exit_DTR ();
	  set_prior(2); 								   /* Regular */
	  }

   vfossil_cursor (1);

   while (KEYPRESS ())
	  {
	  (void) FOSSIL_CHAR ();
	  }

   gotoxy (0, SB_ROWS);
   clear_eol ();
   (void) printf (msgtxt[M_THANKS], ANNOUNCE);
   clear_eol ();
   (void) printf (msgtxt[M_ANOTHER_FINE_PRODUCT]);

   if (vfossil_installed)
	  vfossil_close ();

   if (!share) {
	  set_prior(4); 								   /* Always High */
	  MDM_DISABLE ();
	  set_prior(2); 								   /* Regular */
   }
}

