/*  -*- c -*-
mzargs.c, part of:

muttzilla  v0.40

Copyright (C) 1999-2000 Brian D. Winters

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
*/


#include "muttzilla.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
/*#include <sys/stat.h>*/
/*#include <unistd.h>*/
/*#include <fcntl.h>*/


unsigned char mz_debug = 0;
unsigned char mz_config_initialized = 0;
unsigned char mz_config_reread = 0;

char *mz_tempdir = NULL;
char *mz_mailprog = NULL;
char *mz_newsprog = NULL;
char *mz_userconf = NULL;


/* safely copy strings 

   <paranoid>this is not thread safe but probably should be</paranoid>
 */
void mz_strcpy(char **dest, const char *src) {
  size_t copylen;

  if(*dest)
    free(*dest);
  copylen = strlen(src);
  *dest = (char*)malloc((copylen+1)*sizeof(char));
  (void)strcpy(*dest, src);
}


void config_cleanup() {
  free(mz_tempdir);
  mz_tempdir = NULL;

  free(mz_mailprog);
  mz_mailprog = NULL;

  free(mz_newsprog);
  mz_newsprog = NULL;

  free(mz_userconf);
  mz_userconf = NULL;
}


void config_set_from_defines() {
#ifdef DEBUG
  mz_debug = 1;
#else
  mz_debug = 0;
#endif

#ifdef CONFIG_REREAD
  if(!mz_config_initialized)
    mz_config_reread = CONFIG_REREAD;
#endif

#ifdef TEMPDIR
  mz_strcpy(&mz_tempdir, TEMPDIR);
#else
  free(mz_tempdir);
  mz_tempdir = NULL;
#endif

#ifdef CONFIG_GLOBAL
#ifdef CONFIG_USER
  mz_strcpy(&mz_userconf, CONFIG_USER);
#else
  free(mz_userconf);
  mz_userconf = NULL;
#endif
#else
  free(mz_userconf);
  mz_userconf = NULL;
#endif

#ifdef SUPPORT_MAIL
#ifdef MAILPROG
  mz_strcpy(&mz_mailprog, MAILPROG);
#else
  free(mz_mailprog);
  mz_mailprog = NULL;
#endif
#else /* #ifdef SUPPORT_MAIL */
  free(mz_mailprog);
  mz_mailprog = NULL;
#endif /* #ifdef SUPPORT_MAIL */

#ifdef SUPPORT_NEWS
#ifdef NEWSPROG
  mz_strcpy(&mz_newsprog, NEWSPROG);
#else
  free(mz_newsprog);
  mz_newsprog = NULL;
#endif
#else /* #ifdef SUPPORT_NEWS */
  free(mz_newsprog);
  mz_newsprog = NULL;
#endif /* #ifdef SUPPORT_NEWS */
}

/* <paranoid>this is not thread safe but probably should be</paranoid> */
const char *gettoken(FILE *fp) {
  int i=0, insingle=0, indouble=0, iseq=0, c;
  static char token[128];

  (void)memset(token, '\0', 128);

  c = fgetc(fp);

  /* eat whitespace and comments */
  while(c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '#')
    if(c == '#') {
      while(c != EOF && c != '\n' && c != '\r')
	c = fgetc(fp);
    } else
      c = fgetc(fp);

  /* should be at the start of a token now, or EOF */
  if(c != EOF) {
    if(c == '\'') {
      insingle = 1;
      c = fgetc(fp);
    } else if(c == '"') {
      indouble = 1;
      c = fgetc(fp);
    } else if(c == '=') {
      iseq = 1;
      token[i++] = c;
    }
    if(!iseq) {
      while(c != EOF && !iseq && i < 127 &&
	    ((insingle && c != '\'') ||
	     (indouble && c != '"') ||
	     (!insingle && !indouble &&
	      c != '=' && c != ' ' && c != '\t' && c != '\n' && c != '\r'))) {
	token[i++] = c;
	c = fgetc(fp);
      }
      if(c == '=') {
	if(ungetc(c, fp) != c)
	  fprintf(stderr, "muttzilla error doing ungetc()\n");
	c = token[i-1]; /* not sure I need this, but I'm tired of debugging */
      }
      if(insingle && c != '\'') {
	fprintf(stderr, "mutzilla choke single (%c)\n", c);
	i = 0;
      }
      if(indouble && c != '"') {
	fprintf(stderr, "mutzilla choke double (%c)\n", c);
	i = 0;
      }
    }
  }

  token[i] = '\0';
  return token;
}

