home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / more / position.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-03  |  4.4 KB  |  164 lines

  1. /*
  2.  * Copyright (c) 1988 Mark Nudleman
  3.  * Copyright (c) 1988 Regents of the University of California.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer.
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  * 3. All advertising materials mentioning features or use of this software
  15.  *    must display the following acknowledgement:
  16.  *    This product includes software developed by the University of
  17.  *    California, Berkeley and its contributors.
  18.  * 4. Neither the name of the University nor the names of its contributors
  19.  *    may be used to endorse or promote products derived from this software
  20.  *    without specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32.  * SUCH DAMAGE.
  33.  */
  34.  
  35. #ifndef lint
  36. static char sccsid[] = "@(#)position.c    5.7 (Berkeley) 6/1/90";
  37. #endif /* not lint */
  38.  
  39. /*
  40.  * Routines dealing with the "position" table.
  41.  * This is a table which tells the position (in the input file) of the
  42.  * first char on each currently displayed line.
  43.  *
  44.  * {{ The position table is scrolled by moving all the entries.
  45.  *    Would be better to have a circular table 
  46.  *    and just change a couple of pointers. }}
  47.  */
  48.  
  49. #include <sys/types.h>
  50. #include <less.h>
  51.  
  52. static off_t *table;        /* The position table */
  53. static int tablesize;
  54.  
  55. extern int sc_height;
  56.  
  57. /*
  58.  * Return the starting file position of a line displayed on the screen.
  59.  * The line may be specified as a line number relative to the top
  60.  * of the screen, but is usually one of these special cases:
  61.  *    the top (first) line on the screen
  62.  *    the second line on the screen
  63.  *    the bottom line on the screen
  64.  *    the line after the bottom line on the screen
  65.  */
  66. off_t
  67. position(where)
  68.     int where;
  69. {
  70.     switch (where)
  71.     {
  72.     case BOTTOM:
  73.         where = sc_height - 2;
  74.         break;
  75.     case BOTTOM_PLUS_ONE:
  76.         where = sc_height - 1;
  77.         break;
  78.     case MIDDLE:
  79.         where = sc_height / 2;
  80.     }
  81.     return (table[where]);
  82. }
  83.  
  84. /*
  85.  * Add a new file position to the bottom of the position table.
  86.  */
  87. add_forw_pos(pos)
  88.     off_t pos;
  89. {
  90.     register int i;
  91.  
  92.     /*
  93.      * Scroll the position table up.
  94.      */
  95.     for (i = 1;  i < sc_height;  i++)
  96.         table[i-1] = table[i];
  97.     table[sc_height - 1] = pos;
  98. }
  99.  
  100. /*
  101.  * Add a new file position to the top of the position table.
  102.  */
  103. add_back_pos(pos)
  104.     off_t pos;
  105. {
  106.     register int i;
  107.  
  108.     /*
  109.      * Scroll the position table down.
  110.      */
  111.     for (i = sc_height - 1;  i > 0;  i--)
  112.         table[i] = table[i-1];
  113.     table[0] = pos;
  114. }
  115.  
  116. copytable()
  117. {
  118.     register int a, b;
  119.  
  120.     for (a = 0; a < sc_height && table[a] == NULL_POSITION; a++);
  121.     for (b = 0; a < sc_height; a++, b++) {
  122.         table[b] = table[a];
  123.         table[a] = NULL_POSITION;
  124.     }
  125. }
  126.  
  127. /*
  128.  * Initialize the position table, done whenever we clear the screen.
  129.  */
  130. pos_clear()
  131. {
  132.     register int i;
  133.     extern char *malloc(), *realloc();
  134.  
  135.     if (table == 0) {
  136.         tablesize = sc_height > 25 ? sc_height : 25;
  137.         table = (off_t *)malloc(tablesize * sizeof *table);
  138.     } else if (sc_height >= tablesize) {
  139.         tablesize = sc_height;
  140.         table = (off_t *)realloc(table, tablesize * sizeof *table);
  141.     }
  142.  
  143.     for (i = 0;  i < sc_height;  i++)
  144.         table[i] = NULL_POSITION;
  145. }
  146.  
  147. /*
  148.  * See if the byte at a specified position is currently on the screen.
  149.  * Check the position table to see if the position falls within its range.
  150.  * Return the position table entry if found, -1 if not.
  151.  */
  152. onscreen(pos)
  153.     off_t pos;
  154. {
  155.     register int i;
  156.  
  157.     if (pos < table[0])
  158.         return (-1);
  159.     for (i = 1;  i < sc_height;  i++)
  160.         if (pos < table[i])
  161.             return (i-1);
  162.     return (-1);
  163. }
  164.