]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
1 /* $Id: mandoc.c,v 1.110 2018/12/14 06:33:14 schwarze Exp $ */
3 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2015, 2017, 2018 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>
31 #include "mandoc_aux.h"
34 #include "libmandoc.h"
36 static int a2time(time_t *, const char *, const char *);
37 static char *time2a(time_t);
41 mandoc_escape(const char **end
, const char **start
, int *sz
)
43 const char *local_start
;
49 * When the caller doesn't provide return storage,
59 * Beyond the backslash, at least one input character
60 * is part of the escape sequence. With one exception
61 * (see below), that character won't be returned.
69 switch ((*start
)[-1]) {
71 * First the glyphs. There are several different forms of
72 * these, but each eventually returns a substring of the glyph
92 * Escapes taking no arguments at all.
103 * The \z escape is supposed to output the following
104 * character without advancing the cursor position.
105 * Since we are mostly dealing with terminal mode,
106 * let us just skip the next character.
109 return ESCAPE_SKIPCHAR
;
112 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
113 * 'X' is the trigger. These have opaque sub-strings.
126 if (ESCAPE_ERROR
== gly
)
143 if (strncmp(*start
, "(.T", 3) != 0)
151 * These escapes are of the form \X'Y', where 'X' is the trigger
152 * and 'Y' is any string. These have opaque sub-strings.
153 * The \B and \w escapes are handled in roff.c, roff_res().
166 if (gly
== ESCAPE_ERROR
)
167 gly
= ESCAPE_OVERSTRIKE
;
173 * These escapes are of the form \X'N', where 'X' is the trigger
174 * and 'N' resolves to a numerical expression.
183 if (strchr(" %&()*+-./0123456789:<=>", **start
)) {
188 switch ((*start
)[-1]) {
204 * Special handling for the numbered character escape.
205 * XXX Do any other escapes need similar handling?
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
)
249 *sz
= (*end
)[-1] == 's' &&
250 isdigit((unsigned char)(*end
)[1]) ? 2 : 1;
260 * Anything else is assumed to be a glyph.
261 * In this case, pass back the character after the backslash.
264 gly
= ESCAPE_SPECIAL
;
270 assert(ESCAPE_ERROR
!= gly
);
273 * Read up to the terminating character,
274 * paying attention to nested escapes.
278 while (**end
!= term
) {
285 mandoc_escape(end
, NULL
, NULL
))
293 *sz
= (*end
)++ - *start
;
296 if ((size_t)*sz
> strlen(*start
))
301 /* Run post-processors. */
306 if (**start
== 'C') {
307 if ((*start
)[1] == 'W' ||
308 (*start
)[1] == 'R') {
313 * Treat other constant-width font modes
314 * just like regular font modes.
319 if ((*start
)[0] == 'B' && (*start
)[1] == 'I')
323 } else if (*sz
!= 1) {
325 gly
= ESCAPE_FONTPREV
;
332 gly
= ESCAPE_FONTBOLD
;
336 gly
= ESCAPE_FONTITALIC
;
339 gly
= ESCAPE_FONTPREV
;
343 gly
= ESCAPE_FONTROMAN
;
348 if (**start
== 'c') {
350 gly
= ESCAPE_NOSPACE
;
353 if (*sz
< 6 || *sz
> 7 ||
354 strncmp(*start
, "char", 4) != 0 ||
355 (int)strspn(*start
+ 4, "0123456789") + 4 < *sz
)
358 for (i
= 4; i
< *sz
; i
++)
359 c
= 10 * c
+ ((*start
)[i
] - '0');
360 if (c
< 0x21 || (c
> 0x7e && c
< 0xa0) || c
> 0xff)
364 gly
= ESCAPE_NUMBERED
;
369 * Unicode escapes are defined in groff as \[u0000]
370 * to \[u10FFFF], where the contained value must be
371 * a valid Unicode codepoint. Here, however, only
372 * check the length and range.
374 if (**start
!= 'u' || *sz
< 5 || *sz
> 7)
376 if (*sz
== 7 && ((*start
)[1] != '1' || (*start
)[2] != '0'))
378 if (*sz
== 6 && (*start
)[1] == '0')
380 if (*sz
== 5 && (*start
)[1] == 'D' &&
381 strchr("89ABCDEF", (*start
)[2]) != NULL
)
383 if ((int)strspn(*start
+ 1, "0123456789ABCDEFabcdef")
385 gly
= ESCAPE_UNICODE
;
395 * Parse a quoted or unquoted roff-style request or macro argument.
396 * Return a pointer to the parsed argument, which is either the original
397 * pointer or advanced by one byte in case the argument is quoted.
398 * NUL-terminate the argument in place.
399 * Collapse pairs of quotes inside quoted arguments.
400 * Advance the argument pointer to the next argument,
401 * or to the NUL byte terminating the argument line.
404 mandoc_getarg(char **cpp
, int ln
, int *pos
)
407 int quoted
, pairs
, white
;
409 /* Quoting can only start with a new word. */
419 for (cp
= start
; '\0' != *cp
; cp
++) {
422 * Move the following text left
423 * after quoted quotes and after "\\" and "\t".
430 * In copy mode, translate double to single
431 * backslashes and backslash-t to literal tabs.
442 /* 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_ARG_QUOTE
, ln
, *pos
, NULL
);
472 /* NUL-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_SPACE_EOL
, 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
) {
523 * up to 9 characters for the month (September) + blank
524 * up to 2 characters for the day + comma + blank
525 * 4 characters for the year and a terminating '\0'
528 p
= buf
= mandoc_malloc(10 + 4 + 4 + 1);
530 if ((ssz
= strftime(p
, 10 + 1, "%B ", tm
)) == 0)
535 * The output format is just "%d" here, not "%2d" or "%02d".
536 * That's also the reason why we can't just format the
537 * date as a whole with "%B %e, %Y" or "%B %d, %Y".
538 * Besides, the present approach is less prone to buffer
539 * overflows, in case anybody should ever introduce the bug
540 * of looking at LC_TIME.
543 if ((isz
= snprintf(p
, 4 + 1, "%d, ", tm
->tm_mday
)) == -1)
547 if (strftime(p
, 4 + 1, "%Y", tm
) == 0)
557 mandoc_normdate(struct roff_man
*man
, char *in
, int ln
, int pos
)
562 /* No date specified: use today's date. */
564 if (in
== NULL
|| *in
== '\0' || strcmp(in
, "$" "Mdocdate$") == 0) {
565 mandoc_msg(MANDOCERR_DATE_MISSING
, ln
, pos
, NULL
);
566 return time2a(time(NULL
));
569 /* Valid mdoc(7) date format. */
571 if (a2time(&t
, "$" "Mdocdate: %b %d %Y $", in
) ||
572 a2time(&t
, "%b %d, %Y", in
)) {
574 if (t
> time(NULL
) + 86400)
575 mandoc_msg(MANDOCERR_DATE_FUTURE
, ln
, pos
, "%s", cp
);
576 else if (*in
!= '$' && strcmp(in
, cp
) != 0)
577 mandoc_msg(MANDOCERR_DATE_NORM
, ln
, pos
, "%s", cp
);
581 /* In man(7), do not warn about the legacy format. */
583 if (a2time(&t
, "%Y-%m-%d", in
) == 0)
584 mandoc_msg(MANDOCERR_DATE_BAD
, ln
, pos
, "%s", in
);
585 else if (t
> time(NULL
) + 86400)
586 mandoc_msg(MANDOCERR_DATE_FUTURE
, ln
, pos
, "%s", in
);
587 else if (man
->macroset
== MACROSET_MDOC
)
588 mandoc_msg(MANDOCERR_DATE_LEGACY
, ln
, pos
, "Dd %s", in
);
590 /* Use any non-mdoc(7) date verbatim. */
592 return mandoc_strdup(in
);
596 mandoc_eos(const char *p
, size_t sz
)
605 * End-of-sentence recognition must include situations where
606 * some symbols, such as `)', allow prior EOS punctuation to
610 enclosed
= found
= 0;
611 for (q
= p
+ (int)sz
- 1; q
>= p
; q
--) {
627 (!enclosed
|| isalnum((unsigned char)*q
));
631 return found
&& !enclosed
;
635 * Convert a string to a long that may not be <0.
636 * If the string is invalid, or is less than 0, return -1.
639 mandoc_strntoi(const char *p
, size_t sz
, int base
)
652 v
= strtol(buf
, &ep
, base
);
654 if (buf
[0] == '\0' || *ep
!= '\0')