|
|
1.1 root 1: /* @(#)print.c 1.5 */
2: /*
3: * UNIX shell
4: *
5: * Bell Telephone Laboratories
6: *
7: */
8:
9: #include "defs.h"
10: #include <sys/param.h>
11: #ifdef CRAY
12: #include <sys/machd.h>
13: #else
14: #ifndef HZ
15: #define HZ 60
16: #endif
17: #endif
18:
19: #define BUFLEN 256
20:
21: static char buffer[BUFLEN];
22: static int index = 0;
23: char numbuf[12];
24:
25: extern void prc_buff();
26: extern void prs_buff();
27: extern void prs_2buff();
28: extern void prn_buff();
29: extern void prs_cntl();
30: extern void prn_buff();
31:
32: /*
33: * printing and io conversion
34: */
35: prp()
36: {
37: if ((flags & prompt) == 0 && cmdadr)
38: {
39: prs_cntl(cmdadr);
40: prs(colon);
41: }
42: }
43:
44: prs(as)
45: char *as;
46: {
47: register char *s;
48:
49: if ((s = as) && length(s) > 1)
50: write(output, s, length(s) - 1);
51: }
52:
53: prc(c)
54: int c;
55: {
56: char realc;
57: realc = c;
58: if (c)
59: write(output, &realc, 1);
60: }
61:
62: prt(t)
63: long t;
64: {
65: register int hr, min, sec;
66:
67: t += HZ / 2;
68: t /= HZ;
69: sec = t % HZ;
70: t /= HZ;
71: min = t % HZ;
72:
73: if (hr = t / HZ)
74: {
75: prn_buff(hr);
76: prc_buff('h');
77: }
78:
79: prn_buff(min);
80: prc_buff('m');
81: prn_buff(sec);
82: prc_buff('s');
83: }
84:
85: prn(n)
86: int n;
87: {
88: itos(n);
89:
90: prs(numbuf);
91: }
92:
93: itos(n)
94: {
95: register char *abuf;
96: register unsigned a, i;
97: int pr, d;
98:
99: abuf = numbuf;
100: /* process id's can be very large on the cray (pjw) */
101: pr = FALSE;
102: a = n;
103: for (i = 10000000; i != 1; i /= 10)
104: {
105: if ((pr |= (d = a / i)))
106: *abuf++ = d + '0';
107: a %= i;
108: }
109: *abuf++ = a + '0';
110: *abuf++ = 0;
111: }
112:
113: stoi(icp)
114: char *icp;
115: {
116: register char *cp = icp;
117: register int r = 0;
118: register char c;
119:
120: while ((c = *cp, digit(c)) && c && r >= 0)
121: {
122: r = r * 10 + c - '0';
123: cp++;
124: }
125: if (r < 0 || cp == icp)
126: failed(icp, badnum, 0);
127: else
128: return(r);
129: }
130:
131: prl(n)
132: long n;
133: {
134: int i;
135:
136: i = 11;
137: while (n > 0 && --i >= 0)
138: {
139: numbuf[i] = n % 10 + '0';
140: n /= 10;
141: }
142: numbuf[11] = '\0';
143: prs_buff(&numbuf[i]);
144: }
145:
146: void
147: flushb()
148: {
149: if (index)
150: {
151: buffer[index] = '\0';
152: write(1, buffer, length(buffer) - 1);
153: index = 0;
154: }
155: }
156:
157: void
158: prc_buff(c)
159: char c;
160: {
161: if (c)
162: {
163: if (index + 1 >= BUFLEN)
164: flushb();
165:
166: buffer[index++] = c;
167: }
168: else
169: {
170: flushb();
171: write(1, &c, 1);
172: }
173: }
174:
175: /* rob */
176: void
177: prs_2buff(s, t)
178: char *s, *t;
179: {
180: prs_buff(s);
181: prs_buff(t);
182: }
183:
184: void
185: prs_buff(s)
186: char *s;
187: {
188: register int len = length(s) - 1;
189:
190: if (index + len >= BUFLEN)
191: flushb();
192:
193: if (len >= BUFLEN)
194: write(1, s, len);
195: else
196: {
197: movstr(s, &buffer[index]);
198: index += len;
199: }
200: }
201:
202:
203: clear_buff()
204: {
205: index = 0;
206: }
207:
208:
209: void
210: prs_cntl(s)
211: char *s;
212: {
213: register char *ptr = buffer;
214: register char c;
215:
216: while (*s != '\0')
217: {
218: c = (*s & 0177) ;
219:
220: /* translate a control character into a printable sequence */
221:
222: if (c < '\040')
223: { /* assumes ASCII char */
224: *ptr++ = '^';
225: *ptr++ = (c + 0100); /* assumes ASCII char */
226: }
227: else if (c == 0177)
228: { /* '\0177' does not work */
229: *ptr++ = '^';
230: *ptr++ = '?';
231: }
232: else
233: { /* printable character */
234: *ptr++ = c;
235: }
236:
237: ++s;
238: }
239:
240: *ptr = '\0';
241: prs(buffer);
242: }
243:
244:
245: void
246: prn_buff(n)
247: int n;
248: {
249: itos(n);
250:
251: prs_buff(numbuf);
252: }
253: char *
254: quotedstring(s)
255: register char *s;
256: {
257: register char *t = s;
258: register char *outp=locstak();
259: register quoting=0;
260: while(*t)
261: if(any(*t++, " \t\n\\\"'`;&|$*[](){}<>")){
262: while(*s){
263: if(*s == '\''){
264: if(quoting)
265: pushstak(*s); /* end quote */
266: pushstak('\\');
267: quoting=0;
268: }else if(!quoting){
269: pushstak('\'');
270: quoting=1;
271: }
272: pushstak(*s++);
273: }
274: if(quoting)
275: pushstak('\'');
276: break;
277: }
278: do
279: pushstak(*s);
280: while(*s++);
281: staktop=stakbot;
282: return outp;
283: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.