Home | History | Annotate | Line # | Download | only in dist
libmdoc.h revision 1.1
      1 /*	$Vendor-Id: libmdoc.h,v 1.29 2009/10/15 02:56:51 kristaps Exp $ */
      2 /*
      3  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps (at) kth.se>
      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 #include "mdoc.h"
     21 
     22 enum	mdoc_next {
     23 	MDOC_NEXT_SIBLING = 0,
     24 	MDOC_NEXT_CHILD
     25 };
     26 
     27 struct	mdoc {
     28 	void		 *data;
     29 	struct mdoc_cb	  cb;
     30 	int		  flags;
     31 #define	MDOC_HALT	 (1 << 0)	/* Error in parse. Halt. */
     32 #define	MDOC_LITERAL	 (1 << 1)	/* In a literal scope. */
     33 #define	MDOC_PBODY	 (1 << 2)	/* In the document body. */
     34 	int		  pflags;
     35 	enum mdoc_next	  next;
     36 	struct mdoc_node *last;
     37 	struct mdoc_node *first;
     38 	struct mdoc_meta  meta;
     39 	enum mdoc_sec	  lastnamed;
     40 	enum mdoc_sec	  lastsec;
     41 };
     42 
     43 enum	merr {
     44 	ETAILWS = 0,
     45 	EQUOTPARM,
     46 	EQUOTTERM,
     47 	EMALLOC,
     48 	EARGVAL,
     49 	EBODYPROL,
     50 	EPROLBODY,
     51 	ETEXTPROL,
     52 	ENOBLANK,
     53 	ETOOLONG,
     54 	EESCAPE,
     55 	EPRINT,
     56 	ENODAT,
     57 	ENOPROLOGUE,
     58 	ELINE,
     59 	EATT,
     60 	ENAME,
     61 	ELISTTYPE,
     62 	EDISPTYPE,
     63 	EMULTIDISP,
     64 	EMULTILIST,
     65 	ESECNAME,
     66 	ENAMESECINC,
     67 	EARGREP,
     68 	EBOOL,
     69 	ECOLMIS,
     70 	ENESTDISP,
     71 	EMISSWIDTH,
     72 	EWRONGMSEC,
     73 	ESECOOO,
     74 	ESECREP,
     75 	EBADSTAND,
     76 	ENOMULTILINE,
     77 	EMULTILINE,
     78 	ENOLINE,
     79 	EPROLOOO,
     80 	EPROLREP,
     81 	EBADMSEC,
     82 	EBADSEC,
     83 	EFONT,
     84 	EBADDATE,
     85 	ENUMFMT,
     86 	ENOWIDTH,
     87 	EUTSNAME,
     88 	EOBS,
     89 	EIMPBRK,
     90 	EIGNE,
     91 	EOPEN,
     92 	EQUOTPHR,
     93 	ENOCTX,
     94 	ELIB,
     95 	EBADCHILD,
     96 	ENOTYPE,
     97 	MERRMAX
     98 };
     99 
    100 #define	MACRO_PROT_ARGS	struct mdoc *m, int tok, int line, \
    101 			int ppos, int *pos, char *buf
    102 
    103 struct	mdoc_macro {
    104 	int		(*fp)(MACRO_PROT_ARGS);
    105 	int		  flags;
    106 #define	MDOC_CALLABLE	 (1 << 0)
    107 #define	MDOC_PARSED	 (1 << 1)
    108 #define	MDOC_EXPLICIT	 (1 << 2)
    109 #define	MDOC_PROLOGUE	 (1 << 3)
    110 #define	MDOC_IGNDELIM	 (1 << 4)
    111 	/* Reserved words in arguments treated as text. */
    112 };
    113 
    114 extern	const struct mdoc_macro *const mdoc_macros;
    115 
    116 __BEGIN_DECLS
    117 
    118 #define		  mdoc_perr(m, l, p, t) \
    119 		  mdoc_err((m), (l), (p), 1, (t))
    120 #define		  mdoc_pwarn(m, l, p, t) \
    121 		  mdoc_err((m), (l), (p), 0, (t))
    122 #define		  mdoc_nerr(m, n, t) \
    123 		  mdoc_err((m), (n)->line, (n)->pos, 1, (t))
    124 #define		  mdoc_nwarn(m, n, t) \
    125 		  mdoc_err((m), (n)->line, (n)->pos, 0, (t))
    126 
    127 int		  mdoc_err(struct mdoc *, int, int, int, enum merr);
    128 int		  mdoc_verr(struct mdoc *, int, int, const char *, ...);
    129 int		  mdoc_vwarn(struct mdoc *, int, int, const char *, ...);
    130 
    131 int		  mdoc_macro(MACRO_PROT_ARGS);
    132 int		  mdoc_word_alloc(struct mdoc *,
    133 			int, int, const char *);
    134 int		  mdoc_elem_alloc(struct mdoc *, int, int,
    135 			int, struct mdoc_arg *);
    136 int		  mdoc_block_alloc(struct mdoc *, int, int,
    137 			int, struct mdoc_arg *);
    138 int		  mdoc_head_alloc(struct mdoc *, int, int, int);
    139 int		  mdoc_tail_alloc(struct mdoc *, int, int, int);
    140 int		  mdoc_body_alloc(struct mdoc *, int, int, int);
    141 void		  mdoc_node_free(struct mdoc_node *);
    142 void		  mdoc_node_freelist(struct mdoc_node *);
    143 void		  mdoc_hash_init(void);
    144 int		  mdoc_hash_find(const char *);
    145 int		  mdoc_iscdelim(char);
    146 int		  mdoc_isdelim(const char *);
    147 size_t		  mdoc_isescape(const char *);
    148 enum	mdoc_sec  mdoc_atosec(const char *);
    149 time_t		  mdoc_atotime(const char *);
    150 
    151 size_t		  mdoc_macro2len(int);
    152 const char	 *mdoc_a2att(const char *);
    153 const char	 *mdoc_a2lib(const char *);
    154 const char	 *mdoc_a2st(const char *);
    155 const char	 *mdoc_a2arch(const char *);
    156 const char	 *mdoc_a2vol(const char *);
    157 const char	 *mdoc_a2msec(const char *);
    158 int		  mdoc_valid_pre(struct mdoc *,
    159 			const struct mdoc_node *);
    160 int		  mdoc_valid_post(struct mdoc *);
    161 int		  mdoc_action_pre(struct mdoc *,
    162 			const struct mdoc_node *);
    163 int		  mdoc_action_post(struct mdoc *);
    164 int		  mdoc_argv(struct mdoc *, int, int,
    165 			struct mdoc_arg **, int *, char *);
    166 #define	ARGV_ERROR	(-1)
    167 #define	ARGV_EOLN	(0)
    168 #define	ARGV_ARG	(1)
    169 #define	ARGV_WORD	(2)
    170 void		  mdoc_argv_free(struct mdoc_arg *);
    171 int		  mdoc_args(struct mdoc *, int,
    172 			int *, char *, int, char **);
    173 int		  mdoc_zargs(struct mdoc *, int,
    174 			int *, char *, int, char **);
    175 #define	ARGS_DELIM	(1 << 1)	/* See args(). */
    176 #define	ARGS_TABSEP	(1 << 2)	/* See args(). */
    177 #define	ARGS_NOWARN	(1 << 3)	/* See args(). */
    178 #define	ARGS_ERROR	(-1)
    179 #define	ARGS_EOLN	(0)
    180 #define	ARGS_WORD	(1)
    181 #define	ARGS_PUNCT	(2)
    182 #define	ARGS_QWORD	(3)
    183 #define	ARGS_PHRASE	(4)
    184 int		  mdoc_macroend(struct mdoc *);
    185 
    186 __END_DECLS
    187 
    188 #endif /*!LIBMDOC_H*/
    189