Debian Codegraph @

pTk/XrmOption.c

raw symbols SCIP
1 /*
2 * XrmOption.c --
3 *
4 * Copyright (c) 1990-1994 The Regents of the University of California.
5 * Copyright (c) 1994-1995 Sun Microsystems, Inc.
6 *
7 * See the file "license.terms" for information on usage and redistribution
8 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 */
10
11 #if !defined(__WIN32__) && !defined(_WIN32) && !defined(__PM__)
12
13 static char sccsid[] = "@(#) tkOption.c 1.41 95/06/25 15:30:42";
14
15 #include "tkPort.h"
16 #include "tkInt.h"
17 #include "tkOption.h"
18 #include "tkOption_f.h"
19 #include "tkVMacro.h"
20 #include <X11/Xresource.h>
21 #include "tkXrm.h"
22
23 #if !defined(XlibSpecificationRelease) || XlibSpecificationRelease < 5
24
25 #define XrmPermStringToQuark(x) XrmStringToQuark(x)
26
27 static XrmDatabase
28 XrmGetDatabase(display)
29 Display *display;
30 {
31 return (XrmDatabase) NULL;
32 }
33
34 static void
35 XrmSetDatabase(display, db)
36 Display *display;
37 XrmDatabase db;
38 {
39
40 }
41
42 static void
43 XrmCombineFileDatabase(filename, target_db, override)
44 char *filename;
45 XrmDatabase *target_db;
46 Bool override;
47 {
48 XrmDatabase source_db = XrmGetFileDatabase(filename);
49 if (override || !*target_db)
50 XrmMergeDatabases(source_db, target_db);
51 else
52 {
53 XrmMergeDatabases(*target_db, &source_db);
54 *target_db = source_db;
55 }
56 }
57
58 #endif
59
60 /*
61 * Flags in Element structures:
62 *
63 * CLASS - Non-zero means this element refers to a class,
64 * Zero means this element refers to a name.
65 * NODE - Zero means this is a leaf element (the child
66 * field is a value, not a pointer to another node).
67 * One means this is a node element.
68 * WILDCARD - Non-zero means this there was a star in the
69 * original specification just before this element.
70 * Zero means there was a dot.
71 */
72
73 typedef void ElArray;
74
75 static void OptionInit _ANSI_ARGS_((TkMainInfo * mainPtr));
76 static int ParsePriority _ANSI_ARGS_((Tcl_Interp * interp,
77 char *string));
78 static void ClearOptionTree _ANSI_ARGS_((TkMainInfo * mainPtr));
79 static int ReadOptionFile _ANSI_ARGS_((Tcl_Interp * interp,
80 Tk_Window tkwin, char *fileName, int priority));
81 static int SetupQuarks _ANSI_ARGS_((TkWindow *winPtr,int depth));
82
83
84 static TkWindow *cachedWindow = NULL;
85 static XrmQuarkList Qname; /* name Quark list (malloced) */
86 static XrmQuarkList Qclass; /* class Quark list (malloced) */
87 static int Qsize = 0; /* Number of slots in Quark lists */
88 static int Qindex = 0; /* index just beyond cacheWindow in Quark list */
89
90
91 #ifdef XRM_DEBUG
92 static void
93 Qshow(FILE *f,char *lab,XrmQuarkList l)
94 {
95 int prefix = '"';
96 fprintf(f," %s:",lab);
97 while (*l != NULLQUARK)
98 {
99 fprintf(f,"%c%s",prefix,XrmQuarkToString(*l++));
100 prefix = '.';
101 }
102 fprintf(f,"\"\n");
103 }
104 #endif
105
106 static int
107 SetupQuarks(winPtr,depth)
108 TkWindow *winPtr;
109 int depth;
110 {
111 if (cachedWindow != NULL && cachedWindow->mainPtr == winPtr->mainPtr)
112 {
113 TkWindow *c = cachedWindow;
114 int index = Qindex;
115 while (c != NULL)
116 {
117 if (winPtr == c)
118 {
119 if (index + depth > Qsize)
120 {size_t size = (Qsize = depth+Qindex+5) * sizeof(XrmQuark);
121 Qname = (XrmQuarkList) ckrealloc((char *)Qname,size);
122 Qclass = (XrmQuarkList) ckrealloc((char *)Qclass,size);
123 }
124 #ifdef XRM_DEBUG
125 printf("using %d for %s\n",index,Tk_PathName(c));
126 #endif
127 return index;
128 }
129 index--;
130 c = c->parentPtr;
131 }
132 }
133 if (winPtr->parentPtr != NULL)
134 depth = SetupQuarks(winPtr->parentPtr,depth+1);
135 else
136 {
137 if (depth > Qsize)
138 {
139 size_t size = (Qsize = depth+5) * sizeof(XrmQuark);
140 Qname = (XrmQuarkList)((Qname) ? ckrealloc((char *)Qname,size) : ckalloc(size));
141 Qclass = (XrmQuarkList)((Qclass) ? ckrealloc((char *)Qclass,size) : ckalloc(size));
142 }
143 depth = 0;
144 }
145 Qname[depth] = XrmPermStringToQuark(Tk_Name(winPtr));
146 Qclass[depth] = XrmPermStringToQuark(Tk_Class(winPtr));
147 return depth+1;
148 }
149
150 /*
151 *--------------------------------------------------------------
152 *
153 * Xrm_GetOption --
154 *
155 * Retrieve an option from the option database.
156 *
157 * Results:
158 * The return value is the value specified in the option
159 * database for the given name and class on the given
160 * window. If there is nothing specified in the database
161 * for that option, then NULL is returned.
162 *
163 * Side effects:
164 * The internal caches used to speed up option mapping
165 * may be modified, if this tkwin is different from the
166 * last tkwin used for option retrieval.
167 *
168 *--------------------------------------------------------------
169 */
170
171 Tk_Uid
172 Xrm_GetOption(tkwin, name, className)
173 Tk_Window tkwin; /* Token for window that option is
174 * associated with. */
175 CONST char *name; /* Name of option. */
176 CONST char *className; /* Class of option. NULL means there
177 * is no class for this option: just
178 * check for name. */
179 {
180 TkWindow *winPtr = (TkWindow *) tkwin;
181 XrmDatabase database;
182 XrmRepresentation type = NULLQUARK;
183 XrmValue value;
184
185 if (winPtr->mainPtr->optionRootPtr == NULL)
186 {
187 OptionInit(winPtr->mainPtr);
188 }
189
190 if (winPtr != cachedWindow)
191 {
192 Qindex = SetupQuarks(winPtr,3);
193 cachedWindow = winPtr;
194 }
195
196 #ifdef DEBUGGING
197 if (Qindex + 1 > Qsize)
198 abort();
199 #endif
200
201 Qname[Qindex] = XrmStringToQuark(name);
202 Qclass[Qindex] = XrmStringToQuark(className);
203
204 Qname[Qindex+1] = NULLQUARK;
205 Qclass[Qindex+1] = NULLQUARK;
206
207 #ifdef XRM_DEBUG
208 printf("%s:\n",Tk_PathName(winPtr));
209 Qshow(stdout,"name",Qname);
210 Qshow(stdout,"Class",Qclass);
211 #endif
212
213 database = (XrmDatabase) winPtr->mainPtr->optionRootPtr;
214
215 memset(&value,0,sizeof(value));
216
217 if (database && XrmQGetResource(database, Qname, Qclass, &type, &value))
218 {
219 #ifdef XRM_DEBUG
220 fprintf(stdout, "%s(%s/%s) type=%s val=%.*s\n",
221 Tk_PathName(winPtr), name, className,
222 XrmQuarkToString(type), (int) value.size, value.addr);
223 #endif
224 return Tk_GetUid(value.addr);
225 }
226 return NULL;
227 }
228
229
230 /*
231 *--------------------------------------------------------------
232 *
233 * Xrm_AddOption --
234 *
235 * Add a new option to the option database.
236 *
237 * Results:
238 * None.
239 *
240 * Side effects:
241 * Information is added to the option database.
242 *
243 *--------------------------------------------------------------
244 */
245
246 #undef Xrm_AddOption
247 void
248 Xrm_AddOption(tkwin, name, value, priority)
249 Tk_Window tkwin; /* Window token; option will be associated
250 * with main window for this window. */
251 CONST char *name; /* Multi-element name of option. */
252 CONST char *value; /* String value for option. */
253 int priority; /* Overall priority level to use for
254 * this option, such as TK_USER_DEFAULT_PRIO
255 * or TK_INTERACTIVE_PRIO. Must be between
256 * 0 and TK_MAX_PRIO. */
257 {
258 TkWindow *winPtr = ((TkWindow *) tkwin)->mainPtr->winPtr;
259 XrmDatabase database;
260
261 if (winPtr->mainPtr->optionRootPtr == NULL)
262 {
263 OptionInit(winPtr->mainPtr);
264 }
265 database = (XrmDatabase) winPtr->mainPtr->optionRootPtr;
266
267 /* XrmMergeDatabases ? */
268
269 XrmPutStringResource(&database, name, value);
270 #ifdef XRM_DEBUG
271 fprintf(stdout, "Xrm_AddOption %p %s %s %d\n", database, name, value, priority);
272 #endif
273 }
274
275 /*
276 *--------------------------------------------------------------
277 *
278 * Xrm_OptionCmd --
279 *
280 * This procedure is invoked to process the "option" Tcl command.
281 * See the user documentation for details on what it does.
282 *
283 * Results:
284 * A standard Tcl result.
285 *
286 * Side effects:
287 * See the user documentation.
288 *
289 *--------------------------------------------------------------
290 */
291
292 int
293 Xrm_OptionCmd(clientData, interp, argc, args)
294 ClientData clientData; /* Main window associated with
295 * interpreter. */
296 Tcl_Interp *interp; /* Current interpreter. */
297 int argc; /* Number of arguments. */
298 Tcl_Obj *CONST args[]; /* Argument strings. */
299 {
300 Tk_Window tkwin = (Tk_Window) clientData;
301 size_t length;
302 char c;
303
304 if (argc < 2)
305 {
306 Tcl_AppendResult(interp, "wrong # args: should be \"", Tcl_GetString(args[0]),
307 " cmd arg ?arg ...?\"", NULL);
308 return TCL_ERROR;
309 }
310 c = Tcl_GetString(args[1])[0];
311 length = strlen(Tcl_GetString(args[1]));
312 if ((c == 'a') && (strncmp(Tcl_GetString(args[1]), "add", length) == 0))
313 {
314 int priority;
315
316 if ((argc != 4) && (argc != 5))
317 {
318 Tcl_AppendResult(interp, "wrong # args: should be \"",
319 Tcl_GetString(args[0]), " add pattern value ?priority?\"", NULL);
320 return TCL_ERROR;
321 }
322 if (argc == 4)
323 {
324 priority = TK_INTERACTIVE_PRIO;
325 }
326 else
327 {
328 priority = ParsePriority(interp, Tcl_GetString(args[4]));
329 if (priority < 0)
330 {
331 return TCL_ERROR;
332 }
333 }
334 Xrm_AddOption(tkwin, Tcl_GetString(args[2]), Tcl_GetString(args[3]), priority);
335 return TCL_OK;
336 }
337 else if ((c == 'c') && (strncmp(Tcl_GetString(args[1]), "clear", length) == 0))
338 {
339 TkMainInfo *mainPtr;
340
341 if (argc != 2)
342 {
343 Tcl_AppendResult(interp, "wrong # args: should be \"",
344 Tcl_GetString(args[0]), " clear\"", NULL);
345 return TCL_ERROR;
346 }
347 mainPtr = ((TkWindow *) tkwin)->mainPtr;
348 if (mainPtr->optionRootPtr != NULL)
349 {
350 ClearOptionTree(mainPtr);
351 mainPtr->optionRootPtr = NULL;
352 }
353 cachedWindow = NULL;
354 Qindex = 0;
355 return TCL_OK;
356 }
357 else if ((c == 'g') && (strncmp(Tcl_GetString(args[1]), "get", length) == 0))
358 {
359 Tk_Window window;
360 Tk_Uid value;
361
362 if (argc != 5)
363 {
364 Tcl_AppendResult(interp, "wrong # args: should be \"",
365 Tcl_GetString(args[0]), " get window name class\"", NULL);
366 return TCL_ERROR;
367 }
368 window = Tk_NameToWindow(interp, Tcl_GetString(args[2]), tkwin);
369 if (window == NULL)
370 {
371 return TCL_ERROR;
372 }
373 value = Xrm_GetOption(window, Tcl_GetString(args[3]), Tcl_GetString(args[4]));
374 if (value != NULL)
375 {
376 Tcl_SetResult(interp, (char *) value, TCL_STATIC);
377 }
378 return TCL_OK;
379 }
380 else if ((c == 'r') && (strncmp(Tcl_GetString(args[1]), "readfile", length) == 0))
381 {
382 int priority;
383
384 if ((argc != 3) && (argc != 4))
385 {
386 Tcl_AppendResult(interp, "wrong # args: should be \"",
387 Tcl_GetString(args[0]), " readfile fileName ?priority?\"",
388 NULL);
389 return TCL_ERROR;
390 }
391 if (argc == 4)
392 {
393 priority = ParsePriority(interp, Tcl_GetString(args[3]));
394 if (priority < 0)
395 {
396 return TCL_ERROR;
397 }
398 }
399 else
400 {
401 priority = TK_INTERACTIVE_PRIO;
402 }
403 return ReadOptionFile(interp, tkwin, Tcl_GetString(args[2]), priority);
404 }
405 else
406 {
407 Tcl_AppendResult(interp, "bad option \"", Tcl_GetString(args[1]),
408 "\": must be add, clear, get, or readfile", NULL);
409 return TCL_ERROR;
410 }
411 }
412
413 /*
414 *--------------------------------------------------------------
415 *
416 * TkOptionDeadWindow --
417 *
418 * This procedure is called whenever a window is deleted.
419 * It cleans up any option-related stuff associated with
420 * the window.
421 *
422 * Results:
423 * None.
424 *
425 * Side effects:
426 * Option-related resources are freed. See code below
427 * for details.
428 *
429 *--------------------------------------------------------------
430 */
431
432 void
433 XrmOptionDeadWindow(winPtr)
434 register TkWindow *winPtr; /* Window to be cleaned up. */
435 {
436 /*
437 * If this window was a main window, then delete its option
438 * database.
439 */
440
441 XrmOptionClassChanged(winPtr);
442
443 if ((winPtr->mainPtr != NULL) && (winPtr->mainPtr->winPtr == winPtr)
444 && (winPtr->mainPtr->optionRootPtr != NULL))
445 {
446 if (winPtr->dispPtr->refCount <= 0)
447 {
448 XrmDestroyDatabase((XrmDatabase) winPtr->mainPtr->optionRootPtr);
449 XrmSetDatabase(winPtr->display,(XrmDatabase) NULL);
450 }
451 winPtr->mainPtr->optionRootPtr = NULL;
452 }
453 }
454
455 /*
456 *----------------------------------------------------------------------
457 *
458 * TkOptionClassChanged --
459 *
460 * This procedure is invoked when a window's class changes. If
461 * the window is on the option cache, this procedure flushes
462 * any information for the window, since the new class could change
463 * what is relevant.
464 *
465 * Results:
466 * None.
467 *
468 * Side effects:
469 * The option cache may be flushed in part or in whole.
470 *
471 *----------------------------------------------------------------------
472 */
473 #undef TkOptionClassChanged
474 void
475 XrmOptionClassChanged(winPtr)
476 TkWindow *winPtr; /* Window whose class changed. */
477 {
478 if (winPtr == cachedWindow)
479 {
480 if (cachedWindow->parentPtr)
481 {
482 cachedWindow = cachedWindow->parentPtr;
483 Qindex--;
484 }
485 else
486 {
487 cachedWindow = NULL;
488 Qindex = 0;
489 }
490 }
491 }
492
493 /*
494 *----------------------------------------------------------------------
495 *
496 * ParsePriority --
497 *
498 * Parse a string priority value.
499 *
500 * Results:
501 * The return value is the integer priority level corresponding
502 * to string, or -1 if string doesn't point to a valid priority level.
503 * In this case, an error message is left in Tcl_GetResult(interp).
504 *
505 * Side effects:
506 * None.
507 *
508 *----------------------------------------------------------------------
509 */
510
511 static int
512 ParsePriority(interp, string)
513 Tcl_Interp *interp; /* Interpreter to use for error reporting. */
514 char *string; /* Describes a priority level, either
515 * symbolically or numerically. */
516 {
517 int priority,
518 c;
519 size_t length;
520
521 c = string[0];
522 length = strlen(string);
523 if ((c == 'w') && (strncmp(string, "widgetDefault", length) == 0))
524 {
525
526 return TK_WIDGET_DEFAULT_PRIO;
527 }
528 else if ((c == 's') && (strncmp(string, "startupFile", length) == 0))
529 {
530
531 return TK_STARTUP_FILE_PRIO;
532 }
533 else if ((c == 'u') && (strncmp(string, "userDefault", length) == 0))
534 {
535
536 return TK_USER_DEFAULT_PRIO;
537 }
538 else if ((c == 'i') && (strncmp(string, "interactive", length) == 0))
539 {
540
541 return TK_INTERACTIVE_PRIO;
542 }
543 else
544 {
545 char *end;
546
547 priority = strtoul(string, &end, 0);
548 if ((end == string) || (*end != 0) || (priority < 0)
549 || (priority > 100))
550 {
551 Tcl_AppendResult(interp, "bad priority level \"", string,
552 "\": must be widgetDefault, startupFile, userDefault, ",
553 "interactive, or a number between 0 and 100",
554 NULL);
555 return -1;
556 }
557 }
558 return priority;
559 }
560
561 /*
562 *----------------------------------------------------------------------
563 *
564 * ReadOptionFile --
565 *
566 * Read a file of options ("resources" in the old X terminology)
567 * and load them into the option database.
568 *
569 * Results:
570 * The return value is a standard Tcl return code. In the case of
571 * an error in parsing string, TCL_ERROR will be returned and an
572 * error message will be left in Tcl_GetResult(interp).
573 *
574 * Side effects:
575 * None.
576 *
577 *----------------------------------------------------------------------
578 */
579
580 static int
581 ReadOptionFile(interp, tkwin, fileName, priority)
582 Tcl_Interp *interp; /* Interpreter to use for reporting results. */
583 Tk_Window tkwin; /* Token for window: options are entered
584 * for this window's main window. */
585 char *fileName; /* Name of file containing options. */
586 int priority; /* Priority level to use for options in
587 * this file, such as TK_USER_DEFAULT_PRIO
588 * or TK_INTERACTIVE_PRIO. Must be between
589 * 0 and TK_MAX_PRIO. */
590 {
591 int result = TCL_OK;
592 Tcl_DString newName;
593 char *realName = Tcl_TranslateFileName(interp, fileName, &newName);
594
595 if (realName != NULL)
596 {
597 XrmDatabase database = XrmGetFileDatabase(realName);
598 if (database)
599 {
600 TkWindow *winPtr = (TkWindow *) tkwin;
601 if (priority > TK_WIDGET_DEFAULT_PRIO &&
602 winPtr->mainPtr->optionRootPtr == NULL)
603 {
604 OptionInit(winPtr->mainPtr);
605 }
606 XrmCombineFileDatabase(realName, (XrmDatabase *)(&winPtr->mainPtr->optionRootPtr),
607 (priority > TK_STARTUP_FILE_PRIO));
608 }
609 else
610 {
611 Tcl_AppendResult(interp, "couldn't read file \"", realName, "\"",
612 NULL);
613 result = TCL_ERROR;
614 }
615 }
616 else
617 {
618 return TCL_ERROR;
619 }
620 Tcl_DStringFree(&newName);
621 return result;
622 }
623
624 /*
625 *--------------------------------------------------------------
626 *
627 * OptionInit --
628 *
629 * Initialize data structures for option handling.
630 *
631 * Results:
632 * None.
633 *
634 * Side effects:
635 * Option-related data structures get initialized.
636 *
637 *--------------------------------------------------------------
638 */
639
640 static void
641 OptionInit(mainPtr)
642 register TkMainInfo *mainPtr; /* Top-level information about
643 * window that isn't initialized
644 * yet. */
645 {
646 TkWindow *winPtr = mainPtr->winPtr;
647 static int initialized = 0;
648 /*
649 * First, once-only initialization.
650 */
651
652 if (!initialized)
653 {
654 size_t size = (Qsize = 32) * sizeof(XrmQuark);
655 XrmInitialize();
656 Qindex = 0;
657 Qname = (XrmQuarkList) ckalloc(size);
658 Qclass = (XrmQuarkList) ckalloc(size);
659 initialized = 1;
660 }
661
662 /*
663 * Then, per-main-window initialization.
664 */
665
666 mainPtr->optionRootPtr = (void *) XrmGetDatabase(winPtr->display);
667
668 if (!mainPtr->optionRootPtr)
669 {
670 /*
671 * Try the RESOURCE_MANAGER property on the root window first.
672 */
673
674 if (XResourceManagerString(winPtr->display) != NULL)
675 {
676 mainPtr->optionRootPtr = (void *) XrmGetStringDatabase(XResourceManagerString(winPtr->display));
677 }
678 else
679 {
680 char *regProp = NULL;
681 int actualFormat;
682 unsigned long numItems, bytesAfter;
683 Atom actualType;
684 int result = XGetWindowProperty(winPtr->display,
685 RootWindow(winPtr->display, 0),
686 XA_RESOURCE_MANAGER, 0, 100000,
687 False, XA_STRING, &actualType, &actualFormat,
688 &numItems, &bytesAfter, (unsigned char **) &regProp);
689
690 if ((result == Success) && (actualType == XA_STRING) && (actualFormat == 8))
691 {
692 mainPtr->optionRootPtr = (void *) XrmGetStringDatabase(regProp);
693 XFree(regProp);
694 }
695 else
696 {
697 char *home = getenv("HOME");
698 /*
699 * No luck there. Try a .Xdefaults file in the user's home
700 * directory.
701 */
702 if (regProp != NULL)
703 XFree(regProp);
704 if (home != NULL)
705 {
706 char *fileName = (char *) ckalloc((unsigned) (strlen(home) + 20));
707 sprintf(fileName, "%s/.Xdefaults", home);
708 mainPtr->optionRootPtr = (void *) XrmGetFileDatabase(fileName);
709 ckfree(fileName);
710 }
711 }
712 }
713 if (mainPtr->optionRootPtr)
714 {
715 XrmSetDatabase(winPtr->display, (XrmDatabase) mainPtr->optionRootPtr);
716 }
717 }
718 }
719
720 /*
721 *--------------------------------------------------------------
722 *
723 * ClearOptionTree --
724 *
725 * This procedure is called to erase everything in a
726 * hierarchical option database.
727 *
728 * Results:
729 * None.
730 *
731 * Side effects:
732 * All the options associated with arrayPtr are deleted,
733 * along with all option subtrees. The space pointed to
734 * by arrayPtr is freed.
735 *
736 *--------------------------------------------------------------
737 */
738
739 static void
740 ClearOptionTree(mainPtr)
741 TkMainInfo *mainPtr;
742 {
743 if (mainPtr->optionRootPtr)
744 {
745 mainPtr->optionRootPtr = NULL;
746 }
747 }
748
749 #endif
750
751 void
752 Xrm_import(class)
753 char *class;
754 {
755 #if !defined(__WIN32__) && !defined(_WIN32) && !defined(__PM__)
756 /* This is sneaky - we patch up the function tables so
757 that calls to Tk*Option*() map to Xrm versions.
758 */
759 LangOptionCommand = Xrm_OptionCmd;
760 TkoptionVptr->V_Tk_AddOption = Xrm_AddOption;
761 TkoptionVptr->V_Tk_GetOption = Xrm_GetOption;
762 TkoptionVptr->V_TkOptionClassChanged = XrmOptionClassChanged;
763 TkoptionVptr->V_TkOptionDeadWindow = XrmOptionDeadWindow;
764 #endif
765 }
766
767