| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2011-2018 by Hans-Peter Deifel * |
| 3 | * [email protected] * |
| 4 | * * |
| 5 | * 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 * |
| 7 | * the Free Software Foundation; either version 2 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * |
| 18 | * Boston, MA 02110-1301 USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #include "output.h" |
| 22 | |
| 23 | #include <cstdio> |
| 24 | #include <cstring> |
| 25 | #include <cctype> |
| 26 | #include <iostream> |
| 27 | #include <algorithm> |
| 28 | |
| 29 | using namespace std; |
| 30 | |
| 31 | static bool is_valid_color(const char* colorcode) { |
| 32 | return colorcode != nullptr && strcmp(colorcode, "") != 0; |
| 33 | } |
| 34 | |
| 35 | const int color_xindex = std::ios_base::xalloc(); |
| 36 | |
| 37 | struct color { |
| 38 | color(bool use_colors, const char *colorcode) { |
| 39 | if (use_colors && is_valid_color(colorcode)) { |
| 40 | this->colorcode = colorcode; |
| 41 | } else { |
| 42 | this->colorcode = nullptr; |
| 43 | } |
| 44 | } |
| 45 | const char *colorcode; |
| 46 | }; |
| 47 | |
| 48 | std::ostream& operator<<(ostream &out, const color &c) { |
| 49 | if (c.colorcode != nullptr) { |
| 50 | out.iword(color_xindex) = 1; |
| 51 | return out << "\33[" << c.colorcode << "m\33[K"; |
| 52 | } else { |
| 53 | return out; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | |
| 58 | std::ostream& nocolor(std::ostream &out) { |
| 59 | if (out.iword(color_xindex) == 1) { |
| 60 | out << "\33[m\33[K"; |
| 61 | out.iword(color_xindex) = 0; |
| 62 | } |
| 63 | return out; |
| 64 | } |
| 65 | |
| 66 | struct substr { |
| 67 | const std::string &str; |
| 68 | const size_t begin; |
| 69 | const size_t end; |
| 70 | substr(const std::string &str, size_t begin, size_t end) |
| 71 | :str(str), begin(begin), end(end) |
| 72 | {} |
| 73 | }; |
| 74 | |
| 75 | std::ostream& operator<<(ostream &out, const substr &s) { |
| 76 | auto iend = s.str.begin() + s.end; |
| 77 | |
| 78 | for (auto i = s.str.begin() + s.begin; i != iend; i++) { |
| 79 | out << *i; |
| 80 | } |
| 81 | return out; |
| 82 | } |
| 83 | |
| 84 | void print_only_match(const struct context &context, const struct match &match) |
| 85 | { |
| 86 | line_prefix(context, false); |
| 87 | |
| 88 | cout << color(context.out.color, context.out.colors.highlight) |
| 89 | << substr(match.string, match.start, match.end) |
| 90 | << nocolor |
| 91 | << endl; |
| 92 | } |
| 93 | |
| 94 | ostream& err() { |
| 95 | return cerr << "pdfgrep: "; |
| 96 | } |
| 97 | |
| 98 | void print_only_filename(const Outconf& outconf, const std::string& filename) { |
| 99 | cout << color(outconf.color, outconf.colors.filename) << filename << nocolor; |
| 100 | |
| 101 | if (outconf.null_byte_sep) { |
| 102 | cout << '\0'; |
| 103 | } else { |
| 104 | cout << endl; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | std::ostream& line_prefix(const context& ctx, bool in_context) { |
| 109 | const Outconf outconf = ctx.out; |
| 110 | |
| 111 | if (outconf.filename) { |
| 112 | cout << color(outconf.color, outconf.colors.filename) |
| 113 | << ctx.filename << nocolor; |
| 114 | |
| 115 | // Here, --null takes precedence over --match-prefix-separator |
| 116 | // in the sense, that if --null is given, the null byte is |
| 117 | // always printed after the filename instead of the separator. |
| 118 | if (outconf.null_byte_sep) { |
| 119 | cout << '\0'; |
| 120 | } else { |
| 121 | cout << color(outconf.color, outconf.colors.separator) |
| 122 | << (in_context ? "-" : outconf.prefix_sep ) << nocolor; |
| 123 | } |
| 124 | } |
| 125 | if (outconf.pagenum) { |
| 126 | cout << color(outconf.color, outconf.colors.pagenum); |
| 127 | if (outconf.pagenum_type == PagenumType::INDEX) { |
| 128 | cout << ctx.pagenum; |
| 129 | } else { |
| 130 | cout << ctx.page_label; |
| 131 | } |
| 132 | cout << nocolor; |
| 133 | |
| 134 | cout << color(outconf.color, outconf.colors.separator) |
| 135 | << (in_context ? "-" : outconf.prefix_sep) |
| 136 | << nocolor; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | return cout; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | // Invariant: matches can't be empty |
| 145 | void print_matches(const context& context, const std::vector<match>& matches) { |
| 146 | const match& first_match = matches.front(); |
| 147 | const match& last_match = matches.back(); |
| 148 | |
| 149 | auto str = first_match.string; |
| 150 | |
| 151 | auto a = str.rfind('\n', first_match.start); |
| 152 | auto b = str.find('\n', last_match.end); |
| 153 | |
| 154 | // If a == -1, a gets 0 (beginning of string) and if it's a valid index |
| 155 | // to a newline, it now points to the character after that. |
| 156 | a++; |
| 157 | |
| 158 | if (b == string::npos) { |
| 159 | b = str.size(); |
| 160 | } |
| 161 | |
| 162 | line_prefix(context, false); |
| 163 | |
| 164 | int previous_end = a; |
| 165 | for (auto match : matches) { |
| 166 | // This can happen if the first match is empty (empty pattern) |
| 167 | // and first_match.start is on a newline character. |
| 168 | if (previous_end <= match.start) { |
| 169 | cout << substr(str, previous_end, match.start); |
| 170 | } |
| 171 | |
| 172 | cout << color(context.out.color, context.out.colors.highlight) |
| 173 | << substr(str, match.start, match.end) |
| 174 | << nocolor; |
| 175 | |
| 176 | previous_end = match.end; |
| 177 | } |
| 178 | |
| 179 | cout << substr(str, previous_end, b) << endl; |
| 180 | } |
| 181 | |
| 182 | void print_context_before(const context& context, const match& match, int lines) { |
| 183 | if (!context.out.context_mode) { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | if (lines < 0) { |
| 188 | lines = context.out.context_before; |
| 189 | } |
| 190 | |
| 191 | auto str = match.string; |
| 192 | auto line_begin = str.rfind('\n', match.start); |
| 193 | |
| 194 | // we are at the first line |
| 195 | if (line_begin == string::npos) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | vector<string> lines_to_output; |
| 200 | |
| 201 | auto pos = line_begin; |
| 202 | while (lines --> 0) { |
| 203 | if (pos == 0) { |
| 204 | lines_to_output.emplace_back(""); |
| 205 | break; |
| 206 | } |
| 207 | auto newpos = str.rfind('\n', pos-1); |
| 208 | |
| 209 | auto start_pos = newpos == string::npos ? 0 : newpos + 1; |
| 210 | lines_to_output.push_back(str.substr(start_pos, pos-start_pos)); |
| 211 | |
| 212 | if (newpos == string::npos) { |
| 213 | break; |
| 214 | } |
| 215 | |
| 216 | pos = newpos; |
| 217 | } |
| 218 | |
| 219 | for (auto l = lines_to_output.rbegin(); l != lines_to_output.rend(); ++l) { |
| 220 | line_prefix(context, true) << *l << endl; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void print_context_after(const context& context, const match& match, int lines) { |
| 225 | if (!context.out.context_mode) { |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | if (lines < 0) { |
| 230 | lines = context.out.context_after; |
| 231 | } |
| 232 | |
| 233 | auto str = match.string; |
| 234 | auto line_end = str.find('\n', match.end); |
| 235 | |
| 236 | // we are at the first line |
| 237 | if (line_end == string::npos) { |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | auto pos = line_end; |
| 242 | while (lines --> 0) { |
| 243 | if (pos == str.size()-1) { |
| 244 | break; |
| 245 | } |
| 246 | auto newpos = str.find('\n', pos+1); |
| 247 | |
| 248 | auto end_pos = newpos == string::npos ? str.size() : newpos; |
| 249 | line_prefix(context, true) << str.substr(pos+1, end_pos-pos-1) << endl; |
| 250 | |
| 251 | if (newpos == string::npos) { |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | pos = newpos; |
| 256 | } |
| 257 | |
| 258 | } |
| 259 | |
| 260 | void print_context_between(const context& context, const match& match1, const match& match2) { |
| 261 | if (!context.out.context_mode) { |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | auto str = match1.string; |
| 266 | |
| 267 | auto pos_right = str.find('\n', match1.end); |
| 268 | auto pos_left = str.rfind('\n', match2.start); |
| 269 | |
| 270 | // count the lines that we have to the right of match1 |
| 271 | int lines_right = 0; |
| 272 | while (pos_right != string::npos && lines_right < context.out.context_after |
| 273 | && pos_right < pos_left) { |
| 274 | lines_right++; |
| 275 | pos_right = str.find('\n', pos_right+1); |
| 276 | } |
| 277 | |
| 278 | // count the lines that we have to the left of match2 |
| 279 | int lines_left = 0; |
| 280 | while (pos_left != string::npos && lines_left < context.out.context_before |
| 281 | && pos_left > pos_right) { |
| 282 | lines_left++; |
| 283 | if (pos_left == 0) { |
| 284 | pos_left = string::npos; |
| 285 | break; |
| 286 | } |
| 287 | pos_left = str.rfind('\n', pos_left-1); |
| 288 | } |
| 289 | |
| 290 | print_context_after(context, match1, lines_right); |
| 291 | if (pos_left > pos_right) { |
| 292 | print_context_separator(context.out); |
| 293 | } |
| 294 | print_context_before(context, match2, lines_left); |
| 295 | } |
| 296 | |
| 297 | void print_context_separator(const Outconf &out) { |
| 298 | // TODO Add color here |
| 299 | |
| 300 | if (out.context_mode) { |
| 301 | cout << "--" << endl; |
| 302 | } |
| 303 | } |