]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
1 /* $Id: mandoc.c,v 1.68 2013/08/08 20:07:47 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 **end
, const char **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])
102 * The \z escape is supposed to output the following
103 * character without advancing the cursor position.
104 * Since we are mostly dealing with terminal mode,
105 * let us just skip the next character.
108 return(ESCAPE_SKIPCHAR
);
111 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
112 * 'X' is the trigger. These have opaque sub-strings.
132 if (ESCAPE_ERROR
== gly
)
150 * These escapes are of the form \X'Y', where 'X' is the trigger
151 * and 'Y' is any string. These have opaque sub-strings.
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.
186 gly
= ESCAPE_NUMBERED
;
196 return(ESCAPE_ERROR
);
197 if (ESCAPE_ERROR
== gly
)
204 * Special handling for the numbered character escape.
205 * XXX Do any other escapes need similar handling?
209 return(ESCAPE_ERROR
);
211 if (isdigit((unsigned char)**start
)) {
213 return(ESCAPE_IGNORE
);
216 while (isdigit((unsigned char)**end
))
221 return(ESCAPE_NUMBERED
);
224 * Sizes get a special category of their own.
229 /* See +/- counts as a sign. */
230 if ('+' == **end
|| '-' == **end
|| ASCII_HYPH
== **end
)
254 * Anything else is assumed to be a glyph.
255 * In this case, pass back the character after the backslash.
258 gly
= ESCAPE_SPECIAL
;
264 assert(ESCAPE_ERROR
!= gly
);
267 * Read up to the terminating character,
268 * paying attention to nested escapes.
272 while (**end
!= term
) {
275 return(ESCAPE_ERROR
);
279 mandoc_escape(end
, NULL
, NULL
))
280 return(ESCAPE_ERROR
);
287 *sz
= (*end
)++ - *start
;
290 if ((size_t)*sz
> strlen(*start
))
291 return(ESCAPE_ERROR
);
295 /* Run post-processors. */
300 if ('C' == **start
) {
302 * Treat constant-width font modes
303 * just like regular font modes.
308 if ('B' == (*start
)[0] && 'I' == (*start
)[1])
319 gly
= ESCAPE_FONTBOLD
;
324 gly
= ESCAPE_FONTITALIC
;
327 gly
= ESCAPE_FONTPREV
;
332 gly
= ESCAPE_FONTROMAN
;
336 case (ESCAPE_SPECIAL
):
337 if (1 == *sz
&& 'c' == **start
)
338 gly
= ESCAPE_NOSPACE
;
348 mandoc_calloc(size_t num
, size_t size
)
352 ptr
= calloc(num
, size
);
355 exit((int)MANDOCLEVEL_SYSERR
);
363 mandoc_malloc(size_t size
)
370 exit((int)MANDOCLEVEL_SYSERR
);
378 mandoc_realloc(void *ptr
, size_t size
)
381 ptr
= realloc(ptr
, size
);
384 exit((int)MANDOCLEVEL_SYSERR
);
391 mandoc_strndup(const char *ptr
, size_t sz
)
395 p
= mandoc_malloc(sz
+ 1);
402 mandoc_strdup(const char *ptr
)
409 exit((int)MANDOCLEVEL_SYSERR
);
416 * Parse a quoted or unquoted roff-style request or macro argument.
417 * Return a pointer to the parsed argument, which is either the original
418 * pointer or advanced by one byte in case the argument is quoted.
419 * Null-terminate the argument in place.
420 * Collapse pairs of quotes inside quoted arguments.
421 * Advance the argument pointer to the next argument,
422 * or to the null byte terminating the argument line.
425 mandoc_getarg(struct mparse
*parse
, char **cpp
, int ln
, int *pos
)
428 int quoted
, pairs
, white
;
430 /* Quoting can only start with a new word. */
440 for (cp
= start
; '\0' != *cp
; cp
++) {
443 * Move the following text left
444 * after quoted quotes and after "\\" and "\t".
451 * In copy mode, translate double to single
452 * backslashes and backslash-t to literal tabs.
463 /* Skip escaped blanks. */
470 } else if (0 == quoted
) {
472 /* Unescaped blanks end unquoted args. */
476 } else if ('"' == cp
[0]) {
478 /* Quoted quotes collapse. */
482 /* Unquoted quotes end quoted args. */
489 /* Quoted argument without a closing quote. */
491 mandoc_msg(MANDOCERR_BADQUOTE
, parse
, ln
, *pos
, NULL
);
493 /* Null-terminate this argument and move to the next one. */
501 *pos
+= (int)(cp
- start
) + (quoted
? 1 : 0);
504 if ('\0' == *cp
&& (white
|| ' ' == cp
[-1]))
505 mandoc_msg(MANDOCERR_EOLNSPACE
, parse
, ln
, *pos
, NULL
);
511 a2time(time_t *t
, const char *fmt
, const char *p
)
516 memset(&tm
, 0, sizeof(struct tm
));
520 pp
= strptime(p
, fmt
, &tm
);
522 if (NULL
!= pp
&& '\0' == *pp
) {
542 * up to 9 characters for the month (September) + blank
543 * up to 2 characters for the day + comma + blank
544 * 4 characters for the year and a terminating '\0'
546 p
= buf
= mandoc_malloc(10 + 4 + 4 + 1);
548 if (0 == (ssz
= strftime(p
, 10 + 1, "%B ", tm
)))
552 if (-1 == (isz
= snprintf(p
, 4 + 1, "%d, ", tm
->tm_mday
)))
556 if (0 == strftime(p
, 4 + 1, "%Y", tm
))
566 mandoc_normdate(struct mparse
*parse
, char *in
, int ln
, int pos
)
571 if (NULL
== in
|| '\0' == *in
||
572 0 == strcmp(in
, "$" "Mdocdate$")) {
573 mandoc_msg(MANDOCERR_NODATE
, parse
, ln
, pos
, NULL
);
576 else if (a2time(&t
, "%Y-%m-%d", in
))
578 else if (!a2time(&t
, "$" "Mdocdate: %b %d %Y $", in
) &&
579 !a2time(&t
, "%b %d, %Y", in
)) {
580 mandoc_msg(MANDOCERR_BADDATE
, parse
, ln
, pos
, NULL
);
583 out
= t
? time2a(t
) : NULL
;
584 return(out
? out
: mandoc_strdup(in
));
588 mandoc_eos(const char *p
, size_t sz
, int enclosed
)
597 * End-of-sentence recognition must include situations where
598 * some symbols, such as `)', allow prior EOS punctuation to
603 for (q
= p
+ (int)sz
- 1; q
>= p
; q
--) {
623 return(found
&& (!enclosed
|| isalnum((unsigned char)*q
)));
627 return(found
&& !enclosed
);
631 * Convert a string to a long that may not be <0.
632 * If the string is invalid, or is less than 0, return -1.
635 mandoc_strntoi(const char *p
, size_t sz
, int base
)
648 v
= strtol(buf
, &ep
, base
);
650 if (buf
[0] == '\0' || *ep
!= '\0')