]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
1 /* $Id: mandoc.c,v 1.59 2011/09/18 14:14:15 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 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
)
190 * These escapes are of the form \X'Y', where 'X' is the trigger
191 * and 'Y' is any string. These have opaque sub-strings.
207 return(ESCAPE_ERROR
);
213 * These escapes are of the form \X'N', where 'X' is the trigger
214 * and 'N' resolves to a numerical expression.
227 if (ESCAPE_ERROR
== gly
)
228 gly
= ESCAPE_NUMBERED
;
237 if (ESCAPE_ERROR
== gly
)
240 return(ESCAPE_ERROR
);
241 term
= numeric
= '\'';
245 * Sizes get a special category of their own.
254 /* See +/- counts as a sign. */
256 if ('+' == c
|| '-' == c
|| ASCII_HYPH
== c
)
264 term
= numeric
= ']';
267 term
= numeric
= '\'';
275 /* See +/- counts as a sign. */
277 if ('+' == c
|| '-' == c
|| ASCII_HYPH
== c
)
283 * Anything else is assumed to be a glyph.
286 gly
= ESCAPE_SPECIAL
;
292 assert(ESCAPE_ERROR
!= gly
);
299 * If a terminating block has been specified, we need to
300 * handle the case of recursion, which could have their
301 * own terminating blocks that mess up our parse. This, by the
302 * way, means that the "start" and "size" values will be
303 * effectively meaningless.
307 if (numeric
&& -1 == (ssz
= numescape(&cp
[i
])))
308 return(ESCAPE_ERROR
);
314 * We have a character terminator. Try to read up to that
315 * character. If we can't (i.e., we hit the nil), then return
316 * an error; if we can, calculate our length, read past the
317 * terminating character, and exit.
321 *end
= strchr(&cp
[i
], term
);
323 return(ESCAPE_ERROR
);
325 rlim
= *end
- &cp
[i
];
335 * We have a numeric limit. If the string is shorter than that,
336 * stop and return an error. Else adjust our endpoint, length,
337 * and return the current glyph.
340 if ((size_t)lim
> strlen(&cp
[i
]))
341 return(ESCAPE_ERROR
);
350 assert(rlim
>= 0 && rstart
);
352 /* Run post-processors. */
362 gly
= ESCAPE_FONTBOLD
;
367 gly
= ESCAPE_FONTITALIC
;
370 gly
= ESCAPE_FONTPREV
;
375 gly
= ESCAPE_FONTROMAN
;
379 case (ESCAPE_SPECIAL
):
383 gly
= ESCAPE_NOSPACE
;
393 mandoc_calloc(size_t num
, size_t size
)
397 ptr
= calloc(num
, size
);
400 exit((int)MANDOCLEVEL_SYSERR
);
408 mandoc_malloc(size_t size
)
415 exit((int)MANDOCLEVEL_SYSERR
);
423 mandoc_realloc(void *ptr
, size_t size
)
426 ptr
= realloc(ptr
, size
);
429 exit((int)MANDOCLEVEL_SYSERR
);
436 mandoc_strndup(const char *ptr
, size_t sz
)
440 p
= mandoc_malloc(sz
+ 1);
447 mandoc_strdup(const char *ptr
)
454 exit((int)MANDOCLEVEL_SYSERR
);
461 * Parse a quoted or unquoted roff-style request or macro argument.
462 * Return a pointer to the parsed argument, which is either the original
463 * pointer or advanced by one byte in case the argument is quoted.
464 * Null-terminate the argument in place.
465 * Collapse pairs of quotes inside quoted arguments.
466 * Advance the argument pointer to the next argument,
467 * or to the null byte terminating the argument line.
470 mandoc_getarg(struct mparse
*parse
, char **cpp
, int ln
, int *pos
)
473 int quoted
, pairs
, white
;
475 /* Quoting can only start with a new word. */
485 for (cp
= start
; '\0' != *cp
; cp
++) {
486 /* Move left after quoted quotes and escaped backslashes. */
491 /* Poor man's copy mode. */
494 } else if (0 == quoted
&& ' ' == cp
[1])
495 /* Skip escaped blanks. */
497 } else if (0 == quoted
) {
499 /* Unescaped blanks end unquoted args. */
503 } else if ('"' == cp
[0]) {
505 /* Quoted quotes collapse. */
509 /* Unquoted quotes end quoted args. */
516 /* Quoted argument without a closing quote. */
518 mandoc_msg(MANDOCERR_BADQUOTE
, parse
, ln
, *pos
, NULL
);
520 /* Null-terminate this argument and move to the next one. */
528 *pos
+= (int)(cp
- start
) + (quoted
? 1 : 0);
531 if ('\0' == *cp
&& (white
|| ' ' == cp
[-1]))
532 mandoc_msg(MANDOCERR_EOLNSPACE
, parse
, ln
, *pos
, NULL
);
538 a2time(time_t *t
, const char *fmt
, const char *p
)
543 memset(&tm
, 0, sizeof(struct tm
));
547 pp
= strptime(p
, fmt
, &tm
);
549 if (NULL
!= pp
&& '\0' == *pp
) {
569 * up to 9 characters for the month (September) + blank
570 * up to 2 characters for the day + comma + blank
571 * 4 characters for the year and a terminating '\0'
573 p
= buf
= mandoc_malloc(10 + 4 + 4 + 1);
575 if (0 == (ssz
= strftime(p
, 10 + 1, "%B ", tm
)))
579 if (-1 == (isz
= snprintf(p
, 4 + 1, "%d, ", tm
->tm_mday
)))
583 if (0 == strftime(p
, 4 + 1, "%Y", tm
))
593 mandoc_normdate(struct mparse
*parse
, char *in
, int ln
, int pos
)
598 if (NULL
== in
|| '\0' == *in
||
599 0 == strcmp(in
, "$" "Mdocdate$")) {
600 mandoc_msg(MANDOCERR_NODATE
, parse
, ln
, pos
, NULL
);
603 else if (!a2time(&t
, "$" "Mdocdate: %b %d %Y $", in
) &&
604 !a2time(&t
, "%b %d, %Y", in
) &&
605 !a2time(&t
, "%Y-%m-%d", in
)) {
606 mandoc_msg(MANDOCERR_BADDATE
, parse
, ln
, pos
, NULL
);
609 out
= t
? time2a(t
) : NULL
;
610 return(out
? out
: mandoc_strdup(in
));
614 mandoc_eos(const char *p
, size_t sz
, int enclosed
)
623 * End-of-sentence recognition must include situations where
624 * some symbols, such as `)', allow prior EOS punctuation to
629 for (q
= p
+ (int)sz
- 1; q
>= p
; q
--) {
649 return(found
&& (!enclosed
|| isalnum((unsigned char)*q
)));
653 return(found
&& !enclosed
);
657 * Find out whether a line is a macro line or not. If it is, adjust the
658 * current position and return one; if it isn't, return zero and don't
659 * change the current position.
662 mandoc_getcontrol(const char *cp
, int *ppos
)
668 if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
670 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
675 while (' ' == cp
[pos
] || '\t' == cp
[pos
])
683 * Convert a string to a long that may not be <0.
684 * If the string is invalid, or is less than 0, return -1.
687 mandoc_strntoi(const char *p
, size_t sz
, int base
)
700 v
= strtol(buf
, &ep
, base
);
702 if (buf
[0] == '\0' || *ep
!= '\0')