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