/*
* tkOption.c --
*
* This module contains procedures to manage the option
* database, which allows various strings to be associated
* with windows either by name or by class or both.
*
* Copyright (c) 1990-1994 The Regents of the University of California.
* Copyright (c) 1994-1996 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tkOption.c 1.56 96/10/09 15:18:02
*/
#include "tkInt.h"
/*
* The option database is stored as one tree for each main window.
* Each name or class field in an option is associated with a node or
* leaf of the tree. For example, the options "x.y.z" and "x.y*a"
* each correspond to three nodes in the tree; they share the nodes
* "x" and "x.y", but have different leaf nodes. One of the following
* structures exists for each node or leaf in the option tree. It is
* actually stored as part of the parent node, and describes a particular
* child of the parent.
*/
typedef struct Element {
* an option spec. */
union {
* a pointer to a structure describing
* the remaining elements of all
* options whose prefixes are the
* same up through this element. */
* value of the option. */
} child;
* options. Includes both the
* priority level and a serial #.
* Greater value means higher
* priority. Irrelevant except in
* leaf nodes. */
* below for values. */
} Element;
/*
* Flags in Element structures:
*
* CLASS - Non-zero means this element refers to a class,
* Zero means this element refers to a name.
* NODE - Zero means this is a leaf element (the child
* field is a value, not a pointer to another node).
* One means this is a node element.
* WILDCARD - Non-zero means this there was a star in the
* original specification just before this element.
* Zero means there was a dot.
*/
/*
* The following structure is used to manage a dynamic array of
* Elements. These structures are used for two purposes: to store
* the contents of a node in the option tree, and for the option
* stacks described below.
*/
typedef struct ElArray {
* allocated in the "els" array. */
* use out of els. */
* children of this node. The
* array will actually contain enough
* elements for all of the children
* (and even a few extras, perhaps).
* This must be the last field in
* the structure. */
} ElArray;
/*
* In addition to the option tree, which is a relatively static structure,
* there are eight additional structures called "stacks", which are used
* to speed up queries into the option database. The stack structures
* are designed for the situation where an individual widget makes repeated
* requests for its particular options. The requests differ only in
* the options pertaining to the particular widget and save them in a
* stack-like cache; subsequent requests for the same widget can search
* the cache relatively quickly. In fact, the cache is a hierarchical
* one, storing a list of relevant options for this widget and all of
* its ancestors up to the application root; hence the name "stack".
*
* Each of the eight stacks consists of an array of Elements, ordered in
* terms of levels in the window hierarchy. All the elements relevant
* for the top-level widget appear first in the array, followed by all
* those from the next-level widget on the path to the current widget,
* etc. down to those for the current widget.
*
* Cached information is divided into eight stacks according to the
* CLASS, NODE, and WILDCARD flags. Leaf and non-leaf information is
* kept separate to speed up individual probes (non-leaf information is
* only relevant when building the stacks, but isn't relevant when
* making probes; similarly, only non-leaf information is relevant
* when the stacks are being extended to the next widget down in the
* widget hierarchy). Wildcard elements are handled separately from
* "exact" elements because once they appear at a particular level in
* the stack they remain active for all deeper levels; exact elements
* are only relevant at a particular level. For example, when searching
* for options relevant in a particular window, the entire wildcard
* stacks get checked, but only the portions of the exact stacks that
* pertain to the window's parent. Lastly, name and class stacks are
* kept separate because different search keys are used when searching
* them; keeping them separate speeds up the searches.
*/
* loaded in stacks at present.
* NULL means stacks have never
* been used, or have been
* invalidated because of a change
* to the database. */
/*
* One of the following structures is used to keep track of each
* level in the stacks.
*/
typedef struct StackLevel {
* level. */
* element on stack corresponding to
* this level (used to restore "numUsed"
* fields when popping out of a level. */
} StackLevel;
/*
* Information about all of the stack levels that are currently
* active. This array grows dynamically to become as large as needed.
*/
/* Array describing current stack. */
* curLevel is never 0! (I don't remember
* why anymore...) */
/*
* The variable below is a serial number for all options entered into
* the database so far. It increments on each addition to the option
* database. It is used in computing option priorities, so that the
* most recent entry wins when choosing between options at the same
* priority level.
*/
static int serial = 0;
/*
* Special "no match" Element to use as default for searches.
*/
/*
* Forward declarations for procedures defined in this file:
*/
int leaf));
char *string));
/*
*--------------------------------------------------------------
*
* Tk_AddOption --
*
* Add a new option to the option database.
*
* Results:
* None.
*
* Side effects:
* Information is added to the option database.
*
*--------------------------------------------------------------
*/
void
* with main window for this window. */
char *name; /* Multi-element name of option. */
char *value; /* String value for option. */
int priority; /* Overall priority level to use for
* this option, such as TK_USER_DEFAULT_PRIO
* or TK_INTERACTIVE_PRIO. Must be between
* 0 and TK_MAX_PRIO. */
{
register char *p;
char *field;
}
/*
* Compute the priority for the new element, including both the
* overall level and the serial number (to disambiguate with the
* level).
*/
if (priority < 0) {
priority = 0;
} else if (priority > TK_MAX_PRIO) {
}
serial++;
/*
* Parse the option one field at a time.
*/
p = name;
/*
* Scan the next field from the name and convert it to a Tk_Uid.
* Must copy the field before calling Tk_Uid, so that a terminating
* NULL may be added without modifying the source string.
*/
if (*p == '*') {
p++;
} else {
}
field = p;
while ((*p != 0) && (*p != '.') && (*p != '*')) {
p++;
}
}
}
if (*p != 0) {
/*
* New element will be a node. If this option can't possibly
* apply to this main window, then just skip it. Otherwise,
* add it to the parent, if it isn't already there, and descend
* into it.
*/
return;
}
if (count == 0) {
break;
}
break;
}
}
if (*p == '.') {
p++;
}
} else {
/*
* New element is a leaf. Add it to the parent, if it isn't
* already there. If it exists already, keep whichever value
* has highest priority.
*/
if (count == 0) {
return;
}
}
return;
}
}
}
}
}
/*
*--------------------------------------------------------------
*
* Tk_GetOption --
*
* Retrieve an option from the option database.
*
* Results:
* The return value is the value specified in the option
* database for the given name and class on the given
* window. If there is nothing specified in the database
* for that option, then NULL is returned.
*
* Side effects:
* The internal caches used to speed up option mapping
* may be modified, if this tkwin is different from the
* last tkwin used for option retrieval.
*
*--------------------------------------------------------------
*/
* associated with. */
char *name; /* Name of option. */
char *className; /* Class of option. NULL means there
* is no class for this option: just
* check for name. */
{
register int count;
/*
* Note: no need to call OptionInit here: it will be done by
* the SetupStacks call below (squeeze out those nanoseconds).
*/
}
bestPtr = &defaultMatch;
}
}
}
}
}
}
}
}
}
}
/*
*--------------------------------------------------------------
*
* Tk_OptionCmd --
*
* This procedure is invoked to process the "option" Tcl command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
int
* interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
char c;
if (argc < 2) {
" cmd arg ?arg ...?\"", (char *) NULL);
return TCL_ERROR;
}
c = argv[1][0];
int priority;
return TCL_ERROR;
}
if (argc == 4) {
} else {
if (priority < 0) {
return TCL_ERROR;
}
}
return TCL_OK;
if (argc != 2) {
return TCL_ERROR;
}
}
cachedWindow = NULL;
return TCL_OK;
if (argc != 5) {
return TCL_ERROR;
}
return TCL_ERROR;
}
}
return TCL_OK;
int priority;
argv[0], " readfile fileName ?priority?\"",
(char *) NULL);
return TCL_ERROR;
}
if (argc == 4) {
if (priority < 0) {
return TCL_ERROR;
}
} else {
}
} else {
"\": must be add, clear, get, or readfile", (char *) NULL);
return TCL_ERROR;
}
}
/*
*--------------------------------------------------------------
*
* TkOptionDeadWindow --
*
* This procedure is called whenever a window is deleted.
* It cleans up any option-related stuff associated with
* the window.
*
* Results:
* None.
*
* Side effects:
* Option-related resources are freed. See code below
* for details.
*
*--------------------------------------------------------------
*/
void
{
/*
* If this window is in the option stacks, then clear the stacks.
*/
int i;
for (i = 1; i <= curLevel; i++) {
}
curLevel = -1;
cachedWindow = NULL;
}
/*
* If this window was a main window, then delete its option
* database.
*/
}
}
/*
*----------------------------------------------------------------------
*
* TkOptionClassChanged --
*
* This procedure is invoked when a window's class changes. If
* the window is on the option cache, this procedure flushes
* any information for the window, since the new class could change
* what is relevant.
*
* Results:
* None.
*
* Side effects:
* The option cache may be flushed in part or in whole.
*
*----------------------------------------------------------------------
*/
void
{
int i, j, *basePtr;
return;
}
/*
* Find the lowest stack level that refers to this window, then
* flush all of the levels above the matching one.
*/
for (i = 1; i <= curLevel; i++) {
for (j = i; j <= curLevel; j++) {
}
curLevel = i-1;
for (j = 0; j < NUM_STACKS; j++) {
}
if (curLevel <= 0) {
cachedWindow = NULL;
} else {
}
break;
}
}
}
/*
*----------------------------------------------------------------------
*
* ParsePriority --
*
* Parse a string priority value.
*
* Results:
* The return value is the integer priority level corresponding
* to string, or -1 if string doesn't point to a valid priority level.
* In this case, an error message is left in interp->result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
char *string; /* Describes a priority level, either
* symbolically or numerically. */
{
int priority, c;
c = string[0];
if ((c == 'w')
return TK_WIDGET_DEFAULT_PRIO;
} else if ((c == 's')
return TK_STARTUP_FILE_PRIO;
} else if ((c == 'u')
return TK_USER_DEFAULT_PRIO;
} else if ((c == 'i')
return TK_INTERACTIVE_PRIO;
} else {
char *end;
|| (priority > 100)) {
"\": must be widgetDefault, startupFile, userDefault, ",
"interactive, or a number between 0 and 100",
(char *) NULL);
return -1;
}
}
return priority;
}
/*
*----------------------------------------------------------------------
*
* AddFromString --
*
* Given a string containing lines in the standard format for
* X resources (see other documentation for details on what this
* is), parse the resource specifications and enter them as options
* for tkwin's main window.
*
* Results:
* The return value is a standard Tcl return code. In the case of
* an error in parsing string, TCL_ERROR will be returned and an
* error message will be left in interp->result. The memory at
* string is totally trashed by this procedure. If you care about
* its contents, make a copy before calling here.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
* for this window's main window. */
char *string; /* String containing option specifiers. */
int priority; /* Priority level to use for options in
* this string, such as TK_USER_DEFAULT_PRIO
* or TK_INTERACTIVE_PRIO. Must be between
* 0 and TK_MAX_PRIO. */
{
int lineNum;
lineNum = 1;
while (1) {
/*
* Skip leading white space and empty lines and comment lines, and
* check for the end of the spec.
*/
src++;
}
do {
src++;
src += 2;
lineNum++;
}
}
if (*src == '\n') {
src++;
lineNum++;
continue;
}
if (*src == '\0') {
break;
}
/*
* Parse off the option name, collapsing out backslash-newline
* sequences of course.
*/
while (*src != ':') {
lineNum);
return TCL_ERROR;
}
src += 2;
lineNum++;
} else {
dst++;
src++;
}
}
/*
* Eliminate trailing white space on the name, and null-terminate
* it.
*/
dst--;
}
*dst = '\0';
/*
* Skip white space between the name and the value.
*/
src++;
src++;
}
if (*src == '\0') {
return TCL_ERROR;
}
/*
* Parse off the value, squeezing out backslash-newline sequences
* along the way.
*/
while (*src != '\n') {
if (*src == '\0') {
lineNum);
return TCL_ERROR;
}
src += 2;
lineNum++;
} else {
dst++;
src++;
}
}
*dst = 0;
/*
* Enter the option into the database.
*/
src++;
lineNum++;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* ReadOptionFile --
*
* Read a file of options ("resources" in the old X terminology)
* and load them into the option database.
*
* Results:
* The return value is a standard Tcl return code. In the case of
* an error in parsing string, TCL_ERROR will be returned and an
* error message will be left in interp->result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
* for this window's main window. */
char *fileName; /* Name of file containing options. */
int priority; /* Priority level to use for options in
* this file, such as TK_USER_DEFAULT_PRIO
* or TK_INTERACTIVE_PRIO. Must be between
* 0 and TK_MAX_PRIO. */
{
return TCL_ERROR;
}
return TCL_ERROR;
}
/*
* Compute size of file by seeking to the end of the file. This will
* overallocate if we are performing CRLF translation.
*/
if (bufferSize < 0) {
return TCL_ERROR;
}
if (bufferSize < 0) {
return TCL_ERROR;
}
buffer[bufferSize] = 0;
return result;
}
/*
*--------------------------------------------------------------
*
* NewArray --
*
* Create a new ElArray structure of a given size.
*
* Results:
* The return value is a pointer to a properly initialized
* element array with "numEls" space. The array is marked
* as having no active elements.
*
* Side effects:
* Memory is allocated.
*
*--------------------------------------------------------------
*/
static ElArray *
int numEls; /* How many elements of space to allocate. */
{
return arrayPtr;
}
/*
*--------------------------------------------------------------
*
* ExtendArray --
*
* Add a new element to an array, extending the array if
* necessary.
*
* Results:
* The return value is a pointer to the new array, which
* will be different from arrayPtr if the array got expanded.
*
* Side effects:
* Memory may be allocated or freed.
*
*--------------------------------------------------------------
*/
static ElArray *
{
/*
* If the current array has filled up, make it bigger.
*/
}
return arrayPtr;
}
/*
*--------------------------------------------------------------
*
* SetupStacks --
*
* Arrange the stacks so that they cache all the option
* information for a particular window.
*
* Results:
* None.
*
* Side effects:
* The stacks are modified to hold information for tkwin
* and all its ancestors in the window hierarchy.
*
*--------------------------------------------------------------
*/
static void
* be cached. */
int leaf; /* Non-zero means this is the leaf
* window being probed. Zero means this
* is an ancestor of the desired leaf. */
{
/*
* The following array defines the order in which the current
* stacks are searched to find matching entries to add to the
* stacks. Given the current priority-based scheme, the order
* below is no longer relevant; all that matters is that an
* element is on the list *somewhere*. The ordering is a relic
* of the old days when priorities were determined differently.
*/
}
/*
* Step 1: make sure that options are cached for this window's
* parent.
*/
}
level++;
} else {
level = 1;
}
/*
* Step 2: pop extra unneeded information off the stacks and
* mark those windows as no longer having cached information.
*/
curLevel--;
}
for (i = 0; i < NUM_STACKS; i++) {
}
}
/*
* Step 3: if the root database information isn't loaded or
* isn't valid, initialize level 0 of the stack from the
* database root (this only happens if winPtr is a main window).
*/
if ((curLevel == 1)
&& ((cachedWindow == NULL)
for (i = 0; i < NUM_STACKS; i++) {
}
}
/*
* Step 4: create a new stack level; grow the level array if
* we've run out of levels. Clear the stacks for EXACT_LEAF_NAME
* and EXACT_LEAF_CLASS (anything that was there is of no use
* any more).
*/
(numLevels*sizeof(StackLevel)));
numLevels *= 2;
}
/*
* Step 5: scan the current stack level looking for matches to this
* window's name or class; where found, add new information to the
* stacks.
*/
int count;
i = *iPtr;
if (i & CLASS) {
} else {
}
/*
* For wildcard stacks, check all entries; for non-wildcard
* stacks, only check things that matched in the parent.
*/
if (!(i & WILDCARD)) {
}
continue;
}
}
}
}
/*
*--------------------------------------------------------------
*
* ExtendStacks --
*
* Given an element array, copy all the elements from the
* array onto the system stacks (except for irrelevant leaf
* elements).
*
* Results:
* None.
*
* Side effects:
* The option stacks are extended.
*
*--------------------------------------------------------------
*/
static void
int leaf; /* If zero, then don't copy exact leaf
* elements. */
{
register int count;
continue;
}
}
}
/*
*--------------------------------------------------------------
*
* OptionInit --
*
* Initialize data structures for option handling.
*
* Results:
* None.
*
* Side effects:
* Option-related data structures get initialized.
*
*--------------------------------------------------------------
*/
static void
* window that isn't initialized
* yet. */
{
int i;
/*
* First, once-only initialization.
*/
if (numLevels == 0) {
numLevels = 5;
for (i = 0; i < NUM_STACKS; i++) {
}
defaultMatch.flags = 0;
}
/*
* Then, per-main-window initialization. Create and delete dummy
* interpreter for message logging.
*/
interp = Tcl_CreateInterp();
}
/*
*--------------------------------------------------------------
*
* ClearOptionTree --
*
* This procedure is called to erase everything in a
* hierarchical option database.
*
* Results:
* None.
*
* Side effects:
* All the options associated with arrayPtr are deleted,
* along with all option subtrees. The space pointed to
* by arrayPtr is freed.
*
*--------------------------------------------------------------
*/
static void
* referred to recursively by this. */
{
int count;
}
}
}
/*
*--------------------------------------------------------------
*
* GetDefaultOptions --
*
* This procedure is invoked to load the default set of options
* for a window.
*
* Results:
* None.
*
* Side effects:
* Options are added to those for winPtr's main window. If
* there exists a RESOURCE_MANAGER proprety for winPtr's
* display, that is used. Otherwise, the .Xdefaults file in
* the user's home directory is used.
*
*--------------------------------------------------------------
*/
static int
* associated with this. */
{
char *regProp;
/*
* Try the RESOURCE_MANAGER property on the root window first.
*/
XA_RESOURCE_MANAGER, 0, 100000,
&& (actualFormat == 8)) {
return result;
}
/*
* No luck there. Try a .Xdefaults file in the user's home
* directory.
*/
}
return result;
}