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

Annotation of mandoc/demandoc.c, Revision 1.26

1.26    ! schwarze    1: /*     $Id: demandoc.c,v 1.25 2016/01/08 02:13:39 schwarze 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: #include "config.h"
1.11      schwarze   18:
                     19: #include <sys/types.h>
1.1       kristaps   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:
1.16      schwarze   29: #include "roff.h"
1.1       kristaps   30: #include "man.h"
                     31: #include "mdoc.h"
                     32: #include "mandoc.h"
                     33:
1.4       kristaps   34: static void     pline(int, int *, int *, int);
1.17      schwarze   35: static void     pman(const struct roff_node *, int *, int *, int);
1.4       kristaps   36: static void     pmandoc(struct mparse *, int, const char *, int);
1.17      schwarze   37: static void     pmdoc(const struct roff_node *, int *, int *, int);
1.4       kristaps   38: static void     pstring(const char *, int, int *, int);
1.1       kristaps   39: static void     usage(void);
                     40:
                     41: static const char       *progname;
                     42:
                     43: int
                     44: main(int argc, char *argv[])
                     45: {
                     46:        struct mparse   *mp;
1.13      schwarze   47:        int              ch, fd, i, list;
1.1       kristaps   48:        extern int       optind;
                     49:
1.14      schwarze   50:        if (argc < 1)
                     51:                progname = "demandoc";
                     52:        else if ((progname = strrchr(argv[0], '/')) == NULL)
1.1       kristaps   53:                progname = argv[0];
                     54:        else
                     55:                ++progname;
                     56:
                     57:        mp = NULL;
1.4       kristaps   58:        list = 0;
1.1       kristaps   59:
1.4       kristaps   60:        while (-1 != (ch = getopt(argc, argv, "ikm:pw")))
1.1       kristaps   61:                switch (ch) {
1.4       kristaps   62:                case ('i'):
                     63:                        /* FALLTHROUGH */
                     64:                case ('k'):
                     65:                        /* FALLTHROUGH */
                     66:                case ('m'):
                     67:                        /* FALLTHROUGH */
                     68:                case ('p'):
                     69:                        break;
                     70:                case ('w'):
                     71:                        list = 1;
                     72:                        break;
1.1       kristaps   73:                default:
                     74:                        usage();
1.21      schwarze   75:                        return (int)MANDOCLEVEL_BADARG;
1.1       kristaps   76:                }
                     77:
                     78:        argc -= optind;
                     79:        argv += optind;
                     80:
1.22      schwarze   81:        mchars_alloc();
                     82:        mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_BADARG, NULL, NULL);
1.1       kristaps   83:        assert(mp);
                     84:
1.14      schwarze   85:        if (argc < 1)
1.4       kristaps   86:                pmandoc(mp, STDIN_FILENO, "<stdin>", list);
1.1       kristaps   87:
                     88:        for (i = 0; i < argc; i++) {
                     89:                mparse_reset(mp);
1.26    ! schwarze   90:                if ((fd = mparse_open(mp, argv[i])) == -1) {
1.13      schwarze   91:                        perror(argv[i]);
                     92:                        continue;
                     93:                }
                     94:                pmandoc(mp, fd, argv[i], list);
1.1       kristaps   95:        }
                     96:
                     97:        mparse_free(mp);
1.22      schwarze   98:        mchars_free();
1.21      schwarze   99:        return (int)MANDOCLEVEL_OK;
1.1       kristaps  100: }
                    101:
                    102: static void
                    103: usage(void)
                    104: {
                    105:
1.4       kristaps  106:        fprintf(stderr, "usage: %s [-w] [files...]\n", progname);
1.1       kristaps  107: }
                    108:
                    109: static void
1.4       kristaps  110: pmandoc(struct mparse *mp, int fd, const char *fn, int list)
1.1       kristaps  111: {
1.18      schwarze  112:        struct roff_man *man;
1.1       kristaps  113:        int              line, col;
                    114:
1.13      schwarze  115:        mparse_readfd(mp, fd, fn);
1.25      schwarze  116:        close(fd);
1.19      schwarze  117:        mparse_result(mp, &man, NULL);
1.1       kristaps  118:        line = 1;
                    119:        col = 0;
                    120:
1.19      schwarze  121:        if (man == NULL)
                    122:                return;
1.23      schwarze  123:        if (man->macroset == MACROSET_MDOC) {
                    124:                mdoc_validate(man);
1.20      schwarze  125:                pmdoc(man->first->child, &line, &col, list);
1.24      schwarze  126:        } else {
                    127:                man_validate(man);
1.20      schwarze  128:                pman(man->first->child, &line, &col, list);
1.24      schwarze  129:        }
1.1       kristaps  130:
1.5       kristaps  131:        if ( ! list)
                    132:                putchar('\n');
1.1       kristaps  133: }
                    134:
                    135: /*
                    136:  * Strip the escapes out of a string, emitting the results.
                    137:  */
                    138: static void
