xref: /csrg-svn/local/toolchest/ksh/sh/print.c (revision 35160)
1*35160Smarc /*
2*35160Smarc 
3*35160Smarc  *      Copyright (c) 1984, 1985, 1986 AT&T
4*35160Smarc  *      All Rights Reserved
5*35160Smarc 
6*35160Smarc  *      THIS IS UNPUBLISHED PROPRIETARY SOURCE
7*35160Smarc  *      CODE OF AT&T.
8*35160Smarc  *      The copyright notice above does not
9*35160Smarc  *      evidence any actual or intended
10*35160Smarc  *      publication of such source code.
11*35160Smarc 
12*35160Smarc  */
13*35160Smarc /* @(#)print.c	1.1 */
14*35160Smarc /*
15*35160Smarc  * UNIX shell
16*35160Smarc  *
17*35160Smarc  * S. R. Bourne
18*35160Smarc  * Rewritten by David Korn
19*35160Smarc  * AT&T Bell Laboratories
20*35160Smarc  *
21*35160Smarc  */
22*35160Smarc 
23*35160Smarc #include	"flags.h"
24*35160Smarc #include	"defs.h"
25*35160Smarc #include	"io.h"
26*35160Smarc #include	"shtype.h"
27*35160Smarc #ifndef BSD
28*35160Smarc # ifdef XENIX
29*35160Smarc # include	<sys/types.h>
30*35160Smarc # endif /* XENIX */
31*35160Smarc #include	<sys/param.h>
32*35160Smarc #endif /* BSD */
33*35160Smarc #include	"name.h"
34*35160Smarc #include	"builtins.h"
35*35160Smarc 
36*35160Smarc /* This module defines the following routines */
37*35160Smarc void	p_flush();
38*35160Smarc void	p_list();
39*35160Smarc void	p_nchr();
40*35160Smarc void	p_num();
41*35160Smarc void	p_prp();
42*35160Smarc void	p_setout();
43*35160Smarc void	p_str();
44*35160Smarc void	p_sub();
45*35160Smarc void	p_time();
46*35160Smarc 
47*35160Smarc /* This module references the following externals */
48*35160Smarc extern char	*itos();
49*35160Smarc extern char	*qvalup();
50*35160Smarc extern void	rjust();
51*35160Smarc extern void	setbuf();
52*35160Smarc extern char	*strcpy();
53*35160Smarc 
54*35160Smarc /* printing and io conversion */
55*35160Smarc #ifdef BSD
56*35160Smarc #define TIC_SEC		60	/* number of ticks per second */
57*35160Smarc #else
58*35160Smarc #define TIC_SEC		HZ	/* number of ticks per second */
59*35160Smarc #endif /* BSD */
60*35160Smarc 
61*35160Smarc 
62*35160Smarc /*
63*35160Smarc  *  flush the output queue and reset the output stream
64*35160Smarc  */
65*35160Smarc 
p_setout(fd)66*35160Smarc void	p_setout(fd)
67*35160Smarc register FILE *fd;
68*35160Smarc {
69*35160Smarc 	if(output==fd)
70*35160Smarc 		return;
71*35160Smarc 	p_flush();
72*35160Smarc 	output = fd;
73*35160Smarc 	setbuf(fd,(char*)_sobuf);
74*35160Smarc }
75*35160Smarc 
76*35160Smarc /*
77*35160Smarc  * flush the output if necessary and null terminate the buffer
78*35160Smarc  */
79*35160Smarc 
p_flush()80*35160Smarc void p_flush()
81*35160Smarc {
82*35160Smarc 	register FILE *oldfd = output;
83*35160Smarc 	register char *cp;
84*35160Smarc 	if(oldfd)
85*35160Smarc 	{
86*35160Smarc 		if((cp=(char*)(oldfd->_ptr)) > (char*)(oldfd->_base))
87*35160Smarc 		{
88*35160Smarc 			fflush(oldfd);
89*35160Smarc 			/* leave the previous buffer as a null terminated string */
90*35160Smarc 		}
91*35160Smarc 		if(cp)
92*35160Smarc 			*cp = 0;
93*35160Smarc 	}
94*35160Smarc }
95*35160Smarc 
96*35160Smarc /*
97*35160Smarc  * print a message preceded by the command name
98*35160Smarc  */
99*35160Smarc 
p_prp(s1,ch)100*35160Smarc void p_prp(s1,ch)
101*35160Smarc char *s1;
102*35160Smarc {
103*35160Smarc 	register unsigned char *cp;
104*35160Smarc 	register int c;
105*35160Smarc 	register FILE *fd = output;
106*35160Smarc 	if(cp=(unsigned char *)cmdadr)
107*35160Smarc 	{
108*35160Smarc 		if(*cp=='-')
109*35160Smarc 			cp++;
110*35160Smarc 		c = ((cmdline>1&&ch!=SP)?0:':');
111*35160Smarc 		p_str(cp,c);
112*35160Smarc 		if(c==0)
113*35160Smarc 			p_sub(cmdline,':');
114*35160Smarc 		putc(SP,fd);
115*35160Smarc 	}
116*35160Smarc 	for(cp=(unsigned char *)s1;c= *cp;cp++)
117*35160Smarc 	{
118*35160Smarc 		if(!isprint(c))
119*35160Smarc 		{
120*35160Smarc 			putc(HAT,fd);
121*35160Smarc 			c ^= TO_PRINT;
122*35160Smarc 		}
123*35160Smarc 		putc(c,fd);
124*35160Smarc 	}
125*35160Smarc 	if(ch)
126*35160Smarc 		putc(ch,fd);
127*35160Smarc }
128*35160Smarc 
129*35160Smarc /*
130*35160Smarc  * print a time and a separator
131*35160Smarc  */
132*35160Smarc 
p_time(t,c)133*35160Smarc void	p_time(t,c)
134*35160Smarc long int  t;
135*35160Smarc char c;
136*35160Smarc {
137*35160Smarc 	register int  min, sec, frac;
138*35160Smarc 	register int hr;
139*35160Smarc 	frac = t%TIC_SEC;
140*35160Smarc 	frac = (frac*100)/TIC_SEC;
141*35160Smarc 	t /= TIC_SEC;
142*35160Smarc 	sec=t%60; t /= 60;
143*35160Smarc 	min=t%60;
144*35160Smarc 	if(hr=t/60)
145*35160Smarc 	{
146*35160Smarc 		p_num(hr,'h');
147*35160Smarc 	}
148*35160Smarc 	p_num(min,'m');
149*35160Smarc 	p_num(sec,'.');
150*35160Smarc 	if(frac<10)
151*35160Smarc 		putc('0',output);
152*35160Smarc 	p_num(frac,'s');
153*35160Smarc 	putc(c,output);
154*35160Smarc }
155*35160Smarc 
156*35160Smarc /*
157*35160Smarc  * print a number optionally followed by a character
158*35160Smarc  */
159*35160Smarc 
p_num(n,c)160*35160Smarc void	p_num(n,c)
161*35160Smarc int 	n;
162*35160Smarc char c;
163*35160Smarc {
164*35160Smarc 	p_str(itos(n),c);
165*35160Smarc }
166*35160Smarc 
167*35160Smarc /*
168*35160Smarc  * print a string optionally followed by a character
169*35160Smarc  */
170*35160Smarc 
p_str(string,c)171*35160Smarc void	p_str(string,c)
172*35160Smarc register char *string;
173*35160Smarc register int c;
174*35160Smarc {
175*35160Smarc 	register FILE *fd = output;
176*35160Smarc 	fputs(string,fd);
177*35160Smarc 	if(c)
178*35160Smarc 		putc(c,fd);
179*35160Smarc }
180*35160Smarc 
181*35160Smarc /*
182*35160Smarc  * print a given character a given number of times
183*35160Smarc  */
184*35160Smarc 
p_nchr(c,n)185*35160Smarc void	p_nchr(c,n)
186*35160Smarc register int c,n;
187*35160Smarc {
188*35160Smarc 	register FILE *fd = output;
189*35160Smarc 	while(n-- > 0)
190*35160Smarc 		putc(c,fd);
191*35160Smarc }
192*35160Smarc 
193*35160Smarc /*
194*35160Smarc  * print a list of arguments in columns
195*35160Smarc  */
196*35160Smarc #define NROW	15	/* number of rows in output before going to multi-columns */
197*35160Smarc #define LBLSIZ	3	/* size of label field and interfield spacing */
198*35160Smarc 
p_list(argn,com)199*35160Smarc void	p_list(argn,com)
200*35160Smarc char *com[];
201*35160Smarc {
202*35160Smarc 	register int i,j;
203*35160Smarc 	register char **arg;
204*35160Smarc 	char a1[12];
205*35160Smarc 	int nrow = NROW;
206*35160Smarc 	int ncol = 1;
207*35160Smarc 	int ndigits = 1;
208*35160Smarc 	int fldsize;
209*35160Smarc #if ESH || VSH
210*35160Smarc 	int wsize = e_window();
211*35160Smarc #else
212*35160Smarc 	int wsize = 80;
213*35160Smarc #endif
214*35160Smarc 	char *cp = qvalup(LINES);
215*35160Smarc 	nrow = (cp?1+2*(atoi(cp)/3):NROW);
216*35160Smarc 	for(i=argn;i >= 10;i /= 10)
217*35160Smarc 		ndigits++;
218*35160Smarc 	if(argn < nrow)
219*35160Smarc 	{
220*35160Smarc 		nrow = argn;
221*35160Smarc 		goto skip;
222*35160Smarc 	}
223*35160Smarc 	i = 0;
224*35160Smarc 	for(arg=com; *arg;arg++)
225*35160Smarc 	{
226*35160Smarc 		i = max(i,strlen(*arg));
227*35160Smarc 	}
228*35160Smarc 	i += (ndigits+LBLSIZ);
229*35160Smarc 	if(i < wsize)
230*35160Smarc 		ncol = wsize/i;
231*35160Smarc 	if(argn > nrow*ncol)
232*35160Smarc 	{
233*35160Smarc 		nrow = 1 + (argn-1)/ncol;
234*35160Smarc 	}
235*35160Smarc 	else
236*35160Smarc 	{
237*35160Smarc 		ncol = 1 + (argn-1)/nrow;
238*35160Smarc 		nrow = 1 + (argn-1)/ncol;
239*35160Smarc 	}
240*35160Smarc skip:
241*35160Smarc 	fldsize = (wsize/ncol)-(ndigits+LBLSIZ);
242*35160Smarc 	for(i=0;i<nrow;i++)
243*35160Smarc 	{
244*35160Smarc 		j = i;
245*35160Smarc 		while(1)
246*35160Smarc 		{
247*35160Smarc 			arg = com+j;
248*35160Smarc 			strcpy(a1,itos(j+1));
249*35160Smarc 			rjust(a1,ndigits,' ');
250*35160Smarc 			p_str(a1,')');
251*35160Smarc 			putc(SP,output);
252*35160Smarc 			fputs(*arg,output);
253*35160Smarc 			j += nrow;
254*35160Smarc 			if(j >= argn)
255*35160Smarc 				break;
256*35160Smarc 			p_nchr(SP,fldsize-strlen(*arg));
257*35160Smarc 		}
258*35160Smarc 		newline();
259*35160Smarc 	}
260*35160Smarc }
261*35160Smarc 
262*35160Smarc /*
263*35160Smarc  * Print a number enclosed in [] followed by a character
264*35160Smarc  */
265*35160Smarc 
p_sub(n,c)266*35160Smarc void	p_sub(n,c)
267*35160Smarc register int n;
268*35160Smarc register int c;
269*35160Smarc {
270*35160Smarc 	register FILE *fd=output;
271*35160Smarc 	putc('[',fd);
272*35160Smarc 	p_num(n,']');
273*35160Smarc 	putc(c,fd);
274*35160Smarc }
275