summaryrefslogtreecommitdiff
path: root/src/ResConfig.c
blob: c12fed5209edb2e4cadbc94056a8988e05aed550 (plain)
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
1001
1002
/*

Copyright 1987, 1988, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.

*/
/*****************************************************************

(C) COPYRIGHT International Business Machines Corp. 1992,1997
    All Rights Reserved

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
THE IBM CORPORATION BE LIABLE FOR ANY CLAIM, DAMAGES, INCLUDING,
BUT NOT LIMITED TO CONSEQUENTIAL OR INCIDENTAL DAMAGES, OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the IBM Corporation shall
not be used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior written authorization from the IBM
Corporation.

******************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "Intrinsic.h"
#include "IntrinsicI.h"
#include "Core.h"
#include "CoreP.h"
#include "ShellP.h"
#include "StringDefs.h"
#include "ResConfigP.h"
#include <X11/Xatom.h>
#include <stdio.h>
#include <stdlib.h>

#define MAX_BUFFER 512

static void _search_child(Widget, char *, char *, char *, char *, char, char *);
static void _set_and_search(Widget, char *, char *, char *, char *, char,
                            char *);
static int _locate_children(Widget, Widget **);

/*
 * NAME: _set_resource_values
 *
 * FUNCTION:
 *      This function sets the value on the widget.  It must first determine
 *      if the last part is a valid resource for that widget.  (eg.
 *      labelString is a valid resource for label but not for bulletin board)
 *      It must also add the resource to the application's resource database
 *      and then query it out using specific resource strings that it builds
 *      from the widget information.  This ensures that a customizing tool
 *      on-the-fly paradigm is followed:  an application that is
 *      instantaneously updated should look the same as one that is restarted
 *      and uses the .Xdefaults file.
 *
 * PARAMETERS:
 *      w               the widget to match
 *      resource        the resource string to be matched
 *      value           the value to be set
 *      last_part       the last resource part (e.g. *background)
 *
 * RETURN VALUES: void
 *
 * ERRORS: none
 */
