.ad 8
.bm 8
.fm 4
.bt $Copyright by   Software AG, 1993$$Page %$
.tm 12
.hm 6
.hs 3
.tt 1 $NME$Project Distributed Database System$vos01c$
.tt 2 $$$
.tt 3 $R.Roedling$Common Environment Functions$1997-08-01$
***********************************************************
.nf


    ========== licence begin LGPL
    Copyright (C) 2002 SAP AG

    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
    ========== licence end

.fo
.nf
.sp
Module  : Common_Environment_Functions
=========
.sp
Purpose :
.CM *-END-* purpose -------------------------------------
.sp
.cp 3
Define  :

.CM *-END-* define --------------------------------------
.sp;.cp 3
Use     :

.CM *-END-* use -----------------------------------------
.sp;.cp 3
Synonym :

.CM *-END-* synonym -------------------------------------
.sp;.cp 3
Author  : R.Roedling
.sp
.cp 3
Created : 1994-09-23
.sp
.cp 3
Version : 1994-09-23
.sp
.cp 3
Release :  7.0 	 Date : 1997-08-01
.br
.sp
***********************************************************
.sp
.cp 10
.fo
.oc _/1
Specification:

.CM *-END-* specification -------------------------------
.sp 2
***********************************************************
.sp
.cp 10
.fo
.oc _/1
Description:

.CM *-END-* description ---------------------------------
.sp 2
***********************************************************
.sp
.cp 10
.nf
.oc _/1
Structure:

.CM *-END-* structure -----------------------------------
.sp 2
**********************************************************
.sp
.cp 10
.nf
.oc _/1
.CM -lll-
Code    :
/*PRETTY*/


//
//  INCLUDE FILES
//


//
//  DEFINES
//

#define MOD__  "VOS01C : "
#define MF__   MOD__"UNDEFINED"


#define PGM_PATH                         "pgm\\"
#define WRK_PATH                         "wrk\\"
#define CONFIG_PATH                      "config\\"
#define ALTERNATE_SYMBOL_PATH            "symbols"
#define ALTERNATE_SYMBOL_PATH_ENV_VAR    "_NT_ALT_SYMBOL_PATH"
//
//  MACROS
//


//
//  LOCAL TYPE AND STRUCT DEFINITIONS
//


//
//  EXTERNAL VARIABLES
//


//
//  EXPORTED VARIABLES
//


//
//  LOCAL VARIABLES
//
static PSZ         pszDBRoot    = NULL;
static SQL_DBNAMEC szServerDB;
static tsp_name    szDBLang ;

//
//  LOCAL FUNCTION PROTOTYPES
//


//
// ========================== GLOBAL FUNCTIONS ================================
//


BOOL sql01c_get_serverdb ( PSZ *ppszServerDB  )
  {
  #undef  MF__
  #define MF__ MOD__"sql01c_get_serverdb"

  APIRET  rc          = NO_ERROR;
  PSZ     pszServerDB = NULL;


  DBGIN;

  if ( pszServerDB != NULL )
    {
    *ppszServerDB = szServerDB;

    DBGOUT;
    return ( TRUE );
    }
  else
    {
    rc = GETENV( SERVERDB_ENV_VAR, &pszServerDB );

    if ( (rc == NO_ERROR) && ( *pszServerDB ))
      {
      if ( !BLANKS_ONLY(pszServerDB))
        {
        strncpy ( szServerDB, pszServerDB, sizeof(szServerDB) - 1 );
        strupr  ( szServerDB );
        *ppszServerDB = szServerDB;

        DBGOUT;
        return ( TRUE );
        }
      }

    // --- searching for the old environment variable
    rc = GETENV( DBNAME_ENV_VAR, &pszServerDB );

    if ( (rc == NO_ERROR) && ( *pszServerDB ))
      {
      if ( !BLANKS_ONLY(pszServerDB))
        {
        strncpy ( szServerDB, pszServerDB, sizeof(szServerDB) - 1 );
        strupr  ( szServerDB );
        *ppszServerDB = szServerDB;

        DBGOUT;
        return ( TRUE );
        }
      }
    }

  *ppszServerDB = NULL;

  DBGOUT;
  return ( FALSE );
  }

/*------------------------------*/

BOOL sql01c_get_dbroot ( PSZ *ppszDBRoot  )
  {
  #undef  MF__
  #define MF__ MOD__"sql01c_get_dbroot"

  APIRET  rc = NO_ERROR;
  ULONG   ulDBRootLen;
  ULONG   i;


  DBGIN;

  if ( pszDBRoot != NULL )
    {
    *ppszDBRoot = pszDBRoot;

    DBGOUT;
    return ( TRUE );
    }
  else
    {
    rc = GETENV( DBROOT_ENV_VAR, &pszDBRoot );

    if ( (rc == NO_ERROR) && ( *pszDBRoot ))
      {
      if ( !BLANKS_ONLY(pszDBRoot) )
        {
        *ppszDBRoot = pszDBRoot;
        ulDBRootLen = strlen(pszDBRoot);

        for ( i = 0; i < ulDBRootLen; i++ )
          {
          if ( pszDBRoot[i] == '/' )
            pszDBRoot[i] = '\\';
          }

        if ( pszDBRoot[ulDBRootLen - 1] == '\\')
          {
          pszDBRoot[ulDBRootLen - 1] = '\0';
          }

        DBGOUT;
        return ( TRUE );
        }
      }
    }

  *ppszDBRoot = NULL;

  DBGOUT;
  return ( FALSE );
  }

