disable OM2.3.1 by default, enable with make bertw OM231=1
[nedit-bw.git] / read_from_pipes.patch
blob6c7fedabcb367a24f91b684aa1c29792b656a251
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 @@ -492,33 +492,92 @@ static int doOpen(WindowInfo *window, co
14 "Can't open block device %s", "OK", name);
15 window->filenameSet = TRUE;
16 return FALSE;
18 #endif
19 - fileLen = statbuf.st_size;
21 - /* Allocate space for the whole contents of the file (unfortunately) */
22 - fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
23 - if (fileString == NULL) {
24 - fclose(fp);
25 - window->filenameSet = FALSE; /* Temp. prevent check for changes. */
26 - DialogF(DF_ERR, window->shell, 1, "Error while opening File",
27 - "File is too large to edit", "OK");
28 - window->filenameSet = TRUE;
29 - return FALSE;
30 - }
32 - /* Read the file into fileString and terminate with a null */
33 - readLen = fread(fileString, sizeof(char), fileLen, fp);
34 - if (ferror(fp)) {
35 - fclose(fp);
36 - window->filenameSet = FALSE; /* Temp. prevent check for changes. */
37 - DialogF(DF_ERR, window->shell, 1, "Error while opening File",
38 - "Error reading %s:\n%s", "OK", name, errorString());
39 - window->filenameSet = TRUE;
40 - free(fileString);
41 - return FALSE;
42 + if (S_ISFIFO(statbuf.st_mode)) {
43 + int pipeBufLen, fileSize;
45 + window->fileMissing = TRUE;
46 + SET_PERM_LOCKED(window->lockReasons, TRUE);
48 + pipeBufLen = statbuf.st_blksize;
49 + if (pipeBufLen <= 0)
50 + pipeBufLen = 4096;
52 + fileSize = pipeBufLen << 2;
53 + fileString = malloc(fileSize + 1);
54 + if (fileString == NULL)
55 + {
56 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
57 + "File is too large to include", "OK");
58 + fclose(fp);
59 + return FALSE;
60 + }
62 + fileLen = 0;
63 + do
64 + {
65 + readLen = fread(&fileString[fileLen], sizeof(char), pipeBufLen, fp);
67 + if (ferror(fp))
68 + {
69 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
70 + "Error reading %s:\n%s", "OK", name, errorString());
71 + fclose(fp);
72 + free(fileString);
73 + return FALSE;
74 + }
76 + fileLen += readLen;
77 + if (fileSize - fileLen < pipeBufLen)
78 + {
79 + char *tmpFileString;
81 + fileSize += pipeBufLen << 2;
82 + tmpFileString = realloc(fileString, fileSize + 1);
83 + if (tmpFileString == NULL)
84 + {
85 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
86 + "File is too large to include", "OK");
87 + fclose(fp);
88 + free(fileString);
89 + return FALSE;
90 + }
91 + fileString = tmpFileString;
92 + }
94 + }
95 + while (!feof(fp));
97 + readLen = fileLen;
98 + } else {
99 + fileLen = statbuf.st_size;
101 + /* Allocate space for the whole contents of the file (unfortunately) */
102 + fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
103 + if (fileString == NULL) {
104 + fclose(fp);
105 + window->filenameSet = FALSE; /* Temp. prevent check for changes. */
106 + DialogF(DF_ERR, window->shell, 1, "Error while opening File",
107 + "File is too large to edit", "OK");
108 + window->filenameSet = TRUE;
109 + return FALSE;
112 + /* Read the file into fileString and terminate with a null */
113 + readLen = fread(fileString, sizeof(char), fileLen, fp);
114 + if (ferror(fp)) {
115 + fclose(fp);
116 + window->filenameSet = FALSE; /* Temp. prevent check for changes. */
117 + DialogF(DF_ERR, window->shell, 1, "Error while opening File",
118 + "Error reading %s:\n%s", "OK", name, errorString());
119 + window->filenameSet = TRUE;
120 + free(fileString);
121 + return FALSE;
124 fileString[readLen] = 0;
126 /* Close the file */
127 if (fclose(fp) != 0) {
128 @@ -632,31 +691,87 @@ int IncludeFile(WindowInfo *window, cons
129 DialogF(DF_ERR, window->shell, 1, "Error opening File",
130 "Can't open directory %s", "OK", name);
131 fclose(fp);
132 return FALSE;
134 - fileLen = statbuf.st_size;
136 - /* allocate space for the whole contents of the file */
137 - fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
138 - if (fileString == NULL)
140 - DialogF(DF_ERR, window->shell, 1, "Error opening File",
141 - "File is too large to include", "OK");
142 - fclose(fp);
143 - return FALSE;
146 - /* read the file into fileString and terminate with a null */
147 - readLen = fread(fileString, sizeof(char), fileLen, fp);
148 - if (ferror(fp))
150 - DialogF(DF_ERR, window->shell, 1, "Error opening File",
151 - "Error reading %s:\n%s", "OK", name, errorString());
152 - fclose(fp);
153 - free(fileString);
154 - return FALSE;
155 + if (S_ISFIFO(statbuf.st_mode)) {
156 + int pipeBufLen, fileSize;
158 + pipeBufLen = statbuf.st_blksize;
159 + if (pipeBufLen <= 0)
160 + pipeBufLen = 4096;
162 + fileSize = pipeBufLen << 2;
163 + fileString = malloc(fileSize + 1);
164 + if (fileString == NULL)
166 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
167 + "File is too large to include", "OK");
168 + fclose(fp);
169 + return FALSE;
172 + fileLen = 0;
173 + do
175 + readLen = fread(&fileString[fileLen], sizeof(char), pipeBufLen, fp);
177 + if (ferror(fp))
179 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
180 + "Error reading %s:\n%s", "OK", name, errorString());
181 + fclose(fp);
182 + free(fileString);
183 + return FALSE;
186 + fileLen += readLen;
187 + if (fileSize - fileLen < pipeBufLen)
189 + char *tmpFileString;
191 + fileSize += pipeBufLen << 2;
192 + tmpFileString = realloc(fileString, fileSize + 1);
193 + if (tmpFileString == NULL)
195 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
196 + "File is too large to include", "OK");
197 + fclose(fp);
198 + free(fileString);
199 + return FALSE;
201 + fileString = tmpFileString;
205 + while(!feof(fp));
207 + readLen = fileLen;
208 + } else {
209 + fileLen = statbuf.st_size;
211 + /* allocate space for the whole contents of the file */
212 + fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
213 + if (fileString == NULL)
215 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
216 + "File is too large to include", "OK");
217 + fclose(fp);
218 + return FALSE;
221 + /* read the file into fileString and terminate with a null */
222 + readLen = fread(fileString, sizeof(char), fileLen, fp);
223 + if (ferror(fp))
225 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
226 + "Error reading %s:\n%s", "OK", name, errorString());
227 + fclose(fp);
228 + free(fileString);
229 + return FALSE;
232 fileString[readLen] = 0;
234 /* Detect and convert DOS and Macintosh format files */
235 switch (FormatOfFile(fileString)) {
236 diff --quilt old/util/fileUtils.c new/util/fileUtils.c
237 --- old/util/fileUtils.c
238 +++ new/util/fileUtils.c
239 @@ -673,26 +673,86 @@ char *ReadAnyTextFile(const char *fileNa
240 char *fileString;
241 int format;
243 /* Read the whole file into fileString */
244 if ((fp = fopen(fileName, "r")) == NULL) {
245 - return NULL;
246 + return NULL;
248 if (fstat(fileno(fp), &statbuf) != 0) {
249 - fclose(fp);
250 - return NULL;
251 + fclose(fp);
252 + return NULL;
254 - fileLen = statbuf.st_size;
255 - /* +1 = space for null
256 - ** +1 = possible additional \n
257 - */
258 - fileString = XtMalloc(fileLen + 2);
259 - readLen = fread(fileString, sizeof(char), fileLen, fp);
260 - if (ferror(fp)) {
261 - XtFree(fileString);
262 - fclose(fp);
263 - return NULL;
264 + if (S_ISDIR(statbuf.st_mode)) {
265 + fclose(fp);
266 + return NULL;
269 + if (S_ISFIFO(statbuf.st_mode)) {
270 + int pipeBufLen, fileBufSize;
271 + char *fileBuf;
273 + pipeBufLen = statbuf.st_blksize;
274 + if (pipeBufLen <= 0) {
275 + pipeBufLen = 4096;
278 + fileBufSize = pipeBufLen << 2;
279 + fileBuf = malloc(fileBufSize);
280 + if (fileBuf == NULL) {
281 + fclose(fp);
282 + return NULL;
285 + fileLen = 0;
286 + do {
287 + readLen = fread(&fileBuf[fileLen], sizeof(char), pipeBufLen, fp);
289 + if (ferror(fp)) {
290 + fclose(fp);
291 + free(fileBuf);
292 + return NULL;
295 + fileLen += readLen;
296 + if (fileBufSize - fileLen < pipeBufLen) {
297 + char *tmpFileBuf;
299 + fileBufSize += pipeBufLen << 2;
300 + tmpFileBuf = realloc(fileBuf, fileBufSize);
301 + if (tmpFileBuf == NULL) {
302 + fclose(fp);
303 + free(fileBuf);
304 + return NULL;
306 + fileBuf = tmpFileBuf;
309 + } while (!feof(fp));
311 + readLen = fileLen;
312 + /* +1 = space for null
313 + ** +1 = possible additional \n
314 + */
315 + fileString = XtMalloc(readLen + 2);
316 + memcpy(fileString, fileBuf, readLen * sizeof(char));
317 + free(fileBuf);
318 + } else {
319 + fileLen = statbuf.st_size;
320 + /* +1 = space for null
321 + ** +1 = possible additional \n
322 + */
323 + fileString = XtMalloc(fileLen + 2);
324 + if (fileString == NULL) {
325 + fclose(fp);
326 + return NULL;
328 + readLen = fread(fileString, sizeof(char), fileLen, fp);
329 + if (ferror(fp)) {
330 + XtFree(fileString);
331 + fclose(fp);
332 + return NULL;
335 fclose(fp);
336 fileString[readLen] = 0;
338 /* Convert linebreaks? */
339 @@ -710,5 +770,6 @@ char *ReadAnyTextFile(const char *fileNa
340 fileString[readLen + 1] = '\0';
343 return fileString;