]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
1 /* $Id: mandoc.c,v 1.100 2017/06/02 19:21:23 schwarze Exp $ */
3 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2015, 2017 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"
35 static int a2time(time_t *, const char *, const char *);
36 static char *time2a(time_t);
40 mandoc_escape(const char **end
, const char **start
, int *sz
)
42 const char *local_start
;
48 * When the caller doesn't provide return storage,
58 * Beyond the backslash, at least one input character
59 * is part of the escape sequence. With one exception
60 * (see below), that character won't be returned.
68 switch ((*start
)[-1]) {
70 * First the glyphs. There are several different forms of
71 * these, but each eventually returns a substring of the glyph
91 * Escapes taking no arguments at all.
100 * The \z escape is supposed to output the following
101 * character without advancing the cursor position.
102 * Since we are mostly dealing with terminal mode,
103 * let us just skip the next character.
106 return ESCAPE_SKIPCHAR
;
109 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
110 * 'X' is the trigger. These have opaque sub-strings.
123 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.
143 * The \B and \w escapes are handled in roff.c, roff_res().
156 if (gly
== ESCAPE_ERROR
)
157 gly
= ESCAPE_OVERSTRIKE
;
163 * These escapes are of the form \X'N', where 'X' is the trigger
164 * and 'N' resolves to a numerical expression.
173 if (strchr(" %&()*+-./0123456789:<=>", **start
)) {
178 switch ((*start
)[-1]) {
194 * Special handling for the numbered character escape.
195 * XXX Do any other escapes need similar handling?
201 if (isdigit((unsigned char)**start
)) {
203 return ESCAPE_IGNORE
;
206 while (isdigit((unsigned char)**end
))
211 return ESCAPE_NUMBERED
;
214 * Sizes get a special category of their own.
219 /* See +/- counts as a sign. */
220 if ('+' == **end
|| '-' == **end
|| ASCII_HYPH
== **end
)
239 *sz
= (*end
)[-1] == 's' &&
240 isdigit((unsigned char)(*end
)[1]) ? 2 : 1;
250 * Anything else is assumed to be a glyph.
251 * In this case, pass back the character after the backslash.
254 gly
= ESCAPE_SPECIAL
;
260 assert(ESCAPE_ERROR
!= gly
);
263 * Read up to the terminating character,
264 * paying attention to nested escapes.
268 while (**end
!= term
) {
275 mandoc_escape(end
, NULL
, NULL
))
283 *sz
= (*end
)++ - *start
;
286 if ((size_t)*sz
> strlen(*start
))
291 /* Run post-processors. */
296 if ('C' == **start
) {
298 * Treat constant-width font modes
299 * just like regular font modes.
304 if ('B' == (*start
)[0] && 'I' == (*start
)[1])
314 gly
= ESCAPE_FONTBOLD
;
318 gly
= ESCAPE_FONTITALIC
;
321 gly
= ESCAPE_FONTPREV
;
325 gly
= ESCAPE_FONTROMAN
;
330 if (1 == *sz
&& 'c' == **start
)
331 gly
= ESCAPE_NOSPACE
;
333 * Unicode escapes are defined in groff as \[u0000]
334 * to \[u10FFFF], where the contained value must be
335 * a valid Unicode codepoint. Here, however, only
336 * check the length and range.
338 if (**start
!= 'u' || *sz
< 5 || *sz
> 7)
340 if (*sz
== 7 && ((*start
)[1] != '1' || (*start
)[2] != '0'))
342 if (*sz
== 6 && (*start
)[1] == '0')
344 if (*sz
== 5 && (*start
)[1] == 'D' &&
345 strchr("89ABCDEF", (*start
)[2]) != NULL
)
347 if ((int)strspn(*start
+ 1, "0123456789ABCDEFabcdef")
349 gly
= ESCAPE_UNICODE
;
359 * Parse a quoted or unquoted roff-style request or macro argument.
360 * Return a pointer to the parsed argument, which is either the original
361 * pointer or advanced by one byte in case the argument is quoted.
362 * NUL-terminate the argument in place.
363 * Collapse pairs of quotes inside quoted arguments.
364 * Advance the argument pointer to the next argument,
365 * or to the NUL byte terminating the argument line.
368 mandoc_getarg(struct mparse
*parse
, char **cpp
, int ln
, int *pos
)
371 int quoted
, pairs
, white
;
373 /* Quoting can only start with a new word. */
383 for (cp
= start
; '\0' != *cp
; cp
++) {
386 * Move the following text left
387 * after quoted quotes and after "\\" and "\t".
394 * In copy mode, translate double to single
395 * backslashes and backslash-t to literal tabs.
406 /* Skip escaped blanks. */
413 } else if (0 == quoted
) {
415 /* Unescaped blanks end unquoted args. */
419 } else if ('"' == cp
[0]) {
421 /* Quoted quotes collapse. */
425 /* Unquoted quotes end quoted args. */
432 /* Quoted argument without a closing quote. */
434 mandoc_msg(MANDOCERR_ARG_QUOTE
, parse
, ln
, *pos
, NULL
);
436 /* NUL-terminate this argument and move to the next one. */
444 *pos
+= (int)(cp
- start
) + (quoted
? 1 : 0);
447 if ('\0' == *cp
&& (white
|| ' ' == cp
[-1]))
448 mandoc_msg(MANDOCERR_SPACE_EOL
, parse
, ln
, *pos
, NULL
);
454 a2time(time_t *t
, const char *fmt
, const char *p
)
459 memset(&tm
, 0, sizeof(struct tm
));
463 pp
= strptime(p
, fmt
, &tm
);
465 if (NULL
!= pp
&& '\0' == *pp
) {
487 * up to 9 characters for the month (September) + blank
488 * up to 2 characters for the day + comma + blank
489 * 4 characters for the year and a terminating '\0'
492 p
= buf
= mandoc_malloc(10 + 4 + 4 + 1);
494 if ((ssz
= strftime(p
, 10 + 1, "%B ", tm
)) == 0)
499 * The output format is just "%d" here, not "%2d" or "%02d".
500 * That's also the reason why we can't just format the
501 * date as a whole with "%B %e, %Y" or "%B %d, %Y".
502 * Besides, the present approach is less prone to buffer
503 * overflows, in case anybody should ever introduce the bug
504 * of looking at LC_TIME.
507 if ((isz
= snprintf(p
, 4 + 1, "%d, ", tm
->tm_mday
)) == -1)
511 if (strftime(p
, 4 + 1, "%Y", tm
) == 0)
521 mandoc_normdate(struct mparse
*parse
, char *in
, int ln
, int pos
)
525 /* No date specified: use today's date. */
527 if (in
== NULL
|| *in
== '\0' || strcmp(in
, "$" "Mdocdate$") == 0) {
528 mandoc_msg(MANDOCERR_DATE_MISSING
, parse
, ln
, pos
, NULL
);
529 return time2a(time(NULL
));
532 /* Valid mdoc(7) date format. */
534 if (a2time(&t
, "$" "Mdocdate: %b %d %Y $", in
) ||
535 a2time(&t
, "%b %d, %Y", in
))
538 /* Do not warn about the legacy man(7) format. */
540 if ( ! a2time(&t
, "%Y-%m-%d", in
))
541 mandoc_msg(MANDOCERR_DATE_BAD
, parse
, ln
, pos
, in
);
543 /* Use any non-mdoc(7) date verbatim. */
545 return mandoc_strdup(in
);
549 mandoc_eos(const char *p
, size_t sz
)
558 * End-of-sentence recognition must include situations where
559 * some symbols, such as `)', allow prior EOS punctuation to
563 enclosed
= found
= 0;
564 for (q
= p
+ (int)sz
- 1; q
>= p
; q
--) {
580 (!enclosed
|| isalnum((unsigned char)*q
));
584 return found
&& !enclosed
;
588 * Convert a string to a long that may not be <0.
589 * If the string is invalid, or is less than 0, return -1.
592 mandoc_strntoi(const char *p
, size_t sz
, int base
)
605 v
= strtol(buf
, &ep
, base
);
607 if (buf
[0] == '\0' || *ep
!= '\0')