1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1230
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
2536
|
27 #include <cstdlib> |
1343
|
28 #include <csignal> |
2536
|
29 |
1344
|
30 #include <new> |
|
31 |
1350
|
32 #include <iostream.h> |
|
33 |
|
34 #ifdef HAVE_UNISTD_H |
2442
|
35 #ifdef HAVE_SYS_TYPES_H |
834
|
36 #include <sys/types.h> |
2442
|
37 #endif |
834
|
38 #include <unistd.h> |
|
39 #endif |
1343
|
40 |
1352
|
41 #include "error.h" |
1373
|
42 #include "load-save.h" |
2091
|
43 #include "pager.h" |
1
|
44 #include "sighandlers.h" |
1352
|
45 #include "syswait.h" |
2091
|
46 #include "toplev.h" |
1
|
47 #include "utils.h" |
|
48 |
|
49 // Nonzero means we have already printed a message for this series of |
|
50 // SIGPIPES. We assume that the writer will eventually give up. |
|
51 int pipe_handler_error_count = 0; |
|
52 |
|
53 // Nonzero means we can be interrupted. |
|
54 int can_interrupt = 0; |
|
55 |
2016
|
56 // Allow us to save the signal mask and then restore it to the most |
|
57 // recently saved value. This is necessary when using the POSIX |
|
58 // signal handling interface on some systems calling longjmp out of |
|
59 // the signal handler to get to the top level on an interrupt doesn't |
|
60 // restore the original signal mask. Alternatively, we could use |
|
61 // sigsetjmp/siglongjmp, but saving and restoring the signal mask |
|
62 // ourselves works ok and seems simpler just now. |
|
63 |
|
64 #if defined (HAVE_POSIX_SIGNALS) |
|
65 static sigset_t octave_signal_mask; |
|
66 #endif |
|
67 |
2469
|
68 #if RETSIGTYPE == void |
|
69 #define SIGHANDLER_RETURN(status) return |
|
70 #else |
|
71 #define SIGHANDLER_RETURN(status) return status |
|
72 #endif |
|
73 |
2554
|
74 #if defined (MUST_REINSTALL_SIGHANDLERS) |
|
75 #define MAYBE_REINSTALL_SIGHANDLER(sig, handler) \ |
|
76 octave_set_signal_handler (sig, handler) |
|
77 #else |
|
78 #define MAYBE_REINSTALL_SIGHANDLER(sig, handler) \ |
|
79 do { } while (0) |
|
80 #endif |
|
81 |
|
82 #if defined (__EMX__) |
2626
|
83 #define MAYBE_ACK_SIGNAL(sig) \ |
2554
|
84 octave_set_signal_handler (sig, SIG_ACK) |
|
85 #else |
2626
|
86 #define MAYBE_ACK_SIGNAL(sig) \ |
2554
|
87 do { } while (0) |
|
88 #endif |
|
89 |
2016
|
90 void |
|
91 octave_save_signal_mask (void) |
|
92 { |
|
93 #if defined (HAVE_POSIX_SIGNALS) |
|
94 sigprocmask (0, 0, &octave_signal_mask); |
|
95 #endif |
|
96 } |
|
97 |
|
98 void |
|
99 octave_restore_signal_mask (void) |
|
100 { |
|
101 #if defined (HAVE_POSIX_SIGNALS) |
|
102 sigprocmask (SIG_SETMASK, &octave_signal_mask, 0); |
|
103 #endif |
|
104 } |
|
105 |
1
|
106 static void |
|
107 my_friendly_exit (const char *sig_name, int sig_number) |
|
108 { |
2536
|
109 static bool been_there_done_that = false; |
|
110 |
|
111 if (been_there_done_that) |
|
112 { |
2554
|
113 #if defined (SIGABRT) |
2536
|
114 octave_set_signal_handler (SIGABRT, SIG_DFL); |
|
115 #endif |
|
116 |
2795
|
117 cerr << "error: attempted clean up apparently failed -- aborting...\n"; |
1373
|
118 |
2536
|
119 abort (); |
|
120 } |
|
121 else |
|
122 { |
|
123 been_there_done_that = true; |
1373
|
124 |
2795
|
125 cerr << "error: " << sig_name << " -- stopping myself...\n"; |
2536
|
126 |
|
127 save_user_variables (); |
|
128 |
|
129 clean_up_and_exit (sig_number); |
|
130 } |
1
|
131 } |
|
132 |
635
|
133 // I know, not really a signal handler. |
|
134 |
|
135 static void |
|
136 octave_new_handler (void) |
|
137 { |
2795
|
138 cerr << "error: memory exhausted -- trying to return to prompt\n"; |
1394
|
139 |
|
140 if (can_interrupt) |
|
141 { |
|
142 jump_to_top_level (); |
|
143 panic_impossible (); |
|
144 } |
|
145 else |
1395
|
146 my_friendly_exit ("operator new", 1); |
635
|
147 } |
|
148 |
1446
|
149 sig_handler * |
|
150 octave_set_signal_handler (int sig, sig_handler *handler) |
|
151 { |
|
152 #if defined (HAVE_POSIX_SIGNALS) |
|
153 struct sigaction act, oact; |
|
154 act.sa_handler = handler; |
|
155 act.sa_flags = 0; |
|
156 sigemptyset (&act.sa_mask); |
|
157 sigemptyset (&oact.sa_mask); |
|
158 sigaction (sig, &act, &oact); |
|
159 return oact.sa_handler; |
|
160 #else |
|
161 return signal (sig, handler); |
|
162 #endif |
|
163 } |
|
164 |
1
|
165 static RETSIGTYPE |
1446
|
166 generic_sig_handler (int sig) |
1
|
167 { |
1446
|
168 my_friendly_exit (sys_siglist[sig], sig); |
1
|
169 |
2469
|
170 SIGHANDLER_RETURN (0); |
1
|
171 } |
|
172 |
2091
|
173 // Handle SIGCHLD. |
1230
|
174 |
|
175 static RETSIGTYPE |
2092
|
176 sigchld_handler (int /* sig */) |
1230
|
177 { |
2705
|
178 volatile octave_interrupt_handler saved_interrupt_handler |
2554
|
179 = octave_ignore_interrupts (); |
|
180 |
2626
|
181 // I wonder if this is really right, or if SIGCHLD should just be |
|
182 // blocked on OS/2 systems the same as for systems with POSIX signal |
|
183 // functions. |
|
184 |
|
185 #if defined (__EMX__) |
2554
|
186 volatile sig_handler *saved_sigchld_handler |
2626
|
187 = octave_set_signal_handler (SIGCHLD, SIG_IGN); |
2554
|
188 #endif |
|
189 |
2626
|
190 sigset_t set, oset; |
|
191 |
|
192 BLOCK_CHILD (set, oset); |
|
193 |
2209
|
194 int n = octave_child_list::length (); |
|
195 |
2554
|
196 if (n == 0) |
1230
|
197 { |
2554
|
198 waitpid (-1, 0, WNOHANG); |
|
199 } |
|
200 else |
|
201 { |
|
202 for (int i = 0; i < n; i++) |
|
203 { |
|
204 octave_child& elt = octave_child_list::elem (i); |
2209
|
205 |
2554
|
206 pid_t pid = elt.pid; |
2209
|
207 |
2554
|
208 if (pid > 0) |
2091
|
209 { |
2554
|
210 int status; |
|
211 |
|
212 if (waitpid (pid, &status, WNOHANG) > 0) |
|
213 { |
|
214 elt.pid = -1; |
2197
|
215 |
2554
|
216 octave_child::dead_child_handler f = elt.handler; |
2197
|
217 |
2554
|
218 if (f) |
|
219 f (pid, status); |
2209
|
220 |
2554
|
221 break; |
|
222 } |
2091
|
223 } |
1230
|
224 } |
|
225 } |
2469
|
226 |
2600
|
227 octave_set_interrupt_handler (saved_interrupt_handler); |
2554
|
228 |
2626
|
229 UNBLOCK_CHILD (oset); |
|
230 |
|
231 #ifdef __EMX__ |
2554
|
232 octave_set_signal_handler (SIGCHLD, saved_sigchld_handler); |
2475
|
233 #endif |
|
234 |
2626
|
235 MAYBE_ACK_SIGNAL (SIGCHLD); |
2554
|
236 |
|
237 MAYBE_REINSTALL_SIGHANDLER (SIGCHLD, sigchld_handler); |
|
238 |
2469
|
239 SIGHANDLER_RETURN (0); |
1230
|
240 } |
|
241 |
1373
|
242 #if defined (__alpha__) |
|
243 static RETSIGTYPE |
1488
|
244 sigfpe_handler (int /* sig */) |
1373
|
245 { |
2626
|
246 MAYBE_ACK_SIGNAL (SIGFPE); |
2554
|
247 |
|
248 MAYBE_REINSTALL_SIGHANDLER (SIGFPE, sigfpe_handler); |
1373
|
249 |
2795
|
250 cerr << "error: floating point exception -- trying to return to prompt\n"; |
1373
|
251 |
|
252 if (can_interrupt) |
|
253 { |
|
254 jump_to_top_level (); |
|
255 panic_impossible (); |
|
256 } |
|
257 |
2469
|
258 SIGHANDLER_RETURN (0); |
1373
|
259 } |
|
260 #endif |
|
261 |
635
|
262 // Handle SIGINT by restarting the parser (see octave.cc). |
2554
|
263 // |
|
264 // This also has to work for SIGBREAK (on systems that have it), so we |
|
265 // use the value of sig, instead of just assuming that it is called |
|
266 // for SIGINT only. |
635
|
267 |
1
|
268 static RETSIGTYPE |
2554
|
269 sigint_handler (int sig) |
1
|
270 { |
2626
|
271 MAYBE_ACK_SIGNAL (sig); |
2554
|
272 |
|
273 MAYBE_REINSTALL_SIGHANDLER (sig, sigint_handler); |
834
|
274 |
1
|
275 if (can_interrupt) |
|
276 { |
|
277 jump_to_top_level (); |
|
278 panic_impossible (); |
|
279 } |
|
280 |
2469
|
281 SIGHANDLER_RETURN (0); |
1
|
282 } |
|
283 |
|
284 static RETSIGTYPE |
1488
|
285 sigpipe_handler (int /* sig */) |
1
|
286 { |
2626
|
287 MAYBE_ACK_SIGNAL (SIGPIPE); |
2554
|
288 |
|
289 MAYBE_REINSTALL_SIGHANDLER (SIGPIPE, sigpipe_handler); |
834
|
290 |
1
|
291 if (pipe_handler_error_count++ == 0) |
2795
|
292 cerr << "warning: broken pipe\n"; |
1
|
293 |
1358
|
294 // Don't loop forever on account of this. |
|
295 |
1
|
296 if (pipe_handler_error_count > 100) |
|
297 jump_to_top_level (); |
|
298 |
2469
|
299 SIGHANDLER_RETURN (0); |
1
|
300 } |
|
301 |
2705
|
302 octave_interrupt_handler |
2554
|
303 octave_catch_interrupts (void) |
|
304 { |
2705
|
305 octave_interrupt_handler retval; |
2554
|
306 |
|
307 #ifdef SIGINT |
2705
|
308 retval.int_handler = octave_set_signal_handler (SIGINT, sigint_handler); |
2554
|
309 #endif |
|
310 |
|
311 #ifdef SIGBREAK |
2705
|
312 retval.brk_handler = octave_set_signal_handler (SIGBREAK, sigint_handler); |
2554
|
313 #endif |
2705
|
314 |
|
315 return retval; |
2554
|
316 } |
|
317 |
2705
|
318 octave_interrupt_handler |
|
319 octave_ignore_interrupts (void) |
2554
|
320 { |
2705
|
321 octave_interrupt_handler retval; |
|
322 |
2554
|
323 #ifdef SIGINT |
2705
|
324 retval.int_handler = octave_set_signal_handler (SIGINT, SIG_IGN); |
2554
|
325 #endif |
|
326 |
|
327 #ifdef SIGBREAK |
2705
|
328 retval.brk_handler = octave_set_signal_handler (SIGBREAK, SIG_IGN); |
2554
|
329 #endif |
2705
|
330 |
|
331 return retval; |
|
332 } |
|
333 |
|
334 octave_interrupt_handler |
|
335 octave_set_interrupt_handler (const volatile octave_interrupt_handler& h) |
|
336 { |
|
337 octave_interrupt_handler retval; |
|
338 |
|
339 #ifdef SIGINT |
|
340 retval.int_handler = octave_set_signal_handler (SIGINT, h.int_handler); |
|
341 #endif |
|
342 |
|
343 #ifdef SIGBREAK |
|
344 retval.brk_handler = octave_set_signal_handler (SIGBREAK, h.brk_handler); |
|
345 #endif |
|
346 |
|
347 return retval; |
1651
|
348 } |
|
349 |
635
|
350 // Install all the handlers for the signals we might care about. |
|
351 |
1
|
352 void |
|
353 install_signal_handlers (void) |
|
354 { |
635
|
355 set_new_handler (octave_new_handler); |
|
356 |
2554
|
357 octave_catch_interrupts (); |
|
358 |
1
|
359 #ifdef SIGABRT |
1446
|
360 octave_set_signal_handler (SIGABRT, generic_sig_handler); |
1
|
361 #endif |
|
362 |
|
363 #ifdef SIGALRM |
1446
|
364 octave_set_signal_handler (SIGALRM, generic_sig_handler); |
1
|
365 #endif |
|
366 |
|
367 #ifdef SIGBUS |
1446
|
368 octave_set_signal_handler (SIGBUS, generic_sig_handler); |
1
|
369 #endif |
|
370 |
1230
|
371 #ifdef SIGCHLD |
1446
|
372 octave_set_signal_handler (SIGCHLD, sigchld_handler); |
1230
|
373 #endif |
|
374 |
1
|
375 #ifdef SIGEMT |
1446
|
376 octave_set_signal_handler (SIGEMT, generic_sig_handler); |
1
|
377 #endif |
|
378 |
|
379 #ifdef SIGFPE |
1373
|
380 #if defined (__alpha__) |
1446
|
381 octave_set_signal_handler (SIGFPE, sigfpe_handler); |
1373
|
382 #else |
1446
|
383 octave_set_signal_handler (SIGFPE, generic_sig_handler); |
1
|
384 #endif |
1373
|
385 #endif |
1
|
386 |
|
387 #ifdef SIGHUP |
1446
|
388 octave_set_signal_handler (SIGHUP, generic_sig_handler); |
1
|
389 #endif |
|
390 |
|
391 #ifdef SIGILL |
1446
|
392 octave_set_signal_handler (SIGILL, generic_sig_handler); |
1
|
393 #endif |
|
394 |
|
395 #ifdef SIGIOT |
1446
|
396 octave_set_signal_handler (SIGIOT, generic_sig_handler); |
1
|
397 #endif |
|
398 |
|
399 #ifdef SIGLOST |
1446
|
400 octave_set_signal_handler (SIGLOST, generic_sig_handler); |
1
|
401 #endif |
|
402 |
|
403 #ifdef SIGPIPE |
1446
|
404 octave_set_signal_handler (SIGPIPE, sigpipe_handler); |
1
|
405 #endif |
|
406 |
|
407 #ifdef SIGPOLL |
1446
|
408 octave_set_signal_handler (SIGPOLL, SIG_IGN); |
1
|
409 #endif |
|
410 |
|
411 #ifdef SIGPROF |
1446
|
412 octave_set_signal_handler (SIGPROF, generic_sig_handler); |
1
|
413 #endif |
|
414 |
|
415 #ifdef SIGQUIT |
1446
|
416 octave_set_signal_handler (SIGQUIT, generic_sig_handler); |
1
|
417 #endif |
|
418 |
|
419 #ifdef SIGSEGV |
1446
|
420 octave_set_signal_handler (SIGSEGV, generic_sig_handler); |
1
|
421 #endif |
|
422 |
|
423 #ifdef SIGSYS |
1446
|
424 octave_set_signal_handler (SIGSYS, generic_sig_handler); |
1
|
425 #endif |
|
426 |
|
427 #ifdef SIGTERM |
1446
|
428 octave_set_signal_handler (SIGTERM, generic_sig_handler); |
1
|
429 #endif |
|
430 |
|
431 #ifdef SIGTRAP |
1446
|
432 octave_set_signal_handler (SIGTRAP, generic_sig_handler); |
1
|
433 #endif |
|
434 |
|
435 #ifdef SIGUSR1 |
1446
|
436 octave_set_signal_handler (SIGUSR1, generic_sig_handler); |
1
|
437 #endif |
|
438 |
|
439 #ifdef SIGUSR2 |
1446
|
440 octave_set_signal_handler (SIGUSR2, generic_sig_handler); |
1
|
441 #endif |
|
442 |
|
443 #ifdef SIGVTALRM |
1446
|
444 octave_set_signal_handler (SIGVTALRM, generic_sig_handler); |
1
|
445 #endif |
|
446 |
895
|
447 #ifdef SIGIO |
1446
|
448 octave_set_signal_handler (SIGIO, SIG_IGN); |
895
|
449 #endif |
|
450 |
1
|
451 #ifdef SIGXCPU |
1446
|
452 octave_set_signal_handler (SIGXCPU, generic_sig_handler); |
1
|
453 #endif |
|
454 |
|
455 #ifdef SIGXFSZ |
1446
|
456 octave_set_signal_handler (SIGXFSZ, generic_sig_handler); |
1
|
457 #endif |
|
458 } |
|
459 |
886
|
460 #ifndef HAVE_SYS_SIGLIST |
839
|
461 char *sys_siglist[NSIG + 1] = |
|
462 { |
|
463 #ifdef AIX |
|
464 /* AIX has changed the signals a bit */ |
|
465 "bogus signal", /* 0 */ |
|
466 "hangup", /* 1 SIGHUP */ |
|
467 "interrupt", /* 2 SIGINT */ |
|
468 "quit", /* 3 SIGQUIT */ |
|
469 "illegal instruction", /* 4 SIGILL */ |
|
470 "trace trap", /* 5 SIGTRAP */ |
|
471 "IOT instruction", /* 6 SIGIOT */ |
|
472 "crash likely", /* 7 SIGDANGER */ |
|
473 "floating point exception", /* 8 SIGFPE */ |
|
474 "kill", /* 9 SIGKILL */ |
|
475 "bus error", /* 10 SIGBUS */ |
|
476 "segmentation violation", /* 11 SIGSEGV */ |
|
477 "bad argument to system call", /* 12 SIGSYS */ |
|
478 "write on a pipe with no one to read it", /* 13 SIGPIPE */ |
|
479 "alarm clock", /* 14 SIGALRM */ |
|
480 "software termination signum", /* 15 SIGTERM */ |
|
481 "user defined signal 1", /* 16 SIGUSR1 */ |
|
482 "user defined signal 2", /* 17 SIGUSR2 */ |
|
483 "death of a child", /* 18 SIGCLD */ |
|
484 "power-fail restart", /* 19 SIGPWR */ |
|
485 "bogus signal", /* 20 */ |
|
486 "bogus signal", /* 21 */ |
|
487 "bogus signal", /* 22 */ |
|
488 "bogus signal", /* 23 */ |
|
489 "bogus signal", /* 24 */ |
|
490 "LAN I/O interrupt", /* 25 SIGAIO */ |
|
491 "PTY I/O interrupt", /* 26 SIGPTY */ |
|
492 "I/O intervention required", /* 27 SIGIOINT */ |
|
493 "HFT grant", /* 28 SIGGRANT */ |
|
494 "HFT retract", /* 29 SIGRETRACT */ |
|
495 "HFT sound done", /* 30 SIGSOUND */ |
|
496 "HFT input ready", /* 31 SIGMSG */ |
|
497 #else /* not AIX */ |
2512
|
498 #ifdef __EMX__ |
|
499 "bogus signal #0", /* 0 */ |
|
500 "hangup", /* 1 SIGHUP */ |
|
501 "interrupt (Ctrl-C)", /* 2 SIGINT (Ctrl-C) */ |
|
502 "quit", /* 3 SIGQUIT */ |
|
503 "illegal instruction", /* 4 SIGILL */ |
|
504 "single step", /* 5 SIGTRAP */ |
|
505 "abort", /* 6 SIGABRT */ |
|
506 "EMT instruction", /* 7 SIGEMT */ |
|
507 "floating point exception", /* 8 SIGFPE */ |
|
508 "kill", /* 9 SIGKILL */ |
|
509 "bus error", /* 10 SIGBUS */ |
|
510 "segmentation violation", /* 11 SIGSEGV */ |
|
511 "bad argument to system call", /* 12 SIGSYS */ |
|
512 "broken pipe", /* 13 SIGPIPE */ |
|
513 "alarm clock", /* 14 SIGALRM */ |
|
514 "software termination signum", /* 15 SIGTERM */ |
|
515 "user defined signal 1", /* 16 SIGUSR1 */ |
|
516 "user defined signal 2", /* 17 SIGUSR2 */ |
|
517 "death of a child", /* 18 SIGCHLD */ |
|
518 "bogus signal #20", /* 19 */ |
|
519 "bogus signal #21", /* 20 */ |
|
520 "break (Ctrl-Break)", /* 21 SIGBREAK */ |
|
521 #else /* not __EMX__ */ |
839
|
522 "bogus signal", /* 0 */ |
|
523 "hangup", /* 1 SIGHUP */ |
|
524 "interrupt", /* 2 SIGINT */ |
|
525 "quit", /* 3 SIGQUIT */ |
|
526 "illegal instruction", /* 4 SIGILL */ |
|
527 "trace trap", /* 5 SIGTRAP */ |
|
528 "IOT instruction", /* 6 SIGIOT */ |
|
529 "EMT instruction", /* 7 SIGEMT */ |
|
530 "floating point exception", /* 8 SIGFPE */ |
|
531 "kill", /* 9 SIGKILL */ |
|
532 "bus error", /* 10 SIGBUS */ |
|
533 "segmentation violation", /* 11 SIGSEGV */ |
|
534 "bad argument to system call", /* 12 SIGSYS */ |
|
535 "write on a pipe with no one to read it", /* 13 SIGPIPE */ |
|
536 "alarm clock", /* 14 SIGALRM */ |
|
537 "software termination signum", /* 15 SIGTERM */ |
|
538 "user defined signal 1", /* 16 SIGUSR1 */ |
|
539 "user defined signal 2", /* 17 SIGUSR2 */ |
|
540 "death of a child", /* 18 SIGCLD */ |
|
541 "power-fail restart", /* 19 SIGPWR */ |
|
542 #ifdef sun |
|
543 "window size change", /* 20 SIGWINCH */ |
|
544 "urgent socket condition", /* 21 SIGURG */ |
|
545 "pollable event occured", /* 22 SIGPOLL */ |
|
546 "stop (cannot be caught or ignored)", /* 23 SIGSTOP */ |
|
547 "user stop requested from tty", /* 24 SIGTSTP */ |
|
548 "stopped process has been continued", /* 25 SIGCONT */ |
|
549 "background tty read attempted", /* 26 SIGTTIN */ |
|
550 "background tty write attempted", /* 27 SIGTTOU */ |
|
551 "virtual timer expired", /* 28 SIGVTALRM */ |
|
552 "profiling timer expired", /* 29 SIGPROF */ |
|
553 "exceeded cpu limit", /* 30 SIGXCPU */ |
|
554 "exceeded file size limit", /* 31 SIGXFSZ */ |
|
555 "process's lwps are blocked", /* 32 SIGWAITING */ |
|
556 "special signal used by thread library", /* 33 SIGLWP */ |
|
557 #ifdef SIGFREEZE |
|
558 "Special Signal Used By CPR", /* 34 SIGFREEZE */ |
|
559 #endif |
|
560 #ifdef SIGTHAW |
|
561 "Special Signal Used By CPR", /* 35 SIGTHAW */ |
|
562 #endif |
|
563 #endif /* sun */ |
2512
|
564 #endif /* __EMX__ */ |
|
565 #endif /* AIX */ |
839
|
566 0 |
|
567 }; |
|
568 #endif |
|
569 |
2209
|
570 octave_child_list *octave_child_list::instance = 0; |
|
571 |
2926
|
572 bool |
|
573 octave_child_list::instance_ok (void) |
|
574 { |
|
575 bool retval = true; |
|
576 |
|
577 if (! instance) |
|
578 instance = new octave_child_list (); |
|
579 |
|
580 if (! instance) |
|
581 { |
|
582 ::error ("unable to create child list object!"); |
|
583 |
|
584 retval = false; |
|
585 } |
|
586 |
|
587 return retval; |
|
588 } |
|
589 |
|
590 void |
|
591 octave_child_list::insert (pid_t pid, octave_child::dead_child_handler f) |
|
592 { |
|
593 if (instance_ok ()) |
|
594 instance->do_insert (pid, f); |
|
595 } |
|
596 |
|
597 void |
|
598 octave_child_list::remove (pid_t pid) |
|
599 { |
|
600 if (instance_ok ()) |
|
601 instance->do_remove (pid); |
|
602 } |
|
603 |
|
604 int |
|
605 octave_child_list::length (void) |
|
606 { |
|
607 return (instance_ok ()) ? instance->do_length () : 0; |
|
608 } |
|
609 |
|
610 octave_child& |
|
611 octave_child_list::elem (int i) |
|
612 { |
|
613 static octave_child foo; |
|
614 |
|
615 return (instance_ok ()) ? instance->do_elem (i) : foo; |
|
616 } |
|
617 |
2209
|
618 void |
|
619 octave_child_list::do_insert (pid_t pid, octave_child::dead_child_handler f) |
|
620 { |
|
621 // Insert item in first open slot, increasing size of list if |
|
622 // necessary. |
|
623 |
|
624 bool enlarge = true; |
|
625 |
|
626 for (int i = 0; i < curr_len; i++) |
|
627 { |
2305
|
628 octave_child& tmp = list (i); |
2209
|
629 |
|
630 if (tmp.pid < 0) |
|
631 { |
2305
|
632 list (i) = octave_child (pid, f); |
2209
|
633 enlarge = false; |
|
634 break; |
|
635 } |
|
636 } |
|
637 |
|
638 if (enlarge) |
|
639 { |
|
640 int total_len = list.length (); |
|
641 |
|
642 if (curr_len == total_len) |
|
643 { |
|
644 if (total_len == 0) |
|
645 list.resize (16); |
|
646 else |
|
647 list.resize (total_len * 2); |
|
648 } |
|
649 |
2305
|
650 list (curr_len) = octave_child (pid, f); |
2209
|
651 curr_len++; |
|
652 } |
|
653 } |
|
654 |
|
655 void |
2210
|
656 octave_child_list::do_remove (pid_t pid) |
|
657 { |
|
658 // Mark the record for PID invalid. |
|
659 |
|
660 for (int i = 0; i < curr_len; i++) |
|
661 { |
2305
|
662 octave_child& tmp = list (i); |
2210
|
663 |
|
664 if (tmp.pid == pid) |
|
665 { |
|
666 tmp.pid = -1; |
|
667 break; |
|
668 } |
|
669 } |
|
670 } |
|
671 |
2926
|
672 int |
|
673 octave_child_list::do_length (void) const |
|
674 { |
|
675 return curr_len; |
|
676 } |
|
677 |
|
678 octave_child& |
|
679 octave_child_list::do_elem (int i) |
2210
|
680 { |
2926
|
681 static octave_child foo; |
|
682 |
|
683 int n = do_length (); |
2210
|
684 |
2926
|
685 if (i >= 0 && i < n) |
|
686 return list (i); |
2210
|
687 else |
2926
|
688 return foo; |
2210
|
689 } |
|
690 |
1
|
691 /* |
|
692 ;;; Local Variables: *** |
|
693 ;;; mode: C++ *** |
|
694 ;;; End: *** |
|
695 */ |