File: cdk.c

package info (click to toggle)
libcdk5 5.0.20250116-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,584 kB
  • sloc: ansic: 32,706; sh: 5,364; makefile: 1,198; sed: 48; cpp: 41
file content (1592 lines) | stat: -rw-r--r-- 31,308 bytes parent folder | download
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
#include <cdk_int.h>

/*
 * $Author: tom $
 * $Date: 2025/01/14 21:32:41 $
 * $Revision: 1.232 $
 */

#define L_MARKER '<'
#define R_MARKER '>'

char *GPasteBuffer = NULL;

static boolean usingMarkup = TRUE;

/*
 * This beeps then flushes the stdout stream.
 */
void Beep (void)
{
   beep ();
   fflush (stdout);
}

/*
 * This sets a string to the given character.
 */
void cleanChar (char *s, int len, char character)
{
   if (s != NULL)
   {
      int x;
      for (x = 0; x < len; x++)
      {
	 s[x] = character;
      }
      s[--x] = '\0';
   }
}

void cleanChtype (chtype *s, int len, chtype character)
{
   if (s != NULL)
   {
      int x;
      for (x = 0; x < len; x++)
      {
	 s[x] = character;
      }
      s[--x] = '\0';
   }
}

/*
 * This takes an x and y position and realigns the values iff they sent in
 * values like CENTER, LEFT, RIGHT, ...
 */
void alignxy (WINDOW *window, int *xpos, int *ypos, int boxWidth, int boxHeight)
{
   int first, gap, limit, last;

   first = getbegx (window);
   limit = getmaxx (window);
   if ((gap = (limit - boxWidth)) < 0)
      gap = 0;
   last = first + gap;

   switch (*xpos)
   {
   case LEFT:
      (*xpos) = first;
      break;
   case RIGHT:
      (*xpos) = first + gap;
      break;
   case CENTER:
      (*xpos) = first + (gap / 2);
      break;
   default:
      if ((*xpos) > last)
	 (*xpos) = last;
      else if ((*xpos) < first)
	 (*xpos) = first;
      break;
   }
   (void)last;

   first = getbegy (window);
   limit = getmaxy (window);
   if ((gap = (limit - boxHeight)) < 0)
      gap = 0;
   last = first + gap;

   switch (*ypos)
   {
   case TOP:
      (*ypos) = first;
      break;
   case BOTTOM:
      (*ypos) = first + gap;
      break;
   case CENTER:
      (*ypos) = first + (gap / 2);
      break;
   default:
      if ((*ypos) > last)
      {
	 (*ypos) = last;
      }
      else if ((*ypos) < first)
      {
	 (*ypos) = first;
      }
      break;
   }
}

/*
 * This takes a string, a field width and a justification type
 * and returns the adjustment to make, to fill
 * the justification requirement.
 */
int justifyString (int boxWidth, int mesgLength, int justify)
{
   /*
    * Make sure the message isn't longer than the width.
    * If it is, return 0.
    */
   if (mesgLength >= boxWidth)
      return (0);

   /* Try to justify the message.  */
   if (justify == LEFT)
      return (0);

   if (justify == RIGHT)
      return boxWidth - mesgLength;

   if (justify == CENTER)
      return ((int)((boxWidth - mesgLength) / 2));

   return (justify);
}

/*
 * This frees a string if it is not null. This is a safety
 * measure. Some compilers let you free a null string. I
 * don't like that idea.
 */
void freeChar (char *string)
{
   freeChecked (string);
}

void freeChtype (chtype *string)
{
   freeChecked (string);
}

/*
 * Corresponding list freeing (does not free the list pointer).
 */
void freeCharList (char **list, unsigned size)
{
   if (list != NULL)
   {
      while (size-- != 0)
      {
	 freeChar (list[size]);
	 list[size] = NULL;
      }
   }
}

void freeChtypeList (chtype **list, unsigned size)
{
   if (list != NULL)
   {
      while (size-- != 0)
      {
	 freeChtype (list[size]);
	 list[size] = NULL;
      }
   }
}

/*
 * This performs a safe copy of a string. This means it adds the null
 * terminator on the end of the string, like strdup().
 */
char *copyChar (const char *original)
{
   char *newstring = NULL;

   if (original != NULL)
   {
      if ((newstring = typeMallocN (char, strlen (original) + 1)) != NULL)
	   strcpy (newstring, original);
   }
   return (newstring);
}

chtype *copyChtype (const chtype *original)
{
   chtype *newstring = NULL;

   if (original != NULL)
   {
      int len = chlen (original);

      if ((newstring = typeMallocN (chtype, len + 4)) != NULL)
      {
	 int x;

	 for (x = 0; x < len; x++)
	 {
	    newstring[x] = original[x];
	 }
	 newstring[len] = '\0';
	 newstring[len + 1] = '\0';
      }
   }
   return (newstring);
}

