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
|
/* flags.c -- Flag support for Khepera
* Created: Sat Mar 23 10:11:52 1996 by faith@dict.org
* Copyright 1994-1997, 2002 Rickard E. Faith (faith@dict.org)
* Copyright 2002-2008 Aleksey Cheusov (vle@gmx.net)
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* \section{Flag Support}
*
* \intro These routines provide low-level support for run-time program
* flow control. The mechanism used is similar to that used for debugging
* messages. A set of global flags are maintained using "#define"
* statements. These flags are assumed to be 32-bit integers, the top two
* bits of which are used to select one of four sets of debugging lags.
* Each set, therefore, has 30 bits of flag information. For convenience,
* each flag can be given a unique name, so that flags can be set easily
* with command-line options.
* */
#include "maaP.h"
#define TEST(flags,var) (((flags)>>31) \
? (((flags)>>30) \
? ((var[3] & (flags)) << 2) \
: ((var[2] & (flags)) << 2)) \
: (((flags)>>30) \
? ((var[1] & (flags)) << 2) \
: ((var[0] & (flags)) << 2)))
static hsh_HashTable hash;
static flg_Type setFlags[4];
static flg_Type usedFlags[4];
/* |_flg_exists| returns non-zero if |flag| has been associated with a name
(using the |flg_register| function). */
static int _flg_exists(flg_Type flag)
{
return TEST(flag, usedFlags);
}
/* |flg_name| returns a pointer to the name that was associated with the
|flag|. */
const char *flg_name(flg_Type flag)
{
hsh_Position position;
void *key;
void *datum;
HSH_ITERATE(hash, position, key, datum) {
if (flag == (flg_Type)datum) {
HSH_ITERATE_END(hash);
return key;
}
}
return "unknown flag";
}
/* \doc |flg_register| is used to set up an asociated between a |flag| and
a |name|. After this association is made, calls to |flg_set| can use
|name| to set the global flag. */
void flg_register(flg_Type flag, const char *name)
{
flg_Type tmp;
for (tmp = flag & 0x3fffffff; tmp && !(tmp & 1); tmp >>= 1);
if (!tmp || tmp >> 1)
err_fatal(__func__,
"Malformed flag (%lx):"
" a single low-order bit must be set",
flag);
if (!hash) hash = hsh_create(NULL, NULL);
if (_flg_exists(flag))
err_fatal(__func__,
"The flag %lx has been used for \"%s\""
" and cannot be reused for \"%s\"",
flag,
flg_name(flag),
name);
hsh_insert(hash, name, (void *)flag);
}
/* \doc |flg_set| sets the |name| flag. If |name| is ``none,'' then all
flags are cleared. */
void flg_set(const char *name)
{
flg_Type flag;
if (!name) err_internal(__func__, "name is NULL");
if (!hash) err_fatal(__func__, "No flag names registered");
if (!strcmp(name, "none")) {
setFlags[0] = setFlags[1] = setFlags[2] = setFlags[3] = 0;
return;
}
if (!strcmp(name, "all")) {
setFlags[0] = setFlags[1] = setFlags[2] = setFlags[3] = ~0;
return;
}
if (!(flag = (flg_Type)hsh_retrieve(hash, name))) {
flag = 0;
if (
(*name != '-' && *name != '+') ||
!(flag = (flg_Type) hsh_retrieve(hash, name+1)))
{
fprintf(stderr, "Valid flags are:\n");
flg_list(stderr);
err_fatal(__func__,
"\"%s\" is not a valid flag",
name);
} else {
if (flag){
if (*name == '+') setFlags[ flag >> 30 ] |= flag;
else setFlags[ flag >> 30 ] &= ~flag; /* - */
}
}
} else {
setFlags[ flag >> 30 ] |= flag;
}
}
/* \doc This function tests the |flag|, returning non-zero if the
|flag| is set, and zero otherwise. */
int flg_test(flg_Type flag)
{
return TEST(flag, setFlags);
}
/* \doc |flg_destroy| destroys the memory associated with the flag support
routines. This routine should \emph{never} be called by the programmer:
it is automatically called at program termination on systems that
support the |atexit| or |on_exit| calls. */
void flg_destroy(void)
{
if (hash) hsh_destroy(hash);
hash = NULL;
setFlags[0] = setFlags[1] = setFlags[2] = setFlags[3] = 0;
usedFlags[0] = usedFlags[1] = usedFlags[2] = usedFlags[3] = 0;
}
static int _flg_user(const void *key, const void *datum, void *arg)
{
FILE *stream = (FILE *)arg;
fprintf(stream, " %s\n", (char*) __UNCONST(key));
return 0;
}
/* |flg_list| lists all of the valid flags to the specified |stream|. */
void flg_list(FILE *stream)
{
hsh_iterate_arg(hash, _flg_user, stream);
}
|