/**
 * @file main.c
 * @author Radek Krejci <rkrejci@cesnet.cz>
 * @author Adam Piecek <piecek@cesnet.cz>
 * @brief libyang's YANG Regular Expression tool
 *
 * Copyright (c) 2017 CESNET, z.s.p.o.
 *
 * This source code is licensed under BSD 3-Clause License (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/BSD-3-Clause
 */

#define _GNU_SOURCE /* asprintf, strdup */

#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include "libyang.h"

#include "compat.h"
#include "tools/config.h"

struct yr_pattern {
    char *expr;
    ly_bool invert;
};

void
help(void)
{
    fprintf(stdout, "YANG Regular Expressions processor.\n");
    fprintf(stdout, "Usage:\n");
    fprintf(stdout, "    yangre [-hv]\n");
    fprintf(stdout, "    yangre [-V] -p <regexp1> [-i] [-p <regexp2> [-i] ...] <string>\n");
    fprintf(stdout, "    yangre [-V] -f <file>\n");
    fprintf(stdout, "Returns 0 if string matches the pattern(s) or if otherwise successful.\n");
    fprintf(stdout, "Returns 1 on error.\n");
    fprintf(stdout, "Returns 2 if string does not match the pattern(s).\n\n");
    fprintf(stdout, "Options:\n"
            "  -h, --help              Show this help message and exit.\n"
            "  -v, --version           Show version number and exit.\n"
            "  -V, --verbose           Print the processing information.\n"
            "  -i, --invert-match      Invert-match modifier for the closest preceding\n"
            "                          pattern.\n"
            "  -p, --pattern=\"REGEXP\"  Regular expression including the quoting,\n"
            "                          which is applied the same way as in a YANG module.\n"
            "  -f, --file=\"FILE\"     List of patterns and the <string> (separated by an\n"
            "                          empty line) are taken from <file>. Invert-match is\n"
            "                          indicated by the single space character at the \n"
            "                          beginning of the pattern line. YANG quotation around\n"
            "                          patterns is still expected, but that avoids issues with\n"
            "                          reading quotation by shell. Avoid newline at the end\n"
            "                          of the string line to represent empty <string>.");
    fprintf(stdout, "Examples:\n"
            "  pattern \"[0-9a-fA-F]*\";      -> yangre -p '\"[0-9a-fA-F]*\"' '1F'\n"
            "  pattern '[a-zA-Z0-9\\-_.]*';  -> yangre -p \"'[a-zA-Z0-9\\-_.]*'\" 'a-b'\n"
            "  pattern [xX][mM][lL].*;      -> yangre -p '[xX][mM][lL].*' 'xml-encoding'\n\n");
    fprintf(stdout, "Note that to pass YANG quoting through your shell, you are supposed to use\n"
            "the other quotation around. For not-quoted patterns, use single quotes.\n\n");
}

void
version(void)
{
    fprintf(stdout, "yangre %s\n", PROJECT_VERSION);
}

void
pattern_error(LY_LOG_LEVEL level, const char *msg, const char *path)
{
    (void) path; /* unused */

    if (level == LY_LLERR) {
        fprintf(stderr, "yangre error: %s\n", msg);
    }
}

static int
add_pattern(struct yr_pattern **patterns, int *counter, char *pattern)
{
    void *reallocated;
    int orig_counter;

    /* Store the original number of items. */
    orig_counter = *counter;

    /* Reallocate 'patterns' memory with additional space. */
    reallocated = realloc(*patterns, (orig_counter + 1) * sizeof **patterns);
    if (!reallocated) {
        goto error;
    }
    (*patterns) = reallocated;
    /* Allocated memory is now larger. */
    (*counter)++;
    /* Copy the pattern and store it to the additonal space. */
    (*patterns)[orig_counter].expr = strdup(pattern);
    if (!(*patterns)[orig_counter].expr) {
        goto error;
    }
    (*patterns)[orig_counter].invert = 0;

    return 0;

error:
    fprintf(stderr, "yangre error: memory allocation error.\n");
    return 1;
}

