]>
git.cameronkatri.com Git - mandoc.git/blob - validate.c
1 /* $Id: validate.c,v 1.61 2009/02/26 14:56:27 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 * Pre- and post-validate macros as they're parsed. Pre-validation
27 * occurs when the macro has been detected and its arguments parsed.
28 * Post-validation occurs when all child macros have also been parsed.
29 * In the ELEMENT case, this is simply the parameters of the macro; in
30 * the BLOCK case, this is the HEAD, BODY, TAIL and so on.
33 #define PRE_ARGS struct mdoc *mdoc, const struct mdoc_node *n
34 #define POST_ARGS struct mdoc *mdoc
36 typedef int (*v_pre
)(PRE_ARGS
);
37 typedef int (*v_post
)(POST_ARGS
);
39 /* FIXME: some sections should only occur in specific msecs. */
40 /* FIXME: ignoring Pp. */
41 /* FIXME: math symbols. */
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 size_t, size_t, const char *);
57 static int err_child_lt(struct mdoc
*, const char *, int);
58 static int warn_child_lt(struct mdoc
*, const char *, int);
59 static int err_child_gt(struct mdoc
*, const char *, int);
60 static int warn_child_gt(struct mdoc
*, const char *, int);
61 static int err_child_eq(struct mdoc
*, const char *, int);
62 static int warn_child_eq(struct mdoc
*, const char *, int);
64 /* Utility auxiliaries. */
66 static inline int count_child(struct mdoc
*);
67 static inline int warn_count(struct mdoc
*, const char *,
68 int, const char *, int);
69 static inline int err_count(struct mdoc
*, const char *,
70 int, const char *, int);
72 /* Specific pre-child-parse routines. */
74 static int pre_display(PRE_ARGS
);
75 static int pre_sh(PRE_ARGS
);
76 static int pre_ss(PRE_ARGS
);
77 static int pre_bd(PRE_ARGS
);
78 static int pre_bl(PRE_ARGS
);
79 static int pre_it(PRE_ARGS
);
80 static int pre_cd(PRE_ARGS
);
81 static int pre_er(PRE_ARGS
);
82 static int pre_ex(PRE_ARGS
);
83 static int pre_rv(PRE_ARGS
);
84 static int pre_an(PRE_ARGS
);
85 static int pre_st(PRE_ARGS
);
86 static int pre_prologue(PRE_ARGS
);
87 static int pre_prologue(PRE_ARGS
);
88 static int pre_prologue(PRE_ARGS
);
90 /* Specific post-child-parse routines. */
92 static int herr_ge1(POST_ARGS
);
93 static int hwarn_le1(POST_ARGS
);
94 static int herr_eq0(POST_ARGS
);
95 static int eerr_eq0(POST_ARGS
);
96 static int eerr_le1(POST_ARGS
);
97 static int eerr_le2(POST_ARGS
);
98 static int eerr_eq1(POST_ARGS
);
99 static int eerr_ge1(POST_ARGS
);
100 static int ewarn_eq0(POST_ARGS
);
101 static int ewarn_eq1(POST_ARGS
);
102 static int bwarn_ge1(POST_ARGS
);
103 static int hwarn_eq1(POST_ARGS
);
104 static int ewarn_ge1(POST_ARGS
);
105 static int ebool(POST_ARGS
);
107 static int post_sh(POST_ARGS
);
108 static int post_sh_body(POST_ARGS
);
109 static int post_sh_head(POST_ARGS
);
110 static int post_fd(POST_ARGS
);
111 static int post_bl(POST_ARGS
);
112 static int post_it(POST_ARGS
);
113 static int post_ex(POST_ARGS
);
114 static int post_an(POST_ARGS
);
115 static int post_at(POST_ARGS
);
116 static int post_xr(POST_ARGS
);
117 static int post_nm(POST_ARGS
);
118 static int post_bf(POST_ARGS
);
119 static int post_root(POST_ARGS
);
121 /* Collections of pre-child-parse routines. */
123 static v_pre pres_prologue
[] = { pre_prologue
, NULL
};
124 static v_pre pres_d1
[] = { pre_display
, NULL
};
125 static v_pre pres_bd
[] = { pre_display
, pre_bd
, NULL
};
126 static v_pre pres_bl
[] = { pre_bl
, NULL
};
127 static v_pre pres_it
[] = { pre_it
, NULL
};
128 static v_pre pres_ss
[] = { pre_ss
, NULL
};
129 static v_pre pres_sh
[] = { pre_sh
, NULL
};
130 static v_pre pres_cd
[] = { pre_cd
, NULL
};
131 static v_pre pres_er
[] = { pre_er
, NULL
};
132 static v_pre pres_ex
[] = { pre_ex
, NULL
};
133 static v_pre pres_rv
[] = { pre_rv
, NULL
};
134 static v_pre pres_an
[] = { pre_an
, NULL
};
135 static v_pre pres_st
[] = { pre_st
, NULL
};
137 /* Collections of post-child-parse routines. */
139 static v_post posts_bool
[] = { eerr_eq1
, ebool
, NULL
};
140 static v_post posts_bd
[] = { herr_eq0
, bwarn_ge1
, NULL
};
141 static v_post posts_text
[] = { eerr_ge1
, NULL
};
142 static v_post posts_wtext
[] = { ewarn_ge1
, NULL
};
143 static v_post posts_notext
[] = { eerr_eq0
, NULL
};
144 static v_post posts_wline
[] = { bwarn_ge1
, herr_eq0
, NULL
};
145 static v_post posts_sh
[] = { herr_ge1
, bwarn_ge1
, post_sh
, NULL
};
146 static v_post posts_bl
[] = { herr_eq0
, bwarn_ge1
, post_bl
, NULL
};
147 static v_post posts_it
[] = { post_it
, NULL
};
148 static v_post posts_in
[] = { ewarn_eq1
, NULL
};
149 static v_post posts_ss
[] = { herr_ge1
, NULL
};
150 static v_post posts_pf
[] = { eerr_eq1
, NULL
};
151 static v_post posts_pp
[] = { ewarn_eq0
, NULL
};
152 static v_post posts_ex
[] = { eerr_le1
, post_ex
, NULL
};
153 static v_post posts_an
[] = { post_an
, NULL
};
154 static v_post posts_at
[] = { post_at
, NULL
};
155 static v_post posts_xr
[] = { eerr_ge1
, eerr_le2
, post_xr
, NULL
};
156 static v_post posts_nm
[] = { post_nm
, NULL
};
157 static v_post posts_bf
[] = { hwarn_le1
, post_bf
, NULL
};
158 static v_post posts_rs
[] = { herr_eq0
, bwarn_ge1
, NULL
};
159 static v_post posts_fo
[] = { hwarn_eq1
, bwarn_ge1
, NULL
};
160 static v_post posts_bk
[] = { herr_eq0
, bwarn_ge1
, NULL
};
161 static v_post posts_fd
[] = { ewarn_ge1
, post_fd
, NULL
};
163 /* Per-macro pre- and post-child-check routine collections. */
165 const struct valids mdoc_valids
[MDOC_MAX
] = {
166 { NULL
, NULL
}, /* \" */
167 { pres_prologue
, posts_text
}, /* Dd */
168 { pres_prologue
, NULL
}, /* Dt */
169 { pres_prologue
, NULL
}, /* Os */
170 { pres_sh
, posts_sh
}, /* Sh */
171 { pres_ss
, posts_ss
}, /* Ss */
172 { NULL
, posts_pp
}, /* Pp */
173 { pres_d1
, posts_wline
}, /* D1 */
174 { pres_d1
, posts_wline
}, /* Dl */
175 { pres_bd
, posts_bd
}, /* Bd */
176 { NULL
, NULL
}, /* Ed */
177 { pres_bl
, posts_bl
}, /* Bl */
178 { NULL
, NULL
}, /* El */
179 { pres_it
, posts_it
}, /* It */
180 { NULL
, posts_text
}, /* Ad */
181 { pres_an
, posts_an
}, /* An */
182 { NULL
, NULL
}, /* Ar */
183 { pres_cd
, posts_text
}, /* Cd */
184 { NULL
, NULL
}, /* Cm */
185 { NULL
, posts_text
}, /* Dv */
186 { pres_er
, posts_text
}, /* Er */
187 { NULL
, posts_text
}, /* Ev */
188 { pres_ex
, posts_ex
}, /* Ex */
189 { NULL
, posts_text
}, /* Fa */
190 { NULL
, posts_fd
}, /* Fd */
191 { NULL
, NULL
}, /* Fl */
192 { NULL
, posts_text
}, /* Fn */
193 { NULL
, posts_wtext
}, /* Ft */
194 { NULL
, posts_text
}, /* Ic */
195 { NULL
, posts_in
}, /* In */
196 { NULL
, posts_text
}, /* Li */
197 { NULL
, posts_wtext
}, /* Nd */
198 { NULL
, posts_nm
}, /* Nm */
199 { NULL
, posts_wline
}, /* Op */
200 { NULL
, NULL
}, /* Ot */
201 { NULL
, NULL
}, /* Pa */
202 { pres_rv
, posts_notext
}, /* Rv */
203 { pres_st
, posts_notext
}, /* St */
204 { NULL
, posts_text
}, /* Va */
205 { NULL
, posts_text
}, /* Vt */
206 { NULL
, posts_xr
}, /* Xr */
207 { NULL
, posts_text
}, /* %A */
208 { NULL
, posts_text
}, /* %B */
209 { NULL
, posts_text
}, /* %D */
210 { NULL
, posts_text
}, /* %I */
211 { NULL
, posts_text
}, /* %J */
212 { NULL
, posts_text
}, /* %N */
213 { NULL
, posts_text
}, /* %O */
214 { NULL
, posts_text
}, /* %P */
215 { NULL
, posts_text
}, /* %R */
216 { NULL
, posts_text
}, /* %T */
217 { NULL
, posts_text
}, /* %V */
218 { NULL
, NULL
}, /* Ac */
219 { NULL
, NULL
}, /* Ao */
220 { NULL
, posts_wline
}, /* Aq */
221 { NULL
, posts_at
}, /* At */
222 { NULL
, NULL
}, /* Bc */
223 { NULL
, posts_bf
}, /* Bf */
224 { NULL
, NULL
}, /* Bo */
225 { NULL
, posts_wline
}, /* Bq */
226 { NULL
, NULL
}, /* Bsx */
227 { NULL
, NULL
}, /* Bx */
228 { NULL
, posts_bool
}, /* Db */
229 { NULL
, NULL
}, /* Dc */
230 { NULL
, NULL
}, /* Do */
231 { NULL
, posts_wline
}, /* Dq */
232 { NULL
, NULL
}, /* Ec */
233 { NULL
, NULL
}, /* Ef */
234 { NULL
, posts_text
}, /* Em */
235 { NULL
, NULL
}, /* Eo */
236 { NULL
, NULL
}, /* Fx */
237 { NULL
, posts_text
}, /* Ms */
238 { NULL
, posts_notext
}, /* No */
239 { NULL
, posts_notext
}, /* Ns */
240 { NULL
, NULL
}, /* Nx */
241 { NULL
, NULL
}, /* Ox */
242 { NULL
, NULL
}, /* Pc */
243 { NULL
, posts_pf
}, /* Pf */
244 { NULL
, NULL
}, /* Po */
245 { NULL
, posts_wline
}, /* Pq */
246 { NULL
, NULL
}, /* Qc */
247 { NULL
, posts_wline
}, /* Ql */
248 { NULL
, NULL
}, /* Qo */
249 { NULL
, posts_wline
}, /* Qq */
250 { NULL
, NULL
}, /* Re */
251 { NULL
, posts_rs
}, /* Rs */
252 { NULL
, NULL
}, /* Sc */
253 { NULL
, NULL
}, /* So */
254 { NULL
, posts_wline
}, /* Sq */
255 { NULL
, posts_bool
}, /* Sm */
256 { NULL
, posts_text
}, /* Sx */
257 { NULL
, posts_text
}, /* Sy */
258 { NULL
, posts_text
}, /* Tn */
259 { NULL
, NULL
}, /* Ux */
260 { NULL
, NULL
}, /* Xc */
261 { NULL
, NULL
}, /* Xo */
262 { NULL
, posts_fo
}, /* Fo */
263 { NULL
, NULL
}, /* Fc */
264 { NULL
, NULL
}, /* Oo */
265 { NULL
, NULL
}, /* Oc */
266 { NULL
, posts_bk
}, /* Bk */
267 { NULL
, NULL
}, /* Ek */
268 { NULL
, posts_notext
}, /* Bt */
269 { NULL
, NULL
}, /* Hf */
270 { NULL
, NULL
}, /* Fr */
271 { NULL
, posts_notext
}, /* Ud */
276 mdoc_valid_pre(struct mdoc
*mdoc
,
277 const struct mdoc_node
*node
)
280 struct mdoc_arg
*argv
;
281 size_t argc
, i
, j
, line
, pos
;
284 if (MDOC_TEXT
== node
->type
) {
285 tp
= node
->data
.text
.string
;
288 return(check_text(mdoc
, line
, pos
, tp
));
291 if (MDOC_BLOCK
== node
->type
|| MDOC_ELEM
== node
->type
) {
292 argv
= MDOC_BLOCK
== node
->type
?
293 node
->data
.block
.argv
:
294 node
->data
.elem
.argv
;
295 argc
= MDOC_BLOCK
== node
->type
?
296 node
->data
.block
.argc
:
297 node
->data
.elem
.argc
;
299 for (i
= 0; i
< argc
; i
++) {
302 for (j
= 0; j
< argv
[i
].sz
; j
++) {
303 tp
= argv
[i
].value
[j
];
306 if ( ! check_text(mdoc
, line
, pos
, tp
))
312 if (NULL
== mdoc_valids
[node
->tok
].pre
)
314 for (p
= mdoc_valids
[node
->tok
].pre
; *p
; p
++)
315 if ( ! (*p
)(mdoc
, node
))
322 mdoc_valid_post(struct mdoc
*mdoc
)
327 * This check occurs after the macro's children have been filled
328 * in: postfix validation. Since this happens when we're
329 * rewinding the scope tree, it's possible to have multiple
330 * invocations (as by design, for now), we set bit MDOC_VALID to
331 * indicate that we've validated.
334 if (MDOC_VALID
& mdoc
->last
->flags
)
336 mdoc
->last
->flags
|= MDOC_VALID
;
338 if (MDOC_TEXT
== mdoc
->last
->type
)
340 if (MDOC_ROOT
== mdoc
->last
->type
)
341 return(post_root(mdoc
));
343 if (NULL
== mdoc_valids
[mdoc
->last
->tok
].post
)
345 for (p
= mdoc_valids
[mdoc
->last
->tok
].post
; *p
; p
++)
355 warn_count(struct mdoc
*m
, const char *k
,
356 int want
, const char *v
, int has
)
359 return(mdoc_warn(m
, WARN_SYNTAX
,
360 "suggests %s %s %d (has %d)",
366 err_count(struct mdoc
*m
, const char *k
,
367 int want
, const char *v
, int has
)
370 return(mdoc_err(m
, "requires %s %s %d (has %d)",
376 count_child(struct mdoc
*mdoc
)
381 for (i
= 0, n
= mdoc
->last
->child
; n
; n
= n
->next
, i
++)
389 * Build these up with macros because they're basically the same check
390 * for different inequalities. Yes, this could be done with functions,
391 * but this is reasonable for now.
394 #define CHECK_CHILD_DEFN(lvl, name, ineq) \
396 lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
399 if ((i = count_child(mdoc)) ineq sz) \
401 return(lvl##_count(mdoc, #ineq, sz, p, i)); \
404 #define CHECK_BODY_DEFN(name, lvl, func, num) \
406 b##lvl##_##name(POST_ARGS) \
408 if (MDOC_BODY != mdoc->last->type) \
410 return(func(mdoc, "multiline parameters", (num))); \
413 #define CHECK_ELEM_DEFN(name, lvl, func, num) \
415 e##lvl##_##name(POST_ARGS) \
417 assert(MDOC_ELEM == mdoc->last->type); \
418 return(func(mdoc, "line parameters", (num))); \
421 #define CHECK_HEAD_DEFN(name, lvl, func, num) \
423 h##lvl##_##name(POST_ARGS) \
425 if (MDOC_HEAD != mdoc->last->type) \
427 return(func(mdoc, "line parameters", (num))); \
431 CHECK_CHILD_DEFN(warn
, gt
, >) /* warn_child_gt() */
432 CHECK_CHILD_DEFN(err
, gt
, >) /* err_child_gt() */
433 CHECK_CHILD_DEFN(warn
, eq
, ==) /* warn_child_eq() */
434 CHECK_CHILD_DEFN(err
, eq
, ==) /* err_child_eq() */
435 CHECK_CHILD_DEFN(err
, lt
, <) /* err_child_lt() */
436 CHECK_CHILD_DEFN(warn
, lt
, <) /* warn_child_lt() */
437 CHECK_BODY_DEFN(ge1
, warn
, warn_child_gt
, 0) /* bwarn_ge1() */
438 CHECK_ELEM_DEFN(eq1
, warn
, warn_child_eq
, 1) /* ewarn_eq1() */
439 CHECK_ELEM_DEFN(eq0
, warn
, warn_child_eq
, 0) /* ewarn_eq0() */
440 CHECK_ELEM_DEFN(ge1
, warn
, warn_child_gt
, 0) /* ewarn_gt1() */
441 CHECK_ELEM_DEFN(eq1
, err
, err_child_eq
, 1) /* eerr_eq1() */
442 CHECK_ELEM_DEFN(le2
, err
, err_child_lt
, 3) /* eerr_le2() */
443 CHECK_ELEM_DEFN(le1
, err
, err_child_lt
, 2) /* eerr_le1() */
444 CHECK_ELEM_DEFN(eq0
, err
, err_child_eq
, 0) /* eerr_eq0() */
445 CHECK_ELEM_DEFN(ge1
, err
, err_child_gt
, 0) /* eerr_ge1() */
446 CHECK_HEAD_DEFN(eq0
, err
, err_child_eq
, 0) /* herr_eq0() */
447 CHECK_HEAD_DEFN(le1
, warn
, warn_child_lt
, 2) /* hwarn_le1() */
448 CHECK_HEAD_DEFN(ge1
, err
, err_child_gt
, 0) /* herr_ge1() */
449 CHECK_HEAD_DEFN(eq1
, warn
, warn_child_eq
, 1) /* hwarn_eq1() */
453 check_stdarg(PRE_ARGS
)
456 if (MDOC_Std
== n
->data
.elem
.argv
[0].arg
&&
457 1 == n
->data
.elem
.argc
)
460 return(mdoc_nwarn(mdoc
, n
, WARN_COMPAT
,
461 "one argument suggested"));
466 check_msec(PRE_ARGS
, int sz
, enum mdoc_msec
*msecs
)
470 for (i
= 0; i
< sz
; i
++)
471 if (msecs
[i
] == mdoc
->meta
.msec
)
473 return(mdoc_nwarn(mdoc
, n
, WARN_COMPAT
,
474 "invalid manual section"));
479 check_text(struct mdoc
*mdoc
, size_t line
, size_t pos
, const char *p
)
484 if ( ! isprint((int)*p
) && '\t' != *p
)
485 return(mdoc_perr(mdoc
, line
, pos
,
486 "invalid characters"));
489 if ((c
= mdoc_isescape(p
))) {
493 return(mdoc_perr(mdoc
, line
, pos
,
494 "invalid escape sequence"));
504 check_parent(PRE_ARGS
, int tok
, enum mdoc_type t
)
508 if ((MDOC_ROOT
== t
|| tok
== n
->parent
->tok
) &&
509 (t
== n
->parent
->type
))
512 return(mdoc_nerr(mdoc
, n
, "require parent %s",
513 MDOC_ROOT
== t
? "<root>" : mdoc_macronames
[tok
]));
519 pre_display(PRE_ARGS
)
521 struct mdoc_node
*node
;
523 /* Display elements (`Bd', `D1'...) cannot be nested. */
525 if (MDOC_BLOCK
!= n
->type
)
529 for (node
= mdoc
->last
->parent
; node
; node
= node
->parent
)
530 if (MDOC_BLOCK
== node
->type
)
531 if (MDOC_Bd
== node
->tok
)
536 return(mdoc_nerr(mdoc
, n
, "displays may not be nested"));
543 int type
, i
, width
, offset
;
544 struct mdoc_arg
*argv
;
547 if (MDOC_BLOCK
!= n
->type
)
550 argc
= n
->data
.block
.argc
;
552 /* Make sure that only one type of list is specified. */
554 type
= offset
= width
= -1;
557 for (i
= 0; i
< (int)argc
; i
++) {
558 argv
= &n
->data
.block
.argv
[i
];
586 return(mdoc_perr(mdoc
, argv
->line
, argv
->pos
,
587 "multiple types specified"));
593 return(mdoc_perr(mdoc
, argv
->line
, argv
->pos
,
594 "multiple -%s arguments",
595 mdoc_argnames
[MDOC_Width
]));
601 return(mdoc_perr(mdoc
, argv
->line
, argv
->pos
,
602 "multiple -%s arguments",
603 mdoc_argnames
[MDOC_Offset
]));
610 return(mdoc_err(mdoc
, "no type specified"));
622 return(mdoc_nwarn(mdoc
, n
, WARN_SYNTAX
,
623 "superfluous -%s argument",
624 mdoc_argnames
[MDOC_Width
]));
628 return(mdoc_nwarn(mdoc
, n
, WARN_SYNTAX
,
629 "suggest -%s argument",
630 mdoc_argnames
[MDOC_Width
]));
643 struct mdoc_arg
*argv
;
646 if (MDOC_BLOCK
!= n
->type
)
649 argc
= n
->data
.block
.argc
;
651 /* Make sure that only one type of display is specified. */
654 for (i
= 0, err
= type
= 0; ! err
&& i
< (int)argc
; i
++) {
655 argv
= &n
->data
.block
.argv
[i
];
660 case (MDOC_Unfilled
):
669 return(mdoc_perr(mdoc
, argv
->line
, argv
->pos
,
670 "multiple types specified"));
678 return(mdoc_err(mdoc
, "no type specified"));
686 if (MDOC_BLOCK
!= n
->type
)
688 return(check_parent(mdoc
, n
, MDOC_Sh
, MDOC_BODY
));
696 if (MDOC_BLOCK
!= n
->type
)
698 return(check_parent(mdoc
, n
, -1, MDOC_ROOT
));
706 /* TODO: children too big for -width? */
708 if (MDOC_BLOCK
!= n
->type
)
710 return(check_parent(mdoc
, n
, MDOC_Bl
, MDOC_BODY
));
718 if (1 == n
->data
.elem
.argc
)
720 return(mdoc_nerr(mdoc
, n
, "one argument required"));
728 if (1 >= n
->data
.elem
.argc
)
730 return(mdoc_nerr(mdoc
, n
, "one argument allowed"));
737 enum mdoc_msec msecs
[] = { MSEC_2
, MSEC_3
};
739 if ( ! check_msec(mdoc
, n
, 2, msecs
))
741 return(check_stdarg(mdoc
, n
));
748 enum mdoc_msec msecs
[] = { MSEC_1
, MSEC_6
, MSEC_8
};
750 if ( ! check_msec(mdoc
, n
, 3, msecs
))
752 return(check_stdarg(mdoc
, n
));
759 enum mdoc_msec msecs
[] = { MSEC_2
};
761 return(check_msec(mdoc
, n
, 1, msecs
));
768 enum mdoc_msec msecs
[] = { MSEC_4
};
770 return(check_msec(mdoc
, n
, 1, msecs
));
775 pre_prologue(PRE_ARGS
)
778 if (SEC_PROLOGUE
!= mdoc
->lastnamed
)
779 return(mdoc_nerr(mdoc
, n
, "prologue only"));
781 /* Check for ordering. */
785 if (mdoc
->meta
.title
&& mdoc
->meta
.date
)
787 return(mdoc_nerr(mdoc
, n
, "prologue out-of-order"));
789 if (NULL
== mdoc
->meta
.title
&& mdoc
->meta
.date
)
791 return(mdoc_nerr(mdoc
, n
, "prologue out-of-order"));
793 if (NULL
== mdoc
->meta
.title
&& 0 == mdoc
->meta
.date
)
795 return(mdoc_nerr(mdoc
, n
, "prologue out-of-order"));
801 /* Check for repetition. */
805 if (NULL
== mdoc
->meta
.os
)
809 if (0 == mdoc
->meta
.date
)
813 if (NULL
== mdoc
->meta
.title
)
821 return(mdoc_nerr(mdoc
, n
, "prologue repetition"));
829 struct mdoc_node
*head
;
831 if (MDOC_BLOCK
!= mdoc
->last
->type
)
834 head
= mdoc
->last
->data
.block
.head
;
836 if (0 == mdoc
->last
->data
.block
.argc
) {
837 if (NULL
== head
->child
)
838 return(mdoc_err(mdoc
, "argument expected"));
840 p
= head
->child
->data
.text
.string
;
841 if (xstrcmp(p
, "Em"))
843 else if (xstrcmp(p
, "Li"))
845 else if (xstrcmp(p
, "Sm"))
847 return(mdoc_nerr(mdoc
, head
->child
, "invalid font"));
851 return(mdoc_err(mdoc
, "argument expected"));
853 if (1 == mdoc
->last
->data
.block
.argc
)
855 return(mdoc_err(mdoc
, "argument expected"));
863 if (mdoc
->last
->child
)
867 return(mdoc_err(mdoc
, "not yet invoked with name"));
876 if (NULL
== (n
= mdoc
->last
->child
->next
))
878 if (MSEC_DEFAULT
!= mdoc_atomsec(n
->data
.text
.string
))
880 return(mdoc_nerr(mdoc
, n
, "invalid manual section"));
888 if (NULL
== mdoc
->last
->child
)
890 if (ATT_DEFAULT
!= mdoc_atoatt(mdoc
->last
->child
->data
.text
.string
))
892 return(mdoc_err(mdoc
, "require valid symbol"));
900 if (0 != mdoc
->last
->data
.elem
.argc
) {
901 if (NULL
== mdoc
->last
->child
)
903 return(mdoc_err(mdoc
, "argument(s) expected"));
906 if (mdoc
->last
->child
)
908 return(mdoc_err(mdoc
, "argument(s) expected"));
916 if (0 == mdoc
->last
->data
.elem
.argc
) {
917 if (mdoc
->last
->child
)
919 return(mdoc_err(mdoc
, "argument(s) expected"));
921 if (mdoc
->last
->child
)
922 return(mdoc_err(mdoc
, "argument(s) expected"));
923 if (1 != mdoc
->last
->data
.elem
.argc
)
924 return(mdoc_err(mdoc
, "argument(s) expected"));
925 if (MDOC_Std
!= mdoc
->last
->data
.elem
.argv
[0].arg
)
926 return(mdoc_err(mdoc
, "argument(s) expected"));
936 #define TYPE_NONE (0)
937 #define TYPE_BODY (1)
938 #define TYPE_HEAD (2)
939 #define TYPE_OHEAD (3)
943 if (MDOC_BLOCK
!= mdoc
->last
->type
)
946 n
= mdoc
->last
->parent
->parent
;
948 argc
= n
->data
.block
.argc
;
952 /* Some types require block-head, some not. */
955 for (i
= 0; TYPE_NONE
== type
&& i
< (int)argc
; i
++)
956 switch (n
->data
.block
.argv
[i
].arg
) {
967 sv
= n
->data
.block
.argv
[i
].arg
;
979 sv
= n
->data
.block
.argv
[i
].arg
;
983 sv
= n
->data
.block
.argv
[i
].arg
;
989 assert(TYPE_NONE
!= type
);
991 n
= mdoc
->last
->data
.block
.head
;
993 if (TYPE_HEAD
== type
) {
994 if (NULL
== n
->child
)
995 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
996 "argument(s) suggested"))
999 n
= mdoc
->last
->data
.block
.body
;
1000 if (NULL
== n
->child
)
1001 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1002 "multiline body suggested"))
1005 } else if (TYPE_BODY
== type
) {
1007 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1008 "no argument suggested"))
1011 n
= mdoc
->last
->data
.block
.body
;
1012 if (NULL
== n
->child
)
1013 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1014 "multiline body suggested"))
1017 if (NULL
== n
->child
)
1018 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1019 "argument(s) suggested"))
1022 n
= mdoc
->last
->data
.block
.body
;
1024 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
1025 "no multiline body suggested"))
1029 if (MDOC_Column
!= sv
)
1032 argc
= mdoc
->last
->parent
->parent
->data
.block
.argv
->sz
;
1033 n
= mdoc
->last
->data
.block
.head
->child
;
1035 for (i
= 0; n
; n
= n
->next
)
1041 return(mdoc_err(mdoc
, "need %zu columns (have %d)", argc
, i
));
1052 struct mdoc_node
*n
;
1054 if (MDOC_BODY
!= mdoc
->last
->type
)
1058 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1059 if (MDOC_BLOCK
== n
->type
)
1060 if (MDOC_It
== n
->tok
)
1068 return(mdoc_nerr(mdoc
, n
, "bad child of parent list"));
1073 ebool(struct mdoc
*mdoc
)
1075 struct mdoc_node
*n
;
1078 for (n
= mdoc
->last
->child
; n
; n
= n
->next
) {
1079 if (MDOC_TEXT
!= n
->type
)
1081 if (xstrcmp(n
->data
.text
.string
, "on"))
1083 if (xstrcmp(n
->data
.text
.string
, "off"))
1090 return(mdoc_nerr(mdoc
, n
, "expected boolean"));
1095 post_root(POST_ARGS
)
1098 if (NULL
== mdoc
->first
->child
)
1099 return(mdoc_err(mdoc
, "document lacks data"));
1100 if (SEC_PROLOGUE
== mdoc
->lastnamed
)
1101 return(mdoc_err(mdoc
, "document lacks prologue"));
1103 if (MDOC_BLOCK
!= mdoc
->first
->child
->type
)
1104 return(mdoc_err(mdoc
, "lacking post-prologue %s",
1105 mdoc_macronames
[MDOC_Sh
]));
1106 if (MDOC_Sh
!= mdoc
->first
->child
->tok
)
1107 return(mdoc_err(mdoc
, "lacking post-prologue %s",
1108 mdoc_macronames
[MDOC_Sh
]));
1118 if (MDOC_HEAD
== mdoc
->last
->type
)
1119 return(post_sh_head(mdoc
));
1120 if (MDOC_BODY
== mdoc
->last
->type
)
1121 return(post_sh_body(mdoc
));
1128 post_sh_body(POST_ARGS
)
1130 struct mdoc_node
*n
;
1132 if (SEC_NAME
!= mdoc
->lastnamed
)
1136 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1137 * macros (can have multiple `Nm' and one `Nd'). Note that the
1138 * children of the BODY declaration can also be "text".
1141 if (NULL
== (n
= mdoc
->last
->child
))
1142 return(mdoc_warn(mdoc
, WARN_SYNTAX
,
1143 "section should have %s and %s",
1144 mdoc_macronames
[MDOC_Nm
],
1145 mdoc_macronames
[MDOC_Nd
]));
1147 for ( ; n
&& n
->next
; n
= n
->next
) {
1148 if (MDOC_ELEM
== n
->type
&& MDOC_Nm
== n
->tok
)
1150 if (MDOC_TEXT
== n
->type
)
1152 if ( ! (mdoc_nwarn(mdoc
, n
, WARN_SYNTAX
,
1153 "section should have %s first",
1154 mdoc_macronames
[MDOC_Nm
])))
1158 if (MDOC_ELEM
== n
->type
&& MDOC_Nd
== n
->tok
)
1161 return(mdoc_warn(mdoc
, WARN_SYNTAX
,
1162 "section should have %s last",
1163 mdoc_macronames
[MDOC_Nd
]));
1168 post_sh_head(POST_ARGS
)
1173 assert(MDOC_Sh
== mdoc
->last
->tok
);
1175 if ( ! xstrlcats(buf
, mdoc
->last
->child
, sizeof(buf
)))
1176 return(mdoc_err(mdoc
, "argument too long"));
1178 sec
= mdoc_atosec(buf
);
1180 if (SEC_BODY
== mdoc
->lastnamed
&& SEC_NAME
!= sec
)
1181 return(mdoc_warn(mdoc
, WARN_SYNTAX
,
1182 "section NAME should be first"));
1183 if (SEC_CUSTOM
== sec
)
1185 if (sec
== mdoc
->lastnamed
)
1186 return(mdoc_warn(mdoc
, WARN_SYNTAX
,
1187 "section repeated"));
1188 if (sec
< mdoc
->lastnamed
)
1189 return(mdoc_warn(mdoc
, WARN_SYNTAX
,
1190 "section out of order"));
1200 if (SEC_SYNOPSIS
== mdoc
->last
->sec
)
1202 return(mdoc_warn(mdoc
, WARN_COMPAT
,
1203 "suggested only in section SYNOPSIS"));