-
Notifications
You must be signed in to change notification settings - Fork 1
/
ckOption.c
1354 lines (1227 loc) · 36.9 KB
/
ckOption.c
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ckOption.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-1995 Sun Microsystems, Inc.
* Copyright (c) 1995 Christian Werner
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include <stdio.h>
#include "ckPort.h"
#include "ck.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 {
Ck_Uid nameUid; /* Name or class from one element of
* an option spec. */
union {
struct ElArray *arrayPtr; /* If this is an intermediate node,
* a pointer to a structure describing
* the remaining elements of all
* options whose prefixes are the
* same up through this element. */
Ck_Uid valueUid; /* For leaf nodes, this is the string
* value of the option. */
} child;
int priority; /* Used to select among matching
* options. Includes both the
* priority level and a serial #.
* Greater value means higher
* priority. Irrelevant except in
* leaf nodes. */
int flags; /* OR-ed combination of bits. See
* 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.
*/
#define TYPE_MASK 0x7
#define CLASS 0x1
#define NODE 0x2
#define WILDCARD 0x4
#define EXACT_LEAF_NAME 0x0
#define EXACT_LEAF_CLASS 0x1
#define EXACT_NODE_NAME 0x2
#define EXACT_NODE_CLASS 0x3
#define WILDCARD_LEAF_NAME 0x4
#define WILDCARD_LEAF_CLASS 0x5
#define WILDCARD_NODE_NAME 0x6
#define WILDCARD_NODE_CLASS 0x7
/*
* 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 {
int arraySize; /* Number of elements actually
* allocated in the "els" array. */
int numUsed; /* Number of elements currently in
* use out of els. */
Element *nextToUse; /* Pointer to &els[numUsed]. */
Element els[1]; /* Array of structures describing
* 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;
#define EL_ARRAY_SIZE(numEls) ((unsigned) (sizeof(ElArray) \
+ ((numEls)-1)*sizeof(Element)))
#define INITIAL_SIZE 5
/*
* 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
* their last name/class, so during the first request we extract all
* 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.
*/
#define NUM_STACKS 8
static ElArray *stacks[NUM_STACKS];
static CkWindow *cachedWindow = NULL; /* Lowest-level window currently
* 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 {
CkWindow *winPtr; /* Window corresponding to this stack
* level. */
int bases[NUM_STACKS]; /* For each stack, index of first
* 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.
*/
static StackLevel *levels = NULL;
/* Array describing current stack. */
static int numLevels = 0; /* Total space allocated. */
static int curLevel = -1; /* Highest level currently in use. Note:
* 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.
*/
static Element defaultMatch;
/*
* Forward declarations for procedures defined in this file:
*/
static int AddFromString _ANSI_ARGS_((Tcl_Interp *interp,
CkWindow *winPtr, char *string, int priority));
static void ClearOptionTree _ANSI_ARGS_((ElArray *arrayPtr));
static ElArray * ExtendArray _ANSI_ARGS_((ElArray *arrayPtr,
Element *elPtr));
static void ExtendStacks _ANSI_ARGS_((ElArray *arrayPtr,
int leaf));
static ElArray * NewArray _ANSI_ARGS_((int numEls));
static void OptionInit _ANSI_ARGS_((CkMainInfo *mainPtr));
static int ParsePriority _ANSI_ARGS_((Tcl_Interp *interp,
char *string));
static int ReadOptionFile _ANSI_ARGS_((Tcl_Interp *interp,
CkWindow *winPtr, char *fileName, int priority));
static void SetupStacks _ANSI_ARGS_((CkWindow *winPtr, int leaf));
/*
*--------------------------------------------------------------
*
* Ck_AddOption --
*
* Add a new option to the option database.
*
* Results:
* None.
*
* Side effects:
* Information is added to the option database.
*
*--------------------------------------------------------------
*/
void
Ck_AddOption(winPtr, name, value, priority)
CkWindow *winPtr; /* Window pointer; option will be associated
* 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 CK_USER_DEFAULT_PRIO
* or CK_INTERACTIVE_PRIO. Must be between
* 0 and CK_MAX_PRIO. */
{
register ElArray **arrayPtrPtr;
register Element *elPtr;
Element newEl;
register char *p;
char *field;
int count, firstField, length;
#define TMP_SIZE 100
char tmp[TMP_SIZE+1];
winPtr = winPtr->mainPtr->winPtr;
if (winPtr->mainPtr->optionRootPtr == NULL) {
OptionInit(winPtr->mainPtr);
}
cachedWindow = NULL; /* Invalidate the cache. */
/*
* 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 > CK_MAX_PRIO) {
priority = CK_MAX_PRIO;
}
newEl.priority = (priority << 24) + serial;
serial++;
/*
* Parse the option one field at a time.
*/
arrayPtrPtr = &(winPtr->mainPtr->optionRootPtr);
p = name;
for (firstField = 1; ; firstField = 0) {
/*
* 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 == '*') {
newEl.flags = WILDCARD;
p++;
} else {
newEl.flags = 0;
}
field = p;
while ((*p != 0) && (*p != '.') && (*p != '*')) {
p++;
}
length = p - field;
if (length > TMP_SIZE) {
length = TMP_SIZE;
}
strncpy(tmp, field, (size_t) length);
tmp[length] = 0;
newEl.nameUid = Ck_GetUid(tmp);
if (isupper((unsigned char) *field)) {
newEl.flags |= CLASS;
}
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.
*/
newEl.flags |= NODE;
if (firstField && !(newEl.flags & WILDCARD)
&& (newEl.nameUid != winPtr->nameUid)
&& (newEl.nameUid != winPtr->classUid)) {
return;
}
for (elPtr = (*arrayPtrPtr)->els, count = (*arrayPtrPtr)->numUsed;
; elPtr++, count--) {
if (count == 0) {
newEl.child.arrayPtr = NewArray(5);
*arrayPtrPtr = ExtendArray(*arrayPtrPtr, &newEl);
arrayPtrPtr = &((*arrayPtrPtr)->nextToUse[-1].child.arrayPtr);
break;
}
if ((elPtr->nameUid == newEl.nameUid)
&& (elPtr->flags == newEl.flags)) {
arrayPtrPtr = &(elPtr->child.arrayPtr);
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.
*/
newEl.child.valueUid = Ck_GetUid(value);
for (elPtr = (*arrayPtrPtr)->els, count = (*arrayPtrPtr)->numUsed;
; elPtr++, count--) {
if (count == 0) {
*arrayPtrPtr = ExtendArray(*arrayPtrPtr, &newEl);
return;
}
if ((elPtr->nameUid == newEl.nameUid)
&& (elPtr->flags == newEl.flags)) {
if (elPtr->priority < newEl.priority) {
elPtr->priority = newEl.priority;
elPtr->child.valueUid = newEl.child.valueUid;
}
return;
}
}
}
}
}
/*
*--------------------------------------------------------------
*
* Ck_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.
*
*--------------------------------------------------------------
*/
Ck_Uid
Ck_GetOption(winPtr, name, className)
CkWindow *winPtr; /* Pointer to window that option is
* 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. */
{
Ck_Uid nameId, classId;
register Element *elPtr, *bestPtr;
register int count;
/*
* Note: no need to call OptionInit here: it will be done by
* the SetupStacks call below (squeeze out those nanoseconds).
*/
if (winPtr != cachedWindow) {
SetupStacks(winPtr, 1);
}
nameId = Ck_GetUid(name);
bestPtr = &defaultMatch;
for (elPtr = stacks[EXACT_LEAF_NAME]->els,
count = stacks[EXACT_LEAF_NAME]->numUsed; count > 0;
elPtr++, count--) {
if ((elPtr->nameUid == nameId)
&& (elPtr->priority > bestPtr->priority)) {
bestPtr = elPtr;
}
}
for (elPtr = stacks[WILDCARD_LEAF_NAME]->els,
count = stacks[WILDCARD_LEAF_NAME]->numUsed; count > 0;
elPtr++, count--) {
if ((elPtr->nameUid == nameId)
&& (elPtr->priority > bestPtr->priority)) {
bestPtr = elPtr;
}
}
if (className != NULL) {
classId = Ck_GetUid(className);
for (elPtr = stacks[EXACT_LEAF_CLASS]->els,
count = stacks[EXACT_LEAF_CLASS]->numUsed; count > 0;
elPtr++, count--) {
if ((elPtr->nameUid == classId)
&& (elPtr->priority > bestPtr->priority)) {
bestPtr = elPtr;
}
}
for (elPtr = stacks[WILDCARD_LEAF_CLASS]->els,
count = stacks[WILDCARD_LEAF_CLASS]->numUsed; count > 0;
elPtr++, count--) {
if ((elPtr->nameUid == classId)
&& (elPtr->priority > bestPtr->priority)) {
bestPtr = elPtr;
}
}
}
return bestPtr->child.valueUid;
}
/*
*--------------------------------------------------------------
*
* Ck_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
Ck_OptionCmd(clientData, interp, argc, argv)
ClientData clientData; /* Main window associated with
* interpreter. */
Tcl_Interp *interp; /* Current interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
CkWindow *winPtr = (CkWindow *) clientData;
size_t length;
char c;
if (argc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" cmd arg ?arg ...?\"", (char *) NULL);
return TCL_ERROR;
}
c = argv[1][0];
length = strlen(argv[1]);
if ((c == 'a') && (strncmp(argv[1], "add", length) == 0)) {
int priority;
if ((argc != 4) && (argc != 5)) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " add pattern value ?priority?\"", (char *) NULL);
return TCL_ERROR;
}
if (argc == 4) {
priority = CK_INTERACTIVE_PRIO;
} else {
priority = ParsePriority(interp, argv[4]);
if (priority < 0) {
return TCL_ERROR;
}
}
Ck_AddOption(winPtr, argv[2], argv[3], priority);
return TCL_OK;
} else if ((c == 'c') && (strncmp(argv[1], "clear", length) == 0)) {
CkMainInfo *mainPtr;
if (argc != 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " clear\"", (char *) NULL);
return TCL_ERROR;
}
mainPtr = winPtr->mainPtr;
if (mainPtr->optionRootPtr != NULL) {
ClearOptionTree(mainPtr->optionRootPtr);
mainPtr->optionRootPtr = NULL;
}
cachedWindow = NULL;
return TCL_OK;
} else if ((c == 'g') && (strncmp(argv[1], "get", length) == 0)) {
CkWindow *winPtr2;
Ck_Uid value;
if (argc != 5) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " get window name class\"", (char *) NULL);
return TCL_ERROR;
}
winPtr2 = Ck_NameToWindow(interp, argv[2], winPtr);
if (winPtr2 == NULL) {
return TCL_ERROR;
}
value = Ck_GetOption(winPtr2, argv[3], argv[4]);
if (value != NULL) {
Tcl_SetResult(interp, value, TCL_VOLATILE);
}
return TCL_OK;
} else if ((c == 'r') && (strncmp(argv[1], "readfile", length) == 0)) {
int priority;
if ((argc != 3) && (argc != 4)) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " readfile fileName ?priority?\"",
(char *) NULL);
return TCL_ERROR;
}
if (argc == 4) {
priority = ParsePriority(interp, argv[3]);
if (priority < 0) {
return TCL_ERROR;
}
} else {
priority = CK_INTERACTIVE_PRIO;
}
return ReadOptionFile(interp, winPtr, argv[2], priority);
} else {
Tcl_AppendResult(interp, "bad option \"", argv[1],
"\": must be add, clear, get, or readfile", (char *) NULL);
return TCL_ERROR;
}
}
/*
*--------------------------------------------------------------
*
* CkOptionDeadWindow --
*
* 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
CkOptionDeadWindow(winPtr)
register CkWindow *winPtr; /* Window to be cleaned up. */
{
/*
* If this window is in the option stacks, then clear the stacks.
*/
if (winPtr->optionLevel != -1) {
int i;
for (i = 1; i <= curLevel; i++) {
levels[i].winPtr->optionLevel = -1;
}
curLevel = -1;
cachedWindow = NULL;
}
/*
* If this window was a main window, then delete its option
* database.
*/
if ((winPtr->mainPtr->winPtr == winPtr)
&& (winPtr->mainPtr->optionRootPtr != NULL)) {
ClearOptionTree(winPtr->mainPtr->optionRootPtr);
winPtr->mainPtr->optionRootPtr = NULL;
}
}
/*
*----------------------------------------------------------------------
*
* CkOptionClassChanged --
*
* 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
CkOptionClassChanged(winPtr)
CkWindow *winPtr; /* Window whose class changed. */
{
int i, j, *basePtr;
ElArray *arrayPtr;
if (winPtr->optionLevel == -1) {
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++) {
if (levels[i].winPtr == winPtr) {
for (j = i; j <= curLevel; j++) {
levels[j].winPtr->optionLevel = -1;
}
curLevel = i-1;
basePtr = levels[i].bases;
for (j = 0; j < NUM_STACKS; j++) {
arrayPtr = stacks[j];
arrayPtr->numUsed = basePtr[j];
arrayPtr->nextToUse = &arrayPtr->els[arrayPtr->numUsed];
}
if (curLevel <= 0) {
cachedWindow = NULL;
} else {
cachedWindow = levels[curLevel].winPtr;
}
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
ParsePriority(interp, string)
Tcl_Interp *interp; /* Interpreter to use for error reporting. */
char *string; /* Describes a priority level, either
* symbolically or numerically. */
{
int priority, c;
size_t length;
c = string[0];
length = strlen(string);
if ((c == 'w')
&& (strncmp(string, "widgetDefault", length) == 0)) {
return CK_WIDGET_DEFAULT_PRIO;
} else if ((c == 's')
&& (strncmp(string, "startupFile", length) == 0)) {
return CK_STARTUP_FILE_PRIO;
} else if ((c == 'u')
&& (strncmp(string, "userDefault", length) == 0)) {
return CK_USER_DEFAULT_PRIO;
} else if ((c == 'i')
&& (strncmp(string, "interactive", length) == 0)) {
return CK_INTERACTIVE_PRIO;
} else {
char *end;
priority = strtoul(string, &end, 0);
if ((end == string) || (*end != 0) || (priority < 0)
|| (priority > 100)) {
Tcl_AppendResult(interp, "bad priority level \"", string,
"\": 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
AddFromString(interp, winPtr, string, priority)
Tcl_Interp *interp; /* Interpreter to use for reporting results. */
CkWindow *winPtr; /* Pointer to window: options are entered
* 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. */
{
register char *src, *dst;
char *name, *value;
int lineNum;
char resultbuf[TCL_RESULT_SIZE];
src = string;
lineNum = 1;
while (1) {
/*
* Skip leading white space and empty lines and comment lines, and
* check for the end of the spec.
*/
while ((*src == ' ') || (*src == '\t')) {
src++;
}
if ((*src == '#') || (*src == '!')) {
do {
src++;
if ((src[0] == '\\') && (src[1] == '\n')) {
src += 2;
lineNum++;
}
} while ((*src != '\n') && (*src != 0));
}
if (*src == '\n') {
src++;
lineNum++;
continue;
}
if (*src == '\0') {
break;
}
/*
* Parse off the option name, collapsing out backslash-newline
* sequences of course.
*/
dst = name = src;
while (*src != ':') {
if ((*src == '\0') || (*src == '\n')) {
snprintf(resultbuf, sizeof(resultbuf), "missing colon on line %d",
lineNum);
Tcl_SetResult(interp, resultbuf, TCL_VOLATILE);
return TCL_ERROR;
}
if ((src[0] == '\\') && (src[1] == '\n')) {
src += 2;
lineNum++;
} else {
*dst = *src;
dst++;
src++;
}
}
/*
* Eliminate trailing white space on the name, and null-terminate
* it.
*/
while ((dst != name) && ((dst[-1] == ' ') || (dst[-1] == '\t'))) {
dst--;
}
*dst = '\0';
/*
* Skip white space between the name and the value.
*/
src++;
while ((*src == ' ') || (*src == '\t')) {
src++;
}
if (*src == '\0') {
snprintf(resultbuf, sizeof(resultbuf), "missing value on line %d", lineNum);
Tcl_SetResult(interp, resultbuf, TCL_VOLATILE);
return TCL_ERROR;
}
/*
* Parse off the value, squeezing out backslash-newline sequences
* along the way.
*/
dst = value = src;
while (*src != '\n') {
if (*src == '\0') {
snprintf(resultbuf, sizeof(resultbuf), "missing newline on line %d",
lineNum);
Tcl_SetResult(interp, resultbuf, TCL_VOLATILE);
return TCL_ERROR;
}
if ((src[0] == '\\') && (src[1] == '\n')) {
src += 2;
lineNum++;
} else {
*dst = *src;
dst++;
src++;
}
}
*dst = 0;
/*
* Enter the option into the database.
*/
Ck_AddOption(winPtr, name, value, priority);
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
ReadOptionFile(interp, winPtr, fileName, priority)
Tcl_Interp *interp; /* Interpreter to use for reporting results. */
CkWindow *winPtr; /* Pointer to window: options are entered
* 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. */
{
#if (TCL_MAJOR_VERSION == 7) && (TCL_MINOR_VERSION <= 4)
char *realName, *buffer;
int fileId, result;
struct stat statBuf;
Tcl_DString newName;
realName = Tcl_TildeSubst(interp, fileName, &newName);
if (realName == NULL) {
return TCL_ERROR;
}
fileId = open(realName, O_RDONLY, 0);
Tcl_DStringFree(&newName);
if (fileId < 0) {
Tcl_AppendResult(interp, "couldn't read file \"", fileName, "\"",
(char *) NULL);
return TCL_ERROR;
}
if (fstat(fileId, &statBuf) == -1) {
Tcl_AppendResult(interp, "couldn't stat file \"", fileName, "\"",
(char *) NULL);
close(fileId);
return TCL_ERROR;
}
buffer = (char *) ckalloc((unsigned) statBuf.st_size+1);
if (read(fileId, buffer, (unsigned) statBuf.st_size) != statBuf.st_size) {
Tcl_AppendResult(interp, "error reading file \"", fileName, "\"",
(char *) NULL);
close(fileId);
return TCL_ERROR;
}
close(fileId);
buffer[statBuf.st_size] = 0;
result = AddFromString(interp, winPtr, buffer, priority);
ckfree(buffer);
return result;
#else
char *realName, *buffer;
int result, bufferSize;
Tcl_Channel chan;
Tcl_DString newName;
realName = Tcl_TranslateFileName(interp, fileName, &newName);
if (realName == NULL) {
return TCL_ERROR;
}
chan = Tcl_OpenFileChannel(interp, realName, "r", 0);
Tcl_DStringFree(&newName);
if (chan == NULL) {
return TCL_ERROR;
}
/*
* Compute size of file by seeking to the end of the file.
*/
bufferSize = Tcl_Seek(chan, 0L, SEEK_END);
if (bufferSize < 0) {
Tcl_AppendResult(interp, "error getting file size of \"",
fileName, "\"", (char *) NULL);
Tcl_Close(NULL, chan);
return TCL_ERROR;
}
Tcl_Seek(chan, 0L, SEEK_SET);
buffer = (char *) ckalloc((unsigned) bufferSize + 1);
if (Tcl_Read(chan, buffer, bufferSize) != bufferSize) {
ckfree(buffer);
Tcl_AppendResult(interp, "error reading file \"", fileName, "\"",
(char *) NULL);
Tcl_Close(NULL, chan);
return TCL_ERROR;
}
Tcl_Close(NULL, chan);
buffer[bufferSize] = 0;
result = AddFromString(interp, winPtr, buffer, priority);
ckfree(buffer);
return result;
#endif
}
/*
*--------------------------------------------------------------
*
* 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.
*
*--------------------------------------------------------------
*/