File: chinese.c

package info (click to toggle)
chdrv 1.0.13-0.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,204 kB
  • ctags: 1,069
  • sloc: ansic: 8,972; perl: 412; asm: 234; makefile: 195; sh: 36
file content (1602 lines) | stat: -rw-r--r-- 31,966 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
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
/* Copyright(C) 1993
   The following program is used to display Chinese character in screen
   console without X Window System in Linux.  The program is protected
   under GNU copyleft.  The detailed is as GNU's COPYING which I have
   included in this package.  You can distribute this program freely, but
   YOU MUST DISTRIBUTE THE COMPLETE CODE IN THE PACKAGE.  If you have
   modified the package, you must note it in README file and tell others
   how to get the original version.
*/

/*
   $Id: chinese.c,v 1.1 1994/05/08 02:43:36 root Exp root $
*/

/********************************************************************/
/*     main module                                                  */
/*        Builded date : 1993/08/09                                 */
/*     Modified                                                     */
/*        1993/08/10                                                */
/*             (1) add function ch_cursoron,ch_cursoroff            */
/*             (2) add function ch_cursor_blink                     */
/*        1993/08/14                                                */
/*	       (1) modified ch_cursoron and ch_cursoroff. Now they  */
/*                 are used to turn on/off cursor.                  */
/*        1993/08/28                                                */
/*             (1) Change to accomodate the application of daemon   */
/*        1993/09/03                                                */
/*             (1) support virtual console.  This is version 0.2    */
/********************************************************************/
#include "portable.h"
#include "color.h"
#include "vga.h"
#include "chinese.h"
#include "chmsg.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <ctype.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include <sys/time.h>
#include <linux/vt.h>
#include <linux/kd.h>
#include <asm/param.h>
static void newline();
static void advance();
void ch_goto_systemarea(int);
void ch_cursor();
static void showch_attr(char,int);
#define max(x,y) (((x)>(y))? x:y)
#define UNDERLINE 1
#define INVERT    2
#define COUPLE_LEFT    0x80
#define COUPLE_RIGHT   0x40
#define QUOTE_CHAR     0x20
/* default color scheme */
unsigned char COLOR_DEFAULT_FG = 7;
unsigned char COLOR_DEFAULT_BG = 0;
unsigned char COLOR_SYSTEM_FG = 7; /*15 is white*/
unsigned char COLOR_SYSTEM_BG = 1; /* 17 is blue, 10 is hi-green, 34 is green */
/* the below is no effect now */
int CURSOR_SL = 13;
int CURSOR_EL = 15;
int low_color_table[8],hi_color_table[8];
static int *color_table;


static VSC vscreen[CONS_NUM];
VSC *nscr;
int write_to_screen = 1;
static int activeconsole;
static void ch_redraw_part();
static void ch_attr(int);

#define curx           (nscr->curx)
#define cury           (nscr->cury)
#define oldx           (nscr->oldx)
#define oldy           (nscr->oldy)
#define dimx           (nscr->dimx)
#define dimy           (nscr->dimy)
#define cursor_x       (nscr->cursor_x)
#define cursor_y       (nscr->cursor_y)
#define cursor_blinked (nscr->cursor_blinked)
#define no_cursor       (nscr->no_cursor)
#define cursor_rate    (nscr->cursor_rate)
#define scr_text        (nscr->scrtext)
#define scr_attr        (nscr->scrattr)
#define cursor_on      (nscr->cursor_on)
#define c_stack        (nscr->c_stack)
#define codesys        (nscr->codesys)
#define und            (nscr->und)
#define inv            (nscr->inv)
#define pos            (nscr->pos)
#define sys_attr       (nscr->sys_attr)
#define mode           (nscr->screenmode)
#define autoscroll     (nscr->isscrolled)
#define topl           (nscr->top)
#define bottoml        (nscr->bottom)
#define fontwidth      (nscr->fontwidth)
#define fontheight     (nscr->fontheight)
#define automargin     (nscr->automargin)
#define quick_scroll   (nscr->quick_scroll)
#define line_out       (nscr->line_out)
#define wordalign      (nscr->wordalign)
#define curfg          (nscr->curfg)
#define curbg          (nscr->curbg)
#define quote_char     (nscr->quote_char)
#define sys_pos        (nscr->sys_pos)
#define sys_highbyte   (nscr->sys_highbyte)
#define dirty          (nscr->dirty)
#define alternative    (nscr->alternative)
#define need_wrap      (nscr->need_wrap)
static int chconsole;
static void showcc(CHAR);
void ch_drawctext(int,int,WORD);
void ch_drawtext(int,int,CHAR);

#define IS_WRITE_TO_CURRENT_CONSOLE (nscr == (&vscreen[chconsole]))


