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

Annotation of mandoc/ml.c, Revision 1.4

1.4     ! kristaps    1: /* $Id: ml.c,v 1.3 2008/12/03 19:21:58 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:  */
                     19: #include <stdlib.h>
                     20: #include <string.h>
                     21:
                     22: #include "libmdocml.h"
                     23: #include "private.h"
1.2       kristaps   24: #include "ml.h"
1.1       kristaps   25:
1.2       kristaps   26: #ifdef __linux__
                     27: extern size_t            strlcat(char *, const char *, size_t);
                     28: extern size_t            strlcpy(char *, const char *, size_t);
                     29: #endif
1.1       kristaps   30:
                     31:
1.2       kristaps   32: int
                     33: ml_nputstring(struct md_mbuf *p,
                     34:                const char *buf, size_t sz, size_t *pos)
1.1       kristaps   35: {
                     36:        int              i;
1.4     ! kristaps   37:        const char      *seq;
        !            38:        size_t           ssz;
1.1       kristaps   39:
1.2       kristaps   40:        for (i = 0; i < (int)sz; i++) {
1.1       kristaps   41:                switch (buf[i]) {
1.4     ! kristaps   42:
        !            43:                /* Ampersand ml-escape. */
1.1       kristaps   44:                case ('&'):
1.4     ! kristaps   45:                        seq = "&amp;";
        !            46:                        ssz = 5;
1.1       kristaps   47:                        break;
1.4     ! kristaps   48:
        !            49:                /* Quotation ml-escape. */
1.1       kristaps   50:                case ('"'):
1.4     ! kristaps   51:                        seq = "&quot;";
        !            52:                        ssz = 6;
1.1       kristaps   53:                        break;
1.4     ! kristaps   54:
        !            55:                /* Lt ml-escape. */
1.1       kristaps   56:                case ('<'):
1.4     ! kristaps   57:                        seq = "&lt;";
        !            58:                        ssz = 4;
1.1       kristaps   59:                        break;
1.4     ! kristaps   60:
        !            61:                /* Gt ml-escape. */
1.1       kristaps   62:                case ('>'):
1.4     ! kristaps   63:                        seq = "&gt;";
        !            64:                        ssz = 4;
1.1       kristaps   65:                        break;
1.4     ! kristaps   66:
1.1       kristaps   67:                default:
1.4     ! kristaps   68:                        seq = &buf[i];
        !            69:                        ssz = 1;
1.1       kristaps   70:                        break;
                     71:                }
1.4     ! kristaps   72:
        !            73:                if ( ! ml_nputs(p, seq, ssz, pos))
        !            74:                        return(-1);
1.1       kristaps   75:        }
1.2       kristaps   76:        return(1);
1.1       kristaps   77: }
                     78:
                     79:
1.2       kristaps   80: int
                     81: ml_nputs(struct md_mbuf *p, const char *buf, size_t sz, size_t *pos)
1.1       kristaps   82: {
                     83:
1.4     ! kristaps   84:        if (0 == sz)
        !            85:                return(1);
        !            86:
1.2       kristaps   87:        if ( ! md_buf_puts(p, buf, sz))
                     88:                return(0);
                     89:
                     90:        *pos += sz;
                     91:        return(1);
1.1       kristaps   92: }
                     93:
                     94:
1.2       kristaps   95: int
1.3       kristaps   96: ml_puts(struct md_mbuf *p, const char *buf, size_t *pos)
                     97: {
                     98:        size_t           sz;
                     99:
1.4     ! kristaps  100:        if (0 == (sz = strlen(buf)))
        !           101:                return(1);
        !           102:
1.3       kristaps  103:        if ( ! md_buf_puts(p, buf, sz))
                    104:                return(0);
                    105:        *pos += sz;
                    106:        return(1);
                    107: }
                    108:
                    109:
                    110: int
1.2       kristaps  111: ml_putchars(struct md_mbuf *p, char buf, size_t count, size_t *pos)
1.1       kristaps  112: {
                    113:        size_t           i;
                    114:
1.2       kristaps  115:        for (i = 0; i < count; i++)
                    116:                if ( ! ml_nputs(p, &buf, 1, pos))
                    117:                        return(0);
1.1       kristaps  118:
1.2       kristaps  119:        return(1);
1.1       kristaps  120: }

CVSweb