Fixed error printing code

This commit is contained in:
Bruce Hill 2021-01-18 00:14:01 -08:00
parent b37c7c3985
commit dfdc7bf94a
3 changed files with 10 additions and 6 deletions

6
bp.c
View File

@ -270,7 +270,8 @@ static int inplace_modify_file(def_t *defs, file_t *f, pat_t *pattern)
confirm_t confirm_file = confirm;
for (match_t *m = NULL; (m = next_match(defs, f, m, pattern, ignorecase)); ) {
++matches;
if (print_errors(&pr, m) > 0)
printer_t err_pr = {.file = f, .context_lines = 1, .use_color = 1, .print_line_numbers = 1};
if (print_errors(&err_pr, m) > 0)
exit(1);
// Lazy-open file for writing upon first match:
if (inplace_file == NULL) {
@ -325,7 +326,8 @@ static int print_matches(def_t *defs, file_t *f, pat_t *pattern)
confirm_t confirm_file = confirm;
for (match_t *m = NULL; (m = next_match(defs, f, m, pattern, ignorecase)); ) {
if (print_errors(&pr, m) > 0)
printer_t err_pr = {.file = f, .context_lines = 1, .use_color = 1, .print_line_numbers = 1};
if (print_errors(&err_pr, m) > 0)
exit(1);
if (++matches == 1) {

View File

@ -213,7 +213,7 @@ void fprint_line(FILE *dest, file_t *f, const char *start, const char *end, cons
size_t linenum = get_line_number(f, start);
const char *line = get_line(f, linenum);
size_t charnum = get_char_number(f, start);
fprintf(dest, "\033[1m%s:%ld:\033[0m ", f->filename, linenum);
fprintf(dest, "\033[1m%s:%ld:\033[0m ", f->filename[0] ? f->filename : "stdin", linenum);
va_list args;
va_start(args, fmt);
@ -227,7 +227,7 @@ void fprint_line(FILE *dest, file_t *f, const char *start, const char *end, cons
linenum,
(int)charnum - 1, line,
(int)(end - &line[charnum-1]), &line[charnum-1],
(int)(eol - end), end);
(int)(eol - end - 1), end);
fprintf(dest, " \033[34;1m");
const char *p = line - 1;
for (; p < start; ++p) fputc(' ', dest);

View File

@ -378,9 +378,11 @@ void print_match(FILE *out, printer_t *pr, match_t *m)
int print_errors(printer_t *pr, match_t *m)
{
int ret = 0;
if (m->pat->type == BP_CAPTURE && m->pat->args.capture.name && streq(m->pat->args.capture.name, "!")) {
if (m->pat->type == BP_CAPTURE && m->pat->args.capture.name
&& strncmp(m->pat->args.capture.name, "!", m->pat->args.capture.namelen) == 0) {
printf("\033[31;1m");
print_match(stdout, pr, m);
printer_t tmp = {.file = pr->file}; // No bells and whistles
print_match(stdout, &tmp, m); // Error message
printf("\033[0m\n");
fprint_line(stdout, pr->file, m->start, m->end, " ");
return 1;