<commit_before>/*
 * augtool.c:
 *
 * Copyright (C) 2007, 2008 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: <NAME> <<EMAIL>>
 */

#include <config.h>
#include "augeas.h"
#include "internal.h"

#include <readline/readline.h>
#include <readline/history.h>
#include <argz.h>

struct command {
    const char *name;
    int minargs;
    int maxargs;
    void(*handler) (char *args[]);
    const char *synopsis;
    const char *help;
};

static const struct command const commands[];

static augeas *aug = NULL;
static const char *const progname = "augtool";
static unsigned int flags = AUG_NONE;
const char *root = NULL;
char *loadpath = NULL;

static char *cleanpath(char *path) {
    if (strlen(path) == 0)
        return path;
    char *e = path + strlen(path) - 1;
    while (e != path && (*e == SEP || isspace(*e)))
        *e-- = '\0';
    return path;
}

/*
 * Dup PATH and split it into a directory and basename. The returned value
 * points to the copy of PATH. Adding strlen(PATH)+1 to it gives the
 * basename.
 *
 * If PATH can not be split, returns NULL
 */
ATTRIBUTE_UNUSED
static char *pathsplit(const char *path) {
    char *ppath = strdup(path);
    char *pend = strrchr(ppath, SEP);

    if (pend == NULL || pend == ppath) {
        free(ppath);
        return NULL;
    }
    *pend = '\0';
    return ppath;
}

static char *ls_pattern(const char *path) {
    char *q;
    int r;

    if (path[strlen(path)-1] == SEP)
        r = asprintf(&q, "%s*", path);
    else
        r = asprintf(&q, "%s/*", path);
    if (r == -1)
        return NULL;
    return q;
}

static int child_count(const char *path) {
    char *q = ls_pattern(path);
    int cnt;

    if (q == NULL)
        return 0;
    cnt = aug_match(aug, q, NULL);
    free(q);
    return cnt;
}

static void cmd_ls(char *args[]) {
    int cnt;
    char *path = cleanpath(args[0]);
    char **paths;

    path = ls_pattern(path);
    if (path == NULL)
        return;
    cnt = aug_match(aug, path, &paths);
    for (int i=0; i < cnt; i++) {
        const char *val;
        const char *basnam = strrchr(paths[i], SEP);
        int dir = child_count(paths[i]);
        aug_get(aug, paths[i], &val);
        basnam = (basnam == NULL) ? paths[i] : basnam + 1;
        if (val == NULL)
            val = "(none)";
        printf("%s%s= %s\n", basnam, dir ? "/ " : " ", val);
        free(paths[i]);
    }
    if (cnt > 0)
        free(paths);
    free(path);
}

static void cmd_match(char *args[]) {
    int cnt;
    const char *pattern = cleanpath(args[0]);
    char **matches;
    int filter = (args[1] != NULL) && (strlen(args[1]) > 0);

    cnt = aug_match(aug, pattern, &matches);
    if (cnt == -1) {
        printf("  (error matching %s)\n", pattern);
        return;
    }
    if (cnt == 0) {
        printf("  (no matches)\n");
        return;
    }

    for (int i=0; i < cnt; i++) {
        const char *val;
        aug_get(aug, matches[i], &val);
        if (val == NULL)
            val = "(none)";
        if (filter) {
            if (STREQ(args[1], val))
                printf("%s\n", matches[i]);
        } else {
            printf("%s = %s\n", matches[i], val);
        }
        free((void *) matches[i]);
    }
    free(matches);
}

static void cmd_rm(char *args[]) {
    int cnt;
    const char *path = cleanpath(args[0]);
    printf("rm : %s", path);
    cnt = aug_rm(aug, path);
    printf(" %d\n", cnt);
}

static void cmd_set(char *args[]) {
    const char *path = cleanpath(args[0]);
    const char *val = args[1];
    int r;

    r = aug_set(aug, path, val);
    if (r == -1)
        printf ("Failed\n");
}

static void cmd_clear(char *args[]) {
    const char *path = cleanpath(args[0]);
    int r;

    r = aug_set(aug, path, NULL);
    if (r == -1)
        printf ("Failed\n");
}

static void cmd_get(char *args[]) {
    const char *path = cleanpath(args[0]);
    const char *val;

    printf("%s", path);
    if (aug_get(aug, path, &val) != 1) {
        printf(" (o)\n");
        return;
    } else if (val == NULL) {
        val = "(none)";
    } else {
        printf(" = %s\n", val);
    }
}

static void cmd_print(char *args[]) {
    aug_print(aug, stdout, cleanpath(args[0]));
}

static void cmd_save(ATTRIBUTE_UNUSED char *args[]) {
    int r;
    r = aug_save(aug);
    if (r == -1) {
        printf("Saving failed\n");
    }
}<commit_msg>augtool: Print '(none)' when getting a path with no value<commit_after>/*
 * augtool.c:
 *
 * Copyright (C) 2007, 2008 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: <NAME> <<EMAIL>>
 */