/*------------------------------*/

BOOL sql01c_get_dblang ( PSZ *ppszDBLang  )
  {
  #undef  MF__
  #define MF__ MOD__"sql01c_get_dblang"

  APIRET  rc          = NO_ERROR;
  PSZ     pszDBLang   = NULL;


  DBGIN;

  if ( pszDBLang != NULL )
    {
    *ppszDBLang = szDBLang;

    DBGOUT;
    return ( TRUE );
    }
  else
    {
    rc = GETENV( DBLOCALE_ENV_VAR, &pszDBLang );

    if ( rc == ERROR_ENVVAR_NOT_FOUND )
      rc = GETENV( "DBLANG", &pszDBLang ); // old DBLANG

    if ( (rc == NO_ERROR) && ( *pszDBLang ))
      {
      if ( !BLANKS_ONLY(pszDBLang))
        {
        strncpy ( szDBLang, pszDBLang, sizeof(szDBLang) - 1 );
        strupr  ( szDBLang );
        *ppszDBLang = szDBLang;

        DBGOUT;
        return ( TRUE );
        }
      }
    }

  *ppszDBLang = NULL;

  DBGOUT;
  return ( FALSE );
  }


/*------------------------------*/

BOOL sql01c_set_symbol_path ( PSZ szDBRoot )
  {
  #undef  MF__
  #define MF__ MOD__"sql01c_set_symbol_path"

  #if defined(_WIN32)
   static PATHNAME  szSymbolPathEnv;
          PATHNAME  szTmpDBRoot;
          PSZ       *pszDBRoot ;
          ULONG     ulLen ;

   if ( szDBRoot )
     pszDBRoot = &szDBRoot ;
   else
     { pszDBRoot = (PSZ *)(&szTmpDBRoot) ;
       if ( !sql01c_get_dbroot ( pszDBRoot ) )
         return (FALSE) ;
     }
  // --- set SYMBOL environment variable
  ulLen = strlen(ALTERNATE_SYMBOL_PATH_ENV_VAR) +
          1                                     +    // '='
          strlen(ALTERNATE_SYMBOL_PATH_ENV_VAR) +
          strlen(*pszDBRoot)                    +
          1                                     +    // '\\'
          strlen(ALTERNATE_SYMBOL_PATH);


  if ( ulLen < sizeof(szSymbolPathEnv) )
    {
    szSymbolPathEnv[sizeof(szSymbolPathEnv) - 1] = '\0';

    strcpy ( szSymbolPathEnv, ALTERNATE_SYMBOL_PATH_ENV_VAR );
    strcat ( szSymbolPathEnv, "=" );
    strcat ( szSymbolPathEnv, *pszDBRoot );
    strcat ( szSymbolPathEnv, "\\" );
    strcat ( szSymbolPathEnv, ALTERNATE_SYMBOL_PATH );
    _putenv ( szSymbolPathEnv );
    }
     
  #endif

  DBGOUT;
  return ( TRUE );
  }

/*------------------------------*/

VOID sql01c_build_config_path ( PSZ szDBRoot, PSZ szConfigPath )
  {

  #undef  MF__
  #define MF__ MOD__"sql01c_build_config_path"

  DBGIN;

  strcpy (szConfigPath, pszDBRoot);
  strcat (szConfigPath, "\\"CONFIG_PATH);

  DBGOUT;
  }


/*------------------------------*/

VOID sql01c_build_pgm_path ( PSZ szDBRoot, PSZ szPgmPath )
  {

  #undef  MF__
  #define MF__ MOD__"sql01c_build_pgm_path"

  DBGIN;

  strcpy (szPgmPath, pszDBRoot);
  strcat (szPgmPath, "\\"PGM_PATH);

  DBGOUT;
  }

/*------------------------------*/

