1 /* Copyright (C) 2010 Nils Carlson
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 static int tap_planned
= -1;
24 static int tap_count
= 1;
25 static int tap_passed
= 0;
27 static pthread_t stdout_thread
;
29 static FILE *pipe_r_file
;
30 static FILE *normal_stdout
;
32 static void *_tap_comment_stdout(void *_unused
)
36 while (fgets(&line
[0], 4096, pipe_r_file
)) {
37 if (strncmp(line
, "_TAP", 4)) {
38 fprintf(normal_stdout
, "# %s", line
);
40 fprintf(normal_stdout
, "# %s", &line
[4]);
46 static void tap_comment_stdout(void)
48 int stdout_fileno
, new_stdout
, result
, fd
;
50 if (pipe(pipefd
) < 0) {
51 perror("# Failed to open pipe");
55 pipe_r_file
= fdopen(pipefd
[0], "r");
57 perror("# Couldn't create a FILE from the pipe");
61 stdout_fileno
= fileno(stdout
);
62 if (stdout_fileno
< 0) {
63 perror("# Couldn't get fileno for stdout!?");
64 goto close_pipe_r_file
;
67 new_stdout
= dup(stdout_fileno
);
69 perror("# Couldn't dup stdout");
70 goto close_pipe_r_file
;
73 normal_stdout
= fdopen(new_stdout
, "w");
75 perror("# Could create a FILE from new_stdout");
76 goto close_dup_stdout
;
79 result
= pthread_create(&stdout_thread
, NULL
,
80 _tap_comment_stdout
, NULL
);
82 perror("# Couldn't start stdout_thread");
83 goto close_normal_stdout
;
90 if (fd
!= STDOUT_FILENO
) {
91 fprintf(stderr
, "# Failed to open a new stdout!\n");
92 goto close_normal_stdout
;
95 stdout
= fdopen(fd
, "w");
97 perror("Couldn't open a new stdout");
102 if (fd
!= STDERR_FILENO
) {
103 fprintf(stderr
, "# Failed to open a new stderr!\n");
107 stderr
= fdopen(fd
, "w");
109 perror("Couldn't open a new stderr");
115 setlinebuf(pipe_r_file
);
123 fclose(normal_stdout
);
138 void tap_plan(int count
)
140 printf("1..%d\n", count
);
145 tap_comment_stdout();
151 if (tap_passed
== tap_planned
) {
158 void tap_ok(int bool, const char *format
, ...)
161 char *ok_string
= "_TAPok";
162 char *not_ok_string
= "_TAPnot ok";
165 va_start(args
, format
);
166 vsprintf(string
, format
, args
);
169 printf("%s %d - %s\n", bool ? ok_string
: not_ok_string
,
170 tap_count
++, string
);
This page took 0.033133 seconds and 4 git commands to generate.