/* Single user high level dbeng (Bbuuzzb) configuration API.
   Rick Smereka, Copyright (C) 1999-2004.

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

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, get a copy via the Internet at
   http://gnu.org/copyleft/gpl.html or write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
   MA 02111-1307 USA

   You can contact the author via email at rsmereka@future-lab.com

   Original QNX version Feb/99, Rick Smereka

   Added session table and catalog 'get' and 'set' functions.
   Ported to 32bit Windows under CodeWarrior V4.
   Mar/99, Rick Smereka

   Ported to HP-UX under GNU C 2.8.1.
   Apr/99, Rick Smereka

   Ported to Red Hat Linux 5.2, Jul/99, Rick Smereka
   
   Changed function 'db_config_set_log_flag' to turn system
   logging (multiuser) or personal logging (stand-alone) on
   or off as appropriate. Set include file 'sys_log.h' to
   be included only if a multi-user build. Apr/2000, Rick Smereka
  
   Added functions 'db_config_get_replicate_flag' and
   'db_config_set_replicate_flag'. Jan/2002, Rick Smereka

   Changed function 'db_config_set_log_flag' to obtain the
   application name (multiuser) by calling the function
   'appinit_get_name'. Jul/2002, Rick Smereka

   Ported to Debian Linux. Nov/2002, Rick Smereka

   Enabled all catalog functions in both single and multi
   user modes. Dec/2003, Rick Smereka

   Changed all logging calls from 'sys_log' to 'logman'.
   Removed logging activate and de-activate code in function
   'db_config_set_log_flag'. Mar/2004, Rick Smereka */

#include "stdhead.h"
#include "dbmess.h"
#include "dbengcfg.h"
#include "dblocfg.h"
#include "dbiocode.h"

int db_config_get_tmp_path(char *path)
{
   /* Get the current value of the engine path for temporary files.
      'path' must already be allocated to sufficient size.
      Function returns a 'dbeng' code. */

   char mname[25];
   int ret;

   strcpy(mname, "db_config_get_tmp_path");
   logman("%s:enter", mname);

   if (path == (char *)NULL)
      {
      db_io_code_log(mname, "[path]null", DBENG_INVALID_FUNCTION);
      return(DBENG_INVALID_FUNCTION);
      }

   ret = dbeng_config_get_tmp_path(path);
   db_io_code_log(mname, "normal exit", ret);
   return(ret);
}

int db_config_get_log(char *slog)
{
   /* Get the current value of the engine local error log.
      'slog' must already be allocated to sufficient size.
      Function returns a 'dbeng' code. */

   char mname[25];
   int ret;

   strcpy(mname, "db_config_get_log");
   logman("%s:enter", mname);

   if (slog == (char *)NULL)
      {
      db_io_code_log(mname, "[slog]null", DBENG_INVALID_FUNCTION);
      return(DBENG_INVALID_FUNCTION);
      }

   ret = dbeng_config_get_log(slog);
   db_io_code_log(mname, "normal exit", ret);
   return(ret);
}

int db_config_get_session(char *ses_table)
{
   /* Get the current session table and place into
      'ses_table' which must already be allocated
      to sufficient size. Function returns
      'DBENG_OK' upon success, an engine i/o
      code otherwise. */

   char mname[] = "db_config_get_session";
   int ret;

   logman("%s:enter", mname);

#ifndef MULTIUSER
   db_io_code_log(mname, "not multiuser mode", DBENG_ACCESS_DENIED);
   return(DBENG_ACCESS_DENIED);
#else
   if (ses_table == (char *)NULL)
      {
      db_io_code_log(mname, "[ses_table]null", DBENG_INVALID_FUNCTION);
      return(DBENG_INVALID_FUNCTION);
      }

   ret = dbeng_config_get_session(ses_table);
   db_io_code_log(mname, "normal exit", ret);
   return(ret);
#endif
}

int db_config_get_catalog(char *cat)
{
   /* Get the current catalog and place into
      'cat' which must already be allocated
      to sufficient size. Function returns
      'DBENG_OK' upon success, an engine i/o
      code otherwise. */

   char mname[] = "db_config_get_catalog";
   int ret;

   logman("%s:enter", mname);

   if (cat == (char *)NULL)
      {
      db_io_code_log(mname, "[cat]null", DBENG_INVALID_FUNCTION);
      return(DBENG_INVALID_FUNCTION);
      }

   ret = dbeng_config_get_catalog(cat);
   db_io_code_log(mname, "normal exit", ret);
   return(ret);
}

