update drag-move to v1.5-beta
[nedit-bw.git] / read_from_pipes.patch
blobfbf2e0d0de1458989ee612928e6ef31329ebb8bd
1 Subject: handle reading from pipes
4 ---
6 source/file.c | 199 +++++++++++++++++++++++++++++++++++++++++++------------
7 util/fileUtils.c | 87 ++++++++++++++++++++----
8 2 files changed, 231 insertions(+), 55 deletions(-)
10 diff --quilt old/source/file.c new/source/file.c
11 --- old/source/file.c
12 +++ new/source/file.c
13 @@ -494,29 +494,88 @@ static int doOpen(WindowInfo *window, co
14 return FALSE;
16 #endif
17 - fileLen = statbuf.st_size;
19 - /* Allocate space for the whole contents of the file (unfortunately) */
20 - fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
21 - if (fileString == NULL) {
22 - fclose(fp);
23 - window->filenameSet = FALSE; /* Temp. prevent check for changes. */
24 - DialogF(DF_ERR, window->shell, 1, "Error while opening File",
25 - "File is too large to edit", "OK");
26 - window->filenameSet = TRUE;
27 - return FALSE;
28 - }
30 - /* Read the file into fileString and terminate with a null */
31 - readLen = fread(fileString, sizeof(char), fileLen, fp);
32 - if (ferror(fp)) {
33 - fclose(fp);
34 - window->filenameSet = FALSE; /* Temp. prevent check for changes. */
35 - DialogF(DF_ERR, window->shell, 1, "Error while opening File",
36 - "Error reading %s:\n%s", "OK", name, errorString());
37 - window->filenameSet = TRUE;
38 - free(fileString);
39 - return FALSE;
40 + if (S_ISFIFO(statbuf.st_mode)) {
41 + int pipeBufLen, fileSize;
43 + window->fileMissing = TRUE;
44 + SET_PERM_LOCKED(window->lockReasons, TRUE);
46 + pipeBufLen = statbuf.st_blksize;
47 + if (pipeBufLen <= 0)
48 + pipeBufLen = 4096;
50 + fileSize = pipeBufLen << 2;
51 + fileString = malloc(fileSize + 1);
52 + if (fileString == NULL)
53 + {
54 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
55 + "File is too large to include", "OK");
56 + fclose(fp);
57 + return FALSE;
58 + }
60 + fileLen = 0;
61 + do
62 + {
63 + readLen = fread(&fileString[fileLen], sizeof(char), pipeBufLen, fp);
65 + if (ferror(fp))
66 + {
67 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
68 + "Error reading %s:\n%s", "OK", name, errorString());
69 + fclose(fp);
70 + free(fileString);
71 + return FALSE;
72 + }
74 + fileLen += readLen;
75 + if (fileSize - fileLen < pipeBufLen)
76 + {
77 + char *tmpFileString;
79 + fileSize += pipeBufLen << 2;
80 + tmpFileString = realloc(fileString, fileSize + 1);
81 + if (tmpFileString == NULL)
82 + {
83 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
84 + "File is too large to include", "OK");
85 + fclose(fp);
86 + free(fileString);
87 + return FALSE;
88 + }
89 + fileString = tmpFileString;
90 + }
92 + }
93 + while (!feof(fp));
95 + readLen = fileLen;
96 + } else {
97 + fileLen = statbuf.st_size;
99 + /* Allocate space for the whole contents of the file (unfortunately) */
100 + fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
101 + if (fileString == NULL) {
102 + fclose(fp);
103 + window->filenameSet = FALSE; /* Temp. prevent check for changes. */
104 + DialogF(DF_ERR, window->shell, 1, "Error while opening File",
105 + "File is too large to edit", "OK");
106 + window->filenameSet = TRUE;
107 + return FALSE;
110 + /* Read the file into fileString and terminate with a null */
111 + readLen = fread(fileString, sizeof(char), fileLen, fp);
112 + if (ferror(fp)) {
113 + fclose(fp);
114 + window->filenameSet = FALSE; /* Temp. prevent check for changes. */
115 + DialogF(DF_ERR, window->shell, 1, "Error while opening File",
116 + "Error reading %s:\n%s", "OK", name, errorString());
117 + window->filenameSet = TRUE;
118 + free(fileString);
119 + return FALSE;
122 fileString[readLen] = 0;
124 @@ -634,27 +693,83 @@ int IncludeFile(WindowInfo *window, cons
125 fclose(fp);
126 return FALSE;
128 - fileLen = statbuf.st_size;
130 - /* allocate space for the whole contents of the file */
131 - fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
132 - if (fileString == NULL)
134 - DialogF(DF_ERR, window->shell, 1, "Error opening File",
135 - "File is too large to include", "OK");
136 - fclose(fp);
137 - return FALSE;
140 - /* read the file into fileString and terminate with a null */
141 - readLen = fread(fileString, sizeof(char), fileLen, fp);
142 - if (ferror(fp))
144 - DialogF(DF_ERR, window->shell, 1, "Error opening File",
145 - "Error reading %s:\n%s", "OK", name, errorString());
146 - fclose(fp);
147 - free(fileString);
148 - return FALSE;
149 + if (S_ISFIFO(statbuf.st_mode)) {
150 + int pipeBufLen, fileSize;
152 + pipeBufLen = statbuf.st_blksize;
153 + if (pipeBufLen <= 0)
154 + pipeBufLen = 4096;
156 + fileSize = pipeBufLen << 2;
157 + fileString = malloc(fileSize + 1);
158 + if (fileString == NULL)
160 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
161 + "File is too large to include", "OK");
162 + fclose(fp);
163 + return FALSE;
166 + fileLen = 0;
167 + do
169 + readLen = fread(&fileString[fileLen], sizeof(char), pipeBufLen, fp);
171 + if (ferror(fp))
173 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
174 + "Error reading %s:\n%s", "OK", name, errorString());
175 + fclose(fp);
176 + free(fileString);
177 + return FALSE;
180 + fileLen += readLen;
181 + if (fileSize - fileLen < pipeBufLen)
183 + char *tmpFileString;
185 + fileSize += pipeBufLen << 2;
186 + tmpFileString = realloc(fileString, fileSize + 1);
187 + if (tmpFileString == NULL)
189 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
190 + "File is too large to include", "OK");
191 + fclose(fp);
192 + free(fileString);
193 + return FALSE;
195 + fileString = tmpFileString;
199 + while(!feof(fp));
201 + readLen = fileLen;
202 + } else {
203 + fileLen = statbuf.st_size;
205 + /* allocate space for the whole contents of the file */
206 + fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
207 + if (fileString == NULL)
209 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
210 + "File is too large to include", "OK");
211 + fclose(fp);
212 + return FALSE;
215 + /* read the file into fileString and terminate with a null */
216 + readLen = fread(fileString, sizeof(char), fileLen, fp);
217 + if (ferror(fp))
219 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
220 + "Error reading %s:\n%s", "OK", name, errorString());
221 + fclose(fp);
222 + free(fileString);
223 + return FALSE;
226 fileString[readLen] = 0;
228 diff --quilt old/util/fileUtils.c new/util/fileUtils.c
229 --- old/util/fileUtils.c
230 +++ new/util/fileUtils.c
231 @@ -675,22 +675,82 @@ char *ReadAnyTextFile(const char *fileNa
233 /* Read the whole file into fileString */
234 if ((fp = fopen(fileName, "r")) == NULL) {
235 - return NULL;
236 + return NULL;
238 if (fstat(fileno(fp), &statbuf) != 0) {
239 - fclose(fp);
240 - return NULL;
241 + fclose(fp);
242 + return NULL;
244 - fileLen = statbuf.st_size;
245 - /* +1 = space for null
246 - ** +1 = possible additional \n
247 - */
248 - fileString = XtMalloc(fileLen + 2);
249 - readLen = fread(fileString, sizeof(char), fileLen, fp);
250 - if (ferror(fp)) {
251 - XtFree(fileString);
252 - fclose(fp);
253 - return NULL;
254 + if (S_ISDIR(statbuf.st_mode)) {
255 + fclose(fp);
256 + return NULL;
259 + if (S_ISFIFO(statbuf.st_mode)) {
260 + int pipeBufLen, fileBufSize;
261 + char *fileBuf;
263 + pipeBufLen = statbuf.st_blksize;
264 + if (pipeBufLen <= 0) {
265 + pipeBufLen = 4096;
268 + fileBufSize = pipeBufLen << 2;
269 + fileBuf = malloc(fileBufSize);
270 + if (fileBuf == NULL) {
271 + fclose(fp);
272 + return NULL;
275 + fileLen = 0;
276 + do {
277 + readLen = fread(&fileBuf[fileLen], sizeof(char), pipeBufLen, fp);
279 + if (ferror(fp)) {
280 + fclose(fp);
281 + free(fileBuf);
282 + return NULL;
285 + fileLen += readLen;
286 + if (fileBufSize - fileLen < pipeBufLen) {
287 + char *tmpFileBuf;
289 + fileBufSize += pipeBufLen << 2;
290 + tmpFileBuf = realloc(fileBuf, fileBufSize);
291 + if (tmpFileBuf == NULL) {
292 + fclose(fp);
293 + free(fileBuf);
294 + return NULL;
296 + fileBuf = tmpFileBuf;
299 + } while (!feof(fp));
301 + readLen = fileLen;
302 + /* +1 = space for null
303 + ** +1 = possible additional \n
304 + */
305 + fileString = XtMalloc(readLen + 2);
306 + memcpy(fileString, fileBuf, readLen * sizeof(char));
307 + free(fileBuf);
308 + } else {
309 + fileLen = statbuf.st_size;
310 + /* +1 = space for null
311 + ** +1 = possible additional \n
312 + */
313 + fileString = XtMalloc(fileLen + 2);
314 + if (fileString == NULL) {
315 + fclose(fp);
316 + return NULL;
318 + readLen = fread(fileString, sizeof(char), fileLen, fp);
319 + if (ferror(fp)) {
320 + XtFree(fileString);
321 + fclose(fp);
322 + return NULL;
325 fclose(fp);
326 fileString[readLen] = 0;
327 @@ -712,3 +772,4 @@ char *ReadAnyTextFile(const char *fileNa
329 return fileString;