/* switch to other screen,but no redraw in this place */
void _ch_swap_console(int newconsole)
{
  int i,j;
  static void cursor();
  static VSC *swapscr = NULL;

  
  if (newconsole == -1)
    {
      if (nscr != swapscr)
	{
	  line_out = quick_scroll = 0;
	  nscr = swapscr;
	  ch_have_cursor(1);
	}
    }

  if (newconsole < 0 || newconsole >= CONS_NUM)
    return;
  swapscr = nscr;
  nscr = &(vscreen[newconsole]);
  if (nscr != swapscr)
    ch_have_cursor(0);
  if (scr_text == NULL)
    {
      scr_text = (char *) malloc(dimx*(dimy+1));
      scr_attr = (int *) malloc(sizeof(int)*dimx*(dimy+1));
      if (scr_attr == NULL)
	SYSERR("out of memory");
      for(i=0;i<dimx*(dimy+1);i++)
	{
	  scr_text[i] = ' ';
	  scr_attr[i] = sys_attr;
	}
    }
}
int ch_codesystem()
{
  return codesys;
}
void ch_change_console(int newconsole)
{
  int i,j,lastmode;
  static void cursor();

  if (newconsole < 0 || newconsole >= CONS_NUM)
    return;

  con_change_console(newconsole);
  nscr = &(vscreen[newconsole]);
    
  if (scr_text == NULL)
    {
      scr_text = (char *) malloc(dimx*(dimy+1));
      scr_attr = (int *) malloc(sizeof(int)*dimx*(dimy+1));
      if (scr_attr == NULL)
	SYSERR("out of memory");
      for(i=0;i<dimx*(dimy+1);i++)
	{
	  scr_text[i] = ' ';
	  scr_attr[i] = sys_attr;
	}
    }
  chconsole = newconsole;
  ch_change_input(newconsole);
  if (write_to_screen > 0)
    {
      ch_redraw();
      /*demo_output();*/
      ch_drawsystem();
    }
}

void ch_reset_console(int c)
{
  VSC *nscr = vscreen+c;

  bottoml = dimy-1;
  topl = 0;
}

static void init_virtual_console()
{
  int i;
  ENTER;
  i = 0;

  low_color_table[0] = 0;  /* black */
  low_color_table[1] = 4;  /* red   */
  low_color_table[2] = 2;  /* green */
  low_color_table[3] = 6;  /* yellow*/
  low_color_table[4] = 1;  /* blue  */
  low_color_table[5] = 5;  /* purple*/
  low_color_table[6] = 3;  /* cyan  */
  low_color_table[7] = 7;  /* white */

  hi_color_table[0] = 0x38;  /* black */
  hi_color_table[1] = 0x3c;  /* red   */
  hi_color_table[2] = 0x3a;  /* green */
  hi_color_table[3] = 0x3e;  /* yellow*/
  hi_color_table[4] = 0x39;   /* blue  */
  hi_color_table[5] = 0x3d;  /* purple*/
  hi_color_table[6] = 0x3b;  /* cyan  */
  hi_color_table[7] = 0x3f;  /* white */
  color_table = low_color_table;
  COLOR_DEFAULT_FG = color_table[sys_config.forecolor];
  COLOR_DEFAULT_BG = color_table[sys_config.backcolor];
  COLOR_SYSTEM_FG = color_table[sys_config.systemcolor];
  COLOR_SYSTEM_BG = color_table[sys_config.systemback];
  mon_setcolor(COLOR_DEFAULT_FG+COLOR_DEFAULT_BG*256);

  for(nscr = &(vscreen[0]);i < CONS_NUM;i++,nscr = &(vscreen[i]))
    {
      curx = cury = 0;
      scr_text = NULL;
      scr_attr = NULL;
      dimx = 80;
      dimy = 29;
      cursor_x = cursor_y = 0;
      cursor_blinked = 1;
      no_cursor = 0;
      cursor_on = 0;
      cursor_rate = 1;
      codesys = CODE_BIG5;
      sys_attr = (COLOR_DEFAULT_BG<<24)|(COLOR_DEFAULT_FG<<16);
      mode = TEXT;
      autoscroll = 1;
      topl = 0;
      bottoml = dimy-1;
      oldx = -1;
      fontwidth = 8;
      fontheight = 16;
      automargin = 0;   // I can't make sure this's not a bug
      quick_scroll = 0;
      line_out = 0;
      wordalign = 1;
      curfg = sys_config.forecolor;
      curbg = sys_config.backcolor;
      quote_char = 0;
      sys_pos = dimy*80;
      sys_highbyte=0;
      dirty[0] = dirty[1] = 0;
      alternative = 0;
      need_wrap = 0;
    }
  ch_change_console(0);

  LEAVE;
}

void adm_init()
{
  load_sys_font_table();
  init_virtual_console();
  con_init();
  input_init();
  mon_setcolor(COLOR_DEFAULT_FG+(COLOR_DEFAULT_BG<<8));
  SYSERR2("COLOR %d %d",COLOR_DEFAULT_FG,COLOR_DEFAULT_BG);

}


demo_output()
{
  int i;
  char buf[100];

  mon_setcolor(COLOR_SYSTEM_BG*256+COLOR_SYSTEM_FG);
  for(i=0;i<1000;i++) {
    /*ch_goto_systemarea(0);*/
    buf[0] = 0xaa;
    buf[1] = (i%40)+0x40;
    buf[2] = 0;
    ch_system_showstr("�ܽd ");
    ch_system_showstr(buf);
  }
  while(1);
}

	
ch_drawsystem()
{
  int ooldx = ch_wherex(),
      ooldy = ch_wherey();
  int i,oldw,color;
  int is_no_cursor;
  
  ENTER;
  is_no_cursor = no_cursor;
  i = 0;
  color = mon_curcolor();
  mon_setcolor(COLOR_SYSTEM_BG*256+COLOR_SYSTEM_FG);
  
  ch_goto_systemarea(0);

  for(i=0;i<dimx-1;i++)
    ch_system_showch(' ');
    
  ch_goto_systemarea(0);

  ch_system_showstr("[����");

  ch_system_showch('0'+chconsole);
  ch_system_showch(']');
  ch_goto_systemarea(65);

  ch_system_showstr("�@��:������");
  ch_gotoxy(ooldx,ooldy);
  ch_input_redraw();
  SYSERR1("%d\n",dimy);
  if (dimy != MAX_DIMY)
    {
      ENTER;
      mon_setcolor(COLOR_SYSTEM_FG|(COLOR_SYSTEM_BG<<8));
      for(i=0;i<5;i++)
	mon_drawline(0,dimy*fontheight+i,dimx*8-1,dimy*fontheight+i);
    }
  mon_setcolor(color);
  LEAVE;
}

