/
jove.c
2100 lines (1810 loc) · 43.4 KB
/
jove.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
/**************************************************************************
* This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
* provided by Jonathan and Jovehacks without charge and without *
* warranty. You may copy, modify, and/or distribute JOVE, provided that *
* this notice is included in all the source files and documentation. *
**************************************************************************/
/* Contains the main loop initializations, and some system dependent
type things, e.g. putting terminal in CBREAK mode, etc. */
#include "jove.h"
#include "fp.h"
#include "jctype.h"
#include "chars.h"
#include "disp.h"
#include "re.h" /* for dosearch() */
#include "reapp.h" /* for find_tag(), UseRE */
#include "sysprocs.h"
#include "rec.h"
#include "ask.h"
#include "extend.h"
#include "fmt.h"
#include "macros.h"
#include "marks.h"
#include "mouse.h"
#ifndef MAC /* Mac does without! */
# include "jpaths.h"
#endif
#include "proc.h"
#include "screen.h"
#include "ttystate.h"
#include "term.h"
#include "version.h"
#include "wind.h"
#include "commands.h"
#include "misc.h"
#ifdef IPROCS
# include "iproc.h"
#endif
#ifdef USE_SELECT
# include <sys/time.h>
# include "select.h"
#endif
#ifdef SCO /* ??? what is this for? */
# include <sys/stream.h>
# include <sys/ptem.h>
#endif
#include <signal.h>
#ifdef MAC
# include "mac.h"
#else /* !MAC */
# include <sys/stat.h>
#endif /* !MAC */
#ifdef MSDOS
# include <bios.h>
# include <dos.h>
# include <time.h>
# define SIGHUP 99
# ifdef OWCDOS
# include <malloc.h> /* for _heapgrow */
# endif
#endif /* MSDOS */
#ifdef WIN32
# undef CR /* sigh, used as a field name in some windows header! */
# include <windows.h> /* ??? is this needed? */
# undef FIONREAD /* This is defined but ioctl isn't so we cannot use it. */
#endif
#ifdef STACK_DECL /* provision for setting up appropriate stack */
STACK_DECL
#endif
private void
UnsetTerm proto((bool)),
DoKeys proto((bool firsttime)),
ShowKeyStrokes proto((void));
#ifdef NONBLOCKINGREAD
private void setblock proto((bool on));
#endif
#ifdef POSIX_SIGS
SIGHANDLERTYPE
setsighandler(signo, handler) /* simulate BSD's safe signal() */
int signo;
SIGHANDLERTYPE handler;
{
static struct sigaction act; /* static so unspecified fields are 0 */
struct sigaction oact;
act.sa_handler = handler;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, signo);
sigaction(signo, &act, &oact);
return oact.sa_handler;
}
#endif
bool TimeDisplayed = YES; /* is time actually displayed in modeline? */
char JoveFeatures[MAXCOLS]; /* VAR: list of compiled-in features */
#ifdef UNIX
/* set things up to update the modeline every UpdFreq seconds */
int UpdFreq = 30; /* VAR: how often to update modeline */
bool InSlowRead = NO;
void
SetClockAlarm(
bool unset /* unset alarm if none needed */
)
{
if (TimeDisplayed && UpdFreq != 0) {
(void) alarm((unsigned)(UpdFreq - (time((time_t *)NULL) % UpdFreq)));
} else if (unset) {
alarm((unsigned)0);
}
}
/* AlarmHandler gets all SIGALRMs. It decides, based on InWaitChar,
* whether this is an alarm for updating the mode line or for showing
* keystrokes. Theoretically, InWaitChar is a state variable that ought
* to be reset in complain and other exceptional cases, but waitchar
* is called often enough that it turns out to be self-correcting.
*/
private volatile bool InWaitChar = NO;
/*ARGSUSED*/
private SIGRESTYPE
AlarmHandler(
int UNUSED(junk) /* passed in on signal; of no interest */
)
{
int save_errno = errno; /* Subtle, but necessary! */
resetsighandler(SIGALRM, AlarmHandler);
if (InWaitChar) {
if (InSlowRead) {
InSlowRead = NO;
ShowKeyStrokes();
redisplay();
InSlowRead = YES;
InWaitChar = NO; /* might as well allow modeline updates */
SetClockAlarm(NO);
} else {
alarm((unsigned)1); /* try again later */
}
} else {
UpdModLine = YES;
if (InSlowRead) {
/* needed because of stupid BSD restartable I/O */
InSlowRead = NO;
redisplay();
InSlowRead = YES;
}
SetClockAlarm(NO);
}
errno = save_errno;
return SIGRESVALUE;
}
#endif /* UNIX */
bool stickymsg; /* the last message should stick around */
char NullStr[] = "";
jmp_buf mainjmp;
#ifdef USE_SELECT
fd_set global_fd; /* set of file descriptors of interest (for select) */
int global_maxfd;
#endif
/* paths */
/* VAR: directory path of machine-independent library with joverc, docs, etc */
char ShareDir[FILESIZE] = SHAREDIR;
/* VAR: directory/device to store tmp files */
char TmpDir[FILESIZE] = TMPDIR;
#ifdef SUBSHELL
char
Shell[FILESIZE] = DFLTSHELL, /* VAR: shell to use */
# ifdef MSFILESYSTEM
ShFlags[sizeof(ShFlags)] = "/c"; /* VAR: flags to shell */
# else
ShFlags[sizeof(ShFlags)] = "-c"; /* VAR: flags to shell */
# endif /* MSFILESYSTEM */
#endif
/* VAR: directory path of machine-dependent library (for Portsrv and Recover) */
#if defined(SUBSHELL) || defined(PIPEPROCS) || defined(RECOVER)
# define NEED_LIBDIR 1
char LibDir[FILESIZE] = LIBDIR;
#endif
/* finish: handle bad-news signals.
* It also handles internally generated requests for termination.
* For most values of code (signal types) an attempt
* is made to save the buffers for recovery by "recover".
* For most values of code, this routine stops JOVE
* by calling abort, which causes a core dump under UNIX.
*
* - code -1 is an internally generated request to die with an
* attempt to save the buffers. It had better not be the code
* for some real signal (it cannot be one under UNIX).
*
* - code 0 is an internally generated request to die quietly.
* It had better not be the code for some real signal
* (it cannot be one under UNIX). This is the only code
* for which buffers are not saved.
*
* - SIGHUP is caused by loss of connection. This code and
* code 0 are the only ones which don't cause an abort.
* Generated by OS and by JOVE itself.
*/
SIGRESTYPE
finish(int code)
{
#ifdef RECOVER
int save_errno = errno; /* Subtle, but necessary! */
#endif
bool DelTmps = YES; /* Usually we delete them. */
DisabledRedisplay = YES;
#ifndef MAC
UnsetTerm(NO);
#endif
#ifdef PIPEPROCS
kbd_kill(); /* kill the keyboard process */
#endif
#ifdef RECOVER
if (code != 0) {
static bool Crashing = NO; /* we are in the middle of crashing */
if (!Crashing) {
Crashing = YES;
lsave();
SyncRec();
writef("JOVE CRASH!! (code %d; last errno %d)\n",
code, save_errno);
if (ModBufs(YES)) {
writef("Your buffers have been saved.\n");
writef("Use \"jove -r\" to have a look at them.\n");
DelTmps = NO; /* Don't delete anymore. */
} else {
writef("No buffers needed saving: you didn't lose any work.\n");
}
} else {
writef("\r\nYou may have lost your work!\n");
}
}
#endif /* RECOVER */
flushscreen();
if (DelTmps) {
tmpremove();
#ifdef RECOVER
recremove();
#endif /* RECOVER */
}
#ifdef UNIX
if (code != 0 && code != SIGHUP) {
abort();
}
#endif /* UNIX */
EXIT(0);
/*NOTREACHED*/
}
/* SIGINT is caused by the user hitting the INTR key.
* We give him a choice of death or continuation.
*/
private SIGRESTYPE
handle_sigint(int code)
{
int save_errno = errno; /* Subtle, but necessary! */
char c;
#ifdef WIN32
c = FatalErrorMessage("Fatal interrupt encountered. Abort?");
#else /* !WIN32 */
# ifdef PIPEPROCS
bool started;
# endif
resetsighandler(SIGINT, handle_sigint);
f_mess("Abort (Type 'n' if you're not sure)? ");
Placur(ILI, jmin(CO - 2, calc_pos(mesgbuf, MAXCOLS)));
flushscreen();
# ifdef UNIX
# ifdef PIPEPROCS
started = kbd_stop();
# endif
/*
* Yuk! This doesn't deal with all cases, we really need a
* standard jove input routine that's lower than kbd_getch so
* that this can use it. The code that this replaces was even
* more ugly. What about nonblocking reads? -- MM.
*/
# ifdef NONBLOCKINGREAD
setblock(YES); /* turn blocking on (in case it was off) */
# endif
do {
c = 'n';
} while (read(0, (UnivPtr) &c, sizeof(c)) < 0 && RETRY_ERRNO(errno));
# ifdef PIPEPROCS
if (started) {
kbd_strt();
}
# endif /* PIPEPROCS */
# endif /* UNIX */
# ifdef MSDOS
c = getrawinchar();
# endif /* MSDOS */
message(NullStr);
#endif /* !WIN32 */
if (c == 'y') {
errno = save_errno;
finish(code);
/* NOTREACHED */
}
redisplay();
errno = save_errno;
return SIGRESVALUE;
}
private char smbuf[20],
*bp = smbuf;
private int nchars = 0;
#ifdef NONBLOCKINGREAD
private void
setblock( /* turn blocking on or off */
bool on
)
{
static int blockf, nonblockf;
static bool first = YES;
if (first) {
int flags;
first = NO;
if ((flags = fcntl(0, F_GETFL, 0)) == -1) {
finish(SIGHUP);
}
# ifdef O_NONBLOCK /* POSIX form */
blockf = flags & ~O_NONBLOCK; /* make sure O_NONBLOCK is off */
nonblockf = flags | O_NONBLOCK; /* make sure O_NONBLOCK is on */
# else /* pre-POSIX form */
blockf = flags & ~O_NDELAY; /* make sure O_NDELAY is off */
nonblockf = flags | O_NDELAY; /* make sure O_NDELAY is on */
# endif
}
if (fcntl(0, F_SETFL, on ? blockf : nonblockf) == -1) {
finish(SIGHUP);
}
}
#endif /* NONBLOCKINGREAD */
/* To optimize screen refreshing, we try to detect if there is pending input.
* If there is, we defer screen updating, hoping that when we eventually
* do an update it will be more efficient. To implement this, we use
* charp to poll for pending input (from any source but macro body).
* "InputPending" records the last value returned by charp, or what
* was known by kbd_getch or kbd_ungetch. Note that the kbd_* routines
* only consider keyboard input, but charp considers other sources of
* input. These are the only routines that should set InputPending
* (currently, a fudge to redisplay for the mac also sets it --
* this should be fixed).
*
* This heuristic confuses the user if a command takes a long time:
* the screen may be "frozen" in an inaccurate state until the command
* completes. The variable "SlowCmd" is a count of the nesting of slow
* commands. If it is positive, display updating is not pre-empted.
* The macros PreEmptOutput() and CheapPreEmptOutput() implement the tests.
*/
int SlowCmd = 0; /* depth of nesting of slow commands */
bool InputPending = NO; /* is there input waiting to be processed? */
/* Inputp is used to jam a NUL-terminated string into JOVE's input stream.
* It is used to feed each line of the joverc file, to fill in the default
* make_cmd in compile-it, and to fill in the default i-search string.
* To make this work, we prevent i-search and compile-it from using Inputp
* when it is already in use.
*/
char *Inputp = NULL;
/* kbd_ungetch must only be used to push back a character that
* was just returned by kbd_getch.
*/
private ZXchar kbdpeek = EOF;
void
kbd_ungetch(ZXchar c)
{
InputPending = YES;
kbdpeek = c;
}
ZXchar
kbd_getch(void)
{
ZXchar c;
if (kbdpeek != EOF) {
c = kbdpeek;
kbdpeek = EOF;
InputPending = nchars > 0;
return c;
}
#if NCHARS != UCHAR_ROOF
do {
#endif
while (nchars <= 0) {
bp = smbuf;
#ifdef MSDOS
*bp = getrawinchar();
nchars = 1;
#else /* !MSDOS */
# ifdef WIN32
if (UpdModLine || !charp()) {
redisplay();
flushscreen();
}
nchars = getInputEvents(smbuf, sizeof(smbuf));
# else /* !WIN32 */
# ifdef PTYPROCS
/* Get a character from the keyboard, first checking for
any input from a process. Handle that first, and then
deal with the terminal input. */
{
fd_set reads;
int
nfds,
fd;
bp = smbuf;
for (;;) {
while (procs_to_reap) {
reap_procs(); /* synchronous process reaping */
}
reads = global_fd;
InSlowRead = YES;
nfds = select(global_maxfd,
&reads, (fd_set *)NULL, (fd_set *)NULL,
(struct timeval *)NULL);
InSlowRead = NO;
if (nfds >= 0) {
break;
}
if (errno != EINTR) {
complain("\rerror in select: %s", strerror(errno));
/* NOTREACHED */
}
}
if (FD_ISSET(0, &reads)) {
do {
nchars = read(0, (UnivPtr) smbuf, sizeof(smbuf));
} while (nchars < 0 && errno == EINTR);
if (nchars <= 0) {
finish(SIGHUP);
}
nfds -= 1;
}
for (fd = 1; nfds != 0; fd += 1) {
if (FD_ISSET(fd, &reads)) {
nfds -= 1;
read_pty_proc(fd);
if (UpdModLine) {
redisplay();
}
}
}
}
# else /* !PTYPROCS */
# ifdef PIPEPROCS
if (NumProcs > 0) {
/* Handle process input until kbd input arrives */
struct header header;
size_t n;
InSlowRead = YES;
n = f_readn(ProcInput, (char *) &header, sizeof(header));
InSlowRead = NO;
if (n != sizeof(header)) {
raw_complain("\r\nError reading kbd process, expected %d, got %d bytes", sizeof header, n);
finish(SIGHUP);
}
if (header.pid == kbd_pid) {
size_t tmp_zu;
/* data is from the keyboard process */
tmp_zu = f_readn(ProcInput, smbuf, (size_t)header.nbytes);
if (tmp_zu > INT_MAX) {
fprintf(stderr, "fatal: %s:%d: tmp_zu > INT_MAX\n", __FILE__, __LINE__);
exit(1);
}
nchars = (int)tmp_zu;
if (nchars != header.nbytes) {
raw_complain("\r\nError reading kbd process, expected %d, got %d bytes.", header.nbytes, nchars);
finish(SIGHUP);
}
} else {
/* data is from an interactive process */
read_pipe_proc(header.pid, header.nbytes);
if (NumProcs == 0) {
(void) kbd_stop();
}
if (UpdModLine) {
redisplay();
}
}
} else /*...*/
# endif /* PIPEPROCS */
/*...*/ {
do {
# ifdef UNIX
InSlowRead = YES;
# endif
nchars = (int)read(0, (UnivPtr) smbuf, sizeof smbuf);
# ifdef UNIX
InSlowRead = NO;
# endif
} while (nchars < 0 && RETRY_ERRNO(errno));
if (nchars <= 0) {
finish(SIGHUP);
}
}
# endif /* !PTYPROCS */
# endif /* !WIN32 */
#endif /* !MSDOS */
}
c = ZXRC(*bp++);
#if !defined(PCNONASCII) && !defined(MAC) /* if not done elsewhere */
if ((c & METABIT) && MetaKey) {
*--bp = (char)(c & ~METABIT);
nchars += 1;
c = ESC;
}
#endif /* !defined(PCNONASCII) && !defined(MAC) */
InputPending = --nchars > 0;
#if NCHARS != UCHAR_ROOF
} while (c >= NCHARS); /* discard c if it is a bad char */
#endif
return c;
}
/* Returns YES if a character waiting (excluding macro body) */
bool
charp(void)
{
if (InJoverc != 0 || kbdpeek != EOF || nchars > 0 || Inputp != NULL) {
return InputPending = YES;
}
#ifdef FIONREAD
{
/*
* Some manual pages, notably SunOS4.1.3 say 'c' should be
* 'long', but that's a lie -- it's an 'int' according to all
* kernels I've seen (including SunOS4.1.3) and most other
* manual pages. At any rate, 'int' works correctly on 32- and
* 64-bit architectures, whereas long breaks on the 64
* bitters. -- MM.
*/
int c;
if (ioctl(0, FIONREAD, (UnivPtr) &c) == -1) {
c = 0;
}
return InputPending = c > 0;
}
#else /* !FIONREAD */
# ifdef NONBLOCKINGREAD
setblock(NO); /* turn blocking off */
nchars = read(0, (UnivPtr) smbuf, sizeof smbuf); /* Is anything there? */
setblock(YES); /* turn blocking on */
bp = smbuf; /* make sure bp points to it */
return InputPending = nchars > 0; /* just say we found something */
# else /* !NONBLOCKINGREAD */
# ifdef USE_SELECT
{
struct timeval timer;
fd_set readfds;
timer.tv_sec = 0;
timer.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(0, &readfds);
return InputPending = select(1,
&readfds, (fd_set *)NULL, (fd_set *)NULL,
&timer) > 0;
}
# else /* !USE_SELECT */
# ifdef MSDOS
return InputPending = rawkey_ready();
# else /* !MSDOS */
# ifdef MAC
return InputPending = rawchkc();
# else
# ifdef WIN32
return InputPending = inputEventWaiting(0);
# else
return InputPending = NO; /* who knows? */
# endif /* !WIN32 */
# endif /* !MAC */
# endif /* !MSDOS */
# endif /* !USE_SELECT */
# endif /* !NONBLOCKINGREAD */
#endif /* !FIONREAD */
}
/*
* Tries to pause for delay/10 seconds OR until a character is typed at the
* keyboard. This works well on systems with select() and not so well on the
* rest.
*/
#ifdef MAC
# include <LoMem.h> /* defines Ticks */
#endif
void
SitFor(int delay)
{
#ifdef MAC
long
start,
end;
Keyonly = YES;
redisplay();
start = Ticks;
end = start + delay * 6;
do {} while (!charp() && Ticks < end);
#else /* !MAC */
# ifndef MSDOS
if (!charp()) {
# ifdef USE_SELECT
struct timeval timer;
fd_set readfds;
/* So messages that aren't error messages don't
* hang around forever.
* Gross that I had to snarf this from getch()
*/
if (!UpdMesg && !Asking && mesgbuf[0] && !stickymsg) {
message(NullStr);
}
redisplay();
timer.tv_sec = (delay / 10);
timer.tv_usec = (delay % 10) * 100000;
FD_ZERO(&readfds);
FD_SET(0, &readfds);
(void) select(1,
&readfds, (fd_set *)NULL, (fd_set *)NULL,
&timer);
# else /* ! USE_SELECT */
# ifdef WIN32
redisplay();
inputEventWaiting(delay * 100);
# else /* ! WIN32 */
/* Pause by spitting NULs at the terminal. Ugh! */
static const int cps[] = {
0,
5,
7,
11,
13,
15,
20,
30,
60,
120,
180,
240,
480,
960,
1920,
1920,
};
int lnchars,
check_cnt;
lnchars = (delay * cps[ospeed]) / 10;
check_cnt = ScrBufSize;
redisplay();
if (!NP) {
while ((--lnchars > 0) && !InputPending) {
scr_putchar(PC);
if (--check_cnt == 0) {
check_cnt = ScrBufSize;
(void) charp();
}
}
}
# endif /* !WIN32 */
# endif /* !USE_SELECT */
}
# else /* MSDOS */
/* All time representations must wrap eventually.
* Since all delays are much less than a minute, we represent
* time as hundredths of a second past the minute we start.
* NOTE: this is a busy wait. I know of no alternative.
*/
int start,
now,
end;
struct dostime_t tc;
redisplay();
_dos_gettime(&tc);
start = tc.second * 100 + tc.hsecond;
end = start + delay * 10;
for (;;) {
if (charp()) {
break;
}
_dos_gettime(&tc);
now = tc.second * 100 + tc.hsecond;
if (now < start) {
now += 60 * 100; /* must be in next minute */
}
if (now >= end) {
break;
}
}
# endif /* MSDOS */
#endif /* !MAC */
}
#define WAITCHAR_CURSOR_DOWN 1 /* during slow keying, cursor is after displayed prefix */
private char
key_strokes[100],
*keys_p = key_strokes;
private bool in_ask_ks;
private volatile bool slow_keying = NO; /* for waitchar() */
void
cmd_sync(void)
{
if (this_cmd != ARG_CMD) {
clr_arg_value();
last_cmd = this_cmd;
slow_keying = NO;
in_ask_ks = NO;
keys_p = key_strokes;
}
}
ZXchar
ask_ks(void)
{
in_ask_ks = YES;
keys_p = key_strokes;
return waitchar();
}
void
add_stroke(ZXchar c)
{
if (keys_p < &key_strokes[sizeof(key_strokes) - 1]) {
*keys_p++ = (char)c;
}
}
void
pp_key_strokes(buffer, size)
char *buffer;
size_t size;
{
char
*buf_end = buffer + size - 1,
*kp = key_strokes;
*buffer = '\0';
while (kp != keys_p) {
swritef(buffer, (size_t)(buf_end - buffer), "%p ", *kp++);
buffer += strlen(buffer);
}
}
private void
ShowKeyStrokes(void)
{
char buffer[100];
slow_keying = YES;
pp_key_strokes(buffer, sizeof(buffer));
f_mess(in_ask_ks ? ": %f %s" : "%s", buffer);
#ifdef WAITCHAR_CURSOR_DOWN
Asking = YES;
AskingWidth = (int)strlen(mesgbuf);
#endif
}
#define N_SEC 1 /* will be precisely 1 second on 4.2 */
ZXchar
waitchar(void)
{
ZXchar c;
#ifdef WAITCHAR_CURSOR_DOWN
bool oldAsking;
INTPTR_T oldAskingWidth;
#endif
/* short circuit, if we can */
if (InJoverc || (!Interactive && in_macro()) || InputPending) {
return getch();
}
#ifdef WAITCHAR_CURSOR_DOWN
oldAsking = Asking;
oldAskingWidth = AskingWidth;
#endif
#ifdef MAC
Keyonly = YES;
#endif
if (!slow_keying) {
/* not yet slow_keying */
#ifdef UNIX
/* set up alarm */
InWaitChar = YES;
(void) alarm((unsigned) N_SEC);
#else /* !UNIX */
# ifdef WIN32
if (!charp()) {
if ((slow_keying = !inputEventWaiting(N_SEC * 1000))) {
ShowKeyStrokes();
}
}
# else /* !WIN32 */
/* NOTE: busy wait (until char typed or timeout)! */
time_t sw = N_SEC + time((time_t *)NULL);
while (!slow_keying && !charp()) {
if (time((time_t *)NULL) > sw) {
/* transition to slow_keying */
# ifdef MAC
menus_off();
# endif
ShowKeyStrokes();
}
}
# endif /* !WIN32 */
#endif /* !UNIX */
#ifdef WAITCHAR_CURSOR_DOWN
} else {
/* Already slow_keying: presume bottom line has old keystrokes.
* Tell refresh(?) to place cursor at end of them.
*/
Asking = YES;
AskingWidth = (int)strlen(mesgbuf);
#endif
}
c = getch();
if (slow_keying) {
ShowKeyStrokes();
#ifdef UNIX
} else {
/* not yet slow_keying: tear down alarm */
InWaitChar = NO;
SetClockAlarm(YES);
#endif
}
#ifdef WAITCHAR_CURSOR_DOWN
Asking = oldAsking;
AskingWidth = oldAskingWidth;
#endif
return c;
}
private void
SetTerm(void)
{
#ifdef IBMPCDOS
pcSetTerm();
#endif
ttysetattr(YES);
#ifdef TERMCAP
putpad(TI, 1); /* Cursor addressing start */
putpad(VS, 1); /* Visual start */
putpad(KS, 1); /* Keypad mode start */
# ifdef MOUSE
MouseOn();
# endif
#endif
#ifdef UNIX
(void) chkmail(YES); /* force it to check so we can be accurate */
#endif
}
private void
UnsetTerm(bool WarnUnwritten)
{
#ifdef TERMCAP
# ifdef ID_CHAR
INSmode(NO);
# endif /* ID_CHAR */
# ifdef MOUSE
MouseOff();
# endif
putpad(KE, 1);
putpad(VE, 1);
Placur(ILI, 0);
putpad(CE, 1);
putpad(TE, 1);
#else /* !TERMCAP */
Placur(ILI, 0);
clr_eoln();
#endif /* !TERMCAP */
flushscreen();
#ifdef MSDOS
pcUnsetTerm();
#endif
ttysetattr(NO);
if (WarnUnwritten && ModBufs(NO)) {
raw_complain("[There are modified buffers]");
}
}