/*
 * Copy the given lists.
 */
char **copyCharList (const char **list)
{
   size_t size = (size_t)lenCharList (list) + 1;
   char **result = typeMallocN (char *, size);

   if (result != NULL)
   {
      unsigned n;
      for (n = 0; n < size; ++n)
	 result[n] = copyChar (list[n]);
   }
   return result;
}

chtype **copyChtypeList (const chtype **list)
{
   size_t size = (size_t)lenChtypeList (list) + 1;
   chtype **result = typeMallocN (chtype *, size);

   if (result != NULL)
   {
      unsigned n;
      for (n = 0; n < size; ++n)
	 result[n] = copyChtype (list[n]);
   }
   return result;
}

/*
 * Return the length of the given lists.
 */
int lenCharList (const char **list)
{
   int result = 0;
   if (list != NULL)
   {
      while (*list++ != NULL)
	 ++result;
   }
   return result;
}

int lenChtypeList (const chtype **list)
{
   int result = 0;
   if (list != NULL)
   {
      while (*list++ != NULL)
	 ++result;
   }
   return result;
}

int CDKchdir (const char *directory)
{
   int result;

#ifdef HAVE_WORDEXP
   wordexp_t expanded;
   if (wordexp (directory, &expanded, WRDE_NOCMD) == 0)
   {
      if (expanded.we_wordc == 1 && strcmp (expanded.we_wordv[0], directory))
	 result = chdir (expanded.we_wordv[0]);
      else
	 result = chdir (directory);
      wordfree (&expanded);
   }
   else
#endif
      result = chdir (directory);
   return result;
}

FILE *CDKopenFile (const char *filename, const char *mode)
{
   FILE *result;

#ifdef HAVE_WORDEXP
   wordexp_t expanded;
   if (wordexp (filename, &expanded, WRDE_NOCMD) == 0)
   {
      if (expanded.we_wordc == 1 && strcmp (expanded.we_wordv[0], filename))
	 result = fopen (expanded.we_wordv[0], mode);
      else
	 result = fopen (filename, mode);
      wordfree (&expanded);
   }
   else
#endif
      result = fopen (filename, mode);
   return result;
}

/*
 * This reads a file and sticks it into the char *** provided.
 */
int CDKreadFile (const char *filename, char ***array)
{
   FILE *fd;
   char temp[BUFSIZ];
   unsigned lines = 0;
   unsigned used = 0;

   /* Can we open the file?  */
   if ((fd = CDKopenFile (filename, "r")) == NULL)
   {
      return (-1);
   }

   while ((fgets (temp, sizeof (temp), fd) != NULL))
   {
      size_t len = strlen (temp);
      if (len != 0 && temp[len - 1] == '\n')
	 temp[--len] = '\0';
      used = CDKallocStrings (array, temp, lines++, used);
   }
   fclose (fd);

   return (int)(lines);
}

#define DigitOf(c) ((c)-'0')

static int encodeAttribute (const char *string, int from, chtype *mask)
{
   *mask = 0;
   switch (string[from + 1])
   {
   case 'B':
      *mask = A_BOLD;
      break;
   case 'D':
      *mask = A_DIM;
      break;
   case 'K':
      *mask = A_BLINK;
      break;
   case 'R':
      *mask = A_REVERSE;
      break;
   case 'S':
      *mask = A_STANDOUT;
      break;
   case 'U':
      *mask = A_UNDERLINE;
      break;
   }

   if (*mask != 0)
   {
      from++;
   }
   else
   {
      int digits;
      int pair = 0;

      for (digits = 1; digits <= 3; ++digits)
      {
	 if (!isdigit (CharOf (string[1 + from])))
	    break;
	 pair *= 10;
	 pair += DigitOf (string[++from]);
      }
#ifdef HAVE_START_COLOR
#define MAX_PAIR (int) (A_COLOR / (((unsigned)(~A_COLOR) << 1) & A_COLOR))
      if (pair > MAX_PAIR)
	 pair = MAX_PAIR;
      *mask = (chtype)COLOR_PAIR (pair);
#else
      *mask = A_BOLD;
#endif
   }
   return from;
}

/*
 * The reverse of encodeAttribute()
 * Well almost.  If attributes such as bold and underline are combined in
 * the same string, we do not necessarily reconstruct them in the same order.
 * Also, alignment markers and tabs are lost.
 */
