Mercurial > hg > xemacs-beta
annotate src/sysdep.c @ 4759:aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
<870180fe0911101613m6b8efa4bpf083fd9013950807@mail.gmail.com>.
author | Jerry James <james@xemacs.org> |
---|---|
date | Wed, 18 Nov 2009 08:49:14 -0700 |
parents | b3d7b8b98acf |
children | 217abcf015c4 |
rev | line source |
---|---|
428 | 1 /* Interfaces to system-dependent kernel and library entries. |
2 Copyright (C) 1985-1988, 1992-1995 Free Software Foundation, Inc. | |
3 Copyright (C) 1995 Tinker Systems. | |
2367 | 4 Copyright (C) 2000, 2001, 2002, 2003, 2004 Ben Wing. |
428 | 5 |
6 This file is part of XEmacs. | |
7 | |
8 XEmacs is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
10 Free Software Foundation; either version 2, or (at your option) any | |
11 later version. | |
12 | |
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with XEmacs; see the file COPYING. If not, write to | |
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
21 Boston, MA 02111-1307, USA. */ | |
22 | |
771 | 23 |
428 | 24 /* Synched up with: FSF 19.30 except for some Windows-NT crap. */ |
25 | |
771 | 26 /* Authorship: |
27 | |
28 Current primary author: Various | |
29 | |
30 Originally from FSF. Major changes at various times. | |
31 Substantially cleaned up by Ben Wing, Dec. 1994 / Jan. 1995. | |
32 SIGIO stuff ripped apart and redone by Ben Wing. (during 19.14 devel?) | |
33 Signal stuff totally redone by Ben Wing. (during 19.14 devel? that would | |
34 be Dec 1995 - Apr 1996.) | |
35 Controlling terminal stuff redone by Ben Wing for 19.13. | |
36 System call encapsulation stuff written by Ben Wing for 19.12. (1995) | |
37 Ripped up and redone avoiding preprocessor tricks Aug - Sep 2001 during | |
38 Mule-on-Windows development. | |
39 */ | |
428 | 40 |
41 #include <config.h> | |
42 #include "lisp.h" | |
43 | |
44 /* ------------------------------- */ | |
45 /* basic includes */ | |
46 /* ------------------------------- */ | |
47 | |
800 | 48 |
49 #include "buffer.h" | |
872 | 50 #include "device-impl.h" |
800 | 51 #include "events.h" |
52 #include "frame.h" | |
53 #include "process.h" | |
54 #include "redisplay.h" | |
55 #include "sysdep.h" | |
56 #include "window.h" | |
57 | |
428 | 58 #ifdef HAVE_TTY |
872 | 59 #include "console-tty-impl.h" |
428 | 60 #else |
61 #endif /* HAVE_TTY */ | |
62 | |
872 | 63 #include "console-stream-impl.h" |
442 | 64 #ifdef WIN32_NATIVE |
771 | 65 #include "syswindows.h" |
428 | 66 #endif |
67 | |
800 | 68 #include "sysdir.h" |
69 #include "sysfile.h" | |
70 #include "sysproc.h" | |
71 #include "syspwd.h" | |
859 | 72 #include "syssignal.h" |
800 | 73 #include "systime.h" |
74 #include "systty.h" | |
75 #include "syswait.h" | |
76 | |
77 #include <setjmp.h> | |
78 | |
79 | |
428 | 80 /* ------------------------------- */ |
81 /* TTY definitions */ | |
82 /* ------------------------------- */ | |
83 | |
84 #ifdef USG | |
85 #include <sys/utsname.h> | |
86 #endif /* USG */ | |
87 | |
88 /* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits. */ | |
89 #ifndef LPASS8 | |
90 #define LPASS8 0 | |
91 #endif | |
92 | |
93 #ifndef HAVE_H_ERRNO | |
94 int h_errno; | |
95 #endif | |
96 | |
97 #ifdef HAVE_TTY | |
98 | |
99 static int baud_convert[] = | |
100 #ifdef BAUD_CONVERT | |
101 BAUD_CONVERT; | |
102 #else | |
103 { | |
104 0, 50, 75, 110, 135, 150, 200, 300, 600, 1200, | |
105 1800, 2400, 4800, 9600, 19200, 38400 | |
106 }; | |
107 #endif | |
108 | |
109 #endif | |
110 | |
2340 | 111 #ifdef HAVE_TTY |
112 #define USED_IF_TTY(decl) decl | |
113 #else | |
114 #define USED_IF_TTY(decl) UNUSED (decl) | |
115 #endif | |
116 | |
428 | 117 |
118 /************************************************************************/ | |
119 /* subprocess control */ | |
120 /************************************************************************/ | |
121 | |
853 | 122 #ifdef NEED_SYNC_PROCESS_CODE |
123 | |
124 /* True iff we are about to fork off a synchronous process or if we | |
125 are waiting for it. */ | |
126 volatile int synch_process_alive; | |
127 | |
128 /* Nonzero => this is a string explaining death of synchronous subprocess. */ | |
129 const char *synch_process_death; | |
130 | |
131 /* If synch_process_death is zero, | |
132 this is exit code of synchronous subprocess. */ | |
133 int synch_process_retcode; | |
134 | |
135 #endif /* NEED_SYNC_PROCESS_CODE */ | |
136 | |
428 | 137 #ifdef HAVE_TTY |
138 | |
139 #ifdef SIGTSTP | |
140 | |
141 /* Arrange for character C to be read as the next input from | |
142 the terminal. */ | |
143 void | |
2311 | 144 stuff_char (struct console *con, |
145 #ifdef TIOCSTI | |
146 int c | |
147 #else | |
148 int UNUSED (c) | |
149 #endif | |
150 ) | |
428 | 151 { |
152 int input_fd; | |
153 | |
154 assert (CONSOLE_TTY_P (con)); | |
155 input_fd = CONSOLE_TTY_DATA (con)->infd; | |
156 /* Should perhaps error if in batch mode */ | |
157 #ifdef TIOCSTI | |
158 ioctl (input_fd, TIOCSTI, &c); | |
159 #else /* no TIOCSTI */ | |
563 | 160 invalid_operation ("Cannot stuff terminal input characters in this version of Unix.", Qunbound); |
428 | 161 #endif /* no TIOCSTI */ |
162 } | |
163 | |
164 #endif /* SIGTSTP */ | |
165 | |
166 #endif /* HAVE_TTY */ | |
167 | |
168 void | |
2311 | 169 set_exclusive_use ( |
170 #ifdef FIOCLEX | |
171 int fd | |
172 #else | |
173 int UNUSED (fd) | |
174 #endif | |
175 ) | |
428 | 176 { |
177 #ifdef FIOCLEX | |
178 ioctl (fd, FIOCLEX, 0); | |
179 #endif | |
180 /* Ok to do nothing if this feature does not exist */ | |
181 } | |
182 | |
183 void | |
2340 | 184 set_descriptor_non_blocking ( |
185 #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS)) || defined (AIX) || defined (F_SETFL) | |
186 int fd | |
187 #else | |
188 int UNUSED (fd) | |
189 #endif | |
190 ) | |
428 | 191 { |
192 /* Stride people say it's a mystery why this is needed | |
193 as well as the O_NDELAY, but that it fails without this. */ | |
194 /* For AIX: Apparently need this for non-blocking reads on sockets. | |
195 It seems that O_NONBLOCK applies only to FIFOs? From | |
196 lowry@watson.ibm.com (Andy Lowry). */ | |
197 /* #### Should this be conditionalized on FIONBIO? */ | |
872 | 198 #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS)) || defined (AIX) |
428 | 199 { |
200 int one = 1; | |
201 ioctl (fd, FIONBIO, &one); | |
202 } | |
203 #endif | |
204 | |
205 #ifdef F_SETFL | |
206 fcntl (fd, F_SETFL, O_NONBLOCK); | |
207 #endif | |
208 } | |
209 | |
853 | 210 #ifdef NEED_SYNC_PROCESS_CODE /* #### Used only on super-ancient systems */ |
211 | |
212 static void | |
814 | 213 wait_for_termination (int pid) |
428 | 214 { |
215 /* #### With the new improved SIGCHLD handling stuff, there is much | |
216 less danger of race conditions and some of the comments below | |
217 don't apply. This should be updated. */ | |
218 | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
3700
diff
changeset
|
219 #if defined (HAVE_WAITPID) |
428 | 220 /* Note that, whenever any subprocess terminates (asynch. or synch.), |
221 the SIGCHLD handler will be called and it will call wait(). Thus | |
222 we cannot just call wait() ourselves, and we can't block SIGCHLD | |
223 and then call wait(), because then if an asynch. process dies | |
224 while we're waiting for our synch. process, Emacs will never | |
225 notice that the asynch. process died. | |
226 | |
227 So, the general approach we take is to repeatedly block until a | |
228 signal arrives, and then check if our process died using kill | |
229 (pid, 0). (We could also check the value of `synch_process_alive', | |
230 since the SIGCHLD handler will reset that and we know that we're | |
231 only being called on synchronous processes, but this approach is | |
232 safer. I don't trust the proper delivery of SIGCHLD. | |
233 | |
234 Note also that we cannot use any form of waitpid(). A loop with | |
235 WNOHANG will chew up CPU time; better to use sleep(). A loop | |
236 without WNOWAIT will screw up the SIGCHLD handler (actually this | |
237 is not true, if you duplicate the exit-status-reaping code; see | |
238 below). A loop with WNOWAIT will result in a race condition if | |
239 the process terminates between the process-status check and the | |
240 call to waitpid(). */ | |
241 | |
242 /* Formerly, immediate_quit was set around this function call, but | |
243 that could lead to problems if the QUIT happened when SIGCHLD was | |
244 blocked -- it would remain blocked. Yet another reason why | |
245 immediate_quit is a bad idea. In any case, there is no reason to | |
246 resort to this because either the SIGIO or the SIGALRM will stop | |
247 the block in EMACS_WAIT_FOR_SIGNAL(). */ | |
248 | |
249 /* Apparently there are bugs on some systems with the second method | |
250 used below (the EMACS_BLOCK_SIGNAL method), whereby zombie | |
251 processes get left around. It appears in those cases that the | |
252 SIGCHLD handler is never getting invoked. It's not clear whether | |
253 this is an Emacs bug or a kernel bug or both: on HPUX this | |
254 problem is observed only with XEmacs, but under Solaris 2.4 all | |
255 sorts of different programs have problems with zombies. The | |
256 method we use here does not require a working SIGCHLD (but will | |
257 not break if it is working), and should be safe. */ | |
258 /* | |
259 We use waitpid(), contrary to the remarks above. There is no | |
260 race condition, because the three situations when sigchld_handler | |
261 is invoked should be handled OK: | |
262 | |
263 - handler invoked before waitpid(): In this case, subprocess | |
264 status will be set by sigchld_handler. waitpid() here will | |
265 return -1 with errno set to ECHILD, which is a valid exit | |
266 condition. | |
267 | |
268 - handler invoked during waitpid(): as above, except that errno | |
269 here will be set to EINTR. This will cause waitpid() to be | |
270 called again, and this time it will exit with ECHILD. | |
271 | |
272 - handler invoked after waitpid(): The following code will reap | |
273 the subprocess. In the handler, wait() will return -1 because | |
274 there is no child to reap, and the handler will exit without | |
275 modifying child subprocess status. */ | |
276 int ret, status; | |
277 | |
278 /* Because the SIGCHLD handler can potentially reap the synchronous | |
279 subprocess, we should take care of that. */ | |
280 | |
281 /* Will stay in the do loop as long as: | |
282 1. Process is alive | |
283 2. Ctrl-G is not pressed */ | |
284 do | |
285 { | |
286 QUIT; | |
287 ret = waitpid (pid, &status, 0); | |
288 /* waitpid returns 0 if the process is still alive. */ | |
289 } | |
290 while (ret == 0 || (ret == -1 && errno == EINTR)); | |
291 | |
292 if (ret == pid) /* Success */ | |
293 /* Set synch process globals. This is can also happen | |
294 in sigchld_handler, and that code is duplicated. */ | |
295 { | |
296 synch_process_alive = 0; | |
297 if (WIFEXITED (status)) | |
298 synch_process_retcode = WEXITSTATUS (status); | |
299 else if (WIFSIGNALED (status)) | |
300 synch_process_death = signal_name (WTERMSIG (status)); | |
301 } | |
302 /* On exiting the loop, ret will be -1, with errno set to ECHILD if | |
303 the child has already been reaped, e.g. in the signal handler. */ | |
304 | |
305 /* Otherwise, we've had some error condition here. | |
306 Per POSIX, the only other possibilities are: | |
307 - EFAULT (bus error accessing arg 2) or | |
308 - EINVAL (incorrect arguments), | |
309 which are both program bugs. | |
310 | |
311 Since implementations may add their own error indicators on top, | |
312 we ignore it by default. */ | |
313 #elif defined (EMACS_BLOCK_SIGNAL) && !defined (BROKEN_WAIT_FOR_SIGNAL) && defined (SIGCHLD) | |
314 while (1) | |
315 { | |
316 static int wait_debugging = 0; /* Set nonzero to make following | |
317 function work under dbx (at least for bsd). */ | |
318 QUIT; | |
319 if (wait_debugging) | |
320 return; | |
321 | |
322 EMACS_BLOCK_SIGNAL (SIGCHLD); | |
323 /* Block SIGCHLD from happening during this check, | |
324 to avoid race conditions. */ | |
325 if (kill (pid, 0) < 0) | |
326 { | |
327 EMACS_UNBLOCK_SIGNAL (SIGCHLD); | |
328 return; | |
329 } | |
330 else | |
331 /* WARNING: Whatever this macro does *must* not allow SIGCHLD | |
332 to happen between the time that it's reenabled and when we | |
333 begin to block. Otherwise we may end up blocking for a | |
334 signal that has already arrived and isn't coming again. | |
335 Can you say "race condition"? | |
336 | |
337 I assume that the system calls sigpause() or sigsuspend() | |
338 to provide this atomicness. If you're getting hangs in | |
339 sigpause()/sigsuspend(), then your OS doesn't implement | |
340 this properly (this applies under hpux9, for example). | |
341 Try defining BROKEN_WAIT_FOR_SIGNAL. */ | |
342 EMACS_WAIT_FOR_SIGNAL (SIGCHLD); | |
343 } | |
853 | 344 #else /* not HAVE_WAITPID and (not EMACS_BLOCK_SIGNAL or BROKEN_WAIT_FOR_SIGNAL) */ |
428 | 345 /* This approach is kind of cheesy but is guaranteed(?!) to work |
346 for all systems. */ | |
347 while (1) | |
348 { | |
349 QUIT; | |
350 if (kill (pid, 0) < 0) | |
351 return; | |
771 | 352 stop_interrupts (); |
353 sleep (1); | |
354 start_interrupts (); | |
428 | 355 } |
356 #endif /* OS features */ | |
357 } | |
358 | |
853 | 359 #endif /* NEED_SYNC_PROCESS_CODE */ |
428 | 360 |
361 /* | |
362 * flush any pending output | |
363 * (may flush input as well; it does not matter the way we use it) | |
364 */ | |
365 | |
366 void | |
2286 | 367 flush_pending_output ( |
368 #if !defined (HAVE_TERMIOS) && (defined (TCFLSH) || defined (TIOCFLUSH)) | |
369 int channel | |
370 #else | |
371 int UNUSED (channel) | |
372 #endif | |
373 ) | |
428 | 374 { |
375 #ifdef HAVE_TERMIOS | |
376 /* If we try this, we get hit with SIGTTIN, because | |
377 the child's tty belongs to the child's pgrp. */ | |
378 #elif defined (TCFLSH) | |
379 ioctl (channel, TCFLSH, 1); | |
380 #elif defined (TIOCFLUSH) | |
381 int zero = 0; | |
382 /* 3rd arg should be ignored | |
383 but some 4.2 kernels actually want the address of an int | |
384 and nonzero means something different. */ | |
385 ioctl (channel, TIOCFLUSH, &zero); | |
386 #endif | |
387 } | |
388 | |
442 | 389 #ifndef WIN32_NATIVE |
428 | 390 /* Set up the terminal at the other end of a pseudo-terminal that |
391 we will be controlling an inferior through. | |
392 It should not echo or do line-editing, since that is done | |
393 in Emacs. No padding needed for insertion into an Emacs buffer. */ | |
394 | |
395 void | |
396 child_setup_tty (int out) | |
397 { | |
398 struct emacs_tty s; | |
430 | 399 emacs_get_tty (out, &s); |
428 | 400 |
401 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS) | |
402 assert (isatty(out)); | |
403 s.main.c_oflag |= OPOST; /* Enable output postprocessing */ | |
404 s.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL on output */ | |
513 | 405 |
406 { | |
407 /* Disable all output delays. */ | |
408 tcflag_t delay_mask = 0; | |
428 | 409 #ifdef NLDLY |
513 | 410 delay_mask |= NLDLY; |
411 #endif | |
412 #ifdef CRDLY | |
413 delay_mask |= CRDLY; | |
414 #endif | |
415 #ifdef TABDLY | |
416 delay_mask |= TABDLY; /* Also disables tab expansion (Posix). */ | |
417 #endif | |
418 #ifdef BSDLY | |
419 delay_mask |= BSDLY; | |
420 #endif | |
421 #ifdef VTDLY | |
422 delay_mask |= VTDLY; | |
428 | 423 #endif |
513 | 424 #ifdef FFDLY |
425 delay_mask |= FFDLY; | |
426 #endif | |
427 s.main.c_oflag &= ~delay_mask; | |
428 } | |
429 | |
430 #ifdef OXTABS | |
431 /* Posix defines the TAB3 value for TABDLY to mean: expand tabs to spaces. | |
432 On those systems tab expansion would be disabled by the above code. | |
433 BSD systems use an independent flag, OXTABS. */ | |
434 s.main.c_oflag &= ~OXTABS; /* Disable tab expansion */ | |
435 #endif | |
436 | |
428 | 437 s.main.c_lflag &= ~ECHO; /* Disable echo */ |
438 s.main.c_lflag |= ISIG; /* Enable signals */ | |
439 #ifdef IUCLC | |
440 s.main.c_iflag &= ~IUCLC; /* Disable downcasing on input. */ | |
441 #endif | |
442 #ifdef OLCUC | |
443 s.main.c_oflag &= ~OLCUC; /* Disable upcasing on output. */ | |
444 #endif | |
513 | 445 |
428 | 446 #if defined (CSIZE) && defined (CS8) |
447 s.main.c_cflag = (s.main.c_cflag & ~CSIZE) | CS8; /* Don't strip 8th bit */ | |
448 #endif | |
449 #ifdef ISTRIP | |
450 s.main.c_iflag &= ~ISTRIP; /* Don't strip 8th bit on input */ | |
451 #endif | |
452 #if 0 | |
453 /* Unnecessary as long as ICANON is set */ | |
454 s.main.c_cc[VMIN] = 1; /* minimum number of characters to accept */ | |
455 s.main.c_cc[VTIME] = 0; /* wait forever for at least 1 character */ | |
456 #endif /* 0 */ | |
457 | |
458 s.main.c_lflag |= ICANON; /* Enable erase/kill and eof processing */ | |
459 s.main.c_cc[VEOF] = 04; /* ensure that EOF is Control-D */ | |
460 s.main.c_cc[VERASE] = _POSIX_VDISABLE; /* disable erase processing */ | |
461 s.main.c_cc[VKILL] = _POSIX_VDISABLE; /* disable kill processing */ | |
462 | |
463 #ifdef HPUX | |
464 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */ | |
465 #endif /* HPUX */ | |
466 | |
467 #ifdef AIX | |
468 #ifndef IBMR2AIX | |
469 /* AIX enhanced edit loses NULs, so disable it. */ | |
470 s.main.c_line = 0; | |
471 s.main.c_iflag &= ~ASCEDIT; | |
472 #endif /* IBMR2AIX */ | |
473 /* Also, PTY overloads NUL and BREAK. | |
474 don't ignore break, but don't signal either, so it looks like NUL. | |
475 This really serves a purpose only if running in an XTERM window | |
476 or via TELNET or the like, but does no harm elsewhere. */ | |
477 s.main.c_iflag &= ~IGNBRK; | |
478 s.main.c_iflag &= ~BRKINT; | |
479 #endif /* AIX */ | |
480 #ifdef SIGNALS_VIA_CHARACTERS | |
481 /* TTY `special characters' are used in process_send_signal | |
482 so set them here to something useful. */ | |
483 s.main.c_cc[VQUIT] = '\\'&037; /* Control-\ */ | |
484 s.main.c_cc[VINTR] = 'C' &037; /* Control-C */ | |
485 s.main.c_cc[VSUSP] = 'Z' &037; /* Control-Z */ | |
486 #else /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */ | |
487 /* TTY `special characters' work better as signals, so disable | |
488 character forms */ | |
489 s.main.c_cc[VQUIT] = _POSIX_VDISABLE; | |
490 s.main.c_cc[VINTR] = _POSIX_VDISABLE; | |
491 s.main.c_cc[VSUSP] = _POSIX_VDISABLE; | |
492 s.main.c_lflag &= ~ISIG; | |
493 #endif /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */ | |
494 s.main.c_cc[VEOL] = _POSIX_VDISABLE; | |
495 #if defined (CBAUD) | |
440 | 496 /* <mdiers> #### This is not portable. ### |
428 | 497 POSIX does not specify CBAUD, and 4.4BSD does not have it. |
498 Instead, POSIX suggests to use cfset{i,o}speed(). | |
499 [cf. D. Lewine, POSIX Programmer's Guide, Chapter 8: Terminal | |
500 I/O, O'Reilly 1991] */ | |
501 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */ | |
502 #else | |
503 /* <mdiers> What to do upon failure? Just ignoring rc is probably | |
504 not acceptable, is it? */ | |
505 if (cfsetispeed (&s.main, B9600) == -1) /* ignore */; | |
506 if (cfsetospeed (&s.main, B9600) == -1) /* ignore */; | |
507 #endif /* defined (CBAUD) */ | |
508 | |
509 #else /* not HAVE_TERMIO */ | |
510 | |
511 s.main.sg_flags &= ~(ECHO | CRMOD | ANYP | ALLDELAY | RAW | LCASE | |
512 | CBREAK | TANDEM); | |
513 s.main.sg_flags |= LPASS8; | |
514 s.main.sg_erase = 0377; | |
515 s.main.sg_kill = 0377; | |
516 s.lmode = LLITOUT | s.lmode; /* Don't strip 8th bit */ | |
517 | |
518 #endif /* not HAVE_TERMIO */ | |
430 | 519 emacs_set_tty (out, &s, 0); |
428 | 520 } |
442 | 521 #endif /* WIN32_NATIVE */ |
428 | 522 |
523 | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
3700
diff
changeset
|
524 #if !defined (SIGTSTP) |
428 | 525 |
526 #define SIG_PARAM_TYPE int | |
527 | |
528 /* Record a signal code and the handler for it. */ | |
529 struct save_signal | |
530 { | |
531 int code; | |
872 | 532 RETSIGTYPE (XCDECL * handler) (SIG_PARAM_TYPE); |
428 | 533 }; |
534 | |
535 static void | |
536 save_signal_handlers (struct save_signal *saved_handlers) | |
537 { | |
538 while (saved_handlers->code) | |
539 { | |
540 saved_handlers->handler | |
872 | 541 = (RETSIGTYPE (XCDECL *) (SIG_PARAM_TYPE)) EMACS_SIGNAL (saved_handlers->code, SIG_IGN); |
428 | 542 saved_handlers++; |
543 } | |
544 } | |
545 | |
546 static void | |
547 restore_signal_handlers (struct save_signal *saved_handlers) | |
548 { | |
549 while (saved_handlers->code) | |
550 { | |
613 | 551 EMACS_SIGNAL (saved_handlers->code, saved_handlers->handler); |
428 | 552 saved_handlers++; |
553 } | |
554 } | |
555 | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
3700
diff
changeset
|
556 #endif /* !defined (SIGTSTP) */ |
428 | 557 |
558 | |
559 | |
560 /* Suspend the Emacs process; give terminal to its superior. */ | |
561 void | |
562 sys_suspend (void) | |
563 { | |
564 #if defined (SIGTSTP) | |
565 { | |
566 int pgrp = EMACS_GET_PROCESS_GROUP (); | |
567 EMACS_KILLPG (pgrp, SIGTSTP); | |
568 } | |
569 | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
3700
diff
changeset
|
570 #else /* No SIGTSTP */ |
428 | 571 /* If you don't know what this is don't mess with it */ |
572 ptrace (0, 0, 0, 0); /* set for ptrace - caught by csh */ | |
573 kill (getpid (), SIGQUIT); | |
574 #endif | |
575 } | |
576 | |
577 /* Suspend a process if possible; give terminal to its superior. */ | |
578 void | |
2340 | 579 sys_suspend_process ( |
580 #ifdef SIGTSTP | |
581 int process | |
582 #else | |
583 int UNUSED (process) | |
584 #endif | |
585 ) | |
428 | 586 { |
587 /* I don't doubt that it is possible to suspend processes on | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
3700
diff
changeset
|
588 * VMS machines, but I don't know how to do it, so... |
428 | 589 */ |
590 #if defined (SIGTSTP) | |
591 kill(process, SIGTSTP); | |
592 #endif | |
593 } | |
594 | |
595 | |
596 /* Given FD, obtain pty buffer size. When no luck, a good guess is made, | |
442 | 597 so that the function works even when fd is not a pty. */ |
428 | 598 |
599 int | |
2340 | 600 get_pty_max_bytes ( |
601 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON) | |
602 int fd | |
603 #else | |
604 int UNUSED (fd) | |
605 #endif | |
606 ) | |
428 | 607 { |
442 | 608 /* DEC OSF 4.0 fpathconf returns 255, but xemacs hangs on long shell |
609 input lines if we return 253. 252 is OK!. So let's leave a bit | |
610 of slack for the newline that xemacs will insert, and for those | |
611 inevitable vendor off-by-one-or-two-or-three bugs. */ | |
612 #define MAX_CANON_SLACK 10 | |
613 #define SAFE_MAX_CANON (127 - MAX_CANON_SLACK) | |
428 | 614 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON) |
442 | 615 { |
616 int max_canon = fpathconf (fd, _PC_MAX_CANON); | |
617 #ifdef __hpux__ | |
618 /* HP-UX 10.20 fpathconf returns 768, but this results in | |
619 truncated input lines, while 255 works. */ | |
620 if (max_canon > 255) max_canon = 255; | |
428 | 621 #endif |
442 | 622 return (max_canon < 0 ? SAFE_MAX_CANON : |
623 max_canon > SAFE_MAX_CANON ? max_canon - MAX_CANON_SLACK : | |
624 max_canon); | |
625 } | |
626 #elif defined (_POSIX_MAX_CANON) | |
627 return (_POSIX_MAX_CANON > SAFE_MAX_CANON ? | |
628 _POSIX_MAX_CANON - MAX_CANON_SLACK : | |
629 _POSIX_MAX_CANON); | |
630 #else | |
631 return SAFE_MAX_CANON; | |
632 #endif | |
428 | 633 } |
634 | |
635 /* Figure out the eof character for the FD. */ | |
636 | |
867 | 637 Ibyte |
428 | 638 get_eof_char (int fd) |
639 { | |
867 | 640 const Ibyte ctrl_d = (Ibyte) '\004'; |
428 | 641 |
642 if (!isatty (fd)) | |
643 return ctrl_d; | |
644 #ifdef HAVE_TERMIOS | |
645 { | |
646 struct termios t; | |
647 tcgetattr (fd, &t); | |
648 #if 0 | |
649 /* What is the following line designed to do??? -mrb */ | |
647 | 650 if ((int) strlen ((const char *) t.c_cc) < (VEOF + 1)) |
428 | 651 return ctrl_d; |
652 else | |
867 | 653 return (Ibyte) t.c_cc[VEOF]; |
428 | 654 #endif |
867 | 655 return t.c_cc[VEOF] == _POSIX_VDISABLE ? ctrl_d : (Ibyte) t.c_cc[VEOF]; |
428 | 656 } |
657 #else /* ! HAVE_TERMIOS */ | |
658 /* On Berkeley descendants, the following IOCTL's retrieve the | |
659 current control characters. */ | |
660 #if defined (TIOCGETC) | |
661 { | |
662 struct tchars c; | |
663 ioctl (fd, TIOCGETC, &c); | |
867 | 664 return (Ibyte) c.t_eofc; |
428 | 665 } |
666 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */ | |
667 /* On SYSV descendants, the TCGETA ioctl retrieves the current control | |
668 characters. */ | |
669 #ifdef TCGETA | |
670 { | |
671 struct termio t; | |
672 ioctl (fd, TCGETA, &t); | |
647 | 673 if ((int) strlen ((const char *) t.c_cc) < (VINTR + 1)) |
428 | 674 return ctrl_d; |
675 else | |
867 | 676 return (Ibyte) t.c_cc[VINTR]; |
428 | 677 } |
678 #else /* ! defined (TCGETA) */ | |
679 /* Rather than complain, we'll just guess ^D, which is what | |
680 * earlier emacsen always used. */ | |
681 return ctrl_d; | |
682 #endif /* ! defined (TCGETA) */ | |
683 #endif /* ! defined (TIOCGETC) */ | |
684 #endif /* ! defined (HAVE_TERMIOS) */ | |
685 } | |
686 | |
687 /* Set the logical window size associated with descriptor FD | |
688 to HEIGHT and WIDTH. This is used mainly with ptys. */ | |
689 | |
690 int | |
2340 | 691 set_window_size ( |
692 #if defined (TIOCSWINSZ) || defined (TIOCSSIZE) | |
693 int fd, int height, int width | |
694 #else | |
695 int UNUSED (fd), int UNUSED (height), int UNUSED (width) | |
696 #endif | |
697 ) | |
428 | 698 { |
699 #ifdef TIOCSWINSZ | |
700 | |
701 /* BSD-style. */ | |
702 struct winsize size; | |
703 size.ws_row = height; | |
704 size.ws_col = width; | |
705 | |
706 if (ioctl (fd, TIOCSWINSZ, &size) == -1) | |
707 return 0; /* error */ | |
708 else | |
709 return 1; | |
710 | |
711 #elif defined (TIOCSSIZE) | |
712 | |
713 /* SunOS - style. */ | |
714 struct ttysize size; | |
715 size.ts_lines = height; | |
716 size.ts_cols = width; | |
717 | |
718 if (ioctl (fd, TIOCGSIZE, &size) == -1) | |
719 return 0; | |
720 else | |
721 return 1; | |
722 #else | |
723 return -1; | |
724 #endif | |
725 } | |
726 | |
727 /* Set up the proper status flags for use of a pty. */ | |
728 | |
729 void | |
2340 | 730 setup_pty ( |
731 #ifdef TIOCPKT | |
732 int fd | |
733 #else | |
734 int UNUSED (fd) | |
735 #endif | |
736 ) | |
428 | 737 { |
738 #ifdef TIOCPKT | |
739 /* In some systems (Linux through 2.0.0, at least), packet mode doesn't | |
740 get cleared when a pty is closed, so we need to clear it here. | |
741 Linux pre2.0.13 contained an attempted fix for this (from Ted Ts'o, | |
742 tytso@mit.edu), but apparently it messed up rlogind and telnetd, so he | |
743 removed the fix in pre2.0.14. - dkindred@cs.cmu.edu | |
744 */ | |
745 { | |
746 int off = 0; | |
747 ioctl (fd, TIOCPKT, (char *)&off); | |
748 } | |
535 | 749 #endif /* TIOCPKT */ |
428 | 750 } |
751 | |
752 | |
753 /************************************************************************/ | |
754 /* TTY control */ | |
755 /************************************************************************/ | |
756 | |
757 /* ------------------------------------------------------ */ | |
758 /* get baud rate */ | |
759 /* ------------------------------------------------------ */ | |
760 | |
761 /* It really makes more sense for the baud-rate to be console-specific | |
762 and not device-specific, but it's (at least potentially) used for output | |
763 decisions. */ | |
764 | |
765 void | |
766 init_baud_rate (struct device *d) | |
767 { | |
768 if (DEVICE_WIN_P (d) || DEVICE_STREAM_P (d)) | |
769 { | |
770 DEVICE_BAUD_RATE (d) = 38400; | |
771 return; | |
772 } | |
773 | |
774 #ifdef HAVE_TTY | |
775 assert (DEVICE_TTY_P (d)); | |
776 { | |
647 | 777 struct console *con = XCONSOLE (DEVICE_CONSOLE (d)); |
428 | 778 int input_fd = CONSOLE_TTY_DATA (con)->infd; |
814 | 779 #ifdef HAVE_TERMIOS |
428 | 780 struct termios sg; |
781 | |
782 sg.c_cflag = B9600; | |
783 tcgetattr (input_fd, &sg); | |
784 DEVICE_TTY_DATA (d)->ospeed = cfgetospeed (&sg); | |
785 #elif defined (HAVE_TERMIO) | |
786 struct termio sg; | |
787 | |
788 sg.c_cflag = B9600; | |
789 # ifdef HAVE_TCATTR | |
790 tcgetattr (input_fd, &sg); | |
791 # else | |
792 ioctl (input_fd, TCGETA, &sg); | |
793 # endif | |
794 DEVICE_TTY_DATA (d)->ospeed = sg.c_cflag & CBAUD; | |
795 #else /* neither TERMIOS nor TERMIO */ | |
796 struct sgttyb sg; | |
797 | |
798 sg.sg_ospeed = B9600; | |
799 if (ioctl (input_fd, TIOCGETP, &sg) < 0) | |
2500 | 800 ABORT (); |
428 | 801 DEVICE_TTY_DATA (d)->ospeed = sg.sg_ospeed; |
802 #endif | |
803 } | |
804 | |
805 DEVICE_BAUD_RATE (d) = | |
806 (DEVICE_TTY_DATA (d)->ospeed < countof (baud_convert) | |
807 ? baud_convert[DEVICE_TTY_DATA (d)->ospeed] | |
808 : 9600); | |
809 | |
810 if (DEVICE_BAUD_RATE (d) == 0) | |
811 DEVICE_BAUD_RATE (d) = 1200; | |
812 #endif /* HAVE_TTY */ | |
813 } | |
814 | |
815 | |
816 /* ------------------------------------------------------ */ | |
817 /* SIGIO control */ | |
818 /* ------------------------------------------------------ */ | |
819 | |
853 | 820 #if defined (SIGIO) && !defined (BROKEN_SIGIO) |
428 | 821 |
822 static void | |
823 init_sigio_on_device (struct device *d) | |
824 { | |
825 int filedesc = DEVICE_INFD (d); | |
826 | |
827 #if defined (FIOSSAIOOWN) | |
828 { /* HPUX stuff */ | |
829 int owner = getpid (); | |
830 int ioctl_status; | |
831 if (DEVICE_TTY_P (d)) | |
832 { | |
833 ioctl_status = ioctl (filedesc, FIOGSAIOOWN, | |
834 &DEVICE_OLD_FCNTL_OWNER (d)); | |
835 ioctl_status = ioctl (filedesc, FIOSSAIOOWN, &owner); | |
836 } | |
837 #ifdef HAVE_WINDOW_SYSTEM | |
838 else if (!DEVICE_STREAM_P (d)) | |
839 { | |
840 ioctl_status = ioctl (filedesc, SIOCGPGRP, | |
841 &DEVICE_OLD_FCNTL_OWNER (d)); | |
842 ioctl_status = ioctl (filedesc, SIOCSPGRP, &owner); | |
843 } | |
844 #endif | |
845 } | |
846 #elif defined (F_SETOWN) && !defined (F_SETOWN_BUG) | |
847 DEVICE_OLD_FCNTL_OWNER (d) = fcntl (filedesc, F_GETOWN, 0); | |
848 fcntl (filedesc, F_SETOWN, getpid ()); | |
849 #endif | |
850 } | |
851 | |
852 static void | |
853 reset_sigio_on_device (struct device *d) | |
854 { | |
855 int filedesc = DEVICE_INFD (d); | |
856 | |
857 #if defined (FIOSSAIOOWN) | |
858 { /* HPUX stuff */ | |
859 int ioctl_status; | |
860 if (DEVICE_TTY_P (d)) | |
861 { | |
862 ioctl_status = ioctl (filedesc, FIOSSAIOOWN, | |
863 &DEVICE_OLD_FCNTL_OWNER (d)); | |
864 } | |
865 #ifdef HAVE_WINDOW_SYSTEM | |
866 else if (!DEVICE_STREAM_P (d)) | |
867 { | |
868 ioctl_status = ioctl (filedesc, SIOCSPGRP, | |
869 &DEVICE_OLD_FCNTL_OWNER (d)); | |
870 } | |
871 #endif | |
872 } | |
873 #elif defined (F_SETOWN) && !defined (F_SETOWN_BUG) | |
874 fcntl (filedesc, F_SETOWN, DEVICE_OLD_FCNTL_OWNER (d)); | |
875 #endif | |
876 } | |
877 | |
878 static void | |
879 request_sigio_on_device (struct device *d) | |
880 { | |
881 int filedesc = DEVICE_INFD (d); | |
882 | |
502 | 883 /* NOTE: It appears that Linux has its own mechanism for requesting |
884 SIGIO, using the F_GETSIG and F_SETSIG commands to fcntl(). | |
885 These let you pick which signal you want sent (not just SIGIO), | |
886 and if you do this, you get additional info which tells you which | |
887 file descriptor has input ready on it. The man page says: | |
888 | |
889 Using these mechanisms, a program can implement fully | |
890 asynchronous I/O without using select(2) or poll(2) most | |
891 of the time. | |
892 | |
893 The use of O_ASYNC, F_GETOWN, F_SETOWN is specific to BSD | |
894 and Linux. F_GETSIG and F_SETSIG are Linux-specific. | |
895 POSIX has asynchronous I/O and the aio_sigevent structure | |
896 to achieve similar things; these are also available in | |
897 Linux as part of the GNU C Library (Glibc). | |
898 | |
899 But it appears that Linux also supports O_ASYNC, so I see no | |
900 particular need to switch. --ben | |
901 */ | |
902 | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
3700
diff
changeset
|
903 #if defined (I_SETSIG) && !defined (HPUX11) && !defined (LINUX) |
428 | 904 { |
502 | 905 int events = 0; |
428 | 906 ioctl (filedesc, I_GETSIG, &events); |
907 ioctl (filedesc, I_SETSIG, events | S_INPUT); | |
908 } | |
502 | 909 #elif defined (O_ASYNC) |
910 /* Generally FASYNC and O_ASYNC are both defined, and both equal; | |
911 but let's not depend on that. O_ASYNC appears to be more | |
912 standard (at least the Linux include files think so), so | |
913 check it first. */ | |
914 fcntl (filedesc, F_SETFL, fcntl (filedesc, F_GETFL, 0) | O_ASYNC); | |
428 | 915 #elif defined (FASYNC) |
916 fcntl (filedesc, F_SETFL, fcntl (filedesc, F_GETFL, 0) | FASYNC); | |
917 #elif defined (FIOSSAIOSTAT) | |
918 { | |
919 /* DG: Changed for HP-UX. HP-UX uses different IOCTLs for | |
920 sockets and other devices for some bizarre reason. We guess | |
921 that an X device is a socket, and tty devices aren't. We then | |
922 use the following crud to do the appropriate thing. */ | |
923 int on = 1; | |
924 int ioctl_status; /* ####DG: check if IOCTL succeeds here. */ | |
925 | |
926 if (DEVICE_TTY_P (d)) | |
927 { | |
928 ioctl_status = ioctl (filedesc, FIOSSAIOSTAT, &on); | |
929 } | |
930 #ifdef HAVE_WINDOW_SYSTEM | |
931 else if (!DEVICE_STREAM_P (d)) | |
932 { | |
933 ioctl_status = ioctl (filedesc, FIOASYNC, &on); | |
934 } | |
935 #endif | |
936 } | |
937 #elif defined (FIOASYNC) | |
938 { | |
939 int on = 1; | |
940 ioctl (filedesc, FIOASYNC, &on); | |
941 } | |
942 #endif | |
943 } | |
944 | |
945 static void | |
946 unrequest_sigio_on_device (struct device *d) | |
947 { | |
948 int filedesc = DEVICE_INFD (d); | |
949 | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
3700
diff
changeset
|
950 #if defined (I_SETSIG) && !defined (HPUX11) && !defined (LINUX) |
428 | 951 { |
502 | 952 int events = 0; |
428 | 953 ioctl (filedesc, I_GETSIG, &events); |
954 ioctl (filedesc, I_SETSIG, events & ~S_INPUT); | |
955 } | |
502 | 956 #elif defined (O_ASYNC) |
957 fcntl (filedesc, F_SETFL, fcntl (filedesc, F_GETFL, 0) & ~O_ASYNC); | |
428 | 958 #elif defined (FASYNC) |
959 fcntl (filedesc, F_SETFL, fcntl (filedesc, F_GETFL, 0) & ~FASYNC); | |
960 #elif defined (FIOSSAIOSTAT) | |
961 { | |
962 /* DG: Changed for HP-UX. HP-UX uses different IOCTLs for | |
963 sockets and other devices for some bizarre reason. We guess | |
964 that an X device is a socket, and tty devices aren't. We then | |
965 use the following crud to do the appropriate thing. */ | |
966 | |
967 int off = 0; | |
968 int ioctl_status; | |
969 | |
970 /* See comment for request_sigio_on_device */ | |
971 | |
972 if (DEVICE_TTY_P (d)) | |
973 { | |
974 ioctl_status = ioctl (filedesc, FIOSSAIOSTAT, &off); | |
975 } | |
976 else | |
977 { | |
978 ioctl_status = ioctl (filedesc, FIOASYNC, &off); | |
979 } | |
980 } | |
981 #elif defined (FIOASYNC) | |
982 { | |
983 int off = 0; | |
984 ioctl (filedesc, FIOASYNC, &off); | |
985 } | |
986 #endif | |
987 } | |
988 | |
989 void | |
990 request_sigio (void) | |
991 { | |
992 Lisp_Object devcons, concons; | |
993 | |
994 DEVICE_LOOP_NO_BREAK (devcons, concons) | |
995 { | |
996 struct device *d; | |
997 | |
998 d = XDEVICE (XCAR (devcons)); | |
999 | |
1000 if (!DEVICE_STREAM_P (d)) | |
1001 request_sigio_on_device (d); | |
1002 } | |
1003 } | |
1004 | |
1005 void | |
1006 unrequest_sigio (void) | |
1007 { | |
1008 Lisp_Object devcons, concons; | |
1009 | |
1010 DEVICE_LOOP_NO_BREAK (devcons, concons) | |
1011 { | |
1012 struct device *d; | |
1013 | |
1014 d = XDEVICE (XCAR (devcons)); | |
1015 | |
1016 if (!DEVICE_STREAM_P (d)) | |
1017 unrequest_sigio_on_device (d); | |
1018 } | |
1019 } | |
1020 | |
1021 #endif /* SIGIO */ | |
1022 | |
1023 /* ------------------------------------------------------ */ | |
1024 /* Changing Emacs's process group */ | |
1025 /* ------------------------------------------------------ */ | |
1026 | |
1027 /* Saving and restoring the process group of Emacs's terminal. */ | |
1028 | |
1029 /* On some systems, apparently (?!) Emacs must be in its own process | |
1030 group in order to receive SIGIO correctly. On other systems | |
1031 (e.g. Solaris), it's not required and doing it makes things | |
1032 get fucked up. So, we only do it when | |
1033 SIGIO_REQUIRES_SEPARATE_PROCESS_GROUP is defined. Basically, | |
1034 this is only required for BSD 4.2 systems. (Actually, I bet | |
1035 we don't have to do this at all -- those systems also | |
1036 required interrupt input, which we don't support.) | |
1037 | |
1038 If Emacs was in its own process group (i.e. inherited_pgroup == | |
1039 getpid ()), then we know we're running under a shell with job | |
1040 control (Emacs would never be run as part of a pipeline). | |
1041 Everything is fine. | |
1042 | |
1043 If Emacs was not in its own process group, then we know we're | |
1044 running under a shell (or a caller) that doesn't know how to | |
1045 separate itself from Emacs (like sh). Emacs must be in its own | |
1046 process group in order to receive SIGIO correctly. In this | |
1047 situation, we put ourselves in our own pgroup, forcibly set the | |
1048 tty's pgroup to our pgroup, and make sure to restore and reinstate | |
1049 the tty's pgroup just like any other terminal setting. If | |
1050 inherited_group was not the tty's pgroup, then we'll get a | |
1051 SIGTTmumble when we try to change the tty's pgroup, and a CONT if | |
1052 it goes foreground in the future, which is what should happen. */ | |
1053 | |
1054 #ifdef SIGIO_REQUIRES_SEPARATE_PROCESS_GROUP | |
1055 | |
442 | 1056 static pid_t inherited_pgroup; |
1057 static pid_t inherited_tty_pgroup; | |
428 | 1058 |
1059 #endif | |
1060 | |
1061 void | |
1062 munge_tty_process_group (void) | |
1063 { | |
1064 #ifdef SIGIO_REQUIRES_SEPARATE_PROCESS_GROUP | |
1065 if (noninteractive) | |
1066 return; | |
1067 | |
1068 /* Only do this munging if we have a device on the controlling | |
1069 terminal. See the large comment below. */ | |
1070 | |
1071 if (CONSOLEP (Vcontrolling_terminal) && | |
1072 CONSOLE_LIVE_P (XCONSOLE (Vcontrolling_terminal))) | |
1073 { | |
1074 int fd = open ("/dev/tty", O_RDWR, 0); | |
442 | 1075 pid_t me = getpid (); |
428 | 1076 EMACS_BLOCK_SIGNAL (SIGTTOU); |
1077 EMACS_SET_TTY_PROCESS_GROUP (fd, &me); | |
1078 EMACS_UNBLOCK_SIGNAL (SIGTTOU); | |
771 | 1079 retry_close (fd); |
428 | 1080 } |
1081 #endif | |
1082 } | |
1083 | |
1084 /* Split off the foreground process group to Emacs alone. | |
1085 When we are in the foreground, but not started in our own process | |
1086 group, redirect the TTY to point to our own process group. We need | |
1087 to be in our own process group to receive SIGIO properly. */ | |
1088 static void | |
1089 munge_process_groups (void) | |
1090 { | |
1091 #ifdef SIGIO_REQUIRES_SEPARATE_PROCESS_GROUP | |
1092 if (noninteractive) | |
1093 return; | |
1094 | |
1095 EMACS_SEPARATE_PROCESS_GROUP (); | |
1096 | |
1097 munge_tty_process_group (); | |
1098 #endif | |
1099 } | |
1100 | |
1101 void | |
1102 unmunge_tty_process_group (void) | |
1103 { | |
1104 #ifdef SIGIO_REQUIRES_SEPARATE_PROCESS_GROUP | |
1105 { | |
1106 int fd = open ("/dev/tty", O_RDWR, 0); | |
1107 EMACS_BLOCK_SIGNAL (SIGTTOU); | |
1108 EMACS_SET_TTY_PROCESS_GROUP (fd, &inherited_tty_pgroup); | |
1109 EMACS_UNBLOCK_SIGNAL (SIGTTOU); | |
771 | 1110 retry_close (fd); |
428 | 1111 } |
1112 #endif | |
1113 } | |
1114 | |
1115 /* Set the tty to our original foreground group. | |
1116 Also restore the original process group (put us back into sh's | |
1117 process group), so that ^Z will suspend both us and sh. */ | |
1118 static void | |
1119 unmunge_process_groups (void) | |
1120 { | |
1121 #ifdef SIGIO_REQUIRES_SEPARATE_PROCESS_GROUP | |
1122 if (noninteractive) | |
1123 return; | |
1124 | |
1125 unmunge_tty_process_group (); | |
1126 | |
1127 EMACS_SET_PROCESS_GROUP (inherited_pgroup); | |
1128 #endif | |
1129 } | |
1130 | |
1131 /* According to some old wisdom, we need to be in a separate process | |
1132 group for SIGIO to work correctly (at least on some systems ...). | |
1133 So go ahead and put ourselves into our own process group. This | |
1134 will fail if we're already in our own process group, but who cares. | |
1135 Also record whether we were in our own process group. (In general, | |
1136 we will already be in our own process group if we were started from | |
1137 a job-control shell like csh, but not if we were started from sh). | |
1138 | |
1139 If we succeeded in changing our process group, then we will no | |
1140 longer be in the foreground process group of our controlling | |
1141 terminal. Therefore, if we have a console open onto this terminal, | |
1142 we have to change the controlling terminal's foreground process | |
1143 group (otherwise we will get stopped with a SIGTTIN signal when | |
1144 attempting to read from the terminal). It's important, | |
1145 however, that we do this *only* when we have a console open onto | |
1146 the terminal. It's a decidedly bad idea to do so otherwise, | |
1147 especially if XEmacs was started from the background. */ | |
1148 | |
1149 void | |
1150 init_process_group (void) | |
1151 { | |
1152 #ifdef SIGIO_REQUIRES_SEPARATE_PROCESS_GROUP | |
1153 if (! noninteractive) | |
1154 { | |
1155 int fd = open ("/dev/tty", O_RDWR, 0); | |
1156 inherited_pgroup = EMACS_GET_PROCESS_GROUP (); | |
1157 EMACS_GET_TTY_PROCESS_GROUP (fd, &inherited_tty_pgroup); | |
771 | 1158 retry_close (fd); |
428 | 1159 EMACS_SEPARATE_PROCESS_GROUP (); |
1160 } | |
1161 #endif | |
1162 } | |
1163 | |
1164 void | |
1165 disconnect_controlling_terminal (void) | |
1166 { | |
1167 # ifdef HAVE_SETSID | |
1168 /* Controlling terminals are attached to a session. | |
1169 Create a new session for us; it will have no controlling | |
1170 terminal. This also, of course, puts us in our own | |
1171 process group. */ | |
1172 setsid (); | |
1173 # else | |
1174 /* Put us in our own process group. */ | |
1175 EMACS_SEPARATE_PROCESS_GROUP (); | |
1176 # if defined (TIOCNOTTY) | |
1177 /* This is the older way of disconnecting the controlling | |
1178 terminal, on 4.3 BSD. We must open /dev/tty; using | |
1179 filedesc 0 is not sufficient because it could be | |
1180 something else (e.g. our stdin was redirected to | |
1181 another terminal). | |
1182 */ | |
1183 { | |
1184 int j = open ("/dev/tty", O_RDWR, 0); | |
1185 ioctl (j, TIOCNOTTY, 0); | |
771 | 1186 retry_close (j); |
428 | 1187 } |
1188 # endif /* TIOCNOTTY */ | |
1189 /* | |
1190 On systems without TIOCNOTTY and without | |
1191 setsid(), we don't need to do anything more to | |
1192 disconnect our controlling terminal. Here is | |
1193 what the man page for termio(7) from a SYSV 3.2 | |
1194 system says: | |
1195 | |
1196 "The first terminal file opened by the process group leader | |
1197 of a terminal file not already associated with a process | |
1198 group becomes the control terminal for that process group. | |
1199 The control terminal plays a special role in handling quit | |
1200 and interrupt signals, as discussed below. The control | |
1201 terminal is inherited by a child process during a fork(2). | |
1202 A process can break this association by changing its process | |
1203 group using setpgrp(2)." | |
1204 | |
1205 */ | |
1206 # endif /* not HAVE_SETSID */ | |
1207 } | |
1208 | |
1209 | |
1210 /* ------------------------------------------------------ */ | |
1211 /* Getting and setting emacs_tty structures */ | |
1212 /* ------------------------------------------------------ */ | |
1213 | |
1214 /* It's wrong to encase these into #ifdef HAVE_TTY because we need | |
1215 them for child TTY processes. */ | |
1216 /* However, this does break NT support while we don't do child TTY processes */ | |
442 | 1217 #ifndef WIN32_NATIVE |
428 | 1218 |
1219 /* Set *TC to the parameters associated with the terminal FD. | |
1220 Return zero if all's well, or -1 if we ran into an error we | |
1221 couldn't deal with. */ | |
1222 int | |
1223 emacs_get_tty (int fd, struct emacs_tty *settings) | |
1224 { | |
1225 /* Retrieve the primary parameters - baud rate, character size, etcetera. */ | |
1226 #ifdef HAVE_TCATTR | |
1227 /* We have those nifty POSIX tcmumbleattr functions. */ | |
1228 if (tcgetattr (fd, &settings->main) < 0) | |
1229 return -1; | |
1230 | |
1231 #elif defined HAVE_TERMIO | |
1232 /* The SYSV-style interface? */ | |
1233 if (ioctl (fd, TCGETA, &settings->main) < 0) | |
1234 return -1; | |
1235 | |
814 | 1236 #else |
428 | 1237 /* I give up - I hope you have the BSD ioctls. */ |
1238 if (ioctl (fd, TIOCGETP, &settings->main) < 0) | |
1239 return -1; | |
1240 #endif /* HAVE_TCATTR */ | |
1241 | |
1242 /* Suivant - Do we have to get struct ltchars data? */ | |
1243 #ifdef HAVE_LTCHARS | |
1244 if (ioctl (fd, TIOCGLTC, &settings->ltchars) < 0) | |
1245 return -1; | |
1246 #endif | |
1247 | |
1248 /* How about a struct tchars and a wordful of lmode bits? */ | |
1249 #ifdef HAVE_TCHARS | |
1250 if (ioctl (fd, TIOCGETC, &settings->tchars) < 0 | |
1251 || ioctl (fd, TIOCLGET, &settings->lmode) < 0) | |
1252 return -1; | |
1253 #endif | |
1254 | |
1255 /* We have survived the tempest. */ | |
1256 return 0; | |
1257 } | |
1258 | |
1259 /* Set the parameters of the tty on FD according to the contents of | |
1260 *SETTINGS. If FLUSHP is non-zero, we discard input. | |
430 | 1261 Return 0 if all went well, and -1 if anything failed. |
1262 #### All current callers use FLUSHP == 0. */ | |
428 | 1263 |
1264 int | |
1265 emacs_set_tty (int fd, struct emacs_tty *settings, int flushp) | |
1266 { | |
1267 /* Set the primary parameters - baud rate, character size, etcetera. */ | |
1268 #ifdef HAVE_TCATTR | |
1269 int i; | |
1270 /* We have those nifty POSIX tcmumbleattr functions. | |
1271 William J. Smith <wjs@wiis.wang.com> writes: | |
1272 "POSIX 1003.1 defines tcsetattr() to return success if it was | |
1273 able to perform any of the requested actions, even if some | |
1274 of the requested actions could not be performed. | |
1275 We must read settings back to ensure tty setup properly. | |
1276 AIX requires this to keep tty from hanging occasionally." */ | |
1277 /* This makes sure that we don't loop indefinitely in here. */ | |
1278 for (i = 0 ; i < 10 ; i++) | |
1279 if (tcsetattr (fd, flushp ? TCSAFLUSH : TCSADRAIN, &settings->main) < 0) | |
1280 { | |
1281 if (errno == EINTR) | |
1282 continue; | |
1283 else | |
1284 return -1; | |
1285 } | |
1286 else | |
1287 { | |
2957 | 1288 struct termios new_; |
428 | 1289 |
1290 /* Get the current settings, and see if they're what we asked for. */ | |
2957 | 1291 tcgetattr (fd, &new_); |
428 | 1292 /* We cannot use memcmp on the whole structure here because under |
1293 * aix386 the termios structure has some reserved field that may | |
1294 * not be filled in. | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
3700
diff
changeset
|
1295 * FIXME: Now that aix386 is gone, can we memcmp the whole structure? |
428 | 1296 */ |
2957 | 1297 if ( new_.c_iflag == settings->main.c_iflag |
1298 && new_.c_oflag == settings->main.c_oflag | |
1299 && new_.c_cflag == settings->main.c_cflag | |
1300 && new_.c_lflag == settings->main.c_lflag | |
1301 && memcmp(new_.c_cc, settings->main.c_cc, NCCS) == 0) | |
428 | 1302 break; |
1303 else | |
1304 continue; | |
1305 } | |
1306 #elif defined HAVE_TERMIO | |
1307 /* The SYSV-style interface? */ | |
1308 if (ioctl (fd, flushp ? TCSETAF : TCSETAW, &settings->main) < 0) | |
1309 return -1; | |
1310 | |
814 | 1311 #else |
428 | 1312 /* I give up - I hope you have the BSD ioctls. */ |
1313 if (ioctl (fd, (flushp) ? TIOCSETP : TIOCSETN, &settings->main) < 0) | |
1314 return -1; | |
1315 #endif /* HAVE_TCATTR */ | |
1316 | |
1317 /* Suivant - Do we have to get struct ltchars data? */ | |
1318 #ifdef HAVE_LTCHARS | |
1319 if (ioctl (fd, TIOCSLTC, &settings->ltchars) < 0) | |
1320 return -1; | |
1321 #endif | |
1322 | |
1323 /* How about a struct tchars and a wordful of lmode bits? */ | |
1324 #ifdef HAVE_TCHARS | |
1325 if (ioctl (fd, TIOCSETC, &settings->tchars) < 0 | |
1326 || ioctl (fd, TIOCLSET, &settings->lmode) < 0) | |
1327 return -1; | |
1328 #endif | |
1329 | |
1330 /* We have survived the tempest. */ | |
1331 return 0; | |
1332 } | |
1333 | |
442 | 1334 #endif /* WIN32_NATIVE */ |
428 | 1335 |
1336 /* ------------------------------------------------------ */ | |
1337 /* Initializing a device */ | |
1338 /* ------------------------------------------------------ */ | |
1339 | |
1340 #ifdef HAVE_TTY | |
1341 | |
1342 #if defined (TIOCGLTC) && defined (HAVE_LTCHARS) /* HAVE_LTCHARS */ | |
1343 static struct ltchars new_ltchars = {-1,-1,-1,-1,-1,-1}; | |
1344 #endif | |
1345 #ifdef TIOCGETC /* HAVE_TCHARS */ | |
1346 #ifdef HAVE_TCHARS | |
1347 static struct tchars new_tchars = {-1,-1,-1,-1,-1,-1}; | |
1348 #endif | |
1349 #endif | |
1350 | |
1351 static void | |
1352 tty_init_sys_modes_on_device (struct device *d) | |
1353 { | |
1354 struct emacs_tty tty; | |
2286 | 1355 int input_fd; |
428 | 1356 struct console *con = XCONSOLE (DEVICE_CONSOLE (d)); |
1357 | |
1358 input_fd = CONSOLE_TTY_DATA (con)->infd; | |
1359 | |
430 | 1360 emacs_get_tty (input_fd, &CONSOLE_TTY_DATA (con)->old_tty); |
428 | 1361 tty = CONSOLE_TTY_DATA (con)->old_tty; |
1362 | |
1363 con->tty_erase_char = Qnil; | |
1364 | |
1365 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS) | |
1366 /* after all those years... */ | |
1367 con->tty_erase_char = make_char (tty.main.c_cc[VERASE]); | |
1368 tty.main.c_iflag |= (IGNBRK); /* Ignore break condition */ | |
1369 tty.main.c_iflag &= ~ICRNL; /* Disable map of CR to NL on input */ | |
1370 #ifdef ISTRIP | |
1371 tty.main.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */ | |
1372 #endif | |
1373 tty.main.c_lflag &= ~ECHO; /* Disable echo */ | |
1374 tty.main.c_lflag &= ~ICANON; /* Disable erase/kill processing */ | |
1375 #ifdef IEXTEN | |
1376 tty.main.c_lflag &= ~IEXTEN; /* Disable other editing characters. */ | |
1377 #endif | |
1378 tty.main.c_lflag |= ISIG; /* Enable signals */ | |
1379 if (TTY_FLAGS (con).flow_control) | |
1380 { | |
1381 tty.main.c_iflag |= IXON; /* Enable start/stop output control */ | |
1382 #ifdef IXANY | |
1383 tty.main.c_iflag &= ~IXANY; | |
1384 #endif /* IXANY */ | |
1385 } | |
1386 else | |
1387 tty.main.c_iflag &= ~IXON; /* Disable start/stop output control */ | |
1388 tty.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL | |
1389 on output */ | |
513 | 1390 |
1391 #if 0 | |
1392 /* We used to disable tab expansion here, but this is the user's decision. */ | |
1393 #if defined (TABDLY) && defined (TAB3) | |
1394 if ((tty.main.c_oflag & TABDLY) == TAB3) | |
1395 tty.main.c_oflag &= ~TABDLY; /* Disable tab expansion (Posix). */ | |
1396 #elif defined (OXTABS) | |
1397 tty.main.c_oflag &= ~OXTABS; /* Disable tab expansion (BSD). */ | |
1398 #endif | |
1399 #endif /* 0 */ | |
1400 | |
428 | 1401 #ifdef CS8 |
1402 if (TTY_FLAGS (con).meta_key) | |
1403 { | |
1404 tty.main.c_cflag |= CS8; /* allow 8th bit on input */ | |
1405 tty.main.c_cflag &= ~PARENB;/* Don't check parity */ | |
1406 } | |
1407 #endif | |
1408 if (CONSOLE_TTY_DATA (con)->controlling_terminal) | |
1409 { | |
1204 | 1410 tty.main.c_cc[VINTR] = /* C-g (usually) gives SIGINT */ |
2828 | 1411 event_to_character (CONSOLE_QUIT_EVENT (con), 0, 1); |
428 | 1412 /* Set up C-g for both SIGQUIT and SIGINT. |
1413 We don't know which we will get, but we handle both alike | |
1414 so which one it really gives us does not matter. */ | |
1204 | 1415 tty.main.c_cc[VQUIT] = tty.main.c_cc[VINTR]; |
428 | 1416 } |
1417 else | |
1418 { | |
1419 tty.main.c_cc[VINTR] = _POSIX_VDISABLE; | |
1420 tty.main.c_cc[VQUIT] = _POSIX_VDISABLE; | |
1421 } | |
1422 tty.main.c_cc[VMIN] = 1; /* Input should wait for at | |
1423 least 1 char */ | |
1424 tty.main.c_cc[VTIME] = 0; /* no matter how long that takes. */ | |
1425 #ifdef VSWTCH | |
1426 tty.main.c_cc[VSWTCH] = _POSIX_VDISABLE; /* Turn off shell layering use | |
1427 of C-z */ | |
1428 #endif /* VSWTCH */ | |
1429 /* There was some conditionalizing here on (mips or TCATTR), but | |
1430 I think that's wrong. There was one report of C-y (DSUSP) not being | |
1431 disabled on HP9000s700 systems, and this might fix it. */ | |
1432 #ifdef VSUSP | |
1433 tty.main.c_cc[VSUSP] = _POSIX_VDISABLE; /* Turn off mips handling of C-z. */ | |
1434 #endif /* VSUSP */ | |
1435 #ifdef V_DSUSP | |
1436 tty.main.c_cc[V_DSUSP] = _POSIX_VDISABLE; /* Turn off mips handling of C-y. */ | |
1437 #endif /* V_DSUSP */ | |
1438 #ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP. */ | |
1439 tty.main.c_cc[VDSUSP] = _POSIX_VDISABLE; | |
1440 #endif /* VDSUSP */ | |
1441 #ifdef VLNEXT | |
1442 tty.main.c_cc[VLNEXT] = _POSIX_VDISABLE; | |
1443 #endif /* VLNEXT */ | |
1444 #ifdef VREPRINT | |
1445 tty.main.c_cc[VREPRINT] = _POSIX_VDISABLE; | |
1446 #endif /* VREPRINT */ | |
1447 #ifdef VWERASE | |
1448 tty.main.c_cc[VWERASE] = _POSIX_VDISABLE; | |
1449 #endif /* VWERASE */ | |
1450 #ifdef VDISCARD | |
1451 tty.main.c_cc[VDISCARD] = _POSIX_VDISABLE; | |
1452 #endif /* VDISCARD */ | |
1453 #ifdef VSTART | |
1454 tty.main.c_cc[VSTART] = _POSIX_VDISABLE; | |
1455 #endif /* VSTART */ | |
1456 #ifdef VSTRT | |
1457 tty.main.c_cc[VSTRT] = _POSIX_VDISABLE; /* called VSTRT on some systems */ | |
1458 #endif /* VSTART */ | |
1459 #ifdef VSTOP | |
1460 tty.main.c_cc[VSTOP] = _POSIX_VDISABLE; | |
1461 #endif /* VSTOP */ | |
1462 | |
1463 #ifdef AIX | |
1464 #ifndef IBMR2AIX | |
1465 /* AIX enhanced edit loses NULs, so disable it. */ | |
1466 tty.main.c_line = 0; | |
1467 tty.main.c_iflag &= ~ASCEDIT; | |
1468 #else | |
1469 tty.main.c_cc[VSTRT] = 255; | |
1470 tty.main.c_cc[VSTOP] = 255; | |
1471 tty.main.c_cc[VSUSP] = 255; | |
1472 tty.main.c_cc[VDSUSP] = 255; | |
1473 #endif /* IBMR2AIX */ | |
1474 /* Also, PTY overloads NUL and BREAK. | |
1475 don't ignore break, but don't signal either, so it looks like NUL. | |
1476 This really serves a purpose only if running in an XTERM window | |
1477 or via TELNET or the like, but does no harm elsewhere. */ | |
1478 tty.main.c_iflag &= ~IGNBRK; | |
1479 tty.main.c_iflag &= ~BRKINT; | |
1480 #endif /* AIX */ | |
1481 #else /* if not HAVE_TERMIO */ | |
1482 con->tty_erase_char = make_char (tty.main.sg_erase); | |
1483 tty.main.sg_flags &= ~(ECHO | CRMOD | XTABS); | |
1484 if (TTY_FLAGS (con).meta_key) | |
1485 tty.main.sg_flags |= ANYP; | |
1486 /* #### should we be using RAW mode here? */ | |
1487 tty.main.sg_flags |= /* interrupt_input ? RAW : */ CBREAK; | |
1488 #endif /* not HAVE_TERMIO */ | |
1489 | |
1490 /* If going to use CBREAK mode, we must request C-g to interrupt | |
1491 and turn off start and stop chars, etc. If not going to use | |
1492 CBREAK mode, do this anyway so as to turn off local flow | |
1493 control for user coming over network on 4.2; in this case, | |
1494 only t_stopc and t_startc really matter. */ | |
1495 #ifndef HAVE_TERMIO | |
1496 #ifdef HAVE_TCHARS | |
1497 /* Note: if not using CBREAK mode, it makes no difference how we | |
1498 set this */ | |
1499 tty.tchars = new_tchars; | |
2828 | 1500 tty.tchars.t_intrc = event_to_character (CONSOLE_QUIT_EVENT (con), 0, 1); |
428 | 1501 if (TTY_FLAGS (con).flow_control) |
1502 { | |
1503 tty.tchars.t_startc = '\021'; | |
1504 tty.tchars.t_stopc = '\023'; | |
1505 } | |
1506 | |
1507 tty.lmode = LDECCTQ | LLITOUT | LPASS8 | LNOFLSH | | |
1508 CONSOLE_TTY_DATA (con)->old_tty.lmode; | |
1509 #endif /* HAVE_TCHARS */ | |
1510 #endif /* not HAVE_TERMIO */ | |
1511 | |
1512 #ifdef HAVE_LTCHARS | |
1513 tty.ltchars = new_ltchars; | |
1514 #endif /* HAVE_LTCHARS */ | |
1515 | |
430 | 1516 emacs_set_tty (input_fd, &tty, 0); |
428 | 1517 |
1518 /* This code added to insure that, if flow-control is not to be used, | |
1519 we have an unlocked terminal at the start. */ | |
1520 | |
1521 #ifdef TCXONC | |
1522 if (!TTY_FLAGS (con).flow_control) ioctl (input_fd, TCXONC, 1); | |
1523 #endif | |
1524 #ifdef TIOCSTART | |
1525 if (!TTY_FLAGS (con).flow_control) ioctl (input_fd, TIOCSTART, 0); | |
1526 #endif | |
1527 | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
3700
diff
changeset
|
1528 #if defined (HAVE_TERMIOS) |
428 | 1529 #ifdef TCOON |
1530 if (!TTY_FLAGS (con).flow_control) tcflow (input_fd, TCOON); | |
1531 #endif | |
1532 #endif | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
3700
diff
changeset
|
1533 |
428 | 1534 set_tty_modes (con); |
1535 } | |
1536 | |
1537 #endif /* HAVE_TTY */ | |
1538 | |
1539 void | |
2340 | 1540 init_one_device ( |
1541 #if defined(HAVE_TTY) || (defined(SIGIO) && !defined(BROKEN_SIGIO)) | |
1542 struct device *d | |
1543 #else | |
1544 struct device *UNUSED (d) | |
1545 #endif | |
1546 ) | |
428 | 1547 { |
1548 #ifdef HAVE_TTY | |
1549 if (DEVICE_TTY_P (d)) | |
1550 tty_init_sys_modes_on_device (d); | |
1551 #endif | |
1552 #if defined(SIGIO) && !defined(BROKEN_SIGIO) | |
1553 if (!DEVICE_STREAM_P (d)) | |
1554 { | |
1555 init_sigio_on_device (d); | |
1556 request_sigio_on_device (d); | |
1557 } | |
1558 #endif | |
1559 } | |
1560 | |
1561 void | |
1562 init_one_console (struct console *con) | |
1563 { | |
1564 Lisp_Object devcons; | |
1565 | |
1566 CONSOLE_DEVICE_LOOP (devcons, con) | |
1567 { | |
1568 struct device *d = XDEVICE (XCAR (devcons)); | |
1569 | |
1570 init_one_device (d); | |
1571 } | |
1572 } | |
1573 | |
1574 void | |
1575 reinit_initial_console (void) | |
1576 { | |
1577 munge_process_groups (); | |
1578 if (CONSOLEP (Vcontrolling_terminal) && | |
1579 CONSOLE_LIVE_P (XCONSOLE (Vcontrolling_terminal))) | |
1580 init_one_console (XCONSOLE (Vcontrolling_terminal)); | |
1581 } | |
1582 | |
1583 | |
1584 /* ------------------------------------------------------ */ | |
1585 /* Other TTY functions */ | |
1586 /* ------------------------------------------------------ */ | |
1587 | |
1588 #ifdef HAVE_TTY | |
1589 | |
1590 #if 0 /* not currently used */ | |
1591 | |
1592 /* Return nonzero if safe to use tabs in output. | |
1593 At the time this is called, init_sys_modes has not been done yet. */ | |
1594 | |
1595 int | |
2340 | 1596 tabs_safe_p (struct device *USED_IF_TTY (d)) |
428 | 1597 { |
1598 #ifdef HAVE_TTY | |
1599 if (DEVICE_TTY_P (d)) | |
1600 { | |
1601 struct emacs_tty tty; | |
1602 | |
430 | 1603 emacs_get_tty (DEVICE_INFD (d), &tty); |
428 | 1604 return EMACS_TTY_TABS_OK (&tty); |
1605 } | |
1606 #endif | |
1607 return 1; | |
1608 } | |
1609 | |
1610 #endif /* 0 */ | |
1611 | |
1612 /* Get terminal size from system. | |
1613 Store number of lines into *heightp and width into *widthp. | |
1614 If zero or a negative number is stored, the value is not valid. */ | |
1615 | |
1616 void | |
1617 get_tty_device_size (struct device *d, int *widthp, int *heightp) | |
1618 { | |
1619 int input_fd = DEVICE_INFD (d); | |
1620 | |
1621 assert (DEVICE_TTY_P (d)); | |
1622 | |
1623 #ifdef TIOCGWINSZ | |
1624 { | |
1625 /* BSD-style. */ | |
1626 struct winsize size; | |
1627 | |
1628 if (ioctl (input_fd, TIOCGWINSZ, &size) == -1) | |
1629 *widthp = *heightp = 0; | |
1630 else | |
1631 { | |
1632 *widthp = size.ws_col; | |
1633 *heightp = size.ws_row; | |
1634 } | |
1635 } | |
1636 #elif defined TIOCGSIZE | |
1637 { | |
1638 /* SunOS - style. */ | |
1639 struct ttysize size; | |
1640 | |
1641 if (ioctl (input_fd, TIOCGSIZE, &size) == -1) | |
1642 *widthp = *heightp = 0; | |
1643 else | |
1644 { | |
1645 *widthp = size.ts_cols; | |
1646 *heightp = size.ts_lines; | |
1647 } | |
1648 } | |
1649 #else /* system doesn't know size */ | |
1650 | |
1651 *widthp = 0; | |
1652 *heightp = 0; | |
1653 | |
1654 #endif /* not !TIOCGWINSZ */ | |
1655 } | |
1656 | |
1657 #endif /* HAVE_TTY */ | |
1658 | |
1659 | |
1660 /* ------------------------------------------------------ */ | |
1661 /* Is device 8 bit ? */ | |
1662 /* ------------------------------------------------------ */ | |
1663 | |
1664 #ifdef HAVE_TTY | |
1665 | |
1666 int | |
1667 eight_bit_tty (struct device *d) | |
1668 { | |
1669 struct emacs_tty s; | |
1670 int input_fd; | |
1671 int eight_bit = 0; | |
1672 | |
1673 assert (DEVICE_TTY_P (d)); | |
1674 input_fd = DEVICE_INFD (d); | |
1675 | |
430 | 1676 emacs_get_tty (input_fd, &s); |
428 | 1677 |
1678 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS) | |
1679 eight_bit = (s.main.c_cflag & CSIZE) == CS8; | |
1680 #else | |
1681 eight_bit = 0; /* I don't know how to do it */ | |
1682 #endif | |
1683 return eight_bit; | |
1684 } | |
1685 | |
1686 #endif /* HAVE_TTY */ | |
1687 | |
1688 | |
1689 /* ------------------------------------------------------ */ | |
1690 /* Resetting a device */ | |
1691 /* ------------------------------------------------------ */ | |
1692 | |
1693 #ifdef HAVE_TTY | |
1694 | |
1695 /* Prepare the terminal for exiting Emacs; move the cursor to the | |
1696 bottom of the frame, turn off interrupt-driven I/O, etc. */ | |
1697 static void | |
1698 tty_reset_sys_modes_on_device (struct device *d) | |
1699 { | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
3700
diff
changeset
|
1700 #if defined (BSD) |
2286 | 1701 int output_fd; |
1702 #endif | |
1703 int input_fd; | |
428 | 1704 struct console *con = XCONSOLE (DEVICE_CONSOLE (d)); |
1705 | |
1706 input_fd = CONSOLE_TTY_DATA (con)->infd; | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
3700
diff
changeset
|
1707 #if defined (BSD) |
428 | 1708 output_fd = CONSOLE_TTY_DATA (con)->outfd; |
2286 | 1709 #endif |
428 | 1710 |
1711 tty_redisplay_shutdown (con); | |
1712 /* reset_tty_modes() flushes the connection at its end. */ | |
1713 reset_tty_modes (con); | |
1714 | |
1715 #if defined (BSD) | |
1716 /* Avoid possible loss of output when changing terminal modes. */ | |
1717 fsync (output_fd); | |
1718 #endif | |
1719 | |
430 | 1720 while (emacs_set_tty (input_fd, &CONSOLE_TTY_DATA (con)->old_tty, 0) |
428 | 1721 < 0 && errno == EINTR) |
1722 ; | |
1723 } | |
1724 | |
1725 #endif /* HAVE_TTY */ | |
1726 | |
1727 void | |
1728 reset_one_device (struct device *d) | |
1729 { | |
1730 #ifdef HAVE_TTY | |
1731 if (DEVICE_TTY_P (d)) | |
1732 tty_reset_sys_modes_on_device (d); | |
1733 else | |
1734 #endif | |
1735 if (DEVICE_STREAM_P (d)) | |
1736 fflush (CONSOLE_STREAM_DATA (XCONSOLE (DEVICE_CONSOLE (d)))->out); | |
1737 #if defined(SIGIO) && !defined(BROKEN_SIGIO) | |
1738 if (!DEVICE_STREAM_P (d)) | |
1739 { | |
1740 unrequest_sigio_on_device (d); | |
1741 reset_sigio_on_device (d); | |
1742 } | |
1743 #endif | |
1744 } | |
1745 | |
1746 void | |
1747 reset_one_console (struct console *con) | |
1748 { | |
1749 /* Note: this can be called during GC. */ | |
1750 Lisp_Object devcons; | |
1751 | |
1752 CONSOLE_DEVICE_LOOP (devcons, con) | |
1753 { | |
1754 struct device *d = XDEVICE (XCAR (devcons)); | |
1755 | |
1756 reset_one_device (d); | |
1757 } | |
1758 } | |
1759 | |
1760 void | |
1761 reset_all_consoles (void) | |
1762 { | |
1763 /* Note: this can be called during GC. */ | |
1764 Lisp_Object concons; | |
1765 | |
1766 CONSOLE_LOOP (concons) | |
1767 { | |
1768 struct console *con = XCONSOLE (XCAR (concons)); | |
1769 | |
1770 reset_one_console (con); | |
1771 } | |
1772 | |
1773 unmunge_process_groups (); | |
1774 } | |
1775 | |
1776 void | |
1777 reset_initial_console (void) | |
1778 { | |
1779 if (CONSOLEP (Vcontrolling_terminal) && | |
1780 CONSOLE_LIVE_P (XCONSOLE (Vcontrolling_terminal))) | |
1781 reset_one_console (XCONSOLE (Vcontrolling_terminal)); | |
1782 unmunge_process_groups (); | |
1783 } | |
1784 | |
1785 | |
1786 /************************************************************************/ | |
1787 /* limits of text/data segments */ | |
1788 /************************************************************************/ | |
1789 | |
801 | 1790 /* Need start_of_data() as much as possible now, for total_data_usage(); |
1791 but with PDUMP and WIN32_NATIVE, can't currently do it. */ | |
1330 | 1792 #if ! (defined (PDUMP) && defined (WIN32_NATIVE) && defined (SYSTEM_MALLOC)) |
428 | 1793 #define NEED_STARTS |
1794 #endif | |
1795 | |
1796 #ifdef NEED_STARTS | |
1797 /* Some systems that cannot dump also cannot implement these. */ | |
1798 | |
1799 /* | |
1800 * Return the address of the start of the text segment prior to | |
1801 * doing an unexec. After unexec the return value is undefined. | |
1802 * See crt0.c for further explanation and _start. | |
1803 * | |
1804 */ | |
1805 | |
801 | 1806 #if !defined (HAVE_TEXT_START) && !defined (PDUMP) |
440 | 1807 |
442 | 1808 EXTERN_C int _start (void); |
428 | 1809 |
1810 char * | |
1811 start_of_text (void) | |
1812 { | |
1813 #ifdef TEXT_START | |
442 | 1814 return (char *) TEXT_START; |
428 | 1815 #else |
442 | 1816 return (char *) _start; |
428 | 1817 #endif /* TEXT_START */ |
1818 } | |
440 | 1819 #endif /* !defined(HAVE_TEXT_START) && !defined(PDUMP) */ |
428 | 1820 |
1821 /* | |
1822 * Return the address of the start of the data segment prior to | |
1823 * doing an unexec. After unexec the return value is undefined. | |
442 | 1824 * See ecrt0.c for further information and definition of data_start. |
428 | 1825 * |
1826 * Apparently, on BSD systems this is etext at startup. On | |
1827 * USG systems (swapping) this is highly mmu dependent and | |
1828 * is also dependent on whether or not the program is running | |
1829 * with shared text. Generally there is a (possibly large) | |
1830 * gap between end of text and start of data with shared text. | |
1831 * | |
1832 * On Uniplus+ systems with shared text, data starts at a | |
1833 * fixed address. Each port (from a given oem) is generally | |
1834 * different, and the specific value of the start of data can | |
1835 * be obtained via the UniPlus+ specific "uvar" system call, | |
1836 * however the method outlined in crt0.c seems to be more portable. | |
1837 * | |
1838 * Probably what will have to happen when a USG unexec is available, | |
1839 * at least on UniPlus, is temacs will have to be made unshared so | |
1840 * that text and data are contiguous. Then once loadup is complete, | |
1841 * unexec will produce a shared executable where the data can be | |
1842 * at the normal shared text boundary and the startofdata variable | |
1843 * will be patched by unexec to the correct value. | |
1844 * | |
1845 */ | |
1846 | |
801 | 1847 #if defined (ORDINARY_LINK) && !defined (MINGW) |
428 | 1848 extern char **environ; |
1849 #endif | |
1850 | |
1851 void * | |
1852 start_of_data (void) | |
1853 { | |
1854 #ifdef DATA_START | |
1855 return ((char *) DATA_START); | |
1856 #else | |
452 | 1857 #if defined (ORDINARY_LINK) || defined(PDUMP) |
428 | 1858 /* |
1859 * This is a hack. Since we're not linking crt0.c or pre_crt0.c, | |
1860 * data_start isn't defined. We take the address of environ, which | |
1861 * is known to live at or near the start of the system crt0.c, and | |
1862 * we don't sweat the handful of bytes that might lose. | |
1863 */ | |
442 | 1864 #if defined (HEAP_IN_DATA) && !defined(PDUMP) |
428 | 1865 extern char* static_heap_base; |
1866 if (!initialized) | |
1867 return static_heap_base; | |
1868 #endif | |
801 | 1869 return ((char *) &environ); |
428 | 1870 #else |
1871 extern int data_start; | |
1872 return ((char *) &data_start); | |
1873 #endif /* ORDINARY_LINK */ | |
1874 #endif /* DATA_START */ | |
1875 } | |
1330 | 1876 #endif /* NEED_STARTS aka !(PDUMP && WIN32_NATIVE && SYSTEM_MALLOC) */ |
428 | 1877 |
801 | 1878 extern void *minimum_address_seen; /* from xmalloc() */ |
1879 extern void *maximum_address_seen; /* from xmalloc() */ | |
1880 | |
1881 Bytecount | |
1882 total_data_usage (void) | |
1883 { | |
1884 #ifdef NEED_STARTS | |
1885 void *data_start = start_of_data (); | |
1886 #else | |
1887 void *data_start = minimum_address_seen; | |
1888 #endif | |
854 | 1889 |
1315 | 1890 #ifndef WIN32_ANY |
814 | 1891 void *data_end = sbrk (0); |
801 | 1892 #else |
1893 void *data_end = maximum_address_seen; | |
1894 #endif | |
1895 | |
1896 /* Sanity checking -- the min determined by malloc() should always be | |
1897 greater than data start determined by other means. We could do the | |
1898 same check on the max, except that things like rel-alloc might | |
1899 invalidate it. */ | |
1900 if (minimum_address_seen && | |
1901 (char *) minimum_address_seen < (char *) data_start) | |
1902 data_start = minimum_address_seen; | |
1903 | |
1904 if (data_end < data_start) /* Huh?????????? */ | |
1905 data_end = maximum_address_seen; | |
1906 | |
1907 /* #### Doesn't seem to give good results on Windows; values are much | |
1908 higher than actual memory usage. How to fix??? */ | |
1909 return (char *) data_end - (char *) data_start; | |
1910 } | |
1911 | |
428 | 1912 |
1913 /************************************************************************/ | |
1914 /* get the system name */ | |
1915 /************************************************************************/ | |
1916 | |
1917 /* init_system_name sets up the string for the Lisp function | |
1918 system-name to return. */ | |
1919 | |
1920 extern Lisp_Object Vsystem_name; | |
1921 | |
1922 void | |
1923 init_system_name (void) | |
1924 { | |
442 | 1925 #if defined (WIN32_NATIVE) |
771 | 1926 Extbyte hostname[MAX_XETCHAR_SIZE * (MAX_COMPUTERNAME_LENGTH + 1)]; |
1927 DWORD size = sizeof (hostname) / XETCHAR_SIZE; | |
1928 qxeGetComputerName (hostname, &size); | |
1929 Vsystem_name = build_tstr_string (hostname); | |
428 | 1930 #elif !defined (HAVE_GETHOSTNAME) |
1931 struct utsname uts; | |
1932 uname (&uts); | |
1933 Vsystem_name = build_string (uts.nodename); | |
1934 #else /* HAVE_GETHOSTNAME */ | |
647 | 1935 int hostname_size = 256; |
2367 | 1936 /* !!#### Needs review */ |
851 | 1937 char *hostname = (char *) ALLOCA (hostname_size); |
428 | 1938 |
1939 /* Try to get the host name; if the buffer is too short, try | |
1940 again. Apparently, the only indication gethostname gives of | |
1941 whether the buffer was large enough is the presence or absence | |
1942 of a '\0' in the string. Eech. */ | |
1943 for (;;) | |
1944 { | |
1945 gethostname (hostname, hostname_size - 1); | |
1946 hostname[hostname_size - 1] = '\0'; | |
1947 | |
1948 /* Was the buffer large enough for the '\0'? */ | |
647 | 1949 if ((int) strlen (hostname) < (hostname_size - 1)) |
428 | 1950 break; |
1951 | |
1952 hostname_size <<= 1; | |
2367 | 1953 /* !!#### Needs review */ |
851 | 1954 hostname = (char *) ALLOCA (hostname_size); |
428 | 1955 } |
558 | 1956 # if defined( HAVE_SOCKETS) |
428 | 1957 /* Turn the hostname into the official, fully-qualified hostname. |
1958 Don't do this if we're going to dump; this can confuse system | |
1959 libraries on some machines and make the dumped emacs core dump. */ | |
1960 if (initialized) | |
1961 if (!strchr (hostname, '.')) | |
1962 { | |
440 | 1963 # if !(defined(HAVE_GETADDRINFO) && defined(HAVE_GETNAMEINFO)) |
428 | 1964 struct hostent *hp = NULL; |
1965 int count; | |
440 | 1966 # ifdef TRY_AGAIN |
428 | 1967 for (count = 0; count < 10; count++) |
1968 { | |
1969 h_errno = 0; | |
440 | 1970 # endif |
428 | 1971 /* Some systems can't handle SIGALARM/SIGIO in gethostbyname(). */ |
1972 stop_interrupts (); | |
1973 hp = gethostbyname (hostname); | |
1974 start_interrupts (); | |
440 | 1975 # ifdef TRY_AGAIN |
428 | 1976 if (! (hp == 0 && h_errno == TRY_AGAIN)) |
1977 break; | |
1978 Fsleep_for (make_int (1)); | |
1979 } | |
440 | 1980 # endif |
428 | 1981 if (hp) |
1982 { | |
442 | 1983 const char *fqdn = (const char *) hp->h_name; |
428 | 1984 |
1985 if (!strchr (fqdn, '.')) | |
1986 { | |
1987 /* We still don't have a fully qualified domain name. | |
1988 Try to find one in the list of alternate names */ | |
1989 char **alias = hp->h_aliases; | |
1990 while (*alias && !strchr (*alias, '.')) | |
1991 alias++; | |
1992 if (*alias) | |
1993 fqdn = *alias; | |
1994 } | |
2367 | 1995 /* !!#### Needs review */ |
851 | 1996 hostname = (char *) ALLOCA (strlen (fqdn) + 1); |
428 | 1997 strcpy (hostname, fqdn); |
1998 } | |
440 | 1999 # else /* !(HAVE_GETADDRINFO && HAVE_GETNAMEINFO) */ |
2000 struct addrinfo hints, *res; | |
2001 | |
2002 xzero (hints); | |
2003 hints.ai_flags = AI_CANONNAME; | |
724 | 2004 #ifdef IPV6_CANONICALIZE |
440 | 2005 hints.ai_family = AF_UNSPEC; |
724 | 2006 #else |
2007 hints.ai_family = PF_INET; | |
2008 #endif | |
440 | 2009 hints.ai_socktype = SOCK_STREAM; |
2010 hints.ai_protocol = 0; | |
2011 if (!getaddrinfo (hostname, NULL, &hints, &res)) | |
2012 { | |
2367 | 2013 /* !!#### Needs review */ |
851 | 2014 hostname = (char *) ALLOCA (strlen (res->ai_canonname) + 1); |
440 | 2015 strcpy (hostname, res->ai_canonname); |
2016 | |
2017 freeaddrinfo (res); | |
2018 } | |
2019 # endif /* !(HAVE_GETADDRINFO && HAVE_GETNAMEINFO) */ | |
428 | 2020 } |
2021 # endif /* HAVE_SOCKETS */ | |
2022 Vsystem_name = build_string (hostname); | |
2023 #endif /* HAVE_GETHOSTNAME */ | |
2024 { | |
867 | 2025 Ibyte *p; |
428 | 2026 Bytecount i; |
2027 | |
2028 for (i = 0, p = XSTRING_DATA (Vsystem_name); | |
2029 i < XSTRING_LENGTH (Vsystem_name); | |
2030 i++, p++) | |
2031 { | |
2032 if (*p == ' ' || *p == '\t') | |
2033 *p = '-'; | |
2034 } | |
2035 } | |
2036 } | |
2037 | |
2038 | |
2039 /************************************************************************/ | |
2040 /* Emulation of select() */ | |
2041 /************************************************************************/ | |
2042 | |
2043 #ifndef HAVE_SELECT | |
2044 | |
2045 ERROR: XEmacs requires a working select(). | |
2046 | |
2047 #endif /* not HAVE_SELECT */ | |
2048 | |
2049 | |
2050 /************************************************************************/ | |
2051 /* Emulation of signal stuff */ | |
2052 /************************************************************************/ | |
2053 | |
2054 /* BSD 4.1 crap deleted. 4.2 was released in 1983, for God's sake! I | |
2055 can't imagine that anyone is actually running that OS any more. | |
2056 You can't use X under it (I think) because there's no select(). | |
2057 Anyway, the signal stuff has all been changed. If someone wants to | |
2058 get this stuff working again, look in the FSF Emacs sources. */ | |
2059 | |
2060 /* POSIX signals support - DJB */ | |
2061 | |
2062 #ifdef HAVE_SIGPROCMASK | |
2063 | |
2064 /* #### Is there any reason this is static global rather than local? */ | |
2065 static struct sigaction new_action, old_action; | |
2066 | |
2067 signal_handler_t | |
613 | 2068 qxe_reliable_signal (int signal_number, signal_handler_t action) |
428 | 2069 { |
2070 #if 0 | |
2071 | |
2072 /* XEmacs works better if system calls are *not* restarted. | |
2073 This allows C-g to interrupt reads and writes, on most systems. | |
2074 | |
2075 #### Another possibility is to just longjmp() out of the signal | |
2076 handler. According to W.R. Stevens, this should be OK on all | |
2077 systems. However, I don't want to deal with the potential | |
2078 evil ramifications of this at this point. */ | |
2079 | |
2080 sigemptyset (&new_action.sa_mask); | |
2081 new_action.sa_handler = action; | |
2082 #if defined (SA_RESTART) | |
2083 /* Emacs mostly works better with restartable system services. If this | |
2084 * flag exists, we probably want to turn it on here. | |
2085 */ | |
2086 new_action.sa_flags = SA_RESTART; | |
2087 #else | |
2088 new_action.sa_flags = 0; | |
2089 #endif | |
2090 sigaction (signal_number, &new_action, &old_action); | |
2091 return (old_action.sa_handler); | |
2092 | |
2093 #else /* not 0 */ | |
2094 | |
2095 sigemptyset (&new_action.sa_mask); | |
2096 new_action.sa_handler = action; | |
2097 #if defined (SA_INTERRUPT) /* don't restart system calls, under SunOS */ | |
2098 new_action.sa_flags = SA_INTERRUPT; | |
2099 #else | |
2100 new_action.sa_flags = 0; | |
2101 #endif | |
2102 sigaction (signal_number, &new_action, &old_action); | |
2103 return (signal_handler_t) (old_action.sa_handler); | |
2104 | |
2105 #endif /* not 0 */ | |
2106 } | |
2107 | |
2108 #elif defined (HAVE_SIGBLOCK) | |
2109 | |
2110 /* We use sigvec() rather than signal() if we have it, because | |
2111 it lets us specify interruptible system calls. */ | |
2112 signal_handler_t | |
613 | 2113 qxe_reliable_signal (int signal_number, signal_handler_t action) |
428 | 2114 { |
2115 struct sigvec vec, ovec; | |
2116 | |
2117 vec.sv_handler = action; | |
2118 vec.sv_mask = 0; | |
2119 #ifdef SV_INTERRUPT /* don't restart system calls */ | |
2120 vec.sv_flags = SV_INTERRUPT; | |
2121 #else | |
2122 vec.sv_flags = 0; | |
2123 #endif | |
2124 | |
2125 sigvec (signal_number, &vec, &ovec); | |
2126 | |
2127 return (ovec.sv_handler); | |
2128 } | |
2129 | |
2130 #endif /* HAVE_SIGBLOCK (HAVE_SIGPROCMASK) */ | |
2131 | |
2132 | |
2133 /************************************************************************/ | |
2134 /* Emulation of strerror() and errno support */ | |
2135 /************************************************************************/ | |
2136 | |
2137 #ifndef HAVE_STRERROR | |
2138 | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
3700
diff
changeset
|
2139 #if !defined(__alpha) && !defined(MACH) && !defined(LINUX) && !defined(IRIX) && !defined(__NetBSD__) |
428 | 2140 /* Linux added here by Raymond L. Toy <toy@alydar.crd.ge.com> for XEmacs. */ |
2141 /* Irix added here by gparker@sni-usa.com for XEmacs. */ | |
2142 /* NetBSD added here by James R Grinter <jrg@doc.ic.ac.uk> for XEmacs */ | |
442 | 2143 extern const char *sys_errlist[]; |
428 | 2144 extern int sys_nerr; |
2145 #endif | |
2146 | |
2147 #ifdef __NetBSD__ | |
2148 extern char *sys_errlist[]; | |
2149 extern int sys_nerr; | |
2150 #endif | |
2151 | |
2152 | |
442 | 2153 const char * |
428 | 2154 strerror (int errnum) |
2155 { | |
2156 if (errnum >= 0 && errnum < sys_nerr) | |
2157 return sys_errlist[errnum]; | |
442 | 2158 return ((const char *) GETTEXT ("Unknown error")); |
428 | 2159 } |
2160 | |
2161 #endif /* ! HAVE_STRERROR */ | |
2162 | |
2163 | |
2164 /************************************************************************/ | |
2165 /* Encapsulations of system calls */ | |
2166 /************************************************************************/ | |
2167 | |
2168 /***************** low-level calls ****************/ | |
2169 | |
2170 /* | |
2171 * On USG systems the system calls are INTERRUPTIBLE by signals | |
2172 * that the user program has elected to catch. Thus the system call | |
2173 * must be retried in these cases. To handle this without massive | |
2174 * changes in the source code, we remap the standard system call names | |
2175 * to names for our own functions in sysdep.c that do the system call | |
2176 * with retries. Actually, for portability reasons, it is good | |
2177 * programming practice, as this example shows, to limit all actual | |
2178 * system calls to a single occurrence in the source. Sure, this | |
2179 * adds an extra level of function call overhead but it is almost | |
2180 * always negligible. Fred Fish, Unisoft Systems Inc. | |
2181 */ | |
2182 | |
2183 /* Ben sez: read Dick Gabriel's essay about the Worse Is Better | |
2184 approach to programming and its connection to the silly | |
2185 interruptible-system-call business. To find it, look on | |
2340 | 2186 Jamie's home page (http://www.jwz.org/doc/worse-is-better.html). */ |
428 | 2187 |
771 | 2188 #ifdef WIN32_NATIVE |
2189 | |
2190 static int | |
2191 underlying_open_1 (const Extbyte *path, int oflag, int mode) | |
2192 { | |
2193 if (XEUNICODE_P) | |
2194 return _wopen ((const wchar_t *) path, oflag, mode); | |
2195 else | |
2196 return _open (path, oflag, mode); | |
2197 } | |
2198 | |
2199 #endif /* WIN32_NATIVE */ | |
2200 | |
2367 | 2201 /* Just call open() with normal open() semantics, with some fixups for |
2202 problems under Windows. */ | |
771 | 2203 |
2204 static int | |
2205 underlying_open (const Extbyte *path, int oflag, int mode) | |
2206 { | |
2207 #ifdef WIN32_NATIVE | |
2208 { | |
2209 /* Try to open file without _O_CREAT, to be able to write to hidden | |
2210 and system files. Force all file handles to be | |
2211 non-inheritable. */ | |
2212 int res = underlying_open_1 (path, (oflag & ~_O_CREAT) | _O_NOINHERIT, | |
2213 mode); | |
2214 if (res >= 0) | |
2215 return res; | |
2216 return underlying_open_1 (path, oflag | _O_NOINHERIT, mode); | |
2217 } | |
2218 #else | |
2219 return open (path, oflag, mode); | |
2220 #endif /* WIN32_NATIVE */ | |
2221 } | |
2222 | |
2367 | 2223 static int |
2224 retry_open_1 (const Extbyte *path, int oflag, int mode) | |
428 | 2225 { |
440 | 2226 #ifdef INTERRUPTIBLE_OPEN |
428 | 2227 { |
2228 int rtnval; | |
771 | 2229 while ((rtnval = underlying_open (path, oflag, mode)) == -1 |
428 | 2230 && (errno == EINTR)) |
2231 DO_NOTHING; | |
2232 return rtnval; | |
2233 } | |
2234 #else | |
771 | 2235 return underlying_open (path, oflag, mode); |
428 | 2236 #endif |
2237 } | |
771 | 2238 |
2367 | 2239 /* A version of open() that retries when interrupted. Operates on |
2240 externally-encoded filenames. */ | |
2241 | |
2242 int XCDECL | |
2243 retry_open (const Extbyte *path, int oflag, ...) | |
2244 { | |
2245 int mode; | |
2246 va_list ap; | |
2247 | |
2248 va_start (ap, oflag); | |
2249 mode = va_arg (ap, int); | |
2250 va_end (ap); | |
2251 | |
2252 return retry_open_1 (path, oflag, mode); | |
2253 } | |
2254 | |
2255 #if defined (WIN32_NATIVE) && defined (WEXTTEXT_IS_WIDE) | |
2256 | |
2257 /* Like retry_open() but operate on Wexttext filenames. */ | |
2258 | |
2259 int XCDECL | |
2260 wext_retry_open (const Wexttext *path, int oflag, ...) | |
2261 { | |
2262 int mode; | |
2263 va_list ap; | |
2264 | |
2265 va_start (ap, oflag); | |
2266 mode = va_arg (ap, int); | |
2267 va_end (ap); | |
2268 | |
2269 if (!XEUNICODE_P) | |
2270 return retry_open_1 (WEXTTEXT_TO_MULTIBYTE (path), oflag, mode); | |
2271 else | |
2272 return retry_open_1 ((Extbyte *) path, oflag, mode); | |
2273 } | |
2274 | |
2275 #endif | |
2276 | |
771 | 2277 /* The basic external entry point to open(). Handles conversion to |
2278 external encoding, interruptions, etc. */ | |
2279 | |
872 | 2280 int XCDECL |
867 | 2281 qxe_open (const Ibyte *path, int oflag, ...) |
771 | 2282 { |
2283 Extbyte *pathout; | |
2284 int mode; | |
2285 va_list ap; | |
2286 | |
2287 va_start (ap, oflag); | |
2288 mode = va_arg (ap, int); | |
2289 va_end (ap); | |
2290 | |
2291 PATHNAME_CONVERT_OUT (path, pathout); | |
2292 return retry_open (pathout, oflag, mode); | |
2293 } | |
2294 | |
2295 /* Like qxe_open, only when open() is interrupted by EINTR, check for | |
428 | 2296 QUIT. This allows the callers of this function to be interrupted |
2297 with C-g when, say, reading from named pipes. However, this should | |
1123 | 2298 be used with caution, as it can run random Lisp code (although it |
2299 cannot GC). | |
428 | 2300 |
2301 This function will not function as expected on systems where open() | |
2302 is not interrupted by C-g. However, the worst that can happen is | |
2303 the fallback to simple open(). */ | |
2304 int | |
867 | 2305 qxe_interruptible_open (const Ibyte *path, int oflag, int mode) |
428 | 2306 { |
2307 /* This function can GC */ | |
771 | 2308 Extbyte *pathout; |
2309 | |
2310 PATHNAME_CONVERT_OUT (path, pathout); | |
428 | 2311 |
442 | 2312 #ifdef WIN32_NATIVE |
440 | 2313 /* Make all handles non-inheritable */ |
2314 oflag |= _O_NOINHERIT; | |
2315 #endif | |
2316 | |
428 | 2317 for (;;) |
2318 { | |
771 | 2319 int rtnval = underlying_open (pathout, oflag, mode); |
428 | 2320 if (!(rtnval == -1 && errno == EINTR)) |
2321 return rtnval; | |
2322 /* open() was interrupted. Was QUIT responsible? */ | |
2323 QUIT; | |
2324 } | |
2325 } | |
2326 | |
2327 int | |
771 | 2328 retry_close (int filedes) |
428 | 2329 { |
2330 #ifdef INTERRUPTIBLE_CLOSE | |
2331 int did_retry = 0; | |
2332 REGISTER int rtnval; | |
2333 | |
2334 while ((rtnval = close (filedes)) == -1 | |
2335 && (errno == EINTR)) | |
2336 did_retry = 1; | |
2337 | |
2338 /* If close is interrupted SunOS 4.1 may or may not have closed the | |
2339 file descriptor. If it did the second close will fail with | |
2340 errno = EBADF. That means we have succeeded. */ | |
2341 if (rtnval == -1 && did_retry && errno == EBADF) | |
2342 return 0; | |
2343 | |
2344 return rtnval; | |
2345 #else | |
2346 return close (filedes); | |
2347 #endif | |
2348 } | |
771 | 2349 |
2350 static ssize_t | |
2351 retry_read_1 (int fildes, void *buf, size_t nbyte, int allow_quit) | |
428 | 2352 { |
2353 ssize_t rtnval; | |
2354 | |
2355 /* No harm in looping regardless of the INTERRUPTIBLE_IO setting. */ | |
2356 while ((rtnval = read (fildes, buf, nbyte)) == -1 | |
2357 && (errno == EINTR)) | |
2358 { | |
2359 if (allow_quit) | |
853 | 2360 QUIT; |
428 | 2361 } |
2362 return rtnval; | |
2363 } | |
2364 | |
2365 ssize_t | |
771 | 2366 retry_read (int fildes, void *buf, size_t nbyte) |
428 | 2367 { |
771 | 2368 return retry_read_1 (fildes, buf, nbyte, 0); |
428 | 2369 } |
771 | 2370 |
2371 static ssize_t | |
2372 retry_write_1 (int fildes, const void *buf, size_t nbyte, int allow_quit) | |
428 | 2373 { |
2374 ssize_t bytes_written = 0; | |
442 | 2375 const char *b = (const char *) buf; |
428 | 2376 |
2377 /* No harm in looping regardless of the INTERRUPTIBLE_IO setting. */ | |
2378 while (nbyte > 0) | |
2379 { | |
2380 ssize_t rtnval = write (fildes, b, nbyte); | |
2381 | |
2382 if (allow_quit) | |
853 | 2383 QUIT; |
428 | 2384 |
2385 if (rtnval == -1) | |
2386 { | |
2387 if (errno == EINTR) | |
2388 continue; | |
2389 else | |
2390 return bytes_written ? bytes_written : -1; | |
2391 } | |
2392 b += rtnval; | |
2393 nbyte -= rtnval; | |
2394 bytes_written += rtnval; | |
2395 } | |
2396 return bytes_written; | |
2397 } | |
2398 | |
2399 ssize_t | |
771 | 2400 retry_write (int fildes, const void *buf, size_t nbyte) |
428 | 2401 { |
771 | 2402 return retry_write_1 (fildes, buf, nbyte, 0); |
428 | 2403 } |
771 | 2404 |
2405 /* Versions of read() and write() that allow quitting out of the actual | |
2406 I/O. We don't use immediate_quit (i.e. direct longjmp() out of the | |
2407 signal handler) because that's way too losing. | |
2408 | |
2409 (#### Actually, longjmp()ing out of the signal handler may not be | |
2410 as losing as I thought. See qxe_reliable_signal() in sysdep.c.) */ | |
2411 | |
2412 Bytecount | |
2413 read_allowing_quit (int fildes, void *buf, Bytecount size) | |
2414 { | |
2415 QUIT; | |
2416 return retry_read_1 (fildes, buf, size, 1); | |
2417 } | |
2418 | |
2419 Bytecount | |
2420 write_allowing_quit (int fildes, const void *buf, Bytecount size) | |
2421 { | |
2422 QUIT; | |
2423 return retry_write_1 (fildes, buf, size, 1); | |
2424 } | |
428 | 2425 |
2426 | |
2427 /**************** stdio calls ****************/ | |
2428 | |
2429 /* There is at least some evidence that the stdio calls are interruptible | |
2430 just like the normal system calls, at least on some systems. In any | |
2431 case, it doesn't hurt to encapsulate them. */ | |
2432 | |
2433 /* #### Should also encapsulate fflush(). | |
2434 #### Should conceivably encapsulate getchar() etc. What a pain! */ | |
2435 | |
2436 FILE * | |
2367 | 2437 retry_fopen (const Extbyte *path, const Ascbyte *mode) |
428 | 2438 { |
771 | 2439 #ifdef WIN32_NATIVE |
2440 int fd; | |
2441 int oflag; | |
2367 | 2442 const Ascbyte *mode_save = mode; |
771 | 2443 |
2444 /* Force all file handles to be non-inheritable. This is necessary to | |
2445 ensure child processes don't unwittingly inherit handles that might | |
2446 prevent future file access. */ | |
2447 | |
2448 if (mode[0] == 'r') | |
2449 oflag = O_RDONLY; | |
2450 else if (mode[0] == 'w' || mode[0] == 'a') | |
2451 oflag = O_WRONLY | O_CREAT | O_TRUNC; | |
2452 else | |
2453 return NULL; | |
2454 | |
2455 /* Only do simplistic option parsing. */ | |
2456 while (*++mode) | |
2457 if (mode[0] == '+') | |
2458 { | |
2459 oflag &= ~(O_RDONLY | O_WRONLY); | |
2460 oflag |= O_RDWR; | |
2461 } | |
2462 else if (mode[0] == 'b') | |
2463 { | |
2464 oflag &= ~O_TEXT; | |
2465 oflag |= O_BINARY; | |
2466 } | |
2467 else if (mode[0] == 't') | |
2468 { | |
2469 oflag &= ~O_BINARY; | |
2470 oflag |= O_TEXT; | |
2471 } | |
2472 else break; | |
2473 | |
2474 fd = underlying_open (path, oflag, 0644); | |
2475 if (fd < 0) | |
2476 return NULL; | |
2477 | |
2478 return _fdopen (fd, mode_save); | |
428 | 2479 #elif defined (INTERRUPTIBLE_OPEN) |
2480 { | |
2481 FILE *rtnval; | |
771 | 2482 while (!(rtnval = fopen (path, mode)) && (errno == EINTR)) |
428 | 2483 DO_NOTHING; |
2484 return rtnval; | |
2485 } | |
2486 #else | |
771 | 2487 return fopen (path, mode); |
2488 #endif /* defined (INTERRUPTIBLE_OPEN) */ | |
428 | 2489 } |
771 | 2490 |
2491 FILE * | |
2367 | 2492 qxe_fopen (const Ibyte *path, const Ascbyte *mode) |
771 | 2493 { |
2494 Extbyte *pathout; | |
2495 PATHNAME_CONVERT_OUT (path, pathout); | |
2496 return retry_fopen (pathout, mode); | |
2497 } | |
2498 | |
428 | 2499 int |
771 | 2500 retry_fclose (FILE *stream) |
428 | 2501 { |
2502 #ifdef INTERRUPTIBLE_CLOSE | |
2503 int rtnval; | |
2504 | |
2505 while ((rtnval = fclose (stream)) == EOF | |
2506 && (errno == EINTR)) | |
2507 ; | |
2508 return rtnval; | |
2509 #else | |
2510 return fclose (stream); | |
2511 #endif | |
2512 } | |
771 | 2513 |
428 | 2514 size_t |
771 | 2515 retry_fread (void *ptr, size_t size, size_t nitem, FILE *stream) |
428 | 2516 { |
2517 #ifdef INTERRUPTIBLE_IO | |
2518 size_t rtnval; | |
2519 size_t items_read = 0; | |
2520 char *b = (char *) ptr; | |
2521 | |
2522 while (nitem > 0) | |
2523 { | |
2524 rtnval = fread (b, size, nitem, stream); | |
2525 if (rtnval == 0) | |
2526 { | |
2527 if (ferror (stream) && errno == EINTR) | |
2528 continue; | |
2529 else | |
2530 return items_read; | |
2531 } | |
2532 b += size*rtnval; | |
2533 nitem -= rtnval; | |
2534 items_read += rtnval; | |
2535 } | |
2536 return (items_read); | |
2537 #else | |
2538 return fread (ptr, size, nitem, stream); | |
2539 #endif | |
2540 } | |
771 | 2541 |
428 | 2542 size_t |
771 | 2543 retry_fwrite (const void *ptr, size_t size, size_t nitem, FILE *stream) |
428 | 2544 { |
2545 #ifdef INTERRUPTIBLE_IO | |
2546 size_t rtnval; | |
2547 size_t items_written = 0; | |
442 | 2548 const char *b = (const char *) ptr; |
428 | 2549 |
2550 while (nitem > 0) | |
2551 { | |
2552 rtnval = fwrite (b, size, nitem, stream); | |
2553 if (rtnval == 0) | |
2554 { | |
2555 if (ferror (stream) && errno == EINTR) | |
2556 continue; | |
2557 else | |
2558 return items_written; | |
2559 } | |
2560 b += size*rtnval; | |
2561 nitem -= rtnval; | |
2562 items_written += rtnval; | |
2563 } | |
2564 return (items_written); | |
2565 #else | |
2566 return fwrite (ptr, size, nitem, stream); | |
2567 #endif | |
2568 } | |
2569 | |
2570 /********************* directory calls *******************/ | |
2571 | |
2572 int | |
867 | 2573 qxe_chdir (const Ibyte *path) |
428 | 2574 { |
771 | 2575 Extbyte *pathout; |
2576 PATHNAME_CONVERT_OUT (path, pathout); | |
442 | 2577 #ifdef WIN32_NATIVE |
771 | 2578 if (XEUNICODE_P) |
2579 return _wchdir ((const wchar_t *) pathout); | |
2580 else | |
2581 return _chdir (pathout); | |
428 | 2582 #else |
771 | 2583 return chdir (pathout); |
428 | 2584 #endif |
2585 } | |
771 | 2586 |
2587 int | |
2340 | 2588 qxe_mkdir (const Ibyte *path, |
2589 #ifdef WIN32_NATIVE | |
2590 mode_t UNUSED (mode) | |
2591 #else | |
2592 mode_t mode | |
2593 #endif | |
2594 ) | |
771 | 2595 { |
2596 Extbyte *pathout; | |
2597 PATHNAME_CONVERT_OUT (path, pathout); | |
2598 #ifdef WIN32_NATIVE | |
2599 if (XEUNICODE_P) | |
2600 return _wmkdir ((const wchar_t *) pathout); | |
2601 else | |
2602 return _mkdir (pathout); | |
2603 #else | |
2604 return mkdir (pathout, mode); | |
2605 #endif | |
2606 } | |
2607 | |
428 | 2608 DIR * |
867 | 2609 qxe_opendir (const Ibyte *filename) |
428 | 2610 { |
771 | 2611 #ifdef WIN32_NATIVE |
2612 return mswindows_opendir (filename); | |
2613 #else | |
428 | 2614 DIR *rtnval; |
771 | 2615 Extbyte *pathout; |
2616 PATHNAME_CONVERT_OUT (filename, pathout); | |
2617 | |
2618 while (!(rtnval = opendir (pathout)) | |
428 | 2619 && (errno == EINTR)) |
2620 ; | |
2621 return rtnval; | |
771 | 2622 #endif /* WIN32_NATIVE */ |
428 | 2623 } |
771 | 2624 |
428 | 2625 DIRENTRY * |
771 | 2626 qxe_readdir (DIR *dirp) |
428 | 2627 { |
771 | 2628 #ifdef WIN32_NATIVE |
2629 return mswindows_readdir (dirp); | |
2630 #else /* not WIN32_NATIVE */ | |
428 | 2631 DIRENTRY *rtnval; |
2632 | |
2633 /* Apparently setting errno is necessary on some systems? | |
2634 Maybe readdir() doesn't always set errno ?! */ | |
2635 while (!(errno = 0, rtnval = readdir (dirp)) | |
2636 && (errno == EINTR)) | |
2637 ; | |
2638 #ifndef MULE | |
2639 return rtnval; | |
2640 #else /* MULE */ | |
2641 if (rtnval == NULL) /* End of directory */ | |
2642 return NULL; | |
2643 { | |
442 | 2644 const Extbyte * const external_name = (const Extbyte *) rtnval->d_name; |
665 | 2645 Bytecount external_len = strlen (rtnval->d_name); |
867 | 2646 const Ibyte *internal_name; |
462 | 2647 Bytecount internal_len; |
513 | 2648 |
462 | 2649 TO_INTERNAL_FORMAT (DATA, (external_name, external_len), |
2650 ALLOCA, (internal_name, internal_len), | |
2651 Qfile_name); | |
2652 | |
2653 /* check for common case of ASCII filename */ | |
2654 if (internal_len == external_len && | |
2655 !memcmp (external_name, internal_name, internal_len)) | |
428 | 2656 return rtnval; |
2657 | |
2658 { /* Non-ASCII filename */ | |
867 | 2659 static Ibyte_dynarr *internal_DIRENTRY; |
428 | 2660 if (!internal_DIRENTRY) |
867 | 2661 internal_DIRENTRY = Dynarr_new (Ibyte); |
428 | 2662 else |
2663 Dynarr_reset (internal_DIRENTRY); | |
2664 | |
867 | 2665 Dynarr_add_many (internal_DIRENTRY, (Ibyte *) rtnval, |
428 | 2666 offsetof (DIRENTRY, d_name)); |
2667 | |
2668 | |
2669 Dynarr_add_many (internal_DIRENTRY, internal_name, internal_len); | |
444 | 2670 Dynarr_add (internal_DIRENTRY, '\0'); /* NUL-terminate */ |
428 | 2671 return (DIRENTRY *) Dynarr_atp (internal_DIRENTRY, 0); |
2672 } | |
2673 } | |
2674 #endif /* MULE */ | |
771 | 2675 #endif /* WIN32_NATIVE */ |
428 | 2676 } |
771 | 2677 |
428 | 2678 int |
771 | 2679 qxe_closedir (DIR *dirp) |
428 | 2680 { |
771 | 2681 #ifdef WIN32_NATIVE |
2682 return mswindows_closedir (dirp); | |
2683 #else /* not WIN32_NATIVE */ | |
428 | 2684 int rtnval; |
2685 | |
2686 while ((rtnval = closedir (dirp)) == -1 | |
2687 && (errno == EINTR)) | |
2688 ; | |
2689 return rtnval; | |
771 | 2690 #endif /* WIN32_NATIVE */ |
428 | 2691 } |
771 | 2692 |
428 | 2693 int |
867 | 2694 qxe_rmdir (const Ibyte *path) |
771 | 2695 { |
2696 Extbyte *pathout; | |
2697 PATHNAME_CONVERT_OUT (path, pathout); | |
2698 #ifdef WIN32_NATIVE | |
2699 if (XEUNICODE_P) | |
2700 return _wrmdir ((const wchar_t *) pathout); | |
2701 else | |
2702 return _rmdir (pathout); | |
2703 #else | |
2704 return rmdir (pathout); | |
2705 #endif | |
2706 } | |
2707 | |
867 | 2708 Ibyte * |
771 | 2709 qxe_allocating_getcwd (void) |
428 | 2710 { |
771 | 2711 #ifdef HAVE_GETCWD |
2712 Bytecount cwdsize = 1024; | |
2713 Extbyte *cwd = xnew_array (Extbyte, cwdsize); | |
2714 | |
2715 /* Many getcwd()'s can take a NULL argument and malloc() the right amount | |
2716 of data, but this is non-standard. */ | |
2717 while (1) | |
2718 { | |
2719 #ifdef WIN32_NATIVE | |
2720 Extbyte *ret; | |
2721 | |
2722 if (XEUNICODE_P) | |
2723 ret = (Extbyte *) _wgetcwd ((wchar_t *) cwd, | |
2724 cwdsize / sizeof (wchar_t)); | |
2725 else | |
2726 ret = _getcwd (cwd, cwdsize); | |
2727 | |
2728 if (ret) | |
2729 { | |
867 | 2730 Ibyte *retin; |
771 | 2731 TSTR_TO_C_STRING_MALLOC (ret, retin); |
1726 | 2732 xfree (cwd, Extbyte *); |
771 | 2733 return retin; |
2734 } | |
2735 #else | |
2736 Extbyte *ret = getcwd (cwd, cwdsize); | |
2737 if (ret) | |
2738 { | |
867 | 2739 Ibyte *retin; |
771 | 2740 EXTERNAL_TO_C_STRING_MALLOC (ret, retin, Qfile_name); |
1726 | 2741 xfree (cwd, Extbyte *); |
771 | 2742 return retin; |
2743 } | |
2744 #endif /* WIN32_NATIVE */ | |
2745 | |
2746 if (errno == ERANGE) | |
2747 { | |
2748 cwdsize *= 2; | |
2749 XREALLOC_ARRAY (cwd, Extbyte, cwdsize); | |
2750 } | |
2751 else | |
2752 { | |
1726 | 2753 xfree (cwd, Extbyte *); |
771 | 2754 return NULL; |
2755 } | |
2756 } | |
2757 #else | |
2421 | 2758 Extbyte chingame_limitos_arbitrarios[PATH_MAX_EXTERNAL]; |
867 | 2759 Ibyte *ret2; |
771 | 2760 |
2761 if (!getwd (chingame_limitos_arbitrarios)) | |
2762 return 0; | |
2763 EXTERNAL_TO_C_STRING_MALLOC (chingame_limitos_arbitrarios, ret2, Qfile_name); | |
2764 return ret2; | |
2765 #endif /* HAVE_GETCWD */ | |
428 | 2766 } |
2767 | |
2768 /***************** file-information calls ******************/ | |
2769 | |
2770 int | |
867 | 2771 qxe_access (const Ibyte *path, int mode) |
428 | 2772 { |
771 | 2773 #ifdef WIN32_NATIVE |
2774 return mswindows_access (path, mode); | |
2775 #else /* not WIN32_NATIVE */ | |
2776 Extbyte *pathout; | |
2777 PATHNAME_CONVERT_OUT (path, pathout); | |
2778 return access (pathout, mode); | |
2779 #endif /* WIN32_NATIVE */ | |
428 | 2780 } |
771 | 2781 |
2782 #if defined (HAVE_EACCESS) | |
428 | 2783 int |
867 | 2784 qxe_eaccess (const Ibyte *path, int mode) |
428 | 2785 { |
771 | 2786 Extbyte *pathout; |
2787 PATHNAME_CONVERT_OUT (path, pathout); | |
2788 return eaccess (pathout, mode); | |
428 | 2789 } |
771 | 2790 #endif /* defined (HAVE_EACCESS) */ |
2791 | |
428 | 2792 int |
867 | 2793 qxe_lstat (const Ibyte *path, struct stat *buf) |
428 | 2794 { |
771 | 2795 /* if system does not have symbolic links, it does not have lstat. |
2796 In that case, use ordinary stat instead. */ | |
2797 #ifndef S_IFLNK | |
2798 return qxe_stat (path, buf); | |
2526 | 2799 #elif defined (WIN32_NATIVE) |
2800 if (mswindows_shortcuts_are_symlinks) | |
2801 { | |
2802 /* We want to resolve the directory component and leave the rest | |
2803 alone. */ | |
2804 Ibyte *dirend = find_end_of_directory_component (path, qxestrlen (path)); | |
2805 Bytecount len; | |
2806 | |
2807 if (dirend != path) | |
2808 { | |
2809 Ibyte *resdir; | |
2810 Ichar lastch; | |
2811 DECLARE_EISTRING (resname); | |
2812 DECLARE_EISTRING (dir); | |
2813 | |
2814 eicpy_raw (dir, path, dirend - path); | |
2815 PATHNAME_RESOLVE_LINKS (eidata (dir), resdir); | |
2816 eicpy_rawz (resname, resdir); | |
2817 lastch = eigetch_char (resname, eicharlen (resname) - 1); | |
2818 if (!IS_DIRECTORY_SEP (lastch)) | |
2819 eicat_ch (resname, '\\'); | |
2820 eicat_rawz (resname, dirend); | |
2821 path = eidata (resname); | |
2822 } | |
2823 | |
2824 /* However, if what we are trying to stat is a link, we need to add | |
2825 the .LNK so that the actual file is statted. */ | |
2826 len = qxestrlen (path); | |
2827 if (len > 4 && qxestrcasecmp_ascii (path + len - 4, ".LNK")) | |
2828 { | |
2829 DECLARE_EISTRING (name2); | |
2830 Ibyte *resolved; | |
2831 | |
2832 eicpy_rawz (name2, path); | |
2833 eicat_ascii (name2, ".LNK"); | |
2834 resolved = mswindows_read_link (eidata (name2)); | |
2835 if (resolved) | |
2836 { | |
2837 xfree (resolved, Ibyte *); | |
2838 return mswindows_stat (eidata (name2), buf); | |
2839 } | |
2840 } | |
2841 } | |
2842 | |
2843 return mswindows_stat (path, buf); | |
771 | 2844 #else |
2845 Extbyte *pathout; | |
2846 PATHNAME_CONVERT_OUT (path, pathout); | |
2847 return lstat (pathout, buf); | |
2848 #endif | |
428 | 2849 } |
771 | 2850 |
2851 #if defined (HAVE_READLINK) | |
428 | 2852 int |
867 | 2853 qxe_readlink (const Ibyte *path, Ibyte *buf, size_t bufsiz) |
428 | 2854 { |
771 | 2855 int retval; |
2856 Extbyte *pathout; | |
2857 | |
2858 PATHNAME_CONVERT_OUT (path, pathout); | |
2859 retval = readlink (pathout, (char *) buf, bufsiz); | |
2860 if (retval < 0) | |
2861 return retval; | |
2862 { | |
867 | 2863 Ibyte *intbuf; |
771 | 2864 Bytecount tamanho; |
2865 | |
2866 TO_INTERNAL_FORMAT (DATA, (buf, retval), | |
2867 ALLOCA, (intbuf, tamanho), Qfile_name); | |
2868 /* the man page says this function does not null-terminate */ | |
2869 if (tamanho >= (Bytecount) bufsiz) | |
2870 tamanho = bufsiz; | |
2871 memcpy (buf, intbuf, tamanho); | |
2872 return tamanho; | |
2873 } | |
428 | 2874 } |
771 | 2875 #endif /* defined (HAVE_READLINK) */ |
2876 | |
432 | 2877 int |
771 | 2878 qxe_fstat (int fd, struct stat *buf) |
432 | 2879 { |
442 | 2880 #ifdef WIN32_NATIVE |
2881 return mswindows_fstat (fd, buf); | |
2882 #else | |
432 | 2883 return fstat (fd, buf); |
771 | 2884 #endif /* WIN32_NATIVE */ |
432 | 2885 } |
2886 | |
428 | 2887 int |
867 | 2888 qxe_stat (const Ibyte *path, struct stat *buf) |
428 | 2889 { |
442 | 2890 #ifdef WIN32_NATIVE |
2526 | 2891 Ibyte *resolved; |
2892 PATHNAME_RESOLVE_LINKS (path, resolved); | |
2893 return mswindows_stat (resolved, buf); | |
771 | 2894 #else /* not WIN32_NATIVE */ |
2895 Extbyte *pathout; | |
2896 PATHNAME_CONVERT_OUT (path, pathout); | |
2897 return stat (pathout, buf); | |
2898 #endif /* WIN32_NATIVE */ | |
428 | 2899 } |
2900 | |
771 | 2901 |
428 | 2902 /****************** file-manipulation calls *****************/ |
2903 | |
2904 int | |
867 | 2905 qxe_chmod (const Ibyte *path, mode_t mode) |
428 | 2906 { |
771 | 2907 Extbyte *pathout; |
2908 PATHNAME_CONVERT_OUT (path, pathout); | |
2909 #ifdef WIN32_NATIVE | |
2910 if (XEUNICODE_P) | |
2911 return _wchmod ((const wchar_t *) pathout, mode); | |
2912 else | |
2913 return _chmod (pathout, mode); | |
2914 #else | |
2915 return chmod (pathout, mode); | |
2916 #endif | |
428 | 2917 } |
771 | 2918 |
2919 #if defined (HAVE_LINK) | |
428 | 2920 int |
2957 | 2921 qxe_link (const Ibyte *existing, const Ibyte *new_) |
428 | 2922 { |
771 | 2923 #ifdef WIN32_NATIVE |
2957 | 2924 return mswindows_link (existing, new_); |
771 | 2925 #else /* not WIN32_NATIVE */ |
2926 Extbyte *existingout, *newout; | |
2927 PATHNAME_CONVERT_OUT (existing, existingout); | |
2957 | 2928 PATHNAME_CONVERT_OUT (new_, newout); |
771 | 2929 return link (existingout, newout); |
2930 #endif /* WIN32_NATIVE */ | |
428 | 2931 } |
771 | 2932 #endif /* defined (HAVE_LINK) */ |
2933 | |
428 | 2934 int |
2957 | 2935 qxe_rename (const Ibyte *old, const Ibyte *new_) |
428 | 2936 { |
442 | 2937 #ifdef WIN32_NATIVE |
2957 | 2938 return mswindows_rename (old, new_); |
771 | 2939 #else /* not WIN32_NATIVE */ |
2940 Extbyte *oldout, *newout; | |
2941 PATHNAME_CONVERT_OUT (old, oldout); | |
2957 | 2942 PATHNAME_CONVERT_OUT (new_, newout); |
771 | 2943 return rename (oldout, newout); |
442 | 2944 #endif /* WIN32_NATIVE */ |
428 | 2945 } |
771 | 2946 |
2947 #if defined (HAVE_SYMLINK) | |
428 | 2948 int |
867 | 2949 qxe_symlink (const Ibyte *name1, const Ibyte *name2) |
428 | 2950 { |
771 | 2951 Extbyte *name1out, *name2out; |
2952 PATHNAME_CONVERT_OUT (name1, name1out); | |
2953 PATHNAME_CONVERT_OUT (name2, name2out); | |
2954 return symlink (name1out, name2out); | |
428 | 2955 } |
771 | 2956 #endif /* defined (HAVE_SYMLINK) */ |
2957 | |
428 | 2958 int |
867 | 2959 qxe_unlink (const Ibyte *path) |
428 | 2960 { |
771 | 2961 #ifdef WIN32_NATIVE |
2962 return mswindows_unlink (path); | |
2963 #else /* not WIN32_NATIVE */ | |
2964 Extbyte *pathout; | |
2965 PATHNAME_CONVERT_OUT (path, pathout); | |
2966 return unlink (pathout); | |
2967 #endif /* WIN32_NATIVE */ | |
428 | 2968 } |
771 | 2969 |
2970 | |
2971 /****************** process calls *****************/ | |
2972 | |
428 | 2973 int |
867 | 2974 qxe_execve (const Ibyte *filename, Ibyte * const argv[], |
2975 Ibyte * const envp[]) | |
428 | 2976 { |
771 | 2977 int i, argc, envc; |
2978 Extbyte *pathext; | |
2979 Extbyte **new_argv; | |
2980 Extbyte **new_envp; | |
2981 | |
2982 PATHNAME_CONVERT_OUT (filename, pathext); | |
2983 | |
428 | 2984 for (argc = 0; argv[argc]; argc++) |
2985 ; | |
771 | 2986 new_argv = alloca_array (Extbyte *, argc + 1); |
428 | 2987 for (i = 0; i < argc; i++) |
771 | 2988 C_STRING_TO_EXTERNAL (argv[i], new_argv[i], Qnative); |
428 | 2989 new_argv[argc] = NULL; |
771 | 2990 |
2991 for (envc = 0; envp[envc]; envc++) | |
2992 ; | |
2993 new_envp = alloca_array (Extbyte *, envc + 1); | |
2994 for (i = 0; i < envc; i++) | |
2995 C_STRING_TO_EXTERNAL (envp[i], new_envp[i], Qnative); | |
2996 new_envp[envc] = NULL; | |
2997 | |
1318 | 2998 #if defined (WIN32_NATIVE) |
2999 if (XEUNICODE_P) | |
3000 return _wexecve ((const wchar_t *) pathext, | |
3001 (const wchar_t * const *) new_argv, | |
3002 (const wchar_t * const *) new_envp); | |
3003 #endif | |
771 | 3004 return execve (pathext, new_argv, new_envp); |
3005 } | |
3006 | |
3007 pid_t | |
3008 qxe_getpid (void) | |
3009 { | |
3010 #ifdef WIN32_NATIVE | |
3011 return abs (getpid ()); | |
3012 #else | |
3013 return getpid (); | |
3014 #endif | |
428 | 3015 } |
771 | 3016 |
3017 | |
3018 /****************** passwd calls *****************/ | |
3019 | |
3020 struct passwd cached_pwd; | |
3021 | |
3022 static struct passwd * | |
3023 copy_in_passwd (struct passwd *pwd) | |
3024 { | |
3025 if (!pwd) | |
3026 return NULL; | |
3027 | |
3028 if (cached_pwd.pw_name) | |
1726 | 3029 xfree (cached_pwd.pw_name, char *); |
771 | 3030 if (cached_pwd.pw_passwd) |
1726 | 3031 xfree (cached_pwd.pw_passwd, char *); |
771 | 3032 if (cached_pwd.pw_gecos) |
1726 | 3033 xfree (cached_pwd.pw_gecos, char *); |
771 | 3034 if (cached_pwd.pw_dir) |
1726 | 3035 xfree (cached_pwd.pw_dir, char *); |
771 | 3036 if (cached_pwd.pw_shell) |
1726 | 3037 xfree (cached_pwd.pw_shell, char *); |
771 | 3038 |
3039 cached_pwd = *pwd; | |
3040 if (cached_pwd.pw_name) | |
3041 TO_INTERNAL_FORMAT (C_STRING, cached_pwd.pw_name, | |
3042 C_STRING_MALLOC, cached_pwd.pw_name, Qnative); | |
3043 if (cached_pwd.pw_passwd) | |
3044 TO_INTERNAL_FORMAT (C_STRING, cached_pwd.pw_passwd, | |
3045 C_STRING_MALLOC, cached_pwd.pw_passwd, Qnative); | |
3046 if (cached_pwd.pw_gecos) | |
3047 TO_INTERNAL_FORMAT (C_STRING, cached_pwd.pw_gecos, | |
3048 C_STRING_MALLOC, cached_pwd.pw_gecos, Qnative); | |
3049 if (cached_pwd.pw_dir) | |
3050 TO_INTERNAL_FORMAT (C_STRING, cached_pwd.pw_dir, | |
3051 C_STRING_MALLOC, cached_pwd.pw_dir, Qfile_name); | |
3052 if (cached_pwd.pw_shell) | |
3053 TO_INTERNAL_FORMAT (C_STRING, cached_pwd.pw_shell, | |
3054 C_STRING_MALLOC, cached_pwd.pw_shell, Qfile_name); | |
3055 return &cached_pwd; | |
3056 } | |
3057 | |
3058 struct passwd * | |
867 | 3059 qxe_getpwnam (const Ibyte *name) |
771 | 3060 { |
3061 #ifdef WIN32_NATIVE | |
3062 /* Synthetic versions are defined in nt.c and already do conversion. */ | |
3063 return getpwnam (name); | |
3064 #else | |
3065 Extbyte *nameext; | |
3066 C_STRING_TO_EXTERNAL (name, nameext, Qnative); | |
3067 | |
3068 return copy_in_passwd (getpwnam (nameext)); | |
3069 #endif /* WIN32_NATIVE */ | |
3070 } | |
3071 | |
3072 struct passwd * | |
3073 qxe_getpwuid (uid_t uid) | |
3074 { | |
3075 #ifdef WIN32_NATIVE | |
3076 /* Synthetic versions are defined in nt.c and already do conversion. */ | |
3077 return getpwuid (uid); | |
3078 #else | |
3079 return copy_in_passwd (getpwuid (uid)); | |
3080 #endif /* WIN32_NATIVE */ | |
3081 } | |
3082 | |
3083 #ifndef WIN32_NATIVE | |
3084 | |
3085 struct passwd * | |
3086 qxe_getpwent (void) | |
3087 { | |
3088 /* No WIN32_NATIVE version of this. */ | |
3089 return copy_in_passwd (getpwent ()); | |
3090 } | |
3091 | |
3092 #endif /* not WIN32_NATIVE */ | |
3093 | |
3094 /****************** time calls *****************/ | |
3095 | |
867 | 3096 static Ibyte *ctime_static; |
3097 | |
3098 Ibyte * | |
771 | 3099 qxe_ctime (const time_t *t) |
3100 { | |
3101 Extbyte *str = (Extbyte *) ctime (t); | |
3102 if (!str) /* can happen on MS Windows */ | |
867 | 3103 return (Ibyte *) "Sun Jan 01 00:00:00 1970"; |
771 | 3104 if (ctime_static) |
1726 | 3105 xfree (ctime_static, Ibyte *); |
771 | 3106 EXTERNAL_TO_C_STRING_MALLOC (str, ctime_static, Qnative); |
3107 return ctime_static; | |
3108 } | |
428 | 3109 |
1204 | 3110 |
912 | 3111 /************************************************************************/ |
3112 /* Emulation of missing functions from wchar.h */ | |
3113 /************************************************************************/ | |
3114 | |
3115 #ifndef HAVE_WCHAR_H | |
3116 size_t | |
2367 | 3117 wcslen (const wchar_t *s) |
912 | 3118 { |
3700 | 3119 const wchar_t *p = s; |
3648 | 3120 if (s == NULL) return NULL; |
912 | 3121 |
3122 while (*p++) | |
3123 ; | |
3124 | |
3125 return p - s; | |
3126 } | |
3127 #endif | |
1204 | 3128 |
3129 /************************************************************************/ | |
3130 /* Emulation of missing functions from string.h */ | |
3131 /************************************************************************/ | |
3132 | |
3133 #ifndef HAVE_STRLWR | |
3134 char * | |
3135 strlwr (char *s) | |
3136 { | |
3700 | 3137 REGISTER char *c; |
3648 | 3138 if (s == NULL) return NULL; |
1271 | 3139 |
3140 for (c = s; *c; c++) | |
1204 | 3141 { |
1271 | 3142 *c = tolower (*c); |
1204 | 3143 } |
1271 | 3144 return s; |
1204 | 3145 } |
3146 #endif | |
3147 | |
1271 | 3148 #ifndef HAVE_STRUPR |
1204 | 3149 char * |
3150 strupr (char *s) | |
3151 { | |
1271 | 3152 REGISTER char *c; |
3153 | |
3154 for (c = s; *c; c++) | |
1204 | 3155 { |
1271 | 3156 *c = toupper (*c); |
1204 | 3157 } |
1271 | 3158 return s; |
1204 | 3159 } |
3160 #endif | |
912 | 3161 |
428 | 3162 |
3163 /************************************************************************/ | |
3164 /* Emulations of missing system calls */ | |
3165 /************************************************************************/ | |
3166 | |
3167 /***** (these are primarily required for USG, it seems) *****/ | |
3168 | |
3169 /* | |
3170 * Emulate rename using unlink/link. Note that this is | |
3171 * only partially correct. Also, doesn't enforce restriction | |
3172 * that files be of same type (regular->regular, dir->dir, etc). | |
3173 */ | |
3174 | |
3175 #ifndef HAVE_RENAME | |
3176 int | |
771 | 3177 rename (const Extbyte *from, const Extbyte *to) |
428 | 3178 { |
3179 if (access (from, 0) == 0) | |
3180 { | |
3181 unlink (to); | |
3182 if (link (from, to) == 0) | |
3183 if (unlink (from) == 0) | |
3184 return (0); | |
3185 } | |
3186 return (-1); | |
3187 } | |
3188 #endif /* HAVE_RENAME */ | |
3189 | |
3190 #ifdef HPUX | |
3191 #ifndef HAVE_PERROR | |
3192 | |
3193 /* HPUX curses library references perror, but as far as we know | |
3194 it won't be called. Anyway this definition will do for now. */ | |
3195 | |
3196 perror (void) | |
3197 { | |
3198 } | |
3199 | |
3200 #endif /* not HAVE_PERROR */ | |
3201 #endif /* HPUX */ | |
3202 | |
3203 #ifndef HAVE_DUP2 | |
3204 | |
3205 /* | |
3206 * Emulate BSD dup2. First close newd if it already exists. | |
3207 * Then, attempt to dup oldd. If not successful, call dup2 recursively | |
3208 * until we are, then close the unsuccessful ones. | |
3209 */ | |
3210 | |
3211 int | |
3212 dup2 (int oldd, int newd) | |
3213 { | |
3214 int fd, ret; | |
3215 | |
771 | 3216 retry_close (newd); |
428 | 3217 |
3218 #ifdef F_DUPFD | |
3219 fd = fcntl (oldd, F_DUPFD, newd); | |
3220 if (fd != newd) | |
563 | 3221 signal_ferror_with_frob (Qfile_error, lisp_strerror (errno), |
3222 "can't dup2 (%i, %i)", oldd, newd); | |
428 | 3223 #else |
2957 | 3224 fd = dup (oldd); |
428 | 3225 if (fd == -1) |
3226 return -1; | |
2957 | 3227 if (fd == newd) |
3228 return newd; | |
3229 ret = dup2 (oldd, newd); | |
771 | 3230 retry_close (fd); |
428 | 3231 return ret; |
3232 #endif /* F_DUPFD */ | |
3233 } | |
3234 | |
3235 #endif /* not HAVE_DUP2 */ | |
3236 | |
3237 /* | |
3238 * Gettimeofday. Simulate as much as possible. Only accurate | |
3239 * to nearest second. Emacs doesn't use tzp so ignore it for now. | |
3240 */ | |
3241 | |
3242 #if !defined (HAVE_GETTIMEOFDAY) | |
3243 | |
3244 int | |
3245 gettimeofday (struct timeval *tp, struct timezone *tzp) | |
3246 { | |
3247 extern long time (); | |
3248 | |
3249 tp->tv_sec = time ((long *)0); | |
3250 tp->tv_usec = 0; | |
3251 if (tzp != 0) | |
3252 tzp->tz_minuteswest = -1; | |
3253 return (0); | |
3254 } | |
3255 | |
3256 #endif /* !HAVE_GETTIMEOFDAY */ | |
3257 | |
3258 /* No need to encapsulate utime and utimes explicitly because all | |
3259 access to those functions goes through the following. */ | |
3260 | |
3261 int | |
2340 | 3262 set_file_times ( |
3263 #if defined (WIN32_NATIVE) || defined (HAVE_UTIME) || defined (HAVE_UTIMES) | |
3264 Lisp_Object path, EMACS_TIME atime, EMACS_TIME mtime | |
3265 #else | |
3266 Lisp_Object UNUSED (path), EMACS_TIME UNUSED (atime), | |
3267 EMACS_TIME UNUSED (mtime) | |
3268 #endif | |
3269 ) | |
428 | 3270 { |
592 | 3271 #if defined (WIN32_NATIVE) |
460 | 3272 struct utimbuf utb; |
3273 utb.actime = EMACS_SECS (atime); | |
3274 utb.modtime = EMACS_SECS (mtime); | |
592 | 3275 return mswindows_utime (path, &utb); |
3276 #elif defined (HAVE_UTIME) | |
3277 struct utimbuf utb; | |
3278 Extbyte *filename; | |
3279 utb.actime = EMACS_SECS (atime); | |
3280 utb.modtime = EMACS_SECS (mtime); | |
3281 LISP_STRING_TO_EXTERNAL (path, filename, Qfile_name); | |
460 | 3282 return utime (filename, &utb); |
3283 #elif defined (HAVE_UTIMES) | |
428 | 3284 struct timeval tv[2]; |
592 | 3285 Extbyte *filename; |
428 | 3286 tv[0] = atime; |
3287 tv[1] = mtime; | |
592 | 3288 LISP_STRING_TO_EXTERNAL (path, filename, Qfile_name); |
428 | 3289 return utimes (filename, tv); |
460 | 3290 #else |
3291 /* No file times setting function available. */ | |
3292 return -1; | |
3293 #endif | |
428 | 3294 } |
3295 | |
3296 /* */ | |
3297 | |
3298 static long ticks_per_second; | |
3299 static long orig_user_ticks, orig_system_ticks; | |
3300 EMACS_TIME orig_real_time; | |
3301 | |
3302 static int process_times_available; | |
3303 | |
3304 /* Return the relative user and system tick count. We try to | |
3305 maintain calculations in terms of integers as long as possible | |
3306 for increased accuracy. */ | |
3307 | |
3308 static int | |
2340 | 3309 get_process_times_1 ( |
3310 #if defined (CLOCKS_PER_SEC) || defined (_SC_CLK_TCK) || defined (CLK_TCK) && !defined(WIN32_NATIVE) | |
3311 long *user_ticks, long *system_ticks | |
3312 #else | |
3313 long *UNUSED (user_ticks), long *UNUSED (system_ticks) | |
3314 #endif | |
3315 ) | |
428 | 3316 { |
442 | 3317 #if defined (_SC_CLK_TCK) || defined (CLK_TCK) && !defined(WIN32_NATIVE) |
428 | 3318 /* We have the POSIX times() function available. */ |
777 | 3319 /* #### Perhaps we should just use a configure test for times()? */ |
428 | 3320 struct tms tttt; |
3321 times (&tttt); | |
3322 *user_ticks = (long) tttt.tms_utime; | |
3323 *system_ticks = (long) tttt.tms_stime; | |
3324 return 1; | |
3325 #elif defined (CLOCKS_PER_SEC) | |
3326 *user_ticks = (long) clock (); | |
3327 *system_ticks = 0; | |
3328 return 1; | |
3329 #else | |
3330 return 0; | |
3331 #endif | |
3332 } | |
3333 | |
3334 void | |
3335 init_process_times_very_early (void) | |
3336 { | |
3337 #if defined (_SC_CLK_TCK) | |
3338 ticks_per_second = sysconf (_SC_CLK_TCK); | |
3339 #elif defined (CLK_TCK) | |
3340 ticks_per_second = CLK_TCK; | |
3341 #elif defined (CLOCKS_PER_SEC) | |
3342 ticks_per_second = CLOCKS_PER_SEC; | |
3343 #endif | |
3344 | |
3345 process_times_available = get_process_times_1 (&orig_user_ticks, | |
3346 &orig_system_ticks); | |
3347 EMACS_GET_TIME (orig_real_time); | |
3348 } | |
3349 | |
3350 /* Return the user and system times used up by this process so far. */ | |
3351 void | |
3352 get_process_times (double *user_time, double *system_time, double *real_time) | |
3353 { | |
3354 EMACS_TIME curr_real_time; | |
3355 EMACS_TIME elapsed_time; | |
3356 long curr_user_ticks, curr_system_ticks; | |
3357 | |
3358 EMACS_GET_TIME (curr_real_time); | |
3359 EMACS_SUB_TIME (elapsed_time, curr_real_time, orig_real_time); | |
3360 *real_time = (EMACS_SECS (elapsed_time) | |
3361 + ((double) EMACS_USECS (elapsed_time)) / 1000000); | |
3362 if (get_process_times_1 (&curr_user_ticks, &curr_system_ticks)) | |
3363 { | |
3364 *user_time = (((double) (curr_user_ticks - orig_user_ticks)) | |
3365 / ticks_per_second); | |
3366 *system_time = (((double) (curr_system_ticks - orig_system_ticks)) | |
3367 / ticks_per_second); | |
3368 } | |
3369 else | |
3370 { | |
3371 /* A lame OS */ | |
3372 *user_time = *real_time; | |
3373 *system_time = 0; | |
3374 } | |
3375 } | |
3376 | |
3377 #ifndef HAVE_RANDOM | |
3378 #ifdef random | |
3379 #define HAVE_RANDOM | |
3380 #endif | |
3381 #endif | |
3382 | |
3383 /* Figure out how many bits the system's random number generator uses. | |
3384 `random' and `lrand48' are assumed to return 31 usable bits. | |
3385 BSD `rand' returns a 31 bit value but the low order bits are unusable; | |
3386 so we'll shift it and treat it like the 15-bit USG `rand'. */ | |
3387 | |
3388 #ifndef RAND_BITS | |
3389 # ifdef HAVE_RANDOM | |
3390 # define RAND_BITS 31 | |
3391 # else /* !HAVE_RANDOM */ | |
3392 # ifdef HAVE_LRAND48 | |
3393 # define RAND_BITS 31 | |
3394 # define random lrand48 | |
3395 # else /* !HAVE_LRAND48 */ | |
3396 # define RAND_BITS 15 | |
3397 # if RAND_MAX == 32767 | |
3398 # define random rand | |
3399 # else /* RAND_MAX != 32767 */ | |
3400 # if RAND_MAX == 2147483647 | |
3401 # define random() (rand () >> 16) | |
3402 # else /* RAND_MAX != 2147483647 */ | |
3403 # ifdef USG | |
3404 # define random rand | |
3405 # else | |
3406 # define random() (rand () >> 16) | |
3407 # endif /* !BSD */ | |
3408 # endif /* RAND_MAX != 2147483647 */ | |
3409 # endif /* RAND_MAX != 32767 */ | |
3410 # endif /* !HAVE_LRAND48 */ | |
3411 # endif /* !HAVE_RANDOM */ | |
3412 #endif /* !RAND_BITS */ | |
3413 | |
3414 void | |
3415 seed_random (long arg) | |
3416 { | |
3417 #ifdef HAVE_RANDOM | |
3418 srandom ((unsigned int)arg); | |
3419 #else | |
3420 # ifdef HAVE_LRAND48 | |
3421 srand48 (arg); | |
3422 # else | |
3423 srand ((unsigned int)arg); | |
3424 # endif | |
3425 #endif | |
1983 | 3426 #ifdef HAVE_BIGNUM |
3427 bignum_random_seed ((unsigned long) arg); | |
3428 #endif | |
428 | 3429 } |
3430 | |
3431 /* | |
3432 * Build a full Emacs-sized word out of whatever we've got. | |
3433 * This suffices even for a 64-bit architecture with a 15-bit rand. | |
3434 */ | |
3435 long | |
3436 get_random (void) | |
3437 { | |
3438 long val = random (); | |
2039 | 3439 #if INT_VALBITS > RAND_BITS |
428 | 3440 val = (val << RAND_BITS) ^ random (); |
2039 | 3441 #if INT_VALBITS > 2*RAND_BITS |
428 | 3442 val = (val << RAND_BITS) ^ random (); |
2039 | 3443 #if INT_VALBITS > 3*RAND_BITS |
428 | 3444 val = (val << RAND_BITS) ^ random (); |
2039 | 3445 #if INT_VALBITS > 4*RAND_BITS |
428 | 3446 val = (val << RAND_BITS) ^ random (); |
3447 #endif /* need at least 5 */ | |
3448 #endif /* need at least 4 */ | |
3449 #endif /* need at least 3 */ | |
3450 #endif /* need at least 2 */ | |
2039 | 3451 return val & (EMACS_INT) ((1UL << INT_VALBITS) - 1); |
428 | 3452 } |
3453 | |
3454 | |
3455 /************************************************************************/ | |
3456 /* Strings corresponding to defined signals */ | |
3457 /************************************************************************/ | |
3458 | |
2762 | 3459 #if (!defined(HAVE_DECL_SYS_SIGLIST) || !HAVE_DECL_SYS_SIGLIST ) && !defined (HAVE_SYS_SIGLIST) |
428 | 3460 |
442 | 3461 #if defined(WIN32_NATIVE) || defined(CYGWIN) |
3462 const char *sys_siglist[] = | |
428 | 3463 { |
771 | 3464 /* $$####begin-snarf */ |
428 | 3465 "bum signal!!", |
3466 "hangup", | |
3467 "interrupt", | |
3468 "quit", | |
3469 "illegal instruction", | |
3470 "trace trap", | |
3471 "iot instruction", | |
3472 "emt instruction", | |
3473 "floating point exception", | |
3474 "kill", | |
3475 "bus error", | |
3476 "segmentation violation", | |
3477 "bad argument to system call", | |
3478 "write on a pipe with no one to read it", | |
3479 "alarm clock", | |
3480 "software termination signal from kill", | |
3481 "status signal", | |
3482 "sendable stop signal not from tty", | |
3483 "stop signal from tty", | |
3484 "continue a stopped process", | |
3485 "child status has changed", | |
3486 "background read attempted from control tty", | |
3487 "background write attempted from control tty", | |
3488 "input record available at control tty", | |
3489 "exceeded CPU time limit", | |
3490 "exceeded file size limit" | |
771 | 3491 /* $$####end-snarf */ |
428 | 3492 }; |
3493 #endif | |
3494 | |
3495 #ifdef USG | |
3496 #ifdef AIX | |
442 | 3497 const char *sys_siglist[NSIG + 1] = |
428 | 3498 { |
3499 /* AIX has changed the signals a bit */ | |
863 | 3500 DEFER_GETTEXT ("bogus signal"), /* 0 */ |
3501 DEFER_GETTEXT ("hangup"), /* 1 SIGHUP */ | |
3502 DEFER_GETTEXT ("interrupt"), /* 2 SIGINT */ | |
3503 DEFER_GETTEXT ("quit"), /* 3 SIGQUIT */ | |
3504 DEFER_GETTEXT ("illegal instruction"), /* 4 SIGILL */ | |
3505 DEFER_GETTEXT ("trace trap"), /* 5 SIGTRAP */ | |
3506 DEFER_GETTEXT ("IOT instruction"), /* 6 SIGIOT */ | |
3507 DEFER_GETTEXT ("crash likely"), /* 7 SIGDANGER */ | |
3508 DEFER_GETTEXT ("floating point exception"), /* 8 SIGFPE */ | |
3509 DEFER_GETTEXT ("kill"), /* 9 SIGKILL */ | |
3510 DEFER_GETTEXT ("bus error"), /* 10 SIGBUS */ | |
3511 DEFER_GETTEXT ("segmentation violation"), /* 11 SIGSEGV */ | |
3512 DEFER_GETTEXT ("bad argument to system call"), /* 12 SIGSYS */ | |
3513 DEFER_GETTEXT ("write on a pipe with no one to read it"), /* 13 SIGPIPE */ | |
3514 DEFER_GETTEXT ("alarm clock"), /* 14 SIGALRM */ | |
3515 DEFER_GETTEXT ("software termination signal"), /* 15 SIGTERM */ | |
3516 DEFER_GETTEXT ("user defined signal 1"), /* 16 SIGUSR1 */ | |
3517 DEFER_GETTEXT ("user defined signal 2"), /* 17 SIGUSR2 */ | |
3518 DEFER_GETTEXT ("death of a child"), /* 18 SIGCLD */ | |
3519 DEFER_GETTEXT ("power-fail restart"), /* 19 SIGPWR */ | |
3520 DEFER_GETTEXT ("bogus signal"), /* 20 */ | |
3521 DEFER_GETTEXT ("bogus signal"), /* 21 */ | |
3522 DEFER_GETTEXT ("bogus signal"), /* 22 */ | |
3523 DEFER_GETTEXT ("bogus signal"), /* 23 */ | |
3524 DEFER_GETTEXT ("bogus signal"), /* 24 */ | |
3525 DEFER_GETTEXT ("LAN I/O interrupt"), /* 25 SIGAIO */ | |
3526 DEFER_GETTEXT ("PTY I/O interrupt"), /* 26 SIGPTY */ | |
3527 DEFER_GETTEXT ("I/O intervention required"), /* 27 SIGIOINT */ | |
771 | 3528 /* $$####end-snarf */ |
428 | 3529 0 |
3530 }; | |
3531 #else /* USG, not AIX */ | |
442 | 3532 const char *sys_siglist[NSIG + 1] = |
428 | 3533 { |
863 | 3534 DEFER_GETTEXT ("bogus signal"), /* 0 */ |
3535 DEFER_GETTEXT ("hangup"), /* 1 SIGHUP */ | |
3536 DEFER_GETTEXT ("interrupt"), /* 2 SIGINT */ | |
3537 DEFER_GETTEXT ("quit"), /* 3 SIGQUIT */ | |
3538 DEFER_GETTEXT ("illegal instruction"), /* 4 SIGILL */ | |
3539 DEFER_GETTEXT ("trace trap"), /* 5 SIGTRAP */ | |
3540 DEFER_GETTEXT ("IOT instruction"), /* 6 SIGIOT */ | |
3541 DEFER_GETTEXT ("EMT instruction"), /* 7 SIGEMT */ | |
3542 DEFER_GETTEXT ("floating point exception"), /* 8 SIGFPE */ | |
3543 DEFER_GETTEXT ("kill"), /* 9 SIGKILL */ | |
3544 DEFER_GETTEXT ("bus error"), /* 10 SIGBUS */ | |
3545 DEFER_GETTEXT ("segmentation violation"), /* 11 SIGSEGV */ | |
3546 DEFER_GETTEXT ("bad argument to system call"), /* 12 SIGSYS */ | |
3547 DEFER_GETTEXT ("write on a pipe with no one to read it"), /* 13 SIGPIPE */ | |
3548 DEFER_GETTEXT ("alarm clock"), /* 14 SIGALRM */ | |
3549 DEFER_GETTEXT ("software termination signal"), /* 15 SIGTERM */ | |
3550 DEFER_GETTEXT ("user defined signal 1"), /* 16 SIGUSR1 */ | |
3551 DEFER_GETTEXT ("user defined signal 2"), /* 17 SIGUSR2 */ | |
3552 DEFER_GETTEXT ("death of a child"), /* 18 SIGCLD */ | |
3553 DEFER_GETTEXT ("power-fail restart"), /* 19 SIGPWR */ | |
428 | 3554 #ifdef sun |
771 | 3555 "window size changed", /* 20 SIGWINCH */ |
3556 "urgent socket condition", /* 21 SIGURG */ | |
3557 "pollable event occurred", /* 22 SIGPOLL */ | |
3558 "stop (cannot be caught or ignored)", /* 23 SIGSTOP */ | |
3559 "user stop requested from tty", /* 24 SIGTSTP */ | |
3560 "stopped process has been continued", /* 25 SIGCONT */ | |
3561 "background tty read attempted", /* 26 SIGTTIN */ | |
3562 "background tty write attempted", /* 27 SIGTTOU */ | |
3563 "virtual timer expired", /* 28 SIGVTALRM */ | |
3564 "profiling timer expired", /* 29 SIGPROF */ | |
3565 "exceeded cpu limit", /* 30 SIGXCPU */ | |
3566 "exceeded file size limit", /* 31 SIGXFSZ */ | |
3567 "process's lwps are blocked", /* 32 SIGWAITING */ | |
3568 "special signal used by thread library", /* 33 SIGLWP */ | |
428 | 3569 #ifdef SIGFREEZE |
771 | 3570 "special signal used by CPR", /* 34 SIGFREEZE */ |
428 | 3571 #endif |
3572 #ifdef SIGTHAW | |
771 | 3573 "special signal used by CPR", /* 35 SIGTHAW */ |
428 | 3574 #endif |
3575 #endif /* sun */ | |
771 | 3576 /* $$####end-snarf */ |
428 | 3577 0 |
3578 }; | |
3579 #endif /* not AIX */ | |
3580 #endif /* USG */ | |
3581 | |
2762 | 3582 #endif /* (!defined(HAVE_DECL_SYS_SIGLIST) || !HAVE_DECL_SYS_SIGLIST ) && !defined (HAVE_SYS_SIGLIST) */ |
428 | 3583 |
3584 | |
3585 /************************************************************************/ | |
3586 /* Directory routines for systems that don't have them */ | |
3587 /************************************************************************/ | |
3588 | |
3589 #ifdef SYSV_SYSTEM_DIR | |
3590 | |
3591 #include <dirent.h> | |
3592 | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
3700
diff
changeset
|
3593 #if !defined(HAVE_CLOSEDIR) |
428 | 3594 int |
3595 closedir (DIR *dirp) /* stream from opendir */ | |
3596 { | |
3597 int rtnval; | |
3598 | |
771 | 3599 rtnval = retry_close (dirp->dd_fd); |
428 | 3600 |
3601 /* Some systems (like Solaris) allocate the buffer and the DIR all | |
3602 in one block. Why in the world are we freeing this ourselves | |
3603 anyway? */ | |
3604 #if ! (defined (sun) && defined (USG5_4)) | |
1726 | 3605 xfree (dirp->dd_buf, char *); /* directory block defined in <dirent.h> */ |
428 | 3606 #endif |
1726 | 3607 xfree (dirp, DIR *); |
428 | 3608 return (rtnval); |
3609 } | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
3700
diff
changeset
|
3610 #endif /* not HAVE_CLOSEDIR */ |
428 | 3611 #endif /* SYSV_SYSTEM_DIR */ |
3612 | |
3613 #ifdef NONSYSTEM_DIR_LIBRARY | |
3614 | |
3615 DIR * | |
442 | 3616 opendir (const char *filename) /* name of directory */ |
428 | 3617 { |
3618 DIR *dirp; /* -> malloc'ed storage */ | |
3619 int fd; /* file descriptor for read */ | |
3620 struct stat sbuf; /* result of fstat */ | |
3621 | |
771 | 3622 fd = open (filename, O_RDONLY); |
428 | 3623 if (fd < 0) |
3624 return 0; | |
3625 | |
3626 if (fstat (fd, &sbuf) < 0 | |
3627 || (sbuf.st_mode & S_IFMT) != S_IFDIR | |
3628 || (dirp = (DIR *) malloc (sizeof (DIR))) == 0) | |
3629 { | |
771 | 3630 retry_close (fd); |
428 | 3631 return 0; /* bad luck today */ |
3632 } | |
3633 | |
3634 dirp->dd_fd = fd; | |
3635 dirp->dd_loc = dirp->dd_size = 0; /* refill needed */ | |
3636 | |
3637 return dirp; | |
3638 } | |
3639 | |
3640 void | |
3641 closedir (DIR *dirp) /* stream from opendir */ | |
3642 { | |
771 | 3643 retry_close (dirp->dd_fd); |
1726 | 3644 xfree (dirp, DIR *); |
428 | 3645 } |
3646 | |
3647 | |
3648 #define DIRSIZ 14 | |
3649 struct olddir | |
3650 { | |
3651 ino_t od_ino; /* inode */ | |
3652 char od_name[DIRSIZ]; /* filename */ | |
3653 }; | |
3654 | |
3655 static struct direct dir_static; /* simulated directory contents */ | |
3656 | |
3657 /* ARGUSED */ | |
3658 struct direct * | |
3659 readdir (DIR *dirp) /* stream from opendir */ | |
3660 { | |
3661 struct olddir *dp; /* -> directory data */ | |
3662 | |
3663 for (; ;) | |
3664 { | |
3665 if (dirp->dd_loc >= dirp->dd_size) | |
3666 dirp->dd_loc = dirp->dd_size = 0; | |
3667 | |
3668 if (dirp->dd_size == 0 /* refill buffer */ | |
771 | 3669 && (dirp->dd_size = |
3670 retry_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0) | |
428 | 3671 return 0; |
3672 | |
3673 dp = (struct olddir *) &dirp->dd_buf[dirp->dd_loc]; | |
3674 dirp->dd_loc += sizeof (struct olddir); | |
3675 | |
3676 if (dp->od_ino != 0) /* not deleted entry */ | |
3677 { | |
3678 dir_static.d_ino = dp->od_ino; | |
3679 strncpy (dir_static.d_name, dp->od_name, DIRSIZ); | |
3680 dir_static.d_name[DIRSIZ] = '\0'; | |
3681 dir_static.d_namlen = strlen (dir_static.d_name); | |
3682 dir_static.d_reclen = sizeof (struct direct) | |
3683 - MAXNAMLEN + 3 | |
3684 + dir_static.d_namlen - dir_static.d_namlen % 4; | |
3685 return &dir_static; /* -> simulated structure */ | |
3686 } | |
3687 } | |
3688 } | |
3689 | |
3690 | |
3691 #endif /* NONSYSTEM_DIR_LIBRARY */ | |
3692 | |
3693 | |
3694 /* mkdir and rmdir functions, for systems which don't have them. */ | |
3695 | |
3696 #ifndef HAVE_MKDIR | |
3697 /* | |
3698 * Written by Robert Rother, Mariah Corporation, August 1985. | |
3699 * | |
3700 * If you want it, it's yours. All I ask in return is that if you | |
3701 * figure out how to do this in a Bourne Shell script you send me | |
3702 * a copy. | |
3703 * sdcsvax!rmr or rmr@uscd | |
3704 * | |
3705 * Severely hacked over by John Gilmore to make a 4.2BSD compatible | |
3706 * subroutine. 11Mar86; hoptoad!gnu | |
3707 * | |
3708 * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir, | |
3709 * subroutine didn't return EEXIST. It does now. | |
3710 */ | |
3711 | |
3712 /* | |
3713 * Make a directory. | |
3714 */ | |
3715 #ifdef MKDIR_PROTOTYPE | |
3716 MKDIR_PROTOTYPE | |
3717 #else | |
3718 int | |
442 | 3719 mkdir (const char *dpath, int dmode) |
428 | 3720 #endif |
3721 { | |
3722 int cpid, status, fd; | |
3723 struct stat statbuf; | |
3724 | |
442 | 3725 if (stat (dpath, &statbuf) == 0) /* we do want stat() here */ |
428 | 3726 { |
3727 errno = EEXIST; /* Stat worked, so it already exists */ | |
3728 return -1; | |
3729 } | |
3730 | |
3731 /* If stat fails for a reason other than non-existence, return error */ | |
3732 if (errno != ENOENT) | |
3733 return -1; | |
3734 | |
3735 synch_process_alive = 1; | |
3736 switch (cpid = fork ()) | |
3737 { | |
3738 | |
3739 case -1: /* Error in fork() */ | |
3740 return -1; /* Errno is set already */ | |
3741 | |
3742 case 0: /* Child process */ | |
3743 { | |
3744 /* | |
3745 * Cheap hack to set mode of new directory. Since this | |
3746 * child process is going away anyway, we zap its umask. | |
3747 * ####, this won't suffice to set SUID, SGID, etc. on this | |
3748 * directory. Does anybody care? | |
3749 */ | |
3750 status = umask (0); /* Get current umask */ | |
3751 status = umask (status | (0777 & ~dmode)); /* Set for mkdir */ | |
771 | 3752 fd = open ("/dev/null", O_RDWR); |
428 | 3753 if (fd >= 0) |
3754 { | |
3755 if (fd != STDIN_FILENO) dup2 (fd, STDIN_FILENO); | |
3756 if (fd != STDOUT_FILENO) dup2 (fd, STDOUT_FILENO); | |
3757 if (fd != STDERR_FILENO) dup2 (fd, STDERR_FILENO); | |
3758 } | |
3759 execl ("/bin/mkdir", "mkdir", dpath, (char *) 0); | |
3760 _exit (-1); /* Can't exec /bin/mkdir */ | |
3761 } | |
3762 | |
3763 default: /* Parent process */ | |
3764 wait_for_termination (cpid); | |
3765 } | |
3766 | |
3767 if (synch_process_death != 0 || synch_process_retcode != 0) | |
3768 { | |
3769 errno = EIO; /* We don't know why, but */ | |
3770 return -1; /* /bin/mkdir failed */ | |
3771 } | |
3772 | |
3773 return 0; | |
3774 } | |
3775 #endif /* not HAVE_MKDIR */ | |
3776 | |
3777 #ifndef HAVE_RMDIR | |
3778 int | |
442 | 3779 rmdir (const char *dpath) |
428 | 3780 { |
3781 int cpid, status, fd; | |
3782 struct stat statbuf; | |
3783 | |
442 | 3784 if (stat (dpath, &statbuf) != 0) /* we do want stat() here */ |
428 | 3785 { |
3786 /* Stat just set errno. We don't have to */ | |
3787 return -1; | |
3788 } | |
3789 | |
3790 synch_process_alive = 1; | |
3791 switch (cpid = fork ()) | |
3792 { | |
3793 | |
3794 case -1: /* Error in fork() */ | |
3795 return (-1); /* Errno is set already */ | |
3796 | |
3797 case 0: /* Child process */ | |
771 | 3798 fd = open ("/dev/null", O_RDWR); |
428 | 3799 if (fd >= 0) |
3800 { | |
3801 if (fd != STDIN_FILENO) dup2 (fd, STDIN_FILENO); | |
3802 if (fd != STDOUT_FILENO) dup2 (fd, STDOUT_FILENO); | |
3803 if (fd != STDERR_FILENO) dup2 (fd, STDERR_FILENO); | |
3804 } | |
3805 execl ("/bin/rmdir", "rmdir", dpath, (char *) 0); | |
3806 _exit (-1); /* Can't exec /bin/mkdir */ | |
3807 | |
3808 default: /* Parent process */ | |
3809 wait_for_termination (cpid); | |
3810 } | |
3811 | |
3812 if (synch_process_death != 0 || | |
3813 synch_process_retcode != 0) | |
3814 { | |
3815 errno = EIO; /* We don't know why, but */ | |
3816 return -1; /* /bin/rmdir failed */ | |
3817 } | |
3818 | |
3819 return 0; | |
3820 } | |
3821 #endif /* !HAVE_RMDIR */ | |
3822 | |
3823 | |
3824 /************************************************************************/ | |
3825 /* Misc. SunOS crap */ | |
3826 /************************************************************************/ | |
3827 | |
3828 #ifdef USE_DL_STUBS | |
3829 | |
3830 /* These are included on Sunos 4.1 when we do not use shared libraries. | |
3831 X11 libraries may refer to these functions but (we hope) do not | |
3832 actually call them. */ | |
3833 | |
3834 void * | |
3835 dlopen (void) | |
3836 { | |
3837 return 0; | |
3838 } | |
3839 | |
3840 void * | |
3841 dlsym (void) | |
3842 { | |
3843 return 0; | |
3844 } | |
3845 | |
3846 int | |
3847 dlclose (void) | |
3848 { | |
3849 return -1; | |
3850 } | |
3851 | |
3852 #endif /* USE_DL_STUBS */ |