1 /* $Id: mandoc.c,v 1.44 2011/03/28 23:52:13 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>
32 #include "libmandoc.h"
36 static int a2time(time_t *, const char *, const char *);
37 static char *time2a(time_t);
40 mandoc_special(char *p)
100 if ('+' == *p || '-' == *p) {
124 if (ASCII_HYPH == *p)
126 if ('+' == *p || '-' == *p) {
132 /* Handle embedded numerical subexp or escape. */
135 while (*p && ')' != *p)
137 i = mandoc_special(--p);
147 } else if ('\\' == *p) {
148 if (0 == (i = mandoc_special(p)))
197 if (0 == (i = mandoc_special(p)))
200 return(*p ? (int)(p - sv) : 0);
218 for ( ; *p && term != *p; p++)
219 if (ASCII_HYPH == *p)
221 return(*p ? (int)(p - sv) : 0);
224 for (i = 0; *p && i < len; i++, p++)
225 if (ASCII_HYPH == *p)
227 return(i == len ? (int)(p - sv) : 0);
232 mandoc_calloc(size_t num, size_t size)
236 ptr = calloc(num, size);
239 exit((int)MANDOCLEVEL_SYSERR);
247 mandoc_malloc(size_t size)
254 exit((int)MANDOCLEVEL_SYSERR);
262 mandoc_realloc(void *ptr, size_t size)
265 ptr = realloc(ptr, size);
268 exit((int)MANDOCLEVEL_SYSERR);
276 mandoc_strdup(const char *ptr)
283 exit((int)MANDOCLEVEL_SYSERR);
290 * Parse a quoted or unquoted roff-style request or macro argument.
291 * Return a pointer to the parsed argument, which is either the original
292 * pointer or advanced by one byte in case the argument is quoted.
293 * Null-terminate the argument in place.
294 * Collapse pairs of quotes inside quoted arguments.
295 * Advance the argument pointer to the next argument,
296 * or to the null byte terminating the argument line.
299 mandoc_getarg(struct mparse *parse, char **cpp, int ln, int *pos)
302 int quoted, pairs, white;
304 /* Quoting can only start with a new word. */
314 for (cp = start; '\0' != *cp; cp++) {
315 /* Move left after quoted quotes and escaped backslashes. */
320 /* Poor man's copy mode. */
323 } else if (0 == quoted && ' ' == cp[1])
324 /* Skip escaped blanks. */
326 } else if (0 == quoted) {
328 /* Unescaped blanks end unquoted args. */
332 } else if ('"' == cp[0]) {
334 /* Quoted quotes collapse. */
338 /* Unquoted quotes end quoted args. */
345 /* Quoted argument without a closing quote. */
347 mandoc_msg(MANDOCERR_BADQUOTE, parse, ln, *pos, NULL);
349 /* Null-terminate this argument and move to the next one. */
357 *pos += (int)(cp - start) + (quoted ? 1 : 0);
360 if ('\0' == *cp && (white || ' ' == cp[-1]))
361 mandoc_msg(MANDOCERR_EOLNSPACE, parse, ln, *pos, NULL);
367 a2time(time_t *t, const char *fmt, const char *p)
372 memset(&tm, 0, sizeof(struct tm));
374 pp = strptime(p, fmt, &tm);
375 if (NULL != pp && '\0' == *pp) {
391 localtime_r(&t, &tm);
395 * up to 9 characters for the month (September) + blank
396 * up to 2 characters for the day + comma + blank
397 * 4 characters for the year and a terminating '\0'
399 p = buf = mandoc_malloc(10 + 4 + 4 + 1);
401 if (0 == (ssz = strftime(p, 10 + 1, "%B ", &tm)))
405 if (-1 == (isz = snprintf(p, 4 + 1, "%d, ", tm.tm_mday)))
409 if (0 == strftime(p, 4 + 1, "%Y", &tm))
419 mandoc_normdate(struct mparse *parse, char *in, int ln, int pos)
424 if (NULL == in || '\0' == *in ||
425 0 == strcmp(in, "$" "Mdocdate$")) {
426 mandoc_msg(MANDOCERR_NODATE, parse, ln, pos, NULL);
429 else if (!a2time(&t, "$" "Mdocdate: %b %d %Y $", in) &&
430 !a2time(&t, "%b %d, %Y", in) &&
431 !a2time(&t, "%Y-%m-%d", in)) {
432 mandoc_msg(MANDOCERR_BADDATE, parse, ln, pos, NULL);
435 out = t ? time2a(t) : NULL;
436 return(out ? out : mandoc_strdup(in));
440 mandoc_eos(const char *p, size_t sz, int enclosed)
449 * End-of-sentence recognition must include situations where
450 * some symbols, such as `)', allow prior EOS punctuation to
455 for (q = p + (int)sz - 1; q >= p; q--) {
475 return(found && (!enclosed || isalnum((unsigned char)*q)));
479 return(found && !enclosed);
483 mandoc_hyph(const char *start, const char *c)
487 * Choose whether to break at a hyphenated character. We only
488 * do this if it's free-standing within a word.
491 /* Skip first/last character of buffer. */
492 if (c == start || '\0' == *(c + 1))
494 /* Skip first/last character of word. */
495 if ('\t' == *(c + 1) || '\t' == *(c - 1))
497 if (' ' == *(c + 1) || ' ' == *(c - 1))
499 /* Skip double invocations. */
500 if ('-' == *(c + 1) || '-' == *(c - 1))
503 if ('\\' == *(c - 1))
510 * Find out whether a line is a macro line or not. If it is, adjust the
511 * current position and return one; if it isn't, return zero and don't
512 * change the current position.
515 mandoc_getcontrol(const char *cp, int *ppos)
521 if ('\\' == cp[pos] && '.' == cp[pos + 1])
523 else if ('.' == cp[pos] || '\'' == cp[pos])
528 while (' ' == cp[pos] || '\t' == cp[pos])