]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
1 /* $Id: mandoc.c,v 1.117 2020/01/19 16:44:50 schwarze Exp $ */
3 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2015, 2017-2020 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"
37 static int a2time(time_t *, const char *, const char *);
38 static char *time2a(time_t);
42 mandoc_font(const char *cp
, int sz
)
46 return ESCAPE_FONTPREV
;
51 return ESCAPE_FONTBOLD
;
54 return ESCAPE_FONTITALIC
;
56 return ESCAPE_FONTPREV
;
59 return ESCAPE_FONTROMAN
;
77 return ESCAPE_FONTBOLD
;
79 return ESCAPE_FONTITALIC
;
95 mandoc_escape(const char **end
, const char **start
, int *sz
)
97 const char *local_start
;
103 * When the caller doesn't provide return storage,
108 start
= &local_start
;
113 * Treat "\E" just like "\";
114 * it only makes a difference in copy mode.
121 * Beyond the backslash, at least one input character
122 * is part of the escape sequence. With one exception
123 * (see below), that character won't be returned.
131 switch ((*start
)[-1]) {
133 * First the glyphs. There are several different forms of
134 * these, but each eventually returns a substring of the glyph
138 gly
= ESCAPE_SPECIAL
;
142 if (**start
== ' ') {
146 gly
= ESCAPE_SPECIAL
;
153 gly
= ESCAPE_SPECIAL
;
158 * Escapes taking no arguments at all.
162 return ESCAPE_UNSUPP
;
177 return ESCAPE_IGNORE
;
179 return ESCAPE_NOSPACE
;
184 * The \z escape is supposed to output the following
185 * character without advancing the cursor position.
186 * Since we are mostly dealing with terminal mode,
187 * let us just skip the next character.
190 return ESCAPE_SKIPCHAR
;
193 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
194 * 'X' is the trigger. These have opaque sub-strings.
206 gly
= (*start
)[-1] == 'f' ? ESCAPE_FONT
: ESCAPE_IGNORE
;
209 if ((*start
)[-1] == 'O')
215 if ((*start
)[-1] == 'O')
216 gly
= (*start
)[1] == '5' ?
217 ESCAPE_UNSUPP
: ESCAPE_ERROR
;
222 if ((*start
)[-1] == 'O') {
242 if (strncmp(*start
, "(.T", 3) != 0)
250 * These escapes are of the form \X'Y', where 'X' is the trigger
251 * and 'Y' is any string. These have opaque sub-strings.
252 * The \B and \w escapes are handled in roff.c, roff_res().
265 if (gly
== ESCAPE_ERROR
)
266 gly
= ESCAPE_OVERSTRIKE
;
272 * These escapes are of the form \X'N', where 'X' is the trigger
273 * and 'N' resolves to a numerical expression.
282 if (strchr(" %&()*+-./0123456789:<=>", **start
)) {
287 switch ((*start
)[-1]) {
303 * Special handling for the numbered character escape.
304 * XXX Do any other escapes need similar handling?
310 if (isdigit((unsigned char)**start
)) {
312 return ESCAPE_IGNORE
;
315 while (isdigit((unsigned char)**end
))
320 return ESCAPE_NUMBERED
;
323 * Sizes get a special category of their own.
328 /* See +/- counts as a sign. */
329 if ('+' == **end
|| '-' == **end
|| ASCII_HYPH
== **end
)
348 *sz
= (*end
)[-1] == 's' &&
349 isdigit((unsigned char)(*end
)[1]) ? 2 : 1;
359 * Several special characters can be encoded as
360 * one-byte escape sequences without using \[].
372 gly
= ESCAPE_SPECIAL
;
375 if (gly
== ESCAPE_ERROR
)
383 * Read up to the terminating character,
384 * paying attention to nested escapes.
388 while (**end
!= term
) {
395 mandoc_escape(end
, NULL
, NULL
))
403 *sz
= (*end
)++ - *start
;
406 * The file chars.c only provides one common list
407 * of character names, but \[-] == \- is the only
408 * one of the characters with one-byte names that
409 * allows enclosing the name in brackets.
411 if (gly
== ESCAPE_SPECIAL
&& *sz
== 1 && **start
!= '-')
415 if ((size_t)*sz
> strlen(*start
))
420 /* Run post-processors. */
424 gly
= mandoc_font(*start
, *sz
);
427 if (**start
== 'c') {
428 if (*sz
< 6 || *sz
> 7 ||
429 strncmp(*start
, "char", 4) != 0 ||
430 (int)strspn(*start
+ 4, "0123456789") + 4 < *sz
)
433 for (i
= 4; i
< *sz
; i
++)
434 c
= 10 * c
+ ((*start
)[i
] - '0');
435 if (c
< 0x21 || (c
> 0x7e && c
< 0xa0) || c
> 0xff)
439 gly
= ESCAPE_NUMBERED
;
444 * Unicode escapes are defined in groff as \[u0000]
445 * to \[u10FFFF], where the contained value must be
446 * a valid Unicode codepoint. Here, however, only
447 * check the length and range.
449 if (**start
!= 'u' || *sz
< 5 || *sz
> 7)
451 if (*sz
== 7 && ((*start
)[1] != '1' || (*start
)[2] != '0'))
453 if (*sz
== 6 && (*start
)[1] == '0')
455 if (*sz
== 5 && (*start
)[1] == 'D' &&
456 strchr("89ABCDEF", (*start
)[2]) != NULL
)
458 if ((int)strspn(*start
+ 1, "0123456789ABCDEFabcdef")
460 gly
= ESCAPE_UNICODE
;
470 a2time(time_t *t
, const char *fmt
, const char *p
)
475 memset(&tm
, 0, sizeof(struct tm
));
479 pp
= strptime(p
, fmt
, &tm
);
481 if (NULL
!= pp
&& '\0' == *pp
) {
504 * up to 9 characters for the month (September) + blank
505 * up to 2 characters for the day + comma + blank
506 * 4 characters for the year and a terminating '\0'
509 p
= buf
= mandoc_malloc(10 + 4 + 4 + 1);
511 if ((ssz
= strftime(p
, 10 + 1, "%B ", tm
)) == 0)
516 * The output format is just "%d" here, not "%2d" or "%02d".
517 * That's also the reason why we can't just format the
518 * date as a whole with "%B %e, %Y" or "%B %d, %Y".
519 * Besides, the present approach is less prone to buffer
520 * overflows, in case anybody should ever introduce the bug
521 * of looking at LC_TIME.
524 isz
= snprintf(p
, 4 + 1, "%d, ", tm
->tm_mday
);
525 if (isz
< 0 || isz
> 4)
529 if (strftime(p
, 4 + 1, "%Y", tm
) == 0)
535 return mandoc_strdup("");
539 mandoc_normdate(struct roff_node
*nch
, struct roff_node
*nbl
)
544 /* No date specified. */
548 mandoc_msg(MANDOCERR_DATE_MISSING
, 0, 0, NULL
);
550 mandoc_msg(MANDOCERR_DATE_MISSING
, nbl
->line
,
551 nbl
->pos
, "%s", roff_name
[nbl
->tok
]);
552 return mandoc_strdup("");
554 if (*nch
->string
== '\0') {
555 mandoc_msg(MANDOCERR_DATE_MISSING
, nch
->line
,
556 nch
->pos
, "%s", roff_name
[nbl
->tok
]);
557 return mandoc_strdup("");
559 if (strcmp(nch
->string
, "$" "Mdocdate$") == 0)
560 return time2a(time(NULL
));
562 /* Valid mdoc(7) date format. */
564 if (a2time(&t
, "$" "Mdocdate: %b %d %Y $", nch
->string
) ||
565 a2time(&t
, "%b %d, %Y", nch
->string
)) {
567 if (t
> time(NULL
) + 86400)
568 mandoc_msg(MANDOCERR_DATE_FUTURE
, nch
->line
,
569 nch
->pos
, "%s %s", roff_name
[nbl
->tok
], cp
);
570 else if (*nch
->string
!= '$' &&
571 strcmp(nch
->string
, cp
) != 0)
572 mandoc_msg(MANDOCERR_DATE_NORM
, nch
->line
,
573 nch
->pos
, "%s %s", roff_name
[nbl
->tok
], cp
);
577 /* In man(7), do not warn about the legacy format. */
579 if (a2time(&t
, "%Y-%m-%d", nch
->string
) == 0)
580 mandoc_msg(MANDOCERR_DATE_BAD
, nch
->line
, nch
->pos
,
581 "%s %s", roff_name
[nbl
->tok
], nch
->string
);
582 else if (t
> time(NULL
) + 86400)
583 mandoc_msg(MANDOCERR_DATE_FUTURE
, nch
->line
, nch
->pos
,
584 "%s %s", roff_name
[nbl
->tok
], nch
->string
);
585 else if (nbl
->tok
== MDOC_Dd
)
586 mandoc_msg(MANDOCERR_DATE_LEGACY
, nch
->line
, nch
->pos
,
587 "Dd %s", nch
->string
);
589 /* Use any non-mdoc(7) date verbatim. */
591 return mandoc_strdup(nch
->string
);
595 mandoc_eos(const char *p
, size_t sz
)
604 * End-of-sentence recognition must include situations where
605 * some symbols, such as `)', allow prior EOS punctuation to
609 enclosed
= found
= 0;
610 for (q
= p
+ (int)sz
- 1; q
>= p
; q
--) {
626 (!enclosed
|| isalnum((unsigned char)*q
));
630 return found
&& !enclosed
;
634 * Convert a string to a long that may not be <0.
635 * If the string is invalid, or is less than 0, return -1.
638 mandoc_strntoi(const char *p
, size_t sz
, int base
)
651 v
= strtol(buf
, &ep
, base
);
653 if (buf
[0] == '\0' || *ep
!= '\0')