static unsigned decodeAttribute (char *string,
				 unsigned from,
				 chtype oldattr,
				 chtype newattr)
{
   /* *INDENT-OFF* */
   static const struct {
      int	code;
      chtype	mask;
   } table[] = {
      { 'B', A_BOLD },
      { 'D', A_DIM },
      { 'K', A_BLINK },
      { 'R', A_REVERSE },
      { 'S', A_STANDOUT },
      { 'U', A_UNDERLINE },
   };
   /* *INDENT-ON* */

   char temp[80];
   char *result = (string != NULL) ? (string + from) : temp;
   char *base = result;
   chtype tmpattr = oldattr & A_ATTRIBUTES;

   newattr &= A_ATTRIBUTES;
   if (tmpattr != newattr)
   {
      while (tmpattr != newattr)
      {
	 unsigned n;
	 bool found = FALSE;

	 for (n = 0; n < sizeof (table) / sizeof (table[0]); ++n)
	 {
	    if ((table[n].mask & tmpattr) != (table[n].mask & newattr))
	    {
	       found = TRUE;
	       *result++ = L_MARKER;
	       if (table[n].mask & tmpattr)
	       {
		  *result++ = '!';
		  tmpattr &= ~(table[n].mask);
	       }
	       else
	       {
		  *result++ = '/';
		  tmpattr |= (table[n].mask);
	       }
	       *result++ = (char)table[n].code;
	       break;
	    }
	 }
#ifdef HAVE_START_COLOR
	 if ((tmpattr & A_COLOR) != (newattr & A_COLOR))
	 {
	    int oldpair = PAIR_NUMBER (tmpattr);
	    int newpair = PAIR_NUMBER (newattr);
	    if (!found)
	    {
	       found = TRUE;
	       *result++ = L_MARKER;
	    }
	    if (newpair == 0)
	    {
	       *result++ = '!';
	       sprintf (result, "%d", oldpair);
	    }
	    else
	    {
	       *result++ = '/';
	       sprintf (result, "%d", newpair);
	    }
	    result += strlen (result);
	    tmpattr &= ~A_COLOR;
	    newattr &= ~A_COLOR;
	 }
#endif
	 if (found)
	    *result++ = R_MARKER;
	 else
	    break;
      }
   }

   return (from + (unsigned)(result - base));
}

/*
 * This function takes a character string, full of format markers
 * and translates them into a chtype * array. This is better suited
 * to curses, because curses uses chtype almost exclusively
 */
