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
|
/* $Id$ */
#include "bbs.h"
/*
* visio.c
* piaip's new implementation of visio
* (visio: virtual screen input output, the name from Maple3)
*
* This is not the original visio.c from Maple3.
* We just borrowed its file name and few API names/prototypes
* then re-implemented everything from scratch :)
*
* We will try to keep the API behavior similiar (to help porting)
* but won't stick to it.
* Maybe at the end only 'vmsg' and 'vmsgf' will still be compatible....
*
* m3 visio = (ptt) visio+screen/term.
*
* Author: Hung-Te Lin (piaip), April 2008.
*
* Copyright (c) 2008 Hung-Te Lin <piaip@csie.ntu.edu.tw>
* All rights reserved.
*
* Distributed under BSD license (GPL compatible).
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* To add API here, please...
* (1) name the API in prefix of 'v'.
* (2) use only screen.c APIs.
* (3) take care of wide screen and DBCS.
* (4) utilize the colos in visio.h, and name asa VCLR_* (visio color)
*/
// ---- DEFINITION ---------------------------------------------------
#define MAX_COL (t_columns-1)
#define SAFE_MAX_COL (MAX_COL-1)
#define VBUFLEN (ANSILINELEN)
// this is a special strlen to speed up processing.
// warning: x MUST be #define x "msg".
// otherwise you need to use real strlen.
#define MACROSTRLEN(x) (sizeof(x)-1)
// ---- UTILITIES ----------------------------------------------------
inline void
outnc(int n, unsigned char c)
{
while (n-- > 0)
outc(c);
}
inline void
nblank(int n)
{
outnc(n, ' ');
}
static inline void
fillns(int n, const char *s)
{
while (n > 0 && *s)
outc(*s++), n--;
if (n > 0)
outnc(n, ' ');
}
static inline void
fillns_ansi(int n, const char *s)
{
int d = strat_ansi(n, s);
if (d < 0) {
outs(s); nblank(-d);
} else {
outns(s, d);
}
}
// ---- VREF API --------------------------------------------------
/**
* vscr_save(): 傳回目前畫面的備份物件。
*/
VREFSCR
vscr_save(void)
{
// TODO optimize memory allocation someday.
screen_backup_t *o = (screen_backup_t*)malloc(sizeof(screen_backup_t));
assert(o);
scr_dump(o);
return o;
}
/**
* vscr_restore(obj): 使用並刪除畫面的備份物件。
*/
void
vscr_restore(VREFSCR obj)
{
screen_backup_t *o = (screen_backup_t*)obj;
if (o)
{
scr_restore(o);
memset(o, 0, sizeof(screen_backup_t));
free(o);
}
}
/**
* vcur_save(): 傳回目前游標的備份物件。
*/
VREFCUR
vcur_save(void)
{
// XXX 偷懶不 new object 了, pointer 夠大
int y, x;
VREFCUR v;
getyx(&y, &x);
v = ((unsigned short)y << 16) | (unsigned short)x;
return v;
}
/**
* vcur_restore(obj): 使用並刪除游標的備份物件。
*/
void
vcur_restore(VREFCUR o)
{
int y, x;
y = (unsigned int)(o);
x = (unsigned short)(y & 0xFFFF);
y = (unsigned short)(y >> 16);
move(y, x);
}
// ---- LOW LEVEL API -----------------------------------------------
/**
* prints(fmt, ...): 使用 outs/outc 輸出並格式化字串。
*/
void
prints(const char *fmt,...)
{
va_list args;
char buff[VBUFLEN];
va_start(args, fmt);
vsnprintf(buff, sizeof(buff), fmt, args);
va_end(args);
outs(buff);
}
/**
* mvouts(y, x, str): = mvaddstr
*/
void
mvouts(int y, int x, const char *str)
{
move(y, x);
clrtoeol();
SOLVE_ANSI_CACHE();
outs(str);
}
/**
* vfill(n, flags, s): 印出並填滿 n 個字元的空間
*
* @param n space to occupy
* @param flags VFILL_* parameters
* @param s string to display
*/
void
vfill(int n, int flags, const char *s)
{
// warning: flag determination must take care of default values.
char has_ansi = ((flags & VFILL_HAS_ANSI) || (*s == ESC_CHR));
char has_border = !(flags & VFILL_NO_BORDER);
if (n < 1)
return;
// quick return
if (!*s)
{
nblank(n);
return;
}
// calculate border size (always draw because n > 0)
if (has_border)
n--;
if (n > 0)
{
if (flags & VFILL_RIGHT_ALIGN)
{
// right-align
int l = has_ansi ? strlen_noansi(s) : strlen(s);
if (l >= n) // '=' prevents blanks
l = n;
else {
nblank(n - l);
n = l;
}
// leave the task to left-align
}
// left-align
if (has_ansi)
fillns_ansi(n, s);
else
fillns(n, s);
}
// print border if required
if (has_border)
outc(' ');
// close fill.
if (has_ansi)
outs(ANSI_RESET);
}
/**
* vfill(n, flags, fmt, ...): 使用 vfill 輸出並格式化字串。
*
* @param n space to occupy
* @param flags VFILL_* parameters
* @param fmt string to display
*/
void
vfillf(int n, int flags, const char *s, ...)
{
va_list args;
char buff[VBUFLEN];
va_start(args, s);
vsnprintf(buff, sizeof(buff), s, args);
va_end(args);
vfill(n, flags, s);
}
/**
* vpad(n, pattern): 填滿 n 個字元 (使用的格式為 pattern)
*
* @param n 要填滿的字元數 (無法填滿時會使用空白填補)
* @param pattern 填充用的字串
*/
inline void
vpad(int n, const char *pattern)
{
int len = strlen(pattern);
// assert(len > 0);
while (n >= len)
{
outs(pattern);
n -= len;
}
if (n) nblank(n);
}
/**
* vgety(): 取得目前所在位置的行數
*
* 考慮到 ANSI 系統,getyx() 較為少用且危險。
* vgety() 安全而明確。
*/
inline int
vgety(void)
{
int y, x;
getyx(&y, &x);
return y;
}
// ---- HIGH LEVEL API -----------------------------------------------
/**
* vshowmsg(s): 在底部印出指定訊息或單純的暫停訊息
*
* @param s 指定訊息。 NULL: 任意鍵繼續。 s: 若有 \t 則後面字串靠右 (若無則顯示任意鍵)
*/
void
vshowmsg(const char *msg)
{
int w = SAFE_MAX_COL;
move(b_lines, 0); clrtoeol();
if (!msg)
{
// print default message in middle
outs(VCLR_PAUSE_PAD);
outc(' '); // initial one space
// VMSG_PAUSE MUST BE A #define STRING.
w -= MACROSTRLEN(VMSG_PAUSE); // strlen_noansi(VMSG_PAUSE);
w--; // initial space
vpad(w/2, VMSG_PAUSE_PAD);
outs(VCLR_PAUSE);
outs(VMSG_PAUSE);
outs(VCLR_PAUSE_PAD);
vpad(w - w/2, VMSG_PAUSE_PAD);
} else {
// print in left, with floating (if \t exists)
char *pfloat = strchr(msg, '\t');
int szfloat = 0;
int nmsg = 0;
// print prefix
w -= MACROSTRLEN(VMSG_MSG_PREFIX); // strlen_noansi(VMSG_MSG_PREFIX);
outs(VCLR_MSG);
outs(VMSG_MSG_PREFIX);
// if have float, calculate float size
if (pfloat) {
nmsg = pfloat - msg;
w -= strlen_noansi(msg) -1; // -1 for \t
pfloat ++; // skip \t
szfloat = strlen_noansi(pfloat);
} else {
pfloat = VMSG_MSG_FLOAT;
szfloat = MACROSTRLEN(VMSG_MSG_FLOAT); // strlen_noansi()
w -= strlen_noansi(msg) + szfloat;
}
// calculate if we can display float
if (w < 0)
{
w += szfloat;
szfloat = 0;
}
// print msg body
if (nmsg)
outns(msg, nmsg);
else
outs(msg);
// print padding for floats
if (w > 0)
nblank(w);
// able to print float?
if (szfloat)
{
outs(VCLR_MSG_FLOAT);
outs(pfloat);
}
}
// safe blank
outs(" " ANSI_RESET);
}
/**
* vans(s): 在底部印出訊息與小輸入欄,並傳回使用者的輸入(轉為小寫)。
*
* @param s 指定訊息,見 vshowmsg
*/
int
vans(const char *msg)
{
char buf[3];
move(b_lines, 0);
clrtoeol(); SOLVE_ANSI_CACHE();
outs(msg);
vgets(buf, sizeof(buf), VGET_LOWERCASE);
return (unsigned char)buf[0];
}
/**
* vansf(s, ...): 在底部印出訊息與小輸入欄,並傳回使用者的輸入(轉為小寫)。
*
* @param s 指定訊息,見 vshowmsg
*/
int
vansf(const char *fmt, ...)
{
char msg[VBUFLEN];
va_list ap;
va_start(ap, fmt);
vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);
return vans(msg);
}
/**
* vmsg(s): 在底部印出指定訊息或單純的暫停訊息,並傳回使用者的按鍵。
*
* @param s 指定訊息,見 vshowmsg
*/
int
vmsg(const char *msg)
{
int i = 0;
vshowmsg(msg);
// wait for key
do {
i = igetch();
} while( i == 0 );
// clear message bar
move(b_lines, 0);
clrtoeol();
return i;
}
/**
* vmsgf(s, ...): 格式化輸出暫停訊息並呼叫 vmsg)。
*
* @param s 格式化的訊息
*/
int
vmsgf(const char *fmt,...)
{
char msg[VBUFLEN];
va_list ap;
va_start(ap, fmt);
vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);
return vmsg(msg);
}
/**
* vbarf(s, ...): 格式化輸出左右對齊的字串 (MAX_COL)
*
* @param s 格式化字串 (\t 後的內容會對齊右端)
*/
void
vbarf(const char *s, ...)
{
char msg[VBUFLEN], *s2;
va_list ap;
va_start(ap, s);
vsnprintf(msg, sizeof(msg), s, ap);
va_end(ap);
s2 = strchr(msg, '\t');
if (s2) *s2++ = 0;
else s2 = "";
return vbarlr(msg, s2);
}
/**
* vbarlr(l, r): 左右對齊畫滿螢幕 (MAX_COL)
* 注意: 目前的實作自動認定游標已在行開頭。
*
* @param l 靠左對齊的字串 (可含 ANSI 碼)
* @param r 靠右對齊的字串 (可含 ANSI 碼,後面不會補空白)
*/
void
vbarlr(const char *l, const char *r)
{
// TODO strlen_noansi 跑兩次... 其實 l 可以邊 output 邊算。
int szl = strlen_noansi(l),
szr = strlen_noansi(r);
// assume we are already in (y, 0)
clrtoeol();
outs(l);
szl = MAX_COL - szl;
if (szl > szr)
{
nblank(szl - szr);
szl = szr;
}
if (szl == szr)
outs(r);
else if (szl > 0)
nblank(szl);
outs(ANSI_RESET);
}
// ---- THEMED FORMATTING OUTPUT -------------------------------------
/**
* vs_header(title, mid, right): 清空螢幕並輸出完整標題 (MAX_COL)
*
* @param title: 靠左的主要標題,不會被切斷。
* @param mid: 置中說明,可被切齊。
* @param right: 靠右的說明,空間夠才會顯示。
*/
void
vs_header(const char *title, const char *mid, const char *right)
{
int w = MAX_COL;
int szmid = mid ? strlen(mid) : 0;
int szright = right ? strlen(right) : 0;
clear();
outs(VCLR_HEADER);
if (title)
{
outs(VMSG_HDR_PREFIX);
outs(title);
outs(VMSG_HDR_POSTFIX);
w -= MACROSTRLEN(VMSG_HDR_PREFIX) + MACROSTRLEN(VMSG_HDR_POSTFIX);
w -= strlen(title);
}
// determine if we can display right message, and
// if if we need to truncate mid.
if (szmid + szright > w)
szright = 0;
if (szmid >= w)
szmid = w;
else {
int l = (MAX_COL-szmid)/2;
l -= (MAX_COL-w);
if (l > 0)
nblank(l), w -= l;
}
if (szmid) {
outs(VCLR_HEADER_MID);
outns(mid, szmid);
outs(VCLR_HEADER);
}
nblank(w - szmid);
if (szright) {
outs(VCLR_HEADER_RIGHT);
outs(right);
}
outs(ANSI_RESET "\n");
}
/**
* vs_hdr(title): 清空螢幕並輸出簡易的標題
*
* @param title
*/
void
vs_hdr(const char *title)
{
clear();
outs(VCLR_HDR VMSG_HDR_PREFIX);
outs(title);
outs(VMSG_HDR_POSTFIX ANSI_RESET "\n");
}
/**
* vs_footer(caption, msg): 在螢幕底部印出格式化的 caption msg (不可含 ANSI 碼)
*
* @param caption 左邊的分類字串
* @param msg 訊息字串, \t 後文字靠右、最後面會自動留一個空白。
*/
void
vs_footer(const char *caption, const char *msg)
{
int i = 0;
move(b_lines, 0); clrtoeol();
if (caption)
{
outs(VCLR_FOOTER_CAPTION);
outs(caption);
i += (*caption == ESC_CHR) ?
strlen_noansi(caption) :
strlen(caption);
}
if (!msg) msg = "";
outs(VCLR_FOOTER);
while (*msg && i < SAFE_MAX_COL)
{
if (*msg == '(')
{
outs(VCLR_FOOTER_QUOTE);
}
else if (*msg == '\t')
{
// if we don't have enough space, ignore whole.
int l = strlen(++msg);
if (i + l > SAFE_MAX_COL) break;
l = SAFE_MAX_COL - l - i;
nblank(l);
i += l;
continue;
}
outc(*msg); i++;
if (*msg == ')')
outs(VCLR_FOOTER);
msg ++;
}
nblank(SAFE_MAX_COL-i);
outc(' ');
outs(ANSI_RESET);
}
/**
* vs_cols_layout(cols, ws, n): 依據 cols (大小 n) 的定義計算適合的行寬於 ws
*/
void
vs_cols_layout(const VCOL *cols, VCOLW *ws, int n)
{
int i, tw;
VCOLPRI pri1 = cols[0].pri;
memset(ws, 0, sizeof(VCOLW) * n);
// first run, calculate minimal size
for (i = 0, tw = 0; i < n; i++)
{
// drop any trailing if required
if (tw + cols[i].minw > MAX_COL)
break;
ws[i] = cols[i].minw;
tw += ws[i];
}
if (tw < MAX_COL) {
// calculate highest priorities
// (pri1 already set to col[0].pri)
for (i = 1; i < n; i++)
{
if (cols[i].pri > pri1)
pri1 = cols[i].pri;
}
}
// try to iterate through all.
while (tw < MAX_COL) {
char run = 0;
// also update pri2 here for next run.
VCOLPRI pri2 = cols[0].pri;
for (i = 0; i < n; i++)
{
// if reach max, skip.
if (ws[i] >= cols[i].maxw)
continue;
// lower priority, update pri2 and skip.
if (cols[i].pri < pri1)
{
if (cols[i].pri > pri2)
pri2 = cols[i].pri;
continue;
}
// now increase fields
ws[i] ++;
if (++tw >= MAX_COL) break;
run ++;
}
// if no more fields...
if (!run) {
if (pri1 <= pri2) // no more priorities
break;
pri1 = pri2; // try lower priority.
}
}
}
/**
* vs_cols: 依照已經算好的欄位大小進行輸出
*/
void
vs_cols(const VCOL *cols, const VCOLW *ws, int n, ...)
{
int i = 0, w = 0;
char *s = NULL;
va_list ap;
va_start(ap, n);
for (i = 0; i < n; i++, cols++, ws++)
{
int flags = 0;
s = va_arg(ap, char*);
// quick check input.
if (!s)
{
s = "";
}
w = *ws;
if (cols->attr)
outs(cols->attr);
// build vfill flag
if (cols->flags.right_align) flags |= VFILL_RIGHT_ALIGN;
if (cols->flags.usewhole) flags |= VFILL_NO_BORDER;
vfill(w, flags, s);
if (cols->attr)
outs(ANSI_RESET);
}
va_end(ap);
// end line
outs(ANSI_RESET "\n");
}
////////////////////////////////////////////////////////////////////////
// DBCS Aware Helpers
////////////////////////////////////////////////////////////////////////
#ifdef DBCSAWARE
# define CHKDBCSTRAIL(_buf,_i) (ISDBCSAWARE() && DBCS_Status(_buf, _i) == DBCS_TRAILING)
#else // !DBCSAWARE
# define CHKDBCSTRAIL(buf,i) (0)
#endif // !DBCSAWARE
////////////////////////////////////////////////////////////////////////
// History Helpers
////////////////////////////////////////////////////////////////////////
//
#define IH_MAX_ENTRIES (12) // buffer size = approx. 1k
#define IH_MIN_SIZE (2) // only keep string >= 2 bytes
typedef struct {
int icurr; // current retrival pointer
int iappend; // new location to append
char buf[IH_MAX_ENTRIES][STRLEN];
} InputHistory;
static InputHistory ih; // everything intialized to zero.
int
InputHistoryExists(const char *s)
{
int i = 0;
for (i = 0; i < IH_MAX_ENTRIES; i++)
if (strcmp(s, ih.buf[i]) == 0)
return i+1;
return 0;
}
int
InputHistoryAdd(const char *s)
{
int i = 0;
int l = strlen(s);
if (l < IH_MIN_SIZE)
return 0;
i = InputHistoryExists(s);
if (i > 0) // found
{
i--; // i points to valid index
assert(i < IH_MAX_ENTRIES);
// change order: just delete it.
ih.buf[i][0] = 0;
}
// now append s.
strlcpy(ih.buf[ih.iappend], s, sizeof(ih.buf[ih.iappend]));
ih.iappend ++;
ih.iappend %= IH_MAX_ENTRIES;
ih.icurr = ih.iappend;
return 1;
}
static void
InputHistoryDelta(char *s, int sz, int d)
{
int i, xcurr = 0;
for (i = 1; i <= IH_MAX_ENTRIES; i++)
{
xcurr = (ih.icurr+ IH_MAX_ENTRIES + d*i)%IH_MAX_ENTRIES;
if (ih.buf[xcurr][0])
{
ih.icurr = xcurr;
// copy buffer
strlcpy(s, ih.buf[ih.icurr], sz);
// DBCS safe
i = strlen(s);
if (DBCS_Status(s, i) == DBCS_TRAILING)
s[i-1] = 0;
break;
}
}
}
void
InputHistoryPrev(char *s, int sz)
{
InputHistoryDelta(s, sz, -1);
}
void
InputHistoryNext(char *s, int sz)
{
InputHistoryDelta(s, sz, +1);
}
////////////////////////////////////////////////////////////////////////
// vget*: mini editbox
////////////////////////////////////////////////////////////////////////
static inline int
_vgetcbhandler(VGET_FCALLBACK cbptr, int *pabort, int c, VGET_RUNTIME *prt, void *instance)
{
if (!cbptr)
return 0;
switch(cbptr(c, prt, instance))
{
case VGETCB_NONE:
assert( prt->icurr >= 0 && prt->icurr <= prt->iend && prt->iend <= prt->len );
return 0;
case VGETCB_NEXT:
assert( prt->icurr >= 0 && prt->icurr <= prt->iend && prt->iend <= prt->len );
return 1;
case VGETCB_END:
assert( prt->icurr >= 0 && prt->icurr <= prt->iend && prt->iend <= prt->len );
*pabort = 1;
return 1;
case VGETCB_ABORT:
assert( prt->icurr >= 0 && prt->icurr <= prt->iend && prt->iend <= prt->len );
*pabort = 1;
prt->icurr = 0;
prt->iend = 0;
prt->buf[0] = 0;
return 1;
}
assert(0); // shall never reach here
return 0;
}
int
vgetstring(char *_buf, int len, int flags, const char *defstr, const VGET_CALLBACKS *pcbs, void *instance)
{
// rt.iend points to NUL address, and
// rt.icurr points to cursor.
int line, col;
int abort = 0;
int c;
char ismsgline = 0;
// callback
VGET_CALLBACKS cb = {NULL};
// always use internal buffer to prevent temporary input issue.
char buf[STRLEN] = ""; // zero whole.
// runtime structure
VGET_RUNTIME rt = { buf, len > STRLEN ? STRLEN : len };
// it is wrong to design input with larger buffer
// than STRLEN. Although we support large screen,
// inputting huge line will just make troubles...
if (len > STRLEN) len = STRLEN;
assert(len <= sizeof(buf) && len >= 2);
// memset(buf, 0, len);
if (defstr && *defstr)
{
strlcpy(buf, defstr, len);
strip_ansi(buf, buf, STRIP_ALL); // safer...
rt.icurr = rt.iend = strlen(buf);
}
// setup callbacks
if (pcbs)
cb = *pcbs;
getyx(&line, &col); // now (line,col) is the beginning of our new fields.
// XXX be compatible with traditional...
if (line == b_lines - msg_occupied)
ismsgline = 1;
if (ismsgline)
msg_occupied ++;
// main loop
while (!abort)
{
if (!(flags & VGET_NOECHO))
{
// print current buffer
move(line, col);
clrtoeol();
SOLVE_ANSI_CACHE();
if (!(flags & VGET_TRANSPARENT))
outs(VCLR_INPUT_FIELD); // change color to prompt fields
vfill(len, 0, buf);
if (!(flags & VGET_TRANSPARENT))
outs(ANSI_RESET);
// move to cursor position
move(line, col+rt.icurr);
} else {
// to simulate the "clrtoeol" behavior...
// XXX make this call only once? or not?
clrtoeol();
}
c = vkey();
// callback 1: peek
if (_vgetcbhandler(cb.peek, &abort, c, &rt, instance))
continue;
// standard key bindings
switch(c) {
// history navigation
case KEY_DOWN: case Ctrl('N'):
c = KEY_DOWN;
// let UP do the magic.
case KEY_UP: case Ctrl('P'):
if ((flags & VGET_NOECHO) ||
(flags & VGET_DIGITS))
{
bell();
continue;
}
// NOECHO is already checked...
if (!InputHistoryExists(buf))
InputHistoryAdd(buf);
if (c == KEY_DOWN)
InputHistoryNext(buf, len);
else
InputHistoryPrev(buf, len);
rt.icurr = rt.iend = strlen(buf);
break;
// exiting keys
case KEY_ENTER:
abort = 1;
break;
case Ctrl('C'):
rt.icurr = rt.iend = 0;
buf[0] = 0;
buf[1] = c;
abort = 1;
break;
// standard navigation
case KEY_HOME: case Ctrl('A'):
rt.icurr = 0;
break;
case KEY_END: case Ctrl('E'):
rt.icurr = rt.iend;
break;
case KEY_LEFT: case Ctrl('B'):
if (rt.icurr > 0)
rt.icurr--;
else
bell();
if (rt.icurr > 0 && CHKDBCSTRAIL(buf, rt.icurr))
rt.icurr--;
break;
case KEY_RIGHT: case Ctrl('F'):
if (rt.icurr < rt.iend)
rt.icurr++;
else
bell();
if (rt.icurr < rt.iend && CHKDBCSTRAIL(buf, rt.icurr))
rt.icurr++;
break;
// editing keys
case KEY_DEL: case Ctrl('D'):
if (rt.icurr+1 < rt.iend && CHKDBCSTRAIL(buf, rt.icurr+1)) {
// kill next one character.
memmove(buf+rt.icurr, buf+rt.icurr+1, rt.iend-rt.icurr);
rt.iend--;
}
if (rt.icurr < rt.iend) {
// kill next one character.
memmove(buf+rt.icurr, buf+rt.icurr+1, rt.iend-rt.icurr);
rt.iend--;
}
break;
case KEY_BS: case KEY_BS2:
if (rt.icurr > 0) {
// kill previous one charracter.
memmove(buf+rt.icurr-1, buf+rt.icurr, rt.iend-rt.icurr+1);
rt.icurr--; rt.iend--;
} else
bell();
if (rt.icurr > 0 && CHKDBCSTRAIL(buf, rt.icurr)) {
// kill previous one charracter.
memmove(buf+rt.icurr-1, buf+rt.icurr, rt.iend-rt.icurr+1);
rt.icurr--; rt.iend--;
}
break;
case Ctrl('Y'):
rt.icurr = 0;
// reuse Ctrl-K code
case Ctrl('K'):
rt.iend = rt.icurr;
buf[rt.iend] = 0;
break;
// defaults
default:
// content filter
if (c < ' ' || c >= 0xFF)
{
bell(); continue;
}
if ((flags & VGET_DIGITS) &&
( !isascii(c) || !isdigit(c)))
{
bell(); continue;
}
if (flags & VGET_LOWERCASE)
{
if (!isascii(c))
{
bell(); continue;
}
c = tolower(c);
}
if (flags & VGET_ASCII_ONLY)
{
if (!isprint(c))
{
bell(); continue;
}
}
// size check
if(rt.iend+1 >= len)
{
bell(); continue;
}
// prevent incomplete DBCS
if (c > 0x80 && num_in_buf() &&
len - rt.iend < 3) // we need 3 for DBCS+NUL.
{
drop_input();
bell();
continue;
}
// callback 2: data
if (_vgetcbhandler(cb.data, &abort, c, &rt, instance))
continue;
// size check again, due to data callback.
if(rt.iend+1 >= len)
{
bell(); continue;
}
// add one character.
memmove(buf+rt.icurr+1, buf+rt.icurr, rt.iend-rt.icurr+1);
buf[rt.icurr++] = c;
rt.iend++;
// callback 3: post
if (_vgetcbhandler(cb.post, &abort, c, &rt, instance))
continue;
break;
}
}
assert(rt.iend >= 0 && rt.iend < len);
buf[rt.iend] = 0;
// final filtering
if (rt.iend && (flags & VGET_LOWERCASE))
buf[0] = tolower(buf[0]);
// save the history except password mode
if (buf[0] && !(flags & VGET_NOECHO))
InputHistoryAdd(buf);
// copy buffer!
memcpy(_buf, buf, len);
// XXX update screen display
if (ismsgline)
msg_occupied --;
/* because some code then outs so change new line.*/
move(line+1, 0);
return rt.iend;
}
int
vgets(char *buf, int len, int flags)
{
return vgetstr(buf, len, flags, "");
}
int
vgetstr(char *buf, int len, int flags, const char *defstr)
{
return vgetstring(buf, len, flags, defstr, NULL, NULL);
}
|