]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
1 /* $Id: mandoc.c,v 1.95 2015/10/12 00:08:15 schwarze Exp $ */
3 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2015 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
93 * Escapes taking no arguments at all.
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.
125 if (ESCAPE_ERROR
== gly
)
143 * These escapes are of the form \X'Y', where 'X' is the trigger
144 * and 'Y' is any string. These have opaque sub-strings.
145 * The \B and \w escapes are handled in roff.c, roff_res().
158 if (gly
== ESCAPE_ERROR
)
159 gly
= ESCAPE_OVERSTRIKE
;
165 * These escapes are of the form \X'N', where 'X' is the trigger
166 * and 'N' resolves to a numerical expression.
175 if (strchr(" %&()*+-./0123456789:<=>", **start
)) {
186 * Special handling for the numbered character escape.
187 * XXX Do any other escapes need similar handling?
193 if (isdigit((unsigned char)**start
)) {
195 return ESCAPE_IGNORE
;
198 while (isdigit((unsigned char)**end
))
203 return ESCAPE_NUMBERED
;
206 * Sizes get a special category of their own.
211 /* See +/- counts as a sign. */
212 if ('+' == **end
|| '-' == **end
|| ASCII_HYPH
== **end
)
231 *sz
= (*end
)[-1] == 's' &&
232 isdigit((unsigned char)(*end
)[1]) ? 2 : 1;
242 * Anything else is assumed to be a glyph.
243 * In this case, pass back the character after the backslash.
246 gly
= ESCAPE_SPECIAL
;
252 assert(ESCAPE_ERROR
!= gly
);
255 * Read up to the terminating character,
256 * paying attention to nested escapes.
260 while (**end
!= term
) {
267 mandoc_escape(end
, NULL
, NULL
))
275 *sz
= (*end
)++ - *start
;
278 if ((size_t)*sz
> strlen(*start
))
283 /* Run post-processors. */
288 if ('C' == **start
) {
290 * Treat constant-width font modes
291 * just like regular font modes.
296 if ('B' == (*start
)[0] && 'I' == (*start
)[1])
306 gly
= ESCAPE_FONTBOLD
;
310 gly
= ESCAPE_FONTITALIC
;
313 gly
= ESCAPE_FONTPREV
;
317 gly
= ESCAPE_FONTROMAN
;
322 if (1 == *sz
&& 'c' == **start
)
323 gly
= ESCAPE_NOSPACE
;
325 * Unicode escapes are defined in groff as \[u0000]
326 * to \[u10FFFF], where the contained value must be
327 * a valid Unicode codepoint. Here, however, only
328 * check the length and range.
330 if (**start
!= 'u' || *sz
< 5 || *sz
> 7)
332 if (*sz
== 7 && ((*start
)[1] != '1' || (*start
)[2] != '0'))
334 if (*sz
== 6 && (*start
)[1] == '0')
336 if ((int)strspn(*start
+ 1, "0123456789ABCDEFabcdef")
338 gly
= ESCAPE_UNICODE
;
348 * Parse a quoted or unquoted roff-style request or macro argument.
349 * Return a pointer to the parsed argument, which is either the original
350 * pointer or advanced by one byte in case the argument is quoted.
351 * NUL-terminate the argument in place.
352 * Collapse pairs of quotes inside quoted arguments.
353 * Advance the argument pointer to the next argument,
354 * or to the NUL byte terminating the argument line.
357 mandoc_getarg(struct mparse
*parse
, char **cpp
, int ln
, int *pos
)
360 int quoted
, pairs
, white
;
362 /* Quoting can only start with a new word. */
372 for (cp
= start
; '\0' != *cp
; cp
++) {
375 * Move the following text left
376 * after quoted quotes and after "\\" and "\t".
383 * In copy mode, translate double to single
384 * backslashes and backslash-t to literal tabs.
395 /* Skip escaped blanks. */
402 } else if (0 == quoted
) {
404 /* Unescaped blanks end unquoted args. */
408 } else if ('"' == cp
[0]) {
410 /* Quoted quotes collapse. */
414 /* Unquoted quotes end quoted args. */
421 /* Quoted argument without a closing quote. */
423 mandoc_msg(MANDOCERR_ARG_QUOTE
, parse
, ln
, *pos
, NULL
);
425 /* NUL-terminate this argument and move to the next one. */
433 *pos
+= (int)(cp
- start
) + (quoted
? 1 : 0);
436 if ('\0' == *cp
&& (white
|| ' ' == cp
[-1]))
437 mandoc_msg(MANDOCERR_SPACE_EOL
, parse
, ln
, *pos
, NULL
);
443 a2time(time_t *t
, const char *fmt
, const char *p
)
448 memset(&tm
, 0, sizeof(struct tm
));
452 pp
= strptime(p
, fmt
, &tm
);
454 if (NULL
!= pp
&& '\0' == *pp
) {
476 * up to 9 characters for the month (September) + blank
477 * up to 2 characters for the day + comma + blank
478 * 4 characters for the year and a terminating '\0'
480 p
= buf
= mandoc_malloc(10 + 4 + 4 + 1);
482 if (0 == (ssz
= strftime(p
, 10 + 1, "%B ", tm
)))
486 if (-1 == (isz
= snprintf(p
, 4 + 1, "%d, ", tm
->tm_mday
)))
490 if (0 == strftime(p
, 4 + 1, "%Y", tm
))
500 mandoc_normdate(struct mparse
*parse
, char *in
, int ln
, int pos
)
505 if (NULL
== in
|| '\0' == *in
||
506 0 == strcmp(in
, "$" "Mdocdate$")) {
507 mandoc_msg(MANDOCERR_DATE_MISSING
, parse
, ln
, pos
, NULL
);
510 else if (a2time(&t
, "%Y-%m-%d", in
))
512 else if (!a2time(&t
, "$" "Mdocdate: %b %d %Y $", in
) &&
513 !a2time(&t
, "%b %d, %Y", in
)) {
514 mandoc_msg(MANDOCERR_DATE_BAD
, parse
, ln
, pos
, in
);
517 out
= t
? time2a(t
) : NULL
;
518 return out
? out
: mandoc_strdup(in
);
522 mandoc_eos(const char *p
, size_t sz
)
531 * End-of-sentence recognition must include situations where
532 * some symbols, such as `)', allow prior EOS punctuation to
536 enclosed
= found
= 0;
537 for (q
= p
+ (int)sz
- 1; q
>= p
; q
--) {
553 (!enclosed
|| isalnum((unsigned char)*q
));
557 return found
&& !enclosed
;
561 * Convert a string to a long that may not be <0.
562 * If the string is invalid, or is less than 0, return -1.
565 mandoc_strntoi(const char *p
, size_t sz
, int base
)
578 v
= strtol(buf
, &ep
, base
);
580 if (buf
[0] == '\0' || *ep
!= '\0')