chtype *char2Chtype (const char *string, int *to, int *align)
{
   chtype *result = NULL;
   chtype mask;

   (*to) = 0;
   *align = LEFT;

   if (string != NULL && *string != 0)
   {
      int len = (int)strlen (string);
      int pass;
      int used = 0;
      /*
       * We make two passes because we may have indents and tabs to expand, and
       * do not know in advance how large the result will be.
       */
      for (pass = 0; pass < 2; pass++)
      {
	 chtype attrib;
	 int insideMarker;
	 int from;
	 int adjust;
	 int start;
	 int x = 3;

	 if (pass != 0)
	 {
	    if ((result = typeMallocN (chtype, used + 2)) == NULL)
	    {
	       used = 0;
	       break;
	    }
	 }
	 adjust = 0;
	 attrib = A_NORMAL;
	 start = 0;
	 used = 0;

	 /* Look for an alignment marker.  */
	 if (usingMarkup && (*string == L_MARKER))
	 {
	    if (string[1] == 'C' && string[2] == R_MARKER)
	    {
	       (*align) = CENTER;
	       start = 3;
	    }
	    else if (string[1] == 'R' && string[2] == R_MARKER)
	    {
	       (*align) = RIGHT;
	       start = 3;
	    }
	    else if (string[1] == 'L' && string[2] == R_MARKER)
	    {
	       start = 3;
	    }
	    else if (string[1] == 'B' && string[2] == '=')
	    {
	       /* Set the item index value in the string.       */
	       if (result != NULL)
	       {
		  result[0] = ' ';
		  result[1] = ' ';
		  result[2] = ' ';
	       }

	       /* Pull out the bullet marker.  */
	       while (string[x] != R_MARKER && string[x] != 0)
	       {
		  if (result != NULL)
		     result[x] = (chtype)string[x] | A_BOLD;
		  x++;
	       }
	       adjust = 1;

	       /* Set the alignment variables.  */
	       start = x;
	       used = x;
	    }
	    else if (string[1] == 'I' && string[2] == '=')
	    {
	       from = 2;
	       x = 0;

	       while (string[++from] != R_MARKER && string[from] != 0)
	       {
		  if (isdigit (CharOf (string[from])))
		  {
		     adjust = (adjust * 10) + DigitOf (string[from]);
		     x++;
		  }
	       }

	       start = x + 4;
	    }
	 }

	 while (adjust-- > 0)
	 {
	    if (result != NULL)
	       result[used] = ' ';
	    used++;
	 }

	 /* Set the format marker boolean to false.  */
	 insideMarker = FALSE;

	 /* Start parsing the character string.  */
	 for (from = start; from < len; from++)
	 {
	    /* Are we inside a format marker?  */
	    if (!insideMarker)
	    {
	       if (usingMarkup
		   && string[from] == L_MARKER
		   && (string[from + 1] == '/'
		       || string[from + 1] == '!'
		       || string[from + 1] == '#'))
	       {
		  insideMarker = TRUE;
	       }
	       else if (usingMarkup
			&& string[from] == '\\'
			&& string[from + 1] == L_MARKER)
	       {
		  from++;
		  if (result != NULL)
		     result[used] = CharOf (string[from]) | attrib;
		  used++;
		  from++;
	       }
	       else if (string[from] == '\t')
	       {
		  do
		  {
		     if (result != NULL)
			result[used] = ' ';
		     used++;
		  }
		  while (used & 7);
	       }
	       else
	       {
		  if (result != NULL)
		     result[used] = CharOf (string[from]) | attrib;
		  used++;
	       }
	    }
	    else
	    {
	       switch (string[from])
	       {
	       case R_MARKER:
		  insideMarker = 0;
		  break;
	       case '#':
		  {
		     chtype lastChar = 0;
		     switch (string[from + 2])
		     {
		     case 'L':
			switch (string[from + 1])
			{
			case 'L':
			   lastChar = ACS_LLCORNER;
			   break;
			case 'U':
			   lastChar = ACS_ULCORNER;
			   break;
			case 'H':
			   lastChar = ACS_HLINE;
			   break;
			case 'V':
			   lastChar = ACS_VLINE;
			   break;
			case 'P':
			   lastChar = ACS_PLUS;
			   break;
			}
			break;
		     case 'R':
			switch (string[from + 1])
			{
			case 'L':
			   lastChar = ACS_LRCORNER;
			   break;
			case 'U':
			   lastChar = ACS_URCORNER;
			   break;
			}
			break;
		     case 'T':
			switch (string[from + 1])
			{
			case 'T':
			   lastChar = ACS_TTEE;
			   break;
			case 'R':
			   lastChar = ACS_RTEE;
			   break;
			case 'L':
			   lastChar = ACS_LTEE;
			   break;
			case 'B':
			   lastChar = ACS_BTEE;
			   break;
			}
			break;
		     case 'A':
#if !(defined(__hpux) && !defined(NCURSES_VERSION))
			switch (string[from + 1])
			{
			case 'L':
			   lastChar = ACS_LARROW;
			   break;
			case 'R':
			   lastChar = ACS_RARROW;
			   break;
			case 'U':
			   lastChar = ACS_UARROW;
			   break;
			case 'D':
			   lastChar = ACS_DARROW;
			   break;
			}
#endif
			break;
		     default:
#if 0
			if (isdigit (string[from + 1])
			    && isdigit (string[from + 2]))
			   /* special case to allow control characters to be edited */
			   lastChar = (10 * ((string[from + 1] - '0'))
				       + (string[from + 2] - '0'));
			else
#endif
#if !(defined(__hpux) && !defined(NCURSES_VERSION))
			   if (string[from + 1] == 'D' &&
			       string[from + 2] == 'I')
			   lastChar = ACS_DIAMOND;
			else if (string[from + 1] == 'C' &&
				 string[from + 2] == 'B')
			   lastChar = ACS_CKBOARD;
			else if (string[from + 1] == 'D' &&
				 string[from + 2] == 'G')
			   lastChar = ACS_DEGREE;
			else if (string[from + 1] == 'P' &&
				 string[from + 2] == 'M')
			   lastChar = ACS_PLMINUS;
			else if (string[from + 1] == 'B' &&
				 string[from + 2] == 'U')
			   lastChar = ACS_BULLET;
			else if (string[from + 1] == 'S' &&
				 string[from + 2] == '1')
			   lastChar = ACS_S1;
			else if (string[from + 1] == 'S' &&
				 string[from + 2] == '9')
			   lastChar = ACS_S9;
#endif
			break;
		     }

		     if (lastChar != 0)
		     {
			adjust = 1;
			from += 2;

			if (string[from + 1] == '(')
			   /* Check for a possible numeric modifier.  */
			{
			   from++;
			   adjust = 0;

			   while (string[++from] != ')' && string[from] != 0)
			   {
			      if (isdigit (CharOf (string[from])))
			      {
				 adjust = (adjust * 10) + DigitOf (string[from]);
			      }
			   }
			}
		     }
		     for (x = 0; x < adjust; x++)
		     {
			if (result != NULL)
			   result[used] = lastChar | attrib;
			used++;
		     }
		     break;
		  }
	       case '/':
		  from = encodeAttribute (string, from, &mask);
		  attrib = attrib | mask;
		  break;
	       case '!':
		  from = encodeAttribute (string, from, &mask);
		  attrib = attrib & ~mask;
		  break;
	       }
	    }
	 }

	 if (result != NULL)
	 {
	    result[used] = 0;
	    result[used + 1] = 0;
	 }

	 /*
	  * If there are no characters, put the attribute into the
	  * the first character of the array.
	  */
	 if (used == 0
	     && result != NULL)
	 {
	    result[0] = attrib;
	 }
      }
      *to = used;
   }
   else
   {
      /*
       * Try always to return something; otherwise lists of chtype strings
       * would get a spurious null pointer whenever there is a blank line,
       * and CDKfreeChtypes() would fail to free the whole list.
       */
      result = typeCallocN (chtype, 1);
   }
   return result;
}