void parse_config_file(FILE *fp) {
  const char *token, *arg;
  char *cmd=NULL;
  int num;
  
  token = gettoken(fp);
  while(1) {
    if(strlen(token) == 0)
      break;
    mz_strcpy(&cmd, token);

    token = gettoken(fp);
    if(strcmp(token, "=")) {
      fprintf(stderr, "muttzilla parse error, looking for =, cmd is %s\n", cmd);
      break;
    }

    token = gettoken(fp);
    if(strlen(token) == 0) {
      fprintf(stderr, "muttzilla parse error, looking for arg, cmd is %s\n", cmd);
      break;
    }
    arg = token;

    if(!strcmp(cmd, "debug")) {
      if(mz_debug)
	fprintf(stderr, "muttzilla got debug\n");
      if(sscanf(arg, "%d", &num) == 1) {
	mz_debug = num;
	if(mz_debug)
	  fprintf(stderr, "muttzilla debug is %d\n", mz_debug);
      }
    } else if(!strcmp(cmd, "tempdir")) {
      mz_strcpy(&mz_tempdir, arg);
      if(mz_debug)
	fprintf(stderr, "muttzilla tempdir is %s\n", mz_tempdir);
    } else if(!strcmp(cmd, "userconf")) {
      mz_strcpy(&mz_userconf, arg);
      if(mz_debug)
	fprintf(stderr, "muttzilla userconf is %s\n", mz_userconf);
    } else if(!strcmp(cmd, "mailscript")) {
      mz_strcpy(&mz_mailprog, arg);
      if(mz_debug)
	fprintf(stderr, "muttzilla mailscript is %s\n", mz_mailprog);
    } else if(!strcmp(cmd, "newsscript")) {
      mz_strcpy(&mz_newsprog, arg);
      if(mz_debug)
	fprintf(stderr, "muttzilla newsscript is %s\n", mz_newsprog);
    } else if(!strcmp(cmd, "reread")) {
      if(mz_debug)
	fprintf(stderr, "muttzilla got reread\n");
      if(sscanf(arg, "%d", &num) == 1) {
	mz_config_reread = num;
	if(mz_debug)
	  fprintf(stderr, "muttzilla reread is %d\n", mz_config_reread);
      }
    }

    token = gettoken(fp);
  }

  free(cmd);
}

void config_set_from_file(const char *fn) {
  FILE *fp;

  if(fn) {
    fp = fopen(fn, "r");
    if(fp) {
      parse_config_file(fp);
      (void)fclose(fp);
    }
  }
}


void config_set_from_global() {
#ifdef CONFIG_GLOBAL
  config_set_from_file(CONFIG_GLOBAL);
#endif
}

void config_set_from_user() {
#ifdef CONFIG_GLOBAL
  config_set_from_file(mz_userconf);
#endif
}

void config_set() {
  if(mz_debug)
    fprintf(stderr, "muttzilla rereading config\n");
  config_set_from_defines();
  config_set_from_global();
  config_set_from_user();
}

void config_init() {
  config_set();
  mz_config_initialized = 1;
}

void config_update() {
  if(!mz_config_initialized)
    config_init();
  else if(mz_config_reread)
    config_set();
}


syntax highlighted by Code2HTML, v. 0.9.1