Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Better modularization of the tesh source code
authormquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Fri, 27 Apr 2007 09:02:56 +0000 (09:02 +0000)
committermquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Fri, 27 Apr 2007 09:02:56 +0000 (09:02 +0000)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@3465 48e7efb5-ca39-0410-a469-dd3cf9ba447f

tools/tesh/Makefile.am
tools/tesh/Makefile.in
tools/tesh/buff.c [new file with mode: 0644]
tools/tesh/buff.h [new file with mode: 0644]
tools/tesh/signal.c [new file with mode: 0644]
tools/tesh/tesh.c
tools/tesh/tesh.h [new file with mode: 0644]

index ef2c076..c72f5e8 100644 (file)
@@ -4,7 +4,7 @@ INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/src/include
 
 bin_PROGRAMS = tesh
 
-tesh_SOURCES = tesh.c
+tesh_SOURCES = tesh.c tesh.h buff.h buff.c signal.c
 tesh_LDADD   = $(top_builddir)/src/libsimgrid.la 
 
 TESTS_ENVIRONMENT=./tesh
index 30c7f00..3ea7e9a 100644 (file)
@@ -63,7 +63,7 @@ CONFIG_CLEAN_FILES =
 am__installdirs = "$(DESTDIR)$(bindir)"
 binPROGRAMS_INSTALL = $(INSTALL_PROGRAM)
 PROGRAMS = $(bin_PROGRAMS)
-am_tesh_OBJECTS = tesh.$(OBJEXT)
+am_tesh_OBJECTS = tesh.$(OBJEXT) buff.$(OBJEXT) signal.$(OBJEXT)
 tesh_OBJECTS = $(am_tesh_OBJECTS)
 tesh_DEPENDENCIES = $(top_builddir)/src/libsimgrid.la
 DEFAULT_INCLUDES = -I. -I$(srcdir) -I$(top_builddir)/src
@@ -222,7 +222,7 @@ target_cpu = @target_cpu@
 target_os = @target_os@
 target_vendor = @target_vendor@
 INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/src/include
-tesh_SOURCES = tesh.c
+tesh_SOURCES = tesh.c tesh.h buff.h buff.c signal.c
 tesh_LDADD = $(top_builddir)/src/libsimgrid.la 
 TESTS_ENVIRONMENT = ./tesh
 TESTS = basic.tesh  cd.tesh \
@@ -299,6 +299,8 @@ mostlyclean-compile:
 distclean-compile:
        -rm -f *.tab.c
 
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/buff.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/signal.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tesh.Po@am__quote@
 
 .c.o:
diff --git a/tools/tesh/buff.c b/tools/tesh/buff.c
new file mode 100644 (file)
index 0000000..4c8cbd6
--- /dev/null
@@ -0,0 +1,63 @@
+/* $Id$ */
+
+/* buff -- buffers as needed by tesh                                        */
+
+/* Copyright (c) 2007 Martin Quinson.                                       */
+/* All rights reserved.                                                     */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+/* specific to Borland Compiler */
+#ifdef __BORLANDDC__
+#pragma hdrstop
+#endif
+
+#include "buff.h"
+
+/**
+ ** Buffer code
+ **/
+
+void buff_empty(buff_t *b) {
+  b->used=0;
+  b->data[0]='\n';
+  b->data[1]='\0';
+}
+buff_t *buff_new(void) {
+  buff_t *res=malloc(sizeof(buff_t));
+  res->data=malloc(512);
+  res->size=512;
+  buff_empty(res);
+  return res;
+}
+void buff_free(buff_t *b) {
+  if (b) {
+    if (b->data)
+      free(b->data);
+    free(b);
+  }
+}
+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;  
+}
+void buff_chomp(buff_t *b) {
+  while (b->data[b->used] == '\n') {
+    b->data[b->used] = '\0';
+    if (b->used)
+      b->used--;
+  }
+}
+
+void buff_trim(buff_t* b) {
+  xbt_str_trim(b->data," ");
+  b->used = strlen(b->data);
+}
diff --git a/tools/tesh/buff.h b/tools/tesh/buff.h
new file mode 100644 (file)
index 0000000..a0a1633
--- /dev/null
@@ -0,0 +1,36 @@
+/* $Id$ */
+
+/* buff -- buffers as needed by tesh                                        */
+
+/* Copyright (c) 2007 Martin Quinson.                                       */
+/* All rights reserved.                                                     */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#ifndef TESH_BUFF_H
+#define TESH_BUFF_H
+
+#include "portable.h"
+#include "xbt/sysdep.h"
+#include "xbt/function_types.h"
+#include "xbt/log.h"
+#include "xbt/str.h"
+
+/**
+ ** Buffer code
+ **/
+typedef struct {
+  char *data;
+  int used,size;
+} buff_t;
+
+
+void buff_empty(buff_t *b);
+buff_t *buff_new(void);
+void buff_free(buff_t *b);
+void buff_append(buff_t *b, char *toadd);
+void buff_chomp(buff_t *b);
+void buff_trim(buff_t* b);
+
+#endif
diff --git a/tools/tesh/signal.c b/tools/tesh/signal.c
new file mode 100644 (file)
index 0000000..b70ce5b
--- /dev/null
@@ -0,0 +1,66 @@
+/* $Id$ */
+
+/* signal -- what TESH needs to know about signals                          */
+
+/* Copyright (c) 2007 Martin Quinson.                                       */
+/* All rights reserved.                                                     */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#include "tesh.h"
+#include <signal.h>
+
+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"       ,SIGHUP},
+       {"SIGINT"       ,SIGINT},
+       {"SIGQUIT"      ,SIGQUIT},
+       {"SIGILL"       ,SIGILL},
+       {"SIGTRAP"      ,SIGTRAP},
+       {"SIGABRT"      ,SIGABRT},
+       {"SIGFPE"       ,SIGFPE},
+       {"SIGKILL"      ,SIGKILL},
+       {"SIGBUS"       ,SIGBUS},
+       {"SIGSEGV"      ,SIGSEGV},
+       {"SIGSYS"       ,SIGSYS},
+       {"SIGPIPE"      ,SIGPIPE},
+       {"SIGALRM"      ,SIGALRM},
+       {"SIGTERM"      ,SIGTERM},
+       {"SIGURG"       ,SIGURG},
+       {"SIGSTOP"      ,SIGSTOP},
+       {"SIGTSTP"      ,SIGTSTP},
+       {"SIGCONT"      ,SIGCONT},
+       {"SIGCHLD"      ,SIGCHLD},
+       {"SIGTTIN"      ,SIGTTIN},
+       {"SIGTTOU"      ,SIGTTOU},
+       {"SIGIO"        ,SIGIO},
+       {"SIGXCPU"      ,SIGXCPU},
+       {"SIGXFSZ"      ,SIGXFSZ},
+       {"SIGVTALRM"    ,SIGVTALRM},
+       {"SIGPROF"      ,SIGPROF},
+       {"SIGWINCH"     ,SIGWINCH},
+       {"SIGUSR1"      ,SIGUSR1},
+       {"SIGUSR2"      ,SIGUSR2},
+       {"SIG UNKNOWN"  ,-1}
+};
+
+
+const char* signal_name(unsigned int got, char *expected) {
+  int i;
+
+  /* Make SIGBUS a synonym for SIGSEGV
+     (segfault leads to any of them depending on the system) */
+  if((got == SIGBUS) && !strcmp("SIGSEGV",expected))
+    got = SIGSEGV;
+  
+  for (i=0; signals[i].number != -1; i++)
+    if (signals[i].number == got)
+      return (signals[i].name);
+
+  return "SIG UNKNOWN";  
+}
index 794fd39..7b3db46 100644 (file)
 #pragma hdrstop
 #endif
 
