-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckUtil.c
1289 lines (1183 loc) · 33.8 KB
/
ckUtil.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
/*
* ckUtil.c --
*
* Miscellaneous utility functions.
*
* 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 "ckPort.h"
#include "ck.h"
#if CK_USE_UTF
#include <wchar.h>
#endif
#define REPLACE 1
#define NORMAL 2
#define TAB 3
#define NEWLINE 4
#define GCHAR 5
struct charType {
char type; /* Type of char, see definitions above. */
char width; /* Width if replaced by backslash sequence. */
};
struct charEncoding {
char *name; /* Name for this encoding table. */
struct charType ct[256]; /* Encoding table. */
};
/*
* For ISO8859, codes 0x81..0x99 are mapped to ACS characters
* according to this table:
*/
static char *gcharTab[] = {
"ulcorner", "llcorner", "urcorner", "lrcorner",
"ltee", "rtee", "btee", "ttee",
"hline", "vline", "plus", "s1",
"s9", "diamond", "ckboard", "degree",
"plminus", "bullet", "larrow", "rarrow",
"darrow", "uarrow", "board", "lantern",
"block"
};
static struct charEncoding EncodingTable[] = {
/*
*----------------------------------------------------------------------
*
* ISO 8859 encoding.
*
*----------------------------------------------------------------------
*/
{
"ISO8859", {
{ REPLACE, 4 }, /* \x00 */
{ REPLACE, 4 }, /* \x01 */
{ REPLACE, 4 }, /* \x02 */
{ REPLACE, 4 }, /* \x03 */
{ REPLACE, 4 }, /* \x04 */
{ REPLACE, 4 }, /* \x05 */
{ REPLACE, 4 }, /* \x06 */
{ REPLACE, 4 }, /* \x07 */
{ REPLACE, 2 }, /* \b */
{ TAB, 2 }, /* \t */
{ NEWLINE, 2 }, /* \n */
{ REPLACE, 4 }, /* \x0b */
{ REPLACE, 2 }, /* \f */
{ REPLACE, 2 }, /* \r */
{ REPLACE, 4 }, /* 0x0e */
{ REPLACE, 4 }, /* 0x0f */
/* 0x10 .. 0x1f */
{ REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 },
{ REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 },
{ REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 },
{ REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 },
/* ' ' .. '/' */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* '0' .. '?' */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* '@' .. 'O' */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* 'P' .. '_' */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* '`' .. 'o' */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* 'p' .. '~' */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ REPLACE, 4 }, /* 0x7f */
/* 0x80 .. 0x8f */
{ REPLACE, 4 }, { GCHAR, 1 }, { GCHAR, 1 }, { GCHAR, 1 },
{ GCHAR, 1 }, { GCHAR, 1 }, { GCHAR, 1 }, { GCHAR, 1 },
{ GCHAR, 1 }, { GCHAR, 1 }, { GCHAR, 1 }, { GCHAR, 1 },
{ GCHAR, 1 }, { GCHAR, 1 }, { GCHAR, 1 }, { GCHAR, 1 },
/* 0x90 .. 0x9f */
{ GCHAR, 1 }, { GCHAR, 1 }, { GCHAR, 1 }, { GCHAR, 1 },
{ GCHAR, 1 }, { GCHAR, 1 }, { GCHAR, 1 }, { GCHAR, 1 },
{ GCHAR, 1 }, { GCHAR, 1 }, { REPLACE, 4 }, { REPLACE, 4 },
{ REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 },
/* 0xa0 .. 0xaf */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* 0xb0 .. 0xbf */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* 0xc0 .. 0xcf */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* 0xd0 .. 0xdf */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* 0xe0 .. 0xef */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* 0xf0 .. 0xff */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
}
},
/*
*----------------------------------------------------------------------
*
* IBM code page 437 encoding.
*
*----------------------------------------------------------------------
*/
{
"IBM437", {
{ REPLACE, 4 }, /* \x00 */
{ REPLACE, 4 }, /* \x01 */
{ REPLACE, 4 }, /* \x02 */
{ REPLACE, 4 }, /* \x03 */
{ REPLACE, 4 }, /* \x04 */
{ REPLACE, 4 }, /* \x05 */
{ REPLACE, 4 }, /* \x06 */
{ REPLACE, 4 }, /* \x07 */
{ REPLACE, 2 }, /* \b */
{ TAB, 2 }, /* \t */
{ NEWLINE, 2 }, /* \n */
{ REPLACE, 4 }, /* \x0b */
{ REPLACE, 2 }, /* \f */
{ REPLACE, 2 }, /* \r */
{ REPLACE, 4 }, /* 0x0e */
{ REPLACE, 4 }, /* 0x0f */
/* 0x10 .. 0x1f */
{ REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 },
{ REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 },
{ REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 },
{ REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 }, { REPLACE, 4 },
/* ' ' .. '/' */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* '0' .. '?' */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* '@' .. 'O' */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* 'P' .. '_' */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* '`' .. 'o' */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* 'p' .. '~' */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ REPLACE, 4 }, /* 0x7f */
/* 0x80 .. 0x8f */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* 0x90 .. 0x9a */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, /* 0x9b */
/* 0x9c .. 0x9f */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* 0xa0 .. 0xaf */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* 0xb0 .. 0xbf */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* 0xc0 .. 0xcf */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* 0xd0 .. 0xdf */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* 0xe0 .. 0xef */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
/* 0xf0 .. 0xff */
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 },
{ NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }, { NORMAL, 1 }
}
}
};
/*
* This is the switch for char encoding.
*/
static int Encoding = 0;
#define CHARTYPE(x) EncodingTable[Encoding].ct[(x)]
/*
* Characters used when displaying control sequences.
*/
static char hexChars[] = "0123456789abcdefxtnvr\\";
/*
* The following table maps some control characters to sequences
* like '\n' rather than '\x10'. A zero entry in the table means
* no such mapping exists, and the table only maps characters
* less than 0x10.
*/
static char mapChars[] = {
0, 0, 0, 0, 0, 0, 0, 0,
'b', 't', 'n', 0, 'f', 'r', 0
};
/*
*----------------------------------------------------------------------
*
* CkCopyAndGlobalEval --
*
* This procedure makes a copy of a script then calls Tcl_GlobalEval
* to evaluate it. It's used in situations where the execution of
* a command may cause the original command string to be reallocated.
*
* Results:
* Returns the result of evaluating script, including both a standard
* Tcl completion code and a string in interp->result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
CkCopyAndGlobalEval(interp, script)
Tcl_Interp *interp; /* Interpreter in which to evaluate
* script. */
char *script; /* Script to evaluate. */
{
Tcl_DString buffer;
int code;
Tcl_DStringInit(&buffer);
Tcl_DStringAppend(&buffer, script, -1);
code = Tcl_GlobalEval(interp, Tcl_DStringValue(&buffer));
Tcl_DStringFree(&buffer);
return code;
}
/*
*----------------------------------------------------------------------
*
* Ck_GetScrollInfo --
*
* This procedure is invoked to parse "xview" and "yview"
* scrolling commands for widgets using the new scrolling
* command syntax ("moveto" or "scroll" options).
*
* Results:
* The return value is either CK_SCROLL_MOVETO, CK_SCROLL_PAGES,
* CK_SCROLL_UNITS, or CK_SCROLL_ERROR. This indicates whether
* the command was successfully parsed and what form the command
* took. If CK_SCROLL_MOVETO, *dblPtr is filled in with the
* desired position; if CK_SCROLL_PAGES or CK_SCROLL_UNITS,
* *intPtr is filled in with the number of lines to move (may be
* negative); if CK_SCROLL_ERROR, interp->result contains an
* error message.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
Ck_GetScrollInfo(interp, argc, argv, dblPtr, intPtr)
Tcl_Interp *interp; /* Used for error reporting. */
int argc; /* # arguments for command. */
char **argv; /* Arguments for command. */
double *dblPtr; /* Filled in with argument "moveto"
* option, if any. */
int *intPtr; /* Filled in with number of pages
* or lines to scroll, if any. */
{
int c;
size_t length;
length = strlen(argv[2]);
c = argv[2][0];
if ((c == 'm') && (strncmp(argv[2], "moveto", length) == 0)) {
if (argc != 4) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " ", argv[1], " moveto fraction\"",
(char *) NULL);
return CK_SCROLL_ERROR;
}
if (Tcl_GetDouble(interp, argv[3], dblPtr) != TCL_OK) {
return CK_SCROLL_ERROR;
}
return CK_SCROLL_MOVETO;
} else if ((c == 's')
&& (strncmp(argv[2], "scroll", length) == 0)) {
if (argc != 5) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " ", argv[1], " scroll number units|pages\"",
(char *) NULL);
return CK_SCROLL_ERROR;
}
if (Tcl_GetInt(interp, argv[3], intPtr) != TCL_OK) {
return CK_SCROLL_ERROR;
}
length = strlen(argv[4]);
c = argv[4][0];
if ((c == 'p') && (strncmp(argv[4], "pages", length) == 0)) {
return CK_SCROLL_PAGES;
} else if ((c == 'u')
&& (strncmp(argv[4], "units", length) == 0)) {
return CK_SCROLL_UNITS;
} else {
Tcl_AppendResult(interp, "bad argument \"", argv[4],
"\": must be units or pages", (char *) NULL);
return CK_SCROLL_ERROR;
}
}
Tcl_AppendResult(interp, "unknown option \"", argv[2],
"\": must be moveto or scroll", (char *) NULL);
return CK_SCROLL_ERROR;
}
/*
*--------------------------------------------------------------
*
* Ck_SetEncoding --
*
*--------------------------------------------------------------
*/
int
Ck_SetEncoding(interp, name)
Tcl_Interp *interp;
char *name;
{
int i;
for (i = 0; i < sizeof (EncodingTable) / sizeof (EncodingTable[0]); i++)
if (strcmp(name, EncodingTable[i].name) == 0) {
Encoding = i;
return TCL_OK;
}
Tcl_AppendResult(interp, "no encoding \"", name, "\"", (char *) NULL);
return TCL_ERROR;
}
/*
*--------------------------------------------------------------
*
* Ck_GetEncoding --
*
*--------------------------------------------------------------
*/
int
Ck_GetEncoding(interp)
Tcl_Interp *interp;
{
Tcl_SetObjResult( interp, Tcl_NewStringObj(EncodingTable[Encoding].name,-1));
return TCL_OK;
}
#if CK_USE_UTF
/*
*--------------------------------------------------------------
*
* MakeUCRepl --
*
* Make replacement for unprintable chars.
*
*--------------------------------------------------------------
*/
static int
MakeUCRepl(uch, buf)
Tcl_UniChar uch;
char *buf;
{
unsigned int i, need;
if ((unsigned int) uch < sizeof (mapChars) &&
mapChars[(unsigned int) uch]) {
if (buf) {
*buf++ = '\\';
*buf++ = mapChars[(unsigned int) uch];
*buf++ = '\0';
}
return 2;
}
for (i = 0x100, need = 2; i; i++, need++) {
if ((unsigned int) uch < i) {
break;
}
i = i << 4;
}
if (buf) {
char *p;
*buf++ = '\\';
*buf++ = (need < 3) ? 'x' : 'u';
p = buf + need;
*p = '\0';
for (i = 0; i < need; i++) {
--p;
*p = hexChars[uch & 0xf];
uch = uch >> 4;
}
}
return need + 2;
}
#endif
/*
*--------------------------------------------------------------
*
* CkMeasureChars --
*
* Measure the number of characters from a string that
* will fit in a given horizontal span. The measurement
* is done under the assumption that CkDisplayChars will
* be used to actually display the characters.
*
* Results:
* The return value is the number of characters from source
* that fit in the span given by startX and maxX. *nextXPtr
* is filled in with the x-coordinate at which the first
* character that didn't fit would be drawn, if it were to
* be drawn.
*
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
int
CkMeasureChars(mainPtr, source, maxChars, startX, maxX,
tabOrigin, flags, nextXPtr, nextCPtr)
CkMainInfo *mainPtr; /* Needed for encoding. */
char *source; /* Characters to be displayed. Need not
* be NULL-terminated. */
int maxChars; /* Maximum # of characters to consider from
* source. */
int startX; /* X-position at which first character will
* be drawn. */
int maxX; /* Don't consider any character that would
* cross this x-position. */
int tabOrigin; /* X-location that serves as "origin" for
* tab stops. */
int flags; /* Various flag bits OR-ed together.
* CK_WHOLE_WORDS means stop on a word boundary
* (just before a space character) if
* possible. CK_AT_LEAST_ONE means always
* return a value of at least one, even
* if the character doesn't fit.
* CK_PARTIAL_OK means it's OK to display only
* a part of the last character in the line.
* CK_NEWLINES_NOT_SPECIAL means that newlines
* are treated just like other control chars:
* they don't terminate the line.
* CK_IGNORE_TABS means give all tabs zero
* width. */
int *nextXPtr; /* Return x-position of terminating
* character here. */
int *nextCPtr; /* Return byte position of terminating
character in source. */
{
register char *p; /* Current character. */
register int c;
char *term; /* Pointer to most recent character that
* may legally be a terminating character. */
int termX; /* X-position just after term. */
int curX; /* X-position corresponding to p. */
int newX; /* X-position corresponding to p+1. */
int rem;
int nChars = 0;
#if CK_USE_UTF
int n, m, srcRead, dstWrote, dstChars;
Tcl_UniChar uch;
char buf[TCL_UTF_MAX], buf2[TCL_UTF_MAX];
/*
* Scan the input string one character at a time, until a character
* is found that crosses maxX.
*/
newX = curX = startX;
termX = 0;
term = source;
for (p = source; *p != '\0' && maxChars > 0;) {
char *p2;
n = Tcl_UtfToUniChar(p, &uch);
p2 = p + n;
++nChars;
maxChars -= n;
m = Tcl_UniCharToUtf(uch, buf);
if (mainPtr->isoEncoding) {
buf2[0] = '\0';
Tcl_UtfToExternal(NULL, mainPtr->isoEncoding, buf, m,
TCL_ENCODING_START | TCL_ENCODING_END,
NULL, buf2, sizeof (buf2), &srcRead,
&dstWrote, &dstChars);
} else {
buf2[0] = '?';
if (uch > 0 && uch < 0x100) {
buf2[0] = uch;
}
}
c = buf2[0] & 0xFF;
if (CHARTYPE(c).type == TAB) {
if (!(flags & CK_IGNORE_TABS)) {
newX += 8;
rem = (newX - tabOrigin) % 8;
if (rem < 0) {
rem += 8;
}
newX -= rem;
}
} else if (CHARTYPE(c).type == GCHAR) {
newX += CHARTYPE(c).width;
} else if (CHARTYPE(c).type == NEWLINE) {
if (flags & CK_NEWLINES_NOT_SPECIAL) {
newX += CHARTYPE(c).width;
} else {
break;
}
} else if (mainPtr->isoEncoding) {
if (c == 0 || (c == '?' && uch != '?')) {
newX += MakeUCRepl(uch, 0);
} else {
if (CHARTYPE(c).type == REPLACE) {
newX += CHARTYPE(c).width;
} else {
newX++;
}
}
} else {
int len = wcwidth((wint_t) uch);
if (len < 0) {
newX += MakeUCRepl(uch, 0);
} else {
newX += len;
}
}
if (newX > maxX) {
break;
}
p = p2;
if (maxChars > 1) {
n = Tcl_UtfToUniChar(p, &uch);
p2 = p + n;
m = Tcl_UniCharToUtf(uch, buf);
if (mainPtr->isoEncoding) {
buf2[0] = '\0';
Tcl_UtfToExternal(NULL, mainPtr->isoEncoding, buf, m,
TCL_ENCODING_START | TCL_ENCODING_END,
NULL, buf2, sizeof (buf2), &srcRead,
&dstWrote, &dstChars);
} else {
buf2[0] = '?';
if (uch > 0 && uch < 0x100) {
buf2[0] = uch;
}
}
c = buf2[0] & 0xff;
} else {
c = 0;
}
if (isspace(c) || (c == 0)) {
term = p2;
termX = newX;
}
curX = newX;
}
/*
* P points to the first character that doesn't fit in the desired
* span. Use the flags to figure out what to return.
*/
if ((flags & CK_PARTIAL_OK) && (curX < maxX)) {
curX = newX;
if (*p) {
n = Tcl_UtfToUniChar(p, &uch);
p += n;
++nChars;
}
}
if ((flags & CK_AT_LEAST_ONE) && (term == source) && (maxChars > 0)
&& !isspace((unsigned char) *term)) {
term = p;
termX = curX;
if (term == source) {
n = Tcl_UtfToUniChar(term, &uch);
term += n;
++nChars;
}
} else if ((maxChars == 0) || !(flags & CK_WHOLE_WORDS)) {
term = p;
termX = curX;
}
*nextXPtr = termX;
*nextCPtr = term - source;
return nChars;
#else
/*
* Scan the input string one character at a time, until a character
* is found that crosses maxX.
*/
newX = curX = startX;
termX = 0;
term = source;
for (p = source, c = *p & 0xff; c != '\0' && maxChars > 0;
p++, maxChars--) {
++nChars;
if ((CHARTYPE(c).type == NORMAL) || (CHARTYPE(c).type == REPLACE) ||
(CHARTYPE(c).type == GCHAR)) {
newX += CHARTYPE(c).width;
} else if (CHARTYPE(c).type == TAB) {
if (!(flags & CK_IGNORE_TABS)) {
newX += 8;
rem = (newX - tabOrigin) % 8;
if (rem < 0) {
rem += 8;
}
newX -= rem;
}
} else if (CHARTYPE(c).type == NEWLINE) {
if (flags & CK_NEWLINES_NOT_SPECIAL) {
newX += CHARTYPE(c).width;
} else {
break;
}
}
if (newX > maxX) {
break;
}
if (maxChars > 1) {
c = p[1] & 0xff;
} else {
c = 0;
}
if (isspace(c) || (c == 0)) {
term = p+1;
termX = newX;
}
curX = newX;
}
/*
* P points to the first character that doesn't fit in the desired
* span. Use the flags to figure out what to return.
*/
if ((flags & CK_PARTIAL_OK) && (curX < maxX)) {
curX = newX;
p++;
++nChars;
}
if ((flags & CK_AT_LEAST_ONE) && (term == source) && (maxChars > 0)
&& !isspace((unsigned char) *term)) {
term = p;
termX = curX;
if (term == source && *source) {
term++;
termX = newX;
++nChars;
}
} else if ((maxChars == 0) || !(flags & CK_WHOLE_WORDS)) {
term = p;
termX = curX;
}
*nextXPtr = termX;
*nextCPtr = term - source;
return nChars;
#endif
}
/*
*--------------------------------------------------------------
*
* CkDisplayChars --
*
* Draw a string of characters on the screen, converting
* tabs to the right number of spaces and control characters
* to sequences of the form "\xhh" where hh are two hex
* digits.
*
* Results:
* None.
*
* Side effects:
* Information gets drawn on the screen.
*
*--------------------------------------------------------------
*/
void
CkDisplayChars(mainPtr, window, string, numChars, x, y, tabOrigin, flags)
CkMainInfo *mainPtr; /* Needed for encoding. */
WINDOW *window; /* Curses window. */
char *string; /* Characters to be displayed. */
int numChars; /* Number of characters to display from
* string. */
int x, y; /* Coordinates at which to draw string. */
int tabOrigin; /* X-location that serves as "origin" for
* tab stops. */
int flags; /* Flags to control display. Only
* CK_NEWLINES_NOT_SPECIAL, CK_IGNORE_TABS
* and CK_FILL_UNTIL_EOL are supported right
* now. See CkMeasureChars for information
* about it. */
{
register char *p; /* Current character being scanned. */
register int c;
int startX; /* X-coordinate corresponding to start. */
int curX; /* X-coordinate corresponding to p. */
char replace[16];
int rem, dummy, maxX, nc = 0;
/*
* Scan the string one character at a time and display the
* character.
*/
getmaxyx(window, dummy, maxX);
#if CK_USE_UTF
nc = Tcl_NumUtfChars(string, numChars);
if (nc > maxX)
numChars = Tcl_UtfAtIndex(string, maxX) - string;
else
numChars = nc;
#endif
if (numChars > maxX)
numChars = maxX;
p = string;
if (x < 0) {
x = -x;
#if CK_USE_UTF
x = Tcl_UtfAtIndex(p, x) - p;
#endif
p += x;
numChars -= x;
x = 0;
}
wmove(window, y, x);
startX = curX = x;
for (; numChars > 0; numChars--, p += nc) {
#if CK_USE_UTF
Tcl_UniChar uch;
int len;
if (*p == '\0')
break;
nc = Tcl_UtfToUniChar(p, &uch);
if (mainPtr->isoEncoding) {
int srcRead, dstWrote, dstChars;
char buf[TCL_UTF_MAX];
buf[0] = '\0';
Tcl_UtfToExternal(NULL, mainPtr->isoEncoding, p, nc,
TCL_ENCODING_START | TCL_ENCODING_END,
NULL, buf, sizeof (buf), &srcRead,
&dstWrote, &dstChars);
c = buf[0] & 0xff;
if (dstWrote != 1 || (c == '?' && uch != '?')) {
c = '\0';
}
} else {
c = uch & 0xff;
}
if ((unsigned int) uch < 0x100 && CHARTYPE(c).type == TAB) {
if (!(flags & CK_IGNORE_TABS)) {
curX += 8;
rem = (curX - tabOrigin) % 8;
if (rem < 0) {
rem += 8;
}
curX -= rem;
}
while (startX < curX) {
waddch(window, ' ');
startX++;
}
continue;
} else if ((unsigned int) uch < 0x100 && CHARTYPE(c).type == GCHAR) {
long gchar;
if (Ck_GetGChar(NULL, gcharTab[c - 0x81], &gchar) != TCL_OK)
goto replaceChar;
waddch(window, gchar);
curX++;
} else if ((unsigned int) uch < 0x100 &&
CHARTYPE(c).type == NEWLINE &&
!(flags & CK_NEWLINES_NOT_SPECIAL)) {
y++;
wmove(window, y, x);
curX = x;
} else if (mainPtr->isoEncoding) {
if ((unsigned int) uch < 0x20 ||
CHARTYPE(c).type == REPLACE) {
replaceChar:
len = MakeUCRepl(uch, replace);
if (len + curX > maxX) {
len = maxX - curX;
if (len < 0)
len = 0;
}
waddnstr(window, replace, len);
} else {
len = 1;
waddch(window, c);
}
curX += len;
} else {
len = wcwidth((wint_t) uch);
if (len < 0 || (unsigned int) uch < 0x20) {
len = MakeUCRepl(uch, replace);
if (len + curX > maxX) {
len = maxX - curX;
if (len < 0)
len = 0;
}
waddnstr(window, replace, len);
} else {
#ifdef USE_NCURSESW
wchar_t w[2];
w[0] = uch;
w[1] = 0;
waddnwstr(window, w, 1);
#else
waddnstr(window, p, nc);
#endif
}
curX += len;
}
#else
nc = 1;
c = *p & 0xff;
if (c == '\0')
break;
if (CHARTYPE(c).type == NORMAL) {
waddch(window, c);
curX++;
}
if (CHARTYPE(c).type == TAB) {
if (!(flags & CK_IGNORE_TABS)) {
curX += 8;
rem = (curX - tabOrigin) % 8;
if (rem < 0) {
rem += 8;
}
curX -= rem;
}
while (startX < curX) {
waddch(window, ' ');
startX++;
}
continue;
} else if (CHARTYPE(c).type == GCHAR) {
long gchar;
if (Ck_GetGChar(NULL, gcharTab[c - 0x81], &gchar) != TCL_OK)
goto replaceChar;
waddch(window, gchar);
startX++;
curX = startX;
continue;
} else if (CHARTYPE(c).type == REPLACE || (CHARTYPE(c).type == NEWLINE
&& (flags & CK_NEWLINES_NOT_SPECIAL))) {
replaceChar: