433{
434 int pid;
435 int in_fd[2];
436 int out_fd[2];
437 fd_set fdset;
438 fd_set readset;
439 struct timeval tv;
440 int sel,
index, state;
441 char buffer[256];
442 char c;
443
444 if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
445 {
446 perror ("could open pipe");
448 }
449
450 pid = fork ();
451 if (pid == 0)
452 {
453 si_close (0); si_dup2 (in_fd[0],0);
454 si_close (1); si_dup2 (out_fd[1],1);
455 si_close (2); si_dup2 (out_fd[1],2);
456
457 execvp (args[0], args);
458 perror ("exec failed");
460 }
461 else if (pid == -1)
462 {
463 perror ("could not fork");
465 }
466
467 FD_ZERO (&fdset);
468 FD_SET (out_fd[0], &fdset);
469
470 si_write (in_fd[1], "backtrace\n", 10);
471 si_write (in_fd[1], "p si_stop_stack_trace_x = 0\n", 28);
472 si_write (in_fd[1], "quit\n", 5);
473
475 state = 0;
476
478 {
479 readset = fdset;
480 tv.tv_sec = 1;
481 tv.tv_usec = 0;
482
483 sel = si_select (FD_SETSIZE, &readset,
NULL,
NULL, &tv);
484 if (sel == -1)
485 break;
486
487 if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
488 {
489 if (si_read (out_fd[0], &c, 1))
490 {
491 switch (state)
492 {
493 case 0:
494 if (c == '#')
495 {
496 state = 1;
499 }
500 break;
501 case 1:
503 if ((c == '\n') || (c == '\r'))
504 {
506 fputs (buffer,stderr);
507 state = 0;
509 }
510 break;
511 default:
512 break;
513 }
514 }
515 }
517 break;
518 }
519
520 si_close (in_fd[0]);
521 si_close (in_fd[1]);
522 si_close (out_fd[0]);
523 si_close (out_fd[1]);
525}
static int index(p_Length length, p_Ord ord)