1 /* $NetBSD: prtable.c,v 1.11 2021/05/02 12:50:43 rillig 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.11 2021/05/02 12:50:43 rillig 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
prtable(const char * const base[],int num,int d_cols,int width,void (* prentry)(const char * const[],int),int (* length)(const char * const[],int))66 prtable(const char *const base[], int num, int d_cols, int width,
67 void (*prentry)(const char *const [], int),
68 int (*length)(const char *const [], int))
69 {
70 int c, j;
71 int a, b, cols, loc, maxlen, nrows, z;
72 int col, row;
73
74 if (num == 0)
75 return;
76 maxlen = get_maxlen(base, num, length) + 1;
77 if (d_cols > 0)
78 cols = d_cols;
79 else
80 cols = width / maxlen;
81 if (cols == 0)
82 cols = NCOLS;
83 nrows = (num - 1) / cols + 1;
84 for (a = 1; a <= nrows; a++) {
85 b = c = z = loc = 0;
86 for (j = 0; j < num; j++) {
87 c++;
88 if (c >= a + b)
89 break;
90 }
91 while (j < num) {
92 (*prentry)(base, j);
93 loc += (*length)(base, j);
94 z++;
95 b += nrows;
96 for (j++; j < num; j++) {
97 c++;
98 if (c >= a + b)
99 break;
100 }
101 if (j < num) {
102 while (loc < z * maxlen) {
103 addch(' ');
104 loc++;
105 }
106 }
107 }
108 getyx(stdscr, row, col);
109 __USE(col);
110 move(row + 1, 0);
111 }
112 refresh();
113 }
114
115 static int
get_maxlen(const char * const base[],int num,int (* length)(const char * const *,int))116 get_maxlen(const char *const base[], int num,
117 int (*length)(const char *const *, int))
118 {
119 int i, len, max;
120
121 max = (*length)(base, 0);
122 for (i = 0; i < num; i++) {
123 if ((len = (*length)(base, i)) > max)
124 max = len;
125 }
126 return(max);
127 }
128