/* Common parts for printing diff output */ /* * Copyright (c) 2020 Neels Hofmeyr * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include "diff_internal.h" static int get_atom_byte(int *ch, struct diff_atom *atom, off_t off) { off_t cur; if (atom->at != NULL) { *ch = atom->at[off]; return 0; } cur = ftello(atom->root->f); if (cur == -1) return errno; if (cur != atom->pos + off && fseeko(atom->root->f, atom->pos + off, SEEK_SET) == -1) return errno; *ch = fgetc(atom->root->f); if (*ch == EOF && ferror(atom->root->f)) return errno; return 0; } int diff_output_lines(struct diff_output_info *outinfo, FILE *dest, const char *prefix, struct diff_atom *start_atom, unsigned int count) { struct diff_atom *atom; off_t outoff = 0, *offp; int rc; if (outinfo && outinfo->line_offsets.len > 0) { unsigned int idx = outinfo->line_offsets.len - 1; outoff = outinfo->line_offsets.head[idx]; } foreach_diff_atom(atom, start_atom, count) { off_t outlen = 0; int i, ch; unsigned int len = atom->len; rc = fprintf(dest, "%s", prefix); if (rc < 0) return errno; outlen += rc; if (len) { rc = get_atom_byte(&ch, atom, len - 1); if (rc) return rc; if (ch == '\n') len--; if (len) { rc = get_atom_byte(&ch, atom, len - 1); if (rc) return rc; if (ch == '\r') len--; } } for (i = 0; i < len; i++) { rc = get_atom_byte(&ch, atom, i); if (rc) return rc; if ((ch < 0x20 || ch >= 0x7f) && ch != '\t') rc = fprintf(dest, "\\x%02x", (unsigned char)ch); else rc = fprintf(dest, "%c", ch); if (rc < 0) return errno; outlen += rc; } rc = fprintf(dest, "\n"); if (rc < 0) return errno; outlen += rc; if (outinfo) { ARRAYLIST_ADD(offp, outinfo->line_offsets); if (offp == NULL) return ENOMEM; outoff += outlen; *offp = outoff; } } return DIFF_RC_OK; } int diff_output_chunk_left_version(struct diff_output_info **output_info, FILE *dest, const struct diff_input_info *info, const struct diff_result *result, const struct diff_chunk_context *cc) { int rc, c_idx; struct diff_output_info *outinfo = NULL; if (diff_range_empty(&cc->left)) return DIFF_RC_OK; if (output_info) { *output_info = diff_output_info_alloc(); if (*output_info == NULL) return ENOMEM; outinfo = *output_info; } /* Write out all chunks on the left side. */ for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) { const struct diff_chunk *c = &result->chunks.head[c_idx]; if (c->left_count) { rc = diff_output_lines(outinfo, dest, "", c->left_start, c->left_count); if (rc) return rc; } } return DIFF_RC_OK; } int diff_output_chunk_right_version(struct diff_output_info **output_info, FILE *dest, const struct diff_input_info *info, const struct diff_result *result, const struct diff_chunk_context *cc) { int rc, c_idx; struct diff_output_info *outinfo = NULL; if (diff_range_empty(&cc->right)) return DIFF_RC_OK; if (output_info) { *output_info = diff_output_info_alloc(); if (*output_info == NULL) return ENOMEM; outinfo = *output_info; } /* Write out all chunks on the right side. */ for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) { const struct diff_chunk *c = &result->chunks.head[c_idx]; if (c->right_count) { rc = diff_output_lines(outinfo, dest, "", c->right_start, c->right_count); if (rc) return rc; } } return DIFF_RC_OK; } int diff_output_trailing_newline_msg(struct diff_output_info *outinfo, FILE *dest, const struct diff_chunk *c) { enum diff_chunk_type chunk_type = diff_chunk_type(c); struct diff_atom *atom, *start_atom; unsigned int atom_count; int rc, ch; off_t outoff = 0, *offp; if (chunk_type == CHUNK_MINUS || chunk_type == CHUNK_SAME) { start_atom = c->left_start; atom_count = c->left_count; } else if (chunk_type == CHUNK_PLUS) { start_atom = c->right_start; atom_count = c->right_count; } else return EINVAL; /* Locate the last atom. */ if (atom_count == 0) return EINVAL; atom = &start_atom[atom_count - 1]; rc = get_atom_byte(&ch, atom, atom->len - 1); if (rc != DIFF_RC_OK) return rc; if (ch != '\n') { if (outinfo && outinfo->line_offsets.len > 0) { unsigned int idx = outinfo->line_offsets.len - 1; outoff = outinfo->line_offsets.head[idx]; } rc = fprintf(dest, "\\ No newline at end of file\n"); if (rc < 0) return errno; if (outinfo) { ARRAYLIST_ADD(offp, outinfo->line_offsets); if (offp == NULL) return ENOMEM; outoff += rc; *offp = outoff; } } return DIFF_RC_OK; } struct diff_output_info * diff_output_info_alloc(void) { struct diff_output_info *output_info; off_t *offp; output_info = malloc(sizeof(*output_info)); if (output_info != NULL) { ARRAYLIST_INIT(output_info->line_offsets, 128); ARRAYLIST_ADD(offp, output_info->line_offsets); if (offp == NULL) { diff_output_info_free(output_info); return NULL; } *offp = 0; } return output_info; } void diff_output_info_free(struct diff_output_info *output_info) { ARRAYLIST_FREE(output_info->line_offsets); free(output_info); }