chiark / gitweb /
adnshost: adh-opts.c: Whitespace adjustments to option table
[adns.git] / regress / hfuzzraw.c.m4
index 034922ada1b7ee6fa0c616d684dc8f433cf41a51..b2e82fd01663c3fd6f700b9242461204fa96d731 100644 (file)
@@ -46,14 +46,14 @@ static vbuf fdtab;
 #define FDF_OPEN     001u
 #define FDF_NONBLOCK 002u
 
-static FILE *Tinputfile;
-static int stdout_enable;
+static FILE *Tinputfile, *traceout;
+static int traceprint;
 
-static void Tflushstdout( void) {
-  if (fflush(stdout)) Toutputerr();
+static void Tflushtrace( void) {
+  if (fflush(traceout)) Toutputerr();
 }
 
-void Tensurerecordfile(void) {
+void Tensuresetup(void) {
   static int done;
 
   if (done) return;
@@ -71,14 +71,21 @@ void Tensurerecordfile(void) {
     if (!adns__vbuf_append(&fdtab,&fdfstd,1)) Tnomem();
   }
 
-  const char *proutstr= getenv("ADNS_TEST_FUZZRAW_STDOUT_ENABLE");
-  if (proutstr) stdout_enable= atoi(proutstr);
+  const char *traceprintstr= getenv("ADNS_TEST_FUZZRAW_TRACEPRINT");
+  if (traceprintstr) {
+    traceprint= atoi(traceprintstr);
+    int tracefd= dup(2);
+    if (tracefd<0) Tfailed("dup for tracefd");
+    traceout= fdopen(tracefd,"w");
+    if (!traceout) Tfailed("fdopen for traceout");
+  }
 }
 
 void Q_vb(void) {
+  if (!traceprint) return; /* hcommon.c.m4 can call Q_vb directly */
   if (!adns__vbuf_append(&vb,"",1)) Tnomem();
-  if (fprintf(stdout," %s\n",vb.buf) == EOF) Toutputerr();
-  Tflushstdout();
+  if (fprintf(traceout," %s\n",vb.buf) == EOF) Toutputerr();
+  Tflushtrace();
 }
 
 static void Pformat(const char *what) {
@@ -87,6 +94,7 @@ static void Pformat(const char *what) {
 }
 
 extern void Tshutdown(void) {
+  if (!Tinputfile) return;
   int c= fgetc(Tinputfile);
   if (c!=EOF) Pformat("unwanted additional syscall reply data");
   if (ferror(Tinputfile)) Tfailed("read test log input (at end)");
@@ -98,9 +106,9 @@ static void Pcheckinput(void) {
 }
 
 static void P_read_dump(const unsigned char *p0, size_t count, ssize_t d) {
-  fputs(" | ",stdout);
+  fputs(" | ",traceout);
   while (count) {
-    fprintf(stdout,"%02x", *p0);
+    fprintf(traceout,"%02x", *p0);
     p0 += d;
     count--;
   }
@@ -111,14 +119,14 @@ static void P_read(void *p, size_t sz, const char *what) {
   ssize_t got = fread(p,1,sz,Tinputfile);
   Pcheckinput();
   assert(got==sz);
-  if (stdout_enable && sz) {
-    fprintf(stdout,"%8lx %8s:",pos,what);
+  if (traceprint>1 && sz) {
+    fprintf(traceout,"%8lx %8s:",pos,what);
     P_read_dump(p, sz, +1);
     if (sz<=16) {
       P_read_dump((const unsigned char *)p+sz-1, sz, -1);
     }
-    fputs(" |\n",stdout);
-    Tflushstdout();
+    fputs(" |\n",traceout);
+    Tflushtrace();
   }
 }
 
@@ -199,7 +207,7 @@ m4_define(`syscall_sync',`
 #ifdef FUZZRAW_SYNC
   hm_fr_syscall_ident($'`1)
   static char sync_got[sizeof(sync_expect)];
-  Psync(sync_expect, sync_got, sizeof(sync_got), "sync lost: exp=$1");
+  Psync(sync_expect, sync_got, sizeof(sync_got), "sync lost: program did $1");
 #endif
 ')
 
@@ -241,9 +249,9 @@ int H$1(hm_args_massage($3,void)) {
  hm_create_hqcall_init($1)
  $3
 
- Tensurerecordfile();
+ Tensuresetup();
 
- if (stdout_enable) {
+ if (traceprint) {
    hm_create_hqcall_args
    Q$1(hm_args_massage($3));
  }
@@ -300,6 +308,10 @@ int H$1(hm_args_massage($3,void)) {
     r= newfd;
  }
  ')
+ m4_define(`hm_rv_wlen',`
+  hm_rv_any
+  if (r>$'`1) Pformat("write return value too large");
+ ')
  $2
 
  hm_create_nothing