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

Annotation of mandoc/man_macro.c, Revision 1.5

1.5     ! kristaps    1: /* $Id: man_macro.c,v 1.4 2009/03/23 15:41:09 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
                      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 <assert.h>
                     20: #include <ctype.h>
1.3       kristaps   21: #include <err.h> /* XXX */
1.1       kristaps   22: #include <stdlib.h>
                     23: #include <stdio.h>
                     24: #include <string.h>
                     25:
                     26: #include "libman.h"
                     27:
1.3       kristaps   28: static int              man_args(struct man *, int,
                     29:                                int *, char *, char **);
1.1       kristaps   30:
                     31:
1.3       kristaps   32: int
                     33: man_macro(struct man *man, int tok, int line,
                     34:                int ppos, int *pos, char *buf)
1.1       kristaps   35: {
1.3       kristaps   36:        int              w, la;
                     37:        char            *p;
                     38:        struct man_node *n;
1.1       kristaps   39:
1.3       kristaps   40:        if ( ! man_elem_alloc(man, line, ppos, tok))
1.1       kristaps   41:                return(0);
1.3       kristaps   42:        n = man->last;
                     43:        man->next = MAN_NEXT_CHILD;
1.1       kristaps   44:
                     45:        for (;;) {
                     46:                la = *pos;
1.3       kristaps   47:                w = man_args(man, line, pos, buf, &p);
1.1       kristaps   48:
1.3       kristaps   49:                if (-1 == w)
1.1       kristaps   50:                        return(0);
1.3       kristaps   51:                if (0 == w)
1.1       kristaps   52:                        break;
                     53:
1.3       kristaps   54:                if ( ! man_word_alloc(man, line, la, p))
                     55:                        return(0);
                     56:                man->next = MAN_NEXT_SIBLING;
                     57:        }
1.1       kristaps   58:
1.5     ! kristaps   59:        for ( ; man->last && man->last != n;
        !            60:                        man->last = man->last->parent)
        !            61:                if ( ! man_valid_post(man))
        !            62:                        return(0);
1.1       kristaps   63:
1.5     ! kristaps   64:        assert(man->last);
        !            65:        if ( ! man_valid_post(man))
        !            66:                return(0);
1.3       kristaps   67:        man->next = MAN_NEXT_SIBLING;
1.1       kristaps   68:
                     69:        return(1);
                     70: }
                     71:
1.3       kristaps   72:
1.4       kristaps   73: int
                     74: man_macroend(struct man *m)
                     75: {
                     76:
                     77:        /* TODO: validate & actions. */
                     78:        return(1);
                     79: }
                     80:
                     81:
1.3       kristaps   82: /* ARGSUSED */
1.4       kristaps   83: static int
1.3       kristaps   84: man_args(struct man *man, int line,
                     85:                int *pos, char *buf, char **v)
                     86: {
                     87:
                     88:        if (0 == buf[*pos])
                     89:                return(0);
                     90:
                     91:        /* First parse non-quoted strings. */
                     92:
                     93:        if ('\"' != buf[*pos]) {
                     94:                *v = &buf[*pos];
                     95:
                     96:                while (buf[*pos]) {
                     97:                        if (' ' == buf[*pos])
                     98:                                if ('\\' != buf[*pos - 1])
                     99:                                        break;
                    100:                        (*pos)++;
                    101:                }
                    102:
                    103:                if (0 == buf[*pos])
                    104:                        return(1);
                    105:
                    106:                buf[(*pos)++] = 0;
                    107:
                    108:                if (0 == buf[*pos])
                    109:                        return(1);
                    110:
                    111:                while (buf[*pos] && ' ' == buf[*pos])
                    112:                        (*pos)++;
                    113:
                    114:                if (buf[*pos])
                    115:                        return(1);
                    116:
                    117:                warnx("tail whitespace");
                    118:                return(-1);
                    119:        }
                    120:
                    121:        /*
                    122:         * If we're a quoted string (and quoted strings are allowed),
                    123:         * then parse ahead to the next quote.  If none's found, it's an
                    124:         * error.  After, parse to the next word.
                    125:         */
                    126:
                    127:        *v = &buf[++(*pos)];
                    128:
                    129:        while (buf[*pos] && '\"' != buf[*pos])
                    130:                (*pos)++;
                    131:
                    132:        if (0 == buf[*pos]) {
                    133:                warnx("unterminated quotation");
                    134:                return(-1);
                    135:        }
                    136:
                    137:        buf[(*pos)++] = 0;
                    138:        if (0 == buf[*pos])
                    139:                return(1);
                    140:
                    141:        while (buf[*pos] && ' ' == buf[*pos])
                    142:                (*pos)++;
                    143:
                    144:        if (buf[*pos])
                    145:                return(1);
                    146:
                    147:        warnx("tail whitespace");
                    148:        return(-1);
                    149: }

CVSweb