1 /* $Id: out.c,v 1.11 2009/11/12 08:21:05 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 extern size_t strlcat(char *, const char *, size_t);
53 * Convert a `scaling unit' to a consistent form, or fail. Scaling
54 * units are documented in groff.7, mdoc.7, man.7.
57 a2roffsu(const char *src
, struct roffsu
*dst
, enum roffscale def
)
59 char buf
[BUFSIZ
], hasd
;
83 if ( ! isdigit((u_char
)*src
)) {
94 if (BUFSIZ
== i
|| (*src
&& *(src
+ 1)))
122 if (SCALE_MAX
== def
)
139 if ((dst
->scale
= atof(buf
)) < 0)
149 * Correctly writes the time in nroff form, which differs from standard
150 * form in that a space isn't printed in lieu of the extra %e field for
151 * single-digit dates.
154 time2a(time_t t
, char *dst
, size_t sz
)
162 localtime_r(&t
, &tm
);
169 if (0 == (nsz
= strftime(p
, sz
, "%B ", &tm
)))
175 if (0 == strftime(buf
, sizeof(buf
), "%e, ", &tm
))
178 nsz
= strlcat(p
, buf
+ (' ' == buf
[0] ? 1 : 0), sz
);
186 (void)strftime(p
, sz
, "%Y", &tm
);
191 * Returns length of parsed string (the leading "\" should NOT be
192 * included). This can be zero if the current character is the nil
193 * terminator. "d" is set to the type of parsed decorator, which may
194 * have an adjoining "word" of size "sz" (e.g., "(ab" -> "ab", 2).
197 a2roffdeco(enum roffdeco
*d
,
198 const char **word
, size_t *sz
)
200 int j
, type
, term
, lim
;
214 if ('\0' == *(wp
+ 1))
230 if ('\0' == *(wp
+ 1))
263 if (*wp
== '+' || *wp
== '-')
273 return((int)(wp
- sp
));
283 if ( ! isdigit((u_char
)*wp
))
284 return((int)(wp
- sp
));
286 for (j
= 0; isdigit((u_char
)*wp
); j
++) {
292 if (term
&& term
< 3) {
293 if (1 == term
&& *wp
!= '\'')
294 return((int)(wp
- sp
));
295 if (2 == term
&& *wp
!= ']')
296 return((int)(wp
- sp
));
301 return((int)(wp
- sp
));
347 for (j
= 0; *wp
&& ']' != *wp
; wp
++, j
++)
353 *d
= type
? DECO_SPECIAL
: DECO_RESERVED
;