]>
git.cameronkatri.com Git - mandoc.git/blob - validate.c
1 /* $Id: validate.c,v 1.68 2009/03/05 13:12:12 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.
25 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
28 * Pre- and post-validate macros as they're parsed. Pre-validation
29 * occurs when the macro has been detected and its arguments parsed.
30 * Post-validation occurs when all child macros have also been parsed.
31 * In the ELEMENT case, this is simply the parameters of the macro; in
32 * the BLOCK case, this is the HEAD, BODY, TAIL and so on.
35 #define PRE_ARGS struct mdoc *mdoc, const struct mdoc_node *n
36 #define POST_ARGS struct mdoc *mdoc
38 typedef int (*v_pre
)(PRE_ARGS
);
39 typedef int (*v_post
)(POST_ARGS
);
41 /* TODO: ignoring Pp (it's superfluous in some invocations). */
50 static int check_parent(PRE_ARGS
, int, enum mdoc_type
);
51 static int check_msec(PRE_ARGS
, int, enum mdoc_msec
*);
52 static int check_stdarg(PRE_ARGS
);
54 static int check_text(struct mdoc
*,
55 int, int, const char *);
56 static int check_argv(struct mdoc
*,
57 const struct mdoc_node
*,
58 const struct mdoc_arg
*);
60 static int err_child_lt(struct mdoc
*, const char *, int);
61 static int warn_child_lt(struct mdoc
*, const char *, int);
62 static int err_child_gt(struct mdoc
*, const char *, int);
63 static int warn_child_gt(struct mdoc
*, const char *, int);
64 static int err_child_eq(struct mdoc
*, const char *, int);
65 static int warn_child_eq(struct mdoc
*, const char *, int);
67 /* Utility auxiliaries. */
69 static inline int count_child(struct mdoc
*);
70 static inline int warn_count(struct mdoc
*, const char *,
71 int, const char *, int);
72 static inline int err_count(struct mdoc
*, const char *,
73 int, const char *, int);
75 /* Specific pre-child-parse routines. */
77 static int pre_display(PRE_ARGS
);
78 static int pre_sh(PRE_ARGS
);
79 static int pre_ss(PRE_ARGS
);
80 static int pre_bd(PRE_ARGS
);
81 static int pre_bl(PRE_ARGS
);
82 static int pre_it(PRE_ARGS
);
83 static int pre_cd(PRE_ARGS
);
84 static int pre_er(PRE_ARGS
);
85 static int pre_ex(PRE_ARGS
);
86 static int pre_rv(PRE_ARGS
);
87 static int pre_an(PRE_ARGS
);
88 static int pre_st(PRE_ARGS
);
89 static int pre_prologue(PRE_ARGS
);
90 static int pre_prologue(PRE_ARGS
);
91 static int pre_prologue(PRE_ARGS
);
93 /* Specific post-child-parse routines. */
95 static int herr_ge1(POST_ARGS
);
96 static int hwarn_le1(POST_ARGS
);
97 static int herr_eq0(POST_ARGS
);
98 static int eerr_eq0(POST_ARGS
);
99 static int eerr_le1(POST_ARGS
);
100 static int eerr_le2(POST_ARGS
);
101 static int eerr_eq1(POST_ARGS
);
102 static int eerr_ge1(POST_ARGS
);
103 static int ewarn_eq0(POST_ARGS
);
104 static int ewarn_eq1(POST_ARGS
);
105 static int bwarn_ge1(POST_ARGS
);
106 static int hwarn_eq1(POST_ARGS
);
107 static int ewarn_ge1(POST_ARGS
);
108 static int ebool(POST_ARGS
);
110 static int post_sh(POST_ARGS
);
111 static int post_sh_body(POST_ARGS
);
112 static int post_sh_head(POST_ARGS
);
113 static int post_fd(POST_ARGS
);
114 static int post_bl(POST_ARGS
);
115 static int post_it(POST_ARGS
);
116 static int post_ex(POST_ARGS
);
117 static int post_an(POST_ARGS
);
118 static int post_at(POST_ARGS
);
119 static int post_xr(POST_ARGS
);
120 static int post_nm(POST_ARGS
);
121 static int post_bf(POST_ARGS
);
122 static int post_root(POST_ARGS
);
124 /* Collections of pre-child-parse routines. */
126 static v_pre pres_prologue
[] = { pre_prologue
, NULL
};
127 static v_pre pres_d1
[] = { pre_display
, NULL
};
128 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
129 static v_pre pres_bl
[] = { pre_bl
, NULL
};
130 static v_pre pres_it
[] = { pre_it
, NULL
};
131 static v_pre pres_ss
[] = { pre_ss
, NULL
};
132 static v_pre pres_sh
[] = { pre_sh
, NULL
};
133 static v_pre pres_cd
[] = { pre_cd
, NULL
};
134 static v_pre pres_er
[] = { pre_er
, NULL
};
135 static v_pre pres_ex
[] = { pre_ex
, NULL
};
136 static v_pre pres_rv
[] = { pre_rv
, NULL
};
137 static v_pre pres_an
[] = { pre_an
, NULL
};
138 static v_pre pres_st
[] = { pre_st
, NULL
};
140 /* Collections of post-child-parse routines. */
142 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
143 static v_post posts_bd
[] = { herr_eq0
, bwarn_ge1
, NULL
};
144 static v_post posts_text
[] = { eerr_ge1
, NULL
};
145 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
146 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
147 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
148 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
149 static v_post posts_bl
[] = { herr_eq0
, bwarn_ge1
, post_bl
, NULL
};
150 static v_post posts_it
[] = { post_it
, NULL
};
151 static v_post posts_in
[] = { ewarn_eq1
, NULL
};
152 static v_post posts_ss
[] = { herr_ge1
, NULL
};
153 static v_post posts_pf
[] = { eerr_eq1
, NULL
};
154 static v_post posts_pp
[] = { ewarn_eq0
, NULL
};
155 static v_post posts_ex
[] = { eerr_le1
, post_ex
, NULL
};
156 static v_post posts_an
[] = { post_an
, NULL
};
157 static v_post posts_at
[] = { post_at
, NULL
};
158 static v_post posts_xr
[] = { eerr_ge1
, eerr_le2
, post_xr
, NULL
};
159 static v_post posts_nm
[] = { post_nm
, NULL
};
160 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
161 static v_post posts_rs
[] = { herr_eq0
, bwarn_ge1
, NULL
};
162 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
163 static v_post posts_bk
[] = { herr_eq0
, bwarn_ge1
, NULL
};
164 static v_post posts_fd
[] = { ewarn_ge1
, post_fd
, NULL
};
166 /* Per-macro pre- and post-child-check routine collections. */
168 const struct valids mdoc_valids
[MDOC_MAX
] = {
169 { NULL
, NULL
}, /* \" */
170 { pres_prologue
, posts_text
}, /* Dd */
171 { pres_prologue
, NULL
}, /* Dt */
172 { pres_prologue
, NULL
}, /* Os */
173 { pres_sh
, posts_sh
}, /* Sh */
174 { pres_ss
, posts_ss
}, /* Ss */
175 { NULL
, posts_pp
}, /* Pp */
176 { pres_d1
, posts_wline
}, /* D1 */
177 { pres_d1
, posts_wline
}, /* Dl */
178 { pres_bd
, posts_bd
}, /* Bd */
179 { NULL
, NULL
}, /* Ed */
180 { pres_bl
, posts_bl
}, /* Bl */
181 { NULL
, NULL
}, /* El */
182 { pres_it
, posts_it
}, /* It */
183 { NULL
, posts_text
}, /* Ad */
184 { pres_an
, posts_an
}, /* An */
185 { NULL
, NULL
}, /* Ar */
186 { pres_cd
, posts_text
}, /* Cd */
187 { NULL
, NULL
}, /* Cm */
188 { NULL
, posts_text
}, /* Dv */
189 { pres_er
, posts_text
}, /* Er */
190 { NULL
, posts_text
}, /* Ev */
191 { pres_ex
, posts_ex
}, /* Ex */
192 { NULL
, posts_text
}, /* Fa */
193 { NULL
, posts_fd
}, /* Fd */
194 { NULL
, NULL
}, /* Fl */
195 { NULL
, posts_text
}, /* Fn */
196 { NULL
, posts_wtext
}, /* Ft */
197 { NULL
, posts_text
}, /* Ic */
198 { NULL
, posts_in
}, /* In */
199 { NULL
, posts_text
}, /* Li */
200 { NULL
, posts_wtext
}, /* Nd */
201 { NULL
, posts_nm
}, /* Nm */
202 { NULL
, posts_wline
}, /* Op */
203 { NULL
, NULL
}, /* Ot */
204 { NULL
, NULL
}, /* Pa */
205 { pres_rv
, posts_notext
}, /* Rv */
206 { pres_st
, posts_notext
}, /* St */
207 { NULL
, posts_text
}, /* Va */
208 { NULL
, posts_text
}, /* Vt */
209 { NULL
, posts_xr
}, /* Xr */
210 { NULL
, posts_text
}, /* %A */
211 { NULL
, posts_text
}, /* %B */
212 { NULL
, posts_text
}, /* %D */
213 { NULL
, posts_text
}, /* %I */
214 { NULL
, posts_text
}, /* %J */
215 { NULL
, posts_text
}, /* %N */
216 { NULL
, posts_text
}, /* %O */
217 { NULL
, posts_text
}, /* %P */
218 { NULL
, posts_text
}, /* %R */
219 { NULL
, posts_text
}, /* %T */
220 { NULL
, posts_text
}, /* %V */
221 { NULL
, NULL
}, /* Ac */
222 { NULL
, NULL
}, /* Ao */
223 { NULL
, posts_wline
}, /* Aq */
224 { NULL
, posts_at
}, /* At */
225 { NULL
, NULL
}, /* Bc */
226 { NULL
, posts_bf
}, /* Bf */
227 { NULL
, NULL
}, /* Bo */
228 { NULL
, posts_wline
}, /* Bq */
229 { NULL
, NULL
}, /* Bsx */
230 { NULL
, NULL
}, /* Bx */
231 { NULL
, posts_bool
}, /* Db */
232 { NULL
, NULL
}, /* Dc */
233 { NULL
, NULL
}, /* Do */
234 { NULL
, posts_wline
}, /* Dq */
235 { NULL
, NULL
}, /* Ec */
236 { NULL
, NULL
}, /* Ef */
237 { NULL
, posts_text
}, /* Em */
238 { NULL
, NULL
}, /* Eo */
239 { NULL
, NULL
}, /* Fx */
240 { NULL
, posts_text
}, /* Ms */
241 { NULL
, posts_notext
}, /* No */
242 { NULL
, posts_notext
}, /* Ns */
243 { NULL
, NULL
}, /* Nx */
244 { NULL
, NULL
}, /* Ox */
245 { NULL
, NULL
}, /* Pc */
246 { NULL
, posts_pf
}, /* Pf */
247 { NULL
, NULL
}, /* Po */
248 { NULL
, posts_wline
}, /* Pq */
249 { NULL
, NULL
}, /* Qc */
250 { NULL
, posts_wline
}, /* Ql */
251 { NULL
, NULL
}, /* Qo */
252 { NULL
, posts_wline
}, /* Qq */
253 { NULL
, NULL
}, /* Re */
254 { NULL
, posts_rs
}, /* Rs */
255 { NULL
, NULL
}, /* Sc */
256 { NULL
, NULL
}, /* So */
257 { NULL
, posts_wline
}, /* Sq */
258 { NULL
, posts_bool
}, /* Sm */
259 { NULL
, posts_text
}, /* Sx */
260 { NULL
, posts_text
}, /* Sy */
261 { NULL
, posts_text
}, /* Tn */
262 { NULL
, NULL
}, /* Ux */
263 { NULL
, NULL
}, /* Xc */
264 { NULL
, NULL
}, /* Xo */
265 { NULL
, posts_fo
}, /* Fo */
266 { NULL
, NULL
}, /* Fc */
267 { NULL
, NULL
}, /* Oo */
268 { NULL
, NULL
}, /* Oc */
269 { NULL
, posts_bk
}, /* Bk */
270 { NULL
, NULL
}, /* Ek */
271 { NULL
, posts_notext
}, /* Bt */
272 { NULL
, NULL
}, /* Hf */
273 { NULL
, NULL
}, /* Fr */
274 { NULL
, posts_notext
}, /* Ud */
279 mdoc_valid_pre(struct mdoc
*mdoc
,
280 const struct mdoc_node
*node
)
283 struct mdoc_arg
*argv
;
288 if (MDOC_TEXT
== node
->type
) {
289 tp
= node
->data
.text
.string
;
292 return(check_text(mdoc
, line
, pos
, tp
));
295 if (MDOC_BLOCK
== node
->type
|| MDOC_ELEM
== node
->type
) {
296 argv
= MDOC_BLOCK
== node
->type
?
297 node
->data
.block
.argv
:
298 node
->data
.elem
.argv
;
299 argc
= MDOC_BLOCK
== node
->type
?
300 node
->data
.block
.argc
:
301 node
->data
.elem
.argc
;
303 for (i
= 0; i
< (int)argc
; i
++) {
304 for (j
= 0; j
< (int)argv
[i
].sz
; j
++) {
305 tp
= argv
[i
].value
[j
];
308 if ( ! check_text(mdoc
, line
, pos
, tp
))
311 if ( ! check_argv(mdoc
, node
, &argv
[i
]))
316 if (NULL
== mdoc_valids
[node
->tok
].pre
)
318 for (p
= mdoc_valids
[node
->tok
].pre
; *p
; p
++)
319 if ( ! (*p
)(mdoc
, node
))
326 mdoc_valid_post(struct mdoc
*mdoc
)
331 * This check occurs after the macro's children have been filled
332 * in: postfix validation. Since this happens when we're
333 * rewinding the scope tree, it's possible to have multiple
334 * invocations (as by design, for now), we set bit MDOC_VALID to
335 * indicate that we've validated.
338 if (MDOC_VALID
& mdoc
->last
->flags
)
340 mdoc
->last
->flags
|= MDOC_VALID
;
342 if (MDOC_TEXT
== mdoc
->last
->type
)
344 if (MDOC_ROOT
== mdoc
->last
->type
)
345 return(post_root(mdoc
));
347 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
349 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
359 warn_count(struct mdoc
*m
, const char *k
,
360 int want
, const char *v
, int has
)
363 return(mdoc_warn(m
, WARN_SYNTAX
,
364 "suggests %s %s %d (has %d)",
370 err_count(struct mdoc
*m
, const char *k
,
371 int want
, const char *v
, int has
)
374 return(mdoc_err(m
, "requires %s %s %d (has %d)",
380 count_child(struct mdoc
*mdoc
)
385 for (i
= 0, n
= mdoc
->last
->child
; n
; n
= n
->next
, i
++)
393 * Build these up with macros because they're basically the same check
394 * for different inequalities. Yes, this could be done with functions,
395 * but this is reasonable for now.
398 #define CHECK_CHILD_DEFN(lvl, name, ineq) \
400 lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
403 if ((i = count_child(mdoc)) ineq sz) \
405 return(lvl##_count(mdoc, #ineq, sz, p, i)); \
408 #define CHECK_BODY_DEFN(name, lvl, func, num) \
410 b##lvl##_##name(POST_ARGS) \
412 if (MDOC_BODY != mdoc->last->type) \
414 return(func(mdoc, "multiline parameters", (num))); \
417 #define CHECK_ELEM_DEFN(name, lvl, func, num) \
419 e##lvl##_##name(POST_ARGS) \
421 assert(MDOC_ELEM == mdoc->last->type); \
422 return(func(mdoc, "line parameters", (num))); \
425 #define CHECK_HEAD_DEFN(name, lvl, func, num) \
427 h##lvl##_##name(POST_ARGS) \
429 if (MDOC_HEAD != mdoc->last->type) \
431 return(func(mdoc, "line parameters", (num))); \
435 CHECK_CHILD_DEFN(warn
, gt
, >) /* warn_child_gt() */
436 CHECK_CHILD_DEFN(err
, gt
, >) /* err_child_gt() */
437 CHECK_CHILD_DEFN(warn
, eq
, ==) /* warn_child_eq() */
438 CHECK_CHILD_DEFN(err
, eq
, ==) /* err_child_eq() */
439 CHECK_CHILD_DEFN(err
, lt
, <) /* err_child_lt() */
440 CHECK_CHILD_DEFN(warn
, lt
, <) /* warn_child_lt() */
441 CHECK_BODY_DEFN(ge1
, warn
, warn_child_gt
, 0) /* bwarn_ge1() */
442 CHECK_ELEM_DEFN(eq1
, warn
, warn_child_eq
, 1) /* ewarn_eq1() */
443 CHECK_ELEM_DEFN(eq0
, warn
, warn_child_eq
, 0) /* ewarn_eq0() */
444 CHECK_ELEM_DEFN(ge1
, warn
, warn_child_gt
, 0) /* ewarn_gt1() */
445 CHECK_ELEM_DEFN(eq1
, err
, err_child_eq
, 1) /* eerr_eq1() */
446 CHECK_ELEM_DEFN(le2
, err
, err_child_lt
, 3) /* eerr_le2() */
447 CHECK_ELEM_DEFN(le1
, err
, err_child_lt
, 2) /* eerr_le1() */
448 CHECK_ELEM_DEFN(eq0
, err
, err_child_eq
, 0) /* eerr_eq0() */
449 CHECK_ELEM_DEFN(ge1
, err
, err_child_gt
, 0) /* eerr_ge1() */
450 CHECK_HEAD_DEFN(eq0
, err
, err_child_eq
, 0) /* herr_eq0() */
451 CHECK_HEAD_DEFN(le1
, warn
, warn_child_lt
, 2) /* hwarn_le1() */
452 CHECK_HEAD_DEFN(ge1
, err
, err_child_gt
, 0) /* herr_ge1() */
453 CHECK_HEAD_DEFN(eq1
, warn
, warn_child_eq
, 1) /* hwarn_eq1() */
457 check_stdarg(PRE_ARGS
)
460 if (MDOC_Std
== n
->data
.elem
.argv
[0].arg
&&
461 1 == n
->data
.elem
.argc
)
464 return(mdoc_nwarn(mdoc
, n
, WARN_COMPAT
,
465 "one argument suggested"));
470 check_msec(PRE_ARGS
, int sz
, enum mdoc_msec
*msecs
)
474 for (i
= 0; i
< sz
; i
++)
475 if (msecs
[i
] == mdoc
->meta
.msec
)
477 return(mdoc_nwarn(mdoc
, n
, WARN_COMPAT
,
478 "invalid manual section"));
483 * Check over an argument. When this has more stuff in it, make this
484 * into a table-driven function; until then, a switch is fine.
487 check_argv(struct mdoc
*mdoc
,
488 const struct mdoc_node
*node
,
489 const struct mdoc_arg
*argv
)
498 * If the -std does not have an argument, then
499 * set it with the default name (if set). This
500 * only happens with MDOC_Ex.
504 assert(0 == argv
->sz
);
507 return(mdoc_nerr(mdoc
, node
,
508 "default name not yet set"));
522 check_text(struct mdoc
*mdoc
, int line
, int pos
, const char *p
)
526 /* XXX - indicate deprecated escapes \*(xx and \*x. */
529 if ( ! isprint((u_char
)*p
) && '\t' != *p
)
530 return(mdoc_perr(mdoc
, line
, pos
,
531 "invalid non-printing characters"));
534 if ((c
= mdoc_isescape(p
))) {
538 return(mdoc_perr(mdoc
, line
, pos
,
539 "invalid escape sequence"));
549 check_parent(PRE_ARGS
, int tok
, enum mdoc_type t
)
553 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
554 (t
== n
->parent
->type
))
557 return(mdoc_nerr(mdoc
, n
, "require parent %s",
558 MDOC_ROOT
== t
? "<root>" : mdoc_macronames
[tok
]));
564 pre_display(PRE_ARGS
)
566 struct mdoc_node
*node
;
568 /* Display elements (`Bd', `D1'...) cannot be nested. */
570 if (MDOC_BLOCK
!= n
->type
)
574 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
575 if (MDOC_BLOCK
== node
->type
)
576 if (MDOC_Bd
== node
->tok
)
581 return(mdoc_nerr(mdoc
, n
, "displays may not be nested"));
588 int type
, i
, width
, offset
;
589 struct mdoc_arg
*argv
;
592 if (MDOC_BLOCK
!= n
->type
)
595 argc
= n
->data
.block
.argc
;
597 /* Make sure that only one type of list is specified. */
599 type
= offset
= width
= -1;
602 for (i
= 0; i
< (int)argc
; i
++) {
603 argv
= &n
->data
.block
.argv
[i
];
631 return(mdoc_perr(mdoc
, argv
->line
, argv
->pos
,
632 "multiple types specified"));
638 return(mdoc_perr(mdoc
, argv
->line
, argv
->pos
,
639 "multiple -%s arguments",
640 mdoc_argnames
[MDOC_Width
]));
646 return(mdoc_perr(mdoc
, argv
->line
, argv
->pos
,
647 "multiple -%s arguments",
648 mdoc_argnames
[MDOC_Offset
]));
655 return(mdoc_err(mdoc
, "no type specified"));
667 return(mdoc_nwarn(mdoc
, n
, WARN_SYNTAX
,
668 "superfluous -%s argument",
669 mdoc_argnames
[MDOC_Width
]));
671 if (-1 == width
&& ! mdoc_nwarn(mdoc
, n
, WARN_SYNTAX
,
672 "suggest -%s argument",
673 mdoc_argnames
[MDOC_Width
]))
688 struct mdoc_arg
*argv
;
691 if (MDOC_BLOCK
!= n
->type
)
694 argc
= n
->data
.block
.argc
;
696 /* Make sure that only one type of display is specified. */
699 for (i
= 0, err
= type
= 0; ! err
&& i
< (int)argc
; i
++) {
700 argv
= &n
->data
.block
.argv
[i
];
705 case (MDOC_Unfilled
):
714 return(mdoc_perr(mdoc
, argv
->line
, argv
->pos
,
715 "multiple types specified"));
723 return(mdoc_err(mdoc
, "no type specified"));
731 if (MDOC_BLOCK
!= n
->type
)
733 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
741 if (MDOC_BLOCK
!= n
->type
)
743 return(check_parent(mdoc
, n
, -1, MDOC_ROOT
));
751 if (MDOC_BLOCK
!= n
->type
)
753 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
761 if (1 == n
->data
.elem
.argc
)
763 return(mdoc_nerr(mdoc
, n
, "one argument required"));
771 if (1 >= n
->data
.elem
.argc
)
773 return(mdoc_nerr(mdoc
, n
, "one argument allowed"));
780 enum mdoc_msec msecs
[] = { MSEC_2
, MSEC_3
};
782 if ( ! check_msec(mdoc
, n
, 2, msecs
))
784 return(check_stdarg(mdoc
, n
));
791 enum mdoc_msec msecs
[] = { MSEC_1
, MSEC_6
, MSEC_8
};
793 if ( ! check_msec(mdoc
, n
, 3, msecs
))
795 return(check_stdarg(mdoc
, n
));
802 enum mdoc_msec msecs
[] = { MSEC_2
};
804 return(check_msec(mdoc
, n
, 1, msecs
));
811 enum mdoc_msec msecs
[] = { MSEC_4
};
813 return(check_msec(mdoc
, n
, 1, msecs
));
818 pre_prologue(PRE_ARGS
)
821 if (SEC_PROLOGUE
!= mdoc
->lastnamed
)
822 return(mdoc_nerr(mdoc
, n
, "prologue only"));
824 /* Check for ordering. */
828 if (mdoc
->meta
.title
&& mdoc
->meta
.date
)
830 return(mdoc_nerr(mdoc
, n
, "prologue out-of-order"));
832 if (NULL
== mdoc
->meta
.title
&& mdoc
->meta
.date
)
834 return(mdoc_nerr(mdoc
, n
, "prologue out-of-order"));
836 if (NULL
== mdoc
->meta
.title
&& 0 == mdoc
->meta
.date
)
838 return(mdoc_nerr(mdoc
, n
, "prologue out-of-order"));
844 /* Check for repetition. */
848 if (NULL
== mdoc
->meta
.os
)
852 if (0 == mdoc
->meta
.date
)
856 if (NULL
== mdoc
->meta
.title
)
864 return(mdoc_nerr(mdoc
, n
, "prologue repetition"));
872 struct mdoc_node
*head
;
874 if (MDOC_BLOCK
!= mdoc
->last
->type
)
877 head
= mdoc
->last
->data
.block
.head
;
879 if (0 == mdoc
->last
->data
.block
.argc
) {
880 if (NULL
== head
->child
)
881 return(mdoc_err(mdoc
, "argument expected"));
883 p
= head
->child
->data
.text
.string
;
884 if (xstrcmp(p
, "Em"))
886 else if (xstrcmp(p
, "Li"))
888 else if (xstrcmp(p
, "Sm"))
890 return(mdoc_nerr(mdoc
, head
->child
, "invalid font"));
894 return(mdoc_err(mdoc
, "argument expected"));
896 if (1 == mdoc
->last
->data
.block
.argc
)
898 return(mdoc_err(mdoc
, "argument expected"));
906 if (mdoc
->last
->child
)
910 return(mdoc_err(mdoc
, "not yet invoked with name"));
919 if (NULL
== (n
= mdoc
->last
->child
->next
))
921 if (MSEC_DEFAULT
!= mdoc_atomsec(n
->data
.text
.string
))
923 return(mdoc_nerr(mdoc
, n
, "invalid manual section"));
931 if (NULL
== mdoc
->last
->child
)
933 if (ATT_DEFAULT
!= mdoc_atoatt(mdoc
->last
->child
->data
.text
.string
))
935 return(mdoc_err(mdoc
, "require valid symbol"));
943 if (0 != mdoc
->last
->data
.elem
.argc
) {
944 if (NULL
== mdoc
->last
->child
)
946 return(mdoc_err(mdoc
, "argument(s) expected"));
949 if (mdoc
->last
->child
)
951 return(mdoc_err(mdoc
, "argument(s) expected"));
959 if (0 == mdoc
->last
->data
.elem
.argc
) {
960 if (mdoc
->last
->child
)
962 return(mdoc_err(mdoc
, "argument(s) expected"));
964 if (mdoc
->last
->child
)
965 return(mdoc_err(mdoc
, "argument(s) expected"));
966 if (1 != mdoc
->last
->data
.elem
.argc
)
967 return(mdoc_err(mdoc
, "argument(s) expected"));
968 if (MDOC_Std
!= mdoc
->last
->data
.elem
.argv
[0].arg
)
969 return(mdoc_err(mdoc
, "argument(s) expected"));
979 #define TYPE_NONE (0)
980 #define TYPE_BODY (1)
981 #define TYPE_HEAD (2)
982 #define TYPE_OHEAD (3)
986 if (MDOC_BLOCK
!= mdoc
->last
->type
)
989 n
= mdoc
->last
->parent
->parent
;
991 argc
= n
->data
.block
.argc
;
995 /* Some types require block-head, some not. */
998 for (i
= 0; TYPE_NONE
== type
&& i
< (int)argc
; i
++)
999 switch (n
->data
.block
.argv
[i
].arg
) {
1010 sv
= n
->data
.block
.argv
[i
].arg
;
1022 sv
= n
->data
.block
.argv
[i
].arg
;
1026 sv
= n
->data
.block
.argv
[i
].arg
;
1032 assert(TYPE_NONE
!= type
);
1034 n
= mdoc
->last
->data
.block
.head
;
1036 if (TYPE_HEAD
== type
) {
1037 if (NULL
== n
->child
)
1038 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1039 "argument(s) suggested"))
1042 n
= mdoc
->last
->data
.block
.body
;
1043 if (NULL
== n
->child
)
1044 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1045 "multiline body suggested"))
1048 } else if (TYPE_BODY
== type
) {
1050 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1051 "no argument suggested"))
1054 n
= mdoc
->last
->data
.block
.body
;
1055 if (NULL
== n
->child
)
1056 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1057 "multiline body suggested"))
1060 if (NULL
== n
->child
)
1061 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1062 "argument(s) suggested"))
1065 n
= mdoc
->last
->data
.block
.body
;
1067 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1068 "no multiline body suggested"))
1072 if (MDOC_Column
!= sv
)
1075 argc
= mdoc
->last
->parent
->parent
->data
.block
.argv
->sz
;
1076 n
= mdoc
->last
->data
.block
.head
->child
;
1078 for (i
= 0; n
; n
= n
->next
)
1084 return(mdoc_err(mdoc
, "need %zu columns (have %d)", argc
, i
));
1095 struct mdoc_node
*n
;
1097 if (MDOC_BODY
!= mdoc
->last
->type
)
1099 if (NULL
== (mdoc
->last
->child
))
1103 * Only allow `It' macros to be the immediate descendants of the
1108 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1109 if (MDOC_BLOCK
== n
->type
)
1110 if (MDOC_It
== n
->tok
)
1113 return(mdoc_nerr(mdoc
, n
, "bad child of parent %s",
1114 mdoc_macronames
[mdoc
->last
->tok
]));
1122 ebool(struct mdoc
*mdoc
)
1124 struct mdoc_node
*n
;
1127 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1128 if (MDOC_TEXT
!= n
->type
)
1130 if (xstrcmp(n
->data
.text
.string
, "on"))
1132 if (xstrcmp(n
->data
.text
.string
, "off"))
1139 return(mdoc_nerr(mdoc
, n
, "expected boolean"));
1144 post_root(POST_ARGS
)
1147 if (NULL
== mdoc
->first
->child
)
1148 return(mdoc_err(mdoc
, "document lacks data"));
1149 if (SEC_PROLOGUE
== mdoc
->lastnamed
)
1150 return(mdoc_err(mdoc
, "document lacks prologue"));
1152 if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1153 return(mdoc_err(mdoc
, "lacking post-prologue %s",
1154 mdoc_macronames
[MDOC_Sh
]));
1155 if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1156 return(mdoc_err(mdoc
, "lacking post-prologue %s",
1157 mdoc_macronames
[MDOC_Sh
]));
1167 if (MDOC_HEAD
== mdoc
->last
->type
)
1168 return(post_sh_head(mdoc
));
1169 if (MDOC_BODY
== mdoc
->last
->type
)
1170 return(post_sh_body(mdoc
));
1177 post_sh_body(POST_ARGS
)
1179 struct mdoc_node
*n
;
1181 if (SEC_NAME
!= mdoc
->lastnamed
)
1185 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1186 * macros (can have multiple `Nm' and one `Nd'). Note that the
1187 * children of the BODY declaration can also be "text".
1190 if (NULL
== (n
= mdoc
->last
->child
))
1191 return(mdoc_warn(mdoc
, WARN_SYNTAX
,
1192 "section should have %s and %s",
1193 mdoc_macronames
[MDOC_Nm
],
1194 mdoc_macronames
[MDOC_Nd
]));
1196 for ( ; n
&& n
->next
; n
= n
->next
) {
1197 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1199 if (MDOC_TEXT
== n
->type
)
1201 if ( ! (mdoc_nwarn(mdoc
, n
, WARN_SYNTAX
,
1202 "section should have %s first",
1203 mdoc_macronames
[MDOC_Nm
])))
1207 if (MDOC_ELEM
== n
->type
&& MDOC_Nd
== n
->tok
)
1210 return(mdoc_warn(mdoc
, WARN_SYNTAX
,
1211 "section should have %s last",
1212 mdoc_macronames
[MDOC_Nd
]));
1217 post_sh_head(POST_ARGS
)
1222 assert(MDOC_Sh
== mdoc
->last
->tok
);
1224 if ( ! xstrlcats(buf
, mdoc
->last
->child
, sizeof(buf
)))
1225 return(mdoc_err(mdoc
, "argument too long"));
1227 sec
= mdoc_atosec(buf
);
1229 if (SEC_BODY
== mdoc
->lastnamed
&& SEC_NAME
!= sec
)
1230 return(mdoc_warn(mdoc
, WARN_SYNTAX
,
1231 "section NAME should be first"));
1232 if (SEC_CUSTOM
== sec
)
1234 if (sec
== mdoc
->lastnamed
)
1235 return(mdoc_warn(mdoc
, WARN_SYNTAX
,
1236 "section repeated"));
1237 if (sec
< mdoc
->lastnamed
)
1238 return(mdoc_warn(mdoc
, WARN_SYNTAX
,
1239 "section out of order"));
1249 if (SEC_SYNOPSIS
== mdoc
->last
->sec
)
1251 return(mdoc_warn(mdoc
, WARN_COMPAT
,
1252 "suggested only in section SYNOPSIS"));