]> git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
Make recursive parsing of roff(7) escapes actually work in the general case,
[mandoc.git] / mandoc.c
1 /* $Id: mandoc.c,v 1.64 2012/05/31 22:34:06 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2012 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
41
42 enum mandoc_esc
43 mandoc_escape(const char **end, const char **start, int *sz)
44 {
45 char c, term;
46 int i, rlim;
47 const char *cp, *rstart;
48 enum mandoc_esc gly;
49
50 cp = *end;
51 rstart = cp;
52 if (start)
53 *start = rstart;
54 i = rlim = 0;
55 gly = ESCAPE_ERROR;
56 term = '\0';
57
58 switch ((c = cp[i++])) {
59 /*
60 * First the glyphs. There are several different forms of
61 * these, but each eventually returns a substring of the glyph
62 * name.
63 */
64 case ('('):
65 gly = ESCAPE_SPECIAL;
66 rlim = 2;
67 break;
68 case ('['):
69 gly = ESCAPE_SPECIAL;
70 /*
71 * Unicode escapes are defined in groff as \[uXXXX] to
72 * \[u10FFFF], where the contained value must be a valid
73 * Unicode codepoint. Here, however, only check whether
74 * it's not a zero-width escape.
75 */
76 if ('u' == cp[i] && ']' != cp[i + 1])
77 gly = ESCAPE_UNICODE;
78 term = ']';
79 break;
80 case ('C'):
81 if ('\'' != cp[i])
82 return(ESCAPE_ERROR);
83 gly = ESCAPE_SPECIAL;
84 term = '\'';
85 break;
86
87 /*
88 * The \z escape is supposed to output the following
89 * character without advancing the cursor position.
90 * Since we are mostly dealing with terminal mode,
91 * let us just skip the next character.
92 */
93 case ('z'):
94 (*end)++;
95 return(ESCAPE_SKIPCHAR);
96
97 /*
98 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
99 * 'X' is the trigger. These have opaque sub-strings.
100 */
101 case ('F'):
102 /* FALLTHROUGH */
103 case ('g'):
104 /* FALLTHROUGH */
105 case ('k'):
106 /* FALLTHROUGH */
107 case ('M'):
108 /* FALLTHROUGH */
109 case ('m'):
110 /* FALLTHROUGH */
111 case ('n'):
112 /* FALLTHROUGH */
113 case ('V'):
114 /* FALLTHROUGH */
115 case ('Y'):
116 gly = ESCAPE_IGNORE;
117 /* FALLTHROUGH */
118 case ('f'):
119 if (ESCAPE_ERROR == gly)
120 gly = ESCAPE_FONT;
121
122 rstart= &cp[i];
123 if (start)
124 *start = rstart;
125
126 switch (cp[i++]) {
127 case ('('):
128 rlim = 2;
129 break;
130 case ('['):
131 term = ']';
132 break;
133 default:
134 rlim = 1;
135 i--;
136 break;
137 }
138 break;
139
140 /*
141 * These escapes are of the form \X'Y', where 'X' is the trigger
142 * and 'Y' is any string. These have opaque sub-strings.
143 */
144 case ('A'):
145 /* FALLTHROUGH */
146 case ('b'):
147 /* FALLTHROUGH */
148 case ('D'):
149 /* FALLTHROUGH */
150 case ('o'):
151 /* FALLTHROUGH */
152 case ('R'):
153 /* FALLTHROUGH */
154 case ('X'):
155 /* FALLTHROUGH */
156 case ('Z'):
157 if ('\'' != cp[i++])
158 return(ESCAPE_ERROR);
159 gly = ESCAPE_IGNORE;
160 term = '\'';
161 break;
162
163 /*
164 * These escapes are of the form \X'N', where 'X' is the trigger
165 * and 'N' resolves to a numerical expression.
166 */
167 case ('B'):
168 /* FALLTHROUGH */
169 case ('h'):
170 /* FALLTHROUGH */
171 case ('H'):
172 /* FALLTHROUGH */
173 case ('L'):
174 /* FALLTHROUGH */
175 case ('l'):
176 gly = ESCAPE_NUMBERED;
177 /* FALLTHROUGH */
178 case ('S'):
179 /* FALLTHROUGH */
180 case ('v'):
181 /* FALLTHROUGH */
182 case ('w'):
183 /* FALLTHROUGH */
184 case ('x'):
185 if (ESCAPE_ERROR == gly)
186 gly = ESCAPE_IGNORE;
187 if ('\'' != cp[i++])
188 return(ESCAPE_ERROR);
189 term = '\'';
190 break;
191
192 /*
193 * Special handling for the numbered character escape.
194 * XXX Do any other escapes need similar handling?
195 */
196 case ('N'):
197 if ('\0' == cp[i])
198 return(ESCAPE_ERROR);
199 *end = &cp[++i];
200 if (isdigit((unsigned char)cp[i-1]))
201 return(ESCAPE_IGNORE);
202 while (isdigit((unsigned char)**end))
203 (*end)++;
204 if (start)
205 *start = &cp[i];
206 if (sz)
207 *sz = *end - &cp[i];
208 if ('\0' != **end)
209 (*end)++;
210 return(ESCAPE_NUMBERED);
211
212 /*
213 * Sizes get a special category of their own.
214 */
215 case ('s'):
216 gly = ESCAPE_IGNORE;
217
218 rstart = &cp[i];
219 if (start)
220 *start = rstart;
221
222 /* See +/- counts as a sign. */
223 c = cp[i];
224 if ('+' == c || '-' == c || ASCII_HYPH == c)
225 ++i;
226
227 switch (cp[i++]) {
228 case ('('):
229 rlim = 2;
230 break;
231 case ('['):
232 term = ']';
233 break;
234 case ('\''):
235 term = '\'';
236 break;
237 default:
238 rlim = 1;
239 i--;
240 break;
241 }
242
243 /* See +/- counts as a sign. */
244 c = cp[i];
245 if ('+' == c || '-' == c || ASCII_HYPH == c)
246 ++i;
247
248 break;
249
250 /*
251 * Anything else is assumed to be a glyph.
252 */
253 default:
254 gly = ESCAPE_SPECIAL;
255 rlim = 1;
256 i--;
257 break;
258 }
259
260 assert(ESCAPE_ERROR != gly);
261
262 *end = rstart = &cp[i];
263 if (start)
264 *start = rstart;
265
266 /*
267 * Read up to the terminating character,
268 * paying attention to nested escapes.
269 */
270
271 if ('\0' != term) {
272 while (**end != term) {
273 switch (**end) {
274 case ('\0'):
275 return(ESCAPE_ERROR);
276 case ('\\'):
277 (*end)++;
278 if (ESCAPE_ERROR ==
279 mandoc_escape(end, NULL, NULL))
280 return(ESCAPE_ERROR);
281 break;
282 default:
283 (*end)++;
284 break;
285 }
286 }
287 rlim = (*end)++ - rstart;
288 } else {
289 assert(rlim > 0);
290 if ((size_t)rlim > strlen(rstart))
291 return(ESCAPE_ERROR);
292 *end += rlim;
293 }
294 if (sz)
295 *sz = rlim;
296
297 /* Run post-processors. */
298
299 switch (gly) {
300 case (ESCAPE_FONT):
301 /*
302 * Pretend that the constant-width font modes are the
303 * same as the regular font modes.
304 */
305 if (2 == rlim && 'C' == *rstart)
306 rstart++;
307 else if (1 != rlim)
308 break;
309
310 switch (*rstart) {
311 case ('3'):
312 /* FALLTHROUGH */
313 case ('B'):
314 gly = ESCAPE_FONTBOLD;
315 break;
316 case ('2'):
317 /* FALLTHROUGH */
318 case ('I'):
319 gly = ESCAPE_FONTITALIC;
320 break;
321 case ('P'):
322 gly = ESCAPE_FONTPREV;
323 break;
324 case ('1'):
325 /* FALLTHROUGH */
326 case ('R'):
327 gly = ESCAPE_FONTROMAN;
328 break;
329 }
330 break;
331 case (ESCAPE_SPECIAL):
332 if (1 != rlim)
333 break;
334 if ('c' == *rstart)
335 gly = ESCAPE_NOSPACE;
336 break;
337 default:
338 break;
339 }
340
341 return(gly);
342 }
343
344 void *
345 mandoc_calloc(size_t num, size_t size)
346 {
347 void *ptr;
348
349 ptr = calloc(num, size);
350 if (NULL == ptr) {
351 perror(NULL);
352 exit((int)MANDOCLEVEL_SYSERR);
353 }
354
355 return(ptr);
356 }
357
358
359 void *
360 mandoc_malloc(size_t size)
361 {
362 void *ptr;
363
364 ptr = malloc(size);
365 if (NULL == ptr) {
366 perror(NULL);
367 exit((int)MANDOCLEVEL_SYSERR);
368 }
369
370 return(ptr);
371 }
372
373
374 void *
375 mandoc_realloc(void *ptr, size_t size)
376 {
377
378 ptr = realloc(ptr, size);
379 if (NULL == ptr) {
380 perror(NULL);
381 exit((int)MANDOCLEVEL_SYSERR);
382 }
383
384 return(ptr);
385 }
386
387 char *
388 mandoc_strndup(const char *ptr, size_t sz)
389 {
390 char *p;
391
392 p = mandoc_malloc(sz + 1);
393 memcpy(p, ptr, sz);
394 p[(int)sz] = '\0';
395 return(p);
396 }
397
398 char *
399 mandoc_strdup(const char *ptr)
400 {
401 char *p;
402
403 p = strdup(ptr);
404 if (NULL == p) {
405 perror(NULL);
406 exit((int)MANDOCLEVEL_SYSERR);
407 }
408
409 return(p);
410 }
411
412 /*
413 * Parse a quoted or unquoted roff-style request or macro argument.
414 * Return a pointer to the parsed argument, which is either the original
415 * pointer or advanced by one byte in case the argument is quoted.
416 * Null-terminate the argument in place.
417 * Collapse pairs of quotes inside quoted arguments.
418 * Advance the argument pointer to the next argument,
419 * or to the null byte terminating the argument line.
420 */
421 char *
422 mandoc_getarg(struct mparse *parse, char **cpp, int ln, int *pos)
423 {
424 char *start, *cp;
425 int quoted, pairs, white;
426
427 /* Quoting can only start with a new word. */
428 start = *cpp;
429 quoted = 0;
430 if ('"' == *start) {
431 quoted = 1;
432 start++;
433 }
434
435 pairs = 0;
436 white = 0;
437 for (cp = start; '\0' != *cp; cp++) {
438 /* Move left after quoted quotes and escaped backslashes. */
439 if (pairs)
440 cp[-pairs] = cp[0];
441 if ('\\' == cp[0]) {
442 if ('\\' == cp[1]) {
443 /* Poor man's copy mode. */
444 pairs++;
445 cp++;
446 } else if (0 == quoted && ' ' == cp[1])
447 /* Skip escaped blanks. */
448 cp++;
449 } else if (0 == quoted) {
450 if (' ' == cp[0]) {
451 /* Unescaped blanks end unquoted args. */
452 white = 1;
453 break;
454 }
455 } else if ('"' == cp[0]) {
456 if ('"' == cp[1]) {
457 /* Quoted quotes collapse. */
458 pairs++;
459 cp++;
460 } else {
461 /* Unquoted quotes end quoted args. */
462 quoted = 2;
463 break;
464 }
465 }
466 }
467
468 /* Quoted argument without a closing quote. */
469 if (1 == quoted)
470 mandoc_msg(MANDOCERR_BADQUOTE, parse, ln, *pos, NULL);
471
472 /* Null-terminate this argument and move to the next one. */
473 if (pairs)
474 cp[-pairs] = '\0';
475 if ('\0' != *cp) {
476 *cp++ = '\0';
477 while (' ' == *cp)
478 cp++;
479 }
480 *pos += (int)(cp - start) + (quoted ? 1 : 0);
481 *cpp = cp;
482
483 if ('\0' == *cp && (white || ' ' == cp[-1]))
484 mandoc_msg(MANDOCERR_EOLNSPACE, parse, ln, *pos, NULL);
485
486 return(start);
487 }
488
489 static int
490 a2time(time_t *t, const char *fmt, const char *p)
491 {
492 struct tm tm;
493 char *pp;
494
495 memset(&tm, 0, sizeof(struct tm));
496
497 pp = NULL;
498 #ifdef HAVE_STRPTIME
499 pp = strptime(p, fmt, &tm);
500 #endif
501 if (NULL != pp && '\0' == *pp) {
502 *t = mktime(&tm);
503 return(1);
504 }
505
506 return(0);
507 }
508
509 static char *
510 time2a(time_t t)
511 {
512 struct tm *tm;
513 char *buf, *p;
514 size_t ssz;
515 int isz;
516
517 tm = localtime(&t);
518
519 /*
520 * Reserve space:
521 * up to 9 characters for the month (September) + blank
522 * up to 2 characters for the day + comma + blank
523 * 4 characters for the year and a terminating '\0'
524 */
525 p = buf = mandoc_malloc(10 + 4 + 4 + 1);
526
527 if (0 == (ssz = strftime(p, 10 + 1, "%B ", tm)))
528 goto fail;
529 p += (int)ssz;
530
531 if (-1 == (isz = snprintf(p, 4 + 1, "%d, ", tm->tm_mday)))
532 goto fail;
533 p += isz;
534
535 if (0 == strftime(p, 4 + 1, "%Y", tm))
536 goto fail;
537 return(buf);
538
539 fail:
540 free(buf);
541 return(NULL);
542 }
543
544 char *
545 mandoc_normdate(struct mparse *parse, char *in, int ln, int pos)
546 {
547 char *out;
548 time_t t;
549
550 if (NULL == in || '\0' == *in ||
551 0 == strcmp(in, "$" "Mdocdate$")) {
552 mandoc_msg(MANDOCERR_NODATE, parse, ln, pos, NULL);
553 time(&t);
554 }
555 else if (a2time(&t, "%Y-%m-%d", in))
556 t = 0;
557 else if (!a2time(&t, "$" "Mdocdate: %b %d %Y $", in) &&
558 !a2time(&t, "%b %d, %Y", in)) {
559 mandoc_msg(MANDOCERR_BADDATE, parse, ln, pos, NULL);
560 t = 0;
561 }
562 out = t ? time2a(t) : NULL;
563 return(out ? out : mandoc_strdup(in));
564 }
565
566 int
567 mandoc_eos(const char *p, size_t sz, int enclosed)
568 {
569 const char *q;
570 int found;
571
572 if (0 == sz)
573 return(0);
574
575 /*
576 * End-of-sentence recognition must include situations where
577 * some symbols, such as `)', allow prior EOS punctuation to
578 * propagate outward.
579 */
580
581 found = 0;
582 for (q = p + (int)sz - 1; q >= p; q--) {
583 switch (*q) {
584 case ('\"'):
585 /* FALLTHROUGH */
586 case ('\''):
587 /* FALLTHROUGH */
588 case (']'):
589 /* FALLTHROUGH */
590 case (')'):
591 if (0 == found)
592 enclosed = 1;
593 break;
594 case ('.'):
595 /* FALLTHROUGH */
596 case ('!'):
597 /* FALLTHROUGH */
598 case ('?'):
599 found = 1;
600 break;
601 default:
602 return(found && (!enclosed || isalnum((unsigned char)*q)));
603 }
604 }
605
606 return(found && !enclosed);
607 }
608
609 /*
610 * Find out whether a line is a macro line or not. If it is, adjust the
611 * current position and return one; if it isn't, return zero and don't
612 * change the current position.
613 */
614 int
615 mandoc_getcontrol(const char *cp, int *ppos)
616 {
617 int pos;
618
619 pos = *ppos;
620
621 if ('\\' == cp[pos] && '.' == cp[pos + 1])
622 pos += 2;
623 else if ('.' == cp[pos] || '\'' == cp[pos])
624 pos++;
625 else
626 return(0);
627
628 while (' ' == cp[pos] || '\t' == cp[pos])
629 pos++;
630
631 *ppos = pos;
632 return(1);
633 }
634
635 /*
636 * Convert a string to a long that may not be <0.
637 * If the string is invalid, or is less than 0, return -1.
638 */
639 int
640 mandoc_strntoi(const char *p, size_t sz, int base)
641 {
642 char buf[32];
643 char *ep;
644 long v;
645
646 if (sz > 31)
647 return(-1);
648
649 memcpy(buf, p, sz);
650 buf[(int)sz] = '\0';
651
652 errno = 0;
653 v = strtol(buf, &ep, base);
654
655 if (buf[0] == '\0' || *ep != '\0')
656 return(-1);
657
658 if (v > INT_MAX)
659 v = INT_MAX;
660 if (v < INT_MIN)
661 v = INT_MIN;
662
663 return((int)v);
664 }