#include <config.h>
#include "augeas.h"
#include "internal.h"

#include <readline/readline.h>
#include <readline/history.h>
#include <argz.h>

struct command {
    const char *name;
    int minargs;
    int maxargs;
    void(*handler) (char *args[]);
    const char *synopsis;
    const char *help;
};

static const struct command const commands[];

static augeas *aug = NULL;
static const char *const progname = "augtool";
static unsigned int flags = AUG_NONE;
const char *root = NULL;
char *loadpath = NULL;

static char *cleanpath(char *path) {
    if (strlen(path) == 0)
        return path;
    char *e = path + strlen(path) - 1;
    while (e != path && (*e == SEP || isspace(*e)))
        *e-- = '\0';
    return path;
}

/*
 * Dup PATH and split it into a directory and basename. The returned value
 * points to the copy of PATH. Adding strlen(PATH)+1 to it gives the
 * basename.
 *
 * If PATH can not be split, returns NULL
 */
ATTRIBUTE_UNUSED
static char *pathsplit(const char *path) {
    char *ppath = strdup(path);
    char *pend = strrchr(ppath, SEP);

    if (pend == NULL || pend == ppath) {
        free(ppath);
        return NULL;
    }
    *pend = '\0';
    return ppath;
}

static char *ls_pattern(const char *path) {
    char *q;
    int r;

    if (path[strlen(path)-1] == SEP)
        r = asprintf(&q, "%s*", path);
    else
        r = asprintf(&q, "%s/*", path);
    if (r == -1)
        return NULL;
    return q;
}

static int child_count(const char *path) {
    char *q = ls_pattern(path);
    int cnt;

    if (q == NULL)
        return 0;
    cnt = aug_match(aug, q, NULL);
    free(q);
    return cnt;
}

static void cmd_ls(char *args[]) {
    int cnt;
    char *path = cleanpath(args[0]);
    char **paths;

    path = ls_pattern(path);
    if (path == NULL)
        return;
    cnt = aug_match(aug, path, &paths);
    for (int i=0; i < cnt; i++) {
        const char *val;
        const char *basnam = strrchr(paths[i], SEP);
        int dir = child_count(paths[i]);
        aug_get(aug, paths[i], &val);
        basnam = (basnam == NULL) ? paths[i] : basnam + 1;
        if (val == NULL)
            val = "(none)";
        printf("%s%s= %s\n", basnam, dir ? "/ " : " ", val);
        free(paths[i]);
    }
    if (cnt > 0)
        free(paths);
    free(path);
}

static void cmd_match(char *args[]) {
    int cnt;
    const char *pattern = cleanpath(args[0]);
    char **matches;
    int filter = (args[1] != NULL) && (strlen(args[1]) > 0);

    cnt = aug_match(aug, pattern, &matches);
    if (cnt == -1) {
        printf("  (error matching %s)\n", pattern);
        return;
    }
    if (cnt == 0) {
        printf("  (no matches)\n");
        return;
    }

    for (int i=0; i < cnt; i++) {
        const char *val;
        aug_get(aug, matches[i], &val);
        if (val == NULL)
            val = "(none)";
        if (filter) {
            if (STREQ(args[1], val))
                printf("%s\n", matches[i]);
        } else {
            printf("%s = %s\n", matches[i], val);
        }
        free((void *) matches[i]);
    }
    free(matches);
}

static void cmd_rm(char *args[]) {
    int cnt;
    const char *path = cleanpath(args[0]);
    printf("rm : %s", path);
    cnt = aug_rm(aug, path);
    printf(" %d\n", cnt);
}

static void cmd_set(char *args[]) {
    const char *path = cleanpath(args[0]);
    const char *val = args[1];
    int r;

    r = aug_set(aug, path, val);
    if (r == -1)
        printf ("Failed\n");
}

static void cmd_clear(char *args[]) {
    const char *path = cleanpath(args[0]);
    int r;

    r = aug_set(aug, path, NULL);
    if (r == -1)
        printf ("Failed\n");
}

static void cmd_get(char *args[]) {
    const char *path = cleanpath(args[0]);
    const char *val;

    printf("%s", path);
    if (aug_get(aug, path, &val) != 1) {
        printf(" (o)\n");
    } else if (val == NULL) {
        printf(" (none)\n");
    } else {
        printf(" = %s\n", val);
    }
}

static void cmd_print(char *args[]) {
    aug_print(aug, stdout, cleanpath(args[0]));
}

static void cmd_save(ATTRIBUTE_UNUSED char *args[]) {
    int r;
    r = aug_save(aug);
    if (r == -1) {
        printf("Saving failed\n");
    }
}
{'fraction_non_alphanumeric': 0.15501131110455396, 'fraction_numerical': 0.010622602537621718, 'mean_word_length': 2.440947546531303, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '861025', 'n_tokens_mistral': 3520, 'n_tokens_neox': 3456, 'n_words': 1138}