static void
_set_resource_values(Widget w, char *resource, char *value, char *last_part)
{
    XrmDatabase db = NULL;
    char *resource_name = NULL;
    char *resource_class = NULL;
    char *return_type;
    XrmValue return_value;
    char *resource_value;
    Widget cur = w;
    char *temp;
    XtResourceList resources_return = NULL;
    Cardinal num_resources_return = 0;
    Cardinal res_index;
    Boolean found_resource = False;
    Display *dpy;
    XrmDatabase tmp_db;

    if (last_part == NULL)
        return;

    if (!XtIsWidget(w)) {
        if (w == 0 || w->core.parent == 0)
            return;
        dpy = XtDisplay(w->core.parent);
    }
    else {
        dpy = XtDisplay(w);
    }
    tmp_db = XtDatabase(dpy);

    /*
     * get a list of all the valid resources for this widget
     */
    XtGetResourceList(w->core.widget_class,
                      &resources_return, &num_resources_return);

    /*
     * try to match the last_part of the resource string with
     * a resource in this resource list
     */
    for (res_index = 0; res_index < num_resources_return; res_index++) {
        if ((strcmp(last_part,
                    resources_return[res_index].resource_name) == 0) ||
            (strcmp(last_part,
                    resources_return[res_index].resource_class) == 0)) {
            found_resource = True;
            break;
        }
    }

    /*
     * if resource is not a valid resource for this widget
     * or the resource name or class are NULL
     * then exit this function
     */
    if (!found_resource
        || !resources_return[res_index].resource_name
        || !resources_return[res_index].resource_class) {
        XtFree((char *) resources_return);
        return;
    }

    /*
     * build the full resource name and class specifications so
     * that you can query the resource database
     *      eg: .app.button1.foreground
     *          .App.XmPushButton.Foreground
     */
    while (cur != NULL) {
        /*
         * create resource name string
         */
        if (resource_name) {
            XtAsprintf(&temp, ".%s%s", cur->core.name, resource_name);
            XtFree(resource_name);
        }
        else if (!XtIsWidget(cur) || !cur->core.name) {
            cur = XtParent(cur);
            continue;
        }
        else {
            XtAsprintf(&temp, ".%s", cur->core.name);
        }
        resource_name = temp;

        /*
         * create resource class string
         */
        if ((XtIsTopLevelShell(cur)) && (XtParent(cur) == NULL)) {
            ApplicationShellWidget top = (ApplicationShellWidget) (cur);

            if (resource_class) {
                XtAsprintf(&temp, ".%s%s",
                           top->application.class, resource_class);
            }
            else {
                XtAsprintf(&temp, ".%s", top->application.class);
            }
        }
        else {
            if (resource_class) {
                XtAsprintf(&temp, ".%s%s",
                           cur->core.widget_class->core_class.class_name,
                           resource_class);
            }
            else {
                XtAsprintf(&temp, ".%s",
                           cur->core.widget_class->core_class.class_name);
            }
        }

        XtFree(resource_class);
        resource_class = temp;

        cur = XtParent(cur);
    }

    /*
     * add the resource name to the end of the resource name string
     */
    XtAsprintf(&temp, "%s.%s", resource_name,
               resources_return[res_index].resource_name);

    XtFree(resource_name);
    resource_name = temp;

    /*
     * add the resource class to the end of the resource class string
     */
    XtAsprintf(&temp, "%s.%s", resource_class,
               resources_return[res_index].resource_class);

    XtFree(resource_class);
    resource_class = temp;

#ifdef DEBUG
    fprintf(stderr, "resource_name = %s\n", resource_name);
    fprintf(stderr, "resource_class = %s\n", resource_class);
#endif

    /*
     * put the resource and its value in a resource database and
     * then query it back out again using the specific name and
     * class resource strings that were built above.  This is
     * necessary to maintain a precedence similar to the .Xdefaults
     * file
     */
    XrmPutStringResource(&db, resource, value);
    XrmMergeDatabases(db, &tmp_db);
    XrmGetResource(tmp_db, resource_name, resource_class,
                   &return_type, &return_value);
    if (return_type)
        resource_value = XtNewString(return_value.addr);
    else
        resource_value = XtNewString(value);

#ifdef DEBUG
    fprintf(stderr,
            "Apply:\n\twidget = %s\n\tlast_part = %s\n\tvalue = %s\n",
            (w->core.name == NULL) ? "NULL" : w->core.name,
            resources_return[res_index].resource_name, resource_value);
#endif
    /*
     * use XtVaSetValues with XtVaTypedArg to convert the value of
     * type String the the same type as the resource (last_part).
     * Then set the value.
     */
    XtVaSetValues(w,
                  XtVaTypedArg, resources_return[res_index].resource_name,
                  XtRString, resource_value, strlen(resource_value) + 1, NULL);

    XtFree((char *) resources_return);
    XtFree(resource_name);
    XtFree(resource_class);
    XtFree(resource_value);
}

/*
 * NAME: _apply_values_to_children
 *
 * FUNCTION:
 *      Once the resource string matches the value must be applied to
 *      all children if applicable. (eg. App*Form.background must apply
 *      background to all children of the Form widget)
 *
 * PARAMETERS:
 *      w               the widget to match
 *      remainder       the part of the resource string left over
 *      resource        the resource string to be matched
 *      value           the value to be set
 *      last_token      the last * or . before the final resource part
 *      last_part       the last resource part (e.g. *background)
 *
 * RETURN VALUES: void
 *
 * ERRORS: none
 */
static void
_apply_values_to_children(Widget w,
                          char *remainder,
                          char *resource,
                          char *value,
                          char last_token,
                          char *last_part)
{
    int i;
    int num_children;
    Widget *children;

    /*
     * Recursively search through the children
     */
    num_children = _locate_children(w, &children);

    for (i = 0; i < num_children; i++) {

#ifdef DEBUG
        if (XtIsWidget(children[i]) && XtIsWidget(w))
            fprintf(stderr, "searching child %s of parent %s\n",
                    children[i]->core.name, w->core.name);
        else
            fprintf(stderr, "searching child (NULL) of parent %s\n",
                    w->core.name);
        if (!XtIsWidget(children[i]))
            fprintf(stderr, "children[%d] is NOT a widget\n", i);
        if (!XtIsWidget(w))
            fprintf(stderr, "w is NOT a widget\n");
#endif

        _set_resource_values(children[i], resource, value, last_part);
        _apply_values_to_children(children[i], remainder,
                                  resource, value, last_token, last_part);
    }

    XtFree((char *) children);
}

