]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
1 /* $Id: mandoc.c,v 1.113 2018/12/18 22:00:02 schwarze Exp $ */
3 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2015, 2017, 2018 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
31 #include "mandoc_aux.h"
34 #include "libmandoc.h"
36 static int a2time(time_t *, const char *, const char *);
37 static char *time2a(time_t);
41 mandoc_font(const char *cp
, int sz
)
45 return ESCAPE_FONTPREV
;
50 return ESCAPE_FONTBOLD
;
53 return ESCAPE_FONTITALIC
;
55 return ESCAPE_FONTPREV
;
58 return ESCAPE_FONTROMAN
;
76 return ESCAPE_FONTBOLD
;
78 return ESCAPE_FONTITALIC
;
94 mandoc_escape(const char **end
, const char **start
, int *sz
)
96 const char *local_start
;
102 * When the caller doesn't provide return storage,
107 start
= &local_start
;
112 * Treat "\E" just like "\";
113 * it only makes a difference in copy mode.
120 * Beyond the backslash, at least one input character
121 * is part of the escape sequence. With one exception
122 * (see below), that character won't be returned.
130 switch ((*start
)[-1]) {
132 * First the glyphs. There are several different forms of
133 * these, but each eventually returns a substring of the glyph
137 gly
= ESCAPE_SPECIAL
;
141 if (**start
== ' ') {
145 gly
= ESCAPE_SPECIAL
;
152 gly
= ESCAPE_SPECIAL
;
157 * Escapes taking no arguments at all.
161 return ESCAPE_UNSUPP
;
176 return ESCAPE_IGNORE
;
178 return ESCAPE_NOSPACE
;
183 * The \z escape is supposed to output the following
184 * character without advancing the cursor position.
185 * Since we are mostly dealing with terminal mode,
186 * let us just skip the next character.
189 return ESCAPE_SKIPCHAR
;
192 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
193 * 'X' is the trigger. These have opaque sub-strings.
205 gly
= (*start
)[-1] == 'f' ? ESCAPE_FONT
: ESCAPE_IGNORE
;
208 if ((*start
)[-1] == 'O')
214 if ((*start
)[-1] == 'O')
215 gly
= (*start
)[1] == '5' ?
216 ESCAPE_UNSUPP
: ESCAPE_ERROR
;
221 if ((*start
)[-1] == 'O') {
241 if (strncmp(*start
, "(.T", 3) != 0)
249 * These escapes are of the form \X'Y', where 'X' is the trigger
250 * and 'Y' is any string. These have opaque sub-strings.
251 * The \B and \w escapes are handled in roff.c, roff_res().
264 if (gly
== ESCAPE_ERROR
)
265 gly
= ESCAPE_OVERSTRIKE
;
271 * These escapes are of the form \X'N', where 'X' is the trigger
272 * and 'N' resolves to a numerical expression.
281 if (strchr(" %&()*+-./0123456789:<=>", **start
)) {
286 switch ((*start
)[-1]) {
302 * Special handling for the numbered character escape.
303 * XXX Do any other escapes need similar handling?
309 if (isdigit((unsigned char)**start
)) {
311 return ESCAPE_IGNORE
;
314 while (isdigit((unsigned char)**end
))
319 return ESCAPE_NUMBERED
;
322 * Sizes get a special category of their own.
327 /* See +/- counts as a sign. */
328 if ('+' == **end
|| '-' == **end
|| ASCII_HYPH
== **end
)
347 *sz
= (*end
)[-1] == 's' &&
348 isdigit((unsigned char)(*end
)[1]) ? 2 : 1;
358 * Several special characters can be encoded as
359 * one-byte escape sequences without using \[].
371 gly
= ESCAPE_SPECIAL
;
374 if (gly
== ESCAPE_ERROR
)
382 * Read up to the terminating character,
383 * paying attention to nested escapes.
387 while (**end
!= term
) {
394 mandoc_escape(end
, NULL
, NULL
))
402 *sz
= (*end
)++ - *start
;
405 * The file chars.c only provides one common list
406 * of character names, but \[-] == \- is the only
407 * one of the characters with one-byte names that
408 * allows enclosing the name in brackets.
410 if (gly
== ESCAPE_SPECIAL
&& *sz
== 1 && **start
!= '-')
414 if ((size_t)*sz
> strlen(*start
))
419 /* Run post-processors. */
423 gly
= mandoc_font(*start
, *sz
);
426 if (**start
== 'c') {
427 if (*sz
< 6 || *sz
> 7 ||
428 strncmp(*start
, "char", 4) != 0 ||
429 (int)strspn(*start
+ 4, "0123456789") + 4 < *sz
)
432 for (i
= 4; i
< *sz
; i
++)
433 c
= 10 * c
+ ((*start
)[i
] - '0');
434 if (c
< 0x21 || (c
> 0x7e && c
< 0xa0) || c
> 0xff)
438 gly
= ESCAPE_NUMBERED
;
443 * Unicode escapes are defined in groff as \[u0000]
444 * to \[u10FFFF], where the contained value must be
445 * a valid Unicode codepoint. Here, however, only
446 * check the length and range.
448 if (**start
!= 'u' || *sz
< 5 || *sz
> 7)
450 if (*sz
== 7 && ((*start
)[1] != '1' || (*start
)[2] != '0'))
452 if (*sz
== 6 && (*start
)[1] == '0')
454 if (*sz
== 5 && (*start
)[1] == 'D' &&
455 strchr("89ABCDEF", (*start
)[2]) != NULL
)
457 if ((int)strspn(*start
+ 1, "0123456789ABCDEFabcdef")
459 gly
= ESCAPE_UNICODE
;
469 a2time(time_t *t
, const char *fmt
, const char *p
)
474 memset(&tm
, 0, sizeof(struct tm
));
478 pp
= strptime(p
, fmt
, &tm
);
480 if (NULL
!= pp
&& '\0' == *pp
) {
502 * up to 9 characters for the month (September) + blank
503 * up to 2 characters for the day + comma + blank
504 * 4 characters for the year and a terminating '\0'
507 p
= buf
= mandoc_malloc(10 + 4 + 4 + 1);
509 if ((ssz
= strftime(p
, 10 + 1, "%B ", tm
)) == 0)
514 * The output format is just "%d" here, not "%2d" or "%02d".
515 * That's also the reason why we can't just format the
516 * date as a whole with "%B %e, %Y" or "%B %d, %Y".
517 * Besides, the present approach is less prone to buffer
518 * overflows, in case anybody should ever introduce the bug
519 * of looking at LC_TIME.
522 if ((isz
= snprintf(p
, 4 + 1, "%d, ", tm
->tm_mday
)) == -1)
526 if (strftime(p
, 4 + 1, "%Y", tm
) == 0)
536 mandoc_normdate(struct roff_man
*man
, char *in
, int ln
, int pos
)
541 /* No date specified: use today's date. */
543 if (in
== NULL
|| *in
== '\0' || strcmp(in
, "$" "Mdocdate$") == 0) {
544 mandoc_msg(MANDOCERR_DATE_MISSING
, ln
, pos
, NULL
);
545 return time2a(time(NULL
));
548 /* Valid mdoc(7) date format. */
550 if (a2time(&t
, "$" "Mdocdate: %b %d %Y $", in
) ||
551 a2time(&t
, "%b %d, %Y", in
)) {
553 if (t
> time(NULL
) + 86400)
554 mandoc_msg(MANDOCERR_DATE_FUTURE
, ln
, pos
, "%s", cp
);
555 else if (*in
!= '$' && strcmp(in
, cp
) != 0)
556 mandoc_msg(MANDOCERR_DATE_NORM
, ln
, pos
, "%s", cp
);
560 /* In man(7), do not warn about the legacy format. */
562 if (a2time(&t
, "%Y-%m-%d", in
) == 0)
563 mandoc_msg(MANDOCERR_DATE_BAD
, ln
, pos
, "%s", in
);
564 else if (t
> time(NULL
) + 86400)
565 mandoc_msg(MANDOCERR_DATE_FUTURE
, ln
, pos
, "%s", in
);
566 else if (man
->macroset
== MACROSET_MDOC
)
567 mandoc_msg(MANDOCERR_DATE_LEGACY
, ln
, pos
, "Dd %s", in
);
569 /* Use any non-mdoc(7) date verbatim. */
571 return mandoc_strdup(in
);
575 mandoc_eos(const char *p
, size_t sz
)
584 * End-of-sentence recognition must include situations where
585 * some symbols, such as `)', allow prior EOS punctuation to
589 enclosed
= found
= 0;
590 for (q
= p
+ (int)sz
- 1; q
>= p
; q
--) {
606 (!enclosed
|| isalnum((unsigned char)*q
));
610 return found
&& !enclosed
;
614 * Convert a string to a long that may not be <0.
615 * If the string is invalid, or is less than 0, return -1.
618 mandoc_strntoi(const char *p
, size_t sz
, int base
)
631 v
= strtol(buf
, &ep
, base
);
633 if (buf
[0] == '\0' || *ep
!= '\0')