1 /* $Id: mandoc.c,v 1.119 2021/08/10 12:55:03 schwarze Exp $ */
3 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2015, 2017-2021 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;
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.
207 switch ((*start)[-1]) {
220 if ((*start)[-1] == 'O')
226 if ((*start)[-1] == 'O')
227 gly = (*start)[1] == '5' ?
228 ESCAPE_UNSUPP : ESCAPE_ERROR;
233 if ((*start)[-1] == 'O') {
254 * These escapes are of the form \X'Y', where 'X' is the trigger
255 * and 'Y' is any string. These have opaque sub-strings.
256 * The \B and \w escapes are handled in roff.c, roff_res().
269 if (gly == ESCAPE_ERROR)
270 gly = ESCAPE_OVERSTRIKE;
276 * These escapes are of the form \X'N', where 'X' is the trigger
277 * and 'N' resolves to a numerical expression.
286 if (strchr(" %&()*+-./0123456789:<=>", **start)) {
291 switch ((*start)[-1]) {
307 * Special handling for the numbered character escape.
308 * XXX Do any other escapes need similar handling?
314 if (isdigit((unsigned char)**start)) {
316 return ESCAPE_IGNORE;
319 while (isdigit((unsigned char)**end))
324 return ESCAPE_NUMBERED;
327 * Sizes get a special category of their own.
332 /* See +/- counts as a sign. */
333 if ('+' == **end || '-' == **end || ASCII_HYPH == **end)
352 *sz = (*end)[-1] == 's' &&
353 isdigit((unsigned char)(*end)[1]) ? 2 : 1;
363 * Several special characters can be encoded as
364 * one-byte escape sequences without using \[].
376 gly = ESCAPE_SPECIAL;
379 if (gly == ESCAPE_ERROR)
387 * Read up to the terminating character,
388 * paying attention to nested escapes.
392 while (**end != term) {
399 mandoc_escape(end, NULL, NULL))
407 *sz = (*end)++ - *start;
410 * The file chars.c only provides one common list
411 * of character names, but \[-] == \- is the only
412 * one of the characters with one-byte names that
413 * allows enclosing the name in brackets.
415 if (gly == ESCAPE_SPECIAL && *sz == 1 && **start != '-')
419 if ((size_t)*sz > strlen(*start))
424 /* Run post-processors. */
428 gly = mandoc_font(*start, *sz);
431 if (**start == 'c') {
432 if (*sz < 6 || *sz > 7 ||
433 strncmp(*start, "char", 4) != 0 ||
434 (int)strspn(*start + 4, "0123456789") + 4 < *sz)
437 for (i = 4; i < *sz; i++)
438 c = 10 * c + ((*start)[i] - '0');
439 if (c < 0x21 || (c > 0x7e && c < 0xa0) || c > 0xff)
443 gly = ESCAPE_NUMBERED;
448 * Unicode escapes are defined in groff as \[u0000]
449 * to \[u10FFFF], where the contained value must be
450 * a valid Unicode codepoint. Here, however, only
451 * check the length and range.
453 if (**start != 'u' || *sz < 5 || *sz > 7)
455 if (*sz == 7 && ((*start)[1] != '1' || (*start)[2] != '0'))
457 if (*sz == 6 && (*start)[1] == '0')
459 if (*sz == 5 && (*start)[1] == 'D' &&
460 strchr("89ABCDEF", (*start)[2]) != NULL)
462 if ((int)strspn(*start + 1, "0123456789ABCDEFabcdef")
464 gly = ESCAPE_UNICODE;
467 assert(*sz == 2 && (*start)[0] == '.' && (*start)[1] == 'T');
477 a2time(time_t *t, const char *fmt, const char *p)
482 memset(&tm, 0, sizeof(struct tm));
486 pp = strptime(p, fmt, &tm);
488 if (NULL != pp && '\0' == *pp) {
511 * up to 9 characters for the month (September) + blank
512 * up to 2 characters for the day + comma + blank
513 * 4 characters for the year and a terminating '\0'
516 p = buf = mandoc_malloc(10 + 4 + 4 + 1);
518 if ((ssz = strftime(p, 10 + 1, "%B ", tm)) == 0)
523 * The output format is just "%d" here, not "%2d" or "%02d".
524 * That's also the reason why we can't just format the
525 * date as a whole with "%B %e, %Y" or "%B %d, %Y".
526 * Besides, the present approach is less prone to buffer
527 * overflows, in case anybody should ever introduce the bug
528 * of looking at LC_TIME.
531 isz = snprintf(p, 4 + 1, "%d, ", tm->tm_mday);
532 if (isz < 0 || isz > 4)
536 if (strftime(p, 4 + 1, "%Y", tm) == 0)
542 return mandoc_strdup("");
546 mandoc_normdate(struct roff_node *nch, struct roff_node *nbl)
551 /* No date specified. */
555 mandoc_msg(MANDOCERR_DATE_MISSING, 0, 0, NULL);
557 mandoc_msg(MANDOCERR_DATE_MISSING, nbl->line,
558 nbl->pos, "%s", roff_name[nbl->tok]);
559 return mandoc_strdup("");
561 if (*nch->string == '\0') {
562 mandoc_msg(MANDOCERR_DATE_MISSING, nch->line,
563 nch->pos, "%s", roff_name[nbl->tok]);
564 return mandoc_strdup("");
566 if (strcmp(nch->string, "$" "Mdocdate$") == 0)
567 return time2a(time(NULL));
569 /* Valid mdoc(7) date format. */
571 if (a2time(&t, "$" "Mdocdate: %b %d %Y $", nch->string) ||
572 a2time(&t, "%b %d, %Y", nch->string)) {
574 if (t > time(NULL) + 86400)
575 mandoc_msg(MANDOCERR_DATE_FUTURE, nch->line,
576 nch->pos, "%s %s", roff_name[nbl->tok], cp);
577 else if (*nch->string != '$' &&
578 strcmp(nch->string, cp) != 0)
579 mandoc_msg(MANDOCERR_DATE_NORM, nch->line,
580 nch->pos, "%s %s", roff_name[nbl->tok], cp);
584 /* In man(7), do not warn about the legacy format. */
586 if (a2time(&t, "%Y-%m-%d", nch->string) == 0)
587 mandoc_msg(MANDOCERR_DATE_BAD, nch->line, nch->pos,
588 "%s %s", roff_name[nbl->tok], nch->string);
589 else if (t > time(NULL) + 86400)
590 mandoc_msg(MANDOCERR_DATE_FUTURE, nch->line, nch->pos,
591 "%s %s", roff_name[nbl->tok], nch->string);
592 else if (nbl->tok == MDOC_Dd)
593 mandoc_msg(MANDOCERR_DATE_LEGACY, nch->line, nch->pos,
594 "Dd %s", nch->string);
596 /* Use any non-mdoc(7) date verbatim. */
598 return mandoc_strdup(nch->string);
602 mandoc_eos(const char *p, size_t sz)
611 * End-of-sentence recognition must include situations where
612 * some symbols, such as `)', allow prior EOS punctuation to
616 enclosed = found = 0;
617 for (q = p + (int)sz - 1; q >= p; q--) {
633 (!enclosed || isalnum((unsigned char)*q));
637 return found && !enclosed;
641 * Convert a string to a long that may not be <0.
642 * If the string is invalid, or is less than 0, return -1.
645 mandoc_strntoi(const char *p, size_t sz, int base)
658 v = strtol(buf, &ep, base);
660 if (buf[0] == '\0' || *ep != '\0')