]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
1 /* $Id: mandoc.c,v 1.35 2010/09/04 20:18:53 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
31 #include "libmandoc.h"
33 static int a2time(time_t *, const char *, const char *);
37 mandoc_special(char *p
)
97 if ('+' == *p
|| '-' == *p
) {
121 if (ASCII_HYPH
== *p
)
123 if ('+' == *p
|| '-' == *p
) {
129 /* Handle embedded numerical subexp or escape. */
132 while (*p
&& ')' != *p
)
134 i
= mandoc_special(--p
);
144 } else if ('\\' == *p
) {
145 if (0 == (i
= mandoc_special(p
)))
194 if (0 == (i
= mandoc_special(p
)))
197 return(*p
? (int)(p
- sv
) : 0);
215 for ( ; *p
&& term
!= *p
; p
++)
216 if (ASCII_HYPH
== *p
)
218 return(*p
? (int)(p
- sv
) : 0);
221 for (i
= 0; *p
&& i
< len
; i
++, p
++)
222 if (ASCII_HYPH
== *p
)
224 return(i
== len
? (int)(p
- sv
) : 0);
229 mandoc_calloc(size_t num
, size_t size
)
233 ptr
= calloc(num
, size
);
236 exit((int)MANDOCLEVEL_SYSERR
);
244 mandoc_malloc(size_t size
)
251 exit((int)MANDOCLEVEL_SYSERR
);
259 mandoc_realloc(void *ptr
, size_t size
)
262 ptr
= realloc(ptr
, size
);
265 exit((int)MANDOCLEVEL_SYSERR
);
273 mandoc_strdup(const char *ptr
)
280 exit((int)MANDOCLEVEL_SYSERR
);
288 a2time(time_t *t
, const char *fmt
, const char *p
)
293 memset(&tm
, 0, sizeof(struct tm
));
295 pp
= strptime(p
, fmt
, &tm
);
296 if (NULL
!= pp
&& '\0' == *pp
) {
306 * Convert from a manual date string (see mdoc(7) and man(7)) into a
307 * date according to the stipulated date type.
310 mandoc_a2time(int flags
, const char *p
)
314 if (MTIME_MDOCDATE
& flags
) {
315 if (0 == strcmp(p
, "$" "Mdocdate$"))
317 if (a2time(&t
, "$" "Mdocdate: %b %d %Y $", p
))
321 if (MTIME_CANONICAL
& flags
|| MTIME_REDUCED
& flags
)
322 if (a2time(&t
, "%b %d, %Y", p
))
325 if (MTIME_ISO_8601
& flags
)
326 if (a2time(&t
, "%Y-%m-%d", p
))
329 if (MTIME_REDUCED
& flags
) {
330 if (a2time(&t
, "%d, %Y", p
))
332 if (a2time(&t
, "%Y", p
))
341 mandoc_eos(const char *p
, size_t sz
, int enclosed
)
350 * End-of-sentence recognition must include situations where
351 * some symbols, such as `)', allow prior EOS punctuation to
356 for (q
= p
+ (int)sz
- 1; q
>= p
; q
--) {
376 return(found
&& (!enclosed
|| isalnum((unsigned char)*q
)));
380 return(found
&& !enclosed
);
385 mandoc_hyph(const char *start
, const char *c
)
389 * Choose whether to break at a hyphenated character. We only
390 * do this if it's free-standing within a word.
393 /* Skip first/last character of buffer. */
394 if (c
== start
|| '\0' == *(c
+ 1))
396 /* Skip first/last character of word. */
397 if ('\t' == *(c
+ 1) || '\t' == *(c
- 1))
399 if (' ' == *(c
+ 1) || ' ' == *(c
- 1))
401 /* Skip double invocations. */
402 if ('-' == *(c
+ 1) || '-' == *(c
- 1))
405 if ('\\' == *(c
- 1))