1 /* $Id: mandoc.c,v 1.114 2018/12/30 00:49:55 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"
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) {
503 * up to 9 characters for the month (September) + blank
504 * up to 2 characters for the day + comma + blank
505 * 4 characters for the year and a terminating '\0'
508 p = buf = mandoc_malloc(10 + 4 + 4 + 1);
510 if ((ssz = strftime(p, 10 + 1, "%B ", tm)) == 0)
515 * The output format is just "%d" here, not "%2d" or "%02d".
516 * That's also the reason why we can't just format the
517 * date as a whole with "%B %e, %Y" or "%B %d, %Y".
518 * Besides, the present approach is less prone to buffer
519 * overflows, in case anybody should ever introduce the bug
520 * of looking at LC_TIME.
523 if ((isz = snprintf(p, 4 + 1, "%d, ", tm->tm_mday)) == -1)
527 if (strftime(p, 4 + 1, "%Y", tm) == 0)
537 mandoc_normdate(struct roff_man *man, char *in, int ln, int pos)
542 /* No date specified: use today's date. */
544 if (in == NULL || *in == '\0' || strcmp(in, "$" "Mdocdate$") == 0) {
545 mandoc_msg(MANDOCERR_DATE_MISSING, ln, pos, NULL);
546 return time2a(time(NULL));
549 /* Valid mdoc(7) date format. */
551 if (a2time(&t, "$" "Mdocdate: %b %d %Y $", in) ||
552 a2time(&t, "%b %d, %Y", in)) {
554 if (t > time(NULL) + 86400)
555 mandoc_msg(MANDOCERR_DATE_FUTURE, ln, pos, "%s", cp);
556 else if (*in != '$' && strcmp(in, cp) != 0)
557 mandoc_msg(MANDOCERR_DATE_NORM, ln, pos, "%s", cp);
561 /* In man(7), do not warn about the legacy format. */
563 if (a2time(&t, "%Y-%m-%d", in) == 0)
564 mandoc_msg(MANDOCERR_DATE_BAD, ln, pos, "%s", in);
565 else if (t > time(NULL) + 86400)
566 mandoc_msg(MANDOCERR_DATE_FUTURE, ln, pos, "%s", in);
567 else if (man->meta.macroset == MACROSET_MDOC)
568 mandoc_msg(MANDOCERR_DATE_LEGACY, ln, pos, "Dd %s", in);
570 /* Use any non-mdoc(7) date verbatim. */
572 return mandoc_strdup(in);
576 mandoc_eos(const char *p, size_t sz)
585 * End-of-sentence recognition must include situations where
586 * some symbols, such as `)', allow prior EOS punctuation to
590 enclosed = found = 0;
591 for (q = p + (int)sz - 1; q >= p; q--) {
607 (!enclosed || isalnum((unsigned char)*q));
611 return found && !enclosed;
615 * Convert a string to a long that may not be <0.
616 * If the string is invalid, or is less than 0, return -1.
619 mandoc_strntoi(const char *p, size_t sz, int base)
632 v = strtol(buf, &ep, base);
634 if (buf[0] == '\0' || *ep != '\0')