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

Annotation of mandoc/demandoc.c, Revision 1.5

1.5     ! kristaps    1: /*     $Id: demandoc.c,v 1.4 2011/09/01 20:55:50 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
                      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: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
                     21: #include <assert.h>
1.4       kristaps   22: #include <ctype.h>
1.1       kristaps   23: #include <getopt.h>
                     24: #include <stdio.h>
                     25: #include <stdlib.h>
                     26: #include <string.h>
1.2       kristaps   27: #include <unistd.h>
1.1       kristaps   28:
                     29: #include "man.h"
                     30: #include "mdoc.h"
                     31: #include "mandoc.h"
                     32:
1.4       kristaps   33: static void     pline(int, int *, int *, int);
                     34: static void     pman(const struct man_node *, int *, int *, int);
                     35: static void     pmandoc(struct mparse *, int, const char *, int);
                     36: static void     pmdoc(const struct mdoc_node *, int *, int *, int);
                     37: static void     pstring(const char *, int, int *, int);
1.1       kristaps   38: static void     usage(void);
                     39:
                     40: static const char       *progname;
                     41:
                     42: int
                     43: main(int argc, char *argv[])
                     44: {
                     45:        struct mparse   *mp;
1.4       kristaps   46:        int              ch, i, list;
1.1       kristaps   47:        extern int       optind;
                     48:
                     49:        progname = strrchr(argv[0], '/');
                     50:        if (progname == NULL)
                     51:                progname = argv[0];
                     52:        else
                     53:                ++progname;
                     54:
                     55:        mp = NULL;
1.4       kristaps   56:        list = 0;
1.1       kristaps   57:
1.4       kristaps   58:        while (-1 != (ch = getopt(argc, argv, "ikm:pw")))
1.1       kristaps   59:                switch (ch) {
1.4       kristaps   60:                case ('i'):
                     61:                        /* FALLTHROUGH */
                     62:                case ('k'):
                     63:                        /* FALLTHROUGH */
                     64:                case ('m'):
                     65:                        /* FALLTHROUGH */
                     66:                case ('p'):
                     67:                        break;
                     68:                case ('w'):
                     69:                        list = 1;
                     70:                        break;
1.1       kristaps   71:                default:
                     72:                        usage();
                     73:                        return((int)MANDOCLEVEL_BADARG);
                     74:                }
                     75:
                     76:        argc -= optind;
                     77:        argv += optind;
                     78:
                     79:        mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
                     80:        assert(mp);
                     81:
                     82:        if (0 == argc)
1.4       kristaps   83:                pmandoc(mp, STDIN_FILENO, "<stdin>", list);
1.1       kristaps   84:
                     85:        for (i = 0; i < argc; i++) {
                     86:                mparse_reset(mp);
1.4       kristaps   87:                pmandoc(mp, -1, argv[i], list);
1.1       kristaps   88:        }
                     89:
                     90:        mparse_free(mp);
1.3       kristaps   91:        return((int)MANDOCLEVEL_OK);
1.1       kristaps   92: }
                     93:
                     94: static void
                     95: usage(void)
                     96: {
                     97:
1.4       kristaps   98:        fprintf(stderr, "usage: %s [-w] [files...]\n", progname);
1.1       kristaps   99: }
                    100:
                    101: static void
1.4       kristaps  102: pmandoc(struct mparse *mp, int fd, const char *fn, int list)
1.1       kristaps  103: {
                    104:        struct mdoc     *mdoc;
                    105:        struct man      *man;
                    106:        int              line, col;
                    107:
                    108:        if (mparse_readfd(mp, fd, fn) >= MANDOCLEVEL_FATAL) {
                    109:                fprintf(stderr, "%s: Parse failure\n", fn);
                    110:                return;
                    111:        }
                    112:
                    113:        mparse_result(mp, &mdoc, &man);
                    114:        line = 1;
                    115:        col = 0;
                    116:
                    117:        if (mdoc)
1.4       kristaps  118:                pmdoc(mdoc_node(mdoc), &line, &col, list);
1.1       kristaps  119:        else if (man)
1.4       kristaps  120:                pman(man_node(man), &line, &col, list);
1.1       kristaps  121:        else
                    122:                return;
                    123:
1.5     ! kristaps  124:        if ( ! list)
        !           125:                putchar('\n');
1.1       kristaps  126: }
                    127:
                    128: /*
                    129:  * Strip the escapes out of a string, emitting the results.
                    130:  */
                    131: static void