1.4       kristaps  139: pstring(const char *p, int col, int *colp, int list)
1.1       kristaps  140: {
                    141:        enum mandoc_esc  esc;
1.6       kristaps  142:        const char      *start, *end;
1.5       kristaps  143:        int              emit;
                    144:
                    145:        /*
                    146:         * Print as many column spaces til we achieve parity with the
                    147:         * input document.
                    148:         */
                    149:
                    150: again:
                    151:        if (list && '\0' != *p) {
                    152:                while (isspace((unsigned char)*p))
                    153:                        p++;
                    154:
                    155:                while ('\'' == *p || '(' == *p || '"' == *p)
                    156:                        p++;
                    157:
                    158:                emit = isalpha((unsigned char)p[0]) &&
                    159:                        isalpha((unsigned char)p[1]);
                    160:
                    161:                for (start = p; '\0' != *p; p++)
                    162:                        if ('\\' == *p) {
                    163:                                p++;
                    164:                                esc = mandoc_escape(&p, NULL, NULL);
                    165:                                if (ESCAPE_ERROR == esc)
                    166:                                        return;
                    167:                                emit = 0;
                    168:                        } else if (isspace((unsigned char)*p))
                    169:                                break;
                    170:
1.6       kristaps  171:                end = p - 1;
                    172:
                    173:                while (end > start)
1.15      schwarze  174:                        if ('.' == *end || ',' == *end ||
1.6       kristaps  175:                                        '\'' == *end || '"' == *end ||
                    176:                                        ')' == *end || '!' == *end ||
                    177:                                        '?' == *end || ':' == *end ||
                    178:                                        ';' == *end)
                    179:                                end--;
                    180:                        else
                    181:                                break;
                    182:
                    183:                if (emit && end - start >= 1) {
                    184:                        for ( ; start <= end; start++)
1.5       kristaps  185:                                if (ASCII_HYPH == *start)
                    186:                                        putchar('-');
                    187:                                else
                    188:                                        putchar((unsigned char)*start);
                    189:                        putchar('\n');
                    190:                }
                    191:
                    192:                if (isspace((unsigned char)*p))
                    193:                        goto again;
                    194:
                    195:                return;
                    196:        }
1.1       kristaps  197:
                    198:        while (*colp < col) {
                    199:                putchar(' ');
                    200:                (*colp)++;
                    201:        }
                    202:
1.5       kristaps  203:        /*
                    204:         * Print the input word, skipping any special characters.
                    205:         */
1.15      schwarze  206:        while ('\0' != *p)
1.1       kristaps  207:                if ('\\' == *p) {
                    208:                        p++;
                    209:                        esc = mandoc_escape(&p, NULL, NULL);
                    210:                        if (ESCAPE_ERROR == esc)
1.4       kristaps  211:                                break;
1.1       kristaps  212:                } else {
1.3       kristaps  213:                        putchar((unsigned char )*p++);
1.1       kristaps  214:                        (*colp)++;
                    215:                }
                    216: }
                    217:
                    218: static void
1.4       kristaps  219: pline(int line, int *linep, int *col, int list)
1.1       kristaps  220: {
1.5       kristaps  221:
                    222:        if (list)
                    223:                return;
                    224:
                    225:        /*
                    226:         * Print out as many lines as needed to reach parity with the
1.15      schwarze  227:         * original input.
1.5       kristaps  228:         */
1.1       kristaps  229:
                    230:        while (*linep < line) {
                    231:                putchar('\n');
                    232:                (*linep)++;
                    233:        }
1.4       kristaps  234:
1.1       kristaps  235:        *col = 0;
                    236: }
                    237:
                    238: static void
1.17      schwarze  239: pmdoc(const struct roff_node *p, int *line, int *col, int list)
1.1       kristaps  240: {
                    241:
                    242:        for ( ; p; p = p->next) {
                    243:                if (MDOC_LINE & p->flags)
1.4       kristaps  244:                        pline(p->line, line, col, list);
1.16      schwarze  245:                if (ROFFT_TEXT == p->type)
1.4       kristaps  246:                        pstring(p->string, p->pos, col, list);
1.15      schwarze  247:                if (p->child)
1.4       kristaps  248:                        pmdoc(p->child, line, col, list);
1.1       kristaps  249:        }
                    250: }
                    251:
                    252: static void
1.17      schwarze  253: pman(const struct roff_node *p, int *line, int *col, int list)
1.1       kristaps  254: {
                    255:
                    256:        for ( ; p; p = p->next) {
                    257:                if (MAN_LINE & p->flags)
1.4       kristaps  258:                        pline(p->line, line, col, list);
1.16      schwarze  259:                if (ROFFT_TEXT == p->type)
1.4       kristaps  260:                        pstring(p->string, p->pos, col, list);
1.15      schwarze  261:                if (p->child)
1.4       kristaps  262:                        pman(p->child, line, col, list);
1.1       kristaps  263:        }
                    264: }

CVSweb