Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / man / mann / format.n
CommitLineData
86530b38
AT
1'\"
2'\" Copyright (c) 1993 The Regents of the University of California.
3'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4'\"
5'\" See the file "license.terms" for information on usage and redistribution
6'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7'\"
8'\" RCS: @(#) $Id: format.n,v 1.7.2.1 2004/10/27 12:52:40 dkf Exp $
9'\"
10'\" The definitions below are for supplemental macros used in Tcl/Tk
11'\" manual entries.
12'\"
13'\" .AP type name in/out ?indent?
14'\" Start paragraph describing an argument to a library procedure.
15'\" type is type of argument (int, etc.), in/out is either "in", "out",
16'\" or "in/out" to describe whether procedure reads or modifies arg,
17'\" and indent is equivalent to second arg of .IP (shouldn't ever be
18'\" needed; use .AS below instead)
19'\"
20'\" .AS ?type? ?name?
21'\" Give maximum sizes of arguments for setting tab stops. Type and
22'\" name are examples of largest possible arguments that will be passed
23'\" to .AP later. If args are omitted, default tab stops are used.
24'\"
25'\" .BS
26'\" Start box enclosure. From here until next .BE, everything will be
27'\" enclosed in one large box.
28'\"
29'\" .BE
30'\" End of box enclosure.
31'\"
32'\" .CS
33'\" Begin code excerpt.
34'\"
35'\" .CE
36'\" End code excerpt.
37'\"
38'\" .VS ?version? ?br?
39'\" Begin vertical sidebar, for use in marking newly-changed parts
40'\" of man pages. The first argument is ignored and used for recording
41'\" the version when the .VS was added, so that the sidebars can be
42'\" found and removed when they reach a certain age. If another argument
43'\" is present, then a line break is forced before starting the sidebar.
44'\"
45'\" .VE
46'\" End of vertical sidebar.
47'\"
48'\" .DS
49'\" Begin an indented unfilled display.
50'\"
51'\" .DE
52'\" End of indented unfilled display.
53'\"
54'\" .SO
55'\" Start of list of standard options for a Tk widget. The
56'\" options follow on successive lines, in four columns separated
57'\" by tabs.
58'\"
59'\" .SE
60'\" End of list of standard options for a Tk widget.
61'\"
62'\" .OP cmdName dbName dbClass
63'\" Start of description of a specific option. cmdName gives the
64'\" option's name as specified in the class command, dbName gives
65'\" the option's name in the option database, and dbClass gives
66'\" the option's class in the option database.
67'\"
68'\" .UL arg1 arg2
69'\" Print arg1 underlined, then print arg2 normally.
70'\"
71'\" RCS: @(#) $Id: man.macros,v 1.4 2000/08/25 06:18:32 ericm Exp $
72'\"
73'\" # Set up traps and other miscellaneous stuff for Tcl/Tk man pages.
74.if t .wh -1.3i ^B
75.nr ^l \n(.l
76.ad b
77'\" # Start an argument description
78.de AP
79.ie !"\\$4"" .TP \\$4
80.el \{\
81. ie !"\\$2"" .TP \\n()Cu
82. el .TP 15
83.\}
84.ta \\n()Au \\n()Bu
85.ie !"\\$3"" \{\
86\&\\$1 \\fI\\$2\\fP (\\$3)
87.\".b
88.\}
89.el \{\
90.br
91.ie !"\\$2"" \{\
92\&\\$1 \\fI\\$2\\fP
93.\}
94.el \{\
95\&\\fI\\$1\\fP
96.\}
97.\}
98..
99'\" # define tabbing values for .AP
100.de AS
101.nr )A 10n
102.if !"\\$1"" .nr )A \\w'\\$1'u+3n
103.nr )B \\n()Au+15n
104.\"
105.if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n
106.nr )C \\n()Bu+\\w'(in/out)'u+2n
107..
108.AS Tcl_Interp Tcl_CreateInterp in/out
109'\" # BS - start boxed text
110'\" # ^y = starting y location
111'\" # ^b = 1
112.de BS
113.br
114.mk ^y
115.nr ^b 1u
116.if n .nf
117.if n .ti 0
118.if n \l'\\n(.lu\(ul'
119.if n .fi
120..
121'\" # BE - end boxed text (draw box now)
122.de BE
123.nf
124.ti 0
125.mk ^t
126.ie n \l'\\n(^lu\(ul'
127.el \{\
128.\" Draw four-sided box normally, but don't draw top of
129.\" box if the box started on an earlier page.
130.ie !\\n(^b-1 \{\
131\h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
132.\}
133.el \}\
134\h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
135.\}
136.\}
137.fi
138.br
139.nr ^b 0
140..
141'\" # VS - start vertical sidebar
142'\" # ^Y = starting y location
143'\" # ^v = 1 (for troff; for nroff this doesn't matter)
144.de VS
145.if !"\\$2"" .br
146.mk ^Y
147.ie n 'mc \s12\(br\s0
148.el .nr ^v 1u
149..
150'\" # VE - end of vertical sidebar
151.de VE
152.ie n 'mc
153.el \{\
154.ev 2
155.nf
156.ti 0
157.mk ^t
158\h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n'
159.sp -1
160.fi
161.ev
162.\}
163.nr ^v 0
164..
165'\" # Special macro to handle page bottom: finish off current
166'\" # box/sidebar if in box/sidebar mode, then invoked standard
167'\" # page bottom macro.
168.de ^B
169.ev 2
170'ti 0
171'nf
172.mk ^t
173.if \\n(^b \{\
174.\" Draw three-sided box if this is the box's first page,
175.\" draw two sides but no top otherwise.
176.ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
177.el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
178.\}
179.if \\n(^v \{\
180.nr ^x \\n(^tu+1v-\\n(^Yu
181\kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c
182.\}
183.bp
184'fi
185.ev
186.if \\n(^b \{\
187.mk ^y
188.nr ^b 2
189.\}
190.if \\n(^v \{\
191.mk ^Y
192.\}
193..
194'\" # DS - begin display
195.de DS
196.RS
197.nf
198.sp
199..
200'\" # DE - end display
201.de DE
202.fi
203.RE
204.sp
205..
206'\" # SO - start of list of standard options
207.de SO
208.SH "STANDARD OPTIONS"
209.LP
210.nf
211.ta 5.5c 11c
212.ft B
213..
214'\" # SE - end of list of standard options
215.de SE
216.fi
217.ft R
218.LP
219See the \\fBoptions\\fR manual entry for details on the standard options.
220..
221'\" # OP - start of full description for a single option
222.de OP
223.LP
224.nf
225.ta 4c
226Command-Line Name: \\fB\\$1\\fR
227Database Name: \\fB\\$2\\fR
228Database Class: \\fB\\$3\\fR
229.fi
230.IP
231..
232'\" # CS - begin code excerpt
233.de CS
234.RS
235.nf
236.ta .25i .5i .75i 1i
237..
238'\" # CE - end code excerpt
239.de CE
240.fi
241.RE
242..
243.de UL
244\\$1\l'|0\(ul'\\$2
245..
246.TH format n 8.1 Tcl "Tcl Built-In Commands"
247.BS
248'\" Note: do not modify the .SH NAME line immediately below!
249.SH NAME
250format \- Format a string in the style of sprintf
251.SH SYNOPSIS
252\fBformat \fIformatString \fR?\fIarg arg ...\fR?
253.BE
254
255.SH INTRODUCTION
256.PP
257This command generates a formatted string in the same way as the
258ANSI C \fBsprintf\fR procedure (it uses \fBsprintf\fR in its
259implementation).
260\fIFormatString\fR indicates how to format the result, using
261\fB%\fR conversion specifiers as in \fBsprintf\fR, and the additional
262arguments, if any, provide values to be substituted into the result.
263The return value from \fBformat\fR is the formatted string.
264.SH "DETAILS ON FORMATTING"
265.PP
266The command operates by scanning \fIformatString\fR from left to right.
267Each character from the format string is appended to the result
268string unless it is a percent sign.
269If the character is a \fB%\fR then it is not copied to the result string.
270Instead, the characters following the \fB%\fR character are treated as
271a conversion specifier.
272The conversion specifier controls the conversion of the next successive
273\fIarg\fR to a particular format and the result is appended to
274the result string in place of the conversion specifier.
275If there are multiple conversion specifiers in the format string,
276then each one controls the conversion of one additional \fIarg\fR.
277The \fBformat\fR command must be given enough \fIarg\fRs to meet the needs
278of all of the conversion specifiers in \fIformatString\fR.
279.PP
280Each conversion specifier may contain up to six different parts:
281an XPG3 position specifier,
282a set of flags, a minimum field width, a precision, a length modifier,
283and a conversion character.
284Any of these fields may be omitted except for the conversion character.
285The fields that are present must appear in the order given above.
286The paragraphs below discuss each of these fields in turn.
287.PP
288If the \fB%\fR is followed by a decimal number and a \fB$\fR, as in
289``\fB%2$d\fR'', then the value to convert is not taken from the
290next sequential argument.
291Instead, it is taken from the argument indicated by the number,
292where 1 corresponds to the first \fIarg\fR.
293If the conversion specifier requires multiple arguments because
294of \fB*\fR characters in the specifier then
295successive arguments are used, starting with the argument
296given by the number.
297This follows the XPG3 conventions for positional specifiers.
298If there are any positional specifiers in \fIformatString\fR
299then all of the specifiers must be positional.
300.PP
301The second portion of a conversion specifier may contain any of the
302following flag characters, in any order:
303.TP 10
304\fB\-\fR
305Specifies that the converted argument should be left-justified
306in its field (numbers are normally right-justified with leading
307spaces if needed).
308.TP 10
309\fB+\fR
310Specifies that a number should always be printed with a sign,
311even if positive.
312.TP 10
313\fIspace\fR
314Specifies that a space should be added to the beginning of the
315number if the first character isn't a sign.
316.TP 10
317\fB0\fR
318Specifies that the number should be padded on the left with
319zeroes instead of spaces.
320.TP 10
321\fB#\fR
322Requests an alternate output form. For \fBo\fR and \fBO\fR
323conversions it guarantees that the first digit is always \fB0\fR.
324For \fBx\fR or \fBX\fR conversions, \fB0x\fR or \fB0X\fR (respectively)
325will be added to the beginning of the result unless it is zero.
326For all floating-point conversions (\fBe\fR, \fBE\fR, \fBf\fR,
327\fBg\fR, and \fBG\fR) it guarantees that the result always
328has a decimal point.
329For \fBg\fR and \fBG\fR conversions it specifies that
330trailing zeroes should not be removed.
331.PP
332The third portion of a conversion specifier is a number giving a
333minimum field width for this conversion.
334It is typically used to make columns line up in tabular printouts.
335If the converted argument contains fewer characters than the
336minimum field width then it will be padded so that it is as wide
337as the minimum field width.
338Padding normally occurs by adding extra spaces on the left of the
339converted argument, but the \fB0\fR and \fB\-\fR flags
340may be used to specify padding with zeroes on the left or with
341spaces on the right, respectively.
342If the minimum field width is specified as \fB*\fR rather than
343a number, then the next argument to the \fBformat\fR command
344determines the minimum field width; it must be a numeric string.
345.PP
346The fourth portion of a conversion specifier is a precision,
347which consists of a period followed by a number.
348The number is used in different ways for different conversions.
349For \fBe\fR, \fBE\fR, and \fBf\fR conversions it specifies the number
350of digits to appear to the right of the decimal point.
351For \fBg\fR and \fBG\fR conversions it specifies the total number
352of digits to appear, including those on both sides of the decimal
353point (however, trailing zeroes after the decimal point will still
354be omitted unless the \fB#\fR flag has been specified).
355For integer conversions, it specifies a minimum number of digits
356to print (leading zeroes will be added if necessary).
357For \fBs\fR conversions it specifies the maximum number of characters to be
358printed; if the string is longer than this then the trailing characters will be dropped.
359If the precision is specified with \fB*\fR rather than a number
360then the next argument to the \fBformat\fR command determines the precision;
361it must be a numeric string.
362.PP
363The fifth part of a conversion specifier is a length modifier,
364which must be \fBh\fR or \fBl\fR.
365If it is \fBh\fR it specifies that the numeric value should be
366truncated to a 16-bit value before converting.
367This option is rarely useful.
368.VS 8.4
369If it is \fBl\fR it specifies that the numeric value should be (at
370least) a 64-bit value. If neither \fBh\fR nor \fBl\fR are present,
371numeric values are interpreted as being values of the width of the
372native machine word, as described by \fBtcl_platform(wordSize)\fR.
373.VE
374.PP
375The last thing in a conversion specifier is an alphabetic character
376that determines what kind of conversion to perform.
377The following conversion characters are currently supported:
378.TP 10
379\fBd\fR
380Convert integer to signed decimal string.
381.TP 10
382\fBu\fR
383Convert integer to unsigned decimal string.
384.TP 10
385\fBi\fR
386Convert integer to signed decimal string; the integer may either be
387in decimal, in octal (with a leading \fB0\fR) or in hexadecimal
388(with a leading \fB0x\fR).
389.TP 10
390\fBo\fR
391Convert integer to unsigned octal string.
392.TP 10
393\fBx\fR or \fBX\fR
394Convert integer to unsigned hexadecimal string, using digits
395``0123456789abcdef'' for \fBx\fR and ``0123456789ABCDEF'' for \fBX\fR).
396.VS
397.TP 10
398\fBc\fR
399Convert integer to the Unicode character it represents.
400.VE
401.TP 10
402\fBs\fR
403No conversion; just insert string.
404.TP 10
405\fBf\fR
406Convert floating-point number to signed decimal string of
407the form \fIxx.yyy\fR, where the number of \fIy\fR's is determined by
408the precision (default: 6).
409If the precision is 0 then no decimal point is output.
410.TP 10
411\fBe\fR or \fBe\fR
412Convert floating-point number to scientific notation in the
413form \fIx.yyy\fBe\(+-\fIzz\fR, where the number of \fIy\fR's is determined
414by the precision (default: 6).
415If the precision is 0 then no decimal point is output.
416If the \fBE\fR form is used then \fBE\fR is
417printed instead of \fBe\fR.
418.TP 10
419\fBg\fR or \fBG\fR
420If the exponent is less than \-4 or greater than or equal to the
421precision, then convert floating-point number as for \fB%e\fR or
422\fB%E\fR.
423Otherwise convert as for \fB%f\fR.
424Trailing zeroes and a trailing decimal point are omitted.
425.TP 10
426\fB%\fR
427No conversion: just insert \fB%\fR.
428.LP
429For the numerical conversions the argument being converted must
430be an integer or floating-point string; format converts the argument
431to binary and then converts it back to a string according to
432the conversion specifier.
433.SH "DIFFERENCES FROM ANSI SPRINTF"
434.PP
435The behavior of the format command is the same as the
436ANSI C \fBsprintf\fR procedure except for the following
437differences:
438.IP [1]
439\fB%p\fR and \fB%n\fR specifiers are not currently supported.
440.IP [2]
441For \fB%c\fR conversions the argument must be a decimal string,
442which will then be converted to the corresponding character value.
443.IP [3]
444The \fBl\fR modifier
445.VS 8.4
446is ignored for real values and on 64-bit platforms, which are always
447converted as if the \fBl\fR modifier were present (i.e. the types
448\fBdouble\fR and \fBlong\fR are used for the internal representation
449of real and integer values, respectively).
450.VE 8.4
451If the \fBh\fR modifier is specified then integer values are truncated
452to \fBshort\fR before conversion. Both \fBh\fR and \fBl\fR modifiers
453are ignored on all other conversions.
454.SH EXAMPLES
455Convert the output of \fBtime\fR into seconds to an accuracy of
456hundredths of a second:
457.CS
458set us [lindex [time $someTclCode] 0]
459puts [\fBformat\fR "%.2f seconds to execute" [expr {$us / 1e6}]]
460.CE
461.PP
462Create a packed X11 literal color specification:
463.CS
464# Each color-component should be in range (0..255)
465set color [\fBformat\fR "#%02x%02x%02x" $r $g $b]
466.CE
467.PP
468Use XPG3 format codes to allow reordering of fields (a technique that
469is often used in localized message catalogs; see \fBmsgcat\fR) without
470reordering the data values passed to \fBformat\fR:
471.CS
472set fmt1 "Today, %d shares in %s were bought at $%.2f each"
473puts [\fBformat\fR $fmt1 123 "Global BigCorp" 19.37]
474
475set fmt2 "Bought %2\\$s equity ($%3$.2f x %1\\$d) today"
476puts [\fBformat\fR $fmt2 123 "Global BigCorp" 19.37]
477.CE
478.PP
479Print a small table of powers of three:
480.CS
481# Set up the column widths
482set w1 5
483set w2 10
484
485# Make a nice header (with separator) for the table first
486set sep +-[string repeat - $w1]-+-[string repeat - $w2]-+
487puts $sep
488puts [\fBformat\fR "| %-*s | %-*s |" $w1 "Index" $w2 "Power"]
489puts $sep
490
491# Print the contents of the table
492set p 1
493for {set i 0} {$i<=20} {incr i} {
494 puts [\fBformat\fR "| %*d | %*ld |" $w1 $i $w2 $p]
495 set p [expr {wide($p) * 3}]
496}
497
498# Finish off by printing the separator again
499puts $sep
500.CE
501
502.SH "SEE ALSO"
503scan(n), sprintf(3), string(n)
504
505.SH KEYWORDS
506conversion specifier, format, sprintf, string, substitution