<commit_before>/*
 * Copyright (c) 2012,2014 Red Hat.
 * Copyright (c) 2008 Aconex.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program 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 General Public License
 * for more details.
 */

#include "pmapi.h"
#include "impl.h"

static int apiflag;
static const char *empty = "";
static const char *none = "false";

/* detect shell special characters that will need backslash-prefixing */
static int
isspecial(int c)
{
    if (c == '\n' || c == '\r' || c == '\\' ||
	c == '"' || c == '`' || c == '$' || c == '!')
	return 1;
    return 0;
}

static void
direct_report(const char *var, const char *val)
{
    if (!val || val[0] == '\0')
	val = empty;
    printf("%s=%s\n", var, val);
}

static void
export_report(const char *var, const char *val)
{
    char buffer[4096];
    const char *p;
    int i = 0;

    if (!val || val[0] == '\0')
        val = empty;
    else {
	/* ensure we do not leak any problematic characters into export */
	for (p = val; p != '\0' && i < sizeof(buffer)-1; p++) {
	    if (isspecial((int)*p))
		buffer[i++] = '\\';
	    buffer[i++] = *p;
	}
	buffer[i] = '\0';
	val = buffer;
    }
    if (apiflag)	/* API mode: no override allowed */
	printf("export %s=\"%s\"\n", var, val);
    else
	printf("export %s=${%s:-\"%s\"}\n", var, var, val);
}

static void
pcp_conf_extract(char *var, char *prefix, char *val)
{
    __pmNativeConfig(var, prefix, val);
    val = getenv(var);
    direct_report(var, val);
}

static void
pcp_conf_shell_extract(char *var, char *prefix, char *val)<commit_msg>Use simpler shell-quoting mechanism in pmconfig(1) from getopt(1)<commit_after>/*
 * Copyright (c) 2012,2014 Red Hat.
 * Copyright (c) 2008 Aconex.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program 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 General Public License
 * for more details.
 */

#include "pmapi.h"
#include "impl.h"

static int apiflag;
static const char *empty = "";
static const char *none = "false";

static void
direct_report(const char *var, const char *val)
{
    if (!val || val[0] == '\0')
	val = empty;
    printf("%s=%s\n", var, val);
}

static void
export_report(const char *var, const char *val)
{
    char buffer[4096];
    const char *p;
    int i = 0;

    if (!val || val[0] == '\0')
        val = empty;
    else {
	/* ensure we do not leak any problematic characters into export */
	for (p = val; *p != '\0' && i < sizeof(buffer)-6; p++) {
	    if ((int)*p == '\'') {
		buffer[i++] = '\'';
		buffer[i++] = '\\';
		buffer[i++] = '\'';
		buffer[i++] = '\'';
	    } else {
		buffer[i++] = *p;
	    }
	}
	buffer[i] = '\0';
	val = buffer;
    }
    if (apiflag)	/* API mode: no override allowed */
	printf("export %s='%s'\n", var, val);
    else
	printf("export %s=${%s:-'%s'}\n", var, var, val);
}

static void
pcp_conf_extract(char *var, char *prefix, char *val)
{
    __pmNativeConfig(var, prefix, val);
    val = getenv(var);
    direct_report(var, val);
}

static void
pcp_conf_shell_extract(char *var, char *prefix, char *val)
{'fraction_non_alphanumeric': 0.1581726960654872, 'fraction_numerical': 0.014259308159493002, 'mean_word_length': 3.237136465324385, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4405002', 'n_tokens_mistral': 1320, 'n_tokens_neox': 1263, 'n_words': 466}