void ch_clear()
{
  int i,j;

  ch_cursor_blink(0);
  if (write_to_screen>0)
  	mon_clearblock(0,0,dimx-1,dimy*fontheight);
  ch_gotoxy(0,0);
  for(i=0;i<dimx;i++)
    for(j=0;j<dimy;j++)
      {
	scr_text[j*dimx+i] = ' ';
	scr_attr[j*dimx+i] = sys_attr;
      }
  ch_cursor_blink(1);
}

void ch_clear_console(int con)
{
  int i,j;
  VSC *nscr;

  if (con < 0 || con >= CONS_NUM)
    return;
  nscr = &vscreen[con];
  for(i=0;i<dimx;i++)
    for(j=0;j<dimy;j++)
      {
	scr_text[j*dimx+i] = ' ';
	scr_attr[j*dimx+i] = sys_attr;
      }
}

void ch_clreol()
{
  int i;

  if (write_to_screen>0)
    {
      ch_cursor_blink(0);
      mon_clearblock(curx,cury*fontheight,dimx-1,(cury+1)*fontheight);
      ch_cursor_blink(1);
    }
  for(i=curx;i<dimx-1;i++)
    {
      scr_text[cury*dimx+i] = ' ';
      scr_attr[cury*dimx+i] = sys_attr;
    }
}
void ch_system_clreol()
{
  int color,i;
  int oldw = autoscroll;
  if (write_to_screen>0)
    {
      autoscroll = 0;
      color = mon_curcolor();
      mon_setcolor(COLOR_SYSTEM_FG+COLOR_SYSTEM_BG*256);
      ch_cursor_blink(0);
      for(i=sys_pos;i<dimx*(dimy+1);i++)
	ch_system_showch(' ');
      autoscroll = oldw;  
      ch_cursor_blink(1);
      mon_setcolor(color);
    }
  for(i=sys_pos;i<dimx*(dimy+1);i++)
    {
      scr_text[i] = ' ';
      scr_attr[i] = sys_attr;
    }

}
  
void ch_cursor()
{
  if (cursor_on)
    {
      /* erase the last cursor if it be change place */
      if (curx != cursor_x || cury != cursor_y)
	{
	  if (IS_WRITE_TO_CURRENT_CONSOLE)
	    if (write_to_screen>0)
	      mon_cursor(cursor_x,cursor_y*fontheight);
	  cursor_on = 0;
	  cursor_x = curx;
	  cursor_y = cury;
	}
    }
  else
    {
      cursor_x = curx;
      cursor_y = cury;
    }
  
  if (IS_WRITE_TO_CURRENT_CONSOLE)
    if (write_to_screen>0)
      mon_cursor(curx,cury*fontheight);
  cursor_on = 1 - cursor_on;
}

void ch_gotoxy(int x,int y)
{
  ch_cursor_blink(0);
  if (y > bottoml)
	y = bottoml;
  else if (y<topl)
	y = topl;
	
  if ((x >= 0) && (x < dimx) && (y >= 0) && (y < dimy))
    {
      if ((cury == MAX_DIMY) && (y != MAX_DIMY))
      {
	curx = x;
	cury = y;
      }
      else
      {
	curx = x;
	cury = y;
      }
      
      c_stack = 0;
      pos = cury*dimx+curx;
    }
  ch_cursor_blink(1);
  need_wrap = 0;
}

