-#include "portable.h"
-#include "xbt/sysdep.h"
-#include "xbt/function_types.h"
-#include "xbt/log.h"
-#include "xbt/trim.h"
-
-#include <sys/types.h>
-#include <sys/wait.h>
-
-/**
- ** Buffer code
- **/
-typedef struct {
- char *data;
- int used,size;
-} buff_t;
-
-static void buff_empty(buff_t *b) {
- b->used=0;
- b->data[0]='\n';
- b->data[1]='\0';
-}
-static buff_t *buff_new(void) {
- buff_t *res=malloc(sizeof(buff_t));
- res->data=malloc(512);
- res->size=512;
- buff_empty(res);
- return res;
-}
-static void buff_free(buff_t *b) {
- if (b) {
- if (b->data)
- free(b->data);
- free(b);
- }
-}
-static void buff_append(buff_t *b, char *toadd) {
- int addlen=strlen(toadd);
- int needed_space=b->used+addlen+1;
-
- if (needed_space > b->size) {
- b->data = realloc(b->data, needed_space);
- b->size = needed_space;
- }
- strcpy(b->data+b->used, toadd);
- b->used += addlen;
-}
-static void buff_chomp(buff_t *b) {
- while (b->data[b->used] == '\n') {
- b->data[b->used] = '\0';
- if (b->used)
- b->used--;
- }
-}
-
-static void buff_trim(buff_t* b)
-{
- trim(b->data," ");
- b->used = strlen(b->data);
-}
-
-typedef struct s_signal_entry
-{
- const char* name;
- int number;
-}s_signal_entry_t,* signal_entry_t;
-
-static const s_signal_entry_t signals[] =
-{
- {"SIGHUP" ,1},
- {"SIGINT" ,2},
- {"SIGQUIT" ,3},
- {"SIGILL" ,4},
- {"SIGTRAP" ,5},
- {"SIGABRT" ,6},
- {"SIGEMT" ,7},
- {"SIGFPE" ,8},
- {"SIGKILL" ,9},
- {"SIGBUS" ,10},
- {"SIGSEGV" ,11},
- {"SIGSYS" ,12},
- {"SIGPIPE" ,13},
- {"SIGALRM" ,14},
- {"SIGTERM" ,15},
- {"SIGURG" ,16},
- {"SIGSTOP" ,17},
- {"SIGTSTP" ,18},
- {"SIGCONT" ,19},
- {"SIGCHLD" ,20},
- {"SIGTTIN" ,21},
- {"SIGTTOU" ,22},
- {"SIGIO" ,23},
- {"SIGXCPU" ,24},
- {"SIGXFSZ" ,25},
- {"SIGVTALRM",26},
- {"SIGPROF" ,27},
- {"SIGWINCH" ,28},
- {"SIGINFO" ,29},
- {"SIGUSR1" ,30},
- {"SIGUSR2" ,31}
-};
-
-#define SIGMAX 31
-#define SIGUNKNW SIGMAX + 1
-
-/* returns the name of the signal from it number */
-const char*
-signal_name(unsigned int number);
-
-/**
- ** Options
- **/
-int timeout_value = 5; /* child timeout value */
-char* expected_signal=NULL; /* !=NULL if the following command should raise a signal */
-int expected_return=0; /* the exepeted return code of following command */
-int verbose=0; /* wheather we should wine on problems */
-
-/**
- ** Dealing with timeouts
- **/
-int timeouted;
-static void timeout_handler(int sig) {
- timeouted = 1;
-}
-/**
- ** Dealing with timeouts
- **/
-int brokenpipe;
-static void pipe_handler(int sig) {
- brokenpipe = 1;
-}
-
-/**
- ** Launching a child
- **/
-buff_t *input;
-buff_t *output_wanted;
-buff_t *output_got;
-
-static void check_output() {
- if (output_wanted->used==0
- && output_got->used==0)
- return;
- buff_chomp(output_got);
- buff_chomp(output_wanted);
- buff_trim(output_got);
-buff_trim(output_wanted);
-
- if ( output_got->used != output_wanted->used
- || strcmp(output_got->data, output_wanted->data)) {
- fprintf(stderr,"Output don't match expectations\n");
- fprintf(stderr,">>>>> Expected %d chars:\n%s\n<<<<< Expected\n",
- output_wanted->used,output_wanted->data);
- fprintf(stderr,">>>>> Got %d chars:\n%s\n<<<<< Got\n",
- output_got->used,output_got->data);
- exit(2);
- }
- buff_empty(output_wanted);
- buff_empty(output_got);
-
-}
-
-static void exec_cmd(char *cmd) {
- int child_stdin[2];
- int child_stdout[2];