Hash :
f3c44083
Author :
Date :
2020-11-14T16:39:35
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
/* Common parts for printing diff output */
/*
* Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
*
* 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 <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arraylist.h>
#include <diff_main.h>
#include <diff_output.h>
#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;
rc = fprintf(dest, "%c", (unsigned char)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;
}
static bool
is_function_prototype(const char *buf)
{
return isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$';
}
#define FUNCTION_CONTEXT_SIZE 55
#define begins_with(s, pre) (strncmp(s, pre, sizeof(pre)-1) == 0)
int
diff_output_match_function_prototype(char **prototype,
const struct diff_result *result,
const struct diff_chunk_context *cc)
{
struct diff_atom *start_atom, *atom;
const struct diff_data *data;
unsigned char buf[FUNCTION_CONTEXT_SIZE];
char *state = NULL;
int rc, i;
*prototype = NULL;
if (result->left->atoms.len > 0 && cc->left.start > 0) {
data = result->left;
start_atom = &data->atoms.head[cc->left.start - 1];
} else if (result->right->atoms.len > 0 && cc->right.start > 0) {
data = result->right;
start_atom = &data->atoms.head[cc->right.start - 1];
} else
return DIFF_RC_OK;
diff_data_foreach_atom_backwards_from(start_atom, atom, data) {
for (i = 0; i < atom->len && i < sizeof(buf) - 1; i++) {
unsigned int ch;
rc = get_atom_byte(&ch, atom, i);
if (rc)
return rc;
if (ch == '\n')
break;
buf[i] = ch;
}
buf[i] = '\0';
if (is_function_prototype(buf)) {
if (begins_with(buf, "private:")) {
if (!state)
state = " (private)";
} else if (begins_with(buf, "protected:")) {
if (!state)
state = " (protected)";
} else if (begins_with(buf, "public:")) {
if (!state)
state = " (public)";
} else {
if (state) /* don't care about truncation */
strlcat(buf, state, sizeof(buf));
*prototype = strdup(buf);
if (*prototype == NULL)
return ENOMEM;
return DIFF_RC_OK;
}
}
}
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);
}