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
22 static int tap_planned
= -1;
23 static int tap_count
= 1;
24 static int tap_passed
= 0;
26 static pthread_t stdout_thread
;
28 static FILE *pipe_r_file
;
29 static FILE *normal_stdout
;
31 static void *_tap_comment_stdout(void *_unused
)
35 while (fgets(&line
[0], 4096, pipe_r_file
)) {
36 if (strncmp(line
, "_TAP", 4)) {
37 fprintf(normal_stdout
, "# %s", line
);
39 fprintf(normal_stdout
, &line
[4]);
45 static void tap_comment_stdout(void)
47 int stdout_fileno
, new_stdout
, result
, fd
;
49 if (pipe(pipefd
) < 0) {
50 perror("# Failed to open pipe");
54 pipe_r_file
= fdopen(pipefd
[0], "r");
56 perror("# Couldn't create a FILE from the pipe");
60 stdout_fileno
= fileno(stdout
);
61 if (stdout_fileno
< 0) {
62 perror("# Couldn't get fileno for stdout!?");
63 goto close_pipe_r_file
;
66 new_stdout
= dup(stdout_fileno
);
68 perror("# Couldn't dup stdout");
69 goto close_pipe_r_file
;
72 normal_stdout
= fdopen(new_stdout
, "w");
74 perror("# Could create a FILE from new_stdout");
75 goto close_dup_stdout
;
78 result
= pthread_create(&stdout_thread
, NULL
,
79 _tap_comment_stdout
, NULL
);
81 perror("# Couldn't start stdout_thread");
82 goto close_normal_stdout
;
89 if (fd
!= STDOUT_FILENO
) {
90 fprintf(stderr
, "# Failed to open a new stdout!\n");
91 goto close_normal_stdout
;
94 stdout
= fdopen(fd
, "w");
96 perror("Couldn't open a new stdout");
101 if (fd
!= STDERR_FILENO
) {
102 fprintf(stderr
, "# Failed to open a new stderr!\n");
106 stderr
= fdopen(fd
, "w");
108 perror("Couldn't open a new stderr");
114 setlinebuf(pipe_r_file
);
122 fclose(normal_stdout
);
137 void tap_plan(int count
)
139 printf("1..%d\n", count
);
144 tap_comment_stdout();
150 if (tap_passed
== tap_planned
) {
157 void tap_ok(int bool, const char *format
, ...)
160 char *ok_string
= "_TAPok";
161 char *not_ok_string
= "_TAPnot ok";
164 va_start(args
, format
);
165 vsprintf(string
, format
, args
);
168 printf("%s %d - %s\n", bool ? ok_string
: not_ok_string
,
169 tap_count
++, string
);
This page took 0.0348 seconds and 4 git commands to generate.