static void showcc(CHAR c)
{
  int i;
  struct timeval timeout;

  ch_cursor_blink(0);  

  if (need_wrap)
    {
      newline();
      need_wrap = 0;
    }
  if (quote_char||alternative)
    {
      scr_text[pos] = c;
      scr_attr[pos] = sys_attr | QUOTE_CHAR;
      if (IS_WRITE_TO_CURRENT_CONSOLE)
	if ((write_to_screen>0)&&!quick_scroll)
	  ch_drawtext(curx,cury,scr_text[pos]);
      advance();
      quote_char = 0;
      ch_cursor_blink(1);
      return;
    }

  if (line_out)
    {
      if (cury >= 32)
	{
	  dirty[1] |= (1<(cury-32));
	}
      else
	{
	  dirty[0] |= (1<<cury);
	}
    }

  switch(c)
    {
    case 0:
      break;
    case 7:
      {
	ioctl(ttyfd,KDMKTONE,0x637|(100<<16));
      }
      break;
    case '\n':
      newline();
      break;
    case '\r':  
      ch_gotoxy(0,cury);
      pos = cury*dimx+curx;
      break;
    case '\b':
      ch_gotoxy(max(0,curx-1),cury);
      if (ch_needadjust(-1))
	input_insert_any(-1);
      break;
    case '\t':
      ch_gotoxy((curx / 8 +1)*8-1,cury);
      if (ch_needadjust(-1))
	input_insert_any(-1);
      pos = cury*dimx+curx;
      advance();
      break;
    default:
      scr_text[pos] = c;
      scr_attr[pos] = sys_attr&(~(COUPLE_LEFT|COUPLE_RIGHT));

      if (IS_WRITE_TO_CURRENT_CONSOLE)
	if ((write_to_screen>0)&&!quick_scroll)
	  {
	    ch_attr(sys_attr);
	    ch_drawtext(curx,cury,scr_text[pos]);
	  }
      advance();
    }
  ch_cursor_blink(1);
}
int _ch_redrawch(int x,int y)
{
  int pp;
  WORD code;
  int color,oldattr,ncolor;
  
  if (write_to_screen<=0)
      return;
  pp = y*dimx+x;
  
  oldattr = sys_attr;
  ch_attr(scr_attr[pp]);

  color = mon_curcolor();
  ncolor = scr_attr[pp]>>16;
  if (color != ncolor)
    mon_setcolor(ncolor);


  code = tran_code(codesys,scr_text[pp],scr_text[pp+1]);

  if ((!(scr_attr[pp]&QUOTE_CHAR)) &&(code != 0xffff))
  {
    scr_attr[pp] |= COUPLE_LEFT;
    scr_attr[pp+1] |= COUPLE_RIGHT;
    ch_drawctext(x,y,code);
    sys_attr = oldattr;
    return 1;
  }
  else
  {
    scr_attr[pp] &= ~(COUPLE_LEFT|COUPLE_RIGHT);
    ch_drawtext(x,y,scr_text[pp]);
    sys_attr = oldattr;
    return 0;
  }
}

void adjust_the_whole_line()
{
  int i,pp;
  WORD code;

  pp = cury*dimx;
  for(i=0;i<curx;i++)
    {
      code = tran_code(codesys,scr_text[pp],scr_text[pp+1]);
      if (code != 0xffff)
	{
	  if ((!(scr_attr[pp]&COUPLE_LEFT))||
	      (!(scr_attr[pp+1]&COUPLE_RIGHT)))
	    {
	      scr_attr[pp] |= COUPLE_LEFT;
	      scr_attr[pp+1] |= COUPLE_RIGHT;
	      if (IS_WRITE_TO_CURRENT_CONSOLE)
		ch_drawctext(i,cury,code);
	    }
	  i++;pp++;
	}
      else
	{
	  if (((scr_attr[pp]&COUPLE_LEFT))||
	      ((scr_attr[pp+1]&COUPLE_RIGHT)))
	    {	  
	      scr_attr[pp] &= ~COUPLE_LEFT;
	      scr_attr[pp+1] &= ~COUPLE_RIGHT;
	      if (IS_WRITE_TO_CURRENT_CONSOLE)
		{
		  ch_drawtext(i,cury,scr_text[pp]);
		  ch_drawtext(i+1,cury,scr_text[pp+1]);
		}
	    }
	}
      pp++;
    }
  if (!(scr_attr[pp]&(COUPLE_RIGHT|COUPLE_LEFT)))
    {
      if (IS_WRITE_TO_CURRENT_CONSOLE)
	if (write_to_screen>0)
	  ch_drawtext(i,cury,scr_text[pp]);
    }
}

/*
   It's a hard matter to determine whether a character in a screen is
   a ASCII code or a Chinese codes.Especially for the BIG5.The alogrithm
   we used is

   (1) If a character can combine with the character before it,we thank
       it is the second byte of Chinese code only when the character before
       it is not a second byte of another Chinese code.

   (2) If a character can combine with the character after it and it can't
       combine with the character before it,we think it is a first byte of
       Chinese code.

   (3) If a character can combine with the character after it and the character
       before it,we rescan the whole line.

   (4) If a character can fit the above rules ,but it can be a first character,
       we delay it output for speed of display.
    
   (5) We should avoid display twice when we add first byte and second byte 
       of Chinese code in any sequence.We spilt it into two cases,

       (i) add first byte and then add second byte
           In this case,the first byte will be delay to display and may not
	   be displayed forever.
	   
       (ii) add second byte and then add first byte
           In this case,the second byte will be display first and Chinese
	   character will display when the first byte is add.

*/
	
  

void ch_showch(CHAR c)
{
  WORD code,code1;


  if (quote_char||alternative)
    {
      showcc(c);
      return;
    }
  else if (c == 255)
    {
      quote_char = 1;
      return;
    }
/*  
  if (scr_attr[pos]&COUPLE_LEFT)
    scr_attr[pos+1] &= ~(COUPLE_RIGHT);
  else if (scr_attr[pos]&COUPLE_RIGHT)
    scr_attr[pos-1] &= ~(COUPLE_LEFT);
*/
  
  if ((curx != 0)&&(scr_attr[pos-1]&COUPLE_LEFT))
    {
      /* 
	 the left char is a left char,so we test if we can couple
	 with the left                 
	 */
      code = tran_code(codesys,scr_text[pos-1],c);
      if (code != 0xffff)
	{
	  scr_text[pos] = c;
	  scr_attr[pos] = sys_attr;

	  scr_attr[pos] |= COUPLE_RIGHT;
	  scr_attr[pos-1] |= COUPLE_LEFT;
	  if (IS_WRITE_TO_CURRENT_CONSOLE)
	    if ((write_to_screen>0) && !quick_scroll)
	      {
		ch_cursor_blink(0);
		ch_drawctext(curx-1,cury,code);
		ch_cursor_blink(1);
	      }
	  advance();
	  return;
	}
    }
  /* the char can't couple with its left char */
  /* In this time test if can couple with the right */
  if (!IS_FIRST_CHAR(c))
    {
      showcc(c);
    }
  else
    {
      scr_text[pos] = c;
      scr_attr[pos] = sys_attr|COUPLE_LEFT;
      advance();
    }
}
	  
