xref: /illumos-gate/usr/src/cmd/mandoc/mandoc.h (revision 7f698155b6fd0ee137eb4a2e7c85f0915a94bfee)
1 /* $Id: mandoc.h,v 1.274 2021/08/14 13:53:08 schwarze Exp $ */
2 /*
3  * Copyright (c) 2012-2021 Ingo Schwarze <schwarze@openbsd.org>
4  * Copyright (c) 2010, 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * Error handling, escape sequence, and character utilities.
19  * Can be used by all code in the mandoc package.
20  */
21 
22 #define ASCII_NBRSP	 31  /* non-breaking space */
23 #define	ASCII_HYPH	 30  /* breakable hyphen */
24 #define	ASCII_BREAK	 29  /* breakable zero-width space */
25 
26 /*
27  * Status level.  This refers to both internal status (i.e., whilst
28  * running, when warnings/errors are reported) and an indicator of a
29  * threshold of when to halt (when said internal state exceeds the
30  * threshold).
31  */
32 enum	mandoclevel {
33 	MANDOCLEVEL_OK = 0,
34 	MANDOCLEVEL_STYLE, /* style suggestions */
35 	MANDOCLEVEL_WARNING, /* warnings: syntax, whitespace, etc. */
36 	MANDOCLEVEL_ERROR, /* input has been thrown away */
37 	MANDOCLEVEL_UNSUPP, /* input needs unimplemented features */
38 	MANDOCLEVEL_BADARG, /* bad argument in invocation */
39 	MANDOCLEVEL_SYSERR, /* system error */
40 	MANDOCLEVEL_MAX
41 };
42 
43 /*
44  * All possible things that can go wrong within a parse, be it libroff,
45  * libmdoc, or libman.
46  */
47 enum	mandocerr {
48 	MANDOCERR_OK,
49 
50 	MANDOCERR_BASE, /* ===== start of base system conventions ===== */
51 
52 	MANDOCERR_MDOCDATE, /* Mdocdate found: Dd ... */
53 	MANDOCERR_MDOCDATE_MISSING, /* Mdocdate missing: Dd ... */
54 	MANDOCERR_ARCH_BAD,  /* unknown architecture: Dt ... arch */
55 	MANDOCERR_OS_ARG,  /* operating system explicitly specified: Os ... */
56 	MANDOCERR_RCS_MISSING, /* RCS id missing */
57 
58 	MANDOCERR_STYLE, /* ===== start of style suggestions ===== */
59 
60 	MANDOCERR_DATE_LEGACY, /* legacy man(7) date format: Dd ... */
61 	MANDOCERR_DATE_NORM, /* normalizing date format to: ... */
62 	MANDOCERR_TITLE_CASE, /* lower case character in document title */
63 	MANDOCERR_RCS_REP, /* duplicate RCS id: ... */
64 	MANDOCERR_SEC_TYPO,  /* possible typo in section name: Sh ... */
65 	MANDOCERR_ARG_QUOTE, /* unterminated quoted argument */
66 	MANDOCERR_MACRO_USELESS, /* useless macro: macro */
67 	MANDOCERR_BX, /* consider using OS macro: macro */
68 	MANDOCERR_ER_ORDER, /* errnos out of order: Er ... */
69 	MANDOCERR_ER_REP, /* duplicate errno: Er ... */
70 	MANDOCERR_XR_BAD,  /* referenced manual not found: Xr name sec */
71 	MANDOCERR_DELIM, /* trailing delimiter: macro ... */
72 	MANDOCERR_DELIM_NB, /* no blank before trailing delimiter: macro ... */
73 	MANDOCERR_FI_SKIP, /* fill mode already enabled, skipping: fi */
74 	MANDOCERR_NF_SKIP, /* fill mode already disabled, skipping: nf */
75 	MANDOCERR_TEXT_LONG, /* input text line longer than 80 bytes */
76 	MANDOCERR_DASHDASH, /* verbatim "--", maybe consider using \(em */
77 	MANDOCERR_FUNC, /* function name without markup: name() */
78 	MANDOCERR_SPACE_EOL, /* whitespace at end of input line */
79 	MANDOCERR_COMMENT_BAD, /* bad comment style */
80 
81 	MANDOCERR_WARNING, /* ===== start of warnings ===== */
82 
83 	/* related to the prologue */
84 	MANDOCERR_DT_NOTITLE, /* missing manual title, using UNTITLED: line */
85 	MANDOCERR_TH_NOTITLE, /* missing manual title, using "": [macro] */
86 	MANDOCERR_MSEC_MISSING, /* missing manual section, using "": macro */
87 	MANDOCERR_MSEC_BAD, /* unknown manual section: Dt ... section */
88 	MANDOCERR_MSEC_FILE, /* filename/section mismatch: ... */
89 	MANDOCERR_DATE_MISSING, /* missing date, using "": [macro] */
90 	MANDOCERR_DATE_BAD, /* cannot parse date, using it verbatim: date */
91 	MANDOCERR_DATE_FUTURE, /* date in the future, using it anyway: date */
92 	MANDOCERR_OS_MISSING, /* missing Os macro, using "" */
93 	MANDOCERR_PROLOG_LATE, /* late prologue macro: macro */
94 	MANDOCERR_PROLOG_ORDER, /* prologue macros out of order: macros */
95 
96 	/* related to document structure */
97 	MANDOCERR_SO, /* .so is fragile, better use ln(1): so path */
98 	MANDOCERR_DOC_EMPTY, /* no document body */
99 	MANDOCERR_SEC_BEFORE, /* content before first section header: macro */
100 	MANDOCERR_NAMESEC_FIRST, /* first section is not NAME: Sh title */
101 	MANDOCERR_NAMESEC_NONM, /* NAME section without Nm before Nd */
102 	MANDOCERR_NAMESEC_NOND, /* NAME section without description */
103 	MANDOCERR_NAMESEC_ND, /* description not at the end of NAME */
104 	MANDOCERR_NAMESEC_BAD, /* bad NAME section content: macro */
105 	MANDOCERR_NAMESEC_PUNCT, /* missing comma before name: Nm name */
106 	MANDOCERR_ND_EMPTY, /* missing description line, using "" */
107 	MANDOCERR_ND_LATE, /* description line outside NAME section */
108 	MANDOCERR_SEC_ORDER, /* sections out of conventional order: Sh title */
109 	MANDOCERR_SEC_REP, /* duplicate section title: Sh title */
110 	MANDOCERR_SEC_MSEC, /* unexpected section: Sh title for ... only */
111 	MANDOCERR_XR_SELF,  /* cross reference to self: Xr name sec */
112 	MANDOCERR_XR_ORDER, /* unusual Xr order: ... after ... */
113 	MANDOCERR_XR_PUNCT, /* unusual Xr punctuation: ... after ... */
114 	MANDOCERR_AN_MISSING, /* AUTHORS section without An macro */
115 
116 	/* related to macros and nesting */
117 	MANDOCERR_MACRO_OBS, /* obsolete macro: macro */
118 	MANDOCERR_MACRO_CALL, /* macro neither callable nor escaped: macro */
119 	MANDOCERR_PAR_SKIP, /* skipping paragraph macro: macro ... */
120 	MANDOCERR_PAR_MOVE, /* moving paragraph macro out of list: macro */
121 	MANDOCERR_NS_SKIP, /* skipping no-space macro */
122 	MANDOCERR_BLK_NEST, /* blocks badly nested: macro ... */
123 	MANDOCERR_BD_NEST, /* nested displays are not portable: macro ... */
124 	MANDOCERR_BL_MOVE, /* moving content out of list: macro */
125 	MANDOCERR_TA_LINE, /* first macro on line: Ta */
126 	MANDOCERR_BLK_LINE, /* line scope broken: macro breaks macro */
127 	MANDOCERR_BLK_BLANK, /* skipping blank line in line scope */
128 
129 	/* related to missing arguments */
130 	MANDOCERR_REQ_EMPTY, /* skipping empty request: request */
131 	MANDOCERR_COND_EMPTY, /* conditional request controls empty scope */
132 	MANDOCERR_MACRO_EMPTY, /* skipping empty macro: macro */
133 	MANDOCERR_BLK_EMPTY, /* empty block: macro */
134 	MANDOCERR_ARG_EMPTY, /* empty argument, using 0n: macro arg */
135 	MANDOCERR_BD_NOTYPE, /* missing display type, using -ragged: Bd */
136 	MANDOCERR_BL_LATETYPE, /* list type is not the first argument: Bl arg */
137 	MANDOCERR_BL_NOWIDTH, /* missing -width in -tag list, using 6n */
138 	MANDOCERR_EX_NONAME, /* missing utility name, using "": Ex */
139 	MANDOCERR_FO_NOHEAD, /* missing function name, using "": Fo */
140 	MANDOCERR_IT_NOHEAD, /* empty head in list item: Bl -type It */
141 	MANDOCERR_IT_NOBODY, /* empty list item: Bl -type It */
142 	MANDOCERR_IT_NOARG, /* missing argument, using next line: Bl -c It */
143 	MANDOCERR_BF_NOFONT, /* missing font type, using \fR: Bf */
144 	MANDOCERR_BF_BADFONT, /* unknown font type, using \fR: Bf font */
145 	MANDOCERR_PF_SKIP, /* nothing follows prefix: Pf arg */
146 	MANDOCERR_RS_EMPTY, /* empty reference block: Rs */
147 	MANDOCERR_XR_NOSEC, /* missing section argument: Xr arg */
148 	MANDOCERR_ARG_STD, /* missing -std argument, adding it: macro */
149 	MANDOCERR_OP_EMPTY, /* missing option string, using "": OP */
150 	MANDOCERR_UR_NOHEAD, /* missing resource identifier, using "": UR */
151 	MANDOCERR_EQN_NOBOX, /* missing eqn box, using "": op */
152 
153 	/* related to bad arguments */
154 	MANDOCERR_ARG_REP, /* duplicate argument: macro arg */
155 	MANDOCERR_AN_REP, /* skipping duplicate argument: An -arg */
156 	MANDOCERR_BD_REP, /* skipping duplicate display type: Bd -type */
157 	MANDOCERR_BL_REP, /* skipping duplicate list type: Bl -type */
158 	MANDOCERR_BL_SKIPW, /* skipping -width argument: Bl -type */
159 	MANDOCERR_BL_COL, /* wrong number of cells */
160 	MANDOCERR_AT_BAD, /* unknown AT&T UNIX version: At version */
161 	MANDOCERR_FA_COMMA, /* comma in function argument: arg */
162 	MANDOCERR_FN_PAREN, /* parenthesis in function name: arg */
163 	MANDOCERR_LB_BAD, /* unknown library name: Lb ... */
164 	MANDOCERR_RS_BAD, /* invalid content in Rs block: macro */
165 	MANDOCERR_SM_BAD, /* invalid Boolean argument: macro arg */
166 	MANDOCERR_CHAR_FONT, /* argument contains two font escapes */
167 	MANDOCERR_FT_BAD, /* unknown font, skipping request: ft font */
168 	MANDOCERR_TR_ODD, /* odd number of characters in request: tr char */
169 
170 	/* related to plain text */
171 	MANDOCERR_FI_BLANK, /* blank line in fill mode, using .sp */
172 	MANDOCERR_FI_TAB, /* tab in filled text */
173 	MANDOCERR_EOS, /* new sentence, new line */
174 	MANDOCERR_ESC_BAD, /* invalid escape sequence: esc */
175 	MANDOCERR_ESC_UNDEF, /* undefined escape, printing literally: char */
176 	MANDOCERR_STR_UNDEF, /* undefined string, using "": name */
177 
178 	/* related to tables */
179 	MANDOCERR_TBLLAYOUT_SPAN, /* tbl line starts with span */
180 	MANDOCERR_TBLLAYOUT_DOWN, /* tbl column starts with span */
181 	MANDOCERR_TBLLAYOUT_VERT, /* skipping vertical bar in tbl layout */
182 
183 	MANDOCERR_ERROR, /* ===== start of errors ===== */
184 
185 	/* related to tables */
186 	MANDOCERR_TBLOPT_ALPHA, /* non-alphabetic character in tbl options */
187 	MANDOCERR_TBLOPT_BAD, /* skipping unknown tbl option: option */
188 	MANDOCERR_TBLOPT_NOARG, /* missing tbl option argument: option */
189 	MANDOCERR_TBLOPT_ARGSZ, /* wrong tbl option argument size: option */
190 	MANDOCERR_TBLLAYOUT_NONE, /* empty tbl layout */
191 	MANDOCERR_TBLLAYOUT_CHAR, /* invalid character in tbl layout: char */
192 	MANDOCERR_TBLLAYOUT_PAR, /* unmatched parenthesis in tbl layout */
193 	MANDOCERR_TBLLAYOUT_SPC, /* ignoring excessive spacing in tbl layout */
194 	MANDOCERR_TBLDATA_NONE, /* tbl without any data cells */
195 	MANDOCERR_TBLDATA_SPAN, /* ignoring data in spanned tbl cell: data */
196 	MANDOCERR_TBLDATA_EXTRA, /* ignoring extra tbl data cells: data */
197 	MANDOCERR_TBLDATA_BLK, /* data block open at end of tbl: macro */
198 
199 	/* related to document structure and macros */
200 	MANDOCERR_PROLOG_REP, /* duplicate prologue macro: macro */
201 	MANDOCERR_DT_LATE, /* skipping late title macro: Dt args */
202 	MANDOCERR_ROFFLOOP, /* input stack limit exceeded, infinite loop? */
203 	MANDOCERR_CHAR_BAD, /* skipping bad character: number */
204 	MANDOCERR_MACRO, /* skipping unknown macro: macro */
205 	MANDOCERR_REQ_NOMAC, /* skipping request outside macro: ... */
206 	MANDOCERR_REQ_INSEC, /* skipping insecure request: request */
207 	MANDOCERR_IT_STRAY, /* skipping item outside list: It ... */
208 	MANDOCERR_TA_STRAY, /* skipping column outside column list: Ta */
209 	MANDOCERR_BLK_NOTOPEN, /* skipping end of block that is not open */
210 	MANDOCERR_RE_NOTOPEN, /* fewer RS blocks open, skipping: RE arg */
211 	MANDOCERR_BLK_BROKEN, /* inserting missing end of block: macro ... */
212 	MANDOCERR_BLK_NOEND, /* appending missing end of block: macro */
213 
214 	/* related to request and macro arguments */
215 	MANDOCERR_NAMESC, /* escaped character not allowed in a name: name */
216 	MANDOCERR_ARG_UNDEF, /* using macro argument outside macro */
217 	MANDOCERR_ARG_NONUM, /* argument number is not numeric */
218 	MANDOCERR_BD_FILE, /* NOT IMPLEMENTED: Bd -file */
219 	MANDOCERR_BD_NOARG, /* skipping display without arguments: Bd */
220 	MANDOCERR_BL_NOTYPE, /* missing list type, using -item: Bl */
221 	MANDOCERR_CE_NONUM, /* argument is not numeric, using 1: ce ... */
222 	MANDOCERR_CHAR_ARG, /* argument is not a character: char ... */
223 	MANDOCERR_NM_NONAME, /* missing manual name, using "": Nm */
224 	MANDOCERR_OS_UNAME, /* uname(3) system call failed, using UNKNOWN */
225 	MANDOCERR_ST_BAD, /* unknown standard specifier: St standard */
226 	MANDOCERR_IT_NONUM, /* skipping request without numeric argument */
227 	MANDOCERR_SHIFT, /* excessive shift: ..., but max is ... */
228 	MANDOCERR_SO_PATH, /* NOT IMPLEMENTED: .so with absolute path or ".." */
229 	MANDOCERR_SO_FAIL, /* .so request failed */
230 	MANDOCERR_TG_SPC, /* skipping tag containing whitespace: tag */
231 	MANDOCERR_ARG_SKIP, /* skipping all arguments: macro args */
232 	MANDOCERR_ARG_EXCESS, /* skipping excess arguments: macro ... args */
233 	MANDOCERR_DIVZERO, /* divide by zero */
234 
235 	MANDOCERR_UNSUPP, /* ===== start of unsupported features ===== */
236 
237 	MANDOCERR_TOOLARGE, /* input too large */
238 	MANDOCERR_CHAR_UNSUPP, /* unsupported control character: number */
239 	MANDOCERR_ESC_UNSUPP, /* unsupported escape sequence: escape */
240 	MANDOCERR_REQ_UNSUPP, /* unsupported roff request: request */
241 	MANDOCERR_WHILE_NEST, /* nested .while loops */
242 	MANDOCERR_WHILE_OUTOF, /* end of scope with open .while loop */
243 	MANDOCERR_WHILE_INTO, /* end of .while loop in inner scope */
244 	MANDOCERR_WHILE_FAIL, /* cannot continue this .while loop */
245 	MANDOCERR_TBLOPT_EQN, /* eqn delim option in tbl: arg */
246 	MANDOCERR_TBLLAYOUT_MOD, /* unsupported tbl layout modifier: m */
247 	MANDOCERR_TBLMACRO, /* ignoring macro in table: macro */
248 	MANDOCERR_TBL_TMAN, /* skipping tbl in -Tman mode */
249 	MANDOCERR_EQN_TMAN, /* skipping eqn in -Tman mode */
250 
251 	MANDOCERR_BADARG, /* ===== start of bad invocations ===== */
252 
253 	MANDOCERR_BADARG_BAD, /* bad argument */
254 	MANDOCERR_BADARG_DUPE, /* duplicate argument */
255 	MANDOCERR_BADVAL, /* does not take a value */
256 	MANDOCERR_BADVAL_MISS, /* missing argument value */
257 	MANDOCERR_BADVAL_BAD, /* bad argument value */
258 	MANDOCERR_BADVAL_DUPE, /* duplicate argument value */
259 	MANDOCERR_TAG, /* no such tag */
260 	MANDOCERR_MAN_TMARKDOWN, /* -Tmarkdown unsupported for man(7) input */
261 
262 	MANDOCERR_SYSERR, /* ===== start of system errors ===== */
263 
264 	MANDOCERR_DUP,
265 	MANDOCERR_EXEC,
266 	MANDOCERR_FDOPEN,
267 	MANDOCERR_FFLUSH,
268 	MANDOCERR_FORK,
269 	MANDOCERR_FSTAT,
270 	MANDOCERR_GETLINE,
271 	MANDOCERR_GLOB,
272 	MANDOCERR_GZCLOSE,
273 	MANDOCERR_GZDOPEN,
274 	MANDOCERR_MKSTEMP,
275 	MANDOCERR_OPEN,
276 	MANDOCERR_PLEDGE,
277 	MANDOCERR_READ,
278 	MANDOCERR_WAIT,
279 	MANDOCERR_WRITE,
280 
281 	MANDOCERR_MAX
282 };
283 
284 enum	mandoc_esc {
285 	ESCAPE_ERROR = 0, /* bail! unparsable escape */
286 	ESCAPE_UNSUPP, /* unsupported escape; ignore it */
287 	ESCAPE_IGNORE, /* escape to be ignored */
288 	ESCAPE_UNDEF, /* undefined escape; print literal character */
289 	ESCAPE_SPECIAL, /* a regular special character */
290 	ESCAPE_FONT, /* a generic font mode */
291 	ESCAPE_FONTBOLD, /* bold font mode */
292 	ESCAPE_FONTITALIC, /* italic font mode */
293 	ESCAPE_FONTBI, /* bold italic font mode */
294 	ESCAPE_FONTROMAN, /* roman font mode */
295 	ESCAPE_FONTCR, /* constant width font mode */
296 	ESCAPE_FONTCB, /* constant width bold font mode */
297 	ESCAPE_FONTCI, /* constant width italic font mode */
298 	ESCAPE_FONTPREV, /* previous font mode */
299 	ESCAPE_NUMBERED, /* a numbered glyph */
300 	ESCAPE_UNICODE, /* a unicode codepoint */
301 	ESCAPE_DEVICE, /* print the output device name */
302 	ESCAPE_BREAK, /* break the output line */
303 	ESCAPE_NOSPACE, /* suppress space if the last on a line */
304 	ESCAPE_HORIZ, /* horizontal movement */
305 	ESCAPE_HLINE, /* horizontal line drawing */
306 	ESCAPE_SKIPCHAR, /* skip the next character */
307 	ESCAPE_OVERSTRIKE /* overstrike all chars in the argument */
308 };
309 
310 
311 enum mandoc_esc	  mandoc_font(const char *, int);
312 enum mandoc_esc	  mandoc_escape(const char **, const char **, int *);
313 void		  mandoc_msg_setoutfile(FILE *);
314 const char	 *mandoc_msg_getinfilename(void);
315 void		  mandoc_msg_setinfilename(const char *);
316 enum mandocerr	  mandoc_msg_getmin(void);
317 void		  mandoc_msg_setmin(enum mandocerr);
318 enum mandoclevel  mandoc_msg_getrc(void);
319 void		  mandoc_msg_setrc(enum mandoclevel);
320 void		  mandoc_msg(enum mandocerr, int, int, const char *, ...)
321 			__attribute__((__format__ (__printf__, 4, 5)));
322 void		  mandoc_msg_summary(void);
323 void		  mchars_alloc(void);
324 void		  mchars_free(void);
325 int		  mchars_num2char(const char *, size_t);
326 const char	 *mchars_uc2str(int);
327 int		  mchars_num2uc(const char *, size_t);
328 int		  mchars_spec2cp(const char *, size_t);
329 const char	 *mchars_spec2str(const char *, size_t, size_t *);
330