1 /* $Id: out.c,v 1.12 2010/01/01 17:14:30 kristaps Exp $ */
3 * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
28 /* See a2roffdeco(). */
29 #define C2LIM(c, l) do { \
31 if ('[' == (c) || '\'' == (c)) \
33 else if ('(' == (c)) \
35 while (/* CONSTCOND */ 0)
37 /* See a2roffdeco(). */
38 #define C2TERM(c, t) do { \
42 else if ('[' == (c)) \
44 else if ('(' == (c)) \
46 while (/* CONSTCOND */ 0)
49 * Convert a `scaling unit' to a consistent form, or fail. Scaling
50 * units are documented in groff.7, mdoc.7, man.7.
53 a2roffsu(const char *src
, struct roffsu
*dst
, enum roffscale def
)
55 char buf
[BUFSIZ
], hasd
;
79 if ( ! isdigit((u_char
)*src
)) {
90 if (BUFSIZ
== i
|| (*src
&& *(src
+ 1)))
118 if (SCALE_MAX
== def
)
135 if ((dst
->scale
= atof(buf
)) < 0)
145 * Correctly writes the time in nroff form, which differs from standard
146 * form in that a space isn't printed in lieu of the extra %e field for
147 * single-digit dates.
150 time2a(time_t t
, char *dst
, size_t sz
)
158 localtime_r(&t
, &tm
);
165 if (0 == (nsz
= strftime(p
, sz
, "%B ", &tm
)))
171 if (0 == strftime(buf
, sizeof(buf
), "%e, ", &tm
))
174 nsz
= strlcat(p
, buf
+ (' ' == buf
[0] ? 1 : 0), sz
);
182 (void)strftime(p
, sz
, "%Y", &tm
);
187 * Returns length of parsed string (the leading "\" should NOT be
188 * included). This can be zero if the current character is the nil
189 * terminator. "d" is set to the type of parsed decorator, which may
190 * have an adjoining "word" of size "sz" (e.g., "(ab" -> "ab", 2).
193 a2roffdeco(enum roffdeco
*d
,
194 const char **word
, size_t *sz
)
196 int j
, type
, term
, lim
;
210 if ('\0' == *(wp
+ 1))
226 if ('\0' == *(wp
+ 1))
259 if (*wp
== '+' || *wp
== '-')
269 return((int)(wp
- sp
));
279 if ( ! isdigit((u_char
)*wp
))
280 return((int)(wp
- sp
));
282 for (j
= 0; isdigit((u_char
)*wp
); j
++) {
288 if (term
&& term
< 3) {
289 if (1 == term
&& *wp
!= '\'')
290 return((int)(wp
- sp
));
291 if (2 == term
&& *wp
!= ']')
292 return((int)(wp
- sp
));
297 return((int)(wp
- sp
));
343 for (j
= 0; *wp
&& ']' != *wp
; wp
++, j
++)
349 *d
= type
? DECO_SPECIAL
: DECO_RESERVED
;