1.4       kristaps  132: pstring(const char *p, int col, int *colp, int list)
1.1       kristaps  133: {
                    134:        enum mandoc_esc  esc;
1.5     ! kristaps  135:        const char      *start;
        !           136:        int              emit;
        !           137:
        !           138:        /*
        !           139:         * Print as many column spaces til we achieve parity with the
        !           140:         * input document.
        !           141:         */
        !           142:
        !           143: again:
        !           144:        if (list && '\0' != *p) {
        !           145:                while (isspace((unsigned char)*p))
        !           146:                        p++;
        !           147:
        !           148:                while ('\'' == *p || '(' == *p || '"' == *p)
        !           149:                        p++;
        !           150:
        !           151:                emit = isalpha((unsigned char)p[0]) &&
        !           152:                        isalpha((unsigned char)p[1]);
        !           153:
        !           154:                for (start = p; '\0' != *p; p++)
        !           155:                        if ('\\' == *p) {
        !           156:                                p++;
        !           157:                                esc = mandoc_escape(&p, NULL, NULL);
        !           158:                                if (ESCAPE_ERROR == esc)
        !           159:                                        return;
        !           160:                                emit = 0;
        !           161:                        } else if (isspace((unsigned char)*p))
        !           162:                                break;
        !           163:
        !           164:                if (emit && p - start >= 2) {
        !           165:                        for ( ; start != p; start++)
        !           166:                                if (ASCII_HYPH == *start)
        !           167:                                        putchar('-');
        !           168:                                else
        !           169:                                        putchar((unsigned char)*start);
        !           170:                        putchar('\n');
        !           171:                }
        !           172:
        !           173:                if (isspace((unsigned char)*p))
        !           174:                        goto again;
        !           175:
        !           176:                return;
        !           177:        }
1.1       kristaps  178:
                    179:        while (*colp < col) {
                    180:                putchar(' ');
                    181:                (*colp)++;
                    182:        }
                    183:
1.5     ! kristaps  184:        /*
        !           185:         * Print the input word, skipping any special characters.
        !           186:         */
1.4       kristaps  187:        while ('\0' != *p)
1.1       kristaps  188:                if ('\\' == *p) {
                    189:                        p++;
                    190:                        esc = mandoc_escape(&p, NULL, NULL);
                    191:                        if (ESCAPE_ERROR == esc)
1.4       kristaps  192:                                break;
1.1       kristaps  193:                } else {
1.3       kristaps  194:                        putchar((unsigned char )*p++);
1.1       kristaps  195:                        (*colp)++;
                    196:                }
                    197: }
                    198:
                    199: static void
1.4       kristaps  200: pline(int line, int *linep, int *col, int list)
1.1       kristaps  201: {
1.5     ! kristaps  202:
        !           203:        if (list)
        !           204:                return;
        !           205:
        !           206:        /*
        !           207:         * Print out as many lines as needed to reach parity with the
        !           208:         * original input.
        !           209:         */
1.1       kristaps  210:
                    211:        while (*linep < line) {
                    212:                putchar('\n');
                    213:                (*linep)++;
                    214:        }
1.4       kristaps  215:
1.1       kristaps  216:        *col = 0;
                    217: }
                    218:
                    219: static void
1.4       kristaps  220: pmdoc(const struct mdoc_node *p, int *line, int *col, int list)
1.1       kristaps  221: {
                    222:
                    223:        for ( ; p; p = p->next) {
                    224:                if (MDOC_LINE & p->flags)
1.4       kristaps  225:                        pline(p->line, line, col, list);
1.1       kristaps  226:                if (MDOC_TEXT == p->type)
1.4       kristaps  227:                        pstring(p->string, p->pos, col, list);
1.1       kristaps  228:                if (p->child)
1.4       kristaps  229:                        pmdoc(p->child, line, col, list);
1.1       kristaps  230:        }
                    231: }
                    232:
                    233: static void
1.4       kristaps  234: pman(const struct man_node *p, int *line, int *col, int list)
1.1       kristaps  235: {
                    236:
                    237:        for ( ; p; p = p->next) {
                    238:                if (MAN_LINE & p->flags)
1.4       kristaps  239:                        pline(p->line, line, col, list);
1.1       kristaps  240:                if (MAN_TEXT == p->type)
1.4       kristaps  241:                        pstring(p->string, p->pos, col, list);
1.1       kristaps  242:                if (p->child)
1.4       kristaps  243:                        pman(p->child, line, col, list);
1.1       kristaps  244:        }
                    245: }

CVSweb