1 /* $Id: mandoc.c,v 1.53 2011/05/24 21:31:23 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011 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.
22 #include <sys/types.h>
34 #include "libmandoc.h"
38 static int a2time(time_t *, const char *, const char *);
39 static char *time2a(time_t);
40 static int numescape(const char *);
43 * Pass over recursive numerical expressions. This context of this
44 * function is important: it's only called within character-terminating
45 * escapes (e.g., \s[xxxyyy]), so all we need to do is handle initial
46 * recursion: we don't care about what's in these blocks.
47 * This returns the number of characters skipped or -1 if an error
48 * occurs (the caller should bail).
51 numescape(const char *start)
59 /* The expression consists of a subexpression. */
61 if ('\\' == start[i]) {
64 * Read past the end of the subexpression.
65 * Bail immediately on errors.
67 if (ESCAPE_ERROR == mandoc_escape(&cp, NULL, NULL))
69 return(i + cp - &start[i]);
72 if ('(' != start[i++])
76 * A parenthesised subexpression. Read until the closing
77 * parenthesis, making sure to handle any nested subexpressions
78 * that might ruin our parse.
81 while (')' != start[i]) {
82 sz = strcspn(&start[i], ")\\");
87 else if ('\\' != start[i])
91 if (ESCAPE_ERROR == mandoc_escape(&cp, NULL, NULL))
96 /* Read past the terminating ')'. */
101 mandoc_escape(const char **end, const char **start, int *sz)
103 char c, term, numeric;
104 int i, lim, ssz, rlim;
105 const char *cp, *rstart;
114 term = numeric = '\0';
116 switch ((c = cp[i++])) {
118 * First the glyphs. There are several different forms of
119 * these, but each eventually returns a substring of the glyph
123 gly = ESCAPE_SPECIAL;
127 gly = ESCAPE_SPECIAL;
129 * Unicode escapes are defined in groff as \[uXXXX] to
130 * \[u10FFFF], where the contained value must be a valid
131 * Unicode codepoint. Here, however, only check whether
132 * it's not a zero-width escape.
134 if ('u' == cp[i] && ']' != cp[i + 1])
135 gly = ESCAPE_UNICODE;
140 return(ESCAPE_ERROR);
141 gly = ESCAPE_SPECIAL;
146 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
147 * 'X' is the trigger. These have opaque sub-strings.
164 if (ESCAPE_ERROR == gly)
168 if (ESCAPE_ERROR == gly)
190 * These escapes are of the form \X'Y', where 'X' is the trigger
191 * and 'Y' is any string. These have opaque sub-strings.
207 return(ESCAPE_ERROR);
213 * These escapes are of the form \X'N', where 'X' is the trigger
214 * and 'N' resolves to a numerical expression.
227 if (ESCAPE_ERROR == gly)
228 gly = ESCAPE_NUMBERED;
237 if (ESCAPE_ERROR == gly)
240 return(ESCAPE_ERROR);
241 term = numeric = '\'';
245 * Sizes get a special category of their own.
254 /* See +/- counts as a sign. */
256 if ('+' == c || '-' == c || ASCII_HYPH == c)
264 term = numeric = ']';
267 term = numeric = '\'';
275 /* See +/- counts as a sign. */
277 if ('+' == c || '-' == c || ASCII_HYPH == c)
283 * Anything else is assumed to be a glyph.
286 gly = ESCAPE_SPECIAL;
292 assert(ESCAPE_ERROR != gly);
299 * If a terminating block has been specified, we need to
300 * handle the case of recursion, which could have their
301 * own terminating blocks that mess up our parse. This, by the
302 * way, means that the "start" and "size" values will be
303 * effectively meaningless.
307 if (numeric && -1 == (ssz = numescape(&cp[i])))
308 return(ESCAPE_ERROR);
314 * We have a character terminator. Try to read up to that
315 * character. If we can't (i.e., we hit the nil), then return
316 * an error; if we can, calculate our length, read past the
317 * terminating character, and exit.
321 *end = strchr(&cp[i], term);
323 return(ESCAPE_ERROR);
325 rlim = *end - &cp[i];
335 * We have a numeric limit. If the string is shorter than that,
336 * stop and return an error. Else adjust our endpoint, length,
337 * and return the current glyph.
340 if ((size_t)lim > strlen(&cp[i]))
341 return(ESCAPE_ERROR);
350 assert(rlim >= 0 && rstart);
352 /* Run post-processors. */
362 gly = ESCAPE_FONTBOLD;
367 gly = ESCAPE_FONTITALIC;
370 gly = ESCAPE_FONTPREV;
375 gly = ESCAPE_FONTROMAN;
379 case (ESCAPE_SPECIAL):
383 gly = ESCAPE_NOSPACE;
393 mandoc_calloc(size_t num, size_t size)
397 ptr = calloc(num, size);
400 exit((int)MANDOCLEVEL_SYSERR);
408 mandoc_malloc(size_t size)
415 exit((int)MANDOCLEVEL_SYSERR);
423 mandoc_realloc(void *ptr, size_t size)
426 ptr = realloc(ptr, size);
429 exit((int)MANDOCLEVEL_SYSERR);
437 mandoc_strdup(const char *ptr)
444 exit((int)MANDOCLEVEL_SYSERR);
451 * Parse a quoted or unquoted roff-style request or macro argument.
452 * Return a pointer to the parsed argument, which is either the original
453 * pointer or advanced by one byte in case the argument is quoted.
454 * Null-terminate the argument in place.
455 * Collapse pairs of quotes inside quoted arguments.
456 * Advance the argument pointer to the next argument,
457 * or to the null byte terminating the argument line.
460 mandoc_getarg(struct mparse *parse, char **cpp, int ln, int *pos)
463 int quoted, pairs, white;
465 /* Quoting can only start with a new word. */
475 for (cp = start; '\0' != *cp; cp++) {
476 /* Move left after quoted quotes and escaped backslashes. */
481 /* Poor man's copy mode. */
484 } else if (0 == quoted && ' ' == cp[1])
485 /* Skip escaped blanks. */
487 } else if (0 == quoted) {
489 /* Unescaped blanks end unquoted args. */
493 } else if ('"' == cp[0]) {
495 /* Quoted quotes collapse. */
499 /* Unquoted quotes end quoted args. */
506 /* Quoted argument without a closing quote. */
508 mandoc_msg(MANDOCERR_BADQUOTE, parse, ln, *pos, NULL);
510 /* Null-terminate this argument and move to the next one. */
518 *pos += (int)(cp - start) + (quoted ? 1 : 0);
521 if ('\0' == *cp && (white || ' ' == cp[-1]))
522 mandoc_msg(MANDOCERR_EOLNSPACE, parse, ln, *pos, NULL);
528 a2time(time_t *t, const char *fmt, const char *p)
533 memset(&tm, 0, sizeof(struct tm));
535 pp = strptime(p, fmt, &tm);
536 if (NULL != pp && '\0' == *pp) {
552 localtime_r(&t, &tm);
556 * up to 9 characters for the month (September) + blank
557 * up to 2 characters for the day + comma + blank
558 * 4 characters for the year and a terminating '\0'
560 p = buf = mandoc_malloc(10 + 4 + 4 + 1);
562 if (0 == (ssz = strftime(p, 10 + 1, "%B ", &tm)))
566 if (-1 == (isz = snprintf(p, 4 + 1, "%d, ", tm.tm_mday)))
570 if (0 == strftime(p, 4 + 1, "%Y", &tm))
580 mandoc_normdate(struct mparse *parse, char *in, int ln, int pos)
585 if (NULL == in || '\0' == *in ||
586 0 == strcmp(in, "$" "Mdocdate$")) {
587 mandoc_msg(MANDOCERR_NODATE, parse, ln, pos, NULL);
590 else if (!a2time(&t, "$" "Mdocdate: %b %d %Y $", in) &&
591 !a2time(&t, "%b %d, %Y", in) &&
592 !a2time(&t, "%Y-%m-%d", in)) {
593 mandoc_msg(MANDOCERR_BADDATE, parse, ln, pos, NULL);
596 out = t ? time2a(t) : NULL;
597 return(out ? out : mandoc_strdup(in));
601 mandoc_eos(const char *p, size_t sz, int enclosed)
610 * End-of-sentence recognition must include situations where
611 * some symbols, such as `)', allow prior EOS punctuation to
616 for (q = p + (int)sz - 1; q >= p; q--) {
636 return(found && (!enclosed || isalnum((unsigned char)*q)));
640 return(found && !enclosed);
644 mandoc_hyph(const char *start, const char *c)
648 * Choose whether to break at a hyphenated character. We only
649 * do this if it's free-standing within a word.
652 /* Skip first/last character of buffer. */
653 if (c == start || '\0' == *(c + 1))
655 /* Skip first/last character of word. */
656 if ('\t' == *(c + 1) || '\t' == *(c - 1))
658 if (' ' == *(c + 1) || ' ' == *(c - 1))
660 /* Skip double invocations. */
661 if ('-' == *(c + 1) || '-' == *(c - 1))
664 if ('\\' == *(c - 1))
671 * Find out whether a line is a macro line or not. If it is, adjust the
672 * current position and return one; if it isn't, return zero and don't
673 * change the current position.
676 mandoc_getcontrol(const char *cp, int *ppos)
682 if ('\\' == cp[pos] && '.' == cp[pos + 1])
684 else if ('.' == cp[pos] || '\'' == cp[pos])
689 while (' ' == cp[pos] || '\t' == cp[pos])
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_strntou(const char *p, size_t sz, int base)
714 v = strtol(buf, &ep, base);
716 if (buf[0] == '\0' || *ep != '\0')
719 if ((errno == ERANGE &&
720 (v == LONG_MAX || v == LONG_MIN)) ||
721 (v > INT_MAX || v < 0))