1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
/*
* KON2 - Kanji ON Console -
* Copyright (C) 1992-1996 Takashi MANABE (manabe@papilio.tutics.tut.ac.jp)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY TAKASHI MANABE ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <getcap.h>
#define BUF_SIZE 1024
#define MAX_CAPS 20
static struct capability {
char *name; /* Name of capability label */
initializer *func; /* Function to perform configuration */
int initialized; /* 1 if initialized */
char *arg; /* Command line argument for this capability. */
char *def_value; /* Default value. NULL means required entry. */
} cap[MAX_CAPS];
static struct capability *FindCap(const char *name)
{
int i;
struct capability *cp;
for (i = 0, cp = cap; i < MAX_CAPS; i++, cp++) {
if ((cp->name != NULL) && (strcasecmp(name, cp->name) == 0)) {
return cp;
}
}
return NULL;
}
/* Define initializer function func for capability name. If def_value is nil,
then the entry of the name must exist in configuration file. An error is
flagged if no entry is found. If def_value is non-nil and no entry is found
in configuration file, then func is invoked with def_value. */
void DefineCap(const char *name, initializer *func, const char *def_value)
{
int i;
struct capability *cp;
/* Pass 1 -- try to replace same name entry if exists. */
if ((cp = FindCap(name)) != NULL) {
#ifdef DEBUG
fprintf(stderr, "cap %s redefined (default %s)\r\n", name,
def_value ? def_value : "None");
#endif
/* release previous value - ukai */
if (cp->name)
free(cp->name);
if (cp->def_value)
free(cp->def_value);
cp->name = strdup(name);
cp->func = func;
if (def_value)
cp->def_value = strdup(def_value);
return;
}
/* Pass 2 -- fine empty slot and insert new entry. */
for (i = 0, cp = cap; i < MAX_CAPS; i++, cp++) {
if (cp->name == NULL) {
#ifdef DEBUG
fprintf(stderr, "cap %s defined (default %s)\r\n", name,
def_value ? def_value : "None");
#endif
cp->name = strdup(name);
cp->func = func;
if (def_value)
cp->def_value = strdup(def_value);
return;
}
}
fprintf(stderr, "Fatal: internal error - can't find room for capability `%s'\r\r\n", name);
abort();
}
/* Delete all initializer functions. */
void CapInit(void)
{
struct capability *cp;
int i;
for (i = 0, cp = cap; i < MAX_CAPS; i++, cp++) {
#ifdef DEBUG
if (cp->name)
fprintf(stderr, "cap %s deleted\r\n", cp->name);
#endif
cp->initialized = 0;
if (cp->name)
free(cp->name);
if (cp->arg)
free(cp->arg);
if (cp->def_value)
free(cp->def_value);
cp->name = cp->arg = cp->def_value = NULL;
}
}
static const char label_delim[] = ":,; \t\n";
/* Read configuration file named filename and invoke initializer function for each entry. */
int ReadConfig(const char *filename)
{
FILE *capFp;
char line[MAX_COLS], *p;
char buf[BUF_SIZE];
struct capability *c;
int i;
int errors = 0; /* number of errors during configuration */
if ((capFp = fopen(filename, "r")) == NULL) {
fprintf(stderr, "Error: can't open config file\r\r\n");
perror(filename);
return FAILURE;
}
while(fgets(line, MAX_COLS, capFp) != NULL) {
nextLabel:
if ((p = strchr(line, '#')) != NULL)
*p = '\0';
if (strchr(line, ':') == NULL) continue; /* not a lebel */
for (p = strtok(line, label_delim); p != NULL; p = strtok(NULL, label_delim)) {
/* Process one label line. */
if ((c = FindCap(p)) != NULL) {
/* Found matching capability. Get body from file. */
char *l = buf;
while (fgets(line, MAX_COLS, capFp) != NULL
&& line[0] == '\t') {
char *l2 = line;
while (*l2 != '\n' && *l2 != '#') {
*l++ = *l2++;
}
*l++ = '\n';
}
*l = '\0';
if (! c->initialized) {
/* do initialize */
if (c->arg) {
#ifdef DEBUG
fprintf(stderr, "Capability %s set to arg %s\r\n",
c->name, c->arg);
#endif
if (c->func(c->arg) < 0)
errors++;
} else {
#ifdef DEBUG
fprintf(stderr, "Capability %s set to %s",
c->name, buf);
#endif
if (c->func(buf) < 0)
errors++;
}
c->initialized = 1;
}
goto nextLabel; /* next line already read */
}
}
}
/* Default initialization for unspecified capability. */
for (i = 0, c = cap; i < MAX_CAPS; i++, c++) {
if ((c->name != NULL) && !c->initialized) {
if (c->arg) {
#ifdef DEBUG
fprintf(stderr, "Capability %s defaults to arg %s\r\n", c->name, c->arg);
#endif
if (c->func(c->arg) < 0)
errors++;
} else if (c->def_value) {
#ifdef DEBUG
fprintf(stderr, "Capability %s defaults to %s\r\n", c->name, c->def_value);
#endif
if (c->func(c->def_value) < 0)
errors++;
} else {
fprintf(stderr, "Error: entry for capability `%s' not found\r\r\n", c->name);
errors++;
}
}
}
fclose(capFp);
#ifdef DEBUG
fprintf(stderr, "Finished reading config file\r\n");
#endif
if (errors)
return FAILURE;
else
return SUCCESS;
}
/* Set value for capability capName. */
int SetCapArg(const char *capName, const char *value)
{
struct capability *cp;
if ((cp = FindCap(capName)) == NULL) {
return FAILURE;
}
if (cp->def_value == NULL) {
/* Protected capability. */
return FAILURE;
}
/* release previous value - ukai */
if (cp->arg)
free(cp->arg);
cp->arg = strdup(value);
#ifdef DEBUG
fprintf(stderr, "Setting arg for %s to %s\r\n", capName, value);
#endif
return SUCCESS;
}
/* Utility function that return 1 if confstr is "On" and 0 if "OFF". */
bool BoolConf(const char *confstr)
{
char *name;
name = malloc(strlen(confstr) + 1);
sscanf(confstr, "%s", name); /* its safe because name[strlen(confstr)+1] */
if (strcasecmp(name, "On") == 0 ||
strcasecmp(name, "True") == 0) {
return TRUE;
} else if (strcasecmp(name, "Off") != 0 &&
strcasecmp(name, "False") != 0) {
fprintf(stderr, "Warning: value `%s' unrecognized as boolean; assuming `Off'\r\r\n",
name);
}
return FALSE;
}
|