+#ifndef S_IXGRP
+#define S_IXGRP S_IXUSR
+#endif
+#ifndef S_IXOTH
+#define S_IXOTH S_IXUSR
+#endif
+#ifndef S_IRWXG
+#define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
+#endif
+#ifndef S_ISGID
+#define S_ISGID 0
+#endif
+#endif /* PLATFORM_WIN32 */
+
+/* file permissions for newly written files */
+#define MODE_R_ALL (S_IRUSR | S_IRGRP | S_IROTH)
+#define MODE_W_ALL (S_IWUSR | S_IWGRP | S_IWOTH)
+#define MODE_X_ALL (S_IXUSR | S_IXGRP | S_IXOTH)
+
+#define MODE_W_PRIVATE (S_IWUSR)
+#define MODE_W_PUBLIC (S_IWUSR | S_IWGRP)
+#define MODE_W_PUBLIC_DIR (S_IWUSR | S_IWGRP | S_ISGID)
+
+#define DIR_PERMS_PRIVATE (MODE_R_ALL | MODE_X_ALL | MODE_W_PRIVATE)
+#define DIR_PERMS_PUBLIC (MODE_R_ALL | MODE_X_ALL | MODE_W_PUBLIC_DIR)
+
+#define FILE_PERMS_PRIVATE (MODE_R_ALL | MODE_W_PRIVATE)
+#define FILE_PERMS_PUBLIC (MODE_R_ALL | MODE_W_PUBLIC)
+
+char *getHomeDir()
+{
+ static char *dir = NULL;
+
+#if defined(PLATFORM_WIN32)
+ if (dir == NULL)
+ {
+ dir = checked_malloc(MAX_PATH + 1);
+
+ if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, dir)))
+ strcpy(dir, ".");
+ }
+#elif defined(PLATFORM_UNIX)
+ if (dir == NULL)
+ {
+ if ((dir = getenv("HOME")) == NULL)
+ {
+ struct passwd *pwd;
+
+ if ((pwd = getpwuid(getuid())) != NULL)
+ dir = getStringCopy(pwd->pw_dir);
+ else
+ dir = ".";
+ }
+ }
+#else
+ dir = ".";
+#endif
+
+ return dir;
+}
+
+char *getCommonDataDir(void)
+{
+ static char *common_data_dir = NULL;
+
+#if defined(PLATFORM_WIN32)
+ if (common_data_dir == NULL)
+ {
+ char *dir = checked_malloc(MAX_PATH + 1);
+
+ if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_COMMON_DOCUMENTS, NULL, 0, dir))
+ && !strEqual(dir, "")) /* empty for Windows 95/98 */
+ common_data_dir = getPath2(dir, program.userdata_subdir);
+ else
+ common_data_dir = options.rw_base_directory;
+ }
+#else
+ if (common_data_dir == NULL)
+ common_data_dir = options.rw_base_directory;
+#endif
+
+ return common_data_dir;
+}
+
+char *getPersonalDataDir(void)
+{
+ static char *personal_data_dir = NULL;
+
+#if defined(PLATFORM_MACOSX)
+ if (personal_data_dir == NULL)
+ personal_data_dir = getPath2(getHomeDir(), "Documents");
+#else
+ if (personal_data_dir == NULL)
+ personal_data_dir = getHomeDir();
+#endif
+
+ return personal_data_dir;
+}
+
+char *getUserGameDataDir(void)
+{
+ static char *user_game_data_dir = NULL;
+
+ if (user_game_data_dir == NULL)
+ user_game_data_dir = getPath2(getPersonalDataDir(),
+ program.userdata_subdir);
+
+ return user_game_data_dir;
+}
+
+void updateUserGameDataDir()
+{
+#if defined(PLATFORM_MACOSX)
+ char *userdata_dir_old = getPath2(getHomeDir(), program.userdata_subdir_unix);
+ char *userdata_dir_new = getUserGameDataDir(); /* do not free() this */
+
+ /* convert old Unix style game data directory to Mac OS X style, if needed */
+ if (fileExists(userdata_dir_old) && !fileExists(userdata_dir_new))
+ {
+ if (rename(userdata_dir_old, userdata_dir_new) != 0)
+ {
+ Error(ERR_WARN, "cannot move game data directory '%s' to '%s'",
+ userdata_dir_old, userdata_dir_new);
+
+ /* continue using Unix style data directory -- this should not happen */
+ program.userdata_path = getPath2(getPersonalDataDir(),
+ program.userdata_subdir_unix);
+ }
+ }
+
+ free(userdata_dir_old);
+#endif
+}
+
+char *getSetupDir()
+{
+ return getUserGameDataDir();
+}
+
+static mode_t posix_umask(mode_t mask)
+{
+#if defined(PLATFORM_UNIX)
+ return umask(mask);
+#else
+ return 0;
+#endif
+}
+
+static int posix_mkdir(const char *pathname, mode_t mode)
+{
+#if defined(PLATFORM_WIN32)
+ return mkdir(pathname);
+#else
+ return mkdir(pathname, mode);
+#endif
+}
+
+static boolean posix_process_running_setgid()
+{
+#if defined(PLATFORM_UNIX)
+ return (getgid() != getegid());
+#else
+ return FALSE;
+#endif
+}
+
+void createDirectory(char *dir, char *text, int permission_class)
+{
+ /* leave "other" permissions in umask untouched, but ensure group parts
+ of USERDATA_DIR_MODE are not masked */
+ mode_t dir_mode = (permission_class == PERMS_PRIVATE ?
+ DIR_PERMS_PRIVATE : DIR_PERMS_PUBLIC);
+ mode_t last_umask = posix_umask(0);
+ mode_t group_umask = ~(dir_mode & S_IRWXG);
+ int running_setgid = posix_process_running_setgid();
+
+ /* if we're setgid, protect files against "other" */
+ /* else keep umask(0) to make the dir world-writable */
+
+ if (running_setgid)
+ posix_umask(last_umask & group_umask);
+ else
+ dir_mode |= MODE_W_ALL;
+
+ if (!fileExists(dir))
+ if (posix_mkdir(dir, dir_mode) != 0)
+ Error(ERR_WARN, "cannot create %s directory '%s'", text, dir);
+
+ if (permission_class == PERMS_PUBLIC && !running_setgid)
+ chmod(dir, dir_mode);
+
+ posix_umask(last_umask); /* restore previous umask */
+}
+
+void InitUserDataDirectory()
+{
+ createDirectory(getUserGameDataDir(), "user data", PERMS_PRIVATE);
+}
+
+void SetFilePermissions(char *filename, int permission_class)
+{
+ int running_setgid = posix_process_running_setgid();
+ int perms = (permission_class == PERMS_PRIVATE ?
+ FILE_PERMS_PRIVATE : FILE_PERMS_PUBLIC);
+
+ if (permission_class == PERMS_PUBLIC && !running_setgid)
+ perms |= MODE_W_ALL;
+
+ chmod(filename, perms);
+}
+
+char *getCookie(char *file_type)
+{
+ static char cookie[MAX_COOKIE_LEN + 1];
+
+ if (strlen(program.cookie_prefix) + 1 +
+ strlen(file_type) + strlen("_FILE_VERSION_x.x") > MAX_COOKIE_LEN)
+ return "[COOKIE ERROR]"; /* should never happen */
+
+ sprintf(cookie, "%s_%s_FILE_VERSION_%d.%d",
+ program.cookie_prefix, file_type,
+ program.version_major, program.version_minor);
+
+ return cookie;
+}
+
+int getFileVersionFromCookieString(const char *cookie)
+{
+ const char *ptr_cookie1, *ptr_cookie2;
+ const char *pattern1 = "_FILE_VERSION_";
+ const char *pattern2 = "?.?";
+ const int len_cookie = strlen(cookie);
+ const int len_pattern1 = strlen(pattern1);
+ const int len_pattern2 = strlen(pattern2);
+ const int len_pattern = len_pattern1 + len_pattern2;
+ int version_major, version_minor;
+
+ if (len_cookie <= len_pattern)
+ return -1;
+
+ ptr_cookie1 = &cookie[len_cookie - len_pattern];
+ ptr_cookie2 = &cookie[len_cookie - len_pattern2];
+
+ if (strncmp(ptr_cookie1, pattern1, len_pattern1) != 0)
+ return -1;
+
+ if (ptr_cookie2[0] < '0' || ptr_cookie2[0] > '9' ||
+ ptr_cookie2[1] != '.' ||
+ ptr_cookie2[2] < '0' || ptr_cookie2[2] > '9')
+ return -1;
+
+ version_major = ptr_cookie2[0] - '0';
+ version_minor = ptr_cookie2[2] - '0';
+
+ return VERSION_IDENT(version_major, version_minor, 0, 0);
+}
+
+boolean checkCookieString(const char *cookie, const char *template)
+{
+ const char *pattern = "_FILE_VERSION_?.?";
+ const int len_cookie = strlen(cookie);
+ const int len_template = strlen(template);
+ const int len_pattern = strlen(pattern);
+
+ if (len_cookie != len_template)
+ return FALSE;
+
+ if (strncmp(cookie, template, len_cookie - len_pattern) != 0)
+ return FALSE;
+
+ return TRUE;
+}
+
+/* ------------------------------------------------------------------------- */
+/* setup file list and hash handling functions */
+/* ------------------------------------------------------------------------- */
+
+char *getFormattedSetupEntry(char *token, char *value)
+{
+ int i;
+ static char entry[MAX_LINE_LEN];
+
+ /* if value is an empty string, just return token without value */
+ if (*value == '\0')
+ return token;
+
+ /* start with the token and some spaces to format output line */
+ sprintf(entry, "%s:", token);
+ for (i = strlen(entry); i < token_value_position; i++)
+ strcat(entry, " ");
+
+ /* continue with the token's value */
+ strcat(entry, value);
+
+ return entry;
+}
+
+SetupFileList *newSetupFileList(char *token, char *value)
+{
+ SetupFileList *new = checked_malloc(sizeof(SetupFileList));
+
+ new->token = getStringCopy(token);
+ new->value = getStringCopy(value);
+
+ new->next = NULL;
+
+ return new;
+}
+
+void freeSetupFileList(SetupFileList *list)
+{
+ if (list == NULL)
+ return;
+
+ checked_free(list->token);
+ checked_free(list->value);
+
+ if (list->next)
+ freeSetupFileList(list->next);
+
+ free(list);
+}
+
+char *getListEntry(SetupFileList *list, char *token)
+{
+ if (list == NULL)
+ return NULL;
+
+ if (strEqual(list->token, token))
+ return list->value;
+ else
+ return getListEntry(list->next, token);
+}
+
+SetupFileList *setListEntry(SetupFileList *list, char *token, char *value)
+{
+ if (list == NULL)
+ return NULL;
+
+ if (strEqual(list->token, token))
+ {
+ checked_free(list->value);
+
+ list->value = getStringCopy(value);
+
+ return list;
+ }
+ else if (list->next == NULL)
+ return (list->next = newSetupFileList(token, value));
+ else
+ return setListEntry(list->next, token, value);
+}
+
+SetupFileList *addListEntry(SetupFileList *list, char *token, char *value)
+{
+ if (list == NULL)
+ return NULL;
+
+ if (list->next == NULL)
+ return (list->next = newSetupFileList(token, value));
+ else
+ return addListEntry(list->next, token, value);
+}
+
+#if 0
+#ifdef DEBUG
+static void printSetupFileList(SetupFileList *list)
+{
+ if (!list)
+ return;
+
+ printf("token: '%s'\n", list->token);
+ printf("value: '%s'\n", list->value);
+
+ printSetupFileList(list->next);
+}
+#endif
+#endif
+
+#ifdef DEBUG
+DEFINE_HASHTABLE_INSERT(insert_hash_entry, char, char);
+DEFINE_HASHTABLE_SEARCH(search_hash_entry, char, char);
+DEFINE_HASHTABLE_CHANGE(change_hash_entry, char, char);
+DEFINE_HASHTABLE_REMOVE(remove_hash_entry, char, char);
+#else
+#define insert_hash_entry hashtable_insert
+#define search_hash_entry hashtable_search
+#define change_hash_entry hashtable_change
+#define remove_hash_entry hashtable_remove
+#endif
+
+unsigned int get_hash_from_key(void *key)
+{
+ /*
+ djb2
+
+ This algorithm (k=33) was first reported by Dan Bernstein many years ago in
+ 'comp.lang.c'. Another version of this algorithm (now favored by Bernstein)
+ uses XOR: hash(i) = hash(i - 1) * 33 ^ str[i]; the magic of number 33 (why
+ it works better than many other constants, prime or not) has never been
+ adequately explained.
+
+ If you just want to have a good hash function, and cannot wait, djb2
+ is one of the best string hash functions i know. It has excellent
+ distribution and speed on many different sets of keys and table sizes.
+ You are not likely to do better with one of the "well known" functions
+ such as PJW, K&R, etc.
+
+ Ozan (oz) Yigit [http://www.cs.yorku.ca/~oz/hash.html]
+ */
+
+ char *str = (char *)key;
+ unsigned int hash = 5381;
+ int c;
+
+ while ((c = *str++))
+ hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
+
+ return hash;
+}
+
+static int keys_are_equal(void *key1, void *key2)
+{
+ return (strEqual((char *)key1, (char *)key2));
+}
+
+SetupFileHash *newSetupFileHash()
+{
+ SetupFileHash *new_hash =
+ create_hashtable(16, 0.75, get_hash_from_key, keys_are_equal);
+
+ if (new_hash == NULL)
+ Error(ERR_EXIT, "create_hashtable() failed -- out of memory");
+
+ return new_hash;
+}
+
+void freeSetupFileHash(SetupFileHash *hash)
+{
+ if (hash == NULL)
+ return;
+
+ hashtable_destroy(hash, 1); /* 1 == also free values stored in hash */
+}
+
+char *getHashEntry(SetupFileHash *hash, char *token)
+{
+ if (hash == NULL)
+ return NULL;
+
+ return search_hash_entry(hash, token);
+}
+
+void setHashEntry(SetupFileHash *hash, char *token, char *value)
+{
+ char *value_copy;
+
+ if (hash == NULL)
+ return;
+
+ value_copy = getStringCopy(value);
+
+ /* change value; if it does not exist, insert it as new */
+ if (!change_hash_entry(hash, token, value_copy))
+ if (!insert_hash_entry(hash, getStringCopy(token), value_copy))
+ Error(ERR_EXIT, "cannot insert into hash -- aborting");
+}
+
+char *removeHashEntry(SetupFileHash *hash, char *token)
+{
+ if (hash == NULL)
+ return NULL;
+
+ return remove_hash_entry(hash, token);
+}
+
+#if 0
+static void printSetupFileHash(SetupFileHash *hash)
+{
+ BEGIN_HASH_ITERATION(hash, itr)
+ {
+ printf("token: '%s'\n", HASH_ITERATION_TOKEN(itr));
+ printf("value: '%s'\n", HASH_ITERATION_VALUE(itr));
+ }
+ END_HASH_ITERATION(hash, itr)
+}
+#endif
+
+#define ALLOW_TOKEN_VALUE_SEPARATOR_BEING_WHITESPACE 1
+#define CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING 0
+#define CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH 0
+
+static boolean token_value_separator_found = FALSE;
+#if CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING
+static boolean token_value_separator_warning = FALSE;
+#endif
+#if CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH
+static boolean token_already_exists_warning = FALSE;
+#endif
+
+static boolean getTokenValueFromSetupLineExt(char *line,
+ char **token_ptr, char **value_ptr,
+ char *filename, char *line_raw,
+ int line_nr,
+ boolean separator_required)
+{
+ static char line_copy[MAX_LINE_LEN + 1], line_raw_copy[MAX_LINE_LEN + 1];
+ char *token, *value, *line_ptr;
+
+ /* when externally invoked via ReadTokenValueFromLine(), copy line buffers */
+ if (line_raw == NULL)
+ {
+ strncpy(line_copy, line, MAX_LINE_LEN);
+ line_copy[MAX_LINE_LEN] = '\0';
+ line = line_copy;
+
+ strcpy(line_raw_copy, line_copy);
+ line_raw = line_raw_copy;
+ }
+
+ /* cut trailing comment from input line */
+ for (line_ptr = line; *line_ptr; line_ptr++)
+ {
+ if (*line_ptr == '#')
+ {
+ *line_ptr = '\0';
+ break;
+ }
+ }
+
+ /* cut trailing whitespaces from input line */
+ for (line_ptr = &line[strlen(line)]; line_ptr >= line; line_ptr--)
+ if ((*line_ptr == ' ' || *line_ptr == '\t') && *(line_ptr + 1) == '\0')
+ *line_ptr = '\0';
+
+ /* ignore empty lines */
+ if (*line == '\0')
+ return FALSE;
+
+ /* cut leading whitespaces from token */
+ for (token = line; *token; token++)
+ if (*token != ' ' && *token != '\t')
+ break;
+
+ /* start with empty value as reliable default */
+ value = "";
+
+ token_value_separator_found = FALSE;
+
+ /* find end of token to determine start of value */
+ for (line_ptr = token; *line_ptr; line_ptr++)
+ {
+#if 1
+ /* first look for an explicit token/value separator, like ':' or '=' */
+ if (*line_ptr == ':' || *line_ptr == '=')
+#else
+ if (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == ':')
+#endif
+ {
+ *line_ptr = '\0'; /* terminate token string */
+ value = line_ptr + 1; /* set beginning of value */
+
+ token_value_separator_found = TRUE;
+
+ break;
+ }
+ }
+
+#if ALLOW_TOKEN_VALUE_SEPARATOR_BEING_WHITESPACE
+ /* fallback: if no token/value separator found, also allow whitespaces */
+ if (!token_value_separator_found && !separator_required)
+ {
+ for (line_ptr = token; *line_ptr; line_ptr++)
+ {
+ if (*line_ptr == ' ' || *line_ptr == '\t')
+ {
+ *line_ptr = '\0'; /* terminate token string */
+ value = line_ptr + 1; /* set beginning of value */
+
+ token_value_separator_found = TRUE;
+
+ break;
+ }
+ }
+
+#if CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING
+ if (token_value_separator_found)
+ {
+ if (!token_value_separator_warning)
+ {
+ Error(ERR_INFO_LINE, "-");
+
+ if (filename != NULL)
+ {
+ Error(ERR_WARN, "missing token/value separator(s) in config file:");
+ Error(ERR_INFO, "- config file: '%s'", filename);
+ }
+ else
+ {
+ Error(ERR_WARN, "missing token/value separator(s):");
+ }
+
+ token_value_separator_warning = TRUE;
+ }
+
+ if (filename != NULL)
+ Error(ERR_INFO, "- line %d: '%s'", line_nr, line_raw);
+ else
+ Error(ERR_INFO, "- line: '%s'", line_raw);
+ }
+#endif
+ }
+#endif
+
+ /* cut trailing whitespaces from token */
+ for (line_ptr = &token[strlen(token)]; line_ptr >= token; line_ptr--)
+ if ((*line_ptr == ' ' || *line_ptr == '\t') && *(line_ptr + 1) == '\0')
+ *line_ptr = '\0';
+
+ /* cut leading whitespaces from value */
+ for (; *value; value++)
+ if (*value != ' ' && *value != '\t')
+ break;
+
+#if 0
+ if (*value == '\0')
+ value = "true"; /* treat tokens without value as "true" */
+#endif
+
+ *token_ptr = token;
+ *value_ptr = value;
+
+ return TRUE;
+}
+
+boolean getTokenValueFromSetupLine(char *line, char **token, char **value)
+{
+ /* while the internal (old) interface does not require a token/value
+ separator (for downwards compatibility with existing files which
+ don't use them), it is mandatory for the external (new) interface */
+
+ return getTokenValueFromSetupLineExt(line, token, value, NULL, NULL, 0, TRUE);
+}
+
+#if 1
+static boolean loadSetupFileData(void *setup_file_data, char *filename,
+ boolean top_recursion_level, boolean is_hash)
+{
+ static SetupFileHash *include_filename_hash = NULL;
+ char line[MAX_LINE_LEN], line_raw[MAX_LINE_LEN], previous_line[MAX_LINE_LEN];
+ char *token, *value, *line_ptr;
+ void *insert_ptr = NULL;
+ boolean read_continued_line = FALSE;
+ FILE *file;
+ int line_nr = 0, token_count = 0, include_count = 0;
+
+#if CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING
+ token_value_separator_warning = FALSE;
+#endif
+
+#if CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH
+ token_already_exists_warning = FALSE;
+#endif
+
+ if (!(file = fopen(filename, MODE_READ)))
+ {
+ Error(ERR_WARN, "cannot open configuration file '%s'", filename);
+
+ return FALSE;
+ }
+
+ /* use "insert pointer" to store list end for constant insertion complexity */
+ if (!is_hash)
+ insert_ptr = setup_file_data;
+
+ /* on top invocation, create hash to mark included files (to prevent loops) */
+ if (top_recursion_level)
+ include_filename_hash = newSetupFileHash();
+
+ /* mark this file as already included (to prevent including it again) */
+ setHashEntry(include_filename_hash, getBaseNamePtr(filename), "true");
+
+ while (!feof(file))
+ {
+ /* read next line of input file */
+ if (!fgets(line, MAX_LINE_LEN, file))
+ break;
+
+ /* check if line was completely read and is terminated by line break */
+ if (strlen(line) > 0 && line[strlen(line) - 1] == '\n')
+ line_nr++;
+
+ /* cut trailing line break (this can be newline and/or carriage return) */
+ for (line_ptr = &line[strlen(line)]; line_ptr >= line; line_ptr--)
+ if ((*line_ptr == '\n' || *line_ptr == '\r') && *(line_ptr + 1) == '\0')
+ *line_ptr = '\0';
+
+ /* copy raw input line for later use (mainly debugging output) */
+ strcpy(line_raw, line);
+
+ if (read_continued_line)
+ {
+#if 0
+ /* !!! ??? WHY ??? !!! */
+ /* cut leading whitespaces from input line */
+ for (line_ptr = line; *line_ptr; line_ptr++)
+ if (*line_ptr != ' ' && *line_ptr != '\t')
+ break;
+#endif
+
+ /* append new line to existing line, if there is enough space */
+ if (strlen(previous_line) + strlen(line_ptr) < MAX_LINE_LEN)
+ strcat(previous_line, line_ptr);
+
+ strcpy(line, previous_line); /* copy storage buffer to line */
+
+ read_continued_line = FALSE;
+ }
+
+ /* if the last character is '\', continue at next line */
+ if (strlen(line) > 0 && line[strlen(line) - 1] == '\\')
+ {
+ line[strlen(line) - 1] = '\0'; /* cut off trailing backslash */
+ strcpy(previous_line, line); /* copy line to storage buffer */
+
+ read_continued_line = TRUE;
+
+ continue;
+ }
+
+ if (!getTokenValueFromSetupLineExt(line, &token, &value, filename,
+ line_raw, line_nr, FALSE))
+ continue;
+
+ if (*token)
+ {
+ if (strEqual(token, "include"))
+ {
+ if (getHashEntry(include_filename_hash, value) == NULL)
+ {
+ char *basepath = getBasePath(filename);
+ char *basename = getBaseName(value);
+ char *filename_include = getPath2(basepath, basename);
+
+#if 0
+ Error(ERR_INFO, "[including file '%s']", filename_include);
+#endif
+
+ loadSetupFileData(setup_file_data, filename_include, FALSE, is_hash);
+
+ free(basepath);
+ free(basename);
+ free(filename_include);
+
+ include_count++;
+ }
+ else
+ {
+ Error(ERR_WARN, "ignoring already processed file '%s'", value);
+ }
+ }
+ else
+ {
+ if (is_hash)
+ {
+#if CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH
+ char *old_value =
+ getHashEntry((SetupFileHash *)setup_file_data, token);
+
+ if (old_value != NULL)
+ {
+ if (!token_already_exists_warning)
+ {
+ Error(ERR_INFO_LINE, "-");
+ Error(ERR_WARN, "duplicate token(s) found in config file:");
+ Error(ERR_INFO, "- config file: '%s'", filename);
+
+ token_already_exists_warning = TRUE;
+ }
+
+ Error(ERR_INFO, "- token: '%s' (in line %d)", token, line_nr);
+ Error(ERR_INFO, " old value: '%s'", old_value);
+ Error(ERR_INFO, " new value: '%s'", value);
+ }
+#endif
+
+ setHashEntry((SetupFileHash *)setup_file_data, token, value);
+ }
+ else
+ {
+ insert_ptr = addListEntry((SetupFileList *)insert_ptr, token, value);
+ }
+
+ token_count++;
+ }
+ }
+ }
+
+ fclose(file);
+
+#if CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING
+ if (token_value_separator_warning)
+ Error(ERR_INFO_LINE, "-");
+#endif
+
+#if CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH
+ if (token_already_exists_warning)
+ Error(ERR_INFO_LINE, "-");
+#endif
+
+ if (token_count == 0 && include_count == 0)
+ Error(ERR_WARN, "configuration file '%s' is empty", filename);
+
+ if (top_recursion_level)
+ freeSetupFileHash(include_filename_hash);
+
+ return TRUE;
+}
+
+#else
+
+static boolean loadSetupFileData(void *setup_file_data, char *filename,
+ boolean top_recursion_level, boolean is_hash)
+{
+ static SetupFileHash *include_filename_hash = NULL;
+ char line[MAX_LINE_LEN], line_raw[MAX_LINE_LEN], previous_line[MAX_LINE_LEN];
+ char *token, *value, *line_ptr;
+ void *insert_ptr = NULL;
+ boolean read_continued_line = FALSE;
+ FILE *file;
+ int line_nr = 0;
+ int token_count = 0;
+
+#if CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING
+ token_value_separator_warning = FALSE;
+#endif
+
+ if (!(file = fopen(filename, MODE_READ)))
+ {
+ Error(ERR_WARN, "cannot open configuration file '%s'", filename);
+
+ return FALSE;
+ }
+
+ /* use "insert pointer" to store list end for constant insertion complexity */
+ if (!is_hash)
+ insert_ptr = setup_file_data;
+
+ /* on top invocation, create hash to mark included files (to prevent loops) */
+ if (top_recursion_level)
+ include_filename_hash = newSetupFileHash();
+
+ /* mark this file as already included (to prevent including it again) */
+ setHashEntry(include_filename_hash, getBaseNamePtr(filename), "true");
+
+ while (!feof(file))
+ {
+ /* read next line of input file */
+ if (!fgets(line, MAX_LINE_LEN, file))
+ break;
+
+ /* check if line was completely read and is terminated by line break */
+ if (strlen(line) > 0 && line[strlen(line) - 1] == '\n')
+ line_nr++;
+
+ /* cut trailing line break (this can be newline and/or carriage return) */
+ for (line_ptr = &line[strlen(line)]; line_ptr >= line; line_ptr--)
+ if ((*line_ptr == '\n' || *line_ptr == '\r') && *(line_ptr + 1) == '\0')
+ *line_ptr = '\0';
+
+ /* copy raw input line for later use (mainly debugging output) */
+ strcpy(line_raw, line);
+
+ if (read_continued_line)
+ {
+ /* cut leading whitespaces from input line */
+ for (line_ptr = line; *line_ptr; line_ptr++)
+ if (*line_ptr != ' ' && *line_ptr != '\t')
+ break;
+
+ /* append new line to existing line, if there is enough space */
+ if (strlen(previous_line) + strlen(line_ptr) < MAX_LINE_LEN)
+ strcat(previous_line, line_ptr);
+
+ strcpy(line, previous_line); /* copy storage buffer to line */
+
+ read_continued_line = FALSE;
+ }
+
+ /* if the last character is '\', continue at next line */
+ if (strlen(line) > 0 && line[strlen(line) - 1] == '\\')
+ {
+ line[strlen(line) - 1] = '\0'; /* cut off trailing backslash */
+ strcpy(previous_line, line); /* copy line to storage buffer */
+
+ read_continued_line = TRUE;
+
+ continue;
+ }
+
+ /* cut trailing comment from input line */
+ for (line_ptr = line; *line_ptr; line_ptr++)
+ {
+ if (*line_ptr == '#')
+ {
+ *line_ptr = '\0';
+ break;
+ }
+ }
+
+ /* cut trailing whitespaces from input line */
+ for (line_ptr = &line[strlen(line)]; line_ptr >= line; line_ptr--)
+ if ((*line_ptr == ' ' || *line_ptr == '\t') && *(line_ptr + 1) == '\0')
+ *line_ptr = '\0';
+
+ /* ignore empty lines */
+ if (*line == '\0')
+ continue;
+
+ /* cut leading whitespaces from token */
+ for (token = line; *token; token++)
+ if (*token != ' ' && *token != '\t')
+ break;
+
+ /* start with empty value as reliable default */
+ value = "";
+
+ token_value_separator_found = FALSE;
+
+ /* find end of token to determine start of value */
+ for (line_ptr = token; *line_ptr; line_ptr++)
+ {
+#if 1
+ /* first look for an explicit token/value separator, like ':' or '=' */
+ if (*line_ptr == ':' || *line_ptr == '=')
+#else
+ if (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == ':')
+#endif
+ {
+ *line_ptr = '\0'; /* terminate token string */
+ value = line_ptr + 1; /* set beginning of value */
+
+ token_value_separator_found = TRUE;
+
+ break;
+ }
+ }
+
+#if ALLOW_TOKEN_VALUE_SEPARATOR_BEING_WHITESPACE
+ /* fallback: if no token/value separator found, also allow whitespaces */
+ if (!token_value_separator_found)
+ {
+ for (line_ptr = token; *line_ptr; line_ptr++)
+ {
+ if (*line_ptr == ' ' || *line_ptr == '\t')
+ {
+ *line_ptr = '\0'; /* terminate token string */
+ value = line_ptr + 1; /* set beginning of value */
+
+ token_value_separator_found = TRUE;
+
+ break;
+ }
+ }
+
+#if CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING
+ if (token_value_separator_found)
+ {
+ if (!token_value_separator_warning)
+ {
+ Error(ERR_INFO_LINE, "-");
+ Error(ERR_WARN, "missing token/value separator(s) in config file:");
+ Error(ERR_INFO, "- config file: '%s'", filename);
+
+ token_value_separator_warning = TRUE;
+ }
+
+ Error(ERR_INFO, "- line %d: '%s'", line_nr, line_raw);
+ }
+#endif
+ }
+#endif
+
+ /* cut trailing whitespaces from token */
+ for (line_ptr = &token[strlen(token)]; line_ptr >= token; line_ptr--)
+ if ((*line_ptr == ' ' || *line_ptr == '\t') && *(line_ptr + 1) == '\0')
+ *line_ptr = '\0';
+
+ /* cut leading whitespaces from value */
+ for (; *value; value++)
+ if (*value != ' ' && *value != '\t')
+ break;
+
+#if 0
+ if (*value == '\0')
+ value = "true"; /* treat tokens without value as "true" */