int db_config_get_log_flag(int *flag)
{
   /* Get the value of the current log flag.
      Function reutrns a 'dbeng' i/o code. */

   char mname[25];
   int ret;

   strcpy(mname, "db_config_get_log_flag");
   logman("%s:enter", mname);

   if (flag == (int *)NULL)
      {
      db_io_code_log(mname, "[flag]null", DBENG_INVALID_FUNCTION);
      return(DBENG_INVALID_FUNCTION);
      }

   *flag = FALSE;
   ret = dbeng_config_get_log_flag(flag);
   db_io_code_log(mname, "normal exit", ret);
   return(ret);
}

int db_config_get_session_flag(int *flag)
{
   /* Get the value of the current session flag.
      Function reutrns a 'dbeng' i/o code. */

   char mname[30];
   int ret;

   strcpy(mname, "db_config_get_session_flag");
   logman("%s:enter", mname);

   if (flag == (int *)NULL)
      {
      db_io_code_log(mname, "[flag]null", DBENG_INVALID_FUNCTION);
      return(DBENG_INVALID_FUNCTION);
      }

   *flag = FALSE;

#ifndef MULTIUSER
   db_io_code_log(mname, "not in multiuser mode", DBENG_ACCESS_DENIED);
   return(DBENG_ACCESS_DENIED);
#else
   ret = dbeng_config_get_session_flag(flag);
   logman("%s:normal exit[%d],flag=%d", mname, ret,*flag);
   return(ret);
#endif
}

int db_config_get_catalog_flag(int *flag)
{
   /* Get the value of the current catalog flag.
      Function reutrns a 'dbeng' i/o code. */

   char mname[30];
   int ret;

   strcpy(mname, "db_config_get_catalog_flag");
   logman("%s:enter", mname);

   if (flag == (int *)NULL)
      {
      db_io_code_log(mname, "[flag]null", DBENG_INVALID_FUNCTION);
      return(DBENG_INVALID_FUNCTION);
      }

   *flag = FALSE;

   ret = dbeng_config_get_catalog_flag(flag);
   logman("%s:normal exit[%d],flag=%d", mname, ret, *flag);
   return(ret);
}

int db_config_get_replicate_flag(int *flag)
{
   /* Get the value of the current replicate flag.
      Function reutrns a 'dbeng' i/o code. */

   char mname[] = "db_config_get_replicate_flag";
   int ret;

   logman("%s:enter", mname);

   if (flag == (int *)NULL)
      {
      db_io_code_log(mname, "null[flag]", DBENG_INVALID_FUNCTION);
      return(DBENG_INVALID_FUNCTION);
      }

   *flag = FALSE;

#ifndef MULTIUSER
   db_io_code_log(mname, "not in multiuser mode", DBENG_ACCESS_DENIED);
   return(DBENG_ACCESS_DENIED);
#else
   ret = dbeng_config_get_replicate_flag(flag);
   logman("%s:normal exit[%d],flag=%d", mname, ret, *flag);
   return(ret);
#endif
}

int db_config_get_version(char *ver)
{
   /* Get the 'dbeng' version ID string. Function returns
      'DBENG_OK' upon success with the version string
      loaded into 'ver' which must already be allocated
      to sufficient size. Function returns a 'dbeng'
      i/o code otherwise. */

   char mname[] = "db_config_get_version";
   int ret;

   logman("%s:enter", mname);

   if (ver == (char *)NULL)
      {
      db_io_code_log(mname, "[ver]null", DBENG_INVALID_FUNCTION);
      return(DBENG_INVALID_FUNCTION);
      }

   ret = dbeng_config_get_version(ver);
   db_io_code_log(mname, "normal exit", ret);
   return(ret);
}

int db_config_set_tmp_path(char *path)
{
   /* Set the engine temporary path to 'path'. Function
      returns a 'dbeng' code. */

   char mname[25];
   int ret;

   strcpy(mname, "db_config_set_tmp_path");
   logman("%s:enter", mname);

   if (path == (char *)NULL || !strlen(path))
      {
      db_io_code_log(mname, "[path]null", DBENG_INVALID_FUNCTION);
      return(DBENG_INVALID_FUNCTION);
      }

   ret = dbeng_config_set_tmp_path(path);
   db_io_code_log(mname, "normal exit", ret);
   return(ret);
}

int db_config_set_log(char *slog)
{
   /* Set the engine error log file to 'slog'.
      Function returns a 'dbeng' code. */

   char mname[] = "db_config_set_log";
   int ret;

   logman("%s:enter", mname);

   if (slog == (char *)NULL || !strlen(slog))
      {
      db_io_code_log(mname, "[slog]null or empty", DBENG_INVALID_FUNCTION);
      return(DBENG_INVALID_FUNCTION);
      }

   ret = dbeng_config_set_log(slog);
   db_io_code_log(mname, "normal exit", ret);
   return(ret);
}