void ch_showstr(CHAR *s)
{
  while(*s)
    {
      ch_showch(*s++);
    }
}
	  
int ch_wherex()
{
  return curx;
}
int ch_wherey()
{
  return cury;
}

void ch_setdim(int x,int y)
{
  dimx = x;
  dimy = y;
}
int ch_DimX()
{
  return dimx;
}
int ch_DimY()
{
  return dimy;
}

static void advance()
{
  ch_cursor_blink(0);
  curx++;pos++;
  if (curx >= dimx)
  {
    if (!automargin)
    {
      pos--;
      curx = dimx-1;
    }
    else
    {
      cury++;
      curx = 0;
      if (cury > bottoml)
      {
	if ( autoscroll)
	    need_wrap = 1;
	else
	    cury--;
	cury = bottoml;
	pos = cury*dimx+curx;
      }
    }
  }
  ch_cursor_blink(1);
}

static void newline()
{
  ch_cursor_blink(0);
  cury++;
  if (cury > bottoml)
    {
      if (autoscroll)
      {
	line_out++;
	quick_scroll++;
	ch_scroll(topl,bottoml,1);
	if( line_out >= (bottoml-topl) ) 
	{
	  ch_refresh();
	  SYSERR("REFRESH");
	}
      }
      cury = bottoml;
    }

  pos = cury*dimx+curx;
  ch_cursor_blink(1);
}

void ch_scroll(int ul,int dl,int l)
{
  int i,j;
  int attr;

  if (ul < 0 || dl >= dimy)
    return;

  dirty[0]>>=1;
  dirty[0]|=(dirty[1]&1)>>31;
  dirty[1]>>1;

  if ((write_to_screen>0) && !line_out)
    {
      SYSERR("SCROLL");
      ch_cursor_blink(0);
      mon_scroll(ul*fontheight,(dl+1)*fontheight-1,l*fontheight);
      ch_cursor_blink(1);
    }
    

  for(j=ul;j<=dl-1;j++)
    {
      memcpy(scr_text+j*dimx,scr_text+(j+1)*dimx,dimx);
      memcpy(scr_attr+j*dimx,scr_attr+(j+1)*dimx,dimx*sizeof(int));
    }
  memset(scr_text+j*dimx,' ',dimx);
  attr = (low_color_table[sys_config.forecolor]+
         256*low_color_table[sys_config.backcolor])<<16;
  for(i=0;i<dimx;i++)
    scr_attr[j*dimx+i] = attr;

}

void ch_rscroll(int ul,int dl,int l)
{
  int i,j;
 
  ch_cursor_blink(0);
  if (write_to_screen>0)
    mon_rscroll(ul*fontheight,(dl+1)*fontheight-1,l*fontheight);
  ch_cursor_blink(1);
  for(j=dl-1;j>ul;j--)
    {
      memcpy(scr_text+j*dimx,scr_text+(j-1)*dimx,dimx);
      memcpy(scr_attr+j*dimx,scr_attr+(j-1)*dimx,dimx);
    }
  memset(scr_text+dl*dimx,' ',dimx);
  memset(scr_attr+dl*dimx,sys_attr,dimx);
}

/*
int ch_getch()
{
  return vga_getch();
}
*/
void adm_end()
{
  if (write_to_screen>0)
    mon_textmode(TEXT);
}
  
void ch_cursoron()
{
  if (!cursor_on)
    ch_cursor();
  ch_cursor_blink(1);
}

void ch_cursoroff()
{
  if (cursor_on)
    ch_cursor();
  ch_cursor_blink(0);
}

/*
   The function of 
        ch_cursor_blinked : control the cursor is blinked or not
	ch_have_cursor    : control the cursor should display in screen
	ch_cursor_rate    : control the blink rate of cursor
*/
static void cursor(int signo)
{ 
  if (cursor_blinked <=0)
    {
      return;
    }
  ch_cursor();
  signal(SIGALRM,( void (*)()) cursor);
  alarm(cursor_rate);
} 


void ch_cursor_blink(int i)
{
  cursor_blinked += ((i == 0)? -1 : 1);
  if (no_cursor)
    {
      return;
    }
  
  if (cursor_blinked == 0 && (i == 0))
    {
      if (cursor_on)
	ch_cursor();
      signal(SIGALRM,SIG_IGN);
    }
  else if (cursor_blinked == 1 && (i == 1))
    {
      if (!cursor_on)
	ch_cursor();
      signal(SIGALRM,cursor);
      alarm(cursor_rate);
    }
}


void ch_cursor_rate(int rr)
{
  cursor_rate = rr;
  if (rr == 0)
    ch_cursor_blink(0);
      
}
void ch_have_cursor(int st)
{
  if (st == 0)
    {
      no_cursor = 1;
      alarm(0);
      if (cursor_on)
	ch_cursor();
    }
  else
    {
      no_cursor = 0;
      ch_cursor();
    }
}


