ddb6d4acaf3762ac095203e5f4177b4599f1db6b
[platform/upstream/groff.git] / contrib / hdtbl / examples / common.roff
1 .\"     -*-     mode: nroff     -*-
2 .ig
3
4 common.roff
5
6 This file is part of groff, the GNU roff type-setting system.
7
8 Copyright (C) 2010-2014  Free Software Foundation, Inc.
9 written by Joachim Walsdorff <Joachim.Walsdorff@urz.uni-heidelberg.de>.
10
11 groff is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 groff is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24 ..
25 .
26 .
27 .mso hdtbl.tmac\"                       load table macros
28 .
29 .\"     ******************************************************************
30 .\"     **      Some macros and the page setup used by the examples     **
31 .\"     ******************************************************************
32 .
33 .\"     ******************************************************************
34 .\"     **      some of the following macros use system commands        **
35 .\"     **      and are therefore `unsafe': they need the `-U' argument **
36 .\"     **      when calling groff/troff                                **
37 .\"     ******************************************************************
38 .
39 .\"     ******************************************************************
40 .\"     **              Header macro for the examples                   **
41 .\"     ******************************************************************
42 .de H
43 .  nr *w* (17 * \w\a\\$*\a / 10 + 4n)
44 .  TBL border=1n \
45        bc=yellow \
46        bgc=red4 \
47        fgc=yellow \
48        csp=0 \
49        fst=TB \
50        "fsz=1.7 1.5" \
51        hal=c \
52        tal=c \
53        "width=(\\n[*w*]+4n)<?\n[.l]"
54 .  TR .TD
55 .  t*P1 \\$*
56 .  ETB
57 .  SP
58 ..
59 .
60 .
61 .\"     ******************************************************************
62 .\"     **      Utility macro for the date, requires UNIX date.         **
63 .\"     **      after return string *date contains the date in the      **
64 .\"     **      standard form of the Unix date-command,                 **
65 .\"     **      for example "Sun Dec  5 22:27:57     2004"              **
66 .\"     ******************************************************************
67 .de date
68 .  pso sh -c "echo -n .ds *date\ ;date"
69 .  tm \\*[*date] ***
70 ..
71 .
72 .
73 .\"     ******************************************************************
74 .\"     **      Utility macro for time measurement, requires UNIX date  **
75 .\"     **      .time s[tart]|[end]                                     **
76 .\"     **              .time start:                                    **
77 .\"     **                      reg *time gets the start-time (seconds) **
78 .\"     **              .time [end]:                                    **
79 .\"     **                      reg *time gets the difference of the    **
80 .\"     **                              end- and start-time (seconds)   **
81 .\"     ******************************************************************
82 .de time
83 .  ds * \\$1\"
84 .  substring * 0 0
85 .  ie "\\*[*]"s" \
86 .    pso sh -c "echo -n .nr *time 0+;date +%s"
87 .  el \{\
88 .   pso sh -c "echo -n .nr *time -;date +%s"
89 .   nr *time 0-\\n[*time]
90 .   tm elapsed time: \\n[*time] seconds
91 .  \}
92 ..
93 .
94 .
95 .\"     ******************************************************************
96 .\"     **              Perform n-times all the arbitrary arguments     **
97 .\"     **              .PN n a2 a3 ...                                 **
98 .\"     **                      PN is nestable                          **
99 .\"     ******************************************************************
100 .de PN
101 .  nr *pn +1
102 .  nr PN\\n[*pn] (\\$1 + 1) 1
103 .  shift
104 .
105 .  while \\n-[PN\\n[*pn]] \
106 .    t*P1 \\$@
107 .
108 .  nr *pn -1
109 ..
110 .
111 .
112 .\"     Utility macro:  .d2x decimal_number [base [string_name]]
113 .\"
114 .\"             Convert `decimal_number' to another base `base' (in the
115 .\"             range 1..16) and store the result in string `string_name'.
116 .\"             If `base' is missing or empty, convert to a hexadecimal
117 .\"             number.  If `string_name' is missing or empty, return value
118 .\"             in string `hex#', otherwise return the value in both
119 .\"             `string_name' and `hex#'.
120 .\"
121 .\"             The base value 1 is handled specially: The returned
122 .\"             string contains the character `|' `decimal_number' times
123 .\"             (for example, input value 4 yields `||||').
124 .ds d2x-0 0\"
125 .ds d2x-1 1\"
126 .ds d2x-2 2\"
127 .ds d2x-3 3\"
128 .ds d2x-4 4\"
129 .ds d2x-5 5\"
130 .ds d2x-6 6\"
131 .ds d2x-7 7\"
132 .ds d2x-8 8\"
133 .ds d2x-9 9\"
134 .ds d2x-10 A\"
135 .ds d2x-11 B\"
136 .ds d2x-12 C\"
137 .ds d2x-13 D\"
138 .ds d2x-14 E\"
139 .ds d2x-15 F\"
140 .
141 .
142 .de d2x
143 .  if !\B\a\\$1\a \{\
144 .    tm \\n[.F]:\\n[.c]: invalid or missing first argument
145 .    tm1 "     usage: `.d2x decimal_number [base [string_name]]'
146 .    return
147 .  \}
148 .
149 .  nr i# (-1) 1
150 .  nr j# 1
151 .  ds hex#
152 .  nr dec# (\\$1) 1
153 .
154 .  if !\\$1 \
155 .    nr dec# (-\\n[dec#])
156 .
157 .  ie !"\\$2"" \{\
158 .    ie !\B\a\\$2\a \
159 .      tm \\n[.F]:\\n[.c]: invalid base `\\$2'
160 .    el \
161 .      ie ((\\$2 < 1) : (\\$2 > 16)) \
162 .        tm \\n[.F]:\\n[.c]: invalid base `\\$2'
163 .      el \
164 .        nr b# \\$2
165 .  \}\}
166 .  el \
167 .    nr b# 16
168 .
169 .  nr xb# 1
170 .
171 .  ie (\\n[b#] == 1) \{\
172 .    nr dec# +1
173 .    while \\n-[dec#] \
174 .      as hex# |\"
175 .  \}
176 .  el \{\
177 .    while (\\n[dec#] - \\n[xb#]) \{\
178 .      nr xb# (\\n[xb#] * \\n[b#])
179 .      nr j# +1
180 .    \}
181 .
182 .    while (\\n+[i#] < \\n[j#]) \{\
183 .      nr ** (\\n[dec#] / \\n[xb#])
184 .      as hex# \\*[d2x-\\n[**]]\"
185 .      nr dec# (\\n[dec#] - (\\n[xb#] * \\n[**]))
186 .      nr xb# (\\n[xb#] / \\n[b#])
187 .    \}
188 .  \}
189 .
190 .  \" strip leading zero, if any
191 .  ds * \\*[hex#]\"
192 .  substring * 0 0
193 .  if "\\*[*]"0" \
194 .    substring hex# 1 -1
195 .
196 .  if (\\$1 < 0) \
197 .    ds hex# -\\*[hex#]\"
198 .
199 .  if !"\\$3"" \{\
200 .    ie !\A\a\\$3\a \
201 .      tm \\n[.F]:\\n[.c]: invalid string name `\\$3'
202 .    el \
203 .      ds \\$3 \\*[hex#]\"
204 .  \}
205 ..
206 .
207 .
208 .\"     Utility macro:  .random#
209 .\"                     .random-seed seed1 seed2
210 .\"
211 .\"             Return pseudo-random numbers in the range 0..0xFFFFFF,
212 .\"             represented as the concatenation of `#' and six
213 .\"             hexadecimal digits, in the string `#random'.  The
214 .\"             macro `random-seed' can be used to set seed values,
215 .\"             which should be integers in the range 1..2147483562 and
216 .\"             1..2147483398 for `seed1' and `seed2', respectively
217 .\"             (the macro applies a modulo operation to assure this
218 .\"             range).  If `random-seed' isn't called the registers
219 .\"             `seconds', `minutes', `hours', `dy', `mo', `year', and
220 .\"             `$$' are used to compute it.
221 .\"
222 .\"             The used generator is presented in L'Ecuyer's 1988 paper
223 .\"             `Efficient and Portable Combined Random Number
224 .\"             Generators', which combines two Multiplicative Linear
225 .\"             Congruential Generators (MLCGs) to achieve a period of
226 .\"             2.3*10^18.
227 .af hours 00
228 .af minutes 00
229 .af seconds 00
230 .af year 0000
231 .af mo 00
232 .af dy 00
233 .
234 .ds random-s1 \n[minutes]\n[seconds]\n[$$]\n[hours]\"
235 .\" prevent overflow
236 .substring random-s1 0 8
237 .
238 .nr random-s1 (\*[random-s1] % 2147483562)
239 .nr random-s2 \n[dy]\n[year]\n[mo]
240 .
241 .
242 .de random-seed
243 .  if !(\\n[.$] == 2) \{\
244 .    tm1 "random-seed: Invalid number of arguments.
245 .    tm1 "             usage: `.random-seed seed1 seed2'
246 .    return
247 .  \}
248 .
249 .  nr random-s1 (\\$1 % 2147483562)
250 .  nr random-s2 (\\$2 % 2147483398)
251 ..
252 .
253 .
254 .de random#
255 .  nr * (\\n[random-s1] / 53668)
256 .  nr random-s1 (40014 * (\\n[random-s1] - (\\n[*] * 53668)) \
257                 - (\\n[*] * 12211))
258 .  if !\\n[random-s1] \
259 .    nr random-s1 +2147483563
260 .
261 .  nr * (\\n[random-s2] / 52774)
262 .  nr random-s2 (40692 * (\\n[random-s2] - (\\n[*] * 52774)) \
263                 - (\\n[*] * 3791))
264 .  if !\\n[random-s2] \
265 .    nr random-s2 +2147483399
266 .
267 .  nr * (\\n[random-s1] - \\n[random-s2])
268 .  if (\\n[*] < 1) \
269 .    nr * +2147483562
270 .
271 .  \" reduce the result to the leftmost 24 bits
272 .  nr * (\\n[*] / 128)
273 .
274 .  d2x \\n[*]
275 .  ds hex# 000000\\*[hex#]\"
276 .  substring hex# -6
277 .  ds #random #\\*[hex#]\"
278 ..
279 .
280 .
281 .\"     ******************************************************************
282 .\"     **                      minimal Page setup                      **
283 .\"     ******************************************************************
284 .
285 .nr s \n[.ps]
286 .nr v \n[.v]
287 .nr p \n[.p]
288 .nr o \n[.o]
289 .pv 1.2 1.2 "" X
290 .nr l 6.6i                              \"      set text width
291 .ll \n[t*l]u
292 .nr o 2c                                \"      set offset
293 .po \n[o]u
294 .nr p 29.7c                             \"      set paper length (A4)
295 .pl \n[p]u
296 .nr tH 1i                               \"      set top margin
297 .sp |\n[tH]u
298 .
299 .ds t*HM //arbitrary text for page header, except on the first page//\"
300 .ds t*BM //arbitrary text for page footer, except on the last page/\\n[%]/\"
301 .
302 .ev 99
303 .lt \n[t*l]u
304 .ev
305 .
306 .
307 .de HM
308 .  sp |.5i                              \"      print header in top margin
309 .  tl \\*[t*HM]
310 .  sp |\\n[tH]u
311 .  ev
312 ..
313 .
314 .
315 .de BM
316 .  ev 99
317 .  sp |(\\n[p]u - .5i)                  \"      print footer in bottom margin
318 .  tl \\*[t*BM]
319 .  bp
320 ..
321 .
322 .
323 .de EM
324 .  rm BM                        \"      no page number at bottom of last page
325 .  t*EM
326 ..
327 .
328 .
329 .wh 0 HM
330 .wh 0-1.5i BM
331 .em EM
332 .
333 .\" Some packages (-mm) define their own .SP macro.
334 .\" Use ours if another one isn't already available.
335 .if !d SP .als SP t*SP
336 .
337 .if "\n[.m]"" \
338 .  gcolor black
339 .if "\n[.M]"" \
340 .  fcolor white
341 .
342 .\" EOF