1 /* $Id: mandoc.c,v 1.88 2014/10/28 13:24:44 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2012, 2013, 2014 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>
32 #include "mandoc_aux.h"
33 #include "libmandoc.h"
37 static int a2time(time_t *, const char *, const char *);
38 static char *time2a(time_t);
42 mandoc_escape(const char **end, const char **start, int *sz)
44 const char *local_start;
50 * When the caller doesn't provide return storage,
60 * Beyond the backslash, at least one input character
61 * is part of the escape sequence. With one exception
62 * (see below), that character won't be returned.
70 switch ((*start)[-1]) {
72 * First the glyphs. There are several different forms of
73 * these, but each eventually returns a substring of the glyph
93 * Escapes taking no arguments at all.
98 return(ESCAPE_IGNORE);
101 * The \z escape is supposed to output the following
102 * character without advancing the cursor position.
103 * Since we are mostly dealing with terminal mode,
104 * let us just skip the next character.
107 return(ESCAPE_SKIPCHAR);
110 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
111 * 'X' is the trigger. These have opaque sub-strings.
131 if (ESCAPE_ERROR == gly)
149 * These escapes are of the form \X'Y', where 'X' is the trigger
150 * and 'Y' is any string. These have opaque sub-strings.
151 * The \B and \w escapes are handled in roff.c, roff_res().
167 return(ESCAPE_ERROR);
174 * These escapes are of the form \X'N', where 'X' is the trigger
175 * and 'N' resolves to a numerical expression.
190 if (strchr(" %&()*+-./0123456789:<=>", **start)) {
193 return(ESCAPE_ERROR);
201 * Special handling for the numbered character escape.
202 * XXX Do any other escapes need similar handling?
206 return(ESCAPE_ERROR);
208 if (isdigit((unsigned char)**start)) {
210 return(ESCAPE_IGNORE);
213 while (isdigit((unsigned char)**end))
218 return(ESCAPE_NUMBERED);
221 * Sizes get a special category of their own.
226 /* See +/- counts as a sign. */
227 if ('+' == **end || '-' == **end || ASCII_HYPH == **end)
251 * Anything else is assumed to be a glyph.
252 * In this case, pass back the character after the backslash.
255 gly = ESCAPE_SPECIAL;
261 assert(ESCAPE_ERROR != gly);
264 * Read up to the terminating character,
265 * paying attention to nested escapes.
269 while (**end != term) {
272 return(ESCAPE_ERROR);
276 mandoc_escape(end, NULL, NULL))
277 return(ESCAPE_ERROR);
284 *sz = (*end)++ - *start;
287 if ((size_t)*sz > strlen(*start))
288 return(ESCAPE_ERROR);
292 /* Run post-processors. */
297 if ('C' == **start) {
299 * Treat constant-width font modes
300 * just like regular font modes.
305 if ('B' == (*start)[0] && 'I' == (*start)[1])
316 gly = ESCAPE_FONTBOLD;
321 gly = ESCAPE_FONTITALIC;
324 gly = ESCAPE_FONTPREV;
329 gly = ESCAPE_FONTROMAN;
334 if (1 == *sz && 'c' == **start)
335 gly = ESCAPE_NOSPACE;
337 * Unicode escapes are defined in groff as \[u0000]
338 * to \[u10FFFF], where the contained value must be
339 * a valid Unicode codepoint. Here, however, only
340 * check the length and range.
342 if (**start != 'u' || *sz < 5 || *sz > 7)
344 if (*sz == 7 && ((*start)[1] != '1' || (*start)[2] != '0'))
346 if (*sz == 6 && (*start)[1] == '0')
348 if ((int)strspn(*start + 1, "0123456789ABCDEFabcdef")
350 gly = ESCAPE_UNICODE;
360 * Parse a quoted or unquoted roff-style request or macro argument.
361 * Return a pointer to the parsed argument, which is either the original
362 * pointer or advanced by one byte in case the argument is quoted.
363 * NUL-terminate the argument in place.
364 * Collapse pairs of quotes inside quoted arguments.
365 * Advance the argument pointer to the next argument,
366 * or to the NUL byte terminating the argument line.
369 mandoc_getarg(struct mparse *parse, char **cpp, int ln, int *pos)
372 int quoted, pairs, white;
374 /* Quoting can only start with a new word. */
384 for (cp = start; '\0' != *cp; cp++) {
387 * Move the following text left
388 * after quoted quotes and after "\\" and "\t".
395 * In copy mode, translate double to single
396 * backslashes and backslash-t to literal tabs.
407 /* Skip escaped blanks. */
414 } else if (0 == quoted) {
416 /* Unescaped blanks end unquoted args. */
420 } else if ('"' == cp[0]) {
422 /* Quoted quotes collapse. */
426 /* Unquoted quotes end quoted args. */
433 /* Quoted argument without a closing quote. */
435 mandoc_msg(MANDOCERR_ARG_QUOTE, parse, ln, *pos, NULL);
437 /* NUL-terminate this argument and move to the next one. */
445 *pos += (int)(cp - start) + (quoted ? 1 : 0);
448 if ('\0' == *cp && (white || ' ' == cp[-1]))
449 mandoc_msg(MANDOCERR_SPACE_EOL, parse, ln, *pos, NULL);
455 a2time(time_t *t, const char *fmt, const char *p)
460 memset(&tm, 0, sizeof(struct tm));
464 pp = strptime(p, fmt, &tm);
466 if (NULL != pp && '\0' == *pp) {
486 * up to 9 characters for the month (September) + blank
487 * up to 2 characters for the day + comma + blank
488 * 4 characters for the year and a terminating '\0'
490 p = buf = mandoc_malloc(10 + 4 + 4 + 1);
492 if (0 == (ssz = strftime(p, 10 + 1, "%B ", tm)))
496 if (-1 == (isz = snprintf(p, 4 + 1, "%d, ", tm->tm_mday)))
500 if (0 == strftime(p, 4 + 1, "%Y", tm))
510 mandoc_normdate(struct mparse *parse, char *in, int ln, int pos)
515 if (NULL == in || '\0' == *in ||
516 0 == strcmp(in, "$" "Mdocdate$")) {
517 mandoc_msg(MANDOCERR_DATE_MISSING, parse, ln, pos, NULL);
520 else if (a2time(&t, "%Y-%m-%d", in))
522 else if (!a2time(&t, "$" "Mdocdate: %b %d %Y $", in) &&
523 !a2time(&t, "%b %d, %Y", in)) {
524 mandoc_msg(MANDOCERR_DATE_BAD, parse, ln, pos, in);
527 out = t ? time2a(t) : NULL;
528 return(out ? out : mandoc_strdup(in));
532 mandoc_eos(const char *p, size_t sz)
541 * End-of-sentence recognition must include situations where
542 * some symbols, such as `)', allow prior EOS punctuation to
546 enclosed = found = 0;
547 for (q = p + (int)sz - 1; q >= p; q--) {
567 return(found && (!enclosed || isalnum((unsigned char)*q)));
571 return(found && !enclosed);
575 * Convert a string to a long that may not be <0.
576 * If the string is invalid, or is less than 0, return -1.
579 mandoc_strntoi(const char *p, size_t sz, int base)
592 v = strtol(buf, &ep, base);
594 if (buf[0] == '\0' || *ep != '\0')