]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
1 /* $Id: mandoc.c,v 1.70 2013/11/10 21:34:04 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2012, 2013 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);
43 mandoc_escape(const char const **end
, const char const **start
, int *sz
)
45 const char *local_start
;
51 * When the caller doesn't provide return storage,
61 * Beyond the backslash, at least one input character
62 * is part of the escape sequence. With one exception
63 * (see below), that character won't be returned.
71 switch ((*start
)[-1]) {
73 * First the glyphs. There are several different forms of
74 * these, but each eventually returns a substring of the glyph
84 * Unicode escapes are defined in groff as \[uXXXX] to
85 * \[u10FFFF], where the contained value must be a valid
86 * Unicode codepoint. Here, however, only check whether
87 * it's not a zero-width escape.
89 if ('u' == (*start
)[0] && ']' != (*start
)[1])
97 if ('u' == (*start
)[0] && '\'' != (*start
)[1])
100 gly
= ESCAPE_SPECIAL
;
105 * The \z escape is supposed to output the following
106 * character without advancing the cursor position.
107 * Since we are mostly dealing with terminal mode,
108 * let us just skip the next character.
111 return(ESCAPE_SKIPCHAR
);
114 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
115 * 'X' is the trigger. These have opaque sub-strings.
135 if (ESCAPE_ERROR
== gly
)
153 * These escapes are of the form \X'Y', where 'X' is the trigger
154 * and 'Y' is any string. These have opaque sub-strings.
170 return(ESCAPE_ERROR
);
177 * These escapes are of the form \X'N', where 'X' is the trigger
178 * and 'N' resolves to a numerical expression.
189 gly
= ESCAPE_NUMBERED
;
199 return(ESCAPE_ERROR
);
200 if (ESCAPE_ERROR
== gly
)
207 * Special handling for the numbered character escape.
208 * XXX Do any other escapes need similar handling?
212 return(ESCAPE_ERROR
);
214 if (isdigit((unsigned char)**start
)) {
216 return(ESCAPE_IGNORE
);
219 while (isdigit((unsigned char)**end
))
224 return(ESCAPE_NUMBERED
);
227 * Sizes get a special category of their own.
232 /* See +/- counts as a sign. */
233 if ('+' == **end
|| '-' == **end
|| ASCII_HYPH
== **end
)
257 * Anything else is assumed to be a glyph.
258 * In this case, pass back the character after the backslash.
261 gly
= ESCAPE_SPECIAL
;
267 assert(ESCAPE_ERROR
!= gly
);
270 * Read up to the terminating character,
271 * paying attention to nested escapes.
275 while (**end
!= term
) {
278 return(ESCAPE_ERROR
);
282 mandoc_escape(end
, NULL
, NULL
))
283 return(ESCAPE_ERROR
);
290 *sz
= (*end
)++ - *start
;
293 if ((size_t)*sz
> strlen(*start
))
294 return(ESCAPE_ERROR
);
298 /* Run post-processors. */
303 if ('C' == **start
) {
305 * Treat constant-width font modes
306 * just like regular font modes.
311 if ('B' == (*start
)[0] && 'I' == (*start
)[1])
322 gly
= ESCAPE_FONTBOLD
;
327 gly
= ESCAPE_FONTITALIC
;
330 gly
= ESCAPE_FONTPREV
;
335 gly
= ESCAPE_FONTROMAN
;
339 case (ESCAPE_SPECIAL
):
340 if (1 == *sz
&& 'c' == **start
)
341 gly
= ESCAPE_NOSPACE
;
351 mandoc_calloc(size_t num
, size_t size
)
355 ptr
= calloc(num
, size
);
358 exit((int)MANDOCLEVEL_SYSERR
);
366 mandoc_malloc(size_t size
)
373 exit((int)MANDOCLEVEL_SYSERR
);
381 mandoc_realloc(void *ptr
, size_t size
)
384 ptr
= realloc(ptr
, size
);
387 exit((int)MANDOCLEVEL_SYSERR
);
394 mandoc_strndup(const char *ptr
, size_t sz
)
398 p
= mandoc_malloc(sz
+ 1);
405 mandoc_strdup(const char *ptr
)
412 exit((int)MANDOCLEVEL_SYSERR
);
419 * Parse a quoted or unquoted roff-style request or macro argument.
420 * Return a pointer to the parsed argument, which is either the original
421 * pointer or advanced by one byte in case the argument is quoted.
422 * Null-terminate the argument in place.
423 * Collapse pairs of quotes inside quoted arguments.
424 * Advance the argument pointer to the next argument,
425 * or to the null byte terminating the argument line.
428 mandoc_getarg(struct mparse
*parse
, char **cpp
, int ln
, int *pos
)
431 int quoted
, pairs
, white
;
433 /* Quoting can only start with a new word. */
443 for (cp
= start
; '\0' != *cp
; cp
++) {
446 * Move the following text left
447 * after quoted quotes and after "\\" and "\t".
454 * In copy mode, translate double to single
455 * backslashes and backslash-t to literal tabs.
466 /* Skip escaped blanks. */
473 } else if (0 == quoted
) {
475 /* Unescaped blanks end unquoted args. */
479 } else if ('"' == cp
[0]) {
481 /* Quoted quotes collapse. */
485 /* Unquoted quotes end quoted args. */
492 /* Quoted argument without a closing quote. */
494 mandoc_msg(MANDOCERR_BADQUOTE
, parse
, ln
, *pos
, NULL
);
496 /* Null-terminate this argument and move to the next one. */
504 *pos
+= (int)(cp
- start
) + (quoted
? 1 : 0);
507 if ('\0' == *cp
&& (white
|| ' ' == cp
[-1]))
508 mandoc_msg(MANDOCERR_EOLNSPACE
, parse
, ln
, *pos
, NULL
);
514 a2time(time_t *t
, const char *fmt
, const char *p
)
519 memset(&tm
, 0, sizeof(struct tm
));
523 pp
= strptime(p
, fmt
, &tm
);
525 if (NULL
!= pp
&& '\0' == *pp
) {
545 * up to 9 characters for the month (September) + blank
546 * up to 2 characters for the day + comma + blank
547 * 4 characters for the year and a terminating '\0'
549 p
= buf
= mandoc_malloc(10 + 4 + 4 + 1);
551 if (0 == (ssz
= strftime(p
, 10 + 1, "%B ", tm
)))
555 if (-1 == (isz
= snprintf(p
, 4 + 1, "%d, ", tm
->tm_mday
)))
559 if (0 == strftime(p
, 4 + 1, "%Y", tm
))
569 mandoc_normdate(struct mparse
*parse
, char *in
, int ln
, int pos
)
574 if (NULL
== in
|| '\0' == *in
||
575 0 == strcmp(in
, "$" "Mdocdate$")) {
576 mandoc_msg(MANDOCERR_NODATE
, parse
, ln
, pos
, NULL
);
579 else if (a2time(&t
, "%Y-%m-%d", in
))
581 else if (!a2time(&t
, "$" "Mdocdate: %b %d %Y $", in
) &&
582 !a2time(&t
, "%b %d, %Y", in
)) {
583 mandoc_msg(MANDOCERR_BADDATE
, parse
, ln
, pos
, NULL
);
586 out
= t
? time2a(t
) : NULL
;
587 return(out
? out
: mandoc_strdup(in
));
591 mandoc_eos(const char *p
, size_t sz
, int enclosed
)
600 * End-of-sentence recognition must include situations where
601 * some symbols, such as `)', allow prior EOS punctuation to
606 for (q
= p
+ (int)sz
- 1; q
>= p
; q
--) {
626 return(found
&& (!enclosed
|| isalnum((unsigned char)*q
)));
630 return(found
&& !enclosed
);
634 * Convert a string to a long that may not be <0.
635 * If the string is invalid, or is less than 0, return -1.
638 mandoc_strntoi(const char *p
, size_t sz
, int base
)
651 v
= strtol(buf
, &ep
, base
);
653 if (buf
[0] == '\0' || *ep
!= '\0')