/*
 * This determines the length of a chtype string
 */
int chlen (const chtype *string)
{
   int result = 0;

   if (string != NULL)
   {
      while (string[result] != 0)
	 result++;
   }

   return (result);
}

/*
 * Compare a regular string to a chtype string
 */
int cmpStrChstr (const char *str, const chtype *chstr)
{
   int r = 0;

   if (!str && !chstr)
      return 0;
   if (!str)
      return 1;
   if (!chstr)
      return -1;

   while (!r && *str && *chstr)
   {
      r = *str - CharOf (*chstr);
      ++str;
      ++chstr;
   }

   if (r)
      return r;
   else if (!*str)
      return -1;
   else if (!*chstr)
      return 1;
   return 0;
}

void chstrncpy (char *dest, const chtype *src, int maxcount)
{
   int i = 0;

   while (i < maxcount && *src)
      *dest++ = (char)(*src++);

   *dest = '\0';
}

/*
 * This returns a pointer to char * of a chtype *
 * Formatting codes are omitted.
 */
char *chtype2Char (const chtype *string)
{
   char *newstring = NULL;

   if (string != NULL)
   {
      int len = chlen (string);

      if ((newstring = typeMallocN (char, len + 1)) != NULL)
      {
	 int x;

	 for (x = 0; x < len; x++)
	 {
	    newstring[x] = (char)CharOf (string[x]);
	 }
	 newstring[len] = '\0';
      }
   }
   return (newstring);
}

/*
 * This returns a pointer to char * of a chtype *
 * Formatting codes are embedded.
 */
char *chtype2String (const chtype *string)
{
   char *newstring = NULL;

   if (string != NULL)
   {
      int pass;
      int len = chlen (string);

      for (pass = 0; pass < 2; ++pass)
      {
	 int x;
	 unsigned need = 0;

	 for (x = 0; x < len; ++x)
	 {
	    chtype this_ch = string[x];
	    chtype last_ch = (x > 0) ? string[x - 1] : 0;
	    unsigned next = ((this_ch & 0xff) < 32) ? 5 : 1;

	    need = decodeAttribute (newstring, need, last_ch, this_ch);
	    if (newstring != NULL)
	    {
	       if (next == 1)
	       {
		  newstring[need] = (char)this_ch;
	       }
	       else
	       {
		  unsigned ch = (0xff & this_ch);
		  newstring[need] = L_MARKER;
		  newstring[need + 1] = '#';
		  newstring[need + 2] = (char)('0' + (ch / 10));
		  newstring[need + 3] = (char)('0' + (ch % 10));
		  newstring[need + 4] = R_MARKER;

	       }
	    }
	    need += next;
	 }
	 if (pass)
	 {
	    newstring[need] = 0;	/* keep a null on the end */
	 }
	 ++need;
	 if (!pass)
	 {
	    if ((newstring = typeCallocN (char, need)) == NULL)
	         break;
	 }
      }
   }
   return (newstring);
}

static int comparSort (const void *a, const void *b)
{
   return strcmp (*(const char *const *)a, (*(const char *const *)b));
}

void sortList (CDK_CSTRING *list, int length)
{
   if (length > 1)
      qsort (list, (unsigned)length, sizeof (list[0]), comparSort);
}

/*
 * This strips white space from the given string.
 */
