ch.c revision 1.1.1.2 1 1.1.1.2 tron /* $NetBSD: ch.c,v 1.1.1.2 2013/09/04 19:35:03 tron Exp $ */
2 1.1 tron
3 1.1 tron /*
4 1.1.1.2 tron * Copyright (C) 1984-2012 Mark Nudelman
5 1.1 tron *
6 1.1 tron * You may distribute under the terms of either the GNU General Public
7 1.1 tron * License or the Less License, as specified in the README file.
8 1.1 tron *
9 1.1.1.2 tron * For more information, see the README file.
10 1.1 tron */
11 1.1 tron
12 1.1 tron
13 1.1 tron /*
14 1.1 tron * Low level character input from the input file.
15 1.1 tron * We use these special purpose routines which optimize moving
16 1.1 tron * both forward and backward from the current read pointer.
17 1.1 tron */
18 1.1 tron
19 1.1 tron #include "less.h"
20 1.1 tron #if MSDOS_COMPILER==WIN32C
21 1.1 tron #include <errno.h>
22 1.1 tron #include <windows.h>
23 1.1 tron #endif
24 1.1 tron
25 1.1 tron #if HAVE_STAT_INO
26 1.1 tron #include <sys/stat.h>
27 1.1 tron extern dev_t curr_dev;
28 1.1 tron extern ino_t curr_ino;
29 1.1 tron #endif
30 1.1 tron
31 1.1 tron typedef POSITION BLOCKNUM;
32 1.1 tron
33 1.1 tron public int ignore_eoi;
34 1.1 tron
35 1.1 tron /*
36 1.1 tron * Pool of buffers holding the most recently used blocks of the input file.
37 1.1 tron * The buffer pool is kept as a doubly-linked circular list,
38 1.1 tron * in order from most- to least-recently used.
39 1.1 tron * The circular list is anchored by the file state "thisfile".
40 1.1 tron */
41 1.1 tron struct bufnode {
42 1.1 tron struct bufnode *next, *prev;
43 1.1 tron struct bufnode *hnext, *hprev;
44 1.1 tron };
45 1.1 tron
46 1.1 tron #define LBUFSIZE 8192
47 1.1 tron struct buf {
48 1.1 tron struct bufnode node;
49 1.1 tron BLOCKNUM block;
50 1.1 tron unsigned int datasize;
51 1.1 tron unsigned char data[LBUFSIZE];
52 1.1 tron };
53 1.1 tron #define bufnode_buf(bn) ((struct buf *) bn)
54 1.1 tron
55 1.1 tron /*
56 1.1 tron * The file state is maintained in a filestate structure.
57 1.1 tron * A pointer to the filestate is kept in the ifile structure.
58 1.1 tron */
59 1.1 tron #define BUFHASH_SIZE 64
60 1.1 tron struct filestate {
61 1.1 tron struct bufnode buflist;
62 1.1 tron struct bufnode hashtbl[BUFHASH_SIZE];
63 1.1 tron int file;
64 1.1 tron int flags;
65 1.1 tron POSITION fpos;
66 1.1 tron int nbufs;
67 1.1 tron BLOCKNUM block;
68 1.1 tron unsigned int offset;
69 1.1 tron POSITION fsize;
70 1.1 tron };
71 1.1 tron
72 1.1 tron #define ch_bufhead thisfile->buflist.next
73 1.1 tron #define ch_buftail thisfile->buflist.prev
74 1.1 tron #define ch_nbufs thisfile->nbufs
75 1.1 tron #define ch_block thisfile->block
76 1.1 tron #define ch_offset thisfile->offset
77 1.1 tron #define ch_fpos thisfile->fpos
78 1.1 tron #define ch_fsize thisfile->fsize
79 1.1 tron #define ch_flags thisfile->flags
80 1.1 tron #define ch_file thisfile->file
81 1.1 tron
82 1.1 tron #define END_OF_CHAIN (&thisfile->buflist)
83 1.1 tron #define END_OF_HCHAIN(h) (&thisfile->hashtbl[h])
84 1.1 tron #define BUFHASH(blk) ((blk) & (BUFHASH_SIZE-1))
85 1.1 tron
86 1.1 tron /*
87 1.1 tron * Macros to manipulate the list of buffers in thisfile->buflist.
88 1.1 tron */
89 1.1 tron #define FOR_BUFS(bn) \
90 1.1 tron for (bn = ch_bufhead; bn != END_OF_CHAIN; bn = bn->next)
91 1.1 tron
92 1.1 tron #define BUF_RM(bn) \
93 1.1 tron (bn)->next->prev = (bn)->prev; \
94 1.1 tron (bn)->prev->next = (bn)->next;
95 1.1 tron
96 1.1 tron #define BUF_INS_HEAD(bn) \
97 1.1 tron (bn)->next = ch_bufhead; \
98 1.1 tron (bn)->prev = END_OF_CHAIN; \
99 1.1 tron ch_bufhead->prev = (bn); \
100 1.1 tron ch_bufhead = (bn);
101 1.1 tron
102 1.1 tron #define BUF_INS_TAIL(bn) \
103 1.1 tron (bn)->next = END_OF_CHAIN; \
104 1.1 tron (bn)->prev = ch_buftail; \
105 1.1 tron ch_buftail->next = (bn); \
106 1.1 tron ch_buftail = (bn);
107 1.1 tron
108 1.1 tron /*
109 1.1 tron * Macros to manipulate the list of buffers in thisfile->hashtbl[n].
110 1.1 tron */
111 1.1 tron #define FOR_BUFS_IN_CHAIN(h,bn) \
112 1.1 tron for (bn = thisfile->hashtbl[h].hnext; \
113 1.1 tron bn != END_OF_HCHAIN(h); bn = bn->hnext)
114 1.1 tron
115 1.1 tron #define BUF_HASH_RM(bn) \
116 1.1 tron (bn)->hnext->hprev = (bn)->hprev; \
117 1.1 tron (bn)->hprev->hnext = (bn)->hnext;
118 1.1 tron
119 1.1 tron #define BUF_HASH_INS(bn,h) \
120 1.1 tron (bn)->hnext = thisfile->hashtbl[h].hnext; \
121 1.1 tron (bn)->hprev = END_OF_HCHAIN(h); \
122 1.1 tron thisfile->hashtbl[h].hnext->hprev = (bn); \
123 1.1 tron thisfile->hashtbl[h].hnext = (bn);
124 1.1 tron
125 1.1 tron static struct filestate *thisfile;
126 1.1 tron static int ch_ungotchar = -1;
127 1.1 tron static int maxbufs = -1;
128 1.1 tron
129 1.1 tron extern int autobuf;
130 1.1 tron extern int sigs;
131 1.1 tron extern int secure;
132 1.1 tron extern int screen_trashed;
133 1.1 tron extern int follow_mode;
134 1.1 tron extern constant char helpdata[];
135 1.1 tron extern constant int size_helpdata;
136 1.1 tron extern IFILE curr_ifile;
137 1.1 tron #if LOGFILE
138 1.1 tron extern int logfile;
139 1.1 tron extern char *namelogfile;
140 1.1 tron #endif
141 1.1 tron
142 1.1 tron static int ch_addbuf();
143 1.1 tron
144 1.1 tron
145 1.1 tron /*
146 1.1 tron * Get the character pointed to by the read pointer.
147 1.1 tron */
148 1.1 tron int
149 1.1 tron ch_get()
150 1.1 tron {
151 1.1 tron register struct buf *bp;
152 1.1 tron register struct bufnode *bn;
153 1.1 tron register int n;
154 1.1 tron register int slept;
155 1.1 tron register int h;
156 1.1 tron POSITION pos;
157 1.1 tron POSITION len;
158 1.1 tron
159 1.1 tron if (thisfile == NULL)
160 1.1 tron return (EOI);
161 1.1 tron
162 1.1 tron /*
163 1.1 tron * Quick check for the common case where
164 1.1 tron * the desired char is in the head buffer.
165 1.1 tron */
166 1.1 tron if (ch_bufhead != END_OF_CHAIN)
167 1.1 tron {
168 1.1 tron bp = bufnode_buf(ch_bufhead);
169 1.1 tron if (ch_block == bp->block && ch_offset < bp->datasize)
170 1.1 tron return bp->data[ch_offset];
171 1.1 tron }
172 1.1 tron
173 1.1 tron slept = FALSE;
174 1.1 tron
175 1.1 tron /*
176 1.1 tron * Look for a buffer holding the desired block.
177 1.1 tron */
178 1.1 tron h = BUFHASH(ch_block);
179 1.1 tron FOR_BUFS_IN_CHAIN(h, bn)
180 1.1 tron {
181 1.1 tron bp = bufnode_buf(bn);
182 1.1 tron if (bp->block == ch_block)
183 1.1 tron {
184 1.1 tron if (ch_offset >= bp->datasize)
185 1.1 tron /*
186 1.1 tron * Need more data in this buffer.
187 1.1 tron */
188 1.1 tron break;
189 1.1 tron goto found;
190 1.1 tron }
191 1.1 tron }
192 1.1 tron if (bn == END_OF_HCHAIN(h))
193 1.1 tron {
194 1.1 tron /*
195 1.1 tron * Block is not in a buffer.
196 1.1 tron * Take the least recently used buffer
197 1.1 tron * and read the desired block into it.
198 1.1 tron * If the LRU buffer has data in it,
199 1.1 tron * then maybe allocate a new buffer.
200 1.1 tron */
201 1.1 tron if (ch_buftail == END_OF_CHAIN ||
202 1.1 tron bufnode_buf(ch_buftail)->block != -1)
203 1.1 tron {
204 1.1 tron /*
205 1.1 tron * There is no empty buffer to use.
206 1.1 tron * Allocate a new buffer if:
207 1.1 tron * 1. We can't seek on this file and -b is not in effect; or
208 1.1 tron * 2. We haven't allocated the max buffers for this file yet.
209 1.1 tron */
210 1.1 tron if ((autobuf && !(ch_flags & CH_CANSEEK)) ||
211 1.1 tron (maxbufs < 0 || ch_nbufs < maxbufs))
212 1.1 tron if (ch_addbuf())
213 1.1 tron /*
214 1.1 tron * Allocation failed: turn off autobuf.
215 1.1 tron */
216 1.1 tron autobuf = OPT_OFF;
217 1.1 tron }
218 1.1 tron bn = ch_buftail;
219 1.1 tron bp = bufnode_buf(bn);
220 1.1 tron BUF_HASH_RM(bn); /* Remove from old hash chain. */
221 1.1 tron bp->block = ch_block;
222 1.1 tron bp->datasize = 0;
223 1.1 tron BUF_HASH_INS(bn, h); /* Insert into new hash chain. */
224 1.1 tron }
225 1.1 tron
226 1.1 tron read_more:
227 1.1 tron pos = (ch_block * LBUFSIZE) + bp->datasize;
228 1.1 tron if ((len = ch_length()) != NULL_POSITION && pos >= len)
229 1.1 tron /*
230 1.1 tron * At end of file.
231 1.1 tron */
232 1.1 tron return (EOI);
233 1.1 tron
234 1.1 tron if (pos != ch_fpos)
235 1.1 tron {
236 1.1 tron /*
237 1.1 tron * Not at the correct position: must seek.
238 1.1 tron * If input is a pipe, we're in trouble (can't seek on a pipe).
239 1.1 tron * Some data has been lost: just return "?".
240 1.1 tron */
241 1.1 tron if (!(ch_flags & CH_CANSEEK))
242 1.1 tron return ('?');
243 1.1 tron if (lseek(ch_file, (off_t)pos, SEEK_SET) == BAD_LSEEK)
244 1.1 tron {
245 1.1 tron error("seek error", NULL_PARG);
246 1.1 tron clear_eol();
247 1.1 tron return (EOI);
248 1.1 tron }
249 1.1 tron ch_fpos = pos;
250 1.1 tron }
251 1.1 tron
252 1.1 tron /*
253 1.1 tron * Read the block.
254 1.1 tron * If we read less than a full block, that's ok.
255 1.1 tron * We use partial block and pick up the rest next time.
256 1.1 tron */
257 1.1 tron if (ch_ungotchar != -1)
258 1.1 tron {
259 1.1 tron bp->data[bp->datasize] = ch_ungotchar;
260 1.1 tron n = 1;
261 1.1 tron ch_ungotchar = -1;
262 1.1 tron } else if (ch_flags & CH_HELPFILE)
263 1.1 tron {
264 1.1 tron bp->data[bp->datasize] = helpdata[ch_fpos];
265 1.1 tron n = 1;
266 1.1 tron } else
267 1.1 tron {
268 1.1 tron n = iread(ch_file, &bp->data[bp->datasize],
269 1.1 tron (unsigned int)(LBUFSIZE - bp->datasize));
270 1.1 tron }
271 1.1 tron
272 1.1 tron if (n == READ_INTR)
273 1.1 tron return (EOI);
274 1.1 tron if (n < 0)
275 1.1 tron {
276 1.1 tron #if MSDOS_COMPILER==WIN32C
277 1.1 tron if (errno != EPIPE)
278 1.1 tron #endif
279 1.1 tron {
280 1.1 tron error("read error", NULL_PARG);
281 1.1 tron clear_eol();
282 1.1 tron }
283 1.1 tron n = 0;
284 1.1 tron }
285 1.1 tron
286 1.1 tron #if LOGFILE
287 1.1 tron /*
288 1.1 tron * If we have a log file, write the new data to it.
289 1.1 tron */
290 1.1 tron if (!secure && logfile >= 0 && n > 0)
291 1.1 tron write(logfile, (char *) &bp->data[bp->datasize], n);
292 1.1 tron #endif
293 1.1 tron
294 1.1 tron ch_fpos += n;
295 1.1 tron bp->datasize += n;
296 1.1 tron
297 1.1 tron /*
298 1.1 tron * If we have read to end of file, set ch_fsize to indicate
299 1.1 tron * the position of the end of file.
300 1.1 tron */
301 1.1 tron if (n == 0)
302 1.1 tron {
303 1.1 tron ch_fsize = pos;
304 1.1 tron if (ignore_eoi)
305 1.1 tron {
306 1.1 tron /*
307 1.1 tron * We are ignoring EOF.
308 1.1 tron * Wait a while, then try again.
309 1.1 tron */
310 1.1 tron if (!slept)
311 1.1 tron {
312 1.1 tron PARG parg;
313 1.1 tron parg.p_string = wait_message();
314 1.1 tron ierror("%s", &parg);
315 1.1 tron }
316 1.1 tron #if !MSDOS_COMPILER
317 1.1 tron sleep(1);
318 1.1 tron #else
319 1.1 tron #if MSDOS_COMPILER==WIN32C
320 1.1 tron Sleep(1000);
321 1.1 tron #endif
322 1.1 tron #endif
323 1.1 tron slept = TRUE;
324 1.1 tron
325 1.1 tron #if HAVE_STAT_INO
326 1.1 tron if (follow_mode == FOLLOW_NAME)
327 1.1 tron {
328 1.1 tron /* See whether the file's i-number has changed.
329 1.1 tron * If so, force the file to be closed and
330 1.1 tron * reopened. */
331 1.1 tron struct stat st;
332 1.1 tron int r = stat(get_filename(curr_ifile), &st);
333 1.1 tron if (r == 0 && (st.st_ino != curr_ino ||
334 1.1 tron st.st_dev != curr_dev))
335 1.1 tron {
336 1.1 tron /* screen_trashed=2 causes
337 1.1 tron * make_display to reopen the file. */
338 1.1 tron screen_trashed = 2;
339 1.1 tron return (EOI);
340 1.1 tron }
341 1.1 tron }
342 1.1 tron #endif
343 1.1 tron }
344 1.1 tron if (sigs)
345 1.1 tron return (EOI);
346 1.1 tron }
347 1.1 tron
348 1.1 tron found:
349 1.1 tron if (ch_bufhead != bn)
350 1.1 tron {
351 1.1 tron /*
352 1.1 tron * Move the buffer to the head of the buffer chain.
353 1.1 tron * This orders the buffer chain, most- to least-recently used.
354 1.1 tron */
355 1.1 tron BUF_RM(bn);
356 1.1 tron BUF_INS_HEAD(bn);
357 1.1 tron
358 1.1 tron /*
359 1.1 tron * Move to head of hash chain too.
360 1.1 tron */
361 1.1 tron BUF_HASH_RM(bn);
362 1.1 tron BUF_HASH_INS(bn, h);
363 1.1 tron }
364 1.1 tron
365 1.1 tron if (ch_offset >= bp->datasize)
366 1.1 tron /*
367 1.1 tron * After all that, we still don't have enough data.
368 1.1 tron * Go back and try again.
369 1.1 tron */
370 1.1 tron goto read_more;
371 1.1 tron
372 1.1 tron return (bp->data[ch_offset]);
373 1.1 tron }
374 1.1 tron
375 1.1 tron /*
376 1.1 tron * ch_ungetchar is a rather kludgy and limited way to push
377 1.1 tron * a single char onto an input file descriptor.
378 1.1 tron */
379 1.1 tron public void
380 1.1 tron ch_ungetchar(c)
381 1.1 tron int c;
382 1.1 tron {
383 1.1 tron if (c != -1 && ch_ungotchar != -1)
384 1.1 tron error("ch_ungetchar overrun", NULL_PARG);
385 1.1 tron ch_ungotchar = c;
386 1.1 tron }
387 1.1 tron
388 1.1 tron #if LOGFILE
389 1.1 tron /*
390 1.1 tron * Close the logfile.
391 1.1 tron * If we haven't read all of standard input into it, do that now.
392 1.1 tron */
393 1.1 tron public void
394 1.1 tron end_logfile()
395 1.1 tron {
396 1.1 tron static int tried = FALSE;
397 1.1 tron
398 1.1 tron if (logfile < 0)
399 1.1 tron return;
400 1.1 tron if (!tried && ch_fsize == NULL_POSITION)
401 1.1 tron {
402 1.1 tron tried = TRUE;
403 1.1 tron ierror("Finishing logfile", NULL_PARG);
404 1.1 tron while (ch_forw_get() != EOI)
405 1.1 tron if (ABORT_SIGS())
406 1.1 tron break;
407 1.1 tron }
408 1.1 tron close(logfile);
409 1.1 tron logfile = -1;
410 1.1 tron namelogfile = NULL;
411 1.1 tron }
412 1.1 tron
413 1.1 tron /*
414 1.1 tron * Start a log file AFTER less has already been running.
415 1.1 tron * Invoked from the - command; see toggle_option().
416 1.1 tron * Write all the existing buffered data to the log file.
417 1.1 tron */
418 1.1 tron public void
419 1.1 tron sync_logfile()
420 1.1 tron {
421 1.1 tron register struct buf *bp;
422 1.1 tron register struct bufnode *bn;
423 1.1 tron int warned = FALSE;
424 1.1 tron BLOCKNUM block;
425 1.1 tron BLOCKNUM nblocks;
426 1.1 tron
427 1.1 tron nblocks = (ch_fpos + LBUFSIZE - 1) / LBUFSIZE;
428 1.1 tron for (block = 0; block < nblocks; block++)
429 1.1 tron {
430 1.1 tron int wrote = FALSE;
431 1.1 tron FOR_BUFS(bn)
432 1.1 tron {
433 1.1 tron bp = bufnode_buf(bn);
434 1.1 tron if (bp->block == block)
435 1.1 tron {
436 1.1 tron write(logfile, (char *) bp->data, bp->datasize);
437 1.1 tron wrote = TRUE;
438 1.1 tron break;
439 1.1 tron }
440 1.1 tron }
441 1.1 tron if (!wrote && !warned)
442 1.1 tron {
443 1.1 tron error("Warning: log file is incomplete",
444 1.1 tron NULL_PARG);
445 1.1 tron warned = TRUE;
446 1.1 tron }
447 1.1 tron }
448 1.1 tron }
449 1.1 tron
450 1.1 tron #endif
451 1.1 tron
452 1.1 tron /*
453 1.1 tron * Determine if a specific block is currently in one of the buffers.
454 1.1 tron */
455 1.1 tron static int
456 1.1 tron buffered(block)
457 1.1 tron BLOCKNUM block;
458 1.1 tron {
459 1.1 tron register struct buf *bp;
460 1.1 tron register struct bufnode *bn;
461 1.1 tron register int h;
462 1.1 tron
463 1.1 tron h = BUFHASH(block);
464 1.1 tron FOR_BUFS_IN_CHAIN(h, bn)
465 1.1 tron {
466 1.1 tron bp = bufnode_buf(bn);
467 1.1 tron if (bp->block == block)
468 1.1 tron return (TRUE);
469 1.1 tron }
470 1.1 tron return (FALSE);
471 1.1 tron }
472 1.1 tron
473 1.1 tron /*
474 1.1 tron * Seek to a specified position in the file.
475 1.1 tron * Return 0 if successful, non-zero if can't seek there.
476 1.1 tron */
477 1.1 tron public int
478 1.1 tron ch_seek(pos)
479 1.1 tron register POSITION pos;
480 1.1 tron {
481 1.1 tron BLOCKNUM new_block;
482 1.1 tron POSITION len;
483 1.1 tron
484 1.1 tron if (thisfile == NULL)
485 1.1 tron return (0);
486 1.1 tron
487 1.1 tron len = ch_length();
488 1.1 tron if (pos < ch_zero() || (len != NULL_POSITION && pos > len))
489 1.1 tron return (1);
490 1.1 tron
491 1.1 tron new_block = pos / LBUFSIZE;
492 1.1 tron if (!(ch_flags & CH_CANSEEK) && pos != ch_fpos && !buffered(new_block))
493 1.1 tron {
494 1.1 tron if (ch_fpos > pos)
495 1.1 tron return (1);
496 1.1 tron while (ch_fpos < pos)
497 1.1 tron {
498 1.1 tron if (ch_forw_get() == EOI)
499 1.1 tron return (1);
500 1.1 tron if (ABORT_SIGS())
501 1.1 tron return (1);
502 1.1 tron }
503 1.1 tron return (0);
504 1.1 tron }
505 1.1 tron /*
506 1.1 tron * Set read pointer.
507 1.1 tron */
508 1.1 tron ch_block = new_block;
509 1.1 tron ch_offset = pos % LBUFSIZE;
510 1.1 tron return (0);
511 1.1 tron }
512 1.1 tron
513 1.1 tron /*
514 1.1 tron * Seek to the end of the file.
515 1.1 tron */
516 1.1 tron public int
517 1.1 tron ch_end_seek()
518 1.1 tron {
519 1.1 tron POSITION len;
520 1.1 tron
521 1.1 tron if (thisfile == NULL)
522 1.1 tron return (0);
523 1.1 tron
524 1.1 tron if (ch_flags & CH_CANSEEK)
525 1.1 tron ch_fsize = filesize(ch_file);
526 1.1 tron
527 1.1 tron len = ch_length();
528 1.1 tron if (len != NULL_POSITION)
529 1.1 tron return (ch_seek(len));
530 1.1 tron
531 1.1 tron /*
532 1.1 tron * Do it the slow way: read till end of data.
533 1.1 tron */
534 1.1 tron while (ch_forw_get() != EOI)
535 1.1 tron if (ABORT_SIGS())
536 1.1 tron return (1);
537 1.1 tron return (0);
538 1.1 tron }
539 1.1 tron
540 1.1 tron /*
541 1.1 tron * Seek to the beginning of the file, or as close to it as we can get.
542 1.1 tron * We may not be able to seek there if input is a pipe and the
543 1.1 tron * beginning of the pipe is no longer buffered.
544 1.1 tron */
545 1.1 tron public int
546 1.1 tron ch_beg_seek()
547 1.1 tron {
548 1.1 tron register struct bufnode *bn;
549 1.1 tron register struct bufnode *firstbn;
550 1.1 tron
551 1.1 tron /*
552 1.1 tron * Try a plain ch_seek first.
553 1.1 tron */
554 1.1 tron if (ch_seek(ch_zero()) == 0)
555 1.1 tron return (0);
556 1.1 tron
557 1.1 tron /*
558 1.1 tron * Can't get to position 0.
559 1.1 tron * Look thru the buffers for the one closest to position 0.
560 1.1 tron */
561 1.1 tron firstbn = ch_bufhead;
562 1.1 tron if (firstbn == END_OF_CHAIN)
563 1.1 tron return (1);
564 1.1 tron FOR_BUFS(bn)
565 1.1 tron {
566 1.1 tron if (bufnode_buf(bn)->block < bufnode_buf(firstbn)->block)
567 1.1 tron firstbn = bn;
568 1.1 tron }
569 1.1 tron ch_block = bufnode_buf(firstbn)->block;
570 1.1 tron ch_offset = 0;
571 1.1 tron return (0);
572 1.1 tron }
573 1.1 tron
574 1.1 tron /*
575 1.1 tron * Return the length of the file, if known.
576 1.1 tron */
577 1.1 tron public POSITION
578 1.1 tron ch_length()
579 1.1 tron {
580 1.1 tron if (thisfile == NULL)
581 1.1 tron return (NULL_POSITION);
582 1.1 tron if (ignore_eoi)
583 1.1 tron return (NULL_POSITION);
584 1.1 tron if (ch_flags & CH_HELPFILE)
585 1.1 tron return (size_helpdata);
586 1.1.1.2 tron if (ch_flags & CH_NODATA)
587 1.1.1.2 tron return (0);
588 1.1 tron return (ch_fsize);
589 1.1 tron }
590 1.1 tron
591 1.1 tron /*
592 1.1 tron * Return the current position in the file.
593 1.1 tron */
594 1.1 tron public POSITION
595 1.1 tron ch_tell()
596 1.1 tron {
597 1.1 tron if (thisfile == NULL)
598 1.1 tron return (NULL_POSITION);
599 1.1 tron return (ch_block * LBUFSIZE) + ch_offset;
600 1.1 tron }
601 1.1 tron
602 1.1 tron /*
603 1.1 tron * Get the current char and post-increment the read pointer.
604 1.1 tron */
605 1.1 tron public int
606 1.1 tron ch_forw_get()
607 1.1 tron {
608 1.1 tron register int c;
609 1.1 tron
610 1.1 tron if (thisfile == NULL)
611 1.1 tron return (EOI);
612 1.1 tron c = ch_get();
613 1.1 tron if (c == EOI)
614 1.1 tron return (EOI);
615 1.1 tron if (ch_offset < LBUFSIZE-1)
616 1.1 tron ch_offset++;
617 1.1 tron else
618 1.1 tron {
619 1.1 tron ch_block ++;
620 1.1 tron ch_offset = 0;
621 1.1 tron }
622 1.1 tron return (c);
623 1.1 tron }
624 1.1 tron
625 1.1 tron /*
626 1.1 tron * Pre-decrement the read pointer and get the new current char.
627 1.1 tron */
628 1.1 tron public int
629 1.1 tron ch_back_get()
630 1.1 tron {
631 1.1 tron if (thisfile == NULL)
632 1.1 tron return (EOI);
633 1.1 tron if (ch_offset > 0)
634 1.1 tron ch_offset --;
635 1.1 tron else
636 1.1 tron {
637 1.1 tron if (ch_block <= 0)
638 1.1 tron return (EOI);
639 1.1 tron if (!(ch_flags & CH_CANSEEK) && !buffered(ch_block-1))
640 1.1 tron return (EOI);
641 1.1 tron ch_block--;
642 1.1 tron ch_offset = LBUFSIZE-1;
643 1.1 tron }
644 1.1 tron return (ch_get());
645 1.1 tron }
646 1.1 tron
647 1.1 tron /*
648 1.1 tron * Set max amount of buffer space.
649 1.1 tron * bufspace is in units of 1024 bytes. -1 mean no limit.
650 1.1 tron */
651 1.1 tron public void
652 1.1 tron ch_setbufspace(bufspace)
653 1.1 tron int bufspace;
654 1.1 tron {
655 1.1 tron if (bufspace < 0)
656 1.1 tron maxbufs = -1;
657 1.1 tron else
658 1.1 tron {
659 1.1 tron maxbufs = ((bufspace * 1024) + LBUFSIZE-1) / LBUFSIZE;
660 1.1 tron if (maxbufs < 1)
661 1.1 tron maxbufs = 1;
662 1.1 tron }
663 1.1 tron }
664 1.1 tron
665 1.1 tron /*
666 1.1 tron * Flush (discard) any saved file state, including buffer contents.
667 1.1 tron */
668 1.1 tron public void
669 1.1 tron ch_flush()
670 1.1 tron {
671 1.1 tron register struct bufnode *bn;
672 1.1 tron
673 1.1 tron if (thisfile == NULL)
674 1.1 tron return;
675 1.1 tron
676 1.1 tron if (!(ch_flags & CH_CANSEEK))
677 1.1 tron {
678 1.1 tron /*
679 1.1 tron * If input is a pipe, we don't flush buffer contents,
680 1.1 tron * since the contents can't be recovered.
681 1.1 tron */
682 1.1 tron ch_fsize = NULL_POSITION;
683 1.1 tron return;
684 1.1 tron }
685 1.1 tron
686 1.1 tron /*
687 1.1 tron * Initialize all the buffers.
688 1.1 tron */
689 1.1 tron FOR_BUFS(bn)
690 1.1 tron {
691 1.1 tron bufnode_buf(bn)->block = -1;
692 1.1 tron }
693 1.1 tron
694 1.1 tron /*
695 1.1 tron * Figure out the size of the file, if we can.
696 1.1 tron */
697 1.1 tron ch_fsize = filesize(ch_file);
698 1.1 tron
699 1.1 tron /*
700 1.1 tron * Seek to a known position: the beginning of the file.
701 1.1 tron */
702 1.1 tron ch_fpos = 0;
703 1.1 tron ch_block = 0; /* ch_fpos / LBUFSIZE; */
704 1.1 tron ch_offset = 0; /* ch_fpos % LBUFSIZE; */
705 1.1 tron
706 1.1 tron #if 1
707 1.1 tron /*
708 1.1 tron * This is a kludge to workaround a Linux kernel bug: files in
709 1.1 tron * /proc have a size of 0 according to fstat() but have readable
710 1.1 tron * data. They are sometimes, but not always, seekable.
711 1.1 tron * Force them to be non-seekable here.
712 1.1 tron */
713 1.1 tron if (ch_fsize == 0)
714 1.1 tron {
715 1.1 tron ch_fsize = NULL_POSITION;
716 1.1 tron ch_flags &= ~CH_CANSEEK;
717 1.1 tron }
718 1.1 tron #endif
719 1.1 tron
720 1.1 tron if (lseek(ch_file, (off_t)0, SEEK_SET) == BAD_LSEEK)
721 1.1 tron {
722 1.1 tron /*
723 1.1 tron * Warning only; even if the seek fails for some reason,
724 1.1 tron * there's a good chance we're at the beginning anyway.
725 1.1 tron * {{ I think this is bogus reasoning. }}
726 1.1 tron */
727 1.1 tron error("seek error to 0", NULL_PARG);
728 1.1 tron }
729 1.1 tron }
730 1.1 tron
731 1.1 tron /*
732 1.1 tron * Allocate a new buffer.
733 1.1 tron * The buffer is added to the tail of the buffer chain.
734 1.1 tron */
735 1.1 tron static int
736 1.1 tron ch_addbuf()
737 1.1 tron {
738 1.1 tron register struct buf *bp;
739 1.1 tron register struct bufnode *bn;
740 1.1 tron
741 1.1 tron /*
742 1.1 tron * Allocate and initialize a new buffer and link it
743 1.1 tron * onto the tail of the buffer list.
744 1.1 tron */
745 1.1 tron bp = (struct buf *) calloc(1, sizeof(struct buf));
746 1.1 tron if (bp == NULL)
747 1.1 tron return (1);
748 1.1 tron ch_nbufs++;
749 1.1 tron bp->block = -1;
750 1.1 tron bn = &bp->node;
751 1.1 tron
752 1.1 tron BUF_INS_TAIL(bn);
753 1.1 tron BUF_HASH_INS(bn, 0);
754 1.1 tron return (0);
755 1.1 tron }
756 1.1 tron
757 1.1 tron /*
758 1.1 tron *
759 1.1 tron */
760 1.1 tron static void
761 1.1 tron init_hashtbl()
762 1.1 tron {
763 1.1 tron register int h;
764 1.1 tron
765 1.1 tron for (h = 0; h < BUFHASH_SIZE; h++)
766 1.1 tron {
767 1.1 tron thisfile->hashtbl[h].hnext = END_OF_HCHAIN(h);
768 1.1 tron thisfile->hashtbl[h].hprev = END_OF_HCHAIN(h);
769 1.1 tron }
770 1.1 tron }
771 1.1 tron
772 1.1 tron /*
773 1.1 tron * Delete all buffers for this file.
774 1.1 tron */
775 1.1 tron static void
776 1.1 tron ch_delbufs()
777 1.1 tron {
778 1.1 tron register struct bufnode *bn;
779 1.1 tron
780 1.1 tron while (ch_bufhead != END_OF_CHAIN)
781 1.1 tron {
782 1.1 tron bn = ch_bufhead;
783 1.1 tron BUF_RM(bn);
784 1.1 tron free(bufnode_buf(bn));
785 1.1 tron }
786 1.1 tron ch_nbufs = 0;
787 1.1 tron init_hashtbl();
788 1.1 tron }
789 1.1 tron
790 1.1 tron /*
791 1.1 tron * Is it possible to seek on a file descriptor?
792 1.1 tron */
793 1.1 tron public int
794 1.1 tron seekable(f)
795 1.1 tron int f;
796 1.1 tron {
797 1.1 tron #if MSDOS_COMPILER
798 1.1 tron extern int fd0;
799 1.1 tron if (f == fd0 && !isatty(fd0))
800 1.1 tron {
801 1.1 tron /*
802 1.1 tron * In MS-DOS, pipes are seekable. Check for
803 1.1 tron * standard input, and pretend it is not seekable.
804 1.1 tron */
805 1.1 tron return (0);
806 1.1 tron }
807 1.1 tron #endif
808 1.1 tron return (lseek(f, (off_t)1, SEEK_SET) != BAD_LSEEK);
809 1.1 tron }
810 1.1 tron
811 1.1 tron /*
812 1.1.1.2 tron * Force EOF to be at the current read position.
813 1.1.1.2 tron * This is used after an ignore_eof read, during which the EOF may change.
814 1.1.1.2 tron */
815 1.1.1.2 tron public void
816 1.1.1.2 tron ch_set_eof()
817 1.1.1.2 tron {
818 1.1.1.2 tron ch_fsize = ch_fpos;
819 1.1.1.2 tron }
820 1.1.1.2 tron
821 1.1.1.2 tron
822 1.1.1.2 tron /*
823 1.1 tron * Initialize file state for a new file.
824 1.1 tron */
825 1.1 tron public void
826 1.1 tron ch_init(f, flags)
827 1.1 tron int f;
828 1.1 tron int flags;
829 1.1 tron {
830 1.1 tron /*
831 1.1 tron * See if we already have a filestate for this file.
832 1.1 tron */
833 1.1 tron thisfile = (struct filestate *) get_filestate(curr_ifile);
834 1.1 tron if (thisfile == NULL)
835 1.1 tron {
836 1.1 tron /*
837 1.1 tron * Allocate and initialize a new filestate.
838 1.1 tron */
839 1.1 tron thisfile = (struct filestate *)
840 1.1 tron calloc(1, sizeof(struct filestate));
841 1.1 tron thisfile->buflist.next = thisfile->buflist.prev = END_OF_CHAIN;
842 1.1 tron thisfile->nbufs = 0;
843 1.1 tron thisfile->flags = 0;
844 1.1 tron thisfile->fpos = 0;
845 1.1 tron thisfile->block = 0;
846 1.1 tron thisfile->offset = 0;
847 1.1 tron thisfile->file = -1;
848 1.1 tron thisfile->fsize = NULL_POSITION;
849 1.1 tron ch_flags = flags;
850 1.1 tron init_hashtbl();
851 1.1 tron /*
852 1.1 tron * Try to seek; set CH_CANSEEK if it works.
853 1.1 tron */
854 1.1 tron if ((flags & CH_CANSEEK) && !seekable(f))
855 1.1 tron ch_flags &= ~CH_CANSEEK;
856 1.1 tron set_filestate(curr_ifile, (void *) thisfile);
857 1.1 tron }
858 1.1 tron if (thisfile->file == -1)
859 1.1 tron thisfile->file = f;
860 1.1 tron ch_flush();
861 1.1 tron }
862 1.1 tron
863 1.1 tron /*
864 1.1 tron * Close a filestate.
865 1.1 tron */
866 1.1 tron public void
867 1.1 tron ch_close()
868 1.1 tron {
869 1.1 tron int keepstate = FALSE;
870 1.1 tron
871 1.1 tron if (thisfile == NULL)
872 1.1 tron return;
873 1.1 tron
874 1.1 tron if (ch_flags & (CH_CANSEEK|CH_POPENED|CH_HELPFILE))
875 1.1 tron {
876 1.1 tron /*
877 1.1 tron * We can seek or re-open, so we don't need to keep buffers.
878 1.1 tron */
879 1.1 tron ch_delbufs();
880 1.1 tron } else
881 1.1 tron keepstate = TRUE;
882 1.1 tron if (!(ch_flags & CH_KEEPOPEN))
883 1.1 tron {
884 1.1 tron /*
885 1.1 tron * We don't need to keep the file descriptor open
886 1.1 tron * (because we can re-open it.)
887 1.1 tron * But don't really close it if it was opened via popen(),
888 1.1 tron * because pclose() wants to close it.
889 1.1 tron */
890 1.1 tron if (!(ch_flags & (CH_POPENED|CH_HELPFILE)))
891 1.1 tron close(ch_file);
892 1.1 tron ch_file = -1;
893 1.1 tron } else
894 1.1 tron keepstate = TRUE;
895 1.1 tron if (!keepstate)
896 1.1 tron {
897 1.1 tron /*
898 1.1 tron * We don't even need to keep the filestate structure.
899 1.1 tron */
900 1.1 tron free(thisfile);
901 1.1 tron thisfile = NULL;
902 1.1 tron set_filestate(curr_ifile, (void *) NULL);
903 1.1 tron }
904 1.1 tron }
905 1.1 tron
906 1.1 tron /*
907 1.1 tron * Return ch_flags for the current file.
908 1.1 tron */
909 1.1 tron public int
910 1.1 tron ch_getflags()
911 1.1 tron {
912 1.1 tron if (thisfile == NULL)
913 1.1 tron return (0);
914 1.1 tron return (ch_flags);
915 1.1 tron }
916 1.1 tron
917 1.1 tron #if 0
918 1.1 tron public void
919 1.1 tron ch_dump(struct filestate *fs)
920 1.1 tron {
921 1.1 tron struct buf *bp;
922 1.1 tron struct bufnode *bn;
923 1.1 tron unsigned char *s;
924 1.1 tron
925 1.1 tron if (fs == NULL)
926 1.1 tron {
927 1.1 tron printf(" --no filestate\n");
928 1.1 tron return;
929 1.1 tron }
930 1.1 tron printf(" file %d, flags %x, fpos %x, fsize %x, blk/off %x/%x\n",
931 1.1 tron fs->file, fs->flags, fs->fpos,
932 1.1 tron fs->fsize, fs->block, fs->offset);
933 1.1 tron printf(" %d bufs:\n", fs->nbufs);
934 1.1 tron for (bn = fs->next; bn != &fs->buflist; bn = bn->next)
935 1.1 tron {
936 1.1 tron bp = bufnode_buf(bn);
937 1.1 tron printf("%x: blk %x, size %x \"",
938 1.1 tron bp, bp->block, bp->datasize);
939 1.1 tron for (s = bp->data; s < bp->data + 30; s++)
940 1.1 tron if (*s >= ' ' && *s < 0x7F)
941 1.1 tron printf("%c", *s);
942 1.1 tron else
943 1.1 tron printf(".");
944 1.1 tron printf("\"\n");
945 1.1 tron }
946 1.1 tron }
947 1.1 tron #endif
948