/*
 * NAME: _search_child
 *
 * FUNCTION:
 *      descends through each child of the tree
 *
 * PARAMETERS:
 *      w               the widget whose children are to be searched
 *      indx            index into the resource string
 *      remainder       the remaining part of the resource string
 *      resource        the resource string to be matched
 *      value           the value to be applied
 *      last_token      the last * or . before the final resource part
 *      last_part       the last resource part (e.g. *background)
 *
 * RETURN VALUES: none
 *
 * ERRORS: none
 */
static void
_search_child(Widget w,
              char *indx,
              char *remainder,
              char *resource,
              char *value,
              char last_token,
              char *last_part)
{
    int i;
    int num_children;
    Widget *children;

    /*
     * Recursively search through the children
     */
    num_children = _locate_children(w, &children);
    for (i = 0; i < num_children; i++) {
        _set_and_search(children[i], indx, remainder, resource,
                        value, last_token, last_part);
    }

    XtFree((char *) children);
}

/*
 * NAME: _get_part
 *
 * FUNCTION:
 *      This routine will return the token and following part of the resource
 *      when given the current index it will update the index accordingly
 *
 * PARAMETERS:
 *      remainder       the part of the resource string left over
 *      indx            the index into the resource string
 *      part            the parsed off part of the resource string
 *
 * RETURN VALUES:
 *      char            the token (* or . or ?) preceding the resource part
 *      indx            the index into the resource string
 *      part            the parsed off part of the resource string
 *
 * ERRORS: none
 */
static char
_get_part(char *remainder _X_UNUSED, char **indx, char **part)
{
    char buffer[MAX_BUFFER];
    char *buf_ptr;
    char token = **indx;
    int i = 0;

    /*
     * copy the remainder part into the buffer
     */
    buf_ptr = buffer;
    (*indx)++;                  /* get rid of the token         */
    while (**indx && (**indx != '.') && (**indx != '*')) {
        *buf_ptr++ = *(*indx)++;
        if (++i >= MAX_BUFFER - 1)
            break;
    }
    *buf_ptr = '\0';

    *part = XtNewString(buffer);        /* return a new string to part  */

    if (strcmp(*indx, "") == 0)
        *indx = NULL;

    return (token);             /* return the token             */
}

/*
 * NAME: _match_resource_to_widget
 *
 * FUNCTION:
 *      This function matches the resource part to the widget name or class
 *
 * PARAMETERS:
 *      w               the widget to match
 *      part            the parsed off part of the resource string
 *
 * RETURN VALUES:
 *      Boolean         true if a match occurs
 *
 * ERRORS: none
 */
static Boolean
_match_resource_to_widget(Widget w, char *part)
{
    /*
     * Match any widget at this level if the ? is used
     */
    if (strcmp(part, "?") == 0)
        return (True);

    /*
     * if the object is really a widget then its name can be matched
     * otherwise only use its class.  Note that if you try to reference
     * a widget name when the object is not a widget, you may get a
     * core dump from an invalid pointer reference.
     */
    if (XtIsWidget(w)) {
        if ((strcmp(w->core.name, part) == 0) ||
            (strcmp(w->core.widget_class->core_class.class_name, part) == 0))
            return (True);
        else
            return (False);
    }
    else {
        if ((strcmp(w->core.widget_class->core_class.class_name, part) == 0))
            return (True);
        else
            return (False);
    }
}

/*
 * NAME: _set_and_search
 *
 * FUNCTION:
 *      The algorithm to search the widget tree and apply a resource string
 *
 * PARAMETERS:
 *      w               the widget to match
 *      indx            the index into the resource string
 *      remainder       the part of the resource string left over
 *      resource        the resource string to be matched
 *      value           the value to be set
 *      last_token      the last * or . before the final resource part
 *      last_part       the last resource part (e.g. *background)
 *
 * RETURN VALUES: none
 *
 * ERRORS: none
 *
 * ALGORITHM:
 * loop (look at all children)
 *      if (resource segment and current widget match)
 *              if '.'
 *                      if at end of resource string
 *                              set values (    .=over all children
 *                                              *=this widget only)
 *                      else
 *                              descend the widget tree
 *                              and parse off resource segment
 *                      exit the loop
 *              if '*'
 *                      if at end of resource string
 *                              set values (    .=over all children
 *                                              *=this widget only)
 *                      descend and parse
 *      else
 *              if '.'
 *                      continue looping
 *              if '*'
 *                      descend but don't parse
 *                      continue looping
 * end loop
 *
 * NOTE:  the _set_resource_values routine will not allow a value to be
 *      set on a resource against the rules of the resource database manager
 */
