Debian Codegraph @

fp.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 #include "jove.h"
9 #include "fp.h"
10 #include "jctype.h"
11 #include "disp.h"
12 #include "fmt.h"
13
14 #ifdef MAC
15 # include "mac.h"
16 #else /* !MAC */
17 # include <sys/stat.h>
18 #endif /* !MAC */
19
20 #ifdef RAINBOW
21 private int rbwrite(int, char *, int);
22 #endif
23
24 #ifndef L_SET
25 # define L_SET 0
26 #endif
27
28 #define MAXFILES 20 /* good enough for my purposes */
29
30 private File openfiles[MAXFILES]; /* must be zeroed initially */
31
32 File *
33 fd_open(const char *name, int flags, int fd, char *buffer, int buf_size)
34 {
35 File *fp;
36 int i;
37
38 for (fp = openfiles, i = 0; i < MAXFILES; i++, fp++)
39 if (fp->f_flags == 0)
40 break;
41
42 if (i == MAXFILES) {
43 complain("[Too many open files!]");
44 /* NOTREACHED */
45 }
46 fp->f_bufsize = buf_size;
47 fp->f_cnt = 0;
48 fp->f_fd = fd;
49 fp->f_flags = flags;
50 if (buffer == NULL) {
51 buffer = emalloc((size_t)buf_size);
52 fp->f_flags |= F_MYBUF;
53 }
54 fp->f_base = fp->f_ptr = buffer;
55 fp->f_name = copystr(name);
56
57 return fp;
58 }
59
60 void
61 gc_openfiles(void)
62 {
63 File *fp;
64
65 for (fp = openfiles; fp < &openfiles[MAXFILES]; fp++)
66 if (fp->f_flags != 0 && (fp->f_flags & F_LOCKED) == 0)
67 f_close(fp);
68 }
69
70 File *
71 f_open(const char *name, int flags, char *buffer, int buf_size)
72 {
73 int fd;
74
75 switch (F_MODE(flags)) {
76 case F_READ:
77 fd = open(name, O_RDONLY | O_BINARY | O_CLOEXEC);
78 break;
79
80 case F_APPEND:
81 fd = open(name, O_WRONLY | O_BINARY | O_CLOEXEC);
82 if (fd != -1) {
83 (void) lseek(fd, (off_t)0, 2);
84 break;
85 }
86 /* FALLTHROUGH */
87 case F_WRITE:
88 #ifdef O_CREAT
89 fd = open(name, O_CREAT | O_TRUNC | O_BINARY | O_RDWR | O_CLOEXEC,
90 # ifdef UNIX
91 (jmode_t)CreatMode
92 # else
93 S_IWRITE | S_IREAD
94 # endif
95 );
96 #else
97 fd = creat(name, (jmode_t)CreatMode);
98 #endif
99 #ifdef O_TRUNC_BROKEN
100 /*
101 * Cygwin 3.1.2 open(..., O_TRUNC) seems to
102 * not truncate files on VirtualBox shared
103 * folders, perhaps on any network drive? But
104 * it has ftruncate and that appears to
105 * workaround the problem. Since most uses of
106 * this function will subsequently write to
107 * the minibuf, seems pointless to report
108 * an error on ftruncate, which really should
109 * never happen if we successfully opened the file!
110 */
111 if (fd >= 0)
112 (void) ftruncate(fd, 0);
113 #endif
114 break;
115
116 default:
117 fd = -1; /* avoid uninitialized complaint from gcc -W */
118 error("invalid F_MODE");
119 /* NOTREACHED */
120 }
121 if (fd == -1)
122 return NULL;
123
124 return fd_open(name, flags, fd, buffer, buf_size);
125 }
126
127 void
128 f_close(File *fp)
129 {
130 const char *what = "close";
131 int err = 0;
132
133 if ((fp->f_flags & (F_WRITE|F_APPEND))
134 && (fp->f_flags & F_ERR) == 0)
135 {
136 flushout(fp);
137 #ifdef USE_FSYNC
138 if (fsync(fp->f_fd) != 0) {
139 what = "fsync";
140 err = errno;
141 }
142 #endif
143 }
144 if (close(fp->f_fd) != 0 && err == 0)
145 err = errno;
146 if (fp->f_flags & F_MYBUF)
147 free(fp->f_base);
148 free(fp->f_name);
149 fp->f_flags = 0; /* indicates that we're available */
150 if (err != 0) {
151 /* It would be nice to print fp->f_name, but it's gone.
152 * Perhaps we should allow the memory to leak so that we
153 * could easily print it.
154 */
155 error("[%s error: %s]", what, strerror(err));
156 /* NOTREACHED */
157 }
158 }
159
160 ZXchar
161 f_filbuf(File *fp)
162 {
163 if (fp->f_flags & (F_EOF|F_ERR))
164 return EOF;
165
166 fp->f_ptr = fp->f_base;
167 #ifndef MSDOS
168 do {
169 #endif /* MSDOS */
170 fp->f_cnt = read(fp->f_fd, fp->f_base, (size_t) fp->f_bufsize);
171 #ifndef MSDOS
172 } while (fp->f_cnt == -1 && errno == EINTR);
173 #endif /* MSDOS */
174 if (fp->f_cnt == -1) {
175 /* I/O error -- treat as EOF */
176 writef("[Read error: %s]", strerror(errno));
177 fp->f_flags |= F_ERR | F_EOF;
178 return EOF;
179 }
180 if (fp->f_cnt == 0) {
181 fp->f_flags |= F_EOF;
182 return EOF;
183 }
184 io_chars += fp->f_cnt;
185 return f_getc(fp);
186 }
187
188 void
189 putstr(const char *s)
190 {
191 char c;
192
193 while ((c = *s++) != '\0')
194 scr_putchar(c);
195 }
196
197 void
198 fputnchar(char *s, int n, File *fp)
199 {
200 while (--n >= 0)
201 f_putc(*s++, fp);
202 }
203
204 #ifndef NO_JSTDOUT
205 void
206 flushscreen(void)
207 {
208 flushout(jstdout);
209 }
210 #endif /* !NO_JSTDOUT */
211
212 void
213 f_seek(File *fp, off_t offset)
214 {
215 if (fp->f_flags & (F_WRITE|F_APPEND))
216 flushout(fp);
217 fp->f_cnt = 0; /* next read will f_filbuf(), next write
218 will flush() with no bad effects */
219 lseek(fp->f_fd, offset, L_SET);
220 }
221
222 void
223 flushout(File *fp)
224 {
225 if (fp->f_flags & (F_READ | F_STRING | F_ERR)) {
226 if (fp->f_flags != F_STRING)
227 abort(); /* IMPOSSIBLE */
228 /* We just banged into the end of a string.
229 * In the interests of continuing, we will cause
230 * the rest of the output to be be heaped in the
231 * last position. Surely it will end up as a NUL. UGH!
232 */
233 fp->f_cnt = 1;
234 fp->f_ptr = &fp->f_base[fp->f_bufsize - 1];
235 } else {
236 char *p = fp->f_base;
237
238 for (;;) {
239 JSSIZE_T n = fp->f_ptr - p,
240 wr;
241
242 if (n <= 0)
243 break;
244
245 #ifdef RAINBOW
246 wr = rbwrite(fp->f_fd, p, (size_t)n);
247 #else
248 wr = write(fp->f_fd, p, (size_t)n);
249 #endif
250 if (wr >= 0) {
251 p += wr;
252 } else {
253 #ifndef MSDOS
254 if (errno != EINTR) {
255 #endif /* MSDOS */
256 #ifndef NO_JSTDOUT
257 if (fp == jstdout)
258 break; /* bail out, silently */
259 #endif
260 fp->f_flags |= F_ERR;
261 error("[I/O error(%s); file = %s, fd = %d]",
262 strerror(errno), fp->f_name, fp->f_fd);
263 /* NOTREACHED */
264 #ifndef MSDOS
265 }
266 #endif /* MSDOS */
267 }
268 }
269
270 fp->f_cnt = fp->f_bufsize;
271 fp->f_ptr = fp->f_base;
272 }
273 }
274
275 jbool
276 f_gets(File *fp, char *buf, size_t max)
277 {
278 char *cp = buf,
279 *endp = buf + max - 1;
280 ZXchar c;
281
282 if (fp->f_flags & F_EOF)
283 return YES;
284
285 while ((c = f_getc(fp)) != EOF && c != EOL) {
286 /* We can't store NUL in our buffer, so ignore it.
287 * Similarly, we can only store characters less than NCHARS.
288 * Of course, with a little ingenuity we could store NUL:
289 * NUL could be represented by EOL.
290 */
291 if (c == '\0'
292 #if NCHARS != UCHAR_ROOF
293 || c >= NCHARS
294 #endif
295 )
296 continue;
297
298 if (cp >= endp) {
299 fp->f_flags |= F_LINETOOLONG;
300 add_mess(" [Line too long]");
301 rbell();
302 return YES;
303 }
304 *cp++ = c;
305 }
306 *cp = '\0';
307 if (c == EOF) {
308 if (cp != buf)
309 add_mess(" [Incomplete last line]");
310 return YES;
311 }
312 #ifdef USE_CRLF
313 /* a CR followed by a LF is treated as a NL.
314 * Bug: the line-buffer is effectively shortened by one character.
315 */
316 if (cp != buf && cp[-1] == '\r')
317 *--cp = '\0';
318 #endif /* USE_CRLF */
319 io_lines += 1;
320 return NO; /* this means okay */
321 }
322
323 /* skip to beginning of next line, i.e., next read returns first
324 * character of new line
325 */
326 void
327 f_toNL(File *fp)
328 {
329 if (fp->f_flags & F_EOF)
330 return;
331
332 for (;;) {
333 switch (f_getc(fp)) {
334 case EOF:
335 fp->f_flags |= F_EOF;
336 /*FALLTHROUGH*/
337 case EOL:
338 return;
339 }
340 }
341 }
342
343 #ifdef PIPEPROCS
344 size_t
345 f_readn(File *fp, char *addr, size_t n)
346 {
347 size_t nleft;
348
349 for (nleft = n; nleft > 0; nleft--) {
350 ZXchar c = f_getc(fp);
351
352 if (f_eof(fp))
353 break;
354
355 *addr++ = c;
356 }
357 return n - nleft;
358 }
359 #endif /* PIPEPROCS */
360
361 /* ScrBufSize is the size of the buffer for jstdout. It is also the
362 * number of characters to be output between checks for input, so
363 * it is meaningful even if jstdout isn't used. Its value is set by
364 * settout based on the baud rate of output (on systems with baud rates).
365 */
366 #ifdef NO_JSTDOUT
367 int ScrBufSize = 256;
368 #else
369 int ScrBufSize = 1; /* until settout decides a better value & allocates a buf */
370 #endif
371
372 #ifndef NO_JSTDOUT
373 private char one_buf;
374
375 private File stdout_File = {1, 1, 1, F_WRITE, &one_buf, &one_buf, (char *)NULL};
376 File *jstdout = &stdout_File;
377 #endif
378
379 #ifdef RAINBOW
380
381 /*
382 * use the Rainbow's video output function
383 */
384
385 #include <dos.h>
386
387 private int
388 rbwrite(fd, buf, cnt)
389 int fd;
390 char *buf;
391 int cnt;
392 {
393 union REGS vr;
394
395 if (fd != 1) {
396 write(fd, buf, cnt);
397 } else {
398 while (cnt-- > 0) {
399 vr.x.ax = *buf++;
400 vr.x.di = 0;
401 int86(0x18, &vr, &vr);
402 }
403 }
404 }
405 #endif /* RAINBOW */