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