1 /* $NetBSD: prtable.c,v 1.8 2004/01/27 20:30:29 jsm Exp $ */ 2 3 /*- 4 * Copyright (c) 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Barry Brachman. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. 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 * @(#)prtable.c 8.1 (Berkeley) 6/11/93 35 */ 36 37 #include <sys/cdefs.h> 38 #ifndef lint 39 __RCSID("$NetBSD: prtable.c,v 1.8 2004/01/27 20:30:29 jsm Exp $"); 40 #endif /* not lint */ 41 42 #include <curses.h> 43 44 #include "extern.h" 45 46 #define NCOLS 5 47 48 static int get_maxlen(const char *const [], int, int (*)(const char *const *, int)); 49 50 /* 51 * Routine to print a table 52 * Modified from 'ls.c' mods (BJB/83) 53 * Arguments: 54 * base - address of first entry 55 * num - number of entries 56 * d_cols - number of columns to use if > 0, "best" size if == 0 57 * width - max line width if not zero 58 * prentry - address of the routine to call to print the string 59 * length - address of the routine to call to determine the length 60 * of string to be printed 61 * 62 * prtable and length are called with the address of the base and 63 * an index 64 */ 65 void 66 prtable(base, num, d_cols, width, prentry, length) 67 const char *const base[]; 68 int num, d_cols, width; 69 void (*prentry)(const char *const [], int); 70 int (*length)(const char *const [], int); 71 { 72 int c, j; 73 int a, b, cols, loc, maxlen, nrows, z; 74 int col, row; 75 76 if (num == 0) 77 return; 78 maxlen = get_maxlen(base, num, length) + 1; 79 if (d_cols > 0) 80 cols = d_cols; 81 else 82 cols = width / maxlen; 83 if (cols == 0) 84 cols = NCOLS; 85 nrows = (num - 1) / cols + 1; 86 for (a = 1; a <= nrows; a++) { 87 b = c = z = loc = 0; 88 for (j = 0; j < num; j++) { 89 c++; 90 if (c >= a + b) 91 break; 92 } 93 while (j < num) { 94 (*prentry)(base, j); 95 loc += (*length)(base, j); 96 z++; 97 b += nrows; 98 for (j++; j < num; j++) { 99 c++; 100 if (c >= a + b) 101 break; 102 } 103 if (j < num) { 104 while (loc < z * maxlen) { 105 addch(' '); 106 loc++; 107 } 108 } 109 } 110 getyx(stdscr, row, col); 111 move(row + 1, 0); 112 } 113 refresh(); 114 } 115 116 static int 117 get_maxlen(base, num, length) 118 const char *const base[]; 119 int num; 120 int (*length)(const char *const *, int); 121 { 122 int i, len, max; 123 124 max = (*length)(base, 0); 125 for (i = 0; i < num; i++) { 126 if ((len = (*length)(base, i)) > max) 127 max = len; 128 } 129 return(max); 130 } 131