/*************************************************
*                      Smailmon                  *
*************************************************/

/* Copyright (c) University of Cambridge 1995. */

/* P. Hazel, February 1995*/
/* Last Modified: March 1995 */

/* This module contains code to initialize things from the
environment and the arguments. */


#include "smailmon.h"



/*************************************************
*            Decode stripchart config            *
*************************************************/

/* First determine how many are requested, then compile the
regular expressions and save the title strings. *Note that
stripchart_number is initialized to 1 to count the always-
present queue stripchart. */

static void decode_stripchart_config(char *s)
{
int i;

/* Loop: first time just counts, second time does the
work. */

for (i = 0; i <= 1; i++)

  {
  int first = 1; 
  int count = 0; 
  char *p = s;
   
  if (*p == '/') p++;   /* allow optional / at start */

  /* This loops for all the substrings, using the first flag
  to determine whether each is the first or second of the pairs. */
      
  while (*p)
    {
    char *pp; 
    /* Handle continuations */ 
    if (*p == '\n')
      {
      while (*(++p) == ' ' || *p == '\t');
      if (*p == '/') p++;  
      }  

    /* Find the end of the string and count if first string */
     
    pp = p;
    while (*p && *p != '/') p++; 
    if (first) count++; 
  
    /* Take action on the second time round. So as to give more
    information from the regexp error routine, set a pointer to
    the string we are compiling. */
     
    if (i != 0)
      {
      char buffer[256];
      strncpy(buffer, pp, p-pp);
      buffer[p-pp] = 0;   
      if (first) 
        {
        regexp_compiling = buffer; 
        stripchart_regexp[count] = regcomp(buffer);
        regexp_compiling = NULL; 
        } 
      else stripchart_title[count] = copystring(buffer);
      }
   
    /* Advance past the delimiter and flip the first/second flag */
     
    p++;
    first = !first;
    }
   
  /* On the first pass, we now know the number of stripcharts. Get
  store for holding the pointers to the regular expressions and
  title strings. */

  if (i == 0)
    {
    stripchart_number += count;  
    stripchart_regexp = (regexp **)malloc(stripchart_number * sizeof(regexp *));
    stripchart_title = (char **)malloc(stripchart_number * sizeof(char *));  
    }    
  } 
}


/*************************************************
*                    Initialize                  *
*************************************************/

void init(int argc, char **argv)
{
int x;
char *s;

/* Deal with simple values in the environment. */

s = getenv("LOG_BUFFER");
if (s != NULL)
  {
  char c[1];
  if (sscanf(s, "%d%c", &x, c) > 0)
    {
    if (c[0] == 'K' || c[0] == 'k') x *= 1024;
    if (x < 1024) x = 1024;
    log_buffer_size = x;
    }
  }   

s = getenv("LOG_DEPTH");
if (s != NULL && (x = atoi(s)) != 0) log_depth = x;

s = getenv("LOG_FONT");
if (s != NULL) log_font = copystring(s);

s = getenv("LOG_WIDTH");
if (s != NULL && (x = atoi(s)) != 0) log_width = x;

s = getenv("QUEUE_DEPTH");
if (s != NULL && (x = atoi(s)) != 0) queue_depth = x;

s = getenv("QUEUE_FONT");
if (s != NULL) queue_font = copystring(s);

s = getenv("QUEUE_INTERVAL");
if (s != NULL && (x = atoi(s)) != 0) queue_update = x;

s = getenv("QUEUE_MAX_ADDRESSES");
if (s != NULL && (x = atoi(s)) != 0) queue_max_addresses = x;

s = getenv("QUEUE_WIDTH");
if (s != NULL && (x = atoi(s)) != 0) queue_width = x;

s = getenv("SMAILINPUTDIR");
if (s != NULL) input_dir = copystring(s);

s = getenv("SMAILLOGFILE");
if (s != NULL) log_file = copystring(s);

s = getenv("SMAILMON");
if (s != NULL) smailmon_name = copystring(s);

s = getenv("VISIBLE_NAME");
if (s != NULL) visible_name = copystring(s);

s = getenv("WINDOW_TITLE");
if (s != NULL) window_title = copystring(s);

/* Deal with stripchart configuration in a separate function */

s = getenv("STRIPCHARTS");
if (s != NULL) decode_stripchart_config(s);

s = getenv("STRIPCHART_INTERVAL");
if (s != NULL && (x = atoi(s)) != 0) stripchart_update = x;

/* Pro tem, to make it easy to have a short interval
for testing. */

if (argc > 1) queue_update = stripchart_update = atoi(argv[1]);
}

/* End of init.c */
