/*-
 * Copyright (C)2007 @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 AUTHORS ``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 REGENTS 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.
 */

#ifndef lint
static const char copyright[] = "\
@(#)Copyright (C)2007 @BABOLO http://www.babolo.ru/\n\
@(#)All rights reserved.\n";
static const char rcsid[] = "$Id: texot,v 1.3 2007/07/20 20:23:43 babolo Exp $";
#endif /* not lint */

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

static babolo_lexor qv0 =
{"\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) {
    u_int32_t flag;
    mife_descriptor *in;
    ssize_t len, a = 1;
    const u_char *p, *q;
    char ch;
    int i;
    babolo_lexor *v, *u[] = {&qv0, &qv1, &qv2, &qv3, &qv4};

    flag = 0;
    while ((ch = getopt(argc, argv, "a:s:v")) != EOF)
        switch (ch) {
        case 'a': a = strtol(optarg, NULL, 0);
                  break;
        case 's': flag |= strtol(optarg, NULL, 0);
                  break;
        case 'v': BLIN_VERBOSE(flag);
                  break;
        default:  ;
        }
    argc -= optind;
    argv += optind;
    v = u[a];
    if  (argc > 0) errx(EX_USAGE, "Excessive argument");
    in = mife_opef(0, fileno(stdin));
    if  (!in) errx(EX_IOERR, "mife_opef");
    len = mife_read(in, 0, 0);
    if  (len < 0) errx(EX_IOERR, "mife_read");
    p = mife_get(in, 0);
    if  (!p) errx(EX_IOERR, "mife_get");
    v->flag &= ~(Bpars_CEND | BLIN_VERMASK);
    v->flag |= flag & (Bpars_CEND | Bpars_CMID | BLIN_VERMASK);
    for (i = 0; (u_int32_t)p - (u_int32_t)mife_get(in, 0) < len; i++) {
        q = p;
        printf("%5d %5u %9u\n", i, (u_int32_t)p - (u_int32_t)q, babolo_goword(v, &p));
        if  (q == p) printf("%5d %5u\n", i, (u_int32_t)(++p) - (u_int32_t)q);
    }
    exit(EX_OK);
}
