xref: /dragonfly/contrib/mdocml/libmdoc.h (revision 38b930d0)
1 /*	$Id: libmdoc.h,v 1.81 2012/11/17 00:26:33 schwarze Exp $ */
2 /*
3  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 #ifndef LIBMDOC_H
18 #define LIBMDOC_H
19 
20 enum	mdoc_next {
21 	MDOC_NEXT_SIBLING = 0,
22 	MDOC_NEXT_CHILD
23 };
24 
25 struct	mdoc {
26 	struct mparse	 *parse; /* parse pointer */
27 	char		 *defos; /* default argument for .Os */
28 	int		  flags; /* parse flags */
29 #define	MDOC_HALT	 (1 << 0) /* error in parse: halt */
30 #define	MDOC_LITERAL	 (1 << 1) /* in a literal scope */
31 #define	MDOC_PBODY	 (1 << 2) /* in the document body */
32 #define	MDOC_NEWLINE	 (1 << 3) /* first macro/text in a line */
33 #define	MDOC_PHRASELIT	 (1 << 4) /* literal within a partila phrase */
34 #define	MDOC_PPHRASE	 (1 << 5) /* within a partial phrase */
35 #define	MDOC_FREECOL	 (1 << 6) /* `It' invocation should close */
36 #define	MDOC_SYNOPSIS	 (1 << 7) /* SYNOPSIS-style formatting */
37 	enum mdoc_next	  next; /* where to put the next node */
38 	struct mdoc_node *last; /* the last node parsed */
39 	struct mdoc_node *first; /* the first node parsed */
40 	struct mdoc_meta  meta; /* document meta-data */
41 	enum mdoc_sec	  lastnamed;
42 	enum mdoc_sec	  lastsec;
43 	struct roff	 *roff;
44 };
45 
46 #define	MACRO_PROT_ARGS	struct mdoc *mdoc, \
47 			enum mdoct tok, \
48 			int line, \
49 			int ppos, \
50 			int *pos, \
51 			char *buf
52 
53 struct	mdoc_macro {
54 	int		(*fp)(MACRO_PROT_ARGS);
55 	int		  flags;
56 #define	MDOC_CALLABLE	 (1 << 0)
57 #define	MDOC_PARSED	 (1 << 1)
58 #define	MDOC_EXPLICIT	 (1 << 2)
59 #define	MDOC_PROLOGUE	 (1 << 3)
60 #define	MDOC_IGNDELIM	 (1 << 4)
61 	/* Reserved words in arguments treated as text. */
62 };
63 
64 enum	margserr {
65 	ARGS_ERROR,
66 	ARGS_EOLN, /* end-of-line */
67 	ARGS_WORD, /* normal word */
68 	ARGS_PUNCT, /* series of punctuation */
69 	ARGS_QWORD, /* quoted word */
70 	ARGS_PHRASE, /* Ta'd phrase (-column) */
71 	ARGS_PPHRASE, /* tabbed phrase (-column) */
72 	ARGS_PEND /* last phrase (-column) */
73 };
74 
75 enum	margverr {
76 	ARGV_ERROR,
77 	ARGV_EOLN, /* end of line */
78 	ARGV_ARG, /* valid argument */
79 	ARGV_WORD /* normal word (or bad argument---same thing) */
80 };
81 
82 /*
83  * A punctuation delimiter is opening, closing, or "middle mark"
84  * punctuation.  These govern spacing.
85  * Opening punctuation (e.g., the opening parenthesis) suppresses the
86  * following space; closing punctuation (e.g., the closing parenthesis)
87  * suppresses the leading space; middle punctuation (e.g., the vertical
88  * bar) can do either.  The middle punctuation delimiter bends the rules
89  * depending on usage.
90  */
91 enum	mdelim {
92 	DELIM_NONE = 0,
93 	DELIM_OPEN,
94 	DELIM_MIDDLE,
95 	DELIM_CLOSE,
96 	DELIM_MAX
97 };
98 
99 extern	const struct mdoc_macro *const mdoc_macros;
100 
101 __BEGIN_DECLS
102 
103 #define		  mdoc_pmsg(mdoc, l, p, t) \
104 		  mandoc_msg((t), (mdoc)->parse, (l), (p), NULL)
105 #define		  mdoc_nmsg(mdoc, n, t) \
106 		  mandoc_msg((t), (mdoc)->parse, (n)->line, (n)->pos, NULL)
107 int		  mdoc_macro(MACRO_PROT_ARGS);
108 int		  mdoc_word_alloc(struct mdoc *,
109 			int, int, const char *);
110 int		  mdoc_elem_alloc(struct mdoc *, int, int,
111 			enum mdoct, struct mdoc_arg *);
112 int		  mdoc_block_alloc(struct mdoc *, int, int,
113 			enum mdoct, struct mdoc_arg *);
114 int		  mdoc_head_alloc(struct mdoc *, int, int, enum mdoct);
115 int		  mdoc_tail_alloc(struct mdoc *, int, int, enum mdoct);
116 int		  mdoc_body_alloc(struct mdoc *, int, int, enum mdoct);
117 int		  mdoc_endbody_alloc(struct mdoc *, int, int, enum mdoct,
118 			struct mdoc_node *, enum mdoc_endbody);
119 void		  mdoc_node_delete(struct mdoc *, struct mdoc_node *);
120 int		  mdoc_node_relink(struct mdoc *, struct mdoc_node *);
121 void		  mdoc_hash_init(void);
122 enum mdoct	  mdoc_hash_find(const char *);
123 const char	 *mdoc_a2att(const char *);
124 const char	 *mdoc_a2lib(const char *);
125 const char	 *mdoc_a2st(const char *);
126 const char	 *mdoc_a2arch(const char *);
127 const char	 *mdoc_a2vol(const char *);
128 int		  mdoc_valid_pre(struct mdoc *, struct mdoc_node *);
129 int		  mdoc_valid_post(struct mdoc *);
130 enum margverr	  mdoc_argv(struct mdoc *, int, enum mdoct,
131 			struct mdoc_arg **, int *, char *);
132 void		  mdoc_argv_free(struct mdoc_arg *);
133 enum margserr	  mdoc_args(struct mdoc *, int,
134 			int *, char *, enum mdoct, char **);
135 enum margserr	  mdoc_zargs(struct mdoc *, int,
136 			int *, char *, char **);
137 int		  mdoc_macroend(struct mdoc *);
138 enum mdelim	  mdoc_isdelim(const char *);
139 
140 __END_DECLS
141 
142 #endif /*!LIBMDOC_H*/
143