Add new demo files
[lttng-ust.git] / tests / tap.c
1 /* Copyright (C) 2010 Nils Carlson
2 *
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.
7 *
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.
12 *
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
16 */
17 #include <pthread.h>
18 #include <stdarg.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <string.h>
22
23 static int tap_planned = -1;
24 static int tap_count = 1;
25 static int tap_passed = 0;
26
27 static pthread_t stdout_thread;
28 static int pipefd[2];
29 static FILE *pipe_r_file;
30 static FILE *normal_stdout;
31
32 static void *_tap_comment_stdout(void *_unused)
33 {
34 char line[4096];
35
36 while (fgets(&line[0], 4096, pipe_r_file)) {
37 if (strncmp(line, "_TAP", 4)) {
38 fprintf(normal_stdout, "# %s", line);
39 } else {
40 fprintf(normal_stdout, "# %s", &line[4]);
41 }
42 }
43 pthread_exit(0);
44 }
45
46 static void tap_comment_stdout(void)
47 {
48 int stdout_fileno, new_stdout, result, fd;
49
50 if (pipe(pipefd) < 0) {
51 perror("# Failed to open pipe");
52 return;
53 }
54
55 pipe_r_file = fdopen(pipefd[0], "r");
56 if (!pipe_r_file) {
57 perror("# Couldn't create a FILE from the pipe");
58 goto close_pipe;
59 }
60
61 /* Set it before we create the reading thread */
62 setlinebuf(pipe_r_file);
63
64 stdout_fileno = fileno(stdout);
65 if (stdout_fileno < 0) {
66 perror("# Couldn't get fileno for stdout!?");
67 goto close_pipe_r_file;
68 }
69
70 new_stdout = dup(stdout_fileno);
71 if (new_stdout < 0) {
72 perror("# Couldn't dup stdout");
73 goto close_pipe_r_file;
74 }
75
76 normal_stdout = fdopen(new_stdout, "w");
77 if (!normal_stdout) {
78 perror("# Could create a FILE from new_stdout");
79 goto close_dup_stdout;
80 }
81
82 result = pthread_create(&stdout_thread, NULL,
83 _tap_comment_stdout, NULL);
84 if (result < 0) {
85 perror("# Couldn't start stdout_thread");
86 goto close_normal_stdout;
87 }
88
89 fclose(stdout);
90 fclose(stderr);
91
92 fd = dup(pipefd[1]);
93 if (fd != STDOUT_FILENO) {
94 fprintf(stderr, "# Failed to open a new stdout!\n");
95 goto close_normal_stdout;
96 }
97
98 stdout = fdopen(fd, "w");
99 if (!stdout) {
100 perror("Couldn't open a new stdout");
101 goto close_fd;
102 }
103
104 fd = dup(pipefd[1]);
105 if (fd != STDERR_FILENO) {
106 fprintf(stderr, "# Failed to open a new stderr!\n");
107 goto close_fd;
108 }
109
110 stderr = fdopen(fd, "w");
111 if (!stderr) {
112 perror("Couldn't open a new stderr");
113 goto close_fd;
114 }
115
116 setlinebuf(stdout);
117 setlinebuf(stderr);
118
119
120 return;
121
122 close_fd:
123 close(fd);
124
125 close_normal_stdout:
126 fclose(normal_stdout);
127
128 close_dup_stdout:
129 close(new_stdout);
130
131 close_pipe_r_file:
132 fclose(pipe_r_file);
133
134 close_pipe:
135 close(pipefd[0]);
136 close(pipefd[1]);
137
138 return;
139 }
140
141 void tap_plan(int count)
142 {
143 printf("1..%d\n", count);
144
145 tap_count = 1;
146 tap_planned = count;
147
148 tap_comment_stdout();
149
150 }
151
152 int tap_status(void)
153 {
154 if (tap_passed == tap_planned) {
155 return 0;
156 } else {
157 return 1;
158 }
159 }
160
161 void tap_ok(int bool, const char *format, ...)
162 {
163 va_list args;
164 char *ok_string = "_TAPok";
165 char *not_ok_string = "_TAPnot ok";
166 char string[4000];
167
168 va_start(args, format);
169 vsprintf(string, format, args);
170 va_end(args);
171
172 printf("%s %d - %s\n", bool ? ok_string : not_ok_string,
173 tap_count++, string);
174
175 if (bool)
176 tap_passed++;
177 }
This page took 0.037894 seconds and 4 git commands to generate.