void ch_underline(int i)
{

  if (i==1 && !und)
    {
      sys_attr |= UNDERLINE;
      und = 1;
      mon_underline(1);
    }
  else if ((i==0)&&und)
    {
      sys_attr &= ~UNDERLINE;
      und = 0;
      mon_underline(0);
    }

}
void ch_invert(int i)
{
  if (i==1)
    {
      sys_attr |= INVERT;
      inv = 1;
      mon_invert(1);
    }
  else 
    {
      sys_attr &= (0xffff0000|~INVERT);
      inv = 0;
      mon_invert(0);
    }
}

void ch_deletechar()
{
  int i;

  if (write_to_screen>0) {
    ch_cursor_blink(0);
    mon_moveleft(curx,dimx-1,cury*fontheight,1);
    ch_cursor_blink(1);
  }
  for(i=pos;i<pos+dimx-curx-1;i++)
    {
      scr_text[i] = scr_text[i+1];
      scr_attr[i] = scr_attr[i+1];
    }
  scr_text[i] = ' ';
  scr_attr[i] = sys_attr;
}

void ch_insertchar()
{
  int i;
  for(i=pos+dimx-curx-1;i>pos;i--)
    {
      scr_text[i] = scr_text[i-1];
      scr_attr[i] = scr_attr[i-1];
    }
  scr_text[pos] = ' ';
  scr_attr[pos] = sys_attr;
  if (write_to_screen>0) {
    ch_cursor_blink(0);
    mon_moveright(curx,dimx-1,cury*fontheight,1);
    ch_cursor_blink(1);
  }
  /*
  for(i=curx;i<dimx;i++)
    showch_attr(scr_text[pos+i-curx],scr_attr[pos+i-curx]);
    */
}

void ch_clearblock(int lx,int ty,int rx,int by)
{
  int i,j,pp;
  ch_cursor_blink(0);
  if( write_to_screen>0 )
    mon_clearblock(lx,ty*fontheight,rx,(by+1)*fontheight);
  ch_cursor_blink(1);
  for(j=ty;j<=by;j++)
    {
      pp = j*dimx+lx;
      for(i=lx;i<=rx;i++)
	{
	  scr_text[pp] = ' ';
	  scr_attr[pp++] = sys_attr;
	}
    }
}

void ch_insertline()
{
  ch_rscroll(cury,bottoml,1);
}

void ch_deleteline()
{
  SYSERR2("DELETELINE(%d,%d)\n",cury,bottoml-1);
  ch_scroll(cury,bottoml,1);
}

void ch_goto_systemarea(int loc)
{
  sys_pos = dimy*dimx+loc;
}

void ch_restore_cursor()
{
}

void ch_showBIG5(WORD c)
{
  int color = mon_curcolor();
  if (write_to_screen>0)
      mon_setcolor(COLOR_SYSTEM_FG+COLOR_SYSTEM_BG*256);
  c = tran_code(CODE_BIG5,c/256,c%256);
  if (c != 0xffff)
    if (IS_WRITE_TO_CURRENT_CONSOLE)
      if (write_to_screen>0)
	{
	  ch_cursor_blink(0);
	  ch_drawctext(sys_pos%dimx,sys_pos/dimx,c);
	  ch_cursor_blink(1);
	}
  if (IS_WRITE_TO_CURRENT_CONSOLE)
    if (write_to_screen>0)
      mon_setcolor(color);
}

static char *ttys = {"/dev/tty0"};

void ch_textmode(int con)
{
  int fd;
  if (con <0 || con >= CONS_NUM)
    return;
#if 0
  if (con == chconsole && (vscreen[con].screenmode != TEXT) )
#endif
    mon_textmode();
  vscreen[con].screenmode = TEXT;
}

void ch_graphicmode(int con)
{

  if ((con < 0) || (con >= CONS_NUM))
    return;
  ENTER;
  mon_grmode();
  vscreen[con].screenmode = G640x480x16;
  ch_drawsystem();
  ch_redraw();
  LEAVE;
}
static void ch_attr(int attr)
{
  if (attr&UNDERLINE)
    ch_underline(1);
  else
    ch_underline(0);
  if (attr&INVERT)
    ch_invert(1);
  else
    ch_invert(0);
}
static void showch_attr(char c,int attr)
{
  ch_attr(attr);  
  ch_showch(c);
}

void ch_redraw()
{
  if (write_to_screen)
    mon_clearblock(0,0,dimx-1,dimy*fontheight-1);
  ch_redraw_part();
}


static void ch_redraw_part()
{
  int i,j,color;
  int ooldx = ch_wherex();
  int ooldy = ch_wherey();
  int scrollmode,curcorrate;
  int oldinv = inv;
  int oldund = und;
  
  ENTER;
  scrollmode = autoscroll;
  autoscroll = 0;
  quick_scroll = 0;
  ch_cursor_blink(0);
  ch_gotoxy(0,0);
  c_stack = 0;
  cursor_on = 0;
  
  color = mon_curcolor();
  for(j=0;j<=bottoml;j++)
  {
    if (line_out!=0)
      {
	if (j<32)
	  {
	    if (!(dirty[0]&(1<<j)))
	      continue;
	  }
	else
	  {
	    if (!(dirty[1]&(1<<(j-32))))
	      continue;
	  }
      }
    ch_gotoxy(0,j);
    for(i=0;i<dimx;i++)
      {
	if (_ch_redrawch(i,j))
	    i++;
      }
  } 
  ch_invert(inv);
  ch_underline(und);
  
there:  
  mon_setcolor(color);
  ch_gotoxy(ooldx,ooldy);
  ch_cursor_blink(1);
  cursor(SIGALRM);
  autoscroll = scrollmode;
  LEAVE;
}

