]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
1 /* $Id: mandoc.c,v 1.52 2011/05/15 15:30:33 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011 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 "libmandoc.h"
38 static int a2time(time_t *, const char *, const char *);
39 static char *time2a(time_t);
40 static int numescape(const char *);
43 * Pass over recursive numerical expressions. This context of this
44 * function is important: it's only called within character-terminating
45 * escapes (e.g., \s[xxxyyy]), so all we need to do is handle initial
46 * recursion: we don't care about what's in these blocks.
47 * This returns the number of characters skipped or -1 if an error
48 * occurs (the caller should bail).
51 numescape(const char *start
)
59 /* The expression consists of a subexpression. */
61 if ('\\' == start
[i
]) {
64 * Read past the end of the subexpression.
65 * Bail immediately on errors.
67 if (ESCAPE_ERROR
== mandoc_escape(&cp
, NULL
, NULL
))
69 return(i
+ cp
- &start
[i
]);
72 if ('(' != start
[i
++])
76 * A parenthesised subexpression. Read until the closing
77 * parenthesis, making sure to handle any nested subexpressions
78 * that might ruin our parse.
81 while (')' != start
[i
]) {
82 sz
= strcspn(&start
[i
], ")\\");
87 else if ('\\' != start
[i
])
91 if (ESCAPE_ERROR
== mandoc_escape(&cp
, NULL
, NULL
))
96 /* Read past the terminating ')'. */
101 mandoc_escape(const char **end
, const char **start
, int *sz
)
103 char c
, term
, numeric
;
104 int i
, lim
, ssz
, rlim
;
105 const char *cp
, *rstart
;
114 term
= numeric
= '\0';
116 switch ((c
= cp
[i
++])) {
118 * First the glyphs. There are several different forms of
119 * these, but each eventually returns a substring of the glyph
123 gly
= ESCAPE_SPECIAL
;
127 gly
= ESCAPE_SPECIAL
;
129 * Unicode escapes are defined in groff as \[uXXXX] to
130 * \[u10FFFF], where the contained value must be a valid
131 * Unicode codepoint. Here, however, only check whether
132 * it's not a zero-width escape.
134 if ('u' == cp
[i
] && ']' != cp
[i
+ 1])
135 gly
= ESCAPE_UNICODE
;
140 return(ESCAPE_ERROR
);
141 gly
= ESCAPE_SPECIAL
;
146 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
147 * 'X' is the trigger. These have opaque sub-strings.
164 if (ESCAPE_ERROR
== gly
)
168 if (ESCAPE_ERROR
== gly
)
172 if (ESCAPE_ERROR
== gly
)
194 * These escapes are of the form \X'Y', where 'X' is the trigger
195 * and 'Y' is any string. These have opaque sub-strings.
211 return(ESCAPE_ERROR
);
217 * These escapes are of the form \X'N', where 'X' is the trigger
218 * and 'N' resolves to a numerical expression.
231 if (ESCAPE_ERROR
== gly
)
232 gly
= ESCAPE_NUMBERED
;
241 if (ESCAPE_ERROR
== gly
)
244 return(ESCAPE_ERROR
);
245 term
= numeric
= '\'';
249 * Sizes get a special category of their own.
258 /* See +/- counts as a sign. */
260 if ('+' == c
|| '-' == c
|| ASCII_HYPH
== c
)
268 term
= numeric
= ']';
271 term
= numeric
= '\'';
279 /* See +/- counts as a sign. */
281 if ('+' == c
|| '-' == c
|| ASCII_HYPH
== c
)
287 * Anything else is assumed to be a glyph.
290 gly
= ESCAPE_SPECIAL
;
296 assert(ESCAPE_ERROR
!= gly
);
303 * If a terminating block has been specified, we need to
304 * handle the case of recursion, which could have their
305 * own terminating blocks that mess up our parse. This, by the
306 * way, means that the "start" and "size" values will be
307 * effectively meaningless.
311 if (numeric
&& -1 == (ssz
= numescape(&cp
[i
])))
312 return(ESCAPE_ERROR
);
318 * We have a character terminator. Try to read up to that
319 * character. If we can't (i.e., we hit the nil), then return
320 * an error; if we can, calculate our length, read past the
321 * terminating character, and exit.
325 *end
= strchr(&cp
[i
], term
);
327 return(ESCAPE_ERROR
);
329 rlim
= *end
- &cp
[i
];
339 * We have a numeric limit. If the string is shorter than that,
340 * stop and return an error. Else adjust our endpoint, length,
341 * and return the current glyph.
344 if ((size_t)lim
> strlen(&cp
[i
]))
345 return(ESCAPE_ERROR
);
354 assert(rlim
>= 0 && rstart
);
356 /* Run post-processors. */
366 gly
= ESCAPE_FONTBOLD
;
371 gly
= ESCAPE_FONTITALIC
;
374 gly
= ESCAPE_FONTPREV
;
379 gly
= ESCAPE_FONTROMAN
;
383 case (ESCAPE_SPECIAL
):
387 gly
= ESCAPE_NOSPACE
;
397 mandoc_calloc(size_t num
, size_t size
)
401 ptr
= calloc(num
, size
);
404 exit((int)MANDOCLEVEL_SYSERR
);
412 mandoc_malloc(size_t size
)
419 exit((int)MANDOCLEVEL_SYSERR
);
427 mandoc_realloc(void *ptr
, size_t size
)
430 ptr
= realloc(ptr
, size
);
433 exit((int)MANDOCLEVEL_SYSERR
);
441 mandoc_strdup(const char *ptr
)
448 exit((int)MANDOCLEVEL_SYSERR
);
455 * Parse a quoted or unquoted roff-style request or macro argument.
456 * Return a pointer to the parsed argument, which is either the original
457 * pointer or advanced by one byte in case the argument is quoted.
458 * Null-terminate the argument in place.
459 * Collapse pairs of quotes inside quoted arguments.
460 * Advance the argument pointer to the next argument,
461 * or to the null byte terminating the argument line.
464 mandoc_getarg(struct mparse
*parse
, char **cpp
, int ln
, int *pos
)
467 int quoted
, pairs
, white
;
469 /* Quoting can only start with a new word. */
479 for (cp
= start
; '\0' != *cp
; cp
++) {
480 /* Move left after quoted quotes and escaped backslashes. */
485 /* Poor man's copy mode. */
488 } else if (0 == quoted
&& ' ' == cp
[1])
489 /* Skip escaped blanks. */
491 } else if (0 == quoted
) {
493 /* Unescaped blanks end unquoted args. */
497 } else if ('"' == cp
[0]) {
499 /* Quoted quotes collapse. */
503 /* Unquoted quotes end quoted args. */
510 /* Quoted argument without a closing quote. */
512 mandoc_msg(MANDOCERR_BADQUOTE
, parse
, ln
, *pos
, NULL
);
514 /* Null-terminate this argument and move to the next one. */
522 *pos
+= (int)(cp
- start
) + (quoted
? 1 : 0);
525 if ('\0' == *cp
&& (white
|| ' ' == cp
[-1]))
526 mandoc_msg(MANDOCERR_EOLNSPACE
, parse
, ln
, *pos
, NULL
);
532 a2time(time_t *t
, const char *fmt
, const char *p
)
537 memset(&tm
, 0, sizeof(struct tm
));
539 pp
= strptime(p
, fmt
, &tm
);
540 if (NULL
!= pp
&& '\0' == *pp
) {
556 localtime_r(&t
, &tm
);
560 * up to 9 characters for the month (September) + blank
561 * up to 2 characters for the day + comma + blank
562 * 4 characters for the year and a terminating '\0'
564 p
= buf
= mandoc_malloc(10 + 4 + 4 + 1);
566 if (0 == (ssz
= strftime(p
, 10 + 1, "%B ", &tm
)))
570 if (-1 == (isz
= snprintf(p
, 4 + 1, "%d, ", tm
.tm_mday
)))
574 if (0 == strftime(p
, 4 + 1, "%Y", &tm
))
584 mandoc_normdate(struct mparse
*parse
, char *in
, int ln
, int pos
)
589 if (NULL
== in
|| '\0' == *in
||
590 0 == strcmp(in
, "$" "Mdocdate$")) {
591 mandoc_msg(MANDOCERR_NODATE
, parse
, ln
, pos
, NULL
);
594 else if (!a2time(&t
, "$" "Mdocdate: %b %d %Y $", in
) &&
595 !a2time(&t
, "%b %d, %Y", in
) &&
596 !a2time(&t
, "%Y-%m-%d", in
)) {
597 mandoc_msg(MANDOCERR_BADDATE
, parse
, ln
, pos
, NULL
);
600 out
= t
? time2a(t
) : NULL
;
601 return(out
? out
: mandoc_strdup(in
));
605 mandoc_eos(const char *p
, size_t sz
, int enclosed
)
614 * End-of-sentence recognition must include situations where
615 * some symbols, such as `)', allow prior EOS punctuation to
620 for (q
= p
+ (int)sz
- 1; q
>= p
; q
--) {
640 return(found
&& (!enclosed
|| isalnum((unsigned char)*q
)));
644 return(found
&& !enclosed
);
648 mandoc_hyph(const char *start
, const char *c
)
652 * Choose whether to break at a hyphenated character. We only
653 * do this if it's free-standing within a word.
656 /* Skip first/last character of buffer. */
657 if (c
== start
|| '\0' == *(c
+ 1))
659 /* Skip first/last character of word. */
660 if ('\t' == *(c
+ 1) || '\t' == *(c
- 1))
662 if (' ' == *(c
+ 1) || ' ' == *(c
- 1))
664 /* Skip double invocations. */
665 if ('-' == *(c
+ 1) || '-' == *(c
- 1))
668 if ('\\' == *(c
- 1))
675 * Find out whether a line is a macro line or not. If it is, adjust the
676 * current position and return one; if it isn't, return zero and don't
677 * change the current position.
680 mandoc_getcontrol(const char *cp
, int *ppos
)
686 if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
688 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
693 while (' ' == cp
[pos
] || '\t' == cp
[pos
])
701 * Convert a string to a long that may not be <0.
702 * If the string is invalid, or is less than 0, return -1.
705 mandoc_strntou(const char *p
, size_t sz
, int base
)
718 v
= strtol(buf
, &ep
, base
);
720 if (buf
[0] == '\0' || *ep
!= '\0')
723 if ((errno
== ERANGE
&&
724 (v
== LONG_MAX
|| v
== LONG_MIN
)) ||
725 (v
> INT_MAX
|| v
< 0))