Blob


1 /* Produce ed(1) script output from a diff_result. */
2 /*
3 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdbool.h>
24 #include <arraylist.h>
25 #include <diff_main.h>
26 #include <diff_output.h>
28 #include "diff_internal.h"
30 static int
31 output_edscript_chunk(struct diff_output_info *outinfo,
32 FILE *dest, const struct diff_input_info *info,
33 const struct diff_result *result,
34 struct diff_chunk_context *cc, enum diff_chunk_type chunk_type)
35 {
36 off_t outoff = 0, *offp;
37 int left_start, left_len, right_start, right_len;
38 int rc;
40 left_len = cc->left.end - cc->left.start;
41 if (left_len < 0)
42 return EINVAL;
43 else if (result->left.atoms.len == 0)
44 left_start = 0;
45 else if (left_len == 0 && cc->left.start > 0)
46 left_start = cc->left.start;
47 else
48 left_start = cc->left.start + 1;
50 right_len = cc->right.end - cc->right.start;
51 if (right_len < 0)
52 return EINVAL;
53 else if (result->right.atoms.len == 0)
54 right_start = 0;
55 else if (right_len == 0 && cc->right.start > 0)
56 right_start = cc->right.start;
57 else
58 right_start = cc->right.start + 1;
60 if (chunk_type == CHUNK_MINUS) {
61 if (left_len == 1) {
62 rc = fprintf(dest, "%dd%d", left_start, right_start);
63 } else {
64 rc = fprintf(dest, "%d,%dd%d\n", left_start,
65 cc->left.end, right_start);
66 }
67 } else if (chunk_type == CHUNK_PLUS) {
68 if (right_len == 1) {
69 rc = fprintf(dest, "%da%d\n", left_start, right_start);
70 } else {
71 rc = fprintf(dest, "%da%d,%d\n", left_start,
72 right_start, cc->right.end);
73 }
74 } else
75 return EINVAL;
77 if (rc < 0)
78 return errno;
79 if (outinfo) {
80 ARRAYLIST_ADD(offp, outinfo->line_offsets);
81 if (offp == NULL)
82 return ENOMEM;
83 outoff += rc;
84 *offp = outoff;
85 }
87 return DIFF_RC_OK;
88 }
90 int
91 diff_output_edscript(struct diff_output_info **output_info,
92 FILE *dest, const struct diff_input_info *info,
93 const struct diff_result *result)
94 {
95 struct diff_output_info *outinfo = NULL;
96 int i, rc;
98 if (!result)
99 return EINVAL;
100 if (result->rc != DIFF_RC_OK)
101 return result->rc;
103 if (output_info) {
104 *output_info = diff_output_info_alloc();
105 if (*output_info == NULL)
106 return ENOMEM;
107 outinfo = *output_info;
110 for (i = 0; i < result->chunks.len; i++) {
111 struct diff_chunk *chunk = &result->chunks.head[i];
112 enum diff_chunk_type t = diff_chunk_type(chunk);
113 struct diff_chunk_context cc = {};
115 if (t != CHUNK_MINUS && t != CHUNK_PLUS)
116 continue;
118 diff_chunk_context_get(&cc, result, i, 0);
120 if (diff_range_empty(&cc.chunk))
121 continue;
123 rc = output_edscript_chunk(outinfo, dest, info, result, &cc, t);
124 if (rc != DIFF_RC_OK)
125 return rc;
128 return DIFF_RC_OK;