xref: /illumos-gate/usr/src/cmd/mandoc/roff.h (revision 371584c2eae4cf827fd406ba26c14f021adaaa70)
1 /*	$OpenBSD$	*/
2 /*
3  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2013, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
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 
19 struct	mdoc_arg;
20 union	mdoc_data;
21 
22 enum	roff_macroset {
23 	MACROSET_NONE = 0,
24 	MACROSET_MDOC,
25 	MACROSET_MAN
26 };
27 
28 enum	roff_sec {
29 	SEC_NONE = 0,
30 	SEC_NAME,
31 	SEC_LIBRARY,
32 	SEC_SYNOPSIS,
33 	SEC_DESCRIPTION,
34 	SEC_CONTEXT,
35 	SEC_IMPLEMENTATION,	/* IMPLEMENTATION NOTES */
36 	SEC_RETURN_VALUES,
37 	SEC_ENVIRONMENT,
38 	SEC_FILES,
39 	SEC_EXIT_STATUS,
40 	SEC_EXAMPLES,
41 	SEC_DIAGNOSTICS,
42 	SEC_COMPATIBILITY,
43 	SEC_ERRORS,
44 	SEC_SEE_ALSO,
45 	SEC_STANDARDS,
46 	SEC_HISTORY,
47 	SEC_AUTHORS,
48 	SEC_CAVEATS,
49 	SEC_BUGS,
50 	SEC_SECURITY,
51 	SEC_CUSTOM,
52 	SEC__MAX
53 };
54 
55 enum	roff_type {
56 	ROFFT_ROOT,
57 	ROFFT_BLOCK,
58 	ROFFT_HEAD,
59 	ROFFT_BODY,
60 	ROFFT_TAIL,
61 	ROFFT_ELEM,
62 	ROFFT_TEXT,
63 	ROFFT_TBL,
64 	ROFFT_EQN
65 };
66 
67 enum	roff_next {
68 	ROFF_NEXT_SIBLING = 0,
69 	ROFF_NEXT_CHILD
70 };
71 
72 /*
73  * Indicates that a BODY's formatting has ended, but
74  * the scope is still open.  Used for badly nested blocks.
75  */
76 enum	mdoc_endbody {
77 	ENDBODY_NOT = 0,
78 	ENDBODY_SPACE,	/* Is broken: append a space. */
79 	ENDBODY_NOSPACE	/* Is broken: don't append a space. */
80 };
81 
82 struct	roff_node {
83 	struct roff_node *parent;  /* Parent AST node. */
84 	struct roff_node *child;   /* First child AST node. */
85 	struct roff_node *last;    /* Last child AST node. */
86 	struct roff_node *next;    /* Sibling AST node. */
87 	struct roff_node *prev;    /* Prior sibling AST node. */
88 	struct roff_node *head;    /* BLOCK */
89 	struct roff_node *body;    /* BLOCK/ENDBODY */
90 	struct roff_node *tail;    /* BLOCK */
91 	struct mdoc_arg	 *args;    /* BLOCK/ELEM */
92 	union mdoc_data	 *norm;    /* Normalized arguments. */
93 	char		 *string;  /* TEXT */
94 	const struct tbl_span *span; /* TBL */
95 	const struct eqn *eqn;	   /* EQN */
96 	int		  line;    /* Input file line number. */
97 	int		  pos;     /* Input file column number. */
98 	int		  tok;     /* Request or macro ID. */
99 #define	TOKEN_NONE	 (-1)	   /* No request or macro. */
100 	int		  flags;
101 #define	MDOC_VALID	 (1 << 0)  /* Has been validated. */
102 #define	MDOC_ENDED	 (1 << 1)  /* Gone past body end mark. */
103 #define MDOC_EOS	 (1 << 2)  /* At sentence boundary. */
104 #define	MDOC_LINE	 (1 << 3)  /* First macro/text on line. */
105 #define MDOC_SYNPRETTY	 (1 << 4)  /* SYNOPSIS-style formatting. */
106 #define MDOC_BROKEN	 (1 << 5)  /* Must validate parent when ending. */
107 #define	MDOC_DELIMO	 (1 << 6)
108 #define	MDOC_DELIMC	 (1 << 7)
109 #define	MAN_VALID	  MDOC_VALID
110 #define	MAN_EOS		  MDOC_EOS
111 #define	MAN_LINE	  MDOC_LINE
112 	int		  prev_font; /* Before entering this node. */
113 	int		  aux;     /* Decoded node data, type-dependent. */
114 	enum roff_type	  type;    /* AST node type. */
115 	enum roff_sec	  sec;     /* Current named section. */
116 	enum mdoc_endbody end;     /* BODY */
117 };
118 
119 struct	roff_meta {
120 	char		 *msec;    /* Manual section, usually a digit. */
121 	char		 *vol;     /* Manual volume title. */
122 	char		 *os;      /* Operating system. */
123 	char		 *arch;    /* Machine architecture. */
124 	char		 *title;   /* Manual title, usually CAPS. */
125 	char		 *name;    /* Leading manual name. */
126 	char		 *date;    /* Normalized date. */
127 	int		  hasbody; /* Document is not empty. */
128 };
129 
130 struct	roff_man {
131 	struct roff_meta  meta;    /* Document meta-data. */
132 	struct mparse	 *parse;   /* Parse pointer. */
133 	struct roff	 *roff;    /* Roff parser state data. */
134 	const char	 *defos;   /* Default operating system. */
135 	struct roff_node *first;   /* The first node parsed. */
136 	struct roff_node *last;    /* The last node parsed. */
137 	struct roff_node *last_es; /* The most recent Es node. */
138 	int		  quick;   /* Abort parse early. */
139 	int		  flags;   /* Parse flags. */
140 #define	MDOC_LITERAL	 (1 << 1)  /* In a literal scope. */
141 #define	MDOC_PBODY	 (1 << 2)  /* In the document body. */
142 #define	MDOC_NEWLINE	 (1 << 3)  /* First macro/text in a line. */
143 #define	MDOC_PHRASE	 (1 << 4)  /* In a Bl -column phrase. */
144 #define	MDOC_PHRASELIT	 (1 << 5)  /* Literal within a phrase. */
145 #define	MDOC_FREECOL	 (1 << 6)  /* `It' invocation should close. */
146 #define	MDOC_SYNOPSIS	 (1 << 7)  /* SYNOPSIS-style formatting. */
147 #define	MDOC_KEEP	 (1 << 8)  /* In a word keep. */
148 #define	MDOC_SMOFF	 (1 << 9)  /* Spacing is off. */
149 #define	MDOC_NODELIMC	 (1 << 10) /* Disable closing delimiter handling. */
150 #define	MAN_ELINE	 (1 << 11) /* Next-line element scope. */
151 #define	MAN_BLINE	 (1 << 12) /* Next-line block scope. */
152 #define	MDOC_PHRASEQF	 (1 << 13) /* Quote first word encountered. */
153 #define	MDOC_PHRASEQL	 (1 << 14) /* Quote last word of this phrase. */
154 #define	MDOC_PHRASEQN	 (1 << 15) /* Quote first word of the next phrase. */
155 #define	MAN_LITERAL	  MDOC_LITERAL
156 #define	MAN_NEWLINE	  MDOC_NEWLINE
157 	enum roff_macroset macroset; /* Kind of high-level macros used. */
158 	enum roff_sec	  lastsec; /* Last section seen. */
159 	enum roff_sec	  lastnamed; /* Last standard section seen. */
160 	enum roff_next	  next;    /* Where to put the next node. */
161 };
162 
163 
164 void		 deroff(char **, const struct roff_node *);
165