]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
1 /* $Id: mandoc.c,v 1.111 2018/12/15 19:30:26 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_escape(const char **end
, const char **start
, int *sz
)
43 const char *local_start
;
49 * When the caller doesn't provide return storage,
59 * Treat "\E" just like "\";
60 * it only makes a difference in copy mode.
67 * Beyond the backslash, at least one input character
68 * is part of the escape sequence. With one exception
69 * (see below), that character won't be returned.
77 switch ((*start
)[-1]) {
79 * First the glyphs. There are several different forms of
80 * these, but each eventually returns a substring of the glyph
104 * Escapes taking no arguments at all.
108 return ESCAPE_UNSUPP
;
123 return ESCAPE_IGNORE
;
125 return ESCAPE_NOSPACE
;
130 * The \z escape is supposed to output the following
131 * character without advancing the cursor position.
132 * Since we are mostly dealing with terminal mode,
133 * let us just skip the next character.
136 return ESCAPE_SKIPCHAR
;
139 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
140 * 'X' is the trigger. These have opaque sub-strings.
152 gly
= (*start
)[-1] == 'f' ? ESCAPE_FONT
: ESCAPE_IGNORE
;
155 if ((*start
)[-1] == 'O')
161 if ((*start
)[-1] == 'O')
162 gly
= (*start
)[1] == '5' ?
163 ESCAPE_UNSUPP
: ESCAPE_ERROR
;
168 if ((*start
)[-1] == 'O') {
188 if (strncmp(*start
, "(.T", 3) != 0)
196 * These escapes are of the form \X'Y', where 'X' is the trigger
197 * and 'Y' is any string. These have opaque sub-strings.
198 * The \B and \w escapes are handled in roff.c, roff_res().
211 if (gly
== ESCAPE_ERROR
)
212 gly
= ESCAPE_OVERSTRIKE
;
218 * These escapes are of the form \X'N', where 'X' is the trigger
219 * and 'N' resolves to a numerical expression.
228 if (strchr(" %&()*+-./0123456789:<=>", **start
)) {
233 switch ((*start
)[-1]) {
249 * Special handling for the numbered character escape.
250 * XXX Do any other escapes need similar handling?
256 if (isdigit((unsigned char)**start
)) {
258 return ESCAPE_IGNORE
;
261 while (isdigit((unsigned char)**end
))
266 return ESCAPE_NUMBERED
;
269 * Sizes get a special category of their own.
274 /* See +/- counts as a sign. */
275 if ('+' == **end
|| '-' == **end
|| ASCII_HYPH
== **end
)
294 *sz
= (*end
)[-1] == 's' &&
295 isdigit((unsigned char)(*end
)[1]) ? 2 : 1;
305 * Several special characters can be encoded as
306 * one-byte escape sequences without using \[].
318 gly
= ESCAPE_SPECIAL
;
321 if (gly
== ESCAPE_ERROR
)
329 * Read up to the terminating character,
330 * paying attention to nested escapes.
334 while (**end
!= term
) {
341 mandoc_escape(end
, NULL
, NULL
))
349 *sz
= (*end
)++ - *start
;
352 * The file chars.c only provides one common list
353 * of character names, but \[-] == \- is the only
354 * one of the characters with one-byte names that
355 * allows enclosing the name in brackets.
357 if (gly
== ESCAPE_SPECIAL
&& *sz
== 1 && **start
!= '-')
361 if ((size_t)*sz
> strlen(*start
))
366 /* Run post-processors. */
371 if (**start
== 'C') {
372 if ((*start
)[1] == 'W' ||
373 (*start
)[1] == 'R') {
378 * Treat other constant-width font modes
379 * just like regular font modes.
384 if ((*start
)[0] == 'B' && (*start
)[1] == 'I')
388 } else if (*sz
!= 1) {
390 gly
= ESCAPE_FONTPREV
;
397 gly
= ESCAPE_FONTBOLD
;
401 gly
= ESCAPE_FONTITALIC
;
404 gly
= ESCAPE_FONTPREV
;
408 gly
= ESCAPE_FONTROMAN
;
413 if (**start
== 'c') {
414 if (*sz
< 6 || *sz
> 7 ||
415 strncmp(*start
, "char", 4) != 0 ||
416 (int)strspn(*start
+ 4, "0123456789") + 4 < *sz
)
419 for (i
= 4; i
< *sz
; i
++)
420 c
= 10 * c
+ ((*start
)[i
] - '0');
421 if (c
< 0x21 || (c
> 0x7e && c
< 0xa0) || c
> 0xff)
425 gly
= ESCAPE_NUMBERED
;
430 * Unicode escapes are defined in groff as \[u0000]
431 * to \[u10FFFF], where the contained value must be
432 * a valid Unicode codepoint. Here, however, only
433 * check the length and range.
435 if (**start
!= 'u' || *sz
< 5 || *sz
> 7)
437 if (*sz
== 7 && ((*start
)[1] != '1' || (*start
)[2] != '0'))
439 if (*sz
== 6 && (*start
)[1] == '0')
441 if (*sz
== 5 && (*start
)[1] == 'D' &&
442 strchr("89ABCDEF", (*start
)[2]) != NULL
)
444 if ((int)strspn(*start
+ 1, "0123456789ABCDEFabcdef")
446 gly
= ESCAPE_UNICODE
;
456 * Parse a quoted or unquoted roff-style request or macro argument.
457 * Return a pointer to the parsed argument, which is either the original
458 * pointer or advanced by one byte in case the argument is quoted.
459 * NUL-terminate the argument in place.
460 * Collapse pairs of quotes inside quoted arguments.
461 * Advance the argument pointer to the next argument,
462 * or to the NUL byte terminating the argument line.
465 mandoc_getarg(char **cpp
, int ln
, int *pos
)
468 int quoted
, pairs
, white
;
470 /* Quoting can only start with a new word. */
480 for (cp
= start
; '\0' != *cp
; cp
++) {
483 * Move the following text left
484 * after quoted quotes and after "\\" and "\t".
491 * In copy mode, translate double to single
492 * backslashes and backslash-t to literal tabs.
504 /* Skip escaped blanks. */
511 } else if (0 == quoted
) {
513 /* Unescaped blanks end unquoted args. */
517 } else if ('"' == cp
[0]) {
519 /* Quoted quotes collapse. */
523 /* Unquoted quotes end quoted args. */
530 /* Quoted argument without a closing quote. */
532 mandoc_msg(MANDOCERR_ARG_QUOTE
, ln
, *pos
, NULL
);
534 /* NUL-terminate this argument and move to the next one. */
542 *pos
+= (int)(cp
- start
) + (quoted
? 1 : 0);
545 if ('\0' == *cp
&& (white
|| ' ' == cp
[-1]))
546 mandoc_msg(MANDOCERR_SPACE_EOL
, ln
, *pos
, NULL
);
552 a2time(time_t *t
, const char *fmt
, const char *p
)
557 memset(&tm
, 0, sizeof(struct tm
));
561 pp
= strptime(p
, fmt
, &tm
);
563 if (NULL
!= pp
&& '\0' == *pp
) {
585 * up to 9 characters for the month (September) + blank
586 * up to 2 characters for the day + comma + blank
587 * 4 characters for the year and a terminating '\0'
590 p
= buf
= mandoc_malloc(10 + 4 + 4 + 1);
592 if ((ssz
= strftime(p
, 10 + 1, "%B ", tm
)) == 0)
597 * The output format is just "%d" here, not "%2d" or "%02d".
598 * That's also the reason why we can't just format the
599 * date as a whole with "%B %e, %Y" or "%B %d, %Y".
600 * Besides, the present approach is less prone to buffer
601 * overflows, in case anybody should ever introduce the bug
602 * of looking at LC_TIME.
605 if ((isz
= snprintf(p
, 4 + 1, "%d, ", tm
->tm_mday
)) == -1)
609 if (strftime(p
, 4 + 1, "%Y", tm
) == 0)
619 mandoc_normdate(struct roff_man
*man
, char *in
, int ln
, int pos
)
624 /* No date specified: use today's date. */
626 if (in
== NULL
|| *in
== '\0' || strcmp(in
, "$" "Mdocdate$") == 0) {
627 mandoc_msg(MANDOCERR_DATE_MISSING
, ln
, pos
, NULL
);
628 return time2a(time(NULL
));
631 /* Valid mdoc(7) date format. */
633 if (a2time(&t
, "$" "Mdocdate: %b %d %Y $", in
) ||
634 a2time(&t
, "%b %d, %Y", in
)) {
636 if (t
> time(NULL
) + 86400)
637 mandoc_msg(MANDOCERR_DATE_FUTURE
, ln
, pos
, "%s", cp
);
638 else if (*in
!= '$' && strcmp(in
, cp
) != 0)
639 mandoc_msg(MANDOCERR_DATE_NORM
, ln
, pos
, "%s", cp
);
643 /* In man(7), do not warn about the legacy format. */
645 if (a2time(&t
, "%Y-%m-%d", in
) == 0)
646 mandoc_msg(MANDOCERR_DATE_BAD
, ln
, pos
, "%s", in
);
647 else if (t
> time(NULL
) + 86400)
648 mandoc_msg(MANDOCERR_DATE_FUTURE
, ln
, pos
, "%s", in
);
649 else if (man
->macroset
== MACROSET_MDOC
)
650 mandoc_msg(MANDOCERR_DATE_LEGACY
, ln
, pos
, "Dd %s", in
);
652 /* Use any non-mdoc(7) date verbatim. */
654 return mandoc_strdup(in
);
658 mandoc_eos(const char *p
, size_t sz
)
667 * End-of-sentence recognition must include situations where
668 * some symbols, such as `)', allow prior EOS punctuation to
672 enclosed
= found
= 0;
673 for (q
= p
+ (int)sz
- 1; q
>= p
; q
--) {
689 (!enclosed
|| isalnum((unsigned char)*q
));
693 return found
&& !enclosed
;
697 * Convert a string to a long that may not be <0.
698 * If the string is invalid, or is less than 0, return -1.
701 mandoc_strntoi(const char *p
, size_t sz
, int base
)
714 v
= strtol(buf
, &ep
, base
);
716 if (buf
[0] == '\0' || *ep
!= '\0')