void stripWhiteSpace (EStripType stripType, char *string)
{
   /* Declare local variables.  */
   size_t stringLength = 0;

   /* Make sure the string is not null.  */
   if (string != NULL
       && (stringLength = strlen (string)) != 0)
   {
      /* Strip leading whitespace */
      if (stripType == vFRONT || stripType == vBOTH)
      {
	 unsigned alphaChar = 0;
	 unsigned x;

	 /* Find the first non-whitespace character.  */
	 while (string[alphaChar] == ' ' || string[alphaChar] == '\t')
	 {
	    alphaChar++;
	 }

	 for (x = alphaChar; x <= stringLength; ++x)
	    string[x - alphaChar] = string[x];
      }

      /* Strip trailing whitespace */
      if (stripType == vBACK || stripType == vBOTH)
      {
	 stringLength = strlen (string);
	 while (stringLength-- != 0 &&
		(string[stringLength] == ' ' ||
		 string[stringLength] == '\t'))
	 {
	    string[stringLength] = '\0';
	 }
      }
   }
}

static unsigned countChar (const char *string, int separator)
{
   unsigned result = 0;
   int ch;

   while ((ch = *string++) != 0)
   {
      if (ch == separator)
	 result++;
   }
   return result;
}

/*
 * Split a string into a list of strings.
 */
char **CDKsplitString (const char *string, int separator)
{
   char **result = NULL;
   char *temp;

   if (string != NULL && *string != 0)
   {
      unsigned need = countChar (string, separator) + 2;
      if ((result = typeMallocN (char *, need)) != NULL)
      {
	 unsigned item = 0;
	 const char *first = string;
	 for (;;)
	 {
	    while (*string != 0 && *string != separator)
	       string++;

	    need = (unsigned)(string - first);
	    if ((temp = typeMallocN (char, need + 1)) == NULL)
	         break;

	    memcpy (temp, first, need);
	    temp[need] = 0;
	    result[item++] = temp;

	    if (*string++ == 0)
	       break;
	    first = string;
	 }
	 result[item] = NULL;
      }
   }
   return result;
}

/*
 * Add a new string to a list.  Keep a null pointer on the end so we can use
 * CDKfreeStrings() to deallocate the whole list.
 */
unsigned CDKallocStrings (char ***list, char *item, unsigned length, unsigned used)
{
   unsigned need = 1;

   while (need < length + 2)
      need *= 2;
   if (need > used)
   {
      used = need;
      if (*list == NULL)
      {
	 *list = typeMallocN (char *, used);
      }
      else
      {
	 *list = typeReallocN (char *, *list, used);
      }
   }
   (*list)[length++] = copyChar (item);
   (*list)[length] = NULL;
   return used;
}

/*
 * Count the number of items in a list of strings.
 */
unsigned CDKcountStrings (CDK_CSTRING2 list)
{
   unsigned result = 0;
   if (list != NULL)
   {
      while (*list++ != NULL)
	 result++;
   }
   return result;
}

/*
 * Free a list of strings, terminated by a null pointer.
 */
void CDKfreeStrings (char **list)
{
   if (list != NULL)
   {
      void *base = (void *)list;
      while (*list != NULL)
	 free (*list++);
      free (base);
   }
}

/*
 * Free a list of chtype-strings, terminated by a null pointer.
 */
void CDKfreeChtypes (chtype **list)
{
   if (list != NULL)
   {
      void *base = (void *)list;
      while (*list != NULL)
      {
	 freeChtype (*list++);
      }
      free (base);
   }
}

int mode2Filetype (mode_t mode)
{
   /* *INDENT-OFF* */
   static const struct {
      mode_t	mode;
      char	code;
   } table[] = {
#ifdef S_IFBLK
      { S_IFBLK,  'b' },  /* Block device */
#endif
      { S_IFCHR,  'c' },  /* Character device */
      { S_IFDIR,  'd' },  /* Directory */
      { S_IFREG,  '-' },  /* Regular file */
#ifdef S_IFLNK
      { S_IFLNK,  'l' },  /* Socket */
#endif
#ifdef S_IFSOCK
      { S_IFSOCK, '@' },  /* Socket */
#endif
      { S_IFIFO,  '&' },  /* Pipe */
   };
   /* *INDENT-ON* */

   int filetype = '?';
   unsigned n;

   for (n = 0; n < sizeof (table) / sizeof (table[0]); n++)
   {
      if ((mode & S_IFMT) == table[n].mode)
      {
	 filetype = table[n].code;
	 break;
      }
   }

   return filetype;

}

/*
 * This function takes a mode_t type and creates a string represntation
 * of the permission mode.
 */
