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