-#include "portable.h"
-#include "xbt/sysdep.h"
-#include "xbt/function_types.h"
-#include "xbt/log.h"
-#include "xbt/str.h"
+#include "tesh.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)
-{
-       xbt_str_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
  **/
@@ -158,8 +54,8 @@ static void check_output() {
     return;
   buff_chomp(output_got);
   buff_chomp(output_wanted);
-   buff_trim(output_got);
-buff_trim(output_wanted);
+  buff_trim(output_got);
+  buff_trim(output_wanted);
 
   if (   output_got->used != output_wanted->used
       || strcmp(output_got->data, output_wanted->data)) {
@@ -262,8 +158,12 @@ static void exec_cmd(char *cmd) {
     /* Wait for child, and check why it terminated */
     wait(&status);
 
-    if (WIFSIGNALED(status) && strcmp(signal_name(WTERMSIG(status)),expected_signal)) {
-               fprintf(stderr,"Child got signal %s instead of signal %s\n",signal_name(WTERMSIG(status)), expected_signal);
+    if (WIFSIGNALED(status) && 
+       strcmp(signal_name(WTERMSIG(status),expected_signal),
+              expected_signal)) {
+               fprintf(stderr,"Child got signal %s instead of signal %s\n",
+                       signal_name(WTERMSIG(status),expected_signal),
+                       expected_signal);
                exit(WTERMSIG(status)+4);       
        }
        
@@ -492,15 +392,3 @@ int main(int argc,char *argv[]) {
   return 0;  
 }
 
-const char* 
-signal_name(unsigned int number)
-{
-       if(number > SIGMAX)
-               return "SIGUNKNW";
-               
-       /* special case of SIGSEGV and SIGBUS (be conditional of the implementation)*/
-       if((number == SIGBUS) && strcmp("SIGBUS",expected_signal))
-               number = SIGSEGV;
-               
-       return (signals[number - 1].name);
-}
diff --git a/tools/tesh/tesh.h b/tools/tesh/tesh.h
new file mode 100644 (file)
index 0000000..2659fb7
--- /dev/null
@@ -0,0 +1,26 @@
+/* $Id$ */
+
+/* TESH (Test Shell) -- mini shell specialized in running test units        */
+
+/* Copyright (c) 2007 Martin Quinson.                                       */
+/* All rights reserved.                                                     */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#ifndef TESH_H
+#define TESH_H
+
+/*** Buffers ***/
+/***************/
+#include "buff.h"
+
+/*** What we need to know about signals ***/
+/******************************************/
+/* return the name of a signal, aliasing SIGBUS to SIGSEGV since
+   segfault leads to any of them depending on the system */
+const char* signal_name(unsigned int got, char *expected);
+
+
+
+#endif /* TESH_H */