Annotation of mandoc/man.c, Revision 1.158
1.158 ! schwarze 1: /* $Id: man.c,v 1.157 2015/04/18 17:53:21 schwarze Exp $ */
1.1 kristaps 2: /*
1.102 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.149 schwarze 4: * Copyright (c) 2013, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
1.124 schwarze 5: * Copyright (c) 2011 Joerg Sonnenberger <joerg@netbsd.org>
1.1 kristaps 6: *
7: * Permission to use, copy, modify, and distribute this software for any
1.18 kristaps 8: * purpose with or without fee is hereby granted, provided that the above
9: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 10: *
1.150 schwarze 11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.18 kristaps 12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.150 schwarze 13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.18 kristaps 14: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 18: */
1.47 kristaps 19: #include "config.h"
20:
1.41 kristaps 21: #include <sys/types.h>
22:
1.1 kristaps 23: #include <assert.h>
1.126 schwarze 24: #include <ctype.h>
1.1 kristaps 25: #include <stdarg.h>
26: #include <stdlib.h>
27: #include <stdio.h>
28: #include <string.h>
29:
1.150 schwarze 30: #include "mandoc_aux.h"
31: #include "mandoc.h"
32: #include "roff.h"
1.105 kristaps 33: #include "man.h"
1.150 schwarze 34: #include "libmandoc.h"
1.158 ! schwarze 35: #include "roff_int.h"
1.1 kristaps 36: #include "libman.h"
37:
1.129 schwarze 38: const char *const __man_macronames[MAN_MAX] = {
1.21 kristaps 39: "br", "TH", "SH", "SS",
1.129 schwarze 40: "TP", "LP", "PP", "P",
1.1 kristaps 41: "IP", "HP", "SM", "SB",
42: "BI", "IB", "BR", "RB",
1.11 kristaps 43: "R", "B", "I", "IR",
1.148 schwarze 44: "RI", "sp", "nf",
1.92 kristaps 45: "fi", "RE", "RS", "DT",
46: "UC", "PD", "AT", "in",
1.120 schwarze 47: "ft", "OP", "EX", "EE",
1.128 schwarze 48: "UR", "UE", "ll"
1.1 kristaps 49: };
50:
51: const char * const *man_macronames = __man_macronames;
52:
1.153 schwarze 53: static void man_breakscope(struct roff_man *, int);
54: static void man_descope(struct roff_man *, int, int);
55: static int man_ptext(struct roff_man *, int, char *, int);
56: static int man_pmacro(struct roff_man *, int, char *, int);
1.1 kristaps 57:
58:
1.45 kristaps 59: void
1.153 schwarze 60: man_endparse(struct roff_man *man)
1.1 kristaps 61: {
62:
1.144 schwarze 63: man_macroend(man);
1.1 kristaps 64: }
65:
66: int
1.153 schwarze 67: man_parseln(struct roff_man *man, int ln, char *buf, int offs)
1.1 kristaps 68: {
69:
1.150 schwarze 70: if (man->last->type != ROFFT_EQN || ln > man->last->line)
1.141 schwarze 71: man->flags |= MAN_NEWLINE;
1.97 kristaps 72:
1.119 schwarze 73: return (roff_getcontrol(man->roff, buf, &offs) ?
1.129 schwarze 74: man_pmacro(man, ln, buf, offs) :
75: man_ptext(man, ln, buf, offs));
1.1 kristaps 76: }
1.2 kristaps 77:
1.144 schwarze 78: void
1.153 schwarze 79: man_elem_alloc(struct roff_man *man, int line, int pos, int tok)
1.1 kristaps 80: {
1.151 schwarze 81: struct roff_node *p;
1.1 kristaps 82:
1.158 ! schwarze 83: p = roff_node_alloc(man, line, pos, ROFFT_ELEM, tok);
! 84: roff_node_append(man, p);
1.153 schwarze 85: man->next = ROFF_NEXT_CHILD;
1.29 kristaps 86: }
87:
1.144 schwarze 88: void
1.153 schwarze 89: man_block_alloc(struct roff_man *man, int line, int pos, int tok)
1.29 kristaps 90: {
1.151 schwarze 91: struct roff_node *p;
1.29 kristaps 92:
1.158 ! schwarze 93: p = roff_node_alloc(man, line, pos, ROFFT_BLOCK, tok);
! 94: roff_node_append(man, p);
1.153 schwarze 95: man->next = ROFF_NEXT_CHILD;
1.29 kristaps 96: }
97:
1.144 schwarze 98: void
1.153 schwarze 99: man_word_alloc(struct roff_man *man, int line, int pos, const char *word)
1.1 kristaps 100: {
1.151 schwarze 101: struct roff_node *n;
1.1 kristaps 102:
1.158 ! schwarze 103: n = roff_node_alloc(man, line, pos, ROFFT_TEXT, MAN_MAX);
1.119 schwarze 104: n->string = roff_strdup(man->roff, word);
1.158 ! schwarze 105: roff_node_append(man, n);
! 106: man_valid_post(man);
1.153 schwarze 107: man->next = ROFF_NEXT_SIBLING;
1.142 schwarze 108: }
109:
110: void
1.153 schwarze 111: man_word_append(struct roff_man *man, const char *word)
1.142 schwarze 112: {
1.151 schwarze 113: struct roff_node *n;
1.142 schwarze 114: char *addstr, *newstr;
115:
116: n = man->last;
117: addstr = roff_strdup(man->roff, word);
118: mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
119: free(addstr);
120: free(n->string);
121: n->string = newstr;
1.153 schwarze 122: man->next = ROFF_NEXT_SIBLING;
1.1 kristaps 123: }
124:
1.145 schwarze 125: void
1.153 schwarze 126: man_addeqn(struct roff_man *man, const struct eqn *ep)
1.101 kristaps 127: {
1.151 schwarze 128: struct roff_node *n;
1.101 kristaps 129:
1.158 ! schwarze 130: n = roff_node_alloc(man, ep->ln, ep->pos, ROFFT_EQN, MAN_MAX);
1.101 kristaps 131: n->eqn = ep;
1.140 schwarze 132: if (ep->ln > man->last->line)
133: n->flags |= MAN_LINE;
1.158 ! schwarze 134: roff_node_append(man, n);
1.153 schwarze 135: man->next = ROFF_NEXT_SIBLING;
1.144 schwarze 136: man_descope(man, ep->ln, ep->pos);
1.101 kristaps 137: }
1.1 kristaps 138:
1.145 schwarze 139: void
1.153 schwarze 140: man_addspan(struct roff_man *man, const struct tbl_span *sp)
1.94 kristaps 141: {
1.151 schwarze 142: struct roff_node *n;
1.94 kristaps 143:
1.149 schwarze 144: man_breakscope(man, MAN_MAX);
1.158 ! schwarze 145: n = roff_node_alloc(man, sp->line, 0, ROFFT_TBL, MAN_MAX);
1.100 kristaps 146: n->span = sp;
1.158 ! schwarze 147: roff_node_append(man, n);
! 148: man_valid_post(man);
1.153 schwarze 149: man->next = ROFF_NEXT_SIBLING;
1.144 schwarze 150: man_descope(man, sp->line, 0);
1.94 kristaps 151: }
152:
1.144 schwarze 153: static void
1.153 schwarze 154: man_descope(struct roff_man *man, int line, int offs)
1.94 kristaps 155: {
156: /*
157: * Co-ordinate what happens with having a next-line scope open:
158: * first close out the element scope (if applicable), then close
159: * out the block scope (also if applicable).
160: */
161:
1.144 schwarze 162: if (man->flags & MAN_ELINE) {
1.119 schwarze 163: man->flags &= ~MAN_ELINE;
1.144 schwarze 164: man_unscope(man, man->last->parent);
1.94 kristaps 165: }
1.144 schwarze 166: if ( ! (man->flags & MAN_BLINE))
167: return;
1.119 schwarze 168: man->flags &= ~MAN_BLINE;
1.144 schwarze 169: man_unscope(man, man->last->parent);
1.158 ! schwarze 170: roff_body_alloc(man, line, offs, man->last->tok);
1.94 kristaps 171: }
172:
1.1 kristaps 173: static int
1.153 schwarze 174: man_ptext(struct roff_man *man, int line, char *buf, int offs)
1.1 kristaps 175: {
1.61 kristaps 176: int i;
1.60 kristaps 177:
1.35 kristaps 178: /* Literal free-form text whitespace is preserved. */
179:
1.144 schwarze 180: if (man->flags & MAN_LITERAL) {
181: man_word_alloc(man, line, offs, buf + offs);
182: man_descope(man, line, offs);
183: return(1);
1.35 kristaps 184: }
185:
1.144 schwarze 186: for (i = offs; buf[i] == ' '; i++)
1.31 kristaps 187: /* Skip leading whitespace. */ ;
1.48 kristaps 188:
1.121 schwarze 189: /*
190: * Blank lines are ignored right after headings
191: * but add a single vertical space elsewhere.
192: */
193:
1.144 schwarze 194: if (buf[i] == '\0') {
1.61 kristaps 195: /* Allocate a blank entry. */
1.144 schwarze 196: if (man->last->tok != MAN_SH &&
197: man->last->tok != MAN_SS) {
198: man_elem_alloc(man, line, offs, MAN_sp);
1.153 schwarze 199: man->next = ROFF_NEXT_SIBLING;
1.121 schwarze 200: }
1.118 schwarze 201: return(1);
1.31 kristaps 202: }
203:
1.129 schwarze 204: /*
1.63 kristaps 205: * Warn if the last un-escaped character is whitespace. Then
1.129 schwarze 206: * strip away the remaining spaces (tabs stay!).
1.63 kristaps 207: */
1.29 kristaps 208:
1.61 kristaps 209: i = (int)strlen(buf);
210: assert(i);
1.49 kristaps 211:
1.63 kristaps 212: if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
1.64 kristaps 213: if (i > 1 && '\\' != buf[i - 2])
1.131 schwarze 214: mandoc_msg(MANDOCERR_SPACE_EOL, man->parse,
215: line, i - 1, NULL);
1.63 kristaps 216:
217: for (--i; i && ' ' == buf[i]; i--)
218: /* Spin back to non-space. */ ;
219:
220: /* Jump ahead of escaped whitespace. */
221: i += '\\' == buf[i] ? 2 : 1;
222:
223: buf[i] = '\0';
224: }
1.144 schwarze 225: man_word_alloc(man, line, offs, buf + offs);
1.65 kristaps 226:
227: /*
228: * End-of-sentence check. If the last character is an unescaped
229: * EOS character, then flag the node as being the end of a
230: * sentence. The front-end will know how to interpret this.
231: */
1.67 kristaps 232:
1.65 kristaps 233: assert(i);
1.122 schwarze 234: if (mandoc_eos(buf, (size_t)i))
1.119 schwarze 235: man->last->flags |= MAN_EOS;
1.31 kristaps 236:
1.144 schwarze 237: man_descope(man, line, offs);
238: return(1);
1.1 kristaps 239: }
240:
1.96 kristaps 241: static int
1.153 schwarze 242: man_pmacro(struct roff_man *man, int ln, char *buf, int offs)
1.1 kristaps 243: {
1.151 schwarze 244: struct roff_node *n;
1.143 schwarze 245: const char *cp;
1.151 schwarze 246: int tok;
1.134 schwarze 247: int i, ppos;
248: int bline;
1.143 schwarze 249: char mac[5];
1.1 kristaps 250:
1.107 kristaps 251: ppos = offs;
1.9 kristaps 252:
1.56 kristaps 253: /*
1.107 kristaps 254: * Copy the first word into a nil-terminated buffer.
1.143 schwarze 255: * Stop when a space, tab, escape, or eoln is encountered.
1.56 kristaps 256: */
1.62 kristaps 257:
1.107 kristaps 258: i = 0;
1.143 schwarze 259: while (i < 4 && strchr(" \t\\", buf[offs]) == NULL)
1.107 kristaps 260: mac[i++] = buf[offs++];
1.10 kristaps 261:
1.107 kristaps 262: mac[i] = '\0';
1.1 kristaps 263:
1.107 kristaps 264: tok = (i > 0 && i < 4) ? man_hash_find(mac) : MAN_MAX;
1.1 kristaps 265:
1.143 schwarze 266: if (tok == MAN_MAX) {
1.136 schwarze 267: mandoc_msg(MANDOCERR_MACRO, man->parse,
268: ln, ppos, buf + ppos - 1);
1.12 kristaps 269: return(1);
1.1 kristaps 270: }
271:
1.143 schwarze 272: /* Skip a leading escape sequence or tab. */
273:
274: switch (buf[offs]) {
275: case '\\':
276: cp = buf + offs + 1;
277: mandoc_escape(&cp, NULL, NULL);
278: offs = cp - buf;
279: break;
280: case '\t':
281: offs++;
282: break;
283: default:
284: break;
285: }
286:
287: /* Jump to the next non-whitespace word. */
1.1 kristaps 288:
1.144 schwarze 289: while (buf[offs] && buf[offs] == ' ')
1.107 kristaps 290: offs++;
1.1 kristaps 291:
1.129 schwarze 292: /*
1.62 kristaps 293: * Trailing whitespace. Note that tabs are allowed to be passed
294: * into the parser as "text", so we only warn about spaces here.
295: */
1.48 kristaps 296:
1.144 schwarze 297: if (buf[offs] == '\0' && buf[offs - 1] == ' ')
1.131 schwarze 298: mandoc_msg(MANDOCERR_SPACE_EOL, man->parse,
299: ln, offs - 1, NULL);
1.48 kristaps 300:
1.129 schwarze 301: /*
1.149 schwarze 302: * Some macros break next-line scopes; otherwise, remember
303: * whether we are in next-line scope for a block head.
1.50 kristaps 304: */
1.34 kristaps 305:
1.149 schwarze 306: man_breakscope(man, tok);
1.134 schwarze 307: bline = man->flags & MAN_BLINE;
1.59 kristaps 308:
309: /* Call to handler... */
1.1 kristaps 310:
1.53 kristaps 311: assert(man_macros[tok].fp);
1.144 schwarze 312: (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf);
1.123 schwarze 313:
314: /* In quick mode (for mandocdb), abort after the NAME section. */
315:
1.144 schwarze 316: if (man->quick && tok == MAN_SH) {
1.130 schwarze 317: n = man->last;
1.150 schwarze 318: if (n->type == ROFFT_BODY &&
1.130 schwarze 319: strcmp(n->prev->child->string, "NAME"))
320: return(2);
321: }
1.1 kristaps 322:
1.129 schwarze 323: /*
1.135 schwarze 324: * If we are in a next-line scope for a block head,
325: * close it out now and switch to the body,
326: * unless the next-line scope is allowed to continue.
1.29 kristaps 327: */
328:
1.135 schwarze 329: if ( ! bline || man->flags & MAN_ELINE ||
330: man_macros[tok].flags & MAN_NSCOPED)
1.29 kristaps 331: return(1);
332:
1.144 schwarze 333: assert(man->flags & MAN_BLINE);
1.119 schwarze 334: man->flags &= ~MAN_BLINE;
1.11 kristaps 335:
1.144 schwarze 336: man_unscope(man, man->last->parent);
1.158 ! schwarze 337: roff_body_alloc(man, ln, ppos, man->last->tok);
1.144 schwarze 338: return(1);
1.149 schwarze 339: }
340:
341: void
1.153 schwarze 342: man_breakscope(struct roff_man *man, int tok)
1.149 schwarze 343: {
1.151 schwarze 344: struct roff_node *n;
1.149 schwarze 345:
346: /*
347: * An element next line scope is open,
348: * and the new macro is not allowed inside elements.
349: * Delete the element that is being broken.
350: */
351:
352: if (man->flags & MAN_ELINE && (tok == MAN_MAX ||
353: ! (man_macros[tok].flags & MAN_NSCOPED))) {
354: n = man->last;
1.150 schwarze 355: assert(n->type != ROFFT_TEXT);
1.149 schwarze 356: if (man_macros[n->tok].flags & MAN_NSCOPED)
357: n = n->parent;
358:
359: mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse,
360: n->line, n->pos, "%s breaks %s",
361: tok == MAN_MAX ? "TS" : man_macronames[tok],
362: man_macronames[n->tok]);
363:
1.158 ! schwarze 364: roff_node_delete(man, n);
1.149 schwarze 365: man->flags &= ~MAN_ELINE;
366: }
367:
368: /*
369: * A block header next line scope is open,
370: * and the new macro is not allowed inside block headers.
371: * Delete the block that is being broken.
372: */
373:
374: if (man->flags & MAN_BLINE && (tok == MAN_MAX ||
375: man_macros[tok].flags & MAN_BSCOPE)) {
376: n = man->last;
1.150 schwarze 377: if (n->type == ROFFT_TEXT)
1.149 schwarze 378: n = n->parent;
379: if ( ! (man_macros[n->tok].flags & MAN_BSCOPE))
380: n = n->parent;
381:
1.150 schwarze 382: assert(n->type == ROFFT_HEAD);
1.149 schwarze 383: n = n->parent;
1.150 schwarze 384: assert(n->type == ROFFT_BLOCK);
1.149 schwarze 385: assert(man_macros[n->tok].flags & MAN_SCOPED);
386:
387: mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse,
388: n->line, n->pos, "%s breaks %s",
389: tok == MAN_MAX ? "TS" : man_macronames[tok],
390: man_macronames[n->tok]);
391:
1.158 ! schwarze 392: roff_node_delete(man, n);
1.149 schwarze 393: man->flags &= ~MAN_BLINE;
394: }
1.112 kristaps 395: }
396:
397: const struct mparse *
1.153 schwarze 398: man_mparse(const struct roff_man *man)
1.112 kristaps 399: {
400:
1.119 schwarze 401: assert(man && man->parse);
402: return(man->parse);
1.126 schwarze 403: }
404:
405: void
1.151 schwarze 406: man_deroff(char **dest, const struct roff_node *n)
1.126 schwarze 407: {
408: char *cp;
409: size_t sz;
410:
1.150 schwarze 411: if (n->type != ROFFT_TEXT) {
1.126 schwarze 412: for (n = n->child; n; n = n->next)
413: man_deroff(dest, n);
414: return;
415: }
416:
1.127 schwarze 417: /* Skip leading whitespace and escape sequences. */
1.126 schwarze 418:
1.127 schwarze 419: cp = n->string;
420: while ('\0' != *cp) {
421: if ('\\' == *cp) {
422: cp++;
423: mandoc_escape((const char **)&cp, NULL, NULL);
424: } else if (isspace((unsigned char)*cp))
425: cp++;
426: else
1.126 schwarze 427: break;
1.127 schwarze 428: }
1.126 schwarze 429:
430: /* Skip trailing whitespace. */
431:
432: for (sz = strlen(cp); sz; sz--)
433: if (0 == isspace((unsigned char)cp[sz-1]))
434: break;
435:
436: /* Skip empty strings. */
437:
438: if (0 == sz)
439: return;
440:
441: if (NULL == *dest) {
442: *dest = mandoc_strndup(cp, sz);
443: return;
444: }
445:
446: mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
447: free(*dest);
448: *dest = cp;
1.23 kristaps 449: }
CVSweb