xref: /onnv-gate/usr/src/lib/libcurses/screen/print.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #include <stdlib.h>
43*0Sstevel@tonic-gate #include <string.h>
44*0Sstevel@tonic-gate #include <sys/types.h>
45*0Sstevel@tonic-gate #include "curses_inc.h"
46*0Sstevel@tonic-gate #include "print.h"
47*0Sstevel@tonic-gate #include <signal.h>   /* use this file to determine if this is SVR4.0 system */
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #ifdef SIGSTOP	/* SVR4.0 and beyond */
50*0Sstevel@tonic-gate #define	_ULIBTI	"/usr/share/lib/terminfo"
51*0Sstevel@tonic-gate #else
52*0Sstevel@tonic-gate #define	_ULIBTI	"/usr/lib/terminfo"
53*0Sstevel@tonic-gate #endif
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate char *progname;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate /* global variables */
58*0Sstevel@tonic-gate static enum printtypes printing = pr_none;
59*0Sstevel@tonic-gate static int onecolumn = 0;		/* print a single column */
60*0Sstevel@tonic-gate static int width = 60;			/* width of multi-column printing */
61*0Sstevel@tonic-gate static int restrictterm = 1;		/* restrict termcap names */
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate /* local variables */
64*0Sstevel@tonic-gate static int printed = 0;
65*0Sstevel@tonic-gate static size_t caplen = 0;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate void
68*0Sstevel@tonic-gate pr_init(enum printtypes type)
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate 	printing = type;
71*0Sstevel@tonic-gate }
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate void
74*0Sstevel@tonic-gate pr_onecolumn(int onoff)
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate 	onecolumn = onoff;
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate void
80*0Sstevel@tonic-gate pr_width(int nwidth)
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate 	if (nwidth > 0)
83*0Sstevel@tonic-gate 		width = nwidth;
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate void
87*0Sstevel@tonic-gate pr_caprestrict(int onoff)
88*0Sstevel@tonic-gate {
89*0Sstevel@tonic-gate 	restrictterm = onoff;
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate static char capbools[] =
93*0Sstevel@tonic-gate 	"ambsbwdadbeoeshchshzinkmmimsncnsosptulxbxnxoxsxt";
94*0Sstevel@tonic-gate static int ncapbools = sizeof (capbools) / sizeof (capbools[0]);
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate static char capnums[] =
97*0Sstevel@tonic-gate 	"codBdCdFdNdTknlipbsgug";
98*0Sstevel@tonic-gate static int ncapnums = sizeof (capnums) / sizeof (capnums[0]);
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate static char capstrs[] =
101*0Sstevel@tonic-gate 	"ALDCDLDOICLERISFSRUPaealasbcbtcdcechclcmcsctcvdcdldmdsedeifshoi1i2i"
102*0Sstevel@tonic-gate 	    "cifimipisk0k1k2k3k4k5k6k7k8k9kbkdkekhklkokrkskul0l1l2l3l4l5l6l7l"
103*0Sstevel@tonic-gate 	    "8l9ndnlpcr1r2r3rcrfrpscsesosrsttetitsucueupusvbvevivs";
104*0Sstevel@tonic-gate static int ncapstrs = sizeof (capstrs) / sizeof (capstrs[0]);
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate static int
107*0Sstevel@tonic-gate findcapname(char *capname, char *caplist, int listsize)
108*0Sstevel@tonic-gate {
109*0Sstevel@tonic-gate 	int low = 0, mid, high = listsize - 2;
110*0Sstevel@tonic-gate 	while (low <= high) {
111*0Sstevel@tonic-gate 		mid = (low + high) / 4 * 2;
112*0Sstevel@tonic-gate 		if (capname[0] == caplist[mid]) {
113*0Sstevel@tonic-gate 			if (capname[1] == caplist[mid + 1])
114*0Sstevel@tonic-gate 				return (1);
115*0Sstevel@tonic-gate 			else if (capname[1] < caplist[mid + 1])
116*0Sstevel@tonic-gate 				high = mid - 2;
117*0Sstevel@tonic-gate 			else
118*0Sstevel@tonic-gate 				low = mid + 2;
119*0Sstevel@tonic-gate 		} else if (capname[0] < caplist[mid])
120*0Sstevel@tonic-gate 			high = mid - 2;
121*0Sstevel@tonic-gate 		else
122*0Sstevel@tonic-gate 			low = mid + 2;
123*0Sstevel@tonic-gate 	}
124*0Sstevel@tonic-gate 	return (0);
125*0Sstevel@tonic-gate /*
126*0Sstevel@tonic-gate  *	for (; *caplist; caplist += 2)
127*0Sstevel@tonic-gate  *		if (caplist[0] == capname[0] && caplist[1] == capname[1])
128*0Sstevel@tonic-gate  *			return (1);
129*0Sstevel@tonic-gate  *	return (0);
130*0Sstevel@tonic-gate  */
131*0Sstevel@tonic-gate }
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate /*
134*0Sstevel@tonic-gate  *  Print out the first line of an entry.
135*0Sstevel@tonic-gate  */
136*0Sstevel@tonic-gate void
137*0Sstevel@tonic-gate pr_heading(char *term, char *synonyms)
138*0Sstevel@tonic-gate {
139*0Sstevel@tonic-gate 	int	do_print = 0;	/* Can we print the path of the file ? */
140*0Sstevel@tonic-gate 	char	buffer[512];	/* Holds search pathname */
141*0Sstevel@tonic-gate 	FILE	*work_fp;	/* Used to try and open the files */
142*0Sstevel@tonic-gate 	char	tail[4];	/* Used for terminfo pathname suffix */
143*0Sstevel@tonic-gate 	char	*terminfo;	/* The value of $TERMINFO */
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	/*
147*0Sstevel@tonic-gate 	 *	Try to obtain $TERMINFO
148*0Sstevel@tonic-gate 	 */
149*0Sstevel@tonic-gate 	terminfo = getenv("TERMINFO");
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	if (term == (char *)0)
152*0Sstevel@tonic-gate 		term = "";
153*0Sstevel@tonic-gate 	/*
154*0Sstevel@tonic-gate 	 *	Build the suffix for this device
155*0Sstevel@tonic-gate 	 */
156*0Sstevel@tonic-gate 	tail[0] = '/';
157*0Sstevel@tonic-gate 	tail[1] = *term;
158*0Sstevel@tonic-gate 	tail[2] = '/';
159*0Sstevel@tonic-gate 	tail[3] = '\0';
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	/*
162*0Sstevel@tonic-gate 	 *	If we have it - use it, otherwise use /usr/share/lib/terminfo
163*0Sstevel@tonic-gate 	 *	as base directory
164*0Sstevel@tonic-gate 	 */
165*0Sstevel@tonic-gate 	if (terminfo != NULL)
166*0Sstevel@tonic-gate 		(void) sprintf(buffer, "%s%s%s", terminfo, tail, term);
167*0Sstevel@tonic-gate 	else
168*0Sstevel@tonic-gate 		(void) sprintf(buffer, "%s%s%s", _ULIBTI, tail, term);
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	/*
171*0Sstevel@tonic-gate 	 *	Attempt to open the file.
172*0Sstevel@tonic-gate 	 */
173*0Sstevel@tonic-gate 	if ((work_fp = fopen(buffer, "r")) == NULL) {
174*0Sstevel@tonic-gate 		/*
175*0Sstevel@tonic-gate 		 * Open failed. If we were looking in /usr/share/lib/terminfo
176*0Sstevel@tonic-gate 		 *	we are done, otherwise look there next.
177*0Sstevel@tonic-gate 		 */
178*0Sstevel@tonic-gate 		if (strncmp(buffer, _ULIBTI, strlen(_ULIBTI)) == 0) {
179*0Sstevel@tonic-gate 				/*
180*0Sstevel@tonic-gate 				 * We are done. Not in /usr/share/lib/terminfo,
181*0Sstevel@tonic-gate 				 *	and $TERMINFO is not set.
182*0Sstevel@tonic-gate 				 */
183*0Sstevel@tonic-gate 				(void) fprintf(stderr, "Error: Term \"%s\" not "
184*0Sstevel@tonic-gate 				    "found in %s\n", term, _ULIBTI);
185*0Sstevel@tonic-gate 		} else {
186*0Sstevel@tonic-gate 			/*
187*0Sstevel@tonic-gate 			 * Check /usr/share/lib/terminfo last. If this fails,
188*0Sstevel@tonic-gate 			 * all hope is lost as we know it is not in $TERMINFO.
189*0Sstevel@tonic-gate 			 */
190*0Sstevel@tonic-gate 			(void) sprintf(buffer, "%s%s%s", _ULIBTI, tail, term);
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 			if ((work_fp = fopen(buffer, "r")) == NULL) {
193*0Sstevel@tonic-gate 				/*
194*0Sstevel@tonic-gate 				 *	All hope is lost
195*0Sstevel@tonic-gate 				 */
196*0Sstevel@tonic-gate 				(void) fprintf(stderr, "Error: Term \"%s\" not "
197*0Sstevel@tonic-gate 				    "found in %s or %s\n", term, _ULIBTI,
198*0Sstevel@tonic-gate 				    getenv("TERMINFO"));
199*0Sstevel@tonic-gate 			} else do_print = 1;
200*0Sstevel@tonic-gate 		}
201*0Sstevel@tonic-gate 	} else do_print = 1;
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	/*
204*0Sstevel@tonic-gate 	 *	If we found it - print the comment(after closing the file)
205*0Sstevel@tonic-gate 	 */
206*0Sstevel@tonic-gate 	if (do_print && *term) {
207*0Sstevel@tonic-gate 		(void) fclose(work_fp);
208*0Sstevel@tonic-gate 		(void) printf("#	Reconstructed via infocmp from file: "
209*0Sstevel@tonic-gate 		    "%s\n", buffer);
210*0Sstevel@tonic-gate 	}
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	switch ((int)printing) {
213*0Sstevel@tonic-gate 		case (int)pr_terminfo:
214*0Sstevel@tonic-gate 			(void) printf("%s,\n", synonyms);
215*0Sstevel@tonic-gate 			break;
216*0Sstevel@tonic-gate 		case (int)pr_cap:
217*0Sstevel@tonic-gate 			(void) printf("%s:\\\n", synonyms);
218*0Sstevel@tonic-gate 			caplen = strlen(synonyms) + 1;
219*0Sstevel@tonic-gate 			break;
220*0Sstevel@tonic-gate 		case (int)pr_longnames:
221*0Sstevel@tonic-gate 			(void) printf("Terminal type %s\n", term);
222*0Sstevel@tonic-gate 			(void) printf("  %s\n", synonyms);
223*0Sstevel@tonic-gate 			break;
224*0Sstevel@tonic-gate 	}
225*0Sstevel@tonic-gate }
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate void
228*0Sstevel@tonic-gate pr_bheading(void)
229*0Sstevel@tonic-gate {
230*0Sstevel@tonic-gate 	if (printing == pr_longnames)
231*0Sstevel@tonic-gate 		(void) printf("flags\n");
232*0Sstevel@tonic-gate 	printed = 0;
233*0Sstevel@tonic-gate }
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate void
236*0Sstevel@tonic-gate pr_boolean(char *infoname, char *capname, char *fullname, int value)
237*0Sstevel@tonic-gate {
238*0Sstevel@tonic-gate 	int	vlen;
239*0Sstevel@tonic-gate 	size_t	nlen;
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	if (printing == pr_cap && restrictterm &&
242*0Sstevel@tonic-gate 	    !findcapname(capname, capbools, ncapbools))
243*0Sstevel@tonic-gate 		return;
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	if (onecolumn) {
246*0Sstevel@tonic-gate 		if (value < 0)
247*0Sstevel@tonic-gate 			switch ((int)printing) {
248*0Sstevel@tonic-gate 				case (int)pr_terminfo:
249*0Sstevel@tonic-gate 					(void) printf("\t%s@,\n", infoname);
250*0Sstevel@tonic-gate 					break;
251*0Sstevel@tonic-gate 				case (int)pr_cap:
252*0Sstevel@tonic-gate 					(void) printf("\t:%s@:\\\n", capname);
253*0Sstevel@tonic-gate 					caplen += 4 + strlen(capname);
254*0Sstevel@tonic-gate 					break;
255*0Sstevel@tonic-gate 				case (int)pr_longnames:
256*0Sstevel@tonic-gate 					(void) printf("  %s@\n", fullname);
257*0Sstevel@tonic-gate 			}
258*0Sstevel@tonic-gate 		else
259*0Sstevel@tonic-gate 			switch ((int)printing) {
260*0Sstevel@tonic-gate 				case (int)pr_terminfo:
261*0Sstevel@tonic-gate 					(void) printf("\t%s,\n", infoname);
262*0Sstevel@tonic-gate 					break;
263*0Sstevel@tonic-gate 				case (int)pr_cap:
264*0Sstevel@tonic-gate 					(void) printf("\t:%s:\\\n", capname);
265*0Sstevel@tonic-gate 					caplen += 3 + strlen(capname);
266*0Sstevel@tonic-gate 					break;
267*0Sstevel@tonic-gate 				case (int)pr_longnames:
268*0Sstevel@tonic-gate 					(void) printf("  %s\n", fullname);
269*0Sstevel@tonic-gate 			}
270*0Sstevel@tonic-gate 	} else {
271*0Sstevel@tonic-gate 		switch ((int)printing) {
272*0Sstevel@tonic-gate 			case (int)pr_terminfo:	nlen = strlen(infoname);
273*0Sstevel@tonic-gate 						break;
274*0Sstevel@tonic-gate 			case (int)pr_cap:	nlen = strlen(capname);
275*0Sstevel@tonic-gate 						break;
276*0Sstevel@tonic-gate 			case (int)pr_longnames:
277*0Sstevel@tonic-gate 						nlen = strlen(fullname);
278*0Sstevel@tonic-gate 						break;
279*0Sstevel@tonic-gate 		}
280*0Sstevel@tonic-gate 		vlen = (value < 0) ? 1 : 0;
281*0Sstevel@tonic-gate 		if ((printed > 0) && (printed + nlen + vlen + 1 > width)) {
282*0Sstevel@tonic-gate 			switch ((int)printing) {
283*0Sstevel@tonic-gate 				case (int)pr_terminfo:
284*0Sstevel@tonic-gate 				case (int)pr_longnames:
285*0Sstevel@tonic-gate 						(void) printf("\n");
286*0Sstevel@tonic-gate 						break;
287*0Sstevel@tonic-gate 				case (int)pr_cap:
288*0Sstevel@tonic-gate 						(void) printf(":\\\n");
289*0Sstevel@tonic-gate 						caplen += 1;
290*0Sstevel@tonic-gate 			}
291*0Sstevel@tonic-gate 			printed = 0;
292*0Sstevel@tonic-gate 		}
293*0Sstevel@tonic-gate 		if (printed == 0) {
294*0Sstevel@tonic-gate 			switch ((int)printing) {
295*0Sstevel@tonic-gate 				case (int)pr_terminfo:
296*0Sstevel@tonic-gate 					(void) printf("\t");
297*0Sstevel@tonic-gate 					printed = 8;
298*0Sstevel@tonic-gate 					break;
299*0Sstevel@tonic-gate 				case (int)pr_cap:
300*0Sstevel@tonic-gate 					(void) printf("\t:");
301*0Sstevel@tonic-gate 					printed = 9;
302*0Sstevel@tonic-gate 					caplen += 2;
303*0Sstevel@tonic-gate 					break;
304*0Sstevel@tonic-gate 				case (int)pr_longnames:
305*0Sstevel@tonic-gate 					(void) printf("  ");
306*0Sstevel@tonic-gate 					printed = 2;
307*0Sstevel@tonic-gate 			}
308*0Sstevel@tonic-gate 		} else {
309*0Sstevel@tonic-gate 			switch ((int)printing) {
310*0Sstevel@tonic-gate 				case (int)pr_terminfo:
311*0Sstevel@tonic-gate 				case (int)pr_longnames:
312*0Sstevel@tonic-gate 					(void) printf(" ");
313*0Sstevel@tonic-gate 					break;
314*0Sstevel@tonic-gate 				case (int)pr_cap:
315*0Sstevel@tonic-gate 					(void) printf(":");
316*0Sstevel@tonic-gate 					caplen += 1;
317*0Sstevel@tonic-gate 			}
318*0Sstevel@tonic-gate 			printed++;
319*0Sstevel@tonic-gate 		}
320*0Sstevel@tonic-gate 		if (value < 0)
321*0Sstevel@tonic-gate 			switch ((int)printing) {
322*0Sstevel@tonic-gate 				case (int)pr_terminfo:
323*0Sstevel@tonic-gate 					(void) printf("%s@,", infoname);
324*0Sstevel@tonic-gate 					printed += nlen + 2;
325*0Sstevel@tonic-gate 					break;
326*0Sstevel@tonic-gate 				case (int)pr_cap:
327*0Sstevel@tonic-gate 					(void) printf("%s@", capname);
328*0Sstevel@tonic-gate 					printed += nlen + 1;
329*0Sstevel@tonic-gate 					caplen += nlen + 1;
330*0Sstevel@tonic-gate 					break;
331*0Sstevel@tonic-gate 				case (int)pr_longnames:
332*0Sstevel@tonic-gate 					(void) printf("%s@,", fullname);
333*0Sstevel@tonic-gate 					printed += nlen + 2;
334*0Sstevel@tonic-gate 			}
335*0Sstevel@tonic-gate 		else
336*0Sstevel@tonic-gate 			switch ((int)printing) {
337*0Sstevel@tonic-gate 				case (int)pr_terminfo:
338*0Sstevel@tonic-gate 					(void) printf("%s,", infoname);
339*0Sstevel@tonic-gate 					printed += nlen + 1;
340*0Sstevel@tonic-gate 					break;
341*0Sstevel@tonic-gate 				case (int)pr_cap:
342*0Sstevel@tonic-gate 					(void) printf("%s", capname);
343*0Sstevel@tonic-gate 					printed += nlen;
344*0Sstevel@tonic-gate 					caplen += nlen;
345*0Sstevel@tonic-gate 					break;
346*0Sstevel@tonic-gate 				case (int)pr_longnames:
347*0Sstevel@tonic-gate 					(void) printf("%s,", fullname);
348*0Sstevel@tonic-gate 					printed += nlen + 1;
349*0Sstevel@tonic-gate 			}
350*0Sstevel@tonic-gate 	}
351*0Sstevel@tonic-gate }
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate void
354*0Sstevel@tonic-gate pr_bfooting(void)
355*0Sstevel@tonic-gate {
356*0Sstevel@tonic-gate 	if (!onecolumn && (printed > 0))
357*0Sstevel@tonic-gate 		switch ((int)printing) {
358*0Sstevel@tonic-gate 			case (int)pr_terminfo:
359*0Sstevel@tonic-gate 			case (int)pr_longnames:
360*0Sstevel@tonic-gate 				(void) printf("\n");
361*0Sstevel@tonic-gate 				break;
362*0Sstevel@tonic-gate 			case (int)pr_cap:
363*0Sstevel@tonic-gate 				(void) printf(":\\\n");
364*0Sstevel@tonic-gate 			caplen += 1;
365*0Sstevel@tonic-gate 	    }
366*0Sstevel@tonic-gate }
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate void
369*0Sstevel@tonic-gate pr_nheading(void)
370*0Sstevel@tonic-gate {
371*0Sstevel@tonic-gate 	if (printing == pr_longnames)
372*0Sstevel@tonic-gate 		(void) printf("\nnumbers\n");
373*0Sstevel@tonic-gate 	printed = 0;
374*0Sstevel@tonic-gate }
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate /*
377*0Sstevel@tonic-gate  *  Return the length of the number if it were printed out
378*0Sstevel@tonic-gate  *  with %d. The number is guaranteed to be in the range
379*0Sstevel@tonic-gate  *  0..maxshort.
380*0Sstevel@tonic-gate  */
381*0Sstevel@tonic-gate static int
382*0Sstevel@tonic-gate digitlen(int value)
383*0Sstevel@tonic-gate {
384*0Sstevel@tonic-gate 	return (value >= 10000 ? 5 :
385*0Sstevel@tonic-gate 	    value >=  1000 ? 4 :
386*0Sstevel@tonic-gate 	    value >=   100 ? 3 :
387*0Sstevel@tonic-gate 	    value >=    10 ? 2 :
388*0Sstevel@tonic-gate 	    value >=	0 ? 1 : 0);
389*0Sstevel@tonic-gate }
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate void
392*0Sstevel@tonic-gate pr_number(char *infoname, char *capname, char *fullname, int value)
393*0Sstevel@tonic-gate {
394*0Sstevel@tonic-gate 	int	vlen;
395*0Sstevel@tonic-gate 	size_t	nlen;
396*0Sstevel@tonic-gate 
397*0Sstevel@tonic-gate 	if (printing == pr_cap && restrictterm &&
398*0Sstevel@tonic-gate 	    !findcapname(capname, capnums, ncapnums))
399*0Sstevel@tonic-gate 		return;
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	if (onecolumn) {
402*0Sstevel@tonic-gate 		if (value < 0)
403*0Sstevel@tonic-gate 			switch ((int)printing) {
404*0Sstevel@tonic-gate 				case (int)pr_terminfo:
405*0Sstevel@tonic-gate 					(void) printf("\t%s@,\n", infoname);
406*0Sstevel@tonic-gate 					break;
407*0Sstevel@tonic-gate 				case (int)pr_cap:
408*0Sstevel@tonic-gate 					(void) printf("\t:%s@:\\\n", capname);
409*0Sstevel@tonic-gate 					caplen += 4 + strlen(capname);
410*0Sstevel@tonic-gate 					break;
411*0Sstevel@tonic-gate 				case (int)pr_longnames:
412*0Sstevel@tonic-gate 					(void) printf("  %s @\n", fullname);
413*0Sstevel@tonic-gate 			}
414*0Sstevel@tonic-gate 		else
415*0Sstevel@tonic-gate 			switch ((int)printing) {
416*0Sstevel@tonic-gate 				case (int)pr_terminfo:
417*0Sstevel@tonic-gate 					(void) printf("\t%s#%d,\n", infoname,
418*0Sstevel@tonic-gate 					    value);
419*0Sstevel@tonic-gate 					break;
420*0Sstevel@tonic-gate 				case (int)pr_cap:
421*0Sstevel@tonic-gate 					(void) printf("\t:%s#%d:\\\n",
422*0Sstevel@tonic-gate 					    capname, value);
423*0Sstevel@tonic-gate 					caplen += 4 + strlen(capname) +
424*0Sstevel@tonic-gate 					    digitlen(value);
425*0Sstevel@tonic-gate 					break;
426*0Sstevel@tonic-gate 				case (int)pr_longnames:
427*0Sstevel@tonic-gate 					(void) printf("  %s = %d\n", fullname,
428*0Sstevel@tonic-gate 					    value);
429*0Sstevel@tonic-gate 			}
430*0Sstevel@tonic-gate 	} else {
431*0Sstevel@tonic-gate 		switch ((int)printing) {
432*0Sstevel@tonic-gate 			case (int)pr_terminfo:
433*0Sstevel@tonic-gate 					nlen = strlen(infoname);
434*0Sstevel@tonic-gate 					break;
435*0Sstevel@tonic-gate 			case (int)pr_cap:
436*0Sstevel@tonic-gate 					nlen = strlen(capname);
437*0Sstevel@tonic-gate 					break;
438*0Sstevel@tonic-gate 			case (int)pr_longnames:
439*0Sstevel@tonic-gate 					nlen = strlen(fullname);
440*0Sstevel@tonic-gate 					break;
441*0Sstevel@tonic-gate 		}
442*0Sstevel@tonic-gate 		vlen = digitlen(value);
443*0Sstevel@tonic-gate 		if ((printed > 0) && (printed + nlen + vlen + 2 > width)) {
444*0Sstevel@tonic-gate 			switch ((int)printing) {
445*0Sstevel@tonic-gate 				case (int)pr_terminfo:
446*0Sstevel@tonic-gate 				case (int)pr_longnames:
447*0Sstevel@tonic-gate 					(void) printf("\n");
448*0Sstevel@tonic-gate 					break;
449*0Sstevel@tonic-gate 				case (int)pr_cap:
450*0Sstevel@tonic-gate 					(void) printf(":\\\n");
451*0Sstevel@tonic-gate 					caplen += 1;
452*0Sstevel@tonic-gate 			}
453*0Sstevel@tonic-gate 			printed = 0;
454*0Sstevel@tonic-gate 		}
455*0Sstevel@tonic-gate 		if (printed == 0) {
456*0Sstevel@tonic-gate 			switch ((int)printing) {
457*0Sstevel@tonic-gate 				case (int)pr_terminfo:
458*0Sstevel@tonic-gate 					(void) printf("\t");
459*0Sstevel@tonic-gate 					printed = 8;
460*0Sstevel@tonic-gate 					break;
461*0Sstevel@tonic-gate 				case (int)pr_cap:
462*0Sstevel@tonic-gate 					(void) printf("\t:");
463*0Sstevel@tonic-gate 					printed = 9;
464*0Sstevel@tonic-gate 					caplen += 2;
465*0Sstevel@tonic-gate 					break;
466*0Sstevel@tonic-gate 				case (int)pr_longnames:
467*0Sstevel@tonic-gate 					(void) printf("  ");
468*0Sstevel@tonic-gate 					printed = 2;
469*0Sstevel@tonic-gate 			}
470*0Sstevel@tonic-gate 		} else {
471*0Sstevel@tonic-gate 			switch ((int)printing) {
472*0Sstevel@tonic-gate 				case (int)pr_terminfo:
473*0Sstevel@tonic-gate 				case (int)pr_longnames:
474*0Sstevel@tonic-gate 					(void) printf(" ");
475*0Sstevel@tonic-gate 					break;
476*0Sstevel@tonic-gate 				case (int)pr_cap:
477*0Sstevel@tonic-gate 					(void) printf(":");
478*0Sstevel@tonic-gate 					caplen += 1;
479*0Sstevel@tonic-gate 			}
480*0Sstevel@tonic-gate 			printed++;
481*0Sstevel@tonic-gate 		}
482*0Sstevel@tonic-gate 		if (value < 0) {
483*0Sstevel@tonic-gate 			switch ((int)printing) {
484*0Sstevel@tonic-gate 				case (int)pr_terminfo:
485*0Sstevel@tonic-gate 					(void) printf("%s@,", infoname);
486*0Sstevel@tonic-gate 					printed += nlen + 2;
487*0Sstevel@tonic-gate 					break;
488*0Sstevel@tonic-gate 				case (int)pr_cap:
489*0Sstevel@tonic-gate 					(void) printf("%s@", capname);
490*0Sstevel@tonic-gate 					printed += nlen + 1;
491*0Sstevel@tonic-gate 					caplen += nlen + 1;
492*0Sstevel@tonic-gate 					break;
493*0Sstevel@tonic-gate 				case (int)pr_longnames:
494*0Sstevel@tonic-gate 					(void) printf("%s@,", fullname);
495*0Sstevel@tonic-gate 					printed += nlen + 2;
496*0Sstevel@tonic-gate 			}
497*0Sstevel@tonic-gate 		} else
498*0Sstevel@tonic-gate 			switch ((int)printing) {
499*0Sstevel@tonic-gate 				case (int)pr_terminfo:
500*0Sstevel@tonic-gate 					(void) printf("%s#%d,", infoname,
501*0Sstevel@tonic-gate 					    value);
502*0Sstevel@tonic-gate 					printed += nlen + vlen + 2;
503*0Sstevel@tonic-gate 					break;
504*0Sstevel@tonic-gate 				case (int)pr_cap:
505*0Sstevel@tonic-gate 					(void) printf("%s#%d", capname, value);
506*0Sstevel@tonic-gate 					printed += nlen + vlen + 1;
507*0Sstevel@tonic-gate 					caplen += nlen + vlen + 1;
508*0Sstevel@tonic-gate 					break;
509*0Sstevel@tonic-gate 				case (int)pr_longnames:
510*0Sstevel@tonic-gate 					(void) printf("%s = %d,", fullname,
511*0Sstevel@tonic-gate 					    value);
512*0Sstevel@tonic-gate 					printed += nlen + vlen + 4;
513*0Sstevel@tonic-gate 			}
514*0Sstevel@tonic-gate 	}
515*0Sstevel@tonic-gate }
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate void
518*0Sstevel@tonic-gate pr_nfooting(void)
519*0Sstevel@tonic-gate {
520*0Sstevel@tonic-gate 	if (!onecolumn && (printed > 0))
521*0Sstevel@tonic-gate 		switch ((int)printing) {
522*0Sstevel@tonic-gate 			case (int)pr_terminfo:
523*0Sstevel@tonic-gate 			case (int)pr_longnames:
524*0Sstevel@tonic-gate 				(void) printf("\n");
525*0Sstevel@tonic-gate 				break;
526*0Sstevel@tonic-gate 			case (int)pr_cap:
527*0Sstevel@tonic-gate 				(void) printf(":\\\n");
528*0Sstevel@tonic-gate 				caplen += 1;
529*0Sstevel@tonic-gate 		}
530*0Sstevel@tonic-gate }
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate void
533*0Sstevel@tonic-gate pr_sheading(void)
534*0Sstevel@tonic-gate {
535*0Sstevel@tonic-gate 	if (printing == pr_longnames)
536*0Sstevel@tonic-gate 		(void) printf("\nstrings\n");
537*0Sstevel@tonic-gate 	printed = 0;
538*0Sstevel@tonic-gate }
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate void
541*0Sstevel@tonic-gate pr_string(char *infoname, char *capname, char *fullname, char *value)
542*0Sstevel@tonic-gate {
543*0Sstevel@tonic-gate 	char *evalue;
544*0Sstevel@tonic-gate 	int badcapvalue;
545*0Sstevel@tonic-gate 	size_t nlen, vlen;
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate 	if (printing == pr_cap) {
548*0Sstevel@tonic-gate 		if (restrictterm && !findcapname(capname, capstrs, ncapstrs))
549*0Sstevel@tonic-gate 			return;
550*0Sstevel@tonic-gate 		if (value)
551*0Sstevel@tonic-gate 			value = infotocap(value, &badcapvalue);
552*0Sstevel@tonic-gate 	}
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 	if (onecolumn) {
555*0Sstevel@tonic-gate 		if (value == NULL)
556*0Sstevel@tonic-gate 			switch ((int)printing) {
557*0Sstevel@tonic-gate 				case (int)pr_terminfo:
558*0Sstevel@tonic-gate 					(void) printf("\t%s@,\n", infoname);
559*0Sstevel@tonic-gate 					break;
560*0Sstevel@tonic-gate 				case (int)pr_cap:
561*0Sstevel@tonic-gate 					(void) printf("\t:%s@:\\\n", capname);
562*0Sstevel@tonic-gate 					caplen += 4 + strlen(capname);
563*0Sstevel@tonic-gate 					break;
564*0Sstevel@tonic-gate 				case (int)pr_longnames:
565*0Sstevel@tonic-gate 					(void) printf("  %s@\n", fullname);
566*0Sstevel@tonic-gate 			}
567*0Sstevel@tonic-gate 		else
568*0Sstevel@tonic-gate 			switch ((int)printing) {
569*0Sstevel@tonic-gate 				case (int)pr_terminfo:
570*0Sstevel@tonic-gate 					(void) printf("\t%s=", infoname);
571*0Sstevel@tonic-gate 					tpr(stdout, value);
572*0Sstevel@tonic-gate 					(void) printf(",\n");
573*0Sstevel@tonic-gate 					break;
574*0Sstevel@tonic-gate 				case (int)pr_cap:
575*0Sstevel@tonic-gate 					(void) printf("\t:%s%s=",
576*0Sstevel@tonic-gate 					    badcapvalue ? "." : "", capname);
577*0Sstevel@tonic-gate 					caplen += 3 + strlen(capname) +
578*0Sstevel@tonic-gate 					    (badcapvalue ? 1 : 0);
579*0Sstevel@tonic-gate 					caplen += cpr(stdout, value);
580*0Sstevel@tonic-gate 					(void) printf(":\\\n");
581*0Sstevel@tonic-gate 					caplen += 1;
582*0Sstevel@tonic-gate 					break;
583*0Sstevel@tonic-gate 				case (int)pr_longnames:
584*0Sstevel@tonic-gate 					(void) printf("  %s = '", fullname);
585*0Sstevel@tonic-gate 					tpr(stdout, value);
586*0Sstevel@tonic-gate 					(void) printf("'\n");
587*0Sstevel@tonic-gate 			}
588*0Sstevel@tonic-gate 	} else {
589*0Sstevel@tonic-gate 		switch ((int)printing) {
590*0Sstevel@tonic-gate 			case (int)pr_terminfo:
591*0Sstevel@tonic-gate 				nlen = strlen(infoname);
592*0Sstevel@tonic-gate 				break;
593*0Sstevel@tonic-gate 			case (int)pr_cap:
594*0Sstevel@tonic-gate 				nlen = strlen(capname);
595*0Sstevel@tonic-gate 				if (badcapvalue)
596*0Sstevel@tonic-gate 					nlen++;
597*0Sstevel@tonic-gate 				break;
598*0Sstevel@tonic-gate 			case (int)pr_longnames:
599*0Sstevel@tonic-gate 				nlen = strlen(fullname);
600*0Sstevel@tonic-gate 		}
601*0Sstevel@tonic-gate 		if (value == NULL)
602*0Sstevel@tonic-gate 			vlen = 1;
603*0Sstevel@tonic-gate 		else
604*0Sstevel@tonic-gate 			if (printing == pr_cap)
605*0Sstevel@tonic-gate 				vlen = strlen(evalue = cexpand(value));
606*0Sstevel@tonic-gate 			else
607*0Sstevel@tonic-gate 				vlen = strlen(evalue = iexpand(value));
608*0Sstevel@tonic-gate 		if ((printed > 0) && (printed + nlen + vlen + 1 > width)) {
609*0Sstevel@tonic-gate 			switch ((int)printing) {
610*0Sstevel@tonic-gate 				case (int)pr_terminfo:
611*0Sstevel@tonic-gate 				case (int)pr_longnames:
612*0Sstevel@tonic-gate 					(void) printf("\n");
613*0Sstevel@tonic-gate 					break;
614*0Sstevel@tonic-gate 				case (int)pr_cap:
615*0Sstevel@tonic-gate 					(void) printf(":\\\n");
616*0Sstevel@tonic-gate 					caplen += 1;
617*0Sstevel@tonic-gate 			}
618*0Sstevel@tonic-gate 			printed = 0;
619*0Sstevel@tonic-gate 		}
620*0Sstevel@tonic-gate 		if (printed == 0) {
621*0Sstevel@tonic-gate 			switch ((int)printing) {
622*0Sstevel@tonic-gate 				case (int)pr_terminfo:
623*0Sstevel@tonic-gate 					(void) printf("\t");
624*0Sstevel@tonic-gate 					printed = 8;
625*0Sstevel@tonic-gate 					break;
626*0Sstevel@tonic-gate 				case (int)pr_cap:
627*0Sstevel@tonic-gate 					(void) printf("\t:");
628*0Sstevel@tonic-gate 					printed = 9;
629*0Sstevel@tonic-gate 					caplen += 2;
630*0Sstevel@tonic-gate 					break;
631*0Sstevel@tonic-gate 				case (int)pr_longnames:
632*0Sstevel@tonic-gate 					(void) printf("  ");
633*0Sstevel@tonic-gate 					printed = 2;
634*0Sstevel@tonic-gate 			}
635*0Sstevel@tonic-gate 		} else {
636*0Sstevel@tonic-gate 			switch ((int)printing) {
637*0Sstevel@tonic-gate 				case (int)pr_terminfo:
638*0Sstevel@tonic-gate 				case (int)pr_longnames:
639*0Sstevel@tonic-gate 					(void) printf(" ");
640*0Sstevel@tonic-gate 					break;
641*0Sstevel@tonic-gate 				case (int)pr_cap:
642*0Sstevel@tonic-gate 					(void) printf(":");
643*0Sstevel@tonic-gate 					caplen += 1;
644*0Sstevel@tonic-gate 			}
645*0Sstevel@tonic-gate 			printed++;
646*0Sstevel@tonic-gate 		}
647*0Sstevel@tonic-gate 		if (value == NULL) {
648*0Sstevel@tonic-gate 			switch ((int)printing) {
649*0Sstevel@tonic-gate 				case (int)pr_terminfo:
650*0Sstevel@tonic-gate 					(void) printf("%s@,", infoname);
651*0Sstevel@tonic-gate 					printed += nlen + 2;
652*0Sstevel@tonic-gate 					break;
653*0Sstevel@tonic-gate 				case (int)pr_cap:
654*0Sstevel@tonic-gate 					(void) printf("%s@", capname);
655*0Sstevel@tonic-gate 					printed += nlen + 1;
656*0Sstevel@tonic-gate 					caplen += nlen + 1;
657*0Sstevel@tonic-gate 					break;
658*0Sstevel@tonic-gate 				case (int)pr_longnames:
659*0Sstevel@tonic-gate 					(void) printf("%s@,", fullname);
660*0Sstevel@tonic-gate 					printed += nlen + 2;
661*0Sstevel@tonic-gate 			}
662*0Sstevel@tonic-gate 		} else
663*0Sstevel@tonic-gate 			switch ((int)printing) {
664*0Sstevel@tonic-gate 				case (int)pr_terminfo:
665*0Sstevel@tonic-gate 					(void) printf("%s=%s,", infoname,
666*0Sstevel@tonic-gate 					    evalue);
667*0Sstevel@tonic-gate 					printed += nlen + vlen + 2;
668*0Sstevel@tonic-gate 					break;
669*0Sstevel@tonic-gate 				case (int)pr_cap:
670*0Sstevel@tonic-gate 					if (badcapvalue) {
671*0Sstevel@tonic-gate 						(void) printf(".");
672*0Sstevel@tonic-gate 						caplen += 1;
673*0Sstevel@tonic-gate 					}
674*0Sstevel@tonic-gate 					(void) printf("%s=%s", capname,
675*0Sstevel@tonic-gate 					    evalue);
676*0Sstevel@tonic-gate 					printed += nlen + vlen + 1;
677*0Sstevel@tonic-gate 					caplen += nlen + vlen + 1;
678*0Sstevel@tonic-gate 					break;
679*0Sstevel@tonic-gate 				case (int)pr_longnames:
680*0Sstevel@tonic-gate 					(void) printf("%s = '%s',", fullname,
681*0Sstevel@tonic-gate 					    evalue);
682*0Sstevel@tonic-gate 					printed += nlen + vlen + 6;
683*0Sstevel@tonic-gate 			}
684*0Sstevel@tonic-gate 	}
685*0Sstevel@tonic-gate }
686*0Sstevel@tonic-gate 
687*0Sstevel@tonic-gate void
688*0Sstevel@tonic-gate pr_sfooting(void)
689*0Sstevel@tonic-gate {
690*0Sstevel@tonic-gate 	if (onecolumn) {
691*0Sstevel@tonic-gate 		if (printing == pr_cap)
692*0Sstevel@tonic-gate 			(void) printf("\n");
693*0Sstevel@tonic-gate 	} else {
694*0Sstevel@tonic-gate 		if (printed > 0)
695*0Sstevel@tonic-gate 			switch ((int)printing) {
696*0Sstevel@tonic-gate 				case (int)pr_terminfo:
697*0Sstevel@tonic-gate 				case (int)pr_longnames:
698*0Sstevel@tonic-gate 					(void) printf("\n");
699*0Sstevel@tonic-gate 					break;
700*0Sstevel@tonic-gate 				case (int)pr_cap:
701*0Sstevel@tonic-gate 					(void) printf(":\n");
702*0Sstevel@tonic-gate 					caplen += 1;
703*0Sstevel@tonic-gate 			}
704*0Sstevel@tonic-gate 	}
705*0Sstevel@tonic-gate 	if (caplen >= 1024) {
706*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: WARNING: termcap entry is too "
707*0Sstevel@tonic-gate 		    "long!\n", progname);
708*0Sstevel@tonic-gate 	}
709*0Sstevel@tonic-gate 
710*0Sstevel@tonic-gate 	if (printing == pr_longnames)
711*0Sstevel@tonic-gate 		(void) printf("end of strings\n");
712*0Sstevel@tonic-gate }
713