etc/services - sync with NetBSD-8
[minix.git] / external / bsd / less / dist / ch.c
blob5eaebba8991e350c18b2eb76439784c71880281e
1 /* $NetBSD: ch.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */
3 /*
4 * Copyright (C) 1984-2012 Mark Nudelman
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, see the README file.
14 * Low level character input from the input file.
15 * We use these special purpose routines which optimize moving
16 * both forward and backward from the current read pointer.
19 #include "less.h"
20 #if MSDOS_COMPILER==WIN32C
21 #include <errno.h>
22 #include <windows.h>
23 #endif
25 #if HAVE_STAT_INO
26 #include <sys/stat.h>
27 extern dev_t curr_dev;
28 extern ino_t curr_ino;
29 #endif
31 typedef POSITION BLOCKNUM;
33 public int ignore_eoi;
36 * Pool of buffers holding the most recently used blocks of the input file.
37 * The buffer pool is kept as a doubly-linked circular list,
38 * in order from most- to least-recently used.
39 * The circular list is anchored by the file state "thisfile".
41 struct bufnode {
42 struct bufnode *next, *prev;
43 struct bufnode *hnext, *hprev;
46 #define LBUFSIZE 8192
47 struct buf {
48 struct bufnode node;
49 BLOCKNUM block;
50 unsigned int datasize;
51 unsigned char data[LBUFSIZE];
53 #define bufnode_buf(bn) ((struct buf *) bn)
56 * The file state is maintained in a filestate structure.
57 * A pointer to the filestate is kept in the ifile structure.
59 #define BUFHASH_SIZE 64
60 struct filestate {
61 struct bufnode buflist;
62 struct bufnode hashtbl[BUFHASH_SIZE];
63 int file;
64 int flags;
65 POSITION fpos;
66 int nbufs;
67 BLOCKNUM block;
68 unsigned int offset;
69 POSITION fsize;
72 #define ch_bufhead thisfile->buflist.next
73 #define ch_buftail thisfile->buflist.prev
74 #define ch_nbufs thisfile->nbufs
75 #define ch_block thisfile->block
76 #define ch_offset thisfile->offset
77 #define ch_fpos thisfile->fpos
78 #define ch_fsize thisfile->fsize
79 #define ch_flags thisfile->flags
80 #define ch_file thisfile->file
82 #define END_OF_CHAIN (&thisfile->buflist)
83 #define END_OF_HCHAIN(h) (&thisfile->hashtbl[h])
84 #define BUFHASH(blk) ((blk) & (BUFHASH_SIZE-1))
87 * Macros to manipulate the list of buffers in thisfile->buflist.
89 #define FOR_BUFS(bn) \
90 for (bn = ch_bufhead; bn != END_OF_CHAIN; bn = bn->next)
92 #define BUF_RM(bn) \
93 (bn)->next->prev = (bn)->prev; \
94 (bn)->prev->next = (bn)->next;
96 #define BUF_INS_HEAD(bn) \
97 (bn)->next = ch_bufhead; \
98 (bn)->prev = END_OF_CHAIN; \
99 ch_bufhead->prev = (bn); \
100 ch_bufhead = (bn);
102 #define BUF_INS_TAIL(bn) \
103 (bn)->next = END_OF_CHAIN; \
104 (bn)->prev = ch_buftail; \
105 ch_buftail->next = (bn); \
106 ch_buftail = (bn);
109 * Macros to manipulate the list of buffers in thisfile->hashtbl[n].
111 #define FOR_BUFS_IN_CHAIN(h,bn) \
112 for (bn = thisfile->hashtbl[h].hnext; \
113 bn != END_OF_HCHAIN(h); bn = bn->hnext)
115 #define BUF_HASH_RM(bn) \
116 (bn)->hnext->hprev = (bn)->hprev; \
117 (bn)->hprev->hnext = (bn)->hnext;
119 #define BUF_HASH_INS(bn,h) \
120 (bn)->hnext = thisfile->hashtbl[h].hnext; \
121 (bn)->hprev = END_OF_HCHAIN(h); \
122 thisfile->hashtbl[h].hnext->hprev = (bn); \
123 thisfile->hashtbl[h].hnext = (bn);
125 static struct filestate *thisfile;
126 static int ch_ungotchar = -1;
127 static int maxbufs = -1;
129 extern int autobuf;
130 extern int sigs;
131 extern int secure;
132 extern int screen_trashed;
133 extern int follow_mode;
134 extern constant char helpdata[];
135 extern constant int size_helpdata;
136 extern IFILE curr_ifile;
137 #if LOGFILE
138 extern int logfile;
139 extern char *namelogfile;
140 #endif
142 static int ch_addbuf __P((void));
143 static int buffered __P((BLOCKNUM));
144 static void ch_delbufs __P((void));
148 * Get the character pointed to by the read pointer.
151 ch_get()
153 register struct buf *bp;
154 register struct bufnode *bn;
155 register int n;
156 register int slept;
157 register int h;
158 POSITION pos;
159 POSITION len;
161 if (thisfile == NULL)
162 return (EOI);
165 * Quick check for the common case where
166 * the desired char is in the head buffer.
168 if (ch_bufhead != END_OF_CHAIN)
170 bp = bufnode_buf(ch_bufhead);
171 if (ch_block == bp->block && ch_offset < bp->datasize)
172 return bp->data[ch_offset];
175 slept = FALSE;
178 * Look for a buffer holding the desired block.
180 h = BUFHASH(ch_block);
181 FOR_BUFS_IN_CHAIN(h, bn)
183 bp = bufnode_buf(bn);
184 if (bp->block == ch_block)
186 if (ch_offset >= bp->datasize)
188 * Need more data in this buffer.
190 break;
191 goto found;
194 if (bn == END_OF_HCHAIN(h))
197 * Block is not in a buffer.
198 * Take the least recently used buffer
199 * and read the desired block into it.
200 * If the LRU buffer has data in it,
201 * then maybe allocate a new buffer.
203 if (ch_buftail == END_OF_CHAIN ||
204 bufnode_buf(ch_buftail)->block != -1)
207 * There is no empty buffer to use.
208 * Allocate a new buffer if:
209 * 1. We can't seek on this file and -b is not in effect; or
210 * 2. We haven't allocated the max buffers for this file yet.
212 if ((autobuf && !(ch_flags & CH_CANSEEK)) ||
213 (maxbufs < 0 || ch_nbufs < maxbufs))
214 if (ch_addbuf())
216 * Allocation failed: turn off autobuf.
218 autobuf = OPT_OFF;
220 bn = ch_buftail;
221 bp = bufnode_buf(bn);
222 BUF_HASH_RM(bn); /* Remove from old hash chain. */
223 bp->block = ch_block;
224 bp->datasize = 0;
225 BUF_HASH_INS(bn, h); /* Insert into new hash chain. */
228 read_more:
229 pos = (ch_block * LBUFSIZE) + bp->datasize;
230 if ((len = ch_length()) != NULL_POSITION && pos >= len)
232 * At end of file.
234 return (EOI);
236 if (pos != ch_fpos)
239 * Not at the correct position: must seek.
240 * If input is a pipe, we're in trouble (can't seek on a pipe).
241 * Some data has been lost: just return "?".
243 if (!(ch_flags & CH_CANSEEK))
244 return ('?');
245 if (lseek(ch_file, (off_t)pos, SEEK_SET) == BAD_LSEEK)
247 error("seek error", NULL_PARG);
248 clear_eol();
249 return (EOI);
251 ch_fpos = pos;
255 * Read the block.
256 * If we read less than a full block, that's ok.
257 * We use partial block and pick up the rest next time.
259 if (ch_ungotchar != -1)
261 bp->data[bp->datasize] = ch_ungotchar;
262 n = 1;
263 ch_ungotchar = -1;
264 } else if (ch_flags & CH_HELPFILE)
266 bp->data[bp->datasize] = helpdata[ch_fpos];
267 n = 1;
268 } else
270 n = iread(ch_file, &bp->data[bp->datasize],
271 (unsigned int)(LBUFSIZE - bp->datasize));
274 if (n == READ_INTR)
275 return (EOI);
276 if (n < 0)
278 #if MSDOS_COMPILER==WIN32C
279 if (errno != EPIPE)
280 #endif
282 error("read error", NULL_PARG);
283 clear_eol();
285 n = 0;
288 #if LOGFILE
290 * If we have a log file, write the new data to it.
292 if (!secure && logfile >= 0 && n > 0)
293 write(logfile, (char *) &bp->data[bp->datasize], n);
294 #endif
296 ch_fpos += n;
297 bp->datasize += n;
300 * If we have read to end of file, set ch_fsize to indicate
301 * the position of the end of file.
303 if (n == 0)
305 ch_fsize = pos;
306 if (ignore_eoi)
309 * We are ignoring EOF.
310 * Wait a while, then try again.
312 if (!slept)
314 PARG parg;
315 parg.p_string = wait_message();
316 ierror("%s", &parg);
318 #if !MSDOS_COMPILER
319 sleep(1);
320 #else
321 #if MSDOS_COMPILER==WIN32C
322 Sleep(1000);
323 #endif
324 #endif
325 slept = TRUE;
327 #if HAVE_STAT_INO
328 if (follow_mode == FOLLOW_NAME)
330 /* See whether the file's i-number has changed.
331 * If so, force the file to be closed and
332 * reopened. */
333 struct stat st;
334 int r = stat(get_filename(curr_ifile), &st);
335 if (r == 0 && (st.st_ino != curr_ino ||
336 st.st_dev != curr_dev))
338 /* screen_trashed=2 causes
339 * make_display to reopen the file. */
340 screen_trashed = 2;
341 return (EOI);
344 #endif
346 if (sigs)
347 return (EOI);
350 found:
351 if (ch_bufhead != bn)
354 * Move the buffer to the head of the buffer chain.
355 * This orders the buffer chain, most- to least-recently used.
357 BUF_RM(bn);
358 BUF_INS_HEAD(bn);
361 * Move to head of hash chain too.
363 BUF_HASH_RM(bn);
364 BUF_HASH_INS(bn, h);
367 if (ch_offset >= bp->datasize)
369 * After all that, we still don't have enough data.
370 * Go back and try again.
372 goto read_more;
374 return (bp->data[ch_offset]);
378 * ch_ungetchar is a rather kludgy and limited way to push
379 * a single char onto an input file descriptor.
381 public void
382 ch_ungetchar(c)
383 int c;
385 if (c != -1 && ch_ungotchar != -1)
386 error("ch_ungetchar overrun", NULL_PARG);
387 ch_ungotchar = c;
390 #if LOGFILE
392 * Close the logfile.
393 * If we haven't read all of standard input into it, do that now.
395 public void
396 end_logfile()
398 static int tried = FALSE;
400 if (logfile < 0)
401 return;
402 if (!tried && ch_fsize == NULL_POSITION)
404 tried = TRUE;
405 ierror("Finishing logfile", NULL_PARG);
406 while (ch_forw_get() != EOI)
407 if (ABORT_SIGS())
408 break;
410 close(logfile);
411 logfile = -1;
412 namelogfile = NULL;
416 * Start a log file AFTER less has already been running.
417 * Invoked from the - command; see toggle_option().
418 * Write all the existing buffered data to the log file.
420 public void
421 sync_logfile()
423 register struct buf *bp;
424 register struct bufnode *bn;
425 int warned = FALSE;
426 BLOCKNUM block;
427 BLOCKNUM nblocks;
429 nblocks = (ch_fpos + LBUFSIZE - 1) / LBUFSIZE;
430 for (block = 0; block < nblocks; block++)
432 int wrote = FALSE;
433 FOR_BUFS(bn)
435 bp = bufnode_buf(bn);
436 if (bp->block == block)
438 write(logfile, (char *) bp->data, bp->datasize);
439 wrote = TRUE;
440 break;
443 if (!wrote && !warned)
445 error("Warning: log file is incomplete",
446 NULL_PARG);
447 warned = TRUE;
452 #endif
455 * Determine if a specific block is currently in one of the buffers.
457 static int
458 buffered(block)
459 BLOCKNUM block;
461 register struct buf *bp;
462 register struct bufnode *bn;
463 register int h;
465 h = BUFHASH(block);
466 FOR_BUFS_IN_CHAIN(h, bn)
468 bp = bufnode_buf(bn);
469 if (bp->block == block)
470 return (TRUE);
472 return (FALSE);
476 * Seek to a specified position in the file.
477 * Return 0 if successful, non-zero if can't seek there.
479 public int
480 ch_seek(pos)
481 register POSITION pos;
483 BLOCKNUM new_block;
484 POSITION len;
486 if (thisfile == NULL)
487 return (0);
489 len = ch_length();
490 if (pos < ch_zero() || (len != NULL_POSITION && pos > len))
491 return (1);
493 new_block = pos / LBUFSIZE;
494 if (!(ch_flags & CH_CANSEEK) && pos != ch_fpos && !buffered(new_block))
496 if (ch_fpos > pos)
497 return (1);
498 while (ch_fpos < pos)
500 if (ch_forw_get() == EOI)
501 return (1);
502 if (ABORT_SIGS())
503 return (1);
505 return (0);
508 * Set read pointer.
510 ch_block = new_block;
511 ch_offset = pos % LBUFSIZE;
512 return (0);
516 * Seek to the end of the file.
518 public int
519 ch_end_seek()
521 POSITION len;
523 if (thisfile == NULL)
524 return (0);
526 if (ch_flags & CH_CANSEEK)
527 ch_fsize = filesize(ch_file);
529 len = ch_length();
530 if (len != NULL_POSITION)
531 return (ch_seek(len));
534 * Do it the slow way: read till end of data.
536 while (ch_forw_get() != EOI)
537 if (ABORT_SIGS())
538 return (1);
539 return (0);
543 * Seek to the beginning of the file, or as close to it as we can get.
544 * We may not be able to seek there if input is a pipe and the
545 * beginning of the pipe is no longer buffered.
547 public int
548 ch_beg_seek()
550 register struct bufnode *bn;
551 register struct bufnode *firstbn;
554 * Try a plain ch_seek first.
556 if (ch_seek(ch_zero()) == 0)
557 return (0);
560 * Can't get to position 0.
561 * Look thru the buffers for the one closest to position 0.
563 firstbn = ch_bufhead;
564 if (firstbn == END_OF_CHAIN)
565 return (1);
566 FOR_BUFS(bn)
568 if (bufnode_buf(bn)->block < bufnode_buf(firstbn)->block)
569 firstbn = bn;
571 ch_block = bufnode_buf(firstbn)->block;
572 ch_offset = 0;
573 return (0);
577 * Return the length of the file, if known.
579 public POSITION
580 ch_length()
582 if (thisfile == NULL)
583 return (NULL_POSITION);
584 if (ignore_eoi)
585 return (NULL_POSITION);
586 if (ch_flags & CH_HELPFILE)
587 return (size_helpdata);
588 if (ch_flags & CH_NODATA)
589 return (0);
590 return (ch_fsize);
594 * Return the current position in the file.
596 public POSITION
597 ch_tell()
599 if (thisfile == NULL)
600 return (NULL_POSITION);
601 return (ch_block * LBUFSIZE) + ch_offset;
605 * Get the current char and post-increment the read pointer.
607 public int
608 ch_forw_get()
610 register int c;
612 if (thisfile == NULL)
613 return (EOI);
614 c = ch_get();
615 if (c == EOI)
616 return (EOI);
617 if (ch_offset < LBUFSIZE-1)
618 ch_offset++;
619 else
621 ch_block ++;
622 ch_offset = 0;
624 return (c);
628 * Pre-decrement the read pointer and get the new current char.
630 public int
631 ch_back_get()
633 if (thisfile == NULL)
634 return (EOI);
635 if (ch_offset > 0)
636 ch_offset --;
637 else
639 if (ch_block <= 0)
640 return (EOI);
641 if (!(ch_flags & CH_CANSEEK) && !buffered(ch_block-1))
642 return (EOI);
643 ch_block--;
644 ch_offset = LBUFSIZE-1;
646 return (ch_get());
650 * Set max amount of buffer space.
651 * bufspace is in units of 1024 bytes. -1 mean no limit.
653 public void
654 ch_setbufspace(bufspace)
655 int bufspace;
657 if (bufspace < 0)
658 maxbufs = -1;
659 else
661 maxbufs = ((bufspace * 1024) + LBUFSIZE-1) / LBUFSIZE;
662 if (maxbufs < 1)
663 maxbufs = 1;
668 * Flush (discard) any saved file state, including buffer contents.
670 public void
671 ch_flush()
673 register struct bufnode *bn;
675 if (thisfile == NULL)
676 return;
678 if (!(ch_flags & CH_CANSEEK))
681 * If input is a pipe, we don't flush buffer contents,
682 * since the contents can't be recovered.
684 ch_fsize = NULL_POSITION;
685 return;
689 * Initialize all the buffers.
691 FOR_BUFS(bn)
693 bufnode_buf(bn)->block = -1;
697 * Figure out the size of the file, if we can.
699 ch_fsize = filesize(ch_file);
702 * Seek to a known position: the beginning of the file.
704 ch_fpos = 0;
705 ch_block = 0; /* ch_fpos / LBUFSIZE; */
706 ch_offset = 0; /* ch_fpos % LBUFSIZE; */
708 #if 1
710 * This is a kludge to workaround a Linux kernel bug: files in
711 * /proc have a size of 0 according to fstat() but have readable
712 * data. They are sometimes, but not always, seekable.
713 * Force them to be non-seekable here.
715 if (ch_fsize == 0)
717 ch_fsize = NULL_POSITION;
718 ch_flags &= ~CH_CANSEEK;
720 #endif
722 if (lseek(ch_file, (off_t)0, SEEK_SET) == BAD_LSEEK)
725 * Warning only; even if the seek fails for some reason,
726 * there's a good chance we're at the beginning anyway.
727 * {{ I think this is bogus reasoning. }}
729 error("seek error to 0", NULL_PARG);
734 * Allocate a new buffer.
735 * The buffer is added to the tail of the buffer chain.
737 static int
738 ch_addbuf()
740 register struct buf *bp;
741 register struct bufnode *bn;
744 * Allocate and initialize a new buffer and link it
745 * onto the tail of the buffer list.
747 bp = (struct buf *) calloc(1, sizeof(struct buf));
748 if (bp == NULL)
749 return (1);
750 ch_nbufs++;
751 bp->block = -1;
752 bn = &bp->node;
754 BUF_INS_TAIL(bn);
755 BUF_HASH_INS(bn, 0);
756 return (0);
762 static void
763 init_hashtbl()
765 register int h;
767 for (h = 0; h < BUFHASH_SIZE; h++)
769 thisfile->hashtbl[h].hnext = END_OF_HCHAIN(h);
770 thisfile->hashtbl[h].hprev = END_OF_HCHAIN(h);
775 * Delete all buffers for this file.
777 static void
778 ch_delbufs()
780 register struct bufnode *bn;
782 while (ch_bufhead != END_OF_CHAIN)
784 bn = ch_bufhead;
785 BUF_RM(bn);
786 free(bufnode_buf(bn));
788 ch_nbufs = 0;
789 init_hashtbl();
793 * Is it possible to seek on a file descriptor?
795 public int
796 seekable(f)
797 int f;
799 #if MSDOS_COMPILER
800 extern int fd0;
801 if (f == fd0 && !isatty(fd0))
804 * In MS-DOS, pipes are seekable. Check for
805 * standard input, and pretend it is not seekable.
807 return (0);
809 #endif
810 return (lseek(f, (off_t)1, SEEK_SET) != BAD_LSEEK);
814 * Force EOF to be at the current read position.
815 * This is used after an ignore_eof read, during which the EOF may change.
817 public void
818 ch_set_eof()
820 ch_fsize = ch_fpos;
825 * Initialize file state for a new file.
827 public void
828 ch_init(f, flags)
829 int f;
830 int flags;
833 * See if we already have a filestate for this file.
835 thisfile = (struct filestate *) get_filestate(curr_ifile);
836 if (thisfile == NULL)
839 * Allocate and initialize a new filestate.
841 thisfile = (struct filestate *)
842 calloc(1, sizeof(struct filestate));
843 thisfile->buflist.next = thisfile->buflist.prev = END_OF_CHAIN;
844 thisfile->nbufs = 0;
845 thisfile->flags = 0;
846 thisfile->fpos = 0;
847 thisfile->block = 0;
848 thisfile->offset = 0;
849 thisfile->file = -1;
850 thisfile->fsize = NULL_POSITION;
851 ch_flags = flags;
852 init_hashtbl();
854 * Try to seek; set CH_CANSEEK if it works.
856 if ((flags & CH_CANSEEK) && !seekable(f))
857 ch_flags &= ~CH_CANSEEK;
858 set_filestate(curr_ifile, (void *) thisfile);
860 if (thisfile->file == -1)
861 thisfile->file = f;
862 ch_flush();
866 * Close a filestate.
868 public void
869 ch_close()
871 int keepstate = FALSE;
873 if (thisfile == NULL)
874 return;
876 if (ch_flags & (CH_CANSEEK|CH_POPENED|CH_HELPFILE))
879 * We can seek or re-open, so we don't need to keep buffers.
881 ch_delbufs();
882 } else
883 keepstate = TRUE;
884 if (!(ch_flags & CH_KEEPOPEN))
887 * We don't need to keep the file descriptor open
888 * (because we can re-open it.)
889 * But don't really close it if it was opened via popen(),
890 * because pclose() wants to close it.
892 if (!(ch_flags & (CH_POPENED|CH_HELPFILE)))
893 close(ch_file);
894 ch_file = -1;
895 } else
896 keepstate = TRUE;
897 if (!keepstate)
900 * We don't even need to keep the filestate structure.
902 free(thisfile);
903 thisfile = NULL;
904 set_filestate(curr_ifile, (void *) NULL);
909 * Return ch_flags for the current file.
911 public int
912 ch_getflags()
914 if (thisfile == NULL)
915 return (0);
916 return (ch_flags);
919 #if 0
920 public void
921 ch_dump(struct filestate *fs)
923 struct buf *bp;
924 struct bufnode *bn;
925 unsigned char *s;
927 if (fs == NULL)
929 printf(" --no filestate\n");
930 return;
932 printf(" file %d, flags %x, fpos %x, fsize %x, blk/off %x/%x\n",
933 fs->file, fs->flags, fs->fpos,
934 fs->fsize, fs->block, fs->offset);
935 printf(" %d bufs:\n", fs->nbufs);
936 for (bn = fs->next; bn != &fs->buflist; bn = bn->next)
938 bp = bufnode_buf(bn);
939 printf("%x: blk %x, size %x \"",
940 bp, bp->block, bp->datasize);
941 for (s = bp->data; s < bp->data + 30; s++)
942 if (*s >= ' ' && *s < 0x7F)
943 printf("%c", *s);
944 else
945 printf(".");
946 printf("\"\n");
949 #endif