chiark / gitweb /
gettimeofday wishlist
[adns.git] / regress / hrecord.c.m4
1 m4_dnl hrecord.c.m4
2 m4_dnl (part of complex test harness, not of the library)
3 m4_dnl - recording routines
4
5 m4_dnl  This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson
6 m4_dnl  
7 m4_dnl  This program is free software; you can redistribute it and/or modify
8 m4_dnl  it under the terms of the GNU General Public License as published by
9 m4_dnl  the Free Software Foundation; either version 2, or (at your option)
10 m4_dnl  any later version.
11 m4_dnl  
12 m4_dnl  This program is distributed in the hope that it will be useful,
13 m4_dnl  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 m4_dnl  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 m4_dnl  GNU General Public License for more details.
16 m4_dnl  
17 m4_dnl  You should have received a copy of the GNU General Public License
18 m4_dnl  along with this program; if not, write to the Free Software Foundation,
19 m4_dnl  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
20
21 m4_include(hmacros.i4)
22
23 #include <assert.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <stdlib.h>
28
29 #include "harness.h"
30
31 static int begin_set;
32 static struct timeval begin;
33 static FILE *Toutputfile;
34
35 /* fixme: record syscall durations, rather than gettimeofday results */
36
37 void Tensureoutputfile(void) {
38   const char *fdstr;
39   int fd;
40
41   Toutputfile= stdout;
42   fdstr= getenv("ADNS_TEST_OUT_FD"); if (!fdstr) return;
43   fd= atoi(fdstr);
44   Toutputfile= fdopen(fd,"a"); if (!Toutputfile) Tfailed("fdopen ADNS_TEST_OUT_FD");
45 }
46
47 void Q_vb(void) {
48   if (!adns__vbuf_append(&vb,"",1)) Tnomem();
49   Tensureoutputfile();
50   if (fprintf(Toutputfile," %s\n",vb.buf) == EOF) Toutputerr();
51   if (fflush(Toutputfile)) Toutputerr();
52 }
53
54 static void R_vb(void) {
55   Q_vb();
56 }
57   
58 m4_define(`hm_syscall', `
59  hm_create_proto_h
60 int H$1(hm_args_massage($3,void)) {
61  int r, e;
62
63  hm_create_hqcall_vars
64  $3
65
66  hm_create_hqcall_init($1)
67  $3
68
69  hm_create_hqcall_args
70  Q$1(hm_args_massage($3));
71
72  hm_create_realcall_args
73  r= $1(hm_args_massage($3));
74  e= errno;
75
76  vb.used= 0;
77  Tvba("$1=");
78  m4_define(`hm_rv_any',`
79   if (r==-1) { Tvberrno(e); goto x_error; }
80   Tvbf("%d",r);')
81  m4_define(`hm_rv_fd',`hm_rv_any($'`1)')
82  m4_define(`hm_rv_succfail',`
83   if (r) { Tvberrno(e); goto x_error; }
84   Tvba("OK");')
85  m4_define(`hm_rv_must',`Tmust("$1","return",!r); Tvba("OK");')
86  m4_define(`hm_rv_len',`
87   if (r==-1) { Tvberrno(e); goto x_error; }
88   Tmust("$1","return",r<=$'`1);
89   Tvba("OK");')
90  $2
91
92  hm_create_nothing
93  m4_define(`hm_arg_fdset_io',`Tvba(" $'`1="); Tvbfdset($'`2,$'`1);')
94  m4_define(`hm_arg_addr_out',`Tvba(" $'`1="); Tvbaddr($'`1,*$'`2);')
95  m4_define(`hm_arg_timeval_out_abs',`
96   if (!begin_set) {
97     Tvbf(" $'`1=%ld.%06ld",$'`1->tv_sec,$'`1->tv_usec);
98     begin= *$'`1;
99     begin_set= 1;
100   } else {
101     struct timeval diff;
102     diff.tv_sec= $'`1->tv_sec - begin.tv_sec;
103     diff.tv_usec= $'`1->tv_usec - begin.tv_usec;
104     if (diff.tv_usec < 0) {
105       diff.tv_sec -= 1;
106       diff.tv_usec += 1000000;
107     }
108     assert(diff.tv_sec >= 0);
109     assert(diff.tv_usec >= 0);
110     Tvbf(" $'`1=+%ld.%06ld",diff.tv_sec,diff.tv_usec);
111   }')
112  $3
113
114  hm_create_nothing
115  m4_define(`hm_arg_bytes_out',`Tvbbytes($'`2,r);')
116  $3
117
118  m4_define(`hm_rv_any',`x_error:')
119  m4_define(`hm_rv_fd',`x_error:')
120  m4_define(`hm_rv_succfail',`x_error:')
121  m4_define(`hm_rv_len',`x_error:')
122  m4_define(`hm_rv_must',`')
123  $2
124
125  R_vb();
126  errno= e;
127  return r;
128 }
129 ')
130
131 m4_include(`hsyscalls.i4')