1 /* $OpenBSD: prtable.c,v 1.11 2008/08/04 18:42:09 millert Exp $ */ 2 /* $NetBSD: prtable.c,v 1.2 1995/03/21 12:14:42 cgd Exp $ */ 3 4 /*- 5 * Copyright (c) 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Barry Brachman. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)prtable.c 8.1 (Berkeley) 6/11/93 36 */ 37 38 #include <curses.h> 39 40 #include "extern.h" 41 42 #define NCOLS 5 43 44 static int get_maxlen(char **, int, int (*)(char **, int)); 45 46 /* 47 * Routine to print a table 48 * Modified from 'ls.c' mods (BJB/83) 49 * Arguments: 50 * base - address of first entry 51 * num - number of entries 52 * d_cols - number of columns to use if > 0, "best" size if == 0 53 * width - max line width if not zero 54 * prentry - address of the routine to call to print the string 55 * length - address of the routine to call to determine the length 56 * of string to be printed 57 * 58 * prtable and length are called with the address of the base and 59 * an index 60 */ 61 void 62 prtable(char **base, int num, int d_cols, int width, 63 void (*prentry)(char **, int), int (*length)(char **, int)) 64 { 65 int c, j; 66 int a, b, cols, loc, maxlen, nrows, z; 67 int col __unused, row; 68 69 if (num == 0) 70 return; 71 maxlen = get_maxlen(base, num, length) + 1; 72 if (d_cols > 0) 73 cols = d_cols; 74 else 75 cols = width / maxlen; 76 if (cols == 0) 77 cols = NCOLS; 78 nrows = (num - 1) / cols + 1; 79 for (a = 1; a <= nrows; a++) { 80 b = c = z = loc = 0; 81 for (j = 0; j < num; j++) { 82 c++; 83 if (c >= a + b) 84 break; 85 } 86 while (j < num) { 87 (*prentry)(base, j); 88 loc += (*length)(base, j); 89 z++; 90 b += nrows; 91 for (j++; j < num; j++) { 92 c++; 93 if (c >= a + b) 94 break; 95 } 96 if (j < num) { 97 while (loc < z * maxlen) { 98 addch(' '); 99 loc++; 100 } 101 } 102 } 103 getyx(stdscr, row, col); 104 move(row + 1, 0); 105 if (row + 1 == lastline && a != nrows) { 106 attron(A_REVERSE); 107 printw("--More--"); 108 attroff(A_REVERSE); 109 do { 110 j = inputch(); 111 } while (j != ' ' && j != 'q' && j != 'Q'); 112 if (j == 'q' || j == 'Q') { 113 move(row + 1, 0); 114 wclrtoeol(stdscr); 115 break; 116 } 117 move(LIST_LINE, LIST_COL); 118 wclrtobot(stdscr); 119 } 120 } 121 refresh(); 122 } 123 124 static int 125 get_maxlen(char **base, int num, int (*length)(char **, int)) 126 { 127 int i, len, max; 128 129 max = (*length)(base, 0); 130 for (i = 0; i < num; i++) { 131 if ((len = (*length)(base, i)) > max) 132 max = len; 133 } 134 return(max); 135 } 136