| 1 |
/* $Id$ */ |
| 2 |
/* |
| 3 |
* Copyright (c) 1990-1996 Sam Leffler |
| 4 |
* Copyright (c) 1991-1996 Silicon Graphics, Inc. |
| 5 |
* HylaFAX is a trademark of Silicon Graphics |
| 6 |
* |
| 7 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 8 |
* its documentation for any purpose is hereby granted without fee, provided |
| 9 |
* that (i) the above copyright notices and this permission notice appear in |
| 10 |
* all copies of the software and related documentation, and (ii) the names of |
| 11 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 12 |
* publicity relating to the software without the specific, prior written |
| 13 |
* permission of Sam Leffler and Silicon Graphics. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 16 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 17 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 18 |
* |
| 19 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 20 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 21 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 22 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 23 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 24 |
* OF THIS SOFTWARE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/* |
| 28 |
* HylaFAX Server Configuration Base Class. |
| 29 |
*/ |
| 30 |
#include "FaxConfig.h" |
| 31 |
#include "Str.h" |
| 32 |
|
| 33 |
#include <ctype.h> |
| 34 |
#include <pwd.h> |
| 35 |
|
| 36 |
#include "Sys.h" |
| 37 |
#include "NLS.h" |
| 38 |
|
| 39 |
|
| 40 |
FaxConfig::FaxConfig() |
| 41 |
{ |
| 42 |
lineno = 0; |
| 43 |
lastModTime = 0; |
| 44 |
} |
| 45 |
FaxConfig::FaxConfig(const FaxConfig& other) |
| 46 |
{ |
| 47 |
lineno = other.lineno; |
| 48 |
lastModTime = other.lastModTime; |
| 49 |
} |
| 50 |
FaxConfig::~FaxConfig() {} |
| 51 |
|
| 52 |
void |
| 53 |
FaxConfig::readConfig(const fxStr& filename) |
| 54 |
{ |
| 55 |
FILE* fd = Sys::fopen(tildeExpand(filename), "r"); |
| 56 |
if (fd) { |
| 57 |
configTrace(NLS::TEXT("Read config file %s"), (const char*) filename); |
| 58 |
char line[1024]; |
| 59 |
while (fgets(line, sizeof (line)-1, fd)){ |
| 60 |
size_t len = strlen(line); |
| 61 |
// The last line of a file may not be terminated by a '\n'. |
| 62 |
if (line[len-1] == '\n') |
| 63 |
line[len-1] = '\0'; // Nuke \n at end of line |
| 64 |
(void) readConfigItem(line); |
| 65 |
} |
| 66 |
fclose(fd); |
| 67 |
} |
| 68 |
} |
| 69 |
void FaxConfig::resetConfig() { lineno = 0; } |
| 70 |
|
| 71 |
bool |
| 72 |
FaxConfig::updateConfig(const fxStr& filename) |
| 73 |
{ |
| 74 |
struct stat sb; |
| 75 |
fxStr path(tildeExpand(filename)); |
| 76 |
if (Sys::stat(path, sb) == 0 && sb.st_mtime > lastModTime) { |
| 77 |
resetConfig(); |
| 78 |
readConfig(path); |
| 79 |
lastModTime = sb.st_mtime; |
| 80 |
return (true); |
| 81 |
} else |
| 82 |
return (false); |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* Expand a filename that might have `~' in it. |
| 87 |
*/ |
| 88 |
fxStr |
| 89 |
FaxConfig::tildeExpand(const fxStr& filename) |
| 90 |
{ |
| 91 |
fxStr path(filename); |
| 92 |
if (filename.length() > 1 && filename[0] == '~') { |
| 93 |
path.remove(0); |
| 94 |
const char* cp = getenv("HOME"); |
| 95 |
if (!cp || *cp == '\0') { |
| 96 |
struct passwd* pwd = getpwuid(getuid()); |
| 97 |
if (!pwd) { |
| 98 |
configError(NLS::TEXT("No passwd file entry for uid %u," |
| 99 |
" cannot expand ~ in \"%s\""), |
| 100 |
getuid(), (const char*) filename); |
| 101 |
cp = ""; // NB: XXX maybe this should be fatal? |
| 102 |
} else |
| 103 |
cp = pwd->pw_dir; |
| 104 |
} |
| 105 |
path.insert(cp); |
| 106 |
} |
| 107 |
return (path); |
| 108 |
} |
| 109 |
|
| 110 |
bool |
| 111 |
FaxConfig::findTag(const char* tag, const void* names0, u_int n, u_int& ix) |
| 112 |
{ |
| 113 |
const tags* names = (const tags*) names0; |
| 114 |
|
| 115 |
for (int i = n-1; i >= 0; i--) { |
| 116 |
const char* cp = names[i].name; |
| 117 |
if (cp[0] == tag[0] && streq(cp, tag)) { |
| 118 |
ix = i; |
| 119 |
return (true); |
| 120 |
} |
| 121 |
} |
| 122 |
return (false); |
| 123 |
} |
| 124 |
|
| 125 |
bool |
| 126 |
FaxConfig::findValue(const char* value, const char* values[], u_int n, u_int& ix) |
| 127 |
{ |
| 128 |
for (u_int i = 0; i < n; i++) { |
| 129 |
const char* cp = values[i]; |
| 130 |
if (cp[0] == value[0] && streq(cp, value)) { |
| 131 |
ix = i; |
| 132 |
return (true); |
| 133 |
} |
| 134 |
} |
| 135 |
return (false); |
| 136 |
} |
| 137 |
|
| 138 |
int |
| 139 |
FaxConfig::getNumber(const char* s) |
| 140 |
{ |
| 141 |
return ((int) strtol(s, NULL, 0)); |
| 142 |
} |
| 143 |
|
| 144 |
#define valeq(a,b) (strcasecmp(a,b)==0) |
| 145 |
|
| 146 |
bool |
| 147 |
FaxConfig::getBoolean(const char* cp) |
| 148 |
{ |
| 149 |
return (valeq(cp, "on") || valeq(cp, "yes") || valeq(cp, "true")); |
| 150 |
} |
| 151 |
|
| 152 |
bool |
| 153 |
FaxConfig::readConfigItem(const char* b) |
| 154 |
{ |
| 155 |
char buf[2048]; |
| 156 |
char* cp; |
| 157 |
|
| 158 |
lineno++; |
| 159 |
strncpy(buf, b, sizeof (buf)); |
| 160 |
for (cp = buf; isspace(*cp); cp++) // skip leading white space |
| 161 |
; |
| 162 |
if (*cp == '#' || *cp == '\0') |
| 163 |
return (true); |
| 164 |
const char* tag = cp; // start of tag |
| 165 |
while (*cp && *cp != ':') { // skip to demarcating ':' |
| 166 |
if (isupper(*cp)) |
| 167 |
*cp = tolower(*cp); |
| 168 |
cp++; |
| 169 |
} |
| 170 |
if (*cp != ':') { |
| 171 |
configError(NLS::TEXT("Syntax error at line %u, missing ':' in \"%s\""), |
| 172 |
lineno, b); |
| 173 |
return (false); |
| 174 |
} |
| 175 |
for (*cp++ = '\0'; isspace(*cp); cp++) // skip white space again |
| 176 |
; |
| 177 |
const char* value; |
| 178 |
if (*cp == '"') { // "..." value |
| 179 |
int c; |
| 180 |
/* |
| 181 |
* Parse quoted string and deal with \ escapes. |
| 182 |
*/ |
| 183 |
char* dp = ++cp; |
| 184 |
for (value = dp; (c = *cp) != '"'; cp++) { |
| 185 |
if (c == '\0') { // unmatched quote mark |
| 186 |
configError(NLS::TEXT("Syntax error at line %u, missing quote mark in \"%s\""), |
| 187 |
lineno, b); |
| 188 |
return (false); |
| 189 |
} |
| 190 |
if (c == '\\') { |
| 191 |
c = *++cp; |
| 192 |
if (isdigit(c)) { // \nnn octal escape |
| 193 |
int v = c - '0'; |
| 194 |
if (isdigit(c = cp[1])) { |
| 195 |
cp++, v = (v << 3) + (c - '0'); |
| 196 |
if (isdigit(c = cp[1])) |
| 197 |
cp++, v = (v << 3) + (c - '0'); |
| 198 |
} |
| 199 |
c = v; |
| 200 |
} else { // \<char> escapes |
| 201 |
for (const char* tp = "n\nt\tr\rb\bf\fv\013"; *tp; tp += 2) |
| 202 |
if (c == tp[0]) { |
| 203 |
c = tp[1]; |
| 204 |
break; |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
*dp++ = c; |
| 209 |
} |
| 210 |
*dp = '\0'; |
| 211 |
} else { // value up to 1st non-ws |
| 212 |
for (value = cp; *cp && !isspace(*cp); cp++) |
| 213 |
; |
| 214 |
*cp = '\0'; |
| 215 |
} |
| 216 |
|
| 217 |
if (streq(tag, "include") ) { |
| 218 |
u_int old_lineno = lineno; |
| 219 |
configTrace(NLS::TEXT("%s = %s (line %u)"), tag, value, lineno); |
| 220 |
lineno = 0; |
| 221 |
readConfig(value); |
| 222 |
lineno = old_lineno; |
| 223 |
return (true); |
| 224 |
} |
| 225 |
if (!setConfigItem(tag, value)) { |
| 226 |
configTrace(NLS::TEXT("Unknown configuration parameter \"%s\" ignored at line %u"), |
| 227 |
tag, lineno); |
| 228 |
return (false); |
| 229 |
} else { |
| 230 |
configTrace(NLS::TEXT("%s = %s (line %u)"), tag, value, lineno); |
| 231 |
return (true); |
| 232 |
} |
| 233 |
} |