aboutsummaryrefslogtreecommitdiff
path: root/sys/fmtio/parg.x
blob: d65bd3ce9443a4383492c7eb9e72bf7d50d54ac0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<mach.h>
include	<ctype.h>
include	<printf.h>

.help parg
.nf ___________________________________________________________________________
PARG[CSILRDX] -- Pass a numeric argument to printf.  Get the format spec and
format the number on the output file.  Try to provide reasonable automatic
type conversions.  Avoid any type coercion of indefinites.
We try to make the operand fit in the specified field width, decreasing the
precision if necessary, but if it cannot be made to fit we increase the field
width until it does.  We feel that it is more important to output a readable
number than to keep the output columns justified.
.endhelp ______________________________________________________________________


# PARGD -- Pass a double.

procedure pargd (dval)

double	dval

begin
	call pargg (dval, TY_DOUBLE)
end


# PARGC -- Pass a char.

procedure pargc (cval)

char	cval
double	dval

begin
	dval = cval
	call pargg (dval, TY_CHAR)
end


# PARGS -- Pass a short.

procedure pargs (sval)

short	sval
double	dval

begin
	dval = sval
	if (IS_INDEFS (sval))
	    dval = INDEFD

	call pargg (dval, TY_SHORT)
end


# PARGI -- Pass an int.

procedure pargi (ival)

int	ival
double	dval

begin
	dval = ival
	if (IS_INDEFI (ival))
	    dval = INDEFD

	call pargg (dval, TY_INT)
end


# PARGL -- Pass a long.

procedure pargl (lval)

long	lval
double	dval

begin
	dval = lval
	if (IS_INDEFL (lval))
	    dval = INDEFD

	call pargg (dval, TY_LONG)
end


# PARGR -- Pass a real.

procedure pargr (rval)

real	rval
double	dval

begin
	dval = rval
	if (IS_INDEFR (rval))
	    dval = INDEFD

	call pargg (dval, TY_REAL)
end


# PARGG -- Generic put argument.  Encode a value of a specific datatype passed
# as a double precision value.

procedure pargg (value, dtype)

double	value
int	dtype

char	ch
long	lnum
complex	xnum
int	n, precision, i, junk, ival, nchars, nbits, fmt
int	ctocc(), gltoc(), dtoc(), xtoc(), fprfmt()
errchk	putci, fmtstr, fpradv
include "fmt.com"