int mode2Char (char *string, mode_t mode)
{
   /* *INDENT-OFF* */
   static struct {
      mode_t	mask;
      unsigned	col;
      char	flag;
   } table[] = {
      { S_IRUSR,	1,	'r' },
      { S_IWUSR,	2,	'w' },
      { S_IXUSR,	3,	'x' },
#if defined (S_IRGRP) && defined (S_IWGRP) && defined (S_IXGRP)
      { S_IRGRP,	4,	'r' },
      { S_IWGRP,	5,	'w' },
      { S_IXGRP,	6,	'x' },
#endif
#if defined (S_IROTH) && defined (S_IWOTH) && defined (S_IXOTH)
      { S_IROTH,	7,	'r' },
      { S_IWOTH,	8,	'w' },
      { S_IXOTH,	9,	'x' },
#endif
#ifdef S_ISUID
      { S_ISUID,	3,	's' },
#endif
#ifdef S_ISGID
      { S_ISGID,	6,	's' },
#endif
#ifdef S_ISVTX
      { S_ISVTX,	9,	't' },
#endif
   };
   /* *INDENT-ON* */

   int permissions = 0;
   int filetype = mode2Filetype (mode);
   unsigned n;

   /* Clean the string.  */
   cleanChar (string, 11, '-');
   string[11] = '\0';

   if (filetype == '?')
      return -1;

   for (n = 0; n < sizeof (table) / sizeof (table[0]); n++)
   {
      if ((mode & table[n].mask) != 0)
      {
	 string[table[n].col] = table[n].flag;
	 permissions |= (int)table[n].mask;
      }
   }

   /* Check for unusual permissions.  */
#ifdef S_ISUID
   if (((mode & S_IXUSR) == 0) &&
       ((mode & S_IXGRP) == 0) &&
       ((mode & S_IXOTH) == 0) &&
       (mode & S_ISUID) != 0)
   {
      string[3] = 'S';
   }
#endif

   return permissions;
}

/*
 * This returns the length of the integer.
 */
int intlen (int value)
{
   if (value < 0)
      return 1 + intlen (-value);
   else if (value >= 10)
      return 1 + intlen (value / 10);
   return 1;
}

/*
 * This opens the current directory and reads the contents.
 */
int CDKgetDirectoryContents (const char *directory, char ***list)
{
   /* Declare local variables.  */
   struct dirent *dirStruct;
   int counter = 0;
   DIR *dp;
   unsigned used = 0;

   /* Open the directory.  */
   dp = opendir (directory);

   /* Could we open the directory?  */
   if (dp == NULL)
   {
      return -1;
   }

   /* Read the directory.  */
   while ((dirStruct = readdir (dp)) != NULL)
   {
      if (strcmp (dirStruct->d_name, "."))
	 used = CDKallocStrings (list, dirStruct->d_name,
				 (unsigned)counter++, used);
   }

   /* Close the directory.  */
   closedir (dp);

   /* Sort the info.  */
   sortList ((CDK_CSTRING *)*list, counter);

   /* Return the number of files in the directory.  */
   return counter;
}

/*
 * This looks for a subset of a word in the given list.
 */
int searchList (CDK_CSTRING2 list, int listSize, const char *pattern)
{
   int Index = -1;

   /* Make sure the pattern isn't null. */
   if (pattern != NULL)
   {
      size_t len = strlen (pattern);
      int x;

      /* Cycle through the list looking for the word. */
      for (x = 0; x < listSize; x++)
      {
	 /* Do a string compare. */
	 int ret = strncmp (list[x], pattern, len);

	 /*
	  * If 'ret' is less than 0, then the current word is alphabetically
	  * less than the provided word.  At this point we will set the index
	  * to the current position.  If 'ret' is greater than 0, then the
	  * current word is alphabetically greater than the given word.  We
	  * should return with index, which might contain the last best match.
	  * If they are equal, then we've found it.
	  */
	 if (ret < 0)
	 {
	    Index = ret;
	 }
	 else
	 {
	    if (ret == 0)
	       Index = x;
	    break;
	 }
      }
   }
   return Index;
}

/*
 * This function checks to see if a link has been requested.
 */
int checkForLink (const char *line, char *filename)
{
   int len = 0;
   int fPos = 0;

   /* Make sure the line isn't null. */
   if (line == NULL)
   {
      return -1;
   }
   len = (int)strlen (line);

   /* Strip out the filename. */
   if (usingMarkup
       && line[0] == L_MARKER
       && line[1] == 'F'
       && line[2] == '=')
   {
      int x = 3;

      /* Strip out the filename.  */
      while (x < len)
      {
	 if (line[x] == R_MARKER)
	 {
	    break;
	 }
	 if (fPos < CDK_PATHMAX)
	    filename[fPos++] = line[x];
	 ++x;
      }
   }
   filename[fPos] = '\0';
   return (fPos != 0);
}

