Blame


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