void ch_write_to_screen(int m)
{
    
  write_to_screen += (m>0)? 1:-1;
}

void ch_restoretext()
{
  mon_textmode();
}


void ch_system_showstr(CHAR *s)
{
  int color = mon_curcolor();
  int oldw = autoscroll;
  mon_setcolor(COLOR_SYSTEM_FG+COLOR_SYSTEM_BG*256);
  autoscroll = 0;
  while(*s)
    {
      ch_system_showch(*s++);
    }
  autoscroll = oldw;
  mon_setcolor(color);
}

void ch_system_showch(CHAR c)
{
  int code;
  int color = mon_curcolor();
  int oldw = autoscroll;
  
  if (write_to_screen)
    {
      mon_setcolor(COLOR_SYSTEM_FG+COLOR_SYSTEM_BG*256);
      autoscroll = 0;

      scr_text[sys_pos] = c;
      scr_attr[sys_pos] = sys_attr;

      if (sys_highbyte)
	{
	  code = tran_code(codesys,scr_text[sys_pos-1],scr_text[sys_pos]);
	  if (code != 0xffff)
	    {
	      ch_drawctext((sys_pos%dimx)-1,dimy,code);
	    }
	  else
	    {
	      ch_drawtext((sys_pos%dimx)-1,sys_pos/dimx,scr_text[sys_pos-1]);
	      ch_drawtext((sys_pos%dimx),dimy,scr_text[sys_pos]);
	    }
	  sys_highbyte=0;
	}
      else
	{
	  if (IS_FIRST_CHAR(c))
	    {
	      scr_text[sys_pos] = c;
	      scr_attr[sys_pos] = sys_attr;
	      sys_highbyte = 1;
	    }
	  else
	    {
	      scr_text[sys_pos] = c;
	      scr_attr[sys_pos] = sys_attr;
	      ch_drawtext(sys_pos%dimx,dimy,c);
	    }
	}
      sys_pos++;
      if (sys_pos >= dimx*(dimy+1))
	sys_pos = dimx*(dimy+1)-1;
      autoscroll = oldw;
      mon_setcolor(color);
    }
}

void ch_setscroll(int t,int b)
{
  if (t < 0)
    t = 0;
  else if (t > dimy-3)
    t = dimy-3;
  if (b < 0)
    b = t + 2;
  if (b > dimy-2)
    b = dimy-1;
  bottoml = b;
  topl = t;
}

void ch_setwsize(int tty,int wx,int wy)
{
  VSC *nscr ;
  char *text;
  int  *attr;
  int olddimx,olddimy,i,j;
  if (tty > CONS_NUM)
    return;
  ENTER;
  nscr = &vscreen[tty];
  olddimx = dimx;
  olddimy = dimy;
  if (wx > MAX_DIMX)
    wx = MAX_DIMX;
  else if (wx < 0)
    wx = MIN_DIMX;

  if (wy > MAX_DIMY)
    wy = MAX_DIMY;
  else if (wy < 0)
    wy = 0;

  dimx = wx;
  bottoml = (dimy = wy)-1;
  if (scr_text==NULL)
    {
      LEAVE;
      return;
    }
  text = scr_text ;
  attr = scr_attr;
  scr_text = (CHAR *) malloc(dimx*dimy*sizeof(CHAR));
  scr_attr = (int *) malloc(dimx*dimy*sizeof(int));
  if (scr_attr == NULL)
    {
      SYSERR("Out of memory");
      if (scr_text)
	free(scr_text);
      scr_text = text;
      scr_attr = attr;
      LEAVE;
      return;
    }
  ENTER;
  for(i=0;i<dimx;i++) for(j=0;j<dimy;j++)
    {
      scr_text[j*dimx+i] = text[j*olddimx+i];
      scr_attr[j*dimx+i] = attr[j*olddimx+i];
    }
  free(text);
  free(attr);
  LEAVE;
}

void ch_drawstring(int xx,int yy,CHAR *stext)
{
  while(*stext)
    {
      ch_drawtext(xx,yy,*stext++);
      xx++;
      if (xx > dimx)
	{
	  yy++;
	  xx = 0;
	  if (yy > dimy)
	      yy = 0;
	}
    }
}

void ch_screen_saver(int stat)
{
  static int _posx,_posy;
  static char saver_string[] = " touch me! ";
    
  if (stat == START_SAVER)
    {
      ch_have_cursor(0);
      if (write_to_screen>0)
	  mon_clearblock(0,0,dimx-1,(dimy+1)*fontheight-1);
      _posx = _posy = 0;
      return;
    }
  else if (stat == RUN_SAVER)
    {
      _posx++;
      if (_posx > dimx-10)
	{
	  _posx = 0;
	  _posy++;
	  if (_posy > dimy)
	    {
	      _posy = 0;
	    }
	}
      ch_drawstring(_posx,_posy,saver_string);
    }
  else if (stat == END_SAVER)
    {
      ch_drawsystem();
      ch_redraw();
      ch_have_cursor(1);
    }
}