/**
 * @brief Open the @p filepath, parse patterns and given string-argument.
 *
 * @param[in] filepath File to parse. Contains patterns and string.
 * @param[out] infile The file descriptor of @p filepath.
 * @param[out] patterns Storage of patterns.
 * @param[out] patterns_count Number of items in @p patterns.
 * @param[out] strarg The string-argument to check.
 * @return 0 on success.
 */
static int
parse_patterns_file(const char *filepath, FILE **infile, struct yr_pattern **patterns, int *patterns_count, char **strarg)
{
    int blankline = 0;
    char *str = NULL;
    size_t len = 0;
    ssize_t l;

    *infile = fopen(filepath, "rb");
    if (!(*infile)) {
        fprintf(stderr, "yangre error: unable to open input file %s (%s).\n", optarg, strerror(errno));
        goto error;
    }

    while ((l = getline(&str, &len, *infile)) != -1) {
        if (!blankline && (str[0] == '\n')) {
            /* blank line */
            blankline = 1;
            continue;
        }
        if ((str[0] != '\n') && (str[l - 1] == '\n')) {
            /* remove ending newline */
            str[l - 1] = '\0';
        }
        if (blankline) {
            /* done - str is now the string to check */
            blankline = 0;
            *strarg = str;
            break;
            /* else read the patterns */
        } else if (add_pattern(patterns, patterns_count, (str[0] == ' ') ? &str[1] : str)) {
            goto error;
        }
        if (str[0] == ' ') {
            /* set invert-match */
            (*patterns)[*patterns_count - 1].invert = 1;
        }
    }
    assert(str);
    if (blankline && (str[0] != '\0')) {
        /* corner case, no input after blankline meaning the pattern to check is empty */
        free(str);
        str = malloc(sizeof(char));
        if (!str) {
            fprintf(stderr, "yangre error: memory allocation failed.\n");
            goto error;
        }
        str[0] = '\0';
    }
    *strarg = str;

    return 0;

error:
    free(str);
    if (*infile) {
        fclose(*infile);
        *infile = NULL;
    }
    *strarg = NULL;

    return 1;
}

static char *
modstr_init(void)
{
    const char *module_start = "module yangre {"
            "yang-version 1.1;"
            "namespace urn:cesnet:libyang:yangre;"
            "prefix re;"
            "leaf pattern {"
            "  type string {";

    return strdup(module_start);
}

static char *
modstr_add_pattern(char **modstr, const struct yr_pattern *pattern)
{
    char *new;
    const char *module_invertmatch = " { modifier invert-match; }";
    const char *module_match = ";";

    if (asprintf(&new, "%s pattern %s%s", *modstr, pattern->expr,
            pattern->invert ? module_invertmatch : module_match) == -1) {
        fprintf(stderr, "yangre error: memory allocation failed.\n");
        return NULL;
    }
    free(*modstr);
    *modstr = NULL;

    return new;
}

static char *
modstr_add_ending(char **modstr)
{
    char *new;
    static const char *module_end = "}}}";

    if (asprintf(&new, "%s%s", *modstr, module_end) == -1) {
        fprintf(stderr, "yangre error: memory allocation failed.\n");
        return NULL;
    }
    free(*modstr);
    *modstr = NULL;

    return new;
}

static int
create_module(struct yr_pattern *patterns, int patterns_count, char **mod)
{
    int i;
    char *new = NULL, *modstr;

    if (!(modstr = modstr_init())) {
        goto error;
    }

    for (i = 0; i < patterns_count; i++) {
        if (!(new = modstr_add_pattern(&modstr, &patterns[i]))) {
            goto error;
        }
        modstr = new;
    }

    if (!(new = modstr_add_ending(&modstr))) {
        goto error;
    }

    *mod = new;

    return 0;

error:
    *mod = NULL;
    free(new);
    free(modstr);

    return 1;
}

static void
print_verbose(struct ly_ctx *ctx, struct yr_pattern *patterns, int patterns_count, char *str, LY_ERR match)
{
    int i;

    for (i = 0; i < patterns_count; i++) {
        fprintf(stdout, "pattern  %d: %s\n", i + 1, patterns[i].expr);
        fprintf(stdout, "matching %d: %s\n", i + 1, patterns[i].invert ? "inverted" : "regular");
    }
    fprintf(stdout, "string    : %s\n", str);
    if (match == LY_SUCCESS) {
        fprintf(stdout, "result    : matching\n");
    } else if (match == LY_EVALID) {
        fprintf(stdout, "result    : not matching\n");
    } else {
        fprintf(stdout, "result    : error (%s)\n", ly_errmsg(ctx));
    }
}

