Imported Upstream version 1.13
[platform/upstream/libgpg-error.git] / tests / t-common.h
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/gpg-error.h"
23
24 #ifndef PGM
25 # error Macro PGM not defined.
26 #endif
27
28
29 static int verbose;
30 static int debug;
31 static int errorcount;
32
33
34 static void
35 die (const char *format, ...)
36 {
37   va_list arg_ptr ;
38
39   fflush (stdout);
40 #ifdef HAVE_FLOCKFILE
41   flockfile (stderr);
42 #endif
43   fprintf (stderr, "%s: ", PGM);
44   va_start (arg_ptr, format) ;
45   vfprintf (stderr, format, arg_ptr);
46   va_end (arg_ptr);
47   if (*format && format[strlen(format)-1] != '\n')
48     putc ('\n', stderr);
49 #ifdef HAVE_FLOCKFILE
50   funlockfile (stderr);
51 #endif
52   exit (1);
53 }
54
55
56 static void
57 fail (const char *format, ...)
58 {
59   va_list arg_ptr;
60
61   fflush (stdout);
62 #ifdef HAVE_FLOCKFILE
63   flockfile (stderr);
64 #endif
65   fprintf (stderr, "%s: ", PGM);
66   va_start (arg_ptr, format);
67   vfprintf (stderr, format, arg_ptr);
68   va_end (arg_ptr);
69   if (*format && format[strlen(format)-1] != '\n')
70     putc ('\n', stderr);
71 #ifdef HAVE_FLOCKFILE
72   funlockfile (stderr);
73 #endif
74   errorcount++;
75   if (errorcount >= 50)
76     die ("stopped after 50 errors.");
77 }
78
79
80 static void
81 show (const char *format, ...)
82 {
83   va_list arg_ptr;
84
85   if (!verbose)
86     return;
87 #ifdef HAVE_FLOCKFILE
88   flockfile (stderr);
89 #endif
90   fprintf (stderr, "%s: ", PGM);
91   va_start (arg_ptr, format);
92   vfprintf (stderr, format, arg_ptr);
93   if (*format && format[strlen(format)-1] != '\n')
94     putc ('\n', stderr);
95   va_end (arg_ptr);
96 #ifdef HAVE_FLOCKFILE
97   funlockfile (stderr);
98 #endif
99 }