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