/*-
 * Copyright (C)2007..2024 @BABOLO http://www.babolo.ru/
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ident "@(#) Copyright (C)2007..2024 @BABOLO http://www.babolo.ru/\n"
#ident "@(#) All rights reserved.\n"
#ident "@(#) $Id: mdxot,v 1.4 2024/04/14 02:19:23 babolo Exp $\n"

#define BLIN_COMPAT  4
#define MIFE_COMPAT  5
#define Bpars_COMPAT @@MAJOR@@

#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
#include <sysexits.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <err.h>
#include <babolo/BLINflag.h>
#include <mife.h>
#include "parser.h"
#include "@@INCLUDELEXOR@@/t1.h"
#include "@@INCLUDELEXOR@@/t2.h"
#include "@@INCLUDELEXOR@@/t3.h"
#include "@@INCLUDELEXOR@@/t4.h"

static babolo_lexor qv0 =
{(const u_char *)
 "\000\001\002\003""\004\005\006\007""\010\011\012\013""\014\015\016\017"
 "\020\021\022\023""\024\025\026\027""\030\031\032\033""\034\035\036\037"
 " !\"#"           "$%&\'"           "()*+"            ",-./"
 "0123"            "4567"            "89:;"            "<=>?"
 "@abc"            "defg"            "hijk"            "lmno"
 "pqrs"            "tuvw"            "xyz["            "\\]^_"
 "`abc"            "defg"            "hijk"            "lmno"
 "pqrs"            "tuvw"            "xyz{"            "|}~\177"
 "\200\201\202\203""\204\205\206\207""\210\211\212\213""\214\215\216\217"
 "\220\221\222\223""\224\225\226\227""\230\231\232\233""\234\235\236\237"
 "\240\241\242"   "\244\245\246\247""\250\251\252\253""\254\255\256\257"
 "\260\261\262"   "\264\265\266\267""\270\271\272\273""\274\275\276\277"
 ""            ""            ""            ""
 ""            ""            ""            ""
 ""            ""            ""            ""
 ""            ""            ""            ""
, NULL, Bpars_AABS, 15, 15, 1
,{/*00*/ 1, '\0',~0,  'e', 6,                   3
 ,/*03*/ 1, '\0',~0,  'e', 0,                   3
 ,/*06*/ 2, '\0',~0,  'e',10,  'r',~1,          3
 ,/*10*/ 3, '\0',~0,  'b',~2,  'c',~3,  'p',~4, 3
}};

int
main(int argc, char **argv) {
    BLIN_flag        flags;
    mife_descriptor *in;
    char             ch;
    babolo_lexor    *u[] = {&qv0, &qv1, &qv2, &qv3, &qv4};
    ssize_t          a   = 1;
    off_t            p;
    off_t            q;
    babolo_lexor    *v;
    int              i;

    flags = 0;
    while ((ch = getopt(argc, argv, "a:s:v")) != EOF)
        switch (ch) {
        case 'a': a = strtol(optarg, NULL, 0);
                  break;
        case 's': flags |= strtol(optarg, NULL, 0);
                  break;
        case 'v': BLIN_VERBOSE(flags);
                  break;
        default:  ;
        }
    argc -= optind;
    argv += optind;
    v = u[a];
    if  (argc > 0) errx(EX_USAGE, "Excessive argument");
    in = mife_init(MIFE_FULL);
    if  (0 > mife_ctlfdsc(in, fileno(stdin))) errx(EX_IOERR, "mife_ctlfdsc stdin");
    if  (!in) errx(EX_IOERR, "mife_ctlfdsc");
    v->flags &= ~(Bpars_FIRM | Bpars_CEND | Bpars_CMID | BLIN_MASK);
    v->flags |= flags & (Bpars_FIRM | Bpars_CEND | Bpars_CMID | BLIN_MASK);
    for (i = 0; ; ++i) {
        u_int32_t gw;

        q = mife_offset(in);
        gw = babolo_mdword(v, in);
        p = mife_offset(in);
        printf("%5d %5u %9u\n", i, (u_int)(p - q), gw);
        if  (q == p) {
            if  (!mife_get(in, ++p)) {
                ifBLIN_QW0("mife_get");
                errx(EX_IOERR, "mife_get");
            }
            printf("%5d %5u\n", i, (u_int)(p - q));
        }
        if  (0 >= mife_ctlsize(in)) break;
    }
    exit(EX_OK);
}
