Annotation of mandoc/xml.c, Revision 1.16
1.16 ! kristaps 1: /* $Id: xml.c,v 1.15 2008/12/05 17:43:14 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2008 Kristaps Dzonsons <kristaps@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
7: * above copyright notice and this permission notice appear in all
8: * copies.
9: *
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.
18: */
1.16 ! kristaps 19: #include <assert.h>
1.1 kristaps 20: #include <stdlib.h>
21: #include <string.h>
22:
23: #include "libmdocml.h"
24: #include "private.h"
1.10 kristaps 25: #include "ml.h"
1.9 kristaps 26:
1.6 kristaps 27:
1.16 ! kristaps 28: static int xml_alloc(void **);
! 29: static void xml_free(void *);
1.15 kristaps 30: static ssize_t xml_endtag(struct md_mbuf *, void *,
1.10 kristaps 31: const struct md_args *,
32: enum md_ns, int);
1.15 kristaps 33: static ssize_t xml_begintag(struct md_mbuf *, void *,
1.10 kristaps 34: const struct md_args *,
35: enum md_ns, int,
36: const int *, const char **);
1.13 kristaps 37: static int xml_begin(struct md_mbuf *,
38: const struct md_args *,
39: const struct tm *,
40: const char *, const char *,
41: const char *, const char *);
1.11 kristaps 42: static int xml_end(struct md_mbuf *,
43: const struct md_args *);
1.16 ! kristaps 44: static ssize_t xml_printtagname(struct md_mbuf *,
! 45: enum md_ns, int);
! 46: static ssize_t xml_printtagargs(struct md_mbuf *,
! 47: const int *, const char **);
1.11 kristaps 48:
49:
1.16 ! kristaps 50: static ssize_t
! 51: xml_printtagargs(struct md_mbuf *mbuf, const int *argc,
! 52: const char **argv)
1.11 kristaps 53: {
1.16 ! kristaps 54: int i, c;
1.11 kristaps 55: size_t res;
56:
1.16 ! kristaps 57: if (NULL == argc || NULL == argv)
1.11 kristaps 58: return(0);
1.16 ! kristaps 59: assert(argc && argv);
1.11 kristaps 60:
1.16 ! kristaps 61: for (res = 0, i = 0; ROFF_ARGMAX != (c = argc[i]); i++) {
! 62: if ( ! ml_nputs(mbuf, " ", 1, &res))
! 63: return(-1);
1.11 kristaps 64:
1.16 ! kristaps 65: if ( ! ml_puts(mbuf, tokargnames[c], &res))
! 66: return(-1);
! 67: if ( ! ml_nputs(mbuf, "=\"", 2, &res))
! 68: return(-1);
! 69: if (argv[i]) {
! 70: if ( ! ml_putstring(mbuf, argv[i], &res))
! 71: return(-1);
! 72: } else if ( ! ml_nputs(mbuf, "true", 4, &res))
! 73: return(-1);
! 74: if ( ! ml_nputs(mbuf, "\"", 1, &res))
! 75: return(-1);
! 76: }
1.11 kristaps 77:
1.16 ! kristaps 78: return((ssize_t)res);
1.11 kristaps 79: }
80:
1.10 kristaps 81:
82: static ssize_t
1.16 ! kristaps 83: xml_printtagname(struct md_mbuf *mbuf, enum md_ns ns, int tok)
1.8 kristaps 84: {
1.10 kristaps 85: size_t res;
1.8 kristaps 86:
1.10 kristaps 87: res = 0;
1.6 kristaps 88: switch (ns) {
1.9 kristaps 89: case (MD_NS_BLOCK):
1.10 kristaps 90: if ( ! ml_nputs(mbuf, "block:", 6, &res))
91: return(-1);
1.9 kristaps 92: break;
1.16 ! kristaps 93: case (MD_NS_INLINE):
! 94: if ( ! ml_nputs(mbuf, "inline:", 7, &res))
! 95: return(-1);
! 96: break;
1.11 kristaps 97: case (MD_NS_BODY):
98: if ( ! ml_nputs(mbuf, "body:", 5, &res))
99: return(-1);
100: break;
101: case (MD_NS_HEAD):
102: if ( ! ml_nputs(mbuf, "head:", 5, &res))
103: return(-1);
104: break;
1.9 kristaps 105: default:
1.11 kristaps 106: break;
1.6 kristaps 107: }
108:
1.12 kristaps 109: if ( ! ml_puts(mbuf, toknames[tok], &res))
1.10 kristaps 110: return(-1);
1.16 ! kristaps 111: return((ssize_t)res);
! 112: }
! 113:
! 114:
! 115: /* ARGSUSED */
! 116: static int
! 117: xml_begin(struct md_mbuf *mbuf, const struct md_args *args,
! 118: const struct tm *tm, const char *os,
! 119: const char *title, const char *section,
! 120: const char *vol)
! 121: {
1.6 kristaps 122:
1.16 ! kristaps 123: if ( ! ml_puts(mbuf, "<?xml version=\"1.0\" "
! 124: "encoding=\"UTF-8\"?>\n", NULL))
! 125: return(0);
! 126: return(ml_puts(mbuf, "<mdoc xmlns:block=\"block\" "
! 127: "xmlns:special=\"special\" "
! 128: "xmlns:inline=\"inline\">", NULL));
! 129: }
! 130:
! 131:
! 132: /* ARGSUSED */
! 133: static int
! 134: xml_end(struct md_mbuf *mbuf, const struct md_args *args)
! 135: {
! 136:
! 137: return(ml_puts(mbuf, "</mdoc>", NULL));
! 138: }
! 139:
! 140:
! 141: /* ARGSUSED */
! 142: static ssize_t
! 143: xml_begintag(struct md_mbuf *mbuf, void *data,
! 144: const struct md_args *args, enum md_ns ns,
! 145: int tok, const int *argc, const char **argv)
! 146: {
! 147: ssize_t res, sz;
! 148:
! 149: if (-1 == (res = xml_printtagname(mbuf, ns, tok)))
! 150: return(-1);
! 151: if (-1 == (sz = xml_printtagargs(mbuf, argc, argv)))
! 152: return(-1);
! 153: return(res + sz);
1.6 kristaps 154: }
155:
156:
1.12 kristaps 157: /* ARGSUSED */
1.10 kristaps 158: static ssize_t
1.15 kristaps 159: xml_endtag(struct md_mbuf *mbuf, void *data,
160: const struct md_args *args, enum md_ns ns, int tok)
1.6 kristaps 161: {
162:
1.16 ! kristaps 163: return(xml_printtagname(mbuf, ns, tok));
! 164: }
! 165:
! 166:
! 167: /* ARGSUSED */
! 168: int
! 169: xml_alloc(void **p)
! 170: {
! 171:
! 172: return(1);
! 173: }
1.10 kristaps 174:
1.6 kristaps 175:
1.16 ! kristaps 176: /* ARGSUSED */
! 177: void
! 178: xml_free(void *p)
! 179: {
1.1 kristaps 180:
1.16 ! kristaps 181: /* Do nothing. */
1.1 kristaps 182: }
183:
184:
185: int
1.10 kristaps 186: md_line_xml(void *data, char *buf)
1.1 kristaps 187: {
188:
1.10 kristaps 189: return(mlg_line((struct md_mlg *)data, buf));
1.1 kristaps 190: }
191:
192:
193: int
194: md_exit_xml(void *data, int flush)
195: {
196:
1.10 kristaps 197: return(mlg_exit((struct md_mlg *)data, flush));
1.1 kristaps 198: }
199:
200:
201: void *
202: md_init_xml(const struct md_args *args,
203: struct md_mbuf *mbuf, const struct md_rbuf *rbuf)
204: {
1.16 ! kristaps 205: struct ml_cbs cbs;
! 206:
! 207: cbs.ml_alloc = xml_alloc;
! 208: cbs.ml_free = xml_free;
! 209: cbs.ml_begintag = xml_begintag;
! 210: cbs.ml_endtag = xml_endtag;
! 211: cbs.ml_begin = xml_begin;
! 212: cbs.ml_end = xml_end;
1.9 kristaps 213:
1.16 ! kristaps 214: return(mlg_alloc(args, rbuf, mbuf, &cbs));
1.9 kristaps 215: }
216:
CVSweb