-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos_unix.c
7995 lines (7299 loc) · 186 KB
/
os_unix.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
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
* OS/2 port by Paul Slootman
* VMS merge by Zoltan Arpadffy
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* os_unix.c -- code for all flavors of Unix (BSD, SYSV, SVR4, POSIX, ...)
* Also for OS/2, using the excellent EMX package!!!
* Also for BeOS and Atari MiNT.
*
* A lot of this file was originally written by Juergen Weigert and later
* changed beyond recognition.
*/
/*
* Some systems have a prototype for select() that has (int *) instead of
* (fd_set *), which is wrong. This define removes that prototype. We define
* our own prototype below.
* Don't use it for the Mac, it causes a warning for precompiled headers.
* TODO: use a configure check for precompiled headers?
*/
#if !defined(__APPLE__) && !defined(__TANDEM)
# define select select_declared_wrong
#endif
#include "vim.h"
#ifdef FEAT_MZSCHEME
# include "if_mzsch.h"
#endif
#include "os_unixx.h" /* unix includes for os_unix.c only */
#ifdef USE_XSMP
# include <X11/SM/SMlib.h>
#endif
#ifdef HAVE_SELINUX
# include <selinux/selinux.h>
static int selinux_enabled = -1;
#endif
#ifdef HAVE_SMACK
# include <attr/xattr.h>
# include <linux/xattr.h>
# ifndef SMACK_LABEL_LEN
# define SMACK_LABEL_LEN 1024
# endif
#endif
/*
* Use this prototype for select, some include files have a wrong prototype
*/
#ifndef __TANDEM
# undef select
# ifdef __BEOS__
# define select beos_select
# endif
#endif
#ifdef __CYGWIN__
# ifndef WIN32
# include <cygwin/version.h>
# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
* for cygwin_conv_path() */
# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include "winclip.pro"
# endif
# endif
#endif
#if defined(HAVE_SELECT)
extern int select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
#endif
#ifdef FEAT_MOUSE_GPM
# include <gpm.h>
/* <linux/keyboard.h> contains defines conflicting with "keymap.h",
* I just copied relevant defines here. A cleaner solution would be to put gpm
* code into separate file and include there linux/keyboard.h
*/
/* #include <linux/keyboard.h> */
# define KG_SHIFT 0
# define KG_CTRL 2
# define KG_ALT 3
# define KG_ALTGR 1
# define KG_SHIFTL 4
# define KG_SHIFTR 5
# define KG_CTRLL 6
# define KG_CTRLR 7
# define KG_CAPSSHIFT 8
static void gpm_close(void);
static int gpm_open(void);
static int mch_gpm_process(void);
#endif
#ifdef FEAT_SYSMOUSE
# include <sys/consio.h>
# include <sys/fbio.h>
static int sysmouse_open(void);
static void sysmouse_close(void);
static RETSIGTYPE sig_sysmouse SIGPROTOARG;
#endif
/*
* end of autoconf section. To be extended...
*/
/* Are the following #ifdefs still required? And why? Is that for X11? */
#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
# ifdef SIGWINCH
# undef SIGWINCH
# endif
# ifdef TIOCGWINSZ
# undef TIOCGWINSZ
# endif
#endif
#if defined(SIGWINDOW) && !defined(SIGWINCH) /* hpux 9.01 has it */
# define SIGWINCH SIGWINDOW
#endif
#ifdef FEAT_X11
# include <X11/Xlib.h>
# include <X11/Xutil.h>
# include <X11/Xatom.h>
# ifdef FEAT_XCLIPBOARD
# include <X11/Intrinsic.h>
# include <X11/Shell.h>
# include <X11/StringDefs.h>
static Widget xterm_Shell = (Widget)0;
static void clip_update(void);
static void xterm_update(void);
# endif
# if defined(FEAT_XCLIPBOARD) || defined(FEAT_TITLE)
Window x11_window = 0;
# endif
Display *x11_display = NULL;
# ifdef FEAT_TITLE
static int get_x11_windis(void);
static void set_x11_title(char_u *);
static void set_x11_icon(char_u *);
# endif
#endif
#ifdef FEAT_TITLE
static int get_x11_title(int);
static int get_x11_icon(int);
static char_u *oldtitle = NULL;
static int did_set_title = FALSE;
static char_u *oldicon = NULL;
static int did_set_icon = FALSE;
#endif
static void may_core_dump(void);
#ifdef HAVE_UNION_WAIT
typedef union wait waitstatus;
#else
typedef int waitstatus;
#endif
static pid_t wait4pid(pid_t, waitstatus *);
static int WaitForChar(long msec, int *interrupted, int ignore_input);
static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
#if defined(__BEOS__) || defined(VMS)
int RealWaitForChar(int, long, int *, int *interrupted);
#else
static int RealWaitForChar(int, long, int *, int *interrupted);
#endif
#ifdef FEAT_XCLIPBOARD
static int do_xterm_trace(void);
# define XT_TRACE_DELAY 50 /* delay for xterm tracing */
#endif
static void handle_resize(void);
#if defined(SIGWINCH)
static RETSIGTYPE sig_winch SIGPROTOARG;
#endif
#if defined(SIGINT)
static RETSIGTYPE catch_sigint SIGPROTOARG;
#endif
#if defined(SIGPWR)
static RETSIGTYPE catch_sigpwr SIGPROTOARG;
#endif
#if defined(SIGALRM) && defined(FEAT_X11) \
&& defined(FEAT_TITLE) && !defined(FEAT_GUI_GTK)
# define SET_SIG_ALARM
static RETSIGTYPE sig_alarm SIGPROTOARG;
/* volatile because it is used in signal handler sig_alarm(). */
static volatile int sig_alarm_called;
#endif
static RETSIGTYPE deathtrap SIGPROTOARG;
static void catch_int_signal(void);
static void set_signals(void);
static void catch_signals(RETSIGTYPE (*func_deadly)(), RETSIGTYPE (*func_other)());
#ifdef HAVE_SIGPROCMASK
# define SIGSET_DECL(set) sigset_t set;
# define BLOCK_SIGNALS(set) block_signals(set)
# define UNBLOCK_SIGNALS(set) unblock_signals(set)
#else
# define SIGSET_DECL(set)
# define BLOCK_SIGNALS(set) do { /**/ } while (0)
# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
#endif
static int have_wildcard(int, char_u **);
static int have_dollars(int, char_u **);
static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
#ifndef SIG_ERR
# define SIG_ERR ((RETSIGTYPE (*)())-1)
#endif
/* volatile because it is used in signal handler sig_winch(). */
static volatile int do_resize = FALSE;
static char_u *extra_shell_arg = NULL;
static int show_shell_mess = TRUE;
/* volatile because it is used in signal handler deathtrap(). */
static volatile int deadly_signal = 0; /* The signal we caught */
/* volatile because it is used in signal handler deathtrap(). */
static volatile int in_mch_delay = FALSE; /* sleeping in mch_delay() */
#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
static int dont_check_job_ended = 0;
#endif
static int curr_tmode = TMODE_COOK; /* contains current terminal mode */
#ifdef USE_XSMP
typedef struct
{
SmcConn smcconn; /* The SM connection ID */
IceConn iceconn; /* The ICE connection ID */
char *clientid; /* The client ID for the current smc session */
Bool save_yourself; /* If we're in the middle of a save_yourself */
Bool shutdown; /* If we're in shutdown mode */
} xsmp_config_T;
static xsmp_config_T xsmp;
#endif
#ifdef SYS_SIGLIST_DECLARED
/*
* I have seen
* extern char *_sys_siglist[NSIG];
* on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
* that describe the signals. That is nearly what we want here. But
* autoconf does only check for sys_siglist (without the underscore), I
* do not want to change everything today.... jw.
* This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
*/
#endif
static struct signalinfo
{
int sig; /* Signal number, eg. SIGSEGV etc */
char *name; /* Signal name (not char_u!). */
char deadly; /* Catch as a deadly signal? */
} signal_info[] =
{
#ifdef SIGHUP
{SIGHUP, "HUP", TRUE},
#endif
#ifdef SIGQUIT
{SIGQUIT, "QUIT", TRUE},
#endif
#ifdef SIGILL
{SIGILL, "ILL", TRUE},
#endif
#ifdef SIGTRAP
{SIGTRAP, "TRAP", TRUE},
#endif
#ifdef SIGABRT
{SIGABRT, "ABRT", TRUE},
#endif
#ifdef SIGEMT
{SIGEMT, "EMT", TRUE},
#endif
#ifdef SIGFPE
{SIGFPE, "FPE", TRUE},
#endif
#ifdef SIGBUS
{SIGBUS, "BUS", TRUE},
#endif
#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
/* MzScheme uses SEGV in its garbage collector */
{SIGSEGV, "SEGV", TRUE},
#endif
#ifdef SIGSYS
{SIGSYS, "SYS", TRUE},
#endif
#ifdef SIGALRM
{SIGALRM, "ALRM", FALSE}, /* Perl's alarm() can trigger it */
#endif
#ifdef SIGTERM
{SIGTERM, "TERM", TRUE},
#endif
#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
{SIGVTALRM, "VTALRM", TRUE},
#endif
#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
/* MzScheme uses SIGPROF for its own needs; On Linux with profiling
* this makes Vim exit. WE_ARE_PROFILING is defined in Makefile. */
{SIGPROF, "PROF", TRUE},
#endif
#ifdef SIGXCPU
{SIGXCPU, "XCPU", TRUE},
#endif
#ifdef SIGXFSZ
{SIGXFSZ, "XFSZ", TRUE},
#endif
#ifdef SIGUSR1
{SIGUSR1, "USR1", TRUE},
#endif
#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
/* Used for sysmouse handling */
{SIGUSR2, "USR2", TRUE},
#endif
#ifdef SIGINT
{SIGINT, "INT", FALSE},
#endif
#ifdef SIGWINCH
{SIGWINCH, "WINCH", FALSE},
#endif
#ifdef SIGTSTP
{SIGTSTP, "TSTP", FALSE},
#endif
#ifdef SIGPIPE
{SIGPIPE, "PIPE", FALSE},
#endif
{-1, "Unknown!", FALSE}
};
int
mch_chdir(char *path)
{
if (p_verbose >= 5)
{
verbose_enter();
smsg((char_u *)"chdir(%s)", path);
verbose_leave();
}
# ifdef VMS
return chdir(vms_fixfilename(path));
# else
return chdir(path);
# endif
}
/* Why is NeXT excluded here (and not in os_unixx.h)? */
#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
# define NEW_TTY_SYSTEM
#endif
/*
* Write s[len] to the screen.
*/
void
mch_write(char_u *s, int len)
{
ignored = (int)write(1, (char *)s, len);
if (p_wd) /* Unix is too fast, slow down a bit more */
RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
}
/*
* mch_inchar(): low level input function.
* Get a characters from the keyboard.
* Return the number of characters that are available.
* If wtime == 0 do not wait for characters.
* If wtime == n wait a short time for characters.
* If wtime == -1 wait forever for characters.
*/
int
mch_inchar(
char_u *buf,
int maxlen,
long wtime, /* don't use "time", MIPS cannot handle it */
int tb_change_cnt)
{
int len;
int interrupted = FALSE;
int did_start_blocking = FALSE;
long wait_time;
long elapsed_time = 0;
#ifdef ELAPSED_FUNC
ELAPSED_TYPE start_tv;
ELAPSED_INIT(start_tv);
#endif
/* repeat until we got a character or waited long enough */
for (;;)
{
/* Check if window changed size while we were busy, perhaps the ":set
* columns=99" command was used. */
while (do_resize)
handle_resize();
#ifdef MESSAGE_QUEUE
parse_queued_messages();
/* If input was put directly in typeahead buffer bail out here. */
if (typebuf_changed(tb_change_cnt))
return 0;
#endif
if (wtime < 0 && did_start_blocking)
/* blocking and already waited for p_ut */
wait_time = -1;
else
{
if (wtime >= 0)
wait_time = wtime;
else
/* going to block after p_ut */
wait_time = p_ut;
#ifdef ELAPSED_FUNC
elapsed_time = ELAPSED_FUNC(start_tv);
#endif
wait_time -= elapsed_time;
if (wait_time < 0)
{
if (wtime >= 0)
/* no character available within "wtime" */
return 0;
if (wtime < 0)
{
/* no character available within 'updatetime' */
did_start_blocking = TRUE;
#ifdef FEAT_AUTOCMD
if (trigger_cursorhold() && maxlen >= 3
&& !typebuf_changed(tb_change_cnt))
{
buf[0] = K_SPECIAL;
buf[1] = KS_EXTRA;
buf[2] = (int)KE_CURSORHOLD;
return 3;
}
#endif
/*
* If there is no character available within 'updatetime'
* seconds flush all the swap files to disk.
* Also done when interrupted by SIGWINCH.
*/
before_blocking();
continue;
}
}
}
#ifdef FEAT_JOB_CHANNEL
/* Checking if a job ended requires polling. Do this every 100 msec. */
if (has_pending_job() && (wait_time < 0 || wait_time > 100L))
wait_time = 100L;
/* If there is readahead then parse_queued_messages() timed out and we
* should call it again soon. */
if ((wait_time < 0 || wait_time > 100L) && channel_any_readahead())
wait_time = 10L;
#endif
#ifdef FEAT_BEVAL
if (p_beval && wait_time > 100L)
/* The 'balloonexpr' may indirectly invoke a callback while waiting
* for a character, need to check often. */
wait_time = 100L;
#endif
/*
* We want to be interrupted by the winch signal
* or by an event on the monitored file descriptors.
*/
if (WaitForChar(wait_time, &interrupted, FALSE))
{
/* If input was put directly in typeahead buffer bail out here. */
if (typebuf_changed(tb_change_cnt))
return 0;
/*
* For some terminals we only get one character at a time.
* We want the get all available characters, so we could keep on
* trying until none is available
* For some other terminals this is quite slow, that's why we don't
* do it.
*/
len = read_from_input_buf(buf, (long)maxlen);
if (len > 0)
return len;
continue;
}
/* no character available */
#if !(defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H))
/* estimate the elapsed time */
elapsed_time += wait_time;
#endif
if (do_resize /* interrupted by SIGWINCH signal */
#ifdef FEAT_CLIENTSERVER
|| server_waiting()
#endif
#ifdef MESSAGE_QUEUE
|| interrupted
#endif
|| wait_time > 0
|| (wtime < 0 && !did_start_blocking))
continue;
/* no character available or interrupted */
break;
}
return 0;
}
static void
handle_resize(void)
{
do_resize = FALSE;
shell_resized();
}
/*
* Return non-zero if a character is available.
*/
int
mch_char_avail(void)
{
return WaitForChar(0L, NULL, FALSE);
}
#if defined(FEAT_TERMINAL) || defined(PROTO)
/*
* Check for any pending input or messages.
*/
int
mch_check_messages(void)
{
return WaitForChar(0L, NULL, TRUE);
}
#endif
#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
# ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
# endif
# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
# include <sys/sysctl.h>
# endif
# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
# include <sys/sysinfo.h>
# endif
/*
* Return total amount of memory available in Kbyte.
* Doesn't change when memory has been allocated.
*/
long_u
mch_total_mem(int special UNUSED)
{
long_u mem = 0;
long_u shiftright = 10; /* how much to shift "mem" right for Kbyte */
# ifdef HAVE_SYSCTL
int mib[2], physmem;
size_t len;
/* BSD way of getting the amount of RAM available. */
mib[0] = CTL_HW;
mib[1] = HW_USERMEM;
len = sizeof(physmem);
if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
mem = (long_u)physmem;
# endif
# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
if (mem == 0)
{
struct sysinfo sinfo;
/* Linux way of getting amount of RAM available */
if (sysinfo(&sinfo) == 0)
{
# ifdef HAVE_SYSINFO_MEM_UNIT
/* avoid overflow as much as possible */
while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
{
sinfo.mem_unit = sinfo.mem_unit >> 1;
--shiftright;
}
mem = sinfo.totalram * sinfo.mem_unit;
# else
mem = sinfo.totalram;
# endif
}
}
# endif
# ifdef HAVE_SYSCONF
if (mem == 0)
{
long pagesize, pagecount;
/* Solaris way of getting amount of RAM available */
pagesize = sysconf(_SC_PAGESIZE);
pagecount = sysconf(_SC_PHYS_PAGES);
if (pagesize > 0 && pagecount > 0)
{
/* avoid overflow as much as possible */
while (shiftright > 0 && (pagesize & 1) == 0)
{
pagesize = (long_u)pagesize >> 1;
--shiftright;
}
mem = (long_u)pagesize * pagecount;
}
}
# endif
/* Return the minimum of the physical memory and the user limit, because
* using more than the user limit may cause Vim to be terminated. */
# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
{
struct rlimit rlp;
if (getrlimit(RLIMIT_DATA, &rlp) == 0
&& rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
# ifdef RLIM_INFINITY
&& rlp.rlim_cur != RLIM_INFINITY
# endif
&& ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
)
{
mem = (long_u)rlp.rlim_cur;
shiftright = 10;
}
}
# endif
if (mem > 0)
return mem >> shiftright;
return (long_u)0x1fffff;
}
#endif
void
mch_delay(long msec, int ignoreinput)
{
int old_tmode;
#ifdef FEAT_MZSCHEME
long total = msec; /* remember original value */
#endif
if (ignoreinput)
{
/* Go to cooked mode without echo, to allow SIGINT interrupting us
* here. But we don't want QUIT to kill us (CTRL-\ used in a
* shell may produce SIGQUIT). */
in_mch_delay = TRUE;
old_tmode = curr_tmode;
if (curr_tmode == TMODE_RAW)
settmode(TMODE_SLEEP);
/*
* Everybody sleeps in a different way...
* Prefer nanosleep(), some versions of usleep() can only sleep up to
* one second.
*/
#ifdef FEAT_MZSCHEME
do
{
/* if total is large enough, wait by portions in p_mzq */
if (total > p_mzq)
msec = p_mzq;
else
msec = total;
total -= msec;
#endif
#ifdef HAVE_NANOSLEEP
{
struct timespec ts;
ts.tv_sec = msec / 1000;
ts.tv_nsec = (msec % 1000) * 1000000;
(void)nanosleep(&ts, NULL);
}
#else
# ifdef HAVE_USLEEP
while (msec >= 1000)
{
usleep((unsigned int)(999 * 1000));
msec -= 999;
}
usleep((unsigned int)(msec * 1000));
# else
# ifndef HAVE_SELECT
poll(NULL, 0, (int)msec);
# else
{
struct timeval tv;
tv.tv_sec = msec / 1000;
tv.tv_usec = (msec % 1000) * 1000;
/*
* NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
* a patch from Sun to fix this. Reported by Gunnar Pedersen.
*/
select(0, NULL, NULL, NULL, &tv);
}
# endif /* HAVE_SELECT */
# endif /* HAVE_NANOSLEEP */
#endif /* HAVE_USLEEP */
#ifdef FEAT_MZSCHEME
}
while (total > 0);
#endif
settmode(old_tmode);
in_mch_delay = FALSE;
}
else
WaitForChar(msec, NULL, FALSE);
}
#if defined(HAVE_STACK_LIMIT) \
|| (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
# define HAVE_CHECK_STACK_GROWTH
/*
* Support for checking for an almost-out-of-stack-space situation.
*/
/*
* Return a pointer to an item on the stack. Used to find out if the stack
* grows up or down.
*/
static void check_stack_growth(char *p);
static int stack_grows_downwards;
/*
* Find out if the stack grows upwards or downwards.
* "p" points to a variable on the stack of the caller.
*/
static void
check_stack_growth(char *p)
{
int i;
stack_grows_downwards = (p > (char *)&i);
}
#endif
#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
static char *stack_limit = NULL;
#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
# include <pthread.h>
# include <pthread_np.h>
#endif
/*
* Find out until how var the stack can grow without getting into trouble.
* Called when starting up and when switching to the signal stack in
* deathtrap().
*/
static void
get_stack_limit(void)
{
struct rlimit rlp;
int i;
long lim;
/* Set the stack limit to 15/16 of the allowable size. Skip this when the
* limit doesn't fit in a long (rlim_cur might be "long long"). */
if (getrlimit(RLIMIT_STACK, &rlp) == 0
&& rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
# ifdef RLIM_INFINITY
&& rlp.rlim_cur != RLIM_INFINITY
# endif
)
{
lim = (long)rlp.rlim_cur;
#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
{
pthread_attr_t attr;
size_t size;
/* On FreeBSD the initial thread always has a fixed stack size, no
* matter what the limits are set to. Normally it's 1 Mbyte. */
pthread_attr_init(&attr);
if (pthread_attr_get_np(pthread_self(), &attr) == 0)
{
pthread_attr_getstacksize(&attr, &size);
if (lim > (long)size)
lim = (long)size;
}
pthread_attr_destroy(&attr);
}
#endif
if (stack_grows_downwards)
{
stack_limit = (char *)((long)&i - (lim / 16L * 15L));
if (stack_limit >= (char *)&i)
/* overflow, set to 1/16 of current stack position */
stack_limit = (char *)((long)&i / 16L);
}
else
{
stack_limit = (char *)((long)&i + (lim / 16L * 15L));
if (stack_limit <= (char *)&i)
stack_limit = NULL; /* overflow */
}
}
}
/*
* Return FAIL when running out of stack space.
* "p" must point to any variable local to the caller that's on the stack.
*/
int
mch_stackcheck(char *p)
{
if (stack_limit != NULL)
{
if (stack_grows_downwards)
{
if (p < stack_limit)
return FAIL;
}
else if (p > stack_limit)
return FAIL;
}
return OK;
}
#endif
#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
/*
* Support for using the signal stack.
* This helps when we run out of stack space, which causes a SIGSEGV. The
* signal handler then must run on another stack, since the normal stack is
* completely full.
*/
#if defined(HAVE_AVAILABILITYMACROS_H)
# include <AvailabilityMacros.h>
#endif
#ifndef SIGSTKSZ
# define SIGSTKSZ 8000 /* just a guess of how much stack is needed... */
#endif
# ifdef HAVE_SIGALTSTACK
static stack_t sigstk; /* for sigaltstack() */
# else
static struct sigstack sigstk; /* for sigstack() */
# endif
static void init_signal_stack(void);
static char *signal_stack;
static void
init_signal_stack(void)
{
if (signal_stack != NULL)
{
# ifdef HAVE_SIGALTSTACK
# if defined(__APPLE__) && (!defined(MAC_OS_X_VERSION_MAX_ALLOWED) \
|| MAC_OS_X_VERSION_MAX_ALLOWED <= 1040)
/* missing prototype. Adding it to osdef?.h.in doesn't work, because
* "struct sigaltstack" needs to be declared. */
extern int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss);
# endif
# ifdef HAVE_SS_BASE
sigstk.ss_base = signal_stack;
# else
sigstk.ss_sp = signal_stack;
# endif
sigstk.ss_size = SIGSTKSZ;
sigstk.ss_flags = 0;
(void)sigaltstack(&sigstk, NULL);
# else
sigstk.ss_sp = signal_stack;
if (stack_grows_downwards)
sigstk.ss_sp += SIGSTKSZ - 1;
sigstk.ss_onstack = 0;
(void)sigstack(&sigstk, NULL);
# endif
}
}
#endif
/*
* We need correct prototypes for a signal function, otherwise mean compilers
* will barf when the second argument to signal() is ``wrong''.
* Let me try it with a few tricky defines from my own osdef.h (jw).
*/
#if defined(SIGWINCH)
static RETSIGTYPE
sig_winch SIGDEFARG(sigarg)
{
/* this is not required on all systems, but it doesn't hurt anybody */
signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
do_resize = TRUE;
SIGRETURN;
}
#endif
#if defined(SIGINT)
static RETSIGTYPE
catch_sigint SIGDEFARG(sigarg)
{
/* this is not required on all systems, but it doesn't hurt anybody */
signal(SIGINT, (RETSIGTYPE (*)())catch_sigint);
got_int = TRUE;
SIGRETURN;
}
#endif
#if defined(SIGPWR)
static RETSIGTYPE
catch_sigpwr SIGDEFARG(sigarg)
{
/* this is not required on all systems, but it doesn't hurt anybody */
signal(SIGPWR, (RETSIGTYPE (*)())catch_sigpwr);
/*
* I'm not sure we get the SIGPWR signal when the system is really going
* down or when the batteries are almost empty. Just preserve the swap
* files and don't exit, that can't do any harm.
*/
ml_sync_all(FALSE, FALSE);
SIGRETURN;
}
#endif
#ifdef SET_SIG_ALARM
/*
* signal function for alarm().
*/
static RETSIGTYPE
sig_alarm SIGDEFARG(sigarg)
{
/* doesn't do anything, just to break a system call */
sig_alarm_called = TRUE;
SIGRETURN;
}
#endif
#if (defined(HAVE_SETJMP_H) \
&& ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
|| defined(FEAT_LIBCALL))) \
|| defined(PROTO)
/*
* A simplistic version of setjmp() that only allows one level of using.
* Don't call twice before calling mch_endjmp()!.
* Usage:
* mch_startjmp();
* if (SETJMP(lc_jump_env) != 0)
* {
* mch_didjmp();
* EMSG("crash!");
* }
* else
* {
* do_the_work;
* mch_endjmp();
* }
* Note: Can't move SETJMP() here, because a function calling setjmp() must
* not return before the saved environment is used.
* Returns OK for normal return, FAIL when the protected code caused a
* problem and LONGJMP() was used.
*/
void
mch_startjmp(void)
{
#ifdef SIGHASARG
lc_signal = 0;
#endif
lc_active = TRUE;
}
void
mch_endjmp(void)
{
lc_active = FALSE;