/* * mod_aolserver aolserver emulation --- Copyright 2000 Robert S. Thau. * This file derived from the actual aolserver code, and is distributed * in accord with its license, as follows: * * The contents of this file are subject to the AOLserver Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://aolserver.lcs.mit.edu/. * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. * * The Original Code is AOLserver Code and related documentation * distributed by AOL. * * The Initial Developer of the Original Code is America Online, * Inc. Portions created by AOL are Copyright (C) 1999 America Online, * Inc. All Rights Reserved. * * Alternatively, the contents of this file may be used under the terms * of the GNU General Public License (the "GPL"), in which case the * provisions of GPL are applicable instead of those above. If you wish * to allow use of your version of this file only under the terms of the * GPL and not to allow others to use your version of this file under the * License, indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by the GPL. * If you do not delete the provisions above, a recipient may use your * version of this file under either the License or the GPL. */ /* * config.c -- * * Support for the configuration file * Most of this is in tcl in mod_aolserver. */ #include "nsd.h" #define ISSLASH(c) ((c) == '/' || (c) == '\\') /* *========================================================================== * API functions *========================================================================== */ /* *---------------------------------------------------------------------- * * Ns_ConfigGet -- * * Return a config file value for a given key * * Results: * ASCIIZ ptr to a value * * Side effects: * None. * *---------------------------------------------------------------------- */ /* Strings returned from this function will be valid until the next * restart, *if* called during config phase (as it always is... so far). * If this is called while serving requests, the string only survives * for the duration of this request; treating it as more permanent will * cause the universe to collapse. */ char * Ns_ConfigGet(char *section, char *key) { Tcl_DString cmd; char *val; Tcl_DStringInit (&cmd); Tcl_DStringAppendElement (&cmd, "ns_config"); Tcl_DStringAppendElement (&cmd, section); Tcl_DStringAppendElement (&cmd, key); if (NsTclEval (aolcmd_interp, Tcl_DStringValue (&cmd)) == TCL_ERROR) { Ns_ModLog (Error, "config get", "%s got error %s", Tcl_DStringValue (&cmd), Tcl_GetVar (aolcmd_interp, "errorInfo", TCL_GLOBAL_ONLY)); val = NULL; } else { val = ap_pstrdup (TCL_POOL(), aolcmd_interp->result); } Tcl_DStringFree (&cmd); return val; } /* *---------------------------------------------------------------------- * * Ns_ConfigGetInt -- * * Fetch integer config values * * Results: * S_TRUE if it found an integer value; otherwise, it returns * NS_FALSE and sets the value to 0 * * Side effects: * The integer value is returned by reference * *---------------------------------------------------------------------- */ int Ns_ConfigGetInt(char *section, char *key, int *valuePtr) { char *s; s = Ns_ConfigGet(section, key); if (s == NULL || *s == '\0') { return NS_FALSE; } else if (sscanf(s, "%d", valuePtr) != 1) { Ns_Log(Warning, "config: could not convert [%s]%s=\"%s\" to int", section, key, s); return NS_FALSE; } return NS_TRUE; } /* *---------------------------------------------------------------------- * * Ns_ConfigGetBool -- * * Get a boolean config value. There are many ways to represent * a boolean value. * * Results: * NS_TRUE/NS_FALSE * * Side effects: * The boolean value is returned by reference * *---------------------------------------------------------------------- */ int Ns_ConfigGetBool(char *section, char *key, int *valuePtr) { char *s; s = Ns_ConfigGet(section, key); if (s == NULL) { return NS_FALSE; } if (STREQ(s, "1") || STRIEQ(s, "y") || STRIEQ(s, "yes") || STRIEQ(s, "on") || STRIEQ(s, "t") || STRIEQ(s, "true")) { *valuePtr = 1; } else if (STREQ(s, "0") || STRIEQ(s, "n") || STRIEQ(s, "no") || STRIEQ(s, "off") || STRIEQ(s, "f") || STRIEQ(s, "false")) { *valuePtr = 0; } else if (sscanf(s, "%d", valuePtr) != 1) { Ns_Log(Warning, "config: could not convert [%s]%s=\"%s\" to bool", section, key, s); return NS_FALSE; } return NS_TRUE; } /* *---------------------------------------------------------------------- * * Ns_ConfigGetPath -- * * Get the full name of a config file section if it exists. * * Results: * A pointer to an ASCIIZ string of the full path name, or NULL * if that path is not in the config file. * * Side effects: * None. * *---------------------------------------------------------------------- */ char * Ns_ConfigGetPath(char *server, char *module, ...) { va_list ap; char *s; Ns_DString ds; Ns_DStringInit(&ds); Ns_DStringAppend(&ds, "ns"); if (server != NULL) { Ns_DStringVarAppend(&ds, "/server/", server, NULL); } if (module != NULL) { Ns_DStringVarAppend(&ds, "/module/", module, NULL); } va_start(ap, module); while ((s = va_arg(ap, char *)) != NULL) { Ns_DStringAppend(&ds, "/"); while (*s != '\0' && ISSLASH(*s)) { ++s; } Ns_DStringAppend(&ds, s); while (ISSLASH(ds.string[ds.length - 1])) { ds.string[--ds.length] = '\0'; } } va_end(ap); /* XXX leak storage for now */ s = Ns_DStringExport (&ds); Ns_DStringFree(&ds); return s; }