#include "config.h"
#include "system.h"
#ifdef _WIN32
#include <windows.h>
#endif
#include "prefix.h"
static const char *std_prefix = PREFIX;
static const char *get_key_value PROTO((char *));
static const char *translate_name PROTO((const char *));
static char *save_string PROTO((const char *, int));
#ifdef _WIN32
static char *lookup_key PROTO((char *));
static HKEY reg_key = (HKEY) INVALID_HANDLE_VALUE;
#endif
#ifndef DIR_SEPARATOR
# define IS_DIR_SEPARATOR(ch) ((ch) == '/')
#else
# ifndef DIR_SEPARATOR_2
# define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
# else
# define IS_DIR_SEPARATOR(ch) \
(((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
# endif
#endif
static const char *
get_key_value (key)
char *key;
{
const char *prefix = 0;
char *temp = 0;
#ifdef _WIN32
prefix = lookup_key (key);
#endif
if (prefix == 0)
prefix = getenv (temp = concat (key, "_ROOT", NULL_PTR));
if (prefix == 0)
prefix = std_prefix;
if (temp)
free (temp);
return prefix;
}
char *
concat VPROTO((const char *first, ...))
{
register int length;
register char *newstr;
register char *end;
register const char *arg;
va_list args;
#ifndef ANSI_PROTOTYPES
const char *first;
#endif
VA_START (args, first);
#ifndef ANSI_PROTOTYPES
first = va_arg (args, const char *);
#endif
arg = first;
length = 0;
while (arg != 0)
{
length += strlen (arg);
arg = va_arg (args, const char *);
}
newstr = (char *) malloc (length + 1);
va_end (args);
VA_START (args, first);
#ifndef ANSI_PROTOTYPES
first = va_arg (args, char *);
#endif
end = newstr;
arg = first;
while (arg != 0)
{
while (*arg)
*end++ = *arg++;
arg = va_arg (args, const char *);
}
*end = '\000';
va_end (args);
return (newstr);
}
static char *
save_string (s, len)
const char *s;
int len;
{
register char *result = xmalloc (len + 1);
bcopy (s, result, len);
result[len] = 0;
return result;
}
#ifdef _WIN32
static char *
lookup_key (key)
char *key;
{
char *dst;
DWORD size;
DWORD type;
LONG res;
if (reg_key == (HKEY) INVALID_HANDLE_VALUE)
{
res = RegOpenKeyExA (HKEY_LOCAL_MACHINE, "SOFTWARE", 0,
KEY_READ, ®_key);
if (res == ERROR_SUCCESS)
res = RegOpenKeyExA (reg_key, "Free Software Foundation", 0,
KEY_READ, ®_key);
if (res != ERROR_SUCCESS)
{
reg_key = (HKEY) INVALID_HANDLE_VALUE;
return 0;
}
}
size = 32;
dst = (char *) malloc (size);
res = RegQueryValueExA (reg_key, key, 0, &type, dst, &size);
if (res == ERROR_MORE_DATA && type == REG_SZ)
{
dst = (char *) realloc (dst, size);
res = RegQueryValueExA (reg_key, key, 0, &type, dst, &size);
}
if (type != REG_SZ || res != ERROR_SUCCESS)
{
free (dst);
dst = 0;
}
return dst;
}
#endif
static const char *
translate_name (name)
const char *name;
{
char code = name[0];
char *key;
const char *prefix = 0;
int keylen;
if (code != '@' && code != '$')
return name;
for (keylen = 0;
(name[keylen + 1] != 0 && !IS_DIR_SEPARATOR (name[keylen + 1]));
keylen++)
;
key = (char *) alloca (keylen + 1);
strncpy (key, &name[1], keylen);
key[keylen] = 0;
name = &name[keylen + 1];
if (code == '@')
{
prefix = get_key_value (key);
if (prefix == 0)
prefix = std_prefix;
}
else
prefix = getenv (key);
if (prefix == 0)
prefix = PREFIX;
if (IS_DIR_SEPARATOR (prefix[strlen (prefix) - 1]))
{
char * temp = save_string (prefix, strlen (prefix));
temp[strlen (temp) - 1] = 0;
prefix = temp;
}
return concat (prefix, name, NULL_PTR);
}
const char *
update_path (path, key)
const char *path;
const char *key;
{
if (! strncmp (path, std_prefix, strlen (std_prefix)) && key != 0)
{
if (key[0] != '$')
key = concat ("@", key, NULL_PTR);
path = concat (key, &path[strlen (std_prefix)], NULL_PTR);
while (path[0] == '@' || path[0] == '$')
path = translate_name (path);
}
#ifdef DIR_SEPARATOR_2
if (DIR_SEPARATOR != DIR_SEPARATOR_2)
{
int i;
int len = strlen (path);
char *new_path = save_string (path, len);
for (i = 0; i < len; i++)
if (new_path[i] == DIR_SEPARATOR_2)
new_path[i] = DIR_SEPARATOR;
path = new_path;
}
#endif
#if defined (DIR_SEPARATOR) && !defined (DIR_SEPARATOR_2)
if (DIR_SEPARATOR != '/')
{
int i;
int len = strlen (path);
char *new_path = save_string (path, len);
for (i = 0; i < len; i++)
if (new_path[i] == '/')
new_path[i] = DIR_SEPARATOR;
path = new_path;
}
#endif
return path;
}
void
set_std_prefix (prefix, len)
const char *prefix;
int len;
{
std_prefix = save_string (prefix, len);
}