1 |
/* |
/* |
2 |
instrument.c - file and directory instrumentation routines |
instrument.c - file and directory instrumentation routines |
3 |
Copyright (C) 2007 siliconforks.com |
Copyright (C) 2007, 2008, 2009 siliconforks.com |
4 |
|
|
5 |
This program is free software; you can redistribute it and/or modify |
This program is free software; you can redistribute it and/or modify |
6 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
17 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
*/ |
*/ |
19 |
|
|
20 |
|
#include <config.h> |
21 |
|
|
22 |
#include "instrument.h" |
#include "instrument.h" |
23 |
|
|
24 |
#include <assert.h> |
#include <assert.h> |
29 |
#include <sys/stat.h> |
#include <sys/stat.h> |
30 |
#include <sys/types.h> |
#include <sys/types.h> |
31 |
|
|
32 |
|
#include "encoding.h" |
33 |
|
#include "global.h" |
34 |
#include "instrument-js.h" |
#include "instrument-js.h" |
35 |
#include "resource-manager.h" |
#include "resource-manager.h" |
36 |
#include "util.h" |
#include "util.h" |
54 |
return FILE_TYPE_HTML; |
return FILE_TYPE_HTML; |
55 |
} |
} |
56 |
else { |
else { |
57 |
return FILE_TYPE_UNKNOWN; |
return FILE_TYPE_OTHER; |
58 |
} |
} |
59 |
} |
} |
60 |
|
|
|
static void highlight_file(const char * source_file, const char * destination_file, const char * relative_path) { |
|
|
int depth = 0; |
|
|
for (const char * p = relative_path; *p != '\0'; p++) { |
|
|
if (*p == '/' || *p == '\\') { |
|
|
depth++; |
|
|
} |
|
|
} |
|
|
|
|
|
enum FileType file_type = get_file_type(relative_path); |
|
|
const char * suffix = ".jscoverage.html"; |
|
|
char * highlighted_file = xmalloc(strlen(destination_file) + strlen(suffix) + 1); |
|
|
strcpy(highlighted_file, destination_file); |
|
|
strcat(highlighted_file, suffix); |
|
|
|
|
|
FILE * input = xfopen(source_file, "r"); |
|
|
FILE * output = xfopen(highlighted_file, "w"); |
|
|
|
|
|
free(highlighted_file); |
|
|
|
|
|
char * relative_path_to_ancestor = xmalloc(depth * 3 + 1); |
|
|
for (int i = 0; i < depth; i++) { |
|
|
strcpy(relative_path_to_ancestor + i * 3, "../"); |
|
|
} |
|
|
relative_path_to_ancestor[depth * 3] = '\0'; |
|
|
|
|
|
fprintf(output, "<html><head><title>%s</title>\n", relative_path); |
|
|
fprintf(output, "<link rel=\"stylesheet\" type='text/css' href='%sjscoverage.css'>\n", relative_path_to_ancestor); |
|
|
fprintf(output, "<link rel=\"stylesheet\" type='text/css' href='%sjscoverage-sh_nedit.css'>\n", relative_path_to_ancestor); |
|
|
fprintf(output, "<script src=\"%sjscoverage.js\"></script>\n", relative_path_to_ancestor); |
|
|
fprintf(output, "<script src=\"%sjscoverage-sh_main.js\"></script>\n", relative_path_to_ancestor); |
|
|
fprintf(output, "<script src=\"%sjscoverage-sh_javascript.js\"></script>\n", relative_path_to_ancestor); |
|
|
fprintf(output, "<script>\n"); |
|
|
fprintf(output, "var gCurrentFile = \"%s\";\n", relative_path); |
|
|
fprintf(output, "</script>\n"); |
|
|
fprintf(output, "</head><body onload=\"source_load();\">\n"); |
|
|
fprintf(output, "<h1>%s</h1>\n", relative_path); |
|
|
fprintf(output, "<pre id=\"sourceDiv\" class='sh_%s'>", file_type == FILE_TYPE_JS? "javascript": "html"); |
|
|
free(relative_path_to_ancestor); |
|
|
|
|
|
int c; |
|
|
int atLineStart = 1; |
|
|
int line = 1; |
|
|
while ((c = fgetc(input)) != EOF) { |
|
|
if (atLineStart) { |
|
|
atLineStart = 0; |
|
|
} |
|
|
|
|
|
if (c == '<') { |
|
|
fprintf(output, "<"); |
|
|
} |
|
|
else if (c == '>') { |
|
|
fprintf(output, ">"); |
|
|
} |
|
|
else if (c == '&') { |
|
|
fprintf(output, "&"); |
|
|
} |
|
|
else { |
|
|
if (c == '\n') { |
|
|
line++; |
|
|
atLineStart = 1; |
|
|
} |
|
|
fputc(c, output); |
|
|
} |
|
|
} |
|
|
fprintf(output, "</pre></body></html>\n"); |
|
|
|
|
|
fclose(output); |
|
|
fclose(input); |
|
|
|
|
|
suffix = ".jscoverage.js"; |
|
|
char original_file[strlen(destination_file) + strlen(suffix) + 1]; |
|
|
strcpy(original_file, destination_file); |
|
|
strcat(original_file, suffix); |
|
|
copy_file(source_file, original_file); |
|
|
} |
|
|
|
|
61 |
static void check_same_file(const char * file1, const char * file2) { |
static void check_same_file(const char * file1, const char * file2) { |
62 |
if (is_same_file(file1, file2)) { |
if (is_same_file(file1, file2)) { |
63 |
fatal("source and destination are the same"); |
fatal("source and destination are the same"); |
85 |
if (instrumenting) { |
if (instrumenting) { |
86 |
enum FileType file_type = get_file_type(source_file); |
enum FileType file_type = get_file_type(source_file); |
87 |
switch (file_type) { |
switch (file_type) { |
88 |
case FILE_TYPE_UNKNOWN: |
case FILE_TYPE_OTHER: |
89 |
case FILE_TYPE_HTML: |
case FILE_TYPE_HTML: |
90 |
copy_file(source_file, destination_file); |
copy_file(source_file, destination_file); |
91 |
break; |
break; |
92 |
case FILE_TYPE_JS: |
case FILE_TYPE_JS: |
93 |
{ |
{ |
94 |
FILE * input = xfopen(source_file, "r"); |
FILE * input = xfopen(source_file, "rb"); |
95 |
FILE * output = xfopen(destination_file, "w"); |
FILE * output = xfopen(destination_file, "wb"); |
96 |
const char * suffix = ".jscoverage.js"; |
|
97 |
char temporary_file_name[strlen(destination_file) + strlen(suffix) + 1]; |
Stream * input_stream = Stream_new(0); |
98 |
strcpy(temporary_file_name, destination_file); |
Stream * output_stream = Stream_new(0); |
99 |
strcat(temporary_file_name, suffix); |
|
100 |
jscoverage_instrument_js(id, input, output, temporary_file_name); |
Stream_write_file_contents(input_stream, input); |
101 |
|
|
102 |
|
/* |
103 |
|
Check if the source file looks like an instrumented JavaScript file. |
104 |
|
*/ |
105 |
|
if (input_stream->length >= JSCOVERAGE_INSTRUMENTED_HEADER_LENGTH && |
106 |
|
memcmp(input_stream->data, JSCOVERAGE_INSTRUMENTED_HEADER, JSCOVERAGE_INSTRUMENTED_HEADER_LENGTH) == 0) { |
107 |
|
fatal_command_line("file %s in the source directory appears to be already instrumented", id); |
108 |
|
} |
109 |
|
|
110 |
|
size_t num_characters = input_stream->length; |
111 |
|
uint16_t * characters = NULL; |
112 |
|
int result = jscoverage_bytes_to_characters(jscoverage_encoding, input_stream->data, input_stream->length, &characters, &num_characters); |
113 |
|
if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) { |
114 |
|
fatal("encoding %s not supported", jscoverage_encoding); |
115 |
|
} |
116 |
|
else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) { |
117 |
|
fatal("error decoding %s in file %s", jscoverage_encoding, id); |
118 |
|
} |
119 |
|
jscoverage_instrument_js(id, characters, num_characters, output_stream); |
120 |
|
free(characters); |
121 |
|
|
122 |
|
if (fwrite(output_stream->data, 1, output_stream->length, output) != output_stream->length) { |
123 |
|
fatal("cannot write to file: %s", destination_file); |
124 |
|
} |
125 |
|
|
126 |
|
Stream_delete(input_stream); |
127 |
|
Stream_delete(output_stream); |
128 |
|
|
129 |
fclose(input); |
fclose(input); |
130 |
fclose(output); |
fclose(output); |
131 |
} |
} |
|
highlight_file(source_file, destination_file, id); |
|
132 |
break; |
break; |
133 |
} |
} |
134 |
} |
} |
170 |
fatal("not a directory: %s", destination); |
fatal("not a directory: %s", destination); |
171 |
} |
} |
172 |
if (! directory_is_empty(destination)) { |
if (! directory_is_empty(destination)) { |
173 |
char * jscoverage_html = make_path(destination, "jscoverage.html"); |
char * expected_file = NULL; |
174 |
if (stat(jscoverage_html, &buf) == -1) { |
if (jscoverage_mode == JSCOVERAGE_MOZILLA) { |
175 |
|
char * modules_directory = make_path(destination, "modules"); |
176 |
|
expected_file = make_path(modules_directory, "jscoverage.jsm"); |
177 |
|
free(modules_directory); |
178 |
|
} |
179 |
|
else { |
180 |
|
expected_file = make_path(destination, "jscoverage.html"); |
181 |
|
} |
182 |
|
if (stat(expected_file, &buf) == -1) { |
183 |
fatal("refusing to overwrite directory: %s", destination); |
fatal("refusing to overwrite directory: %s", destination); |
184 |
} |
} |
185 |
free(jscoverage_html); |
free(expected_file); |
186 |
} |
} |
187 |
} |
} |
188 |
else if (errno == ENOENT) { |
else if (errno == ENOENT) { |
193 |
} |
} |
194 |
|
|
195 |
/* copy the resources */ |
/* copy the resources */ |
196 |
jscoverage_copy_resources(destination); |
if (jscoverage_mode == JSCOVERAGE_MOZILLA) { |
197 |
|
char * chrome_directory = make_path(destination, "chrome"); |
198 |
|
char * jscoverage_chrome_directory = make_path(chrome_directory, "jscoverage"); |
199 |
|
mkdirs(jscoverage_chrome_directory); |
200 |
|
copy_resource("jscoverage.manifest", chrome_directory); |
201 |
|
copy_resource("jscoverage.html", jscoverage_chrome_directory); |
202 |
|
copy_resource("jscoverage.css", jscoverage_chrome_directory); |
203 |
|
copy_resource("jscoverage.js", jscoverage_chrome_directory); |
204 |
|
copy_resource("jscoverage-throbber.gif", jscoverage_chrome_directory); |
205 |
|
copy_resource("jscoverage-highlight.css", jscoverage_chrome_directory); |
206 |
|
copy_resource("jscoverage.xul", jscoverage_chrome_directory); |
207 |
|
copy_resource("jscoverage-overlay.js", jscoverage_chrome_directory); |
208 |
|
free(jscoverage_chrome_directory); |
209 |
|
free(chrome_directory); |
210 |
|
|
211 |
|
char * modules_directory = make_path(destination, "modules"); |
212 |
|
mkdirs(modules_directory); |
213 |
|
copy_resource("jscoverage.jsm", modules_directory); |
214 |
|
free(modules_directory); |
215 |
|
} |
216 |
|
else { |
217 |
|
jscoverage_copy_resources(destination); |
218 |
|
} |
219 |
|
|
220 |
/* finally: copy the directory */ |
/* finally: copy the directory */ |
221 |
struct DirListEntry * list = make_recursive_dir_list(source); |
struct DirListEntry * list = make_recursive_dir_list(source); |
254 |
free(s); |
free(s); |
255 |
free(d); |
free(d); |
256 |
} |
} |
|
free_dir_list(list); |
|
|
} |
|
257 |
|
|
258 |
void jscoverage_copy_resources(const char * destination_directory) { |
free_dir_list(list); |
|
copy_resource("jscoverage.html", destination_directory); |
|
|
copy_resource("jscoverage.css", destination_directory); |
|
|
copy_resource("jscoverage.js", destination_directory); |
|
|
copy_resource("jscoverage-throbber.gif", destination_directory); |
|
|
copy_resource("jscoverage-sh_main.js", destination_directory); |
|
|
copy_resource("jscoverage-sh_javascript.js", destination_directory); |
|
|
copy_resource("jscoverage-sh_nedit.css", destination_directory); |
|
259 |
} |
} |