1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
/* Copyright (C) 2010 Nils Carlson
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
static int tap_planned = -1;
static int tap_count = 1;
static int tap_passed = 0;
static pthread_t stdout_thread;
static int pipefd[2];
static FILE *pipe_r_file;
static FILE *normal_stdout;
static void *_tap_comment_stdout(void *_unused)
{
char line[4096];
while (fgets(&line[0], 4096, pipe_r_file)) {
if (strncmp(line, "_TAP", 4)) {
fprintf(normal_stdout, "# %s", line);
} else {
fprintf(normal_stdout, "# %s", &line[4]);
}
}
pthread_exit(0);
}
static void tap_comment_stdout(void)
{
int stdout_fileno, new_stdout, result, fd;
if (pipe(pipefd) < 0) {
perror("# Failed to open pipe");
return;
}
pipe_r_file = fdopen(pipefd[0], "r");
if (!pipe_r_file) {
perror("# Couldn't create a FILE from the pipe");
goto close_pipe;
}
/* Set it before we create the reading thread */
setlinebuf(pipe_r_file);
stdout_fileno = fileno(stdout);
if (stdout_fileno < 0) {
perror("# Couldn't get fileno for stdout!?");
goto close_pipe_r_file;
}
new_stdout = dup(stdout_fileno);
if (new_stdout < 0) {
perror("# Couldn't dup stdout");
goto close_pipe_r_file;
}
normal_stdout = fdopen(new_stdout, "w");
if (!normal_stdout) {
perror("# Could create a FILE from new_stdout");
goto close_dup_stdout;
}
result = pthread_create(&stdout_thread, NULL,
_tap_comment_stdout, NULL);
if (result < 0) {
perror("# Couldn't start stdout_thread");
goto close_normal_stdout;
}
fclose(stdout);
fclose(stderr);
fd = dup(pipefd[1]);
if (fd != STDOUT_FILENO) {
fprintf(stderr, "# Failed to open a new stdout!\n");
goto close_normal_stdout;
}
stdout = fdopen(fd, "w");
if (!stdout) {
perror("Couldn't open a new stdout");
goto close_fd;
}
fd = dup(pipefd[1]);
if (fd != STDERR_FILENO) {
fprintf(stderr, "# Failed to open a new stderr!\n");
goto close_fd;
}
stderr = fdopen(fd, "w");
if (!stderr) {
perror("Couldn't open a new stderr");
goto close_fd;
}
setlinebuf(stdout);
setlinebuf(stderr);
return;
close_fd:
close(fd);
close_normal_stdout:
fclose(normal_stdout);
close_dup_stdout:
close(new_stdout);
close_pipe_r_file:
fclose(pipe_r_file);
close_pipe:
close(pipefd[0]);
close(pipefd[1]);
return;
}
void tap_plan(int count)
{
printf("1..%d\n", count);
tap_count = 1;
tap_planned = count;
tap_comment_stdout();
}
int tap_status(void)
{
if (tap_passed == tap_planned) {
return 0;
} else {
return 1;
}
}
void tap_ok(int bool, const char *format, ...)
{
va_list args;
char *ok_string = "_TAPok";
char *not_ok_string = "_TAPnot ok";
char string[4000];
va_start(args, format);
vsprintf(string, format, args);
va_end(args);
printf("%s %d - %s\n", bool ? ok_string : not_ok_string,
tap_count++, string);
if (bool)
tap_passed++;
}
|