1 /* $Id: mandoc.c,v 1.62 2011/12/03 16:08:51 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 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.
167 if (ESCAPE_ERROR == gly)
189 * These escapes are of the form \X'Y', where 'X' is the trigger
190 * and 'Y' is any string. These have opaque sub-strings.
206 return(ESCAPE_ERROR);
212 * These escapes are of the form \X'N', where 'X' is the trigger
213 * and 'N' resolves to a numerical expression.
224 gly = ESCAPE_NUMBERED;
233 if (ESCAPE_ERROR == gly)
236 return(ESCAPE_ERROR);
237 term = numeric = '\'';
241 * Special handling for the numbered character escape.
242 * XXX Do any other escapes need similar handling?
246 return(ESCAPE_ERROR);
248 if (isdigit((unsigned char)cp[i-1]))
249 return(ESCAPE_IGNORE);
250 while (isdigit((unsigned char)**end))
258 return(ESCAPE_NUMBERED);
261 * Sizes get a special category of their own.
270 /* See +/- counts as a sign. */
272 if ('+' == c || '-' == c || ASCII_HYPH == c)
280 term = numeric = ']';
283 term = numeric = '\'';
291 /* See +/- counts as a sign. */
293 if ('+' == c || '-' == c || ASCII_HYPH == c)
299 * Anything else is assumed to be a glyph.
302 gly = ESCAPE_SPECIAL;
308 assert(ESCAPE_ERROR != gly);
315 * If a terminating block has been specified, we need to
316 * handle the case of recursion, which could have their
317 * own terminating blocks that mess up our parse. This, by the
318 * way, means that the "start" and "size" values will be
319 * effectively meaningless.
323 if (numeric && -1 == (ssz = numescape(&cp[i])))
324 return(ESCAPE_ERROR);
330 * We have a character terminator. Try to read up to that
331 * character. If we can't (i.e., we hit the nil), then return
332 * an error; if we can, calculate our length, read past the
333 * terminating character, and exit.
337 *end = strchr(&cp[i], term);
339 return(ESCAPE_ERROR);
341 rlim = *end - &cp[i];
351 * We have a numeric limit. If the string is shorter than that,
352 * stop and return an error. Else adjust our endpoint, length,
353 * and return the current glyph.
356 if ((size_t)lim > strlen(&cp[i]))
357 return(ESCAPE_ERROR);
366 assert(rlim >= 0 && rstart);
368 /* Run post-processors. */
373 * Pretend that the constant-width font modes are the
374 * same as the regular font modes.
376 if (2 == rlim && 'C' == *rstart)
385 gly = ESCAPE_FONTBOLD;
390 gly = ESCAPE_FONTITALIC;
393 gly = ESCAPE_FONTPREV;
398 gly = ESCAPE_FONTROMAN;
402 case (ESCAPE_SPECIAL):
406 gly = ESCAPE_NOSPACE;
416 mandoc_calloc(size_t num, size_t size)
420 ptr = calloc(num, size);
423 exit((int)MANDOCLEVEL_SYSERR);
431 mandoc_malloc(size_t size)
438 exit((int)MANDOCLEVEL_SYSERR);
446 mandoc_realloc(void *ptr, size_t size)
449 ptr = realloc(ptr, size);
452 exit((int)MANDOCLEVEL_SYSERR);
459 mandoc_strndup(const char *ptr, size_t sz)
463 p = mandoc_malloc(sz + 1);
470 mandoc_strdup(const char *ptr)
477 exit((int)MANDOCLEVEL_SYSERR);
484 * Parse a quoted or unquoted roff-style request or macro argument.
485 * Return a pointer to the parsed argument, which is either the original
486 * pointer or advanced by one byte in case the argument is quoted.
487 * Null-terminate the argument in place.
488 * Collapse pairs of quotes inside quoted arguments.
489 * Advance the argument pointer to the next argument,
490 * or to the null byte terminating the argument line.
493 mandoc_getarg(struct mparse *parse, char **cpp, int ln, int *pos)
496 int quoted, pairs, white;
498 /* Quoting can only start with a new word. */
508 for (cp = start; '\0' != *cp; cp++) {
509 /* Move left after quoted quotes and escaped backslashes. */
514 /* Poor man's copy mode. */
517 } else if (0 == quoted && ' ' == cp[1])
518 /* Skip escaped blanks. */
520 } else if (0 == quoted) {
522 /* Unescaped blanks end unquoted args. */
526 } else if ('"' == cp[0]) {
528 /* Quoted quotes collapse. */
532 /* Unquoted quotes end quoted args. */
539 /* Quoted argument without a closing quote. */
541 mandoc_msg(MANDOCERR_BADQUOTE, parse, ln, *pos, NULL);
543 /* Null-terminate this argument and move to the next one. */
551 *pos += (int)(cp - start) + (quoted ? 1 : 0);
554 if ('\0' == *cp && (white || ' ' == cp[-1]))
555 mandoc_msg(MANDOCERR_EOLNSPACE, parse, ln, *pos, NULL);
561 a2time(time_t *t, const char *fmt, const char *p)
566 memset(&tm, 0, sizeof(struct tm));
570 pp = strptime(p, fmt, &tm);
572 if (NULL != pp && '\0' == *pp) {
592 * up to 9 characters for the month (September) + blank
593 * up to 2 characters for the day + comma + blank
594 * 4 characters for the year and a terminating '\0'
596 p = buf = mandoc_malloc(10 + 4 + 4 + 1);
598 if (0 == (ssz = strftime(p, 10 + 1, "%B ", tm)))
602 if (-1 == (isz = snprintf(p, 4 + 1, "%d, ", tm->tm_mday)))
606 if (0 == strftime(p, 4 + 1, "%Y", tm))
616 mandoc_normdate(struct mparse *parse, char *in, int ln, int pos)
621 if (NULL == in || '\0' == *in ||
622 0 == strcmp(in, "$" "Mdocdate$")) {
623 mandoc_msg(MANDOCERR_NODATE, parse, ln, pos, NULL);
626 else if (a2time(&t, "%Y-%m-%d", in))
628 else if (!a2time(&t, "$" "Mdocdate: %b %d %Y $", in) &&
629 !a2time(&t, "%b %d, %Y", in)) {
630 mandoc_msg(MANDOCERR_BADDATE, parse, ln, pos, NULL);
633 out = t ? time2a(t) : NULL;
634 return(out ? out : mandoc_strdup(in));
638 mandoc_eos(const char *p, size_t sz, int enclosed)
647 * End-of-sentence recognition must include situations where
648 * some symbols, such as `)', allow prior EOS punctuation to
653 for (q = p + (int)sz - 1; q >= p; q--) {
673 return(found && (!enclosed || isalnum((unsigned char)*q)));
677 return(found && !enclosed);
681 * Find out whether a line is a macro line or not. If it is, adjust the
682 * current position and return one; if it isn't, return zero and don't
683 * change the current position.
686 mandoc_getcontrol(const char *cp, int *ppos)
692 if ('\\' == cp[pos] && '.' == cp[pos + 1])
694 else if ('.' == cp[pos] || '\'' == cp[pos])
699 while (' ' == cp[pos] || '\t' == cp[pos])
707 * Convert a string to a long that may not be <0.
708 * If the string is invalid, or is less than 0, return -1.
711 mandoc_strntoi(const char *p, size_t sz, int base)
724 v = strtol(buf, &ep, base);
726 if (buf[0] == '\0' || *ep != '\0')