int db_config_set_session(char *ses_table)
{
   /* Set the engine session table to 'ses_table'.
      This option is only valid in multiuser mode.
      Function returns a 'dbeng' code. */

   char mname[] = "db_config_set_session";
   int ret;

   logman("%s:enter", mname);

#ifndef MULTIUSER
   db_io_code_log(mname, "not in multiuser mode", DBENG_ACCESS_DENIED);
   return(DBENG_ACCESS_DENIED);
#else
   if (ses_table == (char *)NULL || !strlen(ses_table))
      {
      db_io_code_log(mname, "[ses_table]null or empty",
                     DBENG_INVALID_FUNCTION);
      return(DBENG_INVALID_FUNCTION);
      }

   ret = dbeng_config_set_session(ses_table);
   db_io_code_log(mname, "normal exit", ret);
   return(ret);
#endif
}

int db_config_set_catalog(char *cat)
{
   /* Set the engine catalog to 'cat'.
      This option is only valid in multiuser mode.
      Function returns a 'dbeng' code. */

   char mname[] = "db_config_set_catalog";
   int ret;

   logman("%s:enter", mname);

   if (cat == (char *)NULL || !strlen(cat))
      {
      db_io_code_log(mname, "[cat]null or empty", DBENG_INVALID_FUNCTION);
      return(DBENG_INVALID_FUNCTION);
      }

   ret = dbeng_config_set_catalog(cat);
   db_io_code_log(mname, "normal exit", ret);
   return(ret);
}

int db_config_set_log_flag(int flag)
{
   /* Set the engine log flag and turn system
      logging on or off as appropriate. 'flag' must
      be zero or one. Function returns
      'DBENG_OK' upon success, an engine
      i/o code otherwise. */

   char mname[] = "db_config_set_log_flag";
   char mes[128];
   int ret;

   logman("%s:enter", mname);

   if (flag != TRUE && flag != FALSE)
      {
      db_io_code_log(mname, "[flag]not zero or one", DBENG_INVALID_FUNCTION);
      return(DBENG_INVALID_FUNCTION);
      }

   if ((ret = dbeng_config_set_log_flag(flag)) != DBENG_OK)
      {
      db_io_code_log(mname, "exit:bad ret from dbeng_config_set_log_flag", ret);
      return(ret);
      }
                
   db_io_code_log(mname, "normal exit", ret);
   return(ret);
}

int db_config_set_session_flag(int flag)
{
   /* Set the engine session flag. 'flag' must
      be zero or one. Function returns
      'DBENG_OK' upon success, an engine
      i/o code otherwise. */

   char mname[] = "db_config_set_session_flag";
   int ret;

   logman("%s:enter", mname);

#ifndef MULTIUSER
   db_io_code_log(mname, "not in multiuser mode", DBENG_ACCESS_DENIED);
   return(DBENG_ACCESS_DENIED);
#else
   if (flag != TRUE && flag != FALSE)
      {
      db_io_code_log(mname, "[flag]not zero or one", DBENG_INVALID_FUNCTION);
      return(DBENG_INVALID_FUNCTION);
      }

   ret = dbeng_config_set_session_flag(flag);
   db_io_code_log(mname, "normal exit", ret);
   return(ret);
#endif
}

int db_config_set_catalog_flag(int flag)
{
   /* Set the engine catalog flag. 'flag' must
      be zero or one. Function returns
      'DBENG_OK' upon success, an engine
      i/o code otherwise. */

   char mname[] = "db_config_set_catalog_flag";
   int ret;

   logman("%s:enter", mname);

   if (flag != TRUE && flag != FALSE)
      {
      db_io_code_log(mname, "[flag]not zero or one", DBENG_INVALID_FUNCTION);
      return(DBENG_INVALID_FUNCTION);
      }

   ret = dbeng_config_set_catalog_flag(flag);
   db_io_code_log(mname, "normal exit", ret);
   return(ret);
}

int db_config_set_replicate_flag(int flag)
{
   /* Set the engine replication flag. 'flag' must
      be zero or one. Function returns
      'DBENG_OK' upon success, an engine
      i/o code otherwise. */

   char mname[] = "db_config_set_replicate_flag";
   int ret;

   logman("%s:enter", mname);

#ifndef MULTIUSER
   db_io_code_log(mname, "not in multiuser mode", DBENG_ACCESS_DENIED);
   return(DBENG_ACCESS_DENIED);
#else
   if (flag != TRUE && flag != FALSE)
      {
      db_io_code_log(mname, "[flag]not zero or one", DBENG_INVALID_FUNCTION);
      return(DBENG_INVALID_FUNCTION);
      }

   ret = dbeng_config_set_replicate_flag(flag);
   db_io_code_log(mname, "normal exit", ret);
   return(ret);
#endif
}
