Line data Source code
1 : /* t-common.h - Common code for the tests.
2 : * Copyright (C) 2013 g10 Code GmbH
3 : *
4 : * This file is part of libgpg-error.
5 : *
6 : * libgpg-error is free software; you can redistribute it and/or
7 : * modify it under the terms of the GNU Lesser General Public License
8 : * as published by the Free Software Foundation; either version 2.1 of
9 : * the License, or (at your option) any later version.
10 : *
11 : * libgpg-error is distributed in the hope that it will be useful, but
12 : * WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : * Lesser General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU Lesser General Public
17 : * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include <stdarg.h>
21 :
22 : #include "../src/gcrypt.h"
23 :
24 : #ifndef PGMNAME
25 : # error Macro PGMNAME not defined.
26 : #endif
27 : #ifndef _GCRYPT_CONFIG_H_INCLUDED
28 : # error config.h not included
29 : #endif
30 :
31 : /* A couple of useful macros. */
32 : #ifndef DIM
33 : # define DIM(v) (sizeof(v)/sizeof((v)[0]))
34 : #endif
35 : #define my_isascii(c) (!((c) & 0x80))
36 : #define digitp(p) (*(p) >= '0' && *(p) <= '9')
37 : #define hexdigitp(a) (digitp (a) \
38 : || (*(a) >= 'A' && *(a) <= 'F') \
39 : || (*(a) >= 'a' && *(a) <= 'f'))
40 : #define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
41 : *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
42 : #define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
43 : #define xmalloc(a) gcry_xmalloc ((a))
44 : #define xcalloc(a,b) gcry_xcalloc ((a),(b))
45 : #define xstrdup(a) gcry_xstrdup ((a))
46 : #define xfree(a) gcry_free ((a))
47 : #define pass() do { ; } while (0)
48 :
49 :
50 : /* Standard global variables. */
51 : static int verbose;
52 : static int debug;
53 : static int errorcount;
54 :
55 :
56 : /* Reporting functions. */
57 : static void
58 0 : die (const char *format, ...)
59 : {
60 : va_list arg_ptr ;
61 :
62 0 : fflush (stdout);
63 : #ifdef HAVE_FLOCKFILE
64 0 : flockfile (stderr);
65 : #endif
66 0 : fprintf (stderr, "%s: ", PGMNAME);
67 0 : va_start (arg_ptr, format) ;
68 0 : vfprintf (stderr, format, arg_ptr);
69 0 : va_end (arg_ptr);
70 0 : if (*format && format[strlen(format)-1] != '\n')
71 0 : putc ('\n', stderr);
72 : #ifdef HAVE_FLOCKFILE
73 0 : funlockfile (stderr);
74 : #endif
75 0 : exit (1);
76 : }
77 :
78 :
79 : static void
80 0 : fail (const char *format, ...)
81 : {
82 : va_list arg_ptr;
83 :
84 0 : fflush (stdout);
85 : #ifdef HAVE_FLOCKFILE
86 0 : flockfile (stderr);
87 : #endif
88 0 : fprintf (stderr, "%s: ", PGMNAME);
89 0 : va_start (arg_ptr, format);
90 0 : vfprintf (stderr, format, arg_ptr);
91 0 : va_end (arg_ptr);
92 0 : if (*format && format[strlen(format)-1] != '\n')
93 0 : putc ('\n', stderr);
94 : #ifdef HAVE_FLOCKFILE
95 0 : funlockfile (stderr);
96 : #endif
97 0 : errorcount++;
98 0 : if (errorcount >= 50)
99 0 : die ("stopped after 50 errors.");
100 0 : }
101 :
102 :
103 : static void
104 99 : info (const char *format, ...)
105 : {
106 : va_list arg_ptr;
107 :
108 99 : if (!verbose)
109 198 : return;
110 : #ifdef HAVE_FLOCKFILE
111 0 : flockfile (stderr);
112 : #endif
113 0 : fprintf (stderr, "%s: ", PGMNAME);
114 0 : va_start (arg_ptr, format);
115 0 : vfprintf (stderr, format, arg_ptr);
116 0 : if (*format && format[strlen(format)-1] != '\n')
117 0 : putc ('\n', stderr);
118 0 : va_end (arg_ptr);
119 : #ifdef HAVE_FLOCKFILE
120 0 : funlockfile (stderr);
121 : #endif
122 : }
|