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
|
/*
* debugFlags.c --
*
* Debugging module.
* The debugging module provides a standard collection of
* procedures for setting, examining, and testing debugging flags.
*
* *********************************************************************
* * Copyright (C) 1985, 1990 Regents of the University of California. *
* * Permission to use, copy, modify, and distribute this *
* * software and its documentation for any purpose and without *
* * fee is hereby granted, provided that the above copyright *
* * notice appear in all copies. The University of California *
* * makes no representations about the suitability of this *
* * software for any purpose. It is provided "as is" without *
* * express or implied warranty. Export of this software outside *
* * of the United States of America may require an export license. *
* *********************************************************************
*/
#ifndef lint
static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-8.0/debug/debugFlags.c,v 1.1.1.1 2008/02/03 20:43:50 tim Exp $";
#endif /* not lint */
#include <stdio.h>
#include "utils/magic.h"
#include "debug/debug.h"
#include "textio/textio.h"
#include "utils/malloc.h"
#include "utils/utils.h"
struct debugClient debugClients[MAXDEBUGCLIENTS];
int debugNumClients = 0;
/*
* ----------------------------------------------------------------------------
*
* DebugAddClient --
*
* Add a client to the debugging module.
* The argument 'name' is used to identify the client, and the
* argument 'maxflags' indicates the maximum number of flags
* that will be added for that client.
*
* Results:
* Returns a word of ClientData that identifies the
* client just added. This word must be passed to
* DebugAddFlag, DebugSet(), or DebugShow() to identify
* the client being referred to.
*
* Side effects:
* Updates the list of known debugging clients.
*
* ----------------------------------------------------------------------------
*/
ClientData
DebugAddClient(name, maxflags)
char *name;
int maxflags;
{
struct debugClient *dc;
if (debugNumClients >= MAXDEBUGCLIENTS)
{
TxError("No room for debugging client '%s'.\n", name);
TxError("Maximum number of clients is %d\n", MAXDEBUGCLIENTS);
return ((ClientData) (MAXDEBUGCLIENTS-1));
}
dc = &debugClients[debugNumClients];
dc->dc_name = name;
dc->dc_maxflags = maxflags;
dc->dc_nflags = 0;
dc->dc_flags = (struct debugFlag *) mallocMagic((unsigned)
(sizeof (struct debugFlag) * maxflags));
while (--maxflags > 0)
{
dc->dc_flags[maxflags].df_name = (char *) NULL;
dc->dc_flags[maxflags].df_value = FALSE;
}
return ((ClientData) debugNumClients++);
}
/*
* ----------------------------------------------------------------------------
*
* DebugAddFlag --
*
* Add a debugging flag for a particular client.
* This flag can be set when DebugSet() is called with 'clientID',
* and will appear in the display of DebugShow().
*
* WARNING:
* The order in which flags appear for purposes of setting them
* with DebugSet(), and when being displayed with DebugShow(),
* will be the same as the order in which they are passed to
* DebugAddFlag(). To make LookupStruct() work best for DebugSet(),
* the flag names should be ordered monotonically.
*
* Results:
* Returns the index of the debugging flag in the array
* debugFlags[].
*
* Side effects:
* Updates the array debugFlags[].
*
* ----------------------------------------------------------------------------
*/
int
DebugAddFlag(clientID, name)
ClientData clientID; /* Client identifier from DebugAddClient */
char *name; /* Name of debugging flag */
{
int id = (int) clientID;
struct debugClient *dc;
if (id < 0 || id >= debugNumClients)
{
TxError("DebugAddFlag: bad client id %d (flag %s)\n", clientID, name);
return (0);
}
dc = &debugClients[id];
if (dc->dc_nflags >= dc->dc_maxflags)
{
TxError("Too many flags for client %s (maximum was set to %d)\n",
dc->dc_name, dc->dc_maxflags);
return (dc->dc_nflags);
}
dc->dc_flags[dc->dc_nflags].df_name = name;
dc->dc_flags[dc->dc_nflags].df_value = FALSE;
return (dc->dc_nflags++);
}
/*
* ----------------------------------------------------------------------------
*
* DebugShow --
*
* Show all the debugging flags and their values for a particular
* client.
*
* Results:
* None.
*
* Side effects:
* Writes to the terminal.
*
* ----------------------------------------------------------------------------
*/
void
DebugShow(clientID)
ClientData clientID;
{
int id = (int) clientID;
struct debugClient *dc;
int n;
if (id < 0 || id >= debugNumClients)
{
TxError("DebugShow: bad client id %d\n", clientID);
return;
}
dc = &debugClients[id];
for (n = 0; n < dc->dc_nflags; n++)
TxPrintf("%-5.5s %s\n", dc->dc_flags[n].df_value ? "TRUE" : "FALSE",
dc->dc_flags[n].df_name);
}
/*
* ----------------------------------------------------------------------------
*
* DebugSet --
*
* Allow debugging flags to be set or cleared for the client 'clientID'.
* The argument 'argv' contains an array of 'argc' string pointers,
* each of which is the name of a flag that will be set to 'value'
* (either TRUE or FALSE).
*
* Results:
* None.
*
* Side effects:
* Updates the debugging flags specified in (argc, argv).
* Will complain about any unrecognized flag names.
*
* ----------------------------------------------------------------------------
*/
void
DebugSet(clientID, argc, argv, value)
ClientData clientID;
int argc;
char *argv[];
bool value;
{
bool badFlag = FALSE;
int id = (int) clientID;
struct debugClient *dc;
int n;
if (id < 0 || id >= debugNumClients)
{
TxError("DebugSet: bad client id %d\n", clientID);
return;
}
dc = &debugClients[id];
for (; argc-- > 0; argv++)
{
n = LookupStruct(*argv, (LookupTable *) dc->dc_flags,
sizeof dc->dc_flags[0]);
if (n < 0)
{
TxError("Unrecognized flag '%s' for client '%s' (ignored)\n",
*argv, dc->dc_name);
badFlag = TRUE;
continue;
}
dc->dc_flags[n].df_value = value;
}
/* if badFlag passed, give list of valid flags */
if(badFlag)
{
int n;
TxError("Valid flags are: ");
for (n = 0; n < dc->dc_nflags; n++)
TxError("%s ", dc->dc_flags[n].df_name);
TxError("\n");
}
}
|