1 /**
2  * Copyright (c) 2015, Timothy Stack
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither the name of Timothy Stack nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "config.h"
31 
32 #include "base/string_util.hh"
33 #include "pretty_printer.hh"
34 
append_to(attr_line_t & al)35 void pretty_printer::append_to(attr_line_t &al)
36 {
37     pcre_context_static<30> pc;
38     data_token_t dt;
39 
40     this->pp_scanner->reset();
41     while (this->pp_scanner->tokenize2(pc, dt)) {
42         element el(dt, pc);
43 
44         switch (dt) {
45             case DT_XML_EMPTY_TAG:
46                 if (this->pp_is_xml && this->pp_line_length > 0) {
47                     this->start_new_line();
48                 }
49                 this->pp_values.emplace_back(el);
50                 if (this->pp_is_xml) {
51                     this->start_new_line();
52                 }
53                 continue;
54             case DT_XML_OPEN_TAG:
55                 if (this->pp_is_xml) {
56                     this->start_new_line();
57                     this->write_element(el);
58                     this->descend();
59                 } else {
60                     this->pp_values.emplace_back(el);
61                 }
62                 continue;
63             case DT_XML_CLOSE_TAG:
64                 this->flush_values();
65                 this->ascend();
66                 this->write_element(el);
67                 this->start_new_line();
68                 continue;
69             case DT_LCURLY:
70             case DT_LSQUARE:
71             case DT_LPAREN:
72                 this->flush_values(true);
73                 this->pp_values.emplace_back(el);
74                 this->descend();
75                 continue;
76             case DT_RCURLY:
77             case DT_RSQUARE:
78             case DT_RPAREN:
79                 this->flush_values();
80                 if (this->pp_body_lines.top()) {
81                     this->start_new_line();
82                 }
83                 this->ascend();
84                 this->write_element(el);
85                 continue;
86             case DT_COMMA:
87                 if (this->pp_depth > 0) {
88                     this->flush_values(true);
89                     this->write_element(el);
90                     this->start_new_line();
91                     continue;
92                 }
93                 break;
94             case DT_WHITE:
95                 if (this->pp_values.empty() && this->pp_depth == 0 &&
96                     this->pp_line_length == 0) {
97                     this->pp_leading_indent = el.e_capture.length();
98                     continue;
99                 }
100                 break;
101             default:
102                 break;
103         }
104         this->pp_values.emplace_back(el);
105     }
106     while (this->pp_depth > 0) {
107         this->ascend();
108     }
109     this->flush_values();
110 
111     attr_line_t combined;
112     combined.get_string() = this->pp_stream.str();
113     combined.get_attrs() = this->pp_attrs;
114 
115     if (!al.empty()) {
116         al.append("\n");
117     }
118     al.append(combined);
119 }
120 
write_element(const pretty_printer::element & el)121 void pretty_printer::write_element(const pretty_printer::element &el)
122 {
123     if (this->pp_leading_indent == 0 &&
124         this->pp_line_length == 0 &&
125         el.e_token == DT_WHITE) {
126         if (this->pp_depth == 0) {
127             this->pp_soft_indent += el.e_capture.length();
128         }
129         return;
130     }
131     if (((this->pp_leading_indent == 0) ||
132          (this->pp_line_length <= this->pp_leading_indent)) &&
133         el.e_token == DT_LINE) {
134         this->pp_soft_indent = 0;
135         if (this->pp_line_length > 0) {
136             this->pp_line_length = 0;
137             this->pp_stream << std::endl;
138             this->pp_body_lines.top() += 1;
139         }
140         return;
141     }
142     pcre_input &pi = this->pp_scanner->get_input();
143     if (this->pp_line_length == 0) {
144         this->append_indent();
145     }
146     ssize_t start_size = this->pp_stream.tellp();
147     if (el.e_token == DT_QUOTED_STRING) {
148         auto_mem<char> unquoted_str((char *)malloc(el.e_capture.length() + 1));
149         const char *start = pi.get_substr_start(&el.e_capture);
150         unquote(unquoted_str.in(), start, el.e_capture.length());
151         data_scanner ds(unquoted_str.in());
152         string_attrs_t sa;
153         pretty_printer str_pp(&ds, sa,
154                               this->pp_leading_indent + this->pp_depth * 4);
155         attr_line_t result;
156         str_pp.append_to(result);
157         if (result.get_string().find('\n') != std::string::npos) {
158             switch (start[0]) {
159                 case 'r':
160                 case 'u':
161                     this->pp_stream << start[0];
162                     this->pp_stream << start[1] << start[1];
163                     break;
164                 default:
165                     this->pp_stream << start[0] << start[0];
166                     break;
167             }
168             this->pp_stream
169                 << std::endl
170                 << result.get_string();
171             if (result.empty() || result.get_string().back() != '\n') {
172                 this->pp_stream << std::endl;
173             }
174             this->pp_stream
175                 << start[el.e_capture.length() - 1]
176                 << start[el.e_capture.length() - 1];
177         } else {
178             this->pp_stream << pi.get_substr(&el.e_capture);
179         }
180     } else {
181         this->pp_stream << pi.get_substr(&el.e_capture);
182         int shift_amount = start_size - el.e_capture.c_begin - this->pp_shift_accum;
183         shift_string_attrs(this->pp_attrs, el.e_capture.c_begin, shift_amount);
184         this->pp_shift_accum = start_size - el.e_capture.c_begin;
185     }
186     this->pp_line_length += el.e_capture.length();
187     if (el.e_token == DT_LINE) {
188         this->pp_line_length = 0;
189         this->pp_body_lines.top() += 1;
190     }
191 }
192 
append_indent()193 void pretty_printer::append_indent()
194 {
195     this->pp_stream << std::string(this->pp_leading_indent + this->pp_soft_indent, ' ');
196     this->pp_soft_indent = 0;
197     if (this->pp_stream.tellp() == this->pp_leading_indent) {
198         return;
199     }
200     for (int lpc = 0; lpc < this->pp_depth; lpc++) {
201         this->pp_stream << "    ";
202     }
203 }
204 
flush_values(bool start_on_depth)205 bool pretty_printer::flush_values(bool start_on_depth)
206 {
207     bool retval = false;
208 
209     while (!this->pp_values.empty()) {
210         {
211             element &el = this->pp_values.front();
212             this->write_element(this->pp_values.front());
213             if (start_on_depth &&
214                 (el.e_token == DT_LSQUARE ||
215                  el.e_token == DT_LCURLY)) {
216                 if (this->pp_line_length > 0) {
217                     this->pp_stream << std::endl;
218                 }
219                 this->pp_line_length = 0;
220             }
221         }
222         this->pp_values.pop_front();
223         retval = true;
224     }
225     return retval;
226 }
227 
start_new_line()228 void pretty_printer::start_new_line()
229 {
230     bool has_output;
231 
232     if (this->pp_line_length > 0) {
233         this->pp_stream << std::endl;
234         this->pp_line_length = 0;
235     }
236     has_output = this->flush_values();
237     if (has_output && this->pp_line_length > 0) {
238         this->pp_stream << std::endl;
239     }
240     this->pp_line_length = 0;
241     this->pp_body_lines.top() += 1;
242 }
243 
ascend()244 void pretty_printer::ascend()
245 {
246     if (this->pp_depth > 0) {
247         int lines = this->pp_body_lines.top();
248         this->pp_depth -= 1;
249         this->pp_body_lines.pop();
250         this->pp_body_lines.top() += lines;
251     }
252     else {
253         this->pp_body_lines.top() = 0;
254     }
255 }
256 
descend()257 void pretty_printer::descend()
258 {
259     this->pp_depth += 1;
260     this->pp_body_lines.push(0);
261 }
262