]> git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
Clean-ups in -T[x]html: inline print_num(), as it was just a single
[mandoc.git] / mandoc.c
1 /* $Id: mandoc.c,v 1.52 2011/05/15 15:30:33 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
5 *
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.
9 *
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.
17 */
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include <sys/types.h>
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <errno.h>
27 #include <limits.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <time.h>
32
33 #include "mandoc.h"
34 #include "libmandoc.h"
35
36 #define DATESIZE 32
37
38 static int a2time(time_t *, const char *, const char *);
39 static char *time2a(time_t);
40 static int numescape(const char *);
41
42 /*
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).
49 */
50 static int
51 numescape(const char *start)
52 {
53 int i;
54 size_t sz;
55 const char *cp;
56
57 i = 0;
58
59 /* The expression consists of a subexpression. */
60
61 if ('\\' == start[i]) {
62 cp = &start[++i];
63 /*
64 * Read past the end of the subexpression.
65 * Bail immediately on errors.
66 */
67 if (ESCAPE_ERROR == mandoc_escape(&cp, NULL, NULL))
68 return(-1);
69 return(i + cp - &start[i]);
70 }
71
72 if ('(' != start[i++])
73 return(0);
74
75 /*
76 * A parenthesised subexpression. Read until the closing
77 * parenthesis, making sure to handle any nested subexpressions
78 * that might ruin our parse.
79 */
80
81 while (')' != start[i]) {
82 sz = strcspn(&start[i], ")\\");
83 i += (int)sz;
84
85 if ('\0' == start[i])
86 return(-1);
87 else if ('\\' != start[i])
88 continue;
89
90 cp = &start[++i];
91 if (ESCAPE_ERROR == mandoc_escape(&cp, NULL, NULL))
92 return(-1);
93 i += cp - &start[i];
94 }
95
96 /* Read past the terminating ')'. */
97 return(++i);
98 }
99
100 enum mandoc_esc
101 mandoc_escape(const char **end, const char **start, int *sz)
102 {
103 char c, term, numeric;
104 int i, lim, ssz, rlim;
105 const char *cp, *rstart;
106 enum mandoc_esc gly;
107
108 cp = *end;
109 rstart = cp;
110 if (start)
111 *start = rstart;
112 i = lim = 0;
113 gly = ESCAPE_ERROR;
114 term = numeric = '\0';
115
116 switch ((c = cp[i++])) {
117 /*
118 * First the glyphs. There are several different forms of
119 * these, but each eventually returns a substring of the glyph
120 * name.
121 */
122 case ('('):
123 gly = ESCAPE_SPECIAL;
124 lim = 2;
125 break;
126 case ('['):
127 gly = ESCAPE_SPECIAL;
128 /*
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.
133 */
134 if ('u' == cp[i] && ']' != cp[i + 1])
135 gly = ESCAPE_UNICODE;
136 term = ']';
137 break;
138 case ('C'):
139 if ('\'' != cp[i])
140 return(ESCAPE_ERROR);
141 gly = ESCAPE_SPECIAL;
142 term = '\'';
143 break;
144
145 /*
146 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
147 * 'X' is the trigger. These have opaque sub-strings.
148 */
149 case ('F'):
150 /* FALLTHROUGH */
151 case ('g'):
152 /* FALLTHROUGH */
153 case ('k'):
154 /* FALLTHROUGH */
155 case ('M'):
156 /* FALLTHROUGH */
157 case ('m'):
158 /* FALLTHROUGH */
159 case ('n'):
160 /* FALLTHROUGH */
161 case ('V'):
162 /* FALLTHROUGH */
163 case ('Y'):
164 if (ESCAPE_ERROR == gly)
165 gly = ESCAPE_IGNORE;
166 /* FALLTHROUGH */
167 case ('*'):
168 if (ESCAPE_ERROR == gly)
169 gly = ESCAPE_PREDEF;
170 /* FALLTHROUGH */
171 case ('f'):
172 if (ESCAPE_ERROR == gly)
173 gly = ESCAPE_FONT;
174
175 rstart= &cp[i];
176 if (start)
177 *start = rstart;
178
179 switch (cp[i++]) {
180 case ('('):
181 lim = 2;
182 break;
183 case ('['):
184 term = ']';
185 break;
186 default:
187 lim = 1;
188 i--;
189 break;
190 }
191 break;
192
193 /*
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.
196 */
197 case ('A'):
198 /* FALLTHROUGH */
199 case ('b'):
200 /* FALLTHROUGH */
201 case ('D'):
202 /* FALLTHROUGH */
203 case ('o'):
204 /* FALLTHROUGH */
205 case ('R'):
206 /* FALLTHROUGH */
207 case ('X'):
208 /* FALLTHROUGH */
209 case ('Z'):
210 if ('\'' != cp[i++])
211 return(ESCAPE_ERROR);
212 gly = ESCAPE_IGNORE;
213 term = '\'';
214 break;
215
216 /*
217 * These escapes are of the form \X'N', where 'X' is the trigger
218 * and 'N' resolves to a numerical expression.
219 */
220 case ('B'):
221 /* FALLTHROUGH */
222 case ('h'):
223 /* FALLTHROUGH */
224 case ('H'):
225 /* FALLTHROUGH */
226 case ('L'):
227 /* FALLTHROUGH */
228 case ('l'):
229 /* FALLTHROUGH */
230 case ('N'):
231 if (ESCAPE_ERROR == gly)
232 gly = ESCAPE_NUMBERED;
233 /* FALLTHROUGH */
234 case ('S'):
235 /* FALLTHROUGH */
236 case ('v'):
237 /* FALLTHROUGH */
238 case ('w'):
239 /* FALLTHROUGH */
240 case ('x'):
241 if (ESCAPE_ERROR == gly)
242 gly = ESCAPE_IGNORE;
243 if ('\'' != cp[i++])
244 return(ESCAPE_ERROR);
245 term = numeric = '\'';
246 break;
247
248 /*
249 * Sizes get a special category of their own.
250 */
251 case ('s'):
252 gly = ESCAPE_IGNORE;
253
254 rstart = &cp[i];
255 if (start)
256 *start = rstart;
257
258 /* See +/- counts as a sign. */
259 c = cp[i];
260 if ('+' == c || '-' == c || ASCII_HYPH == c)
261 ++i;
262
263 switch (cp[i++]) {
264 case ('('):
265 lim = 2;
266 break;
267 case ('['):
268 term = numeric = ']';
269 break;
270 case ('\''):
271 term = numeric = '\'';
272 break;
273 default:
274 lim = 1;
275 i--;
276 break;
277 }
278
279 /* See +/- counts as a sign. */
280 c = cp[i];
281 if ('+' == c || '-' == c || ASCII_HYPH == c)
282 ++i;
283
284 break;
285
286 /*
287 * Anything else is assumed to be a glyph.
288 */
289 default:
290 gly = ESCAPE_SPECIAL;
291 lim = 1;
292 i--;
293 break;
294 }
295
296 assert(ESCAPE_ERROR != gly);
297
298 rstart = &cp[i];
299 if (start)
300 *start = rstart;
301
302 /*
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.
308 */
309
310 ssz = 0;
311 if (numeric && -1 == (ssz = numescape(&cp[i])))
312 return(ESCAPE_ERROR);
313
314 i += ssz;
315 rlim = -1;
316
317 /*
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.
322 */
323
324 if ('\0' != term) {
325 *end = strchr(&cp[i], term);
326 if ('\0' == *end)
327 return(ESCAPE_ERROR);
328
329 rlim = *end - &cp[i];
330 if (sz)
331 *sz = rlim;
332 (*end)++;
333 goto out;
334 }
335
336 assert(lim > 0);
337
338 /*
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.
342 */
343
344 if ((size_t)lim > strlen(&cp[i]))
345 return(ESCAPE_ERROR);
346
347 rlim = lim;
348 if (sz)
349 *sz = rlim;
350
351 *end = &cp[i] + lim;
352
353 out:
354 assert(rlim >= 0 && rstart);
355
356 /* Run post-processors. */
357
358 switch (gly) {
359 case (ESCAPE_FONT):
360 if (1 != rlim)
361 break;
362 switch (*rstart) {
363 case ('3'):
364 /* FALLTHROUGH */
365 case ('B'):
366 gly = ESCAPE_FONTBOLD;
367 break;
368 case ('2'):
369 /* FALLTHROUGH */
370 case ('I'):
371 gly = ESCAPE_FONTITALIC;
372 break;
373 case ('P'):
374 gly = ESCAPE_FONTPREV;
375 break;
376 case ('1'):
377 /* FALLTHROUGH */
378 case ('R'):
379 gly = ESCAPE_FONTROMAN;
380 break;
381 }
382 break;
383 case (ESCAPE_SPECIAL):
384 if (1 != rlim)
385 break;
386 if ('c' == *rstart)
387 gly = ESCAPE_NOSPACE;
388 break;
389 default:
390 break;
391 }
392
393 return(gly);
394 }
395
396 void *
397 mandoc_calloc(size_t num, size_t size)
398 {
399 void *ptr;
400
401 ptr = calloc(num, size);
402 if (NULL == ptr) {
403 perror(NULL);
404 exit((int)MANDOCLEVEL_SYSERR);
405 }
406
407 return(ptr);
408 }
409
410
411 void *
412 mandoc_malloc(size_t size)
413 {
414 void *ptr;
415
416 ptr = malloc(size);
417 if (NULL == ptr) {
418 perror(NULL);
419 exit((int)MANDOCLEVEL_SYSERR);
420 }
421
422 return(ptr);
423 }
424
425
426 void *
427 mandoc_realloc(void *ptr, size_t size)
428 {
429
430 ptr = realloc(ptr, size);
431 if (NULL == ptr) {
432 perror(NULL);
433 exit((int)MANDOCLEVEL_SYSERR);
434 }
435
436 return(ptr);
437 }
438
439
440 char *
441 mandoc_strdup(const char *ptr)
442 {
443 char *p;
444
445 p = strdup(ptr);
446 if (NULL == p) {
447 perror(NULL);
448 exit((int)MANDOCLEVEL_SYSERR);
449 }
450
451 return(p);
452 }
453
454 /*
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.
462 */
463 char *
464 mandoc_getarg(struct mparse *parse, char **cpp, int ln, int *pos)
465 {
466 char *start, *cp;
467 int quoted, pairs, white;
468
469 /* Quoting can only start with a new word. */
470 start = *cpp;
471 quoted = 0;
472 if ('"' == *start) {
473 quoted = 1;
474 start++;
475 }
476
477 pairs = 0;
478 white = 0;
479 for (cp = start; '\0' != *cp; cp++) {
480 /* Move left after quoted quotes and escaped backslashes. */
481 if (pairs)
482 cp[-pairs] = cp[0];
483 if ('\\' == cp[0]) {
484 if ('\\' == cp[1]) {
485 /* Poor man's copy mode. */
486 pairs++;
487 cp++;
488 } else if (0 == quoted && ' ' == cp[1])
489 /* Skip escaped blanks. */
490 cp++;
491 } else if (0 == quoted) {
492 if (' ' == cp[0]) {
493 /* Unescaped blanks end unquoted args. */
494 white = 1;
495 break;
496 }
497 } else if ('"' == cp[0]) {
498 if ('"' == cp[1]) {
499 /* Quoted quotes collapse. */
500 pairs++;
501 cp++;
502 } else {
503 /* Unquoted quotes end quoted args. */
504 quoted = 2;
505 break;
506 }
507 }
508 }
509
510 /* Quoted argument without a closing quote. */
511 if (1 == quoted)
512 mandoc_msg(MANDOCERR_BADQUOTE, parse, ln, *pos, NULL);
513
514 /* Null-terminate this argument and move to the next one. */
515 if (pairs)
516 cp[-pairs] = '\0';
517 if ('\0' != *cp) {
518 *cp++ = '\0';
519 while (' ' == *cp)
520 cp++;
521 }
522 *pos += (int)(cp - start) + (quoted ? 1 : 0);
523 *cpp = cp;
524
525 if ('\0' == *cp && (white || ' ' == cp[-1]))
526 mandoc_msg(MANDOCERR_EOLNSPACE, parse, ln, *pos, NULL);
527
528 return(start);
529 }
530
531 static int
532 a2time(time_t *t, const char *fmt, const char *p)
533 {
534 struct tm tm;
535 char *pp;
536
537 memset(&tm, 0, sizeof(struct tm));
538
539 pp = strptime(p, fmt, &tm);
540 if (NULL != pp && '\0' == *pp) {
541 *t = mktime(&tm);
542 return(1);
543 }
544
545 return(0);
546 }
547
548 static char *
549 time2a(time_t t)
550 {
551 struct tm tm;
552 char *buf, *p;
553 size_t ssz;
554 int isz;
555
556 localtime_r(&t, &tm);
557
558 /*
559 * Reserve space:
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'
563 */
564 p = buf = mandoc_malloc(10 + 4 + 4 + 1);
565
566 if (0 == (ssz = strftime(p, 10 + 1, "%B ", &tm)))
567 goto fail;
568 p += (int)ssz;
569
570 if (-1 == (isz = snprintf(p, 4 + 1, "%d, ", tm.tm_mday)))
571 goto fail;
572 p += isz;
573
574 if (0 == strftime(p, 4 + 1, "%Y", &tm))
575 goto fail;
576 return(buf);
577
578 fail:
579 free(buf);
580 return(NULL);
581 }
582
583 char *
584 mandoc_normdate(struct mparse *parse, char *in, int ln, int pos)
585 {
586 char *out;
587 time_t t;
588
589 if (NULL == in || '\0' == *in ||
590 0 == strcmp(in, "$" "Mdocdate$")) {
591 mandoc_msg(MANDOCERR_NODATE, parse, ln, pos, NULL);
592 time(&t);
593 }
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);
598 t = 0;
599 }
600 out = t ? time2a(t) : NULL;
601 return(out ? out : mandoc_strdup(in));
602 }
603
604 int
605 mandoc_eos(const char *p, size_t sz, int enclosed)
606 {
607 const char *q;
608 int found;
609
610 if (0 == sz)
611 return(0);
612
613 /*
614 * End-of-sentence recognition must include situations where
615 * some symbols, such as `)', allow prior EOS punctuation to
616 * propagate outward.
617 */
618
619 found = 0;
620 for (q = p + (int)sz - 1; q >= p; q--) {
621 switch (*q) {
622 case ('\"'):
623 /* FALLTHROUGH */
624 case ('\''):
625 /* FALLTHROUGH */
626 case (']'):
627 /* FALLTHROUGH */
628 case (')'):
629 if (0 == found)
630 enclosed = 1;
631 break;
632 case ('.'):
633 /* FALLTHROUGH */
634 case ('!'):
635 /* FALLTHROUGH */
636 case ('?'):
637 found = 1;
638 break;
639 default:
640 return(found && (!enclosed || isalnum((unsigned char)*q)));
641 }
642 }
643
644 return(found && !enclosed);
645 }
646
647 int
648 mandoc_hyph(const char *start, const char *c)
649 {
650
651 /*
652 * Choose whether to break at a hyphenated character. We only
653 * do this if it's free-standing within a word.
654 */
655
656 /* Skip first/last character of buffer. */
657 if (c == start || '\0' == *(c + 1))
658 return(0);
659 /* Skip first/last character of word. */
660 if ('\t' == *(c + 1) || '\t' == *(c - 1))
661 return(0);
662 if (' ' == *(c + 1) || ' ' == *(c - 1))
663 return(0);
664 /* Skip double invocations. */
665 if ('-' == *(c + 1) || '-' == *(c - 1))
666 return(0);
667 /* Skip escapes. */
668 if ('\\' == *(c - 1))
669 return(0);
670
671 return(1);
672 }
673
674 /*
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.
678 */
679 int
680 mandoc_getcontrol(const char *cp, int *ppos)
681 {
682 int pos;
683
684 pos = *ppos;
685
686 if ('\\' == cp[pos] && '.' == cp[pos + 1])
687 pos += 2;
688 else if ('.' == cp[pos] || '\'' == cp[pos])
689 pos++;
690 else
691 return(0);
692
693 while (' ' == cp[pos] || '\t' == cp[pos])
694 pos++;
695
696 *ppos = pos;
697 return(1);
698 }
699
700 /*
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.
703 */
704 int
705 mandoc_strntou(const char *p, size_t sz, int base)
706 {
707 char buf[32];
708 char *ep;
709 long v;
710
711 if (sz > 31)
712 return(-1);
713
714 memcpy(buf, p, sz);
715 buf[(int)sz] = '\0';
716
717 errno = 0;
718 v = strtol(buf, &ep, base);
719
720 if (buf[0] == '\0' || *ep != '\0')
721 return(-1);
722
723 if ((errno == ERANGE &&
724 (v == LONG_MAX || v == LONG_MIN)) ||
725 (v > INT_MAX || v < 0))
726 return(-1);
727
728 return((int)v);
729 }
730