[BACK]Return to xml.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/xml.c, Revision 1.17

1.17    ! kristaps    1: /* $Id: xml.c,v 1.16 2008/12/05 19:45:15 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.17    ! kristaps   61:        /* LINTED */
1.16      kristaps   62:        for (res = 0, i = 0; ROFF_ARGMAX != (c = argc[i]); i++) {
                     63:                if ( ! ml_nputs(mbuf, " ", 1, &res))
                     64:                        return(-1);
1.11      kristaps   65:
1.16      kristaps   66:                if ( ! ml_puts(mbuf, tokargnames[c], &res))
                     67:                        return(-1);
                     68:                if ( ! ml_nputs(mbuf, "=\"", 2, &res))
                     69:                        return(-1);
                     70:                if (argv[i]) {
                     71:                        if ( ! ml_putstring(mbuf, argv[i], &res))
                     72:                                return(-1);
                     73:                } else if ( ! ml_nputs(mbuf, "true", 4, &res))
                     74:                        return(-1);
                     75:                if ( ! ml_nputs(mbuf, "\"", 1, &res))
                     76:                        return(-1);
                     77:        }
1.11      kristaps   78:
1.16      kristaps   79:        return((ssize_t)res);
1.11      kristaps   80: }
                     81:
1.10      kristaps   82:
                     83: static ssize_t
1.16      kristaps   84: xml_printtagname(struct md_mbuf *mbuf, enum md_ns ns, int tok)
1.8       kristaps   85: {
1.10      kristaps   86:        size_t           res;
1.8       kristaps   87:
1.10      kristaps   88:        res = 0;
1.6       kristaps   89:        switch (ns) {
1.9       kristaps   90:        case (MD_NS_BLOCK):
1.10      kristaps   91:                if ( ! ml_nputs(mbuf, "block:", 6, &res))
                     92:                        return(-1);
1.9       kristaps   93:                break;
1.16      kristaps   94:        case (MD_NS_INLINE):
                     95:                if ( ! ml_nputs(mbuf, "inline:", 7, &res))
                     96:                        return(-1);
                     97:                break;
1.11      kristaps   98:        case (MD_NS_BODY):
                     99:                if ( ! ml_nputs(mbuf, "body:", 5, &res))
                    100:                        return(-1);
                    101:                break;
                    102:        case (MD_NS_HEAD):
                    103:                if ( ! ml_nputs(mbuf, "head:", 5, &res))
                    104:                        return(-1);
                    105:                break;
1.9       kristaps  106:        default:
1.11      kristaps  107:                break;
1.6       kristaps  108:        }
                    109:
1.12      kristaps  110:        if ( ! ml_puts(mbuf, toknames[tok], &res))
1.10      kristaps  111:                return(-1);
1.16      kristaps  112:        return((ssize_t)res);
                    113: }
                    114:
                    115:
                    116: /* ARGSUSED */
                    117: static int
                    118: xml_begin(struct md_mbuf *mbuf, const struct md_args *args,
                    119:                const struct tm *tm, const char *os,
                    120:                const char *title, const char *section,
                    121:                const char *vol)
                    122: {
1.6       kristaps  123:
1.16      kristaps  124:        if ( ! ml_puts(mbuf, "<?xml version=\"1.0\" "
                    125:                                "encoding=\"UTF-8\"?>\n", NULL))
                    126:                return(0);
                    127:        return(ml_puts(mbuf, "<mdoc xmlns:block=\"block\" "
                    128:                                "xmlns:special=\"special\" "
                    129:                                "xmlns:inline=\"inline\">", NULL));
                    130: }
                    131:
                    132:
                    133: /* ARGSUSED */
                    134: static int
                    135: xml_end(struct md_mbuf *mbuf, const struct md_args *args)
                    136: {
                    137:
                    138:        return(ml_puts(mbuf, "</mdoc>", NULL));
                    139: }
                    140:
                    141:
                    142: /* ARGSUSED */
                    143: static ssize_t
                    144: xml_begintag(struct md_mbuf *mbuf, void *data,
                    145:                const struct md_args *args, enum md_ns ns,
                    146:                int tok, const int *argc, const char **argv)
                    147: {
                    148:        ssize_t          res, sz;
                    149:
                    150:        if (-1 == (res = xml_printtagname(mbuf, ns, tok)))
                    151:                return(-1);
                    152:        if (-1 == (sz = xml_printtagargs(mbuf, argc, argv)))
                    153:                return(-1);
                    154:        return(res + sz);
1.6       kristaps  155: }
                    156:
                    157:
1.12      kristaps  158: /* ARGSUSED */
1.10      kristaps  159: static ssize_t
1.15      kristaps  160: xml_endtag(struct md_mbuf *mbuf, void *data,
                    161:                const struct md_args *args, enum md_ns ns, int tok)
1.6       kristaps  162: {
                    163:
1.16      kristaps  164:        return(xml_printtagname(mbuf, ns, tok));
                    165: }
                    166:
                    167:
                    168: /* ARGSUSED */
                    169: int
                    170: xml_alloc(void **p)
                    171: {
                    172:
                    173:        return(1);
                    174: }
1.10      kristaps  175:
1.6       kristaps  176:
1.16      kristaps  177: /* ARGSUSED */
                    178: void
                    179: xml_free(void *p)
                    180: {
1.1       kristaps  181:
1.16      kristaps  182:        /* Do nothing. */
1.1       kristaps  183: }
                    184:
                    185:
                    186: int
1.10      kristaps  187: md_line_xml(void *data, char *buf)
1.1       kristaps  188: {
                    189:
1.10      kristaps  190:        return(mlg_line((struct md_mlg *)data, buf));
1.1       kristaps  191: }
                    192:
                    193:
                    194: int
                    195: md_exit_xml(void *data, int flush)
                    196: {
                    197:
1.10      kristaps  198:        return(mlg_exit((struct md_mlg *)data, flush));
1.1       kristaps  199: }
                    200:
                    201:
                    202: void *
                    203: md_init_xml(const struct md_args *args,
                    204:                struct md_mbuf *mbuf, const struct md_rbuf *rbuf)
                    205: {
1.16      kristaps  206:        struct ml_cbs    cbs;
                    207:
                    208:        cbs.ml_alloc = xml_alloc;
                    209:        cbs.ml_free = xml_free;
                    210:        cbs.ml_begintag = xml_begintag;
                    211:        cbs.ml_endtag = xml_endtag;
                    212:        cbs.ml_begin = xml_begin;
                    213:        cbs.ml_end = xml_end;
1.9       kristaps  214:
1.16      kristaps  215:        return(mlg_alloc(args, rbuf, mbuf, &cbs));
1.9       kristaps  216: }
                    217:

CVSweb