int
main(int argc, char *argv[])
{
    LY_ERR match;
    int i, opt_index = 0, ret = 1, verbose = 0;
    struct option options[] = {
        {"help",             no_argument,       NULL, 'h'},
        {"file",             required_argument, NULL, 'f'},
        {"invert-match",     no_argument,       NULL, 'i'},
        {"pattern",          required_argument, NULL, 'p'},
        {"version",          no_argument,       NULL, 'v'},
        {"verbose",          no_argument,       NULL, 'V'},
        {NULL,               0,                 NULL, 0}
    };
    struct yr_pattern *patterns = NULL;
    char *str = NULL, *modstr = NULL;
    int patterns_count = 0;
    struct ly_ctx *ctx = NULL;
    struct lys_module *mod;
    FILE *infile = NULL;
    ly_bool info_printed = 0;

    opterr = 0;
    while ((i = getopt_long(argc, argv, "hf:ivVp:", options, &opt_index)) != -1) {
        switch (i) {
        case 'h':
            help();
            info_printed = 1;
            break;
        case 'f':
            if (infile) {
                help();
                fprintf(stderr, "yangre error: multiple input files are not supported.\n");
                goto cleanup;
            } else if (patterns_count) {
                help();
                fprintf(stderr, "yangre error: command line patterns cannot be mixed with file input.\n");
                goto cleanup;
            }
            if (parse_patterns_file(optarg, &infile, &patterns, &patterns_count, &str)) {
                goto cleanup;
            }
            break;
        case 'i':
            if (!patterns_count || patterns[patterns_count - 1].invert) {
                help();
                fprintf(stderr, "yangre error: invert-match option must follow some pattern.\n");
                goto cleanup;
            }
            patterns[patterns_count - 1].invert = 1;
            break;
        case 'p':
            if (infile) {
                help();
                fprintf(stderr, "yangre error: command line patterns cannot be mixed with file input.\n");
                goto cleanup;
            }
            if (add_pattern(&patterns, &patterns_count, optarg)) {
                goto cleanup;
            }
            break;
        case 'v':
            version();
            info_printed = 1;
            break;
        case 'V':
            verbose = 1;
            break;
        default:
            help();
            if (optopt) {
                fprintf(stderr, "yangre error: invalid option: -%c\n", optopt);
            } else {
                fprintf(stderr, "yangre error: invalid option: %s\n", argv[optind - 1]);
            }
            goto cleanup;
        }
    }

    if (info_printed) {
        ret = 0;
        goto cleanup;
    }

    if (!str) {
        /* check options compatibility */
        if (optind >= argc) {
            help();
            fprintf(stderr, "yangre error: missing <string> parameter to process.\n");
            goto cleanup;
        } else if (!patterns_count) {
            help();
            fprintf(stderr, "yangre error: missing pattern parameter to use.\n");
            goto cleanup;
        }
        str = argv[optind];
    }

    if (create_module(patterns, patterns_count, &modstr)) {
        goto cleanup;
    }

    if (ly_ctx_new(NULL, 0, &ctx)) {
        goto cleanup;
    }

    ly_set_log_clb(pattern_error, 0);
    if (lys_parse_mem(ctx, modstr, LYS_IN_YANG, &mod) || !mod->compiled || !mod->compiled->data) {
        goto cleanup;
    }

    /* check the value */
    match = lyd_value_validate(ctx, mod->compiled->data, str, strlen(str), NULL, NULL, NULL);

    if (verbose) {
        print_verbose(ctx, patterns, patterns_count, str, match);
    }
    if (match == LY_SUCCESS) {
        ret = 0;
    } else if (match == LY_EVALID) {
        ret = 2;
    } else {
        ret = 1;
    }

cleanup:
    ly_ctx_destroy(ctx);
    for (i = 0; i < patterns_count; i++) {
        free(patterns[i].expr);
    }
    if (patterns_count) {
        free(patterns);
    }
    free(modstr);
    if (infile) {
        fclose(infile);
        free(str);
    }

    return ret;
}