static void
_set_and_search(Widget w,
                char *indx,
                char *remainder,
                char *resource,
                char *value,
                char last_token,
                char *last_part)
{
    char *part;
    char *local_index = indx;
    char token;

    /*
     * parse off one part, return token and the new index
     */
    token = _get_part(remainder, &local_index, &part);

    if (_match_resource_to_widget(w, part)) {
        if (token == '.') {
            if (local_index == NULL) {
                if (last_token == '.') {
                    _set_resource_values(w, resource, value, last_part);
                }
                else if (last_token == '*') {
                    _set_resource_values(w, resource, value, last_part);
                    _apply_values_to_children(w,
                                              remainder, resource, value,
                                              last_token, last_part);
                }
            }
            else
                _search_child(w, local_index, remainder,
                              resource, value, last_token, last_part);
            XtFree(part);
            return;
        }
        if (token == '*') {
            if (local_index == NULL) {
                if (last_token == '.') {
                    _set_resource_values(w, resource, value, last_part);
                }
                else if (last_token == '*') {
                    _set_resource_values(w, resource, value, last_part);
                    _apply_values_to_children(w,
                                              remainder, resource, value,
                                              last_token, last_part);
                }
            }
            else
                _search_child(w, local_index, remainder,
                              resource, value, last_token, last_part);
        }
    }
    else {                      /* if the widget name and class don't match the part */
        /* if (token == '.') just continue looping */

        if (token == '*') {
            _search_child(w, indx, remainder, resource, value,
                          last_token, last_part);
        }
    }

    XtFree(part);
}

/*
 * NAME: _get_last_part
 *
 * FUNCTION:
 *      This routine will parse off the last segment of a resource string
 *      and its token and return them.  the remainder of resource is also
 *      returned.  strcoll is used to guarantee no problems with
 *      international strings.
 *
 * PARAMETERS:
 *      remainder       the part of the resource string left over
 *      part            the parsed off part of the resource string
 *
 * RETURN VALUES:
 *      char            the token (* or . or ?) preceding the resource part
 *      remainder       the part of the resource string left over
 *      part            the parsed off part of the resource string
 *
 * ERRORS: none
 */
static char
_get_last_part(char *remainder, char **part)
{
    char *loose, *tight;

    loose = strrchr(remainder, '*');
    tight = strrchr(remainder, '.');

    if ((loose == NULL) && (tight == NULL)) {
        *part = XtNewString(remainder);
        return ('.');
    }
    if ((loose == NULL) || (tight && (strcoll(loose, tight) < 0))) {
        *tight++ = '\0';        /* shorten the remainder string */
        *part = XtNewString(tight);
        return ('.');
    }
    if ((tight == NULL) || (strcoll(tight, loose) < 0)) {
        *loose++ = '\0';
        *part = XtNewString(loose);
        return ('*');
    }
    *part = NULL;

    return ('0');               /* error - return 0 */
}

/*
 * NAME: _search_widget_tree
 *
 * FUNCTION:
 *      This function tries to match a resource string to the widgets
 *      it applies to.  The functions it invokes to do this then set
 *      the value for that resource to each widget.
 *
 *      The resource string has to be parsed into the following format:
 *              resource = App*Form*button1.background
 *              remainder = *Form*button1
 *              last_part = background          last_token = .
 *      As the widget tree is recursively descended, these variables are
 *      passed.  The remainder is parsed at each level in the widget
 *      tree as the _set_and_search function attempts to match
 *      the resource part (eg. part = Form  token = *) to a widget.  When
 *      the entire resource string has been matched, the _set_resource_values
 *      functions is called to apply the value to the widget or widgets.
 *
 * PARAMETERS:
 *      w               a widget from whose toplevel shell ancestor
 *                      the search will start
 *      resource        the resource string to match
 *      value           the value to apply
 *
 * RETURN VALUES: none
 *
 * ERRORS: none
 */
