Annotation of src/usr.bin/mandoc/mdoc.c, Revision 1.45
1.45 ! schwarze 1: /* $Id: mdoc.c,v 1.44 2010/05/08 01:52:07 schwarze Exp $ */
1.1 kristaps 2: /*
1.3 schwarze 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.3 schwarze 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.3 schwarze 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 16: */
1.28 schwarze 17: #include <sys/types.h>
18:
1.1 kristaps 19: #include <assert.h>
20: #include <ctype.h>
21: #include <stdarg.h>
22: #include <stdio.h>
23: #include <stdlib.h>
24: #include <string.h>
1.39 schwarze 25: #include <time.h>
1.1 kristaps 26:
27: #include "libmdoc.h"
1.32 schwarze 28: #include "libmandoc.h"
1.1 kristaps 29:
1.14 schwarze 30: const char *const __mdoc_merrnames[MERRMAX] = {
31: "trailing whitespace", /* ETAILWS */
32: "unexpected quoted parameter", /* EQUOTPARM */
33: "unterminated quoted parameter", /* EQUOTTERM */
34: "argument parameter suggested", /* EARGVAL */
35: "macro disallowed in prologue", /* EBODYPROL */
36: "macro disallowed in body", /* EPROLBODY */
37: "text disallowed in prologue", /* ETEXTPROL */
38: "blank line disallowed", /* ENOBLANK */
39: "text parameter too long", /* ETOOLONG */
40: "invalid escape sequence", /* EESCAPE */
41: "invalid character", /* EPRINT */
42: "document has no body", /* ENODAT */
43: "document has no prologue", /* ENOPROLOGUE */
44: "expected line arguments", /* ELINE */
45: "invalid AT&T argument", /* EATT */
46: "default name not yet set", /* ENAME */
47: "missing list type", /* ELISTTYPE */
48: "missing display type", /* EDISPTYPE */
49: "too many display types", /* EMULTIDISP */
50: "too many list types", /* EMULTILIST */
51: "NAME section must be first", /* ESECNAME */
52: "badly-formed NAME section", /* ENAMESECINC */
53: "argument repeated", /* EARGREP */
54: "expected boolean parameter", /* EBOOL */
55: "inconsistent column syntax", /* ECOLMIS */
56: "nested display invalid", /* ENESTDISP */
57: "width argument missing", /* EMISSWIDTH */
58: "invalid section for this manual section", /* EWRONGMSEC */
59: "section out of conventional order", /* ESECOOO */
60: "section repeated", /* ESECREP */
61: "invalid standard argument", /* EBADSTAND */
62: "multi-line arguments discouraged", /* ENOMULTILINE */
63: "multi-line arguments suggested", /* EMULTILINE */
64: "line arguments discouraged", /* ENOLINE */
65: "prologue macro out of conventional order", /* EPROLOOO */
66: "prologue macro repeated", /* EPROLREP */
67: "invalid manual section", /* EBADMSEC */
68: "invalid section", /* EBADSEC */
69: "invalid font mode", /* EFONT */
70: "invalid date syntax", /* EBADDATE */
71: "invalid number format", /* ENUMFMT */
72: "superfluous width argument", /* ENOWIDTH */
73: "system: utsname error", /* EUTSNAME */
74: "obsolete macro", /* EOBS */
75: "end-of-line scope violation", /* EIMPBRK */
76: "empty macro ignored", /* EIGNE */
77: "unclosed explicit scope", /* EOPEN */
78: "unterminated quoted phrase", /* EQUOTPHR */
79: "closure macro without prior context", /* ENOCTX */
1.29 schwarze 80: "no description found for library", /* ELIB */
81: "bad child for parent context", /* EBADCHILD */
1.30 schwarze 82: "list arguments preceding type", /* ENOTYPE */
1.44 schwarze 83: "deprecated comment style", /* EBADCOMMENT */
1.1 kristaps 84: };
85:
86: const char *const __mdoc_macronames[MDOC_MAX] = {
1.7 schwarze 87: "Ap", "Dd", "Dt", "Os",
1.1 kristaps 88: "Sh", "Ss", "Pp", "D1",
89: "Dl", "Bd", "Ed", "Bl",
90: "El", "It", "Ad", "An",
91: "Ar", "Cd", "Cm", "Dv",
92: "Er", "Ev", "Ex", "Fa",
93: "Fd", "Fl", "Fn", "Ft",
94: "Ic", "In", "Li", "Nd",
95: "Nm", "Op", "Ot", "Pa",
96: "Rv", "St", "Va", "Vt",
97: /* LINTED */
1.33 schwarze 98: "Xr", "%A", "%B", "%D",
1.1 kristaps 99: /* LINTED */
1.33 schwarze 100: "%I", "%J", "%N", "%O",
1.1 kristaps 101: /* LINTED */
1.33 schwarze 102: "%P", "%R", "%T", "%V",
1.1 kristaps 103: "Ac", "Ao", "Aq", "At",
104: "Bc", "Bf", "Bo", "Bq",
105: "Bsx", "Bx", "Db", "Dc",
106: "Do", "Dq", "Ec", "Ef",
107: "Em", "Eo", "Fx", "Ms",
108: "No", "Ns", "Nx", "Ox",
109: "Pc", "Pf", "Po", "Pq",
110: "Qc", "Ql", "Qo", "Qq",
111: "Re", "Rs", "Sc", "So",
112: "Sq", "Sm", "Sx", "Sy",
113: "Tn", "Ux", "Xc", "Xo",
114: "Fo", "Fc", "Oo", "Oc",
115: "Bk", "Ek", "Bt", "Hf",
1.7 schwarze 116: "Fr", "Ud", "Lb", "Lp",
117: "Lk", "Mt", "Brq", "Bro",
1.1 kristaps 118: /* LINTED */
1.33 schwarze 119: "Brc", "%C", "Es", "En",
1.1 kristaps 120: /* LINTED */
1.33 schwarze 121: "Dx", "%Q", "br", "sp",
1.31 schwarze 122: /* LINTED */
1.35 schwarze 123: "%U", "eos"
1.1 kristaps 124: };
125:
126: const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
127: "split", "nosplit", "ragged",
128: "unfilled", "literal", "file",
129: "offset", "bullet", "dash",
130: "hyphen", "item", "enum",
131: "tag", "diag", "hang",
132: "ohang", "inset", "column",
133: "width", "compact", "std",
134: "filled", "words", "emphasis",
1.30 schwarze 135: "symbolic", "nested", "centered"
1.1 kristaps 136: };
137:
138: const char * const *mdoc_macronames = __mdoc_macronames;
139: const char * const *mdoc_argnames = __mdoc_argnames;
140:
1.40 schwarze 141: static void mdoc_node_free(struct mdoc_node *);
142: static void mdoc_node_unlink(struct mdoc *,
143: struct mdoc_node *);
1.1 kristaps 144: static void mdoc_free1(struct mdoc *);
1.32 schwarze 145: static void mdoc_alloc1(struct mdoc *);
1.1 kristaps 146: static struct mdoc_node *node_alloc(struct mdoc *, int, int,
1.37 schwarze 147: enum mdoct, enum mdoc_type);
1.1 kristaps 148: static int node_append(struct mdoc *,
149: struct mdoc_node *);
1.45 ! schwarze 150: static int mdoc_ptext(struct mdoc *, int, char *);
! 151: static int mdoc_pmacro(struct mdoc *, int, char *);
1.1 kristaps 152: static int macrowarn(struct mdoc *, int, const char *);
1.19 schwarze 153: static int pstring(struct mdoc *, int, int,
154: const char *, size_t);
1.1 kristaps 155:
156: const struct mdoc_node *
157: mdoc_node(const struct mdoc *m)
158: {
159:
160: return(MDOC_HALT & m->flags ? NULL : m->first);
161: }
162:
163:
164: const struct mdoc_meta *
165: mdoc_meta(const struct mdoc *m)
166: {
167:
168: return(MDOC_HALT & m->flags ? NULL : &m->meta);
169: }
170:
171:
1.8 schwarze 172: /*
173: * Frees volatile resources (parse tree, meta-data, fields).
174: */
1.1 kristaps 175: static void
176: mdoc_free1(struct mdoc *mdoc)
177: {
178:
179: if (mdoc->first)
1.40 schwarze 180: mdoc_node_delete(mdoc, mdoc->first);
1.1 kristaps 181: if (mdoc->meta.title)
182: free(mdoc->meta.title);
183: if (mdoc->meta.os)
184: free(mdoc->meta.os);
185: if (mdoc->meta.name)
186: free(mdoc->meta.name);
187: if (mdoc->meta.arch)
188: free(mdoc->meta.arch);
189: if (mdoc->meta.vol)
190: free(mdoc->meta.vol);
191: }
192:
193:
1.8 schwarze 194: /*
195: * Allocate all volatile resources (parse tree, meta-data, fields).
196: */
1.32 schwarze 197: static void
1.1 kristaps 198: mdoc_alloc1(struct mdoc *mdoc)
199: {
200:
1.32 schwarze 201: memset(&mdoc->meta, 0, sizeof(struct mdoc_meta));
1.1 kristaps 202: mdoc->flags = 0;
1.9 schwarze 203: mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
1.32 schwarze 204: mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node));
1.1 kristaps 205: mdoc->first = mdoc->last;
206: mdoc->last->type = MDOC_ROOT;
207: mdoc->next = MDOC_NEXT_CHILD;
208: }
209:
210:
211: /*
1.8 schwarze 212: * Free up volatile resources (see mdoc_free1()) then re-initialises the
213: * data with mdoc_alloc1(). After invocation, parse data has been reset
214: * and the parser is ready for re-invocation on a new tree; however,
215: * cross-parse non-volatile data is kept intact.
1.1 kristaps 216: */
1.32 schwarze 217: void
1.1 kristaps 218: mdoc_reset(struct mdoc *mdoc)
219: {
220:
221: mdoc_free1(mdoc);
1.32 schwarze 222: mdoc_alloc1(mdoc);
1.1 kristaps 223: }
224:
225:
226: /*
1.8 schwarze 227: * Completely free up all volatile and non-volatile parse resources.
228: * After invocation, the pointer is no longer usable.
1.1 kristaps 229: */
230: void
231: mdoc_free(struct mdoc *mdoc)
232: {
233:
234: mdoc_free1(mdoc);
235: free(mdoc);
236: }
237:
238:
1.8 schwarze 239: /*
240: * Allocate volatile and non-volatile parse resources.
241: */
1.1 kristaps 242: struct mdoc *
243: mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
244: {
245: struct mdoc *p;
246:
1.32 schwarze 247: p = mandoc_calloc(1, sizeof(struct mdoc));
248:
1.1 kristaps 249: if (cb)
1.32 schwarze 250: memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
1.27 schwarze 251:
1.1 kristaps 252: p->data = data;
253: p->pflags = pflags;
254:
1.32 schwarze 255: mdoc_hash_init();
256: mdoc_alloc1(p);
257: return(p);
1.1 kristaps 258: }
259:
260:
261: /*
262: * Climb back up the parse tree, validating open scopes. Mostly calls
1.8 schwarze 263: * through to macro_end() in macro.c.
1.1 kristaps 264: */
265: int
266: mdoc_endparse(struct mdoc *m)
267: {
268:
269: if (MDOC_HALT & m->flags)
270: return(0);
271: else if (mdoc_macroend(m))
272: return(1);
273: m->flags |= MDOC_HALT;
274: return(0);
275: }
276:
277:
278: /*
279: * Main parse routine. Parses a single line -- really just hands off to
1.45 ! schwarze 280: * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.1 kristaps 281: */
282: int
283: mdoc_parseln(struct mdoc *m, int ln, char *buf)
284: {
285:
286: if (MDOC_HALT & m->flags)
287: return(0);
288:
1.45 ! schwarze 289: return('.' == *buf ? mdoc_pmacro(m, ln, buf) :
! 290: mdoc_ptext(m, ln, buf));
1.1 kristaps 291: }
292:
293:
294: int
1.4 schwarze 295: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
296: const char *fmt, ...)
1.1 kristaps 297: {
298: char buf[256];
299: va_list ap;
300:
301: if (NULL == mdoc->cb.mdoc_err)
302: return(0);
303:
304: va_start(ap, fmt);
305: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
306: va_end(ap);
1.12 schwarze 307:
1.1 kristaps 308: return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
309: }
310:
311:
312: int
1.12 schwarze 313: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1 kristaps 314: {
315: char buf[256];
316: va_list ap;
317:
318: if (NULL == mdoc->cb.mdoc_warn)
319: return(0);
320:
321: va_start(ap, fmt);
322: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
323: va_end(ap);
1.17 schwarze 324:
1.11 schwarze 325: return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, buf));
1.5 schwarze 326: }
327:
328:
329: int
1.14 schwarze 330: mdoc_err(struct mdoc *m, int line, int pos, int iserr, enum merr type)
1.5 schwarze 331: {
1.14 schwarze 332: const char *p;
1.5 schwarze 333:
1.14 schwarze 334: p = __mdoc_merrnames[(int)type];
335: assert(p);
1.5 schwarze 336:
1.14 schwarze 337: if (iserr)
338: return(mdoc_verr(m, line, pos, p));
1.16 schwarze 339:
1.14 schwarze 340: return(mdoc_vwarn(m, line, pos, p));
1.5 schwarze 341: }
342:
343:
344: int
1.37 schwarze 345: mdoc_macro(struct mdoc *m, enum mdoct tok,
1.1 kristaps 346: int ln, int pp, int *pos, char *buf)
347: {
1.37 schwarze 348:
349: assert(tok < MDOC_MAX);
1.24 schwarze 350: /*
351: * If we're in the prologue, deny "body" macros. Similarly, if
352: * we're in the body, deny prologue calls.
353: */
1.1 kristaps 354: if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.9 schwarze 355: MDOC_PBODY & m->flags)
1.16 schwarze 356: return(mdoc_perr(m, ln, pp, EPROLBODY));
1.1 kristaps 357: if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
1.39 schwarze 358: ! (MDOC_PBODY & m->flags)) {
359: if ( ! mdoc_pwarn(m, ln, pp, EBODYPROL))
360: return(0);
361: if (NULL == m->meta.title)
362: m->meta.title = mandoc_strdup("unknown");
363: if (NULL == m->meta.vol)
364: m->meta.vol = mandoc_strdup("local");
365: if (NULL == m->meta.os)
366: m->meta.os = mandoc_strdup("local");
367: if (0 == m->meta.date)
368: m->meta.date = time(NULL);
369: m->flags |= MDOC_PBODY;
370: }
1.1 kristaps 371:
372: return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
373: }
374:
375:
376: static int
377: node_append(struct mdoc *mdoc, struct mdoc_node *p)
378: {
379:
380: assert(mdoc->last);
381: assert(mdoc->first);
382: assert(MDOC_ROOT != p->type);
383:
384: switch (mdoc->next) {
385: case (MDOC_NEXT_SIBLING):
386: mdoc->last->next = p;
387: p->prev = mdoc->last;
388: p->parent = mdoc->last->parent;
389: break;
390: case (MDOC_NEXT_CHILD):
391: mdoc->last->child = p;
392: p->parent = mdoc->last;
393: break;
394: default:
395: abort();
396: /* NOTREACHED */
397: }
398:
1.10 schwarze 399: p->parent->nchild++;
400:
1.1 kristaps 401: if ( ! mdoc_valid_pre(mdoc, p))
402: return(0);
403: if ( ! mdoc_action_pre(mdoc, p))
404: return(0);
405:
406: switch (p->type) {
407: case (MDOC_HEAD):
408: assert(MDOC_BLOCK == p->parent->type);
409: p->parent->head = p;
410: break;
411: case (MDOC_TAIL):
412: assert(MDOC_BLOCK == p->parent->type);
413: p->parent->tail = p;
414: break;
415: case (MDOC_BODY):
416: assert(MDOC_BLOCK == p->parent->type);
417: p->parent->body = p;
418: break;
419: default:
420: break;
421: }
422:
423: mdoc->last = p;
424:
425: switch (p->type) {
426: case (MDOC_TEXT):
427: if ( ! mdoc_valid_post(mdoc))
428: return(0);
429: if ( ! mdoc_action_post(mdoc))
430: return(0);
431: break;
432: default:
433: break;
434: }
435:
436: return(1);
437: }
438:
439:
440: static struct mdoc_node *
1.37 schwarze 441: node_alloc(struct mdoc *m, int line, int pos,
442: enum mdoct tok, enum mdoc_type type)
1.1 kristaps 443: {
444: struct mdoc_node *p;
445:
1.32 schwarze 446: p = mandoc_calloc(1, sizeof(struct mdoc_node));
1.19 schwarze 447: p->sec = m->lastsec;
1.1 kristaps 448: p->line = line;
449: p->pos = pos;
450: p->tok = tok;
1.37 schwarze 451: p->type = type;
1.1 kristaps 452:
453: return(p);
454: }
455:
456:
457: int
1.37 schwarze 458: mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1 kristaps 459: {
460: struct mdoc_node *p;
461:
1.19 schwarze 462: p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.25 schwarze 463: if ( ! node_append(m, p))
464: return(0);
465: m->next = MDOC_NEXT_CHILD;
466: return(1);
1.1 kristaps 467: }
468:
469:
470: int
1.37 schwarze 471: mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1 kristaps 472: {
473: struct mdoc_node *p;
474:
1.19 schwarze 475: assert(m->first);
476: assert(m->last);
1.1 kristaps 477:
1.19 schwarze 478: p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.25 schwarze 479: if ( ! node_append(m, p))
480: return(0);
481: m->next = MDOC_NEXT_CHILD;
482: return(1);
1.1 kristaps 483: }
484:
485:
486: int
1.37 schwarze 487: mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1 kristaps 488: {
489: struct mdoc_node *p;
490:
1.19 schwarze 491: p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.25 schwarze 492: if ( ! node_append(m, p))
493: return(0);
494: m->next = MDOC_NEXT_CHILD;
495: return(1);
1.1 kristaps 496: }
497:
498:
499: int
1.19 schwarze 500: mdoc_block_alloc(struct mdoc *m, int line, int pos,
1.37 schwarze 501: enum mdoct tok, struct mdoc_arg *args)
1.1 kristaps 502: {
503: struct mdoc_node *p;
504:
1.19 schwarze 505: p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
1.4 schwarze 506: p->args = args;
507: if (p->args)
1.1 kristaps 508: (args->refcnt)++;
1.25 schwarze 509: if ( ! node_append(m, p))
510: return(0);
511: m->next = MDOC_NEXT_CHILD;
512: return(1);
1.1 kristaps 513: }
514:
515:
516: int
1.19 schwarze 517: mdoc_elem_alloc(struct mdoc *m, int line, int pos,
1.37 schwarze 518: enum mdoct tok, struct mdoc_arg *args)
1.1 kristaps 519: {
520: struct mdoc_node *p;
521:
1.19 schwarze 522: p = node_alloc(m, line, pos, tok, MDOC_ELEM);
1.4 schwarze 523: p->args = args;
524: if (p->args)
1.1 kristaps 525: (args->refcnt)++;
1.25 schwarze 526: if ( ! node_append(m, p))
527: return(0);
528: m->next = MDOC_NEXT_CHILD;
529: return(1);
1.1 kristaps 530: }
531:
532:
1.19 schwarze 533: static int
534: pstring(struct mdoc *m, int line, int pos, const char *p, size_t len)
1.1 kristaps 535: {
1.19 schwarze 536: struct mdoc_node *n;
537: size_t sv;
1.1 kristaps 538:
1.19 schwarze 539: n = node_alloc(m, line, pos, -1, MDOC_TEXT);
1.32 schwarze 540: n->string = mandoc_malloc(len + 1);
1.19 schwarze 541: sv = strlcpy(n->string, p, len + 1);
542:
543: /* Prohibit truncation. */
544: assert(sv < len + 1);
545:
1.25 schwarze 546: if ( ! node_append(m, n))
547: return(0);
548: m->next = MDOC_NEXT_SIBLING;
549: return(1);
1.19 schwarze 550: }
551:
552:
553: int
554: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
555: {
556:
557: return(pstring(m, line, pos, p, strlen(p)));
1.1 kristaps 558: }
559:
560:
561: void
562: mdoc_node_free(struct mdoc_node *p)
563: {
564:
565: if (p->string)
566: free(p->string);
567: if (p->args)
568: mdoc_argv_free(p->args);
569: free(p);
570: }
571:
572:
1.40 schwarze 573: static void
574: mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
575: {
576:
577: /* Adjust siblings. */
578:
579: if (n->prev)
580: n->prev->next = n->next;
581: if (n->next)
582: n->next->prev = n->prev;
583:
584: /* Adjust parent. */
585:
586: if (n->parent) {
587: n->parent->nchild--;
588: if (n->parent->child == n)
589: n->parent->child = n->prev ? n->prev : n->next;
590: }
591:
592: /* Adjust parse point, if applicable. */
593:
594: if (m && m->last == n) {
595: if (n->prev) {
596: m->last = n->prev;
597: m->next = MDOC_NEXT_SIBLING;
598: } else {
599: m->last = n->parent;
600: m->next = MDOC_NEXT_CHILD;
601: }
602: }
603:
604: if (m && m->first == n)
605: m->first = NULL;
606: }
607:
608:
1.1 kristaps 609: void
1.40 schwarze 610: mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
1.1 kristaps 611: {
612:
1.40 schwarze 613: while (p->child) {
614: assert(p->nchild);
615: mdoc_node_delete(m, p->child);
616: }
617: assert(0 == p->nchild);
1.1 kristaps 618:
1.40 schwarze 619: mdoc_node_unlink(m, p);
1.1 kristaps 620: mdoc_node_free(p);
621: }
622:
623:
624: /*
625: * Parse free-form text, that is, a line that does not begin with the
626: * control character.
627: */
628: static int
1.45 ! schwarze 629: mdoc_ptext(struct mdoc *m, int line, char *buf)
1.1 kristaps 630: {
1.19 schwarze 631: int i, j;
1.34 schwarze 632: char sv;
1.44 schwarze 633:
634: /* Ignore bogus comments. */
635:
636: if ('\\' == buf[0] && '.' == buf[1] && '\"' == buf[2])
637: return(mdoc_pwarn(m, line, 0, EBADCOMMENT));
1.1 kristaps 638:
1.9 schwarze 639: if (SEC_NONE == m->lastnamed)
1.16 schwarze 640: return(mdoc_perr(m, line, 0, ETEXTPROL));
1.19 schwarze 641:
642: /*
643: * If in literal mode, then pass the buffer directly to the
644: * back-end, as it should be preserved as a single term.
645: */
1.1 kristaps 646:
1.25 schwarze 647: if (MDOC_LITERAL & m->flags)
648: return(mdoc_word_alloc(m, line, 0, buf));
1.19 schwarze 649:
650: /* Disallow blank/white-space lines in non-literal mode. */
651:
652: for (i = 0; ' ' == buf[i]; i++)
653: /* Skip leading whitespace. */ ;
1.34 schwarze 654:
1.38 schwarze 655: if ('\0' == buf[i]) {
656: if ( ! mdoc_pwarn(m, line, 0, ENOBLANK))
657: return(0);
1.40 schwarze 658: /*
659: * Assume that a `Pp' should be inserted in the case of
660: * a blank line. Technically, blank lines aren't
661: * allowed, but enough manuals assume this behaviour
662: * that we want to work around it.
663: */
1.42 schwarze 664: if ( ! mdoc_elem_alloc(m, line, 0, MDOC_Pp, NULL))
665: return(0);
666: m->next = MDOC_NEXT_SIBLING;
667: return(1);
1.38 schwarze 668: }
1.1 kristaps 669:
1.19 schwarze 670: /*
671: * Break apart a free-form line into tokens. Spaces are
672: * stripped out of the input.
673: */
674:
675: for (j = i; buf[i]; i++) {
676: if (' ' != buf[i])
677: continue;
678:
679: /* Escaped whitespace. */
680: if (i && ' ' == buf[i] && '\\' == buf[i - 1])
681: continue;
682:
1.34 schwarze 683: sv = buf[i];
684: buf[i++] = '\0';
685:
1.19 schwarze 686: if ( ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
687: return(0);
688:
1.34 schwarze 689: /* Trailing whitespace? Check at overwritten byte. */
690:
691: if (' ' == sv && '\0' == buf[i])
692: if ( ! mdoc_pwarn(m, line, i - 1, ETAILWS))
693: return(0);
694:
1.19 schwarze 695: for ( ; ' ' == buf[i]; i++)
696: /* Skip trailing whitespace. */ ;
697:
698: j = i;
1.34 schwarze 699:
700: /* Trailing whitespace? */
701:
702: if (' ' == buf[i - 1] && '\0' == buf[i])
703: if ( ! mdoc_pwarn(m, line, i - 1, ETAILWS))
704: return(0);
705:
706: if ('\0' == buf[i])
1.19 schwarze 707: break;
708: }
709:
710: if (j != i && ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
1.1 kristaps 711: return(0);
712:
1.35 schwarze 713: /*
714: * Mark the end of a sentence. Only works when you respect
715: * Jason's rule: "new sentence, new line".
716: */
717: if ('.' == buf[i-1] || '!' == buf[i-1] || '?' == buf[i-1]) {
718: m->next = MDOC_NEXT_SIBLING;
719: if ( ! mdoc_elem_alloc(m, line, i, MDOC_eos, NULL))
720: return(0);
721: }
722:
1.1 kristaps 723: m->next = MDOC_NEXT_SIBLING;
724: return(1);
725: }
1.19 schwarze 726:
1.1 kristaps 727:
728:
729: static int
730: macrowarn(struct mdoc *m, int ln, const char *buf)
731: {
732: if ( ! (MDOC_IGN_MACRO & m->pflags))
1.23 schwarze 733: return(mdoc_verr(m, ln, 0,
1.1 kristaps 734: "unknown macro: %s%s",
735: buf, strlen(buf) > 3 ? "..." : ""));
1.23 schwarze 736: return(mdoc_vwarn(m, ln, 0, "unknown macro: %s%s",
1.1 kristaps 737: buf, strlen(buf) > 3 ? "..." : ""));
738: }
739:
740:
741: /*
742: * Parse a macro line, that is, a line beginning with the control
743: * character.
744: */
745: int
1.45 ! schwarze 746: mdoc_pmacro(struct mdoc *m, int ln, char *buf)
1.1 kristaps 747: {
1.25 schwarze 748: int i, j, c;
1.1 kristaps 749: char mac[5];
1.35 schwarze 750: struct mdoc_node *n;
751: char *t;
1.1 kristaps 752:
1.7 schwarze 753: /* Empty lines are ignored. */
1.1 kristaps 754:
1.34 schwarze 755: if ('\0' == buf[1])
1.1 kristaps 756: return(1);
757:
1.24 schwarze 758: i = 1;
759:
760: /* Accept whitespace after the initial control char. */
761:
762: if (' ' == buf[i]) {
763: i++;
1.1 kristaps 764: while (buf[i] && ' ' == buf[i])
765: i++;
1.34 schwarze 766: if ('\0' == buf[i])
1.1 kristaps 767: return(1);
768: }
769:
770: /* Copy the first word into a nil-terminated buffer. */
771:
1.24 schwarze 772: for (j = 0; j < 4; j++, i++) {
1.34 schwarze 773: if ('\0' == (mac[j] = buf[i]))
1.1 kristaps 774: break;
775: else if (' ' == buf[i])
776: break;
1.26 schwarze 777:
778: /* Check for invalid characters. */
779:
780: if (isgraph((u_char)buf[i]))
781: continue;
782: return(mdoc_perr(m, ln, i, EPRINT));
1.1 kristaps 783: }
784:
1.24 schwarze 785: mac[j] = 0;
1.1 kristaps 786:
1.24 schwarze 787: if (j == 4 || j < 2) {
1.1 kristaps 788: if ( ! macrowarn(m, ln, mac))
789: goto err;
790: return(1);
791: }
792:
1.27 schwarze 793: if (MDOC_MAX == (c = mdoc_hash_find(mac))) {
1.1 kristaps 794: if ( ! macrowarn(m, ln, mac))
795: goto err;
796: return(1);
797: }
798:
799: /* The macro is sane. Jump to the next word. */
800:
801: while (buf[i] && ' ' == buf[i])
802: i++;
1.34 schwarze 803:
804: /* Trailing whitespace? */
805:
806: if ('\0' == buf[i] && ' ' == buf[i - 1])
807: if ( ! mdoc_pwarn(m, ln, i - 1, ETAILWS))
808: goto err;
1.1 kristaps 809:
1.24 schwarze 810: /*
811: * Begin recursive parse sequence. Since we're at the start of
812: * the line, we don't need to do callable/parseable checks.
813: */
1.25 schwarze 814: if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
1.1 kristaps 815: goto err;
1.35 schwarze 816:
817: /*
818: * Mark the end of a sentence, but be careful not to insert
1.43 schwarze 819: * markers into reference blocks and after ellipses in
820: * function definitions.
1.35 schwarze 821: */
822: n = m->last;
823: if (n->child)
824: n = n->child;
825: while (n->next)
826: n = n->next;
1.43 schwarze 827: if (MDOC_TEXT == n->type &&
828: MDOC_Fn != n->parent->tok &&
829: MDOC_Rs != m->last->parent->tok) {
1.35 schwarze 830: t = n->string;
1.36 schwarze 831: while (t[0] && t[1])
1.35 schwarze 832: t++;
833: if ('.' == *t || '!' == *t || '?' == *t) {
834: if ( ! mdoc_elem_alloc(m, ln, i, MDOC_eos, NULL))
835: return(0);
836: m->next = MDOC_NEXT_SIBLING;
837: }
838: }
1.1 kristaps 839:
840: return(1);
841:
842: err: /* Error out. */
843:
844: m->flags |= MDOC_HALT;
845: return(0);
846: }
1.24 schwarze 847:
848: