]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
1 /* $Id: mandoc.c,v 1.46 2011/04/09 15:35:30 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>
32 #include "libmandoc.h"
36 static int a2time(time_t *, const char *, const char *);
37 static char *time2a(time_t);
38 static int numescape(const char *);
41 * Pass over recursive numerical expressions. This context of this
42 * function is important: it's only called within character-terminating
43 * escapes (e.g., \s[xxxyyy]), so all we need to do is handle initial
44 * recursion: we don't care about what's in these blocks.
45 * This returns the number of characters skipped or -1 if an error
46 * occurs (the caller should bail).
49 numescape(const char *start
)
57 /* The expression consists of a subexpression. */
59 if ('\\' == start
[i
]) {
62 * Read past the end of the subexpression.
63 * Bail immediately on errors.
65 if (ESCAPE_ERROR
== mandoc_escape(&cp
, NULL
, NULL
))
67 return(i
+ cp
- &start
[i
]);
70 if ('(' != start
[i
++])
74 * A parenthesised subexpression. Read until the closing
75 * parenthesis, making sure to handle any nested subexpressions
76 * that might ruin our parse.
79 while (')' != start
[i
]) {
80 sz
= strcspn(&start
[i
], ")\\");
85 else if ('\\' != start
[i
])
89 if (ESCAPE_ERROR
== mandoc_escape(&cp
, NULL
, NULL
))
94 /* Read past the terminating ')'. */
99 * Handle an escaped sequeence. This should be called with any
100 * string subsequent a `\'. Pass a pointer to this substring as "end";
101 * it will be set to the supremum of the parsed escape sequence. If
102 * this returns ESCAPE_ERROR, the string is bogus and should be thrown
103 * away. If not ESCAPE_ERROR or ESCAPE_IGNORE, "start" is set to the
104 * first relevant character of the substring (font, glyph, whatever) of
105 * length sz. Both "start" and "sz" may be NULL.
108 mandoc_escape(const char **end
, const char **start
, int *sz
)
110 char c
, term
, numeric
;
111 int i
, lim
, ssz
, rlim
;
112 const char *cp
, *rstart
;
121 term
= numeric
= '\0';
123 switch ((c
= cp
[i
++])) {
125 * First the glyphs. There are several different forms of
126 * these, but each eventually returns a substring of the glyph
130 gly
= ESCAPE_SPECIAL
;
134 gly
= ESCAPE_SPECIAL
;
139 return(ESCAPE_ERROR
);
140 gly
= ESCAPE_SPECIAL
;
145 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
146 * 'X' is the trigger. These have opaque sub-strings.
163 if (ESCAPE_ERROR
== gly
)
167 if (ESCAPE_ERROR
== gly
)
171 if (ESCAPE_ERROR
== gly
)
193 * These escapes are of the form \X'Y', where 'X' is the trigger
194 * and 'Y' is any string. These have opaque sub-strings.
210 return(ESCAPE_ERROR
);
216 * These escapes are of the form \X'N', where 'X' is the trigger
217 * and 'N' resolves to a numerical expression.
230 if (ESCAPE_ERROR
== gly
)
231 gly
= ESCAPE_NUMBERED
;
240 if (ESCAPE_ERROR
== gly
)
243 return(ESCAPE_ERROR
);
244 term
= numeric
= '\'';
248 * Sizes get a special category of their own.
257 /* See +/- counts as a sign. */
259 if ('+' == c
|| '-' == c
|| ASCII_HYPH
== c
)
267 term
= numeric
= ']';
270 term
= numeric
= '\'';
278 /* See +/- counts as a sign. */
280 if ('+' == c
|| '-' == c
|| ASCII_HYPH
== c
)
286 * Anything else is assumed to be a glyph.
289 gly
= ESCAPE_SPECIAL
;
295 assert(ESCAPE_ERROR
!= gly
);
302 * If a terminating block has been specified, we need to
303 * handle the case of recursion, which could have their
304 * own terminating blocks that mess up our parse. This, by the
305 * way, means that the "start" and "size" values will be
306 * effectively meaningless.
310 if (numeric
&& -1 == (ssz
= numescape(&cp
[i
])))
311 return(ESCAPE_ERROR
);
317 * We have a character terminator. Try to read up to that
318 * character. If we can't (i.e., we hit the nil), then return
319 * an error; if we can, calculate our length, read past the
320 * terminating character, and exit.
324 *end
= strchr(&cp
[i
], term
);
326 return(ESCAPE_ERROR
);
328 rlim
= *end
- &cp
[i
];
338 * We have a numeric limit. If the string is shorter than that,
339 * stop and return an error. Else adjust our endpoint, length,
340 * and return the current glyph.
343 if ((size_t)lim
> strlen(&cp
[i
]))
344 return(ESCAPE_ERROR
);
353 assert(rlim
>= 0 && rstart
);
355 /* Run post-processors. */
365 gly
= ESCAPE_FONTBOLD
;
370 gly
= ESCAPE_FONTITALIC
;
373 gly
= ESCAPE_FONTPREV
;
378 gly
= ESCAPE_FONTROMAN
;
382 case (ESCAPE_SPECIAL
):
386 gly
= ESCAPE_NOSPACE
;
396 mandoc_calloc(size_t num
, size_t size
)
400 ptr
= calloc(num
, size
);
403 exit((int)MANDOCLEVEL_SYSERR
);
411 mandoc_malloc(size_t size
)
418 exit((int)MANDOCLEVEL_SYSERR
);
426 mandoc_realloc(void *ptr
, size_t size
)
429 ptr
= realloc(ptr
, size
);
432 exit((int)MANDOCLEVEL_SYSERR
);
440 mandoc_strdup(const char *ptr
)
447 exit((int)MANDOCLEVEL_SYSERR
);
454 * Parse a quoted or unquoted roff-style request or macro argument.
455 * Return a pointer to the parsed argument, which is either the original
456 * pointer or advanced by one byte in case the argument is quoted.
457 * Null-terminate the argument in place.
458 * Collapse pairs of quotes inside quoted arguments.
459 * Advance the argument pointer to the next argument,
460 * or to the null byte terminating the argument line.
463 mandoc_getarg(struct mparse
*parse
, char **cpp
, int ln
, int *pos
)
466 int quoted
, pairs
, white
;
468 /* Quoting can only start with a new word. */
478 for (cp
= start
; '\0' != *cp
; cp
++) {
479 /* Move left after quoted quotes and escaped backslashes. */
484 /* Poor man's copy mode. */
487 } else if (0 == quoted
&& ' ' == cp
[1])
488 /* Skip escaped blanks. */
490 } else if (0 == quoted
) {
492 /* Unescaped blanks end unquoted args. */
496 } else if ('"' == cp
[0]) {
498 /* Quoted quotes collapse. */
502 /* Unquoted quotes end quoted args. */
509 /* Quoted argument without a closing quote. */
511 mandoc_msg(MANDOCERR_BADQUOTE
, parse
, ln
, *pos
, NULL
);
513 /* Null-terminate this argument and move to the next one. */
521 *pos
+= (int)(cp
- start
) + (quoted
? 1 : 0);
524 if ('\0' == *cp
&& (white
|| ' ' == cp
[-1]))
525 mandoc_msg(MANDOCERR_EOLNSPACE
, parse
, ln
, *pos
, NULL
);
531 a2time(time_t *t
, const char *fmt
, const char *p
)
536 memset(&tm
, 0, sizeof(struct tm
));
538 pp
= strptime(p
, fmt
, &tm
);
539 if (NULL
!= pp
&& '\0' == *pp
) {
555 localtime_r(&t
, &tm
);
559 * up to 9 characters for the month (September) + blank
560 * up to 2 characters for the day + comma + blank
561 * 4 characters for the year and a terminating '\0'
563 p
= buf
= mandoc_malloc(10 + 4 + 4 + 1);
565 if (0 == (ssz
= strftime(p
, 10 + 1, "%B ", &tm
)))
569 if (-1 == (isz
= snprintf(p
, 4 + 1, "%d, ", tm
.tm_mday
)))
573 if (0 == strftime(p
, 4 + 1, "%Y", &tm
))
583 mandoc_normdate(struct mparse
*parse
, char *in
, int ln
, int pos
)
588 if (NULL
== in
|| '\0' == *in
||
589 0 == strcmp(in
, "$" "Mdocdate$")) {
590 mandoc_msg(MANDOCERR_NODATE
, parse
, ln
, pos
, NULL
);
593 else if (!a2time(&t
, "$" "Mdocdate: %b %d %Y $", in
) &&
594 !a2time(&t
, "%b %d, %Y", in
) &&
595 !a2time(&t
, "%Y-%m-%d", in
)) {
596 mandoc_msg(MANDOCERR_BADDATE
, parse
, ln
, pos
, NULL
);
599 out
= t
? time2a(t
) : NULL
;
600 return(out
? out
: mandoc_strdup(in
));
604 mandoc_eos(const char *p
, size_t sz
, int enclosed
)
613 * End-of-sentence recognition must include situations where
614 * some symbols, such as `)', allow prior EOS punctuation to
619 for (q
= p
+ (int)sz
- 1; q
>= p
; q
--) {
639 return(found
&& (!enclosed
|| isalnum((unsigned char)*q
)));
643 return(found
&& !enclosed
);
647 mandoc_hyph(const char *start
, const char *c
)
651 * Choose whether to break at a hyphenated character. We only
652 * do this if it's free-standing within a word.
655 /* Skip first/last character of buffer. */
656 if (c
== start
|| '\0' == *(c
+ 1))
658 /* Skip first/last character of word. */
659 if ('\t' == *(c
+ 1) || '\t' == *(c
- 1))
661 if (' ' == *(c
+ 1) || ' ' == *(c
- 1))
663 /* Skip double invocations. */
664 if ('-' == *(c
+ 1) || '-' == *(c
- 1))
667 if ('\\' == *(c
- 1))
674 * Find out whether a line is a macro line or not. If it is, adjust the
675 * current position and return one; if it isn't, return zero and don't
676 * change the current position.
679 mandoc_getcontrol(const char *cp
, int *ppos
)
685 if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
687 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
692 while (' ' == cp
[pos
] || '\t' == cp
[pos
])