begin
	# Read format.  If format spec contains "*" fields, VALUE is a part of
	# the format, rather than a true operand.  In that case we return,
	# and the next call again checks to see if the format spec is complete.
	# Note that if VALUE is not part of the format but is instead a floating
	# point value to be printed, it may have an exponent large enough to
	# cause integer overflow in an INT(VALUE) operation, hence we must
	# guard against this.  This is easy since only PARGI will be used to
	# pass format information.

	if (dtype == TY_REAL || dtype == TY_DOUBLE)
	    ival = 0
	else if (IS_INDEFD (value))
	    ival = INDEFI
	else
	    ival = nint (value)

	if (fprfmt (ival) == NOT_DONE_YET)
	    return

 	if (format_char == USE_DEFAULT || format_char == FMT_STRING)
	    switch (dtype) {
	    case TY_CHAR:
		format_char = FMT_CHAR
	    case TY_INT:
		format_char = FMT_DECIMAL
	    default:
		format_char = FMT_GENERAL
	    }

	if (dtype == TY_DOUBLE)				# supply def. precision
	    precision = NDIGITS_DP
	else
	    precision = NDIGITS_RP

	if (width == USE_DEFAULT)			# make as big as needed
	    width = SZ_OBUF

	# Convert number from binary into character form in OBUF, applying
	# defaults as needed.

	# Ignore case in testing format type.
	fmt = format_char
	if (IS_UPPER (fmt))
	    fmt = TO_LOWER(fmt)

	switch (fmt) {
	case FMT_BOOL:
	    if (IS_INDEFD (value))
		call strcpy ("INDEF", obuf, SZ_OBUF)
	    else if (int (value) == 0)
		call strcpy ("NO", obuf, SZ_OBUF)
	    else
		call strcpy ("YES", obuf, SZ_OBUF)

	case FMT_CHAR:
	    if (IS_INDEFD (value))
		call strcpy ("INDEF", obuf, SZ_OBUF)
	    else {
		ch = nint (value)
		junk = ctocc (ch, obuf, SZ_OBUF)
	    }

	case FMT_DECIMAL, FMT_OCTAL, FMT_HEX, FMT_RADIX, FMT_UNSIGNED:
	    switch (fmt) {
	    case FMT_DECIMAL:
		radix = DECIMAL				# signed decimal
	    case FMT_OCTAL:
		radix = -OCTAL				# unsigned octal
	    case FMT_HEX:
		radix = -HEX				# unsigned hex
	    case FMT_UNSIGNED:
		radix = -DECIMAL			# unsigned decimal
	    default:
		radix = -abs(radix)			# unsigned radix
	    }

	    if (IS_INDEFD (value)) {
		lnum = INDEFL
		nchars = gltoc (lnum, obuf, SZ_OBUF, radix)

	    } else {
		lnum = long (value)
		nchars = gltoc (lnum, obuf, SZ_OBUF, radix)

		# Limit sign extension if negative number, hex or octal.
		if (lnum < 0 && (dtype == TY_SHORT || dtype == TY_CHAR)) {
		    nbits = SZB_CHAR * NBITS_BYTE
		    if (dtype == TY_SHORT)
			nbits = nbits * SZ_SHORT
		    if (fmt == FMT_OCTAL) {
			n = nchars - (nbits + 2) / 3
			if (n > 0) {
			    call strcpy (obuf[n+2], obuf[2], SZ_OBUF)
			    obuf[1] = '1'
			}
		    } else if (fmt == FMT_HEX) {
			n = nchars - (nbits + 3) / 4
			if (n > 0)
			    call strcpy (obuf[n+1], obuf[1], SZ_OBUF)
		    }
		}
	    }

	case FMT_EXPON, FMT_FIXED, FMT_GENERAL, FMT_HMS, FMT_MINSEC:
	    if (decpl == USE_DEFAULT || decpl == 0)
		switch (fmt) {
		case FMT_EXPON, FMT_GENERAL:
		    decpl = precision
		case FMT_HMS, FMT_MINSEC:
		    if (decpl == USE_DEFAULT)
			decpl = 1
		default:
		    if (decpl == USE_DEFAULT)
			decpl = precision
		}
	    repeat {
		# Need the case sensitive format char here.
		n = dtoc (value, obuf, SZ_OBUF, decpl, format_char, width+1)
		decpl = decpl - 1
	    } until (n <= width || decpl <= 0)

	case FMT_TOCOLUMN:				# advance to column
	    for (i=int(value);  col < i;  col=col+1)
		call putci (fd, ' ')
	    call fpradv()
	    return

	case FMT_WHITESPACE:				# output whitespace
	    for (i=0;  i < int(value);  i=i+1)
		call putci (fd, ' ')
	    col = col + i
	    call fpradv()
	    return

	case FMT_COMPLEX:
	    if (decpl == USE_DEFAULT)			# set defaults
		decpl = precision
	    else
		decpl = abs (decpl)

	    if (IS_INDEFD (value))
		xnum = INDEFX
	    else
		xnum = complex (value)

	    # Convert, decrease precision until it fits in given field width.
	    repeat {
		n = xtoc (xnum, obuf, SZ_OBUF, decpl, 'e', SZ_OBUF)
		decpl = decpl - 1
	    } until (n <= width || decpl <= 0)
	}
	    
	# Move the string in OBUF to the output file, left or right justifying
	# as specified.  Advance to the next format spec (or finish up).

	if (width == SZ_OBUF)				# free format?
	    width = 0
	call fmtstr (fd, obuf, col, fill_char, left_justify, SZ_OBUF, width)
	call fpradv ()
end