AmendHub

Download

jcs

/

amend

/

patch.c

 

(View History)

jcs   patch: Account for newline Latest amendment: 202 on 2026-10-19

1 /*
2 * Copyright (c) 2021 joshua stein <jcs@jcs.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <stdio.h>
18 #include <string.h>
19
20 #include "amend.h"
21 #include "patch.h"
22 #include "util.h"
23
24 static short patch_state;
25 static char patch_err[128];
26
27 enum {
28 PATCH_STATE_HEADER_FROM,
29 PATCH_STATE_TO,
30 PATCH_STATE_CHUNK_HEADER,
31 PATCH_STATE_CONTEXT
32 };
33
34 short patch_open_source_file(struct repo *repo, char *filename);
35 short patch_open_temp_dest_file(struct repo *repo, char *filename);
36
37
38 short
39 patch_open_source_file(struct repo *repo, char *filename)
40 {
41 short error, ret, i;
42 Str32 pfilename;
43
44 for (i = 0; i < repo->nfiles; i++) {
45 if (strcmp(repo->files[i]->filename, filename) != 0)
46 continue;
47
48 memcpy(pfilename, filename, sizeof(pfilename));
49 CtoPstr(pfilename);
50
51 error = FSOpen(pfilename, repo->bile->vrefnum, &ret);
52 if (error && error == 1234) {
53 /* TODO: what does it return if file isn't there? */
54 error = Create(pfilename, repo->bile->vrefnum,
55 repo->files[i]->creator, repo->files[i]->type);
56 if (error && error != dupFNErr)
57 panic("Failed to create %s: %d", filename, error);
58 error = FSOpen(pfilename, repo->bile->vrefnum, &ret);
59 }
60
61 if (error)
62 panic("Failed to open %s: %d", filename, error);
63
64 return ret;
65 }
66
67 return -1;
68 }
69
70 short
71 patch_open_temp_dest_file(struct repo *repo, char *filename)
72 {
73 short error, ret;
74 char tmpfile[32];
75
76 snprintf(tmpfile, sizeof(tmpfile), "%s.tmp", filename);
77 CtoPstr(tmpfile);
78
79 error = Create(tmpfile, repo->bile->vrefnum, AMEND_CREATOR, 'TEXT');
80 if (error && error != dupFNErr)
81 panic("Failed to create %s: %d", PtoCstr(tmpfile), error);
82 error = FSOpen(tmpfile, repo->bile->vrefnum, &ret);
83 if (error)
84 panic("Failed to open %s: %d", PtoCstr(tmpfile), error);
85
86 return ret;
87 }
88
89 short
90 patch_process(struct repo *repo, Str255 filename, short vrefnum)
91 {
92 char tofilename[256] = { 0 };
93 char buf[1024];
94 size_t i;
95 long patch_size;
96 short linenum = 0, error, ret = -1;
97 short linelen, patch_frefnum = -1, source_frefnum = -1,
98 dest_frefnum = -1;
99 char *line;
100
101 error = FSOpen(filename, vrefnum, &patch_frefnum);
102 if (error)
103 panic("Failed to open patch %s: %d", PtoCstr(filename), error);
104
105 error = GetEOF(patch_frefnum, &patch_size);
106 if (error)
107 panic("Failed to get size of patch %s: %d", PtoCstr(filename),
108 error);
109
110 patch_state = PATCH_STATE_HEADER_FROM;
111
112 for (i = 0; i < patch_size; i += linelen + 1) {
113 linelen = FSReadLine(patch_frefnum, buf, sizeof(buf) - 1);
114 if (linelen < 0)
115 break;
116 buf[linelen] = '\0';
117 linenum++;
118 line = buf;
119
120 switch (patch_state) {
121 case PATCH_STATE_HEADER_FROM:
122 if (strncmp(line, "--- ", 4) != 0)
123 break;
124 patch_state = PATCH_STATE_TO;
125 break;
126 case PATCH_STATE_TO:
127 if (strncmp(line, "+++ ", 4) != 0) {
128 snprintf(patch_err, sizeof(patch_err),
129 "Expected '+++ ' on line %d", linenum);
130 ret = -1;
131 goto patch_done;
132 }
133 line += 4;
134 linelen -= 4;
135
136 tofilename[0] = '\0';
137 for (i = 0; i < linelen; i++) {
138 if (line[i] == '\0' || line[i] == '\t') {
139 memcpy(tofilename, line, i);
140 tofilename[i] = '\0';
141 break;
142 }
143 }
144 if (tofilename[0] == '\0') {
145 snprintf(patch_err, sizeof(patch_err),
146 "Failed to parse filename after +++ on line %d",
147 linenum);
148 ret = -1;
149 goto patch_done;
150 }
151
152 source_frefnum = patch_open_source_file(repo, tofilename);
153 if (source_frefnum == -1) {
154 ret = -1;
155 goto patch_done;
156 }
157
158 dest_frefnum = patch_open_temp_dest_file(repo, tofilename);
159 if (dest_frefnum == -1) {
160 ret = -1;
161 goto patch_done;
162 }
163
164 patch_state = PATCH_STATE_CHUNK_HEADER;
165 break;
166 case PATCH_STATE_CHUNK_HEADER: {
167 short source_line, source_delta, dest_line, dest_delta, count;
168
169 if (strncmp(line, "@@ ", 3) != 0) {
170 snprintf(patch_err, sizeof(patch_err),
171 "Expected '@@ ' on line %d", linenum);
172 ret = -1;
173 goto patch_done;
174 }
175 if (sscanf(line, "@@ %d,%d %d,%d @@%n",
176 &source_line, &source_delta, &dest_line, &dest_delta,
177 &count) != 4 || count < 1) {
178 snprintf(patch_err, sizeof(patch_err),
179 "Malformed '@@ ' on line %d", linenum);
180 ret = -1;
181 goto patch_done;
182 }
183 break;
184 }
185 default:
186 panic("Invalid patch state %d", patch_state);
187 }
188 }
189
190 patch_done:
191 if (patch_frefnum > -1)
192 FSClose(patch_frefnum);
193 if (source_frefnum > -1)
194 FSClose(source_frefnum);
195 if (dest_frefnum > -1)
196 FSClose(dest_frefnum);
197
198 return ret;
199 }