summaryrefslogtreecommitdiff
path: root/src/MultiSrc.c
blob: 33aa06a2bd5a7560e003970fb436b5a21439500f (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
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
/*
 * Copyright 1991 by OMRON Corporation
 *
 * 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, and that the name OMRON not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  OMRON makes no representations
 * about the suitability of this software for any purpose.  It is provided
 * "as is" without express or implied warranty.
 *
 * OMRON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL OMRON BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTUOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 *      Authors: Chris Peterson MIT X Consortium
 *               Li Yuhong      OMRON Corporation
 *               Frank Sheeran  OMRON Corporation
 *
 * Much code taken from X11R3 String and Disk Sources.
 */

/*

Copyright 1991, 1994, 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.

*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <X11/Xfuncs.h>
#include <X11/Xos.h>
#include <X11/Xmu/CharSet.h>
#include <X11/Xmu/Misc.h>
#include <X11/Xaw/XawInit.h>
#include <X11/Xaw/MultiSrcP.h>
#include <X11/Xaw/XawImP.h>
#include "XawI18n.h"
#include "Private.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define MAGIC_VALUE	((XawTextPosition)-1)
#define streq(a, b)	(strcmp((a), (b)) == 0)

#ifdef X_NOT_POSIX
#define Off_t long
#define Size_t unsigned int
#else
#define Off_t off_t
#define Size_t size_t
#endif


/*
 * Class Methods
 */
static XawTextPosition ReadText(Widget, XawTextPosition, XawTextBlock*, int);
static int  ReplaceText(Widget, XawTextPosition, XawTextPosition,
			XawTextBlock*);
static XawTextPosition Scan(Widget, XawTextPosition, XawTextScanType,
			    XawTextScanDirection, int, Bool);
static XawTextPosition  Search(Widget, XawTextPosition, XawTextScanDirection,
			       XawTextBlock*);
static void XawMultiSrcClassInitialize(void);
static void XawMultiSrcDestroy(Widget);
static void XawMultiSrcInitialize(Widget, Widget, ArgList, Cardinal*);
static Boolean XawMultiSrcSetValues(Widget, Widget, Widget,
				    ArgList, Cardinal*);
static void XawMultiSrcGetValuesHook(Widget, ArgList, Cardinal*);

/*
 * Prototypes
 */
static MultiPiece *AllocNewPiece(MultiSrcObject, MultiPiece*);
static void BreakPiece(MultiSrcObject, MultiPiece*);
static Boolean CvtMultiTypeToString(Display*, XrmValuePtr, Cardinal*,
				    XrmValuePtr, XrmValuePtr, XtPointer*);
static void CvtStringToMultiType(XrmValuePtr, Cardinal*,
				 XrmValuePtr, XrmValuePtr);
static MultiPiece *FindPiece(MultiSrcObject, XawTextPosition,
			     XawTextPosition*);
static void FreeAllPieces(MultiSrcObject);
static FILE *InitStringOrFile(MultiSrcObject, Bool);
static void LoadPieces(MultiSrcObject, FILE*, char*);
static void RemovePiece(MultiSrcObject, MultiPiece*);
static void RemoveOldStringOrFile(MultiSrcObject, Bool);
static String StorePiecesInString(MultiSrcObject);
static Bool WriteToFile(String, String);
static void GetDefaultPieceSize(Widget, int, XrmValue*);

/*
 * Initialization
 */
#define offset(field) XtOffsetOf(MultiSrcRec, multi_src.field)
static XtResource resources[] = {
  {
    XtNstring,
    XtCString,
    XtRString,
    sizeof(XtPointer),
    offset(string),
    XtRPointer,
    NULL
  },
  {
    XtNtype,
    XtCType,
    XtRMultiType,
    sizeof(XawAsciiType),
    offset(type),
    XtRImmediate,
    (XtPointer)XawAsciiString
  },
  {
    XtNdataCompression,
    XtCDataCompression,
    XtRBoolean,
    sizeof(Boolean),
    offset(data_compression),
    XtRImmediate,
    (XtPointer)False
  },
  {
    XtNpieceSize,
    XtCPieceSize,
    XtRInt,
    sizeof(XawTextPosition),
    offset(piece_size),
    XtRCallProc,
    (XtPointer)GetDefaultPieceSize
  },
#ifdef OLDXAW
  {
    XtNcallback,
    XtCCallback,
    XtRCallback,
    sizeof(XtPointer),
    offset(callback),
    XtRCallback,
    (XtPointer)NULL
  },
#endif
  {
    XtNuseStringInPlace,
    XtCUseStringInPlace,
    XtRBoolean,
    sizeof(Boolean),
    offset(use_string_in_place),
    XtRImmediate,
    (XtPointer)False
  },
  {
    XtNlength,
    XtCLength,
    XtRInt,
    sizeof(int),
    offset(multi_length),
    XtRImmediate,
    (XtPointer)MAGIC_VALUE
  },
};
#undef offset

#define superclass		(&textSrcClassRec)
MultiSrcClassRec multiSrcClassRec = {
  /* object */
  {
    (WidgetClass)superclass,		/* superclass */
    "MultiSrc",				/* class_name */
    sizeof(MultiSrcRec),		/* widget_size */
    XawMultiSrcClassInitialize,		/* class_initialize */
    NULL,				/* class_part_initialize */
    False,				/* class_inited */
    XawMultiSrcInitialize,		/* initialize */
    NULL,				/* initialize_hook */
    NULL,				/* obj1 */
    NULL,				/* obj2 */
    0,					/* obj3 */
    resources,				/* resources */
    XtNumber(resources),		/* num_resources */
    NULLQUARK,				/* xrm_class */
    False,				/* obj4 */
    False,				/* obj5 */
    False,				/* obj6 */
    False,				/* obj7 */
    XawMultiSrcDestroy,			/* destroy */
    NULL,				/* obj8 */
    NULL,				/* obj9 */
    XawMultiSrcSetValues,		/* set_values */
    NULL,				/* set_values_hook */
    NULL,				/* obj10 */
    XawMultiSrcGetValuesHook,		/* get_values_hook */
    NULL,				/* obj11 */
    XtVersion,				/* version */
    NULL,				/* callback_private */
    NULL,				/* obj12 */
    NULL,				/* obj13 */
    NULL,				/* obj14 */
    NULL,				/* extension */
  },
  /* text_src */
  {
    ReadText,				/* Read */
    ReplaceText,			/* Replace */
    Scan,				/* Scan */
    Search,				/* Search */
    XtInheritSetSelection,		/* SetSelection */
    XtInheritConvertSelection,		/* ConvertSelection */
  },
  /* multi_src */
  {
    NULL,				/* extension */
  },
};

WidgetClass multiSrcObjectClass = (WidgetClass)&multiSrcClassRec;

static XrmQuark Qstring, Qfile;

/*
 * Implementation
 */
static void
XawMultiSrcClassInitialize(void)
{
    XawInitializeWidgetSet();
    Qstring = XrmPermStringToQuark(XtEstring);
    Qfile = XrmPermStringToQuark(XtEfile);
    XtAddConverter(XtRString, XtRMultiType, CvtStringToMultiType, NULL, 0);
    XtSetTypeConverter(XtRMultiType, XtRString, CvtMultiTypeToString, NULL, 0,
		       XtCacheNone, NULL);
}

/*
 * Function:
 *	XawMultiSrcInitialize
 *
 * Parameters:
 *	request  - widget requested by the argument list
 *	cnew	 - the new widget with both resource and non resource values
 *	args	 - (unused)
 *	num_args - (unused)
 *
 * Description:
 *	Initializes the multi src object
 */
/*ARGSUSED*/
static void
XawMultiSrcInitialize(Widget request, Widget cnew,
		      ArgList args, Cardinal *num_args)
{
    MultiSrcObject src = (MultiSrcObject)cnew;
    FILE *file;

    /*
     * Set correct flags (override resources) depending upon widget class
     */
#ifdef OLDXAW
    src->multi_src.changes = False;
#else
    src->text_src.changed = False;
#endif
    src->multi_src.allocated_string = False;

    if (src->multi_src.use_string_in_place && src->multi_src.string == NULL)
	src->multi_src.use_string_in_place = False;

    file = InitStringOrFile(src, src->multi_src.type == XawAsciiFile);
    LoadPieces(src, file, NULL);

    if (file != NULL)
	fclose(file);
    src->text_src.text_format = XawFmtWide;
}

/*
 * Function:
 *	ReadText
 *
 * Parameters:
 *	w      - MultiSource object
 *	pos    - position of the text to retrieve
 *	text   - text block that will contain returned text
 *	length - maximum number of characters to read
 *
 * Description:
 *	This function reads the source.
 *
 * Returns:
 *	The character position following the retrieved text.
 */
static XawTextPosition
ReadText(Widget w, XawTextPosition pos, XawTextBlock *text, int length)
{
    MultiSrcObject src = (MultiSrcObject)w;
    XawTextPosition count, start;
    MultiPiece *piece = FindPiece(src, pos, &start);

    text->format = XawFmtWide;
    text->firstPos = pos;
    text->ptr = (char *)(piece->text + (pos - start));
    count = piece->used - (pos - start);
    text->length = Max(0, (length > count) ? count : length);

    return (pos + text->length);
}

/*
 * Function:
 *	ReplaceText
 *
 * Parameters:
 *	w	 - MultiSource object
 *	startPos - ends of text that will be removed
 *	endPos	 - ""
 *	text	 - new text to be inserted into buffer at startPos
 *
 * Description:
 *	Replaces a block of text with new text.
 *
 * Returns:
 *	XawEditDone on success, XawEditError otherwise
 */
/*ARGSUSED*/
static int
ReplaceText(Widget w, XawTextPosition startPos, XawTextPosition endPos,
	    XawTextBlock *u_text_p)
{
    MultiSrcObject src = (MultiSrcObject)w;
    MultiPiece *start_piece, *end_piece, *temp_piece;
    XawTextPosition start_first, end_first;
    int length, firstPos;
    wchar_t *wptr;
    Bool local_artificial_block = False;
    XawTextBlock text;

    /* STEP 1: The user handed me a text block called `u_text' that may be
     * in either FMTWIDE or FMT8BIT (ie MB.)  Later code needs the block
     * `text' to hold FMTWIDE.	So, this copies `u_text' to `text', and if
     * `u_text' was MB, I knock it up to WIDE
     */
    if (u_text_p->length == 0)	/* if so, the block contents never ref'd */
	text.length = 0;

    else if (u_text_p->format == XawFmtWide) {
	local_artificial_block = False; /* don't have to free it ourselves */
	text.firstPos = u_text_p->firstPos;
	text.length =	u_text_p->length;
	text.ptr =	u_text_p->ptr;
    }
    else {
	/*
	 * WARNING! u_text->firstPos and length are in units of CHAR,
	 * not CHARACTERS!
	 */
	local_artificial_block = True;	/* have to free it ourselves */
	text.firstPos = 0;
	text.length = u_text_p->length; /* _XawTextMBToWC converts this
					 * to wchar len
					 */

	text.ptr = (char*)_XawTextMBToWC(XtDisplay(XtParent(w)),
					 &u_text_p->ptr[u_text_p->firstPos],
					 &text.length);

	/* I assert the following assignment is not needed - since Step 4
	   depends on length, it has no need of a terminating NULL.  I think
	   the ASCII-version has the same needless NULL. */
	/*((wchar_t*)text.ptr)[ text.length ] = NULL;*/
    }

    /* STEP 2: some initialization... */
    if (src->text_src.edit_mode == XawtextRead)
	return (XawEditError);

    start_piece = FindPiece(src, startPos, &start_first);
    end_piece = FindPiece(src, endPos, &end_first);

    /* STEP 3: remove the empty pieces... */
    if (start_piece != end_piece) {
	temp_piece = start_piece->next;

	/* If empty and not the only piece then remove it */
	if (((start_piece->used = startPos - start_first) == 0)
	    &&	!(start_piece->next == NULL && start_piece->prev == NULL))
	    RemovePiece(src, start_piece);

	while (temp_piece != end_piece) {
	    temp_piece = temp_piece->next;
	    RemovePiece(src, temp_piece->prev);
	}
	end_piece->used -= endPos - end_first;
	if (end_piece->used != 0)
	    memmove(end_piece->text, end_piece->text + endPos - end_first,
		    end_piece->used * sizeof(wchar_t));
    }
    else {		    /* We are fully in one piece */
	if ((start_piece->used -= endPos - startPos) == 0) {
	    if (!(start_piece->next == NULL && start_piece->prev == NULL))
		RemovePiece(src, start_piece);
	}
	else {
	    memmove(start_piece->text + (startPos - start_first),
		    start_piece->text + (endPos - start_first),
		    (start_piece->used - (startPos - start_first)) *
		    sizeof(wchar_t));
	    if (src->multi_src.use_string_in_place &&
		((src->multi_src.length - (endPos - startPos))
		< src->multi_src.piece_size - 1))
		start_piece->text[src->multi_src.length - (endPos - startPos)] =
		  (wchar_t)0;
	}
    }

    src->multi_src.length += text.length -(endPos - startPos);

    /* STEP 4: insert the new stuff */
    if ( text.length != 0) {
        start_piece = FindPiece(src, startPos, &start_first);
        length = text.length;
        firstPos = text.firstPos;

	while (length > 0) {
	    wchar_t *ptr;
	    int fill;

	    if (src->multi_src.use_string_in_place) {
		if (start_piece->used == src->multi_src.piece_size - 1)  {

		    /*
		     * The string is used in place, then the string
		     * is not allowed to grow
		     */
		    start_piece->used = src->multi_src.length =
			src->multi_src.piece_size - 1;

		    start_piece->text[src->multi_src.length] = (wchar_t)0;
		    return (XawEditError);
		}
	    }

	    if (start_piece->used == src->multi_src.piece_size) {
		BreakPiece(src, start_piece);
		start_piece = FindPiece(src, startPos, &start_first);
	    }

	    fill = Min((int)(src->multi_src.piece_size - start_piece->used), length);

	    ptr = start_piece->text + (startPos - start_first);
	    memmove(ptr + fill, ptr, (start_piece->used -
		    (startPos - start_first)) * sizeof(wchar_t));
	    wptr =(wchar_t *)text.ptr;
	    (void)wcsncpy(ptr, wptr + firstPos, fill);

	    startPos += fill;
	    firstPos += fill;
	    start_piece->used += fill;
	    length -= fill;
	}
    }

    if (local_artificial_block == True)
	/* In other words, text is not the u_text that the user handed me but
	   one I made myself.  I only care, because I need to free the string */
	XtFree(text.ptr);

    if (src->multi_src.use_string_in_place)
	start_piece->text[start_piece->used] = (wchar_t)0;

#ifdef OLDXAW
    src->multi_src.changes = True;
    XtCallCallbacks(w, XtNcallback, NULL);
#endif

    return (XawEditDone);
}

/*
 * Function:
 *	Scan
 *
 * Parameters:
 *	w	 - MultiSource widget
 *	position - position to start scanning
 *	type	 - type of thing to scan for
 *	dir	 - direction to scan
 *	count	 - which occurance if this thing to search for
 *		   include - whether or not to include the character found in
 *		   the position that is returned
 *
 * Description:
 *	Scans the text source for the number and type of item specified.
 *
 * Returns:
 *	The position of the item found
 *
 * Note:
 *	  While there are only 'n' characters in the file there are n+1
 *	 possible cursor positions (one before the first character and
 *	one after the last character
 */
static XawTextPosition
Scan(Widget w, register XawTextPosition position, XawTextScanType type,
     XawTextScanDirection dir, int count, Bool include)
{
    MultiSrcObject src = (MultiSrcObject)w;
    register char inc;
    MultiPiece *piece;
    XawTextPosition first, first_eol_position = position;
    register wchar_t *ptr;
    int cnt = count;

    if (type == XawstAll) {
	if (dir == XawsdRight)
	    return (src->multi_src.length);
	return (0);
    }

    /* STEP 1: basic sanity checks */
    if (position > src->multi_src.length)
	position = src->multi_src.length;

    if (dir == XawsdRight) {
	if (position == src->multi_src.length)
	    return (src->multi_src.length);
	inc = 1;
    }
    else {
	if (position == 0)
	    return (0);
	inc = -1;
	position--;
    }

    piece = FindPiece(src, position, &first);

    if (piece->used == 0)
	return (0);

    ptr = (position - first) + piece->text;

    switch (type) {
	case XawstEOL:
	case XawstParagraph:
	case XawstWhiteSpace:
	case XawstAlphaNumeric:
	    for (; cnt > 0 ; cnt--) {
		Bool non_space = False, first_eol = True;

		/*CONSTCOND*/
		while (True) {
		    register wchar_t c;

		    if (ptr < piece->text) {
			piece = piece->prev;
			if (piece == NULL)	/* Begining of text */
			    return (0);
			ptr = piece->text + piece->used - 1;
			c = *ptr;
		    }
		    else if (ptr >= piece->text + piece->used) {
			piece = piece->next;
			if (piece == NULL)	/* End of text */
			    return (src->multi_src.length);
			ptr = piece->text;
		    }

		    c = *ptr;
		    ptr += inc;
		    position += inc;

		    if (type == XawstAlphaNumeric) {
			if (!iswalnum(c)) {
			    if (non_space)
				break;
			}
			else
			    non_space = True;
		    }
		    else if (type == XawstWhiteSpace) {
			if (iswspace(c)) {
			    if (non_space)
			      break;
			}
			else
			    non_space = True;
		    }
		    else if (type == XawstEOL) {
			if (c == _Xaw_atowc(XawLF))
			    break;
		    }
		    else {	/* XawstParagraph */
			if (first_eol) {
			    if (c == _Xaw_atowc(XawLF)) {
				first_eol_position = position;
				first_eol = False;
			    }
			}
			else
			    if (c == _Xaw_atowc(XawLF))
				break;
			else if (!iswspace(c))
			    first_eol = True;
		    }
		}
	    }
	    if (!include) {
		if (type == XawstParagraph)
		    position = first_eol_position;
		if (count)
		    position -= inc;
	    }
	    break;
	case XawstPositions:
	    position += count * inc;
	    break;
	default:
	    break;
    }

    if (dir == XawsdLeft)
	position++;

    if (position >= src->multi_src.length)
	return (src->multi_src.length);
    if (position < 0)
	return (0);

    return (position);
}

/*
 * Function:
 *	Search
 *
 * Parameters:
 *	w	 - MultiSource objecy
 *	position - position to start scanning
 *	dir	 - direction to scan
 *	text	 - text block to search for
 *
 * Description:
 *	Searchs the text source for the text block passed.
 *
 * Returns:
 *	The position of the item found
 */
static XawTextPosition
Search(Widget w, register XawTextPosition position, XawTextScanDirection dir,
       XawTextBlock *text)
{
    MultiSrcObject src = (MultiSrcObject)w;
    register int count = 0;
    wchar_t *ptr;
    wchar_t *wtarget;
    int wtarget_len;
    Display *d = XtDisplay(XtParent(w));
    MultiPiece *piece;
    wchar_t *buf;
    XawTextPosition first;
    register char inc;
    int cnt;

    /* STEP 1: First, a brief sanity check */
    if (dir == XawsdRight)
	inc = 1;
    else  {
	inc = -1;
	if (position == 0)
	    return (XawTextSearchError);
	position--;
    }

    /* STEP 2: Ensure I have a local wide string.. */

    /* Since this widget stores 32bit chars, I check here to see if
       I'm being passed a string claiming to be 8bit chars (ie, MB text.)
       If that is the case, naturally I convert to 32bit format */

    /*if the block was FMT8BIT, length will convert to REAL wchar count bellow */
    wtarget_len = text->length;

    if (text->format == XawFmtWide)
	wtarget = &(((wchar_t*)text->ptr) [text->firstPos]);
    else {
	/* The following converts wtarget_len from byte len to wchar count */
	   wtarget = _XawTextMBToWC(d, &text->ptr[text->firstPos], &wtarget_len);
    }

    /* OK, I can now assert that wtarget holds wide characters, wtarget_len
       holds an accurate count of those characters, and that firstPos has been
       effectively factored out of the following computations */

    /* STEP 3: SEARCH! */
    buf = (wchar_t *)XtMalloc(sizeof(wchar_t) * wtarget_len);
    (void)wcsncpy(buf, wtarget, wtarget_len);
    piece = FindPiece(src, position, &first);
    ptr = (position - first) + piece->text;

    /*CONSTCOND*/
    while (True) {
	if (*ptr == (dir == XawsdRight ? *(buf + count)
		     : *(buf + wtarget_len - count - 1))) {
	    if (count == text->length - 1)
		break;
	    else
		count++;
	}
	else {
	    if (count != 0) {
		position -=inc * count;
		ptr -= inc * count;
	    }
	    count = 0;
	}

	ptr += inc;
	position += inc;

	while (ptr < piece->text) {
	    cnt = piece->text - ptr;

	    piece = piece->prev;
	    if (piece == NULL) {	/* Begining of text */
		XtFree((char *)buf);
		return (XawTextSearchError);
	    }
	    ptr = piece->text + piece->used - cnt;
	}

	while (ptr >= piece->text + piece->used) {
	    cnt = ptr - (piece->text + piece->used);

	    piece = piece->next;
	    if (piece == NULL) {	/* End of text */
		XtFree((char *)buf);
		return (XawTextSearchError);
	    }
	    ptr = piece->text + cnt;
	}
    }

    XtFree((char *)buf);
    if (dir == XawsdLeft)
	return(position);

    return(position - (wtarget_len - 1));
}

/*
 * Function:
 *	XawMultiSrcSetValues
 *
 * Parameters:
 *	current  - current state of the widget
 *	request  - what was requested
 *	cnew	 - what the widget will become
 *	args	 - representation of resources that have changed
 *	num_args - number of changed resources
 *
 * Description:
 *	Sets the values for the MultiSource.
 *
 * Returns:
 *	True if redisplay is needed
 */
static Boolean
XawMultiSrcSetValues(Widget current, Widget request, Widget cnew,
		     ArgList args, Cardinal *num_args)
{
    MultiSrcObject src = (MultiSrcObject)cnew;
    MultiSrcObject old_src = (MultiSrcObject)current;
    XtAppContext app_con = XtWidgetToApplicationContext(cnew);
    Bool total_reset = False, string_set = False;
    FILE *file;
    unsigned int i;

    if (old_src->multi_src.use_string_in_place
	!= src->multi_src.use_string_in_place) {
	XtAppWarning(app_con,
		     "MultiSrc: The XtNuseStringInPlace resources "
		     "may not be changed.");
	src->multi_src.use_string_in_place =
	    old_src->multi_src.use_string_in_place;
    }

    for (i = 0; i < *num_args ; i++)
	if (streq(args[i].name, XtNstring)) {
	    string_set = True;
	    break;
	}

    if (string_set || old_src->multi_src.type != src->multi_src.type) {
	RemoveOldStringOrFile(old_src, string_set);
	src->multi_src.allocated_string = old_src->multi_src.allocated_string;
	file = InitStringOrFile(src, string_set);

        LoadPieces(src, file, NULL);
	if (file != NULL)
	    fclose(file);
#ifndef OLDXAW
	for (i = 0; i < src->text_src.num_text; i++)
	    /* Tell text widget what happened */
	    XawTextSetSource(src->text_src.text[i], cnew, 0);
#else
	XawTextSetSource(XtParent(cnew), cnew, 0);
#endif
	total_reset = True;
    }

    if (old_src->multi_src.multi_length != src->multi_src.multi_length)
	src->multi_src.piece_size = src->multi_src.multi_length + 1;

    if ( !total_reset && old_src->multi_src.piece_size
	 != src->multi_src.piece_size) {
	String mb_string = StorePiecesInString(old_src);

	if (mb_string != 0) {
	    FreeAllPieces(old_src);
	    LoadPieces(src, NULL, mb_string);
	    XtFree(mb_string);
	}
	else {
	    /* If the buffer holds bad chars, don't touch it... */
	    XtAppWarningMsg(app_con,
			    "convertError", "multiSource", "XawError",
			     XtName(XtParent((Widget)old_src)), NULL, NULL);
	    XtAppWarningMsg(app_con,
			    "convertError", "multiSource", "XawError",
			    "Non-character code(s) in buffer.", NULL, NULL);
	}
    }

    return (False);
}

static void
XawMultiSrcGetValuesHook(Widget w, ArgList args, Cardinal *num_args)
{
    MultiSrcObject src = (MultiSrcObject)w;
    unsigned int i;

    if (src->multi_src.type == XawAsciiString) {
	for (i = 0; i < *num_args ; i++) {
	    if (streq(args[i].name, XtNstring)) {
		if (src->multi_src.use_string_in_place)
		    *((char **)args[i].value) = (char *)
			src->multi_src.first_piece->text;
		else if (_XawMultiSave(w))	/* If save sucessful */
		    *((char **)args[i].value) = (char *)src->multi_src.string;
		break;
	    }
	}
    }
}

static void
XawMultiSrcDestroy(Widget w)
{
    RemoveOldStringOrFile((MultiSrcObject) w, True);
}

/*
 * Public routines
 */
/*
 * Function:
 *	XawMultiSourceFreeString
 *
 * Parameters:
 *	w - MultiSrc widget
 *
 * Description:
 *	  Frees the string returned by a get values call
 *                   on the string when the source is of type string.
 *
 * Note:
 * The public interface is XawAsciiSourceFreeString!
 */
void
_XawMultiSourceFreeString(Widget w)
{
    MultiSrcObject src = (MultiSrcObject)w;

    if (src->multi_src.allocated_string) {
	XtFree((char *)src->multi_src.string);
	src->multi_src.allocated_string = False;
	src->multi_src.string = NULL;
    }
}

/*
 * Function:
 *	_XawMultiSave
 *
 * Parameters:
 *	w - multiSrc Widget
 *
 * Description:
 *	Saves all the pieces into a file or string as required.
 *
 * Returns:
 *	True if the save was successful
 *
 * Note:
 * The public interface is XawAsciiSave(w)!
 */
Bool
_XawMultiSave(Widget w)
{
    MultiSrcObject src = (MultiSrcObject)w;
    XtAppContext app_con = XtWidgetToApplicationContext(w);
    char *mb_string;

    /*
     * If using the string in place then there is no need to play games
     * to get the internal info into a readable string
     */
    if (src->multi_src.use_string_in_place)
	return (True);

    if (src->multi_src.type == XawAsciiFile) {
#ifdef OLDXAW
	 if (!src->multi_src.changes)
#else
	if (!src->text_src.changed)		/* No changes to save */
#endif
	    return (True);

	mb_string = StorePiecesInString(src);

	if (mb_string != 0) {
	    if (WriteToFile(mb_string, (String)src->multi_src.string) == False) {
		XtFree(mb_string);
		return (False);
	    }
	    XtFree(mb_string);
#ifndef OLDXAW
	    src->text_src.changed = False;
#else
	    src->multi_src.changes = False;
#endif
	    return (True);
	}
	else {
	    /* If the buffer holds bad chars, don't touch it... */
	    XtAppWarningMsg(app_con,
			    "convertError", "multiSource", "XawError",
			    "Due to illegal characters, file not saved.",
			    NULL, NULL);
	    return (False);
	}
    }
    else  {
    /* THIS FUNCTIONALITY IS UNDOCUMENTED, probably UNNEEDED?  The manual
	   says this routine's only function is to save files to
	   disk.  -Sheeran */
	mb_string = StorePiecesInString(src);

	if (mb_string == 0) {
	    /* If the buffer holds bad chars, don't touch it... */
	    XtAppWarningMsg(app_con,
			    "convertError", "multiSource", "XawError",
			    XtName(XtParent((Widget)src)), NULL, NULL);
	    return (False);
	}

	/* assert: mb_string holds good characters so the buffer is fine */
	if (src->multi_src.allocated_string == True)
	    XtFree((char *)src->multi_src.string);
	else
	    src->multi_src.allocated_string = True;

        src->multi_src.string = mb_string;
    }
#ifdef OLDXAW
    src->multi_src.changes = False;
#else
    src->text_src.changed = False;
#endif

    return (True);
}

/*
 * Function:
 *	XawMultiSaveAsFile
 *
 * Parameters:
 *	w - MultiSrc widget
 *	name - name of the file to save this file into
 *
 * Description:
 *	Save the current buffer as a file.
 *
 * Returns:
 *	True if the save was sucessful
 *
 * Note:
 * The public interface is XawAsciiSaveAsFile!
 */
Bool
_XawMultiSaveAsFile(Widget w, _Xconst char* name)
{
    MultiSrcObject src = (MultiSrcObject)w;
    String mb_string;
    Bool ret;

    mb_string = StorePiecesInString(src);

    if (mb_string != 0) {
	ret = WriteToFile(mb_string, (char *)name);
	XtFree(mb_string);

	return (ret);
    }

    /* otherwise there was a conversion error.	So print widget name too */
    XtAppWarningMsg(XtWidgetToApplicationContext(w),
		    "convertError", "multiSource", "XawError",
		    XtName(XtParent(w)), NULL, NULL);

    return (False);
}

/*
 * Private Functions
 */
static void
RemoveOldStringOrFile(MultiSrcObject src, Bool checkString)
{
    FreeAllPieces(src);

    if (checkString && src->multi_src.allocated_string) {
	XtFree((char *)src->multi_src.string);
	src->multi_src.allocated_string = False;
	src->multi_src.string = NULL;
    }
}

/*
 * Function:
 *	WriteToFile
 *
 * Parameters:
 *	string - string to write
 *	name   - name of the file
 *
 * Description:
 *	Write the string specified to the begining of the file  specified.
 *
 * Returns:
 *	Returns True if sucessful, False otherwise
 */
static Bool
WriteToFile(String string, String name)
{
    int fd;

    if (((fd = creat(name, 0666)) == -1)
	|| (write(fd, string, strlen(string)) == -1))
	return (False);

    if (close(fd) == -1)
	return (False);

    return (True);
}


/*
 * Function:
 *	StorePiecesInString
 *
 * Parameters:
 *	src - the multiSrc object to gather data from
 *
 * Description:
 *	Store the pieces in memory into a char string.
 *
 * Returns:
 *	mb_string:	Caller must free
 *	(or)
 *	NULL:		conversion error
 */
static String
StorePiecesInString(MultiSrcObject src)
{
    wchar_t *wc_string;
    char *mb_string;
    int char_count = src->multi_src.length;
    XawTextPosition first;
    MultiPiece *piece;

    /* I believe the char_count + 1 and the NULL termination are unneeded! FS */
    wc_string = (wchar_t*)XtMalloc((char_count + 1) * sizeof(wchar_t));

    for (first = 0, piece = src->multi_src.first_piece ; piece != NULL;
	 first += piece->used, piece = piece->next)
	(void)wcsncpy(wc_string + first, piece->text, piece->used);

    wc_string[char_count] = 0;

    /* This will refill all pieces to capacity */
    if (src->multi_src.data_compression) {
	FreeAllPieces(src);
	LoadPieces(src, NULL, (char *)wc_string);
    }

    /* Lastly, convert it to a MB format and send it back */
    mb_string = _XawTextWCToMB(XtDisplayOfObject((Widget)src),
			       wc_string, &char_count);

    /* NOTE THAT mb_string MAY BE ZERO IF THE CONVERSION FAILED */
    XtFree((char*)wc_string);

    return (mb_string);
}

/*
 * Function:
 *	InitStringOrFile
 *
 * Parameters:
 *	src - MultiSource
 *
 * Description:
 *	Initializes the string or file.
 */
static FILE *
InitStringOrFile(MultiSrcObject src, Bool newString)
{
    mode_t open_mode = 0;
    const char *fdopen_mode = NULL;
    int fd;
    FILE *file;
    Display *d = XtDisplayOfObject((Widget)src);

    if (src->multi_src.type == XawAsciiString) {
	if (src->multi_src.string == NULL)
	    src->multi_src.length = 0;

	else if (!src->multi_src.use_string_in_place) {
	    int length;
	    String temp = XtNewString((char *)src->multi_src.string);

	    if (src->multi_src.allocated_string)
		XtFree((char *)src->multi_src.string);
	    src->multi_src.allocated_string = True;
	    src->multi_src.string = temp;

	    length = strlen((char *)src->multi_src.string);

	    /* Wasteful, throwing away the WC string, but need side effect! */
	    (void)_XawTextMBToWC(d, (char *)src->multi_src.string, &length);
	    src->multi_src.length = (XawTextPosition)length;
	}
	else {
	    src->multi_src.length = strlen((char *)src->multi_src.string);
	    /* In case the length resource is incorrectly set */
	    if (src->multi_src.length > src->multi_src.multi_length)
		src->multi_src.multi_length = src->multi_src.length;

	    if (src->multi_src.multi_length == MAGIC_VALUE)
		src->multi_src.piece_size = src->multi_src.length;
	    else
		src->multi_src.piece_size = src->multi_src.multi_length + 1;
	}

	return (NULL);
    }

    /*
     * type is XawAsciiFile
     */
    src->multi_src.is_tempfile = False;

    switch (src->text_src.edit_mode) {
	case XawtextRead:
	    if (src->multi_src.string == NULL)
		XtErrorMsg("NoFile", "multiSourceCreate", "XawError",
			   "Creating a read only disk widget and no file specified.",
			   NULL, 0);
	    open_mode = O_RDONLY;
	    fdopen_mode = "r";
	    break;
	case XawtextAppend:
	case XawtextEdit:
	    if (src->multi_src.string == NULL) {
		src->multi_src.string = "*multi-src*";
		src->multi_src.is_tempfile = True;
	    }
	    else {
/* O_NOFOLLOW is a BSD & Linux extension */
#ifdef O_NOFOLLOW
		open_mode = O_RDWR | O_NOFOLLOW;
#else
		open_mode = O_RDWR; /* unsafe; subject to race conditions */
#endif
		fdopen_mode = "r+";
	    }
	    break;
	default:
	    XtErrorMsg("badMode", "multiSourceCreate", "XawError",
		       "Bad editMode for multi source; must be "
		       "Read, Append or Edit.", NULL, NULL);
    }

    /* If is_tempfile, allocate a private copy of the text
     * Unlikely to be changed, just to set allocated_string */
    if (newString || src->multi_src.is_tempfile) {
	String temp = XtNewString((char *)src->multi_src.string);

	if (src->multi_src.allocated_string)
	    XtFree((char *)src->multi_src.string);
	src->multi_src.string = temp;
	src->multi_src.allocated_string = True;
    }

    if (!src->multi_src.is_tempfile) {
	if ((fd = open((char *)src->multi_src.string, open_mode, 0666)) != -1) {
	    if ((file = fdopen(fd, fdopen_mode)) != NULL) {
		(void)fseek(file, 0, SEEK_END);
		src->multi_src.length = (XawTextPosition)ftell(file);
		return(file);
	    }
	    else
		close(fd);
	}
	{
	    String params[2];
	    Cardinal num_params = 2;

	    params[0] = (String)src->multi_src.string;
	    params[1] = strerror(errno);
	    XtAppWarningMsg(XtWidgetToApplicationContext((Widget)src),
			    "openError", "multiSourceCreate", "XawWarning",
			    "Cannot open file %s; %s", params, &num_params);
	}
    }
    src->multi_src.length = 0;
    return (NULL);
}

/* LoadPieces:  This routine takes either the MB contents of open file
   `file' or the MB contents of string or the MB contents of
   src->multi_src.string and places them in Pieces in WC format.

   CAUTION: You must have src->multi_src.length set to file length bytes
   when src->multi_src.type == XawAsciiFile.  src->multi_src.length must be
   the length of the parameter string if string is non-NULL
*/
static void
LoadPieces(MultiSrcObject src, FILE *file, char *string)
{
    Display *d = XtDisplayOfObject((Widget)src);
    wchar_t* local_str, *ptr;
    MultiPiece* piece = NULL;
    XawTextPosition left;
    int bytes = sizeof(wchar_t);
    char* temp_mb_holder = NULL;

    /*
     * This is tricky - the _XawTextMBtoWC converter uses its 3rd arg
     * in as MB length, out as WC length.  We want local_length to be
     * WC count.
     */
    int local_length = src->multi_src.length;

    if (string != NULL) {
	/*
	 * ASSERT: IF our caller passed a non-null string, THEN
	 * src->multi_src.length is currently string's * byte count,
	 * AND string is in a MB format
	*/
	local_str = _XawTextMBToWC(d, (char *)string, &local_length);
	src->multi_src.length = (XawTextPosition) local_length;
    }
    else if (src->multi_src.type != XawAsciiFile) {
	/*
	 * here, we are not changing the contents, just reloading,
	 * so don't change len...
	 */
	local_length = src->multi_src.string ?
	    strlen((char *)src->multi_src.string) : 0;
	local_str = _XawTextMBToWC(d, (char *)src->multi_src.string,
				   &local_length);
    }
    else {
	if (src->multi_src.length != 0) {
	    temp_mb_holder =
		XtMalloc((src->multi_src.length + 1) * sizeof(unsigned char));
	    fseek(file, 0, 0);
	    src->multi_src.length = fread(temp_mb_holder,
					  (Size_t)sizeof(unsigned char),
					  (Size_t)src->multi_src.length, file);
	    if (src->multi_src.length <= 0)
		XtAppErrorMsg(XtWidgetToApplicationContext ((Widget) src),
			      "readError", "multiSource", "XawError",
			      "fread returned error.", NULL, NULL);
	    local_length = src->multi_src.length;
	    local_str = _XawTextMBToWC(d, temp_mb_holder, &local_length);
	    src->multi_src.length = local_length;

	    if (local_str == 0) {
		String params[2];
		Cardinal num_params;
		static char err_text[] =
		    "<<< FILE CONTENTS NOT REPRESENTABLE IN THIS LOCALE >>>";

		params[0] = XtName(XtParent((Widget)src));
		params[1] = src->multi_src.string;
		num_params = 2;

		XtAppWarningMsg(XtWidgetToApplicationContext((Widget)src),
				"readLocaleError", "multiSource", "XawError",
				"%s: The file `%s' contains characters "
				"not representable in this locale.",
				params, &num_params);
		src->multi_src.length = sizeof err_text;
		local_length = src->multi_src.length;
		local_str = _XawTextMBToWC(d, err_text, &local_length);
		src->multi_src.length = local_length;
	    }
	}
	else
	    /* ASSERT that since following while loop looks at local_length
	       this isn't needed.	Sheeran, Omron KK, 1993/07/15
	       temp_mb_holder[src->multi_src.length] = '\0'; */
	    local_str = (wchar_t*)temp_mb_holder;
    }

    if (src->multi_src.use_string_in_place) {
	piece = AllocNewPiece(src, piece);
	piece->used = Min(src->multi_src.length, src->multi_src.piece_size);
	piece->text = (wchar_t*)src->multi_src.string;
	return;
    }

    ptr = local_str;
    left = local_length;

    do {
	piece = AllocNewPiece(src, piece);

	piece->text = (wchar_t*)XtMalloc((unsigned)(src->multi_src.piece_size
						    * bytes));
	piece->used = Min(left, src->multi_src.piece_size);
	if (piece->used != 0)
	(void)wcsncpy(piece->text, ptr, piece->used);

	left -= piece->used;
	ptr += piece->used;
    } while (left > 0);

    if (temp_mb_holder)
	XtFree((char*)temp_mb_holder);
}

/*
 * Function:
 *	AllocNewPiece
 *
 * Parameters:
 *	src  - MultiSrc Widget
 *	prev - the piece just before this one, or NULL
 *
 * Description:
 *	Allocates a new piece of memory.
 *
 * Returns:
 *	The allocated piece
 */
static MultiPiece *
AllocNewPiece(MultiSrcObject src, MultiPiece *prev)
{
    MultiPiece *piece = XtNew(MultiPiece);

    if (prev == NULL) {
	src->multi_src.first_piece = piece;
	piece->next = NULL;
    }
    else {
	if (prev->next != NULL)
	    (prev->next)->prev = piece;
	piece->next = prev->next;
	prev->next = piece;
    }

    piece->prev = prev;

    return (piece);
}

/*
 * Function:
 *	FreeAllPieces
 *
 * Parameters:
 *	src - MultiSrc Widget
 *
 * Description:
 *	Frees all the pieces
 */
static void
FreeAllPieces(MultiSrcObject src)
{
    MultiPiece *next, *first = src->multi_src.first_piece;

#ifdef DEBUG
    if (first->prev != NULL)
	printf("Xaw MultiSrc Object: possible memory leak in FreeAllPieces().\n");
#endif

    for (; first != NULL ; first = next) {
	next = first->next;
	RemovePiece(src, first);
    }
}

/*
 * Function:
 *	RemovePiece
 *
 * Parameters:
 *	piece - piece to remove
 *
 * Description:
 *	Removes a piece from the list.
 */
static void
RemovePiece(MultiSrcObject src, MultiPiece *piece)
{
    if (piece->prev == NULL)
	src->multi_src.first_piece = piece->next;
    else
	piece->prev->next = piece->next;

    if (piece->next != NULL)
	piece->next->prev = piece->prev;

    if (!src->multi_src.use_string_in_place)
	XtFree((char *)piece->text);

    XtFree((char *)piece);
}

/*
 * Function:
 *	FindPiece
 *
 * Parameters:
 *	src - MultiSrc Widget
 *	position - position that we are searching for
 *	first - position of the first character in this piece (return)
 *
 * Description:
 *	Finds the piece containing the position indicated.
 *
 * Returns:
 *	Piece that contains this position
 */
static MultiPiece *
FindPiece(MultiSrcObject src, XawTextPosition position, XawTextPosition *first)
{
    MultiPiece *old_piece, *piece;
    XawTextPosition temp;

    for (old_piece = NULL, piece = src->multi_src.first_piece, temp = 0;
         piece; old_piece = piece, piece = piece->next)
	if ((temp += piece->used) > position) {
	    *first = temp - piece->used;
	    return (piece);
	}

    *first = temp - (old_piece ? old_piece->used : 0);

    return (old_piece);	  /* if we run off the end the return the last piece */
}

/*
 * Function:
 *	BreakPiece
 *
 * Parameters:
 *	src - MultiSrc Widget
 *	piece - piece to break
 *
 * Description:
 *	Breaks a full piece into two new pieces.
 */
#define HALF_PIECE (src->multi_src.piece_size >> 1)
static void
BreakPiece(MultiSrcObject src, MultiPiece *piece)
{
    MultiPiece *cnew = AllocNewPiece(src, piece);

    cnew->text = (wchar_t *)
	XtMalloc(src->multi_src.piece_size * sizeof(wchar_t));
    (void)wcsncpy(cnew->text, piece->text + HALF_PIECE,
		  src->multi_src.piece_size - HALF_PIECE);
    piece->used = HALF_PIECE;
    cnew->used = src->multi_src.piece_size - HALF_PIECE;
}

/*ARGSUSED*/
static void
CvtStringToMultiType(XrmValuePtr args, Cardinal *num_args,
		     XrmValuePtr fromVal, XrmValuePtr toVal)
{
    static XawAsciiType type = XawAsciiString;
    XrmQuark q;
    char name[7];

    XmuNCopyISOLatin1Lowered(name, (char *)fromVal->addr, sizeof(name));
    q = XrmStringToQuark(name);

    if (q == Qstring)
	type = XawAsciiString;
    if (q == Qfile)
	type = XawAsciiFile;
    else {
	toVal->size = 0;
	toVal->addr = NULL;
	XtStringConversionWarning((char *)fromVal->addr, XtRAsciiType);
    }

    toVal->size = sizeof(XawAsciiType);
    toVal->addr = (XPointer)&type;
}

/*ARGSUSED*/
static Boolean
CvtMultiTypeToString(Display *dpy, XrmValuePtr args, Cardinal *num_args,
		     XrmValuePtr fromVal, XrmValuePtr toVal,
		     XtPointer *data)
{
    static String buffer;
    Cardinal size;

    switch (*(XawAsciiType *)fromVal->addr) {
	case XawAsciiFile:
	    buffer = XtEfile;
	    break;
	case XawAsciiString:
	    buffer = XtEstring;
	    break;
	default:
	    XawTypeToStringWarning(dpy, XtRAsciiType);
	    toVal->addr = NULL;
	    toVal->size = 0;
	    return (False);
    }

    size = strlen(buffer) + 1;
    if (toVal->addr != NULL) {
	if (toVal->size < size) {
	    toVal->size = size;
	    return (False);
	}
	strcpy((char *)toVal->addr, buffer);
    }
    else
	toVal->addr = (XPointer)buffer;
    toVal->size = sizeof(String);

    return (True);
}

/*ARGSUSED*/
static void
GetDefaultPieceSize(Widget w, int offset, XrmValue *value)
{
    static XPointer pagesize;

    if (pagesize == 0) {
	pagesize = (XPointer)((long)_XawGetPageSize());
	if (pagesize < (XPointer)BUFSIZ)
	    pagesize = (XPointer)BUFSIZ;
    }

    value->addr = (XPointer)&pagesize;
}