]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
1 /* $Id: mandoc.c,v 1.64 2012/05/31 22:34:06 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2012 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
)
47 const char *cp
, *rstart
;
58 switch ((c
= cp
[i
++])) {
60 * First the glyphs. There are several different forms of
61 * these, but each eventually returns a substring of the glyph
71 * Unicode escapes are defined in groff as \[uXXXX] to
72 * \[u10FFFF], where the contained value must be a valid
73 * Unicode codepoint. Here, however, only check whether
74 * it's not a zero-width escape.
76 if ('u' == cp
[i
] && ']' != cp
[i
+ 1])
88 * The \z escape is supposed to output the following
89 * character without advancing the cursor position.
90 * Since we are mostly dealing with terminal mode,
91 * let us just skip the next character.
95 return(ESCAPE_SKIPCHAR
);
98 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
99 * 'X' is the trigger. These have opaque sub-strings.
119 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.
158 return(ESCAPE_ERROR
);
164 * These escapes are of the form \X'N', where 'X' is the trigger
165 * and 'N' resolves to a numerical expression.
176 gly
= ESCAPE_NUMBERED
;
185 if (ESCAPE_ERROR
== gly
)
188 return(ESCAPE_ERROR
);
193 * Special handling for the numbered character escape.
194 * XXX Do any other escapes need similar handling?
198 return(ESCAPE_ERROR
);
200 if (isdigit((unsigned char)cp
[i
-1]))
201 return(ESCAPE_IGNORE
);
202 while (isdigit((unsigned char)**end
))
210 return(ESCAPE_NUMBERED
);
213 * Sizes get a special category of their own.
222 /* See +/- counts as a sign. */
224 if ('+' == c
|| '-' == c
|| ASCII_HYPH
== c
)
243 /* See +/- counts as a sign. */
245 if ('+' == c
|| '-' == c
|| ASCII_HYPH
== c
)
251 * Anything else is assumed to be a glyph.
254 gly
= ESCAPE_SPECIAL
;
260 assert(ESCAPE_ERROR
!= gly
);
262 *end
= rstart
= &cp
[i
];
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 rlim
= (*end
)++ - rstart
;
290 if ((size_t)rlim
> strlen(rstart
))
291 return(ESCAPE_ERROR
);
297 /* Run post-processors. */
302 * Pretend that the constant-width font modes are the
303 * same as the regular font modes.
305 if (2 == rlim
&& 'C' == *rstart
)
314 gly
= ESCAPE_FONTBOLD
;
319 gly
= ESCAPE_FONTITALIC
;
322 gly
= ESCAPE_FONTPREV
;
327 gly
= ESCAPE_FONTROMAN
;
331 case (ESCAPE_SPECIAL
):
335 gly
= ESCAPE_NOSPACE
;
345 mandoc_calloc(size_t num
, size_t size
)
349 ptr
= calloc(num
, size
);
352 exit((int)MANDOCLEVEL_SYSERR
);
360 mandoc_malloc(size_t size
)
367 exit((int)MANDOCLEVEL_SYSERR
);
375 mandoc_realloc(void *ptr
, size_t size
)
378 ptr
= realloc(ptr
, size
);
381 exit((int)MANDOCLEVEL_SYSERR
);
388 mandoc_strndup(const char *ptr
, size_t sz
)
392 p
= mandoc_malloc(sz
+ 1);
399 mandoc_strdup(const char *ptr
)
406 exit((int)MANDOCLEVEL_SYSERR
);
413 * Parse a quoted or unquoted roff-style request or macro argument.
414 * Return a pointer to the parsed argument, which is either the original
415 * pointer or advanced by one byte in case the argument is quoted.
416 * Null-terminate the argument in place.
417 * Collapse pairs of quotes inside quoted arguments.
418 * Advance the argument pointer to the next argument,
419 * or to the null byte terminating the argument line.
422 mandoc_getarg(struct mparse
*parse
, char **cpp
, int ln
, int *pos
)
425 int quoted
, pairs
, white
;
427 /* Quoting can only start with a new word. */
437 for (cp
= start
; '\0' != *cp
; cp
++) {
438 /* Move left after quoted quotes and escaped backslashes. */
443 /* Poor man's copy mode. */
446 } else if (0 == quoted
&& ' ' == cp
[1])
447 /* Skip escaped blanks. */
449 } else if (0 == quoted
) {
451 /* Unescaped blanks end unquoted args. */
455 } else if ('"' == cp
[0]) {
457 /* Quoted quotes collapse. */
461 /* Unquoted quotes end quoted args. */
468 /* Quoted argument without a closing quote. */
470 mandoc_msg(MANDOCERR_BADQUOTE
, parse
, ln
, *pos
, NULL
);
472 /* Null-terminate this argument and move to the next one. */
480 *pos
+= (int)(cp
- start
) + (quoted
? 1 : 0);
483 if ('\0' == *cp
&& (white
|| ' ' == cp
[-1]))
484 mandoc_msg(MANDOCERR_EOLNSPACE
, parse
, ln
, *pos
, NULL
);
490 a2time(time_t *t
, const char *fmt
, const char *p
)
495 memset(&tm
, 0, sizeof(struct tm
));
499 pp
= strptime(p
, fmt
, &tm
);
501 if (NULL
!= pp
&& '\0' == *pp
) {
521 * up to 9 characters for the month (September) + blank
522 * up to 2 characters for the day + comma + blank
523 * 4 characters for the year and a terminating '\0'
525 p
= buf
= mandoc_malloc(10 + 4 + 4 + 1);
527 if (0 == (ssz
= strftime(p
, 10 + 1, "%B ", tm
)))
531 if (-1 == (isz
= snprintf(p
, 4 + 1, "%d, ", tm
->tm_mday
)))
535 if (0 == strftime(p
, 4 + 1, "%Y", tm
))
545 mandoc_normdate(struct mparse
*parse
, char *in
, int ln
, int pos
)
550 if (NULL
== in
|| '\0' == *in
||
551 0 == strcmp(in
, "$" "Mdocdate$")) {
552 mandoc_msg(MANDOCERR_NODATE
, parse
, ln
, pos
, NULL
);
555 else if (a2time(&t
, "%Y-%m-%d", in
))
557 else if (!a2time(&t
, "$" "Mdocdate: %b %d %Y $", in
) &&
558 !a2time(&t
, "%b %d, %Y", in
)) {
559 mandoc_msg(MANDOCERR_BADDATE
, parse
, ln
, pos
, NULL
);
562 out
= t
? time2a(t
) : NULL
;
563 return(out
? out
: mandoc_strdup(in
));
567 mandoc_eos(const char *p
, size_t sz
, int enclosed
)
576 * End-of-sentence recognition must include situations where
577 * some symbols, such as `)', allow prior EOS punctuation to
582 for (q
= p
+ (int)sz
- 1; q
>= p
; q
--) {
602 return(found
&& (!enclosed
|| isalnum((unsigned char)*q
)));
606 return(found
&& !enclosed
);
610 * Find out whether a line is a macro line or not. If it is, adjust the
611 * current position and return one; if it isn't, return zero and don't
612 * change the current position.
615 mandoc_getcontrol(const char *cp
, int *ppos
)
621 if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
623 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
628 while (' ' == cp
[pos
] || '\t' == cp
[pos
])
636 * Convert a string to a long that may not be <0.
637 * If the string is invalid, or is less than 0, return -1.
640 mandoc_strntoi(const char *p
, size_t sz
, int base
)
653 v
= strtol(buf
, &ep
, base
);
655 if (buf
[0] == '\0' || *ep
!= '\0')