forked from zsh-users/zsh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zle_main.c
2308 lines (2048 loc) · 54.1 KB
/
zle_main.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
/*
* zle_main.c - main routines for line editor
*
* This file is part of zsh, the Z shell.
*
* Copyright (c) 1992-1997 Paul Falstad
* All rights reserved.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and to distribute modified versions of this software for any
* purpose, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* In no event shall Paul Falstad or the Zsh Development Group be liable
* to any party for direct, indirect, special, incidental, or consequential
* damages arising out of the use of this software and its documentation,
* even if Paul Falstad and the Zsh Development Group have been advised of
* the possibility of such damage.
*
* Paul Falstad and the Zsh Development Group specifically disclaim any
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose. The software
* provided hereunder is on an "as is" basis, and Paul Falstad and the
* Zsh Development Group have no obligation to provide maintenance,
* support, updates, enhancements, or modifications.
*
*/
#include "zle.mdh"
#include "zle_main.pro"
#ifdef HAVE_POLL_H
# include <poll.h>
#endif
#if defined(HAVE_POLL) && !defined(POLLIN) && !defined(POLLNORM)
# undef HAVE_POLL
#endif
/* The input line assembled so far */
/**/
mod_export ZLE_STRING_T zleline;
/* Cursor position and line length in zle */
/**/
mod_export int zlecs, zlell;
/* != 0 if in a shell function called from completion, such that read -[cl] *
* will work (i.e., the line is metafied, and the above word arrays are OK). */
/**/
mod_export int incompctlfunc;
/* != 0 if completion module is loaded */
/**/
mod_export int hascompmod;
/* Increment for each nested recursive-edit */
/**/
mod_export int zle_recursive;
/* ZLRF_* flags passed to zleread() */
/**/
int zlereadflags;
/* ZLCON_* flags passed to zleread() */
/**/
int zlecontext;
/* != 0 if we're done editing */
/**/
int done;
/* location of mark */
/**/
int mark;
/*
* Status ($?) saved before function entry. This is the
* status we need to use in prompts.
*/
/**/
static int pre_zle_status;
/*
* Last character pressed.
*
* Depending how far we are with processing, the lastcharacter may
* be a single byte read (lastchar_wide_valid is 0, lastchar_wide is not
* valid) or a full wide character. This is needed because we can't be
* sure whether the user is typing old \M-style commands or multibyte
* input.
*
* Calling getfullchar or getrestchar is guaranteed to ensure we have
* a valid wide character (although this may be WEOF). In many states
* we know this and don't need to test lastchar_wide_valid.
*/
/**/
mod_export int
lastchar;
#ifdef MULTIBYTE_SUPPORT
/**/
mod_export ZLE_INT_T lastchar_wide;
/**/
mod_export int
lastchar_wide_valid;
#endif
/* the bindings for the previous and for this key */
/**/
mod_export Thingy lbindk, bindk;
/* insert mode/overwrite mode flag */
/**/
int insmode;
/**/
mod_export int eofchar;
static int eofsent;
/*
* Key timeout in hundredths of a second: we use time_t so
* that we only have the limits on one integer type to worry about.
*/
static time_t keytimeout;
#if defined(HAVE_SELECT) || defined(HAVE_POLL)
/* Terminal baud rate */
static int baud;
static long costmult;
#endif
/* flags associated with last command */
/**/
mod_export int lastcmd;
/**/
mod_export Widget compwidget;
/* the status line, a null-terminated metafied string */
/**/
mod_export char *statusline;
/* The current history line and cursor position for the top line *
* on the buffer stack. */
/**/
int stackhist, stackcs;
/* position in undo stack from when the current vi change started */
/**/
zlong vistartchange;
/* current modifier status */
/**/
mod_export struct modifier zmod;
/* Current command prefix status. This is normally 0. Prefixes set *
* this to 1. Each time round the main loop, this is checked: if it *
* is 0, the modifier status is reset; if it is 1, the modifier *
* status is left unchanged, and this flag is reset to 0. The *
* effect is that several prefix commands can be executed, and have *
* cumulative effect, but any other command execution will clear the *
* modifiers. */
/**/
int prefixflag;
/* Number of characters waiting to be read by the ungetbytes mechanism */
/**/
int kungetct;
/**/
mod_export char *zlenoargs[1] = { NULL };
static char **raw_lp, **raw_rp;
/*
* File descriptors we are watching as well as the terminal fd.
* These are all for reading; we don't watch for writes or exceptions.
*/
/**/
int nwatch; /* Number of fd's we are watching */
/*
* Array of nwatch structures.
*/
/**/
Watch_fd watch_fds;
/* set up terminal */
/**/
mod_export void
zsetterm(void)
{
struct ttyinfo ti;
if (fetchttyinfo) {
/*
* User requested terminal to be returned to normal use,
* so remember the terminal settings if not frozen.
*/
if (!ttyfrozen)
gettyinfo(&shttyinfo);
fetchttyinfo = 0;
}
/* sanitize the tty */
#ifdef HAS_TIO
shttyinfo.tio.c_lflag |= ICANON | ECHO;
# ifdef FLUSHO
shttyinfo.tio.c_lflag &= ~FLUSHO;
# endif
#else /* not HAS_TIO */
shttyinfo.sgttyb.sg_flags = (shttyinfo.sgttyb.sg_flags & ~CBREAK) | ECHO;
shttyinfo.lmodes &= ~LFLUSHO;
#endif
attachtty(mypgrp);
ti = shttyinfo;
#ifdef HAS_TIO
if (unset(FLOWCONTROL))
ti.tio.c_iflag &= ~IXON;
ti.tio.c_lflag &= ~(ICANON | ECHO
# ifdef FLUSHO
| FLUSHO
# endif
);
# ifdef TAB3
ti.tio.c_oflag &= ~TAB3;
# else
# ifdef OXTABS
ti.tio.c_oflag &= ~OXTABS;
# else
# ifdef XTABS
ti.tio.c_oflag &= ~XTABS;
# endif
# endif
# endif
#ifdef ONLCR
ti.tio.c_oflag |= ONLCR;
#endif
ti.tio.c_cc[VQUIT] =
# ifdef VDISCARD
ti.tio.c_cc[VDISCARD] =
# endif
# ifdef VSUSP
ti.tio.c_cc[VSUSP] =
# endif
# ifdef VDSUSP
ti.tio.c_cc[VDSUSP] =
# endif
# ifdef VSWTCH
ti.tio.c_cc[VSWTCH] =
# endif
# ifdef VLNEXT
ti.tio.c_cc[VLNEXT] =
# endif
VDISABLEVAL;
# if defined(VSTART) && defined(VSTOP)
if (unset(FLOWCONTROL))
ti.tio.c_cc[VSTART] = ti.tio.c_cc[VSTOP] = VDISABLEVAL;
# endif
eofchar = ti.tio.c_cc[VEOF];
ti.tio.c_cc[VMIN] = 1;
ti.tio.c_cc[VTIME] = 0;
ti.tio.c_iflag |= (INLCR | ICRNL);
/* this line exchanges \n and \r; it's changed back in getbyte
so that the net effect is no change at all inside the shell.
This double swap is to allow typeahead in common cases, eg.
% bindkey -s '^J' 'echo foo^M'
% sleep 10
echo foo<return> <--- typed before sleep returns
The shell sees \n instead of \r, since it was changed by the kernel
while zsh wasn't looking. Then in getbyte() \n is changed back to \r,
and it sees "echo foo<accept line>", as expected. Without the double
swap the shell would see "echo foo\n", which is translated to
"echo fooecho foo<accept line>" because of the binding.
Note that if you type <line-feed> during the sleep the shell just sees
\n, which is translated to \r in getbyte(), and you just get another
prompt. For type-ahead to work in ALL cases you have to use
stty inlcr.
Unfortunately it's IMPOSSIBLE to have a general solution if both
<return> and <line-feed> are mapped to the same character. The shell
could check if there is input and read it before setting it's own
terminal modes but if we get a \n we don't know whether to keep it or
change to \r :-(
*/
#else /* not HAS_TIO */
ti.sgttyb.sg_flags = (ti.sgttyb.sg_flags | CBREAK) & ~ECHO & ~XTABS;
ti.lmodes &= ~LFLUSHO;
eofchar = ti.tchars.t_eofc;
ti.tchars.t_quitc =
ti.ltchars.t_suspc =
ti.ltchars.t_flushc =
ti.ltchars.t_dsuspc = ti.ltchars.t_lnextc = -1;
#endif
#if defined(TIOCOUTQ) && defined(HAVE_SELECT)
if (baud) { /**/
int n = 0;
while ((ioctl(SHTTY, TIOCOUTQ, (char *)&n) >= 0) && n) {
struct timeval tv;
tv.tv_sec = n / baud;
tv.tv_usec = ((n % baud) * 1000000) / baud;
select(0, NULL, NULL, NULL, &tv);
}
}
#endif
settyinfo(&ti);
}
static char *kungetbuf;
static int kungetsz;
/*
* Note on ungetbyte and ungetbytes for the confused (pws):
* these are low level and deal with bytes before they
* have been converted into (possibly wide) characters.
* Hence the names.
*/
/**/
void
ungetbyte(int ch)
{
if (kungetct == kungetsz)
kungetbuf = realloc(kungetbuf, kungetsz *= 2);
kungetbuf[kungetct++] = ch;
}
/**/
void
ungetbytes(char *s, int len)
{
s += len;
while (len--)
ungetbyte(*--s);
}
/**/
void
ungetbytes_unmeta(char *s, int len)
{
s += len;
while (len--) {
if (len && s[-2] == Meta) {
ungetbyte(*--s ^ 32);
len--;
s--;
} else
ungetbyte(*--s);
}
}
#if defined(pyr) && defined(HAVE_SELECT)
static int
breakread(int fd, char *buf, int n)
{
fd_set f;
FD_ZERO(&f);
FD_SET(fd, &f);
return (select(fd + 1, (SELECT_ARG_2_T) & f, NULL, NULL, NULL) == -1 ?
EOF : read(fd, buf, n));
}
# define read breakread
#endif
/*
* Possible forms of timeout.
*/
enum ztmouttp {
/* No timeout in use. */
ZTM_NONE,
/*
* Key timeout in use (do_keytmout flag set). If this goes off
* we return without anything being read.
*/
ZTM_KEY,
/*
* Function timeout in use (from timedfns list).
* If this goes off we call any functions which have reached
* the time and then continue processing.
*/
ZTM_FUNC,
/*
* Timeout hit the maximum allowed; if it fires we
* need to recalculate. As we may use poll() for the timeout,
* which takes an int value in milliseconds, we might need this
* for times long in the future. (We make no attempt to extend
* the range of time beyond that of time_t, however; that seems
* like a losing battle.)
*
* For key timeouts we just limit the value to
* ZMAXTIMEOUT; that's already absurdly large.
*
* The following is the maximum signed range over 1024 (2^10), which
* is a little more convenient than 1000, but done differently
* to avoid problems with unsigned integers. We assume 8-bit bytes;
* there's no general way to fix up if that's wrong.
*/
ZTM_MAX
#define ZMAXTIMEOUT ((time_t)1 << (sizeof(time_t)*8-11))
};
struct ztmout {
/* Type of timeout setting, see enum above */
enum ztmouttp tp;
/*
* Value for timeout in 100ths of a second if type is not ZTM_NONE.
*/
time_t exp100ths;
};
/*
* See if we need a timeout either for a key press or for a
* timed function.
*
* do_keytmout is passed down from getbyte() here. If it is positive,
* we use the keytimeout value, which is in 100ths of a second (directly
* set from the parameter). If it is negative, we use -(do_keytmout+1)
* (i.e. the one's complement, to allow a zero value to be set). This
* is only used when calling into zle from outside to specify an
* explicit timeout. This is also in 100ths of a second.
*/
static void
calc_timeout(struct ztmout *tmoutp, long do_keytmout, int full)
{
if (do_keytmout && (keytimeout > 0 || do_keytmout < 0)) {
if (do_keytmout < 0)
tmoutp->exp100ths = (time_t)-do_keytmout;
else if (keytimeout > ZMAXTIMEOUT * 100 /* 24 days for a keypress???? */)
tmoutp->exp100ths = ZMAXTIMEOUT * 100;
else
tmoutp->exp100ths = keytimeout;
tmoutp->tp = ZTM_KEY;
} else
tmoutp->tp = ZTM_NONE;
if (full && timedfns) {
for (;;) {
LinkNode tfnode = firstnode(timedfns);
Timedfn tfdat;
time_t diff, exp100ths;
if (!tfnode)
break;
tfdat = (Timedfn)getdata(tfnode);
diff = tfdat->when - time(NULL);
if (diff <= 0) {
/* Already due; call it and rescan. */
tfdat->func();
continue;
}
if (diff > ZMAXTIMEOUT) {
tmoutp->exp100ths = ZMAXTIMEOUT * 100;
tmoutp->tp = ZTM_MAX;
} else if (diff > 0) {
exp100ths = diff * 100;
if (tmoutp->tp != ZTM_KEY ||
exp100ths < tmoutp->exp100ths) {
tmoutp->exp100ths = exp100ths;
tmoutp->tp = ZTM_FUNC;
}
}
break;
}
/* In case we called a function which messed up the display... */
if (resetneeded)
zrefresh();
}
}
/* see calc_timeout for use of do_keytmout */
static int
raw_getbyte(long do_keytmout, char *cptr, int full)
{
int ret;
struct ztmout tmout;
#if defined(HAS_TIO) && \
(defined(sun) || (!defined(HAVE_POLL) && !defined(HAVE_SELECT)))
struct ttyinfo ti;
#endif
#ifndef HAVE_POLL
# ifdef HAVE_SELECT
fd_set foofd, errfd;
FD_ZERO(&errfd);
# endif
#endif
calc_timeout(&tmout, do_keytmout, full);
/*
* Handle timeouts and watched fd's. If a watched fd or a function
* timeout triggers we restart any key timeout. This is likely to
* be harmless: the combination is extremely rare and a function
* is likely to occupy the user for a little while anyway. We used
* to make timeouts take precedence, but we can't now that the
* timeouts may be external, so we may have both a permanent watched
* fd and a long-term timeout.
*/
if ((nwatch || tmout.tp != ZTM_NONE)) {
#if defined(HAVE_SELECT) || defined(HAVE_POLL)
int i, errtry = 0, selret;
# ifdef HAVE_POLL
int nfds;
struct pollfd *fds;
# endif
# if defined(HAS_TIO) && defined(sun)
/*
* Yes, I know this is complicated. Yes, I know we
* already have three bits of code to poll the terminal
* down below. No, I don't want to do this either.
* However, it turns out on certain OSes, specifically
* Solaris, that you can't poll typeahead for love nor
* money without actually trying to read it. But
* if we are trying to select (and we need to if we
* are watching other fd's) we won't pick that up.
* So we just try and read it without blocking in
* the time-honoured (i.e. absurdly baroque) termios
* fashion.
*/
gettyinfo(&ti);
ti.tio.c_cc[VMIN] = 0;
settyinfo(&ti);
winch_unblock();
ret = read(SHTTY, cptr, 1);
winch_block();
ti.tio.c_cc[VMIN] = 1;
settyinfo(&ti);
if (ret > 0)
return 1;
# endif
# ifdef HAVE_POLL
nfds = 1 + nwatch;
/* First pollfd is SHTTY, following are the nwatch fds */
fds = zalloc(sizeof(struct pollfd) * nfds);
fds[0].fd = SHTTY;
/*
* POLLIN, POLLIN, POLLIN,
* Keep those fd's POLLIN...
*/
fds[0].events = POLLIN;
for (i = 0; i < nwatch; i++) {
fds[i+1].fd = watch_fds[i].fd;
fds[i+1].events = POLLIN;
}
# endif
for (;;) {
# ifdef HAVE_POLL
int poll_timeout;
if (tmout.tp != ZTM_NONE)
poll_timeout = tmout.exp100ths * 10;
else
poll_timeout = -1;
winch_unblock();
selret = poll(fds, errtry ? 1 : nfds, poll_timeout);
winch_block();
# else
int fdmax = SHTTY;
struct timeval *tvptr;
struct timeval expire_tv;
FD_ZERO(&foofd);
FD_SET(SHTTY, &foofd);
if (!errtry) {
for (i = 0; i < nwatch; i++) {
int fd = watch_fds[i].fd;
if (FD_ISSET(fd, &errfd))
continue;
FD_SET(fd, &foofd);
if (fd > fdmax)
fdmax = fd;
}
}
FD_ZERO(&errfd);
if (tmout.tp != ZTM_NONE) {
expire_tv.tv_sec = tmout.exp100ths / 100;
expire_tv.tv_usec = (tmout.exp100ths % 100) * 10000L;
tvptr = &expire_tv;
}
else
tvptr = NULL;
winch_unblock();
selret = select(fdmax+1, (SELECT_ARG_2_T) & foofd,
NULL, NULL, tvptr);
winch_block();
# endif
/*
* Make sure a user interrupt gets passed on straight away.
*/
if (selret < 0 && (errflag || retflag || breaks || exit_pending))
break;
/*
* Try to avoid errors on our special fd's from
* messing up reads from the terminal. Try first
* with all fds, then try unsetting the special ones.
*/
if (selret < 0 && !errtry) {
errtry = 1;
continue;
}
if (selret == 0) {
zlong save_lastval;
/*
* Nothing ready and no error, so we timed out.
*/
switch (tmout.tp) {
case ZTM_NONE:
/* keeps compiler happy if not debugging */
#ifdef DEBUG
dputs("BUG: timeout fired with no timeout set.");
#endif
/* treat as if a key timeout triggered */
/*FALLTHROUGH*/
case ZTM_KEY:
/* Special value -2 signals nothing ready */
selret = -2;
break;
case ZTM_FUNC:
save_lastval = lastval;
while (firstnode(timedfns)) {
Timedfn tfdat = (Timedfn)getdata(firstnode(timedfns));
/*
* It's possible a previous function took
* a long time to run (though it can't
* call zle recursively), so recalculate
* the time on each iteration.
*/
time_t now = time(NULL);
if (tfdat->when > now)
break;
tfdat->func();
}
lastval = save_lastval;
/* Function may have messed up the display */
if (resetneeded)
zrefresh();
/* We need to recalculate the timeout */
/*FALLTHROUGH*/
case ZTM_MAX:
/*
* Reached the limit of our range, but not the
* actual timeout; recalculate the timeout.
* We're cheating with the key timeout here:
* if one clashed with a function timeout we
* reconsider the key timeout from scratch.
* The effect of this is microscopic.
*/
calc_timeout(&tmout, do_keytmout, full);
break;
}
/*
* If we handled the timeout successfully,
* carry on.
*/
if (selret == 0)
continue;
}
/* If error or unhandled timeout, give up. */
if (selret < 0)
break;
/*
* If there's user input handle it straight away.
* This improves the user's ability to handle exceptional
* conditions like runaway output.
*/
if (
# ifdef HAVE_POLL
(fds[0].revents & POLLIN)
# else
FD_ISSET(SHTTY, &foofd)
# endif
)
break;
if (nwatch && !errtry) {
/*
* Copy the details of the watch fds in case the
* user decides to delete one from inside the
* handler function.
*/
int lnwatch = nwatch;
Watch_fd lwatch_fds = zalloc(lnwatch*sizeof(struct watch_fd));
memcpy(lwatch_fds, watch_fds, lnwatch*sizeof(struct watch_fd));
for (i = 0; i < lnwatch; i++)
lwatch_fds[i].func = ztrdup(lwatch_fds[i].func);
for (i = 0; i < lnwatch; i++) {
Watch_fd lwatch_fd = lwatch_fds + i;
if (
# ifdef HAVE_POLL
(fds[i+1].revents & (POLLIN|POLLERR|POLLHUP|POLLNVAL))
# else
FD_ISSET(lwatch_fd->fd, &foofd) ||
FD_ISSET(lwatch_fd->fd, &errfd)
# endif
) {
/* Handle the fd. */
char *fdbuf;
{
char buf[BDIGBUFSIZE];
convbase(buf, lwatch_fd->fd, 10);
fdbuf = ztrdup(buf);
}
if (lwatch_fd->widget) {
zlecallhook(lwatch_fd->func, fdbuf);
zsfree(fdbuf);
} else {
LinkList funcargs = znewlinklist();
zaddlinknode(funcargs, ztrdup(lwatch_fd->func));
zaddlinknode(funcargs, fdbuf);
# ifdef HAVE_POLL
# ifdef POLLERR
if (fds[i+1].revents & POLLERR)
zaddlinknode(funcargs, ztrdup("err"));
# endif
# ifdef POLLHUP
if (fds[i+1].revents & POLLHUP)
zaddlinknode(funcargs, ztrdup("hup"));
# endif
# ifdef POLLNVAL
if (fds[i+1].revents & POLLNVAL)
zaddlinknode(funcargs, ztrdup("nval"));
# endif
# else
if (FD_ISSET(lwatch_fd->fd, &errfd))
zaddlinknode(funcargs, ztrdup("err"));
# endif
callhookfunc(lwatch_fd->func, funcargs, 0, NULL);
freelinklist(funcargs, freestr);
}
if (errflag) {
/* No sensible way of handling errors here */
errflag &= ~ERRFLAG_ERROR;
/*
* Paranoia: don't run the hooks again this
* time.
*/
errtry = 1;
}
}
}
/* Function may have invalidated the display. */
if (resetneeded)
zrefresh();
for (i = 0; i < lnwatch; i++)
zsfree(lwatch_fds[i].func);
zfree(lwatch_fds, lnwatch*sizeof(struct watch_fd));
# ifdef HAVE_POLL
/* Function may have added or removed handlers */
nfds = 1 + nwatch;
if (nfds > 1) {
fds = zrealloc(fds, sizeof(struct pollfd) * nfds);
for (i = 0; i < nwatch; i++) {
/*
* This is imperfect because it assumes fds[] and
* watch_fds[] remain in sync, which may be false
* if handlers are shuffled. However, it should
* be harmless (e.g., produce one extra pass of
* the loop) in the event they fall out of sync.
*/
if (fds[i+1].fd == watch_fds[i].fd &&
(fds[i+1].revents & (POLLERR|POLLHUP|POLLNVAL))) {
fds[i+1].events = 0; /* Don't poll this */
} else {
fds[i+1].fd = watch_fds[i].fd;
fds[i+1].events = POLLIN;
}
fds[i+1].revents = 0;
}
}
# endif
}
/* If looping, need to recalculate timeout */
calc_timeout(&tmout, do_keytmout, full);
}
# ifdef HAVE_POLL
zfree(fds, sizeof(struct pollfd) * nfds);
# endif
if (selret < 0)
return selret;
#else
# ifdef HAS_TIO
ti = shttyinfo;
ti.tio.c_lflag &= ~ICANON;
ti.tio.c_cc[VMIN] = 0;
ti.tio.c_cc[VTIME] = tmout.exp100ths / 10;
# ifdef HAVE_TERMIOS_H
tcsetattr(SHTTY, TCSANOW, &ti.tio);
# else
ioctl(SHTTY, TCSETA, &ti.tio);
# endif
winch_unblock();
ret = read(SHTTY, cptr, 1);
winch_block();
# ifdef HAVE_TERMIOS_H
tcsetattr(SHTTY, TCSANOW, &shttyinfo.tio);
# else
ioctl(SHTTY, TCSETA, &shttyinfo.tio);
# endif
return (ret <= 0) ? ret : *cptr;
# endif
#endif
}
winch_unblock();
ret = read(SHTTY, cptr, 1);
winch_block();
return ret;
}
/* see calc_timeout for use of do_keytmout */
/**/
mod_export int
getbyte(long do_keytmout, int *timeout, int full)
{
char cc;
unsigned int ret;
int die = 0, r, icnt = 0;
int old_errno = errno, obreaks = breaks;
if (timeout)
*timeout = 0;
#ifdef MULTIBYTE_SUPPORT
/*
* Reading a single byte always invalidates the status
* of lastchar_wide. We may fix this up in getrestchar
* if this is the last byte of a wide character.
*/
lastchar_wide_valid = 0;
#endif
if (kungetct)
ret = STOUC(kungetbuf[--kungetct]);
else {
for (;;) {
int q = queue_signal_level();
dont_queue_signals();
r = raw_getbyte(do_keytmout, &cc, full);
restore_queue_signals(q);
if (r == -2) {
/* timeout */
if (timeout)
*timeout = 1;
return lastchar = EOF;
}
if (r == 1)
break;
if (r == 0) {
/* The test for IGNOREEOF was added to make zsh ignore ^Ds
that were typed while commands are running. Unfortuantely
this caused trouble under at least one system (SunOS 4.1).
Here shells that lost their xterm (e.g. if it was killed
with -9) didn't fail to read from the terminal but instead
happily continued to read EOFs, so that the above read
returned with 0, and, with IGNOREEOF set, this caused
an infinite loop. The simple way around this was to add
the counter (icnt) so that this happens 20 times and than
the shell gives up (yes, this is a bit dirty...). */
if ((zlereadflags & ZLRF_IGNOREEOF) && icnt++ < 20)
continue;
stopmsg = 1;
zexit(1, 0);
}
icnt = 0;
if (errno == EINTR) {
die = 0;
if (!errflag && !retflag && !breaks && !exit_pending)
continue;
errflag &= ~ERRFLAG_ERROR;
breaks = obreaks;
errno = old_errno;
return lastchar = EOF;
} else if (errno == EWOULDBLOCK) {
fcntl(0, F_SETFL, 0);
} else if (errno == EIO && !die) {
ret = opts[MONITOR];
opts[MONITOR] = 1;
attachtty(mypgrp);
zrefresh(); /* kludge! */
opts[MONITOR] = ret;
die = 1;
} else if (errno != 0) {
zerr("error on TTY read: %e", errno);
stopmsg = 1;
zexit(1, 0);
}
}
if (cc == '\r') /* undo the exchange of \n and \r determined by */
cc = '\n'; /* zsetterm() */
else if (cc == '\n')
cc = '\r';
ret = STOUC(cc);
}
/*
* curvichg.buf is raw bytes, not wide characters, so is dealt
* with here.
*/
if (vichgflag) {
if (curvichg.bufptr == curvichg.bufsz)
curvichg.buf = realloc(curvichg.buf, curvichg.bufsz *= 2);
curvichg.buf[curvichg.bufptr++] = ret;
}
errno = old_errno;
return lastchar = ret;
}
/*
* Get a full character rather than just a single byte.
*/
/**/
mod_export ZLE_INT_T
getfullchar(int do_keytmout)
{
int inchar = getbyte((long)do_keytmout, NULL, 1);
#ifdef MULTIBYTE_SUPPORT
return getrestchar(inchar, NULL, NULL);
#else
return inchar;
#endif
}
/**/
#ifdef MULTIBYTE_SUPPORT
/*
* Get the remainder of a character if we support multibyte
* input strings. It may not require any more input, but
* we haven't yet checked. The character previously returned
* by getbyte() is passed down as inchar.
*/
/**/
mod_export ZLE_INT_T
getrestchar(int inchar, char *outstr, int *outcount)
{
char c = inchar;
wchar_t outchar;
int timeout;
static mbstate_t mbs;
/*
* We are guaranteed to set a valid wide last character,
* although it may be WEOF (which is technically not
* a wide character at all...)
*/
lastchar_wide_valid = 1;
if (outcount)
*outcount = 0;
if (inchar == EOF) {
/* End of input, so reset the shift state. */
memset(&mbs, 0, sizeof mbs);
return lastchar_wide = WEOF;
}