static void
_search_widget_tree(Widget w, char *resource, char *value)
{
    Widget parent = w;
    char *last_part;
    char *remainder = NULL;
    char *loose, *tight;
    int loose_len, tight_len;

    if (resource == NULL)
        return;

    /*
     * Find the root of the tree given any widget
     */
    while (XtParent(parent) != NULL) {
        parent = XtParent(parent);
    }
#ifdef DEBUG
    if (XtIsWidget(w) && XtIsWidget(parent))
        fprintf(stderr, "widget = %s parent = %s\n",
                w->core.name, parent->core.name);
    else
        fprintf(stderr, "widget = NULL parent = NULL\n");
#endif

    /*
     * parse off the Class name that was prepended to this string in
     * a customizing tool
     */
    loose = strchr(resource, '*');
    tight = strchr(resource, '.');
    if ((loose == NULL) && (tight == NULL))
        return;

    loose_len = (loose) ? (int) strlen(loose) : 0;
    tight_len = (tight) ? (int) strlen(tight) : 0;

    if ((loose == NULL) || (tight_len > loose_len))
        remainder = XtNewString(tight);
    else if ((tight == NULL) || (loose_len > tight_len))
        remainder = XtNewString(loose);

    /*
     * Parse last segment off of resource string, (eg. background, font,
     * etc.)
     */
    if (remainder) {
        char last_token;

        last_token = _get_last_part(remainder, &last_part);
        /*
         * this case covers resources of only one level (eg. *background)
         */
        if (remainder[0] == 0) {
            _set_resource_values(w, resource, value, last_part);
            if (last_token == '*')
                _apply_values_to_children(parent, remainder, resource,
                                          value, last_token, last_part);
            /*
             * all other resource strings are recursively applied to the widget tree.
             * Prepend a '.' to the remainder string if there is no leading token.
             */
        }
        else {
            char *indx, *copy;

            if (remainder[0] != '*' && remainder[0] != '.') {
                XtAsprintf(&copy, ".%s", remainder);
                XtFree(remainder);
                remainder = copy;
            }
            indx = remainder;
            _set_and_search(parent, indx, remainder, resource, value,
                            last_token, last_part);
        }

        XtFree(remainder);
        XtFree(last_part);
    }
}

/*
 * NAME: _locate_children
 *
 * FUNCTION:
 *      returns a list of all of a widget's children
 *
 * PARAMETERS:
 *      w               the parent to search for its children
 *      children        the list of children that is created
 *      normal          flag for normal children
 *      popup           flag for popup children
 *
 * RETURN VALUES:
 *      int             the number of children
 *      children        the list of children found
 *
 * ERRORS: none
 */
static int
_locate_children(Widget parent, Widget **children)
{
    CompositeWidget comp = (CompositeWidget) parent;
    Cardinal i;
    int num_children = 0;
    int current = 0;

    /*
     * count the number of children
     */
    if (XtIsWidget(parent))
        num_children =
            (int) ((Cardinal) num_children + parent->core.num_popups);
    if (XtIsComposite(parent))
        num_children =
            (int) ((Cardinal) num_children + comp->composite.num_children);
    if (num_children == 0) {
        *children = NULL;
        return (0);
    }

    *children = (Widget *)
        XtMalloc((Cardinal) (sizeof(Widget) * (size_t) num_children));

    if (XtIsComposite(parent)) {
        for (i = 0; i < comp->composite.num_children; i++) {
            (*children)[current] = comp->composite.children[i];
            current++;
        }
    }

    if (XtIsWidget(parent)) {
        for (i = 0; i < parent->core.num_popups; i++) {
            (*children)[current] = comp->core.popup_list[i];
            current++;
        }
    }

    return (num_children);
}

#ifdef DEBUG
/*
 * NAME: dump_widget_tree
 *
 * FUNCTION:
 *      recursively printout entire widget tree
 *
 * PARAMETERS:
 *      w               the widget to match
 *      indent          the amount to indent each line
 *
 * RETURN VALUES: void
 *
 * ERRORS: none
 */
static void
dump_widget_tree(Widget w, int indent)
{
    int i, j;
    int num_children;
    Widget *children;

    /*
     * Recursively search through the children
     */
    num_children = _locate_children(w, &children);
    indent += 2;
    for (i = 0; i < num_children; i++) {
        if (children[i] != NULL) {
            for (j = 0; j < indent; j++)
                fprintf(stderr, " ");
            if (XtIsWidget(children[i])) {
                fprintf(stderr, "(%s)\t", children[i]->core.name);
                fprintf(stderr, "(%s)\n",
                        children[i]->core.widget_class->core_class.class_name);
            }
            else {
                fprintf(stderr, "(NULL)\t");
                fprintf(stderr, "(%s)\n",
                        children[i]->core.widget_class->core_class.class_name);
            }
        }
        dump_widget_tree(children[i], indent);
    }

    XtFree((char *) children);
}
#endif

