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 /* Set it before we create the reading thread */
62 setlinebuf(pipe_r_file
);
64 stdout_fileno
= fileno(stdout
);
65 if (stdout_fileno
< 0) {
66 perror("# Couldn't get fileno for stdout!?");
67 goto close_pipe_r_file
;
70 new_stdout
= dup(stdout_fileno
);
72 perror("# Couldn't dup stdout");
73 goto close_pipe_r_file
;
76 normal_stdout
= fdopen(new_stdout
, "w");
78 perror("# Could create a FILE from new_stdout");
79 goto close_dup_stdout
;
82 result
= pthread_create(&stdout_thread
, NULL
,
83 _tap_comment_stdout
, NULL
);
85 perror("# Couldn't start stdout_thread");
86 goto close_normal_stdout
;
93 if (fd
!= STDOUT_FILENO
) {
94 fprintf(stderr
, "# Failed to open a new stdout!\n");
95 goto close_normal_stdout
;
98 stdout
= fdopen(fd
, "w");
100 perror("Couldn't open a new stdout");
105 if (fd
!= STDERR_FILENO
) {
106 fprintf(stderr
, "# Failed to open a new stderr!\n");
110 stderr
= fdopen(fd
, "w");
112 perror("Couldn't open a new stderr");
126 fclose(normal_stdout
);
141 void tap_plan(int count
)
143 printf("1..%d\n", count
);
148 tap_comment_stdout();
154 if (tap_passed
== tap_planned
) {
161 void tap_ok(int bool, const char *format
, ...)
164 char *ok_string
= "_TAPok";
165 char *not_ok_string
= "_TAPnot ok";
168 va_start(args
, format
);
169 vsprintf(string
, format
, args
);
172 printf("%s %d - %s\n", bool ? ok_string
: not_ok_string
,
173 tap_count
++, string
);
This page took 0.035709 seconds and 4 git commands to generate.