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