/*
 * Returns the filename portion of the given pathname, i.e., after the last
 * slash.
 */
char *baseName (char *pathname)
{
   char *base = NULL;

   if (pathname != NULL
       && *pathname != '\0'
       && (base = copyChar (pathname)) != NULL)
   {
      size_t pathLen;

      if ((pathLen = strlen (pathname)) != 0)
      {
	 size_t x;

	 for (x = pathLen - 1; x != 0; --x)
	 {
	    /* Find the last '/' in the pathname. */
	    if (pathname[x] == '/')
	    {
	       strcpy (base, pathname + x + 1);
	       break;
	    }
	 }
      }
   }
   return base;
}

/*
 * Returns the directory for the given pathname, i.e., the part before the
 * last slash.
 */
char *dirName (char *pathname)
{
   char *dir = NULL;
   size_t pathLen;

   /* Check if the string is null.  */
   if (pathname != NULL
       && (dir = copyChar (pathname)) != NULL
       && (pathLen = strlen (pathname)) != 0)
   {
      size_t x = pathLen;
      while ((dir[x] != '/') && (x > 0))
      {
	 dir[x--] = '\0';
      }
   }

   return dir;
}

/*
 * If the dimension is a negative value, the dimension will be the full
 * height/width of the parent window - the value of the dimension.  Otherwise,
 * the dimension will be the given value.
 */
int setWidgetDimension (int parentDim, int proposedDim, int adjustment)
{
   int dimension = 0;

   /* If the user passed in FULL, return the parent's size. */
   if ((proposedDim == FULL) || (proposedDim == 0))
   {
      dimension = parentDim;
   }
   else
   {
      /* If they gave a positive value, return it. */
      if (proposedDim >= 0)
      {
	 if (proposedDim >= parentDim)
	    dimension = parentDim;
	 else
	    dimension = (proposedDim + adjustment);
      }
      else
      {
	 /*
	  * If they gave a negative value, then return the
	  * dimension of the parent plus the value given.
	  */
	 dimension = parentDim + proposedDim;

	 /* Just to make sure. */
	 if (dimension < 0)
	    dimension = parentDim;
      }
   }
   return dimension;
}

void enableCursesMarkup (boolean flag)
{
   usingMarkup = flag;
}

/*
 * This safely erases a given window.
 */
void eraseCursesWindow (WINDOW *window)
{
   if (window != NULL)
   {
      werase (window);
      wrefresh (window);
   }
}

/*
 * This safely deletes a given window.
 */
void deleteCursesWindow (WINDOW *window)
{
   if (window != NULL)
   {
      eraseCursesWindow (window);
      delwin (window);
   }
}

/*
 * This moves a given window (if we're able to set the window's beginning).
 * We prefer to not use mvwin(), because SVr4 curses does not move subwindows.
 */
void moveCursesWindow (WINDOW *window, int xdiff, int ydiff)
{
   if (window != NULL)
   {
      int xpos, ypos;

      getbegyx (window, ypos, xpos);
#if defined(setbegyx)
      (void)setbegyx (window, (short)ypos, (short)xpos);
      xpos += xdiff;
      ypos += ydiff;
      werase (window);
      (void)setbegyx (window, (short)ypos, (short)xpos);
#elif defined(HAVE_MVWIN)
      (void)mvwin (window, (short)(ypos + ydiff), (short)(xpos + xdiff));
#endif
      (void)ypos;
      (void)xpos;
   }
}

/*
 * Return an integer like 'floor()', which returns a double.
 */
int floorCDK (double value)
{
   int result = (int)value;
   if (result > value)		/* e.g., value < 0.0 and value is not an integer */
      result--;
   return result;
}

/*
 * Return an integer like 'ceil()', which returns a double.
 */
int ceilCDK (double value)
{
   return -floorCDK (-value);
}

/*
 * Compatibility for different versions of curses.
 */
#if !(defined(HAVE_GETBEGX) && defined(HAVE_GETBEGY))
int getbegx (WINDOW *win)
{
   int y, x;
   getbegyx (win, y, x);
   return x;
}
int getbegy (WINDOW *win)
{
   int y, x;
   getbegyx (win, y, x);
   return y;
}
#endif

#if !(defined(HAVE_GETMAXX) && defined(HAVE_GETMAXY))
int getmaxx (WINDOW *win)
{
   int y, x;
   getmaxyx (win, y, x);
   return x;
}
int getmaxy (WINDOW *win)
{
   int y, x;
   getmaxyx (win, y, x);
   return y;
}
#endif