]> cvs.zerfleddert.de Git - micropolis/blob - src/tk/tkoption.c
Fixes for compilation with gcc 15
[micropolis] / src / tk / tkoption.c
1 /*
2 * tkOption.c --
3 *
4 * This module contains procedures to manage the option
5 * database, which allows various strings to be associated
6 * with windows either by name or by class or both.
7 *
8 * Copyright 1990 Regents of the University of California.
9 * Permission to use, copy, modify, and distribute this
10 * software and its documentation for any purpose and without
11 * fee is hereby granted, provided that the above copyright
12 * notice appear in all copies. The University of California
13 * makes no representations about the suitability of this
14 * software for any purpose. It is provided "as is" without
15 * express or implied warranty.
16 */
17
18 #ifndef lint
19 static char rcsid[] = "$Header: /user6/ouster/wish/RCS/tkOption.c,v 1.25 92/03/16 08:46:14 ouster Exp $ SPRITE (Berkeley)";
20 #endif
21
22 #include "tkconfig.h"
23 #include "tkint.h"
24
25 /*
26 * The option database is stored as one tree for each main window.
27 * Each name or class field in an option is associated with a node or
28 * leaf of the tree. For example, the options "x.y.z" and "x.y*a"
29 * each correspond to three nodes in the tree; they share the nodes
30 * "x" and "x.y", but have different leaf nodes. One of the following
31 * structures exists for each node or leaf in the option tree. It is
32 * actually stored as part of the parent node, and describes a particular
33 * child of the parent.
34 */
35
36 typedef struct Element {
37 Tk_Uid nameUid; /* Name or class from one element of
38 * an option spec. */
39 union {
40 struct ElArray *arrayPtr; /* If this is an intermediate node,
41 * a pointer to a structure describing
42 * the remaining elements of all
43 * options whose prefixes are the
44 * same up through this element. */
45 Tk_Uid valueUid; /* For leaf nodes, this is the string
46 * value of the option. */
47 } child;
48 int priority; /* Used to select among matching
49 * options. Includes both the
50 * priority level and a serial #.
51 * Greater value means higher
52 * priority. Irrelevant except in
53 * leaf nodes. */
54 int flags; /* OR-ed combination of bits. See
55 * below for values. */
56 } Element;
57
58 /*
59 * Flags in NodeElement structures:
60 *
61 * CLASS - Non-zero means this element refers to a class,
62 * Zero means this element refers to a name.
63 * NODE - Zero means this is a leaf element (the child
64 * field is a value, not a pointer to another node).
65 * One means this is a node element.
66 * WILDCARD - Non-zero means this there was a star in the
67 * original specification just before this element.
68 * Zero means there was a dot.
69 */
70
71 #define TYPE_MASK 0x7
72
73 #define CLASS 0x1
74 #define NODE 0x2
75 #define WILDCARD 0x4
76
77 #define EXACT_LEAF_NAME 0x0
78 #define EXACT_LEAF_CLASS 0x1
79 #define EXACT_NODE_NAME 0x2
80 #define EXACT_NODE_CLASS 0x3
81 #define WILDCARD_LEAF_NAME 0x4
82 #define WILDCARD_LEAF_CLASS 0x5
83 #define WILDCARD_NODE_NAME 0x6
84 #define WILDCARD_NODE_CLASS 0x7
85
86 /*
87 * The following structure is used to manage a dynamic array of
88 * Elements. These structures are used for two purposes: to store
89 * the contents of a node in the option tree, and for the option
90 * stacks described below.
91 */
92
93 typedef struct ElArray {
94 int arraySize; /* Number of elements actually
95 * allocated in the "els" array. */
96 int numUsed; /* Number of elements currently in
97 * use out of els. */
98 Element *nextToUse; /* Pointer to &els[numUsed]. */
99 Element els[1]; /* Array of structures describing
100 * children of this node. The
101 * array will actually contain enough
102 * elements for all of the children
103 * (and even a few extras, perhaps).
104 * This must be the last field in
105 * the structure. */
106 } ElArray;
107
108 #define EL_ARRAY_SIZE(numEls) ((unsigned) (sizeof(ElArray) \
109 + ((numEls)-1)*sizeof(Element)))
110 #define INITIAL_SIZE 5
111
112 /*
113 * In addition to the option tree, which is a relatively static structure,
114 * there are eight additional structures called "stacks", which are used
115 * to speed up queries into the option database. The stack structures
116 * are designed for the situation where an individual widget makes repeated
117 * requests for its particular options. The requests differ only in
118 * their last name/class, so during the first request we extract all
119 * the options pertaining to the particular widget and save them in a
120 * stack-like cache; subsequent requests for the same widget can search
121 * the cache relatively quickly. In fact, the cache is a hierarchical
122 * one, storing a list of relevant options for this widget and all of
123 * its ancestors up to the application root; hence the name "stack".
124 *
125 * Each of the eight stacks consists of an array of Elements, ordered in
126 * terms of levels in the window hierarchy. All the elements relevant
127 * for the top-level widget appear first in the array, followed by all
128 * those from the next-level widget on the path to the current widget,
129 * etc. down to those for the current widget.
130 *
131 * Cached information is divided into eight stacks according to the
132 * CLASS, NODE, and WILDCARD flags. Leaf and non-leaf information is
133 * kept separate to speed up individual probes (non-leaf information is
134 * only relevant when building the stacks, but isn't relevant when
135 * making probes; similarly, only non-leaf information is relevant
136 * when the stacks are being extended to the next widget down in the
137 * widget hierarchy). Wildcard elements are handled separately from
138 * "exact" elements because once they appear at a particular level in
139 * the stack they remain active for all deeper levels; exact elements
140 * are only relevant at a particular level. For example, when searching
141 * for options relevant in a particular window, the entire wildcard
142 * stacks get checked, but only the portions of the exact stacks that
143 * pertain to the window's parent. Lastly, name and class stacks are
144 * kept separate because different search keys are used when searching
145 * them; keeping them separate speeds up the searches.
146 */
147
148 #define NUM_STACKS 8
149 static ElArray *stacks[NUM_STACKS];
150 static TkWindow *cachedWindow = NULL; /* Lowest-level window currently
151 * loaded in stacks at present.
152 * NULL means stacks have never
153 * been used, or have been
154 * invalidated because of a change
155 * to the database. */
156
157 /*
158 * One of the following structures is used to keep track of each
159 * level in the stacks.
160 */
161
162 typedef struct StackLevel {
163 TkWindow *winPtr; /* Window corresponding to this stack
164 * level. */
165 int bases[NUM_STACKS]; /* For each stack, index of first
166 * element on stack corresponding to
167 * this level (used to restore "numUsed"
168 * fields when popping out of a level. */
169 } StackLevel;
170
171 /*
172 * Information about all of the stack levels that are currently
173 * active. This array grows dynamically to become as large as needed.
174 */
175
176 static StackLevel *levels = NULL;
177 /* Array describing current stack. */
178 static int numLevels = 0; /* Total space allocated. */
179 static int curLevel = 0; /* Highest level currently in use. */
180
181 /*
182 * The variable below is a serial number for all options entered into
183 * the database so far. It increments on each addition to the option
184 * database. It is used in computing option priorities, so that the
185 * most recent entry wins when choosing between options at the same
186 * priority level.
187 */
188
189 static int serial = 0;
190
191 /*
192 * Special "no match" Element to use as default for searches.
193 */
194
195 static Element defaultMatch;
196
197 /*
198 * Forward declarations for procedures defined in this file:
199 */
200
201 static int AddFromString _ANSI_ARGS_((Tcl_Interp *interp,
202 Tk_Window tkwin, char *string, int priority));
203 static void ClearOptionTree _ANSI_ARGS_((ElArray *arrayPtr));
204 static ElArray * ExtendArray _ANSI_ARGS_((ElArray *arrayPtr,
205 Element *elPtr));
206 static void ExtendStacks _ANSI_ARGS_((ElArray *arrayPtr,
207 int leaf));
208 static int GetDefaultOptions _ANSI_ARGS_((Tcl_Interp *interp,
209 TkWindow *winPtr));
210 static ElArray * NewArray _ANSI_ARGS_((int numEls));
211 static void OptionInit _ANSI_ARGS_((TkMainInfo *mainPtr));
212 static int ParsePriority _ANSI_ARGS_((Tcl_Interp *interp,
213 char *string));
214 static int ReadOptionFile _ANSI_ARGS_((Tcl_Interp *interp,
215 Tk_Window tkwin, char *fileName, int priority));
216 static void SetupStacks _ANSI_ARGS_((TkWindow *winPtr, int leaf));
217 \f
218 /*
219 *--------------------------------------------------------------
220 *
221 * Tk_AddOption --
222 *
223 * Add a new option to the option database.
224 *
225 * Results:
226 * None.
227 *
228 * Side effects:
229 * Information is added to the option database.
230 *
231 *--------------------------------------------------------------
232 */
233
234 void
235 Tk_AddOption (
236 Tk_Window tkwin, /* Window token; option will be associated
237 * with main window for this window. */
238 char *name, /* Multi-element name of option. */
239 char *value, /* String value for option. */
240 int priority /* Overall priority level to use for
241 * this option, such as TK_USER_DEFAULT_PRIO
242 * or TK_INTERACTIVE_PRIO. Must be between
243 * 0 and TK_MAX_PRIO. */
244 )
245 {
246 TkWindow *winPtr = ((TkWindow *) tkwin)->mainPtr->winPtr;
247 register ElArray **arrayPtrPtr;
248 register Element *elPtr;
249 Element newEl;
250 register char *p;
251 char *field;
252 int count, firstField, length;
253 #define TMP_SIZE 100
254 char tmp[TMP_SIZE+1];
255
256 if (winPtr->mainPtr->optionRootPtr == NULL) {
257 OptionInit(winPtr->mainPtr);
258 }
259 cachedWindow = NULL; /* Invalidate the cache. */
260
261 /*
262 * Compute the priority for the new element, including both the
263 * overall level and the serial number (to disambiguate with the
264 * level).
265 */
266
267 if (priority < 0) {
268 priority = 0;
269 } else if (priority > TK_MAX_PRIO) {
270 priority = TK_MAX_PRIO;
271 }
272 newEl.priority = (priority << 24) + serial;
273 serial++;
274
275 /*
276 * Parse the option one field at a time.
277 */
278
279 arrayPtrPtr = &(((TkWindow *) tkwin)->mainPtr->optionRootPtr);
280 p = name;
281 for (firstField = 1; ; firstField = 0) {
282
283 /*
284 * Scan the next field from the name and convert it to a Tk_Uid.
285 * Must copy the field before calling Tk_Uid, so that a terminating
286 * NULL may be added without modifying the source string.
287 */
288
289 if (*p == '*') {
290 newEl.flags = WILDCARD;
291 p++;
292 } else {
293 newEl.flags = 0;
294 }
295 field = p;
296 while ((*p != 0) && (*p != '.') && (*p != '*')) {
297 p++;
298 }
299 length = p - field;
300 if (length > TMP_SIZE) {
301 length = TMP_SIZE;
302 }
303 strncpy(tmp, field, length);
304 tmp[length] = 0;
305 newEl.nameUid = Tk_GetUid(tmp);
306 if (isupper(*field)) {
307 newEl.flags |= CLASS;
308 }
309
310 if (*p != 0) {
311
312 /*
313 * New element will be a node. If this option can't possibly
314 * apply to this main window, then just skip it. Otherwise,
315 * add it to the parent, if it isn't already there, and descend
316 * into it.
317 */
318
319 newEl.flags |= NODE;
320 if (firstField && !(newEl.flags & WILDCARD)
321 && (newEl.nameUid != winPtr->nameUid)
322 && (newEl.nameUid != winPtr->classUid)) {
323 return;
324 }
325 for (elPtr = (*arrayPtrPtr)->els, count = (*arrayPtrPtr)->numUsed;
326 ; elPtr++, count--) {
327 if (count == 0) {
328 newEl.child.arrayPtr = NewArray(5);
329 *arrayPtrPtr = ExtendArray(*arrayPtrPtr, &newEl);
330 arrayPtrPtr = &((*arrayPtrPtr)->nextToUse[-1].child.arrayPtr);
331 break;
332 }
333 if ((elPtr->nameUid == newEl.nameUid)
334 && (elPtr->flags == newEl.flags)) {
335 arrayPtrPtr = &(elPtr->child.arrayPtr);
336 break;
337 }
338 }
339 if (*p == '.') {
340 p++;
341 }
342 } else {
343
344 /*
345 * New element is a leaf. Add it to the parent, if it isn't
346 * already there. If it exists already, keep whichever value
347 * has highest priority.
348 */
349
350 newEl.child.valueUid = Tk_GetUid(value);
351 for (elPtr = (*arrayPtrPtr)->els, count = (*arrayPtrPtr)->numUsed;
352 ; elPtr++, count--) {
353 if (count == 0) {
354 *arrayPtrPtr = ExtendArray(*arrayPtrPtr, &newEl);
355 return;
356 }
357 if ((elPtr->nameUid == newEl.nameUid)
358 && (elPtr->flags == newEl.flags)) {
359 if (elPtr->priority < newEl.priority) {
360 elPtr->priority = newEl.priority;
361 elPtr->child.valueUid = newEl.child.valueUid;
362 }
363 return;
364 }
365 }
366 }
367 }
368 }
369 \f
370 /*
371 *--------------------------------------------------------------
372 *
373 * Tk_GetOption --
374 *
375 * Retrieve an option from the option database.
376 *
377 * Results:
378 * The return value is the value specified in the option
379 * database for the given name and class on the given
380 * window. If there is nothing specified in the database
381 * for that option, then NULL is returned.
382 *
383 * Side effects:
384 * The internal caches used to speed up option mapping
385 * may be modified, if this tkwin is different from the
386 * last tkwin used for option retrieval.
387 *
388 *--------------------------------------------------------------
389 */
390
391 Tk_Uid
392 Tk_GetOption (
393 Tk_Window tkwin, /* Token for window that option is
394 * associated with. */
395 char *name, /* Name of option. */
396 char *className /* Class of option. NULL means there
397 * is no class for this option: just
398 * check for name. */
399 )
400 {
401 Tk_Uid nameId, classId;
402 register Element *elPtr, *bestPtr;
403 register int count;
404
405 /*
406 * Note: no need to call OptionInit here: it will be done by
407 * the SetupStacks call below (squeeze out those nanoseconds).
408 */
409
410 if (tkwin != (Tk_Window) cachedWindow) {
411 SetupStacks((TkWindow *) tkwin, 1);
412 }
413
414 nameId = Tk_GetUid(name);
415 bestPtr = &defaultMatch;
416 for (elPtr = stacks[EXACT_LEAF_NAME]->els,
417 count = stacks[EXACT_LEAF_NAME]->numUsed; count > 0;
418 elPtr++, count--) {
419 if ((elPtr->nameUid == nameId)
420 && (elPtr->priority > bestPtr->priority)) {
421 bestPtr = elPtr;
422 }
423 }
424 for (elPtr = stacks[WILDCARD_LEAF_NAME]->els,
425 count = stacks[WILDCARD_LEAF_NAME]->numUsed; count > 0;
426 elPtr++, count--) {
427 if ((elPtr->nameUid == nameId)
428 && (elPtr->priority > bestPtr->priority)) {
429 bestPtr = elPtr;
430 }
431 }
432 if (className != NULL) {
433 classId = Tk_GetUid(className);
434 for (elPtr = stacks[EXACT_LEAF_CLASS]->els,
435 count = stacks[EXACT_LEAF_CLASS]->numUsed; count > 0;
436 elPtr++, count--) {
437 if ((elPtr->nameUid == classId)
438 && (elPtr->priority > bestPtr->priority)) {
439 bestPtr = elPtr;
440 }
441 }
442 for (elPtr = stacks[WILDCARD_LEAF_CLASS]->els,
443 count = stacks[WILDCARD_LEAF_CLASS]->numUsed; count > 0;
444 elPtr++, count--) {
445 if ((elPtr->nameUid == classId)
446 && (elPtr->priority > bestPtr->priority)) {
447 bestPtr = elPtr;
448 }
449 }
450 }
451 return bestPtr->child.valueUid;
452 }
453 \f
454 /*
455 *--------------------------------------------------------------
456 *
457 * Tk_OptionCmd --
458 *
459 * This procedure is invoked to process the "option" Tcl command.
460 * See the user documentation for details on what it does.
461 *
462 * Results:
463 * A standard Tcl result.
464 *
465 * Side effects:
466 * See the user documentation.
467 *
468 *--------------------------------------------------------------
469 */
470
471 int
472 Tk_OptionCmd (
473 ClientData clientData, /* Main window associated with
474 * interpreter. */
475 Tcl_Interp *interp, /* Current interpreter. */
476 int argc, /* Number of arguments. */
477 char **argv /* Argument strings. */
478 )
479 {
480 Tk_Window tkwin = (Tk_Window) clientData;
481 int length;
482 char c;
483
484 if (argc < 2) {
485 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
486 " cmd arg ?arg ...?\"", (char *) NULL);
487 return TCL_ERROR;
488 }
489 c = argv[1][0];
490 length = strlen(argv[1]);
491 if ((c == 'a') && (strncmp(argv[1], "add", length) == 0)) {
492 int priority;
493
494 if ((argc != 4) && (argc != 5)) {
495 Tcl_AppendResult(interp, "wrong # args: should be \"",
496 argv[0], " add pattern value ?priority?\"", (char *) NULL);
497 return TCL_ERROR;
498 }
499 if (argc == 4) {
500 priority = TK_INTERACTIVE_PRIO;
501 } else {
502 priority = ParsePriority(interp, argv[4]);
503 if (priority < 0) {
504 return TCL_ERROR;
505 }
506 }
507 Tk_AddOption(tkwin, argv[2], argv[3], priority);
508 return TCL_OK;
509 } else if ((c == 'c') && (strncmp(argv[1], "clear", length) == 0)) {
510 TkMainInfo *mainPtr;
511
512 if (argc != 2) {
513 Tcl_AppendResult(interp, "wrong # args: should be \"",
514 argv[0], " clear\"", (char *) NULL);
515 return TCL_ERROR;
516 }
517 mainPtr = ((TkWindow *) tkwin)->mainPtr;
518 if (mainPtr->optionRootPtr != NULL) {
519 ClearOptionTree(mainPtr->optionRootPtr);
520 mainPtr->optionRootPtr = NULL;
521 }
522 cachedWindow = NULL;
523 return TCL_OK;
524 } else if ((c == 'g') && (strncmp(argv[1], "get", length) == 0)) {
525 Tk_Window window;
526 Tk_Uid value;
527
528 if (argc != 5) {
529 Tcl_AppendResult(interp, "wrong # args: should be \"",
530 argv[0], " get window name class\"", (char *) NULL);
531 return TCL_ERROR;
532 }
533 window = Tk_NameToWindow(interp, argv[2], tkwin);
534 if (window == NULL) {
535 return TCL_ERROR;
536 }
537 value = Tk_GetOption(window, argv[3], argv[4]);
538 if (value != NULL) {
539 interp->result = value;
540 }
541 return TCL_OK;
542 } else if ((c == 'r') && (strncmp(argv[1], "readfile", length) == 0)) {
543 int priority;
544
545 if ((argc != 3) && (argc != 4)) {
546 Tcl_AppendResult(interp, "wrong # args: should be \"",
547 argv[0], " readfile fileName ?priority?\"",
548 (char *) NULL);
549 return TCL_ERROR;
550 }
551 if (argc == 4) {
552 priority = ParsePriority(interp, argv[3]);
553 if (priority < 0) {
554 return TCL_ERROR;
555 }
556 } else {
557 priority = TK_INTERACTIVE_PRIO;
558 }
559 return ReadOptionFile(interp, tkwin, argv[2], priority);
560 } else {
561 Tcl_AppendResult(interp, "bad option \"", argv[1],
562 "\": must be add, clear, get, or readfile", (char *) NULL);
563 return TCL_ERROR;
564 }
565 }
566 \f
567 /*
568 *--------------------------------------------------------------
569 *
570 * TkOptionDeadWindow --
571 *
572 * This procedure is called whenever a window is deleted.
573 * It cleans up any option-related stuff associated with
574 * the window.
575 *
576 * Results:
577 * None.
578 *
579 * Side effects:
580 * Option-related resources are freed. See code below
581 * for details.
582 *
583 *--------------------------------------------------------------
584 */
585
586 void
587 TkOptionDeadWindow (
588 register TkWindow *winPtr /* Window to be cleaned up. */
589 )
590 {
591 /*
592 * If this window is in the option stacks, then clear the stacks.
593 */
594
595 if (winPtr->optionLevel != -1) {
596 int i;
597
598 for (i = 1; i <= curLevel; i++) {
599 levels[curLevel].winPtr->optionLevel = -1;
600 }
601 curLevel = 0;
602 cachedWindow = NULL;
603 }
604
605 /*
606 * If this window was a main window, then delete its option
607 * database.
608 */
609
610 if ((winPtr->mainPtr->winPtr == winPtr)
611 && (winPtr->mainPtr->optionRootPtr != NULL)) {
612 ClearOptionTree(winPtr->mainPtr->optionRootPtr);
613 winPtr->mainPtr->optionRootPtr = NULL;
614 }
615 }
616 \f
617 /*
618 *----------------------------------------------------------------------
619 *
620 * ParsePriority --
621 *
622 * Parse a string priority value.
623 *
624 * Results:
625 * The return value is the integer priority level corresponding
626 * to string, or -1 if string doesn't point to a valid priority level.
627 * In this case, an error message is left in interp->result.
628 *
629 * Side effects:
630 * None.
631 *
632 *----------------------------------------------------------------------
633 */
634
635 static int
636 ParsePriority (
637 Tcl_Interp *interp, /* Interpreter to use for error reporting. */
638 char *string /* Describes a priority level, either
639 * symbolically or numerically. */
640 )
641 {
642 char c;
643 int length, priority;
644
645 c = string[0];
646 length = strlen(string);
647 if ((c == 'w')
648 && (strncmp(string, "widgetDefault", length) == 0)) {
649 return TK_WIDGET_DEFAULT_PRIO;
650 } else if ((c == 's')
651 && (strncmp(string, "startupFile", length) == 0)) {
652 return TK_STARTUP_FILE_PRIO;
653 } else if ((c == 'u')
654 && (strncmp(string, "userDefault", length) == 0)) {
655 return TK_USER_DEFAULT_PRIO;
656 } else if ((c == 'i')
657 && (strncmp(string, "interactive", length) == 0)) {
658 return TK_INTERACTIVE_PRIO;
659 } else {
660 char *end;
661
662 priority = strtoul(string, &end, 0);
663 if ((end == string) || (*end != 0) || (priority < 0)
664 || (priority > 100)) {
665 Tcl_AppendResult(interp, "bad priority level \"", string,
666 "\": must be widgetDefault, startupFile, userDefault, ",
667 "interactive, or a number between 0 and 100",
668 (char *) NULL);
669 return -1;
670 }
671 }
672 return priority;
673 }
674 \f
675 /*
676 *----------------------------------------------------------------------
677 *
678 * AddFromString --
679 *
680 * Given a string containing lines in the standard format for
681 * X resources (see other documentation for details on what this
682 * is), parse the resource specifications and enter them as options
683 * for tkwin's main window.
684 *
685 * Results:
686 * The return value is a standard Tcl return code. In the case of
687 * an error in parsing string, TCL_ERROR will be returned and an
688 * error message will be left in interp->result. The memory at
689 * string is totally trashed by this procedure. If you care about
690 * its contents, make a copy before calling here.
691 *
692 * Side effects:
693 * None.
694 *
695 *----------------------------------------------------------------------
696 */
697
698 static int
699 AddFromString (
700 Tcl_Interp *interp, /* Interpreter to use for reporting results. */
701 Tk_Window tkwin, /* Token for window: options are entered
702 * for this window's main window. */
703 char *string, /* String containing option specifiers. */
704 int priority /* Priority level to use for options in
705 * this string, such as TK_USER_DEFAULT_PRIO
706 * or TK_INTERACTIVE_PRIO. Must be between
707 * 0 and TK_MAX_PRIO. */
708 )
709 {
710 register char *src, *dst;
711 char *name, *value;
712 int lineNum;
713
714 src = string;
715 lineNum = 1;
716 while (1) {
717
718 /*
719 * Skip leading white space and empty lines and comment lines, and
720 * check for the end of the spec.
721 */
722
723 while ((*src == ' ') || (*src == '\t')) {
724 src++;
725 }
726 if ((*src == '#') || (*src == '!')) {
727 do {
728 src++;
729 if ((src[0] == '\\') && (src[1] == '\n')) {
730 src += 2;
731 lineNum++;
732 }
733 } while ((*src != '\n') && (*src != 0));
734 }
735 if (*src == '\n') {
736 src++;
737 lineNum++;
738 continue;
739 }
740 if (*src == '\0') {
741 break;
742 }
743
744 /*
745 * Parse off the option name, collapsing out backslash-newline
746 * sequences of course.
747 */
748
749 dst = name = src;
750 while (*src != ':') {
751 if ((*src == '\0') || (*src == '\n')) {
752 sprintf(interp->result, "missing colon on line %d",
753 lineNum);
754 return TCL_ERROR;
755 }
756 if ((src[0] == '\\') && (src[1] == '\n')) {
757 src += 2;
758 lineNum++;
759 } else {
760 *dst = *src;
761 dst++;
762 src++;
763 }
764 }
765
766 /*
767 * Eliminate trailing white space on the name, and null-terminate
768 * it.
769 */
770
771 while ((dst != name) && ((dst[-1] == ' ') || (dst[-1] == '\t'))) {
772 dst--;
773 }
774 *dst = '\0';
775
776 /*
777 * Skip white space between the name and the value.
778 */
779
780 src++;
781 while ((*src == ' ') || (*src == '\t')) {
782 src++;
783 }
784 if (*src == '\0') {
785 sprintf(interp->result, "missing value on line %d", lineNum);
786 return TCL_ERROR;
787 }
788
789 /*
790 * Parse off the value, squeezing out backslash-newline sequences
791 * along the way.
792 */
793
794 dst = value = src;
795 while (*src != '\n') {
796 if (*src == '\0') {
797 sprintf(interp->result, "missing newline on line %d",
798 lineNum);
799 return TCL_ERROR;
800 }
801 if ((src[0] == '\\') && (src[1] == '\n')) {
802 src += 2;
803 lineNum++;
804 } else {
805 *dst = *src;
806 dst++;
807 src++;
808 }
809 }
810 *dst = 0;
811
812 /*
813 * Enter the option into the database.
814 */
815
816 Tk_AddOption(tkwin, name, value, priority);
817 src++;
818 lineNum++;
819 }
820 return TCL_OK;
821 }
822 \f
823 /*
824 *----------------------------------------------------------------------
825 *
826 * ReadOptionFile --
827 *
828 * Read a file of options ("resources" in the old X terminology)
829 * and load them into the option database.
830 *
831 * Results:
832 * The return value is a standard Tcl return code. In the case of
833 * an error in parsing string, TCL_ERROR will be returned and an
834 * error message will be left in interp->result.
835 *
836 * Side effects:
837 * None.
838 *
839 *----------------------------------------------------------------------
840 */
841
842 static int
843 ReadOptionFile (
844 Tcl_Interp *interp, /* Interpreter to use for reporting results. */
845 Tk_Window tkwin, /* Token for window: options are entered
846 * for this window's main window. */
847 char *fileName, /* Name of file containing options. */
848 int priority /* Priority level to use for options in
849 * this file, such as TK_USER_DEFAULT_PRIO
850 * or TK_INTERACTIVE_PRIO. Must be between
851 * 0 and TK_MAX_PRIO. */
852 )
853 {
854 char *realName, *buffer;
855 int fileId, result;
856 struct stat statBuf;
857
858 realName = Tcl_TildeSubst(interp, fileName);
859 if (fileName == NULL) {
860 return TCL_ERROR;
861 }
862 #ifdef MSDOS
863 fileId = open(realName, O_RDONLY | O_BINARY, 0);
864 #else
865 fileId = open(realName, O_RDONLY, 0);
866 #endif
867 if (fileId < 0) {
868 Tcl_AppendResult(interp, "couldn't read file \"", fileName, "\"",
869 (char *) NULL);
870 return TCL_ERROR;
871 }
872 if (fstat(fileId, &statBuf) == -1) {
873 Tcl_AppendResult(interp, "couldn't stat file \"", fileName, "\"",
874 (char *) NULL);
875 close(fileId);
876 return TCL_ERROR;
877 }
878 buffer = (char *) ckalloc((unsigned) statBuf.st_size+1);
879 #ifdef MSDOS
880 if (read(fileId, buffer, (int) statBuf.st_size) < 0) {
881 #else
882 if (read(fileId, buffer, (int) statBuf.st_size) != statBuf.st_size) {
883 #endif
884 Tcl_AppendResult(interp, "error reading file \"", fileName, "\"",
885 (char *) NULL);
886 close(fileId);
887 return TCL_ERROR;
888 }
889 close(fileId);
890 buffer[statBuf.st_size] = 0;
891 result = AddFromString(interp, tkwin, buffer, priority);
892 ckfree(buffer);
893 return result;
894 }
895 \f
896 /*
897 *--------------------------------------------------------------
898 *
899 * NewArray --
900 *
901 * Create a new ElArray structure of a given size.
902 *
903 * Results:
904 * The return value is a pointer to a properly initialized
905 * element array with "numEls" space. The array is marked
906 * as having no active elements.
907 *
908 * Side effects:
909 * Memory is allocated.
910 *
911 *--------------------------------------------------------------
912 */
913
914 static ElArray *
915 NewArray(
916 int numEls /* How many elements of space to allocate. */
917 )
918 {
919 register ElArray *arrayPtr;
920
921 arrayPtr = (ElArray *) ckalloc(EL_ARRAY_SIZE(numEls));
922 arrayPtr->arraySize = numEls;
923 arrayPtr->numUsed = 0;
924 arrayPtr->nextToUse = arrayPtr->els;
925 return arrayPtr;
926 }
927 \f
928 /*
929 *--------------------------------------------------------------
930 *
931 * ExtendArray --
932 *
933 * Add a new element to an array, extending the array if
934 * necessary.
935 *
936 * Results:
937 * The return value is a pointer to the new array, which
938 * will be different from arrayPtr if the array got expanded.
939 *
940 * Side effects:
941 * Memory may be allocated or freed.
942 *
943 *--------------------------------------------------------------
944 */
945
946 static ElArray *
947 ExtendArray(
948 register ElArray *arrayPtr, /* Array to be extended. */
949 register Element *elPtr /* Element to be copied into array. */
950 )
951 {
952 /*
953 * If the current array has filled up, make it bigger.
954 */
955
956 if (arrayPtr->numUsed >= arrayPtr->arraySize) {
957 register ElArray *newPtr;
958
959 newPtr = (ElArray *) ckalloc(EL_ARRAY_SIZE(2*arrayPtr->arraySize));
960 newPtr->arraySize = 2*arrayPtr->arraySize;
961 newPtr->numUsed = arrayPtr->numUsed;
962 newPtr->nextToUse = &newPtr->els[newPtr->numUsed];
963 memcpy((VOID *) newPtr->els, (VOID *) arrayPtr->els,
964 (arrayPtr->arraySize*sizeof(Element)));
965 ckfree((char *) arrayPtr);
966 arrayPtr = newPtr;
967 }
968
969 *arrayPtr->nextToUse = *elPtr;
970 arrayPtr->nextToUse++;
971 arrayPtr->numUsed++;
972 return arrayPtr;
973 }
974 \f
975 /*
976 *--------------------------------------------------------------
977 *
978 * SetupStacks --
979 *
980 * Arrange the stacks so that they cache all the option
981 * information for a particular window.
982 *
983 * Results:
984 * None.
985 *
986 * Side effects:
987 * The stacks are modified to hold information for tkwin
988 * and all its ancestors in the window hierarchy.
989 *
990 *--------------------------------------------------------------
991 */
992
993 static void
994 SetupStacks(
995 TkWindow *winPtr, /* Window for which information is to
996 * be cached. */
997 int leaf /* Non-zero means this is the leaf
998 * window being probed. Zero means this
999 * is an ancestor of the desired leaf. */
1000 )
1001 {
1002 int level, i, *iPtr;
1003 register StackLevel *levelPtr;
1004 register ElArray *arrayPtr;
1005
1006 /*
1007 * The following array defines the order in which the current
1008 * stacks are searched to find matching entries to add to the
1009 * stacks. Given the current priority-based scheme, the order
1010 * below is no longer relevant; all that matters is that an
1011 * element is on the list *somewhere*. The ordering is a relic
1012 * of the old days when priorities were determined differently.
1013 */
1014
1015 static int searchOrder[] = {WILDCARD_NODE_CLASS, WILDCARD_NODE_NAME,
1016 EXACT_NODE_CLASS, EXACT_NODE_NAME, -1};
1017
1018 if (winPtr->mainPtr->optionRootPtr == NULL) {
1019 OptionInit(winPtr->mainPtr);
1020 }
1021
1022 /*
1023 * Step 1: make sure that options are cached for this window's
1024 * parent.
1025 */
1026
1027 if (winPtr->parentPtr != NULL) {
1028 level = winPtr->parentPtr->optionLevel;
1029 if ((level == -1) || (cachedWindow == NULL)) {
1030 SetupStacks(winPtr->parentPtr, 0);
1031 level = winPtr->parentPtr->optionLevel;
1032 }
1033 level++;
1034 } else {
1035 level = 1;
1036 }
1037
1038 /*
1039 * Step 2: pop extra unneeded information off the stacks and
1040 * mark those windows as no longer having cached information.
1041 */
1042
1043 if (curLevel >= level) {
1044 while (curLevel >= level) {
1045 levels[curLevel].winPtr->optionLevel = -1;
1046 curLevel--;
1047 }
1048 levelPtr = &levels[level];
1049 for (i = 0; i < NUM_STACKS; i++) {
1050 arrayPtr = stacks[i];
1051 arrayPtr->numUsed = levelPtr->bases[i];
1052 arrayPtr->nextToUse = &arrayPtr->els[arrayPtr->numUsed];
1053 }
1054 }
1055 curLevel = winPtr->optionLevel = level;
1056
1057 /*
1058 * Step 3: if the root database information isn't loaded or
1059 * isn't valid, initialize level 0 of the stack from the
1060 * database root (this only happens if winPtr is a main window).
1061 */
1062
1063 if ((curLevel == 1)
1064 && ((cachedWindow == NULL)
1065 || (cachedWindow->mainPtr != winPtr->mainPtr))) {
1066 for (i = 0; i < NUM_STACKS; i++) {
1067 arrayPtr = stacks[i];
1068 arrayPtr->numUsed = 0;
1069 arrayPtr->nextToUse = arrayPtr->els;
1070 }
1071 ExtendStacks(winPtr->mainPtr->optionRootPtr, 0);
1072 }
1073
1074 /*
1075 * Step 4: create a new stack level; grow the level array if
1076 * we've run out of levels. Clear the stacks for EXACT_LEAF_NAME
1077 * and EXACT_LEAF_CLASS (anything that was there is of no use
1078 * any more).
1079 */
1080
1081 if (curLevel >= numLevels) {
1082 StackLevel *newLevels;
1083
1084 newLevels = (StackLevel *) ckalloc((unsigned)
1085 (numLevels*2*sizeof(StackLevel)));
1086 memcpy((VOID *) newLevels, (VOID *) levels,
1087 (numLevels*sizeof(StackLevel)));
1088 ckfree((char *) levels);
1089 numLevels *= 2;
1090 levels = newLevels;
1091 }
1092 levelPtr = &levels[curLevel];
1093 levelPtr->winPtr = winPtr;
1094 arrayPtr = stacks[EXACT_LEAF_NAME];
1095 arrayPtr->numUsed = 0;
1096 arrayPtr->nextToUse = arrayPtr->els;
1097 arrayPtr = stacks[EXACT_LEAF_CLASS];
1098 arrayPtr->numUsed = 0;
1099 arrayPtr->nextToUse = arrayPtr->els;
1100 levelPtr->bases[EXACT_LEAF_NAME] = stacks[EXACT_LEAF_NAME]->numUsed;
1101 levelPtr->bases[EXACT_LEAF_CLASS] = stacks[EXACT_LEAF_CLASS]->numUsed;
1102 levelPtr->bases[EXACT_NODE_NAME] = stacks[EXACT_NODE_NAME]->numUsed;
1103 levelPtr->bases[EXACT_NODE_CLASS] = stacks[EXACT_NODE_CLASS]->numUsed;
1104 levelPtr->bases[WILDCARD_LEAF_NAME] = stacks[WILDCARD_LEAF_NAME]->numUsed;
1105 levelPtr->bases[WILDCARD_LEAF_CLASS] = stacks[WILDCARD_LEAF_CLASS]->numUsed;
1106 levelPtr->bases[WILDCARD_NODE_NAME] = stacks[WILDCARD_NODE_NAME]->numUsed;
1107 levelPtr->bases[WILDCARD_NODE_CLASS] = stacks[WILDCARD_NODE_CLASS]->numUsed;
1108
1109
1110 /*
1111 * Step 5: scan the current stack level looking for matches to this
1112 * window's name or class; where found, add new information to the
1113 * stacks.
1114 */
1115
1116 for (iPtr = searchOrder; *iPtr != -1; iPtr++) {
1117 register Element *elPtr;
1118 int count;
1119 Tk_Uid id;
1120
1121 i = *iPtr;
1122 if (i & CLASS) {
1123 id = winPtr->classUid;
1124 } else {
1125 id = winPtr->nameUid;
1126 }
1127 elPtr = stacks[i]->els;
1128 count = levelPtr->bases[i];
1129
1130 /*
1131 * For wildcard stacks, check all entries; for non-wildcard
1132 * stacks, only check things that matched in the parent.
1133 */
1134
1135 if (!(i & WILDCARD)) {
1136 elPtr += levelPtr[-1].bases[i];
1137 count -= levelPtr[-1].bases[i];
1138 }
1139 for ( ; count > 0; elPtr++, count--) {
1140 if (elPtr->nameUid != id) {
1141 continue;
1142 }
1143 ExtendStacks(elPtr->child.arrayPtr, leaf);
1144 }
1145 }
1146 cachedWindow = winPtr;
1147 }
1148 \f
1149 /*
1150 *--------------------------------------------------------------
1151 *
1152 * ExtendStacks --
1153 *
1154 * Given an element array, copy all the elements from the
1155 * array onto the system stacks (except for irrelevant leaf
1156 * elements).
1157 *
1158 * Results:
1159 * None.
1160 *
1161 * Side effects:
1162 * The option stacks are extended.
1163 *
1164 *--------------------------------------------------------------
1165 */
1166
1167 static void
1168 ExtendStacks(
1169 ElArray *arrayPtr, /* Array of elements to copy onto stacks. */
1170 int leaf /* If zero, then don't copy exact leaf
1171 * elements. */
1172 )
1173 {
1174 register int count;
1175 register Element *elPtr;
1176
1177 for (elPtr = arrayPtr->els, count = arrayPtr->numUsed;
1178 count > 0; elPtr++, count--) {
1179 if (!(elPtr->flags & (NODE|WILDCARD)) && !leaf) {
1180 continue;
1181 }
1182 stacks[elPtr->flags] = ExtendArray(stacks[elPtr->flags], elPtr);
1183 }
1184 }
1185 \f
1186 /*
1187 *--------------------------------------------------------------
1188 *
1189 * OptionInit --
1190 *
1191 * Initialize data structures for option handling.
1192 *
1193 * Results:
1194 * None.
1195 *
1196 * Side effects:
1197 * Option-related data structures get initialized.
1198 *
1199 *--------------------------------------------------------------
1200 */
1201
1202 static void
1203 OptionInit(
1204 register TkMainInfo *mainPtr /* Top-level information about
1205 * window that isn't initialized
1206 * yet. */
1207 )
1208 {
1209 int i;
1210 Tcl_Interp *interp;
1211
1212 /*
1213 * First, once-only initialization.
1214 */
1215
1216 if (numLevels == 0) {
1217
1218 numLevels = 5;
1219 levels = (StackLevel *) ckalloc((unsigned) (5*sizeof(StackLevel)));
1220 for (i = 0; i < NUM_STACKS; i++) {
1221 stacks[i] = NewArray(10);
1222 levels[0].bases[i] = 0;
1223 }
1224
1225 defaultMatch.nameUid = NULL;
1226 defaultMatch.child.valueUid = NULL;
1227 defaultMatch.priority = -1;
1228 defaultMatch.flags = 0;
1229 }
1230
1231 /*
1232 * Then, per-main-window initialization. Create and delete dummy
1233 * interpreter for message logging.
1234 */
1235
1236 mainPtr->optionRootPtr = NewArray(20);
1237 interp = Tcl_CreateInterp();
1238 (void) GetDefaultOptions(interp, mainPtr->winPtr);
1239 Tcl_DeleteInterp(interp);
1240 }
1241 \f
1242 /*
1243 *--------------------------------------------------------------
1244 *
1245 * ClearOptionTree --
1246 *
1247 * This procedure is called to erase everything in a
1248 * hierarchical option database.
1249 *
1250 * Results:
1251 * None.
1252 *
1253 * Side effects:
1254 * All the options associated with arrayPtr are deleted,
1255 * along with all option subtrees. The space pointed to
1256 * by arrayPtr is freed.
1257 *
1258 *--------------------------------------------------------------
1259 */
1260
1261 static void
1262 ClearOptionTree(
1263 ElArray *arrayPtr /* Array of options; delete everything
1264 * referred to recursively by this. */
1265 )
1266 {
1267 register Element *elPtr;
1268 int count;
1269
1270 for (count = arrayPtr->numUsed, elPtr = arrayPtr->els; count > 0;
1271 count--, elPtr++) {
1272 if (elPtr->flags & NODE) {
1273 ClearOptionTree(elPtr->child.arrayPtr);
1274 }
1275 }
1276 ckfree((char *) arrayPtr);
1277 }
1278 \f
1279 /*
1280 *--------------------------------------------------------------
1281 *
1282 * GetDefaultOptions --
1283 *
1284 * This procedure is invoked to load the default set of options
1285 * for a window.
1286 *
1287 * Results:
1288 * None.
1289 *
1290 * Side effects:
1291 * Options are added to those for winPtr's main window. If
1292 * there exists a RESOURCE_MANAGER proprety for winPtr's
1293 * display, that is used. Otherwise, the .Xdefaults file in
1294 * the user's home directory is used.
1295 *
1296 *--------------------------------------------------------------
1297 */
1298
1299 static int
1300 GetDefaultOptions(
1301 Tcl_Interp *interp, /* Interpreter to use for error reporting. */
1302 TkWindow *winPtr /* Fetch option defaults for main window
1303 * associated with this. */
1304 )
1305 {
1306 char *regProp, *home, *fileName;
1307 int result, actualFormat;
1308 unsigned long numItems, bytesAfter;
1309 Atom actualType;
1310
1311 /*
1312 * Try the RESOURCE_MANAGER property on the root window first.
1313 */
1314
1315 regProp = NULL;
1316 result = XGetWindowProperty(winPtr->display,
1317 Tk_DefaultRootWindow(winPtr->display),
1318 XA_RESOURCE_MANAGER, 0, 100000,
1319 False, XA_STRING, &actualType, &actualFormat,
1320 &numItems, &bytesAfter, (unsigned char **) &regProp);
1321
1322 if ((result == Success) && (actualType == XA_STRING)
1323 && (actualFormat == 8)) {
1324 result = AddFromString(interp, (Tk_Window) winPtr, regProp,
1325 TK_USER_DEFAULT_PRIO);
1326 XFree(regProp);
1327 return result;
1328 }
1329
1330 /*
1331 * No luck there. Try a .Xdefaults file in the user's home
1332 * directory.
1333 */
1334
1335 if (regProp != NULL) {
1336 XFree(regProp);
1337 }
1338 home = getenv("HOME");
1339 if (home == NULL) {
1340 sprintf(interp->result,
1341 "no RESOURCE_MANAGER property and no HOME envariable");
1342 return TCL_ERROR;
1343 }
1344 fileName = (char *) ckalloc((unsigned) (strlen(home) + 20));
1345 sprintf(fileName, "%s/.Xdefaults", home);
1346 result = ReadOptionFile(interp, (Tk_Window) winPtr, fileName,
1347 TK_USER_DEFAULT_PRIO);
1348 ckfree(fileName);
1349 return result;
1350 }
Impressum, Datenschutz