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