VOID sql01c_get_config_path ( PSZ *ppszConfigPath )
  {
  #undef  MF__
  #define MF__ MOD__"sql01c_get_config_path"

  static PATHNAME            szConfigPath;
  PSZ                        pszTmpDBRoot;

  #if defined(_WIN32)
   APIRET                    rc = NO_ERROR;
   REG_ENTRY_REC             RegistryEntries[1];
  #endif


  DBGIN;

  #if defined(_WIN32)
   RegistryEntries[0].pszValueName = REG_VN_CONFIG_PATH;
   RegistryEntries[0].pValue       = szConfigPath;
   RegistryEntries[0].ulValueSize  = sizeof(szConfigPath);
   RegistryEntries[0].ulValueType  = REG_EXPAND_SZ;

   rc = sql50_reg_get_applic_values ( NULL, HKEY_LOCAL_MACHINE,
                                      NULL, 1, RegistryEntries );

   if ((rc != NO_ERROR) || (szConfigPath[0] == '\0' ))
     {
     if ( sql01c_get_dbroot ( &pszTmpDBRoot ))
       sql01c_build_config_path ( pszTmpDBRoot , szConfigPath ) ;
     else
       sql01c_build_config_path ( "%"DBROOT_ENV_VAR"%\\" , szConfigPath ) ;
     }

  #else
    if ( sql01c_get_dbroot ( pszTmpDBRoot ))
      sql01c_build_config_path ( pszTmpDBRoot , szConfigPath ) ;
    else
       sql01c_build_config_path ( "%"DBROOT_ENV_VAR"%\\" , szConfigPath ) ;
  #endif

  *ppszConfigPath = szConfigPath;

  DBGOUT;
  return;
  }

/*------------------------------*/

LONG sql01c_set_config_path ( PSZ pszConfigPath )
  {
  #undef  MF__
  #define MF__ MOD__"sql01c_set_config_path"

  #if defined(_WIN32)
   APIRET                    rc = NO_ERROR;
   REG_ENTRY_REC             RegistryEntries[1];
  #endif


  DBGIN;

  #if defined(_WIN32)
   RegistryEntries[0].pszValueName = REG_VN_CONFIG_PATH;
   RegistryEntries[0].pValue       = pszConfigPath;
   RegistryEntries[0].ulValueSize  = strlen(pszConfigPath);
   RegistryEntries[0].ulValueType  = REG_EXPAND_SZ;

   rc = sql50_reg_put_applic_values ( NULL, HKEY_LOCAL_MACHINE,
                                      NULL, NULL, 1, RegistryEntries );
  #else
   DBGOUT;
   return ( ERROR_INVALID_FUNCTION );
  #endif


  DBGOUT;
  return ( rc );
  }

/*------------------------------*/

VOID sql01c_get_wrk_path ( PSZ *ppszWrkPath )
  {
  #undef  MF__
  #define MF__ MOD__"sql01c_get_wrk_path"

  static PATHNAME            szWrkPath;
  PSZ                        pszTmpDBRoot;

  #if defined(_WIN32)
   APIRET                    rc = NO_ERROR;
   REG_ENTRY_REC             RegistryEntries[1];
  #endif


  DBGIN;

  #if defined(_WIN32)
   RegistryEntries[0].pszValueName = REG_VN_WRK_PATH;
   RegistryEntries[0].pValue       = szWrkPath;
   RegistryEntries[0].ulValueSize  = sizeof(szWrkPath);
   RegistryEntries[0].ulValueType  = REG_EXPAND_SZ;

   rc = sql50_reg_get_applic_values ( NULL, HKEY_LOCAL_MACHINE,
                                      NULL, 1, RegistryEntries );

   if ((rc != NO_ERROR) || (szWrkPath[0] == '\0' ))
     {
     if ( sql01c_get_dbroot ( &pszTmpDBRoot ))
       {
       strcpy ( szWrkPath, pszTmpDBRoot );
       strcat ( szWrkPath, "\\"WRK_PATH );
      *ppszWrkPath = szWrkPath;
       }
     else
      *ppszWrkPath = "%"DBROOT_ENV_VAR"%\\"WRK_PATH;
     }
   else
     *ppszWrkPath = szWrkPath;

  #else
    if ( sql01c_get_dbroot ( pszTmpDBRoot ))
      {
      strcpy ( szWrkPath, pszTmpDBRoot );
      strcat ( szWrkPath, "\\"WRK_PATH );
      *ppszWrkPath = szWrkPath;
      }
    else
      *ppszWrkPath = "%"DBROOT_ENV_VAR"%\\"WRK_PATH;
  #endif


  DBGOUT;
  return;
  }

/*------------------------------*/

LONG sql01c_set_wrk_path ( PSZ pszWrkPath )
  {
  #undef  MF__
  #define MF__ MOD__"sql01c_set_wrk_path"

  #if defined(_WIN32)
   APIRET                    rc = NO_ERROR;
   REG_ENTRY_REC             RegistryEntries[1];
  #endif


  DBGIN;

  #if defined(_WIN32)
   RegistryEntries[0].pszValueName = REG_VN_WRK_PATH;
   RegistryEntries[0].pValue       = pszWrkPath;
   RegistryEntries[0].ulValueSize  = strlen(pszWrkPath);
   RegistryEntries[0].ulValueType  = REG_EXPAND_SZ;

   rc = sql50_reg_put_applic_values ( NULL, HKEY_LOCAL_MACHINE,
                                      NULL, NULL, 1, RegistryEntries );
  #else
   DBGOUT;
   return ( ERROR_INVALID_FUNCTION );
  #endif


  DBGOUT;
  return ( rc );
  }

//
// ========================== LOCAL FUNCTIONS =================================
//


//
// =============================== END ========================================
//
.CM *-END-* code ----------------------------------------
.SP 2
***********************************************************
.PA
