-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistw.c
1024 lines (807 loc) · 19.5 KB
/
histw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <u.h>
#include <libc.h>
#include <keyboard.h>
/* processing line buffer size */
#define LBFS 1024
#define HPS 512
static int uselocal;
static int useglobal;
static int handlequirk;
static int wsysfd;
static int mod;
static char *home;
static char *prompt;
struct Hstate {
/* track text file state */
int tstate;
int tsrc;
ulong tsize;
ulong tpos;
/* track operation state */
int hop;
/* track filter state */
int fstate;
char *pfilter;
/* track window */
int wwid;
};
typedef struct Hstate Hstate;
static Hstate state;
int
readwctl(char *buf, int nbuf, int id)
{
/* read control file from window with id X */
int fd, n;
char s[64];
snprint(s, sizeof(s), "/dev/wsys/%d/wctl", id);
fd = open(s, OREAD);
if(fd < 0){
buf[0] = 0;
return -1;
}
n = read(fd, buf, nbuf-1);
close(fd);
if(n < 0){
buf[0] = 0;
return -1;
}
buf[n] = 0;
return n;
}
ulong
procfilesize(char *fname)
{
/* get file size by reading through file instead of using file stats */
/* generated files like /dev/text have size 0 in stats */
int fd, r;
ulong sum = 0;
char buf[4096];
fd = open(fname, OREAD);
if(fd < 0){
return 0;
}
do{
r = read(fd, buf, sizeof buf);
sum = sum + r;
}while(r == sizeof buf);
close(fd);
return sum;
}
ulong
diskfilesize(char *fname)
{
/* get file size using stats */
Dir *dir;
ulong sum;
dir = dirstat(fname);
if (dir == nil){
return 0;
}
sum = dir->length;
free(dir);
return sum;
}
int
filtercheck(char *sarr, int slen, char *fstr)
{
/* check if withing given length of char array exist filter string */
/* do so by converting array + size to string then look for substring */
/* no filter, return true */
if(fstr == nil){
return 1;
}
/* prepare and compare strings */
char *sstr = calloc(slen+1, sizeof(char));
memcpy(sstr, sarr, slen);
char *subres = cistrstr(sstr, fstr);
free(sstr);
if(subres != 0){
return 1;
}
return 0;
}
char*
fromprompt(void)
{
/* parse current window text file to scrape any text after prompt */
#define TPS 128
#define PBS 512
int tfd;
char textpath[TPS];
char pbf[PBS];
state.fstate = 1;
/* compose full path to text resource of current terminal */
memset(textpath, 0, TPS);
snprint(textpath, TPS, "/dev/wsys/%d/text", state.wwid);
ulong ts = procfilesize(textpath);
if(ts == 0){
return nil;
}
/* search in reverse MUST NOT have null chars at the beginning */
memset(pbf, 64, PBS);
pbf[PBS-1] = 0;
ulong bfl = PBS - 1;
if(ts < bfl){
/* less text than buffer */
bfl = ts;
}
tfd = open(textpath, OREAD);
if(tfd < 0){
return nil;
}
long rc = pread(tfd, pbf, bfl, (ts-bfl));
close(tfd);
tokenize(getenv("prompt"), &prompt, 1);
int prc = strlen(prompt);
char *ssp = nil;
char *tmpfr = pbf;
do{
tmpfr = strstr(tmpfr, prompt);
if(tmpfr != nil){
ssp = tmpfr;
tmpfr = tmpfr + 1;
}
} while(tmpfr != nil && tmpfr < (pbf + PBS - 1));
free(prompt);
if(ssp == nil){
return nil;
}
char *pstr = nil;
if(rc - ((ssp-pbf)+(prc+1)) > 0){
pstr = (char*) calloc(rc - ((ssp-pbf)+prc), sizeof(char));
memcpy(pstr, ssp+prc+1, rc - ((ssp-pbf)+(prc+1)));
}
return pstr;
}
void
toprompt(char *text, int len)
{
/* set window prompt with supplied text */
/* to ensure known state, delete anything that was there before */
int i;
char kbdbf[3] = {0, 0, 0};
kbdbf[0] = 'c'; /* denote to kbdtap a regular character is being sent*/
kbdbf[1] = 2; /* STX (ctrl+b) - move cursor to prompt */
write(1, kbdbf, 2);
kbdbf[1] = 5; /* ENQ (ctrl+e) - move cursor to end of text in promt */
write(1, kbdbf, 2);
kbdbf[1] = 21; /* NAK (ctrl+u) - delete everything behind cursor */
write(1, kbdbf, 2);
if(len < 1){
return;
}
for(i=0; i<len; i++){
kbdbf[1] = text[i];
write(1, kbdbf, 2);
}
}
void
resetprompt(void){
/* reset window prompt */
/* either delete all text or set text that was used as filter */
if(state.pfilter == nil){
toprompt("", 0);
} else {
toprompt(state.pfilter, strlen(state.pfilter));
}
state.hop = 0;
state.fstate = 0;
}
void
clearprompt(void){
/* clear window prompt */
/* delete all text without changing history state */
toprompt("", 0);
}
void
resethstate(void){
/* reset history state */
state.tstate = 0;
if(uselocal >= useglobal){
state.tsrc = 1;
} else {
state.tsrc = 2;
}
state.tsize = 0;
state.tpos = 0;
state.hop = 0;
state.fstate = 0;
free(state.pfilter);
state.pfilter = nil;
state.wwid = 0;
}
void
processhist(int kbdcmd)
{
/* process and display command history depending on keyboard input */
/* history operations (hop) legend: */
/* >0 - history up by x */
/* <0 - history down by x */
/* history source (tsrc) legend: */
/* 1 - local history */
/* 2 - global history */
int hfd, fr;
char linebf[LBFS];
char histpath[HPS];
long hc;
/* find current window id */
int dsn, dn, wid;
Dir *ds, *d;
char s[256], *t[8];
wid = 0;
seek(wsysfd, 0, 0);
dsn = dirreadall(wsysfd, &ds);
for(dn = 0, d = ds; dn < dsn; dn++, d++){
if(readwctl(s, sizeof(s), atoi(d->name)) <= 0){
continue;
}
if(tokenize(s, t, nelem(t)) != 6){
continue;
}
if(strcmp(t[4], "current") == 0){
wid = atoi(d->name);
break;
}
}
if(wid != state.wwid){
resethstate();
state.wwid = wid;
state.pfilter = fromprompt(); /* window switch, assume from beginning */
clearprompt();
}
/* determine history operation */
if(kbdcmd > 0){
if(state.hop < 0){
state.hop = 2;
} else {
state.hop = 1;
}
}
if(kbdcmd < 0){
if(state.hop > 0){
state.hop = -2;
} else {
state.hop = -1;
}
}
/* check for prompt input as search filter */
if(state.fstate == 0){
state.pfilter = fromprompt();
}
/* process local history from /dev/wsys/%id/text */
if(state.tsrc == 1 && state.hop != 0){
int tfd, rc;
tokenize(getenv("prompt"), &prompt, 1);
int prc = strlen(prompt);
ulong tr; /* text read */
ulong tp; /* text processed */
char *ssp; /* pointer to prompt */
char *sse; /* pointer to EOL */
char *ssee; /* pointer to second last EOL */
char *tmpfr;
int bfl = LBFS - 1; /* buffer left to read in */
int bfld = 0; /* buffer diff between moved and remaining space */
memset(histpath, 0, HPS);
/* compose full path to local user history */
snprint(histpath, HPS, "/dev/wsys/%d/text", state.wwid);
/* no history file has been opened yet or we are changing state */
if(state.tstate == 0){
state.tsize = procfilesize(histpath);
state.tstate = 1;
if(state.hop > 0){
state.tpos = state.tsize; /* starting history up */
} else {
state.tpos = 0; /* starting history down */
}
}
tfd = open(histpath, OREAD);
if(tfd < 0){
toprompt("# NO LOCAL HISTORY", 18);
state.hop = 0;
}
/* history up */
if(state.hop > 0){
/* search in reverse MUST NOT have null chars at the beginning */
memset(linebf, 64, LBFS);
linebf[LBFS-1] = 0;
/* first read exception - less text than buffer*/
if(bfl > (state.tsize - (state.tsize-state.tpos))){
bfl = (state.tsize - (state.tsize-state.tpos));
bfld = ((LBFS-1) - bfl);
}
tr = 0;
tp = state.tpos;
if(tp != state.tsize){
/* not processing history from the end */
tr = state.tsize - tp;
}
while(tp > 0){
rc = pread(tfd, linebf + bfld, bfl, (state.tsize-tr) - bfl);
bfl -= rc;
bfld = 0;
tr += rc;
/* find prompt */
ssp = nil;
tmpfr = linebf;
do{
tmpfr = strstr(tmpfr, prompt);
if(tmpfr != nil){
ssp = tmpfr;
tmpfr = tmpfr + 1;
}
} while(tmpfr != nil && tmpfr < (linebf + LBFS - 1));
/* find newline char */
sse = nil;
ssee = nil;
if(ssp != nil){
tmpfr = ssp;
} else {
tmpfr = linebf;
}
do{
tmpfr = strchr(tmpfr, '\n');
if(tmpfr != nil){
ssee = sse;
sse = tmpfr;
if(ssp != nil){
break; /* if prompt is found only next one is needed */
}
tmpfr = tmpfr + 1;
}
} while(tmpfr != nil && tmpfr < (linebf + LBFS - 1));
/* history hit */
if((ssp != nil) && (sse != nil)){
/* if prompt is found and is behing newline char or start of buffer */
if(*(ssp - 1) == '\n' || *(ssp - 1) == '@' || ssp == linebf){
/* and ignore empty lines */
if((sse-ssp) - prc - 1 > 0){
/* skip displaying same command on direction change */
if(state.hop > 1){
state.hop--;
} else {
/* skip displaying commands without filter string */
if(filtercheck(ssp+prc+1, (sse-ssp)-prc-1, state.pfilter)){
/* command to prompt */
if(tp < (LBFS-1-(ssp-linebf))){
tp = 0;
} else {
tp -= (LBFS-1-(ssp-linebf));
}
toprompt(ssp+prc+1, (sse-ssp)-prc-1);
break;
}
}
}
}
if((ssp-linebf) != 0){
/* move only if there is something to move */
memmove(linebf + (LBFS-1-(ssp-linebf)), linebf, (ssp-linebf));
}
memset(linebf, 64, (LBFS-1-(ssp-linebf)));
bfl += (LBFS-1-(ssp-linebf));
if(tp < (LBFS-1-(ssp-linebf))){
tp = 0;
} else {
tp -= (LBFS-1-(ssp-linebf));
}
}
/* buffer move - prompt */
if((ssp != nil) && (sse == nil)){
if((ssp-linebf) == 0){
/* nothing to move if prompt is at the beginning of buffer */
memset(linebf, 64, (LBFS-1));
bfl += (LBFS-1);
if(tp < (LBFS-1)){
tp = 0;
} else {
tp -= (LBFS-1);
}
} else {
/* move the rest carefully */
memmove(linebf + (LBFS-1-(ssp-linebf)), linebf, (ssp-linebf));
memset(linebf, 64, (LBFS-1-(ssp-linebf)));
bfl += (LBFS-1-(ssp-linebf));
if(tp < (LBFS-1-(ssp-linebf))){
tp = 0;
} else {
tp -= (LBFS-1-(ssp-linebf));
}
}
}
/* buffer move - newline */
if((ssp == nil) && (sse != nil)){
if((sse-linebf) == LBFS-2){
/* if newline char is the last char in buffer ... */
if (ssee != nil){
/* move untill second last newline char */
memmove(linebf + (LBFS-2-(ssee-linebf)), linebf, (ssee-linebf) + 1);
memset(linebf, 64, (LBFS-2-(ssee-linebf)));
bfl += (LBFS-2-(ssee-linebf));
if(tp < (LBFS-2-(ssee-linebf))){
tp = 0;
} else {
tp -= (LBFS-2-(ssee-linebf));
}
} else {
/* nothing to do but clear whole buffer */
memset(linebf, 64, (LBFS-1));
bfl += (LBFS-1);
if(tp < (LBFS-1)){
tp = 0;
} else {
tp -= (LBFS-1);
}
}
} else {
/* move till last newline char */
memmove(linebf + (LBFS-2-(sse-linebf)), linebf, (sse-linebf) + 1);
memset(linebf, 64, (LBFS-2-(sse-linebf)));
bfl += (LBFS-2-(sse-linebf));
if(tp < (LBFS-2-(sse-linebf))){
tp = 0;
} else {
tp -= (LBFS-2-(sse-linebf));
}
}
}
/* no hit in buffer exception */
if((ssp == nil) && (sse == nil)){
memset(linebf, 64, (LBFS-1));
bfl = (LBFS-1);
}
/* last read (end of data) exception */
if((tr+bfl) > state.tsize){
bfld = bfl;
bfl = bfl - ((tr+bfl) - state.tsize);
bfld = bfld - bfl;
}
/* fail-save's - just in case if don't know what's going on */
if((rc == 0) && (bfl != 0)){
/* tainted history - less to read than detected at start */
clearprompt();
tp = 0;
state.hop = 0;
}
if((rc == 0) && (bfl == 0) && (ssp == sse)){
/* tainted buffer - nothing to read or parse */
clearprompt();
tp = 0;
state.hop = 0;
}
state.tpos = tp; /* mark where we stopped */
}
/* no more history, set prompt alert */
if(tp == 0 && state.tpos == 0){
/* switch to global history if configured */
if(useglobal){
toprompt("# START OF GLOBAL HISTORY", 25);
state.tstate = 0;
state.tsrc = 2;
state.hop = 0;
} else {
toprompt("# END OF LOCAL HISTORY", 22);
}
}
/* repeated after the loop to update after break - for prompt alert */
state.tpos = tp; /* mark where we stopped */
}
/* history down */
if(state.hop < 0){
/* search in reverse MUST have null chars at the end */
memset(linebf, 0, LBFS);
/* first read exception - less text than buffer */
if(bfl > (state.tsize - state.tpos)){
bfl = (state.tsize - state.tpos);
bfld = ((LBFS-1) - bfl);
}
tr = 0;
tp = state.tpos;
if(tp != 0){
/* not processing history from the beginning */
tr = tp;
}
/* no history, nothing will happen*/
if(tp == state.tsize){
/* reset prompt */
resetprompt();
}
while(tp < state.tsize){
rc = pread(tfd, linebf + ((LBFS-1)-bfl) - bfld, bfl, tr);
bfl -= rc;
bfld = 0;
tr += rc;
ssp = strstr(linebf, prompt);
if(ssp != nil && (*(ssp - 1) == '\n' || ssp == linebf)){
/* if prompt is found and is behind newline char or start of buffer */
if(ssp-linebf > 0){
/* align prompt with start of the buffer */
memmove(linebf, ssp, LBFS - (ssp-linebf));
memset(linebf + LBFS - (ssp-linebf), 0, (ssp-linebf));
bfl += ((ssp-linebf));
tp += ((ssp-linebf));
}
/* we trust the buffer is long enough for the whole command */
sse = strchr(linebf, '\n');
if(sse != nil){
/* skip empty lines */
if(linebf[prc] != '\n'){
/* skip displaying same command on direction change */
if((sse-linebf) - prc > 1){
if(state.hop < -1){
state.hop++;
} else {
/* skip displaying commands without filter string */
if(filtercheck(linebf+prc+1, (sse-linebf)-prc-1, state.pfilter)){
/* command to prompt */
tp += (sse-linebf)+1;
toprompt(linebf+prc+1, (sse-linebf)-prc-1);
break;
}
}
}
}
memmove(linebf, sse+1, LBFS - (sse-linebf) + 1);
memset(linebf + LBFS - (sse-linebf) + 1, 0, (sse-linebf));
bfl += ((sse-linebf) + 1);
tp += ((sse-linebf) + 1);
}
} else {
/* if there is no prompt in buffer */
/* move to next new line character instead */
sse = strchr(linebf, '\n');
if((sse != nil) && ((sse-linebf) < LBFS-2)){
memmove(linebf, sse+1, LBFS - (sse-linebf) + 1);
memset(linebf + LBFS - (sse-linebf) + 1, 0, (sse-linebf));
bfl += ((sse-linebf) + 1);
tp += ((sse-linebf) + 1);
}
}
/* no hit in buffer exception */
if(bfl == 0){
bfl += LBFS - 1;
tp += strlen(linebf);
memset(linebf, 0, (LBFS-1));
/* no more history, reset prompt */
resetprompt();
}
/* last read (end of data) exception */
if((tr+bfl) > state.tsize){
bfld = bfl;
bfl = bfl - ((tr+bfl) - state.tsize);
bfld = bfld - bfl;
}
/* tainted history - less to read than detected at start */
if((rc == 0) && (bfl != 0)){
/* don't know what's going on, reset prompt */
resetprompt();
tp = state.tsize;
}
}
state.tpos = tp; /* mark where we stopped */
}
close(tfd);
free(prompt);
}
/* process global history from $home/lib/rchistory */
if(state.tsrc == 2 && state.hop != 0){
home = getenv("home");
memset(histpath, 0, HPS);
strcat(histpath, home);
strcat(histpath, "/lib/rchistory");
/* no history file has been opened yet or we are at the end */
if(state.tstate == 0){
state.tpos = diskfilesize(histpath);
state.tstate = 1;
}
memset(linebf, 0, LBFS);
hfd = open(histpath, OREAD);
if(hfd < 0){
toprompt("# NO GLOBAL HISTORY", 19);
state.hop = 0;
}
/* history up */
if(state.hop > 0){
int lbc = LBFS - 1;
for(hc = state.tpos; hc >= 0; hc--){
pread(hfd, linebf+lbc, 1, hc-1);
if(linebf[lbc] == '\n' || hc == 0){
if(hc == 0){
state.tpos = 0;
}
else {
state.tpos = hc - 1;
}
if(lbc == LBFS - 1){
continue;
}
if(state.hop > 1){
state.hop--;
lbc = LBFS - 1;
continue;
}
/* skip displaying commands without filter string */
if(filtercheck(linebf+lbc+1, LBFS-lbc-1, state.pfilter)){
toprompt(linebf+lbc+1, LBFS-lbc-1);
break;
} else {
lbc = LBFS - 1;
continue;
}
}
lbc--;
}
/* no more history, set prompt alert */
if(state.tpos == 0 && hc < 0){
toprompt("# END OF GLOBAL HISTORY", 23);
state.hop = 0;
}
}
/* history down */
if(state.hop < 0){
int lc = 0;
for(hc = state.tpos; ; hc++){
fr = pread(hfd, linebf+lc, 1, hc);
if(fr == 0){
/* no more history */
if(uselocal){
/* switch to local history if configured */
toprompt("# START OF LOCAL HISTORY", 24);
state.tstate = 0;
state.tsrc = 1;
} else {
/* reset prompt */
resetprompt();
}
break;
}
if(linebf[lc] == '\n'){
state.tpos = hc + 1;
if(lc == 0){
continue;
}
if(state.hop < -1){
state.hop++;
lc = 0;
continue;
}
/* skip displaying commands without filter string */
if(filtercheck(linebf, lc, state.pfilter)){
toprompt(linebf, lc);
break;
} else {
lc = 0;
continue;
}
}
lc++;
}
}
close(hfd);
free(home);
}
}
static int
processkbd(char *s)
{
/* process keyboard input */
/* inspired by riow and shortcuts */
char b[128], *p;
int n, o, skip;
Rune r;
o = 0;
b[o++] = *s;
/* mod key */
if(*s == 'k' || *s == 'K'){
mod = 0;
if(utfrune(s+1, Kctl) != nil){
mod |= Kctl;
}
}
for(p = s+1; *p != 0; p += n){
if((n = chartorune(&r, p)) == 1 && r == Runeerror){
/* bail out */
n = strlen(p);
memmove(b+o, p, n);
o += n;
break;
}
skip = 0;
/* react to key combinations */
if(*s == 'k'){
/* note: instead of checking for character key ('c') */
/* we're checking for control key down ('k'), since practical */
/* test showed the control and key combo is received regardles */
/* if tested on hardware or drawterm on any system, while that */
/* is not always true for the individual characters. */
/* This makes it work over drawterm in any case! */
/* react with history operations */
if(mod == Kctl){
if(r == Kup){
processhist(1);
skip = 1;
}
if(r == Kdown){
processhist(-1);
skip = 1;
}
}
/* reset history tracking and state if command entered or canceled */
if(r == 10 || r == 127){
/* 10 - enter key */
/* 127 - delete key */
resethstate();
}
}
/* quirk handling */
if(handlequirk > 0){
/* quirk: handle drawterm compiled for wayland on linux */
/* where it was observed that an extra character */
/* is submitted (7 or BELL when pressing CTRL + DOWN) */
/* or (8 or BACKSPACE when pressing CTRL + UP). */
/* In that instance, skip those characters. */
if (mod > 0){
if(*s == 'c'){
if(r == 7){
skip = 1;
}
if(r == 8){
skip = 1;
}
}
}
}
if(!skip){
memmove(b+o, p, n);
o += n;
}
}
/* move the keyboard chars along to the next one in the chain */
b[o++] = 0;
return (o > 1 && write(1, b, o) <= 0) ? -1 : 0;
}
static void
usage(void)
{
fprint(2, "usage: %s [-g | -G] [-q]\n", argv0);
exits("usage");
}
void
main(int argc, char **argv)
{
uselocal = 1;
useglobal = 0;
handlequirk = 0;
ARGBEGIN{
case 'g':
useglobal = 1;
break;
case 'G':
uselocal = 0;
useglobal = 1;
break;
case 'q':
handlequirk = 1;
break;
default:
usage();
}ARGEND
wsysfd = open("/dev/wsys", OREAD);
if(wsysfd < 0){
sysfatal("%r");