Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / src / writeout.c
blob84f867de7834a4493b6982603860fb005ec0e029
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: writeout.c,v 1.1.1.1 2008-09-23 16:32:06 hoffman Exp $
22 ***************************************************************************/
24 #include "setup.h"
26 #include <stdio.h>
27 #include <string.h>
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #ifdef HAVE_SYS_SELECT_H
33 #include <sys/select.h>
34 #endif
36 #include <curl/curl.h>
38 #define _MPRINTF_REPLACE /* we want curl-functions instead of native ones */
39 #include <curl/mprintf.h>
41 #include "writeout.h"
43 typedef enum {
44 VAR_NONE, /* must be the first */
45 VAR_TOTAL_TIME,
46 VAR_NAMELOOKUP_TIME,
47 VAR_CONNECT_TIME,
48 VAR_APPCONNECT_TIME,
49 VAR_PRETRANSFER_TIME,
50 VAR_STARTTRANSFER_TIME,
51 VAR_SIZE_DOWNLOAD,
52 VAR_SIZE_UPLOAD,
53 VAR_SPEED_DOWNLOAD,
54 VAR_SPEED_UPLOAD,
55 VAR_HTTP_CODE,
56 VAR_HTTP_CODE_PROXY,
57 VAR_HEADER_SIZE,
58 VAR_REQUEST_SIZE,
59 VAR_EFFECTIVE_URL,
60 VAR_CONTENT_TYPE,
61 VAR_NUM_CONNECTS,
62 VAR_REDIRECT_TIME,
63 VAR_REDIRECT_COUNT,
64 VAR_FTP_ENTRY_PATH,
65 VAR_REDIRECT_URL,
66 VAR_SSL_VERIFY_RESULT,
67 VAR_NUM_OF_VARS /* must be the last */
68 } replaceid;
70 struct variable {
71 const char *name;
72 replaceid id;
76 static const struct variable replacements[]={
77 {"url_effective", VAR_EFFECTIVE_URL},
78 {"http_code", VAR_HTTP_CODE},
79 {"response_code", VAR_HTTP_CODE},
80 {"http_connect", VAR_HTTP_CODE_PROXY},
81 {"time_total", VAR_TOTAL_TIME},
82 {"time_namelookup", VAR_NAMELOOKUP_TIME},
83 {"time_connect", VAR_CONNECT_TIME},
84 {"time_appconnect", VAR_APPCONNECT_TIME},
85 {"time_pretransfer", VAR_PRETRANSFER_TIME},
86 {"time_starttransfer", VAR_STARTTRANSFER_TIME},
87 {"size_header", VAR_HEADER_SIZE},
88 {"size_request", VAR_REQUEST_SIZE},
89 {"size_download", VAR_SIZE_DOWNLOAD},
90 {"size_upload", VAR_SIZE_UPLOAD},
91 {"speed_download", VAR_SPEED_DOWNLOAD},
92 {"speed_upload", VAR_SPEED_UPLOAD},
93 {"content_type", VAR_CONTENT_TYPE},
94 {"num_connects", VAR_NUM_CONNECTS},
95 {"time_redirect", VAR_REDIRECT_TIME},
96 {"num_redirects", VAR_REDIRECT_COUNT},
97 {"ftp_entry_path", VAR_FTP_ENTRY_PATH},
98 {"redirect_url", VAR_REDIRECT_URL},
99 {"ssl_verify_result", VAR_SSL_VERIFY_RESULT},
100 {NULL, VAR_NONE}
103 void ourWriteOut(CURL *curl, const char *writeinfo)
105 FILE *stream = stdout;
106 const char *ptr=writeinfo;
107 char *stringp;
108 long longinfo;
109 double doubleinfo;
111 while(*ptr) {
112 if('%' == *ptr) {
113 if('%' == ptr[1]) {
114 /* an escaped %-letter */
115 fputc('%', stream);
116 ptr+=2;
118 else {
119 /* this is meant as a variable to output */
120 char *end;
121 char keepit;
122 int i;
123 if(('{' == ptr[1]) && (end=strchr(ptr, '}'))) {
124 bool match = FALSE;
125 ptr+=2; /* pass the % and the { */
126 keepit=*end;
127 *end=0; /* zero terminate */
128 for(i=0; replacements[i].name; i++) {
129 if(curl_strequal(ptr, replacements[i].name)) {
130 match = TRUE;
131 switch(replacements[i].id) {
132 case VAR_EFFECTIVE_URL:
133 if((CURLE_OK ==
134 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &stringp))
135 && stringp)
136 fputs(stringp, stream);
137 break;
138 case VAR_HTTP_CODE:
139 if(CURLE_OK ==
140 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &longinfo))
141 fprintf(stream, "%03ld", longinfo);
142 break;
143 case VAR_HTTP_CODE_PROXY:
144 if(CURLE_OK ==
145 curl_easy_getinfo(curl, CURLINFO_HTTP_CONNECTCODE,
146 &longinfo))
147 fprintf(stream, "%03ld", longinfo);
148 break;
149 case VAR_HEADER_SIZE:
150 if(CURLE_OK ==
151 curl_easy_getinfo(curl, CURLINFO_HEADER_SIZE, &longinfo))
152 fprintf(stream, "%ld", longinfo);
153 break;
154 case VAR_REQUEST_SIZE:
155 if(CURLE_OK ==
156 curl_easy_getinfo(curl, CURLINFO_REQUEST_SIZE, &longinfo))
157 fprintf(stream, "%ld", longinfo);
158 break;
159 case VAR_NUM_CONNECTS:
160 if(CURLE_OK ==
161 curl_easy_getinfo(curl, CURLINFO_NUM_CONNECTS, &longinfo))
162 fprintf(stream, "%ld", longinfo);
163 break;
164 case VAR_REDIRECT_COUNT:
165 if(CURLE_OK ==
166 curl_easy_getinfo(curl, CURLINFO_REDIRECT_COUNT, &longinfo))
167 fprintf(stream, "%ld", longinfo);
168 break;
169 case VAR_REDIRECT_TIME:
170 if(CURLE_OK ==
171 curl_easy_getinfo(curl, CURLINFO_REDIRECT_TIME, &doubleinfo))
172 fprintf(stream, "%.3f", doubleinfo);
173 break;
174 case VAR_TOTAL_TIME:
175 if(CURLE_OK ==
176 curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &doubleinfo))
177 fprintf(stream, "%.3f", doubleinfo);
178 break;
179 case VAR_NAMELOOKUP_TIME:
180 if(CURLE_OK ==
181 curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME,
182 &doubleinfo))
183 fprintf(stream, "%.3f", doubleinfo);
184 break;
185 case VAR_CONNECT_TIME:
186 if(CURLE_OK ==
187 curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &doubleinfo))
188 fprintf(stream, "%.3f", doubleinfo);
189 break;
190 case VAR_APPCONNECT_TIME:
191 if(CURLE_OK ==
192 curl_easy_getinfo(curl, CURLINFO_APPCONNECT_TIME,
193 &doubleinfo))
194 fprintf(stream, "%.3f", doubleinfo);
195 break;
196 case VAR_PRETRANSFER_TIME:
197 if(CURLE_OK ==
198 curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME,
199 &doubleinfo))
200 fprintf(stream, "%.3f", doubleinfo);
201 break;
202 case VAR_STARTTRANSFER_TIME:
203 if(CURLE_OK ==
204 curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME,
205 &doubleinfo))
206 fprintf(stream, "%.3f", doubleinfo);
207 break;
208 case VAR_SIZE_UPLOAD:
209 if(CURLE_OK ==
210 curl_easy_getinfo(curl, CURLINFO_SIZE_UPLOAD, &doubleinfo))
211 fprintf(stream, "%.0f", doubleinfo);
212 break;
213 case VAR_SIZE_DOWNLOAD:
214 if(CURLE_OK ==
215 curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &doubleinfo))
216 fprintf(stream, "%.0f", doubleinfo);
217 break;
218 case VAR_SPEED_DOWNLOAD:
219 if(CURLE_OK ==
220 curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &doubleinfo))
221 fprintf(stream, "%.3f", doubleinfo);
222 break;
223 case VAR_SPEED_UPLOAD:
224 if(CURLE_OK ==
225 curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &doubleinfo))
226 fprintf(stream, "%.3f", doubleinfo);
227 break;
228 case VAR_CONTENT_TYPE:
229 if((CURLE_OK ==
230 curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &stringp))
231 && stringp)
232 fputs(stringp, stream);
233 break;
234 case VAR_FTP_ENTRY_PATH:
235 if((CURLE_OK ==
236 curl_easy_getinfo(curl, CURLINFO_FTP_ENTRY_PATH, &stringp))
237 && stringp)
238 fputs(stringp, stream);
239 break;
240 case VAR_REDIRECT_URL:
241 if((CURLE_OK ==
242 curl_easy_getinfo(curl, CURLINFO_REDIRECT_URL, &stringp))
243 && stringp)
244 fputs(stringp, stream);
245 break;
246 case VAR_SSL_VERIFY_RESULT:
247 if(CURLE_OK ==
248 curl_easy_getinfo(curl, CURLINFO_SSL_VERIFYRESULT, &longinfo))
249 fprintf(stream, "%ld", longinfo);
250 break;
251 default:
252 break;
254 break;
257 if(!match) {
258 fprintf(stderr, "curl: unknown --write-out variable: '%s'\n", ptr);
260 ptr=end+1; /* pass the end */
261 *end = keepit;
263 else {
264 /* illegal syntax, then just output the characters that are used */
265 fputc('%', stream);
266 fputc(ptr[1], stream);
267 ptr+=2;
271 else if('\\' == *ptr) {
272 switch(ptr[1]) {
273 case 'r':
274 fputc('\r', stream);
275 break;
276 case 'n':
277 fputc('\n', stream);
278 break;
279 case 't':
280 fputc('\t', stream);
281 break;
282 default:
283 /* unknown, just output this */
284 fputc(*ptr, stream);
285 fputc(ptr[1], stream);
286 break;
288 ptr+=2;
290 else {
291 fputc(*ptr, stream);
292 ptr++;