void Read_Console_Config()
{
  int i;
  char *tmp,*tok;

  if (tmp = getenv("CHDRV_COLOR_DEFINE"))
    sys_config.colordef = strdup(tmp);

  if (sys_config.colordef)
    {
      tok = strtok(sys_config.colordef," ");
      i = 0;
      while(tok&&(i<=7))
	{
	  low_color_table[i++] = atoi(tok);
	  tok = strtok(NULL," ");
	  printf("LOW COLOR %d is %d\n",i-1,low_color_table[i-1]);
	}
      i = 0;
      while(tok&&(i<=7))
	{
	  hi_color_table[i++] = atoi(tok);
	  tok = strtok(NULL," ");
	  printf("HIGH COLOR %d is %d\n",i-1,hi_color_table[i-1]);
	}
    }
}

void ch_automargin(int st)
{
    if (st == 0)
	automargin = 0;
    else
	automargin = 1;
}
void ch_quick_scroll(int st)
{
  quick_scroll = st;
}

int ch_is_quick_scroll()
{
  
  return quick_scroll;
}

void ch_refresh()
{
  int i;

  if (IS_WRITE_TO_CURRENT_CONSOLE)
    {
      if (line_out == 0 && quick_scroll)
	{
	  quick_scroll = 0;
	  ch_cursor_blink(0);
	  for(i=0;i<dimx;i++)
	    if (_ch_redrawch(i,cury))
	      i++;
	  ch_cursor_blink(1);
	}
      else if (line_out)
	{
	  ch_cursor_blink(0);
	  if (write_to_screen>0)
	    mon_scroll(topl*fontheight,(bottoml+1)*fontheight,line_out*fontheight);
	  ch_redraw_part();
	  line_out = 0;
	  ch_cursor_blink(1);
	}
    }
}
int ch_fontheight()
{
  return fontheight;
}

int ch_fontwidth()
{
  return fontwidth;
}

void ch_makeup_attr()
{
    sys_attr &= 0x0000ffff;
    sys_attr |= (COLOR_DEFAULT_BG<<24)|(COLOR_DEFAULT_FG<<16);
    if (cursor_on)
      ch_cursor();
    mon_setcolor(COLOR_DEFAULT_FG+(COLOR_DEFAULT_BG<<8));
    if (!cursor_on)
      ch_cursor();
}
#if 0
void ch_parseconfig(char *str)
{
    char *ss;
    
    while(isspace(*str)) str++;
    ss = strchr(str,',');
    if (ss == NULL)
      return;
    *ss=0;
    COLOR_DEFAULT_FG = atoi(str);
    str = ss+1;
    
    while(isspace(*str)) str++;
    ss = strchr(str,',');
    if (ss == NULL)
      return;
    *ss=0;
    COLOR_DEFAULT_BG = atoi(str);
    str = ss+1;
    while(isspace(*str)) str++;
    ss = strchr(str,',');
    if (ss == NULL)
      return;
    *ss=0;
    COLOR_SYSTEM_FG = atoi(str);
    str = ss+1;
    while(isspace(*str)) str++;
    ss = strchr(str,',');
    if (ss == NULL)
      return;
    *ss=0;
    COLOR_SYSTEM_BG = atoi(str);
    ch_makeup_attr();
}
#endif
/*
   setup if we addjust the position of cursor to the begin of two-byte
   Chinese word.If con is -1 ,the default screen is used and if align
   is -1,the align mode does not changed.
*/
    
int ch_wordalign(int con,int align)
{
    int rtn = wordalign;
    VSC *ss;
    if (con == -1)
      ss = nscr;
    else
      ss = vscreen + con;
    if (align != -1)
      wordalign = align;
    return rtn;
}

/*
   Check if the current position need a position adjustment.If return value
   is 0,it stand for doesn't need a adjustment.Otherwise stand for need a
   adjustment.If con is -1,the default console is used.
 */  

int ch_needadjust(int con)
{
    VSC *ss;
    /* If  wordalign is disable,always return 0*/
    if (!wordalign) return 0;
    if (con == -1)
      ss = nscr;
    else
      ss = vscreen + con;

    if (ss->scrattr[pos]&COUPLE_RIGHT)
      return 1;
    else
      return 0;
}
	  
void ch_ansicolor(int fg,int bg)
{
  if (fg == -3)
    {
      color_table = low_color_table;
      curfg = sys_config.forecolor;
      curbg = sys_config.backcolor;
      COLOR_DEFAULT_FG = color_table[sys_config.forecolor];
      COLOR_DEFAULT_BG = low_color_table[sys_config.backcolor];
      ch_makeup_attr();
    }
  else if (fg == -2)
    {
      color_table = hi_color_table;
      COLOR_DEFAULT_FG = color_table[curfg];
      COLOR_DEFAULT_BG = low_color_table[curbg];
      ch_makeup_attr();
    }
  else
    {
      if ((fg >= 0)&&(fg <= 7))
	{
	  COLOR_DEFAULT_FG = color_table[fg];
	  curfg = fg;
	  ch_makeup_attr();
	}
      if ((bg >= 0)&&(bg <= 7))
	{
	  COLOR_DEFAULT_BG = low_color_table[bg];
	  curbg = bg;
	  ch_makeup_attr();
	}
    }
}
  



void ch_enablealternative()
{
  alternative = 1;
}

void ch_disablealternative() 
{
  alternative = 0;
}