Annotation of mandoc/man.c, Revision 1.170
1.170 ! schwarze 1: /* $Id: man.c,v 1.169 2017/04/24 23:06:18 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.153 schwarze 38: static void man_descope(struct roff_man *, int, int);
39: static int man_ptext(struct roff_man *, int, char *, int);
40: static int man_pmacro(struct roff_man *, int, char *, int);
1.1 kristaps 41:
42:
43: int
1.153 schwarze 44: man_parseln(struct roff_man *man, int ln, char *buf, int offs)
1.1 kristaps 45: {
46:
1.150 schwarze 47: if (man->last->type != ROFFT_EQN || ln > man->last->line)
1.141 schwarze 48: man->flags |= MAN_NEWLINE;
1.97 kristaps 49:
1.165 schwarze 50: return roff_getcontrol(man->roff, buf, &offs) ?
1.129 schwarze 51: man_pmacro(man, ln, buf, offs) :
1.165 schwarze 52: man_ptext(man, ln, buf, offs);
1.1 kristaps 53: }
1.2 kristaps 54:
1.144 schwarze 55: static void
1.153 schwarze 56: man_descope(struct roff_man *man, int line, int offs)
1.94 kristaps 57: {
58: /*
59: * Co-ordinate what happens with having a next-line scope open:
60: * first close out the element scope (if applicable), then close
61: * out the block scope (also if applicable).
62: */
63:
1.144 schwarze 64: if (man->flags & MAN_ELINE) {
1.119 schwarze 65: man->flags &= ~MAN_ELINE;
1.144 schwarze 66: man_unscope(man, man->last->parent);
1.94 kristaps 67: }
1.144 schwarze 68: if ( ! (man->flags & MAN_BLINE))
69: return;
1.119 schwarze 70: man->flags &= ~MAN_BLINE;
1.144 schwarze 71: man_unscope(man, man->last->parent);
1.158 schwarze 72: roff_body_alloc(man, line, offs, man->last->tok);
1.94 kristaps 73: }
74:
1.1 kristaps 75: static int
1.153 schwarze 76: man_ptext(struct roff_man *man, int line, char *buf, int offs)
1.1 kristaps 77: {
1.61 kristaps 78: int i;
1.60 kristaps 79:
1.35 kristaps 80: /* Literal free-form text whitespace is preserved. */
81:
1.144 schwarze 82: if (man->flags & MAN_LITERAL) {
1.160 schwarze 83: roff_word_alloc(man, line, offs, buf + offs);
1.144 schwarze 84: man_descope(man, line, offs);
1.165 schwarze 85: return 1;
1.35 kristaps 86: }
87:
1.144 schwarze 88: for (i = offs; buf[i] == ' '; i++)
1.31 kristaps 89: /* Skip leading whitespace. */ ;
1.48 kristaps 90:
1.121 schwarze 91: /*
92: * Blank lines are ignored right after headings
93: * but add a single vertical space elsewhere.
94: */
95:
1.144 schwarze 96: if (buf[i] == '\0') {
1.61 kristaps 97: /* Allocate a blank entry. */
1.144 schwarze 98: if (man->last->tok != MAN_SH &&
99: man->last->tok != MAN_SS) {
1.161 schwarze 100: roff_elem_alloc(man, line, offs, MAN_sp);
1.153 schwarze 101: man->next = ROFF_NEXT_SIBLING;
1.121 schwarze 102: }
1.165 schwarze 103: return 1;
1.31 kristaps 104: }
105:
1.129 schwarze 106: /*
1.63 kristaps 107: * Warn if the last un-escaped character is whitespace. Then
1.129 schwarze 108: * strip away the remaining spaces (tabs stay!).
1.63 kristaps 109: */
1.29 kristaps 110:
1.61 kristaps 111: i = (int)strlen(buf);
112: assert(i);
1.49 kristaps 113:
1.63 kristaps 114: if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
1.64 kristaps 115: if (i > 1 && '\\' != buf[i - 2])
1.131 schwarze 116: mandoc_msg(MANDOCERR_SPACE_EOL, man->parse,
117: line, i - 1, NULL);
1.63 kristaps 118:
119: for (--i; i && ' ' == buf[i]; i--)
120: /* Spin back to non-space. */ ;
121:
122: /* Jump ahead of escaped whitespace. */
123: i += '\\' == buf[i] ? 2 : 1;
124:
125: buf[i] = '\0';
126: }
1.160 schwarze 127: roff_word_alloc(man, line, offs, buf + offs);
1.65 kristaps 128:
129: /*
130: * End-of-sentence check. If the last character is an unescaped
131: * EOS character, then flag the node as being the end of a
132: * sentence. The front-end will know how to interpret this.
133: */
1.67 kristaps 134:
1.65 kristaps 135: assert(i);
1.122 schwarze 136: if (mandoc_eos(buf, (size_t)i))
1.167 schwarze 137: man->last->flags |= NODE_EOS;
1.31 kristaps 138:
1.144 schwarze 139: man_descope(man, line, offs);
1.165 schwarze 140: return 1;
1.1 kristaps 141: }
142:
1.96 kristaps 143: static int
1.153 schwarze 144: man_pmacro(struct roff_man *man, int ln, char *buf, int offs)
1.1 kristaps 145: {
1.151 schwarze 146: struct roff_node *n;
1.143 schwarze 147: const char *cp;
1.170 ! schwarze 148: size_t sz;
! 149: enum roff_tok tok;
! 150: int ppos;
1.134 schwarze 151: int bline;
1.170 ! schwarze 152:
! 153: /* Determine the line macro. */
1.1 kristaps 154:
1.107 kristaps 155: ppos = offs;
1.170 ! schwarze 156: tok = TOKEN_NONE;
! 157: for (sz = 0; sz < 4 && strchr(" \t\\", buf[offs]) == NULL; sz++)
! 158: offs++;
! 159: if (sz > 0 && sz < 4)
! 160: tok = roffhash_find(man->manmac, buf + ppos, sz);
1.159 schwarze 161: if (tok == TOKEN_NONE) {
1.136 schwarze 162: mandoc_msg(MANDOCERR_MACRO, man->parse,
163: ln, ppos, buf + ppos - 1);
1.165 schwarze 164: return 1;
1.1 kristaps 165: }
166:
1.143 schwarze 167: /* Skip a leading escape sequence or tab. */
168:
169: switch (buf[offs]) {
170: case '\\':
171: cp = buf + offs + 1;
172: mandoc_escape(&cp, NULL, NULL);
173: offs = cp - buf;
174: break;
175: case '\t':
176: offs++;
177: break;
178: default:
179: break;
180: }
181:
182: /* Jump to the next non-whitespace word. */
1.1 kristaps 183:
1.168 schwarze 184: while (buf[offs] == ' ')
1.107 kristaps 185: offs++;
1.1 kristaps 186:
1.129 schwarze 187: /*
1.62 kristaps 188: * Trailing whitespace. Note that tabs are allowed to be passed
189: * into the parser as "text", so we only warn about spaces here.
190: */
1.48 kristaps 191:
1.144 schwarze 192: if (buf[offs] == '\0' && buf[offs - 1] == ' ')
1.131 schwarze 193: mandoc_msg(MANDOCERR_SPACE_EOL, man->parse,
194: ln, offs - 1, NULL);
1.48 kristaps 195:
1.129 schwarze 196: /*
1.149 schwarze 197: * Some macros break next-line scopes; otherwise, remember
198: * whether we are in next-line scope for a block head.
1.50 kristaps 199: */
1.34 kristaps 200:
1.149 schwarze 201: man_breakscope(man, tok);
1.134 schwarze 202: bline = man->flags & MAN_BLINE;
1.59 kristaps 203:
204: /* Call to handler... */
1.1 kristaps 205:
1.53 kristaps 206: assert(man_macros[tok].fp);
1.144 schwarze 207: (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf);
1.123 schwarze 208:
209: /* In quick mode (for mandocdb), abort after the NAME section. */
210:
1.144 schwarze 211: if (man->quick && tok == MAN_SH) {
1.130 schwarze 212: n = man->last;
1.150 schwarze 213: if (n->type == ROFFT_BODY &&
1.130 schwarze 214: strcmp(n->prev->child->string, "NAME"))
1.165 schwarze 215: return 2;
1.130 schwarze 216: }
1.1 kristaps 217:
1.129 schwarze 218: /*
1.135 schwarze 219: * If we are in a next-line scope for a block head,
220: * close it out now and switch to the body,
221: * unless the next-line scope is allowed to continue.
1.29 kristaps 222: */
223:
1.135 schwarze 224: if ( ! bline || man->flags & MAN_ELINE ||
225: man_macros[tok].flags & MAN_NSCOPED)
1.165 schwarze 226: return 1;
1.29 kristaps 227:
1.144 schwarze 228: assert(man->flags & MAN_BLINE);
1.119 schwarze 229: man->flags &= ~MAN_BLINE;
1.11 kristaps 230:
1.144 schwarze 231: man_unscope(man, man->last->parent);
1.158 schwarze 232: roff_body_alloc(man, ln, ppos, man->last->tok);
1.165 schwarze 233: return 1;
1.149 schwarze 234: }
235:
236: void
1.153 schwarze 237: man_breakscope(struct roff_man *man, int tok)
1.149 schwarze 238: {
1.151 schwarze 239: struct roff_node *n;
1.149 schwarze 240:
241: /*
242: * An element next line scope is open,
243: * and the new macro is not allowed inside elements.
244: * Delete the element that is being broken.
245: */
246:
1.159 schwarze 247: if (man->flags & MAN_ELINE && (tok == TOKEN_NONE ||
1.149 schwarze 248: ! (man_macros[tok].flags & MAN_NSCOPED))) {
249: n = man->last;
1.150 schwarze 250: assert(n->type != ROFFT_TEXT);
1.149 schwarze 251: if (man_macros[n->tok].flags & MAN_NSCOPED)
252: n = n->parent;
253:
254: mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse,
255: n->line, n->pos, "%s breaks %s",
1.169 schwarze 256: tok == TOKEN_NONE ? "TS" : roff_name[tok],
257: roff_name[n->tok]);
1.149 schwarze 258:
1.158 schwarze 259: roff_node_delete(man, n);
1.149 schwarze 260: man->flags &= ~MAN_ELINE;
1.164 schwarze 261: }
262:
263: /*
264: * Weird special case:
265: * Switching fill mode closes section headers.
266: */
267:
268: if (man->flags & MAN_BLINE &&
269: (tok == MAN_nf || tok == MAN_fi) &&
270: (man->last->tok == MAN_SH || man->last->tok == MAN_SS)) {
271: n = man->last;
272: man_unscope(man, n);
273: roff_body_alloc(man, n->line, n->pos, n->tok);
274: man->flags &= ~MAN_BLINE;
1.149 schwarze 275: }
276:
277: /*
278: * A block header next line scope is open,
279: * and the new macro is not allowed inside block headers.
280: * Delete the block that is being broken.
281: */
282:
1.159 schwarze 283: if (man->flags & MAN_BLINE && (tok == TOKEN_NONE ||
1.149 schwarze 284: man_macros[tok].flags & MAN_BSCOPE)) {
285: n = man->last;
1.150 schwarze 286: if (n->type == ROFFT_TEXT)
1.149 schwarze 287: n = n->parent;
288: if ( ! (man_macros[n->tok].flags & MAN_BSCOPE))
289: n = n->parent;
290:
1.150 schwarze 291: assert(n->type == ROFFT_HEAD);
1.149 schwarze 292: n = n->parent;
1.150 schwarze 293: assert(n->type == ROFFT_BLOCK);
1.149 schwarze 294: assert(man_macros[n->tok].flags & MAN_SCOPED);
295:
296: mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse,
297: n->line, n->pos, "%s breaks %s",
1.169 schwarze 298: tok == TOKEN_NONE ? "TS" : roff_name[tok],
299: roff_name[n->tok]);
1.149 schwarze 300:
1.158 schwarze 301: roff_node_delete(man, n);
1.149 schwarze 302: man->flags &= ~MAN_BLINE;
303: }
1.112 kristaps 304: }
305:
306: const struct mparse *
1.153 schwarze 307: man_mparse(const struct roff_man *man)
1.112 kristaps 308: {
309:
1.119 schwarze 310: assert(man && man->parse);
1.165 schwarze 311: return man->parse;
1.166 schwarze 312: }
313:
314: void
315: man_state(struct roff_man *man, struct roff_node *n)
316: {
317:
318: switch(n->tok) {
319: case MAN_nf:
320: case MAN_EX:
1.167 schwarze 321: if (man->flags & MAN_LITERAL && ! (n->flags & NODE_VALID))
1.166 schwarze 322: mandoc_msg(MANDOCERR_NF_SKIP, man->parse,
323: n->line, n->pos, "nf");
324: man->flags |= MAN_LITERAL;
325: break;
326: case MAN_fi:
327: case MAN_EE:
328: if ( ! (man->flags & MAN_LITERAL) &&
1.167 schwarze 329: ! (n->flags & NODE_VALID))
1.166 schwarze 330: mandoc_msg(MANDOCERR_FI_SKIP, man->parse,
331: n->line, n->pos, "fi");
332: man->flags &= ~MAN_LITERAL;
333: break;
334: default:
335: break;
336: }
1.167 schwarze 337: man->last->flags |= NODE_VALID;
1.166 schwarze 338: }
339:
340: void
341: man_validate(struct roff_man *man)
342: {
343:
344: man->last = man->first;
345: man_node_validate(man);
346: man->flags &= ~MAN_LITERAL;
1.23 kristaps 347: }
CVSweb