]>
git.cameronkatri.com Git - mandoc.git/blob - validate.c
1 /* $Id: validate.c,v 1.69 2009/03/06 14:13:47 kristaps Exp $ */
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
26 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
29 * Pre- and post-validate macros as they're parsed. Pre-validation
30 * occurs when the macro has been detected and its arguments parsed.
31 * Post-validation occurs when all child macros have also been parsed.
32 * In the ELEMENT case, this is simply the parameters of the macro; in
33 * the BLOCK case, this is the HEAD, BODY, TAIL and so on.
36 #define PRE_ARGS struct mdoc *mdoc, const struct mdoc_node *n
37 #define POST_ARGS struct mdoc *mdoc
39 typedef int (*v_pre
)(PRE_ARGS
);
40 typedef int (*v_post
)(POST_ARGS
);
42 /* TODO: ignoring Pp (it's superfluous in some invocations). */
51 static int check_parent(PRE_ARGS
, int, enum mdoc_type
);
52 static int check_msec(PRE_ARGS
, ...);
53 static int check_sec(PRE_ARGS
, ...);
54 static int check_stdarg(PRE_ARGS
);
56 static int check_text(struct mdoc
*,
57 int, int, const char *);
58 static int check_argv(struct mdoc
*,
59 const struct mdoc_node
*,
60 const struct mdoc_arg
*);
62 static int err_child_lt(struct mdoc
*, const char *, int);
63 static int warn_child_lt(struct mdoc
*, const char *, int);
64 static int err_child_gt(struct mdoc
*, const char *, int);
65 static int warn_child_gt(struct mdoc
*, const char *, int);
66 static int err_child_eq(struct mdoc
*, const char *, int);
67 static int warn_child_eq(struct mdoc
*, const char *, int);
69 /* Utility auxiliaries. */
71 static inline int count_child(struct mdoc
*);
72 static inline int warn_count(struct mdoc
*, const char *,
73 int, const char *, int);
74 static inline int err_count(struct mdoc
*, const char *,
75 int, const char *, int);
77 /* Specific pre-child-parse routines. */
79 static int pre_an(PRE_ARGS
);
80 static int pre_bd(PRE_ARGS
);
81 static int pre_bl(PRE_ARGS
);
82 static int pre_cd(PRE_ARGS
);
83 static int pre_dd(PRE_ARGS
);
84 static int pre_display(PRE_ARGS
);
85 static int pre_dt(PRE_ARGS
);
86 static int pre_er(PRE_ARGS
);
87 static int pre_ex(PRE_ARGS
);
88 static int pre_fd(PRE_ARGS
);
89 static int pre_it(PRE_ARGS
);
90 static int pre_lb(PRE_ARGS
);
91 static int pre_os(PRE_ARGS
);
92 static int pre_prologue(PRE_ARGS
);
93 static int pre_rv(PRE_ARGS
);
94 static int pre_sh(PRE_ARGS
);
95 static int pre_ss(PRE_ARGS
);
97 /* Specific post-child-parse routines. */
99 static int herr_ge1(POST_ARGS
);
100 static int hwarn_le1(POST_ARGS
);
101 static int herr_eq0(POST_ARGS
);
102 static int eerr_eq0(POST_ARGS
);
103 static int eerr_le1(POST_ARGS
);
104 static int eerr_le2(POST_ARGS
);
105 static int eerr_eq1(POST_ARGS
);
106 static int eerr_ge1(POST_ARGS
);
107 static int ewarn_eq0(POST_ARGS
);
108 static int ewarn_eq1(POST_ARGS
);
109 static int bwarn_ge1(POST_ARGS
);
110 static int hwarn_eq1(POST_ARGS
);
111 static int ewarn_ge1(POST_ARGS
);
112 static int ebool(POST_ARGS
);
114 static int post_an(POST_ARGS
);
115 static int post_at(POST_ARGS
);
116 static int post_bf(POST_ARGS
);
117 static int post_bl(POST_ARGS
);
118 static int post_ex(POST_ARGS
);
119 static int post_it(POST_ARGS
);
120 static int post_nm(POST_ARGS
);
121 static int post_root(POST_ARGS
);
122 static int post_sh(POST_ARGS
);
123 static int post_sh_body(POST_ARGS
);
124 static int post_sh_head(POST_ARGS
);
125 static int post_st(POST_ARGS
);
127 /* Collections of pre-child-parse routines. */
129 static v_pre pres_an
[] = { pre_an
, NULL
};
130 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
131 static v_pre pres_bl
[] = { pre_bl
, NULL
};
132 static v_pre pres_cd
[] = { pre_cd
, NULL
};
133 static v_pre pres_dd
[] = { pre_prologue
, pre_dd
, NULL
};
134 static v_pre pres_d1
[] = { pre_display
, NULL
};
135 static v_pre pres_dt
[] = { pre_prologue
, pre_dt
, NULL
};
136 static v_pre pres_er
[] = { pre_er
, NULL
};
137 static v_pre pres_ex
[] = { pre_ex
, NULL
};
138 static v_pre pres_fd
[] = { pre_fd
, NULL
};
139 static v_pre pres_it
[] = { pre_it
, NULL
};
140 static v_pre pres_lb
[] = { pre_lb
, NULL
};
141 static v_pre pres_os
[] = { pre_prologue
, pre_os
, NULL
};
142 static v_pre pres_rv
[] = { pre_rv
, NULL
};
143 static v_pre pres_sh
[] = { pre_sh
, NULL
};
144 static v_pre pres_ss
[] = { pre_ss
, NULL
};
146 /* Collections of post-child-parse routines. */
148 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
149 static v_post posts_bd
[] = { herr_eq0
, bwarn_ge1
, NULL
};
150 static v_post posts_text
[] = { eerr_ge1
, NULL
};
151 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
152 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
153 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
154 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
155 static v_post posts_bl
[] = { herr_eq0
, bwarn_ge1
, post_bl
, NULL
};
156 static v_post posts_it
[] = { post_it
, NULL
};
157 static v_post posts_in
[] = { ewarn_eq1
, NULL
};
158 static v_post posts_ss
[] = { herr_ge1
, NULL
};
159 static v_post posts_pf
[] = { eerr_eq1
, NULL
};
160 static v_post posts_lb
[] = { eerr_eq1
, NULL
};
161 static v_post posts_st
[] = { eerr_eq1
, post_st
, NULL
};
162 static v_post posts_pp
[] = { ewarn_eq0
, NULL
};
163 static v_post posts_ex
[] = { eerr_le1
, post_ex
, NULL
};
164 static v_post posts_an
[] = { post_an
, NULL
};
165 static v_post posts_at
[] = { post_at
, NULL
};
166 static v_post posts_xr
[] = { eerr_ge1
, eerr_le2
, NULL
};
167 static v_post posts_nm
[] = { post_nm
, NULL
};
168 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
169 static v_post posts_rs
[] = { herr_eq0
, bwarn_ge1
, NULL
};
170 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
171 static v_post posts_bk
[] = { herr_eq0
, bwarn_ge1
, NULL
};
172 static v_post posts_fd
[] = { ewarn_ge1
, NULL
};
174 /* Per-macro pre- and post-child-check routine collections. */
176 const struct valids mdoc_valids
[MDOC_MAX
] = {
177 { NULL
, NULL
}, /* \" */
178 { pres_dd
, posts_text
}, /* Dd */
179 { pres_dt
, NULL
}, /* Dt */
180 { pres_os
, NULL
}, /* Os */
181 { pres_sh
, posts_sh
}, /* Sh */
182 { pres_ss
, posts_ss
}, /* Ss */
183 { NULL
, posts_pp
}, /* Pp */
184 { pres_d1
, posts_wline
}, /* D1 */
185 { pres_d1
, posts_wline
}, /* Dl */
186 { pres_bd
, posts_bd
}, /* Bd */
187 { NULL
, NULL
}, /* Ed */
188 { pres_bl
, posts_bl
}, /* Bl */
189 { NULL
, NULL
}, /* El */
190 { pres_it
, posts_it
}, /* It */
191 { NULL
, posts_text
}, /* Ad */
192 { pres_an
, posts_an
}, /* An */
193 { NULL
, NULL
}, /* Ar */
194 { pres_cd
, posts_text
}, /* Cd */
195 { NULL
, NULL
}, /* Cm */
196 { NULL
, posts_text
}, /* Dv */
197 { pres_er
, posts_text
}, /* Er */
198 { NULL
, posts_text
}, /* Ev */
199 { pres_ex
, posts_ex
}, /* Ex */
200 { NULL
, posts_text
}, /* Fa */
201 { pres_fd
, posts_fd
}, /* Fd */
202 { NULL
, NULL
}, /* Fl */
203 { NULL
, posts_text
}, /* Fn */
204 { NULL
, posts_wtext
}, /* Ft */
205 { NULL
, posts_text
}, /* Ic */
206 { NULL
, posts_in
}, /* In */
207 { NULL
, posts_text
}, /* Li */
208 { NULL
, posts_wtext
}, /* Nd */
209 { NULL
, posts_nm
}, /* Nm */
210 { NULL
, posts_wline
}, /* Op */
211 { NULL
, NULL
}, /* Ot */
212 { NULL
, NULL
}, /* Pa */
213 { pres_rv
, posts_notext
}, /* Rv */
214 { NULL
, posts_st
}, /* St */
215 { NULL
, posts_text
}, /* Va */
216 { NULL
, posts_text
}, /* Vt */
217 { NULL
, posts_xr
}, /* Xr */
218 { NULL
, posts_text
}, /* %A */
219 { NULL
, posts_text
}, /* %B */
220 { NULL
, posts_text
}, /* %D */
221 { NULL
, posts_text
}, /* %I */
222 { NULL
, posts_text
}, /* %J */
223 { NULL
, posts_text
}, /* %N */
224 { NULL
, posts_text
}, /* %O */
225 { NULL
, posts_text
}, /* %P */
226 { NULL
, posts_text
}, /* %R */
227 { NULL
, posts_text
}, /* %T */
228 { NULL
, posts_text
}, /* %V */
229 { NULL
, NULL
}, /* Ac */
230 { NULL
, NULL
}, /* Ao */
231 { NULL
, posts_wline
}, /* Aq */
232 { NULL
, posts_at
}, /* At */
233 { NULL
, NULL
}, /* Bc */
234 { NULL
, posts_bf
}, /* Bf */
235 { NULL
, NULL
}, /* Bo */
236 { NULL
, posts_wline
}, /* Bq */
237 { NULL
, NULL
}, /* Bsx */
238 { NULL
, NULL
}, /* Bx */
239 { NULL
, posts_bool
}, /* Db */
240 { NULL
, NULL
}, /* Dc */
241 { NULL
, NULL
}, /* Do */
242 { NULL
, posts_wline
}, /* Dq */
243 { NULL
, NULL
}, /* Ec */
244 { NULL
, NULL
}, /* Ef */
245 { NULL
, posts_text
}, /* Em */
246 { NULL
, NULL
}, /* Eo */
247 { NULL
, NULL
}, /* Fx */
248 { NULL
, posts_text
}, /* Ms */
249 { NULL
, posts_notext
}, /* No */
250 { NULL
, posts_notext
}, /* Ns */
251 { NULL
, NULL
}, /* Nx */
252 { NULL
, NULL
}, /* Ox */
253 { NULL
, NULL
}, /* Pc */
254 { NULL
, posts_pf
}, /* Pf */
255 { NULL
, NULL
}, /* Po */
256 { NULL
, posts_wline
}, /* Pq */
257 { NULL
, NULL
}, /* Qc */
258 { NULL
, posts_wline
}, /* Ql */
259 { NULL
, NULL
}, /* Qo */
260 { NULL
, posts_wline
}, /* Qq */
261 { NULL
, NULL
}, /* Re */
262 { NULL
, posts_rs
}, /* Rs */
263 { NULL
, NULL
}, /* Sc */
264 { NULL
, NULL
}, /* So */
265 { NULL
, posts_wline
}, /* Sq */
266 { NULL
, posts_bool
}, /* Sm */
267 { NULL
, posts_text
}, /* Sx */
268 { NULL
, posts_text
}, /* Sy */
269 { NULL
, posts_text
}, /* Tn */
270 { NULL
, NULL
}, /* Ux */
271 { NULL
, NULL
}, /* Xc */
272 { NULL
, NULL
}, /* Xo */
273 { NULL
, posts_fo
}, /* Fo */
274 { NULL
, NULL
}, /* Fc */
275 { NULL
, NULL
}, /* Oo */
276 { NULL
, NULL
}, /* Oc */
277 { NULL
, posts_bk
}, /* Bk */
278 { NULL
, NULL
}, /* Ek */
279 { NULL
, posts_notext
}, /* Bt */
280 { NULL
, NULL
}, /* Hf */
281 { NULL
, NULL
}, /* Fr */
282 { NULL
, posts_notext
}, /* Ud */
283 { pres_lb
, posts_lb
}, /* Lb */
288 mdoc_valid_pre(struct mdoc
*mdoc
,
289 const struct mdoc_node
*node
)
292 struct mdoc_arg
*argv
;
297 if (MDOC_TEXT
== node
->type
) {
298 tp
= node
->data
.text
.string
;
301 return(check_text(mdoc
, line
, pos
, tp
));
304 if (MDOC_BLOCK
== node
->type
|| MDOC_ELEM
== node
->type
) {
305 argv
= MDOC_BLOCK
== node
->type
?
306 node
->data
.block
.argv
:
307 node
->data
.elem
.argv
;
308 argc
= MDOC_BLOCK
== node
->type
?
309 node
->data
.block
.argc
:
310 node
->data
.elem
.argc
;
312 for (i
= 0; i
< (int)argc
; i
++) {
313 for (j
= 0; j
< (int)argv
[i
].sz
; j
++) {
314 tp
= argv
[i
].value
[j
];
317 if ( ! check_text(mdoc
, line
, pos
, tp
))
320 if ( ! check_argv(mdoc
, node
, &argv
[i
]))
325 if (NULL
== mdoc_valids
[node
->tok
].pre
)
327 for (p
= mdoc_valids
[node
->tok
].pre
; *p
; p
++)
328 if ( ! (*p
)(mdoc
, node
))
335 mdoc_valid_post(struct mdoc
*mdoc
)
340 * This check occurs after the macro's children have been filled
341 * in: postfix validation. Since this happens when we're
342 * rewinding the scope tree, it's possible to have multiple
343 * invocations (as by design, for now), we set bit MDOC_VALID to
344 * indicate that we've validated.
347 if (MDOC_VALID
& mdoc
->last
->flags
)
349 mdoc
->last
->flags
|= MDOC_VALID
;
351 if (MDOC_TEXT
== mdoc
->last
->type
)
353 if (MDOC_ROOT
== mdoc
->last
->type
)
354 return(post_root(mdoc
));
356 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
358 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
368 warn_count(struct mdoc
*m
, const char *k
,
369 int want
, const char *v
, int has
)
372 return(mdoc_warn(m
, WARN_SYNTAX
,
373 "suggests %s %s %d (has %d)",
379 err_count(struct mdoc
*m
, const char *k
,
380 int want
, const char *v
, int has
)
383 return(mdoc_err(m
, "requires %s %s %d (has %d)",
389 count_child(struct mdoc
*mdoc
)
394 for (i
= 0, n
= mdoc
->last
->child
; n
; n
= n
->next
, i
++)
402 * Build these up with macros because they're basically the same check
403 * for different inequalities. Yes, this could be done with functions,
404 * but this is reasonable for now.
407 #define CHECK_CHILD_DEFN(lvl, name, ineq) \
409 lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
412 if ((i = count_child(mdoc)) ineq sz) \
414 return(lvl##_count(mdoc, #ineq, sz, p, i)); \
417 #define CHECK_BODY_DEFN(name, lvl, func, num) \
419 b##lvl##_##name(POST_ARGS) \
421 if (MDOC_BODY != mdoc->last->type) \
423 return(func(mdoc, "multiline parameters", (num))); \
426 #define CHECK_ELEM_DEFN(name, lvl, func, num) \
428 e##lvl##_##name(POST_ARGS) \
430 assert(MDOC_ELEM == mdoc->last->type); \
431 return(func(mdoc, "line parameters", (num))); \
434 #define CHECK_HEAD_DEFN(name, lvl, func, num) \
436 h##lvl##_##name(POST_ARGS) \
438 if (MDOC_HEAD != mdoc->last->type) \
440 return(func(mdoc, "line parameters", (num))); \
444 CHECK_CHILD_DEFN(warn
, gt
, >) /* warn_child_gt() */
445 CHECK_CHILD_DEFN(err
, gt
, >) /* err_child_gt() */
446 CHECK_CHILD_DEFN(warn
, eq
, ==) /* warn_child_eq() */
447 CHECK_CHILD_DEFN(err
, eq
, ==) /* err_child_eq() */
448 CHECK_CHILD_DEFN(err
, lt
, <) /* err_child_lt() */
449 CHECK_CHILD_DEFN(warn
, lt
, <) /* warn_child_lt() */
450 CHECK_BODY_DEFN(ge1
, warn
, warn_child_gt
, 0) /* bwarn_ge1() */
451 CHECK_ELEM_DEFN(eq1
, warn
, warn_child_eq
, 1) /* ewarn_eq1() */
452 CHECK_ELEM_DEFN(eq0
, warn
, warn_child_eq
, 0) /* ewarn_eq0() */
453 CHECK_ELEM_DEFN(ge1
, warn
, warn_child_gt
, 0) /* ewarn_gt1() */
454 CHECK_ELEM_DEFN(eq1
, err
, err_child_eq
, 1) /* eerr_eq1() */
455 CHECK_ELEM_DEFN(le2
, err
, err_child_lt
, 3) /* eerr_le2() */
456 CHECK_ELEM_DEFN(le1
, err
, err_child_lt
, 2) /* eerr_le1() */
457 CHECK_ELEM_DEFN(eq0
, err
, err_child_eq
, 0) /* eerr_eq0() */
458 CHECK_ELEM_DEFN(ge1
, err
, err_child_gt
, 0) /* eerr_ge1() */
459 CHECK_HEAD_DEFN(eq0
, err
, err_child_eq
, 0) /* herr_eq0() */
460 CHECK_HEAD_DEFN(le1
, warn
, warn_child_lt
, 2) /* hwarn_le1() */
461 CHECK_HEAD_DEFN(ge1
, err
, err_child_gt
, 0) /* herr_ge1() */
462 CHECK_HEAD_DEFN(eq1
, warn
, warn_child_eq
, 1) /* hwarn_eq1() */
466 check_stdarg(PRE_ARGS
)
469 if (MDOC_Std
== n
->data
.elem
.argv
[0].arg
&&
470 1 == n
->data
.elem
.argc
)
473 return(mdoc_nwarn(mdoc
, n
, WARN_COMPAT
,
474 "one argument suggested"));
479 check_sec(PRE_ARGS
, ...)
487 if (SEC_CUSTOM
== (sec
= va_arg(ap
, enum mdoc_sed
)))
489 if (sec
!= mdoc
->lastsec
)
496 return(mdoc_nwarn(mdoc
, n
, WARN_COMPAT
,
497 "inappropriate document section for macro"));
502 check_msec(PRE_ARGS
, ...)
509 if (0 == (msec
= va_arg(ap
, int)))
511 if (msec
!= mdoc
->meta
.msec
)
518 return(mdoc_nwarn(mdoc
, n
, WARN_COMPAT
,
519 "inappropriate manual section for macro"));
524 * Check over an argument. When this has more stuff in it, make this
525 * into a table-driven function; until then, a switch is fine.
528 check_argv(struct mdoc
*mdoc
,
529 const struct mdoc_node
*node
,
530 const struct mdoc_arg
*argv
)
539 * If the -std does not have an argument, then
540 * set it with the default name (if set). This
541 * only happens with MDOC_Ex.
545 assert(0 == argv
->sz
);
548 return(mdoc_nerr(mdoc
, node
,
549 "default name not yet set"));
563 check_text(struct mdoc
*mdoc
, int line
, int pos
, const char *p
)
567 /* XXX - indicate deprecated escapes \*(xx and \*x. */
570 if ( ! isprint((u_char
)*p
) && '\t' != *p
)
571 return(mdoc_perr(mdoc
, line
, pos
,
572 "invalid non-printing characters"));
575 if ((c
= mdoc_isescape(p
))) {
579 return(mdoc_perr(mdoc
, line
, pos
,
580 "invalid escape sequence"));
590 check_parent(PRE_ARGS
, int tok
, enum mdoc_type t
)
594 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
595 (t
== n
->parent
->type
))
598 return(mdoc_nerr(mdoc
, n
, "require parent %s",
599 MDOC_ROOT
== t
? "<root>" : mdoc_macronames
[tok
]));
605 pre_display(PRE_ARGS
)
607 struct mdoc_node
*node
;
609 /* Display elements (`Bd', `D1'...) cannot be nested. */
611 if (MDOC_BLOCK
!= n
->type
)
615 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
616 if (MDOC_BLOCK
== node
->type
)
617 if (MDOC_Bd
== node
->tok
)
622 return(mdoc_nerr(mdoc
, n
, "displays may not be nested"));
629 int type
, i
, width
, offset
;
630 struct mdoc_arg
*argv
;
633 if (MDOC_BLOCK
!= n
->type
)
636 argc
= n
->data
.block
.argc
;
638 /* Make sure that only one type of list is specified. */
640 type
= offset
= width
= -1;
643 for (i
= 0; i
< (int)argc
; i
++) {
644 argv
= &n
->data
.block
.argv
[i
];
672 return(mdoc_perr(mdoc
, argv
->line
, argv
->pos
,
673 "multiple types specified"));
679 return(mdoc_perr(mdoc
, argv
->line
, argv
->pos
,
680 "multiple -%s arguments",
681 mdoc_argnames
[MDOC_Width
]));
687 return(mdoc_perr(mdoc
, argv
->line
, argv
->pos
,
688 "multiple -%s arguments",
689 mdoc_argnames
[MDOC_Offset
]));
696 return(mdoc_err(mdoc
, "no type specified"));
708 return(mdoc_nwarn(mdoc
, n
, WARN_SYNTAX
,
709 "superfluous -%s argument",
710 mdoc_argnames
[MDOC_Width
]));
712 if (-1 == width
&& ! mdoc_nwarn(mdoc
, n
, WARN_SYNTAX
,
713 "suggest -%s argument",
714 mdoc_argnames
[MDOC_Width
]))
729 struct mdoc_arg
*argv
;
732 if (MDOC_BLOCK
!= n
->type
)
735 argc
= n
->data
.block
.argc
;
737 /* Make sure that only one type of display is specified. */
740 for (i
= 0, err
= type
= 0; ! err
&& i
< (int)argc
; i
++) {
741 argv
= &n
->data
.block
.argv
[i
];
746 case (MDOC_Unfilled
):
755 return(mdoc_perr(mdoc
, argv
->line
, argv
->pos
,
756 "multiple types specified"));
764 return(mdoc_err(mdoc
, "no type specified"));
772 if (MDOC_BLOCK
!= n
->type
)
774 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
782 if (MDOC_BLOCK
!= n
->type
)
784 return(check_parent(mdoc
, n
, -1, MDOC_ROOT
));
792 if (MDOC_BLOCK
!= n
->type
)
794 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
802 if (1 >= n
->data
.elem
.argc
)
804 return(mdoc_nerr(mdoc
, n
, "one argument allowed"));
812 return(check_sec(mdoc
, n
, SEC_LIBRARY
, SEC_CUSTOM
));
820 if ( ! check_msec(mdoc
, n
, 2, 3, 0))
822 return(check_stdarg(mdoc
, n
));
830 if ( ! check_msec(mdoc
, n
, 1, 6, 8, 0))
832 return(check_stdarg(mdoc
, n
));
840 return(check_msec(mdoc
, n
, 2, 0));
848 return(check_msec(mdoc
, n
, 4, 0));
853 pre_prologue(PRE_ARGS
)
856 return(check_sec(mdoc
, n
, SEC_PROLOGUE
, SEC_CUSTOM
));
864 if (0 == mdoc
->meta
.date
|| mdoc
->meta
.os
)
865 if ( ! mdoc_nwarn(mdoc
, n
, WARN_COMPAT
,
866 "out-of-order prologue"))
868 if (mdoc
->meta
.title
)
869 if ( ! mdoc_nwarn(mdoc
, n
, WARN_COMPAT
,
870 "prologue re-invoked"))
880 if (NULL
== mdoc
->meta
.title
|| 0 == mdoc
->meta
.date
)
881 if ( ! mdoc_nwarn(mdoc
, n
, WARN_COMPAT
,
882 "out-of-order prologue"))
885 if ( ! mdoc_nwarn(mdoc
, n
, WARN_COMPAT
,
886 "prologue re-invoked"))
896 if (mdoc
->meta
.title
|| mdoc
->meta
.os
)
897 if ( ! mdoc_nwarn(mdoc
, n
, WARN_COMPAT
,
898 "out-of-order prologue"))
901 if ( ! mdoc_nwarn(mdoc
, n
, WARN_COMPAT
,
902 "prologue re-invoked"))
912 struct mdoc_node
*head
;
914 if (MDOC_BLOCK
!= mdoc
->last
->type
)
917 head
= mdoc
->last
->data
.block
.head
;
919 if (0 == mdoc
->last
->data
.block
.argc
) {
920 if (NULL
== head
->child
)
921 return(mdoc_err(mdoc
, "argument expected"));
923 p
= head
->child
->data
.text
.string
;
924 if (xstrcmp(p
, "Em"))
926 else if (xstrcmp(p
, "Li"))
928 else if (xstrcmp(p
, "Sm"))
930 return(mdoc_nerr(mdoc
, head
->child
, "invalid font"));
934 return(mdoc_err(mdoc
, "argument expected"));
936 if (1 == mdoc
->last
->data
.block
.argc
)
938 return(mdoc_err(mdoc
, "argument expected"));
946 if (mdoc
->last
->child
)
950 return(mdoc_err(mdoc
, "not yet invoked with name"));
958 if (NULL
== mdoc
->last
->child
)
960 if (mdoc_a2att(mdoc
->last
->child
->data
.text
.string
))
962 return(mdoc_err(mdoc
, "require valid AT&T symbol"));
970 if (0 != mdoc
->last
->data
.elem
.argc
) {
971 if (NULL
== mdoc
->last
->child
)
973 return(mdoc_err(mdoc
, "argument(s) expected"));
976 if (mdoc
->last
->child
)
978 return(mdoc_err(mdoc
, "argument(s) expected"));
986 if (0 == mdoc
->last
->data
.elem
.argc
) {
987 if (mdoc
->last
->child
)
989 return(mdoc_err(mdoc
, "argument(s) expected"));
991 if (mdoc
->last
->child
)
992 return(mdoc_err(mdoc
, "argument(s) expected"));
993 if (1 != mdoc
->last
->data
.elem
.argc
)
994 return(mdoc_err(mdoc
, "argument(s) expected"));
995 if (MDOC_Std
!= mdoc
->last
->data
.elem
.argv
[0].arg
)
996 return(mdoc_err(mdoc
, "argument(s) expected"));
1006 #define TYPE_NONE (0)
1007 #define TYPE_BODY (1)
1008 #define TYPE_HEAD (2)
1009 #define TYPE_OHEAD (3)
1011 struct mdoc_node
*n
;
1013 if (MDOC_BLOCK
!= mdoc
->last
->type
)
1016 n
= mdoc
->last
->parent
->parent
;
1018 argc
= n
->data
.block
.argc
;
1022 /* Some types require block-head, some not. */
1025 for (i
= 0; TYPE_NONE
== type
&& i
< (int)argc
; i
++)
1026 switch (n
->data
.block
.argv
[i
].arg
) {
1037 sv
= n
->data
.block
.argv
[i
].arg
;
1049 sv
= n
->data
.block
.argv
[i
].arg
;
1053 sv
= n
->data
.block
.argv
[i
].arg
;
1059 assert(TYPE_NONE
!= type
);
1061 n
= mdoc
->last
->data
.block
.head
;
1063 if (TYPE_HEAD
== type
) {
1064 if (NULL
== n
->child
)
1065 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1066 "argument(s) suggested"))
1069 n
= mdoc
->last
->data
.block
.body
;
1070 if (NULL
== n
->child
)
1071 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1072 "multiline body suggested"))
1075 } else if (TYPE_BODY
== type
) {
1077 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1078 "no argument suggested"))
1081 n
= mdoc
->last
->data
.block
.body
;
1082 if (NULL
== n
->child
)
1083 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1084 "multiline body suggested"))
1087 if (NULL
== n
->child
)
1088 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1089 "argument(s) suggested"))
1092 n
= mdoc
->last
->data
.block
.body
;
1094 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1095 "no multiline body suggested"))
1099 if (MDOC_Column
!= sv
)
1102 argc
= mdoc
->last
->parent
->parent
->data
.block
.argv
->sz
;
1103 n
= mdoc
->last
->data
.block
.head
->child
;
1105 for (i
= 0; n
; n
= n
->next
)
1111 return(mdoc_err(mdoc
, "need %zu columns (have %d)", argc
, i
));
1122 struct mdoc_node
*n
;
1124 if (MDOC_BODY
!= mdoc
->last
->type
)
1126 if (NULL
== (mdoc
->last
->child
))
1130 * Only allow `It' macros to be the immediate descendants of the
1135 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1136 if (MDOC_BLOCK
== n
->type
)
1137 if (MDOC_It
== n
->tok
)
1140 return(mdoc_nerr(mdoc
, n
, "bad child of parent %s",
1141 mdoc_macronames
[mdoc
->last
->tok
]));
1149 ebool(struct mdoc
*mdoc
)
1151 struct mdoc_node
*n
;
1154 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1155 if (MDOC_TEXT
!= n
->type
)
1157 if (xstrcmp(n
->data
.text
.string
, "on"))
1159 if (xstrcmp(n
->data
.text
.string
, "off"))
1166 return(mdoc_nerr(mdoc
, n
, "expected boolean"));
1171 post_root(POST_ARGS
)
1174 if (NULL
== mdoc
->first
->child
)
1175 return(mdoc_err(mdoc
, "document lacks data"));
1176 if (SEC_PROLOGUE
== mdoc
->lastnamed
)
1177 return(mdoc_err(mdoc
, "document lacks prologue"));
1179 if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1180 return(mdoc_err(mdoc
, "lacking post-prologue %s",
1181 mdoc_macronames
[MDOC_Sh
]));
1182 if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1183 return(mdoc_err(mdoc
, "lacking post-prologue %s",
1184 mdoc_macronames
[MDOC_Sh
]));
1194 if (mdoc_a2st(mdoc
->last
->child
->data
.text
.string
))
1197 return(mdoc_warn(mdoc
, WARN_SYNTAX
, "invalid standard"));
1205 if (MDOC_HEAD
== mdoc
->last
->type
)
1206 return(post_sh_head(mdoc
));
1207 if (MDOC_BODY
== mdoc
->last
->type
)
1208 return(post_sh_body(mdoc
));
1215 post_sh_body(POST_ARGS
)
1217 struct mdoc_node
*n
;
1219 if (SEC_NAME
!= mdoc
->lastnamed
)
1223 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1224 * macros (can have multiple `Nm' and one `Nd'). Note that the
1225 * children of the BODY declaration can also be "text".
1228 if (NULL
== (n
= mdoc
->last
->child
))
1229 return(mdoc_warn(mdoc
, WARN_SYNTAX
,
1230 "section should have %s and %s",
1231 mdoc_macronames
[MDOC_Nm
],
1232 mdoc_macronames
[MDOC_Nd
]));
1234 for ( ; n
&& n
->next
; n
= n
->next
) {
1235 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1237 if (MDOC_TEXT
== n
->type
)
1239 if ( ! (mdoc_nwarn(mdoc
, n
, WARN_SYNTAX
,
1240 "section should have %s first",
1241 mdoc_macronames
[MDOC_Nm
])))
1245 if (MDOC_ELEM
== n
->type
&& MDOC_Nd
== n
->tok
)
1248 return(mdoc_warn(mdoc
, WARN_SYNTAX
,
1249 "section should have %s last",
1250 mdoc_macronames
[MDOC_Nd
]));
1255 post_sh_head(POST_ARGS
)
1261 * Process a new section. Sections are either "named" or
1262 * "custom"; custom sections are user-defined, while named ones
1263 * usually follow a conventional order and may only appear in
1264 * certain manual sections.
1267 assert(MDOC_Sh
== mdoc
->last
->tok
);
1269 (void)xstrlcpys(buf
, mdoc
->last
->child
, sizeof(buf
));
1271 sec
= mdoc_atosec(buf
);
1273 /* The NAME section should always be first. */
1275 if (SEC_BODY
== mdoc
->lastnamed
&& SEC_NAME
!= sec
)
1276 return(mdoc_warn(mdoc
, WARN_SYNTAX
,
1277 "section NAME should be first"));
1278 if (SEC_CUSTOM
== sec
)
1281 /* Check for repeated or out-of-order sections. */
1283 if (sec
== mdoc
->lastnamed
)
1284 return(mdoc_warn(mdoc
, WARN_SYNTAX
,
1285 "section repeated"));
1286 if (sec
< mdoc
->lastnamed
)
1287 return(mdoc_warn(mdoc
, WARN_SYNTAX
,
1288 "section out of conventional order"));
1290 /* Check particular section/manual section conventions. */
1294 switch (mdoc
->meta
.msec
) {
1300 return(mdoc_warn(mdoc
, WARN_COMPAT
,
1301 "section in wrong manual section"));
1316 return(check_sec(mdoc
, n
, SEC_SYNOPSIS
, SEC_CUSTOM
));