1 /* $Id: mandoc.c,v 1.98 2015/11/12 22:44:27 schwarze Exp $ */
3 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2015 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"
35 static int a2time(time_t *, const char *, const char *);
36 static char *time2a(time_t);
40 mandoc_escape(const char **end, const char **start, int *sz)
42 const char *local_start;
48 * When the caller doesn't provide return storage,
58 * Beyond the backslash, at least one input character
59 * is part of the escape sequence. With one exception
60 * (see below), that character won't be returned.
68 switch ((*start)[-1]) {
70 * First the glyphs. There are several different forms of
71 * these, but each eventually returns a substring of the glyph
91 * Escapes taking no arguments at all.
100 * The \z escape is supposed to output the following
101 * character without advancing the cursor position.
102 * Since we are mostly dealing with terminal mode,
103 * let us just skip the next character.
106 return ESCAPE_SKIPCHAR;
109 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
110 * 'X' is the trigger. These have opaque sub-strings.
123 if (ESCAPE_ERROR == gly)
141 * These escapes are of the form \X'Y', where 'X' is the trigger
142 * and 'Y' is any string. These have opaque sub-strings.
143 * The \B and \w escapes are handled in roff.c, roff_res().
156 if (gly == ESCAPE_ERROR)
157 gly = ESCAPE_OVERSTRIKE;
163 * These escapes are of the form \X'N', where 'X' is the trigger
164 * and 'N' resolves to a numerical expression.
173 if (strchr(" %&()*+-./0123456789:<=>", **start)) {
184 * Special handling for the numbered character escape.
185 * XXX Do any other escapes need similar handling?
191 if (isdigit((unsigned char)**start)) {
193 return ESCAPE_IGNORE;
196 while (isdigit((unsigned char)**end))
201 return ESCAPE_NUMBERED;
204 * Sizes get a special category of their own.
209 /* See +/- counts as a sign. */
210 if ('+' == **end || '-' == **end || ASCII_HYPH == **end)
229 *sz = (*end)[-1] == 's' &&
230 isdigit((unsigned char)(*end)[1]) ? 2 : 1;
240 * Anything else is assumed to be a glyph.
241 * In this case, pass back the character after the backslash.
244 gly = ESCAPE_SPECIAL;
250 assert(ESCAPE_ERROR != gly);
253 * Read up to the terminating character,
254 * paying attention to nested escapes.
258 while (**end != term) {
265 mandoc_escape(end, NULL, NULL))
273 *sz = (*end)++ - *start;
276 if ((size_t)*sz > strlen(*start))
281 /* Run post-processors. */
286 if ('C' == **start) {
288 * Treat constant-width font modes
289 * just like regular font modes.
294 if ('B' == (*start)[0] && 'I' == (*start)[1])
304 gly = ESCAPE_FONTBOLD;
308 gly = ESCAPE_FONTITALIC;
311 gly = ESCAPE_FONTPREV;
315 gly = ESCAPE_FONTROMAN;
320 if (1 == *sz && 'c' == **start)
321 gly = ESCAPE_NOSPACE;
323 * Unicode escapes are defined in groff as \[u0000]
324 * to \[u10FFFF], where the contained value must be
325 * a valid Unicode codepoint. Here, however, only
326 * check the length and range.
328 if (**start != 'u' || *sz < 5 || *sz > 7)
330 if (*sz == 7 && ((*start)[1] != '1' || (*start)[2] != '0'))
332 if (*sz == 6 && (*start)[1] == '0')
334 if (*sz == 5 && (*start)[1] == 'D' &&
335 strchr("89ABCDEF", (*start)[2]) != NULL)
337 if ((int)strspn(*start + 1, "0123456789ABCDEFabcdef")
339 gly = ESCAPE_UNICODE;
349 * Parse a quoted or unquoted roff-style request or macro argument.
350 * Return a pointer to the parsed argument, which is either the original
351 * pointer or advanced by one byte in case the argument is quoted.
352 * NUL-terminate the argument in place.
353 * Collapse pairs of quotes inside quoted arguments.
354 * Advance the argument pointer to the next argument,
355 * or to the NUL byte terminating the argument line.
358 mandoc_getarg(struct mparse *parse, char **cpp, int ln, int *pos)
361 int quoted, pairs, white;
363 /* Quoting can only start with a new word. */
373 for (cp = start; '\0' != *cp; cp++) {
376 * Move the following text left
377 * after quoted quotes and after "\\" and "\t".
384 * In copy mode, translate double to single
385 * backslashes and backslash-t to literal tabs.
396 /* Skip escaped blanks. */
403 } else if (0 == quoted) {
405 /* Unescaped blanks end unquoted args. */
409 } else if ('"' == cp[0]) {
411 /* Quoted quotes collapse. */
415 /* Unquoted quotes end quoted args. */
422 /* Quoted argument without a closing quote. */
424 mandoc_msg(MANDOCERR_ARG_QUOTE, parse, ln, *pos, NULL);
426 /* NUL-terminate this argument and move to the next one. */
434 *pos += (int)(cp - start) + (quoted ? 1 : 0);
437 if ('\0' == *cp && (white || ' ' == cp[-1]))
438 mandoc_msg(MANDOCERR_SPACE_EOL, parse, ln, *pos, NULL);
444 a2time(time_t *t, const char *fmt, const char *p)
449 memset(&tm, 0, sizeof(struct tm));
453 pp = strptime(p, fmt, &tm);
455 if (NULL != pp && '\0' == *pp) {
477 * up to 9 characters for the month (September) + blank
478 * up to 2 characters for the day + comma + blank
479 * 4 characters for the year and a terminating '\0'
482 p = buf = mandoc_malloc(10 + 4 + 4 + 1);
484 if ((ssz = strftime(p, 10 + 1, "%B ", tm)) == 0)
489 * The output format is just "%d" here, not "%2d" or "%02d".
490 * That's also the reason why we can't just format the
491 * date as a whole with "%B %e, %Y" or "%B %d, %Y".
492 * Besides, the present approach is less prone to buffer
493 * overflows, in case anybody should ever introduce the bug
494 * of looking at LC_TIME.
497 if ((isz = snprintf(p, 4 + 1, "%d, ", tm->tm_mday)) == -1)
501 if (strftime(p, 4 + 1, "%Y", tm) == 0)
511 mandoc_normdate(struct mparse *parse, char *in, int ln, int pos)
515 /* No date specified: use today's date. */
517 if (in == NULL || *in == '\0' || strcmp(in, "$" "Mdocdate$") == 0) {
518 mandoc_msg(MANDOCERR_DATE_MISSING, parse, ln, pos, NULL);
519 return time2a(time(NULL));
522 /* Valid mdoc(7) date format. */
524 if (a2time(&t, "$" "Mdocdate: %b %d %Y $", in) ||
525 a2time(&t, "%b %d, %Y", in))
528 /* Do not warn about the legacy man(7) format. */
530 if ( ! a2time(&t, "%Y-%m-%d", in))
531 mandoc_msg(MANDOCERR_DATE_BAD, parse, ln, pos, in);
533 /* Use any non-mdoc(7) date verbatim. */
535 return mandoc_strdup(in);
539 mandoc_eos(const char *p, size_t sz)
548 * End-of-sentence recognition must include situations where
549 * some symbols, such as `)', allow prior EOS punctuation to
553 enclosed = found = 0;
554 for (q = p + (int)sz - 1; q >= p; q--) {
570 (!enclosed || isalnum((unsigned char)*q));
574 return found && !enclosed;
578 * Convert a string to a long that may not be <0.
579 * If the string is invalid, or is less than 0, return -1.
582 mandoc_strntoi(const char *p, size_t sz, int base)
595 v = strtol(buf, &ep, base);
597 if (buf[0] == '\0' || *ep != '\0')