]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
1 /* $Id: mandoc.c,v 1.76 2014/03/23 11:25:26 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 "mandoc_aux.h"
35 #include "libmandoc.h"
39 static int a2time(time_t *, const char *, const char *);
40 static char *time2a(time_t);
44 mandoc_escape(const char **end
, const char **start
, int *sz
)
46 const char *local_start
;
52 * When the caller doesn't provide return storage,
62 * Beyond the backslash, at least one input character
63 * is part of the escape sequence. With one exception
64 * (see below), that character won't be returned.
72 switch ((*start
)[-1]) {
74 * First the glyphs. There are several different forms of
75 * these, but each eventually returns a substring of the glyph
85 * Unicode escapes are defined in groff as \[uXXXX] to
86 * \[u10FFFF], where the contained value must be a valid
87 * Unicode codepoint. Here, however, only check whether
88 * it's not a zero-width escape.
90 if ('u' == (*start
)[0] && ']' != (*start
)[1])
98 if ('u' == (*start
)[0] && '\'' != (*start
)[1])
101 gly
= ESCAPE_SPECIAL
;
106 * Escapes taking no arguments at all.
111 return(ESCAPE_IGNORE
);
114 * The \z escape is supposed to output the following
115 * character without advancing the cursor position.
116 * Since we are mostly dealing with terminal mode,
117 * let us just skip the next character.
120 return(ESCAPE_SKIPCHAR
);
123 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
124 * 'X' is the trigger. These have opaque sub-strings.
144 if (ESCAPE_ERROR
== gly
)
162 * These escapes are of the form \X'Y', where 'X' is the trigger
163 * and 'Y' is any string. These have opaque sub-strings.
183 return(ESCAPE_ERROR
);
190 * These escapes are of the form \X'N', where 'X' is the trigger
191 * and 'N' resolves to a numerical expression.
207 return(ESCAPE_ERROR
);
214 * Special handling for the numbered character escape.
215 * XXX Do any other escapes need similar handling?
219 return(ESCAPE_ERROR
);
221 if (isdigit((unsigned char)**start
)) {
223 return(ESCAPE_IGNORE
);
226 while (isdigit((unsigned char)**end
))
231 return(ESCAPE_NUMBERED
);
234 * Sizes get a special category of their own.
239 /* See +/- counts as a sign. */
240 if ('+' == **end
|| '-' == **end
|| ASCII_HYPH
== **end
)
264 * Anything else is assumed to be a glyph.
265 * In this case, pass back the character after the backslash.
268 gly
= ESCAPE_SPECIAL
;
274 assert(ESCAPE_ERROR
!= gly
);
277 * Read up to the terminating character,
278 * paying attention to nested escapes.
282 while (**end
!= term
) {
285 return(ESCAPE_ERROR
);
289 mandoc_escape(end
, NULL
, NULL
))
290 return(ESCAPE_ERROR
);
297 *sz
= (*end
)++ - *start
;
300 if ((size_t)*sz
> strlen(*start
))
301 return(ESCAPE_ERROR
);
305 /* Run post-processors. */
310 if ('C' == **start
) {
312 * Treat constant-width font modes
313 * just like regular font modes.
318 if ('B' == (*start
)[0] && 'I' == (*start
)[1])
329 gly
= ESCAPE_FONTBOLD
;
334 gly
= ESCAPE_FONTITALIC
;
337 gly
= ESCAPE_FONTPREV
;
342 gly
= ESCAPE_FONTROMAN
;
346 case (ESCAPE_SPECIAL
):
347 if (1 == *sz
&& 'c' == **start
)
348 gly
= ESCAPE_NOSPACE
;
358 * Parse a quoted or unquoted roff-style request or macro argument.
359 * Return a pointer to the parsed argument, which is either the original
360 * pointer or advanced by one byte in case the argument is quoted.
361 * NUL-terminate the argument in place.
362 * Collapse pairs of quotes inside quoted arguments.
363 * Advance the argument pointer to the next argument,
364 * or to the NUL byte terminating the argument line.
367 mandoc_getarg(struct mparse
*parse
, char **cpp
, int ln
, int *pos
)
370 int quoted
, pairs
, white
;
372 /* Quoting can only start with a new word. */
382 for (cp
= start
; '\0' != *cp
; cp
++) {
385 * Move the following text left
386 * after quoted quotes and after "\\" and "\t".
393 * In copy mode, translate double to single
394 * backslashes and backslash-t to literal tabs.
405 /* Skip escaped blanks. */
412 } else if (0 == quoted
) {
414 /* Unescaped blanks end unquoted args. */
418 } else if ('"' == cp
[0]) {
420 /* Quoted quotes collapse. */
424 /* Unquoted quotes end quoted args. */
431 /* Quoted argument without a closing quote. */
433 mandoc_msg(MANDOCERR_BADQUOTE
, parse
, ln
, *pos
, NULL
);
435 /* NUL-terminate this argument and move to the next one. */
443 *pos
+= (int)(cp
- start
) + (quoted
? 1 : 0);
446 if ('\0' == *cp
&& (white
|| ' ' == cp
[-1]))
447 mandoc_msg(MANDOCERR_EOLNSPACE
, parse
, ln
, *pos
, NULL
);
453 a2time(time_t *t
, const char *fmt
, const char *p
)
458 memset(&tm
, 0, sizeof(struct tm
));
462 pp
= strptime(p
, fmt
, &tm
);
464 if (NULL
!= pp
&& '\0' == *pp
) {
484 * up to 9 characters for the month (September) + blank
485 * up to 2 characters for the day + comma + blank
486 * 4 characters for the year and a terminating '\0'
488 p
= buf
= mandoc_malloc(10 + 4 + 4 + 1);
490 if (0 == (ssz
= strftime(p
, 10 + 1, "%B ", tm
)))
494 if (-1 == (isz
= snprintf(p
, 4 + 1, "%d, ", tm
->tm_mday
)))
498 if (0 == strftime(p
, 4 + 1, "%Y", tm
))
508 mandoc_normdate(struct mparse
*parse
, char *in
, int ln
, int pos
)
513 if (NULL
== in
|| '\0' == *in
||
514 0 == strcmp(in
, "$" "Mdocdate$")) {
515 mandoc_msg(MANDOCERR_NODATE
, parse
, ln
, pos
, NULL
);
518 else if (a2time(&t
, "%Y-%m-%d", in
))
520 else if (!a2time(&t
, "$" "Mdocdate: %b %d %Y $", in
) &&
521 !a2time(&t
, "%b %d, %Y", in
)) {
522 mandoc_msg(MANDOCERR_BADDATE
, parse
, ln
, pos
, NULL
);
525 out
= t
? time2a(t
) : NULL
;
526 return(out
? out
: mandoc_strdup(in
));
530 mandoc_eos(const char *p
, size_t sz
)
539 * End-of-sentence recognition must include situations where
540 * some symbols, such as `)', allow prior EOS punctuation to
544 enclosed
= found
= 0;
545 for (q
= p
+ (int)sz
- 1; q
>= p
; q
--) {
565 return(found
&& (!enclosed
|| isalnum((unsigned char)*q
)));
569 return(found
&& !enclosed
);
573 * Convert a string to a long that may not be <0.
574 * If the string is invalid, or is less than 0, return -1.
577 mandoc_strntoi(const char *p
, size_t sz
, int base
)
590 v
= strtol(buf
, &ep
, base
);
592 if (buf
[0] == '\0' || *ep
!= '\0')