/*
 * NAME: _XtResourceConfiguationEH
 *
 * FUNCTION:
 *      This function is the event handler for the on-the-fly communication
 *      with a resource customization tool.  This event handler must be
 *      registered for the toplevel shell of each app.  This is best done
 *      in the _XtCreatePopupShell and _XtAppCreateShell functions in Xt's
 *      Create.c source file.
 *
 *      The property used to communicate with a customizing tool is
 *      placed on the toplevel shell window of the application.  The
 *      customizing tool places a property on this window which causes
 *      this event handler to be invoked via the PropertyNotify event.
 *      This event handler reads the property and then deletes it from
 *      the server.  The contents of the property are a resource string
 *      and value.  The event handler then calls functions to walk the
 *      applications widget tree, determining which widgets are affected
 *      by the resource string, and then applying the value with XtSetValues.
 *
 * PARAMETERS:
 *      w               the widget that invoked this event handler
 *      client_data     not used
 *      event           the event structure
 *
 * RETURN VALUES: none
 *
 * ERRORS: none
 */
void
_XtResourceConfigurationEH(Widget w,
                           XtPointer client_data _X_UNUSED,
                           XEvent *event)
{
    Atom actual_type;
    int actual_format;
    unsigned long nitems;
    unsigned long leftover;
    char *data = NULL;
    char *data_ptr;

#ifdef DEBUG
    int indent = 0;
#endif
    XtPerDisplay pd;

#ifdef DEBUG
    fprintf(stderr, "in _XtResourceConfiguationEH atom = %u\n",
            (unsigned) event->xproperty.atom);
    fprintf(stderr, "    window = %x\n", (unsigned) XtWindow(w));
    if (XtIsWidget(w))
        fprintf(stderr, "    widget = %zx   name = %s\n", (size_t) w,
                w->core.name);
#endif

    pd = _XtGetPerDisplay(XtDisplay(w));

    /*
     * The window on which a customizing tool places the property
     * is determined at this point.  It should be the applications
     * toplevel shell window.
     *
     * A customizing tool sends a "ping" to the application on
     * the RCM_INIT property.  The application answers the ping
     * by deleting the property.
     */
    if (event->xproperty.atom == pd->rcm_init) {
        XDeleteProperty(XtDisplay(w), XtWindow(w), pd->rcm_init);

#ifdef DEBUG
        if (XtIsWidget(w))
            fprintf(stderr, "%s\n", w->core.name);
        else
            fprintf(stderr, "NULL name\n");
        dump_widget_tree(w, indent);

        fprintf(stderr, "answer ping\n");
#endif
    }

    /*
     * This event handler ignores any property notify events that
     * are not RCM_INIT or RCM_DATA
     */
    if (event->xproperty.atom != pd->rcm_data)
        return;

    /*
     * Retrieve the data from the property
     */
#ifdef DEBUG
    fprintf(stderr, "receiving RCM_DATA property\n");
#endif
    if (XGetWindowProperty(XtDisplay(w),
                           XtWindow(w),
                           pd->rcm_data, 0L, 8192L,
                           TRUE, XA_STRING,
                           &actual_type, &actual_format, &nitems, &leftover,
                           (unsigned char **) &data) == Success &&
        actual_type == XA_STRING && actual_format == 8) {
        /*
         *      data format is:
         *
         *      resource_length, resource, value
         *
         *      convert the resource_length to a long, skip over it, put a
         *      zero byte at the end of the resource, and pick off the
         *      resource and value fields.
         */
        if (data) {
            char *data_end = data + nitems;
            char *data_value;
            unsigned long resource_len;

            resource_len = strtoul(data, &data_ptr, 10);

            if (data_ptr != (char *) data) {
                data_ptr++;
                data_value = data_ptr + resource_len;
            }
            else                /* strtoul failed to convert a number */
                data_ptr = data_value = NULL;

            if (data_value > data_ptr && data_value < data_end) {
                char *resource;
                char *value;

                *data_value++ = '\0';

                resource = XtNewString(data_ptr);
                value = XtNewString(data_value);
#ifdef DEBUG
                fprintf(stderr, "resource_len=%lu\n", resource_len);
                fprintf(stderr, "resource = %s\t value = %s\n",
                        resource, value);
#endif
                /*
                 * descend the application widget tree and
                 * apply the value to the appropriate widgets
                 */
                _search_widget_tree(w, resource, value);

                XtFree(resource);
                XtFree(value);
            }
        }
    }

    XFree((char *) data);
}