Skip to content
This repository was archived by the owner on Jun 14, 2019. It is now read-only.

Commit 5a0eae4

Browse files
author
Aaron Leung
committed
Storing the error status and message into the interface struct.
1 parent f5dc2c3 commit 5a0eae4

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

exceptional.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ div[hux ~= "hello"] {
2121
foo: boo;
2222
@include moogoo;
2323
dux: mux;
24-
@import "foogoo";
24+
@import bugaboo "foogoo"
25+
blah: blah;
2526
}

sass_interface.cpp

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <iostream>
2+
#include <sstream>
23
#include <string>
34
#include <cstdlib>
45
#include <unistd.h>
@@ -53,13 +54,28 @@ extern "C" {
5354
Context cpp_ctx(c_ctx->options.include_paths);
5455
Document doc(0, c_ctx->input_string, cpp_ctx);
5556
c_ctx->output_string = process_document(doc, c_ctx->options.output_style);
57+
c_ctx->error_message = 0;
58+
c_ctx->error_status = 0;
5659
}
5760
catch (Error& e) {
58-
cerr << "ERROR -- " << e.file_name << ", line " << e.line_number << ": " << e.message << endl;
61+
stringstream msg_stream;
62+
msg_stream << "ERROR -- " << e.file_name << ", line " << e.line_number << ": " << e.message << endl;
63+
string msg(msg_stream.str());
64+
char* msg_str = (char*) malloc(msg.size() + 1);
65+
strcpy(msg_str, msg.c_str());
66+
c_ctx->error_status = 1;
5967
c_ctx->output_string = 0;
68+
c_ctx->error_message = msg_str;
6069
}
6170
catch(bad_alloc& ba) {
62-
cerr << "ERROR -- unable to allocate memory: " << ba.what() << endl;
71+
stringstream msg_stream;
72+
msg_stream << "ERROR -- unable to allocate memory: " << ba.what() << endl;
73+
string msg(msg_stream.str());
74+
char* msg_str = (char*) malloc(msg.size() + 1);
75+
strcpy(msg_str, msg.c_str());
76+
c_ctx->error_status = 1;
77+
c_ctx->output_string = 0;
78+
c_ctx->error_message = msg_str;
6379
}
6480
// TO DO: CATCH EVERYTHING ELSE
6581
return 0;
@@ -72,13 +88,28 @@ extern "C" {
7288
Context cpp_ctx(c_ctx->options.include_paths);
7389
Document doc(c_ctx->input_path, 0, cpp_ctx);
7490
c_ctx->output_string = process_document(doc, c_ctx->options.output_style);
91+
c_ctx->error_message = 0;
92+
c_ctx->error_status = 0;
7593
}
7694
catch (Error& e) {
77-
cerr << "ERROR -- " << e.file_name << ", line " << e.line_number << ": " << e.message << endl;
95+
stringstream msg_stream;
96+
msg_stream << "ERROR -- " << e.file_name << ", line " << e.line_number << ": " << e.message << endl;
97+
string msg(msg_stream.str());
98+
char* msg_str = (char*) malloc(msg.size() + 1);
99+
strcpy(msg_str, msg.c_str());
100+
c_ctx->error_status = 1;
78101
c_ctx->output_string = 0;
102+
c_ctx->error_message = msg_str;
79103
}
80104
catch(bad_alloc& ba) {
81-
cerr << "ERROR -- unable to allocate memory: " << ba.what() << endl;
105+
stringstream msg_stream;
106+
msg_stream << "ERROR -- unable to allocate memory: " << ba.what() << endl;
107+
string msg(msg_stream.str());
108+
char* msg_str = (char*) malloc(msg.size() + 1);
109+
strcpy(msg_str, msg.c_str());
110+
c_ctx->error_status = 1;
111+
c_ctx->output_string = 0;
112+
c_ctx->error_message = msg_str;
82113
}
83114
// TO DO: CATCH EVERYTHING ELSE
84115
return 0;

sass_interface.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,24 @@ struct sass_context {
1616
char* input_string;
1717
char* output_string;
1818
struct sass_options options;
19+
int error_status;
20+
char* error_message;
1921
};
2022

2123
struct sass_folder_context {
2224
char* search_path;
2325
char* output_path;
2426
struct sass_options options;
27+
int error_status;
28+
char* error_message;
2529
};
2630

2731
struct sass_file_context {
2832
char* input_path;
2933
char* output_string;
3034
struct sass_options options;
35+
int error_status;
36+
char* error_message;
3137
};
3238

3339
struct sass_context* sass_new_context ();

sassc.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ int main(int argc, char** argv)
3434

3535
sass_compile_file(ctx);
3636

37-
if (ctx->output_string) printf("%s", ctx->output_string);
37+
if (ctx->error_status) {
38+
if (ctx->error_message) printf("%s", ctx->error_message);
39+
else printf("An error occured; no error message available.\n");
40+
}
41+
else if (ctx->output_string) {
42+
printf("%s", ctx->output_string);
43+
}
44+
else {
45+
printf("Unknown internal error.\n");
46+
}
3847

3948
sass_free_file_context(ctx);
4049
return 0;

0 commit comments

Comments
 (0)