]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
1 /* $Id: mandoc.c,v 1.86 2014/08/18 09:11:47 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2012, 2013, 2014 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.
20 #include <sys/types.h>
32 #include "mandoc_aux.h"
33 #include "libmandoc.h"
37 static int a2time(time_t *, const char *, const char *);
38 static char *time2a(time_t);
42 mandoc_escape(const char **end
, const char **start
, int *sz
)
44 const char *local_start
;
50 * When the caller doesn't provide return storage,
60 * Beyond the backslash, at least one input character
61 * is part of the escape sequence. With one exception
62 * (see below), that character won't be returned.
70 switch ((*start
)[-1]) {
72 * First the glyphs. There are several different forms of
73 * these, but each eventually returns a substring of the glyph
83 * Unicode escapes are defined in groff as \[uXXXX] to
84 * \[u10FFFF], where the contained value must be a valid
85 * Unicode codepoint. Here, however, only check whether
86 * it's not a zero-width escape.
88 if ('u' == (*start
)[0] && ']' != (*start
)[1])
96 if ('u' == (*start
)[0] && '\'' != (*start
)[1])
104 * Escapes taking no arguments at all.
109 return(ESCAPE_IGNORE
);
112 * The \z escape is supposed to output the following
113 * character without advancing the cursor position.
114 * Since we are mostly dealing with terminal mode,
115 * let us just skip the next character.
118 return(ESCAPE_SKIPCHAR
);
121 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
122 * 'X' is the trigger. These have opaque sub-strings.
142 if (ESCAPE_ERROR
== gly
)
160 * These escapes are of the form \X'Y', where 'X' is the trigger
161 * and 'Y' is any string. These have opaque sub-strings.
162 * The \B and \w escapes are handled in roff.c, roff_res().
178 return(ESCAPE_ERROR
);
185 * These escapes are of the form \X'N', where 'X' is the trigger
186 * and 'N' resolves to a numerical expression.
201 if (strchr(" %&()*+-./0123456789:<=>", **start
)) {
204 return(ESCAPE_ERROR
);
212 * Special handling for the numbered character escape.
213 * XXX Do any other escapes need similar handling?
217 return(ESCAPE_ERROR
);
219 if (isdigit((unsigned char)**start
)) {
221 return(ESCAPE_IGNORE
);
224 while (isdigit((unsigned char)**end
))
229 return(ESCAPE_NUMBERED
);
232 * Sizes get a special category of their own.
237 /* See +/- counts as a sign. */
238 if ('+' == **end
|| '-' == **end
|| ASCII_HYPH
== **end
)
262 * Anything else is assumed to be a glyph.
263 * In this case, pass back the character after the backslash.
266 gly
= ESCAPE_SPECIAL
;
272 assert(ESCAPE_ERROR
!= gly
);
275 * Read up to the terminating character,
276 * paying attention to nested escapes.
280 while (**end
!= term
) {
283 return(ESCAPE_ERROR
);
287 mandoc_escape(end
, NULL
, NULL
))
288 return(ESCAPE_ERROR
);
295 *sz
= (*end
)++ - *start
;
298 if ((size_t)*sz
> strlen(*start
))
299 return(ESCAPE_ERROR
);
303 /* Run post-processors. */
308 if ('C' == **start
) {
310 * Treat constant-width font modes
311 * just like regular font modes.
316 if ('B' == (*start
)[0] && 'I' == (*start
)[1])
327 gly
= ESCAPE_FONTBOLD
;
332 gly
= ESCAPE_FONTITALIC
;
335 gly
= ESCAPE_FONTPREV
;
340 gly
= ESCAPE_FONTROMAN
;
345 if (1 == *sz
&& 'c' == **start
)
346 gly
= ESCAPE_NOSPACE
;
356 * Parse a quoted or unquoted roff-style request or macro argument.
357 * Return a pointer to the parsed argument, which is either the original
358 * pointer or advanced by one byte in case the argument is quoted.
359 * NUL-terminate the argument in place.
360 * Collapse pairs of quotes inside quoted arguments.
361 * Advance the argument pointer to the next argument,
362 * or to the NUL byte terminating the argument line.
365 mandoc_getarg(struct mparse
*parse
, char **cpp
, int ln
, int *pos
)
368 int quoted
, pairs
, white
;
370 /* Quoting can only start with a new word. */
380 for (cp
= start
; '\0' != *cp
; cp
++) {
383 * Move the following text left
384 * after quoted quotes and after "\\" and "\t".
391 * In copy mode, translate double to single
392 * backslashes and backslash-t to literal tabs.
403 /* Skip escaped blanks. */
410 } else if (0 == quoted
) {
412 /* Unescaped blanks end unquoted args. */
416 } else if ('"' == cp
[0]) {
418 /* Quoted quotes collapse. */
422 /* Unquoted quotes end quoted args. */
429 /* Quoted argument without a closing quote. */
431 mandoc_msg(MANDOCERR_ARG_QUOTE
, parse
, ln
, *pos
, NULL
);
433 /* NUL-terminate this argument and move to the next one. */
441 *pos
+= (int)(cp
- start
) + (quoted
? 1 : 0);
444 if ('\0' == *cp
&& (white
|| ' ' == cp
[-1]))
445 mandoc_msg(MANDOCERR_SPACE_EOL
, parse
, ln
, *pos
, NULL
);
451 a2time(time_t *t
, const char *fmt
, const char *p
)
456 memset(&tm
, 0, sizeof(struct tm
));
460 pp
= strptime(p
, fmt
, &tm
);
462 if (NULL
!= pp
&& '\0' == *pp
) {
482 * up to 9 characters for the month (September) + blank
483 * up to 2 characters for the day + comma + blank
484 * 4 characters for the year and a terminating '\0'
486 p
= buf
= mandoc_malloc(10 + 4 + 4 + 1);
488 if (0 == (ssz
= strftime(p
, 10 + 1, "%B ", tm
)))
492 if (-1 == (isz
= snprintf(p
, 4 + 1, "%d, ", tm
->tm_mday
)))
496 if (0 == strftime(p
, 4 + 1, "%Y", tm
))
506 mandoc_normdate(struct mparse
*parse
, char *in
, int ln
, int pos
)
511 if (NULL
== in
|| '\0' == *in
||
512 0 == strcmp(in
, "$" "Mdocdate$")) {
513 mandoc_msg(MANDOCERR_DATE_MISSING
, parse
, ln
, pos
, NULL
);
516 else if (a2time(&t
, "%Y-%m-%d", in
))
518 else if (!a2time(&t
, "$" "Mdocdate: %b %d %Y $", in
) &&
519 !a2time(&t
, "%b %d, %Y", in
)) {
520 mandoc_msg(MANDOCERR_DATE_BAD
, parse
, ln
, pos
, in
);
523 out
= t
? time2a(t
) : NULL
;
524 return(out
? out
: mandoc_strdup(in
));
528 mandoc_eos(const char *p
, size_t sz
)
537 * End-of-sentence recognition must include situations where
538 * some symbols, such as `)', allow prior EOS punctuation to
542 enclosed
= found
= 0;
543 for (q
= p
+ (int)sz
- 1; q
>= p
; q
--) {
563 return(found
&& (!enclosed
|| isalnum((unsigned char)*q
)));
567 return(found
&& !enclosed
);
571 * Convert a string to a long that may not be <0.
572 * If the string is invalid, or is less than 0, return -1.
575 mandoc_strntoi(const char *p
, size_t sz
, int base
)
588 v
= strtol(buf
, &ep
, base
);
590 